This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / Microsoft.VisualBasic / Microsoft.VisualBasic / Interaction.cs
1 //
2 // Interaction.cs
3 //
4 // Author:
5 //   Chris J Breisch (cjbreisch@altavista.net)
6 //   Joerg Rosenkranz (JoergR@voelcker.com)
7 //
8 // (C) 2002 Chris J Breisch
9 // (C) 2004 Joerg Rosenkranz
10 //
11
12 //
13 // Copyright (c) 2002-2003 Mainsoft Corporation.
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 //
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
23 // 
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
26 // 
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 //
35
36 using System;
37 using System.Reflection;
38 using System.Collections;
39 using System.Diagnostics;
40 //using Windows.Drawing;
41 //using System.Windows.Forms;
42
43 using Microsoft.VisualBasic.CompilerServices;
44
45 namespace Microsoft.VisualBasic {
46         [Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute] 
47         [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Auto)] 
48         sealed public class Interaction {
49                 // Declarations
50                 // Constructors
51                 // Properties
52                 // Methods
53                 //[MonoTODO]
54                 public static System.Int32 Shell (System.String Pathname, 
55                                                   [System.Runtime.InteropServices.Optional] 
56                                                   [System.ComponentModel.DefaultValue(2)] Microsoft.VisualBasic.AppWinStyle Style, 
57                                                   [System.Runtime.InteropServices.Optional] 
58                                                   [System.ComponentModel.DefaultValue(false)] System.Boolean Wait, 
59                                                   [System.Runtime.InteropServices.Optional] 
60                                                   [System.ComponentModel.DefaultValue(-1)] System.Int32 Timeout)
61                 { 
62                         Process prcs = new Process();
63
64                         ProcessWindowStyle PWinStyle = 0;
65                         switch (Style){
66                         case AppWinStyle.Hide:
67                                 PWinStyle = ProcessWindowStyle.Hidden;
68                                 break;
69                         case AppWinStyle.NormalFocus:
70                                 PWinStyle = ProcessWindowStyle.Normal;
71                                 break;
72                         case AppWinStyle.MinimizedFocus:
73                                 PWinStyle = ProcessWindowStyle.Minimized;
74                                 break;
75                         case AppWinStyle.MaximizedFocus:
76                                 PWinStyle = ProcessWindowStyle.Maximized;
77                                 break;
78                         case AppWinStyle.NormalNoFocus:
79                                 PWinStyle = ProcessWindowStyle.Normal; //ToDo: no focus is not set
80                                 break;
81                         case AppWinStyle.MinimizedNoFocus:
82                                 PWinStyle = ProcessWindowStyle.Minimized; //ToDo: no focus is not set
83                                 break;
84                         }
85
86                         prcs.StartInfo.FileName = Pathname;
87                         prcs.StartInfo.WindowStyle = PWinStyle;
88
89                         try     
90                         {
91                                 if(prcs.Start()) 
92                                 {
93                                         if (Wait)
94                                         {
95                                                 if (Timeout == -1)
96                                                         prcs.WaitForExit();
97                                                 else
98                                                         prcs.WaitForExit(Timeout);
99                                         }
100                                         return prcs.Id;
101                                 }
102                                 else
103                                         return 0;
104                         }
105                         catch (System.ComponentModel.Win32Exception e){
106                                 throw new System.IO.FileNotFoundException (
107                                                                            Utils.GetResourceString(53));
108                         }
109                 }
110                         
111                 [MonoTODO]
112                 public static void AppActivate (System.Int32 ProcessId)
113                 { 
114                         throw new NotImplementedException ();
115                 }
116                         
117                 [MonoTODO]
118                 public static void AppActivate (System.String Title)
119                 { 
120                         throw new NotImplementedException ();
121                 }
122                         
123                 [MonoTODO]
124                 public static System.String InputBox (System.String Prompt, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue("")] System.String Title, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue("")] System.String DefaultResponse, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(-1)] System.Int32 XPos, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(-1)] System.Int32 YPos)
125                 { 
126                         throw new NotImplementedException ();
127                 }
128                         
129                 public static System.Object IIf (System.Boolean Expression, System.Object TruePart, System.Object FalsePart)
130                 {
131                         return Expression ? TruePart : FalsePart;
132                 }
133                         
134                 public static System.String Partition (System.Int64 number, System.Int64 start, System.Int64 stop, System.Int64 interval)
135                 { 
136                         String stopStr = "";
137                         String startStr = "";
138                         long startNumber = 0;
139                         int spacesCount = 0;
140                         long endNumber = 0;
141
142                         if (start < 0)
143                                 throw new ArgumentException(
144                                                             Utils.GetResourceString("Argument_InvalidValue1", "Start"));
145                         if (stop <= start)
146                                 throw new ArgumentException(
147                                                             Utils.GetResourceString("Argument_InvalidValue1", "Stop"));
148                         if (interval < 1)
149                                 throw new ArgumentException(
150                                                             Utils.GetResourceString("Argument_InvalidValue1", "Interval"));
151
152                         if (number < start)
153                                 endNumber = start - 1;
154                         else {
155                                 if (number > stop)
156                                         startNumber = stop + 1;
157                                 else {
158                                         if (interval == 1){
159                                                 startNumber = number;
160                                                 endNumber = number;
161                                         }
162                                         else {
163                                                 endNumber = start-1;
164                                                 while (endNumber < number)
165                                                         endNumber += interval;
166                                                 startNumber = endNumber - interval + 1;
167
168                                                 if (endNumber > stop)
169                                                         endNumber = stop;
170                                                 if (startNumber < start)
171                                                         startNumber = start;
172                                         }
173                                 }
174                         }
175                         
176                         startStr = startNumber.ToString();
177                         stopStr = endNumber.ToString();
178
179                         if (stopStr.Length  > startStr.Length)
180                                 spacesCount = stopStr.Length;
181                         else
182                                 spacesCount = startStr.Length;
183         
184                         return startStr.PadLeft(spacesCount) + ":" + stopStr.PadRight(spacesCount);
185                 }
186                         
187                 public static System.Object Switch (params System.Object[] VarExpr)
188                 { 
189                         int counter;
190                         int index;
191
192                         if (VarExpr == null)
193                                 return null;
194
195                         counter = VarExpr.Length;
196                         index = 0;
197
198                         if (counter % 2 != 0)
199                                 throw new ArgumentException(
200                                                             Utils.GetResourceString("Argument_InvalidValue1", "VarExpr"));
201
202                         do {
203                                 if((bool)VarExpr[index])
204                                         return VarExpr[index + 1];
205                                 index += 2;
206                                 counter = counter - 2;
207                         }
208                         while (counter > 0);
209
210                         return null;
211                 }
212                         
213                 [MonoTODO]
214                 public static void DeleteSetting (System.String AppName, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(null)] System.String Section, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(null)] System.String Key)
215                 { 
216                         throw new NotImplementedException ();
217                 }
218                         
219                 [MonoTODO]
220                 public static System.String[,] GetAllSettings (System.String AppName, System.String Section)
221                 { 
222                         throw new NotImplementedException ();
223                 }
224                         
225                 [MonoTODO]
226                 public static System.String GetSetting (System.String AppName, System.String Section, System.String Key, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue("")] System.String Default)
227                 { 
228                         throw new NotImplementedException ();
229                 }
230                         
231                 [MonoTODO]
232                 public static void SaveSetting (System.String AppName, System.String Section, System.String Key, System.String Setting)
233                 { 
234                         throw new NotImplementedException ();
235                 }
236                         
237                 [MonoTODO]
238                 public static System.Object CreateObject (System.String ProgId, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue("")] System.String ServerName)
239                 { 
240                         throw new NotImplementedException ();
241                 }
242                         
243                 [MonoTODO]
244                 public static System.Object GetObject ([System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(null)] System.String PathName, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(null)] System.String Class)
245                 { 
246                         throw new NotImplementedException ();
247                 }
248                 
249
250                 public static Object CallByName (Object objRef, String name, CallType userCallType, Object[] args)
251                 {
252                         Object retVal = null;
253                         Type[] argsType = null;
254
255
256                         if(args != null && args.Length != 0) {
257                                 argsType = new Type[args.Length];
258
259                                 for(int i = 0; i < args.Length; i++) 
260                                         argsType[i] = args[i].GetType();
261                         }
262
263                         Type objType = objRef.GetType();
264         
265                         try
266                         {
267                                 MethodInfo methodInfo = null;
268
269                                 if(userCallType == CallType.Method) {
270                                         Console.WriteLine("Method");
271                                         methodInfo = objType.GetMethod(name, argsType);
272                                 }
273                                 else if(userCallType == CallType.Get) {
274                                         Console.WriteLine("GetMethod");
275                                         methodInfo = objType.GetProperty(name).GetGetMethod();
276                                 }
277                                 else if(userCallType == CallType.Set) {
278
279                                         Console.WriteLine("SetMethod");
280                                         methodInfo = objType.GetProperty(name).GetSetMethod();
281                                 }
282
283                                 return methodInfo.Invoke(objRef, args);
284
285                         }
286                         catch (Exception exp)
287                         {
288                                 throw new ArgumentException();
289                         }
290
291                 }
292
293
294                 public static System.Object Choose (System.Double Index, System.Object[] Choice)
295                 { 
296                         int i;
297
298                         i = (int) Math.Round(Conversion.Fix(Index) - 1.0);
299                         if(Choice.Rank != 1) 
300                                 throw new ArgumentException(Utils.GetResourceString("Argument_RankEQOne1", "Choice"));
301         
302                         if(i < 0 || i > Choice.GetUpperBound(0)) 
303                                 return null;
304                         else
305                                 return Choice[i];
306                 }
307
308
309                 public static System.String Environ (System.Int32 Expression)
310                 { 
311                         int index = 0;
312                         Exception e;
313
314                         //              Console.WriteLine("Coming Here"+Expression);
315
316                         IDictionary envVars = Environment.GetEnvironmentVariables();
317
318                         foreach(DictionaryEntry de in envVars) {
319                                 if(++index == Expression) {
320                                         if( (object) de.Value == null)
321                                                 return "";
322                                         else
323                                                 return String.Concat(de.Key, "=" , de.Value);
324                                 }
325                         }
326                         //              Console.WriteLine("Exiting the loop");
327
328                         return "";
329
330                 }
331                         
332                 public static System.String Environ (System.String Expression)
333                 { 
334                         Exception e;
335                         if (Expression == null) {
336                                 e = ExceptionUtils.VbMakeExceptionEx(5, Utils.GetResourceString("Argument_InvalidValue1", Expression));
337                                 throw e;
338                         }
339                         
340                         string var = Environment.GetEnvironmentVariable (Expression);
341                         return var != null ? var : "";
342                 }
343
344                 public static void Beep ()
345                 { 
346                         Console.WriteLine("\a");
347                 }
348
349
350                 public static System.String Command ()
351                 { 
352                         string [] args = Environment.GetCommandLineArgs ();
353
354                         if (args != null && args.Length > 1) {
355                                 return string.Join (" ", args, 2, args.Length - 1);
356                         } else {
357                                 return "";
358                         }
359                 }
360
361                 [MonoTODO]
362                 public static Microsoft.VisualBasic.MsgBoxResult MsgBox (System.Object Prompt, 
363                                                                          [System.Runtime.InteropServices.Optional] 
364                                                                          [System.ComponentModel.DefaultValue(0)] 
365                                                                          Microsoft.VisualBasic.MsgBoxStyle Buttons, 
366                                                                          [System.Runtime.InteropServices.Optional] 
367                                                                          [System.ComponentModel.DefaultValue(null)] System.Object Title)
368                 { 
369                         throw new NotImplementedException ();
370                         /*      //MessageButtons msgBoxButtons = 0;
371                                 MessageBoxIcon msgBoxIcon = 0;
372                                 MessageBoxDefaultButton msgBoxDefaultButton = 0;
373                                 MessageBoxOptions msgBoxOptions = 0;
374                         
375
376                                 int IconsMask = MsgBoxStyle.Critical | MsgBoxStyle.Question | MsgBoxStyle.Exclamation | MsgBoxStyle.Information;
377
378                                 int ButtonsMask = MsgBoxStyle.OKOnly |MsgBoxStyle.OKCancel | MsgBoxStyle.AbortRetryIgnore |
379                                 MsgBoxStyle.YesNoCancel |
380                                 MsgBoxStyle.YesNo | MsgBoxStyle.RetryCancel;
381
382                                 int DefaultButtonMask = MsgBoxStyle.DeafultButton1 | MsgBoxStyle.DefaultButton2 | 
383                                 MsgBoxStyle.DefaultButton3;
384
385                                 int OptionsMask =  MsgBoxStyle.MsgBoxRight | MsgBoxStyle.MsgBoxRtlReading;
386
387
388                                 switch(Buttons & IconMask) {
389                                 case MsgBoxStyle.OKOnly:
390                                 msgBoxButtons = MessageBoxButtons.OK;
391                                 break;
392
393                                 case MsgBoxStyle.OKCancel:
394                                 msgBoxButtons = MessageBoxButtons.OK;
395                                 break;
396
397                                 case MsgBoxStyle.AbortRetryIgnore:
398                                 msgBoxButtons = MessageBoxButtons.OKCancel;
399                                 break;
400
401                                 case MsgBoxStyle.YesNoCancel:
402                                 msgBoxButtons = MessageBoxButtons.YesNoCancel;
403                                 break;
404
405                                 case MsgBoxStyle.YesNo:
406                                 msgBoxButtons = MessageBoxButtons.YesNo;
407                                 break;
408
409                                 case MsgBoxStyle.RetryCancel:
410                                 msgBoxButtons = MessageBoxButtons.RetryCancel;
411                                 break;
412
413                                 default:
414                                 // handle error
415                                 break;
416                                 }
417
418
419
420                                 switch(Buttons & IconMask) {
421
422                                 case MsgBoxStyle.Critical:
423                                 msgBoxIcon = MessageBoxIcon.Error;
424                                 break;
425
426                                 case MsgBoxStyle.Question:
427                                 msgBoxIcon = MessageBoxIcon.Question;
428                                 break;
429
430                                 case MsgBoxStyle.Exclamation:
431                                 msgBoxIcon = MessageBoxIcon.Exclamation;
432                                 break;
433
434                                 case MsgBoxStyle.Information:
435                                 msgBoxIcon = MessageBoxIcon.Information;
436                                 break;
437
438                                 default:
439                                 // handle error
440                                 break;
441                                 }
442
443                                 switch(Buttons & DefaultButtonMask) {
444                                 case MsgBoxStyle.DefaultButton1:
445                                 msgBoxDefaultButton = MessageBoxDefaultButton.Button1;
446                                 break;
447                                 case MsgBoxStyle.DefaultButton2:
448                                 msgBoxDefaultButton = MessageBoxDefaultButton.Button2;
449                                 break;
450                                 case MsgBoxStyle.DefaultButton3:
451                                 msgBoxDefaultButton = MessageBoxDefaultButton.Button3;
452                                 break;
453                                 default:
454                                 //handle error
455                                 break;
456                                 }
457
458                                 switch(Buttons & OptionsMask) {
459                                 default:
460                                 break;
461                                 
462
463                         
464                                 }       */      
465                 }
466         }
467 }
468