~ubuntu-branches/ubuntu/saucy/monodevelop/saucy-proposed

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Ide/MonoDevelop.Components.Docking/DockItem.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2010-09-10 16:54:48 UTC
  • mfrom: (19.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20100910165448-0rybfk25zd4o9431
Tags: 2.4+dfsg-2
* debian/patches/inject_Mono.Debugger.Soft_source.patch,
  debian/patches/use_system_Mono.Debugger.Soft.patch,
  debian/control:
  + Build against system Soft Debugger, since we now have a new
    enough Mono to match MonoDevelop's required API

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// DockItem.cs
 
3
//
 
4
// Author:
 
5
//   Lluis Sanchez Gual
 
6
//
 
7
 
 
8
//
 
9
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
 
10
//
 
11
// Permission is hereby granted, free of charge, to any person obtaining
 
12
// a copy of this software and associated documentation files (the
 
13
// "Software"), to deal in the Software without restriction, including
 
14
// without limitation the rights to use, copy, modify, merge, publish,
 
15
// distribute, sublicense, and/or sell copies of the Software, and to
 
16
// permit persons to whom the Software is furnished to do so, subject to
 
17
// the following conditions:
 
18
// 
 
19
// The above copyright notice and this permission notice shall be
 
20
// included in all copies or substantial portions of the Software.
 
21
// 
 
22
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
23
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
24
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
25
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
26
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
27
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
28
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
29
//
 
30
 
 
31
using System;
 
32
using System.Xml;
 
33
using Gtk;
 
34
using Mono.Unix;
 
35
 
 
36
namespace MonoDevelop.Components.Docking
 
37
{
 
38
        public class DockItem
 
39
        {
 
40
                Widget content;
 
41
                DockItemContainer widget;
 
42
                string defaultLocation;
 
43
                bool defaultVisible = true;
 
44
                DockItemStatus defaultStatus = DockItemStatus.Dockable;
 
45
                string id;
 
46
                DockFrame frame;
 
47
                int defaultWidth = -1;
 
48
                int defaultHeight = -1;
 
49
                string label;
 
50
                Gdk.Pixbuf icon;
 
51
                bool expand;
 
52
                bool drawFrame = true;
 
53
                DockItemBehavior behavior;
 
54
                Gtk.Window floatingWindow;
 
55
                DockBarItem dockBarItem;
 
56
                bool lastVisibleStatus;
 
57
                bool lastContentVisibleStatus;
 
58
                bool gettingContent;
 
59
                bool isPositionMarker;
 
60
                bool stickyVisible;
 
61
                IDockItemLabelProvider dockLabelProvider;
 
62
                DockItemToolbar toolbarTop;
 
63
                DockItemToolbar toolbarBottom;
 
64
                DockItemToolbar toolbarLeft;
 
65
                DockItemToolbar toolbarRight;
 
66
                
 
67
                public event EventHandler VisibleChanged;
 
68
                public event EventHandler ContentVisibleChanged;
 
69
                public event EventHandler ContentRequired;
 
70
                
 
71
                internal DockItem (DockFrame frame, string id)
 
72
                {
 
73
                        this.frame = frame;
 
74
                        this.id = id;
 
75
                }
 
76
                
 
77
                internal DockItem (DockFrame frame, Widget w, string id)
 
78
                {
 
79
                        this.frame = frame;
 
80
                        this.id = id;
 
81
                        content = w;
 
82
                }
 
83
                
 
84
                public string Id {
 
85
                        get { return id; }
 
86
                }
 
87
 
 
88
                internal bool StickyVisible {
 
89
                        get { return stickyVisible; }
 
90
                        set { stickyVisible = value; }
 
91
                }
 
92
                
 
93
                public string Label {
 
94
                        get { return label ?? string.Empty; }
 
95
                        set {
 
96
                                label = value; 
 
97
                                if (widget != null)
 
98
                                        widget.Label = label;
 
99
                                frame.UpdateTitle (this);
 
100
                                if (floatingWindow != null)
 
101
                                        floatingWindow.Title = GetWindowTitle ();
 
102
                        }
 
103
                }
 
104
 
 
105
                public bool Visible {
 
106
                        get {
 
107
                                return frame.GetVisible (this); 
 
108
                        }
 
109
                        set {
 
110
                                stickyVisible = value;
 
111
                                frame.SetVisible (this, value);
 
112
                                UpdateVisibleStatus ();
 
113
                        }
 
114
                }
 
115
                
 
116
                public bool VisibleInLayout (string layout)
 
117
                {
 
118
                        return frame.GetVisible (this, layout); 
 
119
                }
 
120
                
 
121
                public DockItemStatus Status {
 
122
                        get {
 
123
                                return frame.GetStatus (this); 
 
124
                        }
 
125
                        set {
 
126
                                frame.SetStatus (this, value);
 
127
                        }
 
128
                }
 
129
                
 
130
                public IDockItemLabelProvider DockLabelProvider {
 
131
                        get { return this.dockLabelProvider; }
 
132
                        set { this.dockLabelProvider = value; }
 
133
                }
 
134
                
 
135
                internal DockItemContainer Widget {
 
136
                        get {
 
137
                                if (widget == null) {
 
138
                                        widget = new DockItemContainer (frame, this);
 
139
                                        widget.Visible = false; // Required to ensure that the Shown event is fired
 
140
                                        widget.Label = label;
 
141
                                        widget.Shown += SetupContent;
 
142
                                }
 
143
                                return widget;
 
144
                        }
 
145
                }
 
146
                
 
147
                void SetupContent (object ob, EventArgs args)
 
148
                {
 
149
                        widget.Shown -= SetupContent;
 
150
                        
 
151
                        if (ContentRequired != null) {
 
152
                                gettingContent = true;
 
153
                                try {
 
154
                                        ContentRequired (this, EventArgs.Empty);
 
155
                                } finally {
 
156
                                        gettingContent = false;
 
157
                                }
 
158
                        }
 
159
 
 
160
                        widget.UpdateContent ();
 
161
                        widget.Shown += delegate {
 
162
                                UpdateContentVisibleStatus ();
 
163
                        };
 
164
                        widget.Hidden += delegate {
 
165
                                UpdateContentVisibleStatus ();
 
166
                        };
 
167
                        widget.ParentSet += delegate {
 
168
                                UpdateContentVisibleStatus ();
 
169
                        };
 
170
                        UpdateContentVisibleStatus ();
 
171
                }
 
172
                
 
173
                public Widget Content {
 
174
                        get {
 
175
                                return content;
 
176
                        }
 
177
                        set {
 
178
                                content = value;
 
179
                                if (!gettingContent && widget != null)
 
180
                                        widget.UpdateContent ();
 
181
                        }
 
182
                }
 
183
                
 
184
                public DockItemToolbar GetToolbar (PositionType position)
 
185
                {
 
186
                        switch (position) {
 
187
                                case PositionType.Top:
 
188
                                        if (toolbarTop == null)
 
189
                                                toolbarTop = new DockItemToolbar (this, PositionType.Top);
 
190
                                        return toolbarTop;
 
191
                                case PositionType.Bottom:
 
192
                                        if (toolbarBottom == null)
 
193
                                                toolbarBottom = new DockItemToolbar (this, PositionType.Bottom);
 
194
                                        return toolbarBottom;
 
195
                                case PositionType.Left:
 
196
                                        if (toolbarLeft == null)
 
197
                                                toolbarLeft = new DockItemToolbar (this, PositionType.Left);
 
198
                                        return toolbarLeft;
 
199
                                case PositionType.Right:
 
200
                                        if (toolbarRight == null)
 
201
                                                toolbarRight = new DockItemToolbar (this, PositionType.Right);
 
202
                                        return toolbarRight;
 
203
                                default: throw new ArgumentException ();
 
204
                        }
 
205
                }
 
206
                
 
207
                internal bool HasWidget {
 
208
                        get { return widget != null; }
 
209
                }
 
210
                
 
211
                public string DefaultLocation {
 
212
                        get { return defaultLocation; }
 
213
                        set { defaultLocation = value; }
 
214
                }
 
215
 
 
216
                public bool DefaultVisible {
 
217
                        get { return defaultVisible; }
 
218
                        set { defaultVisible = value; }
 
219
                }
 
220
 
 
221
                public DockItemStatus DefaultStatus {
 
222
                        get { return defaultStatus; }
 
223
                        set { defaultStatus = value; }
 
224
                }
 
225
 
 
226
                public int DefaultWidth {
 
227
                        get {
 
228
                                return defaultWidth;
 
229
                        }
 
230
                        set {
 
231
                                defaultWidth = value;
 
232
                        }
 
233
                }
 
234
 
 
235
                public int DefaultHeight {
 
236
                        get {
 
237
                                return defaultHeight;
 
238
                        }
 
239
                        set {
 
240
                                defaultHeight = value;
 
241
                        }
 
242
                }
 
243
 
 
244
                public Gdk.Pixbuf Icon {
 
245
                        get {
 
246
                                return icon;
 
247
                        }
 
248
                        set {
 
249
                                icon = value;
 
250
                        }
 
251
                }
 
252
 
 
253
                public DockItemBehavior Behavior {
 
254
                        get {
 
255
                                return behavior;
 
256
                        }
 
257
                        set {
 
258
                                behavior = value;
 
259
                                if (widget != null)
 
260
                                        widget.UpdateBehavior ();
 
261
                        }
 
262
                }
 
263
 
 
264
                public bool Expand {
 
265
                        get {
 
266
                                return expand;
 
267
                        }
 
268
                        set {
 
269
                                expand = value;
 
270
                        }
 
271
                }
 
272
 
 
273
                public bool DrawFrame {
 
274
                        get {
 
275
                                return drawFrame;
 
276
                        }
 
277
                        set {
 
278
                                drawFrame = value;
 
279
                        }
 
280
                }
 
281
                
 
282
                public void Present (bool giveFocus)
 
283
                {
 
284
                        if (dockBarItem != null)
 
285
                                dockBarItem.Present (Status == DockItemStatus.AutoHide || giveFocus);
 
286
                        else
 
287
                                frame.Present (this, Status == DockItemStatus.AutoHide || giveFocus);
 
288
                }
 
289
 
 
290
                public bool ContentVisible {
 
291
                        get {
 
292
                                if (widget == null)
 
293
                                        return false;
 
294
                                return widget.Parent != null && widget.Visible;
 
295
                        }
 
296
                }
 
297
                
 
298
                public void SetDockLocation (string location)
 
299
                {
 
300
                        frame.SetDockLocation (this, location);
 
301
                }
 
302
 
 
303
                internal void SetFocus ()
 
304
                {
 
305
                        SetFocus (Content);
 
306
                }
 
307
                
 
308
                internal static void SetFocus (Widget w)
 
309
                {
 
310
                        w.ChildFocus (DirectionType.Down);
 
311
 
 
312
                        Window win = w.Toplevel as Gtk.Window;
 
313
                        if (win == null)
 
314
                                return;
 
315
 
 
316
                        // Make sure focus is not given to internal children
 
317
                        if (win.Focus != null) {
 
318
                                Container c = win.Focus.Parent as Container;
 
319
                                if (c.Children.Length == 0)
 
320
                                        win.Focus = c;
 
321
                        }
 
322
                }
 
323
                
 
324
                internal void UpdateVisibleStatus ()
 
325
                {
 
326
                        bool vis = frame.GetVisible (this);
 
327
                        if (vis != lastVisibleStatus) {
 
328
                                lastVisibleStatus = vis;
 
329
                                if (VisibleChanged != null)
 
330
                                        VisibleChanged (this, EventArgs.Empty);
 
331
                        }
 
332
                        UpdateContentVisibleStatus ();
 
333
                }
 
334
                
 
335
                internal void UpdateContentVisibleStatus ()
 
336
                {
 
337
                        bool vis = ContentVisible;
 
338
                        if (vis != lastContentVisibleStatus) {
 
339
                                lastContentVisibleStatus = vis;
 
340
                                if (ContentVisibleChanged != null)
 
341
                                        ContentVisibleChanged (this, EventArgs.Empty);
 
342
                        }
 
343
                }
 
344
                
 
345
                internal void ShowWidget ()
 
346
                {
 
347
                        if (floatingWindow != null)
 
348
                                floatingWindow.Show ();
 
349
                        if (dockBarItem != null)
 
350
                                dockBarItem.Show ();
 
351
                        Widget.Show ();
 
352
                }
 
353
                
 
354
                internal void HideWidget ()
 
355
                {
 
356
                        if (floatingWindow != null)
 
357
                                floatingWindow.Hide ();
 
358
                        else if (dockBarItem != null)
 
359
                                dockBarItem.Hide ();
 
360
                        else if (widget != null)
 
361
                                widget.Hide ();
 
362
                }
 
363
                
 
364
                internal void SetFloatMode (Gdk.Rectangle rect)
 
365
                {
 
366
                        ResetBarUndockMode ();
 
367
                        if (floatingWindow == null) {
 
368
                                if (Widget.Parent != null)
 
369
                                        Widget.Unparent ();
 
370
                                floatingWindow = new Window (GetWindowTitle ());
 
371
                                floatingWindow.TransientFor = frame.Toplevel as Gtk.Window;
 
372
                                floatingWindow.TypeHint = Gdk.WindowTypeHint.Utility;
 
373
                                floatingWindow.Add (Widget);
 
374
                                floatingWindow.DeleteEvent += delegate (object o, DeleteEventArgs a) {
 
375
                                        if (behavior == DockItemBehavior.CantClose)
 
376
                                                Status = DockItemStatus.Dockable;
 
377
                                        else
 
378
                                                Visible = false;
 
379
                                        a.RetVal = true;
 
380
                                };
 
381
                        }
 
382
                        floatingWindow.Move (rect.X, rect.Y);
 
383
                        floatingWindow.Resize (rect.Width, rect.Height);
 
384
                        floatingWindow.Show ();
 
385
                        Widget.UpdateBehavior ();
 
386
                        Widget.Show ();
 
387
                }
 
388
                
 
389
                internal void ResetFloatMode ()
 
390
                {
 
391
                        if (floatingWindow != null) {
 
392
                                floatingWindow.Remove (Widget);
 
393
                                floatingWindow.Destroy ();
 
394
                                floatingWindow = null;
 
395
                                widget.UpdateBehavior ();
 
396
                        }
 
397
                }
 
398
                
 
399
                internal Gdk.Rectangle FloatingPosition {
 
400
                        get {
 
401
                                if (floatingWindow != null) {
 
402
                                        int x,y,w,h;
 
403
                                        floatingWindow.GetPosition (out x, out y);
 
404
                                        floatingWindow.GetSize (out w, out h);
 
405
                                        return new Gdk.Rectangle (x,y,w,h);
 
406
                                }
 
407
                                else
 
408
                                        return Gdk.Rectangle.Zero;
 
409
                        }
 
410
                }
 
411
                
 
412
                internal void ResetMode ()
 
413
                {
 
414
                        ResetFloatMode ();
 
415
                        ResetBarUndockMode ();
 
416
                }
 
417
                
 
418
                internal void SetAutoHideMode (Gtk.PositionType pos, int size)
 
419
                {
 
420
                        ResetMode ();
 
421
                        if (widget != null) {
 
422
                                widget.Hide (); // Avoids size allocation warning
 
423
                                widget.Unparent ();
 
424
                        }
 
425
                        dockBarItem = frame.BarDock (pos, this, size);
 
426
                        if (widget != null)
 
427
                                widget.UpdateBehavior ();
 
428
                }
 
429
                
 
430
                void ResetBarUndockMode ()
 
431
                {
 
432
                        if (dockBarItem != null) {
 
433
                                dockBarItem.Close ();
 
434
                                dockBarItem = null;
 
435
                                if (widget != null)
 
436
                                        widget.UpdateBehavior ();
 
437
                        }
 
438
                }
 
439
                
 
440
                internal int AutoHideSize {
 
441
                        get {
 
442
                                if (dockBarItem != null)
 
443
                                        return dockBarItem.Size;
 
444
                                else
 
445
                                        return -1;
 
446
                        }
 
447
                }
 
448
 
 
449
                internal bool IsPositionMarker {
 
450
                        get {
 
451
                                return isPositionMarker;
 
452
                        }
 
453
                        set {
 
454
                                isPositionMarker = value;
 
455
                        }
 
456
                }
 
457
                
 
458
                string GetWindowTitle ()
 
459
                {
 
460
                        if (Label.IndexOf ('<') == -1)
 
461
                                return Label;
 
462
                        try {
 
463
                                XmlDocument doc = new XmlDocument ();
 
464
                                doc.LoadXml ("<a>" + Label + "</a>");
 
465
                                return doc.InnerText;
 
466
                        } catch {
 
467
                                return label;
 
468
                        }
 
469
                }
 
470
                
 
471
                internal void ShowDockPopupMenu (uint time)
 
472
                {
 
473
                        Menu menu = new Menu ();
 
474
                        
 
475
                        // Hide menuitem
 
476
                        if ((Behavior & DockItemBehavior.CantClose) == 0) {
 
477
                                MenuItem mitem = new MenuItem (Catalog.GetString("Hide"));
 
478
                                mitem.Activated += delegate { Visible = false; };
 
479
                                menu.Append (mitem);
 
480
                        }
 
481
 
 
482
                        CheckMenuItem citem;
 
483
                        
 
484
                        // Dockable menuitem
 
485
                        citem = new CheckMenuItem (Catalog.GetString("Dockable"));
 
486
                        citem.Active = Status == DockItemStatus.Dockable;
 
487
                        citem.DrawAsRadio = true;
 
488
                        citem.Toggled += delegate { Status = DockItemStatus.Dockable; };
 
489
                        menu.Append (citem);
 
490
 
 
491
                        // Floating menuitem
 
492
                        if ((Behavior & DockItemBehavior.NeverFloating) == 0) {
 
493
                                citem = new CheckMenuItem (Catalog.GetString("Floating"));
 
494
                                citem.Active = Status == DockItemStatus.Floating;
 
495
                                citem.DrawAsRadio = true;
 
496
                                citem.Toggled += delegate { Status = DockItemStatus.Floating; };
 
497
                                menu.Append (citem);
 
498
                        }
 
499
 
 
500
                        // Auto Hide menuitem
 
501
                        if ((Behavior & DockItemBehavior.CantAutoHide) == 0) {
 
502
                                citem = new CheckMenuItem (Catalog.GetString("Auto Hide"));
 
503
                                citem.Active = Status == DockItemStatus.AutoHide;
 
504
                                citem.DrawAsRadio = true;
 
505
                                citem.Toggled += delegate { Status = DockItemStatus.AutoHide; };
 
506
                                menu.Append (citem);
 
507
                        }
 
508
 
 
509
                        menu.ShowAll ();
 
510
                        menu.Popup (null, null, null, 3, time);
 
511
                }
 
512
        }
 
513
        
 
514
        public interface IDockItemLabelProvider
 
515
        {
 
516
                Gtk.Widget CreateLabel (Orientation orientation);
 
517
        }
 
518
}