From 19db0ef00be433e6a0e818e546c3b8a3829b28db Mon Sep 17 00:00:00 2001 From: Mark de Wever Date: Wed, 12 Mar 2008 19:12:39 +0000 Subject: [PATCH] Make the library resize aware. --- src/gui/widgets/settings.cpp | 9 +++++++ src/gui/widgets/settings.hpp | 8 +++++- src/gui/widgets/window.cpp | 50 ++++++++++++++++-------------------- src/gui/widgets/window.hpp | 6 +++++ 4 files changed, 44 insertions(+), 29 deletions(-) diff --git a/src/gui/widgets/settings.cpp b/src/gui/widgets/settings.cpp index 4c66dbeea5f..ea2029092fa 100644 --- a/src/gui/widgets/settings.cpp +++ b/src/gui/widgets/settings.cpp @@ -24,6 +24,7 @@ #include "log.hpp" #include "serialization/parser.hpp" #include "serialization/preprocessor.hpp" +#include "video.hpp" #define DBG_GUI LOG_STREAM(debug, widget) @@ -43,10 +44,18 @@ unsigned tbutton::default_height_ = 10; config tbutton::default_enabled_draw_ = config(); config tbutton::default_mouse_over_draw_ = config(); config tbutton::default_pressed_draw_ = config(); +unsigned screen_width = 0; +unsigned screen_height = 0; void load_settings() { LOG_GUI << "Init gui\n"; + + + const SDL_Rect rect = screen_area(); + screen_width = rect.w; + screen_height = rect.h; + config file; const std::string& filename = "data/hardwired/gui.cfg"; try { diff --git a/src/gui/widgets/settings.hpp b/src/gui/widgets/settings.hpp index a7874a7bd67..c4cb690d318 100644 --- a/src/gui/widgets/settings.hpp +++ b/src/gui/widgets/settings.hpp @@ -22,7 +22,13 @@ class config; namespace gui2 { -void load_settings(); + //! Loads the setting for the theme. + void load_settings(); + + //! The screen resolution should be available for all widgets since their + //! drawing method will depend on it. + extern unsigned screen_width; + extern unsigned screen_height; } // namespace gui2 diff --git a/src/gui/widgets/window.cpp b/src/gui/widgets/window.cpp index ab4bda519de..f8faf79b127 100644 --- a/src/gui/widgets/window.cpp +++ b/src/gui/widgets/window.cpp @@ -18,6 +18,7 @@ #include "gui/widgets/window.hpp" #include "config.hpp" +#include "gui/widgets/settings.hpp" #include "log.hpp" #include "serialization/parser.hpp" #include "variable.hpp" @@ -38,7 +39,8 @@ twindow::twindow(CVideo& video, video_(video), status_(NEW), event_info_(), - event_context_() + event_context_(), + need_layout_(true) { set_x(x); set_y(y); @@ -70,35 +72,11 @@ void twindow::show(const bool restore, void* /*flip_function*/) restorer = screen; } - // draw + // draw Window hack SDL_Rect Xrect = {0, 0, screen->w, screen->h}; fill_rect_alpha(Xrect, 0, 128, screen); - // draw our children - //fixme make draw const and use a const iterator -// for(std::multimap::iterator itor = -// children_().begin(); itor != children_().end(); ++itor) { - - layout(Xrect); - for(tsizer::iterator itor = begin(); itor != end(); ++itor) { - - if(! *itor) { - continue; - } - - log_scope2(widget, "Draw child"); - - itor->draw(screen); - } - - rect = get_rect(); - SDL_BlitSurface(screen, 0, video_.getSurface(), &rect); - update_rect(get_rect()); - flip(); - - DBG_GUI << "Drawing finished\n"; - - // start our loop + // Start our loop drawing will happen here as well. for(status_ = SHOWING; status_ != REQUEST_CLOSE; ) { events::pump(); @@ -115,7 +93,11 @@ void twindow::show(const bool restore, void* /*flip_function*/) break; } - if(dirty()) { + if(dirty() || need_layout_) { + if(need_layout_) { + DBG_GUI << "Layout.\n"; + layout(Xrect); + } #if 0 // Darkening for debugging redraw. SDL_Rect Xrect = {0, 0, screen->w, screen->h}; @@ -153,6 +135,8 @@ void twindow::show(const bool restore, void* /*flip_function*/) void twindow::layout(const SDL_Rect position) { + need_layout_ = false; + tpoint best_size = get_best_size(); if(best_size.x < position.w && best_size.y < position.h) { @@ -197,6 +181,9 @@ void twindow::handle_event(const SDL_Event& event) case SDL_MOUSEMOTION: handle_event_mouse_move(event); break; + case SDL_VIDEORESIZE: + handle_event_resize(event); + break; } } @@ -297,5 +284,12 @@ void twindow::handle_event_mouse_move(const SDL_Event& event) event_info_.mouse_focus = mouse_focus; } +void twindow::handle_event_resize(const SDL_Event& event) +{ + screen_width = event.resize.w; + screen_height = event.resize.h; + need_layout_ = true; +} + } // namespace gui2 diff --git a/src/gui/widgets/window.hpp b/src/gui/widgets/window.hpp index ae497d30224..0f8b671e8fe 100644 --- a/src/gui/widgets/window.hpp +++ b/src/gui/widgets/window.hpp @@ -96,6 +96,12 @@ private: //! Handler for a mouse movement. void handle_event_mouse_move(const SDL_Event& event); + + //! Handler for a resize of the main window. + void handle_event_resize(const SDL_Event& event); + + //! When set the form needs a full layout redraw cycle. + bool need_layout_; };