PB-constants

Just starting out? Need help? Post your questions and find answers here.
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

PB-constants

Post by mestnyi »

Why is such a mess in the numbering of events interesting?

Code: Select all

    Debug #PB_EventType_LeftClick        ; 0    
    Debug #PB_EventType_RightClick       ; 1
    Debug #PB_EventType_LeftDoubleClick  ; 2    
    Debug #PB_EventType_RightDoubleClick ; 3
    Debug #PB_EventType_Up               ; 4
    Debug #PB_EventType_Down             ; 5
    Debug #PB_EventType_Resize           ; 6
    
    Debug #PB_EventType_Focus            ; 256
    Debug #PB_EventType_LostFocus        ; 512
    Debug #PB_EventType_Change           ; 768
    Debug #PB_EventType_DragStart        ; 2048

    Debug #PB_EventType_StatusChange     ; 65518
    Debug #PB_EventType_TitleChange      ; 65517
                                         ; ?
                                         ; ?
                                         ; ?
                                         ; ?
                                         ; ?
                                         ; ?
    Debug #PB_EventType_SizeItem         ; 65534
    Debug #PB_EventType_CloseItem        ; 65535
                                         ; ?
    Debug #PB_EventType_MouseEnter       ; 65537 
    Debug #PB_EventType_MouseLeave       ; 65538
    Debug #PB_EventType_MouseMove        ; 65539 
    Debug #PB_EventType_LeftButtonDown   ; 65540 
    Debug #PB_EventType_LeftButtonUp     ; 65541 
    Debug #PB_EventType_RightButtonDown  ; 65542
    Debug #PB_EventType_RightButtonUp    ; 65543
    Debug #PB_EventType_MiddleButtonDown ; 65544
    Debug #PB_EventType_MiddleButtonUp   ; 65545
    Debug #PB_EventType_MouseWheel       ; 65546
    Debug #PB_EventType_KeyDown          ; 65547
    Debug #PB_EventType_KeyUp            ; 65548
    Debug #PB_EventType_Input            ; 65549
    
    
    
// Moved from "Off Topic" to "Coding Questions" (Kiffi)
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: PB-constants

Post by jacdelad »

Why is this important? It doesn't matter which value is behind each constant. Fred could even change it with every version and it wouldn't matter, if you always use the constants for their intended use.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Fred
Administrator
Administrator
Posts: 16616
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: PB-constants

Post by Fred »

Exactly. It sometimes maps to OS values for ease of use.
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

Re: PB-constants

Post by mestnyi »

jacdelad wrote: Fri Feb 03, 2023 10:15 pm It doesn't matter which value is behind each constant. Fred could even change it with every version and it wouldn't matter, if you always use the constants for their intended use.
I agree with you, but there is one thing but if the numbering was ok, it wouldn't hurt what you're saying, right?
But it will make life much easier for those who will use it. i mean this.
jacdelad wrote: Fri Feb 03, 2023 10:15 pm Why is this important?
Like I said, I'm writing a GUI for purebasic because I really like it. So that the library can be ported with minimal changes.

For example, if there was an order in the pb constants, I could write like this.

Code: Select all

Procedure Bind( *this._S_widget, *callback, eventtype.l = #PB_All, item.l = #PB_All )
  If eventtype >= 0
    *this\event\type[eventtype] = #True
  EndIf
EndProcedure

Procedure DoEvents( *this._S_widget, eventtype.l, *eventdata )
  If *this\event\type[eventtype]
    Post( *this, eventtype, #PB_All, #Null )
  EndIf
EndProcedure
User avatar
mk-soft
Always Here
Always Here
Posts: 5333
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: PB-constants

Post by mk-soft »

With BindEvent and BindGadgetEvent do PureBasic already offers everything you need for your own controls.
Here is a small example with a CanvasGadget

Code: Select all


;-TOP

Structure udtMyCanvasData
  Text.s
  x.i
  y.i
EndStructure

Procedure DrawCanvasGadget(*Data.udtMyCanvasData)
  With *Data
    If *Data
      If StartDrawing(CanvasOutput(Gadget))
        Box(0, 0, GadgetWidth(Gadget), GadgetHeight(Gadget), $0045FF)
        DrawingMode(#PB_2DDrawing_Transparent)
        DrawText(\x, \y, \Text)
        StopDrawing()
      EndIf
    EndIf
  EndWith
EndProcedure

Procedure DoCanvasEvents()
  Protected Gadget = EventGadget()
  Protected *Data.udtMyCanvasData = GetGadgetData(Gadget)
  
  With *Data
    If *Data
      Select EventType()
        Case #PB_EventType_LeftClick
          \x = GetGadgetAttribute(Gadget, #PB_Canvas_MouseX)
          \y = GetGadgetAttribute(Gadget, #PB_Canvas_MouseY)
          DrawCanvasGadget(*Data)
          
        Case #PB_EventType_Resize
          DrawCanvasGadget(*Data)
          
      EndSelect
    EndIf
    
  EndWith
EndProcedure

Procedure SetCanvasData(Gadget, Text.s = "", x = 10, y = 10)
  Protected *Data.udtMyCanvasData
  *Data = AllocateStructure(udtMyCanvasData)
  If *Data
    *Data\Text = Text
    *Data\x = x
    *Data\y = y
    DrawCanvasGadget(*Data)
    SetGadgetData(Gadget, *Data)
    BindGadgetEvent(Gadget, @DoCanvasEvents())
  EndIf
  ProcedureReturn *Data
EndProcedure

Procedure FreeCanvasData(Gadget)
  Protected *Data.udtMyCanvasData
  *Data = GetGadgetData(Gadget)
  If *Data
    UnbindGadgetEvent(Gadget, @DoCanvasEvents())
    SetGadgetData(Gadget, 0)
    FreeStructure(*Data)
  EndIf
EndProcedure

; ----

Procedure UpdateWindow()
  Protected dx, dy
  dx = WindowWidth(0)
  dy = WindowHeight(0) - StatusBarHeight(0) - MenuHeight()
  ; Resize Gadgets
  ResizeGadget(0, 0, 0, dx, dy)
EndProcedure

Procedure Main()
  Protected dx, dy
  Protected *MyData.udtMyCanvasData
  
  #WinStyle = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
  
  If OpenWindow(0, #PB_Ignore, #PB_Ignore, 600, 400, "Test Window", #WinStyle)
    ; MenuBar
    CreateMenu(0, WindowID(0))
    MenuTitle("File")
    
    ; StatusBar
    CreateStatusBar(0, WindowID(0))
    AddStatusBarField(#PB_Ignore)
    
    ; Gadgets
    dx = WindowWidth(0)
    dy = WindowHeight(0) - StatusBarHeight(0) - MenuHeight()
    CanvasGadget(0, 0, 0, dx, dy, #PB_Canvas_Keyboard)
    SetCanvasData(0, "Hello World!")
    
    ; Bind Events
    BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), 0)
    
    ; Main Loop
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Select EventWindow()
            Case 0
              Break
          EndSelect
          
        Case #PB_Event_Menu
          Select EventMenu()
            
          EndSelect
          
        Case #PB_Event_Gadget
          Select EventGadget()
              
          EndSelect
          
      EndSelect
    ForEver
    
  EndIf
  
  FreeCanvasData(0)
  
EndProcedure : Main()

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
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

Re: PB-constants

Post by mestnyi »

mk-soft wrote: Sat Feb 04, 2023 3:06 pm With BindEvent and BindGadgetEvent do PureBasic already offers everything you need for your own controls.
Here is a small example with a CanvasGadget
:shock: And what does this have to do with my problem? Don't I know it?
User avatar
mk-soft
Always Here
Always Here
Posts: 5333
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: PB-constants

Post by mk-soft »

Just wanted to show another way of binding events.

Possibly via maps that assign the event types ...
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