yab | yet another Basic for HAIKU
using a treebox - Printable Version

+- yab | yet another Basic for HAIKU (https://yab.orgfree.com/forum)
+-- Forum: Programming in yab (https://yab.orgfree.com/forum/forumdisplay.php?fid=1)
+--- Forum: Snippets (https://yab.orgfree.com/forum/forumdisplay.php?fid=3)
+--- Thread: using a treebox (/showthread.php?tid=83)



using a treebox - bbjimmy - 03-08-2017

Treeboxes are the yab version of the Outline List View. They allow the user to select an item like a listbox while allowing for sub items for each listed item. This takes some special handeling to set-up.


First, add the treebox :

TREEBOX 0,0 to 300,400, "TB", 1, View$

Next add all the top level items in the order to be shown:

TREEBOX ADD "TB", "First Item"
TREEBOX ADD "TB", "Second Item"
TREEBOX ADD "TB", "Third Item"

Now add the sub-items grouped together and in the reverse order:

TREEBOX ADD "TB", "First Item", "Sub item2",0
TREEBOX ADD "TB", "First Item", "Sub item1",0
TREEBOX ADD "TB", "Sub item1", "Detail2", 0
TREEBOX ADD "TB", "Sub item1", "Detail1", 0
TREEBOX ADD "TB", "Second Item", "Sub item2,2",0
TREEBOX ADD "TB", "Second Item", "Sub item2,1",0


Haiku has issues with the outlinelistview therefore yab has the same issue. If one colapsed a menu and later expands the menu and attempts to make a selection, the wrong item is selected. The following is a work-around that will always make the proper selection, although it adds a minor visual glitch.

In the program loop do the following:

if left$(msg$(everyCommand),11)="TB:_Select:" then
tmp=treebox get "TB"
TREEBOX SELECT "TB", tmp
// now process the selection
endif

It seems that treebox get was not included in the documentation. It makes one wonder how many other yab commands are missing from the docs.

The magic here is:

TREEBOX SELECT "TB", tmp

this selects the item, but the loop will come around soon enough to re-select the sub-idem that was really intended. otherwise outlinelistview will continue to reurn the wrong message and the user will see his selection not take effect.

Example program:

Code:
#!yab

doc Place a description of your
doc program here.
doc
doc Author, date, license

// set DEBUG = 1 to print out all messages on the console
DEBUG = 1
dim tbitem$(10)
OpenWindow()
for x=1 to treebox count "TB"
tbitem$(x) = "This should be \""+(treebox get$ "TB",x)+".\"": print tbitem$(x)
next

// Main Message Loop
dim msg$(1)
while(not leavingLoop)
    nCommands = token(message$, msg$(), "|")

    for everyCommand = 1 to nCommands
        if(DEBUG and msg$(everyCommand)<>"") print msg$(everyCommand)

        switch(msg$(everyCommand))
            case "_QuitRequested"
            case "MainWindow:_QuitRequested"
                leavingLoop = true
                break
            

            default
                
        end switch
        if left$(msg$(everyCommand),11)="TB:_Select:" then
            tmp=treebox get "TB"
            TREEBOX SELECT "TB", tmp
            TEXTEDIT CLEAR "TE"
            TEXTEDIT ADD "TE", "Message: "+ msg$(everyCommand) +"\n\n"+tbitem$(tmp)
            endif
        

    next everyCommand
    

wend

CloseWindow()

end


// Setup the main window here
sub OpenWindow()
    window open 100,100 to 600,500, "MainWindow", "Main Window"
    Window set "MainWindow","flags", "Not-Zoomable Not-Resizable"
    SPLITVIEW 0,0 TO 500,400, "SplitView", 1, 1, "MainWindow"
    SPLITVIEW SET "SplitView", "Divider", 100
    SPLITVIEW SET "SplitView", "MinimumSizes", 100, 200
    TEXTEDIT 0,0 TO 500,400, "TE", 0,"SplitView2"
    TREEBOX 0,0 to 300,400, "TB", 1, "SplitView1"
    TREEBOX ADD "TB", "First Item"
    TREEBOX ADD "TB", "Second Item"
    TREEBOX ADD "TB", "Third Item"
    TREEBOX ADD "TB", "First Item", "Sub item2",0
    TREEBOX ADD "TB", "First Item", "Sub item1",0
    TREEBOX ADD "TB", "Sub item1", "Detail2", 0
    TREEBOX ADD "TB", "Sub item1", "Detail1", 0
    TREEBOX ADD "TB", "Second Item", "Sub item2,2",0
    TREEBOX ADD "TB", "Second Item", "Sub item2,1",0
    
    
end sub    

//close down the main window
sub CloseWindow()
    window close "MainWindow"
    return
end sub



RE: using a treebox - clasqm - 03-09-2017

Very useful, thanks Jim. I must admit I've been avoiding this widget.

Can I add this to the next edition of Programming with yab?


RE: using a treebox - bbjimmy - 03-09-2017

Sure!


Free Web Hosting