WidgetTextInput.h

00001 /*
00002  * This source file is part of libRocket, the HTML/CSS Interface Middleware
00003  *
00004  * For the latest information, see http://www.librocket.com
00005  *
00006  * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
00007  *
00008  * Permission is hereby granted, free of charge, to any person obtaining a copy
00009  * of this software and associated documentation files (the "Software"), to deal
00010  * in the Software without restriction, including without limitation the rights
00011  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00012  * copies of the Software, and to permit persons to whom the Software is
00013  * furnished to do so, subject to the following conditions:
00014  *
00015  * The above copyright notice and this permission notice shall be included in
00016  * all copies or substantial portions of the Software.
00017  * 
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00019  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00020  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00021  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00022  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00023  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00024  * THE SOFTWARE.
00025  *
00026  */
00027 
00028 #ifndef ROCKETCONTROLSWIDGETTEXTINPUT_H
00029 #define ROCKETCONTROLSWIDGETTEXTINPUT_H
00030 
00031 #include <Rocket/Core/EventListener.h>
00032 #include <Rocket/Core/Geometry.h>
00033 #include <Rocket/Core/WString.h>
00034 #include <Rocket/Core/Vertex.h>
00035 
00036 namespace Rocket {
00037 namespace Core {
00038 
00039 class ElementText;
00040 
00041 }
00042 
00043 namespace Controls {
00044 
00045 class ElementFormControl;
00046 
00053 class WidgetTextInput : public Core::EventListener
00054 {
00055 public:
00056         WidgetTextInput(ElementFormControl* parent);
00057         virtual ~WidgetTextInput();
00058 
00061         virtual void SetValue(const Core::String& value);
00062 
00065         void SetMaxLength(int max_length);
00068         int GetMaxLength() const;
00069 
00071         void UpdateSelectionColours();
00072 
00074         void OnUpdate();
00076         void OnRender();
00078         void OnLayout();
00079 
00081         Core::ElementText* GetTextElement();
00083         const Rocket::Core::Vector2f& GetTextDimensions() const;
00084 
00085 protected:
00088         virtual void ProcessEvent(Core::Event& event);
00089 
00093         bool AddCharacter(Rocket::Core::word character);
00097         bool DeleteCharacter(bool back);
00101         virtual bool IsCharacterValid(Rocket::Core::word character) = 0;
00103         virtual void LineBreak() = 0;
00104 
00106         int GetCursorIndex() const;
00107 
00109         Core::Element* GetElement();
00110 
00112         void DispatchChangeEvent();
00113 
00114 private:
00118         void MoveCursorHorizontal(int distance, bool select);
00122         void MoveCursorVertical(int distance, bool select);
00123 
00125         void UpdateAbsoluteCursor();
00127         void UpdateRelativeCursor();
00128 
00132         int CalculateLineIndex(float position);
00137         int CalculateCharacterIndex(int line_index, float position);
00138 
00142         void ShowCursor(bool show, bool move_to_cursor = true);
00143 
00145         void FormatElement();
00148         Rocket::Core::Vector2f FormatText();
00149 
00151         void GenerateCursor();
00153         void UpdateCursorPosition();
00154 
00157         void UpdateSelection(bool selecting);
00159         void ClearSelection();
00161         void DeleteSelection();
00163         void CopySelection();
00164 
00171         void GetLineSelection(Core::WString& pre_selection, Core::WString& selection, Core::WString& post_selection, const Core::WString& line, int line_begin);
00172 
00173         struct Line
00174         {
00175                 // The contents of the line (including the trailing endline, if that terminated the line).
00176                 Core::WString content;
00177                 // The length of the editable characters on the line (excluding any trailing endline).
00178                 int content_length;
00179 
00180                 // The number of extra characters at the end of the content that are not present in the actual value; in the
00181                 // case of a soft return, this may be negative.
00182                 int extra_characters;
00183         };
00184 
00185         ElementFormControl* parent;
00186 
00187         Core::ElementText* text_element;
00188         Core::ElementText* selected_text_element;
00189         Rocket::Core::Vector2f internal_dimensions;
00190         Rocket::Core::Vector2f scroll_offset;
00191 
00192         typedef std::vector< Line > LineList;
00193         LineList lines;
00194 
00195         int max_length;
00196 
00197         int edit_index;
00198 
00199         int absolute_cursor_index;
00200         int cursor_line_index;
00201         int cursor_character_index;
00202 
00203         // Selection. The start and end indices of the selection are in absolute coordinates.
00204         Core::Element* selection_element;
00205         int selection_anchor_index;
00206         int selection_begin_index;
00207         int selection_length;
00208 
00209         // The colour of the background of selected text.
00210         Rocket::Core::Colourb selection_colour;
00211         // The selection background.
00212         Core::Geometry selection_geometry;
00213 
00214         // Cursor visibility and timings.
00215         float cursor_timer;
00216         bool cursor_visible;
00217 
00218         float last_update_time;
00219 
00220         // The cursor geometry.
00221         float ideal_cursor_position;
00222         Rocket::Core::Vector2f cursor_position;
00223         Rocket::Core::Vector2f cursor_size;
00224         Core::Geometry cursor_geometry;
00225 };
00226 
00227 }
00228 }
00229 
00230 #endif