Ongoing work to improve the Image API

An image can now have multiple versions with
different resolutions.
This commit is contained in:
Lluis Sanchez 2013-02-04 10:48:07 +01:00
Родитель cad936fc7b
Коммит d45dad34e1
32 изменённых файлов: 862 добавлений и 186 удалений

Просмотреть файл

@ -84,6 +84,7 @@ namespace Samples
AddSample (n, "Images and Patterns", typeof(DrawingPatternsAndImages));
AddSample (n, "Text", typeof(DrawingText));
AddSample (n, "Partial Images", typeof (PartialImages));
AddSample (n, "Custom Drawn Image", typeof (ImageScaling));
AddSample (null, "Expander", typeof (ExpanderSample));
AddSample (null, "Progress bars", typeof(ProgressBarSample));

Просмотреть файл

@ -171,6 +171,7 @@
<SubType>Component</SubType>
</Compile>
<Compile Include="Samples\ScreensSample.cs" />
<Compile Include="Samples\ImageScaling.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>

Просмотреть файл

@ -57,7 +57,9 @@ namespace Samples
};
PackStart (b2);
PackStart (new Button (Image.FromIcon (StockIcons.ZoomIn, IconSize.Medium)));
PackStart (new Button (StockIcons.ZoomIn));
PackStart (new Button (new CustomImage ()));
MenuButton mb = new MenuButton ("This is a Menu Button");
Menu men = new Menu ();

Просмотреть файл

@ -108,15 +108,15 @@ namespace Samples
var r = store.AddRow ();
store.SetValue (r, textField, "Information");
store.SetValue (r, descField, "Icons are duplicated on purpose");
store.SetValue (r, imgField, Image.FromIcon (StockIcons.Information, IconSize.Small));
store.SetValue (r, imgField, StockIcons.Information);
r = store.AddRow ();
store.SetValue (r, textField, "Error");
store.SetValue (r, descField, "Another item");
store.SetValue (r, imgField, Image.FromIcon (StockIcons.Error, IconSize.Small));
store.SetValue (r, imgField, StockIcons.Error);
r = store.AddRow ();
store.SetValue (r, textField, "Warning");
store.SetValue (r, descField, "A third item");
store.SetValue (r, imgField, Image.FromIcon (StockIcons.Warning, IconSize.Small));
store.SetValue (r, imgField, StockIcons.Warning);
// Four views to show three data fields
cbox.Views.Add (new ImageCellView (imgField));

Просмотреть файл

@ -0,0 +1,90 @@
//
// CustomDrawnImage.cs
//
// Author:
// Lluis Sanchez <lluis@xamarin.com>
//
// Copyright (c) 2013 Xamarin Inc.
//
// 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.
using System;
using Xwt;
using Xwt.Drawing;
namespace Samples
{
public class ImageScaling: Canvas
{
protected override void OnDraw (Context ctx, Rectangle dirtyRect)
{
Image image = new CustomImage ();
int x = 0;
for (int n=4; n < 50; n += 4) {
ctx.DrawImage (image.WithSize (n, n), x, 0);
x += n;
}
int maxSize = 22;
var warn = StockIcons.Add;
x = 0;
for (int n=8; n <= maxSize; n += 2) {
ctx.DrawImage (warn, x, 50, n, n);
x += n;
}
warn = StockIcons.Add.WithSize (maxSize).ToBitmap ();
x = 0;
for (int n=8; n <= maxSize; n += 2) {
ctx.DrawImage (warn, x, 100, n, n);
x += n;
}
ctx.DrawImage (image.WithSize (1000), new Rectangle (400, 0, 200, 1000), new Rectangle (0, 200, 200, 200));
ctx.DrawImage (image.WithSize (1000), new Rectangle (400, 0, 200, 50), new Rectangle (210, 200, 200, 200));
}
}
class CustomImage: DrawingImage
{
protected override void OnDraw (Context ctx, Rectangle bounds)
{
var lineWidth = bounds.Width / 32d;
var section = ((bounds.Width / 2) - lineWidth / 2) / 3;
ctx.SetLineWidth (lineWidth);
ctx.SetColor (Colors.Black);
ctx.Arc (bounds.Center.X, bounds.Center.Y, 0, 0, 360);
ctx.Stroke ();
ctx.SetColor (Colors.Red);
ctx.Arc (bounds.Center.X, bounds.Center.Y, section, 0, 360);
ctx.Stroke ();
ctx.SetColor (Colors.Green);
ctx.Arc (bounds.Center.X, bounds.Center.Y, section * 2, 0, 360);
ctx.Stroke ();
ctx.SetColor (Colors.Blue);
ctx.Arc (bounds.Center.X, bounds.Center.Y, section * 3, 0, 360);
ctx.Stroke ();
}
}
}

Просмотреть файл

@ -54,12 +54,12 @@ namespace Samples
var vbox = new VBox ();
var stockId = (string)stockIcons [i].GetValue (null);
var stockImage = (Image)stockIcons [i].GetValue (null);
var imageView = new ImageView ();
var label = new Label (stockId);
var label = new Label (stockIcons [i].Name);
try {
var icon = Image.FromIcon (stockId, IconSize.Medium);
var icon = stockImage.WithSize (IconSize.Medium);
if (icon != null)
imageView.Image = icon;
} catch { }

Просмотреть файл

