5
// Lluis Sanchez <lluis@xamarin.com>
7
// Copyright (c) 2011 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
31
namespace Xwt.GtkBackend
33
public class ButtonBackend: WidgetBackend, IButtonBackend
35
protected bool ignoreClickEvents;
37
public ButtonBackend ()
41
public override void Initialize ()
43
Widget = new Gtk.Button ();
48
protected new Gtk.Button Widget {
49
get { return (Gtk.Button)base.Widget; }
50
set { base.Widget = value; }
53
protected new IButtonEventSink EventSink {
54
get { return (IButtonEventSink)base.EventSink; }
57
public void SetContent (string label, object imageBackend, ContentPosition position)
59
if (label != null && label.Length == 0)
62
Button b = (Button) Frontend;
63
if (label != null && imageBackend == null && b.Type == ButtonType.Normal) {
68
if (b.Type == ButtonType.Disclosure) {
70
Widget.Image = new Gtk.Arrow (Gtk.ArrowType.Down, Gtk.ShadowType.Out);
71
Widget.Image.ShowAll ();
75
Gtk.Widget contentWidget = null;
77
Gtk.Widget imageWidget = null;
78
if (imageBackend != null)
79
imageWidget = new Gtk.Image ((Gdk.Pixbuf)imageBackend);
81
if (label != null && imageWidget == null) {
82
contentWidget = new Gtk.Label (label);
84
else if (label == null && imageWidget != null) {
85
contentWidget = imageWidget;
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);
91
if (position == ContentPosition.Left || position == ContentPosition.Top) {
92
box.PackStart (imageWidget, false, false, 0);
93
box.PackStart (lab, false, false, 0);
95
box.PackStart (lab, false, false, 0);
96
box.PackStart (imageWidget, false, false, 0);
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);
109
contentWidget = new Gtk.Arrow (Gtk.ArrowType.Down, Gtk.ShadowType.Out);
111
if (contentWidget != null) {
112
contentWidget.ShowAll ();
114
Widget.Image = contentWidget;
119
public void SetButtonStyle (ButtonStyle style)
122
case ButtonStyle.Normal:
124
Widget.Relief = Gtk.ReliefStyle.Normal;
126
case ButtonStyle.Flat:
128
Widget.Relief = Gtk.ReliefStyle.None;
130
case ButtonStyle.Borderless:
132
Widget.Relief = Gtk.ReliefStyle.None;
137
public void SetButtonType (ButtonType type)
139
Button b = (Button) Frontend;
140
SetContent (b.Label, WidgetRegistry.GetBackend (b.Image), b.ImagePosition);
143
public override void EnableEvent (object eventId)
145
base.EnableEvent (eventId);
146
if (eventId is ButtonEvent) {
147
switch ((ButtonEvent)eventId) {
148
case ButtonEvent.Clicked: Widget.Clicked += HandleWidgetClicked; break;
153
public override void DisableEvent (object eventId)
155
base.DisableEvent (eventId);
156
if (eventId is ButtonEvent) {
157
switch ((ButtonEvent)eventId) {
158
case ButtonEvent.Clicked: Widget.Clicked -= HandleWidgetClicked; break;
163
void HandleWidgetClicked (object sender, EventArgs e)
165
if (!ignoreClickEvents) {
166
Toolkit.Invoke (delegate {
167
EventSink.OnClicked ();
174
protected void SetMiniMode (bool miniMode)
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";
180
if (this.miniMode == miniMode)
182
this.miniMode = miniMode;
184
Widget.ExposeEvent += HandleExposeEvent;
185
Widget.SizeAllocated += HandleSizeAllocated;
186
Widget.SizeRequested += HandleSizeRequested;
188
Widget.QueueResize ();
191
void HandleSizeRequested (object o, Gtk.SizeRequestedArgs args)
193
args.Requisition = Widget.Child.SizeRequest ();
197
void HandleSizeAllocated (object o, Gtk.SizeAllocatedArgs args)
199
Widget.Child.SizeAllocate (args.Allocation);
204
void HandleExposeEvent (object o, Gtk.ExposeEventArgs args)
206
var gc = Widget.Style.BackgroundGC (Widget.State);
207
Widget.GdkWindow.DrawRectangle (gc, true, Widget.Allocation);
208
Widget.PropagateExpose (Widget.Child, args.Event);