~ubuntu-branches/ubuntu/maverick/monodevelop/maverick

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Ide/MonoDevelop.Components.DockToolbars/DockToolbarFrame.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2010-07-05 13:00:05 UTC
  • mfrom: (1.2.8 upstream) (1.3.9 experimental)
  • Revision ID: james.westby@ubuntu.com-20100705130005-d6hp4k5gcn1xkj8c
Tags: 2.4+dfsg-1ubuntu1
* debian/patches/remove_support_for_moonlight.patch,
  debian/patches/dont_add_moonlight_to_core_addins.patch,
  debian/control:
  + Enable support for Moonlight
* debian/rules:
  + Ensure Moonlight addin isn't shipped in main MonoDevelop package by
    mistake

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// DockToolbarFrame.cs
 
3
//
 
4
// Author:
 
5
//   Lluis Sanchez Gual
 
6
//
 
7
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person obtaining
 
10
// a copy of this software and associated documentation files (the
 
11
// "Software"), to deal in the Software without restriction, including
 
12
// without limitation the rights to use, copy, modify, merge, publish,
 
13
// distribute, sublicense, and/or sell copies of the Software, and to
 
14
// permit persons to whom the Software is furnished to do so, subject to
 
15
// the following conditions:
 
16
// 
 
17
// The above copyright notice and this permission notice shall be
 
18
// included in all copies or substantial portions of the Software.
 
19
// 
 
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
27
//
 
28
 
 
29
using System;
 
30
using Gtk;
 
31
using Gdk;
 
32
using System.Collections;
 
33
using System.Xml;
 
34
using System.Xml.Serialization;
 
35
using System.Collections.Generic;
 
36
 
 
37
namespace MonoDevelop.Components.DockToolbars
 
38
{
 
39
        public class DockToolbarFrame: EventBox
 
40
        {
 
41
                DockToolbarPanel[] panels;
 
42
                DockToolbarPanel targetPanel;
 
43
                VBox vbox;
 
44
                VBox contentBox;
 
45
                DockToolbar dragBar;
 
46
                int xDragDif, yDragDif;
 
47
                List<DockToolbar> bars = new List<DockToolbar> ();
 
48
                
 
49
                Dictionary<string,DockToolbarStatus[]> layouts = new Dictionary<string,DockToolbarStatus[]> ();
 
50
                string currentLayout = "";
 
51
                
 
52
                Cursor handCursor = new Cursor (CursorType.Fleur);
 
53
                
 
54
                public DockToolbarFrame ()
 
55
                {
 
56
                        vbox = new VBox ();
 
57
                        Add (vbox);
 
58
                        
 
59
                        DockToolbarPanel topPanel = new DockToolbarPanel (this, Placement.Top);
 
60
                        DockToolbarPanel bottomPanel = new DockToolbarPanel (this, Placement.Bottom);
 
61
                        DockToolbarPanel leftPanel = new DockToolbarPanel (this, Placement.Left);
 
62
                        DockToolbarPanel rightPanel = new DockToolbarPanel (this, Placement.Right);
 
63
 
 
64
                        panels = new DockToolbarPanel [4];
 
65
                        panels [(int)Placement.Top] = topPanel;
 
66
                        panels [(int)Placement.Bottom] = bottomPanel;
 
67
                        panels [(int)Placement.Left] = leftPanel;
 
68
                        panels [(int)Placement.Right] = rightPanel;
 
69
                
 
70
                        vbox.PackStart (topPanel, false, false, 0);
 
71
                        
 
72
                        HBox hbox = new HBox ();
 
73
                        contentBox = new VBox ();
 
74
 
 
75
                        hbox.PackStart (leftPanel, false, false, 0);
 
76
                        hbox.PackStart (contentBox, true, true, 0);
 
77
                        hbox.PackStart (rightPanel, false, false, 0);
 
78
                        
 
79
                        vbox.PackStart (hbox, true, true, 0);
 
80
                        vbox.PackStart (bottomPanel, false, false, 0);
 
81
                        
 
82
                        this.Events = EventMask.ButtonPressMask | EventMask.ButtonReleaseMask | EventMask.PointerMotionMask; 
 
83
                        ShowAll ();
 
84
                }
 
85
                
 
86
                public void AddContent (Widget w)
 
87
                {
 
88
                        contentBox.PackStart (w, true, true, 0);
 
89
                }
 
90
                
 
91
                public void RemoveContent (Widget w)
 
92
                {
 
93
                        contentBox.Remove (w);
 
94
                }
 
95
                
 
96
                public string CurrentLayout {
 
97
                        get { return currentLayout; }
 
98
                        set {
 
99
                                if (value != currentLayout) {
 
100
                                        SaveCurrentLayout ();
 
101
                                        RestoreLayout (value);
 
102
                                }
 
103
                        }
 
104
                }
 
105
                
 
106
                public DockToolbarFrameStatus GetStatus ()
 
107
                {
 
108
                        SaveCurrentLayout ();
 
109
                        DockToolbarFrameStatus col = new DockToolbarFrameStatus ();
 
110
                        col.Version = DockToolbarFrameStatus.CurrentVersion;
 
111
                        
 
112
                        foreach (var layout in layouts) {
 
113
                                col.Status.Add (new DockToolbarFrameLayout () {
 
114
                                        Id = layout.Key,
 
115
                                        Bars = layout.Value,
 
116
                                });
 
117
                        }
 
118
                        return col;
 
119
                }
 
120
                
 
121
                public void SetStatus (DockToolbarFrameStatus status)
 
122
                {
 
123
                        layouts.Clear ();
 
124
                        if (status != null && status.Status != null) {
 
125
                                foreach (DockToolbarFrameLayout c in status.Status) {
 
126
                                        if (status.Version < 2) {
 
127
                                                // Convert from old to new toolbar id
 
128
                                                foreach (DockToolbarStatus ts in c.Bars)
 
129
                                                        ts.BarId = ConvertToolbarId (ts.BarId);
 
130
                                        }
 
131
                                        layouts [c.Id] = c.Bars;
 
132
                                }
 
133
                        }
 
134
                        RestoreLayout ("");
 
135
                }
 
136
                
 
137
                public void SaveStatus (XmlWriter writer)
 
138
                {
 
139
                        XmlSerializer ser = new XmlSerializer (typeof(DockToolbarFrameStatus));
 
140
                        ser.Serialize (writer, GetStatus ());
 
141
                }
 
142
                
 
143
                public void LoadStatus (XmlReader reader)
 
144
                {
 
145
                        layouts.Clear ();
 
146
                        XmlSerializer ser = new XmlSerializer (typeof(DockToolbarFrameStatus));
 
147
                        DockToolbarFrameStatus col = (DockToolbarFrameStatus) ser.Deserialize (reader);
 
148
                        SetStatus (col);
 
149
                } 
 
150
                
 
151
                string ConvertToolbarId (string id)
 
152
                {
 
153
                        // Old MD versions include the display name of the toolbar in the id.
 
154
                        // New MD versions use a different id composition.
 
155
                        // This method translates the id from the old to the new format (when possible)
 
156
                        
 
157
                        int i = id.LastIndexOf ('/');
 
158
                        if (i == -1)
 
159
                                return id;
 
160
                        
 
161
                        string baseId = id.Substring (0, i + 1);
 
162
                        
 
163
                        foreach (DockToolbar t in bars) {
 
164
                                if (t.Id == id)
 
165
                                        return id;
 
166
                                if (baseId + t.Title == id)
 
167
                                        return t.Id;
 
168
                        }
 
169
                        return id;
 
170
                }
 
171
                
 
172
                public IDockToolbar AddBar (DockToolbar bar)
 
173
                {
 
174
                        return AddBar (bar, Placement.Top, true);
 
175
                }
 
176
                
 
177
                public IDockToolbar AddBar (DockToolbar bar, Placement defaultPanel, bool defaultVisible)
 
178
                {
 
179
                        bar.SetParentFrame (this);
 
180
                        bars.Add (bar);
 
181
                        
 
182
                        DockToolbarPosition pos = new DockedPosition (defaultPanel);
 
183
                        DockToolbarStatus s = new DockToolbarStatus (bar.Id, defaultVisible, pos);
 
184
                        bar.DefaultStatus = s;
 
185
                        bar.Status = s;
 
186
                        
 
187
                        return bar;
 
188
                }
 
189
                
 
190
                public void RemoveToolbar(DockToolbar bar)
 
191
                {
 
192
                        IDockToolbar db = (IDockToolbar)bar;
 
193
                        db.Visible = false;
 
194
                        bar.Destroy();
 
195
                        bars.Remove(bar);
 
196
                }
 
197
 
 
198
                public void ClearToolbars ()
 
199
                {
 
200
                        foreach (DockToolbar bar in bars) {
 
201
                                IDockToolbar db = (IDockToolbar) bar;
 
202
                                db.Visible = false;
 
203
                                bar.Destroy ();
 
204
                        }
 
205
                        bars.Clear ();
 
206
                }
 
207
                
 
208
                public IDockToolbar GetBar (string id)
 
209
                {
 
210
                        foreach (DockToolbar bar in bars)
 
211
                                if (bar.Id == id) return bar;
 
212
                        return null;
 
213
                }
 
214
                
 
215
                public ICollection<DockToolbar> Toolbars {
 
216
                        get { return bars; }
 
217
                }
 
218
                
 
219
                public void ResetToolbarPositions ()
 
220
                {
 
221
                        foreach (DockToolbarPanel panel in panels)
 
222
                                panel.ResetBarPositions (false);
 
223
                }
 
224
                
 
225
                void SaveCurrentLayout ()
 
226
                {
 
227
                        DockToolbarStatus[] status = SaveStatus ();
 
228
                        layouts [currentLayout] = status;
 
229
                }
 
230
                
 
231
                void RestoreLayout (string layout)
 
232
                {
 
233
                        DockToolbarStatus[] status;
 
234
                        layouts.TryGetValue (layout, out status);
 
235
                        RestoreStatus (status);
 
236
                        currentLayout = layout;
 
237
                }
 
238
                
 
239
                internal void DeleteLayout (string layout)
 
240
                {
 
241
                        if (layouts.ContainsKey (layout))
 
242
                                layouts.Remove (layout);
 
243
                }
 
244
                
 
245
                DockToolbarStatus[] SaveStatus ()
 
246
                {
 
247
                        DockToolbarStatus[] status = new DockToolbarStatus [bars.Count];
 
248
                        for (int n=0; n<bars.Count; n++) {
 
249
                                DockToolbar bar = (DockToolbar) bars [n];
 
250
                                status [n] = bar.Status;
 
251
                        }
 
252
                        return status;
 
253
                }
 
254
                
 
255
                void RestoreStatus (DockToolbarStatus[] status)
 
256
                {
 
257
                        foreach (IDockToolbar b in bars)
 
258
                                b.Visible = false;
 
259
                        
 
260
                        if (status == null) {
 
261
                                foreach (DockToolbar bar in bars)
 
262
                                        bar.Status = bar.DefaultStatus;
 
263
                        } else {
 
264
                                foreach (DockToolbarStatus s in status) {
 
265
                                        DockToolbar bar = (DockToolbar) GetBar (s.BarId);
 
266
                                        if (bar != null)
 
267
                                                bar.Status = s;
 
268
                                }
 
269
                        }
 
270
                }
 
271
                
 
272
                internal int DockMargin {
 
273
                        get { return 7; }
 
274
                }
 
275
                
 
276
                internal void DockToolbar (DockToolbar bar, Placement placement, int offset, int row)
 
277
                {
 
278
                        DockToolbarPanel p = GetPanel (placement);
 
279
                        if (row != -1)
 
280
                                p.AddDockToolbar (bar, offset, row);
 
281
                        else
 
282
                                p.AddDockToolbar (bar);
 
283
                }
 
284
                
 
285
                internal void FloatBar (DockToolbar bar, Orientation orientation, int x, int y)
 
286
                {
 
287
                        FloatingDock fdock = new FloatingDock (this);
 
288
                        fdock.Move (x, y);
 
289
                        bar.ResetSize ();
 
290
                        fdock.Attach (bar);
 
291
                        bar.Orientation = orientation;
 
292
                }
 
293
                
 
294
                internal Gtk.Window TopWindow {
 
295
                        get {
 
296
                                Widget w = Parent;
 
297
                                while (w != null && !(w is Gtk.Window))
 
298
                                        w = w.Parent;
 
299
                                return (Gtk.Window) w;
 
300
                        }
 
301
                }
 
302
                
 
303
                DockToolbarPanel GetPanel (Placement o)
 
304
                {
 
305
                        return panels [(int)o];
 
306
                }
 
307
                
 
308
                protected override bool OnMotionNotifyEvent (EventMotion e)
 
309
                {
 
310
                        if (dragBar != null) {
 
311
                                int sx,sy;
 
312
                                this.GdkWindow.GetOrigin (out sx, out sy);
 
313
                                int rx = (int)e.XRoot - sx;
 
314
                                int ry = (int)e.YRoot - sy;
 
315
                                
 
316
                                if (dragBar.Floating) {
 
317
                                        bool foundPanel = false;
 
318
                                        dragBar.FloatingDock.Move ((int)e.XRoot + xDragDif, (int)e.YRoot + yDragDif);
 
319
                                        Rectangle barRect = new Rectangle (rx + xDragDif, ry + yDragDif, dragBar.Allocation.Width, dragBar.DefaultHeight);
 
320
                                        foreach (DockToolbarPanel p in panels) {
 
321
                                                if (p.Allocation.IntersectsWith (barRect)) {
 
322
                                                        if (targetPanel != null && targetPanel != p)
 
323
                                                                targetPanel.EndDragBar (dragBar);
 
324
                                                        p.Reposition (dragBar, rx, ry, xDragDif, yDragDif);
 
325
                                                        targetPanel = p;
 
326
                                                        foundPanel = true;
 
327
                                                        break;
 
328
                                                }
 
329
                                        }
 
330
                                        if (!foundPanel && targetPanel != null)
 
331
                                                targetPanel.EndDragBar (dragBar);
 
332
                                } else {
 
333
                                        DockToolbarPanel panel = (DockToolbarPanel) dragBar.Parent;
 
334
                                        panel.Reposition (dragBar, rx, ry, xDragDif, yDragDif);
 
335
                                }
 
336
                        }
 
337
                        return base.OnMotionNotifyEvent (e);
 
338
                }
 
339
                
 
340
                internal void StartDragBar (DockToolbar bar, int x, int y, uint time)
 
341
                {
 
342
                        dragBar = bar;
 
343
                        xDragDif = -x;
 
344
                        yDragDif = -y;
 
345
                        Pointer.Grab (this.GdkWindow, false, EventMask.ButtonPressMask | EventMask.ButtonReleaseMask | EventMask.PointerMotionMask, null, handCursor, time);
 
346
                        if (!bar.Floating) {
 
347
                                DockToolbarPanel panel = (DockToolbarPanel) dragBar.Parent;
 
348
                                panel.StartDragBar (bar);
 
349
                        }
 
350
                }
 
351
                
 
352
                internal void EndDragBar (DockToolbar bar, uint time)
 
353
                {
 
354
                        Pointer.Ungrab (time);
 
355
                        if (targetPanel != null) {
 
356
                                targetPanel.DropDragBar (bar);
 
357
                                targetPanel.EndDragBar (bar);
 
358
                        }
 
359
                        dragBar = null;
 
360
                }
 
361
                
 
362
                protected override bool OnButtonReleaseEvent (EventButton e)
 
363
                {
 
364
                        if (dragBar != null)
 
365
                                EndDragBar (dragBar, e.Time);
 
366
 
 
367
                        return base.OnButtonReleaseEvent (e);
 
368
                }
 
369
                
 
370
                protected override bool OnButtonPressEvent (Gdk.EventButton e)
 
371
                {
 
372
                        if (e.Button == 3) {
 
373
                                int sx,sy;
 
374
                                this.GdkWindow.GetOrigin (out sx, out sy);
 
375
                                int rx = (int)e.XRoot - sx;
 
376
                                int ry = (int)e.YRoot - sy;
 
377
                                
 
378
                                foreach (DockToolbarPanel p in panels) {
 
379
                                        if (p.Allocation.Contains (rx, ry))
 
380
                                                OnPanelClick (e, p.Placement);
 
381
                                }
 
382
                        }
 
383
                        return base.OnButtonPressEvent (e);
 
384
                }
 
385
                
 
386
                protected virtual void OnPanelClick (Gdk.EventButton e, Placement placement)
 
387
                {
 
388
                }
 
389
                
 
390
                protected override void OnDestroyed ()
 
391
                {
 
392
                        if (handCursor != null) {
 
393
                                handCursor.Dispose ();
 
394
                                handCursor = null;
 
395
                        }
 
396
                        base.OnDestroyed ();
 
397
                }
 
398
        }
 
399
}