/***************************************************************************
                          SIMPLY POWERFUL TOOLKIT (SPTK)
                          cbuttongroup.h  -  description
                             -------------------
    begin                : Thi Dec 12 2002
    copyright            : (C) 2000-2006 by Alexey S.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 __CButtonGroup_H__
#define __CButtonGroup_H__

#include <FL/Fl_Group.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Input_.H>
#include <FL/Fl_Button.H>
#include <sptk3/CIntList.h>
#include <sptk3/CControl.h>
#include <sptk3/CInput.h>
#include <sptk3/CStrings.h>

namespace sptk {

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

   /// @brief Base class for CCheckButtons and CRadioButtons.
   ///
   /// Implements the most important data communication methods for these button groups.
	class SP_EXPORT CButtonGroup : public CControl {
		int         m_maxHeight;		///< Required group height
	protected:
		std::string m_lastValue;		///< Last value of the group (last list of choices)
		CStrings    m_buttonLabels;	///< Button labels for the buttons inside
		Fl_Button  *m_otherButton;		///< The 'Other' Button if requested (add '*' in the button list)
		CInput_    *m_otherInput;		///< The 'Other' Input if requested (add '*' in the button list)

      /// Internal callback processing
		virtual void controlDataChanged();

      /// Finds a button by label
      /// @param buttonLabel const char *, button label
      /// @returns button index, or -1 if not found
		int  buttonIndex(const char *buttonLabel);

      /// Deselects all buttons
		void deselectAllButtons();

      /// Selects button by index
		void selectButton(int choice);

      /// Creates button. Should be implemented in the derived class.
		virtual Fl_Button *createButton(const char *label,int sz=10,CLayoutAlign layoutAlignment=SP_ALIGN_TOP) = 0L;

      /// Constructor initializer
		void ctor_init();

	protected:

      /// SPTK-style constructor
      /// @param label const char *, the widget label
      /// @param layoutSize int, the size of widget in layout
      /// @param layoutAlignment CLayoutAlign, widget align in the layout
		CButtonGroup(const char * label=0,int layoutSize=20,CLayoutAlign layoutAlignment=SP_ALIGN_TOP);

#ifdef __COMPATIBILITY_MODE__    
      /// FLTK-style constructor
      /// @param x int, widget x-coordinate
      /// @param y int, widget y-coordinate
      /// @param w int, widget width
      /// @param h int, widget height
      /// @param label int, optional widget label
		CButtonGroup(int,int,int,int,const char * = 0);
#endif    

	public:
      /// @brief Sets the list of the buttons.
		///
      /// See CStrings for details on creating and filling the list.
      /// @param buttonList CStrings, list of the buttons
		void buttons(const CStrings& buttonList);

      /// @brief Clears the list of buttons.
		virtual void clearButtons();

      /// @brief Returns the currently selected button(s) as pipe ('|') separated string
		virtual CVariant data() const;

      /// @brief Sets the currently selected button(s)
		///
		/// Buttons are presented as pipe ('|') separated string.
		/// If the button group allows only one button to be selected at a time 
		/// (like radio buttons), only the first item of the string will be used.
		virtual void data(const CVariant v);

      /// @brief Loads the the currently selected button(s)
		///
		/// Buttons should be presented as pipe ('|') separated string.
		virtual void load(CQuery *);

      /// @brief Saves the the currently selected button(s)
		///
		/// Buttons are presented as pipe ('|') separated string
		virtual void save(CQuery *);

      /// @brief Tells if the the current data content is valid
		///
		/// Always true for this widget.
		virtual bool valid() const { return true; }

      /// @brief Computes the preferred size of the button group based on its contents
		virtual void preferredSize(int& w,int& h);
	};
	/// @}
}
#endif
