Page 1 of 1

Is there a way to get gadget num from gadgetid?

Posted: Fri Apr 07, 2023 8:52 pm
by jassing
GadgetNum( gadgetID )
ie:
A reverse of GadgetID( num ) ? (Either generically, or windows specific) Not a big of a deal, just trying to make my code as readable as possible.

Re: Is there a way to get gadget num from gadgetid?

Posted: Fri Apr 07, 2023 9:06 pm
by skywalk
I use #PB_Any on creation and keep track dynamically of both gadNumber & gadHandle in a structured array.
If you use #MY_GADGET_NUMBER, you could still do the same.

Re: Is there a way to get gadget num from gadgetid?

Posted: Fri Apr 07, 2023 9:15 pm
by mk-soft
Use Enumeration for gadget constants and not #PB_Any

Base example:

Code: Select all

;-TOP

#ProgramTitle = "Main Window"
#ProgramVersion = "v1.01.2"

Enumeration Windows
  #Main
EndEnumeration

Enumeration MenuBar
  #MainMenu
EndEnumeration

Enumeration MenuItems
  #MainMenuAbout
  #MainMenuExit
EndEnumeration

Enumeration Gadgets
  #MainEdit
  #MainButtonOk
  #MainButtonCancel
EndEnumeration

Enumeration StatusBar
  #MainStatusBar
EndEnumeration

Procedure UpdateWindow()
  Protected dx, dy
  dx = WindowWidth(#Main)
  dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
  ; Resize gadgets
  ResizeGadget(#MainEdit, 5, 5, dx - 10, dy - 45)
  ResizeGadget(#MainButtonok, 10, dy - 35, 120, 30)
  ResizeGadget(#MainButtonCancel, dx - 130, dy - 35, 120, 30)
EndProcedure

Procedure Main()
  Protected dx, dy
  
  #MainStyle = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
  
  If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, 800, 600, #ProgramTitle , #MainStyle)
    ; Menu
    CreateMenu(#MainMenu, WindowID(#Main))
    MenuTitle("&File")
    CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
      MenuItem(#PB_Menu_About, "")
    CompilerElse
      MenuItem(#MainMenuAbout, "About")
    CompilerEndIf
    ; Menu File Items
    
    CompilerIf Not #PB_Compiler_OS = #PB_OS_MacOS
      MenuBar()
      MenuItem(#MainMenuExit, "E&xit")
    CompilerEndIf
    
    ; StatusBar
    CreateStatusBar(#MainStatusBar, WindowID(#Main))
    AddStatusBarField(#PB_Ignore)
    
    ; Gadgets
    dx = WindowWidth(#Main)
    dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
    EditorGadget(#MainEdit, 5, 5, dx -10, dy - 45)
    ButtonGadget(#MainButtonok, 10, dy - 35, 120, 30, "Ok")
    ButtonGadget(#MainButtonCancel, dx - 130, dy - 35, 120, 30, "Abbruch")
    
    ; Bind Events
    BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), #Main)
    
    ; Event Loop
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Select EventWindow()
            Case #Main
              Break
              
          EndSelect
          
        Case #PB_Event_Menu
          Select EventMenu()
            CompilerIf #PB_Compiler_OS = #PB_OS_MacOS   
              Case #PB_Menu_About
                PostEvent(#PB_Event_Menu, #Main, #MainMenuAbout)
                
              Case #PB_Menu_Preferences
                
              Case #PB_Menu_Quit
                PostEvent(#PB_Event_CloseWindow, #Main, #Null)
                
            CompilerEndIf
            
          Case #MainMenuAbout
            MessageRequester("About", #ProgramTitle + #LF$ + #ProgramVersion, #PB_MessageRequester_Info)
              
          Case #MainMenuExit
            PostEvent(#PB_Event_CloseWindow, #Main, #Null)
            
          EndSelect
          
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #MainEdit
              Select EventType()
                Case #PB_EventType_Change
                  ;
                  
              EndSelect
              
            Case #MainButtonOk
              ;
            Case #MainButtonCancel
              ;
              
          EndSelect
          
      EndSelect
    ForEver
    
  EndIf
  
EndProcedure : Main()


Re: Is there a way to get gadget num from gadgetid?

Posted: Fri Apr 07, 2023 10:06 pm
by JHPJHP
Hi jassing,

I've used the following (posted by hallodri) with success (Windows only): viewtopic.php?p=201822&sid=d348bc8a3d8a ... f7#p201822

Re: Is there a way to get gadget num from gadgetid?

Posted: Fri Apr 07, 2023 10:14 pm
by jassing
All good ideas, but unfortunately, none of them result in a GadgetNum() type result...
Windows API returns a gadgetID / windowsID, not a gadgetnum, or windowsnum, I'm trying to result back w/o enumerating gadgets & comparing gadgetid()

Thank you for your time.

Re: Is there a way to get gadget num from gadgetid?

Posted: Fri Apr 07, 2023 10:16 pm
by jassing
JHPJHP wrote: Fri Apr 07, 2023 10:06 pm Hi jassing,

I've used the following (posted by hallodri) with success (Windows only): viewtopic.php?p=201822&sid=d348bc8a3d8a ... f7#p201822
PERFECT! Thank you!!!

Code: Select all

Macro WindowsNum(w) : GetProp_(w,"PB_WindowID")-1 : EndMacro
Macro GadgetNum(g)  : GetProp_(g,"PB_ID")       : EndMacro
; or
Macro GadgetNum(g) : GetDlgCtrlID_(g)          : EndMacro 

The GetDlgCtrlID() is substantially faster than GetPro_() if called a ridiculous amount of times. But for occasional use, I think it makes no difference.

Re: Is there a way to get gadget num from gadgetid?

Posted: Sat Apr 08, 2023 1:38 am
by mk-soft
I don't understand what you are trying to achieve.
The system handle/object is only needed to make OS specific enhancements. For PB management, the PB id's are enough to do everything else. Either by creating with constants or by creating with #PB_Any. With #PB_Any you get PB id's that are dynamic and have nothing to do with the OS specific handle/object.

Please read the help topics of Purebasic here

Various Topics
- PureBasic objects overview
- Handles and Numbers

Re: Is there a way to get gadget num from gadgetid?

Posted: Sat Apr 08, 2023 8:26 am
by Mesa
@jassing: i think that you are looking for is in the mksoft's multiplatform system module here
viewtopic.php?f=12&t=72980

M.

Re: Is there a way to get gadget num from gadgetid?

Posted: Sat Apr 08, 2023 12:00 pm
by calypso
I'm fairly new to PB but in a running program If I need to get a gadget number and put it into an array I simply

Array_Buttons(Row,Col) = #Gadget_Form1_Button_Row14_Col4

or into a variable

x.i = #Gadget_Launch_VNC_Form1_Button_Row14_Col4

Re: Is there a way to get gadget num from gadgetid?

Posted: Sat Apr 08, 2023 2:13 pm
by jassing
Some windows API's return a window handle (or "WindowID"). If you created a window
OpenWindow(1, .....)

You aren't going to normally 'save' the resulting WindowID. If you happen to call an API that returns a handle, it's "cleaner" (IMHO) to have a function that is "WindowNum( 33943940202 )" to get that it's window #1 vs saving global variables or looping thru available windows to use WindowID() to compare. This is more true if you are creating generic procedures/modules you intend to reuse in other projects.

Sure, I could have
WindowHandle1 = openwindow(1, ....) and then re-use that handle later for comparison, but if the module is generic, it won't know what variable you used at the time, and would have to cycle thru the wndows...

It's a style preference vs "a problem".

Re: Is there a way to get gadget num from gadgetid?

Posted: Sat Apr 08, 2023 4:03 pm
by mk-soft
Short version of Module System

Link: WindowPB, GadgetPB