Code archives/BlitzPlus Gui/GUI Application Template
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
| Template to get a GUI application started, the right way | |||||
SuperStrict
Framework maxgui.win32maxgui
Import brl.eventqueue
AppTitle=StripAll(AppFile)
Global MainWindow:TGadget
MainWindow=CreateWindow(AppTitle,100,100,640,480,Null,WINDOW_TITLEBAR|WINDOW_STATUS|WINDOW_MENU|WINDOW_CLIENTCOORDS)
Local root:TGadget
Local menu:TGadget
root=WindowMenu(MainWindow)
menu=CreateMenu("File",0,root)
CreateMenu("Open",0,menu,KEY_O,MODIFIER_COMMAND)
CreateMenu("Save",0,menu)
CreateMenu("",0,menu)
CreateMenu("Exit",0,menu)
UpdateWindowMenu(MainWindow)
AddHook EmitEventHook,MainHook
AddHook EmitEventHook,MenuHook
Repeat; WaitEvent() ; Forever
Function MainHook:Object(id:Int,data:Object,context:Object)
If data=Null Return Null
Local event:TEvent=TEvent(data)
Select event.id
Case EVENT_WINDOWCLOSE
Select event.source
Case MainWindow
CloseProgram()
EndSelect
End Select
Return data
EndFunction
Function MenuHook:Object(id:Int,data:Object,context:Object)
If data=Null Return Null
Local event:TEvent=TEvent(data)
Select event.id
Case EVENT_MENUACTION
Select GadgetText(TGadget(event.source))
Case "Exit"
CloseProgram()
EndSelect
End Select
Return data
EndFunction
Function CloseProgram()
Select Proceed("Are you sure you want to quit?")
Case 1
End
Case 0,- 1
Return
EndSelect
EndFunction |
Comments
| ||
| Template to get a GUI application started, the right way. Framework maxgui.win32maxgui Are you missing two letters on the end, there? :P Btw, if you replace... Repeat; WaitEvent() ; Forever...with... Repeat;WaitSystem();Forever...then you no longer need to import BRL.EventQueue. ;-) Edit: Oh and finally... I would probably use Confirm() instead of Proceed() here... Function CloseProgram()
Select Proceed("Are you sure you want to quit?")
Case 1
End
Case 0,- 1
Return
EndSelect
EndFunction...as the user, when presented with the dialog, may get confused wondering the difference between 'No' and 'Cancel', despite them meaning the same thing in the code. |
| ||
Also, becareful with your hooks. You'll get a runtime error if anything other than a TEvent is accidentally emitted (not likely, but its best to be safe). See below for a fix:Function MainHook:Object(id:Int,data:Object,context:Object) Local event:TEvent=TEvent(data) If Not event Then Return data Select event.id Case EVENT_WINDOWCLOSE Select event.source Case MainWindow CloseProgram() EndSelect End Select Return data EndFunction Function MenuHook:Object(id:Int,data:Object,context:Object) Local event:TEvent=TEvent(data) If Not event Then Return data Select event.id Case EVENT_MENUACTION Select GadgetText(TGadget(event.source)) Case "Exit" CloseProgram() EndSelect End Select Return data EndFunctionThis should be safe under most, if not all, circumstances. |
| ||
Updated the code with all the suggestions from above.
SuperStrict
Framework Maxgui.Drivers
'Import brl.eventqueue
AppTitle=StripAll(AppFile)
Global MainWindow:TGadget
MainWindow=CreateWindow(AppTitle,100,100,640,480,Null,WINDOW_TITLEBAR|WINDOW_STATUS|WINDOW_MENU|WINDOW_CLIENTCOORDS)
Local root:TGadget
Local menu:TGadget
root=WindowMenu(MainWindow)
menu=CreateMenu("File",0,root)
CreateMenu("Open",0,menu,KEY_O,MODIFIER_COMMAND)
CreateMenu("Save",0,menu)
CreateMenu("",0,menu)
CreateMenu("Exit",0,menu)
UpdateWindowMenu(MainWindow)
AddHook EmitEventHook,MainHook
AddHook EmitEventHook,MenuHook
Repeat;WaitSystem();Forever
Function MainHook:Object(id:Int,data:Object,context:Object)
Local event:TEvent=TEvent(data)
If Not event Then Return data
Select event.id
Case EVENT_WINDOWCLOSE
Select event.source
Case MainWindow
CloseProgram()
EndSelect
End Select
Return data
EndFunction
Function MenuHook:Object(id:Int,data:Object,context:Object)
Local event:TEvent=TEvent(data)
If Not event Then Return data
Select event.id
Case EVENT_MENUACTION
Select GadgetText(TGadget(event.source))
Case "Exit"
CloseProgram()
EndSelect
End Select
Return data
EndFunction
Function CloseProgram()
Select Confirm("Are you sure you want to quit?")
Case 1
End
Case 0,- 1
Return
EndSelect
EndFunction
|
Code Archives Forum