gui/dialogs/simple_item_selector.cpp

Go to the documentation of this file.
00001 /* $Id: simple_item_selector.cpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2010 - 2012 by Ignacio Riquelme Morelle <shadowm2006@gmail.com>
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/simple_item_selector.hpp"
00019 
00020 #include "foreach.hpp"
00021 #include "gui/widgets/button.hpp"
00022 #include "gui/widgets/label.hpp"
00023 #ifdef GUI2_EXPERIMENTAL_LISTBOX
00024     #include "gui/widgets/list.hpp"
00025 #else
00026     #include "gui/widgets/listbox.hpp"
00027 #endif
00028 #include "gui/widgets/settings.hpp"
00029 #include "gui/widgets/window.hpp"
00030 
00031 namespace gui2 {
00032 
00033 /*WIKI
00034  * @page = GUIWindowDefinitionWML
00035  * @order = 2_simple_item_selector
00036  *
00037  * == Simple item selector ==
00038  *
00039  * A simple one-column listbox with OK and Cancel buttons.
00040  *
00041  * @begin{table}{dialog_widgets}
00042  *
00043  * title & & label & m &
00044  *         Dialog title label. $
00045  *
00046  * message & & control & m &
00047  *         Text label displaying a description or instructions. $
00048  *
00049  * listbox & & listbox & m &
00050  *         Listbox displaying user choices. $
00051  *
00052  * -item & & control & m &
00053  *         Widget which shows a listbox item label. $
00054  *
00055  * ok & & button & m &
00056  *         OK button. $
00057  *
00058  * cancel & & button & m &
00059  *         Cancel button. $
00060  *
00061  * @end{table}
00062  */
00063 
00064 REGISTER_DIALOG(simple_item_selector)
00065 
00066 tsimple_item_selector::tsimple_item_selector(const std::string& title, const std::string& message, list_type const& items, bool title_uses_markup, bool message_uses_markup)
00067     : index_(-1)
00068     , single_button_(false)
00069     , items_(items)
00070     , ok_label_()
00071     , cancel_label_()
00072 {
00073     register_label("title", true, title, title_uses_markup);
00074     register_label("message", true, message, message_uses_markup);
00075 }
00076 
00077 void tsimple_item_selector::pre_show(CVideo& /*video*/, twindow& window)
00078 {
00079     tlistbox& list = find_widget<tlistbox>(&window, "listbox", false);
00080     window.keyboard_capture(&list);
00081 
00082     foreach(const tsimple_item_selector::item_type& it, items_) {
00083         std::map<std::string, string_map> data;
00084         string_map column;
00085 
00086         column["label"] = it;
00087         data.insert(std::make_pair("item", column));
00088 
00089         list.add_row(data);
00090     }
00091 
00092     if(index_ != -1 && static_cast<unsigned>(index_) < list.get_item_count()) {
00093         list.select_row(index_);
00094     }
00095 
00096     index_ = -1;
00097 
00098     tbutton& button_ok = find_widget<tbutton>(&window, "ok", false);
00099     tbutton& button_cancel = find_widget<tbutton>(&window, "cancel", false);
00100 
00101     if(!ok_label_.empty()) {
00102         button_ok.set_label(ok_label_);
00103     }
00104 
00105     if(!cancel_label_.empty()) {
00106         button_cancel.set_label(cancel_label_);
00107     }
00108 
00109     if(single_button_) {
00110         button_cancel.set_visible(gui2::twidget::INVISIBLE);
00111     }
00112 }
00113 
00114 void tsimple_item_selector::post_show(twindow& window)
00115 {
00116     if(get_retval() != twindow::OK) {
00117         return;
00118     }
00119 
00120     tlistbox& list = find_widget<tlistbox>(&window, "listbox", false);
00121     index_ = list.get_selected_row();
00122 }
00123 
00124 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

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