WindowMenu refresh on a canvas

BlitzMax Forums/BlitzMax GUI Programming/WindowMenu refresh on a canvas

Help!!! I have a very strange problem here with window menus.
I have 4 menus: file, edit, view & help.
If I have an empty window, there are no problems but if I make a canvas the size of the window, then something strange happens: the first menu I select appears but when I move the mouse cursor over another menu the first will not disappear (only the dropshadow disappears). With all the other menus there are no problems, just with the first one (no matter which menu is the first). I found out that the whole application is 'on hold' when the mouse is on a menu, so there is no possibility of refreshing the canvas, or something and the application does not receive any EventID's.

Is this a bug?

Here is an example illustrating the bug.
Any ideas how to solve this?

' createcanvas.bmx

Strict 

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

Const MENU_CUT%=107
Const MENU_COPY%=108
Const MENU_PASTE%=109

Const MENU_VIEW1% = 110
Const MENU_VIEW2% = 111
Const MENU_VIEW3% = 112
Const MENU_VIEW4% = 113

Const MENU_ABOUT%=114

Global GAME_WIDTH=640
Global GAME_HEIGHT=480

' create a centered window with client size GAME_WIDTH,GAME_HEIGHT

Local wx=(ClientWidth(Desktop())-GAME_WIDTH)/2
Local wy=(ClientHeight(Desktop())-GAME_HEIGHT)/2

Local window:TGadget=CreateWindow("My Canvas",wx,wy,GAME_WIDTH,GAME_HEIGHT,Null,WINDOW_TITLEBAR|WINDOW_CLIENTCOORDS|WINDOW_MENU)
Local filemenu:TGadget=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"&Print...", MENU_PRINT,filemenu,KEY_P,MODIFIER_COMMAND
CreateMenu"",0,filemenu
CreateMenu"E&xit",MENU_EXIT,filemenu,KEY_F4,MODIFIER_COMMAND

Local editmenu:TGadget=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

Local viewmenu:TGadget=CreateMenu("&View",0,WindowMenu(window))
CreateMenu("Drawing A",MENU_VIEW1,viewmenu,KEY_1,MODIFIER_COMMAND)
CreateMenu("Drawing B",MENU_VIEW2,viewmenu,KEY_2,MODIFIER_COMMAND)
CreateMenu("Drawing C",MENU_VIEW3,viewmenu,KEY_3,MODIFIER_COMMAND)
CreateMenu("Drawing D",MENU_VIEW4,viewmenu,KEY_4,MODIFIER_COMMAND)

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

UpdateWindowMenu window


' create a canvas for our game

Local canvas:TGadget=CreateCanvas(0,0,320,240,window)

' create an update timer

CreateTimer 60

While WaitEvent()
	Select EventID()
		Case EVENT_TIMERTICK
			RedrawGadget canvas

		Case EVENT_GADGETPAINT
			SetGraphics CanvasGraphics(canvas)
			SetOrigin 160,120
			SetLineWidth 5
			Cls
			Local t=MilliSecs()
			DrawLine 0,0,120*Cos(t),120*Sin(t)
			DrawLine 0,0,80*Cos(t/60),80*Sin(t/60)
			Flip

		Case EVENT_MOUSEMOVE
			Print "MOVE!"

		Case EVENT_WINDOWCLOSE
			FreeGadget canvas
			End

		Case EVENT_APPTERMINATE
			End
	End Select
Wend


This can be solved by using hooks.

Look at the paint function ;)
Strict 

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

Const MENU_CUT%=107
Const MENU_COPY%=108
Const MENU_PASTE%=109

Const MENU_VIEW1% = 110
Const MENU_VIEW2% = 111
Const MENU_VIEW3% = 112
Const MENU_VIEW4% = 113

Const MENU_ABOUT%=114

Global GAME_WIDTH=640
Global GAME_HEIGHT=480

' create a centered window with client size GAME_WIDTH,GAME_HEIGHT

Local wx=(ClientWidth(Desktop())-GAME_WIDTH)/2
Local wy=(ClientHeight(Desktop())-GAME_HEIGHT)/2

Local window:TGadget=CreateWindow("My Canvas",wx,wy,GAME_WIDTH,GAME_HEIGHT,Null,WINDOW_TITLEBAR|WINDOW_CLIENTCOORDS|WINDOW_MENU)
Local filemenu:TGadget=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"&Print...", MENU_PRINT,filemenu,KEY_P,MODIFIER_COMMAND
CreateMenu"",0,filemenu
CreateMenu"E&xit",MENU_EXIT,filemenu,KEY_F4,MODIFIER_COMMAND

Local editmenu:TGadget=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

Local viewmenu:TGadget=CreateMenu("&View",0,WindowMenu(window))
CreateMenu("Drawing A",MENU_VIEW1,viewmenu,KEY_1,MODIFIER_COMMAND)
CreateMenu("Drawing B",MENU_VIEW2,viewmenu,KEY_2,MODIFIER_COMMAND)
CreateMenu("Drawing C",MENU_VIEW3,viewmenu,KEY_3,MODIFIER_COMMAND)
CreateMenu("Drawing D",MENU_VIEW4,viewmenu,KEY_4,MODIFIER_COMMAND)

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

UpdateWindowMenu window


' create a canvas for our game

Global canvas:TGadget=CreateCanvas(0,0,320,240,window)

' create an update timer

CreateTimer 60

AddHook EmitEventHook, paint

Function paint:Object( id:Int, data:Object, context:Object)
	Local ev:TEvent = TEvent(data)
	If Not ev Then Return data
	If ev.ID = EVENT_TIMERTICK Then
		RedrawGadget canvas
	ElseIf ev.ID = EVENT_GADGETPAINT Then
		SetGraphics CanvasGraphics( canvas)
		SetOrigin 160,120
		SetLineWidth 5
		Cls
		Local t=MilliSecs()
		DrawLine 0,0,120*Cos(t),120*Sin(t)
		DrawLine 0,0,80*Cos(t/60),80*Sin(t/60)
		Flip
		Return Null
	EndIf
	Return data
EndFunction

While WaitEvent()
	Select EventID()
		Case EVENT_WINDOWCLOSE
			FreeGadget canvas
			End

		Case EVENT_APPTERMINATE
			End
	End Select
Wend


Thanks a lot!
I'll look at the code more closer later to see how it works.

The problem is not completely solved yet :(

Run the code below. It's part of a project I'm doing. I have an application window with a canvas and I have a (fake) printer driver window (also with a canvas). Via the file-menu you can call the printer driver window or by pressing Ctrl-P.
If you do not use any setScale, setRotation or setTransform code it seems to work. But I have to do a lot of imaging stuff within the draw method of the printer window. And within the application window I need to be able to offer 4 different views (in this example I use colors, but that will be 4 images).

Modify the code as explained in the code to see the strange effects.
Tell me... what am I doing wrong???
SuperStrict 

Global gApplicationWindow:TApplicationWindow = TApplicationWindow.create()
Global gPrinterDriver:TPrinterDriver = TPrinterDriver.create()


CreateTimer(60)

EnablePolledInput()

AddHook EmitEventHook, paint

gApplicationWindow.open()

Local iEventID%


While True
	PollEvent()
	iEventID = EventID()
	If gPrinterDriver.isOpen() Then
		gPrinterDriver.update(iEventID)
		gPrinterDriver.draw()
	Else
		gApplicationWindow.update(iEventID)
		gApplicationWindow.draw()
	End If
Wend

End


Function paint:Object( id:Int, data:Object, context:Object)
	Local ev:TEvent = TEvent(data)
	If Not ev Then Return data
	If ev.ID = EVENT_TIMERTICK Then
		gApplicationWindow.update(ev.ID)
		gApplicationWindow.draw()
		Return Null
	EndIf
	Return data
End Function



Type TColor
	Field ir%, ig%, ib%
	
	Function create:TColor(r%, g%, b%)
		Local c:TColor = New TColor
		c.ir = r
		c.ig = g
		c.ib = b
		Return c
	End Function

	Method setThisColor()
		SetColor(ir, ig, ib)
	End Method
	
End Type


Type TWindow
	Field poWindow:TGadget
	Field poCanvas:TGadget
	
	Method open()
		draw()
		ActivateWindow(poWindow)
		ShowGadget(poWindow)
	End Method
	
	Method close()
		HideGadget(poWindow)
	End Method

	Method isOpen:Byte()
		Return (Not GadgetHidden(poWindow))
	End Method
	
	Method getWindow:TGadget()
		Return poWindow
	End Method
	
	Method getCanvas:TGadget()
		Return poCanvas
	End Method

	Method update(iEventID%) Abstract
	
	Method draw() Abstract
	
End Type


Type TApplicationWindow Extends TWindow

	Const MENU_NEW%=101
	Const MENU_OPEN%=102
	Const MENU_SAVE%=103
	Const MENU_CLOSE%=104
	Const MENU_PRINT%=105
	Const MENU_EXIT%=106
	
	Const MENU_CUT%=107
	Const MENU_COPY%=108
	Const MENU_PASTE%=109
	
	Const MENU_VIEW1% = 110
	Const MENU_VIEW2% = 111
	Const MENU_VIEW3% = 112
	Const MENU_VIEW4% = 113
	
	Const MENU_ABOUT%=114

	Field ploViewMenu:TGadget[4]
	Field ploBgnd:TColor[4]
	Field piBgnd% = 0

	Function create:TApplicationWindow()
		Local a:TApplicationWindow = New TApplicationWindow 
		Local filemenu:TGadget
		Local editmenu:TGadget
		Local viewmenu:TGadget
		Local helpmenu:TGadget
		
		Local style% = WINDOW_TITLEBAR | WINDOW_CLIENTCOORDS | WINDOW_MENU| WINDOW_HIDDEN
		a.poWindow = CreateWindow("Application", 400, 150, 1024, 768, Null, style)
		
		filemenu=CreateMenu("&File",0,WindowMenu(a.poWindow))
		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"&Print...", MENU_PRINT,filemenu,KEY_P,MODIFIER_COMMAND
		CreateMenu"",0,filemenu
		CreateMenu"E&xit",MENU_EXIT,filemenu,KEY_F4,MODIFIER_COMMAND
		
		editmenu=CreateMenu("&Edit",0,WindowMenu(a.poWindow))
		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
		
		viewmenu=CreateMenu("&View",0,WindowMenu(a.poWindow))
		a.ploViewMenu[0] = CreateMenu("Background 1",MENU_VIEW1,viewmenu,KEY_1,MODIFIER_COMMAND)
		a.ploViewMenu[1] = CreateMenu("Background 2",MENU_VIEW2,viewmenu,KEY_2,MODIFIER_COMMAND)
		a.ploViewMenu[2] = CreateMenu("Background 3",MENU_VIEW3,viewmenu,KEY_3,MODIFIER_COMMAND)
		a.ploViewMenu[3] = CreateMenu("Background 4",MENU_VIEW4,viewmenu,KEY_4,MODIFIER_COMMAND)
		
		helpmenu=CreateMenu("&Help",0,WindowMenu(a.poWindow))
		CreateMenu "&About",MENU_ABOUT,helpmenu
		
		Local dw% = GadgetWidth(a.poWindow)
		Local dh% = GadgetHeight(a.poWindow)
		a.poCanvas:TGadget = CreateCanvas(0, 0, dw, dh, a.poWindow) 

		a.ploBgnd[0] = TColor.create(250, 250, 200)
		a.ploBgnd[1] = TColor.create(200, 250, 200)
		a.ploBgnd[2] = TColor.create(200, 200, 250)
		a.ploBgnd[3] = TColor.create(250, 200, 200)
		
		a.selectBgnd(0) 
		Return a
	End Function


	Method update(iEventID%)
		Select iEventID
			Case EVENT_WINDOWCLOSE
				End
			Case EVENT_MENUACTION
				Select EventData()
					Case MENU_EXIT
						End
					Case MENU_PRINT
						gPrinterDriver.open()
					Case MENU_ABOUT
						draw()
						Notify("My First Multiple Window Application", False)
					Case MENU_VIEW1
						selectBgnd(0)
					Case MENU_VIEW2
						selectBgnd(1)
					Case MENU_VIEW3
						selectBgnd(2)
					Case MENU_VIEW4
						selectBgnd(3)
				End Select
		End Select
	End Method
	
	Method draw()
		SetGraphics CanvasGraphics(poCanvas)
		Cls	
' **** function like setTransform result in strange behavior (rem the next line to see the correct behavior)
		SetTransform 1, 0, 0

		ploBgnd[piBgnd].setThisColor()
		DrawRect(0, 0, GadgetWidth(poCanvas), GadgetHeight(poCanvas))
		Flip
	End Method
	
	
	Method selectBgnd(iBgnd%)
		piBgnd = iBgnd
		For Local i% = 0 To 3
			If i = piBgnd Then
				CheckMenu(ploViewMenu[i])
			Else
				UncheckMenu(ploViewMenu[i])
			End If
		Next		
		UpdateWindowMenu poWindow
	End Method
	
End Type


Type TPrinterDriver Extends TWindow

	Field poImage:TImage
	
	Function create:TPrinterDriver()
		Local p:TPrinterDriver = New TPrinterDriver
		Local dw% = GadgetWidth(Desktop())
		Local dh% = GadgetHeight(Desktop())
		Local dww% = 800
		Local dwh% = 600
		
		p.poWindow = CreateWindow("Print",(dw - dww) / 2, (dh -dwh) / 2, dww, dwh, Null, WINDOW_TITLEBAR|WINDOW_CLIENTCOORDS|WINDOW_HIDDEN) 
		p.poCanvas:TGadget = CreateCanvas(0, 0, dww, dwh, p.poWindow)

		p.poImage = CreateImage(100, 100)
		Return p
	End Function
	
	
	Method update(iEventID%)
		Select iEventID
			Case EVENT_WINDOWCLOSE
				HideGadget(poWindow)
		End Select
	End Method

	
	Method draw()
		SetGraphics CanvasGraphics(poCanvas)
		SetClsColor 255, 255, 255
		Cls
		SetColor 0, 0, 0
		DrawText String(MouseX()) +","+String(MouseY()),10,10
'Rem   **** if you remove te next 4 lines you see that the screen-refresh works ok
		SetColor 255, 255, 255
		SetTransform(45, 2, 2)
		DrawImage(poImage, 100, 100)
		SetTransform(1, 0, 0)
'End Rem		
		Flip
	End Method
		
End Type


It locks up on my puter, but adding the EVENT_GADGETPAINT to the hook makes it responsive.

About the SetTransform.. I assume your resetting it here?
SetTransform 1, 0, 0
' should be
SetTransform 0, 1,1
' or just
SetTransform
Scaling something to 0,0 just makes it disappear ;)

And when your doing SetTransform (or any of the other transform commands), its a good idea to reset it each loop so you know your starting point.

Heres the modified code:
SuperStrict 

Global gApplicationWindow:TApplicationWindow = TApplicationWindow.create()
Global gPrinterDriver:TPrinterDriver = TPrinterDriver.create()


CreateTimer(60)

EnablePolledInput()

AddHook EmitEventHook, paint

gApplicationWindow.open()

Local iEventID%


While True
	PollEvent()
	iEventID = EventID()
	If gPrinterDriver.isOpen() Then
		gPrinterDriver.update(iEventID)
		gPrinterDriver.draw()
	Else
		gApplicationWindow.update(iEventID)
		gApplicationWindow.draw()
	End If
Wend

End


Function paint:Object( id:Int, data:Object, context:Object)
	Local ev:TEvent = TEvent(data)
	If Not ev Then Return data
	If ev.ID = EVENT_TIMERTICK Then
		gApplicationWindow.update(ev.ID)
	ElseIf ev.ID = EVENT_GADGETPAINT Then  '<--
		gApplicationWindow.draw()
	Else
		Return data
	EndIf
	Return Null
End Function



Type TColor
	Field ir%, ig%, ib%
	
	Function create:TColor(r%, g%, b%)
		Local c:TColor = New TColor
		c.ir = r
		c.ig = g
		c.ib = b
		Return c
	End Function

	Method setThisColor()
		SetColor(ir, ig, ib)
	End Method
	
End Type


Type TWindow
	Field poWindow:TGadget
	Field poCanvas:TGadget
	
	Method open()
		draw()
		ActivateWindow(poWindow)
		ShowGadget(poWindow)
	End Method
	
	Method close()
		HideGadget(poWindow)
	End Method

	Method isOpen:Byte()
		Return (Not GadgetHidden(poWindow))
	End Method
	
	Method getWindow:TGadget()
		Return poWindow
	End Method
	
	Method getCanvas:TGadget()
		Return poCanvas
	End Method

	Method update(iEventID%) Abstract
	
	Method draw() Abstract
	
End Type


Type TApplicationWindow Extends TWindow

	Const MENU_NEW%=101
	Const MENU_OPEN%=102
	Const MENU_SAVE%=103
	Const MENU_CLOSE%=104
	Const MENU_PRINT%=105
	Const MENU_EXIT%=106
	
	Const MENU_CUT%=107
	Const MENU_COPY%=108
	Const MENU_PASTE%=109
	
	Const MENU_VIEW1% = 110
	Const MENU_VIEW2% = 111
	Const MENU_VIEW3% = 112
	Const MENU_VIEW4% = 113
	
	Const MENU_ABOUT%=114

	Field ploViewMenu:TGadget[4]
	Field ploBgnd:TColor[4]
	Field piBgnd% = 0

	Function create:TApplicationWindow()
		Local a:TApplicationWindow = New TApplicationWindow 
		Local filemenu:TGadget
		Local editmenu:TGadget
		Local viewmenu:TGadget
		Local helpmenu:TGadget
		
		Local style% = WINDOW_TITLEBAR | WINDOW_CLIENTCOORDS | WINDOW_MENU| WINDOW_HIDDEN
		a.poWindow = CreateWindow("Application", 400, 150, 1024, 768, Null, style)
		
		filemenu=CreateMenu("&File",0,WindowMenu(a.poWindow))
		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"&Print...", MENU_PRINT,filemenu,KEY_P,MODIFIER_COMMAND
		CreateMenu"",0,filemenu
		CreateMenu"E&xit",MENU_EXIT,filemenu,KEY_F4,MODIFIER_COMMAND
		
		editmenu=CreateMenu("&Edit",0,WindowMenu(a.poWindow))
		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
		
		viewmenu=CreateMenu("&View",0,WindowMenu(a.poWindow))
		a.ploViewMenu[0] = CreateMenu("Background 1",MENU_VIEW1,viewmenu,KEY_1,MODIFIER_COMMAND)
		a.ploViewMenu[1] = CreateMenu("Background 2",MENU_VIEW2,viewmenu,KEY_2,MODIFIER_COMMAND)
		a.ploViewMenu[2] = CreateMenu("Background 3",MENU_VIEW3,viewmenu,KEY_3,MODIFIER_COMMAND)
		a.ploViewMenu[3] = CreateMenu("Background 4",MENU_VIEW4,viewmenu,KEY_4,MODIFIER_COMMAND)
		
		helpmenu=CreateMenu("&Help",0,WindowMenu(a.poWindow))
		CreateMenu "&About",MENU_ABOUT,helpmenu
		
		Local dw% = GadgetWidth(a.poWindow)
		Local dh% = GadgetHeight(a.poWindow)
		a.poCanvas:TGadget = CreateCanvas(0, 0, dw, dh, a.poWindow) 

		a.ploBgnd[0] = TColor.create(250, 250, 200)
		a.ploBgnd[1] = TColor.create(200, 250, 200)
		a.ploBgnd[2] = TColor.create(200, 200, 250)
		a.ploBgnd[3] = TColor.create(250, 200, 200)
		
		a.selectBgnd(0) 
		Return a
	End Function


	Method update(iEventID%)
		Select iEventID
			Case EVENT_WINDOWCLOSE
				End
			Case EVENT_MENUACTION
				Select EventData()
					Case MENU_EXIT
						End
					Case MENU_PRINT
						gPrinterDriver.open()
					Case MENU_ABOUT
						draw()
						Notify("My First Multiple Window Application", False)
					Case MENU_VIEW1
						selectBgnd(0)
					Case MENU_VIEW2
						selectBgnd(1)
					Case MENU_VIEW3
						selectBgnd(2)
					Case MENU_VIEW4
						selectBgnd(3)
				End Select
		End Select
	End Method
	
	Method draw()
		SetGraphics CanvasGraphics(poCanvas)
		Cls	
		SetTransform  '<--
' **** function like setTransform result in strange behavior (rem the next line to see the correct behavior)
		'SetTransform 1, 0, 0

		ploBgnd[piBgnd].setThisColor()
		DrawRect(0, 0, GadgetWidth(poCanvas), GadgetHeight(poCanvas))
		Flip
	End Method
	
	
	Method selectBgnd(iBgnd%)
		piBgnd = iBgnd
		For Local i% = 0 To 3
			If i = piBgnd Then
				CheckMenu(ploViewMenu[i])
			Else
				UncheckMenu(ploViewMenu[i])
			End If
		Next		
		UpdateWindowMenu poWindow
	End Method
	
End Type


Type TPrinterDriver Extends TWindow

	Field poImage:TImage
	
	Function create:TPrinterDriver()
		Local p:TPrinterDriver = New TPrinterDriver
		Local dw% = GadgetWidth(Desktop())
		Local dh% = GadgetHeight(Desktop())
		Local dww% = 800
		Local dwh% = 600
		
		p.poWindow = CreateWindow("Print",(dw - dww) / 2, (dh -dwh) / 2, dww, dwh, Null, WINDOW_TITLEBAR|WINDOW_CLIENTCOORDS|WINDOW_HIDDEN) 
		p.poCanvas:TGadget = CreateCanvas(0, 0, dww, dwh, p.poWindow)

		p.poImage = CreateImage(100, 100)
		Return p
	End Function
	
	
	Method update(iEventID%)
		Select iEventID
			Case EVENT_WINDOWCLOSE
				HideGadget(poWindow)
		End Select
	End Method

	
	Method draw()
		SetGraphics CanvasGraphics(poCanvas)
		SetClsColor 255, 255, 255
		Cls
		SetTransform '<--
		SetColor 0, 0, 0
		DrawText String(MouseX()) +","+String(MouseY()),10,10
'Rem   **** if you remove te next 4 lines you see that the screen-refresh works ok
		SetColor 255, 255, 255
		SetTransform(45, 2, 2)
		DrawImage(poImage, 100, 100)
		SetTransform(1, 0, 0)
'End Rem		
		Flip
	End Method
		
End Type


Yes, you're right about setTransform. Stupid mistake!!

First of all, thanks again for your advice. I have to solve this stupid problem!
But about the modification you made. As soon as I open the printer driver it draws the image and mouse location in both windows. When I move the Printer driver window the Application windows refreshes.

I'll try to solve this myself, but I hope you can give me a clue. I still do not really understand how it works with the hook-thing (BlitzMax help is very poor on this item).

Oh, didnt notice that. But its only the first frame just before the window is opened it seems.

Hooks work by intercepting Events (and other stuff) before they reach their destination.
The hook can also stop the event from reaching the event queue by returning Null.

Make sure both Canvases are updated each timer tick, and that they are separated properly.
Passing the whole TEvent object to your Application class instead of just the ID might help, as it has a Source field containing the Gadget the event originated from and some other goodies.

I tried my best but I'm getting more and more confused about the behavior of the events.
As long as I do not move or close the printer driver window the content of both windows remains the same. Why?

I modified the code to my suggestions above, so now the Update method takes a TEvent and filters its actiond depending on the Source field.

I also changed the Hook to reflect this, and removed the code from the main loop as its no longer needed.

Hope this helps =)
SuperStrict 

Global gApplicationWindow:TApplicationWindow = TApplicationWindow.create()
Global gPrinterDriver:TPrinterDriver = TPrinterDriver.create()


CreateTimer(60)

EnablePolledInput()

AddHook EmitEventHook, paint

gApplicationWindow.open()

Local iEventID%


While WaitEvent()
Wend

End


Function paint:Object( id:Int, data:Object, context:Object)
	Local ev:TEvent = TEvent(data)
	If Not ev Then Return data
	gApplicationWindow.update(ev)
	If gPrinterDriver.IsOpen() Then gPrinterDriver.update(ev)
	Return data
End Function



Type TColor
	Field ir%, ig%, ib%
	
	Function create:TColor(r%, g%, b%)
		Local c:TColor = New TColor
		c.ir = r
		c.ig = g
		c.ib = b
		Return c
	End Function

	Method setThisColor()
		SetColor(ir, ig, ib)
	End Method
	
End Type


Type TWindow
	Field poWindow:TGadget
	Field poCanvas:TGadget
	
	Method open()
		draw()
		ActivateWindow(poWindow)
		ShowGadget(poWindow)
	End Method
	
	Method close()
		HideGadget(poWindow)
	End Method

	Method isOpen:Byte()
		Return (Not GadgetHidden(poWindow))
	End Method
	
	Method getWindow:TGadget()
		Return poWindow
	End Method
	
	Method getCanvas:TGadget()
		Return poCanvas
	End Method

	Method update(event:TEvent) Abstract
	
	Method draw() Abstract
	
End Type


Type TApplicationWindow Extends TWindow

	Const MENU_NEW%=101
	Const MENU_OPEN%=102
	Const MENU_SAVE%=103
	Const MENU_CLOSE%=104
	Const MENU_PRINT%=105
	Const MENU_EXIT%=106
	
	Const MENU_CUT%=107
	Const MENU_COPY%=108
	Const MENU_PASTE%=109
	
	Const MENU_VIEW1% = 110
	Const MENU_VIEW2% = 111
	Const MENU_VIEW3% = 112
	Const MENU_VIEW4% = 113
	
	Const MENU_ABOUT%=114

	Field ploViewMenu:TGadget[4]
	Field ploBgnd:TColor[4]
	Field piBgnd% = 0

	Function create:TApplicationWindow()
		Local a:TApplicationWindow = New TApplicationWindow 
		Local filemenu:TGadget
		Local editmenu:TGadget
		Local viewmenu:TGadget
		Local helpmenu:TGadget
		
		Local style% = WINDOW_TITLEBAR | WINDOW_CLIENTCOORDS | WINDOW_MENU| WINDOW_HIDDEN
		a.poWindow = CreateWindow("Application", 400, 150, 1024, 768, Null, style)
		
		filemenu=CreateMenu("&File",0,WindowMenu(a.poWindow))
		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"&Print...", MENU_PRINT,filemenu,KEY_P,MODIFIER_COMMAND
		CreateMenu"",0,filemenu
		CreateMenu"E&xit",MENU_EXIT,filemenu,KEY_F4,MODIFIER_COMMAND
		
		editmenu=CreateMenu("&Edit",0,WindowMenu(a.poWindow))
		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
		
		viewmenu=CreateMenu("&View",0,WindowMenu(a.poWindow))
		a.ploViewMenu[0] = CreateMenu("Background 1",MENU_VIEW1,viewmenu,KEY_1,MODIFIER_COMMAND)
		a.ploViewMenu[1] = CreateMenu("Background 2",MENU_VIEW2,viewmenu,KEY_2,MODIFIER_COMMAND)
		a.ploViewMenu[2] = CreateMenu("Background 3",MENU_VIEW3,viewmenu,KEY_3,MODIFIER_COMMAND)
		a.ploViewMenu[3] = CreateMenu("Background 4",MENU_VIEW4,viewmenu,KEY_4,MODIFIER_COMMAND)
		
		helpmenu=CreateMenu("&Help",0,WindowMenu(a.poWindow))
		CreateMenu "&About",MENU_ABOUT,helpmenu
		
		Local dw% = GadgetWidth(a.poWindow)
		Local dh% = GadgetHeight(a.poWindow)
		a.poCanvas:TGadget = CreateCanvas(0, 0, dw, dh, a.poWindow) 

		a.ploBgnd[0] = TColor.create(250, 250, 200)
		a.ploBgnd[1] = TColor.create(200, 250, 200)
		a.ploBgnd[2] = TColor.create(200, 200, 250)
		a.ploBgnd[3] = TColor.create(250, 200, 200)
		
		a.selectBgnd(0) 
		Return a
	End Function


	Method update(event:TEvent)
		Select event.ID
			Case EVENT_TIMERTICK
				RedrawGadget poCanvas
			Case EVENT_WINDOWCLOSE
				If event.Source = poWindow Then End
			Case EVENT_MENUACTION
				Select event.Data
					Case MENU_EXIT
						End
					Case MENU_PRINT
						gPrinterDriver.open()
					Case MENU_ABOUT
						draw()
						Notify("My First Multiple Window Application", False)
					Case MENU_VIEW1
						selectBgnd(0)
					Case MENU_VIEW2
						selectBgnd(1)
					Case MENU_VIEW3
						selectBgnd(2)
					Case MENU_VIEW4
						selectBgnd(3)
				End Select
			Case EVENT_GADGETPAINT
				If event.Source = poCanvas Then draw()
		End Select
	End Method
	
	Method draw()
		SetGraphics CanvasGraphics(poCanvas)
		Cls	
		SetTransform  '<--
' **** function like setTransform result in strange behavior (rem the next line to see the correct behavior)
		'SetTransform 1, 0, 0

		ploBgnd[piBgnd].setThisColor()
		DrawRect(0, 0, GadgetWidth(poCanvas), GadgetHeight(poCanvas))
		Flip
	End Method
	
	
	Method selectBgnd(iBgnd%)
		piBgnd = iBgnd
		For Local i% = 0 To 3
			If i = piBgnd Then
				CheckMenu(ploViewMenu[i])
			Else
				UncheckMenu(ploViewMenu[i])
			End If
		Next		
		UpdateWindowMenu poWindow
	End Method
	
End Type


Type TPrinterDriver Extends TWindow

	Field poImage:TImage
	
	Function create:TPrinterDriver()
		Local p:TPrinterDriver = New TPrinterDriver
		Local dw% = GadgetWidth(Desktop())
		Local dh% = GadgetHeight(Desktop())
		Local dww% = 800
		Local dwh% = 600
		
		p.poWindow = CreateWindow("Print",(dw - dww) / 2, (dh -dwh) / 2, dww, dwh, Null, WINDOW_TITLEBAR|WINDOW_CLIENTCOORDS|WINDOW_HIDDEN) 
		p.poCanvas:TGadget = CreateCanvas(0, 0, dww, dwh, p.poWindow)

		p.poImage = CreateImage(100, 100)
		Return p
	End Function
	
	
	Method update(event:TEvent)
		Select event.ID
			Case EVENT_TIMERTICK
				RedrawGadget poCanvas
			Case EVENT_WINDOWCLOSE
				If event.Source = poWindow Then HideGadget(poWindow)
			Case EVENT_GADGETPAINT
				If event.Source = poCanvas Then draw()
		End Select
	End Method

	
	Method draw()
		SetGraphics CanvasGraphics(poCanvas)
		SetClsColor 255, 255, 255
		Cls
		SetTransform '<--
		SetColor 0, 0, 0
		DrawText String(MouseX()) +","+String(MouseY()),10,10
'Rem   **** if you remove te next 4 lines you see that the screen-refresh works ok
		SetColor 255, 255, 255
		SetTransform(45, 2, 2)
		DrawImage(poImage, 100, 100)
		SetTransform(1, 0, 0)
'End Rem		
		Flip
	End Method
		
End Type


I checked the code and everythings works the way it should. You are a hero!
I have to go now. I'll try your modifications in my real project to night and let you know the results!
Thanks again!

Ok. Modified my project conform your suggestions and now everything works great!
It all started with a windows menu that did not refresh properly. I still wonder if needs to be that complicated to accomplish what I want, although the finel result shows some nice clean code.
Many thanks for all your effort! Now I can focus on the rest of the project.