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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.WPF/Xwt.WPFBackend/DialogBackend.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
// DialogBackend.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.Collections.ObjectModel;
 
29
using System.Windows;
 
30
using System.Windows.Controls;
 
31
using System.Windows.Controls.Primitives;
 
32
using System.Windows.Data;
 
33
using System.Windows.Input;
 
34
using Xwt.Backends;
 
35
using Xwt.Engine;
 
36
using SWC = System.Windows.Controls;
 
37
 
 
38
namespace Xwt.WPFBackend
 
39
{
 
40
        public class DialogBackend
 
41
                : WindowBackend, IDialogBackend
 
42
        {
 
43
                static DialogBackend()
 
44
                {
 
45
                        var panelFactory = new FrameworkElementFactory (typeof (StackPanel));
 
46
                        panelFactory.SetValue (StackPanel.OrientationProperty, SWC.Orientation.Horizontal);
 
47
 
 
48
                        PanelTemplate = new ItemsPanelTemplate (panelFactory);
 
49
 
 
50
                        ButtonStyle.Setters.Add (new Setter (FrameworkElement.MarginProperty, new Thickness (5)));
 
51
                        ButtonStyle.Setters.Add (new Setter (FrameworkElement.MinWidthProperty, 80d));
 
52
                }
 
53
 
 
54
                private DelegatedCommand cmd;
 
55
 
 
56
                public DialogBackend()
 
57
                {
 
58
                        cmd = new DelegatedCommand<DialogButton> (OnButtonClicked);
 
59
 
 
60
                        this.buttonContainer.ItemsPanel = PanelTemplate;
 
61
                        this.buttonContainer.ItemTemplateSelector =  new DialogButtonTemplateSelector (ButtonStyle, cmd);
 
62
                        this.buttonContainer.ItemsSource = this.buttons;
 
63
                        this.buttonContainer.HorizontalAlignment = HorizontalAlignment.Right;
 
64
 
 
65
                        this.rootPanel.RowDefinitions.Add (new RowDefinition { Height = new GridLength (0, GridUnitType.Auto) });
 
66
                        separator = new SWC.Separator ();
 
67
                        Grid.SetRow (separator, 2);
 
68
                        this.rootPanel.Children.Add (separator);
 
69
 
 
70
                        this.rootPanel.RowDefinitions.Add (new RowDefinition { Height = new GridLength (0, GridUnitType.Auto) });
 
71
                        Grid.SetRow (this.buttonContainer, 3);
 
72
                        this.rootPanel.Children.Add (this.buttonContainer);
 
73
                }
 
74
 
 
75
                public override void SetMinSize (Size s)
 
76
                {
 
77
                        // Take into account the size of the button bar and the separator
 
78
 
 
79
                        buttonContainer.InvalidateMeasure ();
 
80
                        buttonContainer.Measure (new System.Windows.Size (double.PositiveInfinity, double.PositiveInfinity));
 
81
                        separator.InvalidateMeasure ();
 
82
                        separator.Measure (new System.Windows.Size (double.PositiveInfinity, double.PositiveInfinity));
 
83
                        s.Height += buttonContainer.DesiredSize.Height + separator.DesiredSize.Height;
 
84
                        base.SetMinSize (s);
 
85
                }
 
86
 
 
87
                public void SetButtons (IEnumerable<DialogButton> newButtons)
 
88
                {
 
89
                        this.buttons.Clear();
 
90
                        foreach (var button in newButtons) {
 
91
                                this.buttons.Add (button);
 
92
                        }
 
93
                }
 
94
 
 
95
                public void UpdateButton (DialogButton updatedButton)
 
96
                {
 
97
                        for (int i = 0; i < this.buttons.Count; ++i) {
 
98
                                var button = this.buttons [i];
 
99
                                if (button == updatedButton) {
 
100
                                        this.buttons.RemoveAt (i);
 
101
                                        this.buttons.Insert (i, updatedButton);
 
102
                                        break;
 
103
                                }
 
104
                        }
 
105
                }
 
106
 
 
107
                public void RunLoop (IWindowFrameBackend parent)
 
108
                {
 
109
                        if (parent != null)
 
110
                                Window.Owner = ((WindowFrameBackend) parent).Window;
 
111
                        Window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
 
112
                        Window.ShowInTaskbar = false;
 
113
                        Window.ShowDialog ();
 
114
                }
 
115
 
 
116
                public void EndLoop ()
 
117
                {
 
118
                        Window.Close();
 
119
                }
 
120
 
 
121
                private readonly ItemsControl buttonContainer = new ItemsControl();
 
122
                private readonly ObservableCollection<DialogButton> buttons = new ObservableCollection<DialogButton> ();
 
123
                readonly SWC.Separator separator;
 
124
 
 
125
                protected IDialogEventSink DialogEventSink {
 
126
                        get { return (IDialogEventSink) EventSink; }
 
127
                }
 
128
 
 
129
                private void OnButtonClicked (DialogButton button)
 
130
                {
 
131
                        Toolkit.Invoke (() => DialogEventSink.OnDialogButtonClicked (button));
 
132
                }
 
133
 
 
134
                private static readonly ItemsPanelTemplate PanelTemplate;
 
135
                private static readonly Style ButtonStyle = new Style (typeof (SWC.Button));
 
136
 
 
137
                private class DialogButtonTemplateSelector
 
138
                        : DataTemplateSelector
 
139
                {
 
140
                        static void SetupButtonFactory (FrameworkElementFactory factory, Style style, ICommand command)
 
141
                        {
 
142
                                factory.SetBinding (UIElement.IsEnabledProperty, new Binding ("Sensitive"));
 
143
                                factory.SetBinding (UIElement.VisibilityProperty, new Binding ("Visible") { Converter = VisibilityConverter });
 
144
                                factory.SetValue (FrameworkElement.StyleProperty, style);
 
145
                                factory.SetValue (ButtonBase.CommandProperty, command);
 
146
                                factory.SetBinding (ButtonBase.CommandParameterProperty, new Binding ());
 
147
                        }
 
148
 
 
149
                        public DialogButtonTemplateSelector (Style style, ICommand command)
 
150
                        {
 
151
                                var buttonFactory = new FrameworkElementFactory (typeof (SWC.Button));
 
152
                                SetupButtonFactory (buttonFactory, style, command);
 
153
                                buttonFactory.SetBinding (ContentControl.ContentProperty, new Binding ("Label"));
 
154
 
 
155
                                this.normalTemplate = new DataTemplate { VisualTree = buttonFactory };
 
156
 
 
157
                                buttonFactory = new FrameworkElementFactory (typeof (SWC.Button));
 
158
                                SetupButtonFactory (buttonFactory, style, command);
 
159
 
 
160
                                var contentFactory = new FrameworkElementFactory (typeof (DockPanel));
 
161
 
 
162
                                var imageFactory = new FrameworkElementFactory (typeof (Image));
 
163
                                imageFactory.SetBinding (Image.SourceProperty, new Binding ("Image.NativeWidget"));
 
164
                                imageFactory.SetValue (DockPanel.DockProperty, Dock.Left);
 
165
 
 
166
                                contentFactory.AppendChild (imageFactory);
 
167
 
 
168
                                var textFactory = new FrameworkElementFactory (typeof (TextBlock));
 
169
                                textFactory.SetBinding (TextBlock.TextProperty, new Binding ("Label"));
 
170
                                textFactory.SetValue (DockPanel.DockProperty, Dock.Right);
 
171
 
 
172
                                contentFactory.AppendChild (textFactory);
 
173
 
 
174
                                buttonFactory.AppendChild (contentFactory);
 
175
 
 
176
                                this.imageTemplate = new DataTemplate { VisualTree = buttonFactory };
 
177
                        }
 
178
 
 
179
                        public override DataTemplate SelectTemplate (object item, DependencyObject container)
 
180
                        {
 
181
                                var button = item as DialogButton;
 
182
                                if (button == null)
 
183
                                        return base.SelectTemplate (item, container);
 
184
 
 
185
                                return (button.Image == null) ? this.normalTemplate : this.imageTemplate;
 
186
                        }
 
187
 
 
188
                        private static readonly BooleanToVisibilityConverter VisibilityConverter = new BooleanToVisibilityConverter ();
 
189
                        private readonly DataTemplate imageTemplate;
 
190
                        private readonly DataTemplate normalTemplate;
 
191
                }
 
192
        }
 
193
}
 
 
b'\\ No newline at end of file'