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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.Gtk/Xwt.GtkBackend/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
//       Lluis Sanchez <lluis@xamarin.com>
 
6
// 
 
7
// Copyright (c) 2011 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;
 
28
using Xwt.Backends;
 
29
using Xwt.Engine;
 
30
 
 
31
namespace Xwt.GtkBackend
 
32
{
 
33
        public class ButtonBackend: WidgetBackend, IButtonBackend
 
34
        {
 
35
                protected bool ignoreClickEvents;
 
36
                
 
37
                public ButtonBackend ()
 
38
                {
 
39
                }
 
40
 
 
41
                public override void Initialize ()
 
42
                {
 
43
                        Widget = new Gtk.Button ();
 
44
                        base.Widget.Show ();
 
45
                        
 
46
                }
 
47
                
 
48
                protected new Gtk.Button Widget {
 
49
                        get { return (Gtk.Button)base.Widget; }
 
50
                        set { base.Widget = value; }
 
51
                }
 
52
                
 
53
                protected new IButtonEventSink EventSink {
 
54
                        get { return (IButtonEventSink)base.EventSink; }
 
55
                }
 
56
                
 
57
                public void SetContent (string label, object imageBackend, ContentPosition position)
 
58
                {
 
59
                        if (label != null && label.Length == 0)
 
60
                                label = null;
 
61
                        
 
62
                        Button b = (Button) Frontend;
 
63
                        if (label != null && imageBackend == null && b.Type == ButtonType.Normal) {
 
64
                                Widget.Label = label;
 
65
                                return;
 
66
                        }
 
67
                        
 
68
                        if (b.Type == ButtonType.Disclosure) {
 
69
                                Widget.Label = null;
 
70
                                Widget.Image = new Gtk.Arrow (Gtk.ArrowType.Down, Gtk.ShadowType.Out);
 
71
                                Widget.Image.ShowAll ();
 
72
                                return;
 
73
                        }
 
74
                        
 
75
                        Gtk.Widget contentWidget = null;
 
76
                        
 
77
                        Gtk.Widget imageWidget = null;
 
78
                        if (imageBackend != null)
 
79
                                imageWidget = new Gtk.Image ((Gdk.Pixbuf)imageBackend);
 
80
                        
 
81
                        if (label != null && imageWidget == null) {
 
82
                                contentWidget = new Gtk.Label (label); 
 
83
                        }
 
84
                        else if (label == null && imageWidget != null) {
 
85
                                contentWidget = imageWidget;
 
86
                        }
 
87
                        else if (label != null && imageWidget != null) {
 
88
                                Gtk.Box box = position == ContentPosition.Left || position == ContentPosition.Right ? (Gtk.Box) new Gtk.HBox (false, 3) : (Gtk.Box) new Gtk.VBox (false, 3);
 
89
                                var lab = new Gtk.Label (label);
 
90
                                
 
91
                                if (position == ContentPosition.Left || position == ContentPosition.Top) {
 
92
                                        box.PackStart (imageWidget, false, false, 0);
 
93
                                        box.PackStart (lab, false, false, 0);
 
94
                                } else {
 
95
                                        box.PackStart (lab, false, false, 0);
 
96
                                        box.PackStart (imageWidget, false, false, 0);
 
97
                                }
 
98
                                
 
99
                                contentWidget = box;
 
100
                        }
 
101
                        if (b.Type == ButtonType.DropDown) {
 
102
                                if (contentWidget != null) {
 
103
                                        Gtk.HBox box = new Gtk.HBox (false, 3);
 
104
                                        box.PackStart (contentWidget, true, true, 3);
 
105
                                        box.PackStart (new Gtk.VSeparator (), true, true, 0);
 
106
                                        box.PackStart (new Gtk.Arrow (Gtk.ArrowType.Down, Gtk.ShadowType.Out), false, false, 0);
 
107
                                        contentWidget = box;
 
108
                                } else
 
109
                                        contentWidget = new Gtk.Arrow (Gtk.ArrowType.Down, Gtk.ShadowType.Out);
 
110
                        }
 
111
                        if (contentWidget != null) {
 
112
                                contentWidget.ShowAll ();
 
113
                                Widget.Label = null;
 
114
                                Widget.Image = contentWidget;
 
115
                        } else
 
116
                                Widget.Label = null;
 
117
                }
 
118
                
 
119
                public void SetButtonStyle (ButtonStyle style)
 
120
                {
 
121
                        switch (style) {
 
122
                        case ButtonStyle.Normal:
 
123
                                SetMiniMode (false);
 
124
                                Widget.Relief = Gtk.ReliefStyle.Normal;
 
125
                                break;
 
126
                        case ButtonStyle.Flat:
 
127
                                SetMiniMode (false);
 
128
                                Widget.Relief = Gtk.ReliefStyle.None;
 
129
                                break;
 
130
                        case ButtonStyle.Borderless:
 
131
                                SetMiniMode (true);
 
132
                                Widget.Relief = Gtk.ReliefStyle.None;
 
133
                                break;
 
134
                        }
 
135
                }
 
136
                
 
137
                public void SetButtonType (ButtonType type)
 
138
                {
 
139
                        Button b = (Button) Frontend;
 
140
                        SetContent (b.Label, WidgetRegistry.GetBackend (b.Image), b.ImagePosition);
 
141
                }
 
142
                
 
143
                public override void EnableEvent (object eventId)
 
144
                {
 
145
                        base.EnableEvent (eventId);
 
146
                        if (eventId is ButtonEvent) {
 
147
                                switch ((ButtonEvent)eventId) {
 
148
                                case ButtonEvent.Clicked: Widget.Clicked += HandleWidgetClicked; break;
 
149
                                }
 
150
                        }
 
151
                }
 
152
                
 
153
                public override void DisableEvent (object eventId)
 
154
                {
 
155
                        base.DisableEvent (eventId);
 
156
                        if (eventId is ButtonEvent) {
 
157
                                switch ((ButtonEvent)eventId) {
 
158
                                case ButtonEvent.Clicked: Widget.Clicked -= HandleWidgetClicked; break;
 
159
                                }
 
160
                        }
 
161
                }
 
162
 
 
163
                void HandleWidgetClicked (object sender, EventArgs e)
 
164
                {
 
165
                        if (!ignoreClickEvents) {
 
166
                                Toolkit.Invoke (delegate {
 
167
                                        EventSink.OnClicked ();
 
168
                                });
 
169
                        }
 
170
                }
 
171
                
 
172
                bool miniMode;
 
173
                
 
174
                protected void SetMiniMode (bool miniMode)
 
175
                {
 
176
//                      Gtk.Rc.ParseString ("style \"Xwt.GtkBackend.CustomButton\" {\n GtkButton::inner-border = {0,0,0,0} GtkButton::child-displacement-x = {0} GtkButton::child-displacement-y = {0}\n }\n");
 
177
//                      Gtk.Rc.ParseString ("widget \"*.Xwt.GtkBackend.CustomButton\" style  \"Xwt.GtkBackend.CustomButton\"\n");
 
178
//                      Name = "Xwt.GtkBackend.CustomButton";
 
179
                        
 
180
                        if (this.miniMode == miniMode)
 
181
                                return;
 
182
                        this.miniMode = miniMode;
 
183
                        if (miniMode) {
 
184
                                Widget.ExposeEvent += HandleExposeEvent;
 
185
                                Widget.SizeAllocated += HandleSizeAllocated;
 
186
                                Widget.SizeRequested += HandleSizeRequested;
 
187
                        }
 
188
                        Widget.QueueResize ();
 
189
                }
 
190
 
 
191
                void HandleSizeRequested (object o, Gtk.SizeRequestedArgs args)
 
192
                {
 
193
                        args.Requisition = Widget.Child.SizeRequest ();
 
194
                }
 
195
 
 
196
                [GLib.ConnectBefore]
 
197
                void HandleSizeAllocated (object o, Gtk.SizeAllocatedArgs args)
 
198
                {
 
199
                        Widget.Child.SizeAllocate (args.Allocation);
 
200
                        args.RetVal = true;
 
201
                }
 
202
 
 
203
                [GLib.ConnectBefore]
 
204
                void HandleExposeEvent (object o, Gtk.ExposeEventArgs args)
 
205
                {
 
206
                        var gc = Widget.Style.BackgroundGC (Widget.State);
 
207
                        Widget.GdkWindow.DrawRectangle (gc, true, Widget.Allocation);
 
208
                        Widget.PropagateExpose (Widget.Child, args.Event);
 
209
                        args.RetVal = true;
 
210
                }
 
211
        }
 
212
}
 
213