Page 1 of 2

by the handle get gadget ident

Posted: Tue Feb 25, 2014 8:46 pm
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

Re: by the handle is a gadget

Posted: Tue Feb 25, 2014 10:13 pm
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)

Re: by the handle is a gadget

Posted: Tue Feb 25, 2014 10:46 pm
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!

Re: by the handle is a gadget

Posted: Wed Feb 26, 2014 1:33 am
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.

Re: by the handle is a gadget

Posted: Wed Feb 26, 2014 7:03 am
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

Re: by the handle is a gadget

Posted: Wed Feb 26, 2014 7:35 am
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) (так думаю будет понятно (нужно кроссплатформенное решение))

Re: by the handle is a gadget

Posted: Wed Feb 26, 2014 7:47 am
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 (тоже самое но только для линукс нужен)

Re: by the handle is a gadget

Posted: Wed Feb 26, 2014 3:54 pm
by mestnyi
Is there no way to get in Linux "pb_id" with "gadgetid"

Re: by the handle is a gadget

Posted: Wed Feb 26, 2014 7:17 pm
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

Re: by the handle is a gadget

Posted: Wed Feb 26, 2014 7:49 pm
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  


Re: by the handle is a gadget

Posted: Wed Feb 26, 2014 8:42 pm
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

Re: by the handle is a gadget

Posted: Wed Feb 26, 2014 10:23 pm
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)

Re: by the handle is a gadget

Posted: Wed Feb 26, 2014 10:49 pm
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   

  

Re: by the handle is a gadget

Posted: Thu Feb 27, 2014 5:29 am
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. .

Re: by the handle is a gadget

Posted: Thu Feb 27, 2014 9:21 am
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