gui/dialogs/addon_list.cpp

Go to the documentation of this file.
00001 /* $Id: addon_list.cpp 54172 2012-05-13 14:47:03Z 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/addon_list.hpp"
00019 
00020 #include "foreach.hpp"
00021 #include "gettext.hpp"
00022 #include "gui/auxiliary/filter.hpp"
00023 #include "gui/widgets/button.hpp"
00024 #include "gui/widgets/label.hpp"
00025 #ifdef GUI2_EXPERIMENTAL_LISTBOX
00026 #include "gui/widgets/list.hpp"
00027 #else
00028 #include "gui/widgets/listbox.hpp"
00029 #endif
00030 #include "gui/widgets/pane.hpp"
00031 #include "gui/widgets/settings.hpp"
00032 #include "gui/widgets/toggle_button.hpp"
00033 #include "gui/widgets/window.hpp"
00034 #include "serialization/string_utils.hpp"
00035 
00036 #include <boost/bind.hpp>
00037 
00038 namespace gui2 {
00039 
00040 /*WIKI
00041  * @page = GUIWindowDefinitionWML
00042  * @order = 2_addon_list
00043  *
00044  * == Addon list ==
00045  *
00046  * This shows the dialog with the addons to install. This dialog is under
00047  * construction and only used with --new-widgets.
00048  *
00049  * @begin{table}{dialog_widgets}
00050  *
00051  * addons & & listbox & m &
00052  *        A listbox that will contain the info about all addons on the server. $
00053  *
00054  * -name & & control & o &
00055  *        The name of the addon. $
00056  *
00057  * -version & & control & o &
00058  *        The version number of the addon. $
00059  *
00060  * -author & & control & o &
00061  *        The author of the addon. $
00062  *
00063  * -downloads & & control & o &
00064  *        The number of times the addon has been downloaded. $
00065  *
00066  * -size & & control & o &
00067  *        The size of the addon. $
00068  *
00069  * @end{table}
00070  */
00071 
00072 REGISTER_DIALOG(addon_list)
00073 
00074 void taddon_list::collapse(tgrid& grid)
00075 {
00076     find_widget<ttoggle_button>(&grid, "expand", false)
00077             .set_visible(twidget::VISIBLE);
00078 
00079     find_widget<ttoggle_button>(&grid, "collapse", false)
00080             .set_visible(twidget::INVISIBLE);
00081 
00082     find_widget<tlabel>(&grid, "description", false)
00083             .set_visible(twidget::INVISIBLE);
00084 }
00085 
00086 void taddon_list::expand(tgrid& grid)
00087 {
00088     find_widget<ttoggle_button>(&grid, "expand", false)
00089             .set_visible(twidget::HIDDEN);
00090 
00091     find_widget<ttoggle_button>(&grid, "collapse", false)
00092             .set_visible(twidget::VISIBLE);
00093 
00094     find_widget<tlabel>(&grid, "description", false)
00095             .set_visible(twidget::VISIBLE);
00096 }
00097 
00098 void taddon_list::pre_show(CVideo& /*video*/, twindow& window)
00099 {
00100     if(new_widgets) {
00101 
00102         /***** ***** Init buttons. ***** *****/
00103 
00104         tpane& pane = find_widget<tpane>(&window, "addons", false);
00105 
00106 
00107         tpane::tcompare_functor ascending_name_functor =
00108                 boost::bind(&sort<std::string>, _1, _2, "name", true);
00109 
00110         tpane::tcompare_functor descending_name_functor =
00111                 boost::bind(&sort<std::string>, _1, _2, "name", false);
00112 
00113         tpane::tcompare_functor ascending_size_functor =
00114                 boost::bind(&sort<int>, _1, _2, "size", true);
00115 
00116         tpane::tcompare_functor descending_size_functor =
00117                 boost::bind(&sort<int>, _1, _2, "size", false);
00118 
00119 
00120         connect_signal_mouse_left_click(
00121                   find_widget<tbutton>(&window, "sort_name_ascending", false)
00122                 , boost::bind(
00123                       &tpane::sort
00124                     , &pane
00125                     , ascending_name_functor));
00126 
00127         connect_signal_mouse_left_click(
00128                   find_widget<tbutton>(&window, "sort_name_descending", false)
00129                 , boost::bind(
00130                       &tpane::sort
00131                     , &pane
00132                     , descending_name_functor));
00133 
00134         connect_signal_mouse_left_click(
00135                   find_widget<tbutton>(&window, "sort_size_ascending", false)
00136                 , boost::bind(
00137                       &tpane::sort
00138                     , &pane
00139                     , ascending_size_functor));
00140 
00141         connect_signal_mouse_left_click(
00142                   find_widget<tbutton>(&window, "sort_size_descending", false)
00143                 , boost::bind(
00144                       &tpane::sort
00145                     , &pane
00146                     , descending_size_functor));
00147 
00148         /***** ***** Init the filter text box. ***** *****/
00149 
00150         ttext_box& filter_box =
00151                 find_widget<ttext_box>(&window, "filter", false);
00152 
00153         tpane::tfilter_functor filter_functor =
00154                 boost::bind(&contains, _1, "filter", boost::cref(filter_box));
00155 
00156         connect_signal_notify_modified(
00157                   filter_box
00158                 , boost::bind(
00159                       &tpane::filter
00160                     , &pane
00161                     , filter_functor));
00162 
00163         /***** ***** Fill the listbox. ***** *****/
00164 
00165         tbutton* load_button = find_widget<tbutton>(
00166                   &window
00167                 , "load_campaign"
00168                 , false
00169                 , false);
00170         if(load_button) {
00171             connect_signal_mouse_left_click(
00172                       *load_button
00173                     , boost::bind(
00174                           &taddon_list::load
00175                         , this
00176                         , boost::ref(pane)));
00177             load(pane);
00178         } else {
00179             while(cfg_iterators_.first != cfg_iterators_.second) {
00180                 create_campaign(pane, *cfg_iterators_.first);
00181                 ++cfg_iterators_.first;
00182             }
00183         }
00184 
00185     } else {
00186         tlistbox& list = find_widget<tlistbox>(&window, "addons", false);
00187 
00188         /**
00189          * @todo do we really want to keep the length limit for the various
00190          * items?
00191          */
00192         foreach(const config &c, cfg_.child_range("campaign")) {
00193             std::map<std::string, string_map> data;
00194             string_map item;
00195 
00196             item["label"] = c["icon"];
00197             data.insert(std::make_pair("icon", item));
00198 
00199             std::string tmp = c["name"];
00200             utils::truncate_as_wstring(tmp, 20);
00201             item["label"] = tmp;
00202             data.insert(std::make_pair("name", item));
00203 
00204             tmp = c["version"].str();
00205             utils::truncate_as_wstring(tmp, 12);
00206             item["label"] = tmp;
00207             data.insert(std::make_pair("version", item));
00208 
00209             tmp = c["author"].str();
00210             utils::truncate_as_wstring(tmp, 16);
00211             item["label"] = tmp;
00212             data.insert(std::make_pair("author", item));
00213 
00214             item["label"] = c["downloads"];
00215             data.insert(std::make_pair("downloads", item));
00216 
00217             item["label"] = c["size"];
00218             data.insert(std::make_pair("size", item));
00219 
00220             list.add_row(data);
00221         }
00222     }
00223 }
00224 
00225 void taddon_list::create_campaign(tpane& pane, const config& campaign)
00226 {
00227             /***** Determine the data for the widgets. *****/
00228 
00229             std::map<std::string, string_map> data;
00230             string_map item;
00231 
00232             item["label"] = campaign["icon"];
00233             data.insert(std::make_pair("icon", item));
00234 
00235             std::string tmp = campaign["name"];
00236             utils::truncate_as_wstring(tmp, 20);
00237             item["label"] = tmp;
00238             data.insert(std::make_pair("name", item));
00239 
00240             tmp = campaign["version"].str();
00241             utils::truncate_as_wstring(tmp, 12);
00242             item["label"] = tmp;
00243             data.insert(std::make_pair("version", item));
00244 
00245             tmp = campaign["author"].str();
00246             utils::truncate_as_wstring(tmp, 16);
00247             item["label"] = tmp;
00248             data.insert(std::make_pair("author", item));
00249 
00250             item["label"] = campaign["downloads"];
00251             data.insert(std::make_pair("downloads", item));
00252 
00253             item["label"] = utils::si_string(
00254                       campaign["size"]
00255                     , true
00256                     , _("unit_byte^B"));
00257 
00258             data.insert(std::make_pair("size", item));
00259 
00260             item["label"] = campaign["description"];
00261             data.insert(std::make_pair("description", item));
00262 
00263             /***** Determine the tags for the campaign. *****/
00264 
00265             std::map<std::string, std::string> tags;
00266             tags.insert(std::make_pair("name", campaign["name"]));
00267             tags.insert(std::make_pair("size", campaign["size"]));
00268 
00269             std::stringstream filter;
00270             filter << campaign["version"] << '\n'
00271                     << campaign["author"] << '\n'
00272                     << campaign["type"] << '\n'
00273                     << campaign["description"];
00274 
00275             tags.insert(std::make_pair(
00276                       "filter"
00277                     , utils::lowercase(filter.str())));
00278 
00279             /***** Add the campaign. *****/
00280 
00281             const unsigned id = pane.create_item(data, tags);
00282 
00283             tgrid* grid = pane.grid(id);
00284             assert(grid);
00285 
00286             ttoggle_button* collapse = find_widget<ttoggle_button>(
00287                       grid
00288                     , "collapse"
00289                     , false
00290                     , false);
00291 
00292             if(collapse) {
00293                 collapse->set_visible(twidget::INVISIBLE);
00294                 collapse->set_callback_state_change(boost::bind(
00295                           &taddon_list::collapse
00296                         , this
00297                         , boost::ref(*grid)));
00298 
00299                 find_widget<ttoggle_button>(grid, "expand", false)
00300                         .set_callback_state_change(boost::bind(
00301                               &taddon_list::expand
00302                             , this
00303                             , boost::ref(*grid)));
00304 
00305                 find_widget<tlabel>(grid, "description", false)
00306                         .set_visible(twidget::INVISIBLE);
00307             }
00308 
00309 }
00310 
00311 void taddon_list::load(tpane& pane)
00312 {
00313     if(cfg_iterators_.first != cfg_iterators_.second) {
00314         create_campaign(pane, *cfg_iterators_.first);
00315         ++cfg_iterators_.first;
00316     }
00317 
00318     find_widget<tbutton>(pane.get_window(), "load_campaign", false)
00319             .set_active(cfg_iterators_.first != cfg_iterators_.second);
00320 }
00321 
00322 } // namespace gui2
00323 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Tue May 22 2012 01:03:50 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs