gui/auxiliary/window_builder/listbox.cpp

Go to the documentation of this file.
00001 /* $Id: listbox.cpp 54100 2012-05-06 09:44:47Z 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/auxiliary/window_builder/listbox.hpp"
00019 
00020 #include "foreach.hpp"
00021 #include "gettext.hpp"
00022 #include "gui/auxiliary/log.hpp"
00023 #include "gui/auxiliary/widget_definition/listbox.hpp"
00024 #include "gui/auxiliary/window_builder/helper.hpp"
00025 #include "gui/widgets/grid.hpp"
00026 #ifdef GUI2_EXPERIMENTAL_LISTBOX
00027 #include "gui/widgets/list.hpp"
00028 #else
00029 #include "gui/widgets/listbox.hpp"
00030 #endif
00031 #include "gui/widgets/pane.hpp"
00032 #include "gui/widgets/viewport.hpp"
00033 #include "gui/widgets/settings.hpp"
00034 #include "wml_exception.hpp"
00035 
00036 namespace gui2 {
00037 
00038 namespace implementation {
00039 
00040 tbuilder_listbox::tbuilder_listbox(const config& cfg)
00041     : tbuilder_control(cfg)
00042     , vertical_scrollbar_mode(
00043             get_scrollbar_mode(cfg["vertical_scrollbar_mode"]))
00044     , horizontal_scrollbar_mode(
00045             get_scrollbar_mode(cfg["horizontal_scrollbar_mode"]))
00046     , header(NULL)
00047     , footer(NULL)
00048     , list_builder(NULL)
00049     , list_data()
00050 {
00051     if(const config &h = cfg.child("header")) {
00052         header = new tbuilder_grid(h);
00053     }
00054 
00055     if(const config &f = cfg.child("footer")) {
00056         footer = new tbuilder_grid(f);
00057     }
00058 
00059     const config &l = cfg.child("list_definition");
00060 
00061     VALIDATE(l, _("No list defined."));
00062     list_builder = new tbuilder_grid(l);
00063     assert(list_builder);
00064     VALIDATE(list_builder->rows == 1
00065             , _("A 'list_definition' should contain one row."));
00066 
00067     const config &data = cfg.child("list_data");
00068     if(!data) {
00069         return;
00070     }
00071 
00072     foreach(const config& row, data.child_range("row")) {
00073         unsigned col = 0;
00074 
00075         foreach(const config& c, row.child_range("column")) {
00076             list_data.push_back(string_map());
00077             foreach(const config::attribute& i, c.attribute_range()) {
00078                 list_data.back()[i.first] = i.second;
00079             }
00080             ++col;
00081         }
00082 
00083         VALIDATE(col == list_builder->cols
00084                 , _("'list_data' must have the same number of "
00085                     "columns as the 'list_definition'."));
00086     }
00087 }
00088 
00089 twidget* tbuilder_listbox::build() const
00090 {
00091 #ifdef GUI2_EXPERIMENTAL_LISTBOX
00092     tlist *widget = new tlist(true
00093             , true
00094             , tgenerator_::vertical_list
00095             , true
00096             , list_builder);
00097 
00098     init_control(widget);
00099     if(!list_data.empty()) {
00100         widget->append_rows(list_data);
00101     }
00102     return widget;
00103 #else
00104     if(new_widgets) {
00105 
00106         tpane *pane = new tpane(list_builder);
00107         pane->set_id(id);
00108 
00109 
00110         tgrid* grid = new tgrid();
00111         grid->set_rows_cols(1, 1);
00112 #if 0
00113         grid->set_child(
00114                   pane
00115                 , 0
00116                 , 0
00117                 , tgrid::VERTICAL_GROW_SEND_TO_CLIENT
00118                     | tgrid::HORIZONTAL_GROW_SEND_TO_CLIENT
00119                 , tgrid::BORDER_ALL);
00120 #else
00121         tviewport *viewport = new tviewport(*pane);
00122         grid->set_child(
00123                   viewport
00124                 , 0
00125                 , 0
00126                 , tgrid::VERTICAL_GROW_SEND_TO_CLIENT
00127                     | tgrid::HORIZONTAL_GROW_SEND_TO_CLIENT
00128                 , tgrid::BORDER_ALL);
00129 #endif
00130         return grid;
00131     }
00132 
00133     tlistbox *widget = new tlistbox(
00134             true, true, tgenerator_::vertical_list, true);
00135 
00136     init_control(widget);
00137 
00138     widget->set_list_builder(list_builder); // FIXME in finalize???
00139 
00140     widget->set_vertical_scrollbar_mode(vertical_scrollbar_mode);
00141     widget->set_horizontal_scrollbar_mode(horizontal_scrollbar_mode);
00142 
00143     DBG_GUI_G << "Window builder: placed listbox '"
00144             << id << "' with definition '"
00145             << definition << "'.\n";
00146 
00147     boost::intrusive_ptr<const tlistbox_definition::tresolution> conf =
00148             boost::dynamic_pointer_cast
00149                 <const tlistbox_definition::tresolution>(widget->config());
00150     assert(conf);
00151 
00152     widget->init_grid(conf->grid);
00153 
00154     widget->finalize(header, footer, list_data);
00155 
00156     return widget;
00157 #endif
00158 }
00159 
00160 } // namespace implementation
00161 
00162 } // namespace gui2
00163 
00164 /*WIKI_MACRO
00165  * @begin{macro}{listbox_description}
00166  *
00167  *        A listbox is a control that holds several items of the same type.
00168  *        Normally the items in a listbox are ordered in rows, this version
00169  *        might allow more options for ordering the items in the future.
00170  * @end{macro}
00171  */
00172 
00173 /*WIKI
00174  * @page = GUIWidgetInstanceWML
00175  * @order = 2_listbox
00176  *
00177  * == Listbox ==
00178  * @begin{parent}{name="gui/window/resolution/grid/row/column/"}
00179  * @begin{tag}{name="listbox"}{min=0}{max=-1}{super="generic/widget_instance"}
00180  * @macro = listbox_description
00181  *
00182  * List with the listbox specific variables:
00183  * @begin{table}{config}
00184  *     vertical_scrollbar_mode & scrollbar_mode & initial_auto &
00185  *                                     Determines whether or not to show the
00186  *                                     scrollbar. $
00187  *     horizontal_scrollbar_mode & scrollbar_mode & initial_auto &
00188  *                                     Determines whether or not to show the
00189  *                                     scrollbar. $
00190  *
00191  *     header & grid & [] &            Defines the grid for the optional
00192  *                                     header. (This grid will automatically
00193  *                                     get the id _header_grid.) $
00194  *     footer & grid & [] &            Defines the grid for the optional
00195  *                                     footer. (This grid will automatically
00196  *                                     get the id _footer_grid.) $
00197  *
00198  *     list_definition & section & &   This defines how a listbox item
00199  *                                     looks. It must contain the grid
00200  *                                     definition for 1 row of the list. $
00201  *
00202  *     list_data & section & [] &      A grid alike section which stores the
00203  *                                     initial data for the listbox. Every row
00204  *                                     must have the same number of columns as
00205  *                                     the 'list_definition'. $
00206  *
00207  * @end{table}
00208  * @begin{tag}{name="header"}{min=0}{max=1}{super="gui/window/resolution/grid"}
00209  * @end{tag}{name="header"}
00210  * @begin{tag}{name="footer"}{min=0}{max=1}{super="gui/window/resolution/grid"}
00211  * @end{tag}{name="footer"}
00212  * @begin{tag}{name="list_definition"}{min=0}{max=1}
00213  * @begin{tag}{name="row"}{min=1}{max=1}{super="generic/listbox_grid/row"}
00214  * @end{tag}{name="row"}
00215  * @end{tag}{name="list_definition"}x
00216  * @begin{tag}{name="list_data"}{min=0}{max=1}{super="generic/listbox_grid"}
00217  * @end{tag}{name="list_data"}
00218  *
00219  * In order to force widgets to be the same size inside a listbox, the widgets
00220  * need to be inside a linked_group.
00221  *
00222  * Inside the list section there are only the following widgets allowed
00223  * * grid (to nest)
00224  * * selectable widgets which are
00225  * ** toggle_button
00226  * ** toggle_panel
00227  * @end{tag}{name="listbox"}
00228  *
00229  * @end{parent}{name="gui/window/resolution/grid/row/column/"}
00230  */
00231 
00232 /*WIKI
00233  * @begin{parent}{name="generic/"}
00234  * @begin{tag}{name="listbox_grid"}{min="0"}{max="-1"}
00235  * @begin{tag}{name="row"}{min="0"}{max="-1"}
00236  * @begin{table}{config}
00237  *     grow_factor & unsigned & 0 &      The grow factor for a row. $
00238  * @end{table}
00239  * @begin{tag}{name="column"}{min="0"}{max="-1"}{super="gui/window/resolution/grid/row/column"}
00240  * @begin{table}{config}
00241  *     label & t_string & "" &  $
00242  *     tooltip & t_string & "" &  $
00243  *     icon & t_string & "" &  $
00244  * @end{table}
00245  * @allow{link}{name="gui/window/resolution/grid/row/column/toggle_button"}
00246  * @allow{link}{name="gui/window/resolution/grid/row/column/toggle_panel"}
00247  * @end{tag}{name="column"}
00248  * @end{tag}{name="row"}
00249  * @end{tag}{name="listbox_grid"}
00250  * @end{parent}{name="generic/"}
00251  */
00252 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Wed May 23 2012 01:02:41 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs