[bmx] GUI Application Template by JoshK [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:40

Previous topic - Next topic

BlitzBot

Title : GUI Application Template
Author : JoshK
Posted : 1+ years ago

Description : Template to get a GUI application started, the right way

Code :
Code (blitzmax) Select
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 :


SebHoll(Posted 1+ years ago)

 <div class="quote"> Template to get a GUI application started, the right way. </div><div class="quote"> Framework maxgui.win32maxgui </div>Are you missing two letters on the end, there? :PBtw, 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.


SebHoll(Posted 1+ years ago)

 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
EndFunction
This should be safe under most, if not all, circumstances.


Grisu(Posted 1+ years ago)

 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