Mercurial > silverbladetech
comparison Chronosv2/source/Presentation/Windows/Controls/MessageWindowElement.cs @ 10:443821e55f06
Initial cleaned up add from Codeplex files
author | stevenh7776 stevenhollidge@hotmail.com |
---|---|
date | Tue, 21 Feb 2012 17:25:44 +0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
9:904a9faadf8b | 10:443821e55f06 |
---|---|
1 /* | |
2 The MIT License | |
3 | |
4 Copyright (c) 2009-2010. Carlos Guzmán Álvarez. http://chronoswpf.codeplex.com/ | |
5 | |
6 Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 of this software and associated documentation files (the "Software"), to deal | |
8 in the Software without restriction, including without limitation the rights | |
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 copies of the Software, and to permit persons to whom the Software is | |
11 furnished to do so, subject to the following conditions: | |
12 | |
13 The above copyright notice and this permission notice shall be included in | |
14 all copies or substantial portions of the Software. | |
15 | |
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 THE SOFTWARE. | |
23 */ | |
24 | |
25 using System; | |
26 using System.Windows; | |
27 using System.Windows.Input; | |
28 using Chronos.Extensions.Windows; | |
29 using Chronos.Presentation.Core.VirtualDesktops; | |
30 using Chronos.Presentation.Core.Windows; | |
31 using nRoute.Services; | |
32 | |
33 namespace Chronos.Presentation.Windows.Controls | |
34 { | |
35 /// <summary> | |
36 /// Provides the ability to create modal message windows (like messageboxes) | |
37 /// </summary> | |
38 public sealed class MessageWindowElement | |
39 : WindowElement | |
40 { | |
41 #region · Dependency Properties · | |
42 | |
43 /// <summary> | |
44 /// Identifies the Buttons dependency property. | |
45 /// </summary> | |
46 public static readonly DependencyProperty ButtonsProperty = | |
47 DependencyProperty.Register("Buttons", typeof(DialogButton), typeof(MessageWindowElement), | |
48 new FrameworkPropertyMetadata(DialogButton.Ok)); | |
49 | |
50 #endregion | |
51 | |
52 #region · Sync Object · | |
53 | |
54 static readonly object SyncObject = new object(); | |
55 | |
56 #endregion | |
57 | |
58 #region · Static Commands · | |
59 | |
60 /// <summary> | |
61 /// Accept command | |
62 /// </summary> | |
63 public static RoutedCommand AcceptCommand; | |
64 | |
65 /// <summary> | |
66 /// Cancel command | |
67 /// </summary> | |
68 public static RoutedCommand CancelCommand; | |
69 | |
70 #endregion | |
71 | |
72 #region · Static Constructors · | |
73 | |
74 /// <summary> | |
75 /// Initializes the <see cref="MessageWindowElement"/> class. | |
76 /// </summary> | |
77 static MessageWindowElement() | |
78 { | |
79 MessageWindowElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(MessageWindowElement), | |
80 new FrameworkPropertyMetadata(typeof(MessageWindowElement))); | |
81 | |
82 KeyboardNavigation.IsTabStopProperty.OverrideMetadata(typeof(MessageWindowElement), | |
83 new FrameworkPropertyMetadata(false)); | |
84 | |
85 MessageWindowElement.AcceptCommand = new RoutedCommand("Accept", typeof(MessageWindowElement)); | |
86 MessageWindowElement.CancelCommand = new RoutedCommand("Cancel", typeof(MessageWindowElement)); | |
87 } | |
88 | |
89 #endregion | |
90 | |
91 #region · Show "Factory" Methods · | |
92 | |
93 /// <summary> | |
94 /// Shows a new <see cref="MessageWindowElement"/> with the given message | |
95 /// </summary> | |
96 /// <param name="message"></param> | |
97 /// <returns></returns> | |
98 public static DialogResult Show(string message) | |
99 { | |
100 return MessageWindowElement.Show("Message", message, DialogButton.OkCancel); | |
101 } | |
102 | |
103 /// <summary> | |
104 /// Shows a new <see cref="MessageWindowElement"/> with the given caption and message | |
105 /// </summary> | |
106 /// <param name="caption"></param> | |
107 /// <param name="message"></param> | |
108 /// <returns></returns> | |
109 public static DialogResult Show(string caption, string message) | |
110 { | |
111 return MessageWindowElement.Show(caption, message, DialogButton.OkCancel); | |
112 } | |
113 | |
114 /// <summary> | |
115 /// Shows a new <see cref="MessageWindowElement"/> with the given caption, message and buttons | |
116 /// </summary> | |
117 /// <param name="caption"></param> | |
118 /// <param name="message"></param> | |
119 /// <param name="buttons"></param> | |
120 /// <returns></returns> | |
121 public static DialogResult Show(string caption, string message, DialogButton buttons) | |
122 { | |
123 lock (SyncObject) | |
124 { | |
125 DialogResult result = DialogResult.None; | |
126 | |
127 System.Windows.Application.Current.Dispatcher.Invoke( | |
128 (Action)delegate | |
129 { | |
130 MessageWindowElement window = new MessageWindowElement | |
131 { | |
132 Title = caption, | |
133 Content = message, | |
134 Buttons = buttons, | |
135 StartupLocation = StartupPosition.CenterParent | |
136 }; | |
137 | |
138 result = ServiceLocator.GetService<IVirtualDesktopManager>().ShowDialog(window); | |
139 }); | |
140 | |
141 return result; | |
142 } | |
143 } | |
144 | |
145 #endregion | |
146 | |
147 #region · Properties · | |
148 | |
149 /// <summary> | |
150 /// Gets or sets the button combination to be shown | |
151 /// </summary> | |
152 public DialogButton Buttons | |
153 { | |
154 get { return (DialogButton)base.GetValue(MessageWindowElement.ButtonsProperty); } | |
155 set { base.SetValue(MessageWindowElement.ButtonsProperty, value); } | |
156 } | |
157 | |
158 #endregion | |
159 | |
160 #region · Constructors · | |
161 | |
162 /// <summary> | |
163 /// Initializes a new instance of the <see cref="MessageWindowElement"/> class | |
164 /// </summary> | |
165 public MessageWindowElement() | |
166 : base() | |
167 { | |
168 CommandBinding bindinAccept = new CommandBinding(MessageWindowElement.AcceptCommand, new ExecutedRoutedEventHandler(OnAccept)); | |
169 this.CommandBindings.Add(bindinAccept); | |
170 | |
171 CommandBinding bindingCancel = new CommandBinding(MessageWindowElement.CancelCommand, new ExecutedRoutedEventHandler(OnCancel)); | |
172 this.CommandBindings.Add(bindingCancel); | |
173 } | |
174 | |
175 #endregion | |
176 | |
177 #region · Protected Methods · | |
178 | |
179 /// <summary> | |
180 /// Focuses the window | |
181 /// </summary> | |
182 protected override void GiveFocus() | |
183 { | |
184 this.SetFocus(); | |
185 } | |
186 | |
187 #endregion | |
188 | |
189 #region · Command Actions · | |
190 | |
191 private void OnAccept(object sender, ExecutedRoutedEventArgs e) | |
192 { | |
193 if (this.Buttons == DialogButton.Ok || | |
194 this.Buttons == DialogButton.OkCancel) | |
195 { | |
196 this.DialogResult = DialogResult.Ok; | |
197 } | |
198 else | |
199 { | |
200 this.DialogResult = DialogResult.Yes; | |
201 } | |
202 | |
203 this.Hide(); | |
204 } | |
205 | |
206 private void OnCancel(object sender, ExecutedRoutedEventArgs e) | |
207 { | |
208 if (this.Buttons == DialogButton.OkCancel) | |
209 { | |
210 this.DialogResult = DialogResult.Cancel; | |
211 } | |
212 else | |
213 { | |
214 this.DialogResult = DialogResult.No; | |
215 } | |
216 | |
217 this.Hide(); | |
218 } | |
219 | |
220 #endregion | |
221 } | |
222 } |