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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.Gtk/Xwt.GtkBackend/DialogBackend.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
// DialogBackend.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
using System;
 
27
using Xwt.Backends;
 
28
using System.Collections.Generic;
 
29
using System.Linq;
 
30
using Xwt.Engine;
 
31
 
 
32
namespace Xwt.GtkBackend
 
33
{
 
34
        public class DialogBackend: WindowBackend, IDialogBackend
 
35
        {
 
36
                DialogButton[] dialogButtons;
 
37
                Gtk.Button[] buttons;
 
38
                
 
39
                public DialogBackend ()
 
40
                {
 
41
                }
 
42
                
 
43
                public override void Initialize ()
 
44
                {
 
45
                        Window = new Gtk.Dialog ();
 
46
                        Window.VBox.PackStart (CreateMainLayout ());
 
47
                        Window.ActionArea.Hide ();
 
48
                }
 
49
                
 
50
                new Gtk.Dialog Window {
 
51
                        get { return (Gtk.Dialog) base.Window; }
 
52
                        set { base.Window = value; }
 
53
                }
 
54
                
 
55
                new IDialogEventSink EventSink {
 
56
                        get { return (IDialogEventSink) base.EventSink; }
 
57
                }
 
58
                
 
59
                public void SetButtons (IEnumerable<DialogButton> newButtons)
 
60
                {
 
61
                        if (buttons != null) {
 
62
                                foreach (var b in buttons) {
 
63
                                        ((Gtk.Container)b.Parent).Remove (b);
 
64
                                        b.Destroy ();
 
65
                                }
 
66
                        }
 
67
                        dialogButtons = newButtons.ToArray ();
 
68
                        buttons = new Gtk.Button [dialogButtons.Length];
 
69
                        
 
70
                        for (int n=0; n<dialogButtons.Length; n++) {
 
71
                                var db = dialogButtons[n];
 
72
                                Gtk.Button b = new Gtk.Button ();
 
73
                                b.Show ();
 
74
                                b.Label = db.Label;
 
75
                                Window.ActionArea.Add (b);
 
76
                                UpdateButton (db, b);
 
77
                                buttons[n] = b;
 
78
                                buttons[n].Clicked += HandleButtonClicked;
 
79
                        }
 
80
                        UpdateActionAreaVisibility ();
 
81
                }
 
82
 
 
83
                void UpdateActionAreaVisibility ()
 
84
                {
 
85
                        Window.ActionArea.Visible = Window.ActionArea.Children.Any (c => c.Visible);
 
86
                }
 
87
                
 
88
                void UpdateButton (DialogButton btn, Gtk.Button b)
 
89
                {
 
90
                        if (!string.IsNullOrEmpty (btn.Label) && btn.Image == null) {
 
91
                                b.Label = btn.Label;
 
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);
 
100
                                b.Image = box;
 
101
                        }
 
102
                        if (btn.Visible)
 
103
                                b.ShowAll ();
 
104
                        else
 
105
                                b.Hide ();
 
106
                        b.Sensitive = btn.Sensitive;
 
107
                        UpdateActionAreaVisibility ();
 
108
                }
 
109
                
 
110
                void HandleButtonClicked (object o, EventArgs a)
 
111
                {
 
112
                        int i = Array.IndexOf (buttons, (Gtk.Button) o);
 
113
                        Toolkit.Invoke (delegate {
 
114
                                EventSink.OnDialogButtonClicked (dialogButtons[i]);
 
115
                        });
 
116
                }
 
117
 
 
118
                public void UpdateButton (DialogButton btn)
 
119
                {
 
120
                        int i = Array.IndexOf (dialogButtons, btn);
 
121
                        UpdateButton (btn, buttons[i]);
 
122
                }
 
123
 
 
124
                public void RunLoop (IWindowFrameBackend parent)
 
125
                {
 
126
                        SetDefaultPosition ();
 
127
                        var p = (WindowFrameBackend) parent;
 
128
                        MessageService.RunCustomDialog (Window, p != null ? p.Window : null);
 
129
                }
 
130
 
 
131
                public void EndLoop ()
 
132
                {
 
133
                        Window.Respond (Gtk.ResponseType.Ok);
 
134
                }
 
135
        }
 
136
}
 
137