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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt/Xwt/Notebook.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
// Notebook.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 Xwt.Backends;
 
29
using System.Windows.Markup;
 
30
 
 
31
namespace Xwt
 
32
{
 
33
        public class Notebook: Widget
 
34
        {
 
35
                ChildrenCollection<NotebookTab> tabs;
 
36
                EventHandler currentTabChanged;
 
37
                
 
38
                protected new class WidgetBackendHost: Widget.WidgetBackendHost, INotebookEventSink, ICollectionEventSink<NotebookTab>, IContainerEventSink<NotebookTab>
 
39
                {
 
40
                        public void AddedItem (NotebookTab item, int index)
 
41
                        {
 
42
                                ((Notebook)Parent).OnAdd (item);
 
43
                        }
 
44
                        
 
45
                        public void RemovedItem (NotebookTab item, int index)
 
46
                        {
 
47
                                ((Notebook)Parent).OnRemove (item.Child);
 
48
                        }
 
49
                        
 
50
                        public void ChildChanged (NotebookTab child, string hint)
 
51
                        {
 
52
                                ((Notebook)Parent).Backend.UpdateLabel (child, hint);
 
53
                                ((Notebook)Parent).OnPreferredSizeChanged ();
 
54
                        }
 
55
                        
 
56
                        public void ChildReplaced (NotebookTab child, Widget oldWidget, Widget newWidget)
 
57
                        {
 
58
                                ((Notebook)Parent).OnReplaceChild (child, oldWidget, newWidget);
 
59
                        }
 
60
                        
 
61
                        public void OnCurrentTabChanged ()
 
62
                        {
 
63
                                ((Notebook)Parent).OnCurrentTabChanged (EventArgs.Empty);
 
64
                        }
 
65
                }
 
66
                
 
67
                static Notebook ()
 
68
                {
 
69
                        MapEvent (NotebookEvent.CurrentTabChanged, typeof(Notebook), "OnCurrentTabChanged");
 
70
                }
 
71
                
 
72
                public Notebook ()
 
73
                {
 
74
                        tabs = new ChildrenCollection <NotebookTab> ((WidgetBackendHost) BackendHost);
 
75
                }
 
76
                
 
77
                protected override BackendHost CreateBackendHost ()
 
78
                {
 
79
                        return new WidgetBackendHost ();
 
80
                }
 
81
                
 
82
                INotebookBackend Backend {
 
83
                        get { return (INotebookBackend) BackendHost.Backend; }
 
84
                }
 
85
                
 
86
                public void Add (Widget w, string label)
 
87
                {
 
88
                        NotebookTab t = new NotebookTab ((WidgetBackendHost)BackendHost, w);
 
89
                        t.Label = label;
 
90
                        tabs.Add (t);
 
91
                }
 
92
                
 
93
                void OnRemove (Widget child)
 
94
                {
 
95
                        UnregisterChild (child);
 
96
                        Backend.Remove ((IWidgetBackend)GetBackend (child));
 
97
                        OnPreferredSizeChanged ();
 
98
                }
 
99
                
 
100
                void OnAdd (NotebookTab tab)
 
101
                {
 
102
                        RegisterChild (tab.Child);
 
103
                        Backend.Add ((IWidgetBackend)GetBackend (tab.Child), tab);
 
104
                        OnPreferredSizeChanged ();
 
105
                }
 
106
                
 
107
                void OnReplaceChild (NotebookTab tab, Widget oldWidget, Widget newWidget)
 
108
                {
 
109
                        if (oldWidget != null)
 
110
                                OnRemove (oldWidget);
 
111
                        OnAdd (tab);
 
112
                }
 
113
                
 
114
                public ChildrenCollection<NotebookTab> Tabs {
 
115
                        get { return tabs; }
 
116
                }
 
117
                
 
118
                public NotebookTab CurrentTab {
 
119
                        get {
 
120
                                return tabs [Backend.CurrentTab];
 
121
                        }
 
122
                        set {
 
123
                                for (int n=0; n<tabs.Count; n++) {
 
124
                                        if (tabs[n] == value) {
 
125
                                                Backend.CurrentTab = n;
 
126
                                                return;
 
127
                                        }
 
128
                                }
 
129
                                CurrentTabIndex = -1;
 
130
                        }
 
131
                }
 
132
                
 
133
                public int CurrentTabIndex {
 
134
                        get { return Backend.CurrentTab; }
 
135
                        set { Backend.CurrentTab = value; }
 
136
                }
 
137
 
 
138
                public NotebookTabOrientation TabOrientation {
 
139
                        get { return Backend.TabOrientation; }
 
140
                        set { Backend.TabOrientation = value; }
 
141
                }
 
142
                
 
143
                protected virtual void OnCurrentTabChanged (EventArgs e)
 
144
                {
 
145
                        if (currentTabChanged != null)
 
146
                                currentTabChanged (this, e);
 
147
                }
 
148
                
 
149
                public event EventHandler CurrentTabChanged {
 
150
                        add {
 
151
                                BackendHost.OnBeforeEventAdd (NotebookEvent.CurrentTabChanged, currentTabChanged);
 
152
                                currentTabChanged += value;
 
153
                        }
 
154
                        remove {
 
155
                                currentTabChanged -= value;
 
156
                                BackendHost.OnAfterEventRemove (NotebookEvent.CurrentTabChanged, currentTabChanged);
 
157
                        }
 
158
                }
 
159
        }
 
160
        
 
161
        [ContentProperty("Child")]
 
162
        public class NotebookTab
 
163
        {
 
164
                IContainerEventSink<NotebookTab> parent;
 
165
                string label;
 
166
                Widget child;
 
167
                
 
168
                internal NotebookTab (IContainerEventSink<NotebookTab> parent, Widget child)
 
169
                {
 
170
                        this.child = child;
 
171
                        this.parent = parent;
 
172
                }
 
173
                
 
174
                public string Label {
 
175
                        get {
 
176
                                return label;
 
177
                        }
 
178
                        set {
 
179
                                label = value;
 
180
                                parent.ChildChanged (this, "Label");
 
181
                        }
 
182
                }
 
183
                
 
184
                public Widget Child {
 
185
                        get {
 
186
                                return child;
 
187
                        }
 
188
                        set {
 
189
                                var oldVal = child;
 
190
                                child = value;
 
191
                                parent.ChildReplaced (this, oldVal, value);
 
192
                        }
 
193
                }
 
194
        }
 
195
 
 
196
        public enum NotebookTabOrientation {
 
197
                Top,
 
198
                Left,
 
199
                Right,
 
200
                Bottom
 
201
        }
 
202
}
 
203