Tool calculates required size for text in text, button, checkbox, and option gadgets

Share your advanced PureBasic knowledge/code with the community.
Axeman
User
User
Posts: 89
Joined: Mon Nov 03, 2003 5:34 am

Tool calculates required size for text in text, button, checkbox, and option gadgets

Post by Axeman »

This tool will tell you how much room that you need to allow for the text in a TextGadget, ButtonGadget, or CheckBoxGadget (also works for the OptionGadget).

EDIT: Added result output for button gadgets, made the 'Paste From Clipboard' button also do the calculation, and added a 'Make Window Sticky' checkbox to optionally make the app's window stay on top of other windows.

Code: Select all


; Windows.
#MAIN_WINDOW = 0

; Gadgets.
Enumeration
#INPUT_TEXT_LABEL
#INPUT_TEXT_TEXTBOX
#PASTE_INPUT_AND_CALCULATE_BUTTON
#CALCULATE_BUTTON

#TEXTGADGET_WIDTH_OUTPUT_LABEL
#TEXTGADGET_WIDTH_OUTPUT_TEXTBOX
#TEXTGADGET_COPY_OUTPUT_BUTTON

#CHECKBOX_WIDTH_OUTPUT_LABEL
#CHECKBOX_WIDTH_OUTPUT_TEXTBOX
#CHECKBOX_COPY_OUTPUT_BUTTON

#BUTTON_WIDTH_OUTPUT_LABEL
#BUTTON_WIDTH_OUTPUT_TEXTBOX
#BUTTON_COPY_OUTPUT_BUTTON

#MAKE_WINDOW_STICKY_CHECKBOX

#INSTRUCTIONS_LABEL
#INSTRUCTIONS_EDITBOX

#TEST_TEXTGADGET
#TEST_BUTTON
#TEST_CHECKBOX
EndEnumeration


Procedure CreateMainWindow()
flags = #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered | #PB_Window_TitleBar | #PB_Window_SystemMenu
If OpenWindow( #MAIN_WINDOW, 0, 0, 320, 486, "Calculate PB UI Text Width", flags ) = 0 : MessageRequester( "Fatal Error", "Unable to create main window." ) : End : EndIf
x = 10 : y = 10
TextGadget( #INPUT_TEXT_LABEL, x, y, 300, 18, "Enter Input Text Here" ) : y + 18
StringGadget( #INPUT_TEXT_TEXTBOX, x, y, 300, 25, "" ) : y + 25
ButtonGadget( #PASTE_INPUT_AND_CALCULATE_BUTTON, x, y, 203, 25, "Paste From Clipboard and Calculate" )
ButtonGadget( #CALCULATE_BUTTON, x + 203, y, 97, 25, "Calculate" )
y + 35

TextGadget( #TEXTGADGET_WIDTH_OUTPUT_LABEL, x, y, 110, 25, "TextGadget Width:" ) : x + 110
StringGadget( #TEXTGADGET_WIDTH_OUTPUT_TEXTBOX, x, y, 70, 25, "", #PB_String_ReadOnly ) : x + 70
ButtonGadget( #TEXTGADGET_COPY_OUTPUT_BUTTON, x, y, 120, 25, "Copy To Clipboard" )
x = 10 : y + 35

TextGadget( #BUTTON_WIDTH_OUTPUT_LABEL, x, y, 110, 25, "Button Width:" ) : x + 110
StringGadget( #BUTTON_WIDTH_OUTPUT_TEXTBOX, x, y, 70, 25, "", #PB_String_ReadOnly ) : x + 70
ButtonGadget( #BUTTON_COPY_OUTPUT_BUTTON, x, y, 120, 25, "Copy To Clipboard" )
x = 10 : y + 35

TextGadget( #CHECKBOX_WIDTH_OUTPUT_LABEL, x, y, 110, 25, "Checkbox Width:" ) : x + 110
StringGadget( #CHECKBOX_WIDTH_OUTPUT_TEXTBOX, x, y, 70, 25, "", #PB_String_ReadOnly ) : x + 70
ButtonGadget( #CHECKBOX_COPY_OUTPUT_BUTTON, x, y, 120, 25, "Copy To Clipboard" )
x = 10 : y + 35

CheckBoxGadget( #MAKE_WINDOW_STICKY_CHECKBOX, x, y, 130, 18, "Make Window Sticky" )
y + 28

TextGadget( #INSTRUCTIONS_LABEL, x, y, 100, 18, "Instructions" ) : y + 18
EditorGadget( #INSTRUCTIONS_EDITBOX, x, y, 300, 237, #PB_Editor_ReadOnly | #PB_Editor_WordWrap )
SetGadgetText( #INSTRUCTIONS_EDITBOX, "This tool will tell you how much room that you need to allow for the text in a TextGadget, ButtonGadget, or CheckBoxGadget (also works for the OptionGadget)."+~"\n\n"+
"Enter the text you wish to measure into the 'Enter Input Text Here' textbox. You can type it in, paste it in manually, or use the 'Paste From Clipboard and Calculate' Button to paste it in and calculate the widths."+~"\n\n"+
"Once you have entered the text, click the 'Calculate' button to do the calculations. If you used the 'Paste From Clipboard and Calculate' button then this will be done automatically."+~"\n\n"+
"The size needed for a text gadget will appear in the 'TextGadget Width' textbox."+~"\n\n"+
"The size needed for a button gadget will appear in the 'Button Width' textbox. "+~"\n\n"+
"The size needed for a checkbox or option gadget will appear in the 'Checkbox Width' textbox. "+~"\n\n"+
"You can use the 'Copy To Clipboard' buttons next to these text boxes to copy the output to the system clipboard."+~"\n\n"+
"The 'Make Window Sticky' checkbox lets you make this app's window stay on top of other windows (unless it is minimized)."+~"\n\n"+
~"\n\n\n\n" )

TextGadget( #TEST_TEXTGADGET, 0, 1000, 100, 25, "" )
ButtonGadget( #TEST_BUTTON, 0, 1025, 100, 25, "" )
CheckBoxGadget( #TEST_CHECKBOX, 0, 1050, 100, 25, "" )
EndProcedure



Procedure CalculateGadgetWidths()
SetGadgetText( #TEST_TEXTGADGET, GetGadgetText( #INPUT_TEXT_TEXTBOX ) )
SetGadgetText( #TEXTGADGET_WIDTH_OUTPUT_TEXTBOX, Str( GadgetWidth( #TEST_TEXTGADGET, #PB_Gadget_RequiredSize ) ) )

SetGadgetText( #TEST_BUTTON, GetGadgetText( #INPUT_TEXT_TEXTBOX ) )
SetGadgetText( #BUTTON_WIDTH_OUTPUT_TEXTBOX, Str( GadgetWidth( #TEST_BUTTON, #PB_Gadget_RequiredSize ) ) )

SetGadgetText( #TEST_CHECKBOX, GetGadgetText( #INPUT_TEXT_TEXTBOX ) )
SetGadgetText( #CHECKBOX_WIDTH_OUTPUT_TEXTBOX, Str( GadgetWidth( #TEST_CHECKBOX, #PB_Gadget_RequiredSize ) ) )
EndProcedure



Procedure RunEventLoop()
Repeat
	Select WaitWindowEvent()
		Case #PB_Event_CloseWindow
			End
		Case #PB_Event_Gadget
			Select EventGadget()
				Case #CALCULATE_BUTTON
					CalculateGadgetWidths()
					
				Case #PASTE_INPUT_AND_CALCULATE_BUTTON
					SetGadgetText( #INPUT_TEXT_TEXTBOX, GetClipboardText() )
					CalculateGadgetWidths()
					
				Case #BUTTON_COPY_OUTPUT_BUTTON
					SetClipboardText( GetGadgetText( #BUTTON_WIDTH_OUTPUT_TEXTBOX ) )
					
				Case #TEXTGADGET_COPY_OUTPUT_BUTTON
					SetClipboardText( GetGadgetText( #TEXTGADGET_WIDTH_OUTPUT_TEXTBOX ) )
					
				Case #CHECKBOX_COPY_OUTPUT_BUTTON
					SetClipboardText( GetGadgetText( #CHECKBOX_WIDTH_OUTPUT_TEXTBOX ) )
					
				Case #MAKE_WINDOW_STICKY_CHECKBOX
					StickyWindow( #MAIN_WINDOW, Bool( GetGadgetState( #MAKE_WINDOW_STICKY_CHECKBOX ) = #PB_Checkbox_Checked ) )
					
			EndSelect
	EndSelect
ForEver
EndProcedure

CreateMainWindow()
RunEventLoop()


Last edited by Axeman on Sat Dec 18, 2021 11:58 pm, edited 1 time in total.
Mesa
Enthusiast
Enthusiast
Posts: 342
Joined: Fri Feb 24, 2012 10:19 am

Re: Tool calculates required size for text in text, button, checkbox, and option gadgets

Post by Mesa »

i fixed something wrong with "#TEST_TEXTGADGET"

Code: Select all

; Windows.
#MAIN_WINDOW = 0

; Gadgets.
Enumeration
	#INPUT_TEXT_LABEL
	#INPUT_TEXT_TEXTBOX
	#PASTE_INPUT_BUTTON
	#DO_ACTION_BUTTON
	
	#TEXTGADGET_WIDTH_OUTPUT_LABEL
	#TEXTGADGET_WIDTH_OUTPUT_TEXTBOX
	#TEXTGADGET_COPY_OUTPUT_BUTTON
	
	#CHECKBOX_WIDTH_OUTPUT_LABEL
	#CHECKBOX_WIDTH_OUTPUT_TEXTBOX
	#CHECKBOX_COPY_OUTPUT_BUTTON
	
	#INSTRUCTIONS_LABEL
	#INSTRUCTIONS_EDITBOX
	
	#TEST_TEXTGADGET
	#TEST_CHECKBOX
	#TEST_CONTAINER
EndEnumeration


Procedure CreateMainWindow()
	flags = #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered | #PB_Window_TitleBar | #PB_Window_SystemMenu
	If OpenWindow( #MAIN_WINDOW, 0, 0, 320, 586, "Calculate PB UI Text Width", flags ) = 0 : MessageRequester( "Fatal Error", "Unable to create main window." ) : End : EndIf
	x = 10 : y = 10
	TextGadget( #INPUT_TEXT_LABEL, x, y, 300, 18, "Enter Input Text Here" ) : y + 18
	StringGadget( #INPUT_TEXT_TEXTBOX, x, y, 300, 25, "" ) : y + 25
	ButtonGadget( #PASTE_INPUT_BUTTON, x, y, 150, 25, "Paste From Clipboard" )
	ButtonGadget( #DO_ACTION_BUTTON, x + 150, y, 150, 25, "Calculate" )
	y + 35
	
	TextGadget( #TEXTGADGET_WIDTH_OUTPUT_LABEL, x, y, 110, 25, "TextGadget Width:" ) : x + 110
	StringGadget( #TEXTGADGET_WIDTH_OUTPUT_TEXTBOX, x, y, 70, 25, "", #PB_String_ReadOnly ) : x + 70
	ButtonGadget( #TEXTGADGET_COPY_OUTPUT_BUTTON, x, y, 120, 25, "Copy To Clipboard" )
	x = 10 : y + 35
	
	TextGadget( #CHECKBOX_WIDTH_OUTPUT_LABEL, x, y, 110, 25, "Checkbox Width:" ) : x + 110
	StringGadget( #CHECKBOX_WIDTH_OUTPUT_TEXTBOX, x, y, 70, 25, "", #PB_String_ReadOnly ) : x + 70
	ButtonGadget( #CHECKBOX_COPY_OUTPUT_BUTTON, x, y, 120, 25, "Copy To Clipboard" )
	x = 10 : y + 35
	
	TextGadget( #INSTRUCTIONS_LABEL, x, y, 100, 18, "Instructions" ) : y + 18
	EditorGadget( #INSTRUCTIONS_EDITBOX, x, y, 300, 300, #PB_Editor_ReadOnly | #PB_Editor_WordWrap )
	SetGadgetText( #INSTRUCTIONS_EDITBOX, "This tool will tell you how much room that you need to allow for the text in a TextGadget or CheckBoxGadget (also useable for the ButtonGadget and OptionGadget respectively)."+~"\n\n"+
	                                      "Enter the text you wish to measure into the 'Enter Input Text Here' textbox. You can type it in, paste it in manually, or use the 'Paste From Clipboard' Button to paste it in."+~"\n\n"+
	                                      "Once you have entered the text, click the 'Calculate' button to do the calculations."+~"\n\n"+
	                                      "The size needed for a text gadget will appear in the 'TextGadget Width' textbox. This can also be used for a button gadget, though you might want to add some margin spacing."+~"\n\n"+
	                                      "The size needed for a checkbox or option gadget will appear in the 'Checkbox Width' textbox. "+~"\n\n"+
	                                      "You can use the 'Copy To Clipboard' buttons next to these text boxes to copy the output to the system clipboard."+~"\n\n\n\n\n\n" )
	
	x = 10 : y + 320
	TextGadget( #TEST_TEXTGADGET, x, y, 100, 25, "Example" )
	ContainerGadget(#TEST_CONTAINER,x, y+35, 100, 25,#PB_Container_Flat )
	CheckBoxGadget( #TEST_CHECKBOX, 0, 0, 100, 25, "Example" );CheckBoxGadget( #TEST_CHECKBOX, x, y+35, 100, 25, "ok" )
	CloseGadgetList() 
	
	SetGadgetColor(#TEST_TEXTGADGET, #PB_Gadget_BackColor , #Yellow)
	; SetGadgetColor(#TEST_CONTAINER, #PB_Gadget_BackColor , #Yellow)
	
	ResizeGadget(#TEST_TEXTGADGET,#PB_Ignore ,#PB_Ignore ,GadgetWidth( #TEST_TEXTGADGET, #PB_Gadget_RequiredSize ),#PB_Ignore )
	ResizeGadget(#TEST_CHECKBOX,#PB_Ignore ,#PB_Ignore ,GadgetWidth( #TEST_CHECKBOX, #PB_Gadget_RequiredSize ),#PB_Ignore )
	ResizeGadget(#TEST_CONTAINER,#PB_Ignore ,#PB_Ignore ,GadgetWidth( #TEST_CHECKBOX, #PB_Gadget_RequiredSize ),#PB_Ignore )
	
EndProcedure


Procedure RunEventLoop()
	Repeat
		Select WaitWindowEvent()
			Case #PB_Event_CloseWindow
				End
			Case #PB_Event_Gadget
				Select EventGadget()
					Case #DO_ACTION_BUTTON
						SetGadgetText( #TEST_TEXTGADGET, GetGadgetText( #INPUT_TEXT_TEXTBOX ) )
						ResizeGadget(#TEST_TEXTGADGET,#PB_Ignore ,#PB_Ignore ,GadgetWidth( #TEST_TEXTGADGET, #PB_Gadget_RequiredSize ),#PB_Ignore )
						SetGadgetText( #TEXTGADGET_WIDTH_OUTPUT_TEXTBOX, Str( GadgetWidth( #TEST_TEXTGADGET, #PB_Gadget_RequiredSize ) ) )
						
						SetGadgetText( #TEST_CHECKBOX, GetGadgetText( #INPUT_TEXT_TEXTBOX ) )
						SetGadgetText( #CHECKBOX_WIDTH_OUTPUT_TEXTBOX, Str( GadgetWidth( #TEST_CHECKBOX, #PB_Gadget_RequiredSize ) ) )
						ResizeGadget(#TEST_CHECKBOX,#PB_Ignore ,#PB_Ignore ,GadgetWidth( #TEST_CHECKBOX, #PB_Gadget_RequiredSize ),#PB_Ignore )
						ResizeGadget(#TEST_CONTAINER,#PB_Ignore ,#PB_Ignore ,GadgetWidth( #TEST_CHECKBOX, #PB_Gadget_RequiredSize ),#PB_Ignore )
						
					Case #PASTE_INPUT_BUTTON
						SetGadgetText( #INPUT_TEXT_TEXTBOX, GetClipboardText() )
						
					Case #TEXTGADGET_COPY_OUTPUT_BUTTON
						SetClipboardText( GetGadgetText( #TEXTGADGET_WIDTH_OUTPUT_TEXTBOX ) )
						
					Case #CHECKBOX_COPY_OUTPUT_BUTTON
						SetClipboardText( GetGadgetText( #CHECKBOX_WIDTH_OUTPUT_TEXTBOX ) )
				EndSelect
		EndSelect
	ForEver
EndProcedure

CreateMainWindow()
RunEventLoop()

M.
Joris
Addict
Addict
Posts: 885
Joined: Fri Oct 16, 2009 10:12 am
Location: BE

Re: Tool calculates required size for text in text, button, checkbox, and option gadgets

Post by Joris »

Thanks.
Yeah I know, but keep in mind ... Leonardo da Vinci was also an autodidact.
Axeman
User
User
Posts: 89
Joined: Mon Nov 03, 2003 5:34 am

Re: Tool calculates required size for text in text, button, checkbox, and option gadgets

Post by Axeman »

Made a few changes.

Added result output for button gadgets, made the 'Paste From Clipboard' button also do the calculation, and added a 'Make Window Sticky' checkbox to optionally make the app's window stay on top of other windows.
BarryG
Addict
Addict
Posts: 3266
Joined: Thu Apr 18, 2019 8:17 am

Re: Tool calculates required size for text in text, button, checkbox, and option gadgets

Post by BarryG »

Nice tool! I'll be using it for sure. (Currently I calculate manually and then test, adjust, re-test, adjust, etc).
Post Reply