event_handler.hpp

Go to the documentation of this file.
00001 /* $Id: event_handler.hpp 26681 2008-05-18 06:05:59Z mordante $ */
00002 /*
00003    Copyright (C) 2007 - 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 //! @file event_handler.hpp
00016 //! Contains the information with an event.
00017 
00018 #ifndef __GUI_WIDGETS_EVENT_INFO_HPP_INCLUDED__
00019 #define __GUI_WIDGETS_EVENT_INFO_HPP_INCLUDED__
00020 
00021 #include "events.hpp"
00022 #include "gui/widgets/helper.hpp"
00023 #include "gui/widgets/widget.hpp"
00024 
00025 #include "SDL.h"
00026 
00027 class t_string;
00028 
00029 namespace gui2{
00030 
00031 class twindow;
00032 
00033 class tevent_handler : public events::handler
00034 {
00035 public:
00036     tevent_handler();
00037 
00038     virtual ~tevent_handler() { leave(); }
00039 
00040     void process_events() { events::pump(); }
00041 
00042     //! Implement events::handler::handle_event().
00043     void handle_event(const SDL_Event& event);
00044 
00045     virtual twindow& get_window() = 0;
00046     virtual const twindow& get_window() const = 0;
00047 
00048     /** See twidget::find_widget() for the description. */
00049     virtual twidget* find_widget(const tpoint& coordinate, 
00050             const bool must_be_active) = 0;
00051 
00052     /** The const version of find_widget. */
00053     virtual const twidget* find_widget(const tpoint& coordinate, 
00054         const bool must_be_active) const = 0;
00055 
00056     void mouse_capture(const bool capture = true);
00057     void keyboard_capture(twidget* widget) { keyboard_focus_ = widget; }
00058 
00059     tpoint get_mouse() const;
00060 
00061     //! We impement the handling of the tip, but call the do functions
00062     //! which are virtual.
00063     void show_tooltip(const t_string& tooltip, const unsigned timeout);
00064     void remove_tooltip();
00065     void show_help_popup(const t_string& help_popup, const unsigned timeout);
00066     void remove_help_popup();
00067 
00068 private:
00069 
00070     struct tmouse_button {
00071         
00072         tmouse_button(const std::string& name, 
00073             void (tevent_executor::*down) (tevent_handler&),
00074             void (tevent_executor::*up) (tevent_handler&),
00075             void (tevent_executor::*click) (tevent_handler&),
00076             void (tevent_executor::*double_click) (tevent_handler&),
00077             bool (tevent_executor::*wants_double_click) () const) :
00078                 last_click_stamp(0),
00079                 focus(0),
00080                 name(name),
00081                 down(down),
00082                 up(up),
00083                 click(click),
00084                 double_click(double_click),
00085                 wants_double_click(wants_double_click),
00086                 is_down(false)
00087             {}
00088 
00089         //! The time of the last click used for double clicking.
00090         Uint32 last_click_stamp;
00091 
00092         //! If the mouse isn't captured we need to verify the up
00093         //! is on the same widget as the down so we send a proper
00094         //! click, also needed to send the up to the right widget.
00095         twidget* focus;
00096 
00097         //! used for debug messages.
00098         const std::string name;
00099 
00100         //! Pointers to member functions, this way we can call the proper
00101         //! function indirect without writing a case for which button to
00102         //! use.
00103         void (tevent_executor::*down) (tevent_handler&);
00104         void (tevent_executor::*up) (tevent_handler&);
00105         void (tevent_executor::*click) (tevent_handler&);
00106         void (tevent_executor::*double_click) (tevent_handler&);
00107         bool (tevent_executor::*wants_double_click) () const;
00108 
00109         //! Is the button down?
00110         bool is_down;
00111     };
00112 
00113     //! we create a new event context so we're always modal.
00114     //! Maybe this has to change, but not sure yet.
00115     events::event_context event_context_;
00116 
00117     int mouse_x_;                      //! The current mouse x.
00118     int mouse_y_;                      //! The current mouse y.
00119 
00120     tmouse_button left_;
00121     tmouse_button middle_;
00122     tmouse_button right_;
00123 
00124     bool hover_pending_;               //! Is there a hover event pending?
00125     unsigned hover_id_;                //! Id of the pending hover event.
00126     SDL_Rect hover_box_;               //! The area the mouse can move in, moving outside
00127                                        //! invalidates the pending hover event.
00128     bool had_hover_;                   //! A widget only gets one hover event per enter cycle. 
00129 
00130     //! The widget that created the tooltip / tooltip.
00131     twidget* tooltip_;
00132     twidget* help_popup_;
00133 
00134 
00135     twidget* mouse_focus_;
00136     bool mouse_captured_;
00137 
00138     twidget* keyboard_focus_;
00139 
00140     void mouse_enter(const SDL_Event& event, twidget* mouse_over);
00141     void mouse_move(const SDL_Event& event, twidget* mouse_over);
00142     void mouse_hover(const SDL_Event& event, twidget* mouse_over);
00143     void mouse_leave(const SDL_Event& event, twidget* mouse_over);
00144 
00145 
00146     void mouse_button_down(const SDL_Event& event, twidget* mouse_over, tmouse_button& button);
00147     void mouse_button_up(const SDL_Event& event, twidget* mouse_over, tmouse_button& button);
00148     void mouse_click(twidget* widget, tmouse_button& button);
00149 
00150     void set_hover(const bool test_on_widget = false);
00151 
00152     void key_down(const SDL_Event& event);
00153 
00154     virtual void do_show_tooltip(const tpoint& location, const t_string& tooltip) = 0;
00155     virtual void do_remove_tooltip() = 0;
00156     virtual void do_show_help_popup(const tpoint& location, const t_string& help_popup) = 0;
00157     virtual void do_remove_help_popup() = 0;
00158 };
00159 
00160 } // namespace gui2
00161 
00162 #endif

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