forked from PintaProject/Pinta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextTool.cs
1240 lines (986 loc) · 35.4 KB
/
TextTool.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/////////////////////////////////////////////////////////////////////////////////
// Paint.NET //
// Copyright (C) dotPDN LLC, Rick Brewster, Tom Jackson, and contributors. //
// Portions Copyright (C) Microsoft Corporation. All Rights Reserved. //
// See license-pdn.txt for full licensing and attribution details. //
// //
// Ported to Pinta by: Olivier Dufour <[email protected]> //
// Jonathan Pobst <[email protected]> //
/////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Text;
using Gdk;
using Gtk;
using Pinta.Core;
namespace Pinta.Tools
{
public class TextTool : BaseTool
{
// Variables for dragging
private Cairo.PointD startMouseXY;
private Point startClickPoint;
private bool tracking;
private Gdk.Cursor cursor_hand;
private Point clickPoint;
private bool is_editing;
private Rectangle old_cursor_bounds = Rectangle.Zero;
//This is used to temporarily store the UserLayer's and TextLayer's previous ImageSurface states.
private Cairo.ImageSurface text_undo_surface;
private Cairo.ImageSurface user_undo_surface;
private TextEngine undo_engine;
// The selection from when editing started. This ensures that text doesn't suddenly disappear/appear
// if the selection changes before the text is finalized.
private DocumentSelection selection;
private Gtk.IMMulticontext imContext;
private Pinta.Core.TextLayout layout;
private Rectangle CurrentTextBounds
{
get
{
return PintaCore.Workspace.ActiveDocument.CurrentUserLayer.textBounds;
}
set
{
PintaCore.Workspace.ActiveDocument.CurrentUserLayer.previousTextBounds = PintaCore.Workspace.ActiveDocument.CurrentUserLayer.textBounds;
PintaCore.Workspace.ActiveDocument.CurrentUserLayer.textBounds = value;
}
}
private TextEngine CurrentTextEngine
{
get
{
return PintaCore.Workspace.HasOpenDocuments ?
PintaCore.Workspace.ActiveDocument.CurrentUserLayer.tEngine : null;
}
set
{
PintaCore.Workspace.ActiveDocument.CurrentUserLayer.tEngine = value;
}
}
private Pinta.Core.TextLayout CurrentTextLayout
{
get {
if (layout.Engine != CurrentTextEngine)
layout.Engine = CurrentTextEngine;
return layout;
}
}
//While this is true, text will not be finalized upon Surface.Clone calls.
private bool ignoreCloneFinalizations = false;
//Whether or not either (or both) of the Ctrl keys are pressed.
private bool ctrlKey = false;
//Store the most recent mouse position.
private Point lastMousePosition = new Point(0, 0);
//Whether or not the previous TextTool mouse cursor shown was the normal one.
private bool previousMouseCursorNormal = true;
public override string Name { get { return Translations.GetString ("Text"); } }
private string FinalizeName { get { return Translations.GetString("Text - Finalize"); } }
public override string Icon { get { return "Tools.Text.png"; } }
public override Gdk.Key ShortcutKey { get { return Gdk.Key.T; } }
public override int Priority { get { return 37; } }
public override string StatusBarText {
get { return Translations.GetString ("Left click to place cursor, then type desired text. Text color is primary color."); }
}
public override Gdk.Cursor DefaultCursor {
get {
return new Gdk.Cursor (Gdk.Display.Default,
PintaCore.Resources.GetIcon ("Cursor.Text.png"),
16, 16);
}
}
public Gdk.Cursor InvalidEditCursor { get { return new Gdk.Cursor (Gdk.Display.Default, PintaCore.Resources.GetIcon ("Menu.Edit.EraseSelection.png"), 8, 0); } }
#region Constructor
public TextTool ()
{
cursor_hand = new Gdk.Cursor (Gdk.Display.Default, PintaCore.Resources.GetIcon ("Cursor.Pan.png"), 8, 8);
imContext = new Gtk.IMMulticontext ();
imContext.Commit += OnIMCommit;
layout = new Pinta.Core.TextLayout ();
}
static TextTool ()
{
Gtk.IconFactory fact = new Gtk.IconFactory ();
fact.Add ("ShapeTool.Outline.png", new Gtk.IconSet (PintaCore.Resources.GetIcon ("ShapeTool.Outline.png")));
fact.Add ("ShapeTool.Fill.png", new Gtk.IconSet (PintaCore.Resources.GetIcon ("ShapeTool.Fill.png")));
fact.Add ("ShapeTool.OutlineFill.png", new Gtk.IconSet (PintaCore.Resources.GetIcon ("ShapeTool.OutlineFill.png")));
fact.Add ("TextTool.FillBackground.png", new Gtk.IconSet (PintaCore.Resources.GetIcon ("TextTool.FillBackground.png")));
fact.AddDefault ();
}
#endregion
#region ToolBar
private ToolBarLabel font_label;
private ToolBarFontComboBox font_combo;
private ToolBarComboBox size_combo;
private ToolBarToggleButton bold_btn;
private ToolBarToggleButton italic_btn;
private ToolBarToggleButton underscore_btn;
private ToolBarToggleButton left_alignment_btn;
private ToolBarToggleButton center_alignment_btn;
private ToolBarToggleButton Right_alignment_btn;
private ToolBarLabel spacer_label;
private ToolBarLabel fill_label;
private ToolBarDropDownButton fill_button;
private SeparatorToolItem fill_sep;
private SeparatorToolItem outline_sep;
private ToolBarComboBox outline_width;
private ToolBarLabel outline_width_label;
private ToolBarButton outline_width_minus;
private ToolBarButton outline_width_plus;
protected override void OnBuildToolBar (Gtk.Toolbar tb)
{
base.OnBuildToolBar (tb);
if (font_label == null)
font_label = new ToolBarLabel (string.Format (" {0}: ", Translations.GetString ("Font")));
tb.AppendItem (font_label);
if (font_combo == null) {
var fonts = PintaCore.System.Fonts.GetInstalledFonts ();
fonts.Sort ();
// Default to Arial or first in list
int index = Math.Max (fonts.IndexOf ("Arial"), 0);
font_combo = new ToolBarFontComboBox (150, index, fonts.ToArray ());
font_combo.ComboBox.Changed += HandleFontChanged;
}
tb.AppendItem (font_combo);
if (spacer_label == null)
spacer_label = new ToolBarLabel (" ");
tb.AppendItem (spacer_label);
if (size_combo == null) {
size_combo = new ToolBarComboBox (65, 0, true);
size_combo.ComboBox.Changed += HandleSizeChanged;
}
tb.AppendItem (size_combo);
tb.AppendItem (new SeparatorToolItem ());
if (bold_btn == null) {
bold_btn = new ToolBarToggleButton ("Toolbar.Bold.png", Translations.GetString ("Bold"), Translations.GetString ("Bold"));
bold_btn.Toggled += HandleBoldButtonToggled;
}
tb.AppendItem (bold_btn);
if (italic_btn == null) {
italic_btn = new ToolBarToggleButton ("Toolbar.Italic.png", Translations.GetString ("Italic"), Translations.GetString ("Italic"));
italic_btn.Toggled += HandleItalicButtonToggled;
}
tb.AppendItem (italic_btn);
if (underscore_btn == null) {
underscore_btn = new ToolBarToggleButton ("Toolbar.Underline.png", Translations.GetString ("Underline"), Translations.GetString ("Underline"));
underscore_btn.Toggled += HandleUnderscoreButtonToggled;
}
tb.AppendItem (underscore_btn);
tb.AppendItem (new SeparatorToolItem ());
if (left_alignment_btn == null) {
left_alignment_btn = new ToolBarToggleButton ("Toolbar.LeftAlignment.png", Translations.GetString ("Left Align"), Translations.GetString ("Left Align"));
left_alignment_btn.Active = true;
left_alignment_btn.Toggled += HandleLeftAlignmentButtonToggled;
}
tb.AppendItem (left_alignment_btn);
if (center_alignment_btn == null) {
center_alignment_btn = new ToolBarToggleButton ("Toolbar.CenterAlignment.png", Translations.GetString ("Center Align"), Translations.GetString ("Center Align"));
center_alignment_btn.Toggled += HandleCenterAlignmentButtonToggled;
}
tb.AppendItem (center_alignment_btn);
if (Right_alignment_btn == null) {
Right_alignment_btn = new ToolBarToggleButton ("Toolbar.RightAlignment.png", Translations.GetString ("Right Align"), Translations.GetString ("Right Align"));
Right_alignment_btn.Toggled += HandleRightAlignmentButtonToggled;
}
tb.AppendItem (Right_alignment_btn);
if (fill_sep == null)
fill_sep = new Gtk.SeparatorToolItem ();
tb.AppendItem (fill_sep);
if (fill_label == null)
fill_label = new ToolBarLabel (string.Format (" {0}: ", Translations.GetString ("Text Style")));
tb.AppendItem (fill_label);
if (fill_button == null) {
fill_button = new ToolBarDropDownButton ();
fill_button.AddItem (Translations.GetString ("Normal"), "ShapeTool.Fill.png", 0);
fill_button.AddItem (Translations.GetString ("Normal and Outline"), "ShapeTool.OutlineFill.png", 1);
fill_button.AddItem (Translations.GetString ("Outline"), "ShapeTool.Outline.png", 2);
fill_button.AddItem (Translations.GetString ("Fill Background"), "TextTool.FillBackground.png", 3);
fill_button.SelectedItemChanged += HandleBoldButtonToggled;
}
tb.AppendItem (fill_button);
if (outline_sep == null)
outline_sep = new SeparatorToolItem ();
tb.AppendItem (outline_sep);
if (outline_width_label == null)
outline_width_label = new ToolBarLabel (string.Format (" {0}: ", Translations.GetString ("Outline width")));
tb.AppendItem (outline_width_label);
if (outline_width_minus == null) {
outline_width_minus = new ToolBarButton ("Toolbar.MinusButton.png", "", Translations.GetString ("Decrease outline size"));
outline_width_minus.Clicked += MinusButtonClickedEvent;
}
tb.AppendItem (outline_width_minus);
if (outline_width == null) {
outline_width = new ToolBarComboBox (65, 1, true, "1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "11", "12", "13", "14", "15", "20", "25", "30", "35",
"40", "45", "50", "55");
outline_width.ComboBox.Changed += HandleSizeChanged;
}
tb.AppendItem (outline_width);
if (outline_width_plus == null) {
outline_width_plus = new ToolBarButton ("Toolbar.PlusButton.png", "", Translations.GetString ("Increase outline size"));
outline_width_plus.Clicked += PlusButtonClickedEvent;
}
tb.AppendItem (outline_width_plus);
outline_width_plus.Visible = outline_width_minus.Visible = outline_width.Visible
= outline_width_label.Visible = outline_sep.Visible = StrokeText;
UpdateFontSizes ();
if (PintaCore.Workspace.HasOpenDocuments) {
//Make sure the event handler is never added twice.
PintaCore.Workspace.ActiveDocument.LayerCloned -= FinalizeText;
//When an ImageSurface is Cloned, finalize the re-editable text (if applicable).
PintaCore.Workspace.ActiveDocument.LayerCloned += FinalizeText;
}
}
private void HandleFontChanged (object sender, EventArgs e)
{
if (PintaCore.Workspace.HasOpenDocuments)
PintaCore.Workspace.ActiveDocument.Workspace.Canvas.GrabFocus ();
UpdateFontSizes ();
UpdateFont ();
}
private void UpdateFontSizes ()
{
string oldval = size_combo.ComboBox.ActiveText;
ListStore model = (ListStore)size_combo.ComboBox.Model;
model.Clear ();
List<int> sizes = PintaCore.System.Fonts.GetSizes (FontFamily);
foreach (int i in sizes)
size_combo.ComboBox.AppendText (i.ToString ());
int index;
if (string.IsNullOrEmpty (oldval))
index = sizes.IndexOf (12);
else
index = sizes.IndexOf (int.Parse (oldval));
if (index == -1)
index = 0;
size_combo.ComboBox.Active = index;
}
private void HandleSizeChanged (object sender, EventArgs e)
{
string text = size_combo.ComboBox.ActiveText;
if (int.TryParse (text, out FontSize))
UpdateFont ();
}
private Pango.FontFamily FontFamily {
get { return PintaCore.System.Fonts.GetFamily (font_combo.ComboBox.ActiveText); }
}
private int FontSize;
private TextAlignment Alignment {
get {
if (Right_alignment_btn.Active)
return TextAlignment.Right;
else if (center_alignment_btn.Active)
return TextAlignment.Center;
else
return TextAlignment.Left;
}
}
private string Font {
get { return font_combo.ComboBox.ActiveText; }
}
private void HandlePintaCorePalettePrimaryColorChanged (object sender, EventArgs e)
{
if (is_editing)
RedrawText (true, true);
}
private void HandleLeftAlignmentButtonToggled (object sender, EventArgs e)
{
if (left_alignment_btn.Active) {
Right_alignment_btn.Active = false;
center_alignment_btn.Active = false;
} else if (!Right_alignment_btn.Active && !center_alignment_btn.Active) {
left_alignment_btn.Active = true;
}
UpdateFont ();
}
private void HandleCenterAlignmentButtonToggled (object sender, EventArgs e)
{
if (center_alignment_btn.Active) {
Right_alignment_btn.Active = false;
left_alignment_btn.Active = false;
} else if (!Right_alignment_btn.Active && !left_alignment_btn.Active) {
center_alignment_btn.Active = true;
}
UpdateFont ();
}
private void HandleRightAlignmentButtonToggled (object sender, EventArgs e)
{
if (Right_alignment_btn.Active) {
center_alignment_btn.Active = false;
left_alignment_btn.Active = false;
} else if (!center_alignment_btn.Active && !left_alignment_btn.Active) {
Right_alignment_btn.Active = true;
}
UpdateFont ();
}
private void HandleUnderscoreButtonToggled (object sender, EventArgs e)
{
UpdateFont ();
}
private void HandleItalicButtonToggled (object sender, EventArgs e)
{
UpdateFont ();
}
private void HandleBoldButtonToggled (object sender, EventArgs e)
{
outline_width_plus.Visible = outline_width_minus.Visible = outline_width.Visible
= outline_width_label.Visible = outline_sep.Visible = StrokeText;
UpdateFont ();
}
private void HandleSelectedLayerChanged(object sender, EventArgs e)
{
UpdateFont();
}
private void UpdateFont ()
{
if (CurrentTextEngine != null) {
CurrentTextEngine.Alignment = Alignment;
CurrentTextEngine.SetFont (Font, FontSize, bold_btn.Active, italic_btn.Active, underscore_btn.Active);
}
if (is_editing)
RedrawText (true, true);
}
protected virtual void MinusButtonClickedEvent (object o, EventArgs args)
{
if (OutlineWidth > 1)
OutlineWidth--;
}
protected virtual void PlusButtonClickedEvent (object o, EventArgs args)
{
OutlineWidth++;
}
protected int OutlineWidth {
get {
int width;
if (Int32.TryParse (outline_width.ComboBox.ActiveText, out width)) {
if (width > 0) {
outline_width.ComboBox.Entry.Text = width.ToString ();
return width;
}
}
outline_width.ComboBox.Entry.Text = "2";
return 2;
}
set { outline_width.ComboBox.Entry.Text = value.ToString (); }
}
protected bool StrokeText { get { return ((int)fill_button.SelectedItem.Tag >= 1 && (int)fill_button.SelectedItem.Tag != 3); } }
protected bool FillText { get { return (int)fill_button.SelectedItem.Tag <= 1 || (int)fill_button.SelectedItem.Tag == 3; } }
protected bool BackgroundFill { get { return (int)fill_button.SelectedItem.Tag == 3; } }
#endregion
#region Activation/Deactivation
protected override void OnActivated ()
{
base.OnActivated ();
// We may need to redraw our text when the color changes
PintaCore.Palette.PrimaryColorChanged += HandlePintaCorePalettePrimaryColorChanged;
PintaCore.Palette.SecondaryColorChanged += HandlePintaCorePalettePrimaryColorChanged;
PintaCore.Layers.LayerAdded += HandleSelectedLayerChanged;
PintaCore.Layers.LayerRemoved += HandleSelectedLayerChanged;
PintaCore.Layers.SelectedLayerChanged += HandleSelectedLayerChanged;
// We always start off not in edit mode
is_editing = false;
}
protected override void OnCommit ()
{
StopEditing(false);
}
protected override void OnDeactivated(BaseTool newTool)
{
base.OnDeactivated (newTool);
// Stop listening for color change events
PintaCore.Palette.PrimaryColorChanged -= HandlePintaCorePalettePrimaryColorChanged;
PintaCore.Palette.SecondaryColorChanged -= HandlePintaCorePalettePrimaryColorChanged;
PintaCore.Layers.LayerAdded -= HandleSelectedLayerChanged;
PintaCore.Layers.LayerRemoved -= HandleSelectedLayerChanged;
PintaCore.Layers.SelectedLayerChanged -= HandleSelectedLayerChanged;
StopEditing(false);
}
#endregion
#region Mouse Handlers
protected override void OnMouseDown(DrawingArea canvas, ButtonPressEventArgs args, Cairo.PointD point)
{
ctrlKey = (args.Event.State & ModifierType.ControlMask) != 0;
//Store the mouse position.
Point pt = point.ToGdkPoint();
// Grab focus so we can get keystrokes
canvas.GrabFocus ();
if (selection != null)
selection.Dispose ();
selection = PintaCore.Workspace.ActiveDocument.Selection.Clone ();
// A right click allows you to move the text around
if (args.Event.Button == 3)
{
//The user is dragging text with the right mouse button held down, so track the mouse as it moves.
tracking = true;
//Remember the position of the mouse before the text is dragged.
startMouseXY = point;
startClickPoint = clickPoint;
//Change the cursor to indicate that the text is being dragged.
SetCursor(cursor_hand);
return;
}
// The user clicked the left mouse button
if (args.Event.Button == 1)
{
// If the user is [editing or holding down Ctrl] and clicked
//within the text, move the cursor to the click location
if ((is_editing || ctrlKey) && CurrentTextBounds.ContainsCorrect(pt))
{
StartEditing();
//Change the position of the cursor to where the mouse clicked.
TextPosition p = CurrentTextLayout.PointToTextPosition(pt);
CurrentTextEngine.SetCursorPosition(p, true);
//Redraw the text with the new cursor position.
RedrawText(true, true);
return;
}
// We're already editing and the user clicked outside the text,
// commit the user's work, and start a new edit
switch (CurrentTextEngine.State)
{
// We were editing, save and stop
case TextMode.Uncommitted:
StopEditing(true);
break;
// We were editing, but nothing had been
// keyed. Stop editing.
case TextMode.Unchanged:
StopEditing(false);
break;
}
if (ctrlKey)
{
//Go through every UserLayer.
foreach (UserLayer ul in PintaCore.Workspace.ActiveDocument.UserLayers)
{
//Check each UserLayer's editable text boundaries to see if they contain the mouse position.
if (ul.textBounds.ContainsCorrect(pt))
{
//The mouse clicked on editable text.
//Change the current UserLayer to the Layer that contains the text that was clicked on.
PintaCore.Workspace.ActiveDocument.SetCurrentUserLayer(ul);
//The user is editing text now.
is_editing = true;
//Set the cursor in the editable text where the mouse was clicked.
TextPosition p = CurrentTextLayout.PointToTextPosition(pt);
CurrentTextEngine.SetCursorPosition(p, true);
//Redraw the editable text with the cursor.
RedrawText(true, true);
//Don't check any more UserLayers - stop at the first UserLayer that has editable text containing the mouse position.
return;
}
}
}
else
{
if (CurrentTextEngine.State == TextMode.NotFinalized)
{
//The user is making a new text and the old text hasn't been finalized yet.
FinalizeText();
}
if (!is_editing)
{
// Start editing at the cursor location
clickPoint = pt;
CurrentTextEngine.Clear();
clickPoint.Offset (0, -CurrentTextLayout.FontHeight/2);
CurrentTextEngine.Origin = clickPoint;
StartEditing();
RedrawText(true, true);
}
}
}
}
protected override void OnMouseMove (object o, MotionNotifyEventArgs args, Cairo.PointD point)
{
ctrlKey = (args.Event.State & ModifierType.ControlMask) != 0;
lastMousePosition = point.ToGdkPoint();
// If we're dragging the text around, do that
if (tracking)
{
Cairo.PointD delta = new Cairo.PointD(point.X - startMouseXY.X, point.Y - startMouseXY.Y);
clickPoint = new Point((int)(startClickPoint.X + delta.X), (int)(startClickPoint.Y + delta.Y));
CurrentTextEngine.Origin = clickPoint;
RedrawText(true, true);
}
else
{
UpdateMouseCursor();
}
}
protected override void OnMouseUp (Gtk.DrawingArea canvas, Gtk.ButtonReleaseEventArgs args, Cairo.PointD point)
{
// If we were dragging the text around, finish that up
if (tracking) {
Cairo.PointD delta = new Cairo.PointD (point.X - startMouseXY.X, point.Y - startMouseXY.Y);
clickPoint = new Point ((int)(startClickPoint.X + delta.X), (int)(startClickPoint.Y + delta.Y));
CurrentTextEngine.Origin = clickPoint;
RedrawText (false, true);
tracking = false;
SetCursor (null);
}
}
private void UpdateMouseCursor()
{
//Whether or not to show the normal text cursor.
bool showNormalCursor = false;
if (ctrlKey && PintaCore.Workspace.HasOpenDocuments)
{
//Go through every UserLayer.
foreach (UserLayer ul in PintaCore.Workspace.ActiveDocument.UserLayers)
{
//Check each UserLayer's editable text boundaries to see if they contain the mouse position.
if (ul.textBounds.ContainsCorrect(lastMousePosition))
{
//The mouse is over editable text.
showNormalCursor = true;
}
}
}
else
{
showNormalCursor = true;
}
if (showNormalCursor)
{
if (!previousMouseCursorNormal)
{
SetCursor(DefaultCursor);
previousMouseCursorNormal = showNormalCursor;
if (PintaCore.Workspace.HasOpenDocuments)
RedrawText(is_editing, true);
}
}
else
{
if (previousMouseCursorNormal)
{
SetCursor(InvalidEditCursor);
previousMouseCursorNormal = showNormalCursor;
RedrawText(is_editing, true);
}
}
}
#endregion
#region Keyboard Handlers
protected override void OnKeyDown (DrawingArea canvas, KeyPressEventArgs args)
{
if (!PintaCore.Workspace.HasOpenDocuments) {
args.RetVal = false;
return;
}
Gdk.ModifierType modifier = args.Event.State;
// If we are dragging the text, we
// aren't going to handle key presses
if (tracking)
return;
// Ignore anything with Alt pressed
if ((modifier & Gdk.ModifierType.Mod1Mask) != 0)
return;
ctrlKey = (args.Event.Key == Gdk.Key.Control_L || args.Event.Key == Gdk.Key.Control_R);
UpdateMouseCursor ();
// Assume that we are going to handle the key
bool keyHandled = true;
if (is_editing)
{
switch (args.Event.Key)
{
case Gdk.Key.BackSpace:
CurrentTextEngine.PerformBackspace();
break;
case Gdk.Key.Delete:
CurrentTextEngine.PerformDelete();
break;
case Gdk.Key.KP_Enter:
case Gdk.Key.Return:
CurrentTextEngine.PerformEnter();
break;
case Gdk.Key.Left:
CurrentTextEngine.PerformLeft((modifier & Gdk.ModifierType.ControlMask) != 0, (modifier & Gdk.ModifierType.ShiftMask) != 0);
break;
case Gdk.Key.Right:
CurrentTextEngine.PerformRight((modifier & Gdk.ModifierType.ControlMask) != 0, (modifier & Gdk.ModifierType.ShiftMask) != 0);
break;
case Gdk.Key.Up:
CurrentTextEngine.PerformUp((modifier & Gdk.ModifierType.ShiftMask) != 0);
break;
case Gdk.Key.Down:
CurrentTextEngine.PerformDown((modifier & Gdk.ModifierType.ShiftMask) != 0);
break;
case Gdk.Key.Home:
CurrentTextEngine.PerformHome((modifier & Gdk.ModifierType.ControlMask) != 0, (modifier & Gdk.ModifierType.ShiftMask) != 0);
break;
case Gdk.Key.End:
CurrentTextEngine.PerformEnd((modifier & Gdk.ModifierType.ControlMask) != 0, (modifier & Gdk.ModifierType.ShiftMask) != 0);
break;
case Gdk.Key.Next:
case Gdk.Key.Prior:
break;
case Gdk.Key.Escape:
StopEditing(false);
return;
case Gdk.Key.Insert:
if (modifier.IsShiftPressed ())
{
Gtk.Clipboard cb = Gtk.Clipboard.Get(Gdk.Atom.Intern("CLIPBOARD", false));
CurrentTextEngine.PerformPaste(cb);
}
else if (modifier.IsControlPressed ())
{
Gtk.Clipboard cb = Gtk.Clipboard.Get(Gdk.Atom.Intern("CLIPBOARD", false));
CurrentTextEngine.PerformCopy(cb);
}
break;
default:
if (modifier.IsControlPressed ())
{
if (args.Event.Key == Gdk.Key.z)
{
//Ctrl + Z for undo while editing.
TryHandleUndo();
if (PintaCore.Workspace.ActiveDocument.History.CanUndo)
PintaCore.Workspace.ActiveDocument.History.Undo();
return;
}
else if (args.Event.Key == Gdk.Key.i)
{
italic_btn.Toggle ();
UpdateFont ();
}
else if (args.Event.Key == Gdk.Key.b)
{
bold_btn.Toggle ();
UpdateFont ();
}
else if (args.Event.Key == Gdk.Key.u)
{
underscore_btn.Toggle ();
UpdateFont ();
}
else if (args.Event.Key == Gdk.Key.a)
{
// Select all of the text.
CurrentTextEngine.PerformHome (false, false);
CurrentTextEngine.PerformEnd (true, true);
}
else
{
//Ignore command shortcut.
return;
}
}
else
{
keyHandled = TryHandleChar(args.Event);
}
break;
}
// If we processed a key, update the display
if (keyHandled)
{
RedrawText(true, true);
}
}
else
{
// If we're not editing, allow the key press to be handled elsewhere (e.g. for selecting another tool).
keyHandled = false;
}
args.RetVal = keyHandled;
}
protected override void OnKeyUp(DrawingArea canvas, KeyReleaseEventArgs args)
{
if (args.Event.Key == Gdk.Key.Control_L || args.Event.Key == Gdk.Key.Control_R ||
(args.Event.State & ModifierType.ControlMask) != 0)
{
ctrlKey = false;
UpdateMouseCursor();
}
}
private bool TryHandleChar(EventKey eventKey)
{
// Try to handle it as a character
if (imContext.FilterKeypress (eventKey)) {
return true;
}
// We didn't handle the key
return false;
}
private void OnIMCommit (object o, CommitArgs args)
{
try {
var str = new StringBuilder ();
for (int i = 0; i < args.Str.Length; i++) {
char utf32Char;
if (char.IsHighSurrogate (args.Str, i)) {
utf32Char = (char)char.ConvertToUtf32 (args.Str, i);
i++;
} else {
utf32Char = args.Str[i];
}
str.Append (utf32Char.ToString ());
}
CurrentTextEngine.InsertText (str.ToString ());
} finally {
imContext.Reset ();
}
}
#endregion
#region Start/Stop Editing
private void StartEditing ()
{
is_editing = true;
imContext.ClientWindow = PintaCore.Workspace.ActiveWorkspace.Canvas.GdkWindow;
if (selection == null)
selection = PintaCore.Workspace.ActiveDocument.Selection.Clone ();
//Start ignoring any Surface.Clone calls from this point on (so that it doesn't start to loop).
ignoreCloneFinalizations = true;
//Store the previous state of the current UserLayer's and TextLayer's ImageSurfaces.
user_undo_surface = PintaCore.Workspace.ActiveDocument.CurrentUserLayer.Surface.Clone();
text_undo_surface = PintaCore.Workspace.ActiveDocument.CurrentUserLayer.TextLayer.Layer.Surface.Clone();
//Store the previous state of the Text Engine.
undo_engine = CurrentTextEngine.Clone();
//Stop ignoring any Surface.Clone calls from this point on.
ignoreCloneFinalizations = false;
}
private void StopEditing(bool finalize)
{
imContext.ClientWindow = null;
if (!PintaCore.Workspace.HasOpenDocuments)
return;
if (!is_editing)
return;
is_editing = false;
//Make sure that neither undo surface is null, the user is editing, and there are uncommitted changes.
if (text_undo_surface != null && user_undo_surface != null && CurrentTextEngine.State == TextMode.Uncommitted)
{
Document doc = PintaCore.Workspace.ActiveDocument;
RedrawText(false, true);
//Start ignoring any Surface.Clone calls from this point on (so that it doesn't start to loop).
ignoreCloneFinalizations = true;
//Create a new TextHistoryItem so that the committing of text can be undone.
doc.History.PushNewItem(new TextHistoryItem(Icon, Name,
text_undo_surface.Clone(), user_undo_surface.Clone(),
undo_engine.Clone(), doc.CurrentUserLayer));
//Stop ignoring any Surface.Clone calls from this point on.
ignoreCloneFinalizations = false;
//Now that the text has been committed, change its state.
CurrentTextEngine.State = TextMode.NotFinalized;
}
RedrawText(false, true);
if (finalize)
{
FinalizeText();
}
}
#endregion
#region Text Drawing Methods
/// <summary>
/// Clears the entire TextLayer and redraw the previous text boundary.
/// </summary>
private void ClearTextLayer()
{
//Clear the TextLayer.
PintaCore.Workspace.ActiveDocument.CurrentUserLayer.TextLayer.Layer.Surface.Clear();
//Redraw the previous text boundary.
InflateAndInvalidate(PintaCore.Workspace.ActiveDocument.CurrentUserLayer.previousTextBounds);
}
/// <summary>
/// Draws the text.
/// </summary>
/// <param name="showCursor">Whether or not to show the mouse cursor in the drawing.</param>
/// <param name="useTextLayer">Whether or not to use the TextLayer (as opposed to the Userlayer).</param>
private void RedrawText (bool showCursor, bool useTextLayer)
{
Rectangle r = CurrentTextLayout.GetLayoutBounds();
r.Inflate(10 + OutlineWidth, 10 + OutlineWidth);
InflateAndInvalidate(r);
CurrentTextBounds = r;