gui/widgets/control.hpp

Go to the documentation of this file.
00001 /* $Id: control.hpp 54220 2012-05-19 08:46:20Z 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_CONTROL_HPP_INCLUDED
00017 #define GUI_WIDGETS_CONTROL_HPP_INCLUDED
00018 
00019 #include "gui/auxiliary/widget_definition.hpp"
00020 #include "gui/widgets/widget.hpp"
00021 #include "../../text.hpp"
00022 
00023 namespace gui2 {
00024 
00025 namespace implementation {
00026     class tbuilder_control;
00027 } // namespace implementation
00028 
00029 /** Base class for all visible items. */
00030 class tcontrol : public virtual twidget
00031 {
00032     friend class tdebug_layout_graph;
00033 public:
00034 
00035     /** @deprecated Used the second overload. */
00036     explicit tcontrol(const unsigned canvas_count);
00037 
00038     /**
00039      * Constructor.
00040      *
00041      * @param builder             The builder object with the settings for the
00042      *                            object.
00043      *
00044      * @param canvas_count        The number of canvasses in the control.
00045      */
00046     tcontrol(
00047               const implementation::tbuilder_control& builder
00048             , const unsigned canvas_count
00049             , const std::string& control_type);
00050 
00051     /**
00052      * Sets the members of the control.
00053      *
00054      * The map contains named members it can set, controls inheriting from us
00055      * can add additional members to set by this function. The following
00056      * members can by the following key:
00057      *  * label_                  label
00058      *  * tooltip_                tooltip
00059      *  * help_message_           help
00060      *
00061      *
00062      * @param data                Map with the key value pairs to set the members.
00063      */
00064     virtual void set_members(const string_map& data);
00065 
00066     /***** ***** ***** ***** State handling ***** ***** ***** *****/
00067 
00068     /**
00069      * Sets the control's state.
00070      *
00071      *  Sets the control in the active state, when inactive a control can't be
00072      *  used and doesn't react to events. (Note read-only for a ttext_ is a
00073      *  different state.)
00074      */
00075     virtual void set_active(const bool active) = 0;
00076 
00077     /** Gets the active state of the control. */
00078     virtual bool get_active() const = 0;
00079 
00080 protected:
00081     /** Returns the id of the state.
00082      *
00083      * The current state is also the index canvas_.
00084      */
00085     virtual unsigned get_state() const = 0;
00086 
00087 public:
00088 
00089     /***** ***** ***** ***** Easy close handling ***** ***** ***** *****/
00090 
00091     /**
00092      * Inherited from twidget.
00093      *
00094      * The default behavious is that a widget blocks easy close, if not it
00095      * hould override this function.
00096      */
00097     bool disable_click_dismiss() const;
00098 
00099     /** Inherited from twidget. */
00100     virtual iterator::twalker_* create_walker();
00101 
00102     /***** ***** ***** ***** layout functions ***** ***** ***** *****/
00103 
00104     /**
00105      * Gets the minimum size as defined in the config.
00106      *
00107      * @pre                       config_ !=  NULL
00108      *
00109      * @returns                   The size.
00110      */
00111     tpoint get_config_minimum_size() const;
00112 
00113     /**
00114      * Gets the default size as defined in the config.
00115      *
00116      * @pre                       config_ !=  NULL
00117      *
00118      * @returns                   The size.
00119      */
00120     tpoint get_config_default_size() const;
00121 
00122     /**
00123      * Gets the best size as defined in the config.
00124      *
00125      * @pre                       config_ !=  NULL
00126      *
00127      * @returns                   The size.
00128      */
00129     tpoint get_config_maximum_size() const;
00130 
00131     /**
00132      * Returns the number of characters per line.
00133      *
00134      * This value is used to call @ref ttext::set_characters_per_line
00135      * (indirectly).
00136      *
00137      * @returns                   The characters per line. This implementation
00138      *                            always returns 0.
00139      */
00140     virtual unsigned get_characters_per_line() const;
00141 
00142     /** Inherited from twidget. */
00143     /** @todo Also handle the tooltip state if shrunken_ &&
00144      * use_tooltip_on_label_overflow_. */
00145     void layout_init(const bool full_initialization);
00146 
00147     /** Inherited from twidget. */
00148     void request_reduce_width(const unsigned maximum_width);
00149 
00150 protected:
00151     /** Inherited from twidget. */
00152     tpoint calculate_best_size() const;
00153 public:
00154 
00155     /** Inherited from twidget. */
00156     void place(const tpoint& origin, const tpoint& size);
00157 
00158     /***** ***** ***** ***** Inherited ***** ***** ***** *****/
00159 
00160 private:
00161 
00162     /**
00163      * Loads the configuration of the widget.
00164      *
00165      * Controls have their definition stored in a definition object. In order to
00166      * determine sizes and drawing the widget this definition needs to be
00167      * loaded. The member definition_ contains the name of the definition and
00168      * function load the proper configuration.
00169      *
00170      * @depreciated @ref definition_load_configuration() is the replacement.
00171      */
00172     void load_config();
00173 
00174     /**
00175      * Uses the load function.
00176      *
00177      * @note This doesn't look really clean, but the final goal is refactor
00178      * more code and call load_config in the ctor, removing the use case for
00179      * the window. That however is a longer termine refactoring.
00180      */
00181     friend class twindow;
00182 
00183 public:
00184     /** Inherited from twidget. */
00185     twidget* find_at(const tpoint& coordinate, const bool must_be_active)
00186     {
00187         return (twidget::find_at(coordinate, must_be_active)
00188             && (!must_be_active || get_active())) ? this : NULL;
00189     }
00190 
00191     /** Inherited from twidget. */
00192     const twidget* find_at(const tpoint& coordinate,
00193             const bool must_be_active) const
00194     {
00195         return (twidget::find_at(coordinate, must_be_active)
00196             && (!must_be_active || get_active())) ? this : NULL;
00197     }
00198 
00199     /** Inherited from twidget.*/
00200     twidget* find(const std::string& id, const bool must_be_active)
00201     {
00202         return (twidget::find(id, must_be_active)
00203             && (!must_be_active || get_active())) ? this : NULL;
00204     }
00205 
00206     /** Inherited from twidget.*/
00207     const twidget* find(const std::string& id,
00208             const bool must_be_active) const
00209     {
00210         return (twidget::find(id, must_be_active)
00211             && (!must_be_active || get_active())) ? this : NULL;
00212     }
00213 
00214     /**
00215      * Sets the definition.
00216      *
00217      * This function sets the definition of a control and should be called soon
00218      * after creating the object since a lot of internal functions depend on the
00219      * definition.
00220      *
00221      * This function should be called one time only!!!
00222      */
00223     void set_definition(const std::string& definition);
00224 
00225     /***** ***** ***** setters / getters for members ***** ****** *****/
00226     bool get_use_tooltip_on_label_overflow() const { return use_tooltip_on_label_overflow_; }
00227     void set_use_tooltip_on_label_overflow(const bool use_tooltip = true)
00228         { use_tooltip_on_label_overflow_ = use_tooltip; }
00229 
00230     const t_string& label() const { return label_; }
00231     virtual void set_label(const t_string& label);
00232 
00233     virtual void set_use_markup(bool use_markup);
00234     bool get_use_markup() const { return use_markup_; }
00235 
00236     const t_string& tooltip() const { return tooltip_; }
00237     // Note setting the tooltip_ doesn't dirty an object.
00238     void set_tooltip(const t_string& tooltip)
00239         { tooltip_ = tooltip; set_wants_mouse_hover(!tooltip_.empty()); }
00240 
00241     const t_string& help_message() const { return help_message_; }
00242     // Note setting the help_message_ doesn't dirty an object.
00243     void set_help_message(const t_string& help_message) { help_message_ = help_message; }
00244 
00245     // const versions will be added when needed
00246     std::vector<tcanvas>& canvas() { return canvas_; }
00247     tcanvas& canvas(const unsigned index)
00248         { assert(index < canvas_.size()); return canvas_[index]; }
00249 
00250     void set_text_alignment(const PangoAlignment text_alignment);
00251     PangoAlignment get_text_alignment() const
00252     {
00253         return text_alignment_;
00254     }
00255 
00256 protected:
00257     tresolution_definition_ptr config() { return config_; }
00258     tresolution_definition_const_ptr config() const { return config_; }
00259 
00260     void set_config(tresolution_definition_ptr config) { config_ = config; }
00261 
00262     /***** ***** ***** ***** miscellaneous ***** ***** ***** *****/
00263 
00264     /**
00265      * Updates the canvas(ses).
00266      *
00267      * This function should be called if either the size of the widget changes
00268      * or the text on the widget changes.
00269      */
00270     virtual void update_canvas();
00271 
00272     /**
00273      * Returns the maximum width available for the text.
00274      *
00275      * This value makes sense after the widget has been given a size, since the
00276      * maximum width is based on the width of the widget.
00277      */
00278     int get_text_maximum_width() const;
00279 
00280     /**
00281      * Returns the maximum height available for the text.
00282      *
00283      * This value makes sense after the widget has been given a size, since the
00284      * maximum height is based on the height of the widget.
00285      */
00286     int get_text_maximum_height() const;
00287 
00288 private:
00289     /**
00290      * The definition is the id of that widget class.
00291      *
00292      * Eg for a button it [button_definition]id. A button can have multiple
00293      * definitions which all look different but for the engine still is a
00294      * button.
00295      */
00296     std::string definition_;
00297 
00298     /** Contain the non-editable text associated with control. */
00299     t_string label_;
00300 
00301     /** Use markup for the label? */
00302     bool use_markup_;
00303 
00304     /**
00305      * If the text doesn't fit on the label should the text be used as tooltip?
00306      *
00307      * This only happens if the tooltip is empty.
00308      */
00309     bool use_tooltip_on_label_overflow_;
00310 
00311     /**
00312      * Tooltip text.
00313      *
00314      * The hovering event can cause a small tooltip to be shown, this is the
00315      * text to be shown. At the moment the tooltip is a single line of text.
00316      */
00317     t_string tooltip_;
00318 
00319     /**
00320      * Tooltip text.
00321      *
00322      * The help event can cause a tooltip to be shown, this is the text to be
00323      * shown. At the moment the tooltip is a single line of text.
00324      */
00325     t_string help_message_;
00326 
00327     /**
00328      * Holds all canvas objects for a control.
00329      *
00330      * A control can have multiple states, which are defined in the classes
00331      * inheriting from us. For every state there is a separate canvas, which is
00332      * stored here. When drawing the state is determined and that canvas is
00333      * drawn.
00334      */
00335     std::vector<tcanvas> canvas_;
00336 
00337     /**
00338      * Contains the pointer to the configuration.
00339      *
00340      * Every control has a definition of how it should look, this contains a
00341      * pointer to the definition. The definition is resolution dependant, where
00342      * the resolution is the size of the Wesnoth application window. Depending
00343      * on the resolution widgets can look different, use different fonts.
00344      * Windows can use extra scrollbars use abbreviations as text etc.
00345      */
00346     tresolution_definition_ptr config_;
00347 
00348     /**
00349      * Load class dependant config settings.
00350      *
00351      * load_config will call this method after loading the config, by default it
00352      * does nothing but classes can override it to implement custom behaviour.
00353      */
00354     virtual void load_config_extra() {}
00355 
00356     /**
00357      * Loads the configuration of the widget.
00358      *
00359      * Controls have their definition stored in a definition object. In order to
00360      * determine sizes and drawing the widget this definition needs to be
00361      * loaded. The member definition_ contains the name of the definition and
00362      * function load the proper configuration.
00363      */
00364     void definition_load_configuration(const std::string& control_type);
00365 
00366 public:
00367     /**
00368      * Returns the control_type of the control.
00369      *
00370      * The control_type parameter for tgui_definition::get_control() To keep the
00371      * code more generic this type is required so the controls need to return
00372      * the proper string here.  Might be used at other parts as well the get the
00373      * type of
00374      * control involved.
00375      */
00376     virtual const std::string& get_control_type() const = 0;
00377 
00378 protected:
00379     /** Inherited from twidget. */
00380     void impl_draw_background(surface& frame_buffer);
00381     void impl_draw_background(
00382               surface& frame_buffer
00383             , int x_offset
00384             , int y_offset);
00385 
00386     /** Inherited from twidget. */
00387     void impl_draw_foreground(surface& /*frame_buffer*/) { /* do nothing */ }
00388     void impl_draw_foreground(
00389               surface& /*frame_buffer*/
00390             , int /*x_offset*/
00391             , int /*y_offset*/)
00392     { /* do nothing */ }
00393 
00394 private:
00395 
00396 #ifdef GUI2_EXPERIMENTAL_LISTBOX
00397     /**
00398      * Initializes the control.
00399      *
00400      * Not everything can be code in the constructor since virtual functions
00401      * can't be used. So after contruction this function needs to be called and
00402      * only once, this happens when set_definition is called.
00403      */
00404     virtual void init() {}
00405 #endif
00406 
00407     /**
00408      * Gets the best size for a text.
00409      *
00410      * @param minimum_size        The minimum size of the text.
00411      * @param maximum_size        The wanted maximum size of the text, if not
00412      *                            possible it's ignored. A value of 0 means
00413      *                            that it's ignored as well.
00414      *
00415      * @returns                   The best size.
00416      */
00417     tpoint get_best_text_size(const tpoint& minimum_size,
00418         const tpoint& maximum_size = tpoint(0,0)) const;
00419 
00420     /**
00421      * Contains a helper cache for the rendering.
00422      *
00423      * Creating a ttext object is quite expensive and is done on various
00424      * occasions so it's cached here.
00425      *
00426      * @todo Maybe if still too slow we might also copy this cache to the
00427      * canvas so it can reuse our results, but for now it seems fast enough.
00428      * Unfortunately that would make the dependency between the classes bigger
00429      * as wanted.
00430      */
00431     mutable font::ttext renderer_;
00432 
00433     /** The maximum width for the text in a control. */
00434     int text_maximum_width_;
00435 
00436     /** The alignment of the text in a control. */
00437     PangoAlignment text_alignment_;
00438 
00439     /** Is the widget smaller as it's best size? */
00440     bool shrunken_;
00441 
00442     /***** ***** ***** signal handlers ***** ****** *****/
00443 
00444     void signal_handler_show_tooltip(
00445               const event::tevent event
00446             , bool& handled
00447             , const tpoint& location);
00448 
00449     void signal_handler_show_helptip(
00450               const event::tevent event
00451             , bool& handled
00452             , const tpoint& location);
00453 
00454     void signal_handler_notify_remove_tooltip(
00455               const event::tevent event
00456             , bool& handled);
00457 };
00458 
00459 } // namespace gui2
00460 
00461 #endif
00462 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

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