@ -227,32 +227,38 @@ namespace Xwt.CairoBackend
CairoTextLayoutBackendHandler.Draw (ctx, lb, x, y);
}
public override void DrawImage (object backend, object img, double x, double y, double alpha)
{
CairoContextBackend ctx = (CairoContextBackend)backend;
SetSourceImage (ctx.Context, img, x, y);
alpha = alpha * ctx.GlobalAlpha;
if (alpha == 1)
ctx.Context.Paint ();
else
ctx.Context.PaintWithAlpha (alpha);
}
protected virtual void SetSourceImage (Cairo.Context ctx, object img, double x, double y)
{
}
public override bool CanDrawImage (object backend, object img)
{
return true;
}
public override void DrawImage (object backend, object img, double x, double y, double width, double height, double alpha)
{
CairoContextBackend ctx = (CairoContextBackend)backend;
ctx.Context.Save ();
alpha = alpha * ctx.GlobalAlpha;
img = ResolveImage (img, width, height);
var s = GetImageSize (img);
if (s.Width == width && s.Height == height) {
SetSourceImage (ctx.Context, img, x, y);
if (alpha == 1)
ctx.Context.Paint ();
else
ctx.Context.PaintWithAlpha (alpha);
return;
}
ctx.Context.Save ();
double sx = ((double) width) / s.Width;
double sy = ((double) height) / s.Height;
ctx.Context.Translate (x, y);
ctx.Context.Scale (sx, sy);
SetSourceImage (ctx.Context, img, 0, 0);
alpha = alpha * ctx.GlobalAlpha;
if (alpha == 1)
ctx.Context.Paint ();
else
@ -260,7 +266,7 @@ namespace Xwt.CairoBackend
ctx.Context.Restore ();
}
public override void DrawImage (object backend, object img, Rectangle srcRect, Rectangle destRect, double alpha)
public override void DrawImage (object backend, object img, Rectangle srcRect, Rectangle destRect, double width, double height, double alpha)
{
CairoContextBackend ctx = (CairoContextBackend)backend;
ctx.Context.Save ();
@ -285,6 +291,11 @@ namespace Xwt.CairoBackend
return new Size (0,0);
}
protected virtual object ResolveImage (object img, double width, double height)
{
return img;
}
public override void Rotate (object backend, double angle)
{
CairoContextBackend gc = (CairoContextBackend)backend;

Просмотреть файл

@ -26,6 +26,7 @@
using System;
using Xwt.Backends;
using Xwt.Drawing;
namespace Xwt.GtkBackend
@ -54,13 +55,13 @@ namespace Xwt.GtkBackend
get { return (IButtonEventSink)base.EventSink; }
}
public void SetContent (string label, object imageBackend, ContentPosition position)
public void SetContent (string label, Image image, ContentPosition position)
{
if (label != null && label.Length == 0)
label = null;
Button b = (Button) Frontend;
if (label != null && imageBackend == null && b.Type == ButtonType.Normal) {
if (label != null && image == null && b.Type == ButtonType.Normal) {
Widget.Label = label;
return;
}
@ -75,8 +76,8 @@ namespace Xwt.GtkBackend
Gtk.Widget contentWidget = null;
Gtk.Widget imageWidget = null;
if (imageBackend != null)
imageWidget = new Gtk.Image ((Gdk.Pixbuf)imageBackend);
if (image != null)
imageWidget = new Gtk.Image (image.ToPixbuf (Gtk.IconSize.Button));
if (label != null && imageWidget == null) {
contentWidget = new Gtk.Label (label);
@ -137,7 +138,7 @@ namespace Xwt.GtkBackend
public void SetButtonType (ButtonType type)
{
Button b = (Button) Frontend;
SetContent (b.Label, Toolkit.GetBackend (b.Image), b.ImagePosition);
SetContent (b.Label, b.Image, b.ImagePosition);
}
public override void EnableEvent (object eventId)

Просмотреть файл

@ -34,10 +34,17 @@ namespace Xwt.GtkBackend
{
public class ContextBackendHandler: CairoContextBackendHandler
{
protected override object ResolveImage (object img, double width, double height)
{
if (img is String)
return ImageHandler.CreateBitmap ((string)img, width, height);
else
return img;
}
protected override void SetSourceImage (Cairo.Context ctx, object img, double x, double y)
{
Gdk.Pixbuf pb = (Gdk.Pixbuf)img;
Gdk.CairoHelper.SetSourcePixbuf (ctx, pb, x, y);
Gdk.CairoHelper.SetSourcePixbuf (ctx, (Gdk.Pixbuf)img, x, y);
}
protected override Size GetImageSize (object img)

Просмотреть файл

@ -123,8 +123,8 @@ namespace Xwt.GtkBackend
newButton.Label = button.Label;
newButton.UseUnderline = true;
newButton.UseStock = button.IsStockButton;
if (!String.IsNullOrEmpty (button.Icon))
newButton.Image = new Gtk.Image (Util.ToGtkStock (button.Icon), Gtk.IconSize.Button);
if (button.Icon != null)
newButton.Image = new Gtk.Image (button.Icon.ToPixbuf (Gtk.IconSize.Button));
newButton.Clicked += ButtonClicked;
ActionArea.Add (newButton);
}

Просмотреть файл

@ -26,6 +26,7 @@
using System;
using Xwt.Backends;
using Xwt.Drawing;
namespace Xwt.GtkBackend
{
@ -37,25 +38,12 @@ namespace Xwt.GtkBackend
return loader.Pixbuf;
}
public override object LoadFromIcon (string id, IconSize size)
public override Image GetStockIcon (string id)
{
string stockId = Util.ToGtkStock (id);
var gsize = Util.ToGtkSize (size);
Gtk.IconSet iconset = Gtk.IconFactory.LookupDefault (stockId);
if (iconset != null)
return iconset.RenderIcon (Gtk.Widget.DefaultStyle, Gtk.TextDirection.Ltr, Gtk.StateType.Normal, gsize, null, null);
if (Gtk.IconTheme.Default.HasIcon (stockId)) {
int w, h;
Gtk.Icon.SizeLookup (gsize, out w, out h);
Gdk.Pixbuf result = Gtk.IconTheme.Default.LoadIcon (stockId, h, (Gtk.IconLookupFlags)0);
return result;
}
return null;
return ApplicationContext.Toolkit.WrapImage (Util.ToGtkStock (id));
}
public override void SetPixel (object handle, int x, int y, Xwt.Drawing.Color color)
public override void SetBitmapPixel (object handle, int x, int y, Xwt.Drawing.Color color)
{
var pix = (Gdk.Pixbuf)handle;
@ -69,7 +57,7 @@ namespace Xwt.GtkBackend
}
}
public override Xwt.Drawing.Color GetPixel (object handle, int x, int y)
public override Xwt.Drawing.Color GetBitmapPixel (object handle, int x, int y)
{
var pix = (Gdk.Pixbuf)handle;
@ -85,32 +73,32 @@ namespace Xwt.GtkBackend
((Gdk.Pixbuf)backend).Dispose ();
}
public override Size GetSize (object handle)
public override Size GetBitmapSize (object handle)
{
var pix = (Gdk.Pixbuf)handle;
return new Size (pix.Width, pix.Height);
}
public override object Resize (object handle, double width, double height)
public override object ResizeBitmap (object handle, double width, double height)
{
var pix = (Gdk.Pixbuf)handle;
return pix.ScaleSimple ((int)width, (int)height, Gdk.InterpType.Bilinear);
}
public override object Copy (object handle)
public override object CopyBitmap (object handle)
{
var pix = (Gdk.Pixbuf)handle;
return pix.Copy ();
}
public override void CopyArea (object srcHandle, int srcX, int srcY, int width, int height, object destHandle, int destX, int destY)
public override void CopyBitmapArea (object srcHandle, int srcX, int srcY, int width, int height, object destHandle, int destX, int destY)
{
var pixSrc = (Gdk.Pixbuf)srcHandle;
var pixDst = (Gdk.Pixbuf)destHandle;
pixSrc.CopyArea (srcX, srcY, width, height, pixDst, destX, destY);
}
public override object Crop (object handle, int srcX, int srcY, int width, int height)
public override object CropBitmap (object handle, int srcX, int srcY, int width, int height)
{
var pix = (Gdk.Pixbuf)handle;
Gdk.Pixbuf res = new Gdk.Pixbuf (pix.Colorspace, pix.HasAlpha, pix.BitsPerSample, width, height);
@ -119,7 +107,7 @@ namespace Xwt.GtkBackend
return res;
}
public override object ChangeOpacity (object backend, double opacity)
public override object ChangeBitmapOpacity (object backend, double opacity)
{
Gdk.Pixbuf image = (Gdk.Pixbuf) backend;
Gdk.Pixbuf result = image.Copy ();
@ -128,6 +116,47 @@ namespace Xwt.GtkBackend
image.Composite (result, 0, 0, image.Width, image.Height, 0, 0, 1, 1, Gdk.InterpType.Bilinear, (int)(255 * opacity));
return result;
}
public override bool IsBitmap (object handle)
{
return handle is Gdk.Pixbuf;
}
public override object ConvertToBitmap (object handle, double width, double height, bool preserveAspectRatio)
{
Gdk.Pixbuf result = CreateBitmap ((string)handle, width, height);
if (result != null) {
int bw = (int) width;
int bh = (int) height;
if (preserveAspectRatio) {
var r = CalcBoxSizeRatio (width, height, result.Width, result.Height);
bw = (int)((double)result.Width * r);
bh = (int)((double)result.Height * r);
}
if (result.Width != bw || result.Height != bh)
return ResizeBitmap (result, bw, bh);
}
return result;
}
internal static Gdk.Pixbuf CreateBitmap (string stockId, double width, double height)
{
Gdk.Pixbuf result = null;
Gtk.IconSet iconset = Gtk.IconFactory.LookupDefault (stockId);
if (iconset != null) {
// Find the size that better fits the requested size
Gtk.IconSize gsize = Util.GetBestSizeFit (width);
result = iconset.RenderIcon (Gtk.Widget.DefaultStyle, Gtk.TextDirection.Ltr, Gtk.StateType.Normal, gsize, null, null);
}
if (result == null && Gtk.IconTheme.Default.HasIcon (stockId))
result = Gtk.IconTheme.Default.LoadIcon (stockId, (int)width, (Gtk.IconLookupFlags)0);
return result;
}
}
}

Просмотреть файл

@ -75,7 +75,7 @@ namespace Xwt.GtkBackend
else if (value is string)
store.SetValue (it, column, (string)value);
else if (value is Image)
store.SetValue (it, column, (Gdk.Pixbuf)Toolkit.GetBackend (value));
store.SetValue (it, column, ((Image)value).ToPixbuf (Gtk.IconSize.Menu));
else
store.SetValue (it, column, value ?? DBNull.Value);
}

Просмотреть файл

@ -38,6 +38,18 @@ namespace Xwt.GtkBackend
static uint targetIdCounter = 0;
static Dictionary<TransferDataType, Gtk.TargetEntry[]> dragTargets = new Dictionary<TransferDataType, Gtk.TargetEntry[]> ();
static Dictionary<string, TransferDataType> atomToType = new Dictionary<string, TransferDataType> ();
static Size[] iconSizes = new Size[7];
static Util ()
{
for (int i = 0; i < iconSizes.Length; i++) {
int w, h;
if (!Gtk.Icon.SizeLookup ((Gtk.IconSize)i, out w, out h))
w = h = -1;
iconSizes[i].Width = w;
iconSizes[i].Height = h;
}
}
public static void SetDragData (TransferDataSource data, Gtk.DragDataGetArgs args)
{
@ -155,17 +167,17 @@ namespace Xwt.GtkBackend
{
if (icons == null) {
icons = new Dictionary<string, string> ();
icons [StockIcons.ZoomIn] = Gtk.Stock.ZoomIn;
icons [StockIcons.ZoomOut] = Gtk.Stock.ZoomOut;
icons [StockIcons.Zoom100] = Gtk.Stock.Zoom100;
icons [StockIcons.ZoomFit] = Gtk.Stock.ZoomFit;
icons [StockIcons.OrientationPortrait] = Gtk.Stock.OrientationPortrait;
icons [StockIcons.OrientationLandscape] = Gtk.Stock.OrientationLandscape;
icons [StockIcons.Add] = Gtk.Stock.Add;
icons [StockIcons.Remove] = Gtk.Stock.Remove;
icons [StockIcons.Warning] = Gtk.Stock.DialogWarning;
icons [StockIcons.Error] = Gtk.Stock.DialogError;
icons [StockIcons.Information] = Gtk.Stock.DialogInfo;
icons [StockIconId.ZoomIn] = Gtk.Stock.ZoomIn;
icons [StockIconId.ZoomOut] = Gtk.Stock.ZoomOut;
icons [StockIconId.Zoom100] = Gtk.Stock.Zoom100;
icons [StockIconId.ZoomFit] = Gtk.Stock.ZoomFit;
icons [StockIconId.OrientationPortrait] = Gtk.Stock.OrientationPortrait;
icons [StockIconId.OrientationLandscape] = Gtk.Stock.OrientationLandscape;
icons [StockIconId.Add] = Gtk.Stock.Add;
icons [StockIconId.Remove] = Gtk.Stock.Remove;
icons [StockIconId.Warning] = Gtk.Stock.DialogWarning;
icons [StockIconId.Error] = Gtk.Stock.DialogError;
icons [StockIconId.Information] = Gtk.Stock.DialogInfo;
}
string res;
icons.TryGetValue (id, out res);
@ -250,6 +262,33 @@ namespace Xwt.GtkBackend
}
throw new InvalidOperationException("Invalid mouse scroll direction value: " + d);
}
public static Gtk.IconSize GetBestSizeFit (double size)
{
// Find the size that better fits the requested size
for (int n=0; n<iconSizes.Length; n++) {
if (size <= iconSizes [n].Width)
return (Gtk.IconSize)n;
}
return Gtk.IconSize.Dialog;
}
public static double GetBestSizeFitSize (double size)
{
var s = GetBestSizeFit (size);
return iconSizes [(int)s].Width;
}
public static Gdk.Pixbuf ToPixbuf (this Image image, Gtk.IconSize defaultIconSize)
{
if (!image.HasFixedSize) {
var s = iconSizes [(int)defaultIconSize];
image = image.WithSize (s.Width, s.Height);
}
return (Gdk.Pixbuf)Toolkit.GetBackend (image.ToBitmap ());
}
}
}

Просмотреть файл

@ -88,7 +88,7 @@ namespace Xwt.Mac
throw new InvalidOperationException ("Not a bitmnap image");
}
public override Size GetSize (object handle)
public override Size GetBitmapSize (object handle)
{
NSImage img = (NSImage)handle;
return new Size ((int)img.Size.Width, (int)img.Size.Height);

Просмотреть файл

@ -1,4 +1,4 @@
//
//
// ImageHandler.cs
//
// Author:
@ -123,7 +123,7 @@ namespace Xwt.WPFBackend
return pixels * 96 / img.DpiY;
}
public override Size GetSize (object handle)
public override Size GetBitmapSize (object handle)
{
BitmapSource source = handle as BitmapSource;
if (source != null)

Просмотреть файл

@ -103,7 +103,7 @@ Global
{3C7623A9-9E16-41F6-BBB2-0B550F28E749}.Win-Debug|Any CPU.Build.0 = Debug|Any CPU
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = MacTest\MacTest.csproj
StartupItem = GtkTest\GtkTest.csproj
Policies = $0
$0.DotNetNamingPolicy = $1
$1.DirectoryNamespaceAssociation = None

Просмотреть файл

@ -62,11 +62,11 @@ namespace Xwt.Backends
public abstract void DrawTextLayout (object backend, TextLayout layout, double x, double y);
public abstract void DrawImage (object backend, object img, double x, double y, double alpha);
public abstract bool CanDrawImage (object backend, object img);
public abstract void DrawImage (object backend, object img, double x, double y, double width, double height, double alpha);
public abstract void DrawImage (object backend, object img, Rectangle srcRect, Rectangle destRect, double alpha);
public abstract void DrawImage (object backend, object img, Rectangle srcRect, Rectangle destRect, double width, double height, double alpha);
public abstract void Rotate (object backend, double angle);

Просмотреть файл

@ -25,6 +25,7 @@
// THE SOFTWARE.
using System;
using Xwt.Drawing;
namespace Xwt.Backends
{
@ -32,7 +33,7 @@ namespace Xwt.Backends
{
void SetButtonStyle (ButtonStyle style);
void SetButtonType (ButtonType type);
void SetContent (string label, object imageBackend, ContentPosition position);
void SetContent (string label, Image image, ContentPosition position);
}
public interface IButtonEventSink: IWidgetEventSink

Просмотреть файл

@ -57,25 +57,34 @@ namespace Xwt.Backends
return LoadFromStream (s);
}
public double CalcBoxSizeRatio (double boxWidth, double boxHeight, double imageWidth, double imageHeight)
{
return Math.Min (boxWidth / imageWidth, boxHeight / imageHeight);
}
public abstract object LoadFromStream (Stream stream);
public abstract object LoadFromIcon (string id, IconSize size);
public abstract Image GetStockIcon (string id);
public abstract Size GetSize (object handle);
public abstract bool IsBitmap (object handle);
public abstract object Resize (object handle, double width, double height);
public abstract object ConvertToBitmap (object handle, double width, double height, bool preserveAspectRatio);
public abstract object Copy (object handle);
public abstract Size GetBitmapSize (object handle);
public abstract void CopyArea (object srcHandle, int srcX, int srcY, int width, int height, object destHandle, int destX, int destY);
public abstract object ResizeBitmap (object handle, double width, double height);
public abstract object Crop (object handle, int srcX, int srcY, int width, int height);
public abstract object CopyBitmap (object handle);
public abstract object ChangeOpacity (object backend, double opacity);
public abstract void CopyBitmapArea (object srcHandle, int srcX, int srcY, int width, int height, object destHandle, int destX, int destY);
public abstract void SetPixel (object handle, int x, int y, Color color);
public abstract object CropBitmap (object handle, int srcX, int srcY, int width, int height);
public abstract Color GetPixel (object handle, int x, int y);
public abstract object ChangeBitmapOpacity (object backend, double opacity);
public abstract void SetBitmapPixel (object handle, int x, int y, Color color);
public abstract Color GetBitmapPixel (object handle, int x, int y);
}
}

Просмотреть файл

@ -0,0 +1,46 @@
//
// StockIconId.cs
//
// Author:
// Lluis Sanchez <lluis@xamarin.com>
//
// Copyright (c) 2013 Xamarin Inc.
//
// 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.
using System;
namespace Xwt.Backends
{
public class StockIconId
{
public const string Error = "Error";
public const string Warning = "Warning";
public const string Information = "Information";
public const string Question = "Question";
public const string OrientationPortrait = "Portrait";
public const string OrientationLandscape = "Landscape";
public const string ZoomIn = "ZoomIn";
public const string ZoomOut = "ZoomOut";
public const string ZoomFit = "ZoomFit";
public const string Zoom100 = "Zoom100";
public const string Add = "Add";
public const string Remove = "Remove";
}
}

Просмотреть файл

@ -0,0 +1,87 @@
//
// Bitmap.cs
//
// Author:
// Lluis Sanchez <lluis@xamarin.com>
//
// Copyright (c) 2013 Xamarin Inc.
//
// 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.
using System;
namespace Xwt.Drawing
{
public class BitmapImage: Image
{
public BitmapImage (BitmapImage image): base (image.ToolkitEngine.ImageBackendHandler.CopyBitmap (image.Backend), image.ToolkitEngine)
{
}
internal BitmapImage (object backend): base (backend)
{
}
internal BitmapImage (Image origImage): base (origImage)
{
// shares the backend of the image
}
void MakeWrittable ()
{
// If the bitmap only has one reference, that reference is the one held by this instance, so
// no other image is referencing this one. In that case, the bitmap can be safely modified.
// On the other hand, if the bitmap is being referenced by another image, an local copy
// has to be made
if (NativeRef.ReferenceCount > 1) {
Backend = ToolkitEngine.ImageBackendHandler.CopyBitmap (Backend);
NativeRef.ReleaseReference (true);
NativeRef = new NativeImageRef (Backend, ToolkitEngine);
}
}
public void SetPixel (int x, int y, Color color)
{
MakeWrittable ();
ToolkitEngine.ImageBackendHandler.SetBitmapPixel (Backend, x, y, color);
}
public Color GetPixel (int x, int y)
{
return ToolkitEngine.ImageBackendHandler.GetBitmapPixel (Backend, x, y);
}
public void CopyArea (int srcX, int srcY, int width, int height, BitmapImage dest, int destX, int destY)
{
dest.MakeWrittable ();
ToolkitEngine.ImageBackendHandler.CopyBitmapArea (Backend, srcX, srcY, width, height, dest.Backend, destX, destY);
}
public BitmapImage Crop (int srcX, int srcY, int width, int height)
{
return new BitmapImage (ToolkitEngine.ImageBackendHandler.CropBitmap (Backend, srcX, srcY, width, height));
}
public BitmapImage ChangeOpacity (double opacity)
{
return new BitmapImage (ToolkitEngine.ImageBackendHandler.ChangeBitmapOpacity (Backend, opacity));
}
}
}

Просмотреть файл

@ -131,49 +131,96 @@ namespace Xwt.Drawing
handler.DrawTextLayout (Backend, layout, x, y);
}
public void DrawImage (Image img, Point location)
public void DrawImage (Image img, Point location, double alpha = 1)
{
handler.DrawImage (Backend, GetBackend (img), location.X, location.Y, 1);
DrawImage (img, location.X, location.Y, alpha);
}
public void DrawImage (Image img, double x, double y)
public void DrawImage (Image img, double x, double y, double alpha = 1)
{
handler.DrawImage (Backend, GetBackend (img), x, y, 1);
if (!img.HasFixedSize)
throw new InvalidOperationException ("Image doesn't have a fixed size");
if (img.CanDrawInContext (img.Size.Width, img.Size.Height)) {
DrawInContext (img, x, y, img.Size.Width, img.Size.Height, alpha);
return;
}
var bk = GetImageBackend (img);
handler.DrawImage (Backend, bk, x, y, img.Size.Width, img.Size.Height, alpha);
}
public void DrawImage (Image img, Point location, double alpha)
void DrawInContext (Image img, double x, double y, double width, double height, double alpha)
{
handler.DrawImage (Backend, GetBackend (img), location.X, location.Y, alpha);
try {
Save ();
NewPath ();
Rectangle (x, y, width, height);
Clip ();
GlobalAlpha = alpha;
img.DrawInContext (this, x, y, width, height);
} finally {
Restore ();
}
}
public void DrawImage (Image img, double x, double y, double alpha)
object GetImageBackend (Image img)
{
handler.DrawImage (Backend, GetBackend (img), x, y, alpha);
var bk = GetBackend (img);
if (bk == null || !handler.CanDrawImage (Backend, bk))
return GetBackend (img.ToBitmap ());
else
return bk;
}
public void DrawImage (Image img, Rectangle rect)
public void DrawImage (Image img, Rectangle rect, double alpha = 1)
{
handler.DrawImage (Backend, GetBackend (img), rect.X, rect.Y, rect.Width, rect.Height, 1);
DrawImage (img, rect.X, rect.Y, rect.Width, rect.Height, alpha);
}
public void DrawImage (Image img, double x, double y, double width, double height)
public void DrawImage (Image img, double x, double y, double width, double height, double alpha = 1)
{
handler.DrawImage (Backend, GetBackend (img), x, y, width, height, 1);
if (img.CanDrawInContext (width, height)) {
DrawInContext (img, x, y, width, height, alpha);
return;
}
public void DrawImage (Image img, Rectangle rect, double alpha)
{
handler.DrawImage (Backend, GetBackend (img), rect.X, rect.Y, rect.Width, rect.Height, alpha);
var bk = GetImageBackend (img.WithSize (width, height));
handler.DrawImage (Backend, bk, x, y, width, height, alpha);
}
public void DrawImage (Image img, Rectangle srcRect, Rectangle destRect)
{
handler.DrawImage (Backend, GetBackend (img), srcRect, destRect, 1);
DrawImage (img, srcRect, destRect, 1);
}
public void DrawImage (Image img, Rectangle srcRect, Rectangle destRect, double alpha)
{
handler.DrawImage (Backend, GetBackend (img), srcRect, destRect, alpha);
if (!img.HasFixedSize)
throw new InvalidOperationException ("Image doesn't have a fixed size");
if (img.CanDrawInContext (img.Size.Width, img.Size.Height)) {
try {
Save ();
GlobalAlpha = alpha;
NewPath ();
Rectangle (destRect);
Clip ();
var scaleX = destRect.Width / srcRect.Width;
var scaleY = destRect.Height / srcRect.Height;
Translate (destRect.X - srcRect.X * scaleX, destRect.Y - srcRect.Y * scaleY);
Scale (scaleX, scaleY);
img.DrawInContext (this, 0, 0, img.Size.Width, img.Size.Height);
} finally {
Restore ();
}
return;
}
var bk = GetImageBackend (img);
handler.DrawImage (Backend, bk, srcRect, destRect, img.Size.Width, img.Size.Height, alpha);
}
/// <summary>

Просмотреть файл

@ -0,0 +1,58 @@
//
// DrawingImage.cs
//
// Author:
// Lluis Sanchez <lluis@xamarin.com>
//
// Copyright (c) 2013 Xamarin Inc.
//
// 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.
using System;
namespace Xwt.Drawing
{
public class DrawingImage: Image
{
public DrawingImage ()
{
}
protected override BitmapImage GenerateBitmap (Size size)
{
ImageBuilder ib = new ImageBuilder ((int)size.Width, (int)size.Height);
OnDraw (ib.Context, new Rectangle (0, 0, size.Width, size.Height));
return ib.ToImage ().ToBitmap ();
}
internal override bool CanDrawInContext (double width, double height)
{
return true;
}
internal override void DrawInContext (Context ctx, double x, double y, double width, double height)
{
OnDraw (ctx, new Rectangle (x, y, width, height));
}
protected virtual void OnDraw (Context ctx, Rectangle bounds)
{
}
}
}

Просмотреть файл

@ -32,18 +32,66 @@ using System.IO;
namespace Xwt.Drawing
{
public sealed class Image: XwtObject, IDisposable
public class Image: XwtObject, IDisposable
{
Size requestedSize;
internal NativeImageRef NativeRef;
bool preserveAspectRatio;
Image sourceImage;
static readonly object NoBackend = new object ();
internal Image (object backend): base (backend)
{
Init ();
}
internal Image (object backend, Toolkit toolkit): base (backend, toolkit)
{
Init ();
}
public Image (Image image): base (image.ToolkitEngine.ImageBackendHandler.Copy (image.Backend), image.ToolkitEngine)
public Image (Image image): base (image.Backend, image.ToolkitEngine)
{
if (image.NativeRef != null) {
NativeRef = image.NativeRef;
Init ();
}
else
sourceImage = image;
}
protected Image (Toolkit toolkit): base (NoBackend, toolkit)
{
}
protected Image (): base (NoBackend)
{
}
void Init ()
{
if (NativeRef == null) {
NativeRef = new NativeImageRef (Backend, ToolkitEngine);
} else
NativeRef.AddReference ();
}
~Image ()
{
Dispose (false);
}
public void Dispose ()
{
Dispose (true);
GC.SuppressFinalize (this);
}
protected virtual void Dispose (bool disposing)
{
if (NativeRef != null)
NativeRef.ReleaseReference (disposing);
}
public static Image FromResource (Type type, string resource)
@ -76,74 +124,180 @@ namespace Xwt.Drawing
return new Image (toolkit.ImageBackendHandler.LoadFromStream (stream), toolkit);
}
public static Image FromIcon (string id, IconSize size)
public static Image FromMultipleSizes (params Image[] images)
{
var toolkit = Toolkit.CurrentEngine;
return new Image (toolkit.ImageBackendHandler.LoadFromIcon (id, size), toolkit);
return new ImageSet (images);
}
public BitmapImage ToBitmap ()
{
if (sourceImage != null)
return sourceImage.ToBitmap (Size);
else
return ToBitmap (Size);
}
public Size Size {
get { return ToolkitEngine.ImageBackendHandler.GetSize (Backend); }
get {
if (!requestedSize.IsZero)
return requestedSize;
if (Backend != NoBackend && ToolkitEngine.ImageBackendHandler.IsBitmap (Backend))
return ToolkitEngine.ImageBackendHandler.GetBitmapSize (Backend);
else
return GetSize ();
}
}
public void SetPixel (int x, int y, Color color)
public Image WithSize (double width, double height)
{
ToolkitEngine.ImageBackendHandler.SetPixel (Backend, x, y, color);
return new Image (this) {
requestedSize = new Size (width, height)
};
}
public Color GetPixel (int x, int y)
public Image WithSize (Size size)
{
return ToolkitEngine.ImageBackendHandler.GetPixel (Backend, x, y);
return new Image (this) {
requestedSize = size
};
}
public Image WithSize (double squaredSize)
{
return new Image (this) {
requestedSize = new Size (squaredSize, squaredSize)
};
}
public Image WithSize (IconSize size)
{
Size s;
switch (size) {
case IconSize.Small: s = new Size (16, 16); break;
case IconSize.Medium: s = new Size (24, 24); break;
case IconSize.Large: s = new Size (32, 32); break;
default: throw new ArgumentOutOfRangeException ("size");
}
return new Image (this) {
requestedSize = s
};
}
public Image WithBoxSize (double maxWidth, double maxHeight)
{
return new Image (this) {
requestedSize = new Size (maxWidth, maxHeight),
preserveAspectRatio = true
};
}
public Image WithBoxSize (double maxSize)
{
return new Image (this) {
requestedSize = new Size (maxSize, maxSize),
preserveAspectRatio = true
};
}
public bool HasFixedSize {
get { return !Size.IsZero; }
}
public Image Scale (double scale)
{
if (!HasFixedSize)
throw new InvalidOperationException ("Image must have a size in order to be scaled");
double w = Size.Width * scale;
double h = Size.Height * scale;
return new Image (ToolkitEngine.ImageBackendHandler.Resize (Backend, w, h));
return new Image (this) {
requestedSize = new Size (w, h)
};
}
public Image Scale (double scaleX, double scaleY)
{
if (!HasFixedSize)
throw new InvalidOperationException ("Image must have a size in order to be scaled");
double w = Size.Width * scaleX;
double h = Size.Height * scaleY;
return new Image (ToolkitEngine.ImageBackendHandler.Resize (Backend, w, h));
return new Image (this) {
requestedSize = new Size (w, h)
};
}
public Image Resize (double width, double height)
BitmapImage ToBitmap (Size size)
{
return new Image (ToolkitEngine.ImageBackendHandler.Resize (Backend, width, height));
// TODO: Caching
return GenerateBitmap (size);
}
public Image ResizeToFitBox (double width, double height)
protected virtual BitmapImage GenerateBitmap (Size size)
{
double r = Math.Min (width / Size.Width, height / Size.Height);
return new Image (ToolkitEngine.ImageBackendHandler.Resize (Backend, Size.Width * r, Size.Height * r));
if (Backend != NoBackend) {
if (ToolkitEngine.ImageBackendHandler.IsBitmap (Backend)) {
if (size == ToolkitEngine.ImageBackendHandler.GetBitmapSize (Backend)) {
// The backend can be shared
return new BitmapImage (this);
}
else {
// Create a new backend with the new size
var bmp = ToolkitEngine.ImageBackendHandler.ResizeBitmap (Backend, size.Width, size.Height);
return new BitmapImage (bmp);
}
}
else
return new BitmapImage (ToolkitEngine.ImageBackendHandler.ConvertToBitmap (Backend, size.Width, size.Height, preserveAspectRatio));
}
public Image ToGrayscale ()
throw new NotSupportedException ();
}
protected virtual Size GetSize ()
{
throw new NotImplementedException ();
return Size.Zero;
}
public Image ChangeOpacity (double opacity)
internal virtual bool CanDrawInContext (double width, double height)
{
return new Image (ToolkitEngine.ImageBackendHandler.ChangeOpacity (Backend, opacity));
return sourceImage != null ? sourceImage.CanDrawInContext (width, height) : false;
}
public void CopyArea (int srcX, int srcY, int width, int height, Image dest, int destX, int destY)
internal virtual void DrawInContext (Context ctx, double x, double y, double width, double height)
{
ToolkitEngine.ImageBackendHandler.CopyArea (Backend, srcX, srcY, width, height, dest.Backend, destX, destY);
if (sourceImage != null)
sourceImage.DrawInContext (ctx, x, y, width, height);
}
}
public Image Crop (int srcX, int srcY, int width, int height)
class NativeImageRef
{
return new Image (ToolkitEngine.ImageBackendHandler.Crop (Backend, srcX, srcY, width, height));
object backend;
int referenceCount = 1;
Toolkit toolkit;
public int ReferenceCount {
get { return referenceCount; }
}
public void Dispose ()
public NativeImageRef (object backend, Toolkit toolkit)
{
ToolkitEngine.ImageBackendHandler.Dispose (Backend);
this.backend = backend;
this.toolkit = toolkit;
}
public void AddReference ()
{
System.Threading.Interlocked.Increment (ref referenceCount);
}
public void ReleaseReference (bool disposing)
{
if (System.Threading.Interlocked.Decrement (ref referenceCount) == 0 && disposing)
toolkit.ImageBackendHandler.Dispose (backend);
}
}
}

Просмотреть файл

@ -0,0 +1,72 @@
//
// ImageSet.cs
//
// Author:
// Lluis Sanchez <lluis@xamarin.com>
//
// Copyright (c) 2013 Xamarin Inc.
//
// 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.
using System;
using System.Linq;
namespace Xwt.Drawing
{
class ImageSet: Image
{
Image[] images;
public ImageSet (Image[] images)
{
if (images.Count (i => !i.HasFixedSize) > 1)
throw new InvalidOperationException ("There is more than one image with unbound size");
this.images = images.OrderBy (i => i.HasFixedSize ? i.Size.Width + i.Size.Height : int.MaxValue).ToArray ();
}
Image SelectImage (double width, double height)
{
foreach (var img in images) {
if (!img.HasFixedSize || (width + height <= img.Size.Width + img.Size.Height))
return img;
}
return images [images.Length - 1];
}
internal override bool CanDrawInContext (double width, double height)
{
var img = SelectImage (width, height);
return img.CanDrawInContext (width, height);
}
internal override void DrawInContext (Context ctx, double x, double y, double width, double height)
{
var img = SelectImage (width, height);
img.DrawInContext (ctx, x, y, width, height);
}
protected override BitmapImage GenerateBitmap (Size size)
{
var img = SelectImage (size.Width, size.Height);
if (img.Size.Width != size.Width || img.Size.Height != size.Height)
img = img.WithSize (size);
return img.ToBitmap ();
}
}
}

Просмотреть файл

@ -377,6 +377,10 @@
<Compile Include="Xwt.Backends\MacSystemBackend.cs" />
<Compile Include="Xwt.Backends\GnomeSystemBackend.cs" />
<Compile Include="Xwt\DesktopType.cs" />
<Compile Include="Xwt.Backends\StockIconId.cs" />
<Compile Include="Xwt.Drawing\DrawingImage.cs" />
<Compile Include="Xwt.Drawing\BitmapImage.cs" />
<Compile Include="Xwt.Drawing\ImageSet.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup />

Просмотреть файл

@ -92,7 +92,7 @@ namespace Xwt
get { return label; }
set {
label = value;
Backend.SetContent (label, XwtObject.GetBackend (image), imagePosition);
Backend.SetContent (label, image, imagePosition);
OnPreferredSizeChanged ();
}
}
@ -101,7 +101,7 @@ namespace Xwt
get { return image; }
set {
image = value;
Backend.SetContent (label, XwtObject.GetBackend (value), imagePosition);
Backend.SetContent (label, value, imagePosition);
OnPreferredSizeChanged ();
}
}
@ -110,7 +110,7 @@ namespace Xwt
get { return imagePosition; }
set {
imagePosition = value;
Backend.SetContent (label, XwtObject.GetBackend (image), imagePosition);
Backend.SetContent (label, image, imagePosition);
OnPreferredSizeChanged ();
}
}

Просмотреть файл

