/***************************************************************************
                          SIMPLY POWERFUL TOOLKIT (SPTK)
                          CTreeView.h  -  description
                             -------------------
    begin                : June 21 2003
    copyright            : (C) 2002-2005 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"
 ***************************************************************************/

#ifndef __CTREEVIEW_H__
#define __CTREEVIEW_H__

#include <FL/Fl_Pixmap.h>

#include <sptk3/CTreeControl.h>

#include <sptk3/CDataControl.h>

/// @addtogroup gui GUI Classes
/// @{

/// @brief Tree view widget. 
///
/// Shows tree items with icons. Can use different icons for opened/closed folders.
class SP_EXPORT CTreeView : public CControl {

   /// The actual tree control
   CTreeControl *m_treeControl;

   /// Constructor initializer
   void ctor_init();
protected:

   /// Internal tree control callback
   static void internal_callback(Fl_Widget *,void *);

public:

   /// Constructor in SPTK style 
   /// @param label const char *, label
   /// @param layoutSize int, widget align in layout
   /// @param layoutAlign CLayoutAlign, widget align in layout
   CTreeView(const char * label=0,int layoutSize=10,CLayoutAlign layoutAlign=SP_ALIGN_TOP);

#ifdef __COMPATIBILITY_MODE__    
   /// Constructor in FLTK style 
   /// @param x int, x-position
   /// @param y int, y-position
   /// @param w int, width
   /// @param h int, height
   /// @param label, const char * label
   CTreeView(int x,int y,int w,int h,const char *label=0);
#endif

   /// Destructor
   ~CTreeView();    

   /// Returns controls' kind (internal SPTK RTTI).
   virtual CControlKind kind() const { return DCV_TREEVIEW; }

   /// Returns controls' class name (internal SPTK RTTI).
   virtual std::string className() const { return "CTreeView"; }

   /** Adds a child item to the item. If the closedImage parameter is omitted the openedImage is used instead.
    * @param label std::string, the item label
    * @param openedImage Fl_Image *, the image for the opened state
    * @param closedImage Fl_Image *, the image for the closed state
    * @param data void *, the user data or ID attached to the item
    * @returns the new child item
    */
   CTreeItem *addItem(std::string label,Fl_Image *openedImage,Fl_Image *closedImage=0L,void *data=0L) {
      return m_treeControl->addItem(label.c_str(),openedImage,closedImage,data); 
   }

   /** Adds a child item to the item using the path. The required path items are created automatically. 
    * @param path const CStrings&, the path to the new child item relatively to the item
    * @param openedImage Fl_Image *, the image for the opened folder
    * @param closedImage Fl_Image *, the image for the closed folder
    * @param itemImage Fl_Image *, the image for the child item
    * @param data void *, the user data or ID attached to the item
    * @returns the new child item
    */
   CTreeItem *addPath(const std::vector<std::string>& path,Fl_Image *openedImage,Fl_Image *closedImage,Fl_Image *itemImage=0L,void *data=0L) {
      return m_treeControl->addPath(path,openedImage,closedImage,itemImage,data);
   }

   /** Adds a child item to the item using the default folder images.
    * @param path const CStrings&, the item full path in the tree starting with '/'
    * @param itemImage Fl_Image *, the image for the child item
    * @param data void *, the user data or ID attached to the item
    * @returns the new child item
    */
   CTreeItem *addPath(const std::vector<std::string>& path,Fl_Image *itemImage=0L,void *data=0L) { return addPath(path,itemImage,data); }

   /// Removes an item from the parent tree item
   void removeItem(CTreeItem *item) { m_treeControl->removeItem(item); }

   /// Returns currently selected item
   CTreeItem *selected() const { return m_treeControl->selected(); }

   /// Returns currently selected item path in the tree
   std::string selectedPath() const;

   /// Removes all the tree items
   virtual void clear() { m_treeControl->clear(); }

   /// Relayouts the tree. May be necessary after you've changed items data a lot
   void relayout() { m_treeControl->relayout(); redraw(); }

   /// Resizes the control and inside widgets. 
   /// @param x int, x-position
   /// @param y int, y-position
   /// @param w int, width
   /// @param h int, height
   virtual void     resize(int x,int y,int w,int h);

   /// Returns the currently selected item ID (or user_data)
   virtual CVariant data() const;

   /// Selects the item with matching ID (or user_data)
   virtual void     data(CVariant v);

   /// Data connection isn't implemented yet
   virtual void load(CQuery *);

   /// Data connection isn't implemented yet
   virtual void save(CQuery *);

   /// Is data valid? 
   virtual bool valid() const { return true; }
};

/// @}

#endif
