2009-12-07 Rolf Bjarne Kvinge <RKvinge@novell.com>
[moon.git] / src / textbox.h
blob586a12b657c1dc4cd175af6cbaf15264bc7073c1
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,ManagedEvents=Manual */
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 SetIsAttached (bool value);
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 /* @CallInitialize */
345 class TextBox : public TextBoxBase {
346 friend class TextBoxDynamicPropertyValueProvider;
348 protected:
349 virtual const char *GetActualText () { return GetText (); }
351 virtual void EmitSelectionChanged ();
352 virtual void EmitTextChanged ();
354 virtual void SyncSelectedText ();
355 virtual void SyncText ();
357 virtual void SetSelectionStart (int start);
358 virtual void SetSelectionLength (int length);
360 virtual ~TextBox () { }
362 public:
363 /* @PropertyType=bool,DefaultValue=false,Version=2.0,GenerateAccessors */
364 const static int AcceptsReturnProperty;
365 /* @PropertyType=Brush,DefaultValue=new SolidColorBrush("black"),Version=2.0,GenerateAccessors */
366 const static int CaretBrushProperty;
367 /* @PropertyType=FontSource,ManagedFieldAccess=Internal,GenerateAccessors */
368 const static int FontSourceProperty;
369 /* @PropertyType=ScrollBarVisibility,DefaultValue=ScrollBarVisibilityHidden,Version=2.0,ManagedFieldAccess=Internal,GenerateAccessors */
370 const static int HorizontalScrollBarVisibilityProperty;
371 /* @PropertyType=bool,DefaultValue=false,Version=2.0,GenerateAccessors */
372 const static int IsReadOnlyProperty;
373 /* @PropertyType=gint32,DefaultValue=0,Version=2.0,GenerateAccessors,Validator=PositiveIntValidator */
374 const static int MaxLengthProperty;
375 /* @PropertyType=string,DefaultValue=\"\",AlwaysChange,Version=2.0,ManagedFieldAccess=Internal,GenerateAccessors */
376 const static int SelectedTextProperty;
377 /* @PropertyType=Brush,Version=2.0,GenerateAccessors */
378 const static int SelectionBackgroundProperty;
379 /* @PropertyType=Brush,Version=2.0,GenerateAccessors */
380 const static int SelectionForegroundProperty;
381 /* @PropertyType=gint32,DefaultValue=0,AlwaysChange,Version=2.0,ManagedFieldAccess=Internal,GenerateAccessors,Validator=PositiveIntValidator */
382 const static int SelectionLengthProperty;
383 /* @PropertyType=gint32,DefaultValue=0,AlwaysChange,Version=2.0,ManagedFieldAccess=Internal,GenerateAccessors,Validator=PositiveIntValidator */
384 const static int SelectionStartProperty;
385 /* @PropertyType=string,Version=2.0,GenerateAccessors,GenerateManagedAccessors=false */
386 const static int TextProperty;
387 /* @PropertyType=TextAlignment,DefaultValue=TextAlignmentLeft,Version=2.0,GenerateAccessors */
388 const static int TextAlignmentProperty;
389 /* @PropertyType=TextWrapping,DefaultValue=TextWrappingNoWrap,Version=2.0,GenerateAccessors */
390 const static int TextWrappingProperty;
391 /* @PropertyType=ScrollBarVisibility,DefaultValue=ScrollBarVisibilityHidden,Version=2.0,ManagedFieldAccess=Internal,GenerateAccessors */
392 const static int VerticalScrollBarVisibilityProperty;
394 /* @GenerateCBinding,GeneratePInvoke */
395 TextBox ();
398 // Overrides
400 virtual void OnPropertyChanged (PropertyChangedEventArgs *args, MoonError *error);
401 virtual void OnSubPropertyChanged (DependencyProperty *prop, DependencyObject *obj, PropertyChangedEventArgs *subobj_args);
402 virtual void OnApplyTemplate ();
404 virtual const char *GetDisplayText () { return GetText (); }
407 // Property Accessors
409 void SetAcceptsReturn (bool accept);
410 bool GetAcceptsReturn ();
412 void SetCaretBrush (Brush *caret);
413 virtual Brush *GetCaretBrush ();
415 void SetFontSource (FontSource *source);
416 FontSource *GetFontSource ();
418 void SetHorizontalScrollBarVisibility (ScrollBarVisibility visibility);
419 ScrollBarVisibility GetHorizontalScrollBarVisibility ();
421 void SetIsReadOnly (bool readOnly);
422 bool GetIsReadOnly ();
424 void SetMaxLength (int length);
425 int GetMaxLength ();
427 void SetSelectionBackground (Brush *background);
428 virtual Brush *GetSelectionBackground ();
430 void SetSelectionForeground (Brush *foreground);
431 virtual Brush *GetSelectionForeground ();
433 virtual void SetSelectedText (const char *text);
434 virtual const char *GetSelectedText ();
436 virtual int GetSelectionStart ();
437 virtual int GetSelectionLength ();
439 void SetText (const char *text);
440 const char *GetText ();
442 void SetTextAlignment (TextAlignment alignment);
443 virtual TextAlignment GetTextAlignment ();
445 void SetTextWrapping (TextWrapping wrapping);
446 virtual TextWrapping GetTextWrapping ();
448 void SetVerticalScrollBarVisibility (ScrollBarVisibility visibility);
449 ScrollBarVisibility GetVerticalScrollBarVisibility ();
452 // Events
454 /* @DelegateType=RoutedEventHandler */
455 const static int SelectionChangedEvent;
456 /* @DelegateType=TextChangedEventHandler */
457 const static int TextChangedEvent;
461 class PasswordBoxDynamicPropertyValueProvider;
463 /* @Namespace=System.Windows.Controls */
464 /* @CallInitialize */
465 class PasswordBox : public TextBoxBase {
466 friend class PasswordBoxDynamicPropertyValueProvider;
468 GString *display;
470 protected:
471 virtual int CursorDown (int cursor, bool page);
472 virtual int CursorUp (int cursor, bool page);
473 virtual int CursorLineBegin (int cursor);
474 virtual int CursorLineEnd (int cursor, bool include = false);
475 virtual int CursorNextWord (int cursor);
476 virtual int CursorPrevWord (int cursor);
478 virtual void EmitTextChanged ();
480 virtual void SyncSelectedText ();
481 virtual void SyncText ();
482 void SyncDisplayText ();
485 // Protected Property Accessors
487 virtual const char *GetActualText () { return GetPassword (); }
489 virtual void SetSelectedText (const char *text);
490 virtual const char *GetSelectedText ();
492 virtual void SetSelectionStart (int start);
493 virtual int GetSelectionStart ();
495 virtual void SetSelectionLength (int length);
496 virtual int GetSelectionLength ();
498 virtual ~PasswordBox ();
500 public:
501 /* @PropertyType=Brush,DefaultValue=new SolidColorBrush("black"),Version=2.0,GenerateAccessors */
502 const static int CaretBrushProperty;
503 /* @PropertyType=FontSource,ManagedFieldAccess=Internal,GenerateAccessors */
504 const static int FontSourceProperty;
505 /* @PropertyType=gint32,DefaultValue=0,Version=2.0,GenerateAccessors,Validator=PositiveIntValidator */
506 const static int MaxLengthProperty;
507 /* @PropertyType=char,DefaultValue=(gunichar) 9679\, Type::CHAR,Version=2.0,GenerateAccessors */
508 const static int PasswordCharProperty;
509 /* @PropertyType=string,DefaultValue=\"\",AlwaysChange,Version=2.0,GenerateAccessors,Validator=NonNullValidator */
510 const static int PasswordProperty;
511 /* @PropertyType=string,DefaultValue=\"\",Version=2.0,ManagedFieldAccess=Internal,GenerateAccessors */
512 const static int SelectedTextProperty;
513 /* @PropertyType=Brush,Version=2.0,GenerateAccessors */
514 const static int SelectionBackgroundProperty;
515 /* @PropertyType=Brush,Version=2.0,GenerateAccessors */
516 const static int SelectionForegroundProperty;
517 /* @PropertyType=gint32,DefaultValue=0,Version=2.0,ManagedFieldAccess=Internal,GenerateAccessors,Validator=PositiveIntValidator */
518 const static int SelectionLengthProperty;
519 /* @PropertyType=gint32,DefaultValue=0,Version=2.0,ManagedFieldAccess=Internal,GenerateAccessors,Validator=PositiveIntValidator */
520 const static int SelectionStartProperty;
522 /* @GenerateCBinding,GeneratePInvoke */
523 PasswordBox ();
526 // Overrides
528 virtual void OnPropertyChanged (PropertyChangedEventArgs *args, MoonError *error);
529 virtual void OnSubPropertyChanged (DependencyProperty *prop, DependencyObject *obj, PropertyChangedEventArgs *subobj_args);
531 virtual const char *GetDisplayText ();
534 // Property Accesors
536 void SetCaretBrush (Brush *caret);
537 virtual Brush *GetCaretBrush ();
539 void SetFontSource (FontSource *source);
540 FontSource *GetFontSource ();
542 void SetMaxLength (int length);
543 int GetMaxLength ();
545 void SetPassword (const char *password);
546 const char *GetPassword ();
548 void SetPasswordChar (gunichar c);
549 gunichar GetPasswordChar ();
551 void SetSelectionBackground (Brush *background);
552 virtual Brush *GetSelectionBackground ();
554 void SetSelectionForeground (Brush *foreground);
555 virtual Brush *GetSelectionForeground ();
558 // Events
560 /* @DelegateType=RoutedEventHandler */
561 const static int PasswordChangedEvent;
565 /* @Namespace=Microsoft.Internal */
566 class TextBoxView : public FrameworkElement {
567 TextBoxBase *textbox;
568 glong blink_timeout;
569 TextLayout *layout;
570 Rect cursor;
572 int selection_changed:1;
573 int had_selected_text:1;
574 int cursor_visible:1;
575 int dirty:1;
577 // mouse events
578 static void mouse_left_button_down (EventObject *sender, EventArgs *args, gpointer closure);
579 static void mouse_left_button_up (EventObject *sender, EventArgs *args, gpointer closure);
580 void OnMouseLeftButtonDown (MouseButtonEventArgs *args);
581 void OnMouseLeftButtonUp (MouseButtonEventArgs *args);
583 // TextBox events
584 static void model_changed (EventObject *sender, EventArgs *args, gpointer closure);
585 void OnModelChanged (TextBoxModelChangedEventArgs *args);
587 // cursor blink
588 static gboolean blink (void *user_data);
589 void ConnectBlinkTimeout (guint multiplier);
590 void DisconnectBlinkTimeout ();
591 void ResetCursorBlink (bool delay);
592 void DelayCursorBlink ();
593 void BeginCursorBlink ();
594 void EndCursorBlink ();
595 void ShowCursor ();
596 void HideCursor ();
597 bool Blink ();
599 void UpdateCursor (bool invalidate);
600 void InvalidateCursor ();
601 void UpdateText ();
603 void Layout (Size constraint);
604 void Paint (cairo_t *cr);
606 protected:
607 virtual ~TextBoxView ();
609 public:
610 /* @GenerateCBinding,GeneratePInvoke */
611 TextBoxView ();
614 // Overrides
616 virtual void Render (cairo_t *cr, Region *region, bool path_only = false);
617 virtual void GetSizeForBrush (cairo_t *cr, double *width, double *height);
618 virtual Size ComputeActualSize ();
619 virtual Size MeasureOverride (Size availableSize);
620 virtual Size ArrangeOverride (Size finalSize);
623 // Methods
625 int GetLineCount () { return layout->GetLineCount (); }
626 TextLayoutLine *GetLineFromY (double y, int *index = NULL);
627 TextLayoutLine *GetLineFromIndex (int index);
629 int GetCursorFromXY (double x, double y);
630 Rect GetCursor () { return cursor; }
632 void OnLostFocus ();
633 void OnGotFocus ();
636 // Property Accessors
638 TextBoxBase *GetTextBox () { return textbox; }
639 void SetTextBox (TextBoxBase *textbox);
642 #endif /* __TEXTBOX_H__ */