664dc49fde13d9e86eeec485846187bc53400fcb
[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 System.Runtime.InteropServices;
41 using Microsoft.VisualBasic.CompilerServices;
42
43 #if ONLY_1_1
44 using DefaultParameterValueAttribute = Microsoft.VisualBasic.CompilerServices.__DefaultParameterValueAttribute;
45 #endif
46
47 //using Windows.Drawing;
48 //using System.Windows.Forms;
49
50 namespace Microsoft.VisualBasic {
51         [StandardModule] 
52         [StructLayoutAttribute(LayoutKind.Auto)] 
53         sealed public class Interaction {
54
55                 private Interaction (){}
56
57                 // Declarations
58                 // Constructors
59                 // Properties
60                 // Methods
61                 //[MonoTODO]
62                 public static int Shell (string Pathname, 
63                                          [Optional, __DefaultParameterValue(2)] AppWinStyle Style, 
64                                          [Optional, DefaultParameterValue(false)] bool Wait, 
65                                          [Optional, DefaultParameterValue(-1)] int Timeout)
66                 {
67                         Process prcs = new Process();
68
69                         ProcessWindowStyle PWinStyle = 0;
70                         switch (Style){
71                         case AppWinStyle.Hide:
72                                 PWinStyle = ProcessWindowStyle.Hidden;
73                                 break;
74                         case AppWinStyle.NormalFocus:
75                                 PWinStyle = ProcessWindowStyle.Normal;
76                                 break;
77                         case AppWinStyle.MinimizedFocus:
78                                 PWinStyle = ProcessWindowStyle.Minimized;
79                                 break;
80                         case AppWinStyle.MaximizedFocus:
81                                 PWinStyle = ProcessWindowStyle.Maximized;
82                                 break;
83                         case AppWinStyle.NormalNoFocus:
84                                 PWinStyle = ProcessWindowStyle.Normal; //ToDo: no focus is not set
85                                 break;
86                         case AppWinStyle.MinimizedNoFocus:
87                                 PWinStyle = ProcessWindowStyle.Minimized; //ToDo: no focus is not set
88                                 break;
89                         }
90
91                         prcs.StartInfo.FileName = Pathname;
92                         prcs.StartInfo.WindowStyle = PWinStyle;
93
94                         try     
95                         {
96                                 if(prcs.Start()) 
97                                 {
98                                         if (Wait)
99                                         {
100                                                 if (Timeout == -1)
101                                                         prcs.WaitForExit();
102                                                 else
103                                                         prcs.WaitForExit(Timeout);
104                                         }
105                                         return prcs.Id;
106                                 }
107                                 else
108                                         return 0;
109                         }
110                         catch (System.ComponentModel.Win32Exception e){
111                                 throw new System.IO.FileNotFoundException (
112                                                                            Utils.GetResourceString(53));
113                         }
114                 }
115                         
116                 [MonoTODO]
117                 public static void AppActivate (System.Int32 ProcessId)
118                 { 
119                         throw new NotImplementedException ();
120                 }
121                         
122                 [MonoTODO]
123                 public static void AppActivate (System.String Title)
124                 { 
125                         throw new NotImplementedException ();
126                 }
127                         
128                 [MonoTODO]
129                 public static System.String InputBox (System.String Prompt, 
130                                                       [Optional, DefaultParameterValue("")] System.String Title, 
131                                                       [Optional, DefaultParameterValue("")] System.String DefaultResponse, 
132                                                       [Optional, DefaultParameterValue(-1)] System.Int32 XPos, 
133                                                       [Optional, DefaultParameterValue(-1)] System.Int32 YPos)
134                 { 
135                         throw new NotImplementedException ();
136                 }
137                         
138                 public static System.Object IIf (System.Boolean Expression, System.Object TruePart, System.Object FalsePart)
139                 {
140                         return Expression ? TruePart : FalsePart;
141                 }
142                         
143                 public static System.String Partition (System.Int64 number, System.Int64 start, System.Int64 stop, System.Int64 interval)
144                 { 
145                         String stopStr = "";
146                         String startStr = "";
147                         long startNumber = 0;
148                         int spacesCount = 0;
149                         long endNumber = 0;
150
151                         if (start < 0)
152                                 throw new ArgumentException(
153                                                             Utils.GetResourceString("Argument_InvalidValue1", "Start"));
154                         if (stop <= start)
155                                 throw new ArgumentException(
156                                                             Utils.GetResourceString("Argument_InvalidValue1", "Stop"));
157                         if (interval < 1)
158                                 throw new ArgumentException(
159                                                             Utils.GetResourceString("Argument_InvalidValue1", "Interval"));
160
161                         if (number < start)
162                                 endNumber = start - 1;
163                         else {
164                                 if (number > stop)
165                                         startNumber = stop + 1;
166                                 else {
167                                         if (interval == 1){
168                                                 startNumber = number;
169                                                 endNumber = number;
170                                         }
171                                         else {
172                                                 endNumber = start-1;
173                                                 while (endNumber < number)
174                                                         endNumber += interval;
175                                                 startNumber = endNumber - interval + 1;
176
177                                                 if (endNumber > stop)
178                                                         endNumber = stop;
179                                                 if (startNumber < start)
180                                                         startNumber = start;
181                                         }
182                                 }
183                         }
184                         
185                         startStr = startNumber.ToString();
186                         stopStr = endNumber.ToString();
187
188                         if (stopStr.Length  > startStr.Length)
189                                 spacesCount = stopStr.Length;
190                         else
191                                 spacesCount = startStr.Length;
192         
193                         return startStr.PadLeft(spacesCount) + ":" + stopStr.PadRight(spacesCount);
194                 }
195                         
196                 public static System.Object Switch (params System.Object[] VarExpr)
197                 { 
198                         int counter;
199                         int index;
200
201                         if (VarExpr == null)
202                                 return null;
203
204                         counter = VarExpr.Length;
205                         index = 0;
206
207                         if (counter % 2 != 0)
208                                 throw new ArgumentException(
209                                                             Utils.GetResourceString("Argument_InvalidValue1", "VarExpr"));
210
211                         do {
212                                 if((bool)VarExpr[index])
213                                         return VarExpr[index + 1];
214                                 index += 2;
215                                 counter = counter - 2;
216                         }
217                         while (counter > 0);
218
219                         return null;
220                 }
221                         
222                 [MonoTODO]
223                 public static void DeleteSetting (System.String AppName, 
224                                                   [Optional, DefaultParameterValue(null)] System.String Section, 
225                                                   [Optional, DefaultParameterValue(null)] System.String Key)
226                 { 
227                         throw new NotImplementedException ();
228                 }
229                         
230                 [MonoTODO]
231                 public static System.String[,] GetAllSettings (System.String AppName, System.String Section)
232                 { 
233                         throw new NotImplementedException ();
234                 }
235                         
236                 [MonoTODO]
237                 public static System.String GetSetting (System.String AppName, 
238                                                         System.String Section, 
239                                                         System.String Key, 
240                                                         [Optional, DefaultParameterValue("")] System.String Default)
241                 { 
242                         throw new NotImplementedException ();
243                 }
244                         
245                 [MonoTODO]
246                 public static void SaveSetting (System.String AppName, System.String Section, System.String Key, System.String Setting)
247                 { 
248                         throw new NotImplementedException ();
249                 }
250                         
251                 [MonoTODO]
252                 public static System.Object CreateObject (System.String ProgId, 
253                                                           [Optional, DefaultParameterValue("")] System.String ServerName)
254                 { 
255                         throw new NotImplementedException ();
256                 }
257                         
258                 [MonoTODO]
259                 public static System.Object GetObject ([Optional, DefaultParameterValue(null)] System.String PathName, 
260                                                        [Optional, DefaultParameterValue(null)] System.String Class)
261                 { 
262                         throw new NotImplementedException ();
263                 }
264                 
265
266                 public static Object CallByName (Object objRef, String name, CallType userCallType, params Object[] args)
267                 {
268                         Type[] argsType = null;
269                         if (userCallType != CallType.Method &&
270                             userCallType != CallType.Get &&
271                             userCallType != CallType.Set &&
272                             userCallType != CallType.Let)
273                                 throw new ArgumentException (Utils.GetResourceString("Argument_InvalidValue1", "CallType"));
274
275                         if(args != null) {
276                                 argsType = new Type[args.Length];
277
278                                 for(int i = 0; i < args.Length; i++) 
279                                         argsType[i] = args[i].GetType();
280                         }
281                         /*else
282                                 argsType = new Type[0];*/
283
284                         Type objType = objRef.GetType();
285         
286                         try
287                         {
288                                 MethodInfo methodInfo = null;
289
290                                 if(userCallType == CallType.Method) {
291                                         //Console.WriteLine("Method");
292                                         methodInfo = objType.GetMethod(name, argsType);
293                                 }
294                                 else if(userCallType == CallType.Get) {
295                                         //Console.WriteLine("GetMethod");
296                                         methodInfo = objType.GetProperty(name).GetGetMethod();
297                                 }
298                                 else if(userCallType == CallType.Set) {
299
300                                         //Console.WriteLine("SetMethod");
301                                         methodInfo = objType.GetProperty(name).GetSetMethod();
302                                 }
303
304                                 return methodInfo.Invoke(objRef, args);
305
306                         }
307                         catch (Exception exp)
308                         {
309                                 throw new ArgumentException();
310                         }
311
312                 }
313
314
315                 public static System.Object Choose (System.Double Index, params System.Object[] Choice)
316                 { 
317                         int i;
318
319                         i = (int) Math.Round(Conversion.Fix(Index) - 1.0);
320                         if(Choice.Rank != 1) 
321                                 throw new ArgumentException(Utils.GetResourceString("Argument_RankEQOne1", "Choice"));
322         
323                         if(i < 0 || i > Choice.GetUpperBound(0)) 
324                                 return null;
325                         else
326                                 return Choice[i];
327                 }
328
329
330                 public static System.String Environ (System.Int32 Expression)
331                 { 
332                         int index = 0;
333
334                         //              Console.WriteLine("Coming Here"+Expression);
335
336                         IDictionary envVars = Environment.GetEnvironmentVariables();
337
338                         foreach(DictionaryEntry de in envVars) {
339                                 if(++index == Expression) {
340                                         if( (object) de.Value == null)
341                                                 return "";
342                                         else
343                                                 return String.Concat(de.Key, "=" , de.Value);
344                                 }
345                         }
346                         //              Console.WriteLine("Exiting the loop");
347
348                         return "";
349
350                 }
351                         
352                 public static System.String Environ (System.String Expression)
353                 { 
354                         Exception e;
355                         if (Expression == null) {
356                                 e = ExceptionUtils.VbMakeExceptionEx(5, Utils.GetResourceString("Argument_InvalidValue1", Expression));
357                                 throw e;
358                         }
359                         
360                         string var = Environment.GetEnvironmentVariable (Expression);
361                         return var != null ? var : "";
362                 }
363
364                 public static void Beep ()
365                 { 
366                         Console.WriteLine("\a");
367                 }
368
369
370                 public static System.String Command ()
371                 { 
372                         string [] args = Environment.GetCommandLineArgs ();
373
374                         if (args != null && args.Length > 1) {
375                                 return string.Join (" ", args, 1, args.Length - 1);
376                         } else {
377                                 return "";
378                         }
379                 }
380
381                 [MonoTODO]
382                 public static MsgBoxResult MsgBox (object Prompt, 
383                                                    [Optional, __DefaultParameterValue(0)]MsgBoxStyle Buttons, 
384                                                    [Optional, DefaultParameterValue(null)] object Title)
385                 { 
386                         throw new NotImplementedException ();
387                         /*      //MessageButtons msgBoxButtons = 0;
388                                 MessageBoxIcon msgBoxIcon = 0;
389                                 MessageBoxDefaultButton msgBoxDefaultButton = 0;
390                                 MessageBoxOptions msgBoxOptions = 0;
391                         
392
393                                 int IconsMask = MsgBoxStyle.Critical | MsgBoxStyle.Question | MsgBoxStyle.Exclamation | MsgBoxStyle.Information;
394
395                                 int ButtonsMask = MsgBoxStyle.OKOnly |MsgBoxStyle.OKCancel | MsgBoxStyle.AbortRetryIgnore |
396                                 MsgBoxStyle.YesNoCancel |
397                                 MsgBoxStyle.YesNo | MsgBoxStyle.RetryCancel;
398
399                                 int DefaultButtonMask = MsgBoxStyle.DeafultButton1 | MsgBoxStyle.DefaultButton2 | 
400                                 MsgBoxStyle.DefaultButton3;
401
402                                 int OptionsMask =  MsgBoxStyle.MsgBoxRight | MsgBoxStyle.MsgBoxRtlReading;
403
404
405                                 switch(Buttons & IconMask) {
406                                 case MsgBoxStyle.OKOnly:
407                                 msgBoxButtons = MessageBoxButtons.OK;
408                                 break;
409
410                                 case MsgBoxStyle.OKCancel:
411                                 msgBoxButtons = MessageBoxButtons.OK;
412                                 break;
413
414                                 case MsgBoxStyle.AbortRetryIgnore:
415                                 msgBoxButtons = MessageBoxButtons.OKCancel;
416                                 break;
417
418                                 case MsgBoxStyle.YesNoCancel:
419                                 msgBoxButtons = MessageBoxButtons.YesNoCancel;
420                                 break;
421
422                                 case MsgBoxStyle.YesNo:
423                                 msgBoxButtons = MessageBoxButtons.YesNo;
424                                 break;
425
426                                 case MsgBoxStyle.RetryCancel:
427                                 msgBoxButtons = MessageBoxButtons.RetryCancel;
428                                 break;
429
430                                 default:
431                                 // handle error
432                                 break;
433                                 }
434
435
436
437                                 switch(Buttons & IconMask) {
438
439                                 case MsgBoxStyle.Critical:
440                                 msgBoxIcon = MessageBoxIcon.Error;
441                                 break;
442
443                                 case MsgBoxStyle.Question:
444                                 msgBoxIcon = MessageBoxIcon.Question;
445                                 break;
446
447                                 case MsgBoxStyle.Exclamation:
448                                 msgBoxIcon = MessageBoxIcon.Exclamation;
449                                 break;
450
451                                 case MsgBoxStyle.Information:
452                                 msgBoxIcon = MessageBoxIcon.Information;
453                                 break;
454
455                                 default:
456                                 // handle error
457                                 break;
458                                 }
459
460                                 switch(Buttons & DefaultButtonMask) {
461                                 case MsgBoxStyle.DefaultButton1:
462                                 msgBoxDefaultButton = MessageBoxDefaultButton.Button1;
463                                 break;
464                                 case MsgBoxStyle.DefaultButton2:
465                                 msgBoxDefaultButton = MessageBoxDefaultButton.Button2;
466                                 break;
467                                 case MsgBoxStyle.DefaultButton3:
468                                 msgBoxDefaultButton = MessageBoxDefaultButton.Button3;
469                                 break;
470                                 default:
471                                 //handle error
472                                 break;
473                                 }
474
475                                 switch(Buttons & OptionsMask) {
476                                 default:
477                                 break;
478                                 
479
480                         
481                                 }       */      
482                 }
483         }
484 }
485