[Implemented] Canvas background color

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

Re: [Implemented] Canvas background color

Post by mestnyi »

In the windows of the second gadget badly painted over the bottom.

Code: Select all

Procedure CanvasColorCallBack()
  Protected Gadget = EventGadget()
  
  If IsGadget(Gadget) And StartDrawing (CanvasOutput (Gadget))
    ;Box(0, 0, OutputWidth(), OutputHeight(), GetGadgetData(Gadget))
    Box(0, 0, GadgetWidth (Gadget), GadgetHeight (Gadget), GetGadgetData(Gadget))
    StopDrawing()
  EndIf
  
EndProcedure

Procedure SetCanvasColor(Gadget, ColorType, Color)
  If ColorType = #PB_Gadget_BackColor And IsGadget(Gadget) And GadgetType(Gadget) = #PB_GadgetType_Canvas
    SetGadgetData(Gadget, Color) 
    PostEvent(#PB_Event_Gadget, GetActiveWindow(), Gadget, #PB_EventType_Resize)
    BindGadgetEvent(Gadget, @CanvasColorCallBack(), #PB_EventType_Resize)
  Else
    SetGadgetColor(Gadget, ColorType, Color) 
  EndIf
EndProcedure

Macro SetGadgetColor(Gadget, ColorType, Color) : SetCanvasColor(Gadget, ColorType, Color) : EndMacro


OpenWindow (0, 0, 0, 300, 400, "SplitterGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

CanvasGadget   (1, 0, 0,   0,   0) 
CanvasGadget   (2, 0, 0,   0,   0)
SplitterGadget (0, 1, 1, 300, 400, 1, 2)

SetGadgetColor(1, #PB_Gadget_BackColor, #Red)
SetGadgetColor(2, #PB_Gadget_BackColor, #Green)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
But this is how it works smoothly.

Code: Select all

Enumeration #PB_EventType_FirstCustomValue
  #PB_EventType_Size
EndEnumeration

CompilerSelect #PB_Compiler_OS
  CompilerCase #PB_OS_Windows
    Procedure Size_CallBack(hWnd, Msg, wParam, lParam)
      Protected *Func =GetProp_( hWnd, "PB_Size_CallBack") 
      
      If msg = #WM_SIZE
        PostEvent(#PB_Event_Gadget, GetProp_( GetAncestor_( hWnd, #GA_ROOT ), "PB_WindowID")-1, GetProp_( hWnd, "PB_ID") , #PB_EventType_Size)
      EndIf
      
      ProcedureReturn CallWindowProc_(*Func, hWnd, Msg, wParam, lParam)
    EndProcedure
CompilerEndSelect

Procedure SetSizeEvent( Gadget )
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Windows
      SetProp_( GadgetID(Gadget), "PB_Size_CallBack",SetWindowLong_(GadgetID(Gadget), #GWL_WNDPROC, @Size_CallBack()))
  CompilerEndSelect
EndProcedure

Procedure CanvasColorCallBack()
  Protected Gadget = EventGadget()
  
  If IsGadget(Gadget) And StartDrawing (CanvasOutput (Gadget))
    ;Box(0, 0, OutputWidth(), OutputHeight(), GetGadgetData(Gadget))
    Box(0, 0, GadgetWidth (Gadget), GadgetHeight (Gadget), GetGadgetData(Gadget))
    StopDrawing()
  EndIf
  
EndProcedure

Procedure SetCanvasColor(Gadget, ColorType, Color)
  If ColorType = #PB_Gadget_BackColor And IsGadget(Gadget) And GadgetType(Gadget) = #PB_GadgetType_Canvas
    SetSizeEvent( Gadget )
    SetGadgetData(Gadget, Color) 
    PostEvent(#PB_Event_Gadget, GetActiveWindow(), Gadget, #PB_EventType_Size)
    BindGadgetEvent(Gadget, @CanvasColorCallBack(), #PB_EventType_Size)
  Else
    SetGadgetColor(Gadget, ColorType, Color) 
  EndIf
EndProcedure

Macro SetGadgetColor(Gadget, ColorType, Color) : SetCanvasColor(Gadget, ColorType, Color) : EndMacro


OpenWindow (0, 0, 0, 300, 400, "SplitterGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

CanvasGadget   (1, 0, 0,   0,   0) 
CanvasGadget   (2, 0, 0,   0,   0)
SplitterGadget (0, 1, 1, 300, 400, 1, 2)

SetGadgetColor(1, #PB_Gadget_BackColor, #Red)
SetGadgetColor(2, #PB_Gadget_BackColor, #Green)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

Re: [Implemented] Canvas background color

Post by mestnyi »

The first example starts working well if you add this.

Code: Select all

Procedure CanvasColorCallBack()
  Protected Gadget = EventGadget()
  ResizeGadget(Gadget, #PB_Ignore, #PB_Ignore, #PB_Ignore, #PB_Ignore) ; add then work good
          
  If IsGadget(Gadget) And StartDrawing (CanvasOutput (Gadget))
    ;Box(0, 0, OutputWidth(), OutputHeight(), GetGadgetData(Gadget))
    Box(0, 0, GadgetWidth (Gadget), GadgetHeight (Gadget), GetGadgetData(Gadget))
    StopDrawing()
  EndIf
  
EndProcedure
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: [Implemented] Canvas background color

Post by skywalk »

Why does the splitter clear my drawn objects, yet resizing the window does not?

Code: Select all

OpenWindow(0, 0, 0, 300, 600, "SplitterGadget v57b1 background color?", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget)
CanvasGadget(1, 0, 0, 0, 0)
CanvasGadget(2, 0, 0, 0, 0)
SplitterGadget(0, 1, 1, 298, 598, 1, 2)
SetGadgetColor(1, #PB_Gadget_BackColor, #White)
SetGadgetColor(2, #PB_Gadget_BackColor, #Green)
StartDrawing(CanvasOutput(1))
Box(0,0,GadgetWidth(1),GadgetHeight(1),#White)  ; Clear canvasgadget
DrawingMode(#PB_2DDrawing_Transparent)
DrawText(5,75,"Move splitter...Where does my box go? :(", #Blue)
RoundBox(5, 5, 50, 50, 5, 5, #Blue)
StopDrawing()
imgCopy = GetGadgetAttribute(1,#PB_Canvas_Image)
CreateImage(5,GadgetWidth(1),GadgetHeight(1))
StartDrawing(ImageOutput(5))
  DrawImage(imgCopy,0,0)
StopDrawing()
StartDrawing(CanvasOutput(2))
  DrawImage(imgCopy,0,0)
StopDrawing()
;FreeImage(5)
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply