text.hpp

Go to the documentation of this file.
00001 /* $Id: text.hpp 26270 2008-05-01 06:44:21Z 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_HPP_INCLUDED__
00016 #define __GUI_WIDGETS_TEXT_HPP_INCLUDED__
00017 
00018 #include "gui/widgets/control.hpp"
00019 
00020 #include <string>
00021 
00022 namespace gui2 {
00023 
00024 //! Base class for text items will get two base decendends
00025 //! - ttext_box a single line text
00026 //! - ttext_area a multi line text
00027 class ttext_ : public tcontrol
00028 {
00029 
00030 public:
00031     ttext_() :
00032         tcontrol(COUNT),
00033         state_(ENABLED),
00034         text_(),
00035         sel_start_(0),
00036         sel_len_(0),
00037         max_length_(std::string::npos),
00038         dragging_(false)
00039     {
00040     }
00041 
00042     void set_active(const bool /*active*/) { /*FIXME IMPLEMENT*/ };
00043     bool get_active() const { return true; /* FIXME IMPLEMENT */ }
00044     unsigned get_state() const { return state_; }
00045 
00046     void mouse_move(tevent_handler&);
00047 
00048     void mouse_left_button_down(tevent_handler& event);
00049     void mouse_left_button_up(tevent_handler&);
00050     void mouse_left_button_double_click(tevent_handler&);
00051 
00052     void mouse_middle_button_click(tevent_handler&);
00053 
00054     void key_press(tevent_handler& event, bool& handled, SDLKey key, SDLMod modifier, Uint16 unicode);
00055 
00056 
00057     void set_text(const std::string& text); 
00058     std::string get_text() const { return text_; }
00059     const std::string& text() const { return text_; }
00060 
00061 protected:
00062 
00063     virtual void goto_end_of_line(const bool select = false) = 0;
00064     void goto_end_of_data(const bool select = false) { set_cursor(text_.size(), select); }
00065 
00066     virtual void goto_start_of_line(const bool select = false) = 0;
00067     void goto_start_of_data(const bool select = false) { set_cursor(0, select); }
00068 
00069     void select_all() { sel_start_ = 0; goto_end_of_data(true); }
00070 
00071     void set_cursor(const size_t offset, const bool select); // call set dirty
00072 
00073     size_t get_sel_start() const { return sel_start_; }
00074     void  set_sel_start(const size_t sel_start) { sel_start_ = sel_start; set_dirty(); }
00075 
00076     size_t get_sel_len() const { return sel_len_; }
00077     void set_sel_len(const unsigned sel_len) { sel_len_ = sel_len; set_dirty(); }
00078 
00079     //! Inserts a character at the cursor.
00080     virtual void insert_char(Uint16 unicode) = 0;
00081 
00082     //! Deletes the character.
00083     virtual void delete_char(const bool before_cursor) = 0;
00084 
00085     //! Deletes the current selection.
00086     virtual void delete_selection() = 0;
00087 
00088     //! Copies the current selection.
00089     virtual void copy_selection(const bool mouse);
00090 
00091     //! Pastes the current selection.
00092     virtual void paste_selection(const bool mouse);
00093 
00094 protected:
00095 
00096     std::string& text() { return text_; }
00097 
00098     size_t& sel_start() { return sel_start_; }
00099 
00100     int& sel_len() { return sel_len_; }
00101 
00102 private:
00103     //! Note the order of the states must be the same as defined in settings.hpp.
00104     enum tstate { ENABLED, DISABLED, FOCUSSED, COUNT };
00105 
00106     void set_state(tstate state);
00107     tstate state_;
00108 
00109 
00110     //! The text in the widget.
00111     std::string text_;
00112 
00113     //! Calculates the offsets of all chars.
00114     virtual void calculate_char_offset() = 0;
00115 
00116     size_t sel_start_;
00117     //! positive sel_len_ means selection to the right.
00118     //! negative sel_len_ means selection to the left.
00119     //! sel_len_ == 0 means no selection.
00120     int sel_len_;
00121     size_t max_length_;
00122 
00123     //! Is the mouse in dragging mode, this affects selection in mouse movee
00124     bool dragging_;
00125 
00126     // handling of special keys first the pure virtuals
00127     virtual void handle_key_up_arrow(SDLMod modifier, bool& handled) = 0;
00128     virtual void handle_key_down_arrow(SDLMod modifier, bool& handled) = 0;
00129 
00130     // Clears the current line
00131     virtual void handle_key_clear_line(SDLMod modifier, bool& handled) = 0;
00132 
00133     // Go a character left of not at start of buffer else beep.
00134     // ctrl moves a word instead of character.
00135     // shift selects while moving.
00136     virtual void handle_key_left_arrow(SDLMod modifier, bool& handled);
00137 
00138     // Go a character right of not at end of buffer else beep.
00139     // ctrl moves a word instead of character.
00140     // shift selects while moving.
00141     virtual void handle_key_right_arrow(SDLMod modifier, bool& handled);
00142 
00143     // Go to the beginning of the line.
00144     // ctrl moves the start of data (except when ctrl-e but caller does that) 
00145     // shift selects while moving.
00146     virtual void handle_key_home(SDLMod modifier, bool& handled);
00147 
00148     // Go to the end of the line.
00149     // ctrl moves the end of data (except when ctrl-a but caller does that) 
00150     // shift selects while moving.
00151     virtual void handle_key_end(SDLMod modifier, bool& handled);
00152 
00153     // Deletes the character in front of the cursor (if not at the beginning).
00154     virtual void handle_key_backspace(SDLMod modifier, bool& handled);
00155 
00156     // Deletes either the selection or the character beyond the cursor
00157     virtual void handle_key_delete(SDLMod modifier, bool& handled);
00158 
00159     // Default handler, inserts a unicode char if valid
00160     virtual void handle_key_default(bool& handled, SDLKey key, SDLMod modifier, Uint16 unicode);
00161 
00162     // These are ignored by a single line edit box which is the default behaviour.
00163     virtual void handle_key_page_up(SDLMod /*modifier*/, bool& /*handled*/) {}
00164     virtual void handle_key_page_down(SDLMod /*modifier*/, bool& /*handled*/) {}
00165 };
00166 
00167 } // namespace gui2
00168 
00169 #endif

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