5
// Carlos Alberto Cortez <calberto.cortez@gmail.com>
6
// LuĆs Reis <luiscubal@gmail.com>
7
// Eric Maupin <ermau@xamarin.com>
9
// Copyright (c) 2011-2012 Carlos Alberto Cortez
10
// Copyright (c) 2012 LuĆs Reis
11
// Copyright (c) 2012 Xamarin, Inc.
13
// Permission is hereby granted, free of charge, to any person obtaining a copy
14
// of this software and associated documentation files (the "Software"), to deal
15
// in the Software without restriction, including without limitation the rights
16
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
// copies of the Software, and to permit persons to whom the Software is
18
// furnished to do so, subject to the following conditions:
20
// The above copyright notice and this permission notice shall be included in
21
// all copies or substantial portions of the Software.
23
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32
using System.Collections.Specialized;
33
using System.Runtime.InteropServices;
35
using System.Windows.Input;
36
using SW = System.Windows;
37
using SWC = System.Windows.Controls;
38
using SWM = System.Windows.Media;
39
using SD = System.Drawing;
40
using SDI = System.Drawing.Imaging;
42
using Color = Xwt.Drawing.Color;
43
using FontStretch = Xwt.Drawing.FontStretch;
44
using FontStyle = Xwt.Drawing.FontStyle;
45
using FontWeight = Xwt.Drawing.FontWeight;
46
using ImageFormat = Xwt.Drawing.ImageFormat;
48
namespace Xwt.WPFBackend
50
internal static class DataConverter
55
public static Rectangle ToXwtRect (this SW.Rect rect)
57
return new Rectangle (rect.X, rect.Y, rect.Width, rect.Height);
60
public static SW.Rect ToWpfRect (this Rectangle rect)
62
return new SW.Rect (rect.X, rect.Y, rect.Width, rect.Height);
65
public static SD.RectangleF ToSDRectF (this Rectangle rect)
67
return new SD.RectangleF ((float) rect.X, (float) rect.Y, (float) rect.Width, (float) rect.Height);
70
public static Int32Rect ToInt32Rect (this Rectangle rect)
72
return new Int32Rect ((int) rect.X, (int) rect.Y, (int) rect.Width, (int) rect.Height);
75
public static Point ToXwtPoint (this SW.Point point)
77
return new Point (point.X, point.Y);
80
public static SW.Point ToWpfPoint (this Point point)
82
return new SW.Point (point.X, point.Y);
85
public static Size ToXwtSize (this SD.SizeF self)
87
return new Size (self.Width, self.Height);
93
public static Alignment ToXwtAlignment (this SW.HorizontalAlignment alignment)
96
case SW.HorizontalAlignment.Left: return Alignment.Start;
97
case SW.HorizontalAlignment.Center: return Alignment.Center;
98
default: return Alignment.End;
102
public static SW.HorizontalAlignment ToWpfAlignment (this Alignment alignment)
105
case Alignment.Start: return SW.HorizontalAlignment.Left;
106
case Alignment.Center: return SW.HorizontalAlignment.Center;
107
default: return SW.HorizontalAlignment.Right;
114
public static Color ToXwtColor (this SW.Media.Color color)
116
return Color.FromBytes (color.R, color.G, color.B, color.A);
119
public static Color ToXwtColor (this SW.Media.Brush brush)
121
var solidBrush = brush as SW.Media.SolidColorBrush;
122
if (solidBrush == null)
123
throw new ArgumentException();
125
return solidBrush.Color.ToXwtColor();
128
public static SW.Media.Color ToWpfColor (this Color color)
130
return SW.Media.Color.FromArgb (
131
(byte)(color.Alpha * 255.0),
132
(byte)(color.Red * 255.0),
133
(byte)(color.Green * 255.0),
134
(byte)(color.Blue * 255.0));
137
public static System.Drawing.Color ToDrawingColor (this Color color)
139
return System.Drawing.Color.FromArgb (
140
(byte) (color.Alpha * 255.0),
141
(byte) (color.Red * 255.0),
142
(byte) (color.Green * 255.0),
143
(byte) (color.Blue * 255.0));
149
public static SD.Font ToDrawingFont (this Font font)
151
SD.FontStyle style = font.Style.ToDrawingFontStyle ();
152
if (font.Weight > FontWeight.Normal)
153
style |= SD.FontStyle.Bold;
155
return new SD.Font (font.Family, (float)font.Size, style);
158
public static SD.StringTrimming ToDrawingStringTrimming (this Xwt.Drawing.TextTrimming value)
160
if (value == Xwt.Drawing.TextTrimming.Word) return SD.StringTrimming.Word;
161
if (value == Xwt.Drawing.TextTrimming.WordElipsis) return SD.StringTrimming.EllipsisWord;
163
return SD.StringTrimming.Word;
166
public static FontStyle ToXwtFontStyle (this SW.FontStyle value)
168
// No, SW.FontStyles is not an enum
169
if (value == SW.FontStyles.Italic) return FontStyle.Italic;
170
if (value == SW.FontStyles.Oblique) return FontStyle.Oblique;
172
return FontStyle.Normal;
175
public static SW.FontStyle ToWpfFontStyle (this FontStyle value)
177
if (value == FontStyle.Italic) return SW.FontStyles.Italic;
178
if (value == FontStyle.Oblique) return SW.FontStyles.Oblique;
180
return SW.FontStyles.Normal;
183
public static SD.FontStyle ToDrawingFontStyle (this FontStyle value)
186
case FontStyle.Normal:
187
return SD.FontStyle.Regular;
188
case FontStyle.Italic:
189
return SD.FontStyle.Italic;
192
throw new NotImplementedException();
196
public static FontStretch ToXwtFontStretch (this SW.FontStretch value)
198
// No, SW.FontStretches is not an enum
199
if (value == SW.FontStretches.UltraCondensed) return FontStretch.UltraCondensed;
200
if (value == SW.FontStretches.ExtraCondensed) return FontStretch.ExtraCondensed;
201
if (value == SW.FontStretches.Condensed) return FontStretch.Condensed;
202
if (value == SW.FontStretches.SemiCondensed) return FontStretch.SemiCondensed;
203
if (value == SW.FontStretches.SemiExpanded) return FontStretch.SemiExpanded;
204
if (value == SW.FontStretches.Expanded) return FontStretch.Expanded;
205
if (value == SW.FontStretches.ExtraExpanded) return FontStretch.ExtraExpanded;
206
if (value == SW.FontStretches.UltraExpanded) return FontStretch.UltraExpanded;
208
return FontStretch.Normal;
211
public static SW.FontStretch ToWpfFontStretch (this FontStretch value)
213
if (value == FontStretch.UltraCondensed) return SW.FontStretches.UltraCondensed;
214
if (value == FontStretch.ExtraCondensed) return SW.FontStretches.ExtraCondensed;
215
if (value == FontStretch.Condensed) return SW.FontStretches.Condensed;
216
if (value == FontStretch.SemiCondensed) return SW.FontStretches.SemiCondensed;
217
if (value == FontStretch.SemiExpanded) return SW.FontStretches.SemiExpanded;
218
if (value == FontStretch.Expanded) return SW.FontStretches.Expanded;
219
if (value == FontStretch.ExtraExpanded) return SW.FontStretches.ExtraExpanded;
220
if (value == FontStretch.UltraExpanded) return SW.FontStretches.UltraExpanded;
222
return SW.FontStretches.Normal;
225
public static FontWeight ToXwtFontWeight (this SW.FontWeight value)
227
// No, SW.FontWeights is not an enum
228
if (value == SW.FontWeights.UltraLight) return FontWeight.Ultralight;
229
if (value == SW.FontWeights.Light) return FontWeight.Light;
230
if (value == SW.FontWeights.SemiBold) return FontWeight.Semibold;
231
if (value == SW.FontWeights.Bold) return FontWeight.Bold;
232
if (value == SW.FontWeights.UltraBold) return FontWeight.Ultrabold;
233
if (value == SW.FontWeights.Black) return FontWeight.Heavy;
235
return FontWeight.Normal;
238
public static SW.FontWeight ToWpfFontWeight (this FontWeight value)
240
if (value == FontWeight.Ultralight) return SW.FontWeights.UltraLight;
241
if (value == FontWeight.Light) return SW.FontWeights.Light;
242
if (value == FontWeight.Semibold) return SW.FontWeights.SemiBold;
243
if (value == FontWeight.Bold) return SW.FontWeights.Bold;
244
if (value == FontWeight.Ultrabold) return SW.FontWeights.UltraBold;
245
if (value == FontWeight.Heavy) return SW.FontWeights.Black;
247
return SW.FontWeights.Normal;
252
public static SW.Controls.Dock ToWpfDock (this ContentPosition value)
254
if (value == ContentPosition.Left) return SW.Controls.Dock.Left;
255
if (value == ContentPosition.Top) return SW.Controls.Dock.Top;
256
if (value == ContentPosition.Bottom) return SW.Controls.Dock.Bottom;
258
return SW.Controls.Dock.Right;
262
// Mouse/Pointer Button
265
public static PointerButton ToXwtButton (this MouseButton value)
268
case MouseButton.Left: return PointerButton.Left;
269
case MouseButton.Middle: return PointerButton.Middle;
270
case MouseButton.Right: return PointerButton.Right;
271
case MouseButton.XButton1: return PointerButton.ExtendedButton1;
272
case MouseButton.XButton2: return PointerButton.ExtendedButton2;
274
default: throw new ArgumentException();
278
public static MouseButton ToWpfButton (this PointerButton value)
281
case PointerButton.Left: return MouseButton.Left;
282
case PointerButton.Middle: return MouseButton.Middle;
283
case PointerButton.Right: return MouseButton.Right;
284
case PointerButton.ExtendedButton1: return MouseButton.XButton1;
285
case PointerButton.ExtendedButton2: return MouseButton.XButton2;
287
default: throw new ArgumentException();
291
public static SDI.PixelFormat ToPixelFormat (this ImageFormat self)
294
case ImageFormat.ARGB32:
295
return SDI.PixelFormat.Format32bppArgb;
296
case ImageFormat.RGB24:
297
return SDI.PixelFormat.Format24bppRgb;
299
throw new ArgumentException();
303
public static SDI.PixelFormat ToPixelFormat (this SW.Media.PixelFormat self)
305
if (self == SWM.PixelFormats.Rgb24)
306
return SDI.PixelFormat.Format24bppRgb;
307
if (self == SWM.PixelFormats.Bgra32)
308
return SDI.PixelFormat.Format32bppArgb;
309
if (self == SWM.PixelFormats.Pbgra32)
310
return SDI.PixelFormat.Format32bppPArgb;
311
if (self == SWM.PixelFormats.Prgba64)
312
return SDI.PixelFormat.Format64bppPArgb;
313
if (self == SWM.PixelFormats.Indexed1)
314
return SDI.PixelFormat.Format1bppIndexed;
315
if (self == SWM.PixelFormats.Indexed4)
316
return SDI.PixelFormat.Format4bppIndexed;
317
if (self == SWM.PixelFormats.Indexed8)
318
return SDI.PixelFormat.Format8bppIndexed;
319
if (self == SWM.PixelFormats.Gray16)
320
return SDI.PixelFormat.Format16bppGrayScale;
321
if (self == SWM.PixelFormats.Bgr24)
322
return SDI.PixelFormat.Format24bppRgb;
323
if (self == SWM.PixelFormats.Bgr32)
324
return SDI.PixelFormat.Format32bppRgb;
326
throw new ArgumentException();
329
public static SD.Bitmap AsBitmap (object backend)
331
var bmp = backend as SD.Bitmap;
333
var bs = backend as SWM.Imaging.BitmapSource;
335
if (bs.Format == SWM.PixelFormats.Indexed1 || bs.Format == SWM.PixelFormats.Indexed2 || bs.Format == SWM.PixelFormats.Indexed4 || bs.Format == SWM.PixelFormats.Indexed8) {
336
// The conversion method below doesn't work for indexed formats, since it only copies pixel data, not the palette.
337
// Since there is no way of creating a palette for a Bitmap, a solution is to convert the image source to a non-indexed
338
// format before converting to Bitmap
339
var fcb = new SWM.Imaging.FormatConvertedBitmap ();
342
fcb.DestinationFormat = SWM.PixelFormats.Pbgra32;
346
bmp = new SD.Bitmap (bs.PixelWidth, bs.PixelHeight, bs.Format.ToPixelFormat ());
347
SDI.BitmapData data = bmp.LockBits (new System.Drawing.Rectangle (0, 0, bmp.Width, bmp.Height), SDI.ImageLockMode.WriteOnly,
349
bs.CopyPixels (new Int32Rect (0, 0, bmp.Width, bmp.Height), data.Scan0, data.Height * data.Stride, data.Stride);
350
bmp.UnlockBits (data);
357
[DllImport ("gdi32")]
358
private static extern int DeleteObject (IntPtr o);
360
public static SWM.ImageSource AsImageSource (object nativeImage)
362
var source = nativeImage as SWM.ImageSource;
363
if (source == null) {
364
var bitmap = nativeImage as SD.Bitmap;
365
if (bitmap != null) {
366
IntPtr ptr = bitmap.GetHbitmap ();
369
return SW.Interop.Imaging.CreateBitmapSourceFromHBitmap (ptr, IntPtr.Zero, Int32Rect.Empty,
370
SWM.Imaging.BitmapSizeOptions.FromEmptyOptions ());
384
public static DragDropAction ToXwtDropAction (this DragDropEffects value)
386
var action = DragDropAction.None;
387
if ((value & DragDropEffects.Copy) > 0) action |= DragDropAction.Copy;
388
if ((value & DragDropEffects.Move) > 0) action |= DragDropAction.Move;
389
if ((value & DragDropEffects.Link) > 0) action |= DragDropAction.Link;
393
public static DragDropEffects ToWpfDropEffect (this DragDropAction value)
395
var effects = DragDropEffects.None;
396
if ((value & DragDropAction.Copy) > 0) effects |= DragDropEffects.Copy;
397
if ((value & DragDropAction.Move) > 0) effects |= DragDropEffects.Move;
398
if ((value & DragDropAction.Link) > 0) effects |= DragDropEffects.Link;
402
public static string ToWpfDataFormat (this TransferDataType type)
404
if (type == TransferDataType.Text) return DataFormats.UnicodeText;
405
if (type == TransferDataType.Rtf) return DataFormats.Rtf;
406
if (type == TransferDataType.Uri) return DataFormats.FileDrop;
407
if (type == TransferDataType.Image) return DataFormats.Bitmap;
411
public static TransferDataType ToXwtTransferType (this string type)
413
if (type == DataFormats.UnicodeText) return TransferDataType.Text;
414
if (type == DataFormats.Rtf) return TransferDataType.Rtf;
415
if (type == DataFormats.FileDrop) return TransferDataType.Uri;
416
if (type == DataFormats.Bitmap) return TransferDataType.Image;
417
return TransferDataType.FromId (type);
420
// Scrollbar visibility
422
public static SWC.ScrollBarVisibility ToWpfScrollBarVisibility (this ScrollPolicy policy)
425
case ScrollPolicy.Always:
426
return SWC.ScrollBarVisibility.Visible;
427
case ScrollPolicy.Automatic:
428
return SWC.ScrollBarVisibility.Auto;
429
case ScrollPolicy.Never:
430
return SWC.ScrollBarVisibility.Hidden;
433
throw new NotSupportedException ();
437
public static ScrollPolicy ToXwtScrollPolicy (this SWC.ScrollBarVisibility visibility)
439
switch (visibility) {
440
case SWC.ScrollBarVisibility.Auto:
441
return ScrollPolicy.Automatic;
442
case SWC.ScrollBarVisibility.Visible:
443
return ScrollPolicy.Always;
444
case SWC.ScrollBarVisibility.Hidden:
445
return ScrollPolicy.Never;
448
throw new NotSupportedException ();
452
public static DataObject ToDataObject (this TransferDataSource data)
454
var retval = new DataObject ();
455
foreach (var type in data.DataTypes) {
456
var value = data.GetValue (type);
458
if (type == TransferDataType.Text)
459
retval.SetText ((string)value);
460
else if (type == TransferDataType.Uri) {
461
var uris = new StringCollection ();
462
uris.Add (((Uri)value).LocalPath);
463
retval.SetFileDropList (uris);
465
retval.SetData (type.Id, TransferDataSource.SerializeValue (value));