Should creating a window be this messy?

BlitzMax Forums/MaxGUI Module/Should creating a window be this messy?

Ryan Burnside(Posted 2009) [#1]
This is some code I have fumbled together using the maxgui tutorials. It seems really ugly and nested as do my other programs. It takes 5 conditional loops just to check gadget status. Any adivce?

Method get_string:String(text:String)
		' gets a string from a popup winda
		Local w:TGadget = CreateWindow(text, 32, 32, 320, 120, Null, WINDOW_TITLEBAR)
		Local f:TGadget = CreateTextField(10, 10, 240, 20, w)
		Local ok:TGadget = CreateButton("Ok", 10, 30, 60, 20, w)
		Local cancel:TGadget = CreateButton("Cancel", 80, 30, 60, 20, w)
		While 1
			WaitEvent()
		
			Select EventID()
				Case EVENT_GADGETACTION
					Select EventSource()
			
						Case ok
							Local t:String = f.GetText()
							w.free
							Return t
			
						Case cancel
							w.free
							Return ""
					EndSelect
		
				Case EVENT_WINDOWCLOSE
					w.free
					Return ""
			EndSelect
		Wend
	End Method




SebHoll(Posted 2009) [#2]
Usually it's best to contain requester dialogs in their own Types, but you're definitely jumping through the correct hoops.

I've coded a simple one for you:
Import MaxGUI.Drivers

Print TStringPopupWindow.Request("What is your name?", "John")

Type TStringPopupWindow
	
	Field w:TGadget = CreateWindow(AppTitle, 200, 200, 320, 100, Null, WINDOW_TITLEBAR|WINDOW_HIDDEN|WINDOW_CLIENTCOORDS|WINDOW_CENTER|WINDOW_TOOL)
	Field l:TGadget = CreateLabel("Please enter a string:",6,8,ClientWidth(w)-12,20,w)
	Field f:TGadget = CreateTextField(6, 30, ClientWidth(w)-12, 21, w)
	Field ok:TGadget = CreateButton("OK", ClientWidth(w)-101, ClientHeight(w)-36, 95, 30, w, BUTTON_OK)
	Field cancel:TGadget = CreateButton("Cancel", 6, ClientHeight(w)-36, 95, 30, w, BUTTON_CANCEL)
	
	Function Request:String( labeltext$, defaulttext$ = "" )
		Return New TStringPopupWindow.Use( labeltext, defaulttext )
	EndFunction
	
	Method Use:String( labeltext$, defaulttext$ = "" )
		
		SetGadgetText( l, labeltext );SetGadgetText( f, defaulttext )
		ShowGadget( w );ActivateGadget( f )
		
		Repeat
			Select WaitEvent()
				Case EVENT_GADGETACTION
					Select EventSource()
						Case ok
							defaulttext = f.GetText()
							Exit
						Case cancel
							Exit
					EndSelect
				
				Case EVENT_WINDOWCLOSE
					Select EventSource()
						Case w;Exit
					EndSelect
			EndSelect
		Forever
		
		FreeGadget(w)
		
		Return defaulttext
		
	EndMethod
	
EndType

You can now ask for a requester simply by calling:
TStringPopupWindow.Request("What is your name?", "John")
Maybe we should have some default requesters in MaxGUI.ProxyGadgets? :P

P.S. Also, I noticed you were using gadget.free() to free gadgets. This isn't quite right - as with all MaxGUI operations, you should really use the official command (in this case FreeGadget()) or if you really want to use OO (not recommended as this may change over time), use the gadget.CleanUp() instead.


Ked(Posted 2009) [#3]
Maybe we should have some default requesters in MaxGUI.ProxyGadgets? :P

+1
I have a couple made up.


Ryan Burnside(Posted 2009) [#4]
Ah very interesting, I wasn't sure how to incorporate the GUI into the game. The tutorials are good but don't really give much of an idea on how to incorporate the GUI into various aspects of the applications themselves.

Some default popups and prompts would be great! There are some common ones missing I feel. Now it is great that one can make these on their own. However it saves some work for the beginner if you have some pre made prompts in the system. It would add some value to the package I feel if some pre made requestors were added. Please consider it. Saves quite a bit of code space on the programmer end. MaxGUI applications have quite a bit of code and gadget checking, which takes up a good chunk of source code. :)


May I ask what the difference is between gadget.free() and FreeGadget() ?


SebHoll(Posted 2009) [#5]
May I ask what the difference is between gadget.free() and FreeGadget() ?

gadge.free() is the internal driver method for freeing the GUI library handles. gadget.CleanUp() is the recursive function that calls gadget.free() but also frees any pointers, item information and children. FreeGadget() calls gadget.CleanUp().