~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.WPF/Xwt.WPFBackend/DataConverter.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
ļ»æ//
 
2
// DataConverter.cs
 
3
//
 
4
// Authors:
 
5
//       Carlos Alberto Cortez <calberto.cortez@gmail.com>
 
6
//       LuĆ­s Reis <luiscubal@gmail.com>
 
7
//       Eric Maupin <ermau@xamarin.com>
 
8
//
 
9
// Copyright (c) 2011-2012 Carlos Alberto Cortez
 
10
// Copyright (c) 2012 LuĆ­s Reis
 
11
// Copyright (c) 2012 Xamarin, Inc.
 
12
//
 
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:
 
19
//
 
20
// The above copyright notice and this permission notice shall be included in
 
21
// all copies or substantial portions of the Software.
 
22
//
 
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
 
29
// THE SOFTWARE.
 
30
 
 
31
using System;
 
32
using System.Collections.Specialized;
 
33
using System.Runtime.InteropServices;
 
34
using System.Windows;
 
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;
 
41
using Xwt.Drawing;
 
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;
 
47
 
 
48
namespace Xwt.WPFBackend
 
49
{
 
50
        internal static class DataConverter
 
51
        {
 
52
                //
 
53
                // Rect/Point
 
54
                //
 
55
                public static Rectangle ToXwtRect (this SW.Rect rect)
 
56
                {
 
57
                        return new Rectangle (rect.X, rect.Y, rect.Width, rect.Height);
 
58
                }
 
59
 
 
60
                public static SW.Rect ToWpfRect (this Rectangle rect)
 
61
                {
 
62
                        return new SW.Rect (rect.X, rect.Y, rect.Width, rect.Height);
 
63
                }
 
64
 
 
65
                public static SD.RectangleF ToSDRectF (this Rectangle rect)
 
66
                {
 
67
                        return new SD.RectangleF ((float) rect.X, (float) rect.Y, (float) rect.Width, (float) rect.Height);
 
68
                }
 
69
 
 
70
                public static Int32Rect ToInt32Rect (this Rectangle rect)
 
71
                {
 
72
                        return new Int32Rect ((int) rect.X, (int) rect.Y, (int) rect.Width, (int) rect.Height);
 
73
                }
 
74
 
 
75
                public static Point ToXwtPoint (this SW.Point point)
 
76
                {
 
77
                        return new Point (point.X, point.Y);
 
78
                }
 
79
 
 
80
                public static SW.Point ToWpfPoint (this Point point)
 
81
                {
 
82
                        return new SW.Point (point.X, point.Y);
 
83
                }
 
84
 
 
85
                public static Size ToXwtSize (this SD.SizeF self)
 
86
                {
 
87
                        return new Size (self.Width, self.Height);
 
88
                }
 
89
 
 
90
                //
 
91
                // Alignment
 
92
                //
 
93
                public static Alignment ToXwtAlignment (this SW.HorizontalAlignment alignment)
 
94
                {
 
95
                        switch (alignment) {
 
96
                                case SW.HorizontalAlignment.Left: return Alignment.Start;
 
97
                                case SW.HorizontalAlignment.Center: return Alignment.Center;
 
98
                                default: return Alignment.End;
 
99
                        }
 
100
                }
 
101
 
 
102
                public static SW.HorizontalAlignment ToWpfAlignment (this Alignment alignment)
 
103
                {
 
104
                        switch (alignment) {
 
105
                                case Alignment.Start: return SW.HorizontalAlignment.Left;
 
106
                                case Alignment.Center: return SW.HorizontalAlignment.Center;
 
107
                                default: return SW.HorizontalAlignment.Right;
 
108
                        }
 
109
                }
 
110
 
 
111
                //
 
112
                // Color
 
113
                //
 
114
                public static Color ToXwtColor (this SW.Media.Color color)
 
115
                {
 
116
                        return Color.FromBytes (color.R, color.G, color.B, color.A);
 
117
                }
 
118
 
 
119
                public static Color ToXwtColor (this SW.Media.Brush brush)
 
120
                {
 
121
                        var solidBrush = brush as SW.Media.SolidColorBrush;
 
122
                        if (solidBrush == null)
 
123
                                throw new ArgumentException();
 
124
 
 
125
                        return solidBrush.Color.ToXwtColor();
 
126
                }
 
127
 
 
128
                public static SW.Media.Color ToWpfColor (this Color color)
 
129
                {
 
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));
 
135
                }
 
136
 
 
137
                public static System.Drawing.Color ToDrawingColor (this Color color)
 
138
                {
 
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));
 
144
                }
 
145
 
 
146
                //
 
147
                // Font
 
148
                //
 
149
                public static SD.Font ToDrawingFont (this Font font)
 
150
                {
 
151
                        SD.FontStyle style = font.Style.ToDrawingFontStyle ();
 
152
                        if (font.Weight > FontWeight.Normal)
 
153
                                style |= SD.FontStyle.Bold;
 
154
 
 
155
                        return new SD.Font (font.Family, (float)font.Size, style);
 
156
                }
 
157
                
 
158
                public static SD.StringTrimming ToDrawingStringTrimming (this Xwt.Drawing.TextTrimming value)
 
159
                {
 
160
                        if (value == Xwt.Drawing.TextTrimming.Word) return SD.StringTrimming.Word;
 
161
                        if (value == Xwt.Drawing.TextTrimming.WordElipsis) return SD.StringTrimming.EllipsisWord;
 
162
 
 
163
                        return SD.StringTrimming.Word;
 
164
                }
 
165
                
 
166
                public static FontStyle ToXwtFontStyle (this SW.FontStyle value)
 
167
                {
 
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;
 
171
 
 
172
                        return FontStyle.Normal;
 
173
                }
 
174
 
 
175
                public static SW.FontStyle ToWpfFontStyle (this FontStyle value)
 
176
                {
 
177
                        if (value == FontStyle.Italic) return SW.FontStyles.Italic;
 
178
                        if (value == FontStyle.Oblique) return SW.FontStyles.Oblique;
 
179
                        
 
180
                        return SW.FontStyles.Normal;
 
181
                }
 
182
 
 
183
                public static SD.FontStyle ToDrawingFontStyle (this FontStyle value)
 
184
                {
 
185
                        switch (value) {
 
186
                                case FontStyle.Normal:
 
187
                                        return SD.FontStyle.Regular;
 
188
                                case FontStyle.Italic:
 
189
                                        return SD.FontStyle.Italic;
 
190
                                
 
191
                                default:
 
192
                                        throw new NotImplementedException();
 
193
                        }
 
194
                }
 
195
 
 
196
                public static FontStretch ToXwtFontStretch (this SW.FontStretch value)
 
197
                {
 
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;
 
207
 
 
208
                        return FontStretch.Normal;
 
209
                }
 
210
 
 
211
                public static SW.FontStretch ToWpfFontStretch (this FontStretch value)
 
212
                {
 
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;
 
221
 
 
222
                        return SW.FontStretches.Normal;
 
223
                }
 
224
 
 
225
                public static FontWeight ToXwtFontWeight (this SW.FontWeight value)
 
226
                {
 
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;
 
234
 
 
235
                        return FontWeight.Normal;
 
236
                }
 
237
 
 
238
                public static SW.FontWeight ToWpfFontWeight (this FontWeight value)
 
239
                {
 
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;
 
246
                        
 
247
                        return SW.FontWeights.Normal;
 
248
                }
 
249
 
 
250
                // Dock
 
251
 
 
252
                public static SW.Controls.Dock ToWpfDock (this ContentPosition value)
 
253
                {
 
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;
 
257
 
 
258
                        return SW.Controls.Dock.Right;
 
259
                }
 
260
 
 
261
                // 
 
262
                // Mouse/Pointer Button
 
263
                //
 
264
 
 
265
                public static PointerButton ToXwtButton (this MouseButton value)
 
266
                {
 
267
                        switch (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;
 
273
 
 
274
                                default: throw new ArgumentException();
 
275
                        }
 
276
                }
 
277
 
 
278
                public static MouseButton ToWpfButton (this PointerButton value)
 
279
                {
 
280
                        switch (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;
 
286
 
 
287
                                default: throw new ArgumentException();
 
288
                        }
 
289
                }
 
290
 
 
291
                public static SDI.PixelFormat ToPixelFormat (this ImageFormat self)
 
