00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #define GETTEXT_DOMAIN "wesnoth-lib"
00022
00023 #include "gui/widgets/settings.hpp"
00024
00025 #include "asserts.hpp"
00026 #include "config_cache.hpp"
00027 #include "filesystem.hpp"
00028 #include "foreach.hpp"
00029 #include "gettext.hpp"
00030 #include "gui/auxiliary/log.hpp"
00031 #include "gui/auxiliary/tips.hpp"
00032 #include "gui/widgets/window.hpp"
00033 #include "serialization/parser.hpp"
00034 #include "serialization/preprocessor.hpp"
00035 #include "serialization/schema_validator.hpp"
00036 #include "formula_string_utils.hpp"
00037
00038 namespace gui2 {
00039
00040 bool new_widgets = false;
00041
00042 namespace settings {
00043 unsigned screen_width = 0;
00044 unsigned screen_height = 0;
00045
00046 unsigned gamemap_width = 0;
00047 unsigned gamemap_height = 0;
00048
00049 unsigned popup_show_delay = 0;
00050 unsigned popup_show_time = 0;
00051 unsigned help_show_time = 0;
00052 unsigned double_click_time = 0;
00053 unsigned repeat_button_repeat_time = 0;
00054
00055 std::string sound_button_click = "";
00056 std::string sound_toggle_button_click = "";
00057 std::string sound_toggle_panel_click = "";
00058 std::string sound_slider_adjust = "";
00059
00060 t_string has_helptip_message;
00061
00062 std::vector<ttip> tips;
00063
00064 std::vector<ttip> get_tips()
00065 {
00066 return tips::shuffle(tips);
00067 }
00068
00069 }
00070
00071
00072
00073
00074
00075
00076 static std::vector<std::string>& registered_window_types()
00077 {
00078 static std::vector<std::string> result;
00079 return result;
00080 }
00081
00082 typedef std::map<
00083 std::string
00084 , boost::function<void(
00085 tgui_definition&
00086 , const std::string&
00087 , const config&
00088 , const char *key)> > tregistered_widget_type;
00089
00090 static tregistered_widget_type& registred_widget_type()
00091 {
00092 static tregistered_widget_type result;
00093 return result;
00094 }
00095
00096 struct tgui_definition
00097 {
00098 tgui_definition()
00099 : id()
00100 , description()
00101 , control_definition()
00102 , windows()
00103 , window_types()
00104 , popup_show_delay_(0)
00105 , popup_show_time_(0)
00106 , help_show_time_(0)
00107 , double_click_time_(0)
00108 , repeat_button_repeat_time_(0)
00109 , sound_button_click_()
00110 , sound_toggle_button_click_()
00111 , sound_toggle_panel_click_()
00112 , sound_slider_adjust_()
00113 , has_helptip_message_()
00114 , tips_()
00115 {
00116 }
00117
00118 std::string id;
00119 t_string description;
00120
00121 const std::string& read(const config& cfg);
00122
00123
00124 void activate() const;
00125
00126 typedef std::map <std::string ,
00127 std::map<std::string , tcontrol_definition_ptr> >
00128 tcontrol_definition_map;
00129
00130 tcontrol_definition_map control_definition;
00131
00132 std::map<std::string, twindow_definition> windows;
00133
00134 std::map<std::string, twindow_builder> window_types;
00135
00136 void load_widget_definitions(
00137 const std::string& definition_type
00138 , const std::vector<tcontrol_definition_ptr>& definitions);
00139 private:
00140
00141 unsigned popup_show_delay_;
00142 unsigned popup_show_time_;
00143 unsigned help_show_time_;
00144 unsigned double_click_time_;
00145 unsigned repeat_button_repeat_time_;
00146
00147 std::string sound_button_click_;
00148 std::string sound_toggle_button_click_;
00149 std::string sound_toggle_panel_click_;
00150 std::string sound_slider_adjust_;
00151
00152 t_string has_helptip_message_;
00153
00154 std::vector<ttip> tips_;
00155 };
00156
00157 const std::string& tgui_definition::read(const config& cfg)
00158 {
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
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
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269 id = cfg["id"].str();
00270 description = cfg["description"];
00271
00272 VALIDATE(!id.empty(), missing_mandatory_wml_key("gui", "id"));
00273 VALIDATE(!description.empty(), missing_mandatory_wml_key("gui", "description"));
00274
00275 DBG_GUI_P << "Parsing gui " << id << '\n';
00276
00277
00278 typedef std::pair<
00279 const std::string
00280 , boost::function<void(
00281 tgui_definition&
00282 , const std::string&
00283 , const config&
00284 , const char *key)> > thack;
00285
00286 foreach(thack& widget_type, registred_widget_type()) {
00287 widget_type.second(*this, widget_type.first, cfg, NULL);
00288 }
00289
00290
00291 foreach (const config &w, cfg.child_range("window")) {
00292 std::pair<std::string, twindow_builder> child;
00293 child.first = child.second.read(w);
00294 window_types.insert(child);
00295 }
00296
00297 if(id == "default") {
00298
00299
00300 for(std::vector<std::string>::const_iterator itor
00301 = registered_window_types().begin()
00302 ; itor != registered_window_types().end()
00303 ; ++itor) {
00304
00305 const std::string error_msg("Window not defined in WML: '" +
00306 *itor +
00307 "'. Perhaps a mismatch between data and source versions."
00308 " Try --data-dir <trunk-dir>" );
00309 VALIDATE(window_types.find(*itor) != window_types.end(), error_msg );
00310 }
00311 }
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381 const config &settings = cfg.child("settings");
00382
00383 popup_show_delay_ = settings["popup_show_delay"];
00384 popup_show_time_ = settings["popup_show_time"];
00385 help_show_time_ = settings["help_show_time"];
00386 double_click_time_ = settings["double_click_time"];
00387
00388 repeat_button_repeat_time_ = settings["repeat_button_repeat_time"];
00389
00390 VALIDATE(double_click_time_, missing_mandatory_wml_key("settings", "double_click_time"));
00391
00392 sound_button_click_ = settings["sound_button_click"].str();
00393 sound_toggle_button_click_ = settings["sound_toggle_button_click"].str();
00394 sound_toggle_panel_click_ = settings["sound_toggle_panel_click"].str();
00395 sound_slider_adjust_ = settings["sound_slider_adjust"].str();
00396
00397 has_helptip_message_ = settings["has_helptip_message"];
00398
00399 VALIDATE(!has_helptip_message_.empty(),
00400 missing_mandatory_wml_key("[settings]", "has_helptip_message"));
00401
00402 tips_ = tips::load(cfg);
00403
00404 return id;
00405 }
00406
00407 void tgui_definition::activate() const
00408 {
00409 settings::popup_show_delay = popup_show_delay_;
00410 settings::popup_show_time = popup_show_time_;
00411 settings::help_show_time = help_show_time_;
00412 settings::double_click_time = double_click_time_;
00413 settings::repeat_button_repeat_time = repeat_button_repeat_time_;
00414 settings::sound_button_click = sound_button_click_;
00415 settings::sound_toggle_button_click = sound_toggle_button_click_;
00416 settings::sound_toggle_panel_click = sound_toggle_panel_click_;
00417 settings::sound_slider_adjust = sound_slider_adjust_;
00418 settings::has_helptip_message = has_helptip_message_;
00419 settings::tips = tips_;
00420 }
00421
00422 void tgui_definition::load_widget_definitions(
00423 const std::string& definition_type
00424 , const std::vector<tcontrol_definition_ptr>& definitions)
00425 {
00426 foreach(const tcontrol_definition_ptr& def, definitions) {
00427
00428
00429 assert(control_definition[definition_type].find(def->id)
00430 == control_definition[definition_type].end());
00431
00432 control_definition[definition_type]
00433 .insert(std::make_pair(def->id, def));
00434 }
00435
00436 utils::string_map symbols;
00437 symbols["definition"] = definition_type;
00438 symbols["id"] = "default";
00439 t_string msg(vgettext(
00440 "Widget definition '$definition' "
00441 "doesn't contain the definition for '$id'."
00442 , symbols));
00443
00444 VALIDATE(control_definition[definition_type].find("default")
00445 != control_definition[definition_type].end(), msg);
00446
00447 }
00448
00449
00450 std::map<std::string, twindow_builder> windows;
00451
00452
00453 std::map<std::string, tgui_definition> guis;
00454
00455
00456 std::map<std::string, tgui_definition>::const_iterator current_gui = guis.end();
00457
00458 void register_window(const std::string& id)
00459 {
00460 const std::vector<std::string>::iterator itor = std::find(
00461 registered_window_types().begin()
00462 , registered_window_types().end()
00463 , id);
00464
00465 if(itor == registered_window_types().end()) {
00466 registered_window_types().push_back(id);
00467 }
00468 }
00469
00470 std::vector<std::string> tunit_test_access_only::get_registered_window_list()
00471 {
00472 return gui2::registered_window_types();
00473 }
00474
00475 void load_settings()
00476 {
00477 LOG_GUI_G << "Setting: init gui.\n";
00478
00479
00480 twindow::update_screen_size();
00481
00482
00483 config cfg;
00484 try {
00485 schema_validation::schema_validator
00486 validator (get_wml_location("gui/schema.cfg"));
00487 preproc_map preproc(
00488 game_config::config_cache::instance().get_preproc_map());
00489 scoped_istream stream = preprocess_file(get_wml_location("gui/default.cfg"), &preproc);
00490
00491 read(cfg, *stream, &validator);
00492 } catch(config::error& e) {
00493 ERR_GUI_P << e.what() << '\n';
00494 ERR_GUI_P << "Setting: could not read file 'data/gui/default.cfg'.\n";
00495 }
00496 catch(const abstract_validator::error& e){
00497 ERR_GUI_P << "Setting: could not read file 'data/gui/schema.cfg'.\n";
00498 ERR_GUI_P << e.message;
00499 }
00500
00501 foreach (const config &g, cfg.child_range("gui")) {
00502 std::pair<std::string, tgui_definition> child;
00503 child.first = child.second.read(g);
00504 guis.insert(child);
00505 }
00506
00507 VALIDATE(guis.find("default") != guis.end(), _ ("No default gui defined."));
00508
00509 current_gui = guis.find("default");
00510 current_gui->second.activate();
00511 }
00512
00513 tstate_definition::tstate_definition(const config &cfg) :
00514 canvas()
00515 {
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535 const config &draw = *(cfg ? &cfg.child("draw") : &cfg);
00536
00537 VALIDATE(draw, _("No state or draw section defined."));
00538
00539 canvas.set_cfg(draw);
00540 }
00541
00542 void register_widget(const std::string& id
00543 , boost::function<void(
00544 tgui_definition& gui_definition
00545 , const std::string& definition_type
00546 , const config& cfg
00547 , const char *key)> functor)
00548 {
00549 registred_widget_type().insert(std::make_pair(id, functor));
00550 }
00551
00552 void load_widget_definitions(
00553 tgui_definition& gui_definition
00554 , const std::string& definition_type
00555 , const std::vector<tcontrol_definition_ptr>& definitions)
00556 {
00557 DBG_GUI_P << "Load definition '" << definition_type << "'.\n";
00558 gui_definition.load_widget_definitions(definition_type, definitions);
00559 }
00560
00561 tresolution_definition_ptr get_control(
00562 const std::string& control_type, const std::string& definition)
00563 {
00564 const tgui_definition::tcontrol_definition_map::const_iterator
00565 #ifdef GUI2_EXPERIMENTAL_LISTBOX
00566 control_definition = (control_type == "list")
00567 ? current_gui->second.control_definition.find("listbox")
00568 : current_gui->second.control_definition.find(control_type);
00569 #else
00570 control_definition =
00571 current_gui->second.control_definition.find(control_type);
00572 #endif
00573
00574 ASSERT_LOG(control_definition != current_gui->second.control_definition.end(),
00575 "Type '" << control_type << "' is unknown.");
00576
00577 std::map<std::string, tcontrol_definition_ptr>::const_iterator
00578 control = control_definition->second.find(definition);
00579
00580 if(control == control_definition->second.end()) {
00581 LOG_GUI_G << "Control: type '" << control_type << "' definition '"
00582 << definition << "' not found, falling back to 'default'.\n";
00583 control = control_definition->second.find("default");
00584 assert(control != control_definition->second.end());
00585 }
00586
00587 for(std::vector<tresolution_definition_ptr>::const_iterator
00588 itor = (*control->second).resolutions.begin(),
00589 end = (*control->second).resolutions.end();
00590 itor != end;
00591 ++itor) {
00592
00593 if(settings::screen_width <= (**itor).window_width &&
00594 settings::screen_height <= (**itor).window_height) {
00595
00596 return *itor;
00597 } else if (itor == end - 1) {
00598 return *itor;
00599 }
00600 }
00601
00602 ERROR_LOG(false);
00603 }
00604
00605 std::vector<twindow_builder::tresolution>::const_iterator get_window_builder(
00606 const std::string& type)
00607 {
00608 twindow::update_screen_size();
00609
00610 std::map<std::string, twindow_builder>::const_iterator
00611 window = current_gui->second.window_types.find(type);
00612
00613 if(true) {
00614 if(window == current_gui->second.window_types.end()) {
00615 throw twindow_builder_invalid_id();
00616 }
00617 } else {
00618
00619 }
00620
00621 for(std::vector<twindow_builder::tresolution>::const_iterator
00622 itor = window->second.resolutions.begin(),
00623 end = window->second.resolutions.end();
00624 itor != end;
00625 ++itor) {
00626
00627 if(settings::screen_width <= itor->window_width &&
00628 settings::screen_height <= itor->window_height) {
00629
00630 return itor;
00631 } else if (itor == end - 1) {
00632 return itor;
00633 }
00634 }
00635
00636 ERROR_LOG(false);
00637 }
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648 }