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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.WPF/Xwt.WPFBackend/PanedBackend.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
// PanedBackend.cs
 
3
//
 
4
// Author:
 
5
//       Eric Maupin <ermau@xamarin.com>
 
6
//       Lluis Sanchez <lluis@xamarin.com>
 
7
//
 
8
// Copyright (c) 2012 Xamarin, Inc.
 
9
//
 
10
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
11
// of this software and associated documentation files (the "Software"), to deal
 
12
// in the Software without restriction, including without limitation the rights
 
13
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
14
// copies of the Software, and to permit persons to whom the Software is
 
15
// furnished to do so, subject to the following conditions:
 
16
//
 
17
// The above copyright notice and this permission notice shall be included in
 
18
// all copies or substantial portions of the Software.
 
19
//
 
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
21
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
22
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
23
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
24
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
25
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
26
// THE SOFTWARE.
 
27
 
 
28
using System;
 
29
using System.Linq;
 
30
using System.Windows;
 
31
using System.Windows.Controls;
 
32
using System.Windows.Controls.Primitives;
 
33
using Xwt.Backends;
 
34
using Xwt.Engine;
 
35
using Orientation = Xwt.Backends.Orientation;
 
36
using SW = System.Windows;
 
37
using SWC = System.Windows.Controls;
 
38
using System.Collections.Generic;
 
39
 
 
40
namespace Xwt.WPFBackend
 
41
{
 
42
        public class PanedBackend
 
43
                : WidgetBackend, IPanedBackend
 
44
        {
 
45
                double position = -1;
 
46
                Orientation direction;
 
47
                PanelInfo panel1 = new PanelInfo ();
 
48
                PanelInfo panel2 = new PanelInfo ();
 
49
                GridSplitter splitter;
 
50
                private const int SplitterSize = 4;
 
51
                private bool reportPositionChanged;
 
52
                double lastSize;
 
53
 
 
54
                class PanelInfo
 
55
                {
 
56
                        public UIElement Widget;
 
57
                        public bool Resize;
 
58
                        public DefinitionBase Definition;
 
59
                        public int PanelIndex;
 
60
                        public bool Shrink;
 
61
                        public WidgetBackend Backend;
 
62
 
 
63
                        public bool HasWidget { get { return Widget != null; } }
 
64
 
 
65
                        public IWidgetSurface WidgetSurface {
 
66
                                get { return (IWidgetSurface)Backend.Frontend; }
 
67
                        }
 
68
 
 
69
                        public GridLength Size
 
70
                        {
 
71
                                get
 
72
                                {
 
73
                                        if (Definition is ColumnDefinition)
 
74
                                                return ((ColumnDefinition)Definition).Width;
 
75
                                        else
 
76
                                                return ((RowDefinition)Definition).Height;
 
77
                                }
 
78
                                set
 
79
                                {
 
80
                                        if (Definition is ColumnDefinition)
 
81
                                                ((ColumnDefinition)Definition).Width = value;
 
82
                                        else
 
83
                                                ((RowDefinition)Definition).Height = value;
 
84
                                }
 
85
                        }
 
86
                }
 
87
 
 
88
                public void Initialize (Orientation dir)
 
89
                {
 
90
                        this.direction = dir;
 
91
 
 
92
                        Grid = new PanedGrid () { Backend = this };
 
93
 
 
94
                        // Create all the row/column definitions and the splitter
 
95
 
 
96
                        if (direction == Orientation.Horizontal) {
 
97
                                ColumnDefinition definition = new ColumnDefinition ();
 
98
                                definition.Width = new GridLength (1, GridUnitType.Star);
 
99
                                Grid.ColumnDefinitions.Add (definition);
 
100
                                panel1.Definition = definition;
 
101
 
 
102
                                splitter = new GridSplitter {
 
103
                                        ResizeDirection = GridResizeDirection.Columns,
 
104
                                        VerticalAlignment = VerticalAlignment.Stretch,
 
105
                                        HorizontalAlignment = HorizontalAlignment.Center,
 
106
                                        Width = SplitterSize
 
107
                                };
 
108
                                Grid.ColumnDefinitions.Add (new ColumnDefinition { Width = GridLength.Auto });
 
109
                                Grid.SetColumn (splitter, 1);
 
110
                                Grid.Children.Add (splitter);
 
111
 
 
112
                                definition = new ColumnDefinition ();
 
113
                                definition.Width = new GridLength (1, GridUnitType.Star);
 
114
                                Grid.ColumnDefinitions.Add (definition);
 
115
                                panel2.Definition = definition;
 
116
                        }
 
117
                        else {
 
118
                                RowDefinition definition = new RowDefinition ();
 
119
                                definition.Height = new GridLength (1, GridUnitType.Star);
 
120
                                Grid.RowDefinitions.Add (definition);
 
121
                                panel1.Definition = definition;
 
122
 
 
123
                                splitter = new GridSplitter {
 
124
                                        ResizeDirection = GridResizeDirection.Rows,
 
125
                                        HorizontalAlignment = HorizontalAlignment.Stretch,
 
126
                                        VerticalAlignment = VerticalAlignment.Center,
 
127
                                        Height = SplitterSize
 
128
                                };
 
129
                                Grid.RowDefinitions.Add (new RowDefinition { Height = GridLength.Auto });
 
130
                                Grid.SetRow (splitter, 1);
 
131
                                Grid.Children.Add (splitter);
 
132
 
 
133
                                definition = new RowDefinition ();
 
134
                                definition.Height = new GridLength (1, GridUnitType.Star);
 
135
                                Grid.RowDefinitions.Add (definition);
 
136
                                panel2.Definition = definition;
 
137
                        }
 
138
                        panel1.PanelIndex = 0;
 
139
                        panel2.PanelIndex = 2;
 
140
                        splitter.Visibility = Visibility.Hidden;
 
141
 
 
142
                        splitter.DragDelta += delegate
 
143
                        {
 
144
                                if (position != panel1.Size.Value) {
 
145
                                        position = panel1.Size.Value;
 
146
                                        NotifyPositionChanged ();
 
147
                                }
 
148
                        };
 
149
                }
 
150
 
 
151
                public double Position
 
152
                {
 
153
                        get {
 
154
                                return position != -1 ? position * (direction == Orientation.Horizontal ? WidthPixelRatio : HeightPixelRatio) : 0;
 
155
                        }
 
156
                        set {
 
157
                                value /= direction == Orientation.Horizontal ? WidthPixelRatio : HeightPixelRatio;
 
158
                                if (position != value) {
 
159
                                        position = value;
 
160
                                        NotifyPositionChanged ();
 
161
                                }
 
162
                        }
 
163
                }
 
164
 
 
165
                internal void NotifyPositionChanged ()
 
166
                {
 
167
                        if (this.reportPositionChanged)
 
168
                                Toolkit.Invoke (((IPanedEventSink)EventSink).OnPositionChanged);
 
169
                }
 
170
 
 
171
                PanelInfo GetPanel (int panel)
 
172
                {
 
173
                        return panel == 1 ? panel1 : panel2;
 
174
                }
 
175
 
 
176
                bool SplitterVisible
 
177
                {
 
178
                        get { return panel1.HasWidget && panel2.HasWidget; }
 
179
                }
 
180
 
 
181
                public void SetPanel (int panel, IWidgetBackend widget, bool resize, bool shrink)
 
182
                {
 
183
                        var panelWidget = (UIElement)widget.NativeWidget;
 
184
 
 
185
                        var pi = GetPanel (panel);
 
186
                        pi.Widget = panelWidget;
 
187
                        pi.Backend = (WidgetBackend)widget;
 
188
                        pi.Resize = resize;
 
189
                        pi.Shrink = shrink;
 
190
 
 
191
                        if (direction == Orientation.Horizontal)
 
192
                                Grid.SetColumn (pi.Widget, pi.PanelIndex);
 
193
                        else
 
194
                                Grid.SetRow (pi.Widget, pi.PanelIndex);
 
195
 
 
196
                        Grid.Children.Add (pi.Widget);
 
197
 
 
198
                        UpdateSplitterVisibility ();
 
199
                }
 
200
 
 
201
                public void UpdatePanel (int panel, bool resize, bool shrink)
 
202
                {
 
203
                        var pi = GetPanel (panel);
 
204
                        pi.Resize = resize;
 
205
                        pi.Shrink = shrink;
 
206
                        Grid.InvalidateArrange ();
 
207
                }
 
208
 
 
209
                public void RemovePanel (int panel)
 
210
                {
 
211
                        var pi = GetPanel (panel);
 
212
                        Grid.Children.Remove (pi.Widget);
 
213
                        pi.Widget = null;
 
214
                        UpdateSplitterVisibility ();
 
215
                }
 
216
 
 
217
                public override WidgetSize GetPreferredWidth ()
 
218
                {
 
219
                        if (panel1.HasWidget && panel2.HasWidget) {
 
220
                                if (direction == Orientation.Horizontal) {
 
221
                                        var ws = panel1.WidgetSurface.GetPreferredWidth ();
 
222
                                        ws += panel2.WidgetSurface.GetPreferredWidth ();
 
223
                                        ws += SplitterSize;
 
224
                                        return ws;
 
225
                                }
 
226
                                else {
 
227
                                        var ws = panel1.WidgetSurface.GetPreferredWidth ();
 
228
                                        return ws.UnionWith (panel2.WidgetSurface.GetPreferredWidth ());
 
229
                                }
 
230
                        }
 
231
                        else if (panel1.HasWidget)
 
232
                                return panel1.WidgetSurface.GetPreferredWidth ();
 
233
                        else if (panel2.HasWidget)
 
234
                                return panel2.WidgetSurface.GetPreferredWidth ();
 
235
                        else
 
236
                                return new WidgetSize (0);
 
237
                }
 
238
 
 
239
                public override WidgetSize GetPreferredHeightForWidth (double width)
 
240
                {
 
241
                        if (panel1.HasWidget && panel2.HasWidget) {
 
242
                                var tempPos = position;
 
243
                                var availableWidth = width - SplitterSize;
 
244
                                if (direction == Orientation.Horizontal) {
 
245
                                        if (!panel1.Shrink) {
 
246
                                                var w1 = panel1.WidgetSurface.GetPreferredWidth ().MinSize;
 
247
                                                if (tempPos < w1)
 
248
                                                        tempPos = w1;
 
249
                                        }
 
250
                                        if (!panel2.Shrink) {
 
251
                                                var w2 = panel2.WidgetSurface.GetPreferredWidth ().MinSize;
 
252
                                                if (availableWidth - tempPos < w2)
 
253
                                                        tempPos = availableWidth - w2;
 
254
                                        }
 
255
                                        var ws = panel1.WidgetSurface.GetPreferredHeightForWidth (tempPos);
 
256
                                        ws = ws.UnionWith (panel2.WidgetSurface.GetPreferredHeightForWidth (availableWidth - tempPos));
 
257
                                        return ws;
 
258
                                }
 
259
                                else {
 
260
                                        var ws = panel1.WidgetSurface.GetPreferredHeightForWidth (width);
 
261
                                        ws = ws.UnionWith (panel2.WidgetSurface.GetPreferredHeightForWidth (width));
 
262
                                        ws += SplitterSize;
 
263
                                        return ws;
 
264
                                }
 
265
                        }
 
266
                        else if (panel1.HasWidget)
 
267
                                return panel1.WidgetSurface.GetPreferredHeightForWidth (width);
 
268
                        else if (panel2.HasWidget)
 
269
                                return panel2.WidgetSurface.GetPreferredHeightForWidth (width);
 
270
                        else
 
271
                                return new WidgetSize (0);
 
272
                }
 
273
 
 
274
                public override WidgetSize GetPreferredHeight ()
 
275
                {
 
276
                        if (panel1.HasWidget && panel2.HasWidget) {
 
277
                                if (direction == Orientation.Vertical) {
 
278
                                        var ws = panel1.WidgetSurface.GetPreferredHeight ();
 
279
                                        ws += panel2.WidgetSurface.GetPreferredHeight ();
 
280
                                        ws += SplitterSize;
 
281
                                        return ws;
 
282
                                }
 
283
                                else {
 
284
                                        var ws = panel1.WidgetSurface.GetPreferredHeight ();
 
285
                                        return ws.UnionWith (panel2.WidgetSurface.GetPreferredHeight ());
 
286
                                }
 
287
                        }
 
288
                        else if (panel1.HasWidget)
 
289
                                return panel1.WidgetSurface.GetPreferredHeight ();
 
290
                        else if (panel2.HasWidget)
 
291
                                return panel2.WidgetSurface.GetPreferredHeight ();
 
292
                        else
 
293
                                return new WidgetSize (0);
 
294
                }
 
295
 
 
296
                public override WidgetSize GetPreferredWidthForHeight (double width)
 
297
                {
 
298
                        if (panel1.HasWidget && panel2.HasWidget) {
 
299
                                var tempPos = position;
 
300
                                var availableWidth = width - SplitterSize;
 
301
                                if (direction == Orientation.Vertical) {
 
302
                                        if (!panel1.Shrink) {
 
303
                                                var w1 = panel1.WidgetSurface.GetPreferredHeight ().MinSize;
 
304
                                                if (tempPos < w1)
 
305
                                                        tempPos = w1;
 
306
                                        }
 
307
                                        if (!panel2.Shrink) {
 
308
                                                var w2 = panel2.WidgetSurface.GetPreferredHeight ().MinSize;
 
309
                                                if (availableWidth - tempPos < w2)
 
310
                                                        tempPos = availableWidth - w2;
 
311
                                        }
 
312
                                        var ws = panel1.WidgetSurface.GetPreferredWidthForHeight (tempPos);
 
313
                                        ws = ws.UnionWith (panel2.WidgetSurface.GetPreferredWidthForHeight (availableWidth - tempPos));
 
314
                                        return ws;
 
315
                                }
 
316
                                else {
 
317
                                        var ws = panel1.WidgetSurface.GetPreferredWidthForHeight (width);
 
318
                                        ws = ws.UnionWith (panel2.WidgetSurface.GetPreferredWidthForHeight (width));
 
319
                                        ws += SplitterSize;
 
320
                                        return ws;
 
321
                                }
 
322
                        }
 
323
                        else if (panel1.HasWidget)
 
324
                                return panel1.WidgetSurface.GetPreferredWidthForHeight (width);
 
325
                        else if (panel2.HasWidget)
 
326
                                return panel2.WidgetSurface.GetPreferredWidthForHeight (width);
 
327
                        else
 
328
                                return new WidgetSize (0);
 
329
                }
 
330
 
 
331
                internal void ArrangeChildren (SW.Size size)
 
332
                {
 
333
                        double newSize = direction == Orientation.Horizontal ? size.Width : size.Height;
 
334
                        double splitterDesiredSize = SplitterSize;
 
335
                        double availableSize;
 
336
 
 
337
                        availableSize = newSize - splitterDesiredSize;
 
338
                        if (availableSize <= 0)
 
339
                                return;
 
340
 
 
341
                        if (panel1.Widget != null && panel2.Widget != null) {
 
342
 
 
343
                                // If the bounds have changed, we have to calculate a new current position
 
344
                                if (lastSize != newSize || position == -1) {
 
345
                                        double oldAvailableSize = lastSize - SplitterSize;
 
346
                                        if (position == -1)
 
347
                                                position = availableSize / 2;
 
348
                                        else if (IsFixed (panel2)) {
 
349
                                                var oldPanel2Size = oldAvailableSize - position - SplitterSize;
 
350
                                                position = availableSize - oldPanel2Size - SplitterSize;
 
351
                                        }
 
352
                                        else if (!IsFixed (panel1))
 
353
                                                position = availableSize * (position / oldAvailableSize);
 
354
                                }
 
355
 
 
356
                                if (!panel1.Shrink) {
 
357
                                        var w = panel1.WidgetSurface;
 
358
                                        var min = direction == Orientation.Horizontal ? w.GetPreferredWidth ().MinSize: w.GetPreferredHeight ().MinSize;
 
359
                                        if (position < min)
 
360
                                                position = min;
 
361
                                }
 
362
                                if (!panel2.Shrink) {
 
363
                                        var w = panel2.WidgetSurface;
 
364
                                        var min = direction == Orientation.Horizontal ? w.GetPreferredWidth ().MinSize : w.GetPreferredHeight ().MinSize;
 
365
                                        if (availableSize - position < min) {
 
366
                                                position = availableSize - min;
 
367
                                        }
 
368
                                }
 
369
 
 
370
                                if (position < 0)
 
371
                                        position = 0;
 
372
                                if (position > availableSize)
 
373
                                        position = availableSize;
 
374
 
 
375
                                panel1.Size = new GridLength (position, GridUnitType.Star);
 
376
                                panel2.Size = new GridLength (availableSize - position, GridUnitType.Star);
 
377
                        }
 
378
                        else if (panel1.Widget != null)
 
379
                                panel1.Size = new GridLength (1, GridUnitType.Star);
 
380
                        else if (panel2 != null)
 
381
                                panel2.Size = new GridLength (1, GridUnitType.Star);
 
382
 
 
383
                        lastSize = newSize;
 
384
                }
 
385
 
 
386
                bool IsFixed (PanelInfo pi)
 
387
                {
 
388
                        return !pi.Resize && (pi == panel1 ? panel2 : panel1).Resize;
 
389
                }
 
390
 
 
391
                void UpdateSplitterVisibility ()
 
392
                {
 
393
                        if (panel1.Widget != null && panel2.Widget != null)
 
394
                                splitter.Visibility = Visibility.Visible;
 
395
                        else
 
396
                                splitter.Visibility = Visibility.Hidden;
 
397
                }
 
398
 
 
399
                public override void EnableEvent (object eventId)
 
400
                {
 
401
                        base.EnableEvent (eventId);
 
402
 
 
403
                        if (eventId is PanedEvent) {
 
404
                                switch ((PanedEvent)eventId) {
 
405
                                case PanedEvent.PositionChanged:
 
406
                                        this.reportPositionChanged = true;
 
407
                                        break;
 
408
                                }
 
409
                        }
 
410
                }
 
411
 
 
412
                public override void DisableEvent (object eventId)
 
413
                {
 
414
                        base.DisableEvent (eventId);
 
415
 
 
416
                        if (eventId is PanedEvent) {
 
417
                                switch ((PanedEvent)eventId) {
 
418
                                case PanedEvent.PositionChanged:
 
419
                                        this.reportPositionChanged = false;
 
420
                                        break;
 
421
                                }
 
422
                        }
 
423
                }
 
424
 
 
425
                private Grid Grid
 
426
                {
 
427
                        get { return (Grid) Widget; }
 
428
                        set { Widget = value; }
 
429
                }
 
430
        }
 
431
 
 
432
        class PanedGrid: Grid, IWpfWidget
 
433
        {
 
434
                public WidgetBackend Backend { get; set; }
 
435
 
 
436
                protected override SW.Size MeasureOverride (SW.Size constraint)
 
437
                {
 
438
                        // HACK: Fixes invalid size measure
 
439
                        // This line is hack to fix a measuring issue with Grid. For some reason, the grid 'remembers' the constraint
 
440
                        // parameter, so if MeasureOverride is called with a constraining size, but ArrangeOverride is later called
 
441
                        // with a bigger size, the Grid still uses the constrained size when determining the size of the children
 
442
                        constraint = new SW.Size (double.PositiveInfinity, double.PositiveInfinity);
 
443
 
 
444
                        var s = base.MeasureOverride (constraint);
 
445
                        return Backend.MeasureOverride (constraint, s);
 
446
                }
 
447
 
 
448
                protected override System.Windows.Size ArrangeOverride (System.Windows.Size arrangeSize)
 
449
                {
 
450
                        PanedBackend b = ((PanedBackend)Backend);
 
451
                        var oldPos = b.Position;
 
452
                        b.ArrangeChildren (arrangeSize);
 
453
                        var s = base.ArrangeOverride (arrangeSize);
 
454
                        if (oldPos != b.Position)
 
455
                                b.NotifyPositionChanged ();
 
456
                        return s;
 
457
                }
 
458
        }
 
459
}