00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #define GETTEXT_DOMAIN "wesnoth-lib"
00017
00018 #include "gui/dialogs/dialog.hpp"
00019
00020 #include "foreach.hpp"
00021 #include "gui/dialogs/field.hpp"
00022 #include "gui/widgets/integer_selector.hpp"
00023 #include "video.hpp"
00024
00025 namespace gui2 {
00026
00027 tdialog::~tdialog()
00028 {
00029 foreach(tfield_* field, fields_) {
00030 delete field;
00031 }
00032 }
00033
00034 bool tdialog::show(CVideo& video, const unsigned auto_close_time)
00035 {
00036 if(video.faked()) {
00037 return false;
00038 }
00039
00040 std::auto_ptr<twindow> window(build_window(video));
00041 assert(window.get());
00042
00043 post_build(video, *window);
00044
00045 window->set_owner(this);
00046
00047 init_fields(*window);
00048
00049 pre_show(video, *window);
00050
00051 retval_ = window->show(restore_, auto_close_time);
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 events::discard(SDL_EVENTMASK(DOUBLE_CLICK_EVENT));
00066
00067 finalize_fields(*window, (retval_ == twindow::OK || always_save_fields_));
00068
00069 post_show(*window);
00070
00071 return retval_ == twindow::OK;
00072 }
00073
00074 tfield_bool* tdialog::register_bool(
00075 const std::string& id
00076 , const bool mandatory
00077 , bool (*callback_load_value) ()
00078 , void (*callback_save_value) (const bool value)
00079 , void (*callback_change) (twidget* widget))
00080 {
00081 tfield_bool* field = new tfield_bool(
00082 id
00083 , mandatory
00084 , callback_load_value
00085 , callback_save_value, callback_change);
00086
00087 fields_.push_back(field);
00088 return field;
00089 }
00090
00091 tfield_bool* tdialog::register_bool(const std::string& id
00092 , const bool mandatory
00093 , bool& linked_variable
00094 , void (*callback_change) (twidget* widget))
00095 {
00096 tfield_bool* field = new tfield_bool(
00097 id
00098 , mandatory
00099 , linked_variable
00100 , callback_change);
00101
00102 fields_.push_back(field);
00103 return field;
00104 }
00105
00106 tfield_integer* tdialog::register_integer(
00107 const std::string& id
00108 , const bool mandatory
00109 , int (*callback_load_value) ()
00110 , void (*callback_save_value) (const int value))
00111 {
00112 tfield_integer* field = new tfield_integer(
00113 id
00114 , mandatory
00115 , callback_load_value
00116 , callback_save_value);
00117
00118 fields_.push_back(field);
00119 return field;
00120 }
00121
00122 tfield_integer* tdialog::register_integer(const std::string& id
00123 , const bool mandatory
00124 , int& linked_variable)
00125 {
00126 tfield_integer* field = new tfield_integer(
00127 id
00128 , mandatory
00129 , linked_variable);
00130
00131 fields_.push_back(field);
00132 return field;
00133 }
00134
00135 tfield_text* tdialog::register_text(
00136 const std::string& id
00137 , const bool mandatory
00138 , std::string (*callback_load_value) ()
00139 , void (*callback_save_value) (const std::string& value)
00140 , const bool capture_focus)
00141 {
00142 tfield_text* field = new tfield_text(
00143 id
00144 , mandatory
00145 , callback_load_value
00146 , callback_save_value);
00147
00148 if(capture_focus) {
00149 focus_ = id;
00150 }
00151
00152 fields_.push_back(field);
00153 return field;
00154 }
00155
00156 tfield_text* tdialog::register_text(const std::string& id
00157 , const bool mandatory
00158 , std::string& linked_variable
00159 , const bool capture_focus)
00160 {
00161 tfield_text* field = new tfield_text(
00162 id
00163 , mandatory
00164 , linked_variable);
00165
00166 if(capture_focus) {
00167 focus_ = id;
00168 }
00169
00170 fields_.push_back(field);
00171 return field;
00172 }
00173
00174 tfield_label* tdialog::register_label(const std::string& id
00175 , const bool mandatory
00176 , const std::string& text
00177 , const bool use_markup)
00178 {
00179 tfield_label* field = new tfield_label(
00180 id
00181 , mandatory
00182 , text
00183 , use_markup);
00184
00185 fields_.push_back(field);
00186 return field;
00187 }
00188
00189 twindow* tdialog::build_window(CVideo& video) const
00190 {
00191 return build(video, window_id());
00192 }
00193
00194 void tdialog::init_fields(twindow& window)
00195 {
00196 foreach(tfield_* field, fields_) {
00197 field->attach_to_window(window);
00198 field->widget_init(window);
00199 }
00200
00201 if(!focus_.empty()) {
00202 if(twidget* widget = window.find(focus_, false)) {
00203 window.keyboard_capture(widget);
00204 }
00205 }
00206 }
00207
00208 void tdialog::finalize_fields(twindow& window, const bool save_fields)
00209 {
00210 foreach(tfield_* field, fields_) {
00211 if(save_fields) {
00212 field->widget_finalize(window);
00213 }
00214 field->detach_from_window();
00215 }
00216 }
00217
00218 }
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239