-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtilityFunctions.cs
More file actions
375 lines (333 loc) · 14.9 KB
/
Copy pathUtilityFunctions.cs
File metadata and controls
375 lines (333 loc) · 14.9 KB
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
/*
* NOTICE:
* The U.S. Army Corps of Engineers, Risk Management Center (USACE-RMC) makes no guarantees about
* the results, or appropriateness of outputs, obtained from this software.
*
* LIST OF CONDITIONS:
* Redistribution and use in source and binary forms, with or without modification, are permitted
* provided that the following conditions are met:
* ● Redistributions of source code must retain the above notice, this list of conditions, and the
* following disclaimer.
* ● Redistributions in binary form must reproduce the above notice, this list of conditions, and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
* ● The names of the U.S. Government, the U.S. Army Corps of Engineers, the Institute for Water
* Resources, or the Risk Management Center may not be used to endorse or promote products derived
* from this software without specific prior written permission. Nor may the names of its contributors
* be used to endorse or promote products derived from this software without specific prior
* written permission.
*
* DISCLAIMER:
* THIS SOFTWARE IS PROVIDED BY THE U.S. ARMY CORPS OF ENGINEERS RISK MANAGEMENT CENTER
* (USACE-RMC) "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL USACE-RMC BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
namespace FrameworkUI
{
/// <summary>
/// Utility functions for common operations in the framework.
/// </summary>
/// <remarks>
/// <para>
/// <b> Authors: </b>
/// <list type="bullet">
/// <item> Haden Smith, USACE Risk Management Center, cole.h.smith@usace.army.mil </item>
/// </list>
/// </para>
/// </remarks>
public static class UtilityFunctions
{
/// <summary>
/// Converts a UTF-8 string to a byte array.
/// </summary>
/// <param name="str">The string to convert.</param>
/// <returns>A byte array representing the UTF-8 encoded string.</returns>
public static byte[] UTF8StringToBytes(string str)
{
return Encoding.UTF8.GetBytes(str);
}
/// <summary>
/// Converts a UTF-8 byte array to a string.
/// </summary>
/// <param name="bytes">The byte array to convert.</param>
/// <returns>A string decoded from the UTF-8 byte array.</returns>
public static string UTF8BytesToString(byte[] bytes)
{
return Encoding.UTF8.GetString(bytes);
}
/// <summary>
/// Function used to convert an integer to a color.
/// </summary>
/// <param name="ARGB">Alpha, red, green, blue as integer.</param>
/// <returns>A Color object created from the ARGB integer value.</returns>
public static System.Windows.Media.Color IntegerToColor(ref int ARGB)
{
var Bytes = BitConverter.GetBytes(ARGB);
byte Alpha = Bytes[3];
byte Red = Bytes[2];
byte Green = Bytes[1];
byte Blue = Bytes[0];
return System.Windows.Media.Color.FromArgb(Alpha, Red, Green, Blue);
}
/// <summary>
/// Function used to convert a color to an integer.
/// </summary>
/// <param name="color">The color to convert.</param>
/// <returns>An ARGB integer value representing the color.</returns>
public static int ColorToInteger(System.Windows.Media.Color color)
{
return (color.A << 24) | (color.R << 16) | (color.G << 8) | color.B;
}
/// <summary>
/// Gets the free space available on the drive.
/// </summary>
/// <param name="driveName">Drive name.</param>
/// <returns>The available free space on the drive in bytes.</returns>
public static long GetAvailableDriveSpace(string driveName)
{
var allDrives = DriveInfo.GetDrives();
var freeSpace = default(long);
foreach (var dInfo in allDrives)
{
if (dInfo.Name == driveName)
{
freeSpace = dInfo.AvailableFreeSpace;
break;
}
}
return freeSpace;
}
/// <summary>
/// Shortens a pathname for display purposes.
/// </summary>
/// <param name="pathname">The pathname to shorten.</param>
/// <param name="maxLength">The maximum number of characters to be displayed.</param>
/// <remarks>
/// Shortens a pathname by either removing consecutive components of a path
/// and/or by removing characters from the end of the filename and replacing
/// then with three ellipses (...)
/// <para>
/// In all cases, the root of the passed path will be preserved in it's entirety.
/// </para>
/// <para>
/// If a UNC path is used or the pathname and maxLength are particularly short,
/// the resulting path may be longer than maxLength.
/// </para>
/// <para>
/// This method expects fully resolved pathnames to be passed to it.
/// (Use Path.GetFullPath() to obtain this.)
/// </para>
/// <para>
/// This method is taken from Joe Woodbury's article at: http://www.codeproject.com/KB/cs/mrutoolstripmenu.aspx
/// </para>
/// </remarks>
/// <returns>A shortened pathname string.</returns>
public static string ShortenPathname(string pathname, int maxLength)
{
if (pathname.Length <= maxLength)
{
return pathname;
}
string? root = Path.GetPathRoot(pathname);
if (root == null || root.Length == 0) return pathname;
if (root.Length > 3)
{
root += Path.DirectorySeparatorChar.ToString();
}
var elements = pathname.Substring(root.Length).Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
int filenameIndex = elements.GetLength(0) - 1;
if (elements.GetLength(0) == 1)
{
// pathname is just a root and filename
if (elements[0].Length > 5)
{
// long enough to shorten
// if path is a UNC path, root may be rather long
if (root.Length + 6 >= maxLength)
{
return root + elements[0].Substring(0, 3) + "...";
}
else
{
return pathname.Substring(0, maxLength - 3) + "...";
}
}
}
else if (root.Length + 4 + elements[filenameIndex].Length > maxLength)
{
// pathname is just a root and filename
root += @"...\";
int len = elements[filenameIndex].Length;
if (len < 6)
{
return root + elements[filenameIndex];
}
if (root.Length + 6 >= maxLength)
{
len = 3;
}
else
{
len = maxLength - root.Length - 3;
}
return root + elements[filenameIndex].Substring(0, len) + "...";
}
else if (elements.GetLength(0) == 2)
{
return root + @"...\" + elements[1];
}
else
{
int len = 0;
int begin = 0;
for (int i = 0; i < filenameIndex; i++)
{
if (elements[i].Length > len)
{
begin = i;
len = elements[i].Length;
}
}
int totalLength = pathname.Length - len + 3;
int end = begin + 1;
while (totalLength > maxLength)
{
if (begin > 0)
{
totalLength -= elements[System.Threading.Interlocked.Decrement(ref begin)].Length - 1;
}
if (totalLength <= maxLength)
{
break;
}
if (end < filenameIndex)
{
totalLength -= elements[System.Threading.Interlocked.Increment(ref end)].Length - 1;
}
if (begin == 0 && end == filenameIndex)
{
break;
}
}
// assemble final string
for (int i = 0; i < begin; i++)
root += elements[i] + '\\';
root += @"...\";
for (int i = end; i < filenameIndex; i++)
root += elements[i] + '\\';
return root + elements[filenameIndex];
}
return pathname;
}
/// <summary>
/// Counter used to generate unique menu item names when cloning.
/// </summary>
private static int menuNameCounter = 0;
/// <summary>
/// Clones a MenuItem including all its properties and sub-items.
/// </summary>
/// <param name="sourceItem">The source MenuItem to clone.</param>
/// <returns>A deep copy of the MenuItem with all properties and sub-items.</returns>
public static MenuItem Clone(this MenuItem sourceItem)
{
MenuItem copyItem = new MenuItem();
var propInfoList = from p in typeof(MenuItem).GetProperties()
let attributes = p.GetCustomAttributes(true)
let notBrowseable = (from a in attributes where a.GetType() == typeof(BrowsableAttribute) select !((BrowsableAttribute)a).Browsable).FirstOrDefault()
where !notBrowseable && p.CanRead && p.CanWrite && p.Name != "Icon"
orderby p.Name
select p;
// Copy over using reflections
foreach (var propertyInfo in propInfoList)
{
object? propertyInfoValue = propertyInfo.GetValue(sourceItem, null);
propertyInfo.SetValue(copyItem, propertyInfoValue, null);
}
// Create a new menu name
copyItem.Name = sourceItem.Name + "_" + menuNameCounter++;
copyItem.IsHitTestVisible = true;
// Recursively clone the sub items list
foreach (var itm in sourceItem.Items)
{
MenuItem newItem;
if (itm is MenuItem mItem)
{
newItem = Clone(mItem);
copyItem.Items.Add(newItem);
}
else if (itm is Separator)
{
copyItem.Items.Add(new Separator());
}
else
{
throw new NotImplementedException("Menu item is not a MenuItem or a Separator");
}
}
copyItem.CopyClick(sourceItem);
// set the icon
if (!(sourceItem.Icon is Image img)) { return copyItem; }
if (img.Source is InteropBitmap ibmp) { copyItem.Icon = new Image() { Source = ibmp.Clone() }; }
if (img.Source is BitmapImage bmp) { copyItem.Icon = new Image() { Source = bmp.Clone() }; }
return copyItem;
}
/// <summary>
/// Adds the handlers from the source component to the destination component
/// </summary>
/// <param name="destinationComponent">The destination component.</param>
/// <param name="sourceComponent">The source component.</param>
public static void CopyClick(this MenuItem destinationComponent, MenuItem sourceComponent)
{
var events = sourceComponent.GetType().GetEvents().Where(x => x.Name == "Click");
foreach (var theEvent in events)
{
var fieldInfo = sourceComponent.GetType().GetField($"{theEvent.Name}Event");
RoutedEvent? eventKind = fieldInfo?.GetValue(sourceComponent) as RoutedEvent;
if (eventKind is null) { continue; }
var reh = GetRoutedEventHandlers(sourceComponent, eventKind);
foreach (Delegate re in reh)
{
destinationComponent.Click += (RoutedEventHandler)re;
}
}
}
/// <summary>
/// Gets all routed event handlers for a specific routed event on a UI element.
/// </summary>
/// <param name="element">The UI element to get handlers from.</param>
/// <param name="routedEvent">The routed event to get handlers for.</param>
/// <returns>An array of delegates representing the event handlers, or an empty array if none exist.</returns>
public static Delegate[] GetRoutedEventHandlers(UIElement element, RoutedEvent routedEvent)
{
if (element == null || routedEvent == null) { throw new ArgumentNullException(); }
// Access the EventHandlersStore (internal class)
var eventHandlersStoreProperty = typeof(UIElement).GetProperty("EventHandlersStore", BindingFlags.Instance | BindingFlags.NonPublic);
var eventHandlersStore = eventHandlersStoreProperty?.GetValue(element);
if (eventHandlersStore == null) { return Array.Empty<Delegate>(); }
// Get the GetRoutedEventHandlers method
var getRoutedEventHandlersMethod = eventHandlersStore.GetType().GetMethod("GetRoutedEventHandlers", BindingFlags.Instance | BindingFlags.Public);
var handlers = getRoutedEventHandlersMethod?.Invoke(eventHandlersStore, new object[] { routedEvent }) as RoutedEventHandlerInfo[];
if (handlers == null) { return Array.Empty<Delegate>(); }
// Extract the delegates
var delegates = new Delegate[handlers.Length];
for (int i = 0; i < handlers.Length; i++)
{
delegates[i] = handlers[i].Handler;
}
return delegates;
}
}
}