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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt/Xwt/Dialog.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
// Dialog.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 Xwt.Drawing;
 
29
using Xwt.Engine;
 
30
 
 
31
namespace Xwt
 
32
{
 
33
        public class Dialog: Window
 
34
        {
 
35
                DialogButtonCollection commands;
 
36
                Command resultCommand;
 
37
                bool loopEnded;
 
38
                
 
39
                public Dialog ()
 
40
                {
 
41
                        commands = new DialogButtonCollection ((WindowBackendHost)BackendHost);
 
42
                }
 
43
                
 
44
                protected new class WindowBackendHost: Window.WindowBackendHost, ICollectionListener, IDialogEventSink
 
45
                {
 
46
                        new Dialog Parent { get { return (Dialog) base.Parent; } }
 
47
                        
 
48
                        public virtual void ItemAdded (object collection, object item)
 
49
                        {
 
50
                                if (collection == Parent.commands) {
 
51
                                        ((DialogButton)item).ParentDialog = Parent;
 
52
                                        Parent.Backend.SetButtons (Parent.commands);
 
53
                                }
 
54
                        }
 
55
 
 
56
                        public virtual void ItemRemoved (object collection, object item)
 
57
                        {
 
58
                                if (collection == Parent.commands) {
 
59
                                        ((DialogButton)item).ParentDialog = null;
 
60
                                        Parent.Backend.SetButtons (Parent.commands);
 
61
                                }
 
62
                        }
 
63
                        
 
64
                        public void OnDialogButtonClicked (DialogButton btn)
 
65
                        {
 
66
                                btn.RaiseClicked ();
 
67
                                if (btn.Command != null)
 
68
                                        Parent.OnCommandActivated (btn.Command);
 
69
                        }
 
70
                }
 
71
                
 
72
                protected override BackendHost CreateBackendHost ()
 
73
                {
 
74
                        return new WindowBackendHost ();
 
75
                }
 
76
                
 
77
                IDialogBackend Backend {
 
78
                        get { return (IDialogBackend) BackendHost.Backend; } 
 
79
                }
 
80
                
 
81
                public DialogButtonCollection Buttons {
 
82
                        get { return commands; }
 
83
                }
 
84
                
 
85
                protected virtual void OnCommandActivated (Command cmd)
 
86
                {
 
87
                        Respond (cmd);
 
88
                }
 
89
                
 
90
                public Command Run ()
 
91
                {
 
92
                        return Run (null);
 
93
                }
 
94
                
 
95
                public Command Run (WindowFrame parent)
 
96
                {
 
97
                        Toolkit.InvokePlatformCode (delegate {
 
98
                                Backend.RunLoop ((IWindowFrameBackend) WidgetRegistry.GetBackend (parent));
 
99
                        });
 
100
                        return resultCommand;
 
101
                }
 
102
                
 
103
                public void Respond (Command cmd)
 
104
                {
 
105
                        resultCommand = cmd;
 
106
                        if (!loopEnded) {
 
107
                                loopEnded = true;
 
108
                                Backend.EndLoop ();
 
109
                        }
 
110
                }
 
111
                
 
112
                public void EnableCommand (Command cmd)
 
113
                {
 
114
                        var btn = Buttons.GetCommandButton (cmd);
 
115
                        if (btn != null)
 
116
                                btn.Sensitive = true;
 
117
                }
 
118
                
 
119
                public void DisableCommand (Command cmd)
 
120
                {
 
121
                        var btn = Buttons.GetCommandButton (cmd);
 
122
                        if (btn != null)
 
123
                                btn.Sensitive = false;
 
124
                }
 
125
                
 
126
                public void ShowCommand (Command cmd)
 
127
                {
 
128
                        var btn = Buttons.GetCommandButton (cmd);
 
129
                        if (btn != null)
 
130
                                btn.Visible = true;
 
131
                }
 
132
                
 
133
                public void HideCommand (Command cmd)
 
134
                {
 
135
                        var btn = Buttons.GetCommandButton (cmd);
 
136
                        if (btn != null)
 
137
                                btn.Visible = false;
 
138
                }
 
139
                
 
140
                internal void UpdateButton (DialogButton btn)
 
141
                {
 
142
                        Backend.UpdateButton (btn);
 
143
                }
 
144
        }
 
145
        
 
146
        public class DialogButton
 
147
        {
 
148
                Command command;
 
149
                string label;
 
150
                Image image;
 
151
                bool visible = true;
 
152
                bool sensitive = true;
 
153
                internal Dialog ParentDialog;
 
154
                
 
155
                public DialogButton (string label)
 
156
                {
 
157
                        this.label = label;
 
158
                }
 
159
                
 
160
                public DialogButton (string label, Command cmd)
 
161
                {
 
162
                        this.label = label;
 
163
                        this.command = cmd;
 
164
                }
 
165
                
 
166
                public DialogButton (string label, Image icon)
 
167
                {
 
168
                        this.label = label;
 
169
                        this.image = icon;
 
170
                }
 
171
                
 
172
                public DialogButton (string label, Image icon, Command cmd)
 
173
                {
 
174
                        this.label = label;
 
175
                        this.command = cmd;
 
176
                        this.image = icon;
 
177
                }
 
178
                
 
179
                public DialogButton (Command cmd)
 
180
                {
 
181
                        this.command = cmd;
 
182
                }
 
183
                
 
184
                public Command Command {
 
185
                        get { return command; }
 
186
                }
 
187
                
 
188
                public string Label {
 
189
                        get {
 
190
                                if (label != null)
 
191
                                        return label;
 
192
                                if (command != null)
 
193
                                        return command.Label;
 
194
                                return "";
 
195
                        }
 
196
                        set {
 
197
                                label = value;
 
198
                                ParentDialog.UpdateButton (this);
 
199
                        }
 
200
                }
 
201
                
 
202
                public Image Image {
 
203
                        get {
 
204
                                if (image != null)
 
205
                                        return image;
 
206
                                return null;
 
207
                        }
 
208
                        set {
 
209
                                image = value;
 
210
                                ParentDialog.UpdateButton (this);
 
211
                        }
 
212
                }
 
213
                
 
214
                public bool Visible { 
 
215
                        get { return visible; }
 
216
                        set {
 
217
                                visible = value;
 
218
                                ParentDialog.UpdateButton (this);
 
219
                        }
 
220
                }
 
221
                
 
222
                public bool Sensitive { 
 
223
                        get { return sensitive; }
 
224
                        set {
 
225
                                sensitive = value;
 
226
                                ParentDialog.UpdateButton (this);
 
227
                        }
 
228
                }
 
229
                
 
230
                internal void RaiseClicked ()
 
231
                {
 
232
                        if (Clicked != null)
 
233
                                Clicked (this, EventArgs.Empty);
 
234
                }
 
235
                
 
236
                public event EventHandler Clicked;
 
237
        }
 
238
}
 
239