~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Ide/MonoDevelop.Components/GtkUtil.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
                                                (double)color.Blue / ushort.MaxValue);
44
44
                }
45
45
                
 
46
                public static Gdk.Color ToGdkColor (this Cairo.Color color)
 
47
                {
 
48
                        return new Gdk.Color ((byte)(color.R * 255d), (byte)(color.G * 255d), (byte)(color.B * 255d));
 
49
                }
 
50
                
46
51
                /// <summary>
47
52
                /// Makes a color lighter or darker
48
53
                /// </summary>
56
61
                        c.L += lightAmount;
57
62
                        return c;
58
63
                }
 
64
 
 
65
                public static Cairo.Color AddLight (this Cairo.Color color, double lightAmount)
 
66
                {
 
67
                        HslColor c = color;
 
68
                        c.L += lightAmount;
 
69
                        return c;
 
70
                }
 
71
 
 
72
                public static Gtk.Widget ToGtkWidget (this Xwt.Widget widget)
 
73
                {
 
74
                        return (Gtk.Widget) Xwt.Engine.WidgetRegistry.GetNativeWidget (widget);
 
75
                }
59
76
                
60
77
                public static void EnableAutoTooltips (this Gtk.TreeView tree)
61
78
                {
178
195
                                data.ShowTimer = 0;
179
196
                        }
180
197
                }
 
198
 
 
199
                public static Gdk.Rectangle ToScreenCoordinates (Gtk.Widget widget, Gdk.Window w, Gdk.Rectangle rect)
 
200
                {
 
201
                        return new Gdk.Rectangle (ToScreenCoordinates (widget, w, rect.X, rect.Y), rect.Size);
 
202
                }
 
203
 
 
204
                public static Gdk.Point ToScreenCoordinates (Gtk.Widget widget, Gdk.Window w, int x, int y)
 
205
                {
 
206
                        int ox, oy;
 
207
                        w.GetOrigin (out ox, out oy);
 
208
                        ox += widget.Allocation.X;
 
209
                        oy += widget.Allocation.Y;
 
210
                        return new Gdk.Point (ox + x, oy + y);
 
211
                }
 
212
 
 
213
                public static Gdk.Rectangle ToWindowCoordinates (Gtk.Widget widget, Gdk.Window w, Gdk.Rectangle rect)
 
214
                {
 
215
                        return new Gdk.Rectangle (ToWindowCoordinates (widget, w, rect.X, rect.Y), rect.Size);
 
216
                }
 
217
                
 
218
                public static Gdk.Point ToWindowCoordinates (Gtk.Widget widget, Gdk.Window w, int x, int y)
 
219
                {
 
220
                        int ox, oy;
 
221
                        w.GetOrigin (out ox, out oy);
 
222
                        ox += widget.Allocation.X;
 
223
                        oy += widget.Allocation.Y;
 
224
                        return new Gdk.Point (x - ox, y - oy);
 
225
                }
 
226
 
 
227
                public static T ReplaceWithWidget<T> (this Gtk.Widget oldWidget, T newWidget, bool transferChildren = false) where T:Gtk.Widget
 
228
                {
 
229
                        Gtk.Container parent = (Gtk.Container) oldWidget.Parent;
 
230
                        if (parent == null)
 
231
                                throw new InvalidOperationException ();
 
232
 
 
233
                        if (parent is Box) {
 
234
                                var box = (Box) parent;
 
235
                                var bc = (Gtk.Box.BoxChild) parent [oldWidget];
 
236
                                box.Add (newWidget);
 
237
                                var nc = (Gtk.Box.BoxChild) parent [newWidget];
 
238
                                nc.Expand = bc.Expand;
 
239
                                nc.Fill = bc.Fill;
 
240
                                nc.PackType = bc.PackType;
 
241
                                nc.Padding = bc.Padding;
 
242
                                nc.Position = bc.Position;
 
243
                                box.Remove (oldWidget);
 
244
                        }
 
245
                        else if (parent is Table) {
 
246
                                var table = (Table) parent;
 
247
                                var bc = (Gtk.Table.TableChild) parent [oldWidget];
 
248
                                table.Add (newWidget);
 
249
                                var nc = (Gtk.Table.TableChild) parent [newWidget];
 
250
                                nc.BottomAttach = bc.BottomAttach;
 
251
                                nc.LeftAttach = bc.LeftAttach;
 
252
                                nc.RightAttach = bc.RightAttach;
 
253
                                nc.TopAttach = bc.TopAttach;
 
254
                                nc.XOptions = bc.XOptions;
 
255
                                nc.XPadding = bc.XPadding;
 
256
                                nc.YOptions = bc.YOptions;
 
257
                                nc.YPadding = bc.YPadding;
 
258
                                table.Remove (oldWidget);
 
259
                        }
 
260
                        else if (parent is Paned) {
 
261
                                var paned = (Paned) parent;
 
262
                                var bc = (Gtk.Paned.PanedChild) parent [oldWidget];
 
263
                                var resize = bc.Resize;
 
264
                                var shrink = bc.Shrink;
 
265
                                if (oldWidget == paned.Child1) {
 
266
                                        paned.Remove (oldWidget);
 
267
                                        paned.Add1 (newWidget);
 
268
                                } else {
 
269
                                        paned.Remove (oldWidget);
 
270
                                        paned.Add2 (newWidget);
 
271
                                }
 
272
                                var nc = (Gtk.Paned.PanedChild) parent [newWidget];
 
273
                                nc.Resize = resize;
 
274
                                nc.Shrink = shrink;
 
275
                        }
 
276
                        else
 
277
                                throw new NotSupportedException ();
 
278
 
 
279
                        if (transferChildren) {
 
280
                                if (newWidget is Paned && oldWidget is Paned) {
 
281
                                        var panedOld = (Paned) oldWidget;
 
282
                                        var panedNew = (Paned) (object) newWidget;
 
283
                                        if (panedOld.Child1 != null) {
 
284
                                                var c = panedOld.Child1;
 
285
                                                var bc = (Gtk.Paned.PanedChild) panedOld [c];
 
286
                                                var resize = bc.Resize;
 
287
                                                var shrink = bc.Shrink;
 
288
                                                panedOld.Remove (c);
 
289
                                                panedNew.Add1 (c);
 
290
                                                var nc = (Gtk.Paned.PanedChild) panedNew [c];
 
291
                                                nc.Resize = resize;
 
292
                                                nc.Shrink = shrink;
 
293
                                        }
 
294
                                        if (panedOld.Child2 != null) {
 
295
                                                var c = panedOld.Child2;
 
296
                                                var bc = (Gtk.Paned.PanedChild) panedOld [c];
 
297
                                                var resize = bc.Resize;
 
298
                                                var shrink = bc.Shrink;
 
299
                                                panedOld.Remove (c);
 
300
                                                panedNew.Add2 (c);
 
301
                                                var nc = (Gtk.Paned.PanedChild) panedNew [c];
 
302
                                                nc.Resize = resize;
 
303
                                                nc.Shrink = shrink;
 
304
                                        }
 
305
                                }
 
306
                                else
 
307
                                        throw new NotSupportedException ();
 
308
                        }
 
309
 
 
310
                        newWidget.Visible = oldWidget.Visible;
 
311
                        return newWidget;
 
312
                }
 
313
 
 
314
                public static bool ScreenSupportsARGB ()
 
315
                {
 
316
                        return Gdk.Screen.Default.IsComposited;
 
317
                }
 
318
 
 
319
                /// <summary>
 
320
                /// This method can be used to get a reliave Leave event for a widget, which
 
321
                /// is not fired if the pointer leaves the widget to enter a child widget.
 
322
                /// To ubsubscribe the event, dispose the object returned by the method.
 
323
                /// </summary>
 
324
                public static IDisposable SubscribeLeaveEvent (this Gtk.Widget w, System.Action leaveHandler)
 
325
                {
 
326
                        return new LeaveEventData (w, leaveHandler);
 
327
                }
 
328
        }
 
329
 
 
330
        class LeaveEventData: IDisposable
 
331
        {
 
332
                public System.Action LeaveHandler;
 
333
                public Gtk.Widget RootWidget;
 
334
                public bool Inside;
 
335
 
 
336
                public LeaveEventData (Gtk.Widget w, System.Action leaveHandler)
 
337
                {
 
338
                        RootWidget = w;
 
339
                        LeaveHandler = leaveHandler;
 
340
                        if (w.IsRealized) {
 
341
                                RootWidget.Unrealized += HandleUnrealized;
 
342
                                TrackLeaveEvent (w);
 
343
                        }
 
344
                        else
 
345
                                w.Realized += HandleRealized;
 
346
                }
 
347
 
 
348
                void HandleRealized (object sender, EventArgs e)
 
349
                {
 
350
                        RootWidget.Realized -= HandleRealized;
 
351
                        RootWidget.Unrealized += HandleUnrealized;
 
352
                        TrackLeaveEvent (RootWidget);
 
353
                }
 
354
 
 
355
                void HandleUnrealized (object sender, EventArgs e)
 
356
                {
 
357
                        RootWidget.Unrealized -= HandleUnrealized;
 
358
                        UntrackLeaveEvent (RootWidget);
 
359
                        RootWidget.Realized += HandleRealized;
 
360
                        if (Inside) {
 
361
                                Inside = false;
 
362
                                LeaveHandler ();
 
363
                        }
 
364
                }
 
365
 
 
366
                public void Dispose ()
 
367
                {
 
368
                        if (RootWidget.IsRealized) {
 
369
                                UntrackLeaveEvent (RootWidget);
 
370
                                RootWidget.Unrealized -= HandleUnrealized;
 
371
                        } else {
 
372
                                RootWidget.Realized -= HandleRealized;
 
373
                        }
 
374
                }
 
375
 
 
376
                public void TrackLeaveEvent (Gtk.Widget w)
 
377
                {
 
378
                        w.LeaveNotifyEvent += HandleLeaveNotifyEvent;
 
379
                        w.EnterNotifyEvent += HandleEnterNotifyEvent;
 
380
                        if (w is Gtk.Container) {
 
381
                                ((Gtk.Container)w).Added += HandleAdded;
 
382
                                ((Gtk.Container)w).Removed += HandleRemoved;
 
383
                                foreach (var c in ((Gtk.Container)w).Children)
 
384
                                        TrackLeaveEvent (c);
 
385
                        }
 
386
                }
 
387
 
 
388
                void UntrackLeaveEvent (Gtk.Widget w)
 
389
                {
 
390
                        w.LeaveNotifyEvent -= HandleLeaveNotifyEvent;
 
391
                        w.EnterNotifyEvent -= HandleEnterNotifyEvent;
 
392
                        if (w is Gtk.Container) {
 
393
                                ((Gtk.Container)w).Added -= HandleAdded;
 
394
                                ((Gtk.Container)w).Removed -= HandleRemoved;
 
395
                                foreach (var c in ((Gtk.Container)w).Children)
 
396
                                        UntrackLeaveEvent (c);
 
397
                        }
 
398
                }
 
399
 
 
400
                void HandleRemoved (object o, RemovedArgs args)
 
401
                {
 
402
                        UntrackLeaveEvent (args.Widget);
 
403
                }
 
404
 
 
405
                void HandleAdded (object o, AddedArgs args)
 
406
                {
 
407
                        TrackLeaveEvent (args.Widget);
 
408
                }
 
409
 
 
410
                void HandleEnterNotifyEvent (object o, EnterNotifyEventArgs args)
 
411
                {
 
412
                        Inside = true;
 
413
                }
 
414
 
 
415
                void HandleLeaveNotifyEvent (object o, LeaveNotifyEventArgs args)
 
416
                {
 
417
                        Inside = false;
 
418
 
 
419
                        // Delay the call to the leave handler since the pointer may be
 
420
                        // entering a child widget, in which case the event doesn't have to be fired
 
421
                        Gtk.Application.Invoke (delegate {
 
422
                                if (!Inside)
 
423
                                        LeaveHandler ();
 
424
                        });
 
425
                }
181
426
        }
182
427
 
183
428
        class TreeViewTooltipsData
193
438
                TreeViewColumn col;
194
439
                TreeView tree;
195
440
                TreeIter iter;
196
 
                
 
441
 
197
442
                public bool MouseIsOver;
198
443
                
199
444
                public CellTooltipWindow (TreeView tree, TreeViewColumn col, TreePath path)
234
479
                protected override bool OnExposeEvent (Gdk.EventExpose evnt)
235
480
                {
236
481
                        base.OnExposeEvent (evnt);
237
 
                        Gdk.Rectangle rect = Allocation;
238
 
                        col.CellSetCellData (tree.Model, iter, false, false);
 
482
 
 
483
                        Gdk.Rectangle expose = Allocation;
 
484
                        Gdk.Color save = Gdk.Color.Zero;
239
485
                        int x = 1;
240
 
                        Gdk.Color save = Gdk.Color.Zero;
 
486
 
 
487
                        col.CellSetCellData (tree.Model, iter, false, false);
 
488
 
241
489
                        foreach (CellRenderer cr in col.CellRenderers) {
242
490
                                if (!cr.Visible)
243
491
                                        continue;
 
492
 
244
493
                                if (cr is CellRendererText) {
245
494
                                        save = ((CellRendererText)cr).ForegroundGdk;
246
495
                                        ((CellRendererText)cr).ForegroundGdk = Style.Foreground (State);
247
496
                                }
 
497
 
248
498
                                int sp, wi, he, xo, yo;
249
499
                                col.CellGetPosition (cr, out sp, out wi);
250
 
                                Gdk.Rectangle colcrect = new Gdk.Rectangle (x, rect.Y, wi, rect.Height - 2);
251
 
                                cr.GetSize (tree, ref colcrect, out xo, out yo, out wi, out he);
252
 
                                int leftMargin = (int) ((colcrect.Width - wi) * cr.Xalign);
253
 
                                int rightMargin = (int) ((colcrect.Height - he) * cr.Yalign);
254
 
                                Gdk.Rectangle crect = new Gdk.Rectangle (colcrect.X + leftMargin, colcrect.Y + rightMargin + 1, wi, he);
255
 
                                cr.Render (this.GdkWindow, tree, colcrect, crect, rect, CellRendererState.Focused);
256
 
                                x += colcrect.Width + col.Spacing + 1;
 
500
                                Gdk.Rectangle bgrect = new Gdk.Rectangle (x, expose.Y, wi, expose.Height - 2);
 
501
                                cr.GetSize (tree, ref bgrect, out xo, out yo, out wi, out he);
 
502
                                int leftMargin = (int) ((bgrect.Width - wi) * cr.Xalign);
 
503
                                int topMargin = (int) ((bgrect.Height - he) * cr.Yalign);
 
504
                                Gdk.Rectangle cellrect = new Gdk.Rectangle (bgrect.X + leftMargin, bgrect.Y + topMargin + 1, wi, he);
 
505
                                cr.Render (this.GdkWindow, this, bgrect, cellrect, expose, CellRendererState.Focused);
 
506
                                x += bgrect.Width + col.Spacing + 1;
 
507
 
257
508
                                if (cr is CellRendererText) {
258
509
                                        ((CellRendererText)cr).ForegroundGdk = save;
259
510
                                }
260
511
                        }
 
512
 
261
513
                        return true;
262
514
                }
263
515