gui/widgets/repeating_button.cpp

Go to the documentation of this file.
00001 /* $Id: repeating_button.cpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2009 - 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/repeating_button.hpp"
00019 
00020 #include "gui/auxiliary/log.hpp"
00021 #include "gui/auxiliary/timer.hpp"
00022 #include "gui/auxiliary/widget_definition/repeating_button.hpp"
00023 #include "gui/auxiliary/window_builder/repeating_button.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(repeating_button)
00036 
00037 trepeating_button::trepeating_button()
00038     : tcontrol(COUNT)
00039     , tclickable_()
00040     , state_(ENABLED)
00041     , repeat_timer_(0)
00042 {
00043     connect_signal<event::MOUSE_ENTER>(boost::bind(
00044                 &trepeating_button::signal_handler_mouse_enter, this, _2, _3));
00045     connect_signal<event::MOUSE_LEAVE>(boost::bind(
00046                 &trepeating_button::signal_handler_mouse_leave, this, _2, _3));
00047 
00048     connect_signal<event::LEFT_BUTTON_DOWN>(boost::bind(
00049                   &trepeating_button::signal_handler_left_button_down
00050                 , this
00051                 , _2
00052                 , _3));
00053     connect_signal<event::LEFT_BUTTON_UP>(boost::bind(
00054                   &trepeating_button::signal_handler_left_button_up
00055                 , this
00056                 , _2
00057                 , _3));
00058 }
00059 
00060 trepeating_button::~trepeating_button()
00061 {
00062     if(repeat_timer_) {
00063         remove_timer(repeat_timer_);
00064     }
00065 }
00066 
00067 void trepeating_button::connect_signal_mouse_left_down(
00068         const event::tsignal_function& signal)
00069 {
00070     connect_signal<event::LEFT_BUTTON_DOWN>(signal);
00071 }
00072 
00073 void trepeating_button::disconnect_signal_mouse_left_down(
00074         const event::tsignal_function& signal)
00075 {
00076     disconnect_signal<event::LEFT_BUTTON_DOWN>(signal);
00077 }
00078 
00079 void trepeating_button::set_state(const tstate state)
00080 {
00081     if(state != state_) {
00082         state_ = state;
00083         set_dirty(true);
00084 
00085         if(state_ == DISABLED && repeat_timer_) {
00086             remove_timer(repeat_timer_);
00087             repeat_timer_ = 0;
00088         }
00089     }
00090 }
00091 
00092 const std::string& trepeating_button::get_control_type() const
00093 {
00094     static const std::string type = "repeating_button";
00095     return type;
00096 }
00097 
00098 void trepeating_button::signal_handler_mouse_enter(
00099         const event::tevent event, bool& handled)
00100 {
00101     DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
00102 
00103     set_state(FOCUSSED);
00104     handled = true;
00105 }
00106 
00107 void trepeating_button::signal_handler_mouse_leave(
00108         const event::tevent event, bool& handled)
00109 {
00110     DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
00111 
00112     set_state(ENABLED);
00113     handled = true;
00114 }
00115 
00116 void trepeating_button::signal_handler_left_button_down(
00117         const event::tevent event, bool& handled)
00118 {
00119     DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
00120 
00121     // If the timer isn't set it's the initial down event.
00122     if(!repeat_timer_) {
00123 
00124         // mimic the old gui and only play the sound once.
00125         sound::play_UI_sound(settings::sound_button_click);
00126 
00127         twindow* window = get_window();
00128         if(window) {
00129             repeat_timer_ = add_timer(
00130                       settings::repeat_button_repeat_time
00131                     , boost::bind(
00132                           &tdispatcher::fire
00133                         , window
00134                         , event::LEFT_BUTTON_DOWN
00135                         , boost::ref(*this))
00136                     , true);
00137 
00138             window->mouse_capture();
00139         }
00140 
00141         set_state(PRESSED);
00142     }
00143 
00144     handled = true;
00145 }
00146 
00147 void trepeating_button::signal_handler_left_button_up(
00148         const event::tevent event, bool& handled)
00149 {
00150     DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
00151 
00152     if(repeat_timer_) {
00153         remove_timer(repeat_timer_);
00154         repeat_timer_ = 0;
00155     }
00156 
00157     if(get_active()) {
00158         set_state(FOCUSSED);
00159     }
00160     handled = true;
00161 }
00162 
00163 } // 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:55 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs