button.hpp

Go to the documentation of this file.
00001 /* $Id: button.hpp 26682 2008-05-18 07:14:57Z mordante $ */
00002 /*
00003    copyright (C) 2008 by mark de wever <koraq@xs4all.nl>
00004    part of the battle for wesnoth project http://www.wesnoth.org/
00005 
00006    this program is free software; you can redistribute it and/or modify
00007    it under the terms of the gnu general public license version 2
00008    or at your option any later version.
00009    this program is distributed in the hope that it will be useful,
00010    but without any warranty.
00011 
00012    see the copying file for more details.
00013 */
00014 
00015 #ifndef __GUI_WIDGETS_BUTTON_HPP_INCLUDED__
00016 #define __GUI_WIDGETS_BUTTON_HPP_INCLUDED__
00017 
00018 #include "gui/widgets/control.hpp"
00019 
00020 namespace gui2 {
00021 
00022 // Class for a simple push button
00023 class tbutton : public tcontrol
00024 {
00025 public:
00026     tbutton() : 
00027         tcontrol(COUNT),
00028         state_(ENABLED),
00029         retval_(0),
00030         callback_mouse_left_click_(0)
00031     {
00032     }
00033 
00034     void mouse_enter(tevent_handler&);
00035     void mouse_leave(tevent_handler&);
00036 
00037     void mouse_left_button_down(tevent_handler& event);
00038     void mouse_left_button_up(tevent_handler&);
00039     void mouse_left_button_click(tevent_handler&);
00040 
00041     void set_retval(const int retval) { retval_ = retval; }
00042 
00043     //! Default button values, values are subject to change.
00044     //! Note this might be moved somewhere else since it will
00045     //! force people to include the button, while it should
00046     //! be and implementation detail for most callers.
00047     enum RETVAL {
00048         NONE = 0,                      //!< Dialog is closed with no return 
00049                                        //!< value, should be rare but eg a
00050                                        //!< message popup can do it.
00051         OK = -1,                       //!< Dialog is closed with ok button.
00052         CANCEL = -2                    //!< Dialog is closed with the cancel 
00053                                        //!<  button.
00054         };
00055 
00056     //! Gets the retval for the default buttons.
00057     static RETVAL get_retval_by_id(const std::string& id);
00058 
00059     //! Inherited from tcontrol.
00060     void set_active(const bool active) 
00061         { if(get_active() != active) set_state(active ? ENABLED : DISABLED); };
00062     bool get_active() const { return state_ != DISABLED; }
00063     unsigned get_state() const { return state_; }
00064 
00065     void set_callback_mouse_left_click(void (*callback) (twidget*))
00066         { callback_mouse_left_click_ = callback; }
00067 
00068 private:
00069     //! Note the order of the states must be the same as defined in settings.hpp.
00070     enum tstate { ENABLED, DISABLED, PRESSED, FOCUSSED, COUNT };
00071 
00072     void set_state(tstate state);
00073     tstate state_;
00074  
00075     int retval_;
00076 
00077     /** 
00078      * This callback is used when the control gets a left click. Except when the
00079      * button has a retval_, then retval_ is set.
00080      */
00081     void (*callback_mouse_left_click_) (twidget*);
00082 
00083     //! Inherited from tcontrol.
00084     const std::string& get_control_type() const 
00085         { static const std::string type = "button"; return type; }
00086 };
00087 
00088 
00089 } // namespace gui2
00090 
00091 #endif
00092 

Generated by doxygen 1.5.5 on 23 May 2008 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs