CanvasGadget not redrawing on maximize

Just starting out? Need help? Post your questions and find answers here.
wombats
Enthusiast
Enthusiast
Posts: 663
Joined: Thu Dec 29, 2011 5:03 pm

CanvasGadget not redrawing on maximize

Post by wombats »

I have a CanvasGadget on a PanelGadget on a Dialog that should redraw when the window changes size. However, it does not do it on the #PB_Event_MaximizeWindow event. Am I doing something wrong, or is this a bug?

Code: Select all

  #XmlEncoding = #PB_UTF8

  #Dialog = 0
  #Xml = 0
  
  Procedure DrawCanvas()
    StartDrawing(CanvasOutput(5))
    Box(0, 0, OutputWidth(), OutputHeight(), RGB(0, 0, 255))
    StopDrawing()
  EndProcedure
  
  Procedure OnResize()
    DrawCanvas()
  EndProcedure
  
  Procedure OnMaximize()
    DrawCanvas()
  EndProcedure
  
  XML$ = "<window id='#PB_Any' name='test' text='test' minwidth='auto' minheight='auto' flags='#PB_Window_MaximizeGadget | #PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget'>" +
         "  <panel>" +
         "    <tab text='First tab'>" +
         "      <canvas id='5' width='200' height='100'/>" +
         "    </tab>" +
         "    <tab text='Second tab'>" +
         "    </tab>" +
         "  </panel>" +
         "</window>"
  
  If CatchXML(#Xml, @XML$, StringByteLength(XML$), 0, #XmlEncoding) And XMLStatus(#Xml) = #PB_XML_Success
    
    If CreateDialog(#Dialog) And OpenXMLDialog(#Dialog, #Xml, "test")
      
      BindEvent(#PB_Event_SizeWindow, @OnResize(), DialogWindow(#Dialog), 0)
      BindEvent(#PB_Event_MaximizeWindow, @OnMaximize(), DialogWindow(#Dialog), 0)
      
      DrawCanvas()
      
      Repeat
        Event = WaitWindowEvent()
      Until Event = #PB_Event_CloseWindow 
      
    Else  
      Debug "Dialog error: " + DialogError(#Dialog)
    EndIf
  Else
    Debug "XML error: " + XMLError(#Xml) + " (Line: " + XMLErrorLine(#Xml) + ")"
  EndIf
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: CanvasGadget not redrawing on maximize

Post by RASHAD »

Hi

Code: Select all

      ;BindEvent(#PB_Event_SizeWindow, @OnResize(), DialogWindow(#Dialog), 0)
      ;BindEvent(#PB_Event_MaximizeWindow, @OnMaximize(), DialogWindow(#Dialog), 0)
      BindGadgetEvent(5, @OnResize())
Egypt my love
wombats
Enthusiast
Enthusiast
Posts: 663
Joined: Thu Dec 29, 2011 5:03 pm

Re: CanvasGadget not redrawing on maximize

Post by wombats »

Thanks, but that doesn't work in my actual project for some reason.

I don't understand why it doesn't get redrawn properly on the maximize event. Is the event coming through before the window is actually resized, so the OutputWidth() and OutputHeight() commands in the DrawCanvas() procedure are reporting the old size before the window was maximized?
Last edited by wombats on Fri Feb 23, 2018 6:26 pm, edited 1 time in total.
said
Enthusiast
Enthusiast
Posts: 342
Joined: Thu Apr 14, 2011 6:07 pm

Re: CanvasGadget not redrawing on maximize

Post by said »

User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: CanvasGadget not redrawing on maximize

Post by kenmo »

I think there are 1 or 2 bugs going on...

1. When you resize the dialog, the WindowWidth, GadgetWidth, and OutputWidth are all updated before the callback happens. ***
When you MAXIMIZE the dialog, the WindowWidth is updated, but the GadgetWidth and OutputWidth are "old" values.

Code: Select all

Procedure DrawCanvas()
  ;RefreshDialog(0)
  ;
  If StartDrawing(CanvasOutput(5))
    ;
    Debug ""
    Debug "Win = " + Str(WindowWidth(DialogWindow(0)))
    Debug "Gad = " + Str(GadgetWidth(5))
    Debug "Out = " + Str(OutputWidth())
    ;
    Box(0, 0, OutputWidth(), OutputHeight(), RGB(192, 192, 192))
    StopDrawing()
  EndIf
EndProcedure

XML$ = "<window id='#PB_Any' name='test' text='test' minwidth='auto' minheight='auto' flags='#PB_Window_MaximizeGadget | #PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget'>" +
       "  <panel>" +
       "    <tab text='First tab'>" +
       "      <canvas id='5' width='200' height='100'/>" +
       "    </tab>" +
       "    <tab text='Second tab'>" +
       "    </tab>" +
       "  </panel>" +
       "</window>"

If CatchXML(0, @XML$, StringByteLength(XML$), 0) And XMLStatus(0) = #PB_XML_Success
  If CreateDialog(0) And OpenXMLDialog(0, 0, "test")
    
    BindEvent(#PB_Event_SizeWindow, @DrawCanvas(), DialogWindow(0), 0)
    ;BindGadgetEvent(5, @DrawCanvas(), #PB_EventType_Resize)
    
    DrawCanvas()
    Repeat
      Event = WaitWindowEvent()
    Until Event = #PB_Event_CloseWindow
  EndIf
EndIf
2. So a solution might be to call RefreshDialog() before drawing.
This updates the GadgetWidth and OutputWidth, BUT the dialog loses its maximized state!

Code: Select all

Procedure DrawCanvas()
  ;RefreshDialog(0)
  ...
3. OK, so another solution is Rashad's.
Put a callback on the Canvas resize, not the window resize.
This updates the WindowWidth and GadgetWidth, but not the OutputWidth!

Code: Select all

;BindEvent(#PB_Event_SizeWindow, @DrawCanvas(), DialogWindow(0), 0)
    BindGadgetEvent(5, @DrawCanvas(), #PB_EventType_Resize)
On my system, when I maximize, I get:
Win = 1600
Gad = 1556
Out = 196


So in case (1) the callback happens before the dialog sizes get updated.
In (2) the dialog is manually updated but the maximized state loses.
In (3) the callback has the new CanvasGadget size, but not the new Output drawing size!!


*** EDIT: Actually, when drag-resizing, the callback still has the "old" GadgetWidth and OutputWidth, but it's less noticeable because the difference is usually 1 or 2 pixels.
wombats
Enthusiast
Enthusiast
Posts: 663
Joined: Thu Dec 29, 2011 5:03 pm

Re: CanvasGadget not redrawing on maximize

Post by wombats »

I guess a workaround could be to post a #PB_Event_SizeWindow event to the window when it is maximized, but I still feel this is a bug worth looking into. There are still issues with the output width and height values on the resize event, as noted by kenmo.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: CanvasGadget not redrawing on maximize

Post by RASHAD »

Try

Code: Select all

Procedure DrawCanvas()
  StartDrawing(CanvasOutput(5))
    Debug ""
    Debug "Win = " + Str(WindowWidth(DialogWindow(0)))
    Debug "Gad = " + Str(GadgetWidth(5))
    Debug "Out = " + Str(OutputWidth())
    Box(0, 0, OutputWidth(), OutputHeight(), RGB(0, 0, 255))
  StopDrawing()
EndProcedure
.
.
.
.
     ;BindEvent(#PB_Event_SizeWindow, @OnResize(), DialogWindow(#Dialog), 0)
      BindEvent(#PB_Event_Repaint, @OnMaximize(), DialogWindow(#Dialog), 0)
Egypt my love
wombats
Enthusiast
Enthusiast
Posts: 663
Joined: Thu Dec 29, 2011 5:03 pm

Re: CanvasGadget not redrawing on maximize

Post by wombats »

RASHAD wrote:Try

Code: Select all

Procedure DrawCanvas()
  StartDrawing(CanvasOutput(5))
    Debug ""
    Debug "Win = " + Str(WindowWidth(DialogWindow(0)))
    Debug "Gad = " + Str(GadgetWidth(5))
    Debug "Out = " + Str(OutputWidth())
    Box(0, 0, OutputWidth(), OutputHeight(), RGB(0, 0, 255))
  StopDrawing()
EndProcedure
.
.
.
.
     ;BindEvent(#PB_Event_SizeWindow, @OnResize(), DialogWindow(#Dialog), 0)
      BindEvent(#PB_Event_Repaint, @OnMaximize(), DialogWindow(#Dialog), 0)
Tried that in my project...it's still not working properly. Sometimes the canvas redraws and sometimes (mostly) it doesn't. It's quite frustrating.
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: CanvasGadget not redrawing on maximize

Post by kenmo »

One addition to RASHAD's method seems to fix all cases:

Code: Select all

...
BindGadgetEvent(5, @OnCanvasResize(), #PB_EventType_Resize)
...
add

Code: Select all

...
Procedure OnCanvasResize()
  ResizeGadget(5, #PB_Ignore, #PB_Ignore, #PB_Ignore, #PB_Ignore) ; this seems to update the Output size
  DrawCanvas()
EndProcedure
...
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2056
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: CanvasGadget not redrawing on maximize

Post by Andre »

Moved to 'Bugs-Windows' for further investigation...
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
Fred
Administrator
Administrator
Posts: 16619
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: CanvasGadget not redrawing on maximize

Post by Fred »

You will need to use the PB_EventType_Resize to handle it properly:

Code: Select all

#XmlEncoding = #PB_UTF8

  #Dialog = 0
  #Xml = 0
 
  Procedure DrawCanvas()
    StartDrawing(CanvasOutput(5))
    Box(0, 0, OutputWidth(), OutputHeight(), RGB(0, 0, 255))
    StopDrawing()
  EndProcedure
 
  Procedure OnResize()
    DrawCanvas()
  EndProcedure
 
  Procedure OnMaximize()
    DrawCanvas()
  EndProcedure
 
  XML$ = "<window id='#PB_Any' name='test' text='test' minwidth='auto' minheight='auto' flags='#PB_Window_MaximizeGadget | #PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget'>" +
         "  <panel>" +
         "    <tab text='First tab'>" +
         "      <canvas id='5' width='200' height='100'/>" +
         "    </tab>" +
         "    <tab text='Second tab'>" +
         "    </tab>" +
         "  </panel>" +
         "</window>"
 
  If CatchXML(#Xml, @XML$, StringByteLength(XML$), 0, #XmlEncoding) And XMLStatus(#Xml) = #PB_XML_Success
   
    If CreateDialog(#Dialog) And OpenXMLDialog(#Dialog, #Xml, "test")
     
      BindGadgetEvent(5, @OnMaximize(), #PB_EventType_Resize)
     
      DrawCanvas()
     
      Repeat
        Event = WaitWindowEvent()
      Until Event = #PB_Event_CloseWindow
     
    Else 
      Debug "Dialog error: " + DialogError(#Dialog)
    EndIf
  Else
    Debug "XML error: " + XMLError(#Xml) + " (Line: " + XMLErrorLine(#Xml) + ")"
  EndIf
Post Reply