Author: mordante
Date: Sat Mar  5 14:54:24 2011
New Revision: 48755

URL: http://svn.gna.org/viewcvs/wesnoth?rev=48755&view=rev
Log:
Allow a field to be linked to a variable.

This allows variables to be directly updated instead of writing setter
and getter functions, which are then used in the constructor. This
callbacks method is still supported in case that's easier to handle the
changes.

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

Modified: trunk/src/gui/dialogs/field.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/dialogs/field.hpp?rev=48755&r1=48754&r2=48755&view=diff
==============================================================================
--- trunk/src/gui/dialogs/field.hpp (original)
+++ trunk/src/gui/dialogs/field.hpp Sat Mar  5 14:54:24 2011
@@ -39,6 +39,13 @@
 {
 public:
 
+       /**
+        * Constructor.
+        *
+        * @param id                  The id of the widget to connect to the 
window.
+        *                            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)
@@ -208,17 +215,59 @@
 {
 public:
 
+       /**
+        * Constructor.
+        *
+        * @param id                  The id of the widget to connect to the 
window.
+        *                            A widget can only be connected once.
+        * @param optional            Is the widget optional?
+        * @param callback_load_value A callback function which is called when 
the
+        *                            window is shown. This callback returns the
+        *                            initial value of the field.
+        * @param callback_save_value A callback function which is called when 
the
+        *                            window closed with the OK button. The
+        *                            callback is executed with the new value of
+        *                            the field. It's meant to set the value of
+        *                            some variable in the engine after the 
window
+        *                            is closed with OK.
+        */
        tfield(const std::string& id,
                        const bool optional,
                        T (*callback_load_value) (),
                        void (*callback_save_value) (CT value)) :
                tfield_(id, optional),
                value_(T()),
+               link_(value_),
                callback_load_value_(callback_load_value),
                callback_save_value_(callback_save_value)
        {
        }
 
+       /**
+        * Constructor.
+        *
+        * @param id                  The id of the widget to connect to the 
window.
+        *                            A widget can only be connected once.
+        * @param optional            Is the widget optional?
+        * @param linked_variable     The variable which is linked to the field.
+        *                            * Upon loading its value is used as 
initial
+        *                              value of the widget.
+        *                            * Upon closing:
+        *                              * with OK its value is set to the value 
of
+        *                                the widget.
+        *                              * else, its value is undefined.
+        */
+       tfield(const std::string& id
+                       , const bool optional
+                       , T& linked_variable)
+               : tfield_(id, optional)
+               , value_(T())
+               , link_(linked_variable)
+               , callback_load_value_(NULL)
+               , callback_save_value_(NULL)
+       {
+       }
+
        /** Inherited from tfield_. */
        void widget_restore(twindow& window)
        {
@@ -266,6 +315,8 @@
         *
         * This function gets the value of the widget and stores that in the
         * internal cache, then that value is returned.
+        *
+        * @deprecated Use references to a variable instead.
         *
         * @param window              The window containing the widget.
         *
@@ -284,6 +335,8 @@
         * used after the widget no longer exists. The cache is normally updated
         * when the window is closed with succes.
         *
+        * @deprecated Use references to a variable instead.
+        *
         * @returns                   The currently value of the internal cache.
         */
        T get_cache_value()
@@ -300,6 +353,14 @@
        T value_;
 
        /**
+        * The variable linked to the field.
+        *
+        * When set determines the initial value and the final value is stored 
here
+        * in the finallizer.
+        */
+       T& link_;
+
+       /**
         * The callback function to load the value.
         *
         * This is used to load the initial value of the widget, if defined.
@@ -313,6 +374,8 @@
 
                if(callback_load_value_) {
                        value_ = callback_load_value_();
+               } else {
+                       value_ = link_;
                }
 
                restore(window);
@@ -325,6 +388,8 @@
 
                if(callback_save_value_) {
                        callback_save_value_(value_);
+               } else {
+                       link_ = value_;
                }
        }
 
@@ -393,6 +458,15 @@
                {
                }
 
+       tfield_bool(const std::string& id
+                       , const bool optional
+                       , bool& linked_variable
+                       , void (*callback_change) (twidget* widget))
+               : tfield<bool, gui2::tselectable_>(id, optional, 
linked_variable)
+               , callback_change_(callback_change)
+       {
+       }
+
 private:
 
        /** Overridden from tfield_. */
@@ -424,6 +498,16 @@
                {
                }
 
+       tfield_text(const std::string& id
+                       , const bool optional
+                       , std::string& linked_variable)
+               : tfield<std::string, ttext_, const std::string&>(
+                               id
+                               , optional
+                               , linked_variable)
+       {
+       }
+
 private:
        /** Overridden from tfield_. */
        void finalize_specialized(twindow& window)


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

Reply via email to