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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.WPF/Xwt.WPFBackend/NotebookBackend.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
// NotebookBackend.cs
 
3
//  
 
4
// Author:
 
5
//       Thomas Ziegler <ziegler.thomas@web.de>
 
6
//       Eric Maupin <ermau@xamarin.com>
 
7
// 
 
8
// Copyright (c) 2012 Thomas Ziegler
 
9
// Copyright (c) 2012 Xamarin, Inc.
 
10
// 
 
11
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
12
// of this software and associated documentation files (the "Software"), to deal
 
13
// in the Software without restriction, including without limitation the rights
 
14
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
15
// copies of the Software, and to permit persons to whom the Software is
 
16
// furnished to do so, subject to the following conditions:
 
17
// 
 
18
// The above copyright notice and this permission notice shall be included in
 
19
// all copies or substantial portions of the Software.
 
20
// 
 
21
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
22
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
23
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
24
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
25
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
26
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
27
// THE SOFTWARE.
 
28
using System;
 
29
using System.Collections.Generic;
 
30
using System.Linq;
 
31
using System.Windows;
 
32
using System.Windows.Controls;
 
33
using Xwt.Engine;
 
34
using SWC = System.Windows.Controls;
 
35
using Xwt.Backends;
 
36
 
 
37
namespace Xwt.WPFBackend
 
38
{
 
39
        public class NotebookBackend : WidgetBackend, INotebookBackend
 
40
        {
 
41
                public NotebookBackend ()
 
42
                {
 
43
                        TabControl = new WpfNotebook ();
 
44
                }
 
45
 
 
46
                public int CurrentTab {
 
47
                        get { return TabControl.SelectedIndex; }
 
48
                        set { TabControl.SelectedIndex = value; }
 
49
                }
 
50
 
 
51
                public Xwt.NotebookTabOrientation TabOrientation {
 
52
                        get {
 
53
                                Xwt.NotebookTabOrientation tabPos = Xwt.NotebookTabOrientation.Top;
 
54
                                Enum.TryParse (TabControl.TabStripPlacement.ToString (), out tabPos);
 
55
                                return tabPos;
 
56
                        }
 
57
                        set {
 
58
                                SWC.Dock tabPos = SWC.Dock.Top;
 
59
                                Enum.TryParse (value.ToString (), out tabPos);
 
60
                                TabControl.TabStripPlacement = tabPos;
 
61
                        }
 
62
                }
 
63
 
 
64
                public void Add (IWidgetBackend widget, NotebookTab tab)
 
65
                {
 
66
                        UIElement element = (UIElement)widget.NativeWidget;
 
67
                        TabItem ti = new TabItem {
 
68
                                Content = element,
 
69
                                Header = tab.Label,
 
70
                                Tag = tab
 
71
                        };
 
72
 
 
73
                        FrameworkElement felement = element as FrameworkElement;
 
74
                        if (felement != null) {
 
75
                                felement.Tag = widget;
 
76
                                felement.Loaded += OnContentLoaded;
 
77
                        }
 
78
 
 
79
                        TabControl.Items.Add (ti);
 
80
                        
 
81
                        if (TabControl.SelectedIndex == -1)
 
82
                                TabControl.SelectedIndex = 0;
 
83
                }
 
84
 
 
85
                public void Remove (IWidgetBackend widget)
 
86
                {
 
87
                        UIElement element = widget.NativeWidget as UIElement;
 
88
                        if (element == null)
 
89
                                throw new ArgumentException ();
 
90
 
 
91
                        FrameworkElement felement = element as FrameworkElement;
 
92
                        if (felement != null)
 
93
                                felement.Loaded -= OnContentLoaded;
 
94
                        
 
95
                        for (int i = 0; i < TabControl.Items.Count; ++i) {
 
96
                                TabItem tab = (TabItem)TabControl.Items [i];
 
97
                                if (tab.Content == widget.NativeWidget) {
 
98
                                        TabControl.Items.RemoveAt (i);
 
99
                                        break;
 
100
                                }
 
101
                        }
 
102
                }
 
103
 
 
104
                public void UpdateLabel (NotebookTab tab, string hint)
 
105
                {
 
106
                        TabItem item = TabControl.Items.Cast<TabItem> ().FirstOrDefault (t => t.Tag == tab);
 
107
                        if (item != null)
 
108
                                item.Header = tab.Label;
 
109
                }
 
110
 
 
111
                public override void EnableEvent (object eventId)
 
112
                {
 
113
                        base.EnableEvent (eventId);
 
114
                        if (eventId is NotebookEvent) {
 
115
                                switch ((NotebookEvent)eventId) {
 
116
                                case NotebookEvent.CurrentTabChanged:
 
117
                                        TabControl.SelectionChanged += OnCurrentTabChanged;
 
118
                                        break;
 
119
                                }
 
120
                        }
 
121
                }
 
122
 
 
123
                public override void DisableEvent (object eventId)
 
124
                {
 
125
                        base.DisableEvent (eventId);
 
126
                        if (eventId is NotebookEvent) {
 
127
                                switch ((NotebookEvent)eventId) {
 
128
                                case NotebookEvent.CurrentTabChanged:
 
129
                                        TabControl.SelectionChanged -= OnCurrentTabChanged;
 
130
                                        break;
 
131
                                }
 
132
                        }
 
133
                }
 
134
 
 
135
                private void OnCurrentTabChanged (object sender, SelectionChangedEventArgs e)
 
136
                {
 
137
                        Toolkit.Invoke (NotebookEventSink.OnCurrentTabChanged);
 
138
                }
 
139
 
 
140
                protected TabControl TabControl {
 
141
                        get { return (TabControl)Widget; }
 
142
                        set { Widget = value; }
 
143
                }
 
144
 
 
145
                protected INotebookEventSink NotebookEventSink {
 
146
                        get { return (INotebookEventSink) EventSink; }
 
147
                }
 
148
 
 
149
                private void OnContentLoaded (object sender, RoutedEventArgs routedEventArgs)
 
150
                {
 
151
                        WidgetBackend backend = (WidgetBackend)((FrameworkElement) sender).Tag;
 
152
 
 
153
                        var surface = backend.Frontend as IWidgetSurface;
 
154
                        if (surface == null)
 
155
                                return;
 
156
 
 
157
                        surface.Reallocate();
 
158
                }
 
159
        }
 
160
 
 
161
        class WpfNotebook : SWC.TabControl, IWpfWidget
 
162
        {
 
163
                public WidgetBackend Backend { get; set; }
 
164
 
 
165
                protected override System.Windows.Size MeasureOverride (System.Windows.Size constraint)
 
166
                {
 
167
                        var s = base.MeasureOverride (constraint);
 
168
                        return Backend.MeasureOverride (constraint, s);
 
169
                }
 
170
        }
 
171
}
 
 
b'\\ No newline at end of file'