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