~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to samples/ComponentInspector/ComponentInspector.Core/Src/Util/Utils.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// <file>
 
2
//     <copyright see="prj:///doc/copyright.txt"/>
 
3
//     <license see="prj:///doc/license.txt"/>
 
4
//     <owner name="Oakland Software Incorporated" email="general@oaklandsoftware.com"/>
 
5
//     <version>$Revision$</version>
 
6
// </file>
 
7
 
 
8
using System;
 
9
using System.Collections;
 
10
using System.Diagnostics;
 
11
using System.Drawing;
 
12
using System.Reflection;
 
13
using System.Windows.Forms;
 
14
 
 
15
using NoGoop.Win32;
 
16
 
 
17
namespace NoGoop.Util
 
18
{
 
19
        internal interface IHasControl
 
20
        {
 
21
                Control GetControl();
 
22
        }
 
23
        
 
24
        internal class Utils
 
25
        {
 
26
                internal const int SPLITTER_SIZE = 3;
 
27
                internal const int BUTTON_HEIGHT = 23;
 
28
                internal const int BUTTON_WIDTH_PAD = 15;
 
29
                        /***
 
30
                        Object ie = AppDomain.CreateInstance(ie);
 
31
                        **/
 
32
                internal const bool             SHOW_WINDOW = true;
 
33
                
 
34
                protected static void InvokeWithSHDocVw(String uriStr)
 
35
                {
 
36
                        Object ie;
 
37
                        Assembly shdocvw = null;
 
38
                        foreach (Assembly assy in AppDomain.CurrentDomain.GetAssemblies())
 
39
                        {
 
40
                                if (assy.GetName().Name.Equals("SHDocVw"))
 
41
                                {
 
42
                                        shdocvw = assy;
 
43
                                        break;
 
44
                                }
 
45
                        }
 
46
                        if (shdocvw == null)
 
47
                                throw new Exception("SHDocVw assembly not found");
 
48
                        Type ieType = 
 
49
                                shdocvw.GetType("SHDocVw.InternetExplorerClass", true);
 
50
                        ConstructorInfo ieCons = ieType.GetConstructor(new Type[0]);
 
51
                        if (ieCons == null)
 
52
                        {
 
53
                                throw new Exception
 
54
                                        ("InternetExplorerClass constructor not found");
 
55
                        }
 
56
                        ie = ieCons.Invoke(null);
 
57
                        if (ie == null)
 
58
                        {
 
59
                                throw new Exception
 
60
                                        ("Unable to create SHDocVw/InternetExplorer");
 
61
                        }
 
62
                        MethodInfo mi = ieType.GetMethod("Navigate");
 
63
                        mi.Invoke(ie, new Object[] { uriStr, null, null, null, null });
 
64
                        PropertyInfo visProp = ieType.GetProperty("Visible");
 
65
                        visProp.SetValue(ie, true, null);
 
66
                        PropertyInfo hwndProp = ieType.GetProperty("HWND");
 
67
                        int hwnd = (int)hwndProp.GetValue(ie, null);
 
68
                        Windows.SetForegroundWindow(hwnd);
 
69
                }
 
70
 
 
71
                protected static void InvokeWithProcessStart(String uriStr)
 
72
                {
 
73
                        ProcessStartInfo psi = new ProcessStartInfo();
 
74
                        /* Simply using Process.Start(e.Link.LinkData.ToString());
 
75
                           does not work on all pc's! */
 
76
                        if (Environment.OSVersion.Platform == PlatformID.Win32NT)
 
77
                        {
 
78
                                /* Start is an internal command in WinNT/2000/XP */
 
79
                                psi.FileName = "cmd.exe";
 
80
                                psi.Arguments = "/c start " + uriStr;
 
81
                        }
 
82
                        else
 
83
                        {
 
84
                                /* Start is an external command in Win9x */
 
85
                                psi.FileName = "start.exe";
 
86
                                psi.Arguments = uriStr;
 
87
                        }
 
88
                        /* Hide the command prompt window */
 
89
                        psi.WindowStyle = ProcessWindowStyle.Hidden;
 
90
                        //Console.WriteLine("psi.FileName: " + psi.FileName);
 
91
                        //Console.WriteLine("psi.Arguments: " + psi.Arguments);
 
92
                        Process.Start(psi);
 
93
                }
 
94
                
 
95
                internal static void InvokeBrowser(String url)
 
96
                {
 
97
                        InvokeBrowser(url, SHOW_WINDOW);
 
98
                }
 
99
                
 
100
                internal static void InvokeBrowser(Uri url)
 
101
                {
 
102
                        InvokeBrowser(url, SHOW_WINDOW);
 
103
                }
 
104
                
 
105
                internal static void InvokeBrowser(String uri, bool showWindow)
 
106
                {
 
107
                        string uriInput = uri;
 
108
                        if (!uri.StartsWith("http:"))
 
109
                                uriInput = "http://" + uri;
 
110
                        InvokeBrowser(new Uri(uriInput), showWindow);
 
111
                }
 
112
                
 
113
                internal static void InvokeBrowser(Uri uri, bool showWindow)
 
114
                {
 
115
                        try {
 
116
                                TraceUtil.WriteLineInfo(null, "Invoke browser: " + uri);
 
117
                                String uriStr = uri.AbsoluteUri;
 
118
                                try {
 
119
                                        // First try this.
 
120
                                        InvokeWithProcessStart(uriStr);
 
121
                                } catch (Exception ex) {
 
122
                                        TraceUtil.WriteLineWarning(null,
 
123
                                                                                         "Invoke browser Process.Start [" 
 
124
                                                                                         + uri + "]: " + ex);
 
125
                                        // If Process.Start did not work, then that means
 
126
                                        // SHDocVw is loaded, try invoking with that using
 
127
                                        // reflection.  If MSFT ever fixes that bug, then this
 
128
                                        // code can be removed
 
129
                                        InvokeWithSHDocVw(uriStr);
 
130
                                }
 
131
                        } catch (Exception ex) {
 
132
                                TraceUtil.WriteLineError(null,
 
133
                                                                                 "Invoke browser [" 
 
134
                                                                                 + uri + "]: " + ex);
 
135
                        }
 
136
                }
 
137
                
 
138
                // Makes a transparent, read only text box used to describe
 
139
                // something
 
140
                internal static RichTextBox MakeDescText(String text, Control con)
 
141
                {
 
142
                        RichTextBox tb = new RichTextBox();
 
143
                        tb.TabStop = false;
 
144
                        tb.Text = text;
 
145
                        tb.BorderStyle = BorderStyle.None;
 
146
                        tb.Multiline = true;
 
147
                        tb.AutoSize = true;
 
148
                        tb.ReadOnly = true;
 
149
                        tb.WordWrap = true;
 
150
                        tb.HideSelection = true;
 
151
                        tb.BackColor = con.BackColor;
 
152
                        // Get number of actual lines the text box needs 
 
153
                        // given the current width
 
154
                        int lines = (1 + tb.GetLineFromCharIndex(tb.TextLength));
 
155
                        tb.Height = tb.PreferredHeight * lines;
 
156
                        return tb;
 
157
                }
 
158
                
 
159
                // Makes a button with the specified text
 
160
                internal static Button MakeButton(String text)
 
161
                {
 
162
                        Button b = new Button();
 
163
                        Graphics g = b.CreateGraphics();
 
164
                        SizeF size = g.MeasureString(text, b.Font);
 
165
                        b.Height = BUTTON_HEIGHT;
 
166
                        int proposedWidth = (int)(size.Width + BUTTON_WIDTH_PAD);
 
167
                        if (b.Width < proposedWidth)
 
168
                                b.Width = proposedWidth;
 
169
                        b.Text = text;
 
170
                        g.Dispose(); 
 
171
                        return b;
 
172
                }
 
173
                
 
174
                internal static int SetMaxWidth(Control con, String text, int maxWidth)
 
175
                {
 
176
                        Graphics g = con.CreateGraphics();
 
177
                        int width = (int)g.MeasureString(text, con.Font).Width;
 
178
                        if (width > maxWidth)   
 
179
                                maxWidth = width;
 
180
                        g.Dispose();
 
181
                        return maxWidth;
 
182
                }
 
183
                
 
184
                internal static String MakeGuidStr(Guid guid)
 
185
                {
 
186
                        return "{" + guid + "}";
 
187
                }
 
188
                
 
189
                // Adds the specified list of controls to the specified
 
190
                // control in reverse order so that the layout works
 
191
                // correctly.
 
192
                internal static void AddControls(Control control, ArrayList controls)
 
193
                {
 
194
                        AddControls(control, controls, 0);
 
195
                }
 
196
                
 
197
                internal static void AddControls(Control control, ArrayList controls, int tabOffset)
 
198
                {
 
199
                        // This is so stupid
 
200
                        controls.Reverse();
 
201
                        int len = controls.Count;
 
202
                        Control[] c = new Control[len];
 
203
                        Array.Copy(controls.ToArray(), c, len);
 
204
                        control.Controls.AddRange(c);
 
205
                        for (int i = 0; i < len; i++)
 
206
                                control.Controls[i].TabIndex = len - i + tabOffset;
 
207
                }
 
208
                
 
209
                // Adds the specified list control containers to specified
 
210
                // control in reverse order so that the layout works
 
211
                // correctly.
 
212
                internal static void AddHasControls(Control control, ArrayList hasControls)
 
213
                {
 
214
                        AddHasControls(control, hasControls, 0);
 
215
                }
 
216
                
 
217
                internal static void AddHasControls(Control control, ArrayList hasControls, int tabOffset)
 
218
                {
 
219
                        
 
220
                        // This is so stupid
 
221
                        hasControls.Reverse();
 
222
                        int len = hasControls.Count;
 
223
                        Control[] c = new Control[len];
 
224
                        for (int i = 0; i < len; i++) {
 
225
                                c[i] = ((IHasControl)hasControls[i]).GetControl();
 
226
                                c[i].TabIndex = len - i + tabOffset;
 
227
                        }
 
228
                        control.Controls.AddRange(c);
 
229
                }
 
230
                
 
231
                // Works around a problem that some objects (notably Cursor)
 
232
                // throw in their Equals method
 
233
                internal static bool ContainsHack(ArrayList a, Object obj)
 
234
                {
 
235
                        return FindHack(a, obj) != null;
 
236
                }
 
237
                
 
238
                internal static Object FindHack(ArrayList a, Object obj)
 
239
                {
 
240
                        foreach (Object arrayObj in a) {
 
241
                                try {
 
242
                                        if (arrayObj.Equals(obj))
 
243
                                                return arrayObj;
 
244
                                } catch  {
 
245
                                        continue;
 
246
                                }
 
247
                        }
 
248
                        return null;
 
249
                }
 
250
                
 
251
                internal static bool IsArrayListEqual(ArrayList al1, ArrayList al2)
 
252
                {
 
253
                        if (al1 == null && al2 == null)
 
254
                                return true;
 
255
                        if (al1 == null || al2 == null ||
 
256
                                al1.Count != al2.Count)
 
257
                                return false;
 
258
                        bool isEqual = true;
 
259
                        for (int i = 0; i < al2.Count; i++) {
 
260
                                if (al2[i] != al1[i]) {
 
261
                                        isEqual = false;
 
262
                                        break;
 
263
                                }
 
264
                        }
 
265
                        return isEqual;
 
266
                }
 
267
        }
 
268
}