Only now do you understand the true power

BlitzMax Forums/BlitzMax Programming/Only now do you understand the true power

JoshK(Posted 2009) [#1]
My brief excursion into wxWidgets left me with one idea I picked up from looking at the program structure. Instead of using global variables for all my controls, I am separating the application into types with fields for the different gadgets. Instead of having a bunch of global variables like ScriptEditorWindow I can just use the app.scripteditor.window field. Using the context argument with event hooks, all event handling for the relevant controls can be done easily in one place. I thought this might help some other people, since I have been using MaxGUI for a long time, but never thought to be this organized about it:
SuperStrict

Import maxgui.maxgui
Import maxgui.win32maxguiex
Import maxgui.proxygadgets
Import "LoadMenu.bmx"
Import "../Common/Framewerk/Framewerk.bmx"

Type TScriptEditor

	Field window:TGadget
	Field textarea:TGadget

	Method Delete()
		RemoveHook EmitEventHook,EventHook,Self
	EndMethod
	
	Method Init(group:TGadget)

		window=CreateWindow("Script",0,0,600,400,group,WINDOW_TITLEBAR|WINDOW_STATUS|WINDOW_RESIZABLE|WINDOW_MENU)
		textarea=CreateTextArea(0,0,window.ClientWidth(),window.ClientHeight(),window)
		SetGadgetLayout textarea,1,1,1,1
		
		LoadMenu "script.mnu",WindowMenu(window)
		UpdateWindowMenu(window)
		
		AddHook EmitEventHook,EventHook,Self
		
	EndMethod
	
	Method Open()
		ShowGadget window
		ActivateGadget window		
	EndMethod
	
	Method Close()
		ActivateGadget window.parent
		HideGadget window	
	EndMethod
	
	Function EventHook:Object(id:Int,data:Object,context:Object)
		Local event:TEvent=TEvent(data)
		Local scripteditor:TScriptEditor=TScriptEditor(context)
		If event
			Select event.id
				
			Case EVENT_WINDOWCLOSE
				If event.source=scripteditor.window
					scripteditor.close()
					Return Null
				EndIf
		
			Case EVENT_MENUACTION
				Select String(TGadget(event.source).extra)
					
				Case "ScriptEditor"
					scripteditor.open()
					Return Null
					
				Case "CloseProgram"
					End
				
				Case "RunScriptFile"
					Local file:String=RequestFile("Open File","Lua Script (*.lua):lua;Text Files (*.txt):txt;All Files:*")
					If file
						TScript.errorstring=""
						If LoadScript(file)
							
						Else
							If TScript.errorstring
								Notify TScript.errorstring,1
							Else
								Notify "Failed to load script ~q"+file+"~q.",1										
							EndIf
						EndIf
					EndIf
					Return Null
					
				Case "OpenScript"
					Local file:String=RequestFile("Open File","Lua Script (*.lua):lua;Text Files (*.txt):txt;All Files:*")
					If file
						Local source$=LoadString(file)
						If source
							SetGadgetText scripteditor.textarea,source
							ShowGadget scripteditor.window
							If WindowMinimized(scripteditor.window) RestoreWindow scripteditor.window
							ActivateGadget scripteditor.window
						EndIf
					EndIf
					Return Null
					
				Case "CloseScriptEditor"
					scripteditor.close()
					Return Null
				
				EndSelect
				
			EndSelect
		EndIf
		Return data
	EndFunction
	
EndType



TaskMaster(Posted 2009) [#2]
For a minute there, I thought Xylvan was back...


JoshK(Posted 2009) [#3]
In fact, you can create a class like this:
Type TEventHandler
	
	Method New()
		AddHook EmitEventHook,EventHook,Self
	EndMethod
	
	Method Delete()
		RemoveHook EmitEventHook,EventHook,Self
	EndMethod
	
	Method ProcessEvent:TEvent(event:TEvent) Abstract
	
	Function EventHook:Object(id:Int,data:Object,context:Object)
		Local event:TEvent=TEvent(data)
		If event Return TEventHandler(context).ProcessEvent(event) Else Return data
	EndFunction
	
EndType

And your app components are done like this:
SuperStrict

Import maxgui.maxgui
Import maxgui.win32maxguiex
Import maxgui.proxygadgets
Import "../LoadMenu.bmx"
Import "../../../Common/Framewerk/Framewerk.bmx"
Import "../EventHandler.bmx"

Type TScriptEditor Extends TEventHandler

	Field menufile:String="script.mnu"
	Field window:TGadget
	Field textarea:TGadget

	Method Init(group:TGadget)

		window=CreateWindow("Script",0,0,600,400,group,WINDOW_TITLEBAR|WINDOW_STATUS|WINDOW_RESIZABLE|WINDOW_MENU|WINDOW_HIDDEN)
		textarea=CreateTextArea(0,0,window.ClientWidth(),window.ClientHeight(),window)
		SetGadgetLayout textarea,1,1,1,1
		
		If LoadMenu(menufile,WindowMenu(window))
			UpdateWindowMenu(window)
		Else
			Notify "Failed to load menu file ~q"+menufile+"~q.",1
		EndIf
		
	EndMethod
	
	Method Open()
		ShowGadget window
		ActivateGadget window		
	EndMethod
	
	Method Close()
		ActivateGadget window.parent
		HideGadget window	
	EndMethod

	Method ProcessEvent:TEvent(event:TEvent)
		Select event.id
		
		Case EVENT_WINDOWCLOSE
			If event.source=window
				close()
				Return Null
			EndIf
	
		EndSelect
		Return event
	EndMethod

EndType



Jake L.(Posted 2009) [#4]
...but MaxGui is missing some decent snowflakes and bloom effects...