gui/widgets/list.hpp

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