nux-0.9.46
|
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 virtual long ProcessEvent (IEvent &ievent, long TraverseInfo, long ProcessEventInfo); 00084 virtual void Draw (GraphicsEngine &GfxContext, bool force_draw); 00085 virtual void DrawContent (GraphicsEngine &GfxContext, bool force_draw); 00086 virtual void PostDraw (GraphicsEngine &GfxContext, bool force_draw); 00087 00088 void PreLayoutManagement (); 00089 long PostLayoutManagement (long layoutResult); 00090 00091 // Receivers 00092 00093 void RecvMouseDoubleClick (int x, int y, unsigned long button_flags, unsigned long key_flags); 00094 void RecvMouseUp (int x, int y, unsigned long button_flags, unsigned long key_flags); 00095 void RecvMouseDown (int x, int y, unsigned long button_flags, unsigned long key_flags); 00096 void RecvMouseDrag (int x, int y, int dx, int dy, unsigned long button_flags, unsigned long key_flags); 00097 void RecvKeyEvent ( 00098 GraphicsEngine &GfxContext , /*Graphics Context for text operation*/ 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 SetWidth(int width); 00132 // int GetWidth(); 00133 // void SetHeight(int height); 00134 // int GetHeight(); 00135 // void GetSizeRequest(int *width, int *height); 00136 // void SetBold(bool bold); 00137 // bool IsBold(); 00138 // void SetItalic(bool italic); 00139 // bool IsItalic(); 00140 // void SetStrikeout(bool strikeout); 00141 // bool IsStrikeout(); 00142 // void SetUnderline(bool underline); 00143 // bool IsUnderline(); 00144 // void SetMultiline(bool multiline); 00145 // bool IsMultiline(); 00146 // void SetWordWrap(bool wrap); 00147 // bool IsWordWrap(); 00148 // void SetReadOnly(bool readonly); 00149 // bool IsReadOnly(); 00150 void SetText(const char *text); 00151 std::string GetText(); 00152 // void SetBackground(Texture *background); 00153 // const Texture *GetBackground(); 00154 void SetTextColor(const Color &color); 00155 Color GetTextColor() const; 00156 void SetFontFamily (const char *font); 00157 void SetFontSize (double font_size); 00158 void SetFontOptions (const cairo_font_options_t *options); 00159 00160 // std::string GetFontFamily(); 00161 // void OnFontSizeChange(); 00162 // void SetPasswordChar(const char *c); 00163 // std::string GetPasswordChar(); 00164 // bool IsScrollBarRequired(); 00165 // void GetScrollBarInfo(int *range, int *line_step, 00166 // int *page_step, int *cur_pos); 00167 // void ScrollTo(int position); 00168 // void MarkRedraw(); 00170 void Select(int start, int end); 00172 void SelectAll(); 00173 00174 CairoGraphics::Alignment GetAlign() const; 00175 void SetAlign(CairoGraphics::Alignment align); 00176 00177 protected: 00178 virtual void DoSetFocused (bool focused); 00179 bool _block_focus; // used to selectively ignore focus keyevents 00180 00181 private: 00185 enum MovementStep { 00186 VISUALLY, 00187 WORDS, 00188 DISPLAY_LINES, 00189 DISPLAY_LINE_ENDS, 00190 PAGES, 00191 BUFFER 00192 }; 00193 00194 void QueueTextDraw (); 00196 void ResetLayout(); 00201 PangoLayout* EnsureLayout(); 00203 PangoLayout* CreateLayout(); 00205 CairoGraphics* EnsureCanvas(); 00207 void AdjustScroll(); 00213 void QueueRefresh(bool relayout, bool adjust_scroll); 00215 void ResetImContext(); 00217 void ResetPreedit(); 00218 // /** Create a new im context according to current visibility setting */ 00219 // void InitImContext(); 00220 // /** Set the visibility of the edit control */ 00221 // void SetVisibility(bool visible); 00222 // 00223 // /** Check if the cursor should be blinking */ 00224 // bool IsCursorBlinking(); 00225 // /** Send out a request to blink the cursor if necessary */ 00226 // void QueueCursorBlink(); 00227 // /** Timer callback to blink the cursor */ 00228 // bool CursorBlinkCallback(int timer_id); 00229 void ShowCursor(); 00230 void HideCursor(); 00231 00233 void DrawCursor(CairoGraphics *canvas); 00235 void DrawText(CairoGraphics *canvas); 00236 00237 void GetCursorRects(Rect *strong, Rect *weak); 00238 00239 void UpdateCursorRegion(); 00240 00241 void UpdateSelectionRegion(); 00242 00244 void MoveCursor(MovementStep step, int count, bool extend_selection); 00246 int MoveVisually(int current_pos, int count); 00248 int MoveWords(int current_pos, int count); 00250 int MoveDisplayLines(int current_pos, int count); 00252 int MovePages(int current_pos, int count); 00254 int MoveLineEnds(int current_pos, int count); 00255 00257 void SetCursor(int cursor); 00260 int XYToTextIndex(int x, int y); 00262 bool GetSelectionBounds(int *start, int *end); 00264 void SetSelectionBounds(int selection_bound, int cursor); 00265 00267 int TextIndexToLayoutIndex(int text_index, bool consider_preedit_cursor); 00268 00270 int LayoutIndexToTextIndex(int layout_index); 00271 00273 int GetCharLength(int index); 00274 00276 int GetPrevCharLength(int index); 00277 00279 void EnterText(const char *str); 00281 void DeleteText(int start, int end); 00282 00284 void SelectWord(); 00286 void SelectLine(); 00288 void DeleteSelection(); 00289 00291 void CutClipboard(); 00293 void CopyClipboard(); 00295 void PasteClipboard(); 00297 void BackSpace(MovementStep step); 00299 void Delete(MovementStep step); 00301 void ToggleOverwrite(); 00302 // 00304 Color GetSelectionBackgroundColor(); 00306 Color GetSelectionTextColor(); 00307 // 00308 // /** 00309 // * Gets the gtk widget used by the gadget host and the cursor location 00310 // * for gtk im context. relative to the widget coordinate. 00311 // */ 00312 // GtkWidget *GetWidgetAndCursorLocation(GdkRectangle *cur); 00313 // 00317 void GetCursorLocationInLayout(int *strong_x, int *strong_y, int *strong_height, 00318 int *weak_x, int *weak_y, int *weak_height); 00319 00320 // /** 00321 // * Updates the cursor location of input method context. 00322 // */ 00323 // void UpdateIMCursorLocation(); 00324 // 00325 // /** Callback function for IM "commit" signal */ 00326 // static void CommitCallback(GtkIMContext *context, 00327 // const char *str, void *gg); 00328 // /** Callback function for IM "retrieve-surrounding" signal */ 00329 // static gboolean RetrieveSurroundingCallback(GtkIMContext *context, 00330 // void *gg); 00331 // /** Callback function for IM "delete-surrounding" signal */ 00332 // static gboolean DeleteSurroundingCallback(GtkIMContext *context, int offset, 00333 // int n_chars, void *gg); 00334 // /** Callback function for IM "preedit-start" signal */ 00335 // static void PreeditStartCallback(GtkIMContext *context, void *gg); 00336 // /** Callback function for IM "preedit-changed" signal */ 00337 // static void PreeditChangedCallback(GtkIMContext *context, void *gg); 00338 // /** Callback function for IM "preedit-end" signal */ 00339 // static void PreeditEndCallback(GtkIMContext *context, void *gg); 00340 // /** 00341 // * Callback for gtk_clipboard_request_text function. 00342 // * This function performs real paste. 00343 // */ 00344 // static void PasteCallback(GtkClipboard *clipboard, 00345 // const gchar *str, void *gg); 00346 // 00347 // private: 00348 // /** Owner of this gtk edit implementation object. */ 00349 // GtkEditElement *owner_; 00350 // /** Main loop object */ 00351 // MainLoopInterface *main_loop_; 00352 // /** Graphics object, must be CairoGraphics */ 00353 // const GraphicsInterface *graphics_; 00354 // 00356 CairoGraphics *canvas_; 00357 00358 // /** Gtk InputMethod Context */ 00359 // GtkIMContext *im_context_; 00360 00362 PangoLayout *cached_layout_; 00363 00365 std::string _text; 00367 std::string _preedit; 00369 PangoAttrList *preedit_attrs_; 00374 std::string password_char_; 00375 00377 t_u64 last_dblclick_time_; 00378 // 00379 // /** Canvas width */ 00380 // int width_; 00381 // /** Canvas height */ 00382 // int height_; 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 00465 int full_height_; 00466 00467 // /** The background texture of the edit control */ 00468 // Texture *background_; 00469 00471 Color _text_color; 00472 00473 CairoGraphics::Alignment align_; 00474 00475 std::list<Rect> last_selection_region_; 00476 std::list<Rect> selection_region_; 00477 std::list<Rect> last_cursor_region_; 00478 std::list<Rect> cursor_region_; 00479 }; 00480 } 00481 00482 #endif // TEXTENTRY_H