nux-1.14.0
|
00001 #ifndef TEXTENTRY_H 00002 #define TEXTENTRY_H 00003 00004 00005 00006 // Heavily inspired from google gadget code 00007 /* 00008 Copyright 2008 Google Inc. 00009 00010 Licensed under the Apache License, Version 2.0 (the "License"); 00011 you may not use this file except in compliance with the License. 00012 You may obtain a copy of the License at 00013 00014 http://www.apache.org/licenses/LICENSE-2.0 00015 00016 Unless required by applicable law or agreed to in writing, software 00017 distributed under the License is distributed on an "AS IS" BASIS, 00018 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00019 See the License for the specific language governing permissions and 00020 limitations under the License. 00021 */ 00022 00023 00024 #include "cairo/cairo.h" 00025 #include "pango/pango.h" 00026 #include "pango/pangocairo.h" 00027 #include "NuxImage/CairoGraphics.h" 00028 00029 namespace nux 00030 { 00031 class CairoGraphics; 00032 00033 class CairoFont 00034 { 00035 public: 00036 enum Style { 00037 STYLE_NORMAL, 00038 STYLE_ITALIC 00039 }; 00040 00044 enum Weight { 00045 WEIGHT_NORMAL, 00046 WEIGHT_BOLD 00047 }; 00048 00053 CairoFont(const std::string &family, 00054 /*PangoFontDescription *font,*/ 00055 double size, 00056 Style style, 00057 Weight weight); 00058 00059 virtual ~CairoFont(); 00060 00061 virtual Style GetStyle() const {return style_;} 00062 virtual Weight GetWeight() const {return weight_;} 00063 virtual double GetPointSize() const {return size_;} 00064 00065 virtual void Destroy() {delete this;} 00066 00067 const PangoFontDescription *GetFontDescription() const {return font_;} 00068 00069 private: 00070 PangoFontDescription *font_; 00071 double size_; 00072 Style style_; 00073 Weight weight_; 00074 }; 00075 00076 class TextEntry: public View 00077 { 00078 NUX_DECLARE_OBJECT_TYPE (TextEntry, View); 00079 public: 00080 TextEntry (const TCHAR* text, NUX_FILE_LINE_PROTO); 00081 ~TextEntry (); 00082 00083 Area* FindAreaUnderMouse(const Point& mouse_position, NuxEventType event_type); 00084 virtual long ProcessEvent (IEvent &ievent, long TraverseInfo, long ProcessEventInfo); 00085 virtual void Draw (GraphicsEngine &GfxContext, bool force_draw); 00086 virtual void DrawContent (GraphicsEngine &GfxContext, bool force_draw); 00087 virtual void PostDraw (GraphicsEngine &GfxContext, bool force_draw); 00088 00089 void PreLayoutManagement (); 00090 long PostLayoutManagement (long layoutResult); 00091 00092 // Receivers 00093 00094 void RecvMouseDoubleClick (int x, int y, unsigned long button_flags, unsigned long key_flags); 00095 void RecvMouseUp (int x, int y, unsigned long button_flags, unsigned long key_flags); 00096 void RecvMouseDown (int x, int y, unsigned long button_flags, unsigned long key_flags); 00097 void RecvMouseDrag (int x, int y, int dx, int dy, unsigned long button_flags, unsigned long key_flags); 00098 void RecvKeyEvent ( 00099 unsigned long eventType , /*event type*/ 00100 unsigned long keysym , /*event keysym*/ 00101 unsigned long state , /*event state*/ 00102 const TCHAR* character , /*character*/ 00103 unsigned short keyCount /*key repeat count*/); 00104 00105 void RecvStartKeyFocus(); 00106 void RecvEndKeyFocus(); 00107 00108 bool _size_match_text; 00109 BaseTexture *_texture2D; 00110 00111 void MainDraw (); 00112 void ProcessMouseEvent (int event_type, int x, int y, int dx, int dy, unsigned long button_flags, unsigned long key_flags); 00113 void ProcessKeyEvent ( 00114 unsigned long eventType , /*event type*/ 00115 unsigned long keysym , /*event keysym*/ 00116 unsigned long state , /*event state*/ 00117 const TCHAR* character , /*character*/ 00118 unsigned short keyCount /*key repeat count*/); 00119 00120 void FocusInx (); 00121 void FocusOutx (); 00122 00124 00127 sigc::signal <void, TextEntry*> sigTextChanged; 00128 sigc::signal <void> activated; 00129 sigc::signal <void, int> cursor_moved; 00130 00131 void SetText(const char *text); 00132 std::string const& GetText() const; 00133 00134 void SetTextColor(const Color &color); 00135 Color const& GetTextColor() const; 00136 void SetFontFamily (const char *font); 00137 void SetFontSize (double font_size); 00138 void SetFontOptions (const cairo_font_options_t *options); 00139 00141 void Select(int start, int end); 00143 void SelectAll(); 00144 00145 CairoGraphics::Alignment GetAlign() const; 00146 void SetAlign(CairoGraphics::Alignment align); 00147 00148 protected: 00149 virtual void DoSetFocused (bool focused); 00150 bool _block_focus; // used to selectively ignore focus keyevents 00151 00152 virtual void GeometryChanged (); 00153 00157 enum MovementStep { 00158 VISUALLY, 00159 WORDS, 00160 DISPLAY_LINES, 00161 DISPLAY_LINE_ENDS, 00162 PAGES, 00163 BUFFER 00164 }; 00165 00166 void QueueTextDraw (); 00168 void ResetLayout(); 00173 PangoLayout* EnsureLayout(); 00175 PangoLayout* CreateLayout(); 00177 CairoGraphics* EnsureCanvas(); 00179 void AdjustScroll(); 00185 void QueueRefresh(bool relayout, bool adjust_scroll); 00187 void ResetImContext(); 00189 void ResetPreedit(); 00191 void QueueCursorBlink(); 00192 static bool CursorBlinkCallback(TextEntry *data); 00193 00194 void ShowCursor(); 00195 void HideCursor(); 00196 00198 void DrawCursor(CairoGraphics *canvas); 00200 void DrawText(CairoGraphics *canvas); 00201 00202 void GetCursorRects(Rect *strong, Rect *weak); 00203 00204 void UpdateCursorRegion(); 00205 00206 void UpdateSelectionRegion(); 00207 00209 void MoveCursor(MovementStep step, int count, bool extend_selection); 00211 int MoveVisually(int current_pos, int count); 00213 int MoveWords(int current_pos, int count); 00215 int MoveDisplayLines(int current_pos, int count); 00217 int MovePages(int current_pos, int count); 00219 int MoveLineEnds(int current_pos, int count); 00220 00222 void SetCursor(int cursor); 00225 int XYToTextIndex(int x, int y); 00227 bool GetSelectionBounds(int *start, int *end); 00229 void SetSelectionBounds(int selection_bound, int cursor); 00230 00232 int TextIndexToLayoutIndex(int text_index, bool consider_preedit_cursor); 00233 00235 int LayoutIndexToTextIndex(int layout_index); 00236 00238 int GetCharLength(int index); 00239 00241 int GetPrevCharLength(int index); 00242 00244 void EnterText(const char *str); 00246 void DeleteText(int start, int end); 00247 00249 void SelectWord(); 00251 void SelectLine(); 00253 void DeleteSelection(); 00254 00256 void CutClipboard(); 00258 void CopyClipboard(); 00260 void PasteClipboard(); 00262 void BackSpace(MovementStep step); 00264 void Delete(MovementStep step); 00266 void ToggleOverwrite(); 00267 00269 Color GetSelectionBackgroundColor(); 00271 Color GetSelectionTextColor(); 00272 00276 void GetCursorLocationInLayout(int *strong_x, int *strong_y, int *strong_height, 00277 int *weak_x, int *weak_y, int *weak_height); 00278 00280 CairoGraphics* canvas_; 00281 00283 PangoLayout* cached_layout_; 00284 00286 std::string _text; 00288 std::string _preedit; 00290 PangoAttrList *preedit_attrs_; 00295 std::string password_char_; 00296 00298 t_u64 last_dblclick_time_; 00299 00301 int cursor_; 00306 int preedit_cursor_; 00311 int selection_bound_; 00312 00314 int scroll_offset_x_; 00316 int scroll_offset_y_; 00318 int cursor_blink_timer_; 00326 int cursor_blink_status_; 00327 00329 bool visible_; 00331 bool focused_; 00333 bool need_im_reset_; 00335 bool overwrite_; 00337 bool select_words_; 00339 bool select_lines_; 00341 bool button_; 00343 bool bold_; 00345 bool underline_; 00347 bool strikeout_; 00349 bool italic_; 00351 bool multiline_; 00353 bool wrap_; 00355 bool cursor_visible_; 00357 bool readonly_; 00362 bool content_modified_; 00363 00365 bool selection_changed_; 00366 00368 bool cursor_moved_; 00369 00371 bool update_canvas_; 00372 00374 std::string font_family_; 00376 double font_size_; 00377 00378 cairo_font_options_t *font_options_; 00379 double font_dpi_; 00380 00382 Color _text_color; 00383 00384 CairoGraphics::Alignment align_; 00385 00386 std::list<Rect> last_selection_region_; 00387 std::list<Rect> selection_region_; 00388 std::list<Rect> last_cursor_region_; 00389 std::list<Rect> cursor_region_; 00390 00391 protected: 00392 bool text_input_mode_; 00393 bool key_nav_mode_; 00394 00395 virtual bool InspectKeyEvent(unsigned int eventType, 00396 unsigned int keysym, 00397 const char* character); 00398 }; 00399 } 00400 00401 #endif // TEXTENTRY_H