gui/dialogs/wml_message.cpp

Go to the documentation of this file.
00001 /* $Id: wml_message.cpp 53727 2012-03-31 18:31:38Z mordante $ */
00002 /*
00003    Copyright (C) 2008 - 2012 by Mark de Wever <koraq@xs4all.nl>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 #define GETTEXT_DOMAIN "wesnoth-lib"
00017 
00018 #include "gui/dialogs/wml_message.hpp"
00019 
00020 #include "foreach.hpp"
00021 #include "gui/auxiliary/old_markup.hpp"
00022 #include "gui/widgets/button.hpp"
00023 #include "gui/widgets/label.hpp"
00024 #ifdef GUI2_EXPERIMENTAL_LISTBOX
00025 #include "gui/widgets/list.hpp"
00026 #else
00027 #include "gui/widgets/listbox.hpp"
00028 #endif
00029 #include "gui/widgets/settings.hpp"
00030 #include "gui/widgets/text_box.hpp"
00031 #include "gui/widgets/window.hpp"
00032 
00033 namespace gui2 {
00034 
00035 void twml_message_::set_input(const std::string& caption,
00036         std::string* text, const unsigned maximum_length)
00037 {
00038     assert(text);
00039 
00040     has_input_ = true;
00041     input_caption_ = caption;
00042     input_text_ = text;
00043     input_maximum_length_ = maximum_length;
00044 }
00045 
00046 void twml_message_::set_option_list(
00047         const std::vector<std::string>& option_list, int* chosen_option)
00048 {
00049     assert(!option_list.empty());
00050     assert(chosen_option);
00051 
00052     option_list_ = option_list;
00053     chosen_option_ = chosen_option;
00054 }
00055 
00056 /**
00057  * @todo This function enables the wml markup for all items, but the interface
00058  * is a bit hacky. Especially the fiddling in the internals of the listbox is
00059  * ugly. There needs to be a clean interface to set whether a widget has a
00060  * markup and what kind of markup. These fixes will be post 1.6.
00061  */
00062 void twml_message_::pre_show(CVideo& /*video*/, twindow& window)
00063 {
00064     window.canvas(1).set_variable("portrait_image", variant(portrait_));
00065     window.canvas(1).set_variable("portrait_mirror", variant(mirror_));
00066 
00067     // Set the markup
00068     tlabel& title = find_widget<tlabel>(&window, "title", false);
00069     title.set_label(title_);
00070     title.set_use_markup(true);
00071     title.set_can_wrap(true);
00072 
00073     tcontrol& message = find_widget<tcontrol>(&window, "message", false);
00074     message.set_label(message_);
00075     message.set_use_markup(true);
00076     // The message label might not always be a scroll_label but the capturing
00077     // shouldn't hurt.
00078     window.keyboard_capture(&message);
00079 
00080     // Find the input box related fields.
00081     tlabel& caption = find_widget<tlabel>(&window, "input_caption", false);
00082     ttext_box& input = find_widget<ttext_box>(&window, "input", true);
00083 
00084     if(has_input_) {
00085         caption.set_label(input_caption_);
00086         caption.set_use_markup(true);
00087         input.set_value(*input_text_);
00088         input.set_maximum_length(input_maximum_length_);
00089         window.keyboard_capture(&input);
00090         window.set_click_dismiss(false);
00091         window.set_escape_disabled(true);
00092     } else {
00093         caption.set_visible(twidget::INVISIBLE);
00094         input.set_visible(twidget::INVISIBLE);
00095     }
00096 
00097     // Find the option list related fields.
00098     tlistbox& options = find_widget<tlistbox>(&window, "input_list", true);
00099 
00100     if(!option_list_.empty()) {
00101         std::map<std::string, string_map> data;
00102         for(size_t i = 0; i < option_list_.size(); ++i) {
00103             /**
00104              * @todo This syntax looks like a bad hack, it would be nice to write
00105              * a new syntax which doesn't use those hacks (also avoids the problem
00106              * with special meanings for certain characters.
00107              */
00108             tlegacy_menu_item item(option_list_[i]);
00109 
00110             if(item.is_default()) {
00111                 // Number of items hasn't been increased yet so i is ok.
00112                 *chosen_option_ = i;
00113             }
00114 
00115             // Add the data.
00116             data["icon"]["label"] = item.icon();
00117             data["label"]["label"] = item.label();
00118             data["label"]["use_markup"] = "true";
00119             data["description"]["label"] = item.description();
00120             data["description"]["use_markup"] = "true";
00121             options.add_row(data);
00122         }
00123 
00124         // Avoid negetive and 0 since item 0 is already selected.
00125         if(*chosen_option_ > 0
00126                 && static_cast<size_t>(*chosen_option_)
00127                 < option_list_.size()) {
00128 
00129             options.select_row(*chosen_option_);
00130         }
00131 
00132         if(!has_input_) {
00133             window.keyboard_capture(&options);
00134             window.set_click_dismiss(false);
00135             window.set_escape_disabled(true);
00136         } else {
00137             window.add_to_keyboard_chain(&options);
00138             // click_dismiss has been disabled due to the input.
00139         }
00140     } else {
00141         options.set_visible(twidget::INVISIBLE);
00142     }
00143     window.set_click_dismiss(!has_input_ && option_list_.empty());
00144 }
00145 
00146 void twml_message_::post_show(twindow& window)
00147 {
00148     if(has_input_) {
00149         *input_text_ =
00150                 find_widget<ttext_box>(&window, "input", true).get_value();
00151     }
00152 
00153     if(!option_list_.empty()) {
00154         *chosen_option_ = find_widget<tlistbox>(
00155                 &window, "input_list", true).get_selected_row();
00156     }
00157 }
00158 
00159 REGISTER_DIALOG(wml_message_left)
00160 
00161 REGISTER_DIALOG(wml_message_right)
00162 
00163 int show_wml_message(const bool left_side
00164         , CVideo& video
00165         , const std::string& title
00166         , const std::string& message
00167         , const std::string& portrait
00168         , const bool mirror
00169         , const bool has_input
00170         , const std::string& input_caption
00171         , std::string* input_text
00172         , const unsigned maximum_length
00173         , const std::vector<std::string>& option_list
00174         , int* chosen_option)
00175 {
00176     std::auto_ptr<twml_message_> dlg;
00177     if(left_side) {
00178         dlg.reset(new twml_message_left(title, message, portrait, mirror));
00179     } else {
00180         dlg.reset(new twml_message_right(title, message, portrait, mirror));
00181     }
00182     assert(dlg.get());
00183 
00184     if(has_input) {
00185         dlg->set_input(input_caption, input_text, maximum_length);
00186     }
00187 
00188     if(!option_list.empty()) {
00189         dlg->set_option_list(option_list, chosen_option);
00190     }
00191 
00192     dlg->show(video);
00193     return dlg->get_retval();
00194 }
00195 
00196 } // namespace gui2
00197 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:02:59 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs