Page 1 of 1

CanvasGadgetContainer

Posted: Mon Aug 06, 2012 12:01 pm
by electrochrisso
Josh, posted this on the German forum.
http://www.purebasic.fr/german/viewtopi ... 32#p303932

I modify a bit to add another StringGadget, all additions have ;*** at the end of the line.
Is this the correct way to add more gadgets, it seams to work, should I be using OldWinProc for more than one gadget. :?:

Code: Select all

;CanvasGadgetContainer

Global OldWinProc.i

Procedure WinCallback (hWnd, uMsg, wParam, lParam)
  Static Cursor.i = -1
  Define Tme.TRACKMOUSEEVENT
  
  
  Select uMsg
      
    Case #WM_MOUSEMOVE ;notwendig, wenn kein XP Skin Support
      Tme\cbSize    = SizeOf (TRACKMOUSEEVENT)
      Tme\dwFlags   = #TME_LEAVE
      Tme\hwndTrack = hwnd
      TrackMouseEvent_(Tme)
      
    Case #WM_LBUTTONDBLCLK
      SetFocus_(hWnd)
      
    Case #WM_SETCURSOR
      If PeekW (@lParam) = #HTCLIENT
        If Cursor = -1
          Cursor = GetGadgetAttribute (0, #PB_Canvas_Cursor)
        EndIf
        SetGadgetAttribute (0, #PB_Canvas_Cursor, #PB_Cursor_IBeam)
      EndIf
      
    Case #WM_MOUSELEAVE
      SetGadgetAttribute (0, #PB_Canvas_Cursor, Cursor)
      Cursor = -1
      
  EndSelect
  
  ProcedureReturn CallWindowProc_(OldWinProc ,hWnd ,uMsg, wParam, lParam)
  
EndProcedure

If OpenWindow(0, 0, 0, 220, 220, "CanvasGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  CanvasGadget(0, 10, 10, 200, 200, #PB_Canvas_Keyboard|#PB_Canvas_DrawFocus)
  
  LongPtr = GetWindowLongPtr_(GadgetID(0), #GWL_STYLE)
  SetWindowLongPtr_(GadgetID(0), #GWL_STYLE, LongPtr|#WS_CLIPCHILDREN)
  
  StringGadget (1, 10, 90, 180, 20,  "Test1")
  StringGadget (2, 10, 115, 180, 20, "Test2") ;***
  SetParent_ (GadgetID(1), GadgetID(0))
  SetParent_ (GadgetID(2), GadgetID(0)) ;***
  
  OldWinProc = SetWindowLongPtr_ (GadgetID(1), #GWL_WNDPROC, @WinCallback())
  OldWinProc = SetWindowLongPtr_ (GadgetID(2), #GWL_WNDPROC, @WinCallback()) ;***
  
  Repeat
    Event = WaitWindowEvent()
    
    If Event = #PB_Event_Gadget And EventGadget() = 0
      If EventType() = #PB_EventType_LeftButtonDown Or (EventType() = #PB_EventType_MouseMove And GetGadgetAttribute(0, #PB_Canvas_Buttons) & #PB_Canvas_LeftButton)
        If StartDrawing(CanvasOutput(0))
            x = GetGadgetAttribute(0, #PB_Canvas_MouseX)
            y = GetGadgetAttribute(0, #PB_Canvas_MouseY)
            Circle(x, y, 10, RGB(Random(255), Random(255), Random(255)))
          StopDrawing()
        EndIf
      EndIf
    EndIf   
    
  Until Event = #PB_Event_CloseWindow
EndIf

Re: CanvasGadgetContainer

Posted: Mon Aug 06, 2012 12:16 pm
by RASHAD
Hi :D
What is wrong with simple code ?

Code: Select all

;CanvasGadgetContainer

  OpenWindow(0, 0, 0, 220, 220, "CanvasGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  CanvasGadget(0, 10, 10, 200, 200, #PB_Canvas_Keyboard|#PB_Canvas_DrawFocus)
 
  StringGadget (1, 15, 90, 190, 20,  "Test1")
  StringGadget (2, 15, 115, 190, 20, "Test2") ;***
  SetWindowLongPtr_(GadgetID(0), #GWL_STYLE, GetWindowLongPtr_(GadgetID(0), #GWL_STYLE) | #WS_CLIPSIBLINGS)
  SetWindowPos_(GadgetID(0), #HWND_BOTTOM, -1, -1, -1, -1, #SWP_NOSIZE | #SWP_NOMOVE)
;  
  Repeat
    Select WaitWindowEvent()
    
        Case  #PB_Event_CloseWindow
            Quit = 1
       
        Case  #PB_Event_Gadget
            Select EventGadget()
                Case 0
                    Select EventType()
                       Case #PB_EventType_LeftButtonDown
                            Drag = 1
                            
                       Case #PB_EventType_LeftButtonUp
                            drag = 0                       
                       
                       Case #PB_EventType_MouseMove
                               If Drag = 1
                                StartDrawing(CanvasOutput(0))
                                  x = GetGadgetAttribute(0, #PB_Canvas_MouseX)
                                  y = GetGadgetAttribute(0, #PB_Canvas_MouseY)
                                  Circle(x, y, 10, RGB(Random(255), Random(255), Random(255)))
                                StopDrawing()
                               EndIf
                    EndSelect
                  
                Case 1
                     Debug GetGadgetText(1)
                
                Case 2
                     Debug GetGadgetText(2)
                
            EndSelect
    EndSelect
   
  Until Quit = 1
Edit : Modified for more control over the other gadgets

Re: CanvasGadgetContainer

Posted: Mon Aug 06, 2012 1:56 pm
by Josh
looks good :D

Re: CanvasGadgetContainer

Posted: Mon Aug 06, 2012 4:52 pm
by skywalk
Very clean RASHAD!

Re: CanvasGadgetContainer

Posted: Mon Aug 06, 2012 5:09 pm
by IdeasVacuum
....the time has come for Rashad to write a book on programming Windows with PB. I'm first in the queue to buy it! 8)

Re: CanvasGadgetContainer

Posted: Mon Aug 06, 2012 5:30 pm
by RASHAD
Guys :mrgreen: Thank you very much
And take it easy I am lucky sometimes that is it.

Re: CanvasGadgetContainer

Posted: Mon Aug 06, 2012 8:08 pm
by srod
With Rashad's code, the string gadgets disappear if you mouse down in the canvas.

Here's another take...

Code: Select all

Global OldWinProc.i

Procedure WinCallback (hWnd, uMsg, wParam, lParam)
  Select uMsg
    Case #WM_SETCURSOR
      If wParam = GadgetID(0)
        result = CallWindowProc_(OldWinProc ,hWnd ,uMsg, wParam, lParam)
      EndIf
    Default      
      result = CallWindowProc_(OldWinProc ,hWnd ,uMsg, wParam, lParam)
  EndSelect
  ProcedureReturn result
EndProcedure

If OpenWindow(0, 0, 0, 220, 220, "CanvasGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  CanvasGadget(0, 10, 10, 200, 200, #PB_Canvas_Keyboard|#PB_Canvas_DrawFocus)
  UseGadgetList(GadgetID(0))  
  StringGadget (1, 10, 90, 180, 20,  "Test1")
  StringGadget (2, 10, 115, 180, 20, "Test2") ;***
  SetWindowLongPtr_(GadgetID(0), #GWL_STYLE, GetWindowLongPtr_(GadgetID(0), #GWL_STYLE) | #WS_CLIPCHILDREN)
  
  OldWinProc = SetWindowLongPtr_ (GadgetID(0), #GWL_WNDPROC, @WinCallback()) ;***
  
  Repeat
    Event = WaitWindowEvent()
    
    If Event = #PB_Event_Gadget And EventGadget() = 0
      If EventType() = #PB_EventType_LeftButtonDown Or (EventType() = #PB_EventType_MouseMove And GetGadgetAttribute(0, #PB_Canvas_Buttons) & #PB_Canvas_LeftButton)
        If StartDrawing(CanvasOutput(0))
            x = GetGadgetAttribute(0, #PB_Canvas_MouseX)
            y = GetGadgetAttribute(0, #PB_Canvas_MouseY)
            Circle(x, y, 10, RGB(Random(255), Random(255), Random(255)))
          StopDrawing()
        EndIf
      EndIf
    EndIf   
    
  Until Event = #PB_Event_CloseWindow
EndIf

Re: CanvasGadgetContainer

Posted: Mon Aug 06, 2012 8:24 pm
by RASHAD
Can not reproduce what you are saying srod,Sorry
Always The 2 StringGadgets on Top
PB 4.61 XP x86 SP2 - Win 7 x64

Re: CanvasGadgetContainer

Posted: Mon Aug 06, 2012 8:30 pm
by srod
RASHAD wrote:Can not reproduce what you are saying srod,Sorry
Always The 2 StringGadgets on Top
PB 4.61 XP x86 SP2 - Win 7 x64
Neither can I!!! :D

I must be going nuts!

That is strange. Reckon I must have inadvertently commented out the SetWindowPos_()!

Re: CanvasGadgetContainer

Posted: Tue Aug 07, 2012 2:41 am
by electrochrisso
Gee!, you make it look so easy RASHAD. :wink:
IdeasVacuum wrote:....the time has come for Rashad to write a book on programming Windows with PB. I'm first in the queue to buy it! 8)
I already asked him that, I think on the lines of a reference guide to Tip & Tricks, PB API, I reckon he would have a bit of a seller on his hands, me for one. :)