/***************************************************************************
                          SIMPLY POWERFUL TOOLKIT (SPTK)
                               DEMO PROGRAMS SET
                          tree_view.cpp  -  description
                             -------------------
    begin                : Sun Jun 8, 2003
    copyright            : (C) 2003-2006 by Alexey S.Parshin
    email                : alexeyp@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifdef __BORLANDC__
#include <vcl.h>
#pragma hdrstop
#endif

#include <stdio.h>
#include <FL/Fl.H>

#include <sptk3/cgui>
#include <sptk3/CSmallPixmaps.h>

Fl_Pixmap* closedFolder;
Fl_Pixmap* openedFolder;
Fl_Pixmap* fileSmall;

using namespace sptk;

CTreeView *tree;

void exit_cb(Fl_Widget *w,void *) {
	w->window()->hide();
}

void changed_cb(Fl_Widget *w,void *) {
	CTreeItem *item = tree->selected();
	if (item) {
		puts(item->label());
	}
}

void add_item_cb(Fl_Widget *w,void *) {
	CTreeItem *selectedItem = tree->selected();
	CTreeItem *node = 0L;

	CDialog dlg(300,140,"Add new item");

	CComboBox typeCombo("Item Type:");
	typeCombo.labelWidth(80);
	CStrings typeChoices;
	typeChoices.push_back(idstring("Folder",1));
	typeChoices.push_back(idstring("Document",2));
	typeCombo.addRows("type",typeChoices);
	typeCombo.columns()[(unsigned)0].width(150);
	typeCombo.dataMode(LV_DATA_KEY);
	typeCombo.data(1);

	CComboBox modeCombo("Add Mode:");
	modeCombo.labelWidth(80);
	CStrings modeChoices;
	modeChoices.push_back(idstring("To the root level",1));
	modeChoices.push_back(idstring("To selected item",2));
	modeCombo.addRows("type",modeChoices);
	modeCombo.columns()[(unsigned)0].width(150);
	modeCombo.dataMode(LV_DATA_KEY);
	modeCombo.data(1);

	CInput inp("Item Name:");
	inp.labelWidth(80);
	dlg.end();
	if (dlg.showModal()) {
		int mode = int(typeCombo.data()) + int(modeCombo.data()) * 2;
		switch (mode) {
			case 3: // add folder to the root
				node = tree->addItem(inp.data().asString(),openedFolder,closedFolder);
				break;
			case 4: // add item to the root
				node = tree->addItem(inp.data().asString(),fileSmall);
				break;
			case 5: // add folder to the current
				if (selectedItem)
					node = selectedItem->addItem(inp.data().asString().c_str(),openedFolder,closedFolder);
				break;
			case 6: // add item to the current
				if (selectedItem)
					node = selectedItem->addItem(inp.data().asString().c_str(),fileSmall);
				break;
		}
		if (node)
			Fl::focus(node);
		tree->relayout();
	}
}

void remove_item_cb(Fl_Widget *w,void *) {
	CTreeItem *item = tree->selected();
	tree->removeItem(item);
}

int main(int argc, char *argv[]) {
	CWindow window(400,300);

	tree = new CTreeView("",10,SP_ALIGN_CLIENT);
	tree->end();

   //CTreeView *tree = (CTreeView *)cbl.treeControl();

	closedFolder = CSmallPixmaps::pixmaps[SXPM_FOLDER];
	openedFolder = CSmallPixmaps::pixmaps[SXPM_FOLDER_OPENED];
	fileSmall = CSmallPixmaps::pixmaps[SXPM_DOCUMENT];;

	CTreeItem* node;

   // Add some nodes with icons -- some open, some closed.

	node = tree->addItem("aaa",openedFolder,closedFolder);
	node->addItem("bbb 1",openedFolder,closedFolder);
	node->open();

	node = tree->addItem("bbb 2",openedFolder,closedFolder);
	node->close();
	node = node->addItem("ccc",openedFolder,closedFolder);
	node->addItem("ddd", fileSmall);

	node = tree->addItem("eee",openedFolder,closedFolder);
	node->addItem("fff", fileSmall);

	node = tree->addItem("ggg",openedFolder,closedFolder);
	node = node->addItem("hhh", fileSmall);
	node->close();
	node->addItem("iii", fileSmall);

	node = tree->addItem("jjj",openedFolder,closedFolder);
	node->addItem("kkk", fileSmall);

	node = tree->addItem("lll", fileSmall);
	node = tree->addItem("mmm",openedFolder,closedFolder);
	node->close();
	node = node->addItem("nnn",openedFolder,closedFolder);
	node->addItem("ooo", fileSmall);

	node = tree->addItem("ppp",openedFolder,closedFolder);
	node->addItem("qqq", fileSmall);

	node = tree->addItem("rrr",openedFolder,closedFolder);
	node = node->addItem("sss",openedFolder,closedFolder);
	node->addItem("ttt",openedFolder,closedFolder);

	node = tree->addItem("uuu",openedFolder,closedFolder);
	node->addItem("vvv", fileSmall);

	node = tree->addItem("www", fileSmall);
	node = node->addItem("xxx", fileSmall);
	node = node->addItem("yyy", fileSmall);
	node->addItem("zzz", fileSmall);

   //tree->end();

	tree->callback(changed_cb);

	CGroup  group("",10,SP_ALIGN_BOTTOM);
	group.box(FL_THIN_DOWN_BOX);
	group.color(FL_LIGHT1);

	CButton   btn1(SP_EXIT_BUTTON,SP_ALIGN_RIGHT);
	btn1.callback(exit_cb);

	CButton   btn2(SP_DELETE_BUTTON,SP_ALIGN_RIGHT);
	btn2.callback(remove_item_cb);

	CButton   btn3(SP_ADD_BUTTON,SP_ALIGN_RIGHT);
	btn3.callback(add_item_cb);

	window.end();
	window.resizable(&window);

	window.show(argc,argv);

	CThemes::set("OSX");

	Fl::run();

	return 0;
}
