Browse Source

Implemented icons for the Win32 and GTK.

pull/575/head
Jeremy Koritzinsky 10 years ago
parent
commit
75b1d7fbf1
  1. 7
      src/Avalonia.Controls/Avalonia.Controls.csproj
  2. 28
      src/Avalonia.Controls/Icon.cs
  3. 12
      src/Avalonia.Controls/Platform/IIconImpl.cs
  4. 15
      src/Avalonia.Controls/Platform/IPlatformIconLoader.cs
  5. 5
      src/Avalonia.Controls/Platform/IWindowImpl.cs
  6. 1
      src/Gtk/Avalonia.Gtk/Avalonia.Gtk.csproj
  7. 16
      src/Gtk/Avalonia.Gtk/GtkPlatform.cs
  8. 20
      src/Gtk/Avalonia.Gtk/IconImpl.cs
  9. 15
      src/Gtk/Avalonia.Gtk/WindowImpl.cs
  10. 9
      src/Windows/Avalonia.Win32/Avalonia.Win32.csproj
  11. 19
      src/Windows/Avalonia.Win32/IconImpl.cs
  12. 142
      src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs
  13. 18
      src/Windows/Avalonia.Win32/Win32Platform.cs
  14. 9
      src/Windows/Avalonia.Win32/WindowImpl.cs

7
src/Avalonia.Controls/Avalonia.Controls.csproj

@ -56,6 +56,8 @@
<Compile Include="HotkeyManager.cs" />
<Compile Include="IApplicationLifecycle.cs" />
<Compile Include="IScrollable.cs" />
<Compile Include="Icon.cs" />
<Compile Include="INameScope.cs" />
<Compile Include="IPseudoClasses.cs" />
<Compile Include="DropDownItem.cs" />
<Compile Include="ISetInheritanceParent.cs" />
@ -64,6 +66,11 @@
<Compile Include="IVirtualizingPanel.cs" />
<Compile Include="LayoutTransformControl.cs" />
<Compile Include="Mixins\ContentControlMixin.cs" />
<Compile Include="NameScope.cs" />
<Compile Include="NameScopeEventArgs.cs" />
<Compile Include="NameScopeExtensions.cs" />
<Compile Include="Platform\IIconImpl.cs" />
<Compile Include="Platform\IPlatformIconLoader.cs" />
<Compile Include="Platform\ITopLevelRenderer.cs" />
<Compile Include="Platform\IWindowingPlatform.cs" />
<Compile Include="Platform\PlatformManager.cs" />

28
src/Avalonia.Controls/Icon.cs

@ -0,0 +1,28 @@
using Avalonia.Platform;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Avalonia.Controls
{
/// <summary>
/// Represents an icon for a window.
/// </summary>
public class Icon
{
public Icon(string fileName)
{
PlatformImpl = AvaloniaLocator.Current.GetService<IPlatformIconLoader>().LoadIcon(fileName);
}
public Icon(Stream stream)
{
PlatformImpl = AvaloniaLocator.Current.GetService<IPlatformIconLoader>().LoadIcon(stream);
}
public IIconImpl PlatformImpl { get; }
}
}

12
src/Avalonia.Controls/Platform/IIconImpl.cs

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Avalonia.Platform
{
public interface IIconImpl
{
}
}

15
src/Avalonia.Controls/Platform/IPlatformIconLoader.cs

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Avalonia.Platform
{
public interface IPlatformIconLoader
{
IIconImpl LoadIcon(string fileName);
IIconImpl LoadIcon(Stream stream);
}
}

5
src/Avalonia.Controls/Platform/IWindowImpl.cs

@ -39,5 +39,10 @@ namespace Avalonia.Platform
/// Enables of disables system window decorations (title bar, buttons, etc)
/// </summary>
void SetSystemDecorations(bool enabled);
/// <summary>
/// Sets the icon of this window.
/// </summary>
void SetIcon(IIconImpl icon);
}
}

1
src/Gtk/Avalonia.Gtk/Avalonia.Gtk.csproj

@ -46,6 +46,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ClipboardImpl.cs" />
<Compile Include="IconImpl.cs" />
<Compile Include="SystemDialogImpl.cs" />
<Compile Include="CursorFactory.cs" />
<Compile Include="GtkExtensions.cs" />

16
src/Gtk/Avalonia.Gtk/GtkPlatform.cs

@ -25,9 +25,10 @@ namespace Avalonia
namespace Avalonia.Gtk
{
using System.IO;
using Gtk = global::Gtk;
public class GtkPlatform : IPlatformThreadingInterface, IPlatformSettings, IWindowingPlatform
public class GtkPlatform : IPlatformThreadingInterface, IPlatformSettings, IWindowingPlatform, IPlatformIconLoader
{
private static readonly GtkPlatform s_instance = new GtkPlatform();
private static Thread _uiThread;
@ -53,7 +54,8 @@ namespace Avalonia.Gtk
.Bind<IMouseDevice>().ToConstant(GtkMouseDevice.Instance)
.Bind<IPlatformSettings>().ToConstant(s_instance)
.Bind<IPlatformThreadingInterface>().ToConstant(s_instance)
.Bind<ISystemDialogImpl>().ToSingleton<SystemDialogImpl>();
.Bind<ISystemDialogImpl>().ToSingleton<SystemDialogImpl>()
.Bind<IPlatformIconLoader>().ToConstant(s_instance);
SharedPlatform.Register();
_uiThread = Thread.CurrentThread;
}
@ -112,5 +114,15 @@ namespace Avalonia.Gtk
{
return new PopupImpl();
}
public IIconImpl LoadIcon(string fileName)
{
return new IconImpl(new Gdk.Pixbuf(fileName));
}
public IIconImpl LoadIcon(Stream stream)
{
return new IconImpl(new Gdk.Pixbuf(stream));
}
}
}

20
src/Gtk/Avalonia.Gtk/IconImpl.cs

@ -0,0 +1,20 @@
using Avalonia.Platform;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Gdk;
namespace Avalonia.Gtk
{
class IconImpl : IIconImpl
{
public IconImpl(Pixbuf pixbuf)
{
Pixbuf = pixbuf;
}
public Pixbuf Pixbuf { get; }
}
}

15
src/Gtk/Avalonia.Gtk/WindowImpl.cs

@ -53,11 +53,11 @@ namespace Avalonia.Gtk
Realize();
}
protected override void OnRealized ()
{
base.OnRealized ();
_imContext.ClientWindow = this.GdkWindow;
}
protected override void OnRealized ()
{
base.OnRealized ();
_imContext.ClientWindow = this.GdkWindow;
}
public Size ClientSize
{
@ -383,5 +383,10 @@ namespace Avalonia.Gtk
Input(e);
return true;
}
public void SetIcon(IIconImpl icon)
{
Icon = ((IconImpl)icon).Pixbuf;
}
}
}

9
src/Windows/Avalonia.Win32/Avalonia.Win32.csproj

@ -43,6 +43,14 @@
<Reference Include="System.Reactive.Core, Version=3.0.0.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\System.Reactive.Core.3.0.0\lib\net45\System.Reactive.Core.dll</HintPath>
<Private>True</Private>
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Reactive.Core">
<HintPath>..\..\..\packages\Rx-Core.2.2.5\lib\net45\System.Reactive.Core.dll</HintPath>
</Reference>
<Reference Include="System.Reactive.Interfaces">
<HintPath>..\..\..\packages\Rx-Interfaces.2.2.5\lib\net45\System.Reactive.Interfaces.dll</HintPath>
</Reference>
<Reference Include="System.Reactive.Interfaces, Version=3.0.0.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\System.Reactive.Interfaces.3.0.0\lib\net45\System.Reactive.Interfaces.dll</HintPath>
@ -56,6 +64,7 @@
<Link>Properties\SharedAssemblyInfo.cs</Link>
</Compile>
<Compile Include="ClipboardImpl.cs" />
<Compile Include="IconImpl.cs" />
<Compile Include="SystemDialogImpl.cs" />
<Compile Include="CursorFactory.cs" />
<Compile Include="Input\KeyInterop.cs" />

19
src/Windows/Avalonia.Win32/IconImpl.cs

@ -0,0 +1,19 @@
using Avalonia.Platform;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Avalonia.Win32
{
class IconImpl : IIconImpl
{
public IconImpl(System.Drawing.Icon icon)
{
Icon = icon;
}
public System.Drawing.Icon Icon { get; }
}
}

142
src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs

@ -541,72 +541,72 @@ namespace Avalonia.Win32.Interop
WM_DISPATCH_WORK_ITEM = WM_USER,
}
public enum BitmapCompressionMode : uint
{
BI_RGB = 0,
BI_RLE8 = 1,
BI_RLE4 = 2,
BI_BITFIELDS = 3,
BI_JPEG = 4,
BI_PNG = 5
}
public enum DIBColorTable
{
DIB_RGB_COLORS = 0, /* color table in RGBs */
DIB_PAL_COLORS /* color table in palette indices */
};
[StructLayout(LayoutKind.Sequential)]
public struct RGBQUAD
{
public byte rgbBlue;
public byte rgbGreen;
public byte rgbRed;
public byte rgbReserved;
}
[StructLayout(LayoutKind.Sequential)]
public struct BITMAPINFO
{
// C# cannot inlay structs in structs so must expand directly here
//
//[StructLayout(LayoutKind.Sequential)]
//public struct BITMAPINFOHEADER
//{
public uint biSize;
public int biWidth;
public int biHeight;
public ushort biPlanes;
public ushort biBitCount;
public BitmapCompressionMode biCompression;
public uint biSizeImage;
public int biXPelsPerMeter;
public int biYPelsPerMeter;
public uint biClrUsed;
public uint biClrImportant;
//}
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
public uint[] cols;
}
public const int SizeOf_BITMAPINFOHEADER = 40;
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("gdi32.dll")]
public static extern int SetDIBitsToDevice(IntPtr hdc, int XDest, int YDest,
uint dwWidth, uint dwHeight,
int XSrc, int YSrc,
uint uStartScan, uint cScanLines,
IntPtr lpvBits, [In] ref BITMAPINFO lpbmi, uint fuColorUse);
[DllImport("user32.dll")]
public static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("user32.dll", SetLastError = true)]
public enum BitmapCompressionMode : uint
{
BI_RGB = 0,
BI_RLE8 = 1,
BI_RLE4 = 2,
BI_BITFIELDS = 3,
BI_JPEG = 4,
BI_PNG = 5
}
public enum DIBColorTable
{
DIB_RGB_COLORS = 0, /* color table in RGBs */
DIB_PAL_COLORS /* color table in palette indices */
};
[StructLayout(LayoutKind.Sequential)]
public struct RGBQUAD
{
public byte rgbBlue;
public byte rgbGreen;
public byte rgbRed;
public byte rgbReserved;
}
[StructLayout(LayoutKind.Sequential)]
public struct BITMAPINFO
{
// C# cannot inlay structs in structs so must expand directly here
//
//[StructLayout(LayoutKind.Sequential)]
//public struct BITMAPINFOHEADER
//{
public uint biSize;
public int biWidth;
public int biHeight;
public ushort biPlanes;
public ushort biBitCount;
public BitmapCompressionMode biCompression;
public uint biSizeImage;
public int biXPelsPerMeter;
public int biYPelsPerMeter;
public uint biClrUsed;
public uint biClrImportant;
//}
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
public uint[] cols;
}
public const int SizeOf_BITMAPINFOHEADER = 40;
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("gdi32.dll")]
public static extern int SetDIBitsToDevice(IntPtr hdc, int XDest, int YDest,
uint dwWidth, uint dwHeight,
int XSrc, int YSrc,
uint uStartScan, uint cScanLines,
IntPtr lpvBits, [In] ref BITMAPINFO lpbmi, uint fuColorUse);
[DllImport("user32.dll")]
public static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool AdjustWindowRectEx(ref RECT lpRect, uint dwStyle, bool bMenu, uint dwExStyle);
[DllImport("user32.dll")]
@ -845,6 +845,10 @@ namespace Avalonia.Win32.Interop
[DllImport("user32.dll")]
public static extern IntPtr MonitorFromWindow(IntPtr hwnd, MONITOR dwFlags);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
public enum MONITOR
{
MONITOR_DEFAULTTONULL = 0x00000000,
@ -1008,6 +1012,12 @@ namespace Avalonia.Win32.Interop
E_OUTOFMEMORY = 0x8007000E
}
public enum Icons
{
ICON_SMALL = 0,
ICON_BIG = 1
}
public const uint SIGDN_FILESYSPATH = 0x80058000;
[Flags]

18
src/Windows/Avalonia.Win32/Win32Platform.cs

@ -16,6 +16,7 @@ using Avalonia.Shared.PlatformSupport;
using Avalonia.Win32.Input;
using Avalonia.Win32.Interop;
using Avalonia.Controls;
using System.IO;
namespace Avalonia
{
@ -31,7 +32,7 @@ namespace Avalonia
namespace Avalonia.Win32
{
public class Win32Platform : IPlatformThreadingInterface, IPlatformSettings, IWindowingPlatform
public class Win32Platform : IPlatformThreadingInterface, IPlatformSettings, IWindowingPlatform, IPlatformIconLoader
{
private static readonly Win32Platform s_instance = new Win32Platform();
private static Thread _uiThread;
@ -66,7 +67,8 @@ namespace Avalonia.Win32
.Bind<IPlatformSettings>().ToConstant(s_instance)
.Bind<IPlatformThreadingInterface>().ToConstant(s_instance)
.Bind<ISystemDialogImpl>().ToSingleton<SystemDialogImpl>()
.Bind<IWindowingPlatform>().ToConstant(s_instance);
.Bind<IWindowingPlatform>().ToConstant(s_instance)
.Bind<IPlatformIconLoader>().ToConstant(s_instance);
SharedPlatform.Register();
_uiThread = Thread.CurrentThread;
@ -186,5 +188,17 @@ namespace Avalonia.Win32
{
return new PopupImpl();
}
public IIconImpl LoadIcon(string fileName)
{
var icon = new System.Drawing.Icon(fileName);
return new IconImpl(icon);
}
public IIconImpl LoadIcon(Stream stream)
{
var icon = new System.Drawing.Icon(stream);
return new IconImpl(icon);
}
}
}

9
src/Windows/Avalonia.Win32/WindowImpl.cs

@ -604,7 +604,7 @@ namespace Avalonia.Win32
hInstance = Marshal.GetHINSTANCE(GetType().Module),
hCursor = DefaultCursor,
hbrBackground = IntPtr.Zero,
lpszClassName = _className,
lpszClassName = _className
};
ushort atom = UnmanagedMethods.RegisterClassEx(ref wndClassEx);
@ -681,5 +681,12 @@ namespace Avalonia.Win32
UnmanagedMethods.ShowWindow(_hwnd, command);
}
public void SetIcon(IIconImpl icon)
{
var impl = (IconImpl)icon;
var nativeIcon = impl.Icon;
UnmanagedMethods.PostMessage(_hwnd, (int)UnmanagedMethods.WindowsMessage.WM_SETICON,
new IntPtr((int)UnmanagedMethods.Icons.ICON_BIG), nativeIcon.Handle);
}
}
}

Loading…
Cancel
Save