Ooops
January 26, 2021, 05:39:51 AM

Author Topic: [bmx] GUI Application Template by JoshK [ 1+ years ago ]  (Read 586 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] GUI Application Template by JoshK [ 1+ years ago ]
« on: June 29, 2017, 12:28:40 AM »
Title : GUI Application Template
Author : JoshK
Posted : 1+ years ago

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

Code :
Code: BlitzMax
  1. SuperStrict
  2.  
  3. Framework maxgui.win32maxgui
  4. Import brl.eventqueue
  5.  
  6. AppTitle=StripAll(AppFile)
  7.  
  8. Global MainWindow:TGadget
  9.  
  10. MainWindow=CreateWindow(AppTitle,100,100,640,480,Null,WINDOW_TITLEBAR|WINDOW_STATUS|WINDOW_MENU|WINDOW_CLIENTCOORDS)
  11.  
  12. Local root:TGadget
  13. Local menu:TGadget
  14.  
  15. root=WindowMenu(MainWindow)
  16. menu=CreateMenu("File",0,root)
  17. CreateMenu("Open",0,menu,KEY_O,MODIFIER_COMMAND)
  18. CreateMenu("Save",0,menu)
  19. CreateMenu("",0,menu)
  20. CreateMenu("Exit",0,menu)
  21. UpdateWindowMenu(MainWindow)
  22.  
  23. AddHook EmitEventHook,MainHook
  24. AddHook EmitEventHook,MenuHook
  25.  
  26. Repeat; WaitEvent() ; Forever
  27.  
  28. Function MainHook:Object(id:Int,data:Object,context:Object)
  29.         If data=Null Return Null
  30.         Local event:TEvent=TEvent(data)
  31.         Select event.id
  32.                 Case EVENT_WINDOWCLOSE
  33.                         Select event.source
  34.                                 Case MainWindow
  35.                                         CloseProgram()
  36.                         EndSelect
  37.         End Select
  38.         Return data
  39. EndFunction
  40.  
  41. Function MenuHook:Object(id:Int,data:Object,context:Object)
  42.         If data=Null Return Null
  43.         Local event:TEvent=TEvent(data)
  44.         Select event.id
  45.                 Case EVENT_MENUACTION
  46.                         Select GadgetText(TGadget(event.source))
  47.                                 Case "Exit"
  48.                                         CloseProgram()
  49.                         EndSelect
  50.         End Select
  51.         Return data
  52. EndFunction
  53.  
  54. Function CloseProgram()
  55.         Select Proceed("Are you sure you want to quit?")
  56.                 Case 1
  57.                         End
  58.                 Case 0,- 1
  59.                         Return
  60.         EndSelect
  61. 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...
Code: [Select]
Repeat; WaitEvent() ; Forever...with...
Code: [Select]
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...
Code: [Select]
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:
Code: [Select]
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
Code: [Select]
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.
Code: [Select]
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


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal