gui/widgets/text_box.hpp

Go to the documentation of this file.
00001 /* $Id: text_box.hpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
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_BOX_HPP_INCLUDED
00017 #define GUI_WIDGETS_TEXT_BOX_HPP_INCLUDED
00018 
00019 #include "gui/widgets/text.hpp"
00020 
00021 namespace gui2 {
00022 
00023 /**
00024  * Class for text input history.
00025  *
00026  * The history of text items can be stored in the preferences. This class
00027  * handles that. Every item needs an id by which the history is loaded and
00028  * saved.
00029  */
00030 class ttext_history
00031 {
00032 public:
00033     /**
00034      * Gets history that matches id.
00035      *
00036      * @param id                  The id of the history to look for.
00037      * @param enabled             The enabled state of the history.
00038      *
00039      * @returns                   The history object.
00040      */
00041     static ttext_history get_history(const std::string& id, const bool enabled);
00042 
00043     ttext_history() :
00044         history_(0),
00045         pos_(0),
00046         enabled_(false)
00047     {
00048     }
00049 
00050     /**
00051      * Push string into the history.
00052      *
00053      * If the string is empty or the same as the last item in the history this
00054      * function is a nop.
00055      *
00056      * @param text                   The text to push in the history.
00057      */
00058     void push(const std::string& text);
00059 
00060     /**
00061      * One step up in the history.
00062      *
00063      * Pushes text to the history if at the end.
00064      *
00065      * @param text                The text to push in the history.
00066      *
00067      * @returns                   The current value of the history.
00068      */
00069     std::string up(const std::string& text = "");
00070 
00071     /**
00072      * One step down in the history.
00073      *
00074      * Pushes text to the history if at the end.
00075      *
00076      * @param text                The text to push in the history.
00077      *
00078      * @returns                   The current value of the history.
00079      */
00080     std::string down(const std::string& text = "");
00081 
00082     /**
00083      * Gets the current history value.
00084      *
00085      * @returns                   If enabled return the current history
00086      *                            position, otherwise an empty string is
00087      *                            returned.
00088      */
00089     std::string get_value() const;
00090 
00091     /***** ***** ***** setters / getters for members ***** ****** *****/
00092 
00093     void set_enabled(bool enabled = true) { enabled_ = enabled; }
00094     bool get_enabled() const { return enabled_; }
00095 
00096 private:
00097     ttext_history(std::vector<std::string>* history, const bool enabled) :
00098         history_(history),
00099         pos_(history->size()),
00100         enabled_(enabled)
00101     {}
00102 
00103     /** The items in the history. */
00104     std::vector<std::string>* history_;
00105 
00106     /** The current position in the history. */
00107     unsigned pos_;
00108 
00109     /** Is the history enabled. */
00110     bool enabled_;
00111 };
00112 
00113 /** Class for a single line text area. */
00114 class ttext_box : public ttext_
00115 {
00116 public:
00117     ttext_box();
00118 
00119     /** Saves the text in the widget to the history. */
00120     void save_to_history() { history_.push(get_value()); }
00121 
00122     /***** ***** ***** setters / getters for members ***** ****** *****/
00123 
00124     void set_history(const std::string& id)
00125         { history_ = ttext_history::get_history(id, true); }
00126 
00127 protected:
00128 
00129     /***** ***** ***** ***** layout functions ***** ***** ***** *****/
00130 
00131     /** Inherited from tcontrol. */
00132     void place(const tpoint& origin, const tpoint& size);
00133 
00134     /***** ***** ***** ***** Inherited ***** ***** ***** *****/
00135 
00136     /** Inherited from tcontrol. */
00137     void update_canvas();
00138 
00139     /** Inherited from ttext_. */
00140     void goto_end_of_line(const bool select = false)
00141         { goto_end_of_data(select); }
00142 
00143     /** Inherited from ttext_. */
00144     void goto_start_of_line(const bool select = false)
00145         { goto_start_of_data(select); }
00146 
00147     /** Inherited from ttext_. */
00148     void delete_char(const bool before_cursor);
00149 
00150     /** Inherited from ttext_. */
00151     void delete_selection();
00152 
00153     void handle_mouse_selection(tpoint mouse, const bool start_selection);
00154 private:
00155 
00156     /** The history text for this widget. */
00157     ttext_history history_;
00158 
00159     /**
00160      * The x offset in the widget where the text starts.
00161      *
00162      * This value is needed to translate a location in the widget to a location
00163      * in the text.
00164      */
00165     unsigned text_x_offset_;
00166 
00167     /**
00168      * The y offset in the widget where the text starts.
00169      *
00170      * Needed to determine whether a click is on the text.
00171      */
00172     unsigned text_y_offset_;
00173 
00174     /**
00175      * The height of the text itself.
00176      *
00177      * Needed to determine whether a click is on the text.
00178      */
00179     unsigned text_height_;
00180 
00181     /** Updates text_x_offset_ and text_y_offset_. */
00182     void update_offsets();
00183 
00184     /** Is the mouse in dragging mode, this affects selection in mouse move */
00185     bool dragging_;
00186 
00187     /**
00188      * Inherited from ttext_.
00189      *
00190      * Unmodified                 Unhandled.
00191      * Control                    Ignored.
00192      * Shift                      Ignored.
00193      * Alt                        Ignored.
00194      */
00195     void handle_key_up_arrow(SDLMod /*modifier*/, bool& /*handled*/) {}
00196 
00197     /**
00198      * Inherited from ttext_.
00199      *
00200      * Unmodified                 Unhandled.
00201      * Control                    Ignored.
00202      * Shift                      Ignored.
00203      * Alt                        Ignored.
00204      */
00205     void handle_key_down_arrow(SDLMod /*modifier*/, bool& /*handled*/) {}
00206 
00207     /**
00208      * Goes one item up in the history.
00209      *
00210      * @returns                   True if there's a history, false otherwise.
00211      */
00212     bool history_up();
00213 
00214     /**
00215      * Goes one item down in the history.
00216      *
00217      * @returns                   True if there's a history, false otherwise.
00218      */
00219     bool history_down();
00220 
00221     /** Inherited from ttext_. */
00222     void handle_key_default(
00223         bool& handled, SDLKey key, SDLMod modifier, Uint16 unicode);
00224 
00225     /** Inherited from ttext_. */
00226     void handle_key_clear_line(SDLMod modifier, bool& handled);
00227 
00228     /** Inherited from tcontrol. */
00229     const std::string& get_control_type() const;
00230 
00231     /** Inherited from tcontrol. */
00232     void load_config_extra();
00233 
00234     /***** ***** ***** signal handlers ***** ****** *****/
00235 
00236     void signal_handler_mouse_motion(
00237             const event::tevent event, bool& handled, const tpoint& coordinate);
00238 
00239     void signal_handler_left_button_down(
00240             const event::tevent event, bool& handled);
00241 
00242     void signal_handler_left_button_up(
00243             const event::tevent event, bool& handled);
00244 
00245     void signal_handler_left_button_double_click(
00246             const event::tevent event, bool& handled);
00247 };
00248 
00249 } //namespace gui2
00250 
00251 #endif
00252 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

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