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