Menu/canvas problem

BlitzMax Forums/MaxGUI Module/Menu/canvas problem

iprice(Posted 2008) [#1]
I've created a small app that consists of three simple menus and a canvas in a window.

I can click on each menu and they appear as they should, however if I select a menu then move to any other menu, the first menu list is still visible, although loses its focus.

Removing the CreateCanvas command cause the program to work properly, however I need the canvas as I'm drawing images etc. onto it

Here is a cutdown version
Local style = WINDOW_TITLEBAR | WINDOW_MENU  | WINDOW_CLIENTCOORDS

Local MyWindow:TGadget
Local filemenu:TGadget
Local editmenu:TGadget
Local helpmenu:TGadget

Local canvas:TGadget

MyWindow=CreateWindow("Max GUI App.", 112,84,800,600,Null,style)

filemenu=CreateMenu("&File          ",0,WindowMenu(MyWindow))
CreateMenu "&New",101,filemenu,KEY_N,MODIFIER_COMMAND
CreateMenu "&Open file",102,filemenu,KEY_O,MODIFIER_COMMAND
CreateMenu "&Close file",103,filemenu,KEY_C,MODIFIER_COMMAND
CreateMenu"",0,filemenu
CreateMenu "&Save",104,filemenu,KEY_S,MODIFIER_COMMAND
CreateMenu"",0,filemenu
CreateMenu "&Open bitmap image",105,filemenu,KEY_I,MODIFIER_COMMAND
CreateMenu"",0,filemenu
CreateMenu "&Exit",106,fileMenu,KEY_F4,MODIFIER_COMMAND

editmenu=CreateMenu("&Edit          ",0,WindowMenu(MyWindow))
CreateMenu "Cu&t",107,editmenu,KEY_X,MODIFIER_COMMAND
CreateMenu "&Copy",108,editmenu,KEY_C,MODIFIER_COMMAND
CreateMenu "&Paste",109,editmenu,KEY_V,MODIFIER_COMMAND

helpmenu=CreateMenu("&Help          ",0,WindowMenu(MyWindow))
CreateMenu "&About",110,helpmenu

canvas=CreateCanvas(0,0,800,640,MyWindow)

UpdateWindowMenu MyWindow

While True
	WaitEvent 
	Select EventID()
		Case EVENT_MENUACTION
			Select EventData()
			
				Case 106
					End

				Case 110
					Notify "About stuff here"
			End Select
	End Select
Wend


What am I doing wrong?


SebHoll(Posted 2008) [#2]
I can't reproduce here on Vista, but it sounds like the problem may be that you are using an event queue rather than event hooks.

Does the following solve your problem?




iprice(Posted 2008) [#3]
Yes. That works exactly as it should. Thankyou :)


iprice(Posted 2008) [#4]
I've encountered another problem with this. Try as i might, I can't get the EventX(), EventY() from EVENT_MOUSEMOVE or the value returned from EVENT_MOUSEDOWN or EVENT_MOUSEUP.

I can create a canvas easily enough, which does report the outcomes, but the above code, with a CASE EVENT_MOUSExxx doesn't return values, no matter where I put it.

I've rewritten code a dozen times and I can't get menus AND canvas both working properly at the same time. I need to be able to draw on the screen, receive mouse position and press data and access menus (not all at the same time though!).

Further help and pointers would be greatly appreciated.


SebHoll(Posted 2008) [#5]
Try as i might, I can't get the EventX(), EventY() from EVENT_MOUSEMOVE or the value returned from EVENT_MOUSEDOWN or EVENT_MOUSEUP.

You can't use EventX(), EventY() or EventData() to get info about an event processed in an event hook - these functions return values for the first event in an event queue, which we aren't processing inside eventHandler() (the event hasn't even been added to the queue yet).

As such, you have to use the TEvent object fields - look at the select block: it uses pEvent.id instead of EventID().

The equivalent fields for TEvent (which should be used instead inside event hooks) are:

> EventData() - pEvent.data
> EventX() - pEvent.x
> EventY() - pEvent.y


iprice(Posted 2008) [#6]
Err...

Whilst I'm used to coding in Max, this GUI stuff is all nonsense to me. The whole "you can't use menus and canvas at the same time without hooks" leaves me completely blank.

I understand what you're saying about pEvent.X and such, but on putting them in, I can only get them to register mouse position when I click on menus. I need the mouse position MORE than I need the menus, but I still need both.

I am a GUI noob, and proud of it ;)


SebHoll(Posted 2008) [#7]
Sorry, but I don't have too much time at the moment so I can't explain in detail, but its because clicking a menu puts the system into a modal loop where your program's execution is effectively halted until an item is selected. However, modal loops have special callback functions which allow a program to run a specific function as the loop is happening. Adding an event hook basically adds your function to the list that is called while inside the modal loop. This is therefore the only way we can paint to a canvas while a menu is open, by coding our event handler into one of the callback functions.

I can highly recommend Assari's EventHooks tutorial as I found it really easy to understand when I first found out about hooks:

Assari's EventHooks Tutorial

Anyway, this example seems to work fine for me:



Does this example work as expected for you too?


iprice(Posted 2008) [#8]
Doh!

My code (after your initial pointer) was only missing the redrawgadget(canvas) command during the mouse events, otherwise it was pretty much the same.

I was redrawing the canvas within the gadgetpaint event only.

BTW I have been going through the tuts but haven't had time to get to the hook one yet. Perhaps I should have started there!

Many thanks. :)


jsp(Posted 2008) [#9]
Yep, i think that's the point. Only the GadgetPaint is missing and to make it not too complicated we can put only that one in a hook function. All your other stuff can be used then as normal...




iprice(Posted 2008) [#10]
Cheers jsp - I'll have a play with that tomorrow :)