~ubuntu-branches/ubuntu/lucid/tomboy/lucid-proposed

« back to all changes in this revision

Viewing changes to Tomboy/Applet.cs

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Dröge, Jo Shields, Sebastian Dröge
  • Date: 2009-03-06 11:02:03 UTC
  • mfrom: (1.2.1 upstream)
  • mto: (4.2.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 20.
  • Revision ID: james.westby@ubuntu.com-20090306110203-5nbuw7itgo5mn9e0
Tags: 0.12.2-3
[ Jo Shields ]
* debian/control:
  + Add Vcs-* fields

[ Sebastian Dröge ]
* Upload to unstable (Closes: #518414).
* debian/control:
  + Use Gnome# 2.20 for now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
 
2
2
using System;
3
3
using System.IO;
 
4
using System.Text;
4
5
using System.Reflection;
5
6
using System.Runtime.InteropServices;
6
7
using Mono.Unix;
7
8
 
8
 
// Work around bug in Gtk# panel applet bindings by using a local copy with
9
 
// fixed OnBackgroundChanged marshalling.
 
9
#if FIXED_PANELAPPLET
 
10
using Gnome;
 
11
#else
10
12
using _Gnome;
 
13
#endif
11
14
 
12
15
namespace Tomboy
13
16
{
14
17
        public class TomboyApplet : PanelApplet
15
18
        {
16
19
                NoteManager manager;
17
 
                TomboyTray tray;
 
20
                TomboyAppletEventBox applet_event_box;
18
21
                TomboyGConfXKeybinder keybinder;
19
22
 
20
23
                // Keep referenced so our callbacks don't get reaped.
44
47
                        Logger.Log ("Applet Created...");
45
48
 
46
49
                        manager = Tomboy.DefaultNoteManager;
47
 
                        tray = new TomboyTray (manager);
48
 
                        keybinder = new TomboyGConfXKeybinder (manager, tray);
 
50
                        applet_event_box = new TomboyAppletEventBox (manager);
 
51
                        keybinder = new TomboyGConfXKeybinder (manager, applet_event_box.Tray);
49
52
 
50
53
                        Flags |= PanelAppletFlags.ExpandMinor;
51
54
 
52
 
                        Add (tray);
53
 
                        Tomboy.Tray = tray;
 
55
                        Add (applet_event_box);
 
56
                        Tomboy.Tray = applet_event_box.Tray;
54
57
                        OnChangeSize (Size);
55
58
                        ShowAll ();
56
59
 
104
107
                                Gdk.Color                 color,
105
108
                                Gdk.Pixmap                pixmap)
106
109
                {
107
 
                        if (tray == null)
 
110
                        if (applet_event_box == null)
108
111
                                return;
109
112
 
110
113
                        Gtk.RcStyle rc_style = new Gtk.RcStyle ();
111
 
                        tray.Style = null;
112
 
                        tray.ModifyStyle (rc_style);
 
114
                        applet_event_box.Style = null;
 
115
                        applet_event_box.ModifyStyle (rc_style);
113
116
 
114
117
                        switch (type) {
115
118
                        case PanelAppletBackgroundType.ColorBackground:
116
 
                                tray.ModifyBg (Gtk.StateType.Normal, color);
 
119
                                applet_event_box.ModifyBg (Gtk.StateType.Normal, color);
117
120
                                break;
118
121
                        case PanelAppletBackgroundType.NoBackground:
119
122
                                break;
120
123
                        case PanelAppletBackgroundType.PixmapBackground:
121
 
                                Gtk.Style copy = tray.Style.Copy();
 
124
                                Gtk.Style copy = applet_event_box.Style.Copy();
122
125
                                copy.SetBgPixmap (Gtk.StateType.Normal, pixmap);
123
 
                                tray.Style = copy;
 
126
                                applet_event_box.Style = copy;
124
127
                                break;
125
128
                        }
126
129
                }
127
130
 
128
131
                protected override void OnChangeSize (uint size)
129
132
                {
130
 
                        if (tray == null)
 
133
                        if (applet_event_box == null)
131
134
                                return;
132
135
 
133
 
                        tray.SetSizeRequest ((int) size, (int) size);
 
136
                        applet_event_box.SetSizeRequest ((int) size, (int) size);
134
137
                }
135
138
        }
136
 
 
137
 
        public class TomboyTrayIcon : Gtk.Plug
 
139
        
 
140
        public enum PanelOrientation { Horizontal, Vertical };
 
141
        
 
142
        public class TomboyAppletEventBox : Gtk.EventBox
138
143
        {
139
144
                NoteManager manager;
140
145
                TomboyTray tray;
141
 
                TomboyGConfXKeybinder keybinder;
142
 
 
143
 
                [DllImport ("libtomboy")]
144
 
                private static extern IntPtr egg_tray_icon_new (string name);
145
 
 
146
 
                public TomboyTrayIcon ()
147
 
: this (Tomboy.DefaultNoteManager)
148
 
                {
149
 
                }
150
 
 
151
 
                public TomboyTrayIcon (NoteManager manager)
152
 
                {
153
 
                        this.Raw = egg_tray_icon_new (Catalog.GetString ("Tomboy Notes"));
 
146
                Gtk.Tooltips tips;
 
147
                Gtk.Image image;
 
148
                int panel_size;
 
149
 
 
150
                public TomboyAppletEventBox (NoteManager manager)
 
151
: base ()
 
152
                {
154
153
                        this.manager = manager;
155
 
 
156
 
                        tray = new TomboyTray (manager);
157
 
                        tray.ButtonPressEvent += ButtonPress;
158
 
 
159
 
                        keybinder = new TomboyGConfXKeybinder (manager, tray);
160
 
 
161
 
                        Add (tray);
162
 
                        ShowAll ();
 
154
                        tray = new TomboyTray (manager, this);
 
155
 
 
156
                        // Load a 16x16-sized icon to ensure we don't end up with a
 
157
                        // 1x1 pixel.
 
158
                        panel_size = 16;
 
159
                        this.image = new Gtk.Image (GuiUtils.GetIcon ("tomboy", panel_size));
 
160
 
 
161
                        this.CanFocus = true;
 
162
                        this.ButtonPressEvent += ButtonPress;
 
163
                        this.Add (image);
 
164
                        this.ShowAll ();
 
165
 
 
166
                        string tip_text = TomboyTrayUtils.GetToolTipText ();
 
167
 
 
168
                        tips = new Gtk.Tooltips ();
 
169
                        tips.SetTip (this, tip_text, null);
 
170
                        tips.Enable ();
 
171
                        tips.Sink ();
 
172
 
 
173
                        SetupDragAndDrop ();
 
174
                }
 
175
                
 
176
                public TomboyTray Tray
 
177
                {
 
178
                        get {
 
179
                                return tray;
 
180
                        }
163
181
                }
164
182
 
165
183
                void ButtonPress (object sender, Gtk.ButtonPressEventArgs args)
166
184
                {
167
185
                        Gtk.Widget parent = (Gtk.Widget) sender;
168
186
 
169
 
                        if (args.Event.Button == 3) {
170
 
                                Gtk.Menu menu = MakeRightClickMenu (parent);
171
 
                                GuiUtils.PopupMenu (menu, args.Event);
 
187
                        switch (args.Event.Button) {
 
188
                        case 1:
 
189
                                TomboyTrayUtils.UpdateTomboyTrayMenu (tray, parent);
 
190
                                GuiUtils.PopupMenu (tray.TomboyTrayMenu, args.Event);
172
191
                                args.RetVal = true;
 
192
                                break;
 
193
                        case 2:
 
194
                                if ((bool) Preferences.Get (Preferences.ENABLE_ICON_PASTE)) {
 
195
                                        // Give some visual feedback
 
196
                                        Gtk.Drag.Highlight (this);
 
197
                                        args.RetVal = PastePrimaryClipboard ();
 
198
                                        Gtk.Drag.Unhighlight (this);
 
199
                                }
 
200
                                break;
173
201
                        }
174
202
                }
175
203
 
176
 
                Gtk.Menu MakeRightClickMenu (Gtk.Widget parent)
177
 
                {
178
 
                        Gtk.Menu menu = new Gtk.Menu ();
179
 
                        menu.AttachToWidget (parent, GuiUtils.DetachMenu);
180
 
 
181
 
                        Gtk.AccelGroup accel_group = new Gtk.AccelGroup ();
182
 
                        menu.AccelGroup = accel_group;
183
 
 
184
 
                        Gtk.ImageMenuItem item;
185
 
 
186
 
                        item = new Gtk.ImageMenuItem (Catalog.GetString ("_Preferences"));
187
 
                        item.Image = new Gtk.Image (Gtk.Stock.Preferences, Gtk.IconSize.Menu);
188
 
                        item.Activated += ShowPreferences;
189
 
                        menu.Append (item);
190
 
 
191
 
                        item = new Gtk.ImageMenuItem (Catalog.GetString ("_Help"));
192
 
                        item.Image = new Gtk.Image (Gtk.Stock.Help, Gtk.IconSize.Menu);
193
 
                        item.Activated += ShowHelpContents;
194
 
                        menu.Append (item);
195
 
 
196
 
                        item = new Gtk.ImageMenuItem (Catalog.GetString ("_About Tomboy"));
197
 
                        item.Image = new Gtk.Image (Gtk.Stock.About, Gtk.IconSize.Menu);
198
 
                        item.Activated += ShowAbout;
199
 
                        menu.Append (item);
200
 
 
201
 
                        menu.Append (new Gtk.SeparatorMenuItem ());
202
 
 
203
 
                        item = new Gtk.ImageMenuItem (Catalog.GetString ("_Quit"));
204
 
                        item.Image = new Gtk.Image (Gtk.Stock.Quit, Gtk.IconSize.Menu);
205
 
                        item.Activated += Quit;
206
 
                        menu.Append (item);
207
 
 
208
 
                        menu.ShowAll ();
209
 
                        return menu;
210
 
                }
211
 
 
212
 
                void ShowPreferences (object sender, EventArgs args)
213
 
                {
214
 
                        Tomboy.ActionManager ["ShowPreferencesAction"].Activate ();
215
 
                }
216
 
 
217
 
                void ShowHelpContents (object sender, EventArgs args)
218
 
                {
219
 
                        Tomboy.ActionManager ["ShowHelpAction"].Activate ();
220
 
                }
221
 
 
222
 
                void ShowAbout (object sender, EventArgs args)
223
 
                {
224
 
                        Tomboy.ActionManager ["ShowAboutAction"].Activate ();
225
 
                }
226
 
 
227
 
                void Quit (object sender, EventArgs args)
228
 
                {
229
 
                        Tomboy.ActionManager ["QuitTomboyAction"].Activate ();
230
 
                }
231
 
 
232
 
                public TomboyTray TomboyTray
 
204
                void PrependTimestampedText (Note note, DateTime timestamp, string text)
 
205
                {
 
206
                        NoteBuffer buffer = note.Buffer;
 
207
                        StringBuilder insert_text = new StringBuilder ();
 
208
 
 
209
                        insert_text.Append ("\n"); // initial newline
 
210
                        string date_format = Catalog.GetString ("dddd, MMMM d, h:mm tt");
 
211
                        insert_text.Append (timestamp.ToString (date_format));
 
212
                        insert_text.Append ("\n"); // begin content
 
213
                        insert_text.Append (text);
 
214
                        insert_text.Append ("\n"); // trailing newline
 
215
 
 
216
                        buffer.Undoer.FreezeUndo ();
 
217
 
 
218
                        // Insert the date and list of links...
 
219
                        Gtk.TextIter cursor = buffer.StartIter;
 
220
                        cursor.ForwardLines (1); // skip title
 
221
 
 
222
                        buffer.Insert (ref cursor, insert_text.ToString ());
 
223
 
 
224
                        // Make the date string a small font...
 
225
                        cursor = buffer.StartIter;
 
226
                        cursor.ForwardLines (2); // skip title & leading newline
 
227
 
 
228
                        Gtk.TextIter end = cursor;
 
229
                        end.ForwardToLineEnd (); // end of date
 
230
 
 
231
                        buffer.ApplyTag ("datetime", cursor, end);
 
232
 
 
233
                        // Select the text we've inserted (avoid trailing newline)...
 
234
                        end = cursor;
 
235
                        end.ForwardChars (insert_text.Length - 1);
 
236
 
 
237
                        buffer.MoveMark (buffer.SelectionBound, cursor);
 
238
                        buffer.MoveMark (buffer.InsertMark, end);
 
239
 
 
240
                        buffer.Undoer.ThawUndo ();
 
241
                }
 
242
 
 
243
                bool PastePrimaryClipboard ()
 
244
                {
 
245
                        Gtk.Clipboard clip = GetClipboard (Gdk.Selection.Primary);
 
246
                        string text = clip.WaitForText ();
 
247
 
 
248
                        if (text == null || text.Trim() == string.Empty)
 
249
                                return false;
 
250
 
 
251
                        Note link_note = manager.FindByUri (NoteManager.StartNoteUri);
 
252
                        if (link_note == null)
 
253
                                return false;
 
254
 
 
255
                        link_note.Window.Present ();
 
256
                        PrependTimestampedText (link_note,
 
257
                                                DateTime.Now,
 
258
                                                text);
 
259
 
 
260
                        return true;
 
261
                }
 
262
 
 
263
                // Used by TomboyApplet to modify the icon background.
 
264
                public Gtk.Image Image
233
265
                {
234
266
                        get {
235
 
                                return tray;
236
 
                        }
 
267
                                return image;
 
268
                        }
 
269
                }
 
270
 
 
271
                public void ShowMenu (bool select_first_item)
 
272
                {
 
273
                        TomboyTrayUtils.UpdateTomboyTrayMenu (tray, this);
 
274
                        if (select_first_item)
 
275
                                tray.TomboyTrayMenu.SelectFirst (false);
 
276
 
 
277
                        GuiUtils.PopupMenu (tray.TomboyTrayMenu, null);
 
278
                }
 
279
 
 
280
                // Support dropping text/uri-lists and _NETSCAPE_URLs currently.
 
281
                void SetupDragAndDrop ()
 
282
                {
 
283
                        Gtk.TargetEntry [] targets =
 
284
                        new Gtk.TargetEntry [] {
 
285
                                new Gtk.TargetEntry ("text/uri-list", 0, 0),
 
286
                                new Gtk.TargetEntry ("_NETSCAPE_URL", 0, 0)
 
287
                        };
 
288
 
 
289
                        Gtk.Drag.DestSet (this,
 
290
                                          Gtk.DestDefaults.All,
 
291
                                          targets,
 
292
                                          Gdk.DragAction.Copy);
 
293
 
 
294
                        DragDataReceived += OnDragDataReceived;
 
295
                }
 
296
 
 
297
                // Pop up Start Here and insert dropped links, in the form:
 
298
                // Wednesday, December 8, 6:45 AM
 
299
                // http://luna/kwiki/index.cgi?AdelaideUniThoughts
 
300
                // http://www.beatniksoftware.com/blog/
 
301
                // And select the inserted text.
 
302
                //
 
303
                // FIXME: Make undoable, make sure our date-sizing tag never "bleeds".
 
304
                //
 
305
                void OnDragDataReceived (object sender, Gtk.DragDataReceivedArgs args)
 
306
                {
 
307
                        UriList uri_list = new UriList (args.SelectionData);
 
308
                        if (uri_list.Count == 0)
 
309
                                return;
 
310
 
 
311
                        StringBuilder insert_text = new StringBuilder ();
 
312
                        bool more_than_one = false;
 
313
 
 
314
                        foreach (Uri uri in uri_list) {
 
315
                                if (more_than_one)
 
316
                                        insert_text.Append ("\n");
 
317
 
 
318
                                if (uri.IsFile)
 
319
                                        insert_text.Append (uri.LocalPath);
 
320
                                else
 
321
                                        insert_text.Append (uri.ToString ());
 
322
 
 
323
                                more_than_one = true;
 
324
                        }
 
325
 
 
326
                        Note link_note = manager.FindByUri (NoteManager.StartNoteUri);
 
327
                        if (link_note != null) {
 
328
                                link_note.Window.Present ();
 
329
                                PrependTimestampedText (link_note,
 
330
                                                        DateTime.Now,
 
331
                                                        insert_text.ToString ());
 
332
                        }
 
333
                }
 
334
 
 
335
                void InitPixbuf ()
 
336
                {
 
337
                        // For some reason, the first time we ask for the allocation,
 
338
                        // it's a 1x1 pixel.  Prevent against this by returning a
 
339
                        // reasonable default.  Setting the icon causes OnSizeAllocated
 
340
                        // to be called again anyhow.
 
341
                        int icon_size = panel_size;
 
342
                        if (icon_size < 16)
 
343
                                icon_size = 16;
 
344
 
 
345
 
 
346
                        // Control specifically which icon is used at the smaller sizes
 
347
                        // so that no scaling occurs.  In the case of the panel applet,
 
348
                        // add a couple extra pixels of padding so it matches the behavior
 
349
                        // of the notification area tray icon.  See bug #403500 for more
 
350
                        // info.
 
351
                        if (Tomboy.IsPanelApplet)
 
352
                                icon_size = icon_size - 2; // padding
 
353
                        if (icon_size <= 21)
 
354
                                icon_size = 16;
 
355
                        else if (icon_size <= 31)
 
356
                                icon_size = 22;
 
357
                        else if (icon_size <= 47)
 
358
                                icon_size = 32;
 
359
 
 
360
                        Gdk.Pixbuf new_icon = GuiUtils.GetIcon ("tomboy", icon_size);
 
361
                        image.Pixbuf = new_icon;
 
362
                }
 
363
 
 
364
                ///
 
365
                /// Determine whether the tray is inside a horizontal or vertical
 
366
                /// panel so the size of the icon can adjust correctly.
 
367
                ///
 
368
                PanelOrientation GetPanelOrientation ()
 
369
                {
 
370
                        if (this.ParentWindow == null) {
 
371
                                return PanelOrientation.Horizontal;
 
372
                        }
 
373
 
 
374
                        Gdk.Window top_level_window = this.ParentWindow.Toplevel;
 
375
 
 
376
                        Gdk.Rectangle rect = top_level_window.FrameExtents;
 
377
                        if (rect.Width < rect.Height)
 
378
                                return PanelOrientation.Vertical;
 
379
 
 
380
                        return PanelOrientation.Horizontal;
 
381
                }
 
382
 
 
383
                protected override void OnSizeAllocated (Gdk.Rectangle rect)
 
384
                {
 
385
                        base.OnSizeAllocated (rect);
 
386
 
 
387
                        // Determine the orientation
 
388
                        if (GetPanelOrientation () == PanelOrientation.Horizontal) {
 
389
                                if (panel_size == Allocation.Height)
 
390
                                        return;
 
391
 
 
392
                                panel_size = Allocation.Height;
 
393
                        } else {
 
394
                                if (panel_size == Allocation.Width)
 
395
                                        return;
 
396
 
 
397
                                panel_size = Allocation.Width;
 
398
                        }
 
399
 
 
400
                        InitPixbuf ();
237
401
                }
238
402
        }
239
403
}