nux-0.9.48
|
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 virtual void GeometryChanged (); 00182 00183 private: 00187 enum MovementStep { 00188 VISUALLY, 00189 WORDS, 00190 DISPLAY_LINES, 00191 DISPLAY_LINE_ENDS, 00192 PAGES, 00193 BUFFER 00194 }; 00195 00196 void QueueTextDraw (); 00198 void ResetLayout(); 00203 PangoLayout* EnsureLayout(); 00205 PangoLayout* CreateLayout(); 00207 CairoGraphics* EnsureCanvas(); 00209 void AdjustScroll(); 00215 void QueueRefresh(bool relayout, bool adjust_scroll); 00217 void ResetImContext(); 00219 void ResetPreedit(); 00220 // /** Create a new im context according to current visibility setting */ 00221 // void InitImContext(); 00222 // /** Set the visibility of the edit control */ 00223 // void SetVisibility(bool visible); 00224 // 00225 // /** Check if the cursor should be blinking */ 00226 // bool IsCursorBlinking(); 00227 // /** Send out a request to blink the cursor if necessary */ 00228 // void QueueCursorBlink(); 00229 // /** Timer callback to blink the cursor */ 00230 // bool CursorBlinkCallback(int timer_id); 00231 void ShowCursor(); 00232 void HideCursor(); 00233 00235 void DrawCursor(CairoGraphics *canvas); 00237 void DrawText(CairoGraphics *canvas); 00238 00239 void GetCursorRects(Rect *strong, Rect *weak); 00240 00241 void UpdateCursorRegion(); 00242 00243 void UpdateSelectionRegion(); 00244 00246 void MoveCursor(MovementStep step, int count, bool extend_selection); 00248 int MoveVisually(int current_pos, int count); 00250 int MoveWords(int current_pos, int count); 00252 int MoveDisplayLines(int current_pos, int count); 00254 int MovePages(int current_pos, int count); 00256 int MoveLineEnds(int current_pos, int count); 00257 00259 void SetCursor(int cursor); 00262 int XYToTextIndex(int x, int y); 00264 bool GetSelectionBounds(int *start, int *end); 00266 void SetSelectionBounds(int selection_bound, int cursor); 00267 00269 int TextIndexToLayoutIndex(int text_index, bool consider_preedit_cursor); 00270 00272 int LayoutIndexToTextIndex(int layout_index); 00273 00275 int GetCharLength(int index); 00276 00278 int GetPrevCharLength(int index); 00279 00281 void EnterText(const char *str); 00283 void DeleteText(int start, int end); 00284 00286 void SelectWord(); 00288 void SelectLine(); 00290 void DeleteSelection(); 00291 00293 void CutClipboard(); 00295 void CopyClipboard(); 00297 void PasteClipboard(); 00299 void BackSpace(MovementStep step); 00301 void Delete(MovementStep step); 00303 void ToggleOverwrite(); 00304 // 00306 Color GetSelectionBackgroundColor(); 00308 Color GetSelectionTextColor(); 00309 // 00310 // /** 00311 // * Gets the gtk widget used by the gadget host and the cursor location 00312 // * for gtk im context. relative to the widget coordinate. 00313 // */ 00314 // GtkWidget *GetWidgetAndCursorLocation(GdkRectangle *cur); 00315 // 00319 void GetCursorLocationInLayout(int *strong_x, int *strong_y, int *strong_height, 00320 int *weak_x, int *weak_y, int *weak_height); 00321 00322 // /** 00323 // * Updates the cursor location of input method context. 00324 // */ 00325 // void UpdateIMCursorLocation(); 00326 // 00327 // /** Callback function for IM "commit" signal */ 00328 // static void CommitCallback(GtkIMContext *context, 00329 // const char *str, void *gg); 00330 // /** Callback function for IM "retrieve-surrounding" signal */ 00331 // static gboolean RetrieveSurroundingCallback(GtkIMContext *context, 00332 // void *gg); 00333 // /** Callback function for IM "delete-surrounding" signal */ 00334 // static gboolean DeleteSurroundingCallback(GtkIMContext *context, int offset, 00335 // int n_chars, void *gg); 00336 // /** Callback function for IM "preedit-start" signal */ 00337 // static void PreeditStartCallback(GtkIMContext *context, void *gg); 00338 // /** Callback function for IM "preedit-changed" signal */ 00339 // static void PreeditChangedCallback(GtkIMContext *context, void *gg); 00340 // /** Callback function for IM "preedit-end" signal */ 00341 // static void PreeditEndCallback(GtkIMContext *context, void *gg); 00342 // /** 00343 // * Callback for gtk_clipboard_request_text function. 00344 // * This function performs real paste. 00345 // */ 00346 // static void PasteCallback(GtkClipboard *clipboard, 00347 // const gchar *str, void *gg); 00348 // 00349 // private: 00350 // /** Owner of this gtk edit implementation object. */ 00351 // GtkEditElement *owner_; 00352 // /** Main loop object */ 00353 // MainLoopInterface *main_loop_; 00354 // /** Graphics object, must be CairoGraphics */ 00355 // const GraphicsInterface *graphics_; 00356 // 00358 CairoGraphics *canvas_; 00359 00360 // /** Gtk InputMethod Context */ 00361 // GtkIMContext *im_context_; 00362 00364 PangoLayout *cached_layout_; 00365 00367 std::string _text; 00369 std::string _preedit; 00371 PangoAttrList *preedit_attrs_; 00376 std::string password_char_; 00377 00379 t_u64 last_dblclick_time_; 00380 // 00381 // /** Canvas width */ 00382 // int width_; 00383 // /** Canvas height */ 00384 // int height_; 00385 // 00387 int cursor_; 00392 int preedit_cursor_; 00397 int selection_bound_; 00398 00400 int scroll_offset_x_; 00402 int scroll_offset_y_; 00404 int cursor_blink_timer_; 00412 int cursor_blink_status_; 00413 00415 bool visible_; 00417 bool focused_; 00419 bool need_im_reset_; 00421 bool overwrite_; 00423 bool select_words_; 00425 bool select_lines_; 00427 bool button_; 00429 bool bold_; 00431 bool underline_; 00433 bool strikeout_; 00435 bool italic_; 00437 bool multiline_; 00439 bool wrap_; 00441 bool cursor_visible_; 00443 bool readonly_; 00448 bool content_modified_; 00449 00451 bool selection_changed_; 00452 00454 bool cursor_moved_; 00455 00457 bool update_canvas_; 00458 00460 std::string font_family_; 00462 double font_size_; 00463 00464 cairo_font_options_t *font_options_; 00465 double font_dpi_; 00466 00467 int full_height_; 00468 00469 // /** The background texture of the edit control */ 00470 // Texture *background_; 00471 00473 Color _text_color; 00474 00475 CairoGraphics::Alignment align_; 00476 00477 std::list<Rect> last_selection_region_; 00478 std::list<Rect> selection_region_; 00479 std::list<Rect> last_cursor_region_; 00480 std::list<Rect> cursor_region_; 00481 }; 00482 } 00483 00484 #endif // TEXTENTRY_H