Author: mordante
Date: Thu Apr 30 22:56:41 2009
New Revision: 35360

URL: http://svn.gna.org/viewcvs/wesnoth?rev=35360&view=rev
Log:
Add the autoclose feature for dialogs/windows.

This might not be really useful for normal usage, but allows the
unit tests to show a window. (This will be implemented later.)

Modified:
    trunk/src/events.hpp
    trunk/src/gui/dialogs/dialog.cpp
    trunk/src/gui/dialogs/dialog.hpp
    trunk/src/gui/widgets/event_handler.cpp
    trunk/src/gui/widgets/window.cpp
    trunk/src/gui/widgets/window.hpp

Modified: trunk/src/events.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/events.hpp?rev=35360&r1=35359&r2=35360&view=diff
==============================================================================
--- trunk/src/events.hpp (original)
+++ trunk/src/events.hpp Thu Apr 30 22:56:41 2009
@@ -23,6 +23,7 @@
 #define HOVER_EVENT (SDL_USEREVENT + 1)
 #define HOVER_REMOVE_POPUP_EVENT (SDL_USEREVENT + 2)
 #define DRAW_EVENT (SDL_USEREVENT + 3)
+#define CLOSE_WINDOW_EVENT (SDL_USEREVENT + 4)
 
 namespace events
 {

Modified: trunk/src/gui/dialogs/dialog.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/dialogs/dialog.cpp?rev=35360&r1=35359&r2=35360&view=diff
==============================================================================
--- trunk/src/gui/dialogs/dialog.cpp (original)
+++ trunk/src/gui/dialogs/dialog.cpp Thu Apr 30 22:56:41 2009
@@ -29,7 +29,7 @@
        }
 }
 
-void tdialog::show(CVideo& video)
+void tdialog::show(CVideo& video, const unsigned auto_close_time)
 {
        std::auto_ptr<twindow> window(build_window(video));
        assert(window.get());
@@ -40,7 +40,7 @@
 
        pre_show(video, *window);
 
-       retval_ = window->show(restore_);
+       retval_ = window->show(restore_, auto_close_time);
 
        if(retval_ ==  twindow::OK) {
                finalize_fields(*window);

Modified: trunk/src/gui/dialogs/dialog.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/dialogs/dialog.hpp?rev=35360&r1=35359&r2=35360&view=diff
==============================================================================
--- trunk/src/gui/dialogs/dialog.hpp (original)
+++ trunk/src/gui/dialogs/dialog.hpp Thu Apr 30 22:56:41 2009
@@ -43,8 +43,18 @@
 
        virtual ~tdialog();
 
-       /** Shows the window */
-       void show(CVideo& video);
+       /** 
+        * Shows the window.
+        *
+        * @param video               The video which contains the surface to 
draw
+        *                            upon.
+        * @param auto_close_time     The time in ms after which the dialog will
+        *                            automatically close, if 0 it doesn't 
close.
+        *                            @note the timeout is a minimum time and
+        *                            there's no quarantee about how fast it 
closes
+        *                            after the minimum.
+        */
+       void show(CVideo& video, const unsigned auto_close_time = 0);
 
        /***** ***** ***** setters / getters for members ***** ****** *****/
 

Modified: trunk/src/gui/widgets/event_handler.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/event_handler.cpp?rev=35360&r1=35359&r2=35360&view=diff
==============================================================================
--- trunk/src/gui/widgets/event_handler.cpp (original)
+++ trunk/src/gui/widgets/event_handler.cpp Thu Apr 30 22:56:41 2009
@@ -256,6 +256,15 @@
 
                case DRAW_EVENT:
                        get_window().draw();
+                       break;
+
+               case CLOSE_WINDOW_EVENT:
+                       {
+                               twindow* window = 
twindow::window_instance(event.user.code);
+                               if(window) {
+                                       window->set_retval(twindow::AUTO_CLOSE);
+                               }
+                       }
                        break;
 
                case SDL_KEYDOWN:

Modified: trunk/src/gui/widgets/window.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/window.cpp?rev=35360&r1=35359&r2=35360&view=diff
==============================================================================
--- trunk/src/gui/widgets/window.cpp (original)
+++ trunk/src/gui/widgets/window.cpp Thu Apr 30 22:56:41 2009
@@ -84,6 +84,33 @@
 
        SDL_PushEvent(&event);
        return draw_interval;
+}
+
+/**
+ * SDL_AddTimer() callback for delay_event.
+ *
+ * @param event                   The event to push in the event queue.
+ *
+ * @return                        The new timer interval (always 0).
+ */
+static Uint32 delay_event_callback(const Uint32, void* event)
+{
+       SDL_PushEvent(static_cast<SDL_Event*>(event));
+       return 0;
+}
+
+/**
+ * Allows an event to be delayed a certain amount of time.
+ *
+ * @note the delay is the minimum time, after the time has passed the event
+ * will be pushed in the SDL event queue, so it might delay more.
+ *
+ * @param event                   The event to delay.
+ * @param delay                   The number of ms to delay the event.
+ */
+static void delay_event(const SDL_Event& event, const Uint32 delay)
+{
+       SDL_AddTimer(delay, delay_event_callback, new SDL_Event(event));
 }
 
 /**
@@ -322,7 +349,7 @@
        }
 }
 
-int twindow::show(const bool restore)
+int twindow::show(const bool restore, const unsigned auto_close_timeout)
 {
        log_scope2(log_gui_draw, "Window: show.");
 
@@ -347,6 +374,25 @@
         */
        invalidate_layout();
        suspend_drawing_ = false;
+
+       if(auto_close_timeout) {
+               // Make sure we're drawn before we try to close ourselves, 
which can
+               // happen if the timeout is small.
+               draw();
+
+               SDL_Event event;
+               SDL_UserEvent data;
+
+               data.type = CLOSE_WINDOW_EVENT;
+               data.code = tmanager::instance().get_id(*this);
+               data.data1 = NULL;
+               data.data2 = NULL;
+
+               event.type = CLOSE_WINDOW_EVENT;
+               event.user = data;
+
+               delay_event(event, auto_close_timeout);
+       }
 
        // Start our loop drawing will happen here as well.
        for(status_ = SHOWING; status_ != REQUEST_CLOSE; ) {

Modified: trunk/src/gui/widgets/window.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/window.hpp?rev=35360&r1=35359&r2=35360&view=diff
==============================================================================
--- trunk/src/gui/widgets/window.hpp (original)
+++ trunk/src/gui/widgets/window.hpp Thu Apr 30 22:56:41 2009
@@ -133,11 +133,17 @@
         *
         * @param restore             Restore the screenarea the window was on
         *                            after closing it?
+        * @param auto_close_time     The time in ms after which the window will
+        *                            automatically close, if 0 it doesn't 
close.
+        *                            @note the timeout is a minimum time and
+        *                            there's no quarantee about how fast it 
closes
+        *                            after the minimum.
         *
         * @returns                   The close code of the window, predefined
         *                            values are listed in tretval.
         */
-       int show(const bool restore = true);
+       int show(const bool restore = true,
+                       const unsigned auto_close_timeout = 0);
 
        /**
         * Draws the window.


_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits

Reply via email to