by the handle get gadget ident

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

by the handle get gadget ident

Post by mestnyi »

Code: Select all

CompilerSelect #PB_Compiler_OS 
  CompilerCase #PB_OS_MacOS
    Import ""
      PB_Window_GetID(hWnd) 
    EndImport
CompilerEndSelect

Procedure IDWindow( handle.i ) ; Return the id of the window from the window handle
  Protected Window 
  
  CompilerSelect #PB_Compiler_OS 
    CompilerCase #PB_OS_Linux
      Window = g_object_get_data_( handle, "pb_id" )
    CompilerCase #PB_OS_Windows
      Window = GetProp_( handle, "PB_WindowID" ) - 1
    CompilerCase #PB_OS_MacOS
      Window = PB_Window_GetID( handle )
  CompilerEndSelect
  
  If IsWindow( Window ) And 
     WindowID( Window ) = handle
    ProcedureReturn Window
  Else
    ProcedureReturn - 1
  EndIf
EndProcedure

Procedure IDgadget( handle.i )  ; Return the id of the gadget from the gadget handle
  Protected gadget
  
  CompilerSelect #PB_Compiler_OS 
    CompilerCase #PB_OS_Linux
      gadget = g_object_get_data_( handle, "pb_id" ) - 1 
    CompilerCase #PB_OS_Windows
      gadget = GetProp_( handle, "PB_ID" )
    CompilerCase #PB_OS_MacOS
      gadget = CocoaMessage( 0, handle, "tag" )
  CompilerEndSelect
  
  If IsGadget( gadget ) And 
     GadgetID( gadget ) = handle
    ProcedureReturn gadget
  Else
    ProcedureReturn - 1
  EndIf
EndProcedure
Last edited by mestnyi on Thu Mar 03, 2022 7:03 pm, edited 1 time in total.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: by the handle is a gadget

Post by blueznl »

I guess in those cases where the language might pose a problem you might consider specifying your question in English as well as in your native tongue.

I, for one, haven't got a clue what you are talking about 8)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: by the handle is a gadget

Post by srod »

Aye, double Dutch with a wee bit of Swahili thrown in! I cannot even guess what the question is to be able to provide any semblance of an answer!
I may look like a mule, but I'm not a complete ass.
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: by the handle is a gadget

Post by Demivec »

Is your question 'Allow functions to use a gadget's number or its handle (i.e. IsGadget () for example)'?

Give an example of how you would need to use it in PureBasic code.
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: by the handle is a gadget

Post by Danilo »

He wants to know from a handle if it is a PureBasic Gadget.
Like IsGadget(number) but with a handle, probably the hWnd.
gadget = IsGadgetForRooms(hWndListView)
:wink:

Similar to ImageFromImageID()

EDIT: Something like this on Windows (untested, written on Mac :D)

Code: Select all

#GWLP_ID = -12
Procedure GadgetFromHwnd(hWnd)
    Protected id = GetWindowLongPtr_(hWnd, #GWLP_ID)
    If IsGadget(id) And GadgetID(id)=hWnd
        ProcedureReturn id ; return gadget number
    EndIf
    ProcedureReturn -1 ; no PB gadget
EndIf
mestnyi
Addict
Addict
Posts: 1098
Joined: Mon Nov 25, 2013 6:41 am

Re: by the handle is a gadget

Post by mestnyi »

Code: Select all

ProcedureDLL GetGadgetParent(Gadget.i) ;В()
 CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  ProcedureReturn GetParent_(Gadget)
 CompilerElseIf #PB_Compiler_OS = #PB_OS_Linux
  ProcedureReturn Gtk_Widget_Get_Parent_(Gadget)
 CompilerEndIf  
EndProcedure


win1 = OpenWindow(1,0,0,400,300,"Test", #PB_Window_SystemMenu|#PB_Window_ScreenCentered| #PB_Window_SizeGadget)
    Debug WindowID(1)
    
    ContainerGadget(5,10,220,62,22,#PB_Container_Flat )
    Debug GadgetID(5)
     but=ButtonGadget(1,5,5,111,33,"")
    CloseGadgetList()
    
    Debug GetGadgetParent(but)
    
    Debug  IsGadget(GetGadgetParent(but))
    
    
      Repeat
    Select WaitWindowEvent()
         
   Case #PB_Event_Gadget
     ;Debug gadget
     ;Debug EventGadget()
          Case #PB_Event_CloseWindow
                Quit = 1     
               
      EndSelect

    Until Quit = 1
    End
so I think it will be clear (cross platform solution needed) (так думаю будет понятно (нужно кроссплатформенное решение))
mestnyi
Addict
Addict
Posts: 1098
Joined: Mon Nov 25, 2013 6:41 am

Re: by the handle is a gadget

Post by mestnyi »

Code: Select all

Procedure GadgetFromHwnd(hWnd)
        ;Protected id = GetWindowLongPtr_(hWnd, #GWLP_ID)
        Protected id = GetDlgCtrlID_(hWnd)
        If IsGadget(id) And GadgetID(id)=hWnd
            ProcedureReturn id ; return gadget number
        EndIf
        ProcedureReturn -1 ; no PB gadget

      EndProcedure  
     
the same thing but only for Linux needed (тоже самое но только для линукс нужен)
mestnyi
Addict
Addict
Posts: 1098
Joined: Mon Nov 25, 2013 6:41 am

Re: by the handle is a gadget

Post by mestnyi »

Is there no way to get in Linux "pb_id" with "gadgetid"
mestnyi
Addict
Addict
Posts: 1098
Joined: Mon Nov 25, 2013 6:41 am

Re: by the handle is a gadget

Post by mestnyi »

In windows for three functions

Code: Select all

id = GetWindowLongPtr_(hWnd, #GWLP_ID)
id = GetDlgCtrlID_(hWnd)
id = GetProp_(hWnd,"PB_ID")
in Linux and no what? It does not happen, well, somebody help
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: by the handle is a gadget

Post by idle »

This may help

Code: Select all

Procedure SetOSGadgetID(gadget)  
   CompilerIf #PB_Compiler_OS = #PB_OS_Linux   
       g_object_set_data_(GadgetID(gadget),"OSGadgetID",Gadget))   
   CompilerElseIf #PB_Compiler_OS = #PB_OS_Windows
       SetProp_(GadgetID(gadget),"OSGadgetID",Gadget) 
   CompilerEndIf   
EndProcedure    
 
Procedure GetGadgetID(OSGadgetID)  
   CompilerIf #PB_Compiler_OS = #PB_OS_Linux   
      ProcedureReturn  g_object_get_data_(OSGadgetID,"OSGadgetID")
   CompilerElseIf #PB_Compiler_OS = #PB_OS_Windows
      ProcedureReturn GetProp_(OSGadgetID,"OSGadgetID")
   CompilerEndIf    
EndProcedure    

;   but=ButtonGadget(1,5,5,111,33,"hello") 
;   SetOSGadgetID(1)  ;set with gadget number  
;   GetGadgetID(hwnd) ;get gadget number from GadgetID  

Windows 11, Manjaro, Raspberry Pi OS
Image
mestnyi
Addict
Addict
Posts: 1098
Joined: Mon Nov 25, 2013 6:41 am

Re: by the handle is a gadget

Post by mestnyi »

this as SetGadgetData () and GetGadgetData () I only need to read the key and I do not know what Fred put (g_object_get_data_ (GadgetID, "PB_ID")) is not suitable
User avatar
Bisonte
Addict
Addict
Posts: 1305
Joined: Tue Oct 09, 2007 2:15 am

Re: by the handle is a gadget

Post by Bisonte »

Danilo wrote:Similar to ImageFromImageID()
Read Danilo's post and follow the link....

Or try this on Linux :

Code: Select all

Import ""
    PB_Object_EnumerateAll(*object,*callback,*d)
    PB_Object_EnumerateStart(*object)
    PB_Object_EnumerateNext(*object,*id.Integer)
    PB_Object_EnumerateAbort(*object)
    PB_Object_Count(*object)
EndImport

Procedure GadgetFromGadgetID(GadgetID)
    Protected id, Gadget_Objects, result = -1
    CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
        !extrn PB_Gadget_Objects
        !PUSH  qword [PB_Gadget_Objects]
        !POP   qword [p.v_Gadget_Objects]
    CompilerElse
        !extrn PB_Gadget_Objects
        !PUSH  dword [PB_Gadget_Objects]
        !POP   dword [p.v_Gadget_Objects]
    CompilerEndIf

    PB_Object_EnumerateStart(Gadget_Objects)
    While PB_Object_EnumerateNext(Gadget_Objects,@id)
        If GadgetID = GadgetID(id)
            result = id
            PB_Object_EnumerateAbort(Gadget_Objects)
            Break
        EndIf
    Wend
    ProcedureReturn result
EndProcedure


OpenWindow(10, 100, 100, 500, 300, "testwindow")
ButtonGadget(22, 10, 10, 100, 20 , "test")

Debug GadgetFromGadgetID(GadgetID(22))

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
I think this works (If I understand Danilo)
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: by the handle is a gadget

Post by ts-soft »

The same, shorter, without asm and with some correction:

Code: Select all

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
Import ""
CompilerElse
ImportC ""
CompilerEndIf
  PB_Object_EnumerateStart(*object)
  PB_Object_EnumerateNext(*object,*id.Integer)
  PB_Object_EnumerateAbort(*object)
  PB_Gadget_Objects.i
EndImport
  
Procedure GetGadgetID(hWnd.i)
  Protected id
  
  PB_Object_EnumerateStart(PB_Gadget_Objects)
  While PB_Object_EnumerateNext(PB_Gadget_Objects, @id)
    If hWnd = GadgetID(id)
      PB_Object_EnumerateAbort(PB_Gadget_Objects)
      ProcedureReturn id
    EndIf
  Wend
  ProcedureReturn -1
EndProcedure
  
  
OpenWindow(10, 100, 100, 500, 300, "testwindow")
ButtonGadget(22, 10, 10, 100, 20 , "test")

Debug GetGadgetID(GadgetID(22))

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow   

  
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
mestnyi
Addict
Addict
Posts: 1098
Joined: Mon Nov 25, 2013 6:41 am

Re: by the handle is a gadget

Post by mestnyi »

I understand this is a special purebasic function, and I thought that means OS as it could do as much or as one problem is solved, thank you. .
User avatar
Bisonte
Addict
Addict
Posts: 1305
Joined: Tue Oct 09, 2007 2:15 am

Re: by the handle is a gadget

Post by Bisonte »

With ts-soft's start, I added Window, Gadget and Image.... (I only tested this with Windows)

Code: Select all

DeclareModule PB
  
  Enumeration
    #PB_ID_Window 
    #PB_ID_Gadget
    #PB_ID_Image
  EndEnumeration
  
  CompilerIf #PB_Compiler_OS = #PB_OS_Windows
    Import ""
  CompilerElse
    ImportC ""
  CompilerEndIf  
    PB_Object_EnumerateStart(*object)
    PB_Object_EnumerateNext(*object,*id.Integer)
    PB_Object_EnumerateAbort(*object)
    PB_Gadget_Objects.i
    PB_Image_Objects.i
    PB_Window_Objects.i
  EndImport
  
  Declare PBID(OS_Handle, Type = #PB_ID_Window)
    
EndDeclareModule
Module        PB
  
  EnableExplicit
  
  Procedure PBID(OS_Handle, Type = #PB_ID_Window)
    
    Protected ID, Objects, Result = -1
    
    If OS_Handle
      
      Select Type
        Case #PB_ID_Gadget : Objects = PB_Gadget_Objects
        Case #PB_ID_Image  : Objects = PB_Image_Objects
        Case #PB_ID_Window : Objects = PB_Window_Objects
      EndSelect
      
      PB_Object_EnumerateStart(Objects)
      While PB_Object_EnumerateNext(Objects, @ID)
        Select Type
          Case #PB_ID_Gadget
            If OS_Handle = GadgetID(ID) : Result = ID : PB_Object_EnumerateAbort(Objects) : Break : EndIf
          Case #PB_ID_Image
            If OS_Handle = ImageID(ID)  : Result = ID : PB_Object_EnumerateAbort(Objects) : Break : EndIf
          Case #PB_ID_Window
            If OS_Handle = WindowID(ID) : Result = ID : PB_Object_EnumerateAbort(Objects) : Break : EndIf
        EndSelect
      Wend
      
    EndIf
    
    ProcedureReturn Result
    
  EndProcedure

EndModule

LoadImage(12, #PB_Compiler_Home + "examples/sources/data/geegee2.bmp")
OpenWindow(3, 100, 100, 500, 300, "testwindow")
ButtonGadget(22, 10, 10, 100, 20 , "test")
Debug "Window = " + Str(PB::PBID(WindowID(3)))
Debug "Gadget = " + Str(PB::PBID(GadgetID(22),PB::#PB_ID_Gadget))
Debug "Image  = " + Str(PB::PBID(ImageID(12), PB::#PB_ID_Image))  
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow  
Last edited by Bisonte on Thu Feb 27, 2014 6:35 pm, edited 1 time in total.
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
Post Reply