gui/widgets/text.hpp

Go to the documentation of this file.
00001 /* $Id: text.hpp 54028 2012-04-29 20:48:43Z 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_TEXT_HPP_INCLUDED
00017 #define GUI_WIDGETS_TEXT_HPP_INCLUDED
00018 
00019 //#include "gui/auxiliary/event/dispatcher.hpp"
00020 #include "gui/widgets/control.hpp"
00021 #include "../../text.hpp"
00022 
00023 #include <string>
00024 
00025 #include <boost/function.hpp>
00026 
00027 namespace gui2 {
00028 
00029 /**
00030  * Abstract base class for text items.
00031  *
00032  * All other text classes should inherit from this base class.
00033  *
00034  * The NOTIFY_MODIFIED event is send when the text is modified.
00035  *
00036  * @todo Validate whether the NOTIFY_MODIFIED is always fired properly. The
00037  * current implementation is added for some quick testing so some cases might
00038  * be forgotten.
00039  *
00040  * Common signal handlers:
00041  * - connect_signal_pre_key_press
00042  */
00043 class ttext_ : public tcontrol
00044 {
00045 
00046 public:
00047     ttext_();
00048 
00049     /** Inherited from tcontrol. */
00050     void set_active(const bool active)
00051         { if(get_active() != active) set_state(active ? ENABLED : DISABLED); };
00052 
00053     /** Inherited from tcontrol. */
00054     bool get_active() const { return state_ != DISABLED; }
00055 
00056     /** Inherited from tcontrol. */
00057     unsigned get_state() const { return state_; }
00058 
00059     /***** ***** ***** ***** expose some functions ***** ***** ***** *****/
00060 
00061     void set_maximum_length(const size_t maximum_length);
00062 
00063     size_t get_length() const { return text_.get_length(); }
00064 
00065     /***** ***** ***** setters / getters for members ***** ****** *****/
00066 
00067     /**
00068      * The set_value is virtual for the @ref tpassword_box class.
00069      *
00070      * That class overrides the set_value function to replace it with asterisk.
00071      * There might be more generic way to do it when more classes are needed.
00072      */
00073     virtual void set_value(const std::string& text);
00074     std::string get_value() const { return text_.text(); }
00075 
00076     const std::string& text() const { return text_.text(); }
00077 
00078     /** Set the text_changed callback. */
00079     void set_text_changed_callback(
00080         boost::function< void (ttext_* textbox, const std::string text) > cb)
00081     {
00082         text_changed_callback_ = cb;
00083     }
00084 
00085 protected:
00086 
00087     /**
00088      * Moves the cursor to the end of the line.
00089      *
00090      * @param select              Select the text from the original cursor
00091      *                            position till the end of the line?
00092      */
00093     virtual void goto_end_of_line(const bool select = false) = 0;
00094 
00095     /**
00096      * Moves the cursor to the end of all text.
00097      *
00098      * For a single line text this is the same as goto_end_of_line().
00099      *
00100      * @param select              Select the text from the original cursor
00101      *                            position till the end of the data?
00102      */
00103     void goto_end_of_data(const bool select = false)
00104         { set_cursor(text_.get_length(), select); }
00105 
00106     /**
00107      * Moves the cursor to the beginning of the line
00108      *
00109      * @param select              Select the text from the original cursor
00110      *                            position till the beginning of the line?
00111      */
00112     virtual void goto_start_of_line(const bool select = false) = 0;
00113 
00114     /**
00115      * Moves the cursor to the beginning of the data.
00116      *
00117      * @param select              Select the text from the original cursor
00118      *                            position till the beginning of the data?
00119      */
00120     void goto_start_of_data(const bool select = false) { set_cursor(0, select); }
00121 
00122     /** Selects all text. */
00123     void select_all() { selection_start_ = 0; goto_end_of_data(true); }
00124 
00125     /**
00126      * Moves the cursor at the wanted position.
00127      *
00128      * @param offset              The wanted new cursor position.
00129      * @param select              Select the text from the original cursor
00130      *                            position till the new position?
00131      */
00132     void set_cursor(const size_t offset, const bool select);
00133 
00134     /**
00135      * Inserts a character at the cursor.
00136      *
00137      * This function is preferred over set_text since it's optimized for
00138      * updating the internal bookkeeping.
00139      *
00140      * @param unicode             The unicode value of the character to insert.
00141      */
00142     virtual void insert_char(const Uint16 unicode);
00143 
00144     /**
00145      *  Deletes the character.
00146      *
00147      *  @param before_cursor     If true it deletes the character before the cursor
00148      *                           (backspace) else the character after the cursor
00149      *                           (delete).
00150      */
00151     virtual void delete_char(const bool before_cursor) = 0;
00152 
00153     /** Deletes the current selection. */
00154     virtual void delete_selection() = 0;
00155 
00156     /** Copies the current selection. */
00157     virtual void copy_selection(const bool mouse);
00158 
00159     /** Pastes the current selection. */
00160     virtual void paste_selection(const bool mouse);
00161 
00162     /***** ***** ***** ***** expose some functions ***** ***** ***** *****/
00163 
00164     gui2::tpoint get_cursor_position(
00165         const unsigned column, const unsigned line = 0) const
00166         { return text_.get_cursor_position(column, line); }
00167 
00168     tpoint get_column_line(const tpoint& position) const
00169         { return text_.get_column_line(position); }
00170 
00171     void set_font_size(const unsigned font_size)
00172         { text_.set_font_size(font_size); }
00173 
00174     void set_font_style(const unsigned font_style)
00175         { text_.set_font_style(font_style); }
00176 
00177     void set_maximum_width(const int width)
00178         { text_.set_maximum_width(width); }
00179 
00180     void set_maximum_height(const int height)
00181         { text_.set_maximum_height(height); }
00182 
00183     void set_ellipse_mode(const PangoEllipsizeMode ellipse_mode)
00184         { text_.set_ellipse_mode(ellipse_mode); }
00185 
00186     /***** ***** ***** setters / getters for members ***** ****** *****/
00187 
00188     size_t get_selection_start() const { return selection_start_; }
00189     void  set_selection_start(const size_t selection_start);
00190 
00191     size_t get_selection_length() const { return selection_length_; }
00192     void set_selection_length(const int selection_length);
00193 
00194 
00195 private:
00196     /** Note the order of the states must be the same as defined in settings.hpp. */
00197     enum tstate { ENABLED, DISABLED, FOCUSSED, COUNT };
00198 
00199     void set_state(const tstate state);
00200 
00201     /**
00202      * Current state of the widget.
00203      *
00204      * The state of the widget determines what to render and how the widget
00205      * reacts to certain 'events'.
00206      */
00207     tstate state_;
00208 
00209     /** The text entered in the widget. */
00210     font::ttext text_;
00211 
00212     /** Start of the selected text. */
00213     size_t selection_start_;
00214 
00215     /**
00216      * Length of the selected text.
00217      *
00218      * * positive selection_len_ means selection to the right.
00219      * * negative selection_len_ means selection to the left.
00220      * * selection_len_ == 0 means no selection.
00221      */
00222     int selection_length_;
00223 
00224     /****** handling of special keys first the pure virtuals *****/
00225 
00226     /**
00227      * Every key can have several behaviours.
00228      *
00229      * Unmodified                 No modifier is pressed.
00230      * Control                    The control key is pressed.
00231      * Shift                      The shift key is pressed.
00232      * Alt                        The alt key is pressed.
00233      *
00234      * If modifiers together do something else as the sum of the modifiers
00235      * it's listed separately eg.
00236      *
00237      * Control                    Moves 10 steps at the time.
00238      * Shift                      Selects the text.
00239      * Control + Shift            Inserts 42 in the text.
00240      *
00241      * There are some predefined actions for results.
00242      * Unhandled                  The key/modifier is ignored and also reported
00243      *                            unhandled.
00244      * Ignored                    The key/modifier is ignored and it's _expected_
00245      *                            the inherited classes do the same.
00246      * Implementation defined     The key/modifier is ignored and it's expected
00247      *                            the inherited classes will define some meaning
00248      *                            to it.
00249      */
00250 
00251     /**
00252      * Up arrow key pressed.
00253      *
00254      * The behaviour is implementation defined.
00255      */
00256     virtual void handle_key_up_arrow(SDLMod modifier, bool& handled) = 0;
00257 
00258     /**
00259      * Down arrow key pressed.
00260      *
00261      * The behaviour is implementation defined.
00262      */
00263     virtual void handle_key_down_arrow(SDLMod modifier, bool& handled) = 0;
00264 
00265     /**
00266      * Clears the current line.
00267      *
00268      * Unmodified                 Clears the current line.
00269      * Control                    Ignored.
00270      * Shift                      Ignored.
00271      * Alt                        Ignored.
00272      */
00273     virtual void handle_key_clear_line(SDLMod modifier, bool& handled) = 0;
00274 
00275     /**
00276      * Left arrow key pressed.
00277      *
00278      * Unmodified                 Moves the cursor a character to the left.
00279      * Control                    Like unmodified but a word instead of a letter
00280      *                            at the time.
00281      * Shift                      Selects the text while moving.
00282      * Alt                        Ignored.
00283      */
00284     virtual void handle_key_left_arrow(SDLMod modifier, bool& handled);
00285 
00286     /**
00287      * Right arrow key pressed.
00288      *
00289      * Unmodified                 Moves the cursor a character to the right.
00290      * Control                    Like unmodified but a word instead of a letter
00291      *                            at the time.
00292      * Shift                      Selects the text while moving.
00293      * Alt                        Ignored.
00294      */
00295     virtual void handle_key_right_arrow(SDLMod modifier, bool& handled);
00296 
00297     /**
00298      * Home key pressed.
00299      *
00300      * Unmodified                 Moves the cursor a to the beginning of the
00301      *                            line.
00302      * Control                    Like unmodified but to the beginning of the
00303      *                            data.
00304      * Shift                      Selects the text while moving.
00305      * Alt                        Ignored.
00306      */
00307     virtual void handle_key_home(SDLMod modifier, bool& handled);
00308 
00309     /**
00310      * End key pressed.
00311      *
00312      * Unmodified                 Moves the cursor a to the end of the line.
00313      * Control                    Like unmodified but to the end of the data.
00314      * Shift                      Selects the text while moving.
00315      * Alt                        Ignored.
00316      */
00317     virtual void handle_key_end(SDLMod modifier, bool& handled);
00318 
00319     /**
00320      * Backspace key pressed.
00321      *
00322      * Unmodified                 Deletes the character before the cursor,
00323      *                            ignored if at the beginning of the data.
00324      * Control                    Ignored.
00325      * Shift                      Ignored.
00326      * Alt                        Ignored.
00327      */
00328     virtual void handle_key_backspace(SDLMod modifier, bool& handled);
00329 
00330     /**
00331      * Delete key pressed.
00332      *
00333      * Unmodified                 If there is a selection that's deleted.
00334      *                            Else if not at the end of the data the
00335      *                            character after the cursor is deleted.
00336      *                            Else the key is ignored.
00337      *                            ignored if at the beginning of the data.
00338      * Control                    Ignored.
00339      * Shift                      Ignored.
00340      * Alt                        Ignored.
00341      */
00342     virtual void handle_key_delete(SDLMod modifier, bool& handled);
00343 
00344     /**
00345      * Page up key.
00346      *
00347      * Unmodified                 Unhandled.
00348      * Control                    Ignored.
00349      * Shift                      Ignored.
00350      * Alt                        Ignored.
00351      */
00352     virtual void handle_key_page_up(SDLMod /*modifier*/, bool& /*handled*/) {}
00353 
00354     /**
00355      * Page down key.
00356      *
00357      * Unmodified                 Unhandled.
00358      * Control                    Ignored.
00359      * Shift                      Ignored.
00360      * Alt                        Ignored.
00361      */
00362     virtual void handle_key_page_down(SDLMod /*modifier*/, bool& /*handled*/) {}
00363 
00364 protected:
00365     /**
00366      * Default key handler if none of the above functions is called.
00367      *
00368      * Unmodified                 If invalid unicode it's ignored.
00369      *                            Else if text selected the selected text is
00370      *                            replaced with the unicode character send.
00371      *                            Else the unicode character is inserted after
00372      *                            the cursor.
00373      * Control                    Ignored.
00374      * Shift                      Ignored (already in the unicode value).
00375      * Alt                        Ignored.
00376      */
00377     virtual void handle_key_default(
00378         bool& handled, SDLKey key, SDLMod modifier, Uint16 unicode);
00379 
00380 private:
00381 
00382     /**
00383      * Text changed callback.
00384      *
00385      * This callback is called in key_press after the key_press event has been
00386      * handled by the control. The parameters to the function are:
00387      * - The widget invoking the callback
00388      * - The new text of the textbox.
00389      */
00390     boost::function< void (ttext_* textbox, const std::string text) > text_changed_callback_;
00391 
00392     /***** ***** ***** signal handlers ***** ****** *****/
00393 
00394     void signal_handler_middle_button_click(
00395             const event::tevent event, bool& handled);
00396 
00397     void signal_handler_sdl_key_down(const event::tevent event, bool& handled
00398             , const SDLKey key, SDLMod modifier, const Uint16 unicode);
00399 
00400     void signal_handler_receive_keyboard_focus(const event::tevent event);
00401     void signal_handler_lose_keyboard_focus(const event::tevent event);
00402 };
00403 
00404 } // namespace gui2
00405 
00406 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

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