gui/widgets/listbox.hpp

Go to the documentation of this file.
00001 /* $Id: listbox.hpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
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 #ifndef GUI_WIDGETS_LISTBOX_HPP_INCLUDED
00017 #define GUI_WIDGETS_LISTBOX_HPP_INCLUDED
00018 
00019 #ifndef GUI2_EXPERIMENTAL_LISTBOX
00020 
00021 #include "gui/widgets/generator.hpp"
00022 #include "gui/widgets/scrollbar_container.hpp"
00023 
00024 namespace gui2 {
00025 
00026 namespace implementation {
00027     struct tbuilder_listbox;
00028     struct tbuilder_horizontal_listbox;
00029 }
00030 
00031 /** The listbox class. */
00032 class tlistbox
00033         : public tscrollbar_container
00034 {
00035     friend struct implementation::tbuilder_listbox;
00036     friend struct implementation::tbuilder_horizontal_listbox;
00037     friend class tdebug_layout_graph;
00038 public:
00039     /**
00040      * Constructor.
00041      *
00042      * @param has_minimum         Does the listbox need to have one item
00043      *                            selected.
00044      * @param has_maximum         Can the listbox only have one item
00045      *                            selected.
00046      * @param placement           How are the items placed.
00047      * @param select              Select an item when selected, if false it
00048      *                            changes the visible state instead.
00049      */
00050     tlistbox(const bool has_minimum, const bool has_maximum,
00051             const tgenerator_::tplacement placement, const bool select);
00052 
00053     /***** ***** ***** ***** Row handling. ***** ***** ****** *****/
00054     /**
00055      * When an item in the list is selected by the user we need to
00056      * update the state. We installed a callback handler which
00057      * calls us.
00058      *
00059      * @param item                The data send to the set_members of the
00060      *                            widgets.
00061      * @param index               The item before which to add the new item,
00062      *                            0 == begin, -1 == end.
00063      */
00064     void add_row(const string_map& item, const int index = -1);
00065 
00066     /**
00067      * Adds single row to the grid.
00068      *
00069      * This function expect a row to have multiple widgets (either multiple
00070      * columns or one column with multiple widgets).
00071      *
00072      *
00073      * @param data                The data to send to the set_members of the
00074      *                            widgets. If the member id is not an empty
00075      *                            string it is only send to the widget that has
00076      *                            the wanted id (if any). If the member id is an
00077      *                            empty string, it is send to all members.
00078      *                            Having both empty and non-empty id's gives
00079      *                            undefined behaviour.
00080      * @param index               The item before which to add the new item,
00081      *                            0 == begin, -1 == end.
00082      */
00083     void add_row(
00084               const std::map<std::string /* widget id */, string_map>& data
00085             , const int index = -1);
00086 
00087     /**
00088      * Removes a row in the listbox.
00089      *
00090      * @param row                 The row to remove, when not in
00091      *                            range the function is ignored.
00092      * @param count               The number of rows to remove, 0 means all
00093      *                            rows (starting from row).
00094      */
00095     void remove_row(const unsigned row, unsigned count = 1);
00096 
00097     /** Removes all the rows in the listbox, clearing it. */
00098     void clear();
00099 
00100     /** Returns the number of items in the listbox. */
00101     unsigned get_item_count() const;
00102 
00103     /**
00104      * Makes a row active or inactive.
00105      *
00106      * NOTE this doesn't change the select status of the row.
00107      *
00108      * @param row                 The row to (de)activate.
00109      * @param active              true activate, false deactivate.
00110      */
00111     void set_row_active(const unsigned row, const bool active);
00112 
00113     /**
00114      * Makes a row visible or invisible.
00115      *
00116      * @param row                 The row to show or hide.
00117      * @param shown               true visible, false invisible.
00118      */
00119     void set_row_shown(const unsigned row, const bool shown);
00120 
00121     /**
00122      * Makes a row visible or invisible.
00123      *
00124      * Use this version if you want to show hide multiple items since it's
00125      * optimized for that purpose, for one it calls the selection changed
00126      * callback only once instead of several times.
00127      *
00128      * @param shown               A vector with the show hide status for every
00129      *                            row. The number of items in the vector must
00130      *                            be equal to the number of items in the
00131      *                            listbox.
00132      */
00133     void set_row_shown(const std::vector<bool>& shown);
00134 
00135     /**
00136      * Returns the grid of the wanted row.
00137      *
00138      * There's only a const version since allowing callers to modify the grid
00139      * behind our backs might give problems. We return a pointer instead of a
00140      * reference since dynamic casting of pointers is easier (no try catch
00141      * needed).
00142      *
00143      * @param row                 The row to get the grid from, the caller has
00144      *                            to make sure the row is a valid row.
00145      * @returns                   The grid of the wanted row.
00146      */
00147     const tgrid* get_row_grid(const unsigned row) const;
00148 
00149     /**
00150      * The possibly-giving-problems nonconst version of get_row_grid
00151      *
00152      * @param row                 The row to get the grid from, the caller has
00153      *                            to make sure the row is a valid row.
00154      * @returns                   The grid of the wanted row.
00155      */
00156     tgrid* get_row_grid(const unsigned row);
00157 
00158     /**
00159      * Selectes a row.
00160      *
00161      * @param row                 The row to select.
00162      * @param select              Select or deselect the row.
00163      */
00164     bool select_row(const unsigned row, const bool select = true);
00165 
00166     /**
00167      * Returns the first selected row
00168      *
00169      * @returns                   The first selected row.
00170      * @retval -1                 No row selected.
00171      */
00172     int get_selected_row() const;
00173 
00174     /** Function to call after the user clicked on a row. */
00175     void list_item_clicked(twidget* caller);
00176 
00177     /** Inherited from tcontainer_. */
00178     void set_self_active(const bool /*active*/)  {}
00179 //      { state_ = active ? ENABLED : DISABLED; }
00180 //
00181 
00182     /**
00183      * Request to update the size of the content after changing the content.
00184      *
00185      * When a resize is required the container first can try to handle it
00186      * itself. If it can't honour the request the function will call @ref
00187      * twindow::invalidate_layout().
00188      *
00189      * @note Calling this function on a widget with size == (0, 0) results
00190      * false but doesn't call invalidate_layout, the engine expects to be in
00191      * build up phase with the layout already invalidated.
00192      *
00193      * @returns                      True if the resizing succeeded, false
00194      *                               otherwise.
00195      */
00196     bool update_content_size();
00197 
00198     /***** ***** ***** ***** inherited ***** ***** ****** *****/
00199 
00200     /** Inherited from tscrollbar_container. */
00201     void place(const tpoint& origin, const tpoint& size);
00202 
00203     /** Inherited from tscrollbar_container. */
00204     void layout_children();
00205 
00206     /** Inherited from tscrollbar_container. */
00207     void child_populate_dirty_list(twindow& caller,
00208             const std::vector<twidget*>& call_stack);
00209 
00210     /***** ***** ***** setters / getters for members ***** ****** *****/
00211 
00212     void set_callback_value_change(void (*callback) (twidget* caller))
00213         { callback_value_changed_ = callback; }
00214 
00215     void set_list_builder(tbuilder_grid_ptr list_builder)
00216         { list_builder_ = list_builder; }
00217 
00218 protected:
00219 
00220     /***** ***** ***** ***** keyboard functions ***** ***** ***** *****/
00221 
00222     /** Inherited from tscrollbar_container. */
00223     void handle_key_up_arrow(SDLMod modifier, bool& handled);
00224 
00225     /** Inherited from tscrollbar_container. */
00226     void handle_key_down_arrow(SDLMod modifier, bool& handled);
00227 
00228     /** Inherited from tscrollbar_container. */
00229     void handle_key_left_arrow(SDLMod modifier, bool& handled);
00230 
00231     /** Inherited from tscrollbar_container. */
00232     void handle_key_right_arrow(SDLMod modifier, bool& handled);
00233 
00234 private:
00235 
00236     /**
00237      * @todo A listbox must have the following config parameters in the
00238      * instanciation:
00239      * - fixed row height?
00240      * - fixed column width?
00241      * and if so the following ways to set them
00242      * - fixed depending on header ids
00243      * - fixed depending on footer ids
00244      * - fixed depending on first row ids
00245      * - fixed depending on list (the user has to enter a list of ids)
00246      *
00247      * For now it's always fixed width depending on the first row.
00248      */
00249 
00250 
00251     /**
00252      * Finishes the building initialization of the widget.
00253      *
00254      * @param header              Builder for the header.
00255      * @param footer              Builder for the footer.
00256      * @param list_data           The initial data to fill the listbox with.
00257      */
00258     void finalize(
00259             tbuilder_grid_const_ptr header,
00260             tbuilder_grid_const_ptr footer,
00261             const std::vector<string_map>& list_data);
00262     /**
00263      * Contains a pointer to the generator.
00264      *
00265      * The pointer is not owned by this class, it's stored in the content_grid_
00266      * of the tscrollbar_container super class and freed when it's grid is
00267      * freed.
00268      */
00269     tgenerator_* generator_;
00270 
00271     /** Contains the builder for the new items. */
00272     tbuilder_grid_const_ptr list_builder_;
00273 
00274     /**
00275      * This callback is called when the value in the listbox changes.
00276      *
00277      * @todo the implementation of the callback hasn't been tested a lot and
00278      * there might be too many calls. That might happen if an arrow up didn't
00279      * change the selected item.
00280      */
00281     void (*callback_value_changed_) (twidget*);
00282 
00283     bool need_layout_;
00284 
00285     /**
00286      * Resizes the content.
00287      *
00288      * The resize either happens due to resizing the content or invalidate the
00289      * layout of the window.
00290      *
00291      * @param width_modification  The wanted modification to the width:
00292      *                            * negative values reduce width.
00293      *                            * zero leave width as is.
00294      *                            * positive values increase width.
00295      * @param height_modification The wanted modification to the height:
00296      *                            * negative values reduce height.
00297      *                            * zero leave height as is.
00298      *                            * positive values increase height.
00299      */
00300     void resize_content(
00301               const int width_modification
00302             , const int height_modification);
00303 
00304     /** Layouts the children if needed. */
00305     void layout_children(const bool force);
00306 
00307     /** Inherited from tscrollbar_container. */
00308     virtual void set_content_size(const tpoint& origin, const tpoint& size);
00309 
00310     /** Inherited from tcontrol. */
00311     const std::string& get_control_type() const;
00312 };
00313 
00314 } // namespace gui2
00315 
00316 #endif
00317 #endif
00318 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

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