Menus on windows with canvases

BlitzMax Forums/BlitzMax Beginners Area/Menus on windows with canvases

Qweeg(Posted 2006) [#1]
If I have a window with a canvas on it and a menu, when the menu is clicked on it appears but remains on the screen until the mouse is clicked again. You can see this in the code below. This is taken straight out of the help - the only difference is a single line that creates a canvas on the window. If You click on the file menu it appears, but if you move the mouse along to the other menus they appear as expected but the initial file menu is still shown on the screen as well. Removing the canvas makes the menu work normally.

So the question is what should I be doing to ensure my menu refreshes correctly when I have a canvas on the window?

Const MENU_NEW=101
Const MENU_OPEN=102
Const MENU_SAVE=103
Const MENU_CLOSE=104
Const MENU_EXIT=105

Const MENU_CUT=106
Const MENU_COPY=107
Const MENU_PASTE=108

Const MENU_ABOUT=109

window=CreateWindow("My Window",40,40,320,240)
Local canvas:TGadget=CreateCanvas(0,0,320,240,window)


filemenu=CreateMenu("&File",0,WindowMenu(window))
CreateMenu"&New",MENU_NEW,filemenu,KEY_N,MODIFIER_COMMAND
CreateMenu"&Open",MENU_OPEN,filemenu,KEY_O,MODIFIER_COMMAND
CreateMenu"&Close",MENU_CLOSE,filemenu,KEY_W,MODIFIER_COMMAND
CreateMenu"",0,filemenu
CreateMenu"&Save",MENU_SAVE,filemenu,KEY_S,MODIFIER_COMMAND
CreateMenu"",0,filemenu
CreateMenu"E&xit",MENU_EXIT,filemenu,KEY_F4,MODIFIER_COMMAND

editmenu=CreateMenu("&Edit",0,WindowMenu(window))
CreateMenu "Cu&t",MENU_CUT,editmenu,KEY_X,MODIFIER_COMMAND
CreateMenu "&Copy",MENU_COPY,editmenu,KEY_C,MODIFIER_COMMAND
CreateMenu "&Paste",MENU_PASTE,editmenu,KEY_V,MODIFIER_COMMAND

helpmenu=CreateMenu("&Help",0,WindowMenu(window))
CreateMenu "&About",MENU_ABOUT,helpmenu

UpdateWindowMenu window

While True
	WaitEvent 
	Select EventID()
		Case EVENT_WINDOWCLOSE
			End
		Case EVENT_MENUACTION
			Select EventData()
				Case MENU_EXIT
					End
				Case MENU_ABOUT
					Notify "Incrediabler~n(C)2005 Incredible Software"
			End Select
	End Select
Wend



CS_TBL(Posted 2006) [#2]
see the help for EVENT_GADGETPAINT


assari(Posted 2006) [#3]
One way is to use eventhook like so:-
Const MENU_NEW=101
Const MENU_OPEN=102
Const MENU_SAVE=103
Const MENU_CLOSE=104
Const MENU_EXIT=105

Const MENU_CUT=106
Const MENU_COPY=107
Const MENU_PASTE=108

Const MENU_ABOUT=109

window=CreateWindow("My Window",40,40,320,240)
Global canvas:TGadget=CreateCanvas(0,0,320,240,window)'<---GLOBAL


filemenu=CreateMenu("&File",0,WindowMenu(window))
CreateMenu"&New",MENU_NEW,filemenu,KEY_N,MODIFIER_COMMAND
CreateMenu"&Open",MENU_OPEN,filemenu,KEY_O,MODIFIER_COMMAND
CreateMenu"&Close",MENU_CLOSE,filemenu,KEY_W,MODIFIER_COMMAND
CreateMenu"",0,filemenu
CreateMenu"&Save",MENU_SAVE,filemenu,KEY_S,MODIFIER_COMMAND
CreateMenu"",0,filemenu
CreateMenu"E&xit",MENU_EXIT,filemenu,KEY_F4,MODIFIER_COMMAND

editmenu=CreateMenu("&Edit",0,WindowMenu(window))
CreateMenu "Cu&t",MENU_CUT,editmenu,KEY_X,MODIFIER_COMMAND
CreateMenu "&Copy",MENU_COPY,editmenu,KEY_C,MODIFIER_COMMAND
CreateMenu "&Paste",MENU_PASTE,editmenu,KEY_V,MODIFIER_COMMAND

helpmenu=CreateMenu("&Help",0,WindowMenu(window))
CreateMenu "&About",MENU_ABOUT,helpmenu

UpdateWindowMenu window'<---NEW
AddHook EmitEventHook, MyHook

While True
	WaitEvent 
	Select EventID()
	Case EVENT_WINDOWCLOSE
		End
	Case EVENT_MENUACTION
	Select EventData()
		Case MENU_EXIT
			End
		Case MENU_ABOUT
			Notify "Incrediabler~n(C)2005 Incredible Software"
	End Select
	RedrawGadget(canvas)'<---NEW
	Case EVENT_GADGETPAINT '<---NEW
		UpdateCanvas()'<---NEW
	End Select
Wend

End

'-----------------NEW---------------------------------------
Function MyHook:Object(iId:Int,tData:Object,tContext:Object)
  Local Event:TEvent=TEvent(tData)

  If Event.source=Canvas 'And Event.ID=EVENT_GADGETPAINT
     UpdateCanvas()
     Return Null
  EndIf

  Return tData
End Function

Function UpdateCanvas:Int()
     SetGraphics CanvasGraphics(Canvas)
     Cls
     Flip
End Function
'-----------------------------------------------------------

see tutorial here if you want to learn more


Qweeg(Posted 2006) [#4]
Oh, thanks loads Assari. I had the code to update the canvas on a gadgetpaint event already, but it didn't twig that I needed an event hook for this as well.

BTW I have worked through your tutorials already and thought they were top notch!