/***************************************************************************
                          SIMPLY POWERFUL TOOLKIT (SPTK)
                          CTreeView.cpp  -  description
                             -------------------
    begin                : June 21 2003
    copyright            : (C) 2002-2006 by Alexey Parshin
    email                : alexeyp@gmail.com
 ***************************************************************************/

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

   This library is distributed in the hope that it will be useful, but 
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
   General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; if not, write to the Free Software Foundation, 
   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.

   Please report all bugs and problems to "alexeyp@gmail.com"
 ***************************************************************************/

#include <FL/Fl.h>

#include <sptk3/CDataControl.h>
#include <sptk3/CTreeControl.h>
#include <sptk3/CTreeView.h>

using namespace std;
using namespace sptk;

void CTreeView::internal_callback(Fl_Widget *widget,void *) {
   CTreeView *tree = (CTreeView *)widget->parent();
   tree->do_callback();
}

void CTreeView::ctor_init() {
   m_controlFlags = FGE_MULTILINEENTRY;
   m_treeControl = new CTreeControl("",10,SP_ALIGN_NONE);
   m_control = m_treeControl;
   m_control->callback(CControl::internalCallback);
   labelWidth(0);
   end();
}

CTreeView::CTreeView(const char * label,int layoutSize,CLayoutAlign layoutAlignment) 
: CControl(label,layoutSize,layoutAlignment)
{
   ctor_init();
}

#ifdef __COMPATIBILITY_MODE__    
CTreeView::CTreeView(int x,int y,int w,int h,const char *label) 
: CControl(x,y,w,h,label)
{
   ctor_init();
}
#endif

CTreeView::~CTreeView() {
}

std::string CTreeView::selectedPath() const {
   std::string result;
   CTreeItem *node = m_treeControl->selected();
   if (!node) return "";
   do {
      result = "/" + std::string(node->label()) + result;
      node = node->parentItem();
   } while (node && node->parentItem());
   return result;
}

void CTreeView::resize(int x,int y,int w,int h) {
   CControl::resize(x,y,w,h);

   w -= m_labelWidth;
   x += m_labelWidth;

   if (m_menuButton)
      m_menuButton->resize(x,y,w,this->h());

   m_treeControl->resize(x,y,w,h);
}

void CTreeView::load(CQuery *loadQuery) {
   if (!m_fieldName.length()) return;
   CField& fld = (*loadQuery)[m_fieldName.c_str()];
   data( fld );
}

void CTreeView::save(CQuery *updateQuery) {
   if (!m_fieldName.length()) return;
   CParam& param = updateQuery->param(m_fieldName.c_str());
   param = data();
}

CVariant CTreeView::data() const {
   CTreeItem *node = m_treeControl->selected();
   if (node) return (long) node->user_data();
   return int(0);
}

void CTreeView::data(const CVariant v) {
   CTreeItem *node = m_treeControl->findData((void *)v.asInteger());
   if (node)
      m_treeControl->makeVisible(node);
}
