Author: mordante
Date: Sat Apr  5 11:31:33 2008
New Revision: 25557

URL: http://svn.gna.org/viewcvs/wesnoth?rev=25557&view=rev
Log:
Proof of concept; to use buttons to return a value for a dialog.

Modified:
    trunk/src/gui/dialogs/addon_connect.cpp
    trunk/src/gui/dialogs/addon_connect.hpp
    trunk/src/gui/widgets/button.cpp
    trunk/src/gui/widgets/button.hpp
    trunk/src/gui/widgets/widget.cpp
    trunk/src/gui/widgets/widget.hpp
    trunk/src/gui/widgets/window.cpp
    trunk/src/gui/widgets/window.hpp
    trunk/src/gui/widgets/window_builder.cpp

Modified: trunk/src/gui/dialogs/addon_connect.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/dialogs/addon_connect.cpp?rev=25557&r1=25556&r2=25557&view=diff
==============================================================================
--- trunk/src/gui/dialogs/addon_connect.cpp (original)
+++ trunk/src/gui/dialogs/addon_connect.cpp Sat Apr  5 11:31:33 2008
@@ -35,7 +35,7 @@
        std::cerr << "\n\n\nHello world.\n\n\n";
 }
 
-void addon_connect(CVideo& video, const std::string& server)
+int addon_connect(CVideo& video, const std::string& server)
 {
 
        gui2::init();
@@ -43,7 +43,7 @@
 
 //     window.connect("connect", &hello);
 
-       window.show(true);
+       return window.show(true);
 }
 
 

Modified: trunk/src/gui/dialogs/addon_connect.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/dialogs/addon_connect.hpp?rev=25557&r1=25556&r2=25557&view=diff
==============================================================================
--- trunk/src/gui/dialogs/addon_connect.hpp (original)
+++ trunk/src/gui/dialogs/addon_connect.hpp Sat Apr  5 11:31:33 2008
@@ -21,7 +21,7 @@
 
 namespace gui2 {
 
-       void addon_connect(CVideo& video, const std::string& server);
+       int addon_connect(CVideo& video, const std::string& server);
 
 } // namespace gui2
 

Modified: trunk/src/gui/widgets/button.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/button.cpp?rev=25557&r1=25556&r2=25557&view=diff
==============================================================================
--- trunk/src/gui/widgets/button.cpp (original)
+++ trunk/src/gui/widgets/button.cpp Sat Apr  5 11:31:33 2008
@@ -14,6 +14,8 @@
 
 #include "gui/widgets/button.hpp"
 
+#include "gui/widgets/window.hpp"
+
 #define DBG_G LOG_STREAM(debug, gui)
 #define LOG_G LOG_STREAM(info, gui)
 #define WRN_G LOG_STREAM(warn, gui)
@@ -112,6 +114,17 @@
 void tbutton::mouse_left_button_click(tevent_handler&) 
 { 
        DBG_G_E << "Button: left mouse button click.\n"; 
+
+       // If a button has a retval do the default handling.
+       if(retval_ != 0) {
+               twindow* window = get_window();
+               if(window) {
+                       window->set_retval(retval_);
+                       return;
+               }
+       }
+
+       // Do the custom handling (not implemented yet) FIXME
 }
 
 void tbutton::mouse_left_button_double_click(tevent_handler&) 
@@ -169,6 +182,20 @@
        set_y(origin.y);
        set_width(definition_->default_width);
        set_height(definition_->default_height);
+}
+
+tbutton::RETVAL tbutton::get_retval_by_id(const std::string& id)
+{
+       //! Note it might change to a map later depending on the number
+       //! of items.
+       if(id == "ok") {
+               return OK;
+       } else if(id == "cancel") {
+               return CANCEL;
+       } else {
+               return NONE;
+       }
+
 }
 
 void tbutton::set_state(tstate state)

Modified: trunk/src/gui/widgets/button.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/button.hpp?rev=25557&r1=25556&r2=25557&view=diff
==============================================================================
--- trunk/src/gui/widgets/button.hpp (original)
+++ trunk/src/gui/widgets/button.hpp Sat Apr  5 11:31:33 2008
@@ -35,7 +35,8 @@
                canvas_pressed_(),
                canvas_focussed_(),
                state_(ENABLED),
-               definition_()
+               definition_(),
+               retval_(0)
                {
                }
 
@@ -62,6 +63,23 @@
 
        void set_best_size(const tpoint& origin);
 
+       void set_retval(const int retval) { retval_ = retval; }
+
+       //! Default button values, values are subject to change.
+       //! Note this might be moved somewhere else since it will
+       //! force people to include the button, while it should
+       //! be and implementation detail for most callers.
+       enum RETVAL {
+               NONE = 0,                      //!< Dialog is closed with no 
return 
+                                              //!< value, should be rare but 
eg a
+                                              //!< message popup can do it.
+               OK = -1,                       //!< Dialog is closed with ok 
button.
+               CANCEL = -2,                   //!< Dialog is closed with the 
cancel 
+                                              //!<  button.
+               };
+
+       //! Gets the retval for the default buttons.
+       static RETVAL get_retval_by_id(const std::string& id);
 protected:
        
 private:
@@ -80,6 +98,8 @@
        std::vector<tbutton_definition::tresolution>::const_iterator 
definition_;
 
        void resolve_definition();
+
+       int retval_;
 };
 
 

Modified: trunk/src/gui/widgets/widget.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/widget.cpp?rev=25557&r1=25556&r2=25557&view=diff
==============================================================================
--- trunk/src/gui/widgets/widget.cpp (original)
+++ trunk/src/gui/widgets/widget.cpp Sat Apr  5 11:31:33 2008
@@ -16,6 +16,7 @@
 
 #include "filesystem.hpp"
 #include "gui/widgets/settings.hpp"
+#include "gui/widgets/window.hpp"
 #include "log.hpp"
 #include "serialization/parser.hpp"
 #include "serialization/preprocessor.hpp"
@@ -73,6 +74,20 @@
        return stream;
 }
 
+twindow* twidget::get_window()
+{
+       // Go up into the parent tree until we find the top level
+       // parent, we can also be the toplevel so start with
+       // ourselves instead of our parent.
+       twidget* result = this;
+       while(result->parent_) {
+               result = result->parent_;
+       }
+
+       // on error dynamic_cast return 0 which is what we want.
+       return dynamic_cast<twindow*>(result);
+}
+
 #if 0
 tcontainer::~tcontainer()
 {

Modified: trunk/src/gui/widgets/widget.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/widget.hpp?rev=25557&r1=25556&r2=25557&view=diff
==============================================================================
--- trunk/src/gui/widgets/widget.hpp (original)
+++ trunk/src/gui/widgets/widget.hpp Sat Apr  5 11:31:33 2008
@@ -156,6 +156,8 @@
        bool wants_mouse_middle_double_click_;
        bool wants_mouse_right_double_click_;
 };
+
+class twindow;
 
 //! Base class for all widgets.
 //! This is a non visible widget but it does have dimentions and size hints.
@@ -238,6 +240,9 @@
                        coordinate.y >= y_ && coordinate.y < (y_ + h_) ? this : 
0;
        }
 
+       //! The toplevel item should always be a window if not null is returned
+       twindow* get_window();
+
 protected:     
        virtual void set_dirty(const bool dirty = true) 
        { 

Modified: trunk/src/gui/widgets/window.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/window.cpp?rev=25557&r1=25556&r2=25557&view=diff
==============================================================================
--- trunk/src/gui/widgets/window.cpp (original)
+++ trunk/src/gui/widgets/window.cpp Sat Apr  5 11:31:33 2008
@@ -54,6 +54,7 @@
        tevent_handler(),
        video_(video),
        status_(NEW),
+       retval_(0),
        need_layout_(true),
        restorer_(),
        canvas_background_(),
@@ -65,7 +66,7 @@
        set_height(h);
 }
 
-void twindow::show(const bool restore, void* /*flip_function*/)
+int twindow::show(const bool restore, void* /*flip_function*/)
 {
        log_scope2(gui_draw, "Window: show.");  
 
@@ -75,7 +76,7 @@
 
        }
        
-       // We cut a piec of the screen and use that, that way all coordinates
+       // We cut a piece of the screen and use that, that way all coordinates
        // are relative to the window.
        SDL_Rect rect = get_rect();
        restorer_ = get_surface_portion(video_.getSurface(), rect);
@@ -143,6 +144,8 @@
                update_rect(get_rect());
                flip();
        }
+
+       return retval_;
 }
 
 void twindow::layout(const SDL_Rect position)

Modified: trunk/src/gui/widgets/window.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/window.hpp?rev=25557&r1=25556&r2=25557&view=diff
==============================================================================
--- trunk/src/gui/widgets/window.hpp (original)
+++ trunk/src/gui/widgets/window.hpp Sat Apr  5 11:31:33 2008
@@ -56,12 +56,17 @@
 
        // 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);
+       int show(const bool restore = true, void* flip_function = 0);
 
        // layout the window
        void layout(const SDL_Rect position);
 
        enum tstatus{ NEW, SHOWING, REQUEST_CLOSE, CLOSED };
+
+       void close() { status_ = REQUEST_CLOSE; }
+
+       void set_retval(const int retval, const bool close_window = true)
+               { retval_ = retval; if(close_window) close(); }
 
        void set_width(const unsigned width);
 
@@ -87,6 +92,9 @@
 
        tstatus status_;
 
+       // return value of the window, 0 default.
+       int retval_;
+
        //! When set the form needs a full layout redraw cycle.
        bool need_layout_;
 

Modified: trunk/src/gui/widgets/window_builder.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/window_builder.cpp?rev=25557&r1=25556&r2=25557&view=diff
==============================================================================
--- trunk/src/gui/widgets/window_builder.cpp (original)
+++ trunk/src/gui/widgets/window_builder.cpp Sat Apr  5 11:31:33 2008
@@ -253,6 +253,12 @@
        button->set_definition(definition);
        button->set_label(label);
 
+       //fixme the reader for button needs to read return_value
+       //if 0 test the id to be a common id.
+       
+       //FIXME needs to be a map as lookup
+       button->set_retval(tbutton::get_retval_by_id(id));
+
        DBG_G << "Window builder: placed button '" << id << "' with defintion 
'" 
                << definition << "'.\n";
 


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

Reply via email to