5
// Eric Maupin <ermau@xamarin.com>
7
// Copyright (c) 2012 Xamarin, Inc.
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:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
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
27
using System.Collections.Generic;
28
using System.Collections.ObjectModel;
30
using System.Windows.Controls;
31
using System.Windows.Controls.Primitives;
32
using System.Windows.Data;
33
using System.Windows.Input;
36
using SWC = System.Windows.Controls;
38
namespace Xwt.WPFBackend
40
public class DialogBackend
41
: WindowBackend, IDialogBackend
43
static DialogBackend()
45
var panelFactory = new FrameworkElementFactory (typeof (StackPanel));
46
panelFactory.SetValue (StackPanel.OrientationProperty, SWC.Orientation.Horizontal);
48
PanelTemplate = new ItemsPanelTemplate (panelFactory);
50
ButtonStyle.Setters.Add (new Setter (FrameworkElement.MarginProperty, new Thickness (5)));
51
ButtonStyle.Setters.Add (new Setter (FrameworkElement.MinWidthProperty, 80d));
54
private DelegatedCommand cmd;
56
public DialogBackend()
58
cmd = new DelegatedCommand<DialogButton> (OnButtonClicked);
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;
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);
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);
75
public override void SetMinSize (Size s)
77
// Take into account the size of the button bar and the separator
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;
87
public void SetButtons (IEnumerable<DialogButton> newButtons)
90
foreach (var button in newButtons) {
91
this.buttons.Add (button);
95
public void UpdateButton (DialogButton updatedButton)
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);
107
public void RunLoop (IWindowFrameBackend parent)
110
Window.Owner = ((WindowFrameBackend) parent).Window;
111
Window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
112
Window.ShowInTaskbar = false;
113
Window.ShowDialog ();
116
public void EndLoop ()
121
private readonly ItemsControl buttonContainer = new ItemsControl();
122
private readonly ObservableCollection<DialogButton> buttons = new ObservableCollection<DialogButton> ();
123
readonly SWC.Separator separator;
125
protected IDialogEventSink DialogEventSink {
126
get { return (IDialogEventSink) EventSink; }
129
private void OnButtonClicked (DialogButton button)
131
Toolkit.Invoke (() => DialogEventSink.OnDialogButtonClicked (button));
134
private static readonly ItemsPanelTemplate PanelTemplate;
135
private static readonly Style ButtonStyle = new Style (typeof (SWC.Button));
137
private class DialogButtonTemplateSelector
138
: DataTemplateSelector
140
static void SetupButtonFactory (FrameworkElementFactory factory, Style style, ICommand command)
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 ());
149
public DialogButtonTemplateSelector (Style style, ICommand command)
151
var buttonFactory = new FrameworkElementFactory (typeof (SWC.Button));
152
SetupButtonFactory (buttonFactory, style, command);
153
buttonFactory.SetBinding (ContentControl.ContentProperty, new Binding ("Label"));
155
this.normalTemplate = new DataTemplate { VisualTree = buttonFactory };
157
buttonFactory = new FrameworkElementFactory (typeof (SWC.Button));
158
SetupButtonFactory (buttonFactory, style, command);
160
var contentFactory = new FrameworkElementFactory (typeof (DockPanel));
162
var imageFactory = new FrameworkElementFactory (typeof (Image));
163
imageFactory.SetBinding (Image.SourceProperty, new Binding ("Image.NativeWidget"));
164
imageFactory.SetValue (DockPanel.DockProperty, Dock.Left);
166
contentFactory.AppendChild (imageFactory);
168
var textFactory = new FrameworkElementFactory (typeof (TextBlock));
169
textFactory.SetBinding (TextBlock.TextProperty, new Binding ("Label"));
170
textFactory.SetValue (DockPanel.DockProperty, Dock.Right);
172
contentFactory.AppendChild (textFactory);
174
buttonFactory.AppendChild (contentFactory);
176
this.imageTemplate = new DataTemplate { VisualTree = buttonFactory };
179
public override DataTemplate SelectTemplate (object item, DependencyObject container)
181
var button = item as DialogButton;
183
return base.SelectTemplate (item, container);
185
return (button.Image == null) ? this.normalTemplate : this.imageTemplate;
188
private static readonly BooleanToVisibilityConverter VisibilityConverter = new BooleanToVisibilityConverter ();
189
private readonly DataTemplate imageTemplate;
190
private readonly DataTemplate normalTemplate;
b'\\ No newline at end of file'