    '====================================================================
    ' Treeview example, modal dialog as main.
    '====================================================================
     
    $ filename "t.exe"
    'uses rtl32
    'uses rtl64
     
    '% review
    uses dialogs
     
    string crlf=chr(13,10)
    macro TreeView_GetItem(hwnd,pitem) SendMessage(hwnd, TVM_GETITEM,0, pitem)
    macro TreeView_GetNextItem(hwnd,hitem,code) SendMessage(hwnd, TVM_GETNEXTITEM, code, hitem)
    macro TreeView_GetSelection(hwnd) TreeView_GetNextItem(hwnd,null,TVGN_CARET)
    macro TreeView_InsertItem(hwnd,lpis) SendMessage(hwnd, TVM_INSERTITEM,0, lpis)
    macro TreeView_SetItem(hwnd,pitem) SendMessage(hwnd, TVM_SETITEM,0, pitem)
     
    % TVGN_CARET=9
    % TVIF_TEXT=1
    % TVIF_HANDLE=16
    % TVIF_CHILDREN=64
    % TVM_INSERTITEM=4352
    % TVM_GETNEXTITEM=4362
    % TVM_GETITEM=4364
    % TVM_SETITEM=4365
    % TVN_SELCHANGED= -402
    % TVS_HASBUTTONS=1
    % TVS_HASLINES=2
    % TVS_LINESATROOT=4
     
    type NMHDR  'WinData.inc
      sys   hwndFrom
      sys   idFrom     'UINT_PTR
      uint  code
    end type
     
    type TVITEM  
      uint  mask 
      sys   hItem 'HTREEITEM 
      uint  state 
      uint  stateMask 
      char* pszText 
      int   cchTextMax 
      int   iImage 
      int   iSelectedImage 
      int   cChildren 
      sys   lParam 
    end type
    typedef TVITEM TV_ITEM
     
    type TVINSERTSTRUCT  
      sys     hParent      'HTREEITEM 
      sys     hInsertAfter 'HTREEITEM
      TV_ITEM item 
    end type
    typedef TVINSERTSTRUCT TV_INSERTSTRUCT 
     
     
    'Identifiers
    % IDC_TREEVIEW = 1001
    % IDC_MLE      = 1002
    % IDC_BUTTON   = 1003
     
    declare sub InitTreeView(sys hWnd, lID, int lCount)
     
    =============================================
    'MAIN CODE
    =============================================
     
    init_common_controls()
     
    char* cmdline
    @cmdline=GetCommandLine()
    sys hInstance = GetModuleHandle(null)
     
     
    function DialogProc( sys hDlg, uint uMsg, sys wParam, lParam ) as int callback
       NMHDR ptnmhdr at lParam 
        
       sys hMle=GetDlgItem(hDlg, IDC_MLE)
       static string buffer, txt
     
       select case uMsg
      
          case WM_INITDIALOG
             InitTreeView(hDlg,IDC_TREEVIEW, 1)
           
          case WM_COMMAND
             select case loword(wParam)
                case IDCANCEL, IDC_BUTTON
                   EndDialog( hDlg, null )
            end select
     
          case WM_NOTIFY
             select case ptnmhdr.idFrom
                case IDC_TREEVIEW
                   select case ptnmhdr.code
                     case TVN_SELCHANGED
                       TV_ITEM tTVItem
                                    
                       'Get handle of item
                       sys hTV=GetDlgItem(hDlg, IDC_TREEVIEW)                 
                       sys hTVItem = TreeView_GetSelection(hTV)                                             
                       buffer = nuls 128
                       'Get text
                       tTVItem.hItem = hTVItem                  
                       tTVItem.mask = TVIF_TEXT                 
                       tTVItem.cchTextMax = 128                  
                       tTVItem.pszText = buffer                  
                       if TreeView_GetItem(hTV, &tTVItem) then                  
                       if len(tTVItem.pszText) then
                          txt=tTVItem.pszText
                          if txt="Title 1" then
                             txt="Ipsum lorem" & crlf & crlf &
                             "At vero eos et accusam et justo" & crlf &
                             "labore et dolore magna aliquyam erat," & crlf &
                             "no sea takimata sanctus est."                   
                             SendMessage(hMle, WM_SETTEXT, 0, txt)
                          else   
                             SendMessage(hMle, WM_SETTEXT, 0, txt & " is selected")
                          end if                  
                       end if                      
                       end if              
                      
                end select 
             end select
         
          case WM_CLOSE
             EndDialog( hDlg, null )
                    
       end select
     
       return 0
    end function
     
     
    sub winmain()
       uint Style, StyleEx
       
       Dialog( 0, 0, 520*0.6, 320*0.5, "Treeview example in modal dialog using OxygenBasic",
               WS_OVERLAPPEDWINDOW or DS_CENTER or DS_SETFONT,
               8, "MS Sans Serif", WS_EX_CLIENTEDGE)
     
       Style   = WS_CHILD OR WS_CLIPSIBLINGS OR WS_TABSTOP OR _
                 WS_VISIBLE OR TVS_HASBUTTONS OR TVS_HASLINES OR _
                 TVS_LINESATROOT
       StyleEx = WS_EX_CLIENTEDGE
       Control("Treeview",  IDC_TREEVIEW, "SysTreeView32", Style,
               10*0.6,10*0.5,190*0.6,270*0.5 , StyleEx )        
     
       Style   = WS_CHILD OR WS_CLIPSIBLINGS OR WS_TABSTOP OR _
                 WS_VISIBLE OR ES_AUTOHSCROLL OR ES_AUTOVSCROLL OR _
                 ES_MULTILINE OR ES_WANTRETURN
       Control("",  IDC_MLE, "EDIT", Style,
               216*0.6,10*0.5,290*0.6,180*0.5 , StyleEx )        
     
       PushButton( "Close" , IDC_BUTTON, 320*0.6, 230*0.5, 100*0.6, 30*0.5 )
       CreateModalDialog( null, @DialogProc, 0)
    end sub
     
    winmain()
     
    '=====================================================
     
    sub TreeViewInsertItem(sys hTree, hParent, string sItem)
       TV_INSERTSTRUCT tTVInsert
       TV_ITEM tTVItem
     
       if hParent then
         tTVItem.mask        = TVIF_CHILDREN or TVIF_HANDLE
         tTVItem.hItem       = hParent
         tTVItem.cchildren   = 1
         TreeView_SetItem (hTree, tTVItem) 
       end if
     
       tTVInsert.hParent              = hParent 
       tTVInsert.Item.mask            = TVIF_TEXT
       tTVInsert.Item.pszText         = strptr(sItem)
       tTVInsert.Item.cchTextMax      = len(sItem)
     
       TreeView_InsertItem (hTree, &tTVInsert)
    end sub
     
     
    sub InitTreeView(sys hWnd, lID, int lCount)
        sys hRoot, hParent
        int i,j,k
        sys hCtl
     
        hCtl = GetDlgItem(hWnd,lID)
     
        for i = 1 to lCount
           hRoot = TreeViewInsertItem(hCtl, null, "Title " & str(i))
           for j = 1 to 3
              hParent = TreeViewInsertItem(hCtl, hRoot, "Chapter " & str(j))
              for k = 1 to 4
                 TreeViewInsertItem(hCtl, hParent, "Paragraph " &  str(j) & "." & str(k)) 
              next k
            next j
        next i
    end sub
     