Back to index

nux  3.0.0
TextEntry.h
Go to the documentation of this file.
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 "NuxGraphics/CairoGraphics.h"
00028 
00029 namespace nux
00030 {
00031   class CairoGraphics;
00032   class IBusIMEContext;
00033 
00034   class CairoFont
00035   {
00036   public:
00037     enum Style {
00038       STYLE_NORMAL,
00039       STYLE_ITALIC
00040     };
00041 
00045     enum Weight {
00046       WEIGHT_NORMAL,
00047       WEIGHT_BOLD
00048     };
00049 
00054     CairoFont(const std::string &family,
00055       /*PangoFontDescription *font,*/
00056       double size,
00057       Style style,
00058       Weight weight);
00059 
00060     virtual ~CairoFont();
00061 
00062     virtual Style GetStyle() const {return style_;}
00063     virtual Weight GetWeight() const {return weight_;}
00064     virtual double GetPointSize() const {return size_;}
00065 
00066     virtual void Destroy() {delete this;}
00067 
00068     const PangoFontDescription *GetFontDescription() const {return font_;}
00069 
00070   private:
00071     PangoFontDescription *font_;
00072     double size_;
00073     Style style_;
00074     Weight weight_;
00075   };
00076 
00077   class TextEntry: public View
00078   {
00079     NUX_DECLARE_OBJECT_TYPE(TextEntry, View);
00080   public:
00081     TextEntry(const char* text, NUX_FILE_LINE_PROTO);
00082     ~TextEntry();
00083 
00084     Area* FindAreaUnderMouse(const Point& mouse_position, NuxEventType event_type);
00085     virtual void Draw(GraphicsEngine& graphics_engine, bool force_draw);
00086     virtual void DrawContent(GraphicsEngine& graphics_engine, bool force_draw);
00087     virtual void PostDraw(GraphicsEngine& graphics_engine, 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 RecvMouseEnter(int x, int y, unsigned long button_flags, unsigned long key_flags);
00099     void RecvMouseLeave(int x, int y, unsigned long button_flags, unsigned long key_flags);
00100     void RecvKeyEvent(
00101       unsigned long    eventType  ,   /*event type*/
00102       unsigned long    keysym     ,   /*event keysym*/
00103       unsigned long    state      ,   /*event state*/
00104       const char*     character  ,   /*character*/
00105       unsigned short   keyCount       /*key repeat count*/);
00106 
00107     void RecvStartKeyFocus();
00108     void RecvEndKeyFocus();
00109 
00110     bool _size_match_text;
00111     BaseTexture *_texture2D;
00112 
00113     void MainDraw();
00114     void ProcessMouseEvent(int event_type, int x, int y, int dx, int dy, unsigned long button_flags, unsigned long key_flags);
00115     void ProcessKeyEvent   (
00116       unsigned long    eventType  ,   /*event type*/
00117       unsigned long    keysym     ,   /*event keysym*/
00118       unsigned long    state      ,   /*event state*/
00119       const char*     character  ,   /*character*/
00120       unsigned short   keyCount       /*key repeat count*/);
00121 
00122     void FocusInx();
00123     void FocusOutx();
00124 
00126 
00129     sigc::signal <void, TextEntry*> text_changed;
00130     sigc::signal <void> activated;
00131     sigc::signal <void, int> cursor_moved;
00132 
00133     void SetText(const char* text);
00134     std::string const& GetText() const;
00135     std::string GetTextSelection() const;
00136 
00137     void SetCompletion(const char* text); // Should use std::string, does not for consistancy
00138     std::string const& GetCompletion() const;
00139 
00140     void SetCompletionColor(const Color& color);
00141     Color const& GetCompletionColor() const;
00142 
00143     void SetTextColor(const Color& color);
00144     Color const& GetTextColor() const;
00145     void SetFontFamily(const char* font);
00146     void SetFontSize(double font_size);
00147     void SetFontOptions(const cairo_font_options_t* options);
00148 
00150     void Select(int start, int end);
00152     void SelectAll();
00153 
00154     CairoGraphics::Alignment GetAlign() const;
00155     void SetAlign(CairoGraphics::Alignment align);
00156 
00157     bool im_active();
00158     bool im_running();
00159 
00160     void MoveCursorToLineStart();
00161     void MoveCursorToLineEnd();
00162 
00163 
00173     void SetLoseKeyFocusOnKeyNavDirectionUp(bool b);
00174 
00175     bool GetLoseKeyFocusOnKeyNavDirectionUp() const;
00176 
00186     void SetLoseKeyFocusOnKeyNavDirectionDown(bool b);
00187 
00188     bool GetLoseKeyFocusOnKeyNavDirectionDown() const;
00189     
00199     bool IsInTextInputMode() const;
00200 
00204     void EnterText(const char* str);
00205     
00209     void DeleteText(int start, int end);
00210 
00211   protected:
00212     bool _block_focus; // used to selectively ignore focus keyevents
00213 
00214     virtual void GeometryChanged();
00215 
00219     enum MovementStep {
00220       VISUALLY,
00221       WORDS,
00222       DISPLAY_LINES,
00223       DISPLAY_LINE_ENDS,
00224       PAGES,
00225       BUFFER
00226     };
00227 
00231     enum SearchState {
00232       NO_MATCH,
00233       PARTIAL,
00234       MATCH
00235     };
00236 
00238     bool HandledDeadKeys(int keysym, int state, const char* character);
00239 
00241     bool HandledComposition(int keysym, const char* character);
00242     
00243     void QueueTextDraw();
00245     void ResetLayout();
00250     PangoLayout* EnsureLayout();
00252     PangoLayout* CreateLayout();
00254     CairoGraphics* EnsureCanvas();
00256     void AdjustScroll();
00262     void QueueRefresh(bool relayout, bool adjust_scroll);
00264     void ResetImContext();
00266     void ResetPreedit();
00268     void QueueCursorBlink();
00269     static bool CursorBlinkCallback(TextEntry* data);
00270 
00271     void ShowCursor();
00272     void HideCursor();
00273 
00275     void DrawCursor(CairoGraphics* canvas);
00277     void DrawText(CairoGraphics* canvas);
00278 
00279     void GetCursorRects(Rect* strong, Rect* weak);
00280 
00281     void UpdateCursorRegion();
00282 
00283     void UpdateSelectionRegion();
00284 
00286     void MoveCursor(MovementStep step, int count, bool extend_selection);
00288     int MoveVisually(int current_pos, int count);
00290     int MoveWords(int current_pos, int count);
00292     int MoveDisplayLines(int current_pos, int count);
00294     int MovePages(int current_pos, int count);
00296     int MoveLineEnds(int current_pos, int count);
00297 
00299     void SetCursor(int cursor);
00302     int XYToTextIndex(int x, int y);
00304     bool GetSelectionBounds(int* start, int* end) const;
00306     void SetSelectionBounds(int selection_bound, int cursor);
00307 
00309     int TextIndexToLayoutIndex(int text_index, bool consider_preedit_cursor);
00310 
00312     int LayoutIndexToTextIndex(int layout_index);
00313 
00315     int GetCharLength(int index);
00316 
00318     int GetPrevCharLength(int index);
00319 
00321     void SelectWord();
00323     void SelectLine();
00325     void DeleteSelection();
00326 
00328     virtual void CutClipboard();
00330     virtual void CopyClipboard();
00332     virtual void PasteClipboard();
00333 #if defined(NUX_OS_LINUX)
00334 
00335     virtual void PastePrimaryClipboard();
00336 #endif
00337 
00338     void BackSpace(MovementStep step);
00340     void Delete(MovementStep step);
00342     void ToggleOverwrite();
00343 
00345     Color GetSelectionBackgroundColor();
00347     Color GetSelectionTextColor();
00348 
00352     void GetCursorLocationInLayout(int* strong_x, int* strong_y, int* strong_height,
00353                                    int* weak_x, int* weak_y, int* weak_height);
00354 
00355     int LookForMatch(std::string& str);
00356 
00358     CairoGraphics* canvas_;
00359 
00361     PangoLayout* cached_layout_;
00362 
00364     std::string text_;
00366     std::string preedit_;
00368     PangoAttrList* preedit_attrs_;
00373     std::string password_char_;
00374 
00376     std::string completion_;
00377 
00379     Color completion_color_;
00380 
00382     unsigned long long last_dblclick_time_;
00383 
00385     int cursor_;
00390     int preedit_cursor_;
00395     int selection_bound_;
00396 
00398     int scroll_offset_x_;
00400     int scroll_offset_y_;
00402     int cursor_blink_timer_;
00410     int cursor_blink_status_;
00411 
00413     bool visible_;
00415     bool focused_;
00417     bool need_im_reset_;
00419     bool overwrite_;
00421     bool select_words_;
00423     bool select_lines_;
00425     bool button_;
00427     bool bold_;
00429     bool underline_;
00431     bool strikeout_;
00433     bool italic_;
00435     bool multiline_;
00437     bool wrap_;
00439     bool cursor_visible_;
00441     bool readonly_;
00446     bool content_modified_;
00447 
00449     bool selection_changed_;
00450 
00452     bool cursor_moved_;
00453 
00455     bool update_canvas_;
00456 
00458     std::string font_family_;
00460     double font_size_;
00461 
00462     cairo_font_options_t* font_options_;
00463     double font_dpi_;
00464 
00466     Color _text_color;
00467 
00468     CairoGraphics::Alignment align_;
00469     
00470 #if defined(NUX_OS_LINUX)
00471     Cursor caret_cursor_;
00472 #endif
00473 
00474     std::list<Rect> last_selection_region_;
00475     std::list<Rect> selection_region_;
00476     std::list<Rect> last_cursor_region_;
00477     std::list<Rect> cursor_region_;
00478 
00479 #if defined(NUX_OS_LINUX)
00480     IBusIMEContext* ime_;
00481     friend class IBusIMEContext;
00482 #endif
00483 
00484     bool ime_active_;
00485 
00486     bool text_input_mode_;    
00487     bool key_nav_mode_;
00488 
00489     bool lose_key_focus_on_key_nav_direction_up_;
00490     bool lose_key_focus_on_key_nav_direction_down_;
00491 
00492     bool dead_key_mode_;
00493     std::string dead_key_string_;
00494 
00495     bool composition_mode_;
00496     std::string composition_string_;
00497 
00498     virtual bool InspectKeyEvent(Event const& event);
00499     virtual bool InspectKeyEvent(unsigned int eventType, unsigned int keysym, const char* character);
00500 };
00501 }
00502 
00503 #endif // TEXTENTRY_H