control.hpp

Go to the documentation of this file.
00001 /* $Id: control.hpp 26681 2008-05-18 06:05:59Z mordante $ */
00002 /*
00003    copyright (C) 2008 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 version 2
00008    or at your option any later version.
00009    this program is distributed in the hope that it will be useful,
00010    but without any warranty.
00011 
00012    see the copying file for more details.
00013 */
00014 
00015 #ifndef __GUI_WIDGETS_CONTROL_HPP_INCLUDED__
00016 #define __GUI_WIDGETS_CONTROL_HPP_INCLUDED__
00017 
00018 #include "gui/widgets/canvas.hpp"
00019 #include "gui/widgets/settings.hpp"
00020 #include "gui/widgets/widget.hpp"
00021 #include "tstring.hpp"
00022 
00023 #include <cassert>
00024 
00025 namespace gui2 {
00026 
00027 //! Base class for all visible items.
00028 class tcontrol : public virtual twidget
00029 {
00030 
00031     tcontrol();
00032 public:
00033 
00034     tcontrol(const unsigned canvas_count);
00035     virtual ~tcontrol() {}
00036 
00037     //! Inherted from twidget.
00038     void mouse_hover(tevent_handler& event);
00039 
00040     //! Inherted from twidget.
00041     void help_key(tevent_handler& event);
00042 
00043     //! Inherited from twidget.
00044     void set_size(const SDL_Rect& rect);
00045 
00046     /** Inherited from twidget. */
00047     twidget* find_widget(const tpoint& coordinate, const bool must_be_active) 
00048     {
00049         return (twidget::find_widget(coordinate, must_be_active) 
00050             && (!must_be_active || get_active())) ? this : 0;
00051     }
00052 
00053     /** Inherited from twidget. */
00054     const twidget* find_widget(const tpoint& coordinate, 
00055             const bool must_be_active) const
00056     {
00057         return (twidget::find_widget(coordinate, must_be_active) 
00058             && (!must_be_active || get_active())) ? this : 0;
00059     }
00060 
00061     /** Inherited from twidget.*/
00062     twidget* find_widget(const std::string& id, const bool must_be_active)
00063     {
00064         return (twidget::find_widget(id, must_be_active) 
00065             && (!must_be_active || get_active())) ? this : 0;
00066     }
00067 
00068     /** Inherited from twidget.*/
00069     const twidget* find_widget(const std::string& id, 
00070             const bool must_be_active) const
00071     {
00072         return (twidget::find_widget(id, must_be_active) 
00073             && (!must_be_active || get_active())) ? this : 0;
00074     }
00075 
00076     void set_visible(const bool visible = true) 
00077         { if(visible_ != visible) { visible_ = visible; set_dirty();} }
00078     bool get_visible() const { return visible_; }
00079 
00080     void set_multiline_label(const bool multiline = true) 
00081         { if(multiline != multiline_label_) { multiline_label_ = multiline; set_dirty(); } }
00082 
00083     void set_label(const t_string& label);
00084 
00085     const t_string& label() const { return label_; }
00086 
00087     // Note setting the tooltip_ doesn't dirty an object.
00088     void set_tooltip(const t_string& tooltip) 
00089         { tooltip_ = tooltip; set_wants_mouse_hover(!tooltip_.empty()); }
00090     const t_string& tooltip() const { return tooltip_; }
00091 
00092     // Note setting the help_message_ doesn't dirty an object.
00093     void set_help_message(const t_string& help_message) { help_message_ = help_message; }
00094     const t_string& help_message() const { return help_message_; }
00095 
00096     std::vector<tcanvas>& canvas() { return canvas_; }
00097     tcanvas& canvas(const unsigned index) 
00098         { assert(index < canvas_.size()); return canvas_[index]; }
00099 
00100     /** 
00101      * Inherited from twidget. 
00102      * 
00103      * This function sets the defintion of a control and should be called soon
00104      * after creating the object since a lot of internal functions depend on the
00105      * definition.
00106      *
00107      * This function should be called one time only!!!
00108      */
00109     void set_definition(const std::string& definition);
00110 
00111     //! Inherited from twidget.
00112     void draw(surface& surface);
00113 
00114     //! Sets the control in the active state, when inactive a control can't be
00115     //! used and doesn't react to events. (Note read-only for a ttext_ is a
00116     //! different state.)
00117     virtual void set_active(const bool active) = 0;
00118 
00119     //! Gets the active state of the control.
00120     virtual bool get_active() const = 0;
00121 
00122     // note we should check whether the label fits in the button
00123     // Inherited from twidget.
00124     tpoint get_minimum_size() const;
00125     tpoint get_best_size() const;
00126     tpoint get_maximum_size() const;
00127 
00128     /**
00129      * Inherited from twidget.
00130      *
00131      * This function shouldn't be called directly it's called by set_definition().
00132      */
00133     void load_config();
00134 
00135 private:
00136     //! Helpers
00137     tpoint get_single_line_best_size(const tpoint& config_size) const;
00138     tpoint get_multi_line_best_size(const tpoint& config_size) const;
00139 
00140 public:
00141 protected:
00142 
00143     //! Returns the id of the state, which is also the index for the canvas.
00144     virtual unsigned get_state() const = 0;
00145 
00146     //! Does the widget need to restore the surface before (re)painting?
00147     virtual bool full_redraw() const;
00148 
00149     //! Sets the text variable for the canvases.
00150     virtual void set_canvas_text();
00151 
00152     tresolution_definition_* config() { return config_; }
00153     const tresolution_definition_* config() const { return config_; }
00154 
00155     void set_config(tresolution_definition_* config) { config_ = config; }
00156 
00157 private:
00158 
00159     //! Visible state of the control, invisible isn't drawn.
00160     bool visible_;
00161 
00162     //! Can the label contain multiple lines.
00163     bool multiline_label_;
00164 
00165     //! If multiline we need to have a version of the label wrapped to fit
00166     //! in the widget. This value is cached so changes to the label or widget
00167     //! size should clear us.
00168     std::string wrapped_label_;
00169 
00170     //! The label associated with a lot of widgets to show a (non editable) text.
00171     t_string label_;
00172 
00173     //! When hovering a tooltip with extra information can show up.
00174     t_string tooltip_;
00175 
00176     //! When the user presses help a tooltip with even more info can be shown.
00177     t_string help_message_;
00178 
00179     //! Holds all canvas objects for a control. 
00180     std::vector<tcanvas> canvas_;
00181 
00182     //! Holds a copy of the original background which can be used before
00183     //! redrawing. This is needed for semi-tranparent items, the user
00184     //! defines whether it's required or not.
00185     surface restorer_;
00186 
00187     //! Saves the portion of the background.
00188     void save_background(const surface& src);
00189 
00190     //! Restores a portion of the background.
00191     void restore_background(surface& dst);
00192 
00193     //! Contains a pointer to the configuration of this button at the 
00194     //! current resolution.
00195     tresolution_definition_* config_;
00196 
00197     //! Once the config is loaded this function is called.
00198     virtual void load_config_extra() {}
00199 
00200     //! The control_type parameter for tgui_definition::get_control()
00201     //! To keep the code more generic this type is required so the
00202     //! controls need to return the proper string here.
00203     //! Might be used at other parts as well the get the type of 
00204     //! control involved.
00205     virtual const std::string& get_control_type() const = 0;
00206 };
00207 
00208 } // namespace gui2
00209 
00210 #endif
00211 

Generated by doxygen 1.5.5 on 23 May 2008 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs