5
// Luís Reis <luiscubal@gmail.com>
6
// Eric Maupin <ermau@xamarin.com>
8
// Copyright (c) 2012 Luís Reis
9
// Copyright (c) 2012 Xamarin, Inc.
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:
18
// The above copyright notice and this permission notice shall be included in
19
// all copies or substantial portions of the Software.
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
31
using System.Windows.Controls.Primitives;
32
using System.Windows.Markup;
33
using System.Windows.Media;
34
using SWC = System.Windows.Controls;
38
namespace Xwt.WPFBackend
40
public class ButtonBackend : WidgetBackend, IButtonBackend
42
public ButtonBackend ()
43
: this (new WpfButton ())
47
protected ButtonBackend (ButtonBase impl)
50
throw new ArgumentNullException ("impl");
55
protected ButtonBase Button {
56
get { return (ButtonBase)Widget; }
59
protected new IButtonEventSink EventSink {
60
get { return (IButtonEventSink)base.EventSink; }
63
public void SetButtonStyle (ButtonStyle style) {
66
case ButtonStyle.Normal:
67
Button.ClearValue (SWC.Control.BackgroundProperty);
68
Button.ClearValue (SWC.Control.BorderThicknessProperty);
69
Button.ClearValue (SWC.Control.BorderBrushProperty);
71
case ButtonStyle.Flat:
72
Button.Background = Brushes.Transparent;
73
Button.BorderBrush = Brushes.Transparent;
75
case ButtonStyle.Borderless:
76
Button.ClearValue (SWC.Control.BackgroundProperty);
77
Button.BorderThickness = new Thickness (0);
78
Button.BorderBrush = Brushes.Transparent;
81
Button.InvalidateMeasure ();
84
public virtual void SetButtonType (ButtonType type) {
86
case ButtonType.Normal:
90
case ButtonType.DropDown:
91
Button.Style = (Style) ButtonResources ["NormalDropDown"];
95
Button.InvalidateMeasure ();
98
public void SetContent (string label, object imageBackend, ContentPosition position)
100
if (imageBackend == null)
101
Button.Content = label;
104
SWC.DockPanel grid = new SWC.DockPanel ();
106
var img = DataConverter.AsImageSource (imageBackend);
107
SWC.Image imageCtrl = new SWC.Image
114
SWC.DockPanel.SetDock (imageCtrl, DataConverter.ToWpfDock (position));
115
grid.Children.Add (imageCtrl);
117
if (!string.IsNullOrEmpty (label)) {
118
SWC.Label labelCtrl = new SWC.Label ();
119
labelCtrl.Content = label;
120
grid.Children.Add (labelCtrl);
122
Button.Content = grid;
124
Button.InvalidateMeasure ();
127
public override void EnableEvent (object eventId)
129
base.EnableEvent (eventId);
130
if (eventId is ButtonEvent)
132
switch ((ButtonEvent)eventId)
134
case ButtonEvent.Clicked: Button.Click += HandleWidgetClicked; break;
139
public override void DisableEvent (object eventId)
141
base.DisableEvent (eventId);
142
if (eventId is ButtonEvent)
144
switch ((ButtonEvent)eventId)
146
case ButtonEvent.Clicked: Button.Click -= HandleWidgetClicked; break;
151
void HandleWidgetClicked (object sender, EventArgs e)
153
Toolkit.Invoke (EventSink.OnClicked);
156
private static ResourceDictionary buttonsDictionary;
157
protected static ResourceDictionary ButtonResources
161
if (buttonsDictionary == null) {
162
Uri uri = new Uri ("pack://application:,,,/Xwt.WPF;component/XWT.WPFBackend/Buttons.xaml");
163
buttonsDictionary = (ResourceDictionary)XamlReader.Load (System.Windows.Application.GetResourceStream (uri).Stream);
166
return buttonsDictionary;
171
class WpfButton : SWC.Button, IWpfWidget
173
public WidgetBackend Backend { get; set; }
175
protected override System.Windows.Size MeasureOverride (System.Windows.Size constraint)
177
var s = base.MeasureOverride (constraint);
178
return Backend.MeasureOverride (constraint, s);
181
protected override System.Windows.Size ArrangeOverride (System.Windows.Size arrangeBounds)
183
return base.ArrangeOverride (arrangeBounds);