Skip to content

Commit

Permalink
Fixes #543 and #544
Browse files Browse the repository at this point in the history
  • Loading branch information
batzen committed Mar 13, 2018
1 parent af63135 commit c286c7a
Show file tree
Hide file tree
Showing 25 changed files with 521 additions and 160 deletions.
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@

- ### Bug fixes
- [#535](../../issues/535) - BorderBush on bottom of RibbonTabItem (and Ribbon)
- [#543](../../issues/543) - Using images that can't be found during design time crashes designer
A generic "error" image is rendered during design time and an exception is thrown during runtime.

- ### Enhancements
- [#533](../../issues/533) - Issue when using templated ribbon items
- [#544](../../issues/544) - Add proper DPI support for icons/images aquired through ObjectToImageConverter on .NET 4.6.2

## 6.1.0

Expand Down
1 change: 0 additions & 1 deletion Fluent.Ribbon/Controls/RibbonTabControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ namespace Fluent
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using ControlzEx.Standard;
using Fluent.Internal;
using Fluent.Internal.KnownBoxes;
Expand Down
99 changes: 74 additions & 25 deletions Fluent.Ribbon/Converters/IconConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,114 @@ namespace Fluent
{
using System;
using System.Diagnostics;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using ControlzEx.Standard;
using Fluent.Converters;
using Fluent.Internal;

/// <summary>
/// Icon converter provides application default icon if user-defined is not present.
/// Icon converter provides window or application default icon if user-defined is not present.
/// </summary>
[ValueConversion(sourceType: typeof(string), targetType: typeof(Image))]
[ValueConversion(sourceType: typeof(Uri), targetType: typeof(Image))]
[ValueConversion(sourceType: typeof(System.Drawing.Icon), targetType: typeof(Image))]
[ValueConversion(sourceType: typeof(ImageSource), targetType: typeof(Image))]
[ValueConversion(sourceType: typeof(string), targetType: typeof(ImageSource))]
[ValueConversion(sourceType: typeof(Uri), targetType: typeof(ImageSource))]
[ValueConversion(sourceType: typeof(System.Drawing.Icon), targetType: typeof(ImageSource))]
[ValueConversion(sourceType: typeof(ImageSource), targetType: typeof(ImageSource))]
public sealed class IconConverter : IValueConverter
public sealed class IconConverter : ObjectToImageConverter
{
#region Implementation of IValueConverter
/// <summary>
/// Creates a new instance.
/// </summary>
/// <param name="iconBinding">The binding to which the converter should be applied to.</param>
public IconConverter(Binding iconBinding)
: base(iconBinding, new Size(SystemParameters.SmallIconWidth, SystemParameters.SmallIconHeight))
{
}

/// <inheritdoc />
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
/// <summary>
/// Creates a new instance.
/// </summary>
/// <param name="desiredSize">The desired size for the image.</param>
public IconConverter(Size desiredSize)
: base(desiredSize)
{
var desiredSize = new Size(SystemParameters.SmallIconWidth, SystemParameters.SmallIconHeight);
if (desiredSize.IsEmpty
|| DoubleUtil.AreClose(desiredSize.Width, 0)
|| DoubleUtil.AreClose(desiredSize.Height, 0))
{
throw new ArgumentException("DesiredSize must not be empty and width/height must be greater than 0.", nameof(desiredSize));
}
}

/// <inheritdoc />
protected override object GetValueToConvert(object value, Size desiredSize)
{
if (value == null)
{
if (Application.Current != null
&& Application.Current.CheckAccess()
&& Application.Current.MainWindow != null
&& Application.Current.MainWindow.CheckAccess())
var defaultIcon = GetDefaultIcon(this.TargetVisual, desiredSize);

if (defaultIcon != null)
{
return defaultIcon;
}
}

return base.GetValueToConvert(value, desiredSize);
}

private static ImageSource GetDefaultIcon(DependencyObject targetVisual, Size desiredSize)
{
if (targetVisual != null)
{
var window = Window.GetWindow(targetVisual);

if (window != null)
{
try
{
return GetDefaultIcon(new WindowInteropHelper(Application.Current.MainWindow).Handle, desiredSize);
return GetDefaultIcon(new WindowInteropHelper(window).Handle, desiredSize);
}
catch (InvalidOperationException)
catch (InvalidOperationException exception)
{
return null;
Trace.WriteLine(exception);
}
}
}

if (Application.Current != null
&& Application.Current.CheckAccess()
&& Application.Current.MainWindow != null
&& Application.Current.MainWindow.CheckAccess())
{
try
{
return GetDefaultIcon(new WindowInteropHelper(Application.Current.MainWindow).Handle, desiredSize);
}
catch (InvalidOperationException exception)
{
Trace.WriteLine(exception);
}
}

var p = Process.GetCurrentProcess();
using (var p = Process.GetCurrentProcess())
{
if (p.MainWindowHandle != IntPtr.Zero)
{
return GetDefaultIcon(p.MainWindowHandle, desiredSize);
}
}

return ObjectToImageConverter.CreateImageSource(value, desiredSize);
}

/// <inheritdoc />
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return Binding.DoNothing;
return null;
}

#endregion

private static ImageSource GetDefaultIcon(IntPtr hwnd, Size desiredSize)
{
#pragma warning disable CS0219 // Variable is assigned but its value is never used
Expand Down Expand Up @@ -91,7 +140,7 @@ private static ImageSource GetDefaultIcon(IntPtr hwnd, Size desiredSize)
try
{
#pragma warning disable 618
var iconPtr = NativeMethods.SendMessage(hwnd, WM.GETICON, new IntPtr(ICON_SMALL), IntPtr.Zero);
var iconPtr = NativeMethods.SendMessage(hwnd, WM.GETICON, new IntPtr(ICON_SMALL2), IntPtr.Zero);

if (iconPtr == IntPtr.Zero)
{
Expand Down
Loading

0 comments on commit c286c7a

Please sign in to comment.