Is there a way to get gadget num from gadgetid?

Just starting out? Need help? Post your questions and find answers here.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Is there a way to get gadget num from gadgetid?

Post 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.
User avatar
skywalk
Addict
Addict
Posts: 4210
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

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

Post 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.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

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

Post 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()

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
User avatar
JHPJHP
Addict
Addict
Posts: 2250
Joined: Sat Oct 09, 2010 3:47 am

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

Post by JHPJHP »

Hi jassing,

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

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

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

Post 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.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

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

Post 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.
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

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

Post 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
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
Mesa
Enthusiast
Enthusiast
Posts: 433
Joined: Fri Feb 24, 2012 10:19 am

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

Post 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.
calypso
User
User
Posts: 17
Joined: Sun Mar 26, 2023 7:23 am
Location: Vancouver BC Canada

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

Post 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
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

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

Post 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".
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

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

Post by mk-soft »

Short version of Module System

Link: WindowPB, GadgetPB
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