@ -24,6 +24,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using Xwt.Drawing;
namespace Xwt
{
@ -39,7 +40,7 @@ namespace Xwt
public string Label { get; private set; }
public string Icon { get; private set; }
public Image Icon { get; private set; }
public bool IsStockButton { get; private set; }

Просмотреть файл

@ -71,8 +71,7 @@ namespace Xwt
public MenuItem (Command command): this ()
{
Label = command.Label;
if (!string.IsNullOrEmpty (command.Icon))
Image = Image.FromIcon (command.Icon, IconSize.Small);
Image = command.Icon;
}
public MenuItem (string label): this ()

Просмотреть файл

@ -49,7 +49,7 @@ namespace Xwt
}
public static void ShowError (WindowFrame parent, string primaryText, string secondaryText)
{
GenericAlert (parent, StockIcons.Error, primaryText, secondaryText, Command.Ok);
GenericAlert (parent, StockIconId.Error, primaryText, secondaryText, Command.Ok);
}
#endregion
@ -68,7 +68,7 @@ namespace Xwt
}
public static void ShowWarning (WindowFrame parent, string primaryText, string secondaryText)
{
GenericAlert (parent, StockIcons.Warning, primaryText, secondaryText, Command.Ok);
GenericAlert (parent, StockIconId.Warning, primaryText, secondaryText, Command.Ok);
}
#endregion
@ -88,7 +88,7 @@ namespace Xwt
}
public static void ShowMessage (WindowFrame parent, string primaryText, string secondaryText)
{
GenericAlert (parent, StockIcons.Information, primaryText, secondaryText, Command.Ok);
GenericAlert (parent, StockIconId.Information, primaryText, secondaryText, Command.Ok);
}
#endregion
@ -100,7 +100,7 @@ namespace Xwt
public static bool Confirm (string primaryText, string secondaryText, Command button)
{
return GenericAlert (RootWindow, StockIcons.Question, primaryText, secondaryText, Command.Cancel, button) == button;
return GenericAlert (RootWindow, StockIconId.Question, primaryText, secondaryText, Command.Cancel, button) == button;
}
public static bool Confirm (string primaryText, Command button, bool confirmIsDefault)
{
@ -109,7 +109,7 @@ namespace Xwt
public static bool Confirm (string primaryText, string secondaryText, Command button, bool confirmIsDefault)
{
return GenericAlert (RootWindow, StockIcons.Question, primaryText, secondaryText, confirmIsDefault ? 0 : 1, Command.Cancel, button) == button;
return GenericAlert (RootWindow, StockIconId.Question, primaryText, secondaryText, confirmIsDefault ? 0 : 1, Command.Cancel, button) == button;
}
public static bool Confirm (ConfirmationMessage message)
@ -126,7 +126,7 @@ namespace Xwt
public static Command AskQuestion (string primaryText, string secondaryText, params Command[] buttons)
{
return GenericAlert (RootWindow, StockIcons.Question, primaryText, secondaryText, buttons);
return GenericAlert (RootWindow, StockIconId.Question, primaryText, secondaryText, buttons);
}
public static Command AskQuestion (string primaryText, int defaultButton, params Command[] buttons)
{
@ -135,7 +135,7 @@ namespace Xwt
public static Command AskQuestion (string primaryText, string secondaryText, int defaultButton, params Command[] buttons)
{
return GenericAlert (RootWindow, StockIcons.Question, primaryText, secondaryText, defaultButton, buttons);
return GenericAlert (RootWindow, StockIconId.Question, primaryText, secondaryText, defaultButton, buttons);
}
public static Command AskQuestion (QuestionMessage message)
@ -266,7 +266,7 @@ namespace Xwt
{
public QuestionMessage ()
{
Icon = StockIcons.Question;
Icon = StockIconId.Question;
}
public QuestionMessage (string text): this ()
@ -290,7 +290,7 @@ namespace Xwt
public ConfirmationMessage ()
{
Icon = StockIcons.Question;
Icon = StockIconId.Question;
Buttons.Add (Command.Cancel);
}

Просмотреть файл

@ -24,23 +24,30 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using Xwt.Drawing;
using Xwt.Backends;
namespace Xwt
{
public static class StockIcons
{
public const string Error = "Error";
public const string Warning = "Warning";
public const string Information = "Information";
public const string Question = "Question";
public const string OrientationPortrait = "Portrait";
public const string OrientationLandscape = "Landscape";
public const string ZoomIn = "ZoomIn";
public const string ZoomOut = "ZoomOut";
public const string ZoomFit = "ZoomFit";
public const string Zoom100 = "Zoom100";
public const string Add = "Add";
public const string Remove = "Remove";
static Image GetIcon (string id)
{
return Toolkit.CurrentEngine.GetStockIcon (id);
}
public static Image Error { get { return GetIcon (StockIconId.Error); } }
public static Image Warning { get { return GetIcon (StockIconId.Warning); } }
public static Image Information { get { return GetIcon (StockIconId.Information); } }
public static Image Question { get { return GetIcon (StockIconId.Question); } }
public static Image OrientationPortrait { get { return GetIcon (StockIconId.OrientationPortrait); } }
public static Image OrientationLandscape { get { return GetIcon (StockIconId.OrientationLandscape); } }
public static Image ZoomIn { get { return GetIcon (StockIconId.ZoomIn); } }
public static Image ZoomOut { get { return GetIcon (StockIconId.ZoomOut); } }
public static Image ZoomFit { get { return GetIcon (StockIconId.ZoomFit); } }
public static Image Zoom100 { get { return GetIcon (StockIconId.Zoom100); } }
public static Image Add { get { return GetIcon (StockIconId.Add); } }
public static Image Remove { get { return GetIcon (StockIconId.Remove); } }
}
}

Просмотреть файл

@ -42,6 +42,8 @@ namespace Xwt
Queue<Action> exitActions = new Queue<Action> ();
bool exitCallbackRegistered;
Dictionary<string,Image> stockIcons = new Dictionary<string, Image> ();
public static Toolkit CurrentEngine {
get { return currentEngine; }
}
@ -313,6 +315,14 @@ namespace Xwt
throw new NotImplementedException ();
}
internal Image GetStockIcon (string id)
{
Image img;
if (!stockIcons.TryGetValue (id, out img))
stockIcons [id] = img = ImageBackendHandler.GetStockIcon (id);
return img;
}
internal ContextBackendHandler ContextBackendHandler;
internal GradientBackendHandler GradientBackendHandler;
internal TextLayoutBackendHandler TextLayoutBackendHandler;