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
28
using System.Collections.Generic;
32
namespace Xwt.GtkBackend
34
public class DialogBackend: WindowBackend, IDialogBackend
36
DialogButton[] dialogButtons;
39
public DialogBackend ()
43
public override void Initialize ()
45
Window = new Gtk.Dialog ();
46
Window.VBox.PackStart (CreateMainLayout ());
47
Window.ActionArea.Hide ();
50
new Gtk.Dialog Window {
51
get { return (Gtk.Dialog) base.Window; }
52
set { base.Window = value; }
55
new IDialogEventSink EventSink {
56
get { return (IDialogEventSink) base.EventSink; }
59
public void SetButtons (IEnumerable<DialogButton> newButtons)
61
if (buttons != null) {
62
foreach (var b in buttons) {
63
((Gtk.Container)b.Parent).Remove (b);
67
dialogButtons = newButtons.ToArray ();
68
buttons = new Gtk.Button [dialogButtons.Length];
70
for (int n=0; n<dialogButtons.Length; n++) {
71
var db = dialogButtons[n];
72
Gtk.Button b = new Gtk.Button ();
75
Window.ActionArea.Add (b);
78
buttons[n].Clicked += HandleButtonClicked;
80
UpdateActionAreaVisibility ();
83
void UpdateActionAreaVisibility ()
85
Window.ActionArea.Visible = Window.ActionArea.Children.Any (c => c.Visible);
88
void UpdateButton (DialogButton btn, Gtk.Button b)
90
if (!string.IsNullOrEmpty (btn.Label) && btn.Image == null) {
92
} else if (string.IsNullOrEmpty (btn.Label) && btn.Image != null) {
93
var pix = (Gdk.Pixbuf) WidgetRegistry.GetBackend (btn.Image);
94
b.Image = new Gtk.Image (pix);
95
} else if (!string.IsNullOrEmpty (btn.Label)) {
96
Gtk.Box box = new Gtk.HBox (false, 3);
97
var pix = (Gdk.Pixbuf) WidgetRegistry.GetBackend (btn.Image);
98
box.PackStart (new Gtk.Image (pix), false, false, 0);
99
box.PackStart (new Gtk.Label (btn.Label), true, true, 0);
106
b.Sensitive = btn.Sensitive;
107
UpdateActionAreaVisibility ();
110
void HandleButtonClicked (object o, EventArgs a)
112
int i = Array.IndexOf (buttons, (Gtk.Button) o);
113
Toolkit.Invoke (delegate {
114
EventSink.OnDialogButtonClicked (dialogButtons[i]);
118
public void UpdateButton (DialogButton btn)
120
int i = Array.IndexOf (dialogButtons, btn);
121
UpdateButton (btn, buttons[i]);
124
public void RunLoop (IWindowFrameBackend parent)
126
SetDefaultPosition ();
127
var p = (WindowFrameBackend) parent;
128
MessageService.RunCustomDialog (Window, p != null ? p.Window : null);
131
public void EndLoop ()
133
Window.Respond (Gtk.ResponseType.Ok);