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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.Gtk/Xwt.GtkBackend/FrameBackend.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
// FrameBackend.cs
 
3
//  
 
4
// Author:
 
5
//       Lluis Sanchez <lluis@xamarin.com>
 
6
// 
 
7
// Copyright (c) 2011 Xamarin Inc
 
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 Xwt.Backends;
 
28
using Xwt.Drawing;
 
29
 
 
30
namespace Xwt.GtkBackend
 
31
{
 
32
        public class FrameBackend:  WidgetBackend, IFrameBackend
 
33
        {
 
34
                Gtk.Alignment paddingAlign;
 
35
                string label;
 
36
                Color? borderColor;
 
37
                
 
38
                public FrameBackend ()
 
39
                {
 
40
                        Widget = new Gtk.Frame ();
 
41
                        Widget.Show ();
 
42
                }
 
43
                
 
44
                protected new Gtk.Bin Widget {
 
45
                        get { return (Gtk.Bin)base.Widget; }
 
46
                        set { base.Widget = value; }
 
47
                }
 
48
                
 
49
                protected new IFrameEventSink EventSink {
 
50
                        get { return (IFrameEventSink)base.EventSink; }
 
51
                }
 
52
 
 
53
                #region IFrameBackend implementation
 
54
 
 
55
                public void SetContent (IWidgetBackend child)
 
56
                {
 
57
                        Gtk.Bin parent = paddingAlign != null ? paddingAlign : Widget;
 
58
                        if (child != null) {
 
59
                                var w = GetWidget (child);
 
60
                                if (parent.Child != null)
 
61
                                        parent.Remove (parent.Child);
 
62
                                parent.Child = w;
 
63
                        } else {
 
64
                                parent.Child = null;
 
65
                        }
 
66
                }
 
67
                public void SetFrameType (FrameType type)
 
68
                {
 
69
                        Frame f = (Frame) Frontend;
 
70
                        
 
71
                        switch (type) {
 
72
                        case FrameType.Custom:
 
73
                                if (!(Widget is HeaderBox)) {
 
74
                                        HeaderBox box = new HeaderBox ();
 
75
                                        box.Show ();
 
76
                                        box.DrawBackground = UsingCustomBackgroundColor;
 
77
                                        box.SetMargins ((int)f.Margin.Top, (int)f.Margin.Bottom, (int)f.Margin.Left, (int)f.Margin.Right);
 
78
                                        box.SetPadding ((int)f.Padding.Top, (int)f.Padding.Bottom, (int)f.Padding.Left, (int)f.Padding.Right);
 
79
                                        if (borderColor != null)
 
80
                                                box.SetBorderColor (borderColor.Value);
 
81
                                        var c = paddingAlign != null ? paddingAlign.Child : Widget.Child;
 
82
                                        if (c != null) {
 
83
                                                ((Gtk.Container)c.Parent).Remove (c);
 
84
                                                box.Add (c);
 
85
                                        }
 
86
                                        Widget = box;
 
87
                                        if (paddingAlign != null) {
 
88
                                                paddingAlign.Destroy ();
 
89
                                                paddingAlign = null;
 
90
                                        }
 
91
                                }
 
92
                                break;
 
93
                        case FrameType.WidgetBox:
 
94
                                if (!(Widget is Gtk.Frame)) {
 
95
                                        var c = Widget.Child;
 
96
                                        if (c != null)
 
97
                                                Widget.Remove (c);
 
98
                                        Gtk.Frame gf = new Gtk.Frame ();
 
99
                                        if (!string.IsNullOrEmpty (label))
 
100
                                                gf.Label = label;
 
101
                                        if (f.Padding.HorizontalSpacing != 0 || f.Padding.VerticalSpacing != 0) {
 
102
                                                paddingAlign = new Gtk.Alignment (0, 0, 1, 1);
 
103
                                                paddingAlign.Show ();
 
104
                                                UreatePaddingAlign (f.Padding.Top, f.Padding.Bottom, f.Padding.Left, f.Padding.Right);
 
105
                                                if (c != null)
 
106
                                                        paddingAlign.Add (c);
 
107
                                                gf.Add (paddingAlign);
 
108
                                        } else {
 
109
                                                if (c != null)
 
110
                                                        gf.Add (c);
 
111
                                        }
 
112
                                        gf.Show ();
 
113
                                        Widget = gf;
 
114
                                }
 
115
                                break;
 
116
                        }
 
117
                }
 
118
                
 
119
                void UreatePaddingAlign (double top, double bottom, double left, double right)
 
120
                {
 
121
                        paddingAlign.TopPadding = (uint) top;
 
122
                        paddingAlign.BottomPadding = (uint) bottom;
 
123
                        paddingAlign.LeftPadding = (uint) left;
 
124
                        paddingAlign.RightPadding = (uint) right;
 
125
                }
 
126
 
 
127
                public void SetBorderSize (double left, double right, double top, double bottom)
 
128
                {
 
129
                        HeaderBox hb = Widget as HeaderBox;
 
130
                        if (hb != null) {
 
131
                                hb.SetMargins ((int)top, (int)bottom, (int)left, (int)right);
 
132
                        }
 
133
                }
 
134
 
 
135
                public void SetPadding (double left, double right, double top, double bottom)
 
136
                {
 
137
                        if (Widget is HeaderBox) {
 
138
                                HeaderBox hb = (HeaderBox) Widget;
 
139
                                hb.SetPadding ((int)top, (int)bottom, (int)left, (int)right);
 
140
                                return;
 
141
                        }
 
142
                        
 
143
                        if (left == 0 && right == 0 && top == 0 && bottom == 0 && paddingAlign == null)
 
144
                                return;
 
145
 
 
146
                        if (paddingAlign == null) {
 
147
                                paddingAlign = new Gtk.Alignment (0, 0, 1, 1);
 
148
                                paddingAlign.Show ();
 
149
                                var c = Widget.Child;
 
150
                                if (c != null) {
 
151
                                        Widget.Remove (c);
 
152
                                        paddingAlign.Add (c);
 
153
                                }
 
154
                                Widget.Add (paddingAlign);
 
155
                        }
 
156
                        UreatePaddingAlign (top, bottom, left, right);
 
157
                }
 
158
 
 
159
                public Color BorderColor {
 
160
                        get {
 
161
                                if (borderColor == null)
 
162
                                        return Util.ToXwtColor (Widget.Style.Dark (Gtk.StateType.Normal));
 
163
                                else
 
164
                                        return borderColor.Value;
 
165
                        }
 
166
                        set {
 
167
                                borderColor = value;
 
168
                                HeaderBox hb = Widget as HeaderBox;
 
169
                                if (hb != null)
 
170
                                        hb.SetBorderColor (value);
 
171
                        }
 
172
                }
 
173
                
 
174
                public override Color BackgroundColor {
 
175
                        get {
 
176
                                return base.BackgroundColor;
 
177
                        }
 
178
                        set {
 
179
                                base.BackgroundColor = value;
 
180
                                if (Widget is HeaderBox)
 
181
                                        ((HeaderBox)Widget).DrawBackground = true;
 
182
                        }
 
183
                }
 
184
 
 
185
                public string Label {
 
186
                        get {
 
187
                                return label;
 
188
                        }
 
189
                        set {
 
190
                                label = value;
 
191
                                if (Widget is Gtk.Frame)
 
192
                                        ((Gtk.Frame)Widget).Label = value;
 
193
                        }
 
194
                }
 
195
                #endregion
 
196
        }
 
197
}
 
198