292
                {
 
293
                        switch (self) {
 
294
                                case ImageFormat.ARGB32:
 
295
                                        return SDI.PixelFormat.Format32bppArgb;
 
296
                                case ImageFormat.RGB24:
 
297
                                        return SDI.PixelFormat.Format24bppRgb;
 
298
                                default:
 
299
                                        throw new ArgumentException();
 
300
                        }
 
301
                }
 
302
 
 
303
                public static SDI.PixelFormat ToPixelFormat (this SW.Media.PixelFormat self)
 
304
                {
 
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;
 
325
 
 
326
                        throw new ArgumentException();
 
327
                }
 
328
 
 
329
                public static SD.Bitmap AsBitmap (object backend)
 
330
                {
 
331
                        var bmp = backend as SD.Bitmap;
 
332
                        if (bmp == null) {
 
333
                                var bs = backend as SWM.Imaging.BitmapSource;
 
334
                                if (bs != null) {
 
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 ();
 
340
                                                fcb.BeginInit ();
 
341
                                                fcb.Source = bs;
 
342
                                                fcb.DestinationFormat = SWM.PixelFormats.Pbgra32;
 
343
                                                fcb.EndInit ();
 
344
                                                bs = fcb;
 
345
                                        }
 
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,
 
348
                                                                        bmp.PixelFormat);
 
349
                                        bs.CopyPixels (new Int32Rect (0, 0, bmp.Width, bmp.Height), data.Scan0, data.Height * data.Stride, data.Stride);
 
350
                                        bmp.UnlockBits (data);
 
351
                                }
 
352
                        }
 
353
 
 
354
                        return bmp;
 
355
                }
 
356
 
 
357
                [DllImport ("gdi32")]
 
358
                private static extern int DeleteObject (IntPtr o);
 
359
 
 
360
                public static SWM.ImageSource AsImageSource (object nativeImage)
 
361
                {
 
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 ();
 
367
 
 
368
                                        try {
 
369
                                                return SW.Interop.Imaging.CreateBitmapSourceFromHBitmap (ptr, IntPtr.Zero, Int32Rect.Empty,
 
370
                                                                                                                                          SWM.Imaging.BitmapSizeOptions.FromEmptyOptions ());
 
371
                                        }
 
372
                                        finally {
 
373
                                                DeleteObject (ptr);
 
374
                                        }
 
375
                                }
 
376
                        }
 
377
 
 
378
                        return source;
 
379
                }
 
380
 
 
381
                //
 
382
                // Drag and Drop
 
383
                //
 
384
                public static DragDropAction ToXwtDropAction (this DragDropEffects value)
 
385
                {
 
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;
 
390
                        return action;
 
391
                }
 
392
 
 
393
                public static DragDropEffects ToWpfDropEffect (this DragDropAction value)
 
394
                {
 
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;
 
399
                        return effects;
 
400
                }
 
401
 
 
402
                public static string ToWpfDataFormat (this TransferDataType type)
 
403
                {
 
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;
 
408
                        return type.Id;
 
409
                }
 
410
 
 
411
                public static TransferDataType ToXwtTransferType (this string type)
 
412
                {
 
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);
 
418
                }
 
419
 
 
420
                // Scrollbar visibility
 
421
 
 
422
                public static SWC.ScrollBarVisibility ToWpfScrollBarVisibility (this ScrollPolicy policy)
 
423
                {
 
424
                        switch (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;
 
431
 
 
432
                                default:
 
433
                                        throw new NotSupportedException ();
 
434
                        }
 
435
                }
 
436
 
 
437
                public static ScrollPolicy ToXwtScrollPolicy (this SWC.ScrollBarVisibility visibility)
 
438
                {
 
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;
 
446
 
 
447
                                default:
 
448
                                        throw new NotSupportedException ();
 
449
                        }
 
450
                }
 
451
 
 
452
                public static DataObject ToDataObject (this TransferDataSource data)
 
453
                {
 
454
                        var retval = new DataObject ();
 
455
                        foreach (var type in data.DataTypes) {
 
456
                                var value = data.GetValue (type);
 
457
 
 
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);
 
464
                                } else
 
465
                                        retval.SetData (type.Id, TransferDataSource.SerializeValue (value));
 
466
                        }
 
467
 
 
468
                        return retval;
 
469
                }
 
470
        }
 
471
}