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

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.MacDev/MonoDevelop.MacDev.PlistEditor/MacExpander.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:
1
 
// 
2
 
// DocumentTypeContainer.cs
3
 
//  
4
 
// Author:
5
 
//       Mike KrĆ¼ger <mkrueger@xamarin.com>
6
 
// 
7
 
// Copyright (c) 2011 Xamarin <http://xamarin.com>
8
 
// 
9
 
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
 
// of this software and associated documentation files (the "Software"), to deal
11
 
// in the Software without restriction, including without limitation the rights
12
 
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 
// copies of the Software, and to permit persons to whom the Software is
14
 
// furnished to do so, subject to the following conditions:
15
 
// 
16
 
// The above copyright notice and this permission notice shall be included in
17
 
// all copies or substantial portions of the Software.
18
 
// 
19
 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
 
// THE SOFTWARE.
26
 
using System;
27
 
using System.ComponentModel;
28
 
 
29
 
using Gdk;
30
 
 
31
 
using Mono.TextEditor;
32
 
using MonoDevelop.Components;
33
 
using MonoDevelop.Core;
34
 
using Gtk;
35
 
 
36
 
namespace MonoDevelop.MacDev.PlistEditor
37
 
{
38
 
        [ToolboxItem(true)]
39
 
        public class MacExpander : VBox
40
 
        {
41
 
                ExpanderHeader header;
42
 
                VBox contentBox;
43
 
                
44
 
                public string ContentLabel {
45
 
                        get {
46
 
                                
47
 
                                return header.Label;
48
 
                        }
49
 
                        set {
50
 
                                header.Label = !string.IsNullOrEmpty (value) ? value : GettextCatalog.GetString ("Untitled");
51
 
                                QueueDraw ();
52
 
                        }
53
 
                }
54
 
                bool expanded;
55
 
                public bool Expanded {
56
 
                        get {
57
 
                                return expanded;
58
 
                        }
59
 
                        set {
60
 
                                contentBox.Visible = expanded = value;
61
 
                                header.StartTimeout ();
62
 
                        }
63
 
                }
64
 
                
65
 
                public bool Expandable {
66
 
                        get;
67
 
                        set;
68
 
                }
69
 
                
70
 
                public bool Closeable {
71
 
                        get;
72
 
                        set;
73
 
                }
74
 
                
75
 
                class Border : DrawingArea
76
 
                {
77
 
                        protected override void OnSizeRequested (ref Requisition requisition)
78
 
                        {
79
 
                                base.OnSizeRequested (ref requisition);
80
 
                                requisition.Height = 1;
81
 
                        }
82
 
                        
83
 
                        protected override bool OnExposeEvent (EventExpose evnt)
84
 
                        {
85
 
                                using (var cr = CairoHelper.Create (evnt.Window)) {
86
 
                                        cr.Color = (Mono.TextEditor.HslColor)Style.Dark (StateType.Normal);
87
 
                                        cr.Rectangle (0, 0, Allocation.Width, Allocation.Height);
88
 
                                        cr.Fill ();
89
 
                                }
90
 
                                return true;
91
 
                        }
92
 
                }
93
 
                
94
 
                class ExpanderHeader : DrawingArea
95
 
                {
96
 
                        MacExpander container;
97
 
                        uint animationTimeout;
98
 
                        ExpanderStyle expanderStyle = ExpanderStyle.Expanded;
99
 
                        
100
 
                        public string Label {
101
 
                                get;
102
 
                                set;
103
 
                        }
104
 
                        
105
 
                        public void UpdateInitialExpanderState ()
106
 
                        {
107
 
                                expanderStyle = container.Expanded? ExpanderStyle.Expanded : ExpanderStyle.Collapsed;
108
 
                        }
109
 
                        
110
 
                        public ExpanderHeader (MacExpander container)
111
 
                        {
112
 
                                this.container = container;
113
 
                                Events |= EventMask.AllEventsMask;
114
 
                        }
115
 
                        
116
 
                        protected override void OnSizeRequested (ref Requisition requisition)
117
 
                        {
118
 
                                base.OnSizeRequested (ref requisition);
119
 
                                using (var layout = new Pango.Layout (PangoContext)) {
120
 
                                        layout.SetMarkup ("<b>" + Label + "</b>");
121
 
                                        int w, h;
122
 
                                        layout.GetPixelSize (out w, out h);
123
 
                                        requisition.Height = h + 4;
124
 
                                }
125
 
                                if (container.Closeable)
126
 
                                        requisition.Height += 4;
127
 
                        }
128
 
                        
129
 
                        void RemoveTimeout ()
130
 
                        {
131
 
                                if (animationTimeout != 0) {
132
 
                                        GLib.Source.Remove (animationTimeout);
133
 
                                        animationTimeout = 0;
134
 
                                }
135
 
                        }
136
 
                        
137
 
                        public void StartTimeout ()
138
 
                        {
139
 
                                RemoveTimeout ();
140
 
                                animationTimeout = GLib.Timeout.Add (50, delegate {
141
 
                                        bool finished = false;
142
 
                                        if (container.Expanded) {
143
 
                                                if (expanderStyle == ExpanderStyle.Collapsed) {
144
 
                                                        expanderStyle = ExpanderStyle.SemiExpanded;
145
 
                                                } else {
146
 
                                                        expanderStyle = ExpanderStyle.Expanded;
147
 
                                                        finished = true;
148
 
                                                }
149
 
                                        } else {
150
 
                                                if (expanderStyle == ExpanderStyle.Expanded) {
151
 
                                                        expanderStyle = ExpanderStyle.SemiCollapsed;
152
 
                                                } else {
153
 
                                                        expanderStyle = ExpanderStyle.Collapsed;
154
 
                                                        finished = true;
155
 
                                                }
156
 
                                        }
157
 
                                        QueueDraw ();
158
 
                                        if (finished) 
159
 
                                                animationTimeout = 0;
160
 
                                        return !finished;
161
 
                                });
162
 
                        }
163
 
                        
164
 
                        protected override void OnDestroyed ()
165
 
                        {
166
 
                                base.OnDestroyed ();
167
 
                                RemoveTimeout ();
168
 
                        }
169
 
                        
170
 
                        bool mouseOver;
171
 
                        protected override bool OnEnterNotifyEvent (EventCrossing evnt)
172
 
                        {
173
 
                                mouseOver = true;
174
 
                                QueueDraw ();
175
 
                                return base.OnEnterNotifyEvent (evnt);
176
 
                        }
177
 
                        
178
 
                        protected override bool OnLeaveNotifyEvent (EventCrossing evnt)
179
 
                        {
180
 
                                mouseOver = false;
181
 
                                QueueDraw ();
182
 
                                return base.OnLeaveNotifyEvent (evnt);
183
 
                        }
184
 
                        
185
 
                        double mx, my;
186
 
                        protected override bool OnMotionNotifyEvent (EventMotion evnt)
187
 
                        {
188
 
                                mx = evnt.X;
189
 
                                my = evnt.Y;
190
 
                                QueueDraw ();
191
 
                                
192
 
                                return base.OnMotionNotifyEvent (evnt);
193
 
                        }
194
 
                        
195
 
                        protected override bool OnButtonPressEvent (EventButton evnt)
196
 
                        {
197
 
                                return base.OnButtonPressEvent (evnt);
198
 
                        }
199
 
                        protected override bool OnButtonReleaseEvent (EventButton evnt)
200
 
                        {
201
 
                                if (container.Expandable && evnt.Button == 1)
202
 
                                        container.Expanded = !container.Expanded;
203
 
                                return base.OnButtonReleaseEvent (evnt);
204
 
                        }
205
 
                        
206
 
                        // constants taken from gtk
207
 
                        const int DEFAULT_EXPANDER_SIZE = 10;
208
 
                        const int DEFAULT_EXPANDER_SPACING = 2;
209
 
                        
210
 
                        Rectangle GetExpanderBounds ()
211
 
                        {
212
 
                                return new Rectangle (DEFAULT_EXPANDER_SPACING, 1 + (Allocation.Height - DEFAULT_EXPANDER_SIZE) / 2, DEFAULT_EXPANDER_SIZE, DEFAULT_EXPANDER_SIZE);
213
 
                        }
214
 
                        
215
 
                        protected override bool OnExposeEvent (EventExpose evnt)
216
 
                        {
217
 
                                var expanderBounds = GetExpanderBounds ();
218
 
                                using (var cr = CairoHelper.Create (evnt.Window)) {
219
 
                                        cr.Rectangle (0, 0, Allocation.Width, Allocation.Height);
220
 
                                        var lg = new Cairo.LinearGradient (0, 0, 0, Allocation.Height);
221
 
                                        var state = mouseOver ? StateType.Prelight : StateType.Normal;
222
 
                                        
223
 
                                        if (container.Closeable) {
224
 
                                                lg.AddColorStop (0, (Mono.TextEditor.HslColor)Style.Mid (state));
225
 
                                                lg.AddColorStop (1, (Mono.TextEditor.HslColor)Style.Dark (state));
226
 
                                        } else {
227
 
                                                lg.AddColorStop (0, (Mono.TextEditor.HslColor)Style.Light (state));
228
 
                                                lg.AddColorStop (1, (Mono.TextEditor.HslColor)Style.Mid (state));
229
 
                                        }
230
 
                                        
231
 
                                        cr.Pattern = lg;
232
 
                                        cr.Fill ();
233
 
                                        
234
 
                                        if (mouseOver && container.Expandable) {
235
 
                                                cr.Rectangle (0, 0, Allocation.Width, Allocation.Height);
236
 
                                                double rx = mx;
237
 
                                                double ry = my;
238
 
                                                Cairo.RadialGradient gradient = new Cairo.RadialGradient (rx, ry, Allocation.Width * 2, rx, ry, 2);
239
 
                                                gradient.AddColorStop (0, new Cairo.Color (0 ,0, 0, 0));
240
 
                                                Cairo.Color color = (Mono.TextEditor.HslColor)Style.Light (StateType.Normal);
241
 
                                                color.A = 0.2;
242
 
                                                gradient.AddColorStop (1, color);
243
 
                                                cr.Pattern = gradient;
244
 
                                                cr.Fill ();
245
 
                                        }
246
 
                                        cr.MoveTo (0, 0);
247
 
                                        cr.LineTo (0, Allocation.Height);
248
 
                                        cr.LineTo (Allocation.Width, Allocation.Height);
249
 
                                        cr.LineTo (Allocation.Width, 0);
250
 
                                        if (!container.Expandable)
251
 
                                                cr.LineTo (0, 0);
252
 
                                        cr.Color = (Mono.TextEditor.HslColor)Style.Dark (StateType.Normal);
253
 
                                        cr.Stroke ();
254
 
                                        
255
 
                                        using (var layout = new Pango.Layout (PangoContext)) {
256
 
                                                layout.SetMarkup ("<b>" + Label + "</b>");
257
 
                                                int w, h;
258
 
                                                layout.GetPixelSize (out w, out h);
259
 
                                                
260
 
                                                const int padding = 4;
261
 
                                                
262
 
                                                cr.MoveTo (container.Expandable ? expanderBounds.Right + padding : padding, (Allocation.Height - h) / 2);
263
 
                                                cr.Color = new Cairo.Color (0, 0, 0);
264
 
                                                cr.ShowLayout (layout);
265
 
                                        }
266
 
                                }
267
 
                                
268
 
                                if (container.Expandable) {
269
 
                                        
270
 
                                        var state2 = mouseOver ? StateType.Prelight : StateType.Normal;
271
 
                                        Style.PaintExpander (Style, 
272
 
                                                evnt.Window, 
273
 
                                                state2,
274
 
                                                evnt.Region.Clipbox, 
275
 
                                                this, 
276
 
                                                "expander",
277
 
                                                expanderBounds.X + expanderBounds.Width / 2, expanderBounds.Y + expanderBounds.Width / 2,
278
 
                                                expanderStyle);
279
 
                                }
280
 
                                
281
 
                                return base.OnExposeEvent (evnt);
282
 
                        }
283
 
                }
284
 
                
285
 
                Border border = new Border ();
286
 
                public MacExpander ()
287
 
                {
288
 
                        header = new ExpanderHeader (this);
289
 
                        PackStart (header, false, false, 0);
290
 
                        
291
 
                        contentBox = new VBox ();
292
 
                        contentBox.PackEnd (border, false, false, 0);
293
 
                        expanded = true;
294
 
                        
295
 
                        PackStart (contentBox, true, true, 0);
296
 
                        ShowAll ();
297
 
                }
298
 
                
299
 
                public void SetWidget (Gtk.Widget widget)
300
 
                {
301
 
                        contentBox.PackStart (widget, true, true, 0);
302
 
                        header.UpdateInitialExpanderState ();
303
 
                }
304
 
                
305
 
                protected override void OnRealized ()
306
 
                {
307
 
                        base.OnRealized ();
308
 
                        if (expanded)
309
 
                                contentBox.Show ();
310
 
                }
311
 
                
312
 
                protected override void OnUnrealized ()
313
 
                {
314
 
                        base.OnUnrealized ();
315
 
                        if (expanded)
316
 
                                contentBox.Hide ();
317
 
                }
318
 
        }
319
 
}
320