/' IupTree: Example in C
   Creates a tree with some branches and leaves.
   Two callbacks are registered: one deletes marked nodes when the Del key is pressed,
   and the other, called when the right mouse button is pressed, opens a menu with options.
'/

#include once "crt.bi"
#include once "inc/iup.bi"
#include once "inc/iupkey.bi"
#include once "inc/iupcd.bi"

sub _init_ constructor
  IupOpen(0,0)
end sub

sub _exit_ destructor
  IupClose()
end sub


' Callback called when a leaf is added by the menu.
function  addleaf cdecl() as integer
  dim as zstring*10 attr
  dim as Ihandle ptr tree = IupGetHandle("tree")
  dim as integer id = IupGetInt(tree,"VALUE")
  sprintf(attr,"ADDLEAF%d",id)
  IupSetAttribute(tree,@attr,"(neu)")
  return IUP_DEFAULT
end function

' Callback called when a branch is added by the menu.
function addbranch cdecl() As integer
  dim As zstring*10 attr
  dim As Ihandle ptr tree = IupGetHandle("tree")
  dim As integer id = IupGetInt(tree,"VALUE")
  sprintf(attr,"ADDBRANCH%d",id)
  IupSetAttribute(tree,@attr,"(new)")
  return IUP_DEFAULT
end function

' Callback called when a node is removed by the menu.
function removenode cdecl() as integer
  dim as Ihandle ptr tree = IupGetHandle("tree")
  IupSetAttribute(tree,"DELNODE","MARKED")
  return IUP_DEFAULT
end function

' from the menu
function renamenode cdecl() As Integer
  return IUP_DEFAULT
end function

function executeleaf_cb cdecl(h as Ihandle ptr, id as integer) as integer
  printf(!"executeleaf_cb (%d)\n", id)
  return IUP_DEFAULT
end function

function rename_cb cdecl(h as Ihandle ptr, id as integer, name_ as zstring ptr) as integer
  printf(!"rename_cb (%d=%s)\n", id, name_)
  if (StrCmp(name_, "fool") = 0) then return IUP_IGNORE
  return IUP_DEFAULT
end function

function branchopen_cb cdecl(h as Ihandle ptr, id as integer) as integer
  printf(!"branchopen_cb (%d)\n", id)
  return IUP_DEFAULT
end function

function branchclose_cb cdecl(h as Ihandle ptr, id as integer) as integer
  printf(!"branchclose_cb (%d)\n", id)
  return IUP_DEFAULT
end function

function dragdrop_cb cdecl(h as Ihandle ptr, drag_id as integer, drop_id as integer, isshift_ as integer, iscontrol_ as integer) as integer
  printf(!"dragdrop_cb (%d)->(%d)\n", drag_id, drop_id)
  return IUP_DEFAULT
end function

' Callback called when a key is hit
function k_any_cb cdecl(h as Ihandle ptr, c as integer) as integer
  if (c = K_DEL) Then
   IupSetAttribute(h,"DELNODE","MARKED")
  end if
  return IUP_DEFAULT
end function

function selectnode cdecl(h as Ihandle ptr) as integer
  dim as Ihandle ptr tree = IupGetHandle("tree")
  IupSetAttribute(tree,"VALUE",IupGetAttribute(h, "TITLE"))
  return IUP_DEFAULT
end function

' Callback called when the right mouse button is pressed
function rightclick_cb cdecl(h as Ihandle ptr, id as integer) as integer
  dim as Ihandle ptr popup_menu
  dim as zstring*10 id_string
  popup_menu = _
  IupMenu(_
    IupItem ("Add Leaf","addleaf"),_
    IupItem ("Add Branch","addbranch"),_
    IupItem ("Rename Node","renamenode"),_
    IupItem ("Remove Node","removenode"),_
    IupSubmenu("Selection", _
      IupMenu(_
        IupItem ("ROOT", "selectnode"),_
        IupItem ("LAST", "selectnode"),_
        IupItem ("PGUP", "selectnode"),_
        IupItem ("PGDN", "selectnode"),_
        IupItem ("NEXT", "selectnode"),_
        IupItem ("PREVIOUS", "selectnode"),_
        IupSeparator(),_
        IupItem ("INVERT", "selectnode"),_
        IupItem ("BLOCK", "selectnode"),_
        IupItem ("CLEARALL", "selectnode"),_
        IupItem ("MARKALL", "selectnode"),_
        IupItem ("INVERTALL", "selectnode"),_
      NULL) _
    ),_
  NULL)

  IupSetFunction("selectnode", cast(Icallback, @selectnode))
  IupSetFunction("addleaf"   , cast(Icallback, @addleaf))
  IupSetFunction("addbranch" , cast(Icallback, @addbranch))
  IupSetFunction("removenode", cast(Icallback, @removenode))
  IupSetFunction("renamenode", cast(Icallback, @renamenode))

  sprintf(id_string,"%d",id)
  IupSetAttribute(h, "VALUE", id_string)
  IupPopup(popup_menu,IUP_MOUSEPOS,IUP_MOUSEPOS)

  IupDestroy(popup_menu)

  return IUP_DEFAULT
end function

' Initializes IupTree and registers callbacks
sub init_tree()
  dim as Ihandle ptr  tree = IupTree()
  IupSetCallback(tree, "EXECUTELEAF_CB", cast(Icallback, @executeleaf_cb))
  IupSetCallback(tree, "RENAME_CB"     , cast(Icallback, @rename_cb))
  IupSetCallback(tree, "BRANCHCLOSE_CB", cast(Icallback, @branchclose_cb))
  IupSetCallback(tree, "BRANCHOPEN_CB" , cast(Icallback, @branchopen_cb))
  IupSetCallback(tree, "DRAGDROP_CB"   , cast(Icallback, @dragdrop_cb))
  IupSetCallback(tree, "RIGHTCLICK_CB" , cast(Icallback, @rightclick_cb))
  IupSetCallback(tree, "K_ANY"         , cast(Icallback, @k_any_cb))

  'IupSetAttribute(tree, "FONT","COURIER_NORMAL")
  'IupSetAttribute(tree, "CTRL","YES")
  'IupSetAttribute(tree, "SHIFT","YES")
  'IupSetAttribute(tree, "ADDEXPANDED", "NO")
  'IupSetAttribute(tree, "SHOWDRAGDROP", "YES")
  IupSetAttribute(tree, "SHOWRENAME", "YES")
  IupSetHandle("tree",tree)
end sub

' Initializes the dialog
sub init_dlg()
  dim as Ihandle ptr  tree = IupGetHandle("tree")
  dim as Ihandle ptr  box = IupVbox(IupHbox(tree, IupButton("test", NULL), NULL), NULL)
  dim as Ihandle ptr  dlg = IupDialog(box)
  IupSetAttribute(dlg, "TITLE", "IupTree use the right mouse button")
  IupSetAttribute(box, "MARGIN", "20x20")
  IupSetHandle("dlg",dlg)
end sub

' Initializes the IupTree’s attributes
sub init_tree_atributes()
  dim as Ihandle ptr tree = IupGetHandle("tree")
  IupSetAttribute(tree, "NAME","Figures")
  IupSetAttribute(tree, "ADDBRANCH","3D")
  IupSetAttribute(tree, "ADDBRANCH","2D")
  IupSetAttribute(tree, "ADDLEAF","test")
  IupSetAttribute(tree, "ADDBRANCH1","parallelogram")
  IupSetAttribute(tree, "ADDLEAF2","diamond")
  IupSetAttribute(tree, "ADDLEAF2","square")
  IupSetAttribute(tree, "ADDBRANCH1","triangle")
  IupSetAttribute(tree, "ADDLEAF2","scalenus")
  IupSetAttribute(tree, "ADDLEAF2","isoceles")
  IupSetAttribute(tree, "ADDLEAF2","equilateral")
  IupSetAttribute(tree, "VALUE","6")
end sub

'
' main
'
Dim As Ihandle Ptr dlg
init_tree()                          ' Initializes IupTree
init_dlg()                           ' Initializes the dialog
dlg = IupGetHandle("dlg")            ' Retrieves the dialog handle
IupShowXY(dlg,IUP_CENTER,IUP_CENTER) ' Displays the dialog
init_tree_atributes()                ' Initializes attributes, can be done here or anywhere
IupMainLoop()                        ' Main loop



