Move tevent_executor to its own header.

This commit is contained in:
Mark de Wever 2008-11-27 18:10:14 +00:00
parent 7256873571
commit e71347aef3
2 changed files with 247 additions and 223 deletions

View file

@ -0,0 +1,246 @@
/* $Id$ */
/*
copyright (C) 2007 - 2008 by mark de wever <koraq@xs4all.nl>
part of the battle for wesnoth project http://www.wesnoth.org/
this program is free software; you can redistribute it and/or modify
it under the terms of the gnu general public license version 2
or at your option any later version.
this program is distributed in the hope that it will be useful,
but without any warranty.
see the copying file for more details.
*/
#ifndef GUI_WIDGETS_EVENT_EXECUTOR_HPP_INCLUDED
#define GUI_WIDGETS_EVENT_EXECUTOR_HPP_INCLUDED
#include "SDL.h"
namespace gui2 {
class tevent_handler;
/**
* Event execution calls.
*
* Base class with all possible events, most widgets can ignore most of these,
* but they are available. In order to use an event simply override the
* execution function and implement the wanted behaviour. The default behaviour
* defined here is to do nothing.
*
* For more info about the event handling have a look at the tevent_handler
* class which 'translates' sdl events into 'widget' events.
*/
class tevent_executor
{
public:
tevent_executor() :
wants_mouse_hover_(false),
wants_mouse_left_double_click_(false),
wants_mouse_middle_double_click_(false),
wants_mouse_right_double_click_(false)
{}
virtual ~tevent_executor() {}
/***** ***** ***** ***** mouse movement ***** ***** ***** *****/
/**
* The mouse 'enters' the widget.
*
* Entering happens when the mouse moves on a widget it wasn't on before.
* When the mouse is captured by another widget this event does not occur.
*
* @param event_handler The event handler that send the event.
*/
virtual void mouse_enter(tevent_handler& /*event_handler*/) {}
/**
* The mouse moves 'over' the widget.
*
* The mouse either moves over the widget or it has the mouse captured in
* which case every move causes a move event for the capturing widget.
*
* @param event_handler The event handler that send the event.
*/
virtual void mouse_move(tevent_handler& /*event_handler*/) {}
/**
* The mouse 'hovers' over a widget.
*
* If the mouse remains a while without moving over a widget this event can
* be send. This event can be used to show a tooltip.
*
* @param event_handler The event handler that send the event.
*/
virtual void mouse_hover(tevent_handler& /*event_handler*/) {}
/**
* The mouse 'leaves' the widget.
*
* If the mouse is moves off the widget this event is called. When the leave
* occurs while the mouse is captured the event will be send when still of
* the widget if the capture is released. This event is only triggered when
* wants_mouse_hover_ is set.
*
* @param event_handler The event handler that send the event.
*/
virtual void mouse_leave(tevent_handler& /*event_handler*/) {}
/***** ***** ***** ***** mouse left button ***** ***** ***** *****/
/**
* The left mouse button is pressed down.
*
* This is a rather low level event, most of the time you want to have a
* look at mouse_left_button_click instead.
*
* @param event_handler The event handler that send the event.
*/
virtual void mouse_left_button_down(tevent_handler& /*event_handler*/) {}
/**
* The left mouse button is released down.
*
* This is a rather low level event, most of the time you want to have a
* look at mouse_left_button_click instead.
*
* @param event_handler The event handler that send the event.
*/
virtual void mouse_left_button_up(tevent_handler& /*event_handler*/) {}
/**
* The left button is clicked.
*
* This event happens when the left mouse button is pressed and released on
* the same widget. It's execution can be a little delayed when
* wants_mouse_left_double_click_ is set.
*
* @param event_handler The event handler that send the event.
*/
virtual void mouse_left_button_click(tevent_handler& /*event_handler*/) {}
/**
* The left button is double clicked.
*
* This event happens when the left mouse button is pressed and released on
* the same widget twice within a short time. This event will only occur if
* wants_mouse_left_double_click_ is set.
*
* @param event_handler The event handler that send the event.
*/
virtual void mouse_left_button_double_click(tevent_handler& /*event_handler*/) {}
/***** ***** ***** ***** mouse middle button ***** ***** ***** *****/
/** See mouse_left_button_down. */
virtual void mouse_middle_button_down(tevent_handler&) {}
/** See mouse_left_button_up. */
virtual void mouse_middle_button_up(tevent_handler&) {}
/** See mouse_left_button_click. */
virtual void mouse_middle_button_click(tevent_handler&) {}
/** See mouse_left_button_double_click. */
virtual void mouse_middle_button_double_click(tevent_handler&) {}
/***** ***** ***** ***** mouse right button ***** ***** ***** *****/
/** See mouse_left_button_down. */
virtual void mouse_right_button_down(tevent_handler&) {}
/** See mouse_left_button_up. */
virtual void mouse_right_button_up(tevent_handler&) {}
/** See mouse_left_button_click. */
virtual void mouse_right_button_click(tevent_handler&) {}
/** See mouse_left_button_double_click. */
virtual void mouse_right_button_double_click(tevent_handler&) {}
/***** ***** ***** ***** mouse right button ***** ***** ***** *****/
/**
* A key is pressed.
*
* When a key is pressed it's send to the widget that has the focus, if this
* widget doesn't handle the key it is send to the next handler. Some keys
* might get captured before send to the widget (eg F1).
*
* @param event_handler The event handler that send the event.
* @param handled Do we handle the event.
* @param key The SDL key code, needed for special keys.
* @param modifier The keyboard modifiers when the key was
* pressed.
* @param unicode The unicode for the pressed key.
*/
virtual void key_press(tevent_handler& /*event_handler*/, bool& /*handled*/,
SDLKey /*key*/, SDLMod /*modifier*/, Uint16 /*unicode*/) {}
/**
* The F1 key was pressed.
*
* This event is special since we normally want a help when this key is
* pressed.
*
* @param event_handler The event handler that send the event.
*/
virtual void help_key(tevent_handler&) {}
/***** ***** ***** ***** window management ***** ***** ***** *****/
/**
* The window is resized.
*
* @param event_handler The event handler that send the event.
* @param new_width Width of the application window after resizing.
* @param new_height Height of the application window after
* resizing.
*/
virtual void window_resize(tevent_handler&, const unsigned /* new_width */,
const unsigned /* new_height */) {}
/***** ***** ***** setters / getters for members ***** ****** *****/
void set_wants_mouse_hover(const bool hover = true)
{ wants_mouse_hover_ = hover; }
bool wants_mouse_hover() const { return wants_mouse_hover_; }
void set_wants_mouse_left_double_click(const bool click = true)
{ wants_mouse_left_double_click_ = click; }
bool wants_mouse_left_double_click() const
{ return wants_mouse_left_double_click_; }
void set_wants_mouse_middle_double_click(const bool click = true)
{ wants_mouse_middle_double_click_ = click; }
bool wants_mouse_middle_double_click() const
{ return wants_mouse_middle_double_click_; }
tevent_executor& set_wants_mouse_right_double_click(const bool click = true)
{ wants_mouse_right_double_click_ = click; return *this; }
bool wants_mouse_right_double_click() const
{ return wants_mouse_right_double_click_; }
private:
/** Does the widget want a hover event? See mouse_hover. */
bool wants_mouse_hover_;
/**
* Does the widget want a left button double click? See
* mouse_left_button_double_click.
*/
bool wants_mouse_left_double_click_;
/** See wants_mouse_left_double_click_ */
bool wants_mouse_middle_double_click_;
/** See wants_mouse_left_double_click_ */
bool wants_mouse_right_double_click_;
};
} // namespace gui2
#endif

View file

@ -15,241 +15,19 @@
#ifndef GUI_WIDGETS_WIDGET_HPP_INCLUDED
#define GUI_WIDGETS_WIDGET_HPP_INCLUDED
#include "gui/widgets/event_executor.hpp"
#include "gui/widgets/helper.hpp"
#include "sdl_utils.hpp"
#include "wml_exception.hpp"
#include "SDL.h"
#include <string>
#include <cassert>
namespace gui2 {
class tdialog;
class tevent_handler;
class twindow;
/**
* Event execution calls.
*
* Base class with all possible events, most widgets can ignore most of these,
* but they are available. In order to use an event simply override the
* execution function and implement the wanted behaviour. The default behaviour
* defined here is to do nothing.
*
* For more info about the event handling have a look at the tevent_handler
* class which 'translates' sdl events into 'widget' events.
*/
class tevent_executor
{
public:
tevent_executor() :
wants_mouse_hover_(false),
wants_mouse_left_double_click_(false),
wants_mouse_middle_double_click_(false),
wants_mouse_right_double_click_(false)
{}
virtual ~tevent_executor() {}
/***** ***** ***** ***** mouse movement ***** ***** ***** *****/
/**
* The mouse 'enters' the widget.
*
* Entering happens when the mouse moves on a widget it wasn't on before.
* When the mouse is captured by another widget this event does not occur.
*
* @param event_handler The event handler that send the event.
*/
virtual void mouse_enter(tevent_handler& /*event_handler*/) {}
/**
* The mouse moves 'over' the widget.
*
* The mouse either moves over the widget or it has the mouse captured in
* which case every move causes a move event for the capturing widget.
*
* @param event_handler The event handler that send the event.
*/
virtual void mouse_move(tevent_handler& /*event_handler*/) {}
/**
* The mouse 'hovers' over a widget.
*
* If the mouse remains a while without moving over a widget this event can
* be send. This event can be used to show a tooltip.
*
* @param event_handler The event handler that send the event.
*/
virtual void mouse_hover(tevent_handler& /*event_handler*/) {}
/**
* The mouse 'leaves' the widget.
*
* If the mouse is moves off the widget this event is called. When the leave
* occurs while the mouse is captured the event will be send when still of
* the widget if the capture is released. This event is only triggered when
* wants_mouse_hover_ is set.
*
* @param event_handler The event handler that send the event.
*/
virtual void mouse_leave(tevent_handler& /*event_handler*/) {}
/***** ***** ***** ***** mouse left button ***** ***** ***** *****/
/**
* The left mouse button is pressed down.
*
* This is a rather low level event, most of the time you want to have a
* look at mouse_left_button_click instead.
*
* @param event_handler The event handler that send the event.
*/
virtual void mouse_left_button_down(tevent_handler& /*event_handler*/) {}
/**
* The left mouse button is released down.
*
* This is a rather low level event, most of the time you want to have a
* look at mouse_left_button_click instead.
*
* @param event_handler The event handler that send the event.
*/
virtual void mouse_left_button_up(tevent_handler& /*event_handler*/) {}
/**
* The left button is clicked.
*
* This event happens when the left mouse button is pressed and released on
* the same widget. It's execution can be a little delayed when
* wants_mouse_left_double_click_ is set.
*
* @param event_handler The event handler that send the event.
*/
virtual void mouse_left_button_click(tevent_handler& /*event_handler*/) {}
/**
* The left button is double clicked.
*
* This event happens when the left mouse button is pressed and released on
* the same widget twice within a short time. This event will only occur if
* wants_mouse_left_double_click_ is set.
*
* @param event_handler The event handler that send the event.
*/
virtual void mouse_left_button_double_click(tevent_handler& /*event_handler*/) {}
/***** ***** ***** ***** mouse middle button ***** ***** ***** *****/
/** See mouse_left_button_down. */
virtual void mouse_middle_button_down(tevent_handler&) {}
/** See mouse_left_button_up. */
virtual void mouse_middle_button_up(tevent_handler&) {}
/** See mouse_left_button_click. */
virtual void mouse_middle_button_click(tevent_handler&) {}
/** See mouse_left_button_double_click. */
virtual void mouse_middle_button_double_click(tevent_handler&) {}
/***** ***** ***** ***** mouse right button ***** ***** ***** *****/
/** See mouse_left_button_down. */
virtual void mouse_right_button_down(tevent_handler&) {}
/** See mouse_left_button_up. */
virtual void mouse_right_button_up(tevent_handler&) {}
/** See mouse_left_button_click. */
virtual void mouse_right_button_click(tevent_handler&) {}
/** See mouse_left_button_double_click. */
virtual void mouse_right_button_double_click(tevent_handler&) {}
/***** ***** ***** ***** mouse right button ***** ***** ***** *****/
/**
* A key is pressed.
*
* When a key is pressed it's send to the widget that has the focus, if this
* widget doesn't handle the key it is send to the next handler. Some keys
* might get captured before send to the widget (eg F1).
*
* @param event_handler The event handler that send the event.
* @param handled Do we handle the event.
* @param key The SDL key code, needed for special keys.
* @param modifier The keyboard modifiers when the key was
* pressed.
* @param unicode The unicode for the pressed key.
*/
virtual void key_press(tevent_handler& /*event_handler*/, bool& /*handled*/,
SDLKey /*key*/, SDLMod /*modifier*/, Uint16 /*unicode*/) {}
/**
* The F1 key was pressed.
*
* This event is special since we normally want a help when this key is
* pressed.
*
* @param event_handler The event handler that send the event.
*/
virtual void help_key(tevent_handler&) {}
/***** ***** ***** ***** window management ***** ***** ***** *****/
/**
* The window is resized.
*
* @param event_handler The event handler that send the event.
* @param new_width Width of the application window after resizing.
* @param new_height Height of the application window after
* resizing.
*/
virtual void window_resize(tevent_handler&, const unsigned /* new_width */,
const unsigned /* new_height */) {}
/***** ***** ***** setters / getters for members ***** ****** *****/
void set_wants_mouse_hover(const bool hover = true)
{ wants_mouse_hover_ = hover; }
bool wants_mouse_hover() const { return wants_mouse_hover_; }
void set_wants_mouse_left_double_click(const bool click = true)
{ wants_mouse_left_double_click_ = click; }
bool wants_mouse_left_double_click() const
{ return wants_mouse_left_double_click_; }
void set_wants_mouse_middle_double_click(const bool click = true)
{ wants_mouse_middle_double_click_ = click; }
bool wants_mouse_middle_double_click() const
{ return wants_mouse_middle_double_click_; }
tevent_executor& set_wants_mouse_right_double_click(const bool click = true)
{ wants_mouse_right_double_click_ = click; return *this; }
bool wants_mouse_right_double_click() const
{ return wants_mouse_right_double_click_; }
private:
/** Does the widget want a hover event? See mouse_hover. */
bool wants_mouse_hover_;
/**
* Does the widget want a left button double click? See
* mouse_left_button_double_click.
*/
bool wants_mouse_left_double_click_;
/** See wants_mouse_left_double_click_ */
bool wants_mouse_middle_double_click_;
/** See wants_mouse_left_double_click_ */
bool wants_mouse_right_double_click_;
};
/**
* Base class for all widgets.
*