gui/widgets/toggle_panel.cpp

Go to the documentation of this file.
00001 /* $Id: toggle_panel.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/toggle_panel.hpp"
00019 
00020 #include "foreach.hpp"
00021 #include "gui/auxiliary/log.hpp"
00022 #include "gui/auxiliary/widget_definition/toggle_panel.hpp"
00023 #include "gui/auxiliary/window_builder/toggle_panel.hpp"
00024 #include "gui/widgets/settings.hpp"
00025 #include "gui/widgets/window.hpp"
00026 #include "sound.hpp"
00027 
00028 #include <boost/bind.hpp>
00029 
00030 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
00031 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
00032 
00033 namespace gui2 {
00034 
00035 REGISTER_WIDGET(toggle_panel)
00036 
00037 ttoggle_panel::ttoggle_panel()
00038     : tpanel(COUNT)
00039     , state_(ENABLED)
00040     , retval_(0)
00041     , callback_state_change_(0)
00042     , callback_mouse_left_double_click_()
00043 {
00044     set_wants_mouse_left_double_click();
00045 
00046     connect_signal<event::MOUSE_ENTER>(boost::bind(
00047                 &ttoggle_panel::signal_handler_mouse_enter, this, _2, _3));
00048     connect_signal<event::MOUSE_LEAVE>(boost::bind(
00049                 &ttoggle_panel::signal_handler_mouse_leave, this, _2, _3));
00050 
00051     connect_signal<event::LEFT_BUTTON_CLICK>(boost::bind(
00052                   &ttoggle_panel::signal_handler_pre_left_button_click
00053                 , this, _2)
00054             , event::tdispatcher::back_pre_child);
00055     connect_signal<event::LEFT_BUTTON_CLICK>(boost::bind(
00056                 &ttoggle_panel::signal_handler_left_button_click
00057                     , this, _2, _3));
00058     connect_signal<event::LEFT_BUTTON_CLICK>(boost::bind(
00059                   &ttoggle_panel::signal_handler_left_button_click
00060                 , this, _2, _3)
00061             , event::tdispatcher::back_post_child);
00062     connect_signal<event::LEFT_BUTTON_DOUBLE_CLICK>(boost::bind(
00063                   &ttoggle_panel::signal_handler_left_button_double_click
00064                 , this, _2, _3));
00065     connect_signal<event::LEFT_BUTTON_DOUBLE_CLICK>(boost::bind(
00066                   &ttoggle_panel::signal_handler_left_button_double_click
00067                 , this, _2, _3)
00068             , event::tdispatcher::back_post_child);
00069 }
00070 
00071 void ttoggle_panel::set_child_members(const std::map<std::string /* widget id */, string_map>& data)
00072 {
00073     // typedef boost problem work around.
00074     typedef std::pair<std::string, string_map> hack ;
00075     foreach(const hack& item, data) {
00076         tcontrol* control = dynamic_cast<tcontrol*>(find(item.first, false));
00077         if(control) {
00078             control->set_members(item.second);
00079         }
00080     }
00081 }
00082 
00083 void ttoggle_panel::set_active(const bool active)
00084 {
00085     if(active) {
00086         if(get_value()) {
00087             set_state(ENABLED_SELECTED);
00088         } else {
00089             set_state(ENABLED);
00090         }
00091     } else {
00092         if(get_value()) {
00093             set_state(DISABLED_SELECTED);
00094         } else {
00095             set_state(DISABLED);
00096         }
00097     }
00098 }
00099 
00100 SDL_Rect ttoggle_panel::get_client_rect() const
00101 {
00102     boost::intrusive_ptr<const ttoggle_panel_definition::tresolution> conf =
00103         boost::dynamic_pointer_cast<const ttoggle_panel_definition::tresolution>(config());
00104     assert(conf);
00105 
00106     SDL_Rect result = get_rect();
00107     result.x += conf->left_border;
00108     result.y += conf->top_border;
00109     result.w -= conf->left_border + conf->right_border;
00110     result.h -= conf->top_border + conf->bottom_border;
00111 
00112     return result;
00113 }
00114 
00115 tpoint ttoggle_panel::border_space() const
00116 {
00117     boost::intrusive_ptr<const ttoggle_panel_definition::tresolution> conf =
00118         boost::dynamic_pointer_cast<const ttoggle_panel_definition::tresolution>(config());
00119     assert(conf);
00120 
00121     return tpoint(conf->left_border + conf->right_border,
00122         conf->top_border + conf->bottom_border);
00123 }
00124 
00125 void ttoggle_panel::set_value(const bool selected)
00126 {
00127     if(selected == get_value()) {
00128         return;
00129     }
00130 
00131     if(selected) {
00132         set_state(static_cast<tstate>(state_ + ENABLED_SELECTED));
00133     } else {
00134         set_state(static_cast<tstate>(state_ - ENABLED_SELECTED));
00135     }
00136 }
00137 
00138 void ttoggle_panel::set_retval(const int retval)
00139 {
00140     retval_ = retval;
00141 }
00142 
00143 void ttoggle_panel::set_state(const tstate state)
00144 {
00145     if(state == state_) {
00146         return;
00147     }
00148 
00149     state_ = state;
00150     set_dirty(true);
00151 
00152     boost::intrusive_ptr<const ttoggle_panel_definition::tresolution> conf =
00153         boost::dynamic_pointer_cast<const ttoggle_panel_definition::tresolution>(config());
00154     assert(conf);
00155 }
00156 
00157 const std::string& ttoggle_panel::get_control_type() const
00158 {
00159     static const std::string type = "toggle_panel";
00160     return type;
00161 }
00162 
00163 void ttoggle_panel::signal_handler_mouse_enter(
00164         const event::tevent event, bool& handled)
00165 {
00166     DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
00167 
00168     if(get_value()) {
00169         set_state(FOCUSSED_SELECTED);
00170     } else {
00171         set_state(FOCUSSED);
00172     }
00173     handled = true;
00174 }
00175 
00176 void ttoggle_panel::signal_handler_mouse_leave(
00177         const event::tevent event, bool& handled)
00178 {
00179     DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
00180 
00181     if(get_value()) {
00182         set_state(ENABLED_SELECTED);
00183     } else {
00184         set_state(ENABLED);
00185     }
00186     handled = true;
00187 }
00188 
00189 void ttoggle_panel::signal_handler_pre_left_button_click(
00190         const event::tevent event)
00191 {
00192     DBG_GUI_E << get_control_type() << "[" << id() << "]: " << event << ".\n";
00193 
00194     set_value(true);
00195     if(callback_state_change_) {
00196         callback_state_change_(this);
00197     }
00198 }
00199 
00200 void ttoggle_panel::signal_handler_left_button_click(
00201         const event::tevent event, bool& handled)
00202 {
00203     DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
00204 
00205     sound::play_UI_sound(settings::sound_toggle_panel_click);
00206 
00207     if(get_value()) {
00208         set_state(ENABLED);
00209     } else {
00210         set_state(ENABLED_SELECTED);
00211     }
00212 
00213     if(callback_state_change_) {
00214         callback_state_change_(this);
00215     }
00216     handled = true;
00217 }
00218 
00219 void ttoggle_panel::signal_handler_left_button_double_click(
00220         const event::tevent event, bool& handled)
00221 {
00222     DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
00223 
00224     if (retval_) {
00225         twindow* window = get_window();
00226         assert(window);
00227 
00228         window->set_retval(retval_);
00229     }
00230 
00231     if (callback_mouse_left_double_click_) {
00232         callback_mouse_left_double_click_(this);
00233     }
00234     handled = true;
00235 }
00236 
00237 } // namespace gui2
00238 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

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