Let tevent_handler derive from events::handler...

...and twindow derive from tevent_handler instead of
events::handler. This means that the event handling is now done in
tevent_handler instead of in twindow.
This commit is contained in:
Mark de Wever 2008-03-24 07:24:25 +00:00
parent 948553bb4c
commit 87705e8d94
4 changed files with 47 additions and 43 deletions

View File

@ -20,8 +20,8 @@
#include "gui/widgets/event_handler.hpp"
#include "config.hpp"
#include "events.hpp"
#include "gui/widgets/widget.hpp"
#include "gui/widgets/window.hpp"
#include "log.hpp"
#include "serialization/parser.hpp"
#include "variable.hpp"
@ -57,6 +57,8 @@ static Uint32 hover_callback(Uint32 interval, void *param)
//! blocker is used.
tevent_handler::tevent_handler() :
// fixme get state at construction
events::handler(false), // don't join we haven't created a context yet
event_context_(),
mouse_x_(-1),
mouse_y_(-1),
mouse_left_button_down_(false),
@ -77,23 +79,33 @@ tevent_handler::tevent_handler() :
assert(false);
}
}
// The event context is created now we join it.
join();
}
void tevent_handler::handle_event(const SDL_Event& event, twidget* mouse_over)
void tevent_handler::handle_event(const SDL_Event& event)
{
twidget* mouse_over = 0;
switch(event.type) {
case SDL_MOUSEMOTION:
mouse_x_ = event.motion.x;
mouse_y_ = event.motion.y;
mouse_over =
get_widget(get_window().client_position(tpoint(mouse_x_, mouse_y_)));
mouse_move(event, mouse_over);
break;
case SDL_MOUSEBUTTONDOWN:
mouse_x_ = event.button.x;
mouse_y_ = event.button.y;
mouse_over =
get_widget(get_window().client_position(tpoint(mouse_x_, mouse_y_)));
switch(event.button.button) {
case SDL_BUTTON_LEFT :
@ -111,6 +123,8 @@ void tevent_handler::handle_event(const SDL_Event& event, twidget* mouse_over)
mouse_x_ = event.button.x;
mouse_y_ = event.button.y;
mouse_over =
get_widget(get_window().client_position(tpoint(mouse_x_, mouse_y_)));
switch(event.button.button) {
@ -140,7 +154,6 @@ void tevent_handler::handle_event(const SDL_Event& event, twidget* mouse_over)
WRN_G_E << "Unhandled event " << static_cast<Uint32>(event.type) << ".\n";
break;
}
}
void tevent_handler::mouse_capture(const bool capture)

View File

@ -18,23 +18,39 @@
#ifndef __GUI_WIDGETS_EVENT_INFO_HPP_INCLUDED__
#define __GUI_WIDGETS_EVENT_INFO_HPP_INCLUDED__
#include "events.hpp"
#include "SDL.h"
namespace gui2{
class tpoint;
class twidget;
class twindow;
class tevent_handler
class tevent_handler : public events::handler
{
public:
tevent_handler();
void handle_event(const SDL_Event& event, twidget* mouse_over);
virtual ~tevent_handler() { leave(); }
void process_events() { events::pump(); }
//! Implement events::handler::handle_event().
void handle_event(const SDL_Event& event);
virtual twindow& get_window() = 0;
virtual twidget* get_widget(const tpoint& coordinate) = 0;
void mouse_capture(const bool capture = true);
private:
//! we create a new event context so we're always modal.
//! Maybe this has to change, but not sure yet.
events::event_context event_context_;
int mouse_x_; //! The current mouse x.
int mouse_y_; //! The current mouse y.

View File

@ -51,11 +51,9 @@ namespace gui2{
twindow::twindow(CVideo& video,
const int x, const int y, const int w, const int h) :
tpanel(),
events::handler(false), // don't join we haven't created a context yet
tevent_handler(),
video_(video),
status_(NEW),
event_info_(),
event_context_(),
need_layout_(true),
restorer_(),
canvas_background_(),
@ -65,9 +63,6 @@ twindow::twindow(CVideo& video,
set_y(y);
set_width(w);
set_height(h);
// The event context is created now we join it.
join();
}
void twindow::show(const bool restore, void* /*flip_function*/)
@ -88,7 +83,7 @@ void twindow::show(const bool restore, void* /*flip_function*/)
// Start our loop drawing will happen here as well.
for(status_ = SHOWING; status_ != REQUEST_CLOSE; ) {
events::pump();
process_events();
// fixme manual destroy
if(status_ == REQUEST_CLOSE) {
@ -198,18 +193,6 @@ void twindow::flip()
video_.flip();
}
//! Implement events::handler::handle_event().
void twindow::handle_event(const SDL_Event& event)
{
if(event.type == SDL_MOUSEBUTTONDOWN || event.type == SDL_MOUSEBUTTONUP) {
event_info_.handle_event(event, get_widget(tpoint(event.button.x - get_x(), event.button.y - get_y())));
} else if (event.type == SDL_MOUSEMOTION) {
event_info_.handle_event(event, get_widget(tpoint(event.motion.x - get_x(), event.motion.y - get_y())));
} else {
event_info_.handle_event(event, 0);
}
}
void twindow::window_resize(tevent_handler&,
const unsigned new_width, const unsigned new_height)
{
@ -221,7 +204,7 @@ void twindow::window_resize(tevent_handler&,
void twindow::resolve_definition()
{
if(definition_ == std::vector<twindow_definition::tresolution>::const_iterator()) {
definition_ = get_window(definition());
definition_ = gui2::get_window(definition());
canvas_background_ = definition_->background.canvas;
canvas_background_.set_width(get_width());

View File

@ -23,6 +23,8 @@
#include "gui/widgets/event_handler.hpp"
#include "gui/widgets/grid.hpp"
#include "gui/widgets/settings.hpp"
// The following due to tpoint.
#include "gui/widgets/widget.hpp"
#include "sdl_utils.hpp"
#include "video.hpp"
@ -47,17 +49,11 @@ namespace gui2{
// event aan ons te sturen, oftewel een movemove can dit genereren indien gewenst
//
// mogelijk dit ook gebruiken in de toekomst als aansturing van flip()
class twindow : public tpanel, public events::handler/*, public virtual tevent_executor */
class twindow : public tpanel, public tevent_handler
{
public:
twindow(CVideo& video, const int x, const int y, const int w, const int h);
~twindow()
{
// We have to leave the event context before it's destroyed.
leave();
}
// show the window
// The flip function is the disp_.flip() if ommitted the video_flip() is used
void show(const bool restore = true, void* flip_function = 0);
@ -71,6 +67,13 @@ public:
void set_height(const unsigned height);
twindow& get_window() { return *this; }
twidget* get_widget(const tpoint& coordinate) { return tgrid::get_widget(coordinate); }
tpoint client_position(const tpoint& screen_position) const
{ return tpoint(screen_position.x - get_x(), screen_position.y - get_y()); }
protected:
private:
@ -81,17 +84,6 @@ private:
tstatus status_;
tevent_handler event_info_;
/***** The event processing stuff *****/
//! we create a new event context so we're always modal.
//! Maybe this has to change, but not sure yet.
events::event_context event_context_;
//! Implement events::handler::handle_event().
void handle_event(const SDL_Event& event);
void window_resize(tevent_handler&,
const unsigned new_width, const unsigned new_height);