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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt/Xwt/Placement.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
// Placement.cs
 
3
//
 
4
// Author:
 
5
//       Jérémie Laval <jeremie.laval@xamarin.com>
 
6
//
 
7
// Copyright (c) 2012 Xamarin, Inc.
 
8
using System;
 
9
using Xwt.Backends;
 
10
 
 
11
namespace Xwt
 
12
{
 
13
        public class Placement : Widget
 
14
        {
 
15
                Widget child;
 
16
                WidgetSpacing padding;
 
17
 
 
18
                protected new class WidgetBackendHost : Widget.WidgetBackendHost<Placement,IBoxBackend>
 
19
                {
 
20
                }
 
21
 
 
22
                protected override BackendHost CreateBackendHost ()
 
23
                {
 
24
                        return new WidgetBackendHost ();
 
25
                }
 
26
 
 
27
                IBoxBackend Backend {
 
28
                        get { return (IBoxBackend) BackendHost.Backend; }
 
29
                }
 
30
 
 
31
                public Placement ()
 
32
                {
 
33
                }
 
34
 
 
35
                public Widget Child {
 
36
                        get {
 
37
                                return child;
 
38
                        }
 
39
                        set {
 
40
                                if (child != null) {
 
41
                                        UnregisterChild (child);
 
42
                                        Backend.Remove ((IWidgetBackend)GetBackend (child));
 
43
                                }
 
44
                                child = value;
 
45
                                if (child != null) {
 
46
                                        RegisterChild (child);
 
47
                                        Backend.Add ((IWidgetBackend)GetBackend (child));
 
48
                                }
 
49
                                OnPreferredSizeChanged ();
 
50
                        }
 
51
                }
 
52
 
 
53
                public double XAlign {
 
54
                        get;
 
55
                        set;
 
56
                }
 
57
 
 
58
                public double YAlign {
 
59
                        get;
 
60
                        set;
 
61
                }
 
62
 
 
63
                public WidgetSpacing Padding {
 
64
                        get { return padding; }
 
65
                        set {
 
66
                                padding = value;
 
67
                                UpdatePadding ();
 
68
                        }
 
69
                }
 
70
 
 
71
                public double PaddingLeft {
 
72
                        get { return padding.Left; }
 
73
                        set {
 
74
                                padding.Left = value;
 
75
                                UpdatePadding (); 
 
76
                        }
 
77
                }
 
78
 
 
79
                public double PaddingRight {
 
80
                        get { return padding.Right; }
 
81
                        set {
 
82
                                padding.Right = value;
 
83
                                UpdatePadding (); 
 
84
                        }
 
85
                }
 
86
 
 
87
                public double PaddingTop {
 
88
                        get { return padding.Top; }
 
89
                        set {
 
90
                                padding.Top = value;
 
91
                                UpdatePadding (); 
 
92
                        }
 
93
                }
 
94
 
 
95
                public double PaddingBottom {
 
96
                        get { return padding.Bottom; }
 
97
                        set {
 
98
                                padding.Bottom = value;
 
99
                                UpdatePadding (); 
 
100
                        }
 
101
                }
 
102
 
 
103
                void UpdatePadding ()
 
104
                {
 
105
                        OnPreferredSizeChanged();
 
106
                }
 
107
 
 
108
                protected override void OnReallocate ()
 
109
                {
 
110
                        var size = Backend.Size;
 
111
                        double childWidth, childHeight;
 
112
 
 
113
                        if (child != null) {
 
114
                                if (child.Surface.SizeRequestMode == SizeRequestMode.HeightForWidth) {
 
115
                                        childWidth = child.Surface.GetPreferredWidth ().NaturalSize;
 
116
                                        if (childWidth > size.Width)
 
117
                                                childWidth = size.Width;
 
118
                                        childHeight = child.Surface.GetPreferredHeightForWidth (childWidth).NaturalSize;
 
119
                                        if (childHeight > size.Height)
 
120
                                                childHeight = size.Height;
 
121
                                }
 
122
                                else {
 
123
                                        childHeight = child.Surface.GetPreferredHeight ().NaturalSize;
 
124
                                        if (childHeight > size.Height)
 
125
                                                childHeight = size.Height;
 
126
                                        childWidth = child.Surface.GetPreferredWidthForHeight (childHeight).NaturalSize;
 
127
                                        if (childWidth > size.Width)
 
128
                                                childWidth = size.Width;
 
129
                                }
 
130
 
 
131
                                if (childWidth < 0)
 
132
                                        childWidth = 0;
 
133
                                if (childHeight < 0)
 
134
                                        childHeight = 0;
 
135
 
 
136
                                var x = XAlign * (size.Width - childWidth - Padding.HorizontalSpacing) + Padding.Left - Padding.Right;
 
137
                                var y = YAlign * (size.Height - childHeight - Padding.VerticalSpacing) + Padding.Top - Padding.Bottom;
 
138
 
 
139
                                Backend.SetAllocation (new[] { (IWidgetBackend)GetBackend (child) }, new[] { new Rectangle (x, y, childWidth, childHeight) });
 
140
 
 
141
                                if (!Application.EngineBackend.HandlesSizeNegotiation)
 
142
                                        child.Surface.Reallocate ();
 
143
                        }
 
144
                }
 
145
 
 
146
                protected override WidgetSize OnGetPreferredHeight ()
 
147
                {
 
148
                        WidgetSize s = new WidgetSize ();
 
149
                        if (child != null)
 
150
                                s += child.Surface.GetPreferredHeight ();
 
151
                        return s;
 
152
                }
 
153
 
 
154
                protected override WidgetSize OnGetPreferredWidth ()
 
155
                {
 
156
                        WidgetSize s = new WidgetSize ();
 
157
                        if (child != null)
 
158
                                s += child.Surface.GetPreferredWidth ();
 
159
                        return s;
 
160
                }
 
161
 
 
162
                protected override WidgetSize OnGetPreferredHeightForWidth (double width)
 
163
                {
 
164
                        WidgetSize s = new WidgetSize ();
 
165
                        if (child != null)
 
166
                                s += child.Surface.GetPreferredHeightForWidth (width);
 
167
                        return s;
 
168
                }
 
169
 
 
170
                protected override WidgetSize OnGetPreferredWidthForHeight (double height)
 
171
                {
 
172
                        WidgetSize s = new WidgetSize ();
 
173
                        if (child != null)
 
174
                                s += child.Surface.GetPreferredWidthForHeight (height);
 
175
                        return s;
 
176
                }
 
177
        }
 
178
}
 
179