Author: mordante
Date: Sun Mar 20 19:26:12 2011
New Revision: 48959

URL: http://svn.gna.org/viewcvs/wesnoth?rev=48959&view=rev
Log:
Let a field store its linked widget.

This allows to remove a lot of window parameters and simplifying parts
of the code. Also added a mandatory field, next to the optional, the
latter is deprecated.

Modified:
    trunk/src/gui/dialogs/dialog.cpp
    trunk/src/gui/dialogs/dialog.hpp
    trunk/src/gui/dialogs/field.hpp

Modified: trunk/src/gui/dialogs/dialog.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/dialogs/dialog.cpp?rev=48959&r1=48958&r2=48959&view=diff
==============================================================================
--- trunk/src/gui/dialogs/dialog.cpp (original)
+++ trunk/src/gui/dialogs/dialog.cpp Sun Mar 20 19:26:12 2011
@@ -64,9 +64,7 @@
         */
        events::discard(SDL_EVENTMASK(DOUBLE_CLICK_EVENT));
 
-       if(retval_ ==  twindow::OK || always_save_fields_) {
-               finalize_fields(*window);
-       }
+       finalize_fields(*window, (retval_ ==  twindow::OK || 
always_save_fields_));
 
        post_show(*window);
 
@@ -226,6 +224,7 @@
 void tdialog::init_fields(twindow& window)
 {
        foreach(tfield_* field, fields_) {
+               field->attach_to_window(window);
                field->widget_init(window);
        }
 
@@ -236,10 +235,13 @@
        }
 }
 
-void tdialog::finalize_fields(twindow& window)
+void tdialog::finalize_fields(twindow& window, const bool save_fields)
 {
        foreach(tfield_* field, fields_) {
-               field->widget_finalize(window);
+               if(save_fields) {
+                       field->widget_finalize(window);
+               }
+               field->detach_from_window();
        }
 }
 

Modified: trunk/src/gui/dialogs/dialog.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/dialogs/dialog.hpp?rev=48959&r1=48958&r2=48959&view=diff
==============================================================================
--- trunk/src/gui/dialogs/dialog.hpp (original)
+++ trunk/src/gui/dialogs/dialog.hpp Sun Mar 20 19:26:12 2011
@@ -438,8 +438,9 @@
         * Saving only happens if a callback handler is installed.
         *
         * @param window              The window which has been shown.
-        */
-       virtual void finalize_fields(twindow& window);
+        * @param save_fields         Does the value in the fields need to be 
saved?
+        */
+       virtual void finalize_fields(twindow& window, const bool save_fields);
 };
 
 } // namespace gui2

Modified: trunk/src/gui/dialogs/field.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/dialogs/field.hpp?rev=48959&r1=48958&r2=48959&view=diff
==============================================================================
--- trunk/src/gui/dialogs/field.hpp (original)
+++ trunk/src/gui/dialogs/field.hpp Sun Mar 20 19:26:12 2011
@@ -37,6 +37,10 @@
 
 /**
  * Abstract base class for the fields.
+ *
+ * @note In this context a widget is a @ref gui2::tcontrol and not a @ref
+ * gui2::twidget. This name widget is a generic name and fits, however some
+ * functions used are first declared in a control.
  */
 class tfield_
 {
@@ -49,13 +53,37 @@
         *                            A widget can only be connected once.
         * @param optional            Is the widget optional?
         */
-       tfield_(const std::string& id, const bool optional) :
-               id_(id),
-               optional_(optional)
+       tfield_(const std::string& id, const bool optional)
+               : id_(id)
+               , optional_(optional)
+               , mandatory_(!optional)
+               , widget_(NULL)
        {
        }
 
        virtual ~tfield_() {}
+
+       /**
+        * Attaches the field to a window.
+        *
+        * When attached the widget which we're a wrapper around is stored 
linked
+        * in here.
+        *
+        * @warning After attaching the window must remain a valid. Before the
+        * window is destroyed the @ref detach_from_window function must be 
called.
+        *
+        * @todo Most functions that have a window parameter only use it to get 
the
+        * widget. Evaluate and remove the window parameter where applicable.
+        *
+        * @pre widget_ == NULL
+        *
+        * @param window               The window to be attached to.
+        */
+       void attach_to_window(twindow& window)
+       {
+               assert(!widget_);
+               widget_ = find_widget<tcontrol>(&window, id(), false, 
mandatory_);
+       }
 
        /**
         * Initializes the widget.
@@ -101,6 +129,19 @@
        }
 
        /**
+        * Detaches the field from a window.
+        *
+        * @pre widget_ != NULL || !mandatory_
+        *
+        * @param window               The window to be attached to.
+        */
+       void detach_from_window()
+       {
+               assert(!mandatory_ || widget_);
+               widget_ = NULL;
+       }
+
+       /**
         * Saves a widget.
         *
         * It can be a window must be recreated, in that case the state needs 
to be
@@ -157,34 +198,38 @@
                widget->set_active(enable);
        }
 
-       /**
-        * Returns the widget associated with the field.
-        *
-        * @param window              The window containing the widget.
-        *
-        * @returns                   The widget NULL if not found and optional.
-        */
-       twidget* widget(twindow& window) {
-
-               twidget* widget = dynamic_cast<tcontrol*>(window.find(id(), 
false));
-               VALIDATE(optional_ || widget, missing_widget(id()));
-
-               return widget;
-       }
-
        /***** ***** ***** setters / getters for members ***** ****** *****/
 
        const std::string& id() const { return id_; }
 
        bool is_optional() const { return optional_; }
+
+       tcontrol* widget()
+       {
+               return widget_;
+       }
+
+       const tcontrol* widget() const
+       {
+               return widget_;
+       }
 
 private:
        /** The id field of the widget, should be unique in a window. */
        const std::string id_;
 
+       /**
+        * Is the widget optional or mandatory in this window.
+        *
+        * @deprecated Use @ref mandatory_ instead.
+        */
+       const bool optional_;
+
        /** Is the widget optional or mandatory in this window. */
-       bool optional_;
-
+       const bool mandatory_;
+
+       /** The widget attached to the field. */
+       tcontrol* widget_;
 
        /** See widget_init. */
        virtual void init_generic(twindow& window) = 0;


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

Reply via email to