text_box.hpp

Go to the documentation of this file.
00001 /* $Id: text_box.hpp 26662 2008-05-17 07:18:31Z 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_TEXT_BOX_HPP_INCLUDED__
00016 #define __GUI_WIDGETS_TEXT_BOX_HPP_INCLUDED__
00017 
00018 #include "gui/widgets/text.hpp"
00019 
00020 
00021 namespace gui2 {
00022 
00023 //! Class for text input history
00024 class ttext_history
00025 {
00026 public:
00027     //! Gets history that matches id and enables or disables it.
00028     static ttext_history get_history(const std::string& id, const bool enabled);
00029 
00030     // Defaults to a disabled history
00031     ttext_history() : 
00032         history_(0), 
00033         pos_(0), 
00034         enabled_(false) 
00035     {
00036     }
00037     
00038     //! Push string into the history if it is non-empty and is not the same as the last item
00039     //! updates position to end of history.
00040     void push(const std::string& text);
00041     
00042     //! One step up/down in history. Pushes text to the history if at the end.
00043     std::string up(const std::string& text = "");
00044     std::string down(const std::string& text = "");
00045     
00046     //! Returns the value at the current history position. Returns "" if not enabled
00047     //! or if at the end of the history.
00048     std::string get_value() const;
00049     
00050     void set_enabled(bool enabled = true) { enabled_ = enabled; }
00051     bool get_enabled() const { return enabled_; }
00052     
00053 private:
00054     ttext_history(std::vector<std::string>* history, const bool enabled) : 
00055         history_(history), 
00056         pos_(history->size()), 
00057         enabled_(enabled) 
00058     {}
00059     
00060     std::vector<std::string>* history_;
00061     unsigned pos_;
00062     
00063     bool enabled_;
00064 };
00065 
00066 //! Class for a single line text area.
00067 class ttext_box : public ttext_
00068 {
00069 
00070 public:
00071 
00072     ttext_box() :
00073         ttext_(),
00074         character_offset_(),
00075         history_(),
00076         text_x_offset_(0),
00077         text_y_offset_(0),
00078         text_height_(0),
00079         dragging_(false)
00080     { 
00081         set_wants_mouse_left_double_click(); 
00082     }
00083 
00084     void set_history(const std::string& id) 
00085         { history_ = ttext_history::get_history(id, true); }
00086     
00087     void save_to_history() { history_.push(text()); }
00088     void save_to_history(const std::string& text) { history_.push(text); }
00089 
00090 protected:
00091     
00092     // Gets the character position at the wanted place
00093     // as if the mouse would click there.
00094 //  virtual unsigned get_position_at(tpoint& coordinate);
00095 
00096 //  void delete_selection();
00097 //  void copy();
00098 //  void paste();
00099 //  
00100     //! Inherited from ttext_.
00101     void insert_char(Uint16 unicode);
00102 
00103     //! Inherited from ttext_.
00104     void delete_char(const bool before_cursor);
00105 
00106     //! Inherited from ttext_.
00107     void delete_selection();
00108 
00109     //! Inherited from tcontrol.
00110     void set_canvas_text();
00111 
00112     //! Inherited from tcontrol..
00113     void set_size(const SDL_Rect& rect);
00114 
00115     void goto_end_of_line(const bool select = false) { goto_end_of_data(select); }
00116     void goto_start_of_line(const bool select = false) { goto_start_of_data(select); }
00117 
00118     //! Handles the selection in a mouse down or mouse move event.
00119     void handle_mouse_selection(tevent_handler& event, const bool start_selection);
00120 
00121     //! Inherited from twidget.
00122     void mouse_left_button_down(tevent_handler& event);
00123 
00124     //! Inherited from twidget.
00125     void mouse_move(tevent_handler& event);
00126 
00127     //! Inherited from twidget.
00128     void mouse_left_button_up(tevent_handler&);
00129 
00130     //! Inherited from twidget.
00131     void mouse_left_button_double_click(tevent_handler&);
00132 
00133 private:
00134 
00135     void handle_key_up_arrow(SDLMod modifier, bool& handled);
00136     void handle_key_down_arrow(SDLMod modifier, bool& handled);
00137 
00138     // Clears the current line
00139     void handle_key_clear_line(SDLMod modifier, bool& handled);
00140 
00141     //! Contains the end offset of each character in the text area.
00142     std::vector<unsigned> character_offset_;
00143 
00144     //! Inherited from ttext_.
00145     void calculate_char_offset();
00146 
00147     //! Gets the character at the wanted offset, everything beyond will
00148     //! select the last character.
00149     unsigned get_character_offset_at(const unsigned offset);
00150 
00151     ttext_history history_;
00152 
00153     //! Inherited from tcontrol.
00154     const std::string& get_control_type() const 
00155         { static const std::string type = "text_box"; return type; }
00156 
00157     //! Inherited from tcontrol.
00158     void load_config_extra();
00159     
00160     unsigned text_x_offset_;
00161     unsigned text_y_offset_;
00162     unsigned text_height_;
00163 
00164     // Updates text_x_offset_ and text_x_offset_.
00165     void update_offsets();
00166 
00167     bool dragging_;
00168 };
00169 
00170 
00171 
00172 } //namespace gui2
00173 
00174 #endif
00175 

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