GetParent() - (for Windows only)

Share your advanced PureBasic knowledge/code with the community.
User avatar
TI-994A
Addict
Addict
Posts: 2741
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

GetParent() - (for Windows only)

Post by TI-994A »

A simple function to get the number of the parent window or container of a gadget:

Code: Select all

;==============================================================
;   GetParent() returns a gadget's parent type and number
;   through a delimited string - Windows only
;
;   works only if parent is a Window or ContainerGadget()
;   
;   tested & working on Win 8.1 with PureBasic v5.31 (x64)
;
;   by TI-994A - free to use, improve, share...
;
;   18th December 2014
;==============================================================

Procedure.s GetParent(gadgetNo)
  parentID = GetParent_(GadgetID(gadgetNo))
  windowNo = GetProp_(parentID, StringField("PB_WINDOWID", 1, ","))
  If windowNo
    result$ = "w," + Str(windowNo - 1)
  Else
    result$ = "g," + Str(GetDlgCtrlID_(parentID))
  EndIf
  ProcedureReturn result$
EndProcedure

;demo code
wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(0, 0, 0, 240, 180, "GetParent() Win0", wFlags)
ResizeWindow(0, WindowX(0) - 130, #PB_Ignore, #PB_Ignore, #PB_Ignore)
TextGadget(10, 10, 10, 220, 20, "Window & Container with same numbers:")
ButtonGadget(1, 10, 40, 220, 30, "Button in Window 0")

ContainerGadget(0, 0, 80, 240, 50)
SetGadgetColor(0, #PB_Gadget_BackColor, #Cyan)
ButtonGadget(2, 10, 10, 220, 30, "Button in Container 0")
CloseGadgetList()

ButtonGadget(3, 10, 140, 220, 30, "Button in Window 0")

OpenWindow(123, 0, 0, 240, 180, "GetParent() Win123", wFlags)
ResizeWindow(123, WindowX(123) + 130, #PB_Ignore, #PB_Ignore, #PB_Ignore)
TextGadget(11, 10, 10, 220, 20, "Window & Container with different numbers:")
ButtonGadget(4, 10, 40, 220, 30, "Button in Window 123")

ContainerGadget(69, 0, 80, 240, 50)
SetGadgetColor(69, #PB_Gadget_BackColor, #Magenta)
ButtonGadget(5, 10, 10, 220, 30, "Button in Container 69")
CloseGadgetList()

ButtonGadget(6, 10, 140, 220, 30, "Button in Window 123")

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      appQuit = 1
    Case #PB_Event_Gadget 
      Select EventGadget()
        Case 1 To 6
          Select EventType()
            Case #PB_EventType_LeftClick
              parentType$ = StringField(GetParent(EventGadget()), 1, ",")
              parentNo$ = StringField(GetParent(EventGadget()), 2, ",")
              If parentType$ = "w"
                result$ = "Window #" + parentNo$
              Else
                result$ = "Gadget #" + parentNo$
              EndIf
              SetGadgetText(EventGadget(), "My Papa is " + result$)
            Case #PB_EventType_LeftButtonUp
          EndSelect
      EndSelect
  EndSelect
Until appQuit
For some reason, the GetDlgCtrlID() API function does not work with the PanelGadget() or ScrollAreaGadget(). If anyone knows how to do this, please update the code.

Thank you. :D
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
mestnyi
Addict
Addict
Posts: 1098
Joined: Mon Nov 25, 2013 6:41 am

Re: GetParent() - (for Windows only)

Post by mestnyi »

For some reason, the GetDlgCtrlID() API function does not work with the PanelGadget() or ScrollAreaGadget()

Code: Select all

;==============================================================
;   GetParent() returns a gadget's parent type and number
;   through a delimited string - Windows only
;
;   works only if parent is a Window or ContainerGadget()
;   
;   tested & working on Win 8.1 with PureBasic v5.31 (x64)
;
;   by TI-994A - free to use, improve, share...
;
;   18th December 2014
;==============================================================

Procedure.s GetParent(gadgetNo)
  parentID = GetParent_(GadgetID(gadgetNo))
  windowNo = GetProp_(parentID, StringField("PB_WINDOWID", 1, ","))
  If windowNo
    result$ = "w," + Str(windowNo - 1)
  Else
    *Memory = ReAllocateMemory (0, 255) :GetClassName_ (parentID, *Memory, 255) 
    If GetDlgCtrlID_(parentID) =-1 Or PeekS (*Memory) = "Static"
      result$ = "g," + Str(GetDlgCtrlID_(GetParent_(parentID)))
    Else
      result$ = "g," + Str(GetDlgCtrlID_(parentID))
    EndIf  
  EndIf
  ProcedureReturn result$
EndProcedure

;demo code
wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(0, 0, 0, 240, 180, "GetParent() Win0", wFlags)
ResizeWindow(0, WindowX(0) - 130, #PB_Ignore, #PB_Ignore, #PB_Ignore)
TextGadget(10, 10, 10, 220, 20, "Window & Container with same numbers:")
ButtonGadget(1, 10, 40, 220, 30, "Button in Window 0")

;ContainerGadget(0, 0, 80, 240, 50)
PanelGadget(0, 0, 80, 240, 50) :AddGadgetItem(0,-1,"Panel")
SetGadgetColor(0, #PB_Gadget_BackColor, #Cyan)
ButtonGadget(2, 5, 1, 220, 30, "Button in Container 0")
CloseGadgetList()

ButtonGadget(3, 10, 140, 220, 30, "Button in Window 0")

OpenWindow(123, 0, 0, 240, 180, "GetParent() Win123", wFlags)
ResizeWindow(123, WindowX(123) + 130, #PB_Ignore, #PB_Ignore, #PB_Ignore)
TextGadget(11, 10, 10, 220, 20, "Window & Container with different numbers:")
ButtonGadget(4, 10, 40, 220, 30, "Button in Window 123")

;ContainerGadget(69, 0, 80, 240, 50)
ScrollAreaGadget(69, 0, 80, 240, 50,240,50)
SetGadgetColor(69, #PB_Gadget_BackColor, #Magenta)
;ScrollAreaGadget(5, 10, 10, 220, 30,240,50)
ButtonGadget(5, 5, 1, 220, 30, "Button in Container 69")
CloseGadgetList()

ButtonGadget(6, 10, 140, 220, 30, "Button in Window 123")

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      appQuit = 1
    Case #PB_Event_Gadget 
      Select EventGadget()
        Case 1 To 6
          Select EventType()
            Case #PB_EventType_LeftClick
              parentType$ = StringField(GetParent(EventGadget()), 1, ",")
              parentNo$ = StringField(GetParent(EventGadget()), 2, ",")
              If parentType$ = "w"
                result$ = "Window #" + parentNo$
              Else
                result$ = "Gadget #" + parentNo$
                
              EndIf
              SetGadgetText(EventGadget(), "My Papa is " + result$)
            Case #PB_EventType_LeftButtonUp
          EndSelect
      EndSelect
  EndSelect
Until appQuit
Post Reply