* FileDialog.cs : more implementation added
authorAleksey Ryabchuk <aleksey@mono-cvs.ximian.com>
Tue, 20 May 2003 16:32:37 +0000 (16:32 -0000)
committerAleksey Ryabchuk <aleksey@mono-cvs.ximian.com>
Tue, 20 May 2003 16:32:37 +0000 (16:32 -0000)
* win32Enums.cs : added some error codes  for common dialogs
* win32functions.cs : added prototype for CommDlgExtendedError

svn path=/trunk/mcs/; revision=14727

mcs/class/System.Windows.Forms/System.Windows.Forms/ChangeLog
mcs/class/System.Windows.Forms/System.Windows.Forms/FileDialog.cs
mcs/class/System.Windows.Forms/System.Windows.Forms/win32Enums.cs
mcs/class/System.Windows.Forms/System.Windows.Forms/win32functions.cs

index 94e7bd5fd0557627992fdd57ea901e93c8cbe47a..663fd2a0ebb55d3dc688a4003b35941fc6670466 100644 (file)
@@ -1,3 +1,8 @@
+2003-05-20  Aleksey Ryabchuk <ryabchuk@yahoo.com>
+       * FileDialog.cs : more implementation added
+       * win32Enums.cs : added some error codes  for common dialogs
+       * win32functions.cs : added prototype for CommDlgExtendedError
+
 2003-05-19  Aleksey Ryabchuk <ryabchuk@yahoo.com>
         * CommonDialog.cs
        * FileDialog.cs
index ad9c410dd84c1402a34485d88849b31f761f3db1..61cfed6a0dc5c05ef66068e6e616aa50e4d02f3e 100644 (file)
@@ -4,6 +4,7 @@
 // Author:
 //   stubbed out by Daniel Carrera (dcarrera@math.toronto.edu)
 //   Dennis Hayes (dennish@raytek.com)
+//   Aleksey Ryabchuk (ryabchuk@yahoo.com)
 // (C) 2002 Ximian, Inc
 //
 using System.ComponentModel;
@@ -16,86 +17,84 @@ namespace System.Windows.Forms {
        // </summary>
 
     public abstract class FileDialog : CommonDialog {
-               internal string fileName = "";
                internal const  int MAX_PATH = 1024*8;
-               bool checkFileExists = false;
-               //
-               //  --- Public Properties
-               //
-               [MonoTODO]
+
+               string fileName;
+               bool checkFileExists;
+               bool addExtencsion;
+               string defaultExt;
+               int filterIndex;
+               string filter;
+               bool checkPathExists;
+               bool dereferenceLinks;
+               bool showHelp;
+
+               internal FileDialog ( )
+               {
+                       setDefaults ( );
+               }
                public bool AddExtension {
-                       get {
-                               throw new NotImplementedException ();
-                       }
-                       set {
-                               //FIXME:
-                       }
+                       get { return addExtencsion;  }
+                       set { addExtencsion = value; }
                }
-               [MonoTODO]
+
                public virtual bool CheckFileExists {
                        get { return checkFileExists;  }
                        set { checkFileExists = value; }
                }
-               [MonoTODO]
+
                public bool CheckPathExists {
-                       get {
-                               throw new NotImplementedException ();
-                       }
-                       set {
-                               //FIXME:
-                       }
+                       get { return checkPathExists;   }
+                       set { checkPathExists = value;  }
                }
-               [MonoTODO]
+
                public string DefaultExt {
-                       get {
-                               throw new NotImplementedException ();
-                       }
-                       set {
-                               //FIXME:
+                       get { return defaultExt;  }
+                       set { 
+                               if ( value.StartsWith ( "." ) )
+                                       defaultExt = value.Remove( 0, 1 );
+                               else 
+                                       defaultExt = value; 
                        }
                }
-               [MonoTODO]
+
                public bool DereferenceLinks {
-                       get {
-                               throw new NotImplementedException ();
-                       }
-                       set {
-                               //FIXME:
-                       }
+                       get { return dereferenceLinks;  }
+                       set { dereferenceLinks = value; }
                }
-               [MonoTODO]
+
                public string FileName {
-                       get {
-                               return fileName;
-                       }
-                       set {
-                               fileName = value;
-                       }
+                       get { return fileName;  }
+                       set { fileName = value; }
                }
+
                [MonoTODO]
                public string[] FileNames {
                        get {
                                throw new NotImplementedException ();
                        }
                }
-               [MonoTODO]
+
                public string Filter {
-                       get {
-                               throw new NotImplementedException ();
-                       }
-                       set {
-                               throw new NotImplementedException ();
+                       get { return filter; }
+                       set { 
+                               if ( value.Length > 0 ) {
+                                       int sepNum = getSeparatorsCount ( value );
+                                       if ( ( sepNum / 2 ) * 2 == sepNum ) {
+                                               // number of separators should be odd
+                                               throw new ArgumentException ("Parameter format is invalid");
+                                       }
+                               }
+                               filter = value;
                        }
                }
-               [MonoTODO]
+
                public int FilterIndex {
-                       get {
-                               throw new NotImplementedException ();
-                       }
-                       set {
-                               throw new NotImplementedException ();
-                       }
+                       get { return filterIndex;  }
+                       set { filterIndex = value; }
                }
+
                [MonoTODO]
                public string InitialDirectory {
                        get {
@@ -114,15 +113,12 @@ namespace System.Windows.Forms {
                                throw new NotImplementedException ();
                        }
                }
-               [MonoTODO]
+
                public bool ShowHelp {
-                       get {
-                               throw new NotImplementedException ();
-                       }
-                       set {
-                               throw new NotImplementedException ();
-                       }
+                       get { return showHelp;  }
+                       set { showHelp = value; }
                }
+
                [MonoTODO]
                public string Title {
                        get {
@@ -142,15 +138,9 @@ namespace System.Windows.Forms {
                        }
                }
 
-               //
-               //  --- Public Methods
-               //
-
-
-               [MonoTODO]
                public override void Reset()
                {
-                       CheckFileExists = false;
+                       setDefaults ( );
                }
 
                [MonoTODO]
@@ -160,14 +150,7 @@ namespace System.Windows.Forms {
                        return base.ToString();
                }
 
-               //
-               // --- Public Events
-               //
-
                public event CancelEventHandler FileOk;
-               //
-               // --- Protected Methods
-               //
 
                [MonoTODO]
                protected  override IntPtr HookProc( IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam )
@@ -175,11 +158,13 @@ namespace System.Windows.Forms {
                        //FIXME:
                        return base.HookProc(hWnd, msg, wparam, lparam);
                }
-               [MonoTODO]
-               protected  void OnFileOk( CancelEventArgs e)
+
+               protected  void OnFileOk( CancelEventArgs e )
                {
-                       //FIXME:
+                       if ( FileOk != null )
+                               FileOk ( this, e );
                }
+
                [MonoTODO]
                protected  override bool RunDialog( IntPtr hWndOwner )
                {
@@ -189,8 +174,22 @@ namespace System.Windows.Forms {
                        initOpenFileName ( ref opf );
 
                        bool res = Win32.GetOpenFileName ( ref opf );
-                       if ( res ) {
+                       if ( res )
                                FileName = opf.lpstrFile;
+                       else {
+                               uint error = Win32.CommDlgExtendedError ( );
+                               if ( error != 0 ) {
+                                       string errorMes = string.Empty;
+                                       switch ( error ) {
+                                       case (uint)CommDlgErrors.FNERR_BUFFERTOOSMALL:
+                                               errorMes = "Too many files selected. Please select fewer files and try again.";
+                                       break;
+                                       case (uint)CommDlgErrors.FNERR_INVALIDFILENAME:
+                                               errorMes = "A file name is invalid.";
+                                       break;
+                                       }
+                                       throw new InvalidOperationException( errorMes );
+                               }
                        }
                        return res;
                }
@@ -203,6 +202,60 @@ namespace System.Windows.Forms {
                        opf.nMaxFile = (uint) opf.lpstrFile.Length;
                        opf.lpfnHook = new Win32.FnHookProc ( base.HookProc );
                        opf.Flags = (int) ( OpenFileDlgFlags.OFN_ENABLEHOOK | OpenFileDlgFlags.OFN_EXPLORER );
+
+                       // convert filter to the proper format accepted by GetOpenFileName
+                       int FilterLength =  Filter.Length;
+                       if ( FilterLength > 0 ) {
+                               char[] FilterString = new char[ FilterLength + 1 ];
+                               Filter.CopyTo ( 0, FilterString, 0, FilterLength );
+                               int index = Array.IndexOf ( FilterString, '|' );
+                               while ( index != -1 ) {
+                                       FilterString [ index ] = '\0';
+                                       index = Array.IndexOf ( FilterString, '|', index );
+                               }
+                               FilterString [ FilterLength ] = '\0';
+                               opf.lpstrFilter = new string ( FilterString );
+                       }
+
+                       if ( FilterIndex >= 0 )
+                               opf.nFilterIndex = (uint)FilterIndex;
+
+                       opf.lpstrDefExt = DefaultExt;
+
+                       if ( CheckFileExists )
+                               opf.Flags |= (int) ( OpenFileDlgFlags.OFN_FILEMUSTEXIST );
+                       if ( CheckPathExists )
+                               opf.Flags |= (int) ( OpenFileDlgFlags.OFN_PATHMUSTEXIST );
+                       if ( !DereferenceLinks )
+                               opf.Flags |= (int) ( OpenFileDlgFlags.OFN_NODEREFERENCELINKS );
+                       if ( ShowHelp )
+                               opf.Flags |= (int) ( OpenFileDlgFlags.OFN_SHOWHELP );
+               }
+
+               private int getSeparatorsCount ( string filter )
+               {
+                       int sepNum = 0;
+                       int index = filter.IndexOf ( '|' );
+                       while ( index != -1 ) {
+                               sepNum ++;
+                               if ( index < filter.Length )
+                                       index ++;
+                               index = filter.IndexOf ( '|', index );
+                       }
+                       return sepNum;
+               }
+               
+               private void setDefaults ( ) 
+               {
+                       fileName = string.Empty;
+                       checkFileExists = false;
+                       addExtencsion = true;
+                       defaultExt = string.Empty;
+                       filter = string.Empty;
+                       filterIndex = 1;
+                       checkPathExists = true;
+                       dereferenceLinks = true;
+                       showHelp = false;
                }
         }
 }
index 3d98a9e54a4783b67fbe1e3d9c74a28f61dd6a98..35cec3ef201b8c027075a8baa33c3832c84fe8cf 100644 (file)
@@ -2826,4 +2826,11 @@ namespace System.Windows.Forms{
                OFN_ENABLEINCLUDENOTIFY      = 0x00400000,
                OFN_ENABLESIZING             = 0x00800000
        }
+
+       internal enum CommDlgErrors : uint {
+               FNERR_FILENAMECODES          = 0x3000,
+               FNERR_SUBCLASSFAILURE        = 0x3001,
+               FNERR_INVALIDFILENAME        = 0x3002,
+               FNERR_BUFFERTOOSMALL         = 0x3003
+       }
 }
index 692942e83d30f7dac48792829dc39c4270980f2e..f91f76407b8f7f08ea6cedcb04dad06f57079bda 100644 (file)
@@ -949,6 +949,11 @@ namespace System.Windows.Forms{
                         CharSet = CharSet.Ansi)]
                internal static extern bool GetOpenFileName ( ref OPENFILENAME lpofn );
 
+               [DllImport ("Comdlg32.dll",
+                        CallingConvention = CallingConvention.StdCall, 
+                        CharSet = CharSet.Ansi)]
+               internal static extern uint CommDlgExtendedError ( );
+
                #endregion
 
                internal static void UpdateWindowStyle( IntPtr hwnd, int RemoveStyle, int AddStyle) {