1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
|
//
// VisualStyleRenderer.cs
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright (c) 2006 Novell, Inc.
//
// Authors:
// Jonathan Pobst (monkey@jpobst.com)
//
using System.Drawing;
namespace System.Windows.Forms.VisualStyles
{
public sealed class VisualStyleRenderer
{
private string class_name;
private int part;
private int state;
private IntPtr theme;
private int last_hresult = 0;
private ThemeHandleManager theme_handle_manager = new ThemeHandleManager ();
#region Public Constructors
public VisualStyleRenderer (string className, int part, int state)
{
theme_handle_manager.VisualStyleRenderer = this;
this.SetParameters (className, part, state);
}
public VisualStyleRenderer (VisualStyleElement element)
: this (element.ClassName, element.Part, element.State) {
}
#endregion
#region Public Properties
public String Class { get { return this.class_name; } }
public IntPtr Handle { get { return this.theme; } }
public int LastHResult { get { return this.last_hresult; } }
public int Part { get { return this.part; } }
public int State { get { return this.state; } }
public static bool IsSupported {
get {
if (!VisualStyleInformation.IsEnabledByUser)
return false;
if (Application.VisualStyleState == VisualStyleState.ClientAndNonClientAreasEnabled ||
Application.VisualStyleState == VisualStyleState.ClientAreaEnabled)
return true;
return false;
}
}
#endregion
#region Public Static Methods
public static bool IsElementDefined (VisualStyleElement element)
{
if (!IsSupported)
throw new InvalidOperationException ("Visual Styles are not enabled.");
if (IsElementKnownToBeSupported (element.ClassName, element.Part, element.State))
return true;
IntPtr theme = VisualStyles.UxThemeOpenThemeData (IntPtr.Zero, element.ClassName);
if (theme == IntPtr.Zero)
return false;
bool retval = VisualStyles.UxThemeIsThemePartDefined (theme, element.Part);
VisualStyles.UxThemeCloseThemeData (theme);
return retval;
}
#endregion
#region Public Instance Methods
public void DrawBackground (IDeviceContext dc, Rectangle bounds)
{
if (dc == null)
throw new ArgumentNullException ("dc");
last_hresult = VisualStyles.UxThemeDrawThemeBackground (theme, dc, this.part, this.state, bounds);
}
public void DrawBackground (IDeviceContext dc, Rectangle bounds, Rectangle clipRectangle)
{
if (dc == null)
throw new ArgumentNullException ("dc");
last_hresult = VisualStyles.UxThemeDrawThemeBackground (theme, dc, this.part, this.state, bounds, clipRectangle);
}
public Rectangle DrawEdge (IDeviceContext dc, Rectangle bounds, Edges edges, EdgeStyle style, EdgeEffects effects)
{
if (dc == null)
throw new ArgumentNullException ("dc");
Rectangle result;
last_hresult = VisualStyles.UxThemeDrawThemeEdge (theme, dc, this.part, this.state, bounds, edges, style, effects, out result);
return result;
}
public void DrawImage (Graphics g, Rectangle bounds, ImageList imageList, int imageIndex)
{
if (g == null)
throw new ArgumentNullException ("g");
if (imageIndex < 0 || imageIndex > imageList.Images.Count - 1)
throw new ArgumentOutOfRangeException ("imageIndex");
if (imageList.Images[imageIndex] == null)
throw new ArgumentNullException ("imageIndex");
g.DrawImage (imageList.Images[imageIndex], bounds);
}
public void DrawImage (Graphics g, Rectangle bounds, Image image)
{
if (g == null)
throw new ArgumentNullException ("g");
if (image == null)
throw new ArgumentNullException ("image");
g.DrawImage (image, bounds);
}
public void DrawParentBackground (IDeviceContext dc, Rectangle bounds, Control childControl)
{
if (dc == null)
throw new ArgumentNullException ("dc");
last_hresult = VisualStyles.UxThemeDrawThemeParentBackground (dc, bounds, childControl);
}
public void DrawText (IDeviceContext dc, Rectangle bounds, string textToDraw, bool drawDisabled, TextFormatFlags flags)
{
if (dc == null)
throw new ArgumentNullException ("dc");
last_hresult = VisualStyles.UxThemeDrawThemeText (theme, dc, this.part, this.state, textToDraw, flags, bounds);
}
public void DrawText (IDeviceContext dc, Rectangle bounds, string textToDraw, bool drawDisabled)
{
this.DrawText (dc, bounds, textToDraw, drawDisabled, TextFormatFlags.Default);
}
public void DrawText (IDeviceContext dc, Rectangle bounds, string textToDraw)
{
this.DrawText (dc, bounds, textToDraw, false, TextFormatFlags.Default);
}
public Rectangle GetBackgroundContentRectangle (IDeviceContext dc, Rectangle bounds)
{
if (dc == null)
throw new ArgumentNullException ("dc");
Rectangle result;
last_hresult = VisualStyles.UxThemeGetThemeBackgroundContentRect (theme, dc, this.part, this.state, bounds, out result);
return result;
}
public Rectangle GetBackgroundExtent (IDeviceContext dc, Rectangle contentBounds)
{
if (dc == null)
throw new ArgumentNullException ("dc");
Rectangle result;
last_hresult = VisualStyles.UxThemeGetThemeBackgroundExtent (theme, dc, this.part, this.state, contentBounds, out result);
return result;
}
[System.Security.SuppressUnmanagedCodeSecurity]
public Region GetBackgroundRegion (IDeviceContext dc, Rectangle bounds)
{
if (dc == null)
throw new ArgumentNullException ("dc");
Region result;
last_hresult = VisualStyles.UxThemeGetThemeBackgroundRegion (theme, dc, this.part, this.state, bounds, out result);
return result;
}
public bool GetBoolean (BooleanProperty prop)
{
if (!Enum.IsDefined (typeof (BooleanProperty), prop))
throw new System.ComponentModel.InvalidEnumArgumentException ("prop", (int)prop, typeof (BooleanProperty));
bool result;
last_hresult = VisualStyles.UxThemeGetThemeBool (theme, this.part, this.state, prop, out result);
return result;
}
public Color GetColor (ColorProperty prop)
{
if (!Enum.IsDefined (typeof (ColorProperty), prop))
throw new System.ComponentModel.InvalidEnumArgumentException ("prop", (int)prop, typeof (ColorProperty));
Color result;
last_hresult = VisualStyles.UxThemeGetThemeColor (theme, this.part, this.state, prop, out result);
return result;
}
public int GetEnumValue (EnumProperty prop)
{
if (!Enum.IsDefined (typeof (EnumProperty), prop))
throw new System.ComponentModel.InvalidEnumArgumentException ("prop", (int)prop, typeof (EnumProperty));
int result;
last_hresult = VisualStyles.UxThemeGetThemeEnumValue (theme, this.part, this.state, prop, out result);
return result;
}
public string GetFilename (FilenameProperty prop)
{
if (!Enum.IsDefined (typeof (FilenameProperty), prop))
throw new System.ComponentModel.InvalidEnumArgumentException ("prop", (int)prop, typeof (FilenameProperty));
string result;
last_hresult = VisualStyles.UxThemeGetThemeFilename (theme, this.part, this.state, prop, out result);
return result;
}
[MonoTODO(@"I can't get MS's to return anything but null, so I can't really get this one right")]
public Font GetFont (IDeviceContext dc, FontProperty prop)
{
throw new NotImplementedException();
//if (dc == null)
// throw new ArgumentNullException ("dc");
//if (!Enum.IsDefined (typeof (FontProperty), prop))
// throw new System.ComponentModel.InvalidEnumArgumentException ("prop", (int)prop, typeof (FontProperty));
//UXTheme.LOGFONT lf = new UXTheme.LOGFONT();
//UXTheme.GetThemeFont (theme, dc.GetHdc (), this.part, this.state, (int)prop, out lf);
//IntPtr fontPtr = UXTheme.CreateFontIndirect(lf);
//dc.ReleaseHdc();
//return Font.FromLogFont(lf);
//return null;
}
public int GetInteger (IntegerProperty prop)
{
if (!Enum.IsDefined (typeof (IntegerProperty), prop))
throw new System.ComponentModel.InvalidEnumArgumentException ("prop", (int)prop, typeof (IntegerProperty));
int result;
last_hresult = VisualStyles.UxThemeGetThemeInt (theme, this.part, this.state, prop, out result);
return result;
}
[MonoTODO(@"MS's causes a PInvokeStackUnbalance on me, so this is not verified against MS.")]
public Padding GetMargins (IDeviceContext dc, MarginProperty prop)
{
if (dc == null)
throw new ArgumentNullException ("dc");
if (!Enum.IsDefined (typeof (MarginProperty), prop))
throw new System.ComponentModel.InvalidEnumArgumentException ("prop", (int)prop, typeof (MarginProperty));
Padding result;
last_hresult = VisualStyles.UxThemeGetThemeMargins (theme, dc, this.part, this.state, prop, out result);
return result;
}
public Size GetPartSize (IDeviceContext dc, Rectangle bounds, ThemeSizeType type)
{
if (dc == null)
throw new ArgumentNullException ("dc");
if (!Enum.IsDefined (typeof (ThemeSizeType), type))
throw new System.ComponentModel.InvalidEnumArgumentException ("prop", (int)type, typeof (ThemeSizeType));
Size result;
last_hresult = VisualStyles.UxThemeGetThemePartSize (theme, dc, this.part, this.state, bounds, type, out result);
return result;
}
public Size GetPartSize (IDeviceContext dc, ThemeSizeType type)
{
if (dc == null)
throw new ArgumentNullException ("dc");
if (!Enum.IsDefined (typeof (ThemeSizeType), type))
throw new System.ComponentModel.InvalidEnumArgumentException ("prop", (int)type, typeof (ThemeSizeType));
Size result;
last_hresult = VisualStyles.UxThemeGetThemePartSize (theme, dc, this.part, this.state, type, out result);
return result;
}
public Point GetPoint (PointProperty prop)
{
if (!Enum.IsDefined (typeof (PointProperty), prop))
throw new System.ComponentModel.InvalidEnumArgumentException ("prop", (int)prop, typeof (PointProperty));
Point result;
last_hresult = VisualStyles.UxThemeGetThemePosition (theme, this.part, this.state, prop, out result);
return result;
}
[MonoTODO(@"Can't find any values that return anything on MS to test against")]
public string GetString (StringProperty prop)
{
if (!Enum.IsDefined (typeof (StringProperty), prop))
throw new System.ComponentModel.InvalidEnumArgumentException ("prop", (int)prop, typeof (StringProperty));
string result;
last_hresult = VisualStyles.UxThemeGetThemeString (theme, this.part, this.state, prop, out result);
return result;
}
public Rectangle GetTextExtent (IDeviceContext dc, Rectangle bounds, string textToDraw, TextFormatFlags flags)
{
if (dc == null)
throw new ArgumentNullException ("dc");
Rectangle result;
last_hresult = VisualStyles.UxThemeGetThemeTextExtent (theme, dc, this.part, this.state, textToDraw, flags, bounds, out result);
return result;
}
public Rectangle GetTextExtent (IDeviceContext dc, string textToDraw, TextFormatFlags flags)
{
if (dc == null)
throw new ArgumentNullException ("dc");
Rectangle result;
last_hresult = VisualStyles.UxThemeGetThemeTextExtent (theme, dc, this.part, this.state, textToDraw, flags, out result);
return result;
}
public TextMetrics GetTextMetrics (IDeviceContext dc)
{
if (dc == null)
throw new ArgumentNullException ("dc", "dc cannot be null.");
TextMetrics result;
last_hresult = VisualStyles.UxThemeGetThemeTextMetrics (theme, dc, this.part, this.state, out result);
return result;
}
public HitTestCode HitTestBackground (IDeviceContext dc, Rectangle backgroundRectangle, IntPtr hRgn, Point pt, HitTestOptions options)
{
if (dc == null)
throw new ArgumentNullException ("dc");
HitTestCode result;
last_hresult = VisualStyles.UxThemeHitTestThemeBackground(theme, dc, this.part, this.state, options, backgroundRectangle, hRgn, pt, out result);
return result;
}
public HitTestCode HitTestBackground (Graphics g, Rectangle backgroundRectangle, Region region, Point pt, HitTestOptions options)
{
if (g == null)
throw new ArgumentNullException ("g");
IntPtr hRgn = region.GetHrgn(g);
return this.HitTestBackground(g, backgroundRectangle, hRgn, pt, options);
}
public HitTestCode HitTestBackground (IDeviceContext dc, Rectangle backgroundRectangle, Point pt, HitTestOptions options)
{
return this.HitTestBackground (dc, backgroundRectangle, IntPtr.Zero, pt, options);
}
public bool IsBackgroundPartiallyTransparent ()
{
return VisualStyles.UxThemeIsThemeBackgroundPartiallyTransparent (theme, this.part, this.state);
}
public void SetParameters (string className, int part, int state)
{
if (theme != IntPtr.Zero)
last_hresult = VisualStyles.UxThemeCloseThemeData (theme);
if (!IsSupported)
throw new InvalidOperationException ("Visual Styles are not enabled.");
this.class_name = className;
this.part = part;
this.state = state;
theme = VisualStyles.UxThemeOpenThemeData (IntPtr.Zero, this.class_name);
if (IsElementKnownToBeSupported (className, part, state))
return;
if (theme == IntPtr.Zero || !VisualStyles.UxThemeIsThemePartDefined (theme, this.part))
throw new ArgumentException ("This element is not supported by the current visual style.");
}
public void SetParameters (VisualStyleElement element)
{
this.SetParameters (element.ClassName, element.Part, element.State);
}
#endregion
#region Private Properties
internal static IVisualStyles VisualStyles {
get { return VisualStylesEngine.Instance; }
}
#endregion
#region Private Instance Methods
internal void DrawBackgroundExcludingArea (IDeviceContext dc, Rectangle bounds, Rectangle excludedArea)
{
VisualStyles.VisualStyleRendererDrawBackgroundExcludingArea (theme, dc, part, state, bounds, excludedArea);
}
#endregion
#region Private Static Methods
private static bool IsElementKnownToBeSupported (string className, int part, int state)
{
return className == "STATUS" && part == 0 && state == 0;
}
#endregion
#region Private Classes
private class ThemeHandleManager
{
public VisualStyleRenderer VisualStyleRenderer;
~ThemeHandleManager ()
{
if (VisualStyleRenderer.theme == IntPtr.Zero)
return;
VisualStyles.UxThemeCloseThemeData (VisualStyleRenderer.theme);
}
}
#endregion
}
}
|