TreeGadget() > GetSubLevels() ?

Just starting out? Need help? Post your questions and find answers here.
Offin
User
User
Posts: 34
Joined: Mon May 04, 2009 10:53 am
Location: France

TreeGadget() > GetSubLevels() ?

Post by Offin »

Hi,

Is there a way to retreive the complete details of the selected line (including the names of its SubLevels), in a TreeGadget() ?
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: TreeGadget() > GetSubLevels() ?

Post by TI-994A »

Offin wrote:Is there a way to retreive the complete details of the selected line (including the names of its SubLevels), in a TreeGadget() ?
Hello Offin. This example lists all the items under the selected node of a TreeGadget(), with option buttons to limit the displayed list to the selected sub-level:

Code: Select all

EnableExplicit

Enumeration
  #MainWindow
  #Button  
  #Tree
  #List
  #Opt1
  #Opt2
EndEnumeration

Define L, appQuit, selectionLevel, displayLevel = 1
Declare InitialiseTree()

OpenWindow(#MainWindow, 0, 0, 400, 400, "TreeGadget", 
           #PB_Window_SystemMenu | 
           #PB_Window_ScreenCentered)

TreeGadget(#Tree, 10, 10, 200, 380)
ButtonGadget(#Button, 220, 10, 170, 30, "Display sub-levels...")
ListViewGadget(#List, 220, 80, 170, 310)
OptionGadget(#Opt1, 225, 50, 90, 20, "to level 2 only")
OptionGadget(#Opt2, 320, 50, 90, 20, "to level 3")
SetGadgetState(#Opt1, 1)
InitialiseTree()

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      appQuit = 1
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #Opt1, #Opt2
          If GetGadgetState(#Opt1)
            displayLevel = 1
          Else
            displayLevel = 2
          EndIf
        Case #Button
          ClearGadgetItems(#List)
          selectionLevel = GetGadgetItemAttribute(#Tree, GetGadgetState(#Tree),
                                                  #PB_Tree_SubLevel)
          AddGadgetItem(#List, -1, "Sub-levels of " +
                                   GetGadgetItemText(#Tree, GetGadgetState(#Tree)) + " :")
          For L = GetGadgetState(#Tree) + 1 To CountGadgetItems(#Tree) - 1
            If GetGadgetItemAttribute(#Tree, L, #PB_Tree_SubLevel) <= selectionLevel
              Break
            Else
              If GetGadgetItemAttribute(#Tree, L, #PB_Tree_SubLevel) <= displayLevel
                AddGadgetItem(#List, -1, "- " + GetGadgetItemText(#Tree, L))
              EndIf
            EndIf
          Next L
      EndSelect
  EndSelect
Until appQuit = 1 

Procedure InitialiseTree()
  Protected L, LL, item.s
  AddGadgetItem(#Tree, -1, "Random Lists", 0, 0)
  For L = 1 To 5
    Read.s item
    AddGadgetItem(#Tree, -1, item, 0, 1)
    For LL = 1 To 6
      Read.s item
      AddGadgetItem(#Tree, -1, item, 0, 2)
    Next LL
    SetGadgetItemState(#Tree, CountGadgetItems(#Tree) - 7, #PB_Tree_Expanded)
  Next L
  SetGadgetItemState(#Tree, 0, #PB_Tree_Expanded)
  SetGadgetState(#Tree, 0)
EndProcedure

DataSection
  Data.s "Colours", "Red", "Blue", "Green", "Yellow", "Cyan", "Magenta"
  Data.s "Fruits", "Apple", "Banana", "Cherry", "Lemon", "Mango", "Strawberry"
  Data.s "Animals", "Monkey", "Giraffe", "Horse", "Lion", "Tiger", "Zebra"
  Data.s "Planets", "Pluto", "Jupiter", "Neptune", "Venus", "Mercury", "Mars"
  Data.s "Countries", "France", "Germany", "China", "Japan", "United Kingdom", "U.S.A."
EndDataSection
Hope it's what you're looking for. :)
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
Offin
User
User
Posts: 34
Joined: Mon May 04, 2009 10:53 am
Location: France

Re: TreeGadget() > GetSubLevels() ?

Post by Offin »

Thank you for your help, but this is not what i am looking for.
Following your example, for instance, if i click on the line "Cherry", i would like to be informed that the name of the upper sublevel is "Fruits"
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: TreeGadget() > GetSubLevels() ?

Post by TI-994A »

Offin wrote:...i would like to be informed that the name of the upper sublevel...
Hello again. I've modified the example, and it will now display the selected node, along with its parent, siblings, and sub-levels, if any:

Code: Select all

EnableExplicit

Enumeration
  #MainWindow
  #Tree
  #List
EndEnumeration

Declare InitialiseTree()
Define L, appQuit, selectionLevel, parentNode, subExists, siblingsExists

OpenWindow(#MainWindow, 0, 0, 400, 400, "TreeGadget (select with mouse or keyboard)", 
           #PB_Window_SystemMenu | 
           #PB_Window_ScreenCentered)

TreeGadget(#Tree, 10, 10, 180, 380)
ListViewGadget(#List, 200, 10, 190, 380)
InitialiseTree()

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      appQuit = 1
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #Tree
          Select EventType()
            Case #PB_EventType_LeftClick, #PB_EventType_Change
              subExists = 0
              siblingsExists = 0
              ClearGadgetItems(#List)
              selectionLevel = GetGadgetItemAttribute(#Tree, GetGadgetState(#Tree),
                                                      #PB_Tree_SubLevel)
              AddGadgetItem(#List, -1, "You selected: " + 
                                       UCase(GetGadgetItemText(#Tree, GetGadgetState(#Tree))))
              AddGadgetItem(#List, -1, "")
              For L = GetGadgetState(#Tree) To 0 Step -1
                If GetGadgetItemAttribute(#Tree, L, #PB_Tree_SubLevel) < selectionLevel
                  parentNode = L
                  AddGadgetItem(#List, -1, "Parent node: " + UCase(GetGadgetItemText(#Tree, L)))
                  AddGadgetItem(#List, -1, "")
                  Break
                EndIf
              Next L
              AddGadgetItem(#List, -1, "Sub-levels:")
              For L = GetGadgetState(#Tree) + 1 To CountGadgetItems(#Tree) - 1
                If GetGadgetItemAttribute(#Tree, L, #PB_Tree_SubLevel) <= selectionLevel
                  Break
                Else
                  AddGadgetItem(#List, -1, "- " + UCase(GetGadgetItemText(#Tree, L)))
                  subExists = 1
                EndIf
              Next L
              If Not subExists
                AddGadgetItem(#List, -1, "- NONE -")
              EndIf
              AddGadgetItem(#List, -1, "")
              AddGadgetItem(#List, -1, "Sibling Nodes:")
              For L = parentNode + 1 To CountGadgetItems(#Tree) - 1
                If GetGadgetItemAttribute(#Tree, L, #PB_Tree_SubLevel) > selectionLevel
                  Continue
                ElseIf GetGadgetItemAttribute(#Tree, L, #PB_Tree_SubLevel) < selectionLevel
                  Break
                EndIf
                If GetGadgetItemText(#Tree, GetGadgetState(#Tree)) <> GetGadgetItemText(#Tree, L)
                  AddGadgetItem(#List, -1, "- " + UCase(GetGadgetItemText(#Tree, L)))
                  siblingsExists = 1
                EndIf
              Next L
              If Not siblingsExists
                AddGadgetItem(#List, -1, "- NONE -")
              EndIf
          EndSelect
      EndSelect
  EndSelect
Until appQuit = 1 

Procedure InitialiseTree()
  Protected L, LL, item.s
  AddGadgetItem(#Tree, -1, "Random Lists", 0, 0)
  For L = 1 To 5
    Read.s item
    AddGadgetItem(#Tree, -1, item, 0, 1)
    For LL = 1 To 6
      Read.s item
      AddGadgetItem(#Tree, -1, item, 0, 2)
    Next LL
    SetGadgetItemState(#Tree, CountGadgetItems(#Tree) - 7, #PB_Tree_Expanded)
  Next L
  SetGadgetItemState(#Tree, 0, #PB_Tree_Expanded)
  SetGadgetState(#Tree, 0)
EndProcedure

DataSection
  Data.s "Colours", "Red", "Blue", "Green", "Yellow", "Cyan", "Magenta"
  Data.s "Fruits", "Apple", "Banana", "Cherry", "Lemon", "Mango", "Strawberry"
  Data.s "Animals", "Monkey", "Giraffe", "Horse", "Lion", "Tiger", "Zebra"
  Data.s "Planets", "Pluto", "Jupiter", "Neptune", "Venus", "Mercury", "Mars"
  Data.s "Countries", "France", "Germany", "China", "Japan", "United Kingdom", "U.S.A."
EndDataSection
Hope it helps. :)
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
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: TreeGadget() > GetSubLevels() ?

Post by davido »

@TI-994A,

Another fine example. Thank you for sharing. :D
DE AA EB
Offin
User
User
Posts: 34
Joined: Mon May 04, 2009 10:53 am
Location: France

Re: TreeGadget() > GetSubLevels() ?

Post by Offin »

Thanks to your code, i am now able to retreive the complete details of the selected line (including the names of its SubLevels), in a TreeGadget().
It works well.
Thank you very much for your help.
Post Reply