-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathNoteWindow.cs
1730 lines (1443 loc) · 48.5 KB
/
NoteWindow.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
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using Mono.Unix;
using Gtk;
namespace Tomboy
{
public class NoteWindow : ForcedPresentWindow
{
Note note;
Gtk.AccelGroup accel_group;
Gtk.Toolbar toolbar;
Gtk.Tooltips toolbar_tips;
Gtk.ToolButton link_button;
NoteTextMenu text_menu;
Gtk.Menu plugin_menu;
Gtk.ImageMenuItem sync_menu_item;
Gtk.TextView editor;
Gtk.ScrolledWindow editor_window;
NoteFindBar find_bar;
Gtk.ToolButton delete;
Gtk.Box template_widget;
GlobalKeybinder global_keys;
InterruptableTimeout mark_set_timeout;
//
// Construct a window to display a note
//
// Currently a toolbar with Link, Search, Text, Delete buttons
// and a Gtk.TextView as the body.
//
public NoteWindow (Note note)
: base (note.Title)
{
this.note = note;
this.IconName = "tomboy";
this.SetDefaultSize (450, 360);
Resizable = true;
accel_group = new Gtk.AccelGroup ();
AddAccelGroup (accel_group);
text_menu = new NoteTextMenu (accel_group, note.Buffer, note.Buffer.Undoer);
// Add the Find menu item to the toolbar Text menu. It
// should only show up in the toplevel Text menu, since
// the context menu already has a Find submenu.
Gtk.SeparatorMenuItem spacer = new Gtk.SeparatorMenuItem ();
spacer.Show ();
text_menu.Append (spacer);
Gtk.ImageMenuItem find_item =
new Gtk.ImageMenuItem (Catalog.GetString("Find in This Note"));
find_item.Image = new Gtk.Image (Gtk.Stock.Find, Gtk.IconSize.Menu);
find_item.Activated += FindActivate;
find_item.AddAccelerator ("activate",
accel_group,
(uint) Gdk.Key.f,
Gdk.ModifierType.ControlMask,
Gtk.AccelFlags.Visible);
find_item.Show ();
text_menu.Append (find_item);
plugin_menu = MakePluginMenu ();
toolbar = MakeToolbar ();
toolbar.Show ();
template_widget = MakeTemplateBar ();
// The main editor widget
editor = new NoteEditor (note.Buffer);
editor.PopulatePopup += OnPopulatePopup;
editor.Show ();
// Sensitize the Link toolbar button on text selection
mark_set_timeout = new InterruptableTimeout();
mark_set_timeout.Timeout += UpdateLinkButtonSensitivity;
note.Buffer.MarkSet += OnSelectionMarkSet;
// FIXME: I think it would be really nice to let the
// window get bigger up till it grows more than
// 60% of the screen, and then show scrollbars.
editor_window = new Gtk.ScrolledWindow ();
editor_window.HscrollbarPolicy = Gtk.PolicyType.Automatic;
editor_window.VscrollbarPolicy = Gtk.PolicyType.Automatic;
editor_window.Add (editor);
editor_window.Show ();
FocusChild = editor;
find_bar = new NoteFindBar (note);
find_bar.Visible = false;
find_bar.NoShowAll = true;
find_bar.Hidden += FindBarHidden;
Gtk.VBox box = new Gtk.VBox (false, 2);
box.PackStart (toolbar, false, false, 0);
box.PackStart (template_widget, false, false, 0);
box.PackStart (editor_window, true, true, 0);
box.PackStart (find_bar, false, false, 0);
box.Show ();
// Don't set up Ctrl-W or Ctrl-N if Emacs is in use
bool using_emacs = false;
string gtk_key_theme = (string)
Preferences.Get ("/desktop/gnome/interface/gtk_key_theme");
if (gtk_key_theme != null && gtk_key_theme.CompareTo ("Emacs") == 0)
using_emacs = true;
// NOTE: Since some of our keybindings are only
// available in the context menu, and the context menu
// is created on demand, register them with the
// global keybinder
global_keys = new GlobalKeybinder (accel_group);
// Close window (Ctrl-W)
if (!using_emacs)
global_keys.AddAccelerator (new EventHandler (CloseWindowHandler),
(uint) Gdk.Key.w,
Gdk.ModifierType.ControlMask,
Gtk.AccelFlags.Visible);
// Escape has been moved to be handled by a KeyPress Handler so that
// Escape can be used to close the FindBar.
// Close all windows on current Desktop (Ctrl-Q)
global_keys.AddAccelerator (new EventHandler (CloseAllWindowsHandler),
(uint) Gdk.Key.q,
Gdk.ModifierType.ControlMask,
Gtk.AccelFlags.Visible);
// Find Next (Ctrl-G)
global_keys.AddAccelerator (new EventHandler (FindNextActivate),
(uint) Gdk.Key.g,
Gdk.ModifierType.ControlMask,
Gtk.AccelFlags.Visible);
// Find Previous (Ctrl-Shift-G)
global_keys.AddAccelerator (new EventHandler (FindPreviousActivate),
(uint) Gdk.Key.g,
(Gdk.ModifierType.ControlMask |
Gdk.ModifierType.ShiftMask),
Gtk.AccelFlags.Visible);
// Open Help (F1)
global_keys.AddAccelerator (new EventHandler (OpenHelpActivate),
(uint) Gdk.Key.F1,
0,
0);
// Create a new note
if (!using_emacs)
global_keys.AddAccelerator (new EventHandler (CreateNewNote),
(uint) Gdk.Key.n,
Gdk.ModifierType.ControlMask,
Gtk.AccelFlags.Visible);
// Have Esc key close the find bar or note window
KeyPressEvent += KeyPressed;
// Increase Indent
global_keys.AddAccelerator (new EventHandler (ChangeDepthRightHandler),
(uint) Gdk.Key.Right,
Gdk.ModifierType.Mod1Mask,
Gtk.AccelFlags.Visible);
// Decrease Indent
global_keys.AddAccelerator (new EventHandler (ChangeDepthLeftHandler),
(uint) Gdk.Key.Left,
Gdk.ModifierType.Mod1Mask,
Gtk.AccelFlags.Visible);
this.Add (box);
}
protected override bool OnDeleteEvent (Gdk.Event evnt)
{
Preferences.SettingChanged -= Preferences_SettingChanged;
CloseWindowHandler (null, null);
return true;
}
protected override void OnHidden ()
{
base.OnHidden ();
// Workaround Gtk bug, where adding or changing Widgets
// while the Window is hidden causes it to be reshown at
// 0,0...
int x, y;
GetPosition (out x, out y);
Move (x, y);
}
void KeyPressed (object sender, Gtk.KeyPressEventArgs args)
{
args.RetVal = true;
switch (args.Event.Key)
{
case Gdk.Key.Escape:
if (find_bar != null && find_bar.Visible)
find_bar.Hide ();
else if ((bool) Preferences.Get(Preferences.ENABLE_CLOSE_NOTE_ON_ESCAPE))
CloseWindowHandler (null, null);
break;
default:
args.RetVal = false;
break;
}
}
// FIXME: Need to just emit a delete event, and do this work in
// the default delete handler, so that plugins can attach to
// delete event and have it always work.
void CloseWindowHandler (object sender, EventArgs args)
{
// Unmaximize before hiding to avoid reopening
// pseudo-maximized
if ((GdkWindow.State & Gdk.WindowState.Maximized) > 0)
Unmaximize ();
Hide ();
}
[DllImport("libtomboy")]
static extern int tomboy_window_get_workspace (IntPtr win_raw);
void CloseAllWindowsHandler (object sender, EventArgs args)
{
#if WIN32 || MAC
Tomboy.Exit (0);
#else
int workspace = tomboy_window_get_workspace (note.Window.Handle);
foreach (Note iter in note.Manager.Notes) {
if (!iter.IsOpened)
continue;
// Close windows on the same workspace, or all
// open windows if no workspace.
if (workspace < 0 ||
tomboy_window_get_workspace (iter.Window.Handle) == workspace) {
iter.Window.CloseWindowHandler (null, null);
}
}
#endif
}
//
// Delete this Note.
//
void OnDeleteButtonClicked (object sender, EventArgs args)
{
// Prompt for note deletion
List<Note> single_note_list = new List<Note> (1);
single_note_list.Add (note);
NoteUtils.ShowDeletionDialog (single_note_list, this);
}
//
// Public Children Accessors
//
public Gtk.TextView Editor {
get {
return editor;
}
}
public Gtk.Toolbar Toolbar {
get {
return toolbar;
}
}
/// <summary>
/// The Delete Toolbar Button
/// </summary>
public Gtk.ToolButton DeleteButton
{
get {
return delete;
}
}
public Gtk.Menu PluginMenu {
get {
return plugin_menu;
}
}
public Gtk.Menu TextMenu {
get {
return text_menu;
}
}
public Gtk.AccelGroup AccelGroup {
get {
return accel_group;
}
}
//
// Sensitize the Link toolbar button on text selection
//
void OnSelectionMarkSet (object sender, Gtk.MarkSetArgs args)
{
// FIXME: Process in a timeout due to GTK+ bug #172050.
mark_set_timeout.Reset (0);
}
void UpdateLinkButtonSensitivity (object sender, EventArgs args)
{
link_button.Sensitive = (note.Buffer.Selection != null);
}
//
// Right-click menu
//
// Add Undo, Redo, Link, Link To menu, Font menu to the start of
// the editor's context menu.
//
[GLib.ConnectBefore]
void OnPopulatePopup (object sender, Gtk.PopulatePopupArgs args)
{
args.Menu.AccelGroup = accel_group;
Logger.Debug ("Populating context menu...");
// Remove the lame-o gigantic Insert Unicode Control
// Characters menu item.
Gtk.Widget lame_unicode;
lame_unicode = (Gtk.Widget)
args.Menu.Children [args.Menu.Children.Length - 1];
args.Menu.Remove (lame_unicode);
Gtk.MenuItem spacer1 = new Gtk.SeparatorMenuItem ();
spacer1.Show ();
Gtk.ImageMenuItem search = new Gtk.ImageMenuItem (
Catalog.GetString ("_Search All Notes"));
search.Image = new Gtk.Image (Gtk.Stock.Find, Gtk.IconSize.Menu);
search.Activated += SearchActivate;
search.AddAccelerator ("activate",
accel_group,
(uint) Gdk.Key.f,
(Gdk.ModifierType.ControlMask |
Gdk.ModifierType.ShiftMask),
Gtk.AccelFlags.Visible);
search.Show ();
Gtk.ImageMenuItem link =
new Gtk.ImageMenuItem (Catalog.GetString ("_Link to New Note"));
link.Image = new Gtk.Image (Gtk.Stock.JumpTo, Gtk.IconSize.Menu);
link.Sensitive = (note.Buffer.Selection != null);
link.Activated += LinkToNoteActivate;
link.AddAccelerator ("activate",
accel_group,
(uint) Gdk.Key.l,
Gdk.ModifierType.ControlMask,
Gtk.AccelFlags.Visible);
link.Show ();
Gtk.ImageMenuItem text_item =
new Gtk.ImageMenuItem (Catalog.GetString ("Te_xt"));
text_item.Image = new Gtk.Image (Gtk.Stock.SelectFont, Gtk.IconSize.Menu);
text_item.Submenu = new NoteTextMenu (accel_group,
note.Buffer,
note.Buffer.Undoer);
text_item.Show ();
Gtk.ImageMenuItem find_item =
new Gtk.ImageMenuItem (Catalog.GetString ("_Find in This Note"));
find_item.Image = new Gtk.Image (Gtk.Stock.Find, Gtk.IconSize.Menu);
find_item.Submenu = MakeFindMenu ();
find_item.Show ();
Gtk.MenuItem spacer2 = new Gtk.SeparatorMenuItem ();
spacer2.Show ();
args.Menu.Prepend (spacer1);
args.Menu.Prepend (text_item);
args.Menu.Prepend (find_item);
args.Menu.Prepend (link);
args.Menu.Prepend (search);
Gtk.MenuItem close_all =
new Gtk.MenuItem (Catalog.GetString ("Clos_e All Notes"));
close_all.Activated += CloseAllWindowsHandler;
close_all.AddAccelerator ("activate",
accel_group,
(uint) Gdk.Key.q,
Gdk.ModifierType.ControlMask,
Gtk.AccelFlags.Visible);
close_all.Show ();
Gtk.ImageMenuItem close_window =
new Gtk.ImageMenuItem (Catalog.GetString ("_Close"));
close_window.Image = new Gtk.Image (Gtk.Stock.Close, Gtk.IconSize.Menu);
close_window.Activated += CloseWindowHandler;
close_window.AddAccelerator ("activate",
accel_group,
(uint) Gdk.Key.w,
Gdk.ModifierType.ControlMask,
Gtk.AccelFlags.Visible);
close_window.Show ();
args.Menu.Append (close_all);
args.Menu.Append (close_window);
}
//
// Toolbar
//
// Add Link button, Font menu, Delete button to the window's
// toolbar.
//
Gtk.Toolbar MakeToolbar ()
{
Gtk.Toolbar tb = new Gtk.Toolbar ();
tb.Tooltips = true;
toolbar_tips = new Gtk.Tooltips ();
Gtk.ToolButton search = new Gtk.ToolButton (
new Gtk.Image (Gtk.Stock.Find, tb.IconSize),
Catalog.GetString ("Search"));
search.IsImportant = true;
search.Clicked += SearchActivate;
// TODO: If we ever add a way to customize internal keybindings, this will need to change
toolbar_tips.SetTip (search, Catalog.GetString ("Search your notes") + " (Ctrl-Shift-F)", null);
search.AddAccelerator ("clicked",
accel_group,
(uint) Gdk.Key.f,
(Gdk.ModifierType.ControlMask |
Gdk.ModifierType.ShiftMask),
Gtk.AccelFlags.Visible);
search.ShowAll ();
tb.Insert (search, -1);
link_button = new Gtk.ToolButton (
new Gtk.Image (Gtk.Stock.JumpTo, tb.IconSize),
Catalog.GetString ("Link"));
link_button.IsImportant = true;
link_button.Sensitive = (note.Buffer.Selection != null);
link_button.Clicked += LinkToNoteActivate;
// TODO: If we ever add a way to customize internal keybindings, this will need to change
toolbar_tips.SetTip (
link_button,
Catalog.GetString ("Link selected text to a new note") + " (Ctrl-L)",
null);
link_button.AddAccelerator ("clicked",
accel_group,
(uint) Gdk.Key.l,
Gdk.ModifierType.ControlMask,
Gtk.AccelFlags.Visible);
link_button.ShowAll ();
tb.Insert (link_button, -1);
ToolMenuButton text_button =
new ToolMenuButton (tb,
Gtk.Stock.SelectFont,
Catalog.GetString ("_Text"),
text_menu);
text_button.IsImportant = true;
text_button.ShowAll ();
tb.Insert (text_button, -1);
toolbar_tips.SetTip (text_button, Catalog.GetString ("Set properties of text"), null);
ToolMenuButton plugin_button =
new ToolMenuButton (tb,
Gtk.Stock.Execute,
Catalog.GetString ("T_ools"),
plugin_menu);
plugin_button.ShowAll ();
tb.Insert (plugin_button, -1);
toolbar_tips.SetTip (plugin_button, Catalog.GetString ("Use tools on this note"), null);
tb.Insert (new Gtk.SeparatorToolItem (), -1);
delete = new Gtk.ToolButton (Gtk.Stock.Delete);
delete.Clicked += OnDeleteButtonClicked;
delete.ShowAll ();
tb.Insert (delete, -1);
toolbar_tips.SetTip (delete, Catalog.GetString ("Delete this note"), null);
// Don't allow deleting the "Start Here" note...
if (note.IsSpecial)
delete.Sensitive = false;
tb.Insert (new Gtk.SeparatorToolItem (), -1);
sync_menu_item = new Gtk.ImageMenuItem (Catalog.GetString ("Synchronize Notes"));
sync_menu_item.Image = new Gtk.Image (Gtk.Stock.Convert, Gtk.IconSize.Menu);
sync_menu_item.Activated += SyncItemSelected;
sync_menu_item.Show ();
PluginMenu.Add (sync_menu_item);
// We might want to know when various settings are altered.
Preferences.SettingChanged += Preferences_SettingChanged;
// Update items based on configuration.
UpdateMenuItems ();
tb.ShowAll ();
return tb;
}
void Preferences_SettingChanged (object sender, EventArgs args)
{
// Update items based on configuration.
UpdateMenuItems ();
}
void UpdateMenuItems ()
{
// Is synchronization configured and active?
string sync_addin_id = Preferences.Get (Preferences.SYNC_SELECTED_SERVICE_ADDIN) as string;
sync_menu_item.Sensitive = !string.IsNullOrEmpty (sync_addin_id);
}
void SyncItemSelected (object sender, EventArgs args)
{
Tomboy.ActionManager ["NoteSynchronizationAction"].Activate ();
}
//
// Plugin toolbar menu
//
// This menu can be
// populated by individual plugins using
// NotePlugin.AddPluginMenuItem().
//
Gtk.Menu MakePluginMenu ()
{
Gtk.Menu menu = new Gtk.Menu ();
return menu;
}
private Gtk.Box MakeTemplateBar ()
{
// TODO: Move these to static area
Tag template_tag = TagManager.GetOrCreateSystemTag (TagManager.TemplateNoteSystemTag);
Tag template_save_size_tag = TagManager.GetOrCreateSystemTag (TagManager.TemplateNoteSaveSizeSystemTag);
Tag template_save_selection_tag = TagManager.GetOrCreateSystemTag (TagManager.TemplateNoteSaveSelectionSystemTag);
Tag template_save_title_tag = TagManager.GetOrCreateSystemTag (TagManager.TemplateNoteSaveTitleSystemTag);
var bar = new Gtk.VBox ();
var infoLabel = new Gtk.Label (Catalog.GetString ("This note is a template note. It determines " +
"the default content of regular notes, and will " +
"not show up in the note menu or search window."));
infoLabel.Wrap = true;
var untemplateButton = new Gtk.Button ();
untemplateButton.Label = Catalog.GetString ("Convert to regular note");
untemplateButton.Clicked += (o, e) => {
note.RemoveTag (template_tag);
};
var saveSizeCheckbutton = new Gtk.CheckButton (Catalog.GetString ("Save Si_ze"));
saveSizeCheckbutton.Active = note.ContainsTag (template_save_size_tag);
saveSizeCheckbutton.Toggled += (o, e) => {
if (saveSizeCheckbutton.Active)
note.AddTag (template_save_size_tag);
else
note.RemoveTag (template_save_size_tag);
};
var saveSelectionCheckbutton = new Gtk.CheckButton (Catalog.GetString ("Save Se_lection"));
saveSelectionCheckbutton.Active = note.ContainsTag (template_save_selection_tag);
saveSelectionCheckbutton.Toggled += (o, e) => {
if (saveSelectionCheckbutton.Active)
note.AddTag (template_save_selection_tag);
else
note.RemoveTag (template_save_selection_tag);
};
var saveTitleCheckbutton = new Gtk.CheckButton (Catalog.GetString ("Save _Title"));
saveTitleCheckbutton.Active = note.ContainsTag (template_save_title_tag);
saveTitleCheckbutton.Toggled += (o, e) => {
if (saveTitleCheckbutton.Active)
note.AddTag (template_save_title_tag);
else
note.RemoveTag (template_save_title_tag);
};
bar.PackStart (infoLabel);
bar.PackStart (untemplateButton);
bar.PackStart (saveSizeCheckbutton);
bar.PackStart (saveSelectionCheckbutton);
bar.PackStart (saveTitleCheckbutton);
if (note.ContainsTag (template_tag))
bar.ShowAll ();
note.TagAdded += delegate (Note taggedNote, Tag tag) {
if (taggedNote == note && tag == template_tag)
bar.ShowAll ();
};
note.TagRemoved += delegate (Note taggedNote, string tag) {
if (taggedNote == note && tag == template_tag.NormalizedName)
bar.HideAll ();
};
return bar;
}
//
// Find context menu
//
// Find, Find Next, Find Previous menu items. Next nd previous
// are only sensitized when there are search results for this
// buffer to iterate.
//
Gtk.Menu MakeFindMenu ()
{
Gtk.Menu menu = new Gtk.Menu ();
menu.AccelGroup = accel_group;
Gtk.ImageMenuItem find =
new Gtk.ImageMenuItem (Catalog.GetString ("_Find..."));
find.Image = new Gtk.Image (Gtk.Stock.Find, Gtk.IconSize.Menu);
find.Activated += FindActivate;
find.AddAccelerator ("activate",
accel_group,
(uint) Gdk.Key.f,
Gdk.ModifierType.ControlMask,
Gtk.AccelFlags.Visible);
find.Show ();
Gtk.ImageMenuItem find_next =
new Gtk.ImageMenuItem (Catalog.GetString ("Find _Next"));
find_next.Image = new Gtk.Image (Gtk.Stock.GoForward, Gtk.IconSize.Menu);
find_next.Sensitive = Find.FindNextButton.Sensitive;
find_next.Activated += FindNextActivate;
find_next.AddAccelerator ("activate",
accel_group,
(uint) Gdk.Key.g,
Gdk.ModifierType.ControlMask,
Gtk.AccelFlags.Visible);
find_next.Show ();
Gtk.ImageMenuItem find_previous =
new Gtk.ImageMenuItem (Catalog.GetString ("Find _Previous"));
find_previous.Image = new Gtk.Image (Gtk.Stock.GoBack, Gtk.IconSize.Menu);
find_previous.Sensitive = Find.FindPreviousButton.Sensitive;
find_previous.Activated += FindPreviousActivate;
find_previous.AddAccelerator ("activate",
accel_group,
(uint) Gdk.Key.g,
(Gdk.ModifierType.ControlMask |
Gdk.ModifierType.ShiftMask),
Gtk.AccelFlags.Visible);
find_previous.Show ();
menu.Append (find);
menu.Append (find_next);
menu.Append (find_previous);
return menu;
}
//
// Open the find dialog, passing any currently selected text
//
public NoteFindBar Find {
get {
return find_bar;
}
}
void FindButtonClicked ()
{
Find.ShowAll ();
Find.Visible = true;
Find.SearchText = note.Buffer.Selection;
}
void FindActivate (object sender, EventArgs args)
{
FindButtonClicked ();
}
void FindNextActivate (object sender, EventArgs args)
{
Find.FindNextButton.Click ();
}
void FindPreviousActivate (object sender, EventArgs args)
{
Find.FindPreviousButton.Click ();
}
void FindBarHidden (object sender, EventArgs args)
{
// Reposition the current focus back to the editor so the
// cursor will be ready for typing.
editor.GrabFocus ();
}
//
// Link menu item activate
//
// Create a new note, names according to the buffer's selected
// text. Does nothing if there is no active selection.
//
void LinkButtonClicked ()
{
string select = note.Buffer.Selection;
if (select == null)
return;
string body_unused;
string title = NoteManager.SplitTitleFromContent (select, out body_unused);
if (title == null)
return;
Note match = note.Manager.Find (title);
if (match == null) {
try {
match = note.Manager.Create (select);
} catch (Exception e) {
HIGMessageDialog dialog =
new HIGMessageDialog (
this,
Gtk.DialogFlags.DestroyWithParent,
Gtk.MessageType.Error,
Gtk.ButtonsType.Ok,
Catalog.GetString ("Cannot create note"),
e.Message);
dialog.Run ();
dialog.Destroy ();
return;
}
}
match.Window.Present ();
}
void LinkToNoteActivate (object sender, EventArgs args)
{
LinkButtonClicked ();
}
void OpenHelpActivate (object sender, EventArgs args)
{
GuiUtils.ShowHelp ("tomboy", "editing-notes", Screen, this);
}
void CreateNewNote (object sender, EventArgs args)
{
Tomboy.ActionManager ["NewNoteAction"].Activate ();
}
void SearchButtonClicked ()
{
NoteRecentChanges search = NoteRecentChanges.GetInstance (note.Manager);
if (note.Buffer.Selection != null) {
search.SearchText = note.Buffer.Selection;
}
search.Present ();
}
void SearchActivate (object sender, EventArgs args)
{
SearchButtonClicked ();
}
void ChangeDepthRightHandler (object sender, EventArgs args)
{
((NoteBuffer)editor.Buffer).ChangeCursorDepthDirectional (true);
}
void ChangeDepthLeftHandler (object sender, EventArgs args)
{
((NoteBuffer)editor.Buffer).ChangeCursorDepthDirectional (false);
}
}
public class NoteFindBar : Gtk.HBox
{
private Note note;
Gtk.Entry entry;
Gtk.Button next_button;
Gtk.Button prev_button;
Gtk.Label labelCount = new Gtk.Label (); //Label of current find matches and current position in List of Matches
List<Match> current_matches;
string prev_search_text;
InterruptableTimeout entry_changed_timeout;
InterruptableTimeout note_changed_timeout;
bool shift_key_pressed;
public NoteFindBar (Note note)
: base (false, 0)
{
this.note = note;
BorderWidth = 2;
Gtk.Button button = new Gtk.Button ();
button.Image = new Gtk.Image (Gtk.Stock.Close, Gtk.IconSize.Menu);
button.Relief = Gtk.ReliefStyle.None;
button.Clicked += HideFindBar;
button.Show ();
PackStart (button, false, false, 4);
Gtk.Label label = new Gtk.Label (Catalog.GetString ("_Find:"));
label.Show ();
PackStart (label, false, false, 0);
entry = new Gtk.Entry ();
label.MnemonicWidget = entry;
entry.Changed += OnFindEntryChanged;
entry.Activated += OnFindEntryActivated;
entry.Show ();
PackStart (entry, true, true, 0);
labelCount.Show ();
PackStart (labelCount, false,false, 0);
prev_button = new Gtk.Button (Catalog.GetString ("_Previous"));
prev_button.Image = new Gtk.Arrow (Gtk.ArrowType.Left, Gtk.ShadowType.None);
prev_button.Relief = Gtk.ReliefStyle.None;
prev_button.Sensitive = false;
prev_button.FocusOnClick = false;
prev_button.Clicked += OnPrevClicked;
prev_button.Show ();
PackStart (prev_button, false, false, 0);
next_button = new Gtk.Button (Catalog.GetString ("_Next"));
next_button.Image = new Gtk.Arrow (Gtk.ArrowType.Right, Gtk.ShadowType.None);
next_button.Relief = Gtk.ReliefStyle.None;
next_button.Sensitive = false;
next_button.FocusOnClick = false;
next_button.Clicked += OnNextClicked;
next_button.Show ();
PackStart (next_button, false, false, 0);
// Bind ESC to close the FindBar if it's open and has
// focus or the window otherwise. Also bind Return and
// Shift+Return to advance the search if the search
// entry has focus.
shift_key_pressed = false;
entry.KeyPressEvent += KeyPressed;
entry.KeyReleaseEvent += KeyReleased;
}
/// <summary>
/// Updates the match count.
/// </summary>
/// <param name='location'>
/// Current location in the List of Matched notes
/// </param>
protected void UpdateMatchCount (int location)
{
if (current_matches == null || current_matches.Count == 0)
return;
// x of x - specifically Number of Matches and what location is the cursor in the list of matches
labelCount.Text = String.Format (Catalog.GetString("{0} of {1}"), (location + 1), current_matches.Count);
}
/// <summary>
/// Clears the match count.
/// </summary>
protected void ClearMatchCount ()
{
labelCount.Text = "";
}
protected override void OnShown ()
{
entry.GrabFocus ();
// Highlight words from a previous existing search
HighlightMatches (true);
// Call PerformSearch on newly inserted text when
// the FindBar is visible
note.Buffer.InsertText += OnInsertText;
note.Buffer.DeleteRange += OnDeleteRange;
base.OnShown ();
}
protected override void OnHidden ()
{
HighlightMatches (false);
// Prevent searching when the FindBar is not visible
note.Buffer.InsertText -= OnInsertText;
note.Buffer.DeleteRange -= OnDeleteRange;
base.OnHidden ();
}
void HideFindBar (object sender, EventArgs args)
{
Hide ();
}
void OnPrevClicked (object sender, EventArgs args)
{
if (current_matches == null || current_matches.Count == 0)
return;
for (int i = current_matches.Count; i > 0; i--) {
Match match = current_matches [i - 1] as Match;
NoteBuffer buffer = match.Buffer;
Gtk.TextIter cursor = buffer.GetIterAtMark (buffer.InsertMark);
Gtk.TextIter end = buffer.GetIterAtMark (match.EndMark);
if (end.Offset < cursor.Offset) {
UpdateMatchCount (current_matches.IndexOf (match));
JumpToMatch (match);
return;
}
}
UpdateMatchCount (current_matches.Count - 1);
// Wrap to first match
JumpToMatch (current_matches [current_matches.Count - 1] as Match);
}
void OnNextClicked (object sender, EventArgs args)
{
if (current_matches == null || current_matches.Count == 0)
return;
for (int i = 0; i < current_matches.Count; i++) {
Match match = current_matches [i] as Match;
NoteBuffer buffer = match.Buffer;
Gtk.TextIter cursor = buffer.GetIterAtMark (buffer.InsertMark);
Gtk.TextIter start = buffer.GetIterAtMark (match.StartMark);
if (start.Offset >= cursor.Offset) {
UpdateMatchCount (current_matches.IndexOf (match));
JumpToMatch (match);
return;
}
}
UpdateMatchCount(0);
// Else wrap to first match
JumpToMatch (current_matches [0] as Match);
}
void JumpToMatch (Match match)
{
NoteBuffer buffer = match.Buffer;
Gtk.TextIter start = buffer.GetIterAtMark (match.StartMark);
Gtk.TextIter end = buffer.GetIterAtMark (match.EndMark);
// Move cursor to end of match, and select match text
buffer.PlaceCursor (end);
buffer.MoveMark (buffer.SelectionBound, start);
Gtk.TextView editor = note.Window.Editor;
editor.ScrollMarkOnscreen (buffer.InsertMark);
}
void OnFindEntryActivated (object sender, EventArgs args)
{
if (entry_changed_timeout != null) {
entry_changed_timeout.Cancel ();
entry_changed_timeout = null;
}
if (prev_search_text != null &&
SearchText != null &&
prev_search_text.CompareTo (SearchText) == 0)