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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.WPF/Xwt.WPFBackend/ComboBoxBackend.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
// ComboBoxBackend.cs
 
3
//  
 
4
// Author:
 
5
//       Eric Maupin <ermau@xamarin.com>
 
6
// 
 
7
// Copyright (c) 2012 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.Collections.Generic;
 
28
using System.Windows;
 
29
using System.Windows.Controls;
 
30
using System.Windows.Data;
 
31
using Xwt.Backends;
 
32
using Xwt.WPFBackend.Utilities;
 
33
using WindowsSeparator = System.Windows.Controls.Separator;
 
34
using WindowsComboBox = System.Windows.Controls.ComboBox;
 
35
using WindowsOrientation = System.Windows.Controls.Orientation;
 
36
using WindowsComboBoxItem = System.Windows.Controls.ComboBoxItem;
 
37
 
 
38
namespace Xwt.WPFBackend
 
39
{
 
40
        public class ComboBoxBackend
 
41
                : WidgetBackend, IComboBoxBackend
 
42
        {
 
43
                private static readonly Style ContainerStyle;
 
44
                //private static readonly DataTemplate DefaultTemplate;
 
45
 
 
46
                static ComboBoxBackend()
 
47
                {
 
48
                        var factory = new FrameworkElementFactory (typeof (WindowsSeparator));
 
49
                        factory.SetValue (FrameworkElement.HorizontalAlignmentProperty, HorizontalAlignment.Stretch);
 
50
                        
 
51
                        var sepTemplate = new ControlTemplate (typeof (ComboBoxItem));
 
52
                        sepTemplate.VisualTree = factory;
 
53
 
 
54
                        DataTrigger trigger = new DataTrigger();
 
55
                        trigger.Binding = new Binding (".[1]") { Converter = new TypeToStringConverter() };
 
56
                        trigger.Value = typeof(ItemSeparator).Name;
 
57
                        trigger.Setters.Add (new Setter (Control.TemplateProperty, sepTemplate));
 
58
                        trigger.Setters.Add (new Setter (UIElement.IsEnabledProperty, false));
 
59
 
 
60
                        ContainerStyle = new Style (typeof (ComboBoxItem));
 
61
                        ContainerStyle.Triggers.Add (trigger);
 
62
                }
 
63
 
 
64
                public ComboBoxBackend()
 
65
                {
 
66
                        ComboBox = new ExComboBox();
 
67
                        ComboBox.DisplayMemberPath = ".[0]";
 
68
                        //ComboBox.ItemTemplate = DefaultTemplate;
 
69
                        ComboBox.ItemContainerStyle = ContainerStyle;
 
70
                }
 
71
 
 
72
                public void SetViews (CellViewCollection views)
 
73
                {
 
74
                        ComboBox.DisplayMemberPath = null;
 
75
                        ComboBox.ItemTemplate = GetDataTemplate (views);
 
76
                }
 
77
 
 
78
                public void SetSource (IListDataSource source, IBackend sourceBackend)
 
79
                {
 
80
                        var dataSource = sourceBackend as ListDataSource;
 
81
                        if (dataSource != null)
 
82
                                ComboBox.ItemsSource = dataSource;
 
83
                        else
 
84
                                ComboBox.ItemsSource = new ListSourceNotifyWrapper (source);
 
85
                }
 
86
 
 
87
                public int SelectedRow
 
88
                {
 
89
                        get { return ComboBox.SelectedIndex; }
 
90
                        set { ComboBox.SelectedIndex = value; }
 
91
                }
 
92
 
 
93
                public override void EnableEvent (object eventId)
 
94
                {
 
95
                        base.EnableEvent (eventId);
 
96
                        
 
97
                        if (eventId is ComboBoxEvent) {
 
98
 
 
99
                                switch ((ComboBoxEvent)eventId) {
 
100
                                        case ComboBoxEvent.SelectionChanged:
 
101
                                                ComboBox.SelectionChanged += OnSelectionChanged;
 
102
                                        break;
 
103
                                }
 
104
                        }
 
105
                }
 
106
 
 
107
                public override void DisableEvent (object eventId)
 
108
                {
 
109
                        base.DisableEvent (eventId);
 
110
                        
 
111
                        if (eventId is ComboBoxEvent) {
 
112
 
 
113
                                switch ((ComboBoxEvent)eventId) {
 
114
                                        case ComboBoxEvent.SelectionChanged:
 
115
                                                ComboBox.SelectionChanged -= OnSelectionChanged;
 
116
                                        break;
 
117
                                }
 
118
                        }
 
119
                }
 
120
 
 
121
                protected ExComboBox ComboBox
 
122
                {
 
123
                        get { return (ExComboBox) Widget; }
 
124
                        set { Widget = value; }
 
125
                }
 
126
 
 
127
                protected IComboBoxEventSink ComboBoxEventSink
 
128
                {
 
129
                        get { return (IComboBoxEventSink) EventSink; }
 
130
                }
 
131
 
 
132
                private void OnSelectionChanged (object sender, SelectionChangedEventArgs e)
 
133
                {
 
134
                        Xwt.Engine.Toolkit.Invoke (ComboBoxEventSink.OnSelectionChanged);
 
135
                }
 
136
 
 
137
                private DataTemplate GetDataTemplate (IList<CellView> views)
 
138
                {
 
139
                        var template = new DataTemplate (typeof (object[]));
 
140
 
 
141
                        FrameworkElementFactory root;
 
142
                        if (views.Count > 1) {
 
143
                                FrameworkElementFactory spFactory = new FrameworkElementFactory (typeof (StackPanel));
 
144
                                spFactory.SetValue (StackPanel.OrientationProperty, WindowsOrientation.Horizontal);
 
145
 
 
146
                                foreach (var view in views) {
 
147
                                        spFactory.AppendChild (CellUtil.CreateBoundCellRenderer (view));
 
148
                                }
 
149
 
 
150
                                root = spFactory;
 
151
                        } else {
 
152
                                root = CellUtil.CreateBoundCellRenderer (views [0]);
 
153
                        }
 
154
 
 
155
                        template.VisualTree = root;
 
156
                        return template;
 
157
                }
 
158
        }
 
159
}