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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.WPF/Xwt.WPFBackend/ButtonBackend.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
// ButtonBackend.cs
 
3
//  
 
4
// Author:
 
5
//       Luís Reis <luiscubal@gmail.com>
 
6
//       Eric Maupin <ermau@xamarin.com>
 
7
// 
 
8
// Copyright (c) 2012 Luís Reis
 
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
 
 
29
using System;
 
30
using System.Windows;
 
31
using System.Windows.Controls.Primitives;
 
32
using System.Windows.Markup;
 
33
using System.Windows.Media;
 
34
using SWC = System.Windows.Controls;
 
35
using Xwt.Backends;
 
36
using Xwt.Engine;
 
37
 
 
38
namespace Xwt.WPFBackend
 
39
{
 
40
        public class ButtonBackend : WidgetBackend, IButtonBackend
 
41
        {
 
42
                public ButtonBackend ()
 
43
                        : this (new WpfButton ())
 
44
                {
 
45
                }
 
46
 
 
47
                protected ButtonBackend (ButtonBase impl)
 
48
                {
 
49
                        if (impl == null)
 
50
                                throw new ArgumentNullException ("impl");
 
51
 
 
52
                        Widget = impl;
 
53
                }
 
54
 
 
55
                protected ButtonBase Button {
 
56
                        get { return (ButtonBase)Widget; }
 
57
                }
 
58
 
 
59
                protected new IButtonEventSink EventSink {
 
60
                        get { return (IButtonEventSink)base.EventSink; }
 
61
                }
 
62
 
 
63
                public void SetButtonStyle (ButtonStyle style) {
 
64
                        switch (style)
 
65
                        {
 
66
                                case ButtonStyle.Normal:
 
67
                                        Button.ClearValue (SWC.Control.BackgroundProperty);
 
68
                                        Button.ClearValue (SWC.Control.BorderThicknessProperty);
 
69
                                        Button.ClearValue (SWC.Control.BorderBrushProperty);
 
70
                                        break;
 
71
                                case ButtonStyle.Flat:
 
72
                                        Button.Background = Brushes.Transparent;
 
73
                                        Button.BorderBrush = Brushes.Transparent;
 
74
                                        break;
 
75
                                case ButtonStyle.Borderless:
 
76
                                        Button.ClearValue (SWC.Control.BackgroundProperty);
 
77
                                        Button.BorderThickness = new Thickness (0);
 
78
                                        Button.BorderBrush = Brushes.Transparent;
 
79
                                        break;
 
80
                        }
 
81
                        Button.InvalidateMeasure ();
 
82
                }
 
83
 
 
84
                public virtual void SetButtonType (ButtonType type) {
 
85
                        switch (type) {
 
86
                        case ButtonType.Normal:
 
87
                                Button.Style = null;
 
88
                                break;
 
89
 
 
90
                        case ButtonType.DropDown:
 
91
                                Button.Style = (Style) ButtonResources ["NormalDropDown"];
 
92
                                break;
 
93
                        }
 
94
 
 
95
                        Button.InvalidateMeasure ();
 
96
                }
 
97
 
 
98
                public void SetContent (string label, object imageBackend, ContentPosition position)
 
99
                {
 
100
                        if (imageBackend == null)
 
101
                                Button.Content = label;
 
102
                        else
 
103
                        {
 
104
                                SWC.DockPanel grid = new SWC.DockPanel ();
 
105
 
 
106
                                var img = DataConverter.AsImageSource (imageBackend);
 
107
                                SWC.Image imageCtrl = new SWC.Image
 
108
                                {
 
109
                                        Source = img,
 
110
                                        Width = img.Width,
 
111
                                        Height = img.Height
 
112
                                };
 
113
 
 
114
                                SWC.DockPanel.SetDock (imageCtrl, DataConverter.ToWpfDock (position));
 
115
                                grid.Children.Add (imageCtrl);
 
116
 
 
117
                                if (!string.IsNullOrEmpty (label)) {
 
118
                                        SWC.Label labelCtrl = new SWC.Label ();
 
119
                                        labelCtrl.Content = label;
 
120
                                        grid.Children.Add (labelCtrl);
 
121
                                }
 
122
                                Button.Content = grid;
 
123
                        }
 
124
                        Button.InvalidateMeasure ();
 
125
                }
 
126
 
 
127
                public override void EnableEvent (object eventId)
 
128
                {
 
129
                        base.EnableEvent (eventId);
 
130
                        if (eventId is ButtonEvent)
 
131
                        {
 
132
                                switch ((ButtonEvent)eventId)
 
133
                                {
 
134
                                        case ButtonEvent.Clicked: Button.Click += HandleWidgetClicked; break;
 
135
                                }
 
136
                        }
 
137
                }
 
138
 
 
139
                public override void DisableEvent (object eventId)
 
140
                {
 
141
                        base.DisableEvent (eventId);
 
142
                        if (eventId is ButtonEvent)
 
143
                        {
 
144
                                switch ((ButtonEvent)eventId)
 
145
                                {
 
146
                                        case ButtonEvent.Clicked: Button.Click -= HandleWidgetClicked; break;
 
147
                                }
 
148
                        }
 
149
                }
 
150
 
 
151
                void HandleWidgetClicked (object sender, EventArgs e)
 
152
                {
 
153
                        Toolkit.Invoke (EventSink.OnClicked);
 
154
                }
 
155
 
 
156
                private static ResourceDictionary buttonsDictionary;
 
157
                protected static ResourceDictionary ButtonResources
 
158
                {
 
159
                        get
 
160
                        {
 
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);
 
164
                                }
 
165
 
 
166
                                return buttonsDictionary;
 
167
                        }
 
168
                }
 
169
        }
 
170
 
 
171
        class WpfButton : SWC.Button, IWpfWidget
 
172
        {
 
173
                public WidgetBackend Backend { get; set; }
 
174
 
 
175
                protected override System.Windows.Size MeasureOverride (System.Windows.Size constraint)
 
176
                {
 
177
                        var s = base.MeasureOverride (constraint);
 
178
                        return Backend.MeasureOverride (constraint, s);
 
179
                }
 
180
 
 
181
                protected override System.Windows.Size ArrangeOverride (System.Windows.Size arrangeBounds)
 
182
                {
 
183
                        return base.ArrangeOverride (arrangeBounds);
 
184
                }
 
185
        }
 
186
}