~ubuntu-branches/ubuntu/karmic/tomboy/karmic

« back to all changes in this revision

Viewing changes to Tomboy/Tray.cs

  • Committer: Bazaar Package Importer
  • Author(s): Brandon Hale
  • Date: 2004-10-11 09:31:35 UTC
  • Revision ID: james.westby@ubuntu.com-20041011093135-00f2snu2ny5i6wto
Tags: upstream-0.2.0
ImportĀ upstreamĀ versionĀ 0.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
using System;
 
3
using System.Collections;
 
4
using Mono.Posix;
 
5
using System.Runtime.InteropServices;
 
6
 
 
7
namespace Tomboy
 
8
{
 
9
        public class TomboyTray 
 
10
        {
 
11
                NoteManager manager;
 
12
                Egg.TrayIcon icon;
 
13
                Gtk.Tooltips tips;
 
14
 
 
15
                static Gdk.Pixbuf tintin;
 
16
                static Gdk.Pixbuf stock_notes;
 
17
 
 
18
                static TomboyTray ()
 
19
                {
 
20
                        tintin = GuiUtils.GetMiniIcon ("tintin.png");
 
21
                        stock_notes = GuiUtils.GetMiniIcon ("stock_notes.png");
 
22
                }
 
23
 
 
24
                public TomboyTray (NoteManager manager) 
 
25
                {
 
26
                        this.manager = manager;
 
27
 
 
28
                        Gtk.EventBox ev = new Gtk.EventBox ();
 
29
                        ev.CanFocus = true;
 
30
                        ev.ButtonPressEvent += ButtonPress;
 
31
                        ev.Add (new Gtk.Image (tintin));
 
32
 
 
33
                        string tip_text = Catalog.GetString ("Tomboy Notes");
 
34
 
 
35
                        string shortcut = 
 
36
                                GConfKeybindingToAccel.GetShortcut (
 
37
                                        TomboyGConfXKeybinder.MENU_BINDING);
 
38
                        if (shortcut != null)
 
39
                                tip_text += String.Format (" ({0})", shortcut);
 
40
 
 
41
                        tips = new Gtk.Tooltips ();
 
42
                        tips.SetTip (ev, tip_text, null);
 
43
                        tips.Enable ();
 
44
 
 
45
                        icon = new Egg.TrayIcon (Catalog.GetString ("Tomboy"));
 
46
                        icon.Add (ev);
 
47
                        icon.ShowAll ();
 
48
                }
 
49
 
 
50
                public void ShowMenu ()
 
51
                {
 
52
                        Gtk.Menu recent_menu = MakeRecentNotesMenu (icon);
 
53
                        GuiUtils.PopupMenu (recent_menu, null);
 
54
                }
 
55
 
 
56
                void ButtonPress (object sender, Gtk.ButtonPressEventArgs args) 
 
57
                {
 
58
                        Gtk.Widget parent = (Gtk.Widget) sender;
 
59
                        Gtk.Menu recent_menu = MakeRecentNotesMenu (parent);
 
60
                        GuiUtils.PopupMenu (recent_menu, args.Event);
 
61
                }
 
62
 
 
63
                Gtk.Menu MakeRecentNotesMenu (Gtk.Widget parent) 
 
64
                {
 
65
                        Gtk.Menu menu = new Gtk.Menu ();
 
66
                        menu.AttachToWidget (parent, null);
 
67
 
 
68
                        Gtk.ImageMenuItem item;
 
69
 
 
70
                        item = new Gtk.ImageMenuItem (Catalog.GetString ("Create _New Note"));
 
71
                        item.Image = new Gtk.Image (Gtk.Stock.New, Gtk.IconSize.Menu);
 
72
                        item.Activated += CreateNewNote;
 
73
                        menu.Append (item);
 
74
 
 
75
                        GConfKeybindingToAccel.AddAccelerator (
 
76
                                item, 
 
77
                                TomboyGConfXKeybinder.NEW_NOTE_BINDING);
 
78
 
 
79
                        // FIXME: Pull this from GConf
 
80
                        int min_size = 5;
 
81
                        int max_size = 18;
 
82
                        int list_size = 0;
 
83
 
 
84
                        DateTime two_days_ago = DateTime.Now.AddDays (-2);
 
85
 
 
86
                        // List the i most recently changed notes, and any
 
87
                        // currently opened notes...
 
88
                        foreach (Note note in manager.Notes) {
 
89
                                if (note.IsSpecial)
 
90
                                        continue;
 
91
 
 
92
                                if (note.IsOpened || 
 
93
                                    note.ChangeDate > two_days_ago ||
 
94
                                    list_size < min_size) {
 
95
                                        item = MakeNoteMenuItem (note);
 
96
                                        menu.Append (item);
 
97
                                }
 
98
 
 
99
                                list_size++;
 
100
                                if (list_size == max_size)
 
101
                                        break;
 
102
                        }
 
103
 
 
104
                        uint keyval;
 
105
                        Gdk.ModifierType mods;
 
106
 
 
107
                        Note start = manager.Find (Catalog.GetString ("Start Here"));
 
108
                        if (start != null) {
 
109
                                item = MakeNoteMenuItem (start);
 
110
                                menu.Append (item);
 
111
 
 
112
                                GConfKeybindingToAccel.AddAccelerator (
 
113
                                        item, 
 
114
                                        TomboyGConfXKeybinder.START_BINDING);
 
115
                        }
 
116
 
 
117
                        menu.Append (new Gtk.SeparatorMenuItem ());
 
118
 
 
119
                        item = new Gtk.ImageMenuItem (Catalog.GetString ("_Recent Changes"));
 
120
                        item.Image = new Gtk.Image (Gtk.Stock.SortAscending, Gtk.IconSize.Menu);
 
121
                        item.Activated += ViewRecentChanges;
 
122
                        menu.Append (item);
 
123
 
 
124
                        GConfKeybindingToAccel.AddAccelerator (
 
125
                                item, 
 
126
                                TomboyGConfXKeybinder.RECENT_BINDING);
 
127
 
 
128
                        item = new Gtk.ImageMenuItem (Catalog.GetString ("_Search Notes..."));
 
129
                        item.Image = new Gtk.Image (Gtk.Stock.Find, Gtk.IconSize.Menu);
 
130
                        item.Activated += SearchNotes;
 
131
                        menu.Append (item);
 
132
 
 
133
                        GConfKeybindingToAccel.AddAccelerator (
 
134
                                item, 
 
135
                                TomboyGConfXKeybinder.SEARCH_BINDING);
 
136
 
 
137
                        menu.Append (new Gtk.SeparatorMenuItem ());
 
138
 
 
139
                        item = new Gtk.ImageMenuItem (Catalog.GetString ("_Quit"));
 
140
                        item.Image = new Gtk.Image (Gtk.Stock.Quit, Gtk.IconSize.Menu);
 
141
                        item.Activated += Quit;
 
142
                        menu.Append (item);
 
143
 
 
144
                        menu.ShowAll ();
 
145
                        return menu;
 
146
                }
 
147
 
 
148
                Gtk.ImageMenuItem MakeNoteMenuItem (Note note)
 
149
                {
 
150
                        string display_name = note.Title;
 
151
                        if (note.IsNew)
 
152
                                display_name += Catalog.GetString (" (new)");
 
153
 
 
154
                        Gtk.ImageMenuItem item = new Gtk.ImageMenuItem (display_name);
 
155
                        item.Image = new Gtk.Image (stock_notes);
 
156
                        item.Data ["Note"] = note;
 
157
                        item.Activated += ShowNote;
 
158
 
 
159
                        return item;
 
160
                }
 
161
 
 
162
                void ShowNote (object sender, EventArgs args) 
 
163
                {
 
164
                        Note note = (Note) ((Gtk.Widget) sender).Data ["Note"];
 
165
                        if (note != null)
 
166
                                note.Window.Present ();
 
167
                }
 
168
 
 
169
                void CreateNewNote (object sender, EventArgs args) 
 
170
                {
 
171
                        Note new_note = manager.Create ();
 
172
                        new_note.Window.Show ();
 
173
                }
 
174
 
 
175
                void SearchNotes (object sender, EventArgs args) 
 
176
                {
 
177
                        NoteFindDialog find_dialog = NoteFindDialog.GetInstance (manager);
 
178
                        find_dialog.Present ();
 
179
                }
 
180
 
 
181
                void ViewRecentChanges (object sender, EventArgs args)
 
182
                {
 
183
                        Gtk.Window recent = new NoteRecentChanges (manager);
 
184
                        recent.Show ();
 
185
                }
 
186
 
 
187
                void Quit (object sender, EventArgs args)
 
188
                {
 
189
                        Console.WriteLine ("Quitting Tomboy.  Ciao!");
 
190
                        Environment.Exit (0);
 
191
                }
 
192
 
 
193
                // FIXME: If receiving a drag, pop up last window used, or a new
 
194
                //        window, or the recent list?  I think recent list
 
195
        }
 
196
 
 
197
        // 
 
198
        // This is a helper to take the XKeybinding string from GConf, and
 
199
        // convert it to a widget accelerator label, so note menu items can
 
200
        // display their global X keybinding.
 
201
        //
 
202
        // FIXME: It would be totally sweet to allow setting the accelerator
 
203
        // visually through the menuitem, and have the new value be stored in
 
204
        // GConf.
 
205
        //
 
206
 
 
207
        public class GConfKeybindingToAccel
 
208
        {
 
209
                static GConf.Client client;
 
210
                static Gtk.AccelGroup accel_group;
 
211
 
 
212
                static GConfKeybindingToAccel ()
 
213
                {
 
214
                        client = new GConf.Client ();
 
215
                        accel_group = new Gtk.AccelGroup ();
 
216
                }
 
217
 
 
218
                public static string GetShortcut (string gconf_path)
 
219
                {
 
220
                        try {
 
221
                                string binding = (string) client.Get (gconf_path);
 
222
                                if (binding == null || 
 
223
                                    binding == String.Empty || 
 
224
                                    binding == "disabled")
 
225
                                        return null;
 
226
 
 
227
                                binding = binding.Replace ("<", "");
 
228
                                binding = binding.Replace (">", "-");
 
229
 
 
230
                                return binding;
 
231
                        } catch {
 
232
                                return null;
 
233
                        }
 
234
                }
 
235
 
 
236
                [DllImport("libtrayicon")]
 
237
                static extern bool egg_accelerator_parse_virtual (string keystring,
 
238
                                                                  out uint keysym,
 
239
                                                                  out uint virtual_mods);
 
240
 
 
241
                [DllImport("libtrayicon")]
 
242
                static extern void egg_keymap_resolve_virtual_modifiers (IntPtr keymap,
 
243
                                                                         uint virtual_mods,
 
244
                                                                         out Gdk.ModifierType real_mods);
 
245
 
 
246
                public static bool GetAccelKeys (string               gconf_path, 
 
247
                                                 out uint             keyval, 
 
248
                                                 out Gdk.ModifierType mods)
 
249
                {
 
250
                        keyval = 0;
 
251
                        mods = 0;
 
252
 
 
253
                        try {
 
254
                                string binding = (string) client.Get (gconf_path);
 
255
                                if (binding == null || 
 
256
                                    binding == String.Empty || 
 
257
                                    binding == "disabled")
 
258
                                        return false;
 
259
 
 
260
                                uint virtual_mods = 0;
 
261
                                if (!egg_accelerator_parse_virtual (binding,
 
262
                                                                    out keyval,
 
263
                                                                    out virtual_mods))
 
264
                                        return false;
 
265
 
 
266
                                Gdk.Keymap keymap = Gdk.Keymap.Default;
 
267
                                egg_keymap_resolve_virtual_modifiers (keymap.Handle,
 
268
                                                                      virtual_mods,
 
269
                                                                      out mods);
 
270
 
 
271
                                return true;
 
272
                        } catch {
 
273
                                return false;
 
274
                        }
 
275
                }
 
276
 
 
277
                public static void AddAccelerator (Gtk.MenuItem item, string gconf_path)
 
278
                {
 
279
                        uint keyval;
 
280
                        Gdk.ModifierType mods;
 
281
 
 
282
                        if (GetAccelKeys (gconf_path, out keyval, out mods))
 
283
                                item.AddAccelerator ("activate",
 
284
                                                     accel_group,
 
285
                                                     keyval,
 
286
                                                     mods,
 
287
                                                     Gtk.AccelFlags.Visible);
 
288
                }
 
289
        }
 
290
}