comparison Chronosv2/source/Presentation/Windows/Controls/WidgetElement.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.Controls;
28 using System.Windows.Controls.Primitives;
29 using System.Windows.Input;
30 using System.Windows.Media;
31 using Chronos.Extensions.Windows;
32
33 namespace Chronos.Presentation.Windows.Controls
34 {
35 /// <summary>
36 /// Provides the ability to create, configure, show, and manage the lifetime of widgets
37 /// </summary>
38 [TemplatePart(Name = WidgetElement.PART_MinimizeButton, Type = typeof(ButtonBase))]
39 [TemplatePart(Name = WidgetElement.PART_CloseButton, Type = typeof(ButtonBase))]
40 [TemplatePart(Name = WidgetElement.PART_ContentPresenter, Type = typeof(ContentPresenter))]
41 public class WidgetElement
42 : DesktopElement
43 {
44 #region · Constants ·
45
46 private const string PART_ContentPresenter = "PART_ContentPresenter";
47 private const string PART_MinimizeButton = "PART_MinimizeButton";
48 private const string PART_CloseButton = "PART_CloseButton";
49
50 #endregion
51
52 #region · Dependency Properties ·
53
54 /// <summary>
55 /// Identifies the Title dependency property.
56 /// </summary>
57 public static readonly DependencyProperty TitleProperty =
58 DependencyProperty.Register("Title", typeof(String), typeof(WidgetElement),
59 new FrameworkPropertyMetadata(String.Empty));
60
61 /// <summary>
62 /// Identifies the WindowState dependency property.
63 /// </summary>
64 public static readonly DependencyProperty WidgetStateProperty =
65 DependencyProperty.Register("WidgetState", typeof(WindowState), typeof(WidgetElement),
66 new FrameworkPropertyMetadata(WindowState.Normal));
67
68 /// <summary>
69 /// Identifies the ShowMinimizeButton dependency property.
70 /// </summary>
71 public static readonly DependencyProperty ShowMinimizeButtonProperty =
72 DependencyProperty.Register("ShowMinimizeButton", typeof(bool), typeof(WidgetElement),
73 new FrameworkPropertyMetadata(true));
74
75 #endregion
76
77 #region · Routed Commands ·
78
79 /// <summary>
80 /// Minimize window command
81 /// </summary>
82 public static RoutedCommand MinimizeCommand;
83
84 #endregion
85
86 #region · Static Constructor ·
87
88 /// <summary>
89 /// Initializes the <see cref="WidgetElement"/> class.
90 /// </summary>
91 static WidgetElement()
92 {
93 WidgetElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(WidgetElement),
94 new FrameworkPropertyMetadata(typeof(WidgetElement)));
95
96 WidgetElement.MinimizeCommand = new RoutedCommand("Minimize", typeof(WidgetElement));
97
98 Control.IsTabStopProperty.OverrideMetadata(typeof(WidgetElement),
99 new FrameworkPropertyMetadata(false));
100
101 KeyboardNavigation.DirectionalNavigationProperty.OverrideMetadata(
102 typeof(WidgetElement), new FrameworkPropertyMetadata(KeyboardNavigationMode.Cycle));
103
104 KeyboardNavigation.TabNavigationProperty.OverrideMetadata(
105 typeof(WidgetElement), new FrameworkPropertyMetadata(KeyboardNavigationMode.Cycle));
106
107 KeyboardNavigation.ControlTabNavigationProperty.OverrideMetadata(
108 typeof(WidgetElement), new FrameworkPropertyMetadata(KeyboardNavigationMode.Once));
109
110 if (!DesignMode.IsInDesignMode
111 && Application.Current.GetRenderTier() != RenderTier.Tier2)
112 {
113 WidgetElement.CacheModeProperty.OverrideMetadata(typeof(WidgetElement),
114 new FrameworkPropertyMetadata(new BitmapCache { EnableClearType = true, RenderAtScale = 1, SnapsToDevicePixels = true }));
115 }
116 }
117
118 #endregion
119
120 #region · Fields ·
121
122 private Size previousSize;
123
124 #endregion
125
126 #region · Properties ·
127
128 /// <summary>
129 /// Gets or sets a value that indicates whether a window is restored or minimized.
130 /// This is a dependency property.
131 /// </summary>
132 /// <value>A <see cref="WindowState"/> that determines whether a window is restored, minimized, or maximized. The default is Normal (restored).</value>
133 public WindowState WidgetState
134 {
135 get { return (WindowState)base.GetValue(WidgetStateProperty); }
136 set
137 {
138 if ((WindowState)this.GetValue(WidgetStateProperty) != value)
139 {
140 base.SetValue(WidgetStateProperty, value);
141 }
142
143 this.AdjustLayout();
144 }
145 }
146
147 /// <summary>
148 /// Gets a value that indicates whether the minimize button is visible.
149 /// This is a dependency property.
150 /// </summary>
151 public bool ShowMinimizeButton
152 {
153 get { return (bool)base.GetValue(ShowMinimizeButtonProperty); }
154 set { base.SetValue(ShowMinimizeButtonProperty, value); }
155 }
156
157 /// <summary>
158 /// Gets or sets the Widget title
159 /// </summary>
160 public string Title
161 {
162 get { return (string)base.GetValue(TitleProperty); }
163 set { base.SetValue(TitleProperty, value); }
164 }
165
166 #endregion
167
168 #region · Internal Properties ·
169
170 /// <summary>
171 /// Gets the Widget real Height based on the <see cref="WidgetState"/>.
172 /// </summary>
173 /// <remarks>Used for Widget serialization on <see cref="DesktopSerializer"/> class</remarks>
174 internal double RealHeight
175 {
176 get
177 {
178 if (this.WidgetState == WindowState.Minimized)
179 {
180 return this.previousSize.Height;
181 }
182
183 return this.Height;
184 }
185 }
186
187 #endregion
188
189 #region · Constructors ·
190
191 /// <summary>
192 /// Initializes a new instance of the <see cref="WidgetElement"/> class.
193 /// </summary>
194 public WidgetElement()
195 : base()
196 {
197 CommandBinding bindingMinimize = new CommandBinding(WidgetElement.MinimizeCommand, new ExecutedRoutedEventHandler(this.OnMinimizeWindow));
198 this.CommandBindings.Add(bindingMinimize);
199 }
200
201 #endregion
202
203 #region · Methods ·
204
205 /// <summary>
206 /// Closes the desktop element
207 /// </summary>
208 public override void Close()
209 {
210 this.CommandBindings.Clear();
211
212 base.Close();
213 }
214
215 #endregion
216
217 #region · Protected Methods
218
219 /// <summary>
220 /// Focuses the window
221 /// </summary>
222 protected override void GiveFocus()
223 {
224 this.MoveFocus(FocusNavigationDirection.Next);
225 }
226
227 #endregion
228
229 #region · Command Actions ·
230
231 /// <summary>
232 /// Occurs when the widget is going to be minimized
233 /// </summary>
234 /// <param name="sender"></param>
235 /// <param name="e"></param>
236 private void OnMinimizeWindow(object sender, ExecutedRoutedEventArgs e)
237 {
238 if (this.WidgetState != WindowState.Minimized)
239 {
240 this.WidgetState = WindowState.Minimized;
241 }
242 else
243 {
244 this.WidgetState = WindowState.Normal;
245 }
246 }
247
248 #endregion
249
250 #region · Private Methods ·
251
252 private void AdjustLayout()
253 {
254 if (this.WidgetState == WindowState.Minimized)
255 {
256 this.previousSize = new Size(this.Width, this.Height);
257 this.Height = 35;
258 this.CanResize = false;
259 }
260 else
261 {
262 this.Height = this.previousSize.Height;
263 this.previousSize = new Size(this.Width, this.Height);
264 this.CanResize = true;
265 }
266 }
267
268 #endregion
269 }
270 }