[5.70b1] Resizing Dialog trashes CanvasGadget

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

[5.70b1] Resizing Dialog trashes CanvasGadget

Post by wombats »

This works fine in previous PureBasic versions and in 5.70 on macOS, but not on Windows.

Code: Select all

#XmlEncoding = #PB_UTF8

Runtime Enumeration
  #Dialog
  #CanvasGadget
  #Xml
EndEnumeration

Procedure DrawCanvasGadget()
  Protected blt_img
  If StartDrawing(CanvasOutput(#CanvasGadget))
    blt_img = CreateImage(#PB_Any, OutputWidth(), OutputHeight(), 32, RGB(255, 255, 255))
    StopDrawing()
  EndIf
  If StartDrawing(ImageOutput(blt_img))
    Box(10, 10, 32, 32, RGB(0, 0, 255))
    StopDrawing()
  EndIf
  If StartDrawing(CanvasOutput(#CanvasGadget))
    DrawImage(ImageID(blt_img), 0, 0)
    StopDrawing()
  EndIf
  FreeImage(blt_img)
EndProcedure

Procedure OnDialogResized()
  DrawCanvasGadget()
EndProcedure

XML$ = "<window id='#PB_Any' name='test' text='test' minwidth='auto' minheight='auto' flags='#PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget'>" +
       "  <canvas id='#CanvasGadget' width='350' height='300' flags='#PB_Canvas_Border'/>" +
       "</window>"

If ParseXML(#Xml, XML$)
  If CreateDialog(#Dialog) And OpenXMLDialog(#Dialog, #Xml, "test")
    
    DrawCanvasGadget()
    
    BindEvent(#PB_Event_SizeWindow, @OnDialogResized(), DialogWindow(#Dialog))
    
    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
Cyllceaux
Enthusiast
Enthusiast
Posts: 465
Joined: Mon Jun 23, 2014 1:18 pm
Contact:

Re: [5.70b1] Resizing Dialog trashes CanvasGadget

Post by Cyllceaux »

Works fine for me:

Win10 x64 on 5.70 LTS Beta 1
wombats
Enthusiast
Enthusiast
Posts: 664
Joined: Thu Dec 29, 2011 5:03 pm

Re: [5.70b1] Resizing Dialog trashes CanvasGadget

Post by wombats »

Cyllceaux wrote:Works fine for me:

Win10 x64 on 5.70 LTS Beta 1
I wish I knew why it's not working for me, then. We're on the same OS and version of PB.
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: [5.70b1] Resizing Dialog trashes CanvasGadget

Post by #NULL »

Code: Select all

blt_img = CreateImage(#PB_Any, OutputWidth(), OutputHeight()
Did you check about blt_img/IsImage()/OutputWidth()/OutputHeight()?
Fred
Administrator
Administrator
Posts: 16681
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: [5.70b1] Resizing Dialog trashes CanvasGadget

Post by Fred »

You will need to use instead, as the canvas is cleared after the window resize

Code: Select all

#XmlEncoding = #PB_UTF8

Runtime Enumeration
  #Dialog
  #CanvasGadget
  #Xml
EndEnumeration

Procedure DrawCanvasGadget()
  Protected blt_img
  If StartDrawing(CanvasOutput(#CanvasGadget))
    blt_img = CreateImage(#PB_Any, OutputWidth(), OutputHeight(), 32, RGB(255, 255, 255))
    StopDrawing()
  EndIf
  If StartDrawing(ImageOutput(blt_img))
    Box(10, 10, 32, 32, RGB(0, 0, 255))
    StopDrawing()
  EndIf
  If StartDrawing(CanvasOutput(#CanvasGadget))
    DrawImage(ImageID(blt_img), 0, 0)
    StopDrawing()
  EndIf
  FreeImage(blt_img)
EndProcedure

Procedure OnDialogResized()
  DrawCanvasGadget()
EndProcedure

XML$ = "<window id='#PB_Any' name='test' text='test' minwidth='auto' minheight='auto' flags='#PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget'>" +
       "  <canvas id='#CanvasGadget' width='350' height='300' flags='#PB_Canvas_Border'/>" +
       "</window>"

If ParseXML(#Xml, XML$)
  If CreateDialog(#Dialog) And OpenXMLDialog(#Dialog, #Xml, "test")
   
    DrawCanvasGadget()
   
    BindGadgetEvent(#CanvasGadget, @OnDialogResized(), #PB_EventType_Resize)
   
    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
User avatar
skywalk
Addict
Addict
Posts: 3997
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: [5.70b1] Resizing Dialog trashes CanvasGadget

Post by skywalk »

Hi Fred,
The manual says canvas is persistent?
But, the request for background color for canvas introduced this clearing and automatic paint of background.
I have a large canvas on a scrollarea gadget, and now it is much slower to always repaint. :(
Any chance to make this optional for those who do not want automatic background changes?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Fred
Administrator
Administrator
Posts: 16681
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: [5.70b1] Resizing Dialog trashes CanvasGadget

Post by Fred »

An option could be the way. You don't want the old picture being invalidated on size right ? So extra space will be filled with white ?
User avatar
skywalk
Addict
Addict
Posts: 3997
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: [5.70b1] Resizing Dialog trashes CanvasGadget

Post by skywalk »

Yes, that should help responsiveness. :D
In my case, my canvas was being repainted with backcolor when small splittergadget moves that never clipped the canvas.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
mk-soft
Always Here
Always Here
Posts: 5398
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: [5.70b1] Resizing Dialog trashes CanvasGadget

Post by mk-soft »

Another possibility is to pack the CanvasGadget into a container or scrollarea beforehand.
But there is a problem with the gadgets in the container of CanvasGadget. They are not always redrawn cleanly by the Windows OS...

Code: Select all

;-TOP

; Workaround MacOS PB v5.60 - v5.62. Canvas Gadget Container Bugfix

; Link https://www.purebasic.fr/english/viewtopic.php?f=24&t=71269

; -----------------------------------------------------------------------------

DeclareModule __Workaround
  Declare FixResizeGadget(Gadget, x, y, width, height)
EndDeclareModule

Module __Workaround
  Procedure FixResizeGadget(Gadget, x, y, width, height)
    ResizeGadget(Gadget, x, y, width, height)
    CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
      ; Bugfix gadget container from canvasgadget
      Protected rect.NSRect, sv, container
      If GadgetType(Gadget) <> #PB_GadgetType_Canvas
        ProcedureReturn 1
      EndIf
      sv = CocoaMessage(0, GadgetID(Gadget), "subviews")
      If CocoaMessage(0, sv, "count")
        container = CocoaMessage(0, sv, "objectAtIndex:", 0)
        If container
          CocoaMessage(@rect, GadgetID(Gadget), "frame")
          rect\origin\x = 0
          rect\origin\y = 0
          CocoaMessage(0, container, "setFrame:@", @rect)
        EndIf
      EndIf
    CompilerEndIf
  EndProcedure
EndModule

DeclareModule Workaround
  CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
    Macro ResizeGadget(Gadget, x, y, width, height)
      __Workaround::FixResizeGadget(Gadget, x, y, width, height)
    EndMacro
  CompilerEndIf
EndDeclareModule

Module Workaround
EndModule

; -----------------------------------------------------------------------------

CompilerIf #PB_Compiler_IsMainFile
  
  UseModule Workaround
  
  ; ---------------------------------------------------------------------------
  
  Procedure RedrawCanvas()
    ResizeGadget(0, #PB_Ignore, #PB_Ignore, #PB_Ignore, #PB_Ignore)
    StartDrawing(CanvasOutput(0))
    Box(0, 0, GadgetWidth(0), GadgetHeight(0), $FF0000)
    Box(2, 2, GadgetWidth(0) - 4, GadgetHeight(0) - 4, $B48246)
    StopDrawing()
  EndProcedure
  
  Procedure RedrawCanvas3()
    ;ResizeGadget(5, #PB_Ignore, #PB_Ignore, #PB_Ignore, #PB_Ignore)
  EndProcedure
  
  Procedure SizeWindow()
    ResizeGadget(4, 10, 10, WindowWidth(0) - 20, WindowHeight(0) - 20)
  EndProcedure
  
  ; ---------------------------------------------------------------------------
  
  Define Event
  
  If OpenWindow(0, 0, 0, 220, 220, "Canvas Container Workaround", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget)
    
    CanvasGadget(0, 0, 0, 200, 200, #PB_Canvas_Container)
    ButtonGadget(1, 10, 10, 80, 30, "Button")
    CloseGadgetList()
      
    ScrollAreaGadget(2, 0, 0, 400, 400, 400, 400); #PB_Container_BorderLess)
    CanvasGadget(3, 0, 0, 400, 400, #PB_Canvas_Container)
    ButtonGadget(5, 10, 10, 80, 30, "Button")
    CloseGadgetList()
    CloseGadgetList()
    
    StartDrawing(CanvasOutput(3))
    Define x, y
    y = 0
    For x = 0 To 195 Step 10
      Box(x, y, 400-2*x, 400-2*y, RGB(Random(255), Random(255), Random(255)))
      y + 10        ; the same as y = y + 10
    Next x
    StopDrawing()
    
    SplitterGadget(4, 10, 10, 200, 200, 2, 0);, #PB_Splitter_Separator)
    
    RedrawCanvas()
    
    BindEvent(#PB_Event_SizeWindow, @SizeWindow(), 0)
    
    BindGadgetEvent(0, @RedrawCanvas(), #PB_EventType_Resize)
    BindGadgetEvent(2, @RedrawCanvas3(), #PB_EventType_Resize)
    
    Repeat
      Event = WaitWindowEvent()
      
    Until Event = #PB_Event_CloseWindow
    
  EndIf
  
CompilerEndIf
[/size]
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply