/' IupList: Example in C 
   Creates a dialog with three frames, each one containing a list.
   The first is a simple list, the second one is a multiple list and the last one is a drop-down list.
   The second list has a callback associated.
'/
#include once "inc/iup.bi"

sub _init_ constructor
  IupOpen(0,0)
end sub

sub _exit_ destructor
  IupClose()
end sub

'
' main
'
function list_cb cdecl (self as IHandle ptr, t as zstring ptr, i as integer, v as integer) as integer
  dim as IHandle ptr lbl = cptr(IHandle ptr,IupGetAttribute(self, "_LABEL"))
  IupStoreAttribute(lbl, "TITLE", t)
  return IUP_DEFAULT
end function

function edit_cb cdecl (self as IHandle ptr, c as integer, after as zstring ptr) as integer
  dim as IHandle ptr lbl = cptr(IHandle ptr,IupGetAttribute(self, "_LABEL"))
  if (c=0)  then return IUP_DEFAULT
  IupStoreAttribute(lbl, "TITLE", after)
  return IUP_DEFAULT
end function

function btclose_cb cdecl (self as IHandle ptr) as integer
  return IUP_CLOSE
end function

function bt_cb cdecl (self as IHandle ptr) as integer
  dim as IHandle ptr list = cptr(IHandle ptr,IupGetAttribute(self, "_LIST"))
  IupMessagef ("List", "Value=%s", IupGetAttribute(list, "VALUE"))
  return IUP_DEFAULT
end function

function getfocus_cb cdecl (self as IHandle ptr) as integer
  dim as IHandle ptr bt = cptr(IHandle ptr,IupGetAttribute(self, "_BUTTON"))
  IupSetAttribute(bt, "BGCOLOR", "255 0 128")
  return IUP_DEFAULT
end function

function killfocus_cb cdecl (self as IHandle ptr) as integer
  dim as IHandle ptr bt = cptr(IHandle ptr,IupGetAttribute(self, "_BUTTON"))
  IupSetAttribute(bt, "BGCOLOR", NULL)
  return IUP_DEFAULT
end function


dim as IHandle ptr bt1 = IupButton("Drop+Edit", NULL)
IupSetCallback(bt1, "ACTION", cptr(ICallback,@bt_cb))

dim as IHandle ptr bt2 = IupButton("Drop", NULL)
IupSetCallback(bt2, "ACTION", cptr(ICallback,@bt_cb))

dim as IHandle ptr bt3 = IupButton("List+Edit", NULL)
IupSetCallback(bt3, "ACTION", cptr(ICallback,@bt_cb))

dim as IHandle ptr bt4 = IupButton("List", NULL)
IupSetCallback(bt4, "ACTION", cptr(ICallback,@bt_cb))


dim as IHandle ptr list1 = IupList(NULL)
IupSetAttributes(list1, !"1=\"US$ 1000\", 2=\"US$ 2000\", 3=\"US$ 30000000\", 4=\"US$ 4000\", 5=\"US$ 5000\", 6=\"US$ 6000\", 7=\"US$ 7000\",EXPAND=HORIZONTAL, EDITBOX=YES, DROPDOWN=YES, VISIBLE_ITEMS=5")
IupSetCallback(list1, "ACTION", cptr(ICallback,@list_cb))
IupSetCallback(list1, "GETFOCUS_CB",  cptr(ICallback,@getfocus_cb))
IupSetCallback(list1, "KILLFOCUS_CB", cptr(ICallback,@killfocus_cb))
IupSetCallback(list1, "EDIT_CB", cptr(ICallback,@edit_cb))
IupSetAttribute(list1, "_BUTTON", cptr(zstring ptr,bt1))

dim as IHandle ptr list2 = IupList(NULL)
IupSetAttributes(list2, !"1=\"R$ 1000\", 2=\"R$ 2000\", 3=\"R$ 3000\", 4=\"R$ 4000\", 5=\"R$ 5000\", 6=\"R$ 6000\", 7=\"R$ 7000\",EXPAND=HORIZONTAL, DROPDOWN=YES, VISIBLE_ITEMS=5")
IupSetCallback(list2, "ACTION", cptr(ICallback,@list_cb))
IupSetCallback(list2, "GETFOCUS_CB",  cptr(ICallback,@getfocus_cb))
IupSetCallback(list2, "KILLFOCUS_CB", cptr(ICallback,@killfocus_cb))
IupSetAttribute(list2, "_BUTTON", cptr(zstring ptr,bt2))

dim as IHandle ptr list3 = IupList(NULL)
IupSetAttributes(list3, !"1=\"Char A\", 2=\"Char B\", 3=\"Char CCCCC\", 4=\"Char D\", 5=\"Char F\", 6=\"Char G\", 7=\"Char H\",EXPAND=YES, EDITBOX=YES")
IupSetCallback(list3, "ACTION", cptr(ICallback,@list_cb))
IupSetCallback(list3, "GETFOCUS_CB",  cptr(ICallback,@getfocus_cb))
IupSetCallback(list3, "KILLFOCUS_CB", cptr(ICallback,@killfocus_cb))
IupSetCallback(list3, "EDIT_CB", cptr(ICallback,@edit_cb))
IupSetAttribute(list3, "_BUTTON", cptr(zstring ptr,bt3))

dim as IHandle ptr list4 = IupList(NULL)
IupSetAttributes(list4, !"1=\"Number 1\", 2=\"Number 2\", 3=\"Number 3\", 4=\"Number 4\", 5=\"Number 5\", 6=\"Number 6\", 7=\"Number 7\",EXPAND=YES")
IupSetCallback(list4, "ACTION", cptr(ICallback,@list_cb))
IupSetCallback(list4, "GETFOCUS_CB",  cptr(ICallback,@getfocus_cb))
IupSetCallback(list4, "KILLFOCUS_CB", cptr(ICallback,@killfocus_cb))
IupSetAttribute(list4, "_BUTTON", cptr(zstring ptr,bt4))

IupSetAttribute(bt1, "_LIST", cptr(zstring ptr,list1))
IupSetAttribute(bt2, "_LIST", cptr(zstring ptr,list2))
IupSetAttribute(bt3, "_LIST", cptr(zstring ptr,list3))
IupSetAttribute(bt4, "_LIST", cptr(zstring ptr,list4))

IupSetAttribute(bt1, "BGCOLOR", "192 192 192")
IupSetAttribute(bt2, "BGCOLOR", "192 192 192")
IupSetAttribute(bt3, "BGCOLOR", "192 192 192")
IupSetAttribute(bt4, "BGCOLOR", "192 192 192")


/' IupSetAttribute(list3, "READONLY", "YES") '/

dim as IHandle ptr box1 = IupVbox(list1, bt1, NULL)
dim as IHandle ptr box2 = IupVbox(list2, bt2, NULL)
dim as IHandle ptr box3 = IupVbox(list3, bt3, NULL)
dim as IHandle ptr box4 = IupVbox(list4, bt4, NULL)

dim as IHandle ptr lbl = IupLabel("")
IupSetAttribute(lbl, "EXPAND", "HORIZONTAL")

dim as IHandle ptr btok = IupButton("OK", NULL)
IupSetCallback(btok, "ACTION", cptr(ICallback,@btclose_cb))
IupSetHandle("btok", btok)
IupSetAttribute(btok, "_LIST1", cptr(zstring ptr,list1))
IupSetAttribute(btok, "_LIST2", cptr(zstring ptr,list2))
IupSetAttribute(btok, "_LIST3", cptr(zstring ptr,list3))
IupSetAttribute(btok, "_LIST4", cptr(zstring ptr,list4))

dim as IHandle ptr btcancel = IupButton("Cancel", "btclose_cb")
IupSetCallback(btcancel, "ACTION", cptr(ICallback,@btclose_cb))
IupSetHandle("btcancel", btcancel)

dim as IHandle ptr dlg = IupDialog(IupSetAttributes(IupVbox(IupHbox(box1, box2, box3, box4, NULL), _
                                                            lbl, _
                                                            btok, _
                                                            btcancel, _
                                                            NULL),"MARGIN=10x10, GAP=10"))
IupSetAttribute(dlg, "TITLE", "IupList Example")
IupSetAttribute(dlg, "_LABEL", cptr(zstring ptr,lbl))
IupSetAttribute(dlg, "DEFAULTENTER", "btok")
IupSetAttribute(dlg, "DEFAULTESC", "btcancel")

IupShowXY(dlg, IUP_CENTER, IUP_CENTER)

IupMainLoop()








