1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
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.
19 #include "fontsource.h"
20 #include "moon-path.h"
21 #include "eventargs.h"
22 #include "thickness.h"
30 /* @Namespace=System.Windows.Input */
31 class InputMethod
: public DependencyObject
{
33 virtual ~InputMethod () {}
36 /* @PropertyType=bool,Attached,DefaultValue=true,Validator=IsInputMethodEnabledValidator */
37 const static int IsInputMethodEnabledProperty
;
39 /* @ManagedAccess=Internal,GeneratePInvoke,GenerateCBinding */
40 InputMethod () { SetObjectType (Type::INPUTMETHOD
); }
44 class TextChangedEventArgs
: public RoutedEventArgs
{
46 virtual ~TextChangedEventArgs () { }
49 /* @GenerateCBinding,GeneratePInvoke */
50 TextChangedEventArgs () { SetObjectType (Type::TEXTCHANGEDEVENTARGS
); }
55 class CursorPositionChangedEventArgs
: public EventArgs
{
59 virtual ~CursorPositionChangedEventArgs () { }
62 /* @GenerateCBinding,GeneratePInvoke */
63 CursorPositionChangedEventArgs ()
65 SetObjectType (Type::CURSORPOSITIONCHANGEDEVENTARGS
);
71 CursorPositionChangedEventArgs (double height
, double x
, double y
)
73 SetObjectType (Type::CURSORPOSITIONCHANGEDEVENTARGS
);
74 this->height
= height
;
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
{
104 virtual ~TextBoxModelChangedEventArgs () { }
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
;
119 class TextBoxUndoStack
;
121 /* @Namespace=None */
122 class TextBoxBase
: public Control
, public ITextAttributes
{
124 friend class TextBoxView
;
126 DependencyObject
*contentElement
;
128 TextFontDescription
*font
;
129 GPtrArray
*downloaders
;
132 TextBoxUndoStack
*undo
;
133 TextBoxUndoStack
*redo
;
134 int selection_anchor
;
135 int selection_cursor
;
136 double cursor_offset
;
137 GtkIMContext
*im_ctx
;
142 short accepts_return
:1;
143 short need_im_reset
:1;
144 short is_read_only
:1;
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
);
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
);
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;
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
);
248 const static int ModelChangedEvent
;
251 // Initialization/Destruction
253 void Initialize (Type::Kind type
, const char *type_name
);
254 virtual ~TextBoxBase ();
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
)
276 return GetSelectionBackground ();
281 virtual Brush
*Foreground (bool selected
)
284 return GetSelectionForeground ();
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
317 // Selection Operations
319 /* @GenerateCBinding,GeneratePInvoke */
320 bool SelectWithError (int start
, int length
, MoonError
*error
);
322 /* @GenerateCBinding,GeneratePInvoke */
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;
337 const static int CursorPositionChangedEvent
;
341 class TextBoxDynamicPropertyValueProvider
;
343 /* @Namespace=System.Windows.Controls */
344 class TextBox
: public TextBoxBase
{
345 friend class TextBoxDynamicPropertyValueProvider
;
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 () { }
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 */
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
);
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 ();
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
;
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 ();
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 */
526 virtual void OnPropertyChanged (PropertyChangedEventArgs
*args
, MoonError
*error
);
527 virtual void OnSubPropertyChanged (DependencyProperty
*prop
, DependencyObject
*obj
, PropertyChangedEventArgs
*subobj_args
);
529 virtual const char *GetDisplayText ();
534 void SetCaretBrush (Brush
*caret
);
535 virtual Brush
*GetCaretBrush ();
537 void SetFontSource (FontSource
*source
);
538 FontSource
*GetFontSource ();
540 void SetMaxLength (int length
);
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 ();
558 /* @DelegateType=RoutedEventHandler */
559 const static int PasswordChangedEvent
;
563 /* @Namespace=Microsoft.Internal */
564 class TextBoxView
: public FrameworkElement
{
565 TextBoxBase
*textbox
;
570 int selection_changed
:1;
571 int had_selected_text
:1;
572 int cursor_visible
:1;
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
);
582 static void model_changed (EventObject
*sender
, EventArgs
*args
, gpointer closure
);
583 void OnModelChanged (TextBoxModelChangedEventArgs
*args
);
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 ();
597 void UpdateCursor (bool invalidate
);
598 void InvalidateCursor ();
601 void Layout (Size constraint
);
602 void Paint (cairo_t
*cr
);
605 virtual ~TextBoxView ();
608 /* @GenerateCBinding,GeneratePInvoke */
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
);
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
; }
634 // Property Accessors
636 TextBoxBase
*GetTextBox () { return textbox
; }
637 void SetTextBox (TextBoxBase
*textbox
);
640 #endif /* __TEXTBOX_H__ */