gui/widgets/button.cpp

Go to the documentation of this file.
00001 /* $Id: button.cpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2008 - 2012 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 as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 #define GETTEXT_DOMAIN "wesnoth-lib"
00017 
00018 #include "gui/widgets/button.hpp"
00019 
00020 #include "gui/auxiliary/log.hpp"
00021 #include "gui/auxiliary/widget_definition/button.hpp"
00022 #include "gui/auxiliary/window_builder/button.hpp"
00023 #include "gui/widgets/settings.hpp"
00024 #include "gui/widgets/window.hpp"
00025 #include "sound.hpp"
00026 
00027 #include <boost/bind.hpp>
00028 
00029 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
00030 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
00031 
00032 namespace gui2 {
00033 
00034 REGISTER_WIDGET(button)
00035 
00036 tbutton::tbutton()
00037     : tcontrol(COUNT)
00038     , tclickable_()
00039     , state_(ENABLED)
00040     , retval_(0)
00041 {
00042     connect_signal<event::MOUSE_ENTER>(boost::bind(
00043                 &tbutton::signal_handler_mouse_enter, this, _2, _3));
00044     connect_signal<event::MOUSE_LEAVE>(boost::bind(
00045                 &tbutton::signal_handler_mouse_leave, this, _2, _3));
00046 
00047     connect_signal<event::LEFT_BUTTON_DOWN>(boost::bind(
00048                 &tbutton::signal_handler_left_button_down, this, _2, _3));
00049     connect_signal<event::LEFT_BUTTON_UP>(boost::bind(
00050                 &tbutton::signal_handler_left_button_up, this, _2, _3));
00051     connect_signal<event::LEFT_BUTTON_CLICK>(boost::bind(
00052                 &tbutton::signal_handler_left_button_click, this, _2, _3));
00053 }
00054 
00055 void tbutton::set_state(const tstate state)
00056 {
00057     if(state != state_) {
00058         state_ = state;
00059         set_dirty(true);
00060     }
00061 }
00062 
00063 const std::string& tbutton::get_control_type() const
00064 {
00065     static const std::string type = "button";
00066     return type;
00067 }
00068 
00069 void tbutton::signal_handler_mouse_enter(
00070         const event::tevent event, bool& handled)
00071 {
00072     DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
00073 
00074     set_state(FOCUSSED);
00075     handled = true;
00076 }
00077 
00078 void tbutton::signal_handler_mouse_leave(
00079         const event::tevent event, bool& handled)
00080 {
00081     DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
00082 
00083     set_state(ENABLED);
00084     handled = true;
00085 }
00086 
00087 void tbutton::signal_handler_left_button_down(
00088         const event::tevent event, bool& handled)
00089 {
00090     DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
00091 
00092     twindow* window = get_window();
00093     if(window) {
00094         window->mouse_capture();
00095     }
00096 
00097     set_state(PRESSED);
00098     handled = true;
00099 }
00100 
00101 void tbutton::signal_handler_left_button_up(
00102         const event::tevent event, bool& handled)
00103 {
00104     DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
00105 
00106     set_state(FOCUSSED);
00107     handled = true;
00108 }
00109 
00110 void tbutton::signal_handler_left_button_click(
00111         const event::tevent event, bool& handled)
00112 {
00113     DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
00114 
00115     sound::play_UI_sound(settings::sound_button_click);
00116 
00117     // If a button has a retval do the default handling.
00118     if(retval_ != 0) {
00119         twindow* window = get_window();
00120         if(window) {
00121             window->set_retval(retval_);
00122             return;
00123         }
00124     }
00125 
00126     handled = true;
00127 }
00128 
00129 } // namespace gui2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:02:54 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs