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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.Gtk/Xwt.GtkBackend/BoxBackend.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
// BoxBackend.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
 
 
27
using System;
 
28
using System.Linq;
 
29
using Xwt.Backends;
 
30
using Xwt;
 
31
using System.Collections.Generic;
 
32
 
 
33
namespace Xwt.GtkBackend
 
34
{
 
35
        class BoxBackend: WidgetBackend, IBoxBackend
 
36
        {
 
37
                public BoxBackend ()
 
38
                {
 
39
                        Widget = new CustomContainer () { Backend = this };
 
40
                        Widget.Show ();
 
41
                }
 
42
                
 
43
                new CustomContainer Widget {
 
44
                        get { return (CustomContainer)base.Widget; }
 
45
                        set { base.Widget = value; }
 
46
                }
 
47
                
 
48
                public void Add (IWidgetBackend widget)
 
49
                {
 
50
                        WidgetBackend wb = (WidgetBackend) widget;
 
51
                        Widget.Add (wb.Frontend, GetWidget (widget));
 
52
                }
 
53
 
 
54
                public void Remove (IWidgetBackend widget)
 
55
                {
 
56
                        Widget.Remove (GetWidget (widget));
 
57
                }
 
58
                
 
59
                public void SetAllocation (IWidgetBackend[] widgets, Rectangle[] rects)
 
60
                {
 
61
                        bool changed = false;
 
62
                        for (int n=0; n<widgets.Length; n++) {
 
63
                                var w = GetWidget (widgets[n]);
 
64
                                if (Widget.SetAllocation (w, rects[n]))
 
65
                                        changed = true;
 
66
                        }
 
67
                        if (changed && !Widget.IsReallocating)
 
68
                                Widget.QueueResize ();
 
69
                }
 
70
        }
 
71
        
 
72
        class CustomContainer: Gtk.Container, IGtkContainer
 
73
        {
 
74
                public BoxBackend Backend;
 
75
                public bool IsReallocating;
 
76
                Dictionary<Gtk.Widget, WidgetData> children = new Dictionary<Gtk.Widget, WidgetData> ();
 
77
                
 
78
                struct WidgetData
 
79
                {
 
80
                        public Rectangle Rect;
 
81
                        public Widget Widget;
 
82
                }
 
83
                
 
84
                public CustomContainer ()
 
85
                {
 
86
                        GtkWorkarounds.FixContainerLeak (this);
 
87
                        WidgetFlags |= Gtk.WidgetFlags.NoWindow;
 
88
                }
 
89
                
 
90
                public void ReplaceChild (Gtk.Widget oldWidget, Gtk.Widget newWidget)
 
91
                {
 
92
                        WidgetData r = children [oldWidget];
 
93
                        Remove (oldWidget);
 
94
                        Add (newWidget);
 
95
                        children [newWidget] = r;
 
96
                }
 
97
                
 
98
                public bool SetAllocation (Gtk.Widget w, Rectangle rect)
 
99
                {
 
100
                        WidgetData r;
 
101
                        children.TryGetValue (w, out r);
 
102
                        if (r.Rect != rect) {
 
103
                                r.Rect = rect;
 
104
                                children [w] = r;
 
105
                                return true;
 
106
                        } else
 
107
                                return false;
 
108
                }
 
109
                
 
110
                public void Add (Widget w, Gtk.Widget gw)
 
111
                {
 
112
                        children.Add (gw, new WidgetData () { Widget = w, Rect = new Rectangle (0,0,0,0) });
 
113
                        Add (gw);
 
114
                }
 
115
                
 
116
                protected override void OnAdded (Gtk.Widget widget)
 
117
                {
 
118
                        widget.Parent = this;
 
119
                }
 
120
                
 
121
                protected override void OnRemoved (Gtk.Widget widget)
 
122
                {
 
123
                        children.Remove (widget);
 
124
                        widget.Unparent ();
 
125
                        QueueResize ();
 
126
                }
 
127
                
 
128
                protected override void OnUnrealized ()
 
129
                {
 
130
                        base.OnUnrealized ();
 
131
                }
 
132
                
 
133
                protected override void OnSizeAllocated (Gdk.Rectangle allocation)
 
134
                {
 
135
                        base.OnSizeAllocated (allocation);
 
136
                        if (Backend.IsPreallocating)
 
137
                                return;
 
138
                        try {
 
139
                                IsReallocating = true;
 
140
                                ((IWidgetSurface)Backend.Frontend).Reallocate ();
 
141
                        } catch {
 
142
                                IsReallocating = false;
 
143
                        }
 
144
                        foreach (var cr in children) {
 
145
                                var r = cr.Value.Rect;
 
146
                                cr.Key.SizeAllocate (new Gdk.Rectangle (allocation.X + (int)r.X, allocation.Y + (int)r.Y, (int)r.Width, (int)r.Height));
 
147
                        }
 
148
                }
 
149
                
 
150
                protected override void ForAll (bool includeInternals, Gtk.Callback callback)
 
151
                {
 
152
                        base.ForAll (includeInternals, callback);
 
153
                        foreach (var c in children.Keys.ToArray ())
 
154
                                callback (c);
 
155
                }
 
156
        }
 
157
}
 
158