aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Editors/UIHelper.cs
blob: d75e9bbdf2e8147e6aed39e06e89dacf3b724f7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;

namespace Tango.Editors
{
    /// <exclude/>
    /// <summary>
    /// Contains some helper methods for UI interaction.
    /// </summary>
    internal static class UIHelper
    {
        #region Native Classes
        [StructLayout(LayoutKind.Sequential)]
        internal struct RECT
        {
            private int _Left;
            private int _Top;
            private int _Right;
            private int _Bottom;

            public RECT(RECT Rectangle)
                : this(Rectangle.Left, Rectangle.Top, Rectangle.Right, Rectangle.Bottom)
            {
            }
            public RECT(int Left, int Top, int Right, int Bottom)
            {
                _Left = Left;
                _Top = Top;
                _Right = Right;
                _Bottom = Bottom;
            }

            public int X
            {
                get { return _Left; }
                set { _Left = value; }
            }
            public int Y
            {
                get { return _Top; }
                set { _Top = value; }
            }
            public int Left
            {
                get { return _Left; }
                set { _Left = value; }
            }
            public int Top
            {
                get { return _Top; }
                set { _Top = value; }
            }
            public int Right
            {
                get { return _Right; }
                set { _Right = value; }
            }
            public int Bottom
            {
                get { return _Bottom; }
                set { _Bottom = value; }
            }
            public int Height
            {
                get { return _Bottom - _Top; }
                set { _Bottom = value + _Top; }
            }
            public int Width
            {
                get { return _Right - _Left; }
                set { _Right = value + _Left; }
            }
            public System.Drawing.Point Location
            {
                get { return new System.Drawing.Point(Left, Top); }
                set
                {
                    _Left = value.X;
                    _Top = value.Y;
                }
            }
            public System.Drawing.Size Size
            {
                get { return new System.Drawing.Size(Width, Height); }
                set
                {
                    _Right = value.Width + _Left;
                    _Bottom = value.Height + _Top;
                }
            }

            public static implicit operator System.Drawing.Rectangle(RECT Rectangle)
            {
                return new System.Drawing.Rectangle(Rectangle.Left, Rectangle.Top, Rectangle.Width, Rectangle.Height);
            }
            public static implicit operator RECT(System.Drawing.Rectangle Rectangle)
            {
                return new RECT(Rectangle.Left, Rectangle.Top, Rectangle.Right, Rectangle.Bottom);
            }
            public static bool operator ==(RECT Rectangle1, RECT Rectangle2)
            {
                return Rectangle1.Equals(Rectangle2);
            }
            public static bool operator !=(RECT Rectangle1, RECT Rectangle2)
            {
                return !Rectangle1.Equals(Rectangle2);
            }

            public override string ToString()
            {
                return "{Left: " + _Left + "; " + "Top: " + _Top + "; Right: " + _Right + "; Bottom: " + _Bottom + "}";
            }

            public override int GetHashCode()
            {
                return ToString().GetHashCode();
            }

            public bool Equals(RECT Rectangle)
            {
                return Rectangle.Left == _Left && Rectangle.Top == _Top && Rectangle.Right == _Right && Rectangle.Bottom == _Bottom;
            }

            public override bool Equals(object Object)
            {
                if (Object is RECT)
                {
                    return Equals((RECT)Object);
                }
                else if (Object is System.Drawing.Rectangle)
                {
                    return Equals(new RECT((System.Drawing.Rectangle)Object));
                }

                return false;
            }
        }

        #endregion

        [DllImport("user32.dll")]
        internal static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
        [DllImport("user32.dll")]
        internal static extern bool PrintWindow(IntPtr hWnd, IntPtr hdcBlt, int nFlags);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        internal static extern IntPtr GetCapture();

        internal static void InvokeUI(Action action)
        {
            EnsureDispatcher();


            try
            {
                Application.Current.Dispatcher.BeginInvoke(action);
            }
            catch { }
        }

        internal static void InvokeUIUrgent(Action action)
        {
            EnsureDispatcher();
            Application.Current.Dispatcher.Invoke(action, DispatcherPriority.Send);
        }

        internal static void EnsureDispatcher()
        {
            try
            {
                if (System.Windows.Application.Current == null) //Using this to enable processing outside a WPF application.
                {
                    new System.Windows.Application { ShutdownMode = ShutdownMode.OnExplicitShutdown };
                }
            }
            catch { }
        }

        internal static void DoEvents()
        {
            Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
                                                  new Action(delegate { }));
        }

        internal static void ReleaseMouseCapture()
        {
            IntPtr capturingHandle = GetCapture();
            for (int i = 0; i < Application.Current.Windows.Count; i++)
            {
                if (new WindowInteropHelper(Application.Current.Windows[i]).Handle == capturingHandle)
                {
                    Mouse.Capture(Application.Current.Windows[i], CaptureMode.Element);
                    Application.Current.Windows[i].ReleaseMouseCapture();
                    break;
                }
            }
        }

        internal static void RemoveChild(DependencyObject parent, UIElement child)
        {
            var panel = parent as Panel;
            if (panel != null)
            {
                panel.Children.Remove(child);
                return;
            }

            var decorator = parent as Decorator;
            if (decorator != null)
            {
                if (decorator.Child == child)
                {
                    decorator.Child = null;
                }
                return;
            }

            var contentPresenter = parent as ContentPresenter;
            if (contentPresenter != null)
            {
                if (contentPresenter.Content == child)
                {
                    contentPresenter.Content = null;
                }
                return;
            }

            var contentControl = parent as ContentControl;
            if (contentControl != null)
            {
                if (contentControl.Content == child)
                {
                    contentControl.Content = null;
                }
                return;
            }
        }

        internal static void AddChild(DependencyObject parent, UIElement child)
        {
            var panel = parent as Panel;
            if (panel != null)
            {
                panel.Children.Add(child);
                return;
            }

            var decorator = parent as Decorator;
            if (decorator != null)
            {
                decorator.Child = child;
                return;
            }

            var contentPresenter = parent as ContentPresenter;
            if (contentPresenter != null)
            {
                contentPresenter.Content = child;
                return;
            }

            var contentControl = parent as ContentControl;
            if (contentControl != null)
            {
                contentControl.Content = child;
                return;
            }
        }

        public static RenderTargetBitmap TakeSnapshot(Visual oVisual, Size oSize)
        {

            int nWidth = (int)Math.Ceiling(oSize.Width);
            int nHeight = (int)Math.Ceiling(oSize.Height);

            RenderTargetBitmap oTargetBitmap = new RenderTargetBitmap(
                nWidth,
                nHeight,
                96,
                96,
                PixelFormats.Pbgra32
            );

            DrawingVisual oDrawingVisual = new DrawingVisual();

            using (DrawingContext oDrawingContext = oDrawingVisual.RenderOpen())
            {
                VisualBrush oVisualBrush = new VisualBrush(oVisual) { Stretch = Stretch.Fill };

                oDrawingContext.DrawRectangle(
                    oVisualBrush,
                    null,
                    new Rect(
                        new Point(),
                        new Size(nWidth, nHeight)
                    )
                );

                oDrawingContext.Close();
                oTargetBitmap.Render(oDrawingVisual);
            }

            oTargetBitmap.Freeze();

            return oTargetBitmap;
        }

        public static RenderTargetBitmap TakeSnapshot(FrameworkElement element)
        {

            int nWidth = (int)Math.Ceiling(element.ActualWidth);
            int nHeight = (int)Math.Ceiling(element.ActualHeight);

            RenderTargetBitmap oTargetBitmap = new RenderTargetBitmap(
                nWidth,
                nHeight,
                96,
                96,
                PixelFormats.Pbgra32
            );

            DrawingVisual oDrawingVisual = new DrawingVisual();

            using (DrawingContext oDrawingContext = oDrawingVisual.RenderOpen())
            {
                VisualBrush oVisualBrush = new VisualBrush(element) { Stretch = Stretch.Fill };

                oDrawingContext.DrawRectangle(
                    oVisualBrush,
                    null,
                    new Rect(
                        new Point(),
                        new Size(nWidth, nHeight)
                    )
                );

                oDrawingContext.Close();
                oTargetBitmap.Render(oDrawingVisual);
            }

            return oTargetBitmap;
        }

        public static System.Drawing.Bitmap TakeSnapshot(Window window)
        {

            var handle = new WindowInteropHelper(window).Handle;
            RECT rc;
            GetWindowRect(handle, out rc);

            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(rc.Width, rc.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            var gfxBmp = System.Drawing.Graphics.FromImage(bmp);
            IntPtr hdcBitmap = gfxBmp.GetHdc();

            PrintWindow(handle, hdcBitmap, 0);

            gfxBmp.ReleaseHdc(hdcBitmap);
            gfxBmp.Dispose();

            return bmp;
        }

        internal static Stream GetApplicationResource(String resourcePath)
        {
            String callingAssembly = Assembly.GetCallingAssembly().GetName().Name;
            Uri uri = new Uri("/" + callingAssembly + ";component/" + resourcePath, UriKind.Relative);
            System.Windows.Resources.StreamResourceInfo info = Application.GetResourceStream(uri);
            return info.Stream;
        }

        internal static BitmapSource GetImageFromApplicationResource(String resourcePath)
        {
            String callingAssembly = Assembly.GetCallingAssembly().GetName().Name;
            Uri uri = new Uri("/" + callingAssembly + ";component/" + resourcePath, UriKind.Relative);
            System.Windows.Resources.StreamResourceInfo info = Application.GetResourceStream(uri);

            var bitmapImage = new BitmapImage();
            bitmapImage.BeginInit();
            bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
            bitmapImage.StreamSource = info.Stream;
            bitmapImage.EndInit();
            return bitmapImage;
        }

        internal static String GetStartupPath()
        {
            return System.AppDomain.CurrentDomain.BaseDirectory;
        }

        internal static String GetRandomAnimationName()
        {
            return "ani" + Guid.NewGuid().ToString().Replace("-", "");
        }
    }
}