Author: mordante
Date: Sat Mar 5 14:54:27 2011
New Revision: 48756
URL: http://svn.gna.org/viewcvs/wesnoth?rev=48756&view=rev
Log:
Improve the dialogs.
- Allow the linked variable fields to be used.
- Let show return a status boolean.
Also wrote some documentation about how to improve dialog code. During a
code review saw a lot of small wrapper functions for setters and getters
of variables. The new idea is to send these variables per reference and
let the dialog update them so the caller only needs to check the execute
status. The next commit has an example of its usage the other dialogs
will be converted during their code review.
Modified:
trunk/src/gui/dialogs/dialog.cpp
trunk/src/gui/dialogs/dialog.hpp
Modified: trunk/src/gui/dialogs/dialog.cpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/dialogs/dialog.cpp?rev=48756&r1=48755&r2=48756&view=diff
==============================================================================
--- trunk/src/gui/dialogs/dialog.cpp (original)
+++ trunk/src/gui/dialogs/dialog.cpp Sat Mar 5 14:54:27 2011
@@ -31,10 +31,10 @@
}
}
-void tdialog::show(CVideo& video, const unsigned auto_close_time)
+bool tdialog::show(CVideo& video, const unsigned auto_close_time)
{
if(video.faked()) {
- return;
+ return false;
}
std::auto_ptr<twindow> window(build_window(video));
@@ -69,6 +69,8 @@
}
post_show(*window);
+
+ return retval_ == twindow::OK;
}
tfield_bool* tdialog::register_bool(
@@ -88,6 +90,21 @@
return field;
}
+tfield_bool* tdialog::register_bool(const std::string& id
+ , const bool optional
+ , bool& linked_variable
+ , void (*callback_change) (twidget* widget))
+{
+ tfield_bool* field = new tfield_bool(
+ id
+ , optional
+ , linked_variable
+ , callback_change);
+
+ fields_.push_back(field);
+ return field;
+}
+
tfield_integer* tdialog::register_integer(
const std::string& id
, const bool optional
@@ -104,6 +121,19 @@
return field;
}
+tfield_integer* tdialog::register_integer(const std::string& id
+ , const bool optional
+ , int& linked_variable)
+{
+ tfield_integer* field = new tfield_integer(
+ id
+ , optional
+ , linked_variable);
+
+ fields_.push_back(field);
+ return field;
+}
+
tfield_text* tdialog::register_text(
const std::string& id
, const bool optional
@@ -115,6 +145,19 @@
, optional
, callback_load_value
, callback_save_value);
+
+ fields_.push_back(field);
+ return field;
+}
+
+tfield_text* tdialog::register_text(const std::string& id
+ , const bool optional
+ , std::string& linked_variable)
+{
+ tfield_text* field = new tfield_text(
+ id
+ , optional
+ , linked_variable);
fields_.push_back(field);
return field;
Modified: trunk/src/gui/dialogs/dialog.hpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/dialogs/dialog.hpp?rev=48756&r1=48755&r2=48756&view=diff
==============================================================================
--- trunk/src/gui/dialogs/dialog.hpp (original)
+++ trunk/src/gui/dialogs/dialog.hpp Sat Mar 5 14:54:27 2011
@@ -104,6 +104,24 @@
* class will hold the parameters used for a certain window, eg a server
* connection dialog will hold the name of the selected server as parameter
that
* way the caller doesn't need to know about the 'contents' of the window.
+ *
+ * @par Usage
+ *
+ * Simple dialogs that are shown to query user information it is recommended to
+ * add a static member called @p execute. The parameters to the function are:
+ * - references to in + out parameters by reference
+ * - references to the in parameters
+ * - the parameters for @ref tdialog::show.
+ *
+ * The 'in + out parameters' are used as initial value and final value when the
+ * OK button is pressed. The 'in parameters' are just extra parameters for
+ * showing.
+ *
+ * When a function only has 'in parameters' it should return a void value and
+ * the function should be called @p display, if it has 'in + out parameters' it
+ * must return a bool value. This value indicates whether or not the OK button
+ * was pressed to close the dialog. See @ref teditor_new_map::execute for an
+ * example.
*/
class tdialog
{
@@ -134,8 +152,11 @@
* @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);
+ *
+ * @returns Whether the final retval_ == twindow::OK
+ */
+ bool show(CVideo& video, const unsigned auto_close_time = 0);
+
/***** ***** ***** setters / getters for members ***** ****** *****/
@@ -171,9 +192,29 @@
, void (*callback_change) (twidget* widget) = NULL);
/**
+ * Creates a new boolean field.
+ *
+ * The field created is owned by tdialog, the returned pointer can be
used
+ * in the child classes as access to a field.
+ *
+ * @param id Id of the widget, same value as in WML.
+ * @param optional Is the widget mandatory or optional.
+ * @param linked_variable The variable the widget is linked to. See
+ * @ref tfield::tfield for more information.
+ * @param callback_change When the value of the widget changes this
+ * callback is called.
+ *
+ * @returns Pointer to the created widget.
+ */
+ tfield_bool* register_bool(const std::string& id
+ , const bool optional
+ , bool& linked_variable
+ , void (*callback_change) (twidget* widget) = NULL);
+
+ /**
* Creates a new integer field.
*
- * See register_bool for more info.
+ * See @ref register_bool for more info.
*/
tfield_integer* register_integer(const std::string& id
, const bool optional = false
@@ -181,14 +222,31 @@
, void (*callback_save_value) (const int value) = NULL);
/**
+ * Creates a new integer field.
+ *
+ * See @ref register_bool for more info.
+ */
+ tfield_integer* register_integer(const std::string& id
+ , const bool optional
+ , int& linked_variable);
+ /**
* Creates a new text field.
*
- * See register_bool for more info.
+ * See @ref register_bool for more info.
*/
tfield_text* register_text(const std::string& id
, const bool optional = false
, std::string (*callback_load_value) () = NULL
, void (*callback_save_value) (const std::string&
value) = NULL);
+
+ /**
+ * Creates a new text field.
+ *
+ * See @ref register_bool for more info.
+ */
+ tfield_text* register_text(const std::string& id
+ , const bool optional
+ , std::string& linked_variable);
private:
/** Returns the window exit status, 0 means not shown. */
int retval_;
_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits