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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.Mac/Xwt.Mac/ExpanderBackend.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
using System;
 
2
using System.Drawing;
 
3
 
 
4
using Xwt;
 
5
using Xwt.Backends;
 
6
 
 
7
using MonoMac.AppKit;
 
8
using MonoMac.Foundation;
 
9
 
 
10
namespace Xwt.Mac
 
11
{
 
12
        public class ExpanderBackend : ViewBackend<MacExpander, IExpandEventSink>, IExpanderBackend
 
13
        {
 
14
                MacExpander expander;
 
15
 
 
16
                public ExpanderBackend ()
 
17
                {
 
18
                        ViewObject = new MacExpander ();
 
19
                }
 
20
 
 
21
                public string Label {
 
22
                        get {
 
23
                                return Widget.Expander.Label;
 
24
                        }
 
25
                        set {
 
26
                                Widget.Expander.Label = value;
 
27
                        }
 
28
                }
 
29
 
 
30
                public bool Expanded {
 
31
                        get {
 
32
                                return Widget.Box.Expanded;
 
33
                        }
 
34
                        set {
 
35
                                Widget.Box.Expanded = value;
 
36
                                Widget.Expander.On = value;
 
37
                        }
 
38
                }
 
39
 
 
40
                public void SetContent (IWidgetBackend widget)
 
41
                {
 
42
                        Widget.Box.SetContent (GetWidget (widget));
 
43
                }
 
44
        }
 
45
 
 
46
        public class MacExpander : NSView, IViewObject
 
47
        {
 
48
                ExpanderWidget expander;
 
49
                CollapsibleBox box;
 
50
 
 
51
                public MacExpander ()
 
52
                {
 
53
                        SetFrameSize (new SizeF (80, 50 + CollapsibleBox.DefaultCollapsedHeight));
 
54
                        expander = new ExpanderWidget () {
 
55
                                Frame = new RectangleF (0, 0, 80, 21),
 
56
                                AutoresizingMask = NSViewResizingMask.WidthSizable
 
57
                        };
 
58
                        box = new CollapsibleBox () {
 
59
                                Frame = new RectangleF (0, 25, 80, 100),
 
60
                                AutoresizingMask = NSViewResizingMask.HeightSizable | NSViewResizingMask.WidthSizable
 
61
                        };
 
62
                        expander.DisclosureToggled += (sender, e) => box.Expanded = expander.On;
 
63
                        AutoresizesSubviews = true;
 
64
                        AddSubview (expander);
 
65
                        AddSubview (box);
 
66
                }
 
67
 
 
68
                public override bool IsFlipped {
 
69
                        get {
 
70
                                return true;
 
71
                        }
 
72
                }
 
73
 
 
74
                public ExpanderWidget Expander {
 
75
                        get {
 
76
                                return expander;
 
77
                        }
 
78
                }
 
79
 
 
80
                public CollapsibleBox Box {
 
81
                        get {
 
82
                                return box;
 
83
                        }
 
84
                }
 
85
 
 
86
                public Widget Frontend { get; set; }
 
87
                
 
88
                public NSView View {
 
89
                        get { return this; }
 
90
                }
 
91
                
 
92
                public void EnableEvent (Xwt.Backends.ButtonEvent ev)
 
93
                {
 
94
                }
 
95
 
 
96
                public void DisableEvent (Xwt.Backends.ButtonEvent ev)
 
97
                {
 
98
                }
 
99
        }
 
100
 
 
101
        public class ExpanderWidget : NSView
 
102
        {
 
103
                public event EventHandler DisclosureToggled;
 
104
 
 
105
                NSTextView label;
 
106
                NSButton disclosure;
 
107
                NSGradient backgroundGradient;
 
108
                NSColor strokeColor;
 
109
 
 
110
                public ExpanderWidget ()
 
111
                {
 
112
                        label = new NSTextView () {
 
113
                                AutoresizingMask = NSViewResizingMask.MaxYMargin | NSViewResizingMask.WidthSizable,
 
114
                                Alignment = NSTextAlignment.Left,
 
115
                                Editable = false,
 
116
                                Selectable = false,
 
117
                                DrawsBackground = false,
 
118
                                Frame = new RectangleF (17, 3, 60, 13)
 
119
                        };
 
120
                        disclosure = new NSButton () {
 
121
                                BezelStyle = NSBezelStyle.Disclosure,
 
122
                                AutoresizingMask = NSViewResizingMask.MaxYMargin,
 
123
                                ImagePosition = NSCellImagePosition.ImageOnly,
 
124
                                Frame = new RectangleF (5, 4, 13, 13),
 
125
                                State = NSCellStateValue.On
 
126
                        };
 
127
                        disclosure.SetButtonType (NSButtonType.OnOff);
 
128
 
 
129
                        disclosure.AddObserver (this, new NSString ("cell.state"), NSKeyValueObservingOptions.New, IntPtr.Zero);
 
130
                        AutoresizesSubviews = true;
 
131
                        backgroundGradient = new NSGradient (NSColor.FromCalibratedRgba (0.93f, 0.93f, 0.97f, 1.0f),
 
132
                                                             NSColor.FromCalibratedRgba (0.74f, 0.76f, 0.83f, 1.0f));
 
133
                        strokeColor = NSColor.FromCalibratedRgba (0.60f, 0.60f, 0.60f, 1.0f);
 
134
 
 
135
                        AddSubview (label);
 
136
                        AddSubview (disclosure);
 
137
                }
 
138
 
 
139
                public override void ObserveValue (NSString keyPath, NSObject ofObject, NSDictionary change, IntPtr context)
 
140
                {
 
141
                        if (DisclosureToggled != null)
 
142
                                DisclosureToggled (this, EventArgs.Empty);
 
143
                }
 
144
 
 
145
                public string Label {
 
146
                        get {
 
147
                                return label.Value;
 
148
                        }
 
149
                        set {
 
150
                                label.Value = value;
 
151
                        }
 
152
                }
 
153
 
 
154
                public bool On {
 
155
                        get {
 
156
                                return disclosure.State == NSCellStateValue.On;
 
157
                        }
 
158
                        set {
 
159
                                disclosure.State = value ? NSCellStateValue.On : NSCellStateValue.Off;
 
160
                        }
 
161
                }
 
162
 
 
163
                public override void DrawRect (RectangleF dirtyRect)
 
164
                {
 
165
                        backgroundGradient.DrawInRect (Frame, -90);
 
166
                        if (dirtyRect == Frame) {
 
167
                                strokeColor.SetStroke ();
 
168
                                NSBezierPath.StrokeRect (dirtyRect);
 
169
                        }
 
170
                }
 
171
        }
 
172
 
 
173
        public class CollapsibleBox : NSBox
 
174
        {
 
175
                internal const float DefaultCollapsedHeight = 1f;
 
176
                bool expanded;
 
177
                float otherHeight;
 
178
 
 
179
                public CollapsibleBox ()
 
180
                {
 
181
                        expanded = true;
 
182
                        otherHeight = DefaultCollapsedHeight;
 
183
                        TitlePosition = NSTitlePosition.NoTitle;
 
184
                        BorderType = NSBorderType.NoBorder;
 
185
                        BoxType = NSBoxType.NSBoxPrimary;
 
186
                }
 
187
 
 
188
                public void SetContent (NSView view)
 
189
                {
 
190
                        ContentView = view;
 
191
                }
 
192
 
 
193
                public bool Expanded {
 
194
                        get { return expanded; }
 
195
                        set {
 
196
                                SetExpanded (value, true);
 
197
                        }
 
198
                }
 
199
 
 
200
                public void SetExpanded (bool value, bool animate)
 
201
                {
 
202
                        if (expanded != value) {
 
203
                                expanded = value;
 
204
                                var frameSize = Frame.Size;
 
205
                                SizeF newFrameSize = new SizeF (frameSize.Width, otherHeight);
 
206
                                otherHeight = frameSize.Height;
 
207
                                SetFrameSize (newFrameSize, animate);
 
208
                        }
 
209
                }
 
210
 
 
211
                public override bool IsFlipped {
 
212
                        get {
 
213
                                return true;
 
214
                        }
 
215
                }
 
216
 
 
217
                RectangleF FrameForNewSizePinnedToTopLeft (SizeF newFrameSize)
 
218
                {
 
219
                        var frame = Frame;
 
220
                        frame.Size = newFrameSize;
 
221
                        return frame;
 
222
                }
 
223
 
 
224
                public void SetFrameSize (SizeF newFrameSize, bool animating)
 
225
                {
 
226
                        RectangleF newFrame = FrameForNewSizePinnedToTopLeft (newFrameSize);
 
227
                        if (animating) {
 
228
                                NSAnimation animation = new NSViewAnimation (new [] {
 
229
                                        NSDictionary.FromObjectsAndKeys (
 
230
                                            new object[] { this, NSValue.FromRectangleF (Frame), NSValue.FromRectangleF (newFrame) },
 
231
                                                new object[] { NSViewAnimation.TargetKey, NSViewAnimation.StartFrameKey, NSViewAnimation.EndFrameKey }
 
232
                                        )
 
233
                                });
 
234
                                animation.AnimationBlockingMode = NSAnimationBlockingMode.Nonblocking;
 
235
                                animation.Duration = 0.25;
 
236
                                animation.StartAnimation ();
 
237
                        } else {
 
238
                                Superview.SetNeedsDisplayInRect (Frame);
 
239
                                Frame = newFrame;
 
240
                                NeedsDisplay = true;
 
241
                        }
 
242
                }
 
243
        }
 
244
}
 
245