2009-10-20 Chris Toshok <toshok@ximian.com>
[moon.git] / src / textbox.h
bloba2e801beb7dea34fd3431046f784b725a7f26cbf
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * textbox.h:
5 * Contact:
6 * Moonlight List (moonlight-list@lists.ximian.com)
8 * Copyright 2007 Novell, Inc. (http://www.novell.com)
10 * See the LICENSE file included with the distribution for details.
13 #ifndef __TEXTBOX_H__
14 #define __TEXTBOX_H__
16 #include <gtk/gtk.h>
17 #include <cairo.h>
19 #include "fontsource.h"
20 #include "moon-path.h"
21 #include "eventargs.h"
22 #include "thickness.h"
23 #include "control.h"
24 #include "layout.h"
25 #include "brush.h"
26 #include "fonts.h"
27 #include "size.h"
30 /* @Namespace=System.Windows.Input */
31 class InputMethod : public DependencyObject {
32 protected:
33 virtual ~InputMethod () {}
35 public:
36 /* @PropertyType=bool,Attached,DefaultValue=true,Validator=IsInputMethodEnabledValidator */
37 const static int IsInputMethodEnabledProperty;
39 /* @ManagedAccess=Internal,GeneratePInvoke,GenerateCBinding */
40 InputMethod () { SetObjectType (Type::INPUTMETHOD); }
43 /* @Namespace=None */
44 class TextChangedEventArgs : public RoutedEventArgs {
45 protected:
46 virtual ~TextChangedEventArgs () { }
48 public:
49 /* @GenerateCBinding,GeneratePInvoke */
50 TextChangedEventArgs () { SetObjectType (Type::TEXTCHANGEDEVENTARGS); }
54 /* @Namespace=None */
55 class CursorPositionChangedEventArgs : public EventArgs {
56 double height, x, y;
58 protected:
59 virtual ~CursorPositionChangedEventArgs () { }
61 public:
62 /* @GenerateCBinding,GeneratePInvoke */
63 CursorPositionChangedEventArgs ()
65 SetObjectType (Type::CURSORPOSITIONCHANGEDEVENTARGS);
66 this->height = 0.0;
67 this->x = 0.0;
68 this->y = 0.0;
71 CursorPositionChangedEventArgs (double height, double x, double y)
73 SetObjectType (Type::CURSORPOSITIONCHANGEDEVENTARGS);
74 this->height = height;
75 this->x = x;
76 this->y = y;
79 /* @GenerateCBinding,GeneratePInvoke */
80 double GetCursorHeight () { return height; }
82 /* @GenerateCBinding,GeneratePInvoke */
83 double GetCursorX () { return x; }
85 /* @GenerateCBinding,GeneratePInvoke */
86 double GetCursorY () { return y; }
90 enum TextBoxModelChangeType {
91 TextBoxModelChangedNothing,
92 TextBoxModelChangedTextAlignment,
93 TextBoxModelChangedTextWrapping,
94 TextBoxModelChangedSelection,
95 TextBoxModelChangedBrush,
96 TextBoxModelChangedFont,
97 TextBoxModelChangedText
101 /* @Namespace=None */
102 class TextBoxModelChangedEventArgs : public EventArgs {
103 protected:
104 virtual ~TextBoxModelChangedEventArgs () { }
106 public:
107 PropertyChangedEventArgs *property;
108 TextBoxModelChangeType changed;
110 TextBoxModelChangedEventArgs (TextBoxModelChangeType changed, PropertyChangedEventArgs *property = NULL)
112 SetObjectType (Type::TEXTBOXMODELCHANGEDEVENTARGS);
113 this->property = property;
114 this->changed = changed;
118 class TextBuffer;
119 class TextBoxUndoStack;
121 /* @Namespace=None */
122 class TextBoxBase : public Control, public ITextAttributes {
123 protected:
124 friend class TextBoxView;
126 DependencyObject *contentElement;
128 TextFontDescription *font;
129 GPtrArray *downloaders;
130 char *font_source;
132 TextBoxUndoStack *undo;
133 TextBoxUndoStack *redo;
134 int selection_anchor;
135 int selection_cursor;
136 double cursor_offset;
137 GtkIMContext *im_ctx;
138 TextBuffer *buffer;
139 TextBoxView *view;
140 int max_length;
142 short accepts_return:1;
143 short need_im_reset:1;
144 short is_read_only:1;
145 short have_offset:1;
146 short multiline:1;
147 short selecting:1;
148 short setvalue:1;
149 short captured:1;
150 short focused:1;
151 short secret:1;
153 short events_mask:2;
154 short emit:2;
156 short batch;
158 // internal mouse events
159 static void mouse_left_button_multi_click (EventObject *sender, EventArgs *args, gpointer closure);
160 void OnMouseLeftButtonMultiClick (MouseButtonEventArgs *args);
162 // GtkIMContext events
163 static gboolean delete_surrounding (GtkIMContext *context, int offset, int n_chars, gpointer user_data);
164 static gboolean retrieve_surrounding (GtkIMContext *context, gpointer user_data);
165 static void commit (GtkIMContext *context, const char *str, gpointer user_data);
166 bool DeleteSurrounding (int offset, int n_chars);
167 void Commit (const char *str);
168 bool RetrieveSurrounding ();
170 // GtkClipboard callbacks
171 static void paste (GtkClipboard *clipboard, const char *text, gpointer closure);
172 void Paste (GtkClipboard *clipboard, const char *text);
175 // Cursor Navigation
177 double GetCursorOffset ();
178 virtual int CursorDown (int cursor, bool page);
179 virtual int CursorUp (int cursor, bool page);
180 virtual int CursorLineBegin (int cursor);
181 virtual int CursorLineEnd (int cursor, bool include = false);
182 virtual int CursorNextWord (int cursor);
183 virtual int CursorPrevWord (int cursor);
186 // Keyboard Input
188 bool KeyPressUnichar (gunichar c);
190 bool KeyPressBackSpace (GdkModifierType modifiers);
191 bool KeyPressDelete (GdkModifierType modifiers);
192 bool KeyPressPageDown (GdkModifierType modifiers);
193 bool KeyPressPageUp (GdkModifierType modifiers);
194 bool KeyPressHome (GdkModifierType modifiers);
195 bool KeyPressEnd (GdkModifierType modifiers);
196 bool KeyPressRight (GdkModifierType modifiers);
197 bool KeyPressLeft (GdkModifierType modifiers);
198 bool KeyPressDown (GdkModifierType modifiers);
199 bool KeyPressUp (GdkModifierType modifiers);
201 void ResetIMContext ();
203 void EmitCursorPositionChanged (double height, double x, double y);
205 virtual void EmitSelectionChanged () { }
206 virtual void EmitTextChanged () = 0;
208 virtual void SyncSelectedText () = 0;
209 virtual void SyncText () = 0;
211 void BatchPush ();
212 void BatchPop ();
214 void SyncAndEmit (bool sync_text = true);
216 void AddFontResource (const char *resource);
217 void AddFontSource (Downloader *downloader);
219 void CleanupDownloaders ();
220 void DownloaderComplete (Downloader *downloader);
222 static void downloader_complete (EventObject *sender, EventArgs *calldata, gpointer closure);
225 // Protected Property Accessors
227 bool HasSelectedText () { return selection_cursor != selection_anchor; }
228 TextBuffer *GetBuffer () { return buffer; }
229 int GetCursor () { return selection_cursor; }
230 bool IsFocused () { return focused; }
232 virtual const char *GetActualText () = 0;
234 virtual void SetSelectedText (const char *text) = 0;
235 virtual const char *GetSelectedText () = 0;
237 virtual void SetSelectionStart (int start) = 0;
238 virtual int GetSelectionStart () = 0;
240 virtual void SetSelectionLength (int length) = 0;
241 virtual int GetSelectionLength () = 0;
243 void ClearSelection (int start);
246 // Protected Events
248 const static int ModelChangedEvent;
251 // Initialization/Destruction
253 void Initialize (Type::Kind type, const char *type_name);
254 virtual ~TextBoxBase ();
256 public:
257 TextBoxBase () { }
260 // Overrides
262 virtual void OnPropertyChanged (PropertyChangedEventArgs *args, MoonError *error);
263 virtual void OnSubPropertyChanged (DependencyProperty *prop, DependencyObject *obj, PropertyChangedEventArgs *subobj_args);
264 virtual void SetSurface (Surface *surface);
265 virtual void OnApplyTemplate ();
268 // ITextAttributes Interface Methods
270 virtual TextDecorations Decorations () { return TextDecorationsNone; }
271 virtual TextFontDescription *FontDescription () { return font; }
273 virtual Brush *Background (bool selected)
275 if (selected)
276 return GetSelectionBackground ();
277 else
278 return NULL;
281 virtual Brush *Foreground (bool selected)
283 if (selected)
284 return GetSelectionForeground ();
285 else
286 return GetForeground ();
289 /* @GenerateCBinding,GeneratePInvoke */
290 void OnMouseLeftButtonDown (MouseButtonEventArgs *args);
291 /* @GenerateCBinding,GeneratePInvoke */
292 void OnMouseLeftButtonUp (MouseButtonEventArgs *args);
293 /* @GenerateCBinding,GeneratePInvoke */
294 void OnMouseMove (MouseEventArgs *args);
296 /* @GenerateCBinding,GeneratePInvoke */
297 void PostOnKeyDown (KeyEventArgs *args);
298 /* @GenerateCBinding,GeneratePInvoke */
299 void OnKeyDown (KeyEventArgs *args);
300 /* @GenerateCBinding,GeneratePInvoke */
301 void OnKeyUp (KeyEventArgs *args);
303 /* @GenerateCBinding,GeneratePInvoke */
304 void OnGotFocus (RoutedEventArgs *args);
305 /* @GenerateCBinding,GeneratePInvoke */
306 void OnLostFocus (RoutedEventArgs *args);
309 // Undo/Redo Operations
311 bool CanUndo ();
312 bool CanRedo ();
313 void Undo ();
314 void Redo ();
317 // Selection Operations
319 /* @GenerateCBinding,GeneratePInvoke */
320 bool SelectWithError (int start, int length, MoonError *error);
322 /* @GenerateCBinding,GeneratePInvoke */
323 void SelectAll ();
325 virtual Brush *GetSelectionBackground () = 0;
326 virtual Brush *GetSelectionForeground () = 0;
327 virtual Brush *GetCaretBrush () = 0;
329 virtual TextAlignment GetTextAlignment () { return TextAlignmentLeft; }
330 virtual TextWrapping GetTextWrapping () { return TextWrappingNoWrap; }
332 virtual const char *GetDisplayText () = 0;
335 // Events
337 const static int CursorPositionChangedEvent;
341 class TextBoxDynamicPropertyValueProvider;
343 /* @Namespace=System.Windows.Controls */
344 class TextBox : public TextBoxBase {
345 friend class TextBoxDynamicPropertyValueProvider;
347 protected:
348 virtual const char *GetActualText () { return GetText (); }
350 virtual void EmitSelectionChanged ();
351 virtual void EmitTextChanged ();
353 virtual void SyncSelectedText ();
354 virtual void SyncText ();
356 virtual void SetSelectionStart (int start);
357 virtual void SetSelectionLength (int length);
359 virtual ~TextBox () { }
361 public:
362 /* @PropertyType=bool,DefaultValue=false,Version=2.0,GenerateAccessors */
363 const static int AcceptsReturnProperty;
364 /* @PropertyType=Brush,DefaultValue=new SolidColorBrush("black"),Version=2.0,GenerateAccessors */
365 const static int CaretBrushProperty;
366 /* @PropertyType=FontSource,ManagedFieldAccess=Internal,GenerateAccessors */
367 const static int FontSourceProperty;
368 /* @PropertyType=ScrollBarVisibility,DefaultValue=ScrollBarVisibilityHidden,Version=2.0,ManagedFieldAccess=Internal,GenerateAccessors */
369 const static int HorizontalScrollBarVisibilityProperty;
370 /* @PropertyType=bool,DefaultValue=false,Version=2.0,GenerateAccessors */
371 const static int IsReadOnlyProperty;
372 /* @PropertyType=gint32,DefaultValue=0,Version=2.0,GenerateAccessors,Validator=PositiveIntValidator */
373 const static int MaxLengthProperty;
374 /* @PropertyType=string,DefaultValue=\"\",AlwaysChange,Version=2.0,ManagedFieldAccess=Internal,GenerateAccessors */
375 const static int SelectedTextProperty;
376 /* @PropertyType=Brush,Version=2.0,GenerateAccessors */
377 const static int SelectionBackgroundProperty;
378 /* @PropertyType=Brush,Version=2.0,GenerateAccessors */
379 const static int SelectionForegroundProperty;
380 /* @PropertyType=gint32,DefaultValue=0,AlwaysChange,Version=2.0,ManagedFieldAccess=Internal,GenerateAccessors,Validator=PositiveIntValidator */
381 const static int SelectionLengthProperty;
382 /* @PropertyType=gint32,DefaultValue=0,AlwaysChange,Version=2.0,ManagedFieldAccess=Internal,GenerateAccessors,Validator=PositiveIntValidator */
383 const static int SelectionStartProperty;
384 /* @PropertyType=string,Version=2.0,GenerateAccessors,GenerateManagedAccessors=false */
385 const static int TextProperty;
386 /* @PropertyType=TextAlignment,DefaultValue=TextAlignmentLeft,Version=2.0,GenerateAccessors */
387 const static int TextAlignmentProperty;
388 /* @PropertyType=TextWrapping,DefaultValue=TextWrappingNoWrap,Version=2.0,GenerateAccessors */
389 const static int TextWrappingProperty;
390 /* @PropertyType=ScrollBarVisibility,DefaultValue=ScrollBarVisibilityHidden,Version=2.0,ManagedFieldAccess=Internal,GenerateAccessors */
391 const static int VerticalScrollBarVisibilityProperty;
393 /* @GenerateCBinding,GeneratePInvoke */
394 TextBox ();
397 // Overrides
399 virtual void OnPropertyChanged (PropertyChangedEventArgs *args, MoonError *error);
400 virtual void OnSubPropertyChanged (DependencyProperty *prop, DependencyObject *obj, PropertyChangedEventArgs *subobj_args);
401 virtual void OnApplyTemplate ();
403 virtual const char *GetDisplayText () { return GetText (); }
406 // Property Accessors
408 void SetAcceptsReturn (bool accept);
409 bool GetAcceptsReturn ();
411 void SetCaretBrush (Brush *caret);
412 virtual Brush *GetCaretBrush ();
414 void SetFontSource (FontSource *source);
415 FontSource *GetFontSource ();
417 void SetHorizontalScrollBarVisibility (ScrollBarVisibility visibility);
418 ScrollBarVisibility GetHorizontalScrollBarVisibility ();
420 void SetIsReadOnly (bool readOnly);
421 bool GetIsReadOnly ();
423 void SetMaxLength (int length);
424 int GetMaxLength ();
426 void SetSelectionBackground (Brush *background);
427 virtual Brush *GetSelectionBackground ();
429 void SetSelectionForeground (Brush *foreground);
430 virtual Brush *GetSelectionForeground ();
432 virtual void SetSelectedText (const char *text);
433 virtual const char *GetSelectedText ();
435 virtual int GetSelectionStart ();
436 virtual int GetSelectionLength ();
438 void SetText (const char *text);
439 const char *GetText ();
441 void SetTextAlignment (TextAlignment alignment);
442 virtual TextAlignment GetTextAlignment ();
444 void SetTextWrapping (TextWrapping wrapping);
445 virtual TextWrapping GetTextWrapping ();
447 void SetVerticalScrollBarVisibility (ScrollBarVisibility visibility);
448 ScrollBarVisibility GetVerticalScrollBarVisibility ();
451 // Events
453 /* @DelegateType=RoutedEventHandler */
454 const static int SelectionChangedEvent;
455 /* @DelegateType=TextChangedEventHandler */
456 const static int TextChangedEvent;
460 class PasswordBoxDynamicPropertyValueProvider;
462 /* @Namespace=System.Windows.Controls */
463 class PasswordBox : public TextBoxBase {
464 friend class PasswordBoxDynamicPropertyValueProvider;
466 GString *display;
468 protected:
469 virtual int CursorDown (int cursor, bool page);
470 virtual int CursorUp (int cursor, bool page);
471 virtual int CursorLineBegin (int cursor);
472 virtual int CursorLineEnd (int cursor, bool include = false);
473 virtual int CursorNextWord (int cursor);
474 virtual int CursorPrevWord (int cursor);
476 virtual void EmitTextChanged ();
478 virtual void SyncSelectedText ();
479 virtual void SyncText ();
480 void SyncDisplayText ();
483 // Protected Property Accessors
485 virtual const char *GetActualText () { return GetPassword (); }
487 virtual void SetSelectedText (const char *text);
488 virtual const char *GetSelectedText ();
490 virtual void SetSelectionStart (int start);
491 virtual int GetSelectionStart ();
493 virtual void SetSelectionLength (int length);
494 virtual int GetSelectionLength ();
496 virtual ~PasswordBox ();
498 public:
499 /* @PropertyType=Brush,DefaultValue=new SolidColorBrush("black"),Version=2.0,GenerateAccessors */
500 const static int CaretBrushProperty;
501 /* @PropertyType=FontSource,ManagedFieldAccess=Internal,GenerateAccessors */
502 const static int FontSourceProperty;
503 /* @PropertyType=gint32,DefaultValue=0,Version=2.0,GenerateAccessors,Validator=PositiveIntValidator */
504 const static int MaxLengthProperty;
505 /* @PropertyType=char,DefaultValue=(gunichar) 9679\, Type::CHAR,Version=2.0,GenerateAccessors */
506 const static int PasswordCharProperty;
507 /* @PropertyType=string,DefaultValue=\"\",AlwaysChange,Version=2.0,GenerateAccessors,Validator=NonNullValidator */
508 const static int PasswordProperty;
509 /* @PropertyType=string,DefaultValue=\"\",Version=2.0,ManagedFieldAccess=Internal,GenerateAccessors */
510 const static int SelectedTextProperty;
511 /* @PropertyType=Brush,Version=2.0,GenerateAccessors */
512 const static int SelectionBackgroundProperty;
513 /* @PropertyType=Brush,Version=2.0,GenerateAccessors */
514 const static int SelectionForegroundProperty;
515 /* @PropertyType=gint32,DefaultValue=0,Version=2.0,ManagedFieldAccess=Internal,GenerateAccessors,Validator=PositiveIntValidator */
516 const static int SelectionLengthProperty;
517 /* @PropertyType=gint32,DefaultValue=0,Version=2.0,ManagedFieldAccess=Internal,GenerateAccessors,Validator=PositiveIntValidator */
518 const static int SelectionStartProperty;
520 /* @GenerateCBinding,GeneratePInvoke */
521 PasswordBox ();
524 // Overrides
526 virtual void OnPropertyChanged (PropertyChangedEventArgs *args, MoonError *error);
527 virtual void OnSubPropertyChanged (DependencyProperty *prop, DependencyObject *obj, PropertyChangedEventArgs *subobj_args);
529 virtual const char *GetDisplayText ();
532 // Property Accesors
534 void SetCaretBrush (Brush *caret);
535 virtual Brush *GetCaretBrush ();
537 void SetFontSource (FontSource *source);
538 FontSource *GetFontSource ();
540 void SetMaxLength (int length);
541 int GetMaxLength ();
543 void SetPassword (const char *password);
544 const char *GetPassword ();
546 void SetPasswordChar (gunichar c);
547 gunichar GetPasswordChar ();
549 void SetSelectionBackground (Brush *background);
550 virtual Brush *GetSelectionBackground ();
552 void SetSelectionForeground (Brush *foreground);
553 virtual Brush *GetSelectionForeground ();
556 // Events
558 /* @DelegateType=RoutedEventHandler */
559 const static int PasswordChangedEvent;
563 /* @Namespace=Microsoft.Internal */
564 class TextBoxView : public FrameworkElement {
565 TextBoxBase *textbox;
566 glong blink_timeout;
567 TextLayout *layout;
568 Rect cursor;
570 int selection_changed:1;
571 int had_selected_text:1;
572 int cursor_visible:1;
573 int dirty:1;
575 // mouse events
576 static void mouse_left_button_down (EventObject *sender, EventArgs *args, gpointer closure);
577 static void mouse_left_button_up (EventObject *sender, EventArgs *args, gpointer closure);
578 void OnMouseLeftButtonDown (MouseButtonEventArgs *args);
579 void OnMouseLeftButtonUp (MouseButtonEventArgs *args);
581 // TextBox events
582 static void model_changed (EventObject *sender, EventArgs *args, gpointer closure);
583 void OnModelChanged (TextBoxModelChangedEventArgs *args);
585 // cursor blink
586 static gboolean blink (void *user_data);
587 void ConnectBlinkTimeout (guint multiplier);
588 void DisconnectBlinkTimeout ();
589 void ResetCursorBlink (bool delay);
590 void DelayCursorBlink ();
591 void BeginCursorBlink ();
592 void EndCursorBlink ();
593 void ShowCursor ();
594 void HideCursor ();
595 bool Blink ();
597 void UpdateCursor (bool invalidate);
598 void InvalidateCursor ();
599 void UpdateText ();
601 void Layout (Size constraint);
602 void Paint (cairo_t *cr);
604 protected:
605 virtual ~TextBoxView ();
607 public:
608 /* @GenerateCBinding,GeneratePInvoke */
609 TextBoxView ();
612 // Overrides
614 virtual void Render (cairo_t *cr, Region *region, bool path_only = false);
615 virtual void GetSizeForBrush (cairo_t *cr, double *width, double *height);
616 virtual Size ComputeActualSize ();
617 virtual Size MeasureOverride (Size availableSize);
618 virtual Size ArrangeOverride (Size finalSize);
621 // Methods
623 int GetLineCount () { return layout->GetLineCount (); }
624 TextLayoutLine *GetLineFromY (double y, int *index = NULL);
625 TextLayoutLine *GetLineFromIndex (int index);
627 int GetCursorFromXY (double x, double y);
628 Rect GetCursor () { return cursor; }
630 void OnLostFocus ();
631 void OnGotFocus ();
634 // Property Accessors
636 TextBoxBase *GetTextBox () { return textbox; }
637 void SetTextBox (TextBoxBase *textbox);
640 #endif /* __TEXTBOX_H__ */