Some general dialog popup message functions.

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

Some general dialog popup message functions.

Post by Axeman »

Some general dialog popup message functions. I use these in almost all my projects, so I thought I'd share them. See the bottom of the code for a usage example (just run the code to show the demo in action).

Code: Select all



; Note that any functions that are not actually called in your code are not compiled with the code. The declaration of globals, etc, used by those functions may be compiled into the code, however (I'm not actually sure how this is handled).



; An example 'EndProgram()' function. This is called by the 'FatalError()' function and is used in the example code at the bottom of the file.
Procedure EndProgram()
; Does cleanup and saving and then ends the program.

; ** Save data, etc, here. **

End
EndProcedure



; ---



Procedure FlushWindowEventQue()
; Clears the event que.

While WindowEvent() : Delay( 1 ) : Wend
EndProcedure



Procedure Message( message.s )
; Displays a message.
; Caret '^' characters in the message are turned into double newlines.

MessageRequester( "Message", ReplaceString( message.s, "^", ~"\n\n" ) )
EndProcedure



Procedure Confirm( message.s )
; Displays a confirm dialog with Yes/No buttons. Returns #TRUE if the 'Yes' button is clicked, and #FALSE otherwise.
; Caret '^' characters in the message are turned into double newlines.

ProcedureReturn Bool( MessageRequester( "Confirm Action", ReplaceString( message.s, "^", ~"\n\n" ), #PB_MessageRequester_YesNo ) = #PB_MessageRequester_Yes )
EndProcedure



Procedure Error( message.s )
; Displays an error message.
; Caret '^' characters in the message are turned into double newlines.

MessageRequester( "Error", ReplaceString( message.s, "^", ~"\n\n" ) )
EndProcedure



Procedure FileError( message.s, filepath.s )
; Displays a file error message.
; Caret '^' characters in the message are turned into double newlines.

MessageRequester( "File Error", ReplaceString( message.s, "^", ~"\n\n" ) + ~"\n\nFILE: " + filepath.s )
EndProcedure



Procedure FatalError( message.s )
; Displays a fatal error message and then calls the 'EndProgram()' function to shutdown the program. Replace 'EndProgram()' with 'End' or a different function as needed.
; Caret '^' characters in the message are turned into double newlines.

MessageRequester( "Fatal Error - Ending Program", ReplaceString( "The program has encountered a fatal error and needs to close down.^The error encountered is:-^" + message.s, "^", ~"\n\n" ) )
EndProgram() ; << Use 'End' here if you don't have an 'EndProgram()' or similar function that you use to shutdown the program.
EndProcedure



; ## COMMENT THIS SECTION OUT IF YOU DON'T USE THE HELP BUTTON LIBRARY ##

; Declarations for the help button library.
Structure HELP_BUTTON
gadget_id.i ; This field probably isn't needed for the callback version of the updater function, but we'll leave it here.
help_text.s
;*help_struct
EndStructure
Global NewList HelpButtonList.HELP_BUTTON()
#HELP_BUTTON_WIDTH = 15 : #HELP_BUTTON_CLEARANCE = #HELP_BUTTON_WIDTH + 10
Global G_help_font_id = FontID( LoadFont( #PB_Any, "Arial", 10, #PB_Font_Bold ) )

Procedure HelpButtonCallback()
; The callback used to update the help buttons.

*help_struct.HELP_BUTTON = GetGadgetData( EventGadget() )
MessageRequester( "Help Info", ReplaceString( *help_struct\help_text, "^", ~"\n\n" ) )
EndProcedure



Procedure HelpButton( x, y, help_text.s )
; Creates a small help button at the specified x, y position with a question mark in the button. The buttons are updated via a callback.
; 'help_text.s' should contain the text message that you want to display when the button is clicked.
; Caret '^' characters in 'help_text.s' are turned into double newlines.
; Use the '#HELP_BUTTON_CLEARANCE' constant to position items that follow the help button on the same line. This also adds a 10 pixel space between the help button and the following item.
; -
; Note that these buttons use the font which has its FontID() stored in the global variable 'G_help_font_id', so make sure that font is loaded. ~
; ~ eg. Global G_help_font_id = FontID( LoadFont( #PB_Any, "Arial", 10, #PB_Font_Bold ) )

AddElement( HelpButtonList() )
HelpButtonList()\gadget_id = ButtonGadget( #PB_Any, x, y, #HELP_BUTTON_WIDTH, 25, "?" )
HelpButtonList()\help_text = help_text.s
SetGadgetFont( HelpButtonList()\gadget_id, G_help_font_id )
SetGadgetData( HelpButtonList()\gadget_id, @HelpButtonList() )
BindGadgetEvent( HelpButtonList()\gadget_id, @HelpButtonCallback(), #PB_EventType_LeftClick ) 
EndProcedure

; ## - END AREA - ##



; ## EXAMPLE CODE ##

#MAIN_WINDOW = 0

Enumeration
#MESSAGE_DIALOG_BUTTON
#CONFIRM_DIALOG_BUTTON
#ERROR_DIALOG_BUTTON
#FILE_ERROR_DIALOG_BUTTON
#FATAL_ERROR_DIALOG_BUTTON
#END_PROGRAM_BUTTON
#MAKE_WINDOW_STICKY_CHECKBOX
EndEnumeration



Procedure CreateMainWindow()

If OpenWindow( #MAIN_WINDOW, 0, 0, 345, 255, "Message Dialogs Example", #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered | #PB_Window_TitleBar | #PB_Window_SystemMenu ) = 0 : FatalError( "Unable to create main window." ) : EndIf

x = 10 : y = 10

HelpButton( x, y, "This button opens a message popup window.^Use a caret character to include a double newline in this help message." )
ButtonGadget( #MESSAGE_DIALOG_BUTTON, x + #HELP_BUTTON_CLEARANCE, y, 300, 25, "Message Dialog" )
y + 35

HelpButton( x, y, "This button opens a confirm dialog window.^The dialog will return #TRUE if the 'Yes' button is clicked, and #FALSE otherwise." )
ButtonGadget( #CONFIRM_DIALOG_BUTTON, x + #HELP_BUTTON_CLEARANCE, y, 300, 25, "Confirm Dialog" )
y + 35

HelpButton( x, y, "This button shows an error message in a popup window." )
ButtonGadget( #ERROR_DIALOG_BUTTON, x + #HELP_BUTTON_CLEARANCE, y, 300, 25, "Error Dialog" )
y + 35

HelpButton( x, y, "This button shows a file error message in a popup window.^You can also include the filepath or filename of the problem file." )
ButtonGadget( #FILE_ERROR_DIALOG_BUTTON, x + #HELP_BUTTON_CLEARANCE, y, 300, 25, "File Error Dialog" )
y + 35

HelpButton( x, y, "This button shows a fatal error message in a popup window.^The program will shutdown once the message window is closed." )
ButtonGadget( #FATAL_ERROR_DIALOG_BUTTON, x + #HELP_BUTTON_CLEARANCE, y, 300, 25, "Fatal Error Dialog" )
y + 35

HelpButton( x, y, "This button ends the program." )
ButtonGadget( #END_PROGRAM_BUTTON, x + #HELP_BUTTON_CLEARANCE, y, 300, 25, "End Program" )
y + 35

HelpButton( x, y, "This checkbox lets you make this app's window stay on top of other windows (unless it is minimized)." )
CheckBoxGadget( #MAKE_WINDOW_STICKY_CHECKBOX, x + #HELP_BUTTON_CLEARANCE, y, 130, 18, "Make Window Sticky" )
EndProcedure



Procedure RunEventLoop()
Repeat

	Select WaitWindowEvent()
	
		Case #PB_Event_CloseWindow
			EndProgram()
			
		Case #PB_Event_Gadget
		
			Select EventGadget()
				
				Case #END_PROGRAM_BUTTON
					EndProgram()
			
				Case #MESSAGE_DIALOG_BUTTON
					Message( "This is a message from your computer.^I hope you have a great day." )
				
				Case #CONFIRM_DIALOG_BUTTON
					Select Confirm( "It's the start of a new day.^Would you like to have a great day?" )
						Case 0 : Message( "You had a bad day.^Perhaps tomorrow will be better." )
						Case 1 : Message( "You had a great day.^Hopefully you have another one tomorrow." )
					EndSelect
				
				Case #ERROR_DIALOG_BUTTON
					Error( "There was an error in your day.^Please reset your day." )
				
				Case #FILE_ERROR_DIALOG_BUTTON
					FileError( "The file that your day is stored in could not be found.^Please select a different day.", "your-day.txt" )
				
				Case #FATAL_ERROR_DIALOG_BUTTON
					FatalError( "Your day has experienced a fatal error and needs to end.^Please respawn." )
				
				Case #END_PROGRAM_BUTTON
					EndProgram()
					
				Case #MAKE_WINDOW_STICKY_CHECKBOX
					StickyWindow( #MAIN_WINDOW, Bool( GetGadgetState( #MAKE_WINDOW_STICKY_CHECKBOX ) = #PB_Checkbox_Checked ) )
						
			EndSelect
	
	EndSelect
	
ForEver
EndProcedure



CreateMainWindow()
RunEventLoop()

; ## - END AREA - ##