//
//	Legato Code Examples
//	--------------------
//
//	Dialog Examples -- List Box Controls
//
//	Group:		Dialog
//	Title:		List Box Controls 
//	Description:	Example of coding list box controls exhibiting various features.
//	Revised:	12/16/2014
//
//	(c) Novaworks, LLC
//


								/************************************************/

									/****************************************/
#include "Listbox Controls 01.rc"

								/************************************************/

									/****************************************/
									/* ** Prototypes			*/
	int 		list_update			();		/* Our Support for Dialog Status	*/
									/*					*/
									/* ** Static Data			*/
	int		s_ix;						/* Persistent Select Index		*/
        int		drop;						/* Drop Toggle Flag			*/


								/************************************************/

									/****************************************/
									/* Program Entry			*/
									/****************************************/

int main () {
	DialogBox("ListBoxTest01", "list_");
	}


								/************************************************/

									/****************************************/
									/* Dialog Service - Load		*/
									/****************************************/

int list_load() {
	ListBoxAddItem(LB_LIST, "Item 1 - Cats");
	ListBoxAddItem(LB_LIST, "Item 2 - Dogs");
	ListBoxAddItem(LB_LIST, "Item 3 - Ferrets");
	ListBoxAddItem(LB_LIST, "Item 4 - Mouses");
	list_update();
	EditSetText(LB_TEXT, "Set Text Test");
    	}
        
									/****************************************/
									/* Dialog Service - Action		*/
									/****************************************/
int list_action(int c_id, int c_ac) {
    	string		s1;
        int		rc, width;


	if (c_id == LB_LIST) {						// List Box
	  if (c_ac == LBN_SELCHANGE) {
	    list_update();
	    return 0;
	    }          
	  if (c_ac == LBN_DBLCLK) {
	    rc = ListBoxGetSelectIndex(LB_LIST);
	    s1 = FormatString("Double Click on %d", rc);
	    EditSetText(LB_CONTENT, s1);
	    return 0;
	    }          
	  return 0;
          }
	if (c_id == LB_LOAD) {						// Load
	  s1 = EditGetText(LB_TEXT);
	  if (s1 == "") {
	    s1 = "A1, A2 B, A3 CC, A4 DDD, A5 E E E, A6 END";
            }
          ListBoxReset(LB_LIST);
	  ListBoxLoadList(LB_LIST, s1);
	  list_update();
	  EditSetText(LB_CONTENT, s1);
	  return 0;
          }
	if (c_id == LB_FOLDER) {					// Load Folder
	  s1 = EditGetText(LB_TEXT);
	  if (s1 == "") {
	    s1 = "C:\\Windows\\*.*";
            }
          ListBoxReset(LB_LIST);
	  ListBoxLoadFolder(LB_LIST, s1, DDL_ARCHIVE);
	  list_update();
	  EditSetText(LB_CONTENT, s1);
	  return 0;
          }
	if (c_id == LB_RESET) {						// Reset
          ListBoxReset(LB_LIST);
	  list_update();
	  return 0;
          }
	if (c_id == LB_INSERT) {					// Insert
          ListBoxInsertItem(LB_LIST, 1, "Inserted at Position 1");
	  list_update();
	  return 0;
          }
	if (c_id == LB_DELETE) {					// Delete
	  s_ix = ListBoxGetSelectIndex(LB_LIST);
	  if (s_ix < 0) {
            MessageBox('X', "Nothing selected");
            }
          ListBoxDeleteItem(LB_LIST, s_ix);
	  rc = ListBoxGetItemCount(LB_LIST);
          if (s_ix >= rc) { s_ix--; }
          ListBoxSetSelectIndex(LB_LIST, s_ix);
	  list_update();
	  return 0;
          }
	if (c_id == LB_SELECT) {					// Select
          ListBoxSetSelectIndex(LB_LIST, s_ix);
	  s_ix++;
	  list_update();
	  return 0;
          }
	if (c_id == LB_SEL_RESET) {					// Select Reset
	  s_ix = -1;
          ListBoxSetSelectIndex(LB_LIST, s_ix);
	  list_update();
	  return 0;
          }
    	}

									/****************************************/
									/* Dialog Support 			*/
									/****************************************/
int list_update() {
	string		s1;
	int		ix, ic;

	ix = ListBoxGetSelectIndex(LB_LIST);
	ic = ListBoxGetItemCount(LB_LIST);
        if (ix < 0) {
          EditSetText(LB_INDEX, "(not selected)");
	  EditSetText(LB_CONTENT, "(no data)");
	  ControlDisable(LB_DELETE);
          }
        else {
          EditSetText(LB_INDEX, ix);
	  s1 = ListBoxGetItemText(LB_LIST, ix);
	  EditSetText(LB_CONTENT, s1);
	  ControlEnable(LB_DELETE);
          }
        EditSetText(LB_TOTAL_ITEMS, ic);
    	}
