GetGadgetWindow()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Justin
Addict
Addict
Posts: 948
Joined: Sat Apr 26, 2003 2:49 pm

GetGadgetWindow()

Post by Justin »

If you need to post a custom event from a CanvasGadget you always need the top level window so i think it makes sense to have it.
I know it can be done using the api but a native solution is needed.

Code: Select all

PostEvent(#CustEvent, GetGadgetWindow(gadget), gadget, #CustEventType, data)
User avatar
Bisonte
Addict
Addict
Posts: 1313
Joined: Tue Oct 09, 2007 2:15 am

Re: GetGadgetWindow()

Post by Bisonte »

With the SDK it is build in for all platforms...

Code: Select all

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  Import ""
    PB_Object_EnumerateStart          (PB_Object)
    PB_Object_EnumerateNext           (PB_Object, ID)
    PB_Object_EnumerateAbort          (PB_Object)
    PB_Gadget_GetRootWindow           (GadgetID)
    PB_Window_Objects
  EndImport
CompilerElse
  ImportC ""
    PB_Object_EnumerateStart          (PB_Object)
    PB_Object_EnumerateNext           (PB_Object, ID)
    PB_Object_EnumerateAbort          (PB_Object)
    PB_Gadget_GetRootWindow(GadgetID)
    PB_Window_Objects
  EndImport
CompilerEndIf

Procedure.i GetWindowObject(WindowID)
  
  Protected Result = -1
  
  PB_Object_EnumerateStart(PB_Window_Objects)
  
  While PB_Object_EnumerateNext(PB_Window_Objects, @Result)
    If WindowID = WindowID(Result)
      PB_Object_EnumerateAbort(PB_Window_Objects)
      Break
    EndIf
  Wend    
  
  ProcedureReturn Result
  
EndProcedure
Procedure.i GetGadgetWindow(Gadget)
  
  Protected hWnd, Window = -1
  
  If IsGadget(Gadget)
    hWnd = PB_Gadget_GetRootWindow(GadgetID(Gadget))
    If hWnd
      Window = GetWindowObject(hWnd)
    EndIf
  EndIf
  
  ProcedureReturn Window
  
EndProcedure
  
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
mk-soft
Always Here
Always Here
Posts: 6246
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: GetGadgetWindow()

Post by mk-soft »

PB_Gadget_GetRootWindow not for macOS ...

See Module System
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
Justin
Addict
Addict
Posts: 948
Joined: Sat Apr 26, 2003 2:49 pm

Re: GetGadgetWindow()

Post by Justin »

Hi, thanks for the suggestions. Like i said i already know about them, the point is to have a native solution that will always work regardless of some pb internal change.
And if the functionality is already implemented it should be very easy to do.
mestnyi
Addict
Addict
Posts: 1098
Joined: Mon Nov 25, 2013 6:41 am

Re: GetGadgetWindow()

Post by mestnyi »

Justin wrote: Thu Oct 17, 2024 8:26 am Hi, thanks for the suggestions. Like i said i already know about them, the point is to have a native solution that will always work regardless of some pb internal change.
And if the functionality is already implemented it should be very easy to do.
+ 1
User avatar
ChrisR
Addict
Addict
Posts: 1466
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: GetGadgetWindow()

Post by ChrisR »

+1, a native function would be nice, get the parent Gadget would be useful too, perhaps something like

Code: Select all

GetParentGadget(#Gadget[, Flags])   ; #PB_ParentGadget (default), #PB_RootWindow
For PostEvent, it would be welcome to accept #PB_Any for the window, if Gadget is specified, with GetRootWindow done internally.

Code: Select all

PostEvent(#CustEvent, #PB_Any, Gadget, #CustEventType, Data)
And with the window specified, the Gadget number should not be mandatory for some events, e.g:

Code: Select all

PostEvent(#PB_Event_CloseWindow, 0) 
Quin
Addict
Addict
Posts: 1133
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: GetGadgetWindow()

Post by Quin »

+1
Justin
Addict
Addict
Posts: 948
Joined: Sat Apr 26, 2003 2:49 pm

Re: GetGadgetWindow()

Post by Justin »

Yes GetetParentGadget() is also needed, and since we are asking:
GetGadgetChildren(gadget, List children.i()) for containers
and
GetNextGadget(gadget)
GetPreviousGadget(gadget)
for gadget siblings.
User avatar
mk-soft
Always Here
Always Here
Posts: 6246
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: GetGadgetWindow()

Post by mk-soft »

I have optimised the GetGadgetWindow function once.
I would also like to have this function as standard. Especially to build own controls with the CanvasGadget to store the parent window for the PostEvents.

Code: Select all

CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
  ; PB Internal Structure Gadget MacOS
  Structure sdkGadget
    *gadget
    *container
    *vt
    UserData.i
    Window.i
    Type.i
    Flags.i
  EndStructure
  
  Import ""
    PB_Window_GetID(Object) 
  EndImport
  
CompilerEndIf

; ---------------------------------------------------------------------------------------

Procedure GetGadgetWindow(Gadget)
  Protected ID, r1
  
  If IsGadget(Gadget)
    CompilerSelect #PB_Compiler_OS
      CompilerCase #PB_OS_MacOS
        Protected *Gadget.sdkGadget = IsGadget(Gadget)
        If *Gadget
          ID = WindowID(*Gadget\Window)
          r1 = PB_Window_GetID(ID)
        Else
          r1 = -1
        EndIf
      CompilerCase #PB_OS_Linux
        ID = gtk_widget_get_toplevel_(GadgetID(Gadget))
        If ID
          r1 = g_object_get_data_(ID, "pb_id" )
        Else
          r1 = -1
        EndIf
      CompilerCase #PB_OS_Windows           
        ID = GetAncestor_(GadgetID(Gadget), #GA_ROOT)
        r1 = GetProp_(ID, "PB_WINDOWID")
        If r1 > 0
          r1 - 1
        Else
          r1 = -1
        EndIf
    CompilerEndSelect
  Else
    r1 = -1
  EndIf
  ProcedureReturn r1
EndProcedure

; ---------------------------------------------------------------------------------------

;-Test

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

Enumeration Windows 5 ; Test set to 5
  #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)
    
    Debug GetGadgetWindow(#MainEdit)
    Debug GetGadgetWindow(#MainButtonOk)
    Debug GetGadgetWindow(#MainButtonCancel)
    
    ; 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
Post Reply