Author: mordante
Date: Thu Apr 30 22:56:35 2009
New Revision: 35359
URL: http://svn.gna.org/viewcvs/wesnoth?rev=35359&view=rev
Log:
Add helper class to track all window instances.
This will be used to send event to the proper window, this allows
windows to post messages to themselves and let them delay for a certain
amount of time.
Modified:
trunk/src/gui/widgets/window.cpp
trunk/src/gui/widgets/window.hpp
Modified: trunk/src/gui/widgets/window.cpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/window.cpp?rev=35359&r1=35358&r2=35359&view=diff
==============================================================================
--- trunk/src/gui/widgets/window.cpp (original)
+++ trunk/src/gui/widgets/window.cpp Thu Apr 30 22:56:35 2009
@@ -84,6 +84,88 @@
SDL_PushEvent(&event);
return draw_interval;
+}
+
+/**
+ * Small helper class to get an unique id for every window instance.
+ *
+ * This is used to send event to the proper window, this allows windows to post
+ * messages to themselves and let them delay for a certain amount of time.
+ */
+class tmanager
+{
+ tmanager();
+public:
+
+ static tmanager& instance();
+
+ void add(twindow& window);
+
+ void remove(twindow& window);
+
+ unsigned get_id(twindow& window);
+
+ twindow* window(const unsigned id);
+
+private:
+
+ // The number of active window should be rather small
+ // so keep it simple and don't add a reverse lookup map.
+ std::map<unsigned, twindow*> windows_;
+};
+
+tmanager::tmanager()
+ : windows_()
+{
+}
+
+tmanager& tmanager::instance()
+{
+ static tmanager window_manager;
+ return window_manager;
+}
+
+void tmanager::add(twindow& window)
+{
+ static unsigned id;
+ ++id;
+ windows_[id] = &window;
+}
+
+void tmanager::remove(twindow& window)
+{
+ for(std::map<unsigned, twindow*>::iterator itor = windows_.begin();
+ itor != windows_.end(); ++itor) {
+
+ if(itor->second == &window) {
+ windows_.erase(itor);
+ return;
+ }
+ }
+ assert(false);
+}
+
+unsigned tmanager::get_id(twindow& window)
+{
+ for(std::map<unsigned, twindow*>::iterator itor = windows_.begin();
+ itor != windows_.end(); ++itor) {
+
+ if(itor->second == &window) {
+ return itor->first;
+ }
+ }
+ assert(false);
+}
+
+twindow* tmanager::window(const unsigned id)
+{
+ std::map<unsigned, twindow*>::iterator itor = windows_.find(id);
+
+ if(itor == windows_.end()) {
+ return NULL;
+ } else {
+ return itor->second;
+ }
}
} // namespace
@@ -139,6 +221,24 @@
help_popup_.set_definition("default");
help_popup_.set_parent(this);
help_popup_.set_visible(twidget::HIDDEN);
+
+ tmanager::instance().add(*this);
+}
+
+twindow::~twindow()
+{
+ tmanager::instance().remove(*this);
+
+#ifdef DEBUG_WINDOW_LAYOUT_GRAPHS
+
+ delete debug_layout_;
+
+#endif
+}
+
+twindow* twindow::window_instance(const unsigned handle)
+{
+ return tmanager::instance().window(handle);
}
void twindow::update_screen_size()
@@ -990,10 +1090,6 @@
}
#ifdef DEBUG_WINDOW_LAYOUT_GRAPHS
-twindow::~twindow()
-{
- delete debug_layout_;
-}
void twindow::generate_dot_file(const std::string& generator,
const unsigned domain)
Modified: trunk/src/gui/widgets/window.hpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/window.hpp?rev=35359&r1=35358&r2=35359&view=diff
==============================================================================
--- trunk/src/gui/widgets/window.hpp (original)
+++ trunk/src/gui/widgets/window.hpp Thu Apr 30 22:56:35 2009
@@ -75,6 +75,8 @@
const unsigned vertical_placement,
const std::string& definition);
+ ~twindow();
+
/**
* Update the size of the screen variables in settings.
*
@@ -84,6 +86,15 @@
* event.
*/
static void update_screen_size();
+
+ /**
+ * Returns the intance of a window.
+ *
+ * @param handle The instance id of the window.
+ *
+ * @returns The window or NULL.
+ */
+ static twindow* window_instance(const unsigned handle);
/**
* Default return values.
@@ -521,8 +532,6 @@
tdebug_layout_graph* debug_layout_;
public:
- // The destructor only needs to delete the debug_layout_ so declared
here.
- ~twindow();
/** wrapper for tdebug_layout_graph::generate_dot_file. */
void generate_dot_file(
_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits