[Solved] Updating canvas after #PB_Event_SizeWindow doesn't work

Just starting out? Need help? Post your questions and find answers here.
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

[Solved] Updating canvas after #PB_Event_SizeWindow doesn't work

Post by jacdelad »

Playing with the Dialog-library (and I really like it now) I found something that I cannot explain, but reproduce. Hopefully it's not a bug, just something I don't understand. Tested on Windows.
The following code creates a dialog with a canvas on it. When resizing the window I get a #PB_Event_SizeWindow, so far so good. Processing this within the event loop works as usual: it is called after releasing the left button on the mouse. Usually I can circumvent this by binding the event via BindEvent, then it is processed whenever the window is resized, even while holding the mousebutton. This doesn't work if I create my canvas on a window with the dialog library: The function is called, but the canvas is not redrawn.

Code: Select all

Runtime Enumeration Gadgets
  #Canvas
EndEnumeration

Procedure ResizeHandler()
  StartDrawing(CanvasOutput(#Canvas))
  Box(0,0,GadgetWidth(#Canvas),GadgetHeight(#Canvas),GetSysColor_(#COLOR_BTNFACE))
  StopDrawing()
EndProcedure

XML$ = "<window id='#PB_Any' name='test' width='300' height='400' flags='#PB_Window_SizeGadget | #PB_Window_SystemMenu | #PB_Window_ScreenCentered'>"+
       "<canvas id='#Canvas'/>"+
       "</window>"

ParseXML(0, XML$)
CreateDialog(0)
OpenXMLDialog(0, 0, "test")

MainWindow=DialogWindow(0)
ResizeHandler()
BindEvent(#PB_Event_SizeWindow,@ResizeHandler(),MainWindow)
Repeat
  Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow 
Am I doing something wrong or is it maybe a bug?

For comparison, the same done with normal controls:

Code: Select all

Runtime Enumeration Gadgets
  #Canvas
EndEnumeration

Procedure ResizeHandler()
  Debug ElapsedMilliseconds()
  StartDrawing(CanvasOutput(#Canvas))
  Box(0,0,GadgetWidth(#Canvas),GadgetHeight(#Canvas),GetSysColor_(#COLOR_BTNFACE))
  StopDrawing()
EndProcedure

OpenWindow(0,0,0,300,400,"Test",#PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget)
CanvasGadget(#Canvas,10,10,280,380)
ResizeHandler()

BindEvent(#PB_Event_SizeWindow,@ResizeHandler(),MainWindow)
Repeat
  Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow 
Edit: I have a suspect, but I'm not sure. If I remember right PureBasic creates a stack of BindEvents with the last ones added being processed first. So maybe my repaint is processed before the automatic resizing of the dialog takes effect?!?
Last edited by jacdelad on Fri Dec 02, 2022 10:39 pm, edited 1 time in total.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Cyllceaux
Enthusiast
Enthusiast
Posts: 458
Joined: Mon Jun 23, 2014 1:18 pm
Contact:

Re: Updating canvas after #PB_Event_SizeWindow doesn't work

Post by Cyllceaux »

Why don't you use #PB_EventType_Resize from the canvas gadget?

Code: Select all

BindGadgetEvent(Canvas,@callback(),#PB_EventType_Resize)
User avatar
HeX0R
Addict
Addict
Posts: 980
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Updating canvas after #PB_Event_SizeWindow doesn't work

Post by HeX0R »

or like this:

Code: Select all

Runtime Enumeration Gadgets
  #Canvas
EndEnumeration

Runtime Procedure ResizeHandler()
  If EventType() = #PB_EventType_Resize
   StartDrawing(CanvasOutput(#Canvas))
   Box(0,0,GadgetWidth(#Canvas),GadgetHeight(#Canvas),#Red)
   StopDrawing()
  EndIf
EndProcedure

XML$ = "<window id='#PB_Any' name='test' width='300' height='400' flags='#PB_Window_SizeGadget | #PB_Window_SystemMenu | #PB_Window_ScreenCentered'>"+
       "<canvas id='#Canvas' onevent='ResizeHandler()' />"+
       "</window>"

ParseXML(0, XML$)
CreateDialog(0)
OpenXMLDialog(0, 0, "test")

Repeat
  Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow 
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Updating canvas after #PB_Event_SizeWindow doesn't work

Post by netmaestro »

Nice clean sharp implementation, HeXOR :idea:
BERESHEIT
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Updating canvas after #PB_Event_SizeWindow doesn't work

Post by jacdelad »

Thanks @all, this works perfectly. I didn't think of this, because until now (without dialogs) I put the resizing of all gadgets into one function (because I had to do it manually) and processed it all at once.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Post Reply