gui/widgets/generator.hpp

Go to the documentation of this file.
00001 /* $Id: generator.hpp 54007 2012-04-28 19:16:10Z 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 #ifndef GUI_WIDGETS_GENERATOR_HPP_INCLUDED
00017 #define GUI_WIDGETS_GENERATOR_HPP_INCLUDED
00018 
00019 #include "widget.hpp"
00020 #include "config.hpp"
00021 
00022 #include <boost/intrusive_ptr.hpp>
00023 
00024 namespace gui2 {
00025 
00026 struct tbuilder_grid;
00027 typedef boost::intrusive_ptr<const tbuilder_grid> tbuilder_grid_const_ptr;
00028 
00029 class tgrid;
00030 
00031 /**
00032  * Abstract base class for the generator.
00033  *
00034  * A generator is a class which holds multiple grids and controls their
00035  * placement on the screen. The final class is policy based, more info about
00036  * the possible policies is documented in the build() function. This function
00037  * is the factory to generate the classes as well.
00038  */
00039 class tgenerator_
00040         : public virtual twidget
00041 {
00042     friend class tdebug_layout_graph;
00043 
00044 public:
00045     virtual ~tgenerator_() {}
00046 
00047     /** Determines how the items are placed. */
00048     enum tplacement
00049             { horizontal_list
00050             , vertical_list
00051             , grid
00052             , independent
00053             };
00054 
00055     /**
00056      * Create a new generator.
00057      *
00058      * @param has_minimum         Does one item need to be selected.
00059      * @param has_maximum         Is one the maximum number of items that can
00060      *                            be selected?
00061      * @param placement           The placement of the grids, see tplacement
00062      *                            for more info.
00063      * @param select              If a grid is selected, what should happen?
00064      *                            If true the grid is selected, if false the
00065      *                            grid is shown.
00066      *
00067      * @returns                   A pointer to a new object. The caller gets
00068      *                            ownership of the new object.
00069      */
00070     static tgenerator_* build(const bool has_minimum, const bool has_maximum,
00071             const tplacement placement, const bool select);
00072 
00073     /**
00074      * Deletes an item.
00075      */
00076     virtual void delete_item(const unsigned index) = 0;
00077 
00078     /** Deletes all items. */
00079     virtual void clear() = 0;
00080 
00081     /**
00082      * (De)selects an item.
00083      *
00084      * @param index               The item to (de)select.
00085      * @param select              If true selects, if false deselects.
00086      */
00087     virtual void select_item(const unsigned index,
00088             const bool select = true) = 0;
00089 
00090     /**
00091      * Toggles the selection state of an item.
00092      *
00093      * @param index               The item to toggle.
00094      */
00095     void toggle_item(const unsigned index)
00096     {
00097         select_item(index, !is_selected(index));
00098     }
00099 
00100     /** Returns whether the item is selected. */
00101     virtual bool is_selected(const unsigned index) const = 0;
00102 
00103     /**
00104      * Shows or hides an item.
00105      *
00106      * The caller is responsible for reformatting the grid.
00107      *
00108      * @param index               The item to show or hide.
00109      * @param show                If true shows the item, else hides it.
00110      */
00111     virtual void set_item_shown(const unsigned index, const bool show) = 0;
00112 
00113     /** Returns whether the item is shown. */
00114     virtual bool get_item_shown(const unsigned index) const = 0;
00115 
00116     /** Returns the number of items. */
00117     virtual unsigned get_item_count() const = 0;
00118 
00119     /** Returns the number of selected items. */
00120     virtual unsigned get_selected_item_count() const = 0;
00121 
00122     /**
00123      * Returns the selected item.
00124      *
00125      * If a list has multiple selected items it looks whether it knows the last
00126      * item acutally selected, if that item is selected that one is chosen.
00127      * Else is goes through all selected items and returns the first one
00128      * selected.
00129      *
00130      * @note tstacked_widget depends on that behaviour it always has all items
00131      * selected and thus shown and by default the last selected item (the top
00132      * one) is active.
00133      *
00134      * @returns                   The selected item, -1 if none selected.
00135      */
00136     virtual int get_selected_item() const = 0;
00137 
00138     /** Gets the grid of an item. */
00139     virtual tgrid& item(const unsigned index) = 0;
00140 
00141     /** Gets the grid of an item. */
00142     virtual const tgrid& item(const unsigned index) const = 0;
00143 
00144     /***** ***** ***** ***** Create items ***** ***** ***** *****/
00145 
00146     /**
00147      * Creates a new item.
00148      *
00149      * The item_data is used for the first widget found, this normally should
00150      * be used when there's one widget in an item.
00151      *
00152      * @param index               The item before which to add the new item,
00153      *                            0 == begin, -1 == end.
00154      * @param list_builder        A grid builder that's will build the
00155      *                            contents of the new item.
00156      * @param item_data           The data to initialize the parameters of
00157      *                            the new item.
00158      * @param callback            The callback function to call when an item
00159      *                            in the grid is (de)selected.
00160      *
00161      * @returns                   A reference to the newly created grid.
00162      */
00163     virtual tgrid& create_item(const int index,
00164             tbuilder_grid_const_ptr list_builder,
00165             const string_map& item_data,
00166             void (*callback)(twidget*)) = 0;
00167 
00168     /**
00169      * Creates a new item.
00170      *
00171      * The item_data is used by id, and is meant to set multiple widgets in
00172      * an item.
00173      *
00174      * @param index               The item before which to add the new item,
00175      *                            0 == begin, -1 == end.
00176      * @param list_builder        A grid builder that's will build the
00177      *                            contents of the new item.
00178      * @param data                The data to initialize the parameters of
00179      *                            the new item.
00180      * @param callback            The callback function to call when an item
00181      *                            in the grid is (de)selected.
00182      *
00183      * @returns                   A reference to the newly created grid.
00184      */
00185     virtual tgrid& create_item(const int index,
00186             tbuilder_grid_const_ptr list_builder,
00187             const std::map<std::string /* widget id */,
00188             string_map>& data,
00189             void (*callback)(twidget*)) = 0;
00190 
00191     /**
00192      * Creates one or more new item(s).
00193      *
00194      * For every item in item_data a new item is generated. This version
00195      * expects one widget per item.
00196      *
00197      * @param index               The item before which to add the new item,
00198      *                            0 == begin, -1 == end.
00199      * @param list_builder        A grid builder that's will build the
00200      *                            contents of the new item.
00201      * @param data                The data to initialize the parameters of
00202      *                            the new item.
00203      * @param callback            The callback function to call when an item
00204      *                            in the grid is (de)selected.
00205      */
00206     virtual void create_items(const int index,
00207             tbuilder_grid_const_ptr list_builder,
00208             const std::vector<string_map>& data,
00209             void (*callback)(twidget*)) = 0;
00210 
00211     /**
00212      * Creates one or more new item(s).
00213      *
00214      * For every item in item_data a new item is generated. This version
00215      * expects multiple widgets per item.
00216      *
00217      * @param index               The item before which to add the new item,
00218      *                            0 == begin, -1 == end.
00219      * @param list_builder        A grid builder that's will build the
00220      *                            contents of the new item.
00221      * @param data                The data to initialize the parameters of
00222      *                            the new item.
00223      * @param callback            The callback function to call when an item
00224      *                            in the grid is (de)selected.
00225      */
00226     virtual void create_items(const int index,
00227             tbuilder_grid_const_ptr list_builder,
00228             const std::vector<std::map<std::string /*widget id*/,
00229             string_map> >& data,
00230             void (*callback)(twidget*)) = 0;
00231 
00232     /***** ***** ***** ***** Inherited ***** ***** ***** *****/
00233 
00234     /*
00235      * These functions must be defined in our child classes so make sure they
00236      * become pure virtuals.
00237      */
00238 
00239     /** Inherited from twidget. */
00240     virtual void layout_init(const bool full_initialization) = 0;
00241 
00242     /** Inherited from twidget. */
00243     virtual void request_reduce_width(const unsigned maximum_width) = 0;
00244 
00245     /** Inherited from twidget. */
00246     virtual void request_reduce_height(const unsigned maximum_height) = 0;
00247 
00248     /** Inherited from twidget. */
00249     virtual tpoint calculate_best_size() const = 0;
00250 
00251     /** Inherited from twidget. */
00252     virtual void place(const tpoint& origin, const tpoint& size) = 0;
00253 
00254     /** Inherited from twidget. */
00255     virtual void set_origin(const tpoint& origin) = 0;
00256 
00257     /** Inherited from twidget. */
00258     void set_visible_area(const SDL_Rect& area) = 0;
00259 
00260     /** Inherited from twidget. */
00261     virtual void impl_draw_children(surface& frame_buffer) = 0;
00262     virtual void impl_draw_children(
00263               surface& frame_buffer
00264             , int x_offset
00265             , int y_offset) = 0;
00266 
00267 protected:
00268 
00269     /** Inherited from twidget. */
00270     virtual void child_populate_dirty_list(twindow& caller,
00271             const std::vector<twidget*>& call_stack) = 0;
00272 
00273 public:
00274 
00275     /** Inherited from twidget. */
00276     virtual twidget* find_at(
00277             const tpoint& coordinate, const bool must_be_active) = 0;
00278 
00279     /** Inherited from twidget. */
00280     virtual const twidget* find_at(
00281             const tpoint& coordinate, const bool must_be_active) const = 0;
00282 
00283     /***** ***** ***** ***** keyboard functions ***** ***** ***** *****/
00284 
00285     /**
00286      * Up arrow key pressed.
00287      *
00288      * @param modifier            The SDL keyboard modifier when the key was
00289      *                            pressed.
00290      * @param handled             If the function handles the key it should
00291      *                            set handled to true else do not modify it.
00292      *                            This is used in the keyboard event
00293      *                            changing.
00294      */
00295     virtual void handle_key_up_arrow(SDLMod modifier, bool& handled) = 0;
00296 
00297     /**
00298      * Down arrow key pressed.
00299      *
00300      * @param modifier            The SDL keyboard modifier when the key was
00301      *                            pressed.
00302      * @param handled             If the function handles the key it should
00303      *                            set handled to true else do not modify it.
00304      *                            This is used in the keyboard event
00305      *                            changing.
00306      */
00307     virtual void handle_key_down_arrow(SDLMod modifier, bool& handled) = 0;
00308 
00309     /**
00310      * Left arrow key pressed.
00311      *
00312      * @param modifier            The SDL keyboard modifier when the key was
00313      *                            pressed.
00314      * @param handled             If the function handles the key it should
00315      *                            set handled to true else do not modify it.
00316      *                            This is used in the keyboard event
00317      *                            changing.
00318      */
00319     virtual void handle_key_left_arrow(SDLMod modifier, bool& handled) = 0;
00320 
00321     /**
00322      * Right arrow key pressed.
00323      *
00324      * @param modifier            The SDL keyboard modifier when the key was
00325      *                            pressed.
00326      * @param handled             If the function handles the key it should
00327      *                            set handled to true else do not modify it.
00328      *                            This is used in the keyboard event
00329      *                            changing.
00330      */
00331     virtual void handle_key_right_arrow(SDLMod modifier, bool& handled) = 0;
00332 protected:
00333 
00334     /**
00335      * Selects a not selected item.
00336      *
00337      * @param index               The index of a not selected item.
00338      */
00339     virtual void do_select_item(const unsigned index) = 0;
00340 
00341     /**
00342      * Deselects a selected item.
00343      *
00344      * @param index               The index of a selected item.
00345      */
00346     virtual void do_deselect_item(const unsigned index) = 0;
00347 };
00348 
00349 } // namespace gui2
00350 
00351 #endif
00352 
 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