Best Practice for Handling Multiple Windows

BlitzMax Forums/MaxGUI Module/Best Practice for Handling Multiple Windows

Jayjay(Posted 2012) [#1]
Hi all

Thanks in advance for any help. I have been away a while but have returned to be a bmax faithful!

I want to create an application that uses several windows (at different times) each will have a canvas for drawing and buttons etc!

Which is the best way to handle multiple window control from the main program? I understand there are several ways of doing this..the sample in the tutorials which creates an applet leaves the program in the applet event hook, and it seems control is not passed back to the main loop???

Any ideas greatly appreciated as this is really holding me up!


matibee(Posted 2012) [#2]
Code your window as its own type and have it register an event hook. You can then leave it to its own devices.

When you want the window to command the main application to do something, post a user event for your main app (or any other, interested event handler ) to act upon.

Here's the source to one of my little dialog window types. You won't be able to compile it but you can see it's a self contained unit. When the user hits the ok button it posts a user defined event which the main app picks up.




Jayjay(Posted 2012) [#3]
Hi Matibee

Thats is just what I am looking for but could you give me an example main app event loop that would pick up the event returned from the okay button?

Really appreciate your help!


matibee(Posted 2012) [#4]
The main app event loop will look like any other - check out the samples.

After the main window is created, and before the app starts its message processing loop, I need to call ToolDialog.Init() with the main app window (so it knows who its parent is.)

The user event is 'fired' (rather than 'returned') when the user hits the ok button (see code line 56 above). As the main app window is still going through its own message loop it'll be picked up there.

The main app message processing loop might be something like this...

While True
	WaitEvent()

	Select EventID()
		Case EVENT_GADGETACTION
			Select EventSource()
				Case toolOriginBtn
					ToolOriginDialog.Show()
			End Select 	
		
		Case UEVENT_TOOLDATUM_SET
			TProject.SetToolDatum( ToolOriginDialog.Position() )
		
		Case EVENT_WINDOWCLOSE
			Select EventSource()
				Case window
					MenuAction_OnExit() ' does same thing
			End Select 
			
		Case EVENT_GADGETPAINT
			OnGadgetPaint()
			
		Case EVENT_WINDOWSIZE
			If EventSource() = window
				SetViewport( 0, 0, window.ClientWidth(), window.ClientHeight() )
			End If 
	EndSelect
	
Wend


User events are just const integer values, and should start with the brl defined user event mask, so the id used above was defined like this..

Global UEVENT_TOOLDATUM_SET%	 		= EVENT_USEREVENTMASK + $0300


I must admit there are methods available to register user event ids rather than defining them adhoc, but I've had no issues doing it this way.


jsp(Posted 2012) [#5]
To use several windows on one program it is not mandatory to use hooks. While hooks are most of the time useful it is not a must!
Just define all windows you need as hidden and have all gadgets in your main WaitEvent() loop.
Then show or hide only those windows you need at a certain time.