2005-09-19 Chris Toshok <toshok@ximian.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / FileDialog.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2004 Novell, Inc. (http://www.novell.com)
21 //
22 // Authors:
23 //
24 //  Alexander Olk       xenomorph2@onlinehome.de
25 //
26
27 // NOT COMPLETE - work in progress
28
29 // TODO:
30 // file/path security stuff ???
31
32 using System;
33 using System.Drawing;
34 using System.ComponentModel;
35 using System.Resources;
36 using System.IO;
37 using System.Collections;
38 using System.Collections.Specialized;
39
40 namespace System.Windows.Forms
41 {
42         [DefaultProperty( "FileName" )]
43         [DefaultEvent( "FileOk" )]
44         public abstract class FileDialog : CommonDialog
45         {
46                 internal class FileDialogForm : DialogForm
47                 {
48                         internal FileDialogForm( CommonDialog owner )
49                         : base( owner )
50                         {}
51                         
52                         protected override CreateParams CreateParams
53                         {
54                                 get {
55                                         CreateParams    cp;
56                                         
57                                         ControlBox = true;
58                                         MinimizeBox = false;
59                                         MaximizeBox = false;
60                                         
61                                         cp = base.CreateParams;
62                                         cp.Style = (int)( WindowStyles.WS_POPUP | WindowStyles.WS_CAPTION | WindowStyles.WS_SYSMENU | WindowStyles.WS_CLIPCHILDREN | WindowStyles.WS_CLIPSIBLINGS );
63                                         cp.Style |= (int)WindowStyles.WS_OVERLAPPEDWINDOW;
64                                         
65                                         return cp;
66                                 }
67                         }
68                 }
69                 
70                 internal enum FileDialogType
71                 {
72                         OpenFileDialog,
73                         SaveFileDialog
74                 }
75                 
76                 internal FileDialogPanel fileDialogPanel;
77                 
78                 private bool addExtension = true;
79                 internal bool checkFileExists = false;
80                 private bool checkPathExists = true;
81                 private string defaultExt = "";
82                 private bool dereferenceLinks = true;
83                 private string fileName = "";
84                 private string[] fileNames;
85                 private string filter;
86                 private int filterIndex = 1;
87                 private string initialDirectory = "";
88                 private bool restoreDirectory = false;
89                 private bool showHelp = false;
90                 private string title = "";
91                 private bool validateNames = true;
92                 
93                 //protected bool readOnlyChecked = false;
94                 
95                 internal string openSaveButtonText;
96                 internal string searchSaveLabelText;
97                 internal bool showReadOnly = false;
98                 internal bool readOnlyChecked = false;
99                 internal bool multiSelect = false;
100                 internal bool createPrompt = false;
101                 internal bool overwritePrompt = true;
102                 
103                 private bool showHiddenFiles = false;
104                 
105                 internal FileDialogType fileDialogType;
106                 
107                 internal FileDialog( ) : base()
108                 {
109                 }
110                 
111                 [DefaultValue(true)]
112                 public bool AddExtension
113                 {
114                         get {
115                                 return addExtension;
116                         }
117                         
118                         set {
119                                 addExtension = value;
120                         }
121                 }
122                 
123                 [DefaultValue(false)]
124                 public virtual bool CheckFileExists
125                 {
126                         get {
127                                 return checkFileExists;
128                         }
129                         
130                         set {
131                                 checkFileExists = value;
132                         }
133                 }
134                 
135                 [DefaultValue(true)]
136                 public bool CheckPathExists
137                 {
138                         get {
139                                 return checkPathExists;
140                         }
141                         
142                         set {
143                                 checkPathExists = value;
144                         }
145                 }
146                 
147                 [DefaultValue("")]
148                 public string DefaultExt
149                 {
150                         get {
151                                 return defaultExt;
152                         }
153                         
154                         set {
155                                 defaultExt = value;
156                                 
157                                 // if there is a dot remove it and everything before it
158                                 if ( defaultExt.LastIndexOf( '.' ) != - 1 )
159                                 {
160                                         string[] split = defaultExt.Split( new char[] { '.' } );
161                                         defaultExt = split[ split.Length - 1 ];
162                                 }
163                         }
164                 }
165                 
166                 // in MS.NET it doesn't make a difference if
167                 // DerefenceLinks is true or false
168                 // if the selected file is a link FileDialog
169                 // always returns the link
170                 [DefaultValue(true)]
171                 public bool DereferenceLinks
172                 {
173                         get {
174                                 return dereferenceLinks;
175                         }
176                         
177                         set {
178                                 dereferenceLinks = value;
179                         }
180                 }
181                 
182                 [DefaultValue("")]
183                 public string FileName
184                 {
185                         get {
186                                 return fileName;
187                         }
188                         
189                         set {
190                                 fileName = value;
191                         }
192                 }
193                 
194                 [Browsable(false)]
195                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
196                 public string[] FileNames
197                 {
198                         get {
199                                 if ( multiSelect )
200                                         return fileNames;
201                                 
202                                 return null;
203                         }
204                 }
205                 
206                 [DefaultValue("")]
207                 [Localizable(true)]
208                 public string Filter
209                 {
210                         get {
211                                 return filter;
212                         }
213                         
214                         set {
215                                 if ( value == null )
216                                         throw new NullReferenceException( "Filter" );
217                                 
218                                 filter = value;
219                                 
220                                 fileFilter = new FileFilter( filter );
221                                 
222                                 fileDialogPanel.UpdateFilters( );
223                         }
224                 }
225                 
226                 [DefaultValue(1)]
227                 public int FilterIndex
228                 {
229                         get {
230                                 return filterIndex;
231                         }
232                         
233                         set {
234                                 filterIndex = value;
235                         }
236                 }
237                 
238                 [DefaultValue("")]
239                 public string InitialDirectory
240                 {
241                         get {
242                                 return initialDirectory;
243                         }
244                         
245                         set {
246                                 initialDirectory = value;
247                         }
248                 }
249                 
250                 [DefaultValue(false)]
251                 public bool RestoreDirectory
252                 {
253                         get {
254                                 return restoreDirectory;
255                         }
256                         
257                         set {
258                                 restoreDirectory = value;
259                         }
260                 }
261                 
262                 [DefaultValue(false)]
263                 public bool ShowHelp
264                 {
265                         get {
266                                 return showHelp;
267                         }
268                         
269                         set {
270                                 showHelp = value;
271                                 fileDialogPanel.ResizeAndRelocateForHelpOrReadOnly( );
272                         }
273                 }
274                 
275                 [DefaultValue("")]
276                 [Localizable(true)]
277                 public string Title
278                 {
279                         get {
280                                 return title;
281                         }
282                         
283                         set {
284                                 title = value;
285                                 
286                                 form.Text = title;
287                         }
288                 }
289                 
290                 // this one is a hard one ;)
291                 // Win32 filename:
292                 // - up to MAX_PATH characters (windef.h) = 260
293                 // - no trailing dots or spaces
294                 // - case preserving
295                 // - etc...
296                 // NTFS/Posix filename:
297                 // - up to 32,768 Unicode characters
298                 // - trailing periods or spaces
299                 // - case sensitive
300                 // - etc...
301                 [DefaultValue(true)]
302                 public bool ValidateNames
303                 {
304                         get {
305                                 return validateNames;
306                         }
307                         
308                         set {
309                                 validateNames = value;
310                         }
311                 }
312                 
313                 internal string OpenSaveButtonText
314                 {
315                         set {
316                                 openSaveButtonText = value;
317                         }
318                         
319                         get {
320                                 return openSaveButtonText;
321                         }
322                 }
323                 
324                 internal string SearchSaveLabelText
325                 {
326                         set {
327                                 searchSaveLabelText = value;
328                         }
329                         
330                         get {
331                                 return searchSaveLabelText;
332                         }
333                 }
334                 
335                 internal virtual bool ShowReadOnly
336                 {
337                         set {
338                                 showReadOnly = value;
339                                 fileDialogPanel.ResizeAndRelocateForHelpOrReadOnly( );
340                         }
341                         
342                         get {
343                                 return showReadOnly;
344                         }
345                 }
346                 
347                 internal virtual bool ReadOnlyChecked
348                 {
349                         set {
350                                 readOnlyChecked = value;
351                                 fileDialogPanel.CheckBox.Checked = value;
352                         }
353                         
354                         get {
355                                 return readOnlyChecked;
356                         }
357                 }
358                 
359                 internal virtual bool Multiselect
360                 {
361                         set {
362                                 multiSelect = value;
363                                 fileDialogPanel.MultiSelect = value;
364                         }
365                         
366                         get {
367                                 return multiSelect;
368                         }
369                 }
370                 
371                 // extension to MS.NET framework...
372                 // Must keep this internal, otherwise our signature doesn't match MS
373                 internal bool ShowHiddenFiles
374                 {
375                         set {
376                                 showHiddenFiles = value;
377                         }
378                         
379                         get {
380                                 return showHiddenFiles;
381                         }
382                 }
383                 
384                 internal virtual bool CreatePrompt
385                 {
386                         set {
387                                 createPrompt = value;
388                         }
389                         
390                         get {
391                                 return createPrompt;
392                         }
393                 }
394                 
395                 internal virtual bool OverwritePrompt
396                 {
397                         set {
398                                 overwritePrompt = value;
399                         }
400                         
401                         get {
402                                 return overwritePrompt;
403                         }
404                 }
405                 
406                 internal FileFilter FileFilter
407                 {
408                         set {
409                                 fileFilter = value;
410                         }
411                         
412                         get {
413                                 return fileFilter;
414                         }
415                 }
416                 
417                 public override void Reset( )
418                 {
419                         addExtension = true;
420                         checkFileExists = false;
421                         checkPathExists = true;
422                         defaultExt = "";
423                         dereferenceLinks = true;
424                         fileName = "";
425                         fileNames = null;
426                         Filter = "";
427                         filterIndex = 1;
428                         initialDirectory = "";
429                         restoreDirectory = false;
430                         ShowHelp = false;
431                         Title = "";
432                         validateNames = true;
433                         
434                         fileDialogPanel.UpdateFilters( );
435                 }
436                 
437                 public override string ToString( )
438                 {
439                         return base.ToString( );
440                 }
441                 
442                 public event CancelEventHandler FileOk;
443                 
444                 protected  override IntPtr HookProc( IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam )
445                 {
446                         throw new NotImplementedException( );
447                 }
448                 
449                 protected void OnFileOk( CancelEventArgs e )
450                 {
451                         if ( FileOk != null ) FileOk( this, e );
452                 }
453                 
454                 [MonoTODO]
455                 protected  override bool RunDialog( IntPtr hWndOwner )
456                 {
457                         form.Controls.Add( fileDialogPanel );
458                         
459                         return true;
460                 }
461                 
462                 internal void SendHelpRequest( EventArgs e )
463                 {
464                         OnHelpRequest( e );
465                 }
466                 
467                 internal void SetFilenames( string[] filenames )
468                 {
469                         fileNames = filenames;
470                 }
471                 
472                 internal FileFilter fileFilter;
473                 
474                 internal class FileDialogPanel : Panel
475                 {
476                         private Button cancelButton;
477                         private ToolBarButton upToolBarButton;
478                         private PopupButtonPanel popupButtonPanel;
479                         private Button openSaveButton;
480                         private Button helpButton;
481                         private Label fileTypeLabel;
482                         private ToolBarButton menueToolBarButton;
483                         private ContextMenu menueToolBarButtonContextMenu;
484                         private ToolBarButton desktopToolBarButton;
485                         private ToolBar smallButtonToolBar;
486                         private DirComboBox dirComboBox;
487                         private ToolBarButton lastUsedToolBarButton;
488                         private ComboBox fileNameComboBox;
489                         private ToolBarButton networkToolBarButton;
490                         private Label fileNameLabel;
491                         private MWFFileView mwfFileView;
492                         private Label searchSaveLabel;
493                         private ToolBarButton newdirToolBarButton;
494                         private ToolBarButton backToolBarButton;
495                         private ToolBarButton homeToolBarButton;
496                         private ToolBarButton workplaceToolBarButton;
497                         private ComboBox fileTypeComboBox;
498                         private ImageList imageListTopToolbar;
499                         private ContextMenu contextMenu;
500                         private CheckBox checkBox;
501                         
502                         internal FileDialog fileDialog;
503                         
504                         private string currentDirectoryName;
505                         
506                         internal string currentFileName = "";
507                         
508                         // store current directoryInfo
509                         private DirectoryInfo directoryInfo;
510                         
511                         // store DirectoryInfo for backButton
512                         internal Stack directoryStack = new Stack();
513                         
514                         private MenuItem previousCheckedMenuItem;
515                         
516                         private bool multiSelect = false;
517                         
518                         private string restoreDirectory = "";
519                         
520                         public FileDialogPanel( FileDialog fileDialog )
521                         {
522                                 this.fileDialog = fileDialog;
523                                 
524                                 fileTypeComboBox = new ComboBox( );
525                                 workplaceToolBarButton = new ToolBarButton( );
526                                 homeToolBarButton = new ToolBarButton( );
527                                 backToolBarButton = new ToolBarButton( );
528                                 newdirToolBarButton = new ToolBarButton( );
529                                 searchSaveLabel = new Label( );
530                                 mwfFileView = new MWFFileView( );
531                                 fileNameLabel = new Label( );
532                                 networkToolBarButton = new ToolBarButton( );
533                                 fileNameComboBox = new ComboBox( );
534                                 lastUsedToolBarButton = new ToolBarButton( );
535                                 dirComboBox = new DirComboBox( );
536                                 smallButtonToolBar = new ToolBar( );
537                                 desktopToolBarButton = new ToolBarButton( );
538                                 menueToolBarButton = new ToolBarButton( );
539                                 fileTypeLabel = new Label( );
540                                 openSaveButton = new Button( );
541                                 fileDialog.form.AcceptButton = openSaveButton;
542                                 helpButton = new Button( );
543                                 popupButtonPanel = new PopupButtonPanel( this );
544                                 upToolBarButton = new ToolBarButton( );
545                                 cancelButton = new Button( );
546                                 fileDialog.form.CancelButton = cancelButton;
547                                 imageListTopToolbar = new ImageList( );
548                                 menueToolBarButtonContextMenu = new ContextMenu( );
549                                 contextMenu = new ContextMenu( );
550                                 checkBox = new CheckBox( );
551                                 
552                                 SuspendLayout( );
553                                 
554                                 //imageListTopToolbar
555                                 imageListTopToolbar.ColorDepth = ColorDepth.Depth32Bit;
556                                 imageListTopToolbar.ImageSize = new Size( 16, 16 ); // 16, 16
557                                 imageListTopToolbar.Images.Add( (Image)Locale.GetResource( "back_arrow" ) );
558                                 imageListTopToolbar.Images.Add( (Image)Locale.GetResource( "folder_arrow_up" ) );
559                                 imageListTopToolbar.Images.Add( (Image)Locale.GetResource( "folder_star" ) );
560                                 imageListTopToolbar.Images.Add( (Image)Locale.GetResource( "window" ) );
561                                 imageListTopToolbar.TransparentColor = Color.Transparent;
562                                 
563                                 // searchLabel
564                                 searchSaveLabel.FlatStyle = FlatStyle.System;
565                                 searchSaveLabel.Location = new Point( 7, 8 );
566                                 searchSaveLabel.Size = new Size( 72, 21 );
567                                 searchSaveLabel.TabIndex = 0;
568                                 searchSaveLabel.Text = fileDialog.SearchSaveLabelText;
569                                 searchSaveLabel.TextAlign = ContentAlignment.MiddleRight;
570                                 
571                                 // dirComboBox
572                                 dirComboBox.Anchor = ( (AnchorStyles)( ( ( AnchorStyles.Top | AnchorStyles.Left ) | AnchorStyles.Right ) ) );
573                                 dirComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
574                                 dirComboBox.Location = new Point( 99, 8 );
575                                 dirComboBox.Size = new Size( 260, 21 );
576                                 dirComboBox.TabIndex = 1;
577                                 
578                                 // smallButtonToolBar
579                                 smallButtonToolBar.Anchor = ( (AnchorStyles)( ( AnchorStyles.Top | AnchorStyles.Right ) ) );
580                                 smallButtonToolBar.Appearance = ToolBarAppearance.Flat;
581                                 smallButtonToolBar.AutoSize = false;
582                                 smallButtonToolBar.Buttons.AddRange( new ToolBarButton[] {
583                                                                             backToolBarButton,
584                                                                             upToolBarButton,
585                                                                             newdirToolBarButton,
586                                                                             menueToolBarButton} );
587                                 smallButtonToolBar.ButtonSize = new Size( 21, 16 ); // 21, 16
588                                 smallButtonToolBar.Divider = false;
589                                 smallButtonToolBar.Dock = DockStyle.None;
590                                 smallButtonToolBar.DropDownArrows = true;
591                                 smallButtonToolBar.ImageList = imageListTopToolbar;
592                                 smallButtonToolBar.Location = new Point( 372, 8 );
593                                 smallButtonToolBar.ShowToolTips = true;
594                                 smallButtonToolBar.Size = new Size( 110, 20 );
595                                 smallButtonToolBar.TabIndex = 2;
596                                 smallButtonToolBar.TextAlign = ToolBarTextAlign.Right;
597                                 
598                                 // buttonPanel
599                                 popupButtonPanel.Dock = DockStyle.None;
600                                 popupButtonPanel.Location = new Point( 7, 37 );
601                                 popupButtonPanel.TabIndex = 3;
602                                 
603                                 // mwfFileView
604                                 mwfFileView.Anchor = ( (AnchorStyles)( ( ( ( AnchorStyles.Top | AnchorStyles.Bottom ) | AnchorStyles.Left ) | AnchorStyles.Right ) ) );
605                                 mwfFileView.Location = new Point( 99, 37 );
606                                 mwfFileView.Size = new Size( 449, 282 );
607                                 mwfFileView.Columns.Add( " Name", 170, HorizontalAlignment.Left );
608                                 mwfFileView.Columns.Add( "Size ", 80, HorizontalAlignment.Right );
609                                 mwfFileView.Columns.Add( " Type", 100, HorizontalAlignment.Left );
610                                 mwfFileView.Columns.Add( " Last Access", 150, HorizontalAlignment.Left );
611                                 mwfFileView.AllowColumnReorder = true;
612                                 mwfFileView.MultiSelect = false;
613                                 mwfFileView.TabIndex = 4;
614                                 mwfFileView.FilterIndex = fileDialog.FilterIndex;
615                                 
616                                 // fileNameLabel
617                                 fileNameLabel.Anchor = ( (AnchorStyles)( ( AnchorStyles.Bottom | AnchorStyles.Left ) ) );
618                                 fileNameLabel.FlatStyle = FlatStyle.System;
619                                 fileNameLabel.Location = new Point( 102, 330 );
620                                 fileNameLabel.Size = new Size( 70, 21 );
621                                 fileNameLabel.TabIndex = 5;
622                                 fileNameLabel.Text = "Filename:";
623                                 fileNameLabel.TextAlign = ContentAlignment.MiddleLeft;
624                                 
625                                 // fileNameComboBox
626                                 fileNameComboBox.Anchor = ( (AnchorStyles)( ( ( AnchorStyles.Bottom | AnchorStyles.Left ) | AnchorStyles.Right ) ) );
627                                 fileNameComboBox.Location = new Point( 195, 330 );
628                                 fileNameComboBox.Size = new Size( 245, 21 );
629                                 fileNameComboBox.TabIndex = 6;
630                                 
631                                 // fileTypeLabel
632                                 fileTypeLabel.Anchor = ( (AnchorStyles)( ( AnchorStyles.Bottom | AnchorStyles.Left ) ) );
633                                 fileTypeLabel.FlatStyle = FlatStyle.System;
634                                 fileTypeLabel.Location = new Point( 102, 356 );
635                                 fileTypeLabel.Size = new Size( 70, 21 );
636                                 fileTypeLabel.TabIndex = 7;
637                                 fileTypeLabel.Text = "Filetype:";
638                                 fileTypeLabel.TextAlign = ContentAlignment.MiddleLeft;
639                                 
640                                 // fileTypeComboBox
641                                 fileTypeComboBox.Anchor = ( (AnchorStyles)( ( ( AnchorStyles.Bottom | AnchorStyles.Left ) | AnchorStyles.Right ) ) );
642                                 fileTypeComboBox.Location = new Point( 195, 356 );
643                                 fileTypeComboBox.Size = new Size( 245, 21 );
644                                 fileTypeComboBox.TabIndex = 8;
645                                 
646                                 // backToolBarButton
647                                 backToolBarButton.ImageIndex = 0;
648                                 backToolBarButton.Enabled = false;
649                                 backToolBarButton.Style = ToolBarButtonStyle.ToggleButton;
650                                 
651                                 // upToolBarButton
652                                 upToolBarButton.ImageIndex = 1;
653                                 upToolBarButton.Style = ToolBarButtonStyle.ToggleButton;
654                                 
655                                 // newdirToolBarButton
656                                 newdirToolBarButton.ImageIndex = 2;
657                                 newdirToolBarButton.Style = ToolBarButtonStyle.ToggleButton;
658                                 
659                                 // menueToolBarButton
660                                 menueToolBarButton.ImageIndex = 3;
661                                 menueToolBarButton.DropDownMenu = menueToolBarButtonContextMenu;
662                                 menueToolBarButton.Style = ToolBarButtonStyle.DropDownButton;
663                                 
664                                 // menueToolBarButtonContextMenu
665                                 MenuItem mi = new MenuItem( "Small Icon", new EventHandler( OnClickMenuToolBarContextMenu ) );
666                                 menueToolBarButtonContextMenu.MenuItems.Add( mi );
667                                 mi = new MenuItem( "Tiles", new EventHandler( OnClickMenuToolBarContextMenu ) );
668                                 menueToolBarButtonContextMenu.MenuItems.Add( mi );
669                                 mi = new MenuItem( "Large Icon", new EventHandler( OnClickMenuToolBarContextMenu ) );
670                                 menueToolBarButtonContextMenu.MenuItems.Add( mi );
671                                 mi = new MenuItem( "List", new EventHandler( OnClickMenuToolBarContextMenu ) );
672                                 mi.Checked = true;
673                                 previousCheckedMenuItem = mi;
674                                 menueToolBarButtonContextMenu.MenuItems.Add( mi );
675                                 mi = new MenuItem( "Details", new EventHandler( OnClickMenuToolBarContextMenu ) );
676                                 menueToolBarButtonContextMenu.MenuItems.Add( mi );
677                                 
678                                 // contextMenu
679                                 mi = new MenuItem( "Show hidden files", new EventHandler( OnClickContextMenu ) );
680                                 mi.Checked = fileDialog.ShowHiddenFiles;
681                                 mwfFileView.ShowHiddenFiles = fileDialog.ShowHiddenFiles;
682                                 contextMenu.MenuItems.Add( mi );
683                                 
684                                 // openSaveButton
685                                 openSaveButton.Anchor = ( (AnchorStyles)( ( AnchorStyles.Bottom | AnchorStyles.Right ) ) );
686                                 openSaveButton.FlatStyle = FlatStyle.System;
687                                 openSaveButton.Location = new Point( 475, 330 );
688                                 openSaveButton.Size = new Size( 72, 21 );
689                                 openSaveButton.TabIndex = 9;
690                                 openSaveButton.Text = fileDialog.OpenSaveButtonText;
691                                 openSaveButton.FlatStyle = FlatStyle.System;
692                                 
693                                 // cancelButton
694                                 cancelButton.Anchor = ( (AnchorStyles)( ( AnchorStyles.Bottom | AnchorStyles.Right ) ) );
695                                 cancelButton.FlatStyle = FlatStyle.System;
696                                 cancelButton.Location = new Point( 475, 356 );
697                                 cancelButton.Size = new Size( 72, 21 );
698                                 cancelButton.TabIndex = 10;
699                                 cancelButton.Text = "Cancel";
700                                 cancelButton.FlatStyle = FlatStyle.System;
701                                 
702                                 // helpButton
703                                 helpButton.Anchor = ( (AnchorStyles)( ( AnchorStyles.Bottom | AnchorStyles.Right ) ) );
704                                 helpButton.FlatStyle = FlatStyle.System;
705                                 helpButton.Location = new Point( 475, 350 );
706                                 helpButton.Size = new Size( 72, 21 );
707                                 helpButton.TabIndex = 11;
708                                 helpButton.Text = "Help";
709                                 helpButton.FlatStyle = FlatStyle.System;
710                                 helpButton.Hide( );
711                                 
712                                 // checkBox
713                                 checkBox.Anchor = ( (AnchorStyles)( ( ( AnchorStyles.Bottom | AnchorStyles.Left ) | AnchorStyles.Right ) ) );
714                                 checkBox.Text = "Open Readonly";
715                                 checkBox.Location = new Point( 195, 350 );
716                                 checkBox.Size = new Size( 245, 21 );
717                                 checkBox.FlatStyle = FlatStyle.System;
718                                 checkBox.TabIndex = 12;
719                                 checkBox.Hide( );
720                                 
721                                 ClientSize = new Size( 554, 405 ); // 384
722                                 
723                                 ContextMenu = contextMenu;
724                                 
725                                 Dock = DockStyle.Fill;
726                                 
727                                 Controls.Add( smallButtonToolBar );
728                                 Controls.Add( cancelButton );
729                                 Controls.Add( openSaveButton );
730                                 Controls.Add( helpButton );
731                                 Controls.Add( mwfFileView );
732                                 Controls.Add( fileTypeLabel );
733                                 Controls.Add( fileNameLabel );
734                                 Controls.Add( fileTypeComboBox );
735                                 Controls.Add( fileNameComboBox );
736                                 Controls.Add( dirComboBox );
737                                 Controls.Add( searchSaveLabel );
738                                 Controls.Add( popupButtonPanel );
739                                 Controls.Add( checkBox );
740                                 
741                                 ResumeLayout( false );
742                                 
743                                 if ( fileDialog.InitialDirectory == String.Empty )
744                                         currentDirectoryName = Environment.CurrentDirectory;
745                                 else
746                                         currentDirectoryName = fileDialog.InitialDirectory;
747                                 
748                                 directoryInfo = new DirectoryInfo( currentDirectoryName );
749                                 
750                                 dirComboBox.CurrentPath = currentDirectoryName;
751                                 
752                                 if ( fileDialog.RestoreDirectory )
753                                         restoreDirectory = currentDirectoryName;
754                                 
755                                 mwfFileView.UpdateFileView( directoryInfo );
756                                 
757                                 openSaveButton.Click += new EventHandler( OnClickOpenSaveButton );
758                                 cancelButton.Click += new EventHandler( OnClickCancelButton );
759                                 helpButton.Click += new EventHandler( OnClickHelpButton );
760                                 
761                                 smallButtonToolBar.ButtonClick += new ToolBarButtonClickEventHandler( OnClickSmallButtonToolBar );
762                                 
763                                 // Key events DONT'T work
764                                 fileNameComboBox.KeyUp += new KeyEventHandler( OnKeyUpFileNameComboBox );
765                                 
766                                 fileTypeComboBox.SelectedIndexChanged += new EventHandler( OnSelectedIndexChangedFileTypeComboBox );
767                                 
768                                 mwfFileView.SelectedFileChanged += new EventHandler( OnSelectedFileChangedFileView );
769                                 mwfFileView.DirectoryChanged += new EventHandler( OnDirectoryChangedFileView );
770                                 mwfFileView.ForceDialogEnd += new EventHandler( OnForceDialogEndFileView );
771                                 mwfFileView.SelectedFilesChanged += new EventHandler( OnSelectedFilesChangedFileView );
772                                 
773                                 dirComboBox.DirectoryChanged += new EventHandler( OnDirectoryChangedDirComboBox );
774                                 
775                                 checkBox.CheckedChanged += new EventHandler( OnCheckCheckChanged );
776                         }
777                         
778                         public ComboBox FileNameComboBox
779                         {
780                                 set {
781                                         fileNameComboBox = value;
782                                 }
783                                 
784                                 get {
785                                         return fileNameComboBox;
786                                 }
787                         }
788                         
789                         public string CurrentFileName
790                         {
791                                 set {
792                                         currentFileName = value;
793                                 }
794                                 
795                                 get {
796                                         return currentFileName;
797                                 }
798                         }
799                         
800                         public DirectoryInfo DirectoryInfo
801                         {
802                                 set {
803                                         directoryInfo = value;
804                                 }
805                                 
806                                 get {
807                                         return directoryInfo;
808                                 }
809                         }
810                         
811                         public bool MultiSelect
812                         {
813                                 set {
814                                         multiSelect = value;
815                                         mwfFileView.MultiSelect = value;
816                                 }
817                                 
818                                 get {
819                                         return multiSelect;
820                                 }
821                         }
822                         
823                         public CheckBox CheckBox
824                         {
825                                 set {
826                                         checkBox = value;
827                                 }
828                                 
829                                 get {
830                                         return checkBox;
831                                 }
832                         }
833                         
834                         void OnClickContextMenu( object sender, EventArgs e )
835                         {
836                                 MenuItem senderMenuItem = sender as MenuItem;
837                                 
838                                 if ( senderMenuItem.Index == 0 )
839                                 {
840                                         senderMenuItem.Checked = !senderMenuItem.Checked;
841                                         fileDialog.ShowHiddenFiles = senderMenuItem.Checked;
842                                         mwfFileView.ShowHiddenFiles = fileDialog.ShowHiddenFiles;
843                                         mwfFileView.UpdateFileView( directoryInfo );
844                                 }
845                         }
846                         
847                         void OnClickOpenSaveButton( object sender, EventArgs e )
848                         {
849                                 if ( !multiSelect )
850                                 {
851                                         string fileFromComboBox = fileNameComboBox.Text.Trim( );
852                                                                                                                         
853                                         if ( fileFromComboBox.Length > 0 ) {
854                                                 if (!Path.IsPathRooted (fileFromComboBox))
855                                                   fileFromComboBox = Path.Combine( currentDirectoryName, fileFromComboBox );
856                                                 
857                                                 FileInfo fileInfo = new FileInfo (fileFromComboBox);
858                                                 if (fileInfo.Exists)
859                                                   currentFileName = fileFromComboBox;
860                                                 else {
861                                                         DirectoryInfo dirInfo = new DirectoryInfo (fileFromComboBox);
862                                                         if (dirInfo.Exists) {
863                                                                                                                                 
864                                                                 PushDirectory( dirInfo );                                                               
865                                                                 directoryInfo = dirInfo.Parent;
866                                                                 currentDirectoryName = dirInfo.FullName;
867                                                                 dirComboBox.CurrentPath = currentDirectoryName;
868                                                                 mwfFileView.UpdateFileView( dirInfo );
869                                                                 
870                                                                 currentFileName = "";                                                           
871                                                                 return;
872                                                         }                                                               
873                                                 }
874                                         }
875                                         
876                                         if ( currentFileName != fileFromComboBox )
877                                                 currentFileName = fileFromComboBox;
878                                         
879                                         if ( currentFileName.Length == 0 )
880                                                 return;
881                                         
882
883                                         if ( fileDialog.fileDialogType == FileDialogType.OpenFileDialog )
884                                         {
885                                                 if ( fileDialog.CheckFileExists )
886                                                 {
887                                                         if ( !File.Exists( currentFileName ) )
888                                                         {
889                                                                 string message = currentFileName + " doesn't exist. Please verify that you have entered the correct file name.";
890                                                                 MessageBox.Show( message, fileDialog.OpenSaveButtonText, MessageBoxButtons.OK, MessageBoxIcon.Warning );
891                                                                 
892                                                                 currentFileName = "";
893                                                                 
894                                                                 return;
895                                                         }
896                                                 }
897                                         }
898                                         else // FileDialogType == SaveFileDialog
899                                         {
900                                                 if ( fileDialog.OverwritePrompt )
901                                                 {
902                                                         if ( File.Exists( currentFileName ) )
903                                                         {
904                                                                 string message = currentFileName + " exists. Overwrite ?";
905                                                                 DialogResult dr = MessageBox.Show( message, fileDialog.OpenSaveButtonText, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning );
906                                                                 
907                                                                 if ( dr == DialogResult.Cancel )
908                                                                 {
909                                                                         currentFileName = "";
910                                                                         
911                                                                         return;
912                                                                 }
913                                                         }
914                                                 }
915                                                 
916                                                 if ( fileDialog.CreatePrompt )
917                                                 {
918                                                         if ( !File.Exists( currentFileName ) )
919                                                         {
920                                                                 string message = currentFileName + " doesn't exist. Create ?";
921                                                                 DialogResult dr = MessageBox.Show( message, fileDialog.OpenSaveButtonText, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning );
922                                                                 
923                                                                 if ( dr == DialogResult.Cancel )
924                                                                 {
925                                                                         currentFileName = "";
926                                                                         
927                                                                         return;
928                                                                 }
929                                                         }
930                                                 }
931                                         }
932                                         
933                                         if ( fileDialog.fileDialogType == FileDialogType.SaveFileDialog )
934                                         {
935                                                 if ( fileDialog.AddExtension && fileDialog.DefaultExt.Length > 0 )
936                                                 {
937                                                         if ( !currentFileName.EndsWith( fileDialog.DefaultExt ) )
938                                                         {
939                                                                 currentFileName += "." + fileDialog.DefaultExt;
940                                                         }
941                                                 }
942                                         }
943                                         
944                                         fileDialog.FileName = currentFileName;
945                                 }
946                                 else // multiSelect = true
947                                 if ( fileDialog.fileDialogType != FileDialogType.SaveFileDialog )
948                                 {
949                                         if ( mwfFileView.SelectedItems.Count > 0 )
950                                         {
951                                                 // first remove all selected directories
952                                                 ArrayList al = new ArrayList( );
953                                                 
954                                                 foreach ( ListViewItem lvi in mwfFileView.SelectedItems )
955                                                 {
956                                                         FileStruct fileStruct = (FileStruct)mwfFileView.FileHashtable[ lvi.Text ];
957                                                         
958                                                         if ( fileStruct.attributes != FileAttributes.Directory )
959                                                         {
960                                                                 al.Add( fileStruct );
961                                                         }
962                                                 }
963                                                 
964                                                 fileDialog.FileName = ( (FileStruct)al[ 0 ] ).fullname;
965                                                 
966                                                 string[] filenames = new string[ al.Count ];
967                                                 
968                                                 for ( int i = 0; i < al.Count; i++ )
969                                                 {
970                                                         filenames[ i ] = ( (FileStruct)al[ i ] ).fullname;
971                                                 }
972                                                 
973                                                 fileDialog.SetFilenames( filenames );
974                                         }
975                                 }
976                                 
977                                 if ( fileDialog.CheckPathExists )
978                                 {
979                                         if ( !Directory.Exists( currentDirectoryName ) )
980                                         {
981                                                 string message = currentDirectoryName + " doesn't exist. Please verify that you have entered the correct directory name.";
982                                                 MessageBox.Show( message, fileDialog.OpenSaveButtonText, MessageBoxButtons.OK, MessageBoxIcon.Warning );
983                                                 
984                                                 if ( fileDialog.InitialDirectory == String.Empty )
985                                                         currentDirectoryName = Environment.CurrentDirectory;
986                                                 else
987                                                         currentDirectoryName = fileDialog.InitialDirectory;
988                                                 
989                                                 return;
990                                         }
991                                 }
992                                 
993                                 if ( fileDialog.RestoreDirectory )
994                                         currentDirectoryName = restoreDirectory;
995                                 
996                                 CancelEventArgs cancelEventArgs = new CancelEventArgs( );
997                                 
998                                 cancelEventArgs.Cancel = false;
999                                 
1000                                 fileDialog.OnFileOk( cancelEventArgs );
1001                                 
1002                                 fileDialog.form.Controls.Remove( this );
1003                                 fileDialog.form.DialogResult = DialogResult.OK;
1004                         }
1005                         
1006                         void OnClickCancelButton( object sender, EventArgs e )
1007                         {
1008                                 if ( fileDialog.RestoreDirectory )
1009                                         currentDirectoryName = restoreDirectory;
1010                                 
1011                                 CancelEventArgs cancelEventArgs = new CancelEventArgs( );
1012                                 
1013                                 cancelEventArgs.Cancel = true;
1014                                 
1015                                 fileDialog.OnFileOk( cancelEventArgs );
1016                                 
1017                                 fileDialog.form.Controls.Remove( this );
1018                                 fileDialog.form.DialogResult = DialogResult.Cancel;
1019                         }
1020                         
1021                         void OnClickHelpButton( object sender, EventArgs e )
1022                         {
1023                                 fileDialog.SendHelpRequest( e );
1024                         }
1025                         
1026                         void OnClickSmallButtonToolBar( object sender, ToolBarButtonClickEventArgs e )
1027                         {
1028                                 if ( e.Button == upToolBarButton )
1029                                 {
1030                                         if ( directoryInfo.Parent != null )
1031                                         {
1032                                                 PushDirectory( directoryInfo );
1033                                                 
1034                                                 directoryInfo = directoryInfo.Parent;
1035                                                 
1036                                                 currentDirectoryName = directoryInfo.FullName;
1037                                                 
1038                                                 dirComboBox.CurrentPath = currentDirectoryName;
1039                                                 
1040                                                 mwfFileView.UpdateFileView( directoryInfo );
1041                                         }
1042                                 }
1043                                 else
1044                                 if ( e.Button == backToolBarButton )
1045                                 {
1046                                         if ( directoryStack.Count != 0 )
1047                                         {
1048                                                 PopDirectory( );
1049                                                 
1050                                                 dirComboBox.CurrentPath = currentDirectoryName;
1051                                                 
1052                                                 mwfFileView.UpdateFileView( directoryInfo );
1053                                         }
1054                                 }
1055                                 else
1056                                 if ( e.Button == newdirToolBarButton )
1057                                 {
1058                                         
1059                                 }
1060                         }
1061                         
1062                         void OnClickMenuToolBarContextMenu( object sender, EventArgs e )
1063                         {
1064                                 MenuItem senderMenuItem = (MenuItem)sender;
1065                                 
1066                                 previousCheckedMenuItem.Checked = false;
1067                                 senderMenuItem.Checked = true;
1068                                 previousCheckedMenuItem = senderMenuItem;
1069                                 
1070                                 // FIXME...
1071                                 
1072                                 switch ( senderMenuItem.Index  )
1073                                 {
1074                                         case 0:
1075                                                 mwfFileView.View = View.SmallIcon;
1076                                                 break;
1077                                         case 1:
1078                                                 mwfFileView.View = View.LargeIcon;
1079                                                 break;
1080                                         case 2:
1081                                                 mwfFileView.View = View.LargeIcon;
1082                                                 break;
1083                                         case 3:
1084                                                 mwfFileView.View = View.List;
1085                                                 break;
1086                                         case 4:
1087                                                 mwfFileView.View = View.Details;
1088                                                 break;
1089                                         default:
1090                                                 break;
1091                                 }
1092                                 
1093                                 
1094                                 mwfFileView.UpdateFileView( directoryInfo );
1095                         }
1096                         
1097                         void OnKeyUpFileNameComboBox( object sender, KeyEventArgs e )
1098                         {
1099                                 if ( e.KeyCode == Keys.Enter )
1100                                 {
1101                                         currentFileName = currentDirectoryName + fileNameComboBox.Text;
1102                                         ForceDialogEnd( );
1103                                 }
1104                         }
1105                         
1106                         void OnSelectedIndexChangedFileTypeComboBox( object sender, EventArgs e )
1107                         {
1108                                 fileDialog.FilterIndex = fileTypeComboBox.SelectedIndex + 1;
1109                                 
1110                                 mwfFileView.FilterIndex = fileDialog.FilterIndex;
1111                                 
1112                                 mwfFileView.UpdateFileView( directoryInfo );
1113                         }
1114                         
1115                         void OnSelectedFileChangedFileView( object sender, EventArgs e )
1116                         {
1117                                 fileNameComboBox.Text = mwfFileView.FileName;
1118                                 currentFileName = mwfFileView.FullFileName;
1119                         }
1120                         
1121                         void OnDirectoryChangedFileView( object sender, EventArgs e )
1122                         {
1123                                 ChangeDirectory( sender, mwfFileView.FullFileName );
1124                         }
1125                         
1126                         void OnForceDialogEndFileView( object sender, EventArgs e )
1127                         {
1128                                 ForceDialogEnd( );
1129                         }
1130                         
1131                         void OnSelectedFilesChangedFileView( object sender, EventArgs e )
1132                         {
1133                                 fileNameComboBox.Text = mwfFileView.SelectedFilesString;
1134                         }
1135                         
1136                         void OnDirectoryChangedDirComboBox( object sender, EventArgs e )
1137                         {
1138                                 ChangeDirectory( sender, dirComboBox.CurrentPath );
1139                         }
1140                         
1141                         void OnCheckCheckChanged( object sender, EventArgs e )
1142                         {
1143                                 fileDialog.ReadOnlyChecked = checkBox.Checked;
1144                         }
1145                         
1146                         public void UpdateFilters( )
1147                         {
1148                                 ArrayList filters = fileDialog.FileFilter.FilterArrayList;
1149                                 
1150                                 fileTypeComboBox.Items.Clear( );
1151                                 
1152                                 fileTypeComboBox.BeginUpdate( );
1153                                 
1154                                 foreach ( FilterStruct fs in filters )
1155                                 {
1156                                         fileTypeComboBox.Items.Add( fs.filterName );
1157                                 }
1158                                 
1159                                 fileTypeComboBox.SelectedIndex = fileDialog.FilterIndex - 1;
1160                                 
1161                                 fileTypeComboBox.EndUpdate( );
1162                                 
1163                                 mwfFileView.FilterArrayList = filters;
1164                                 
1165                                 mwfFileView.FilterIndex = fileDialog.FilterIndex;
1166                                 
1167                                 mwfFileView.UpdateFileView( directoryInfo );
1168                         }
1169                         
1170                         public void ChangeDirectory( object sender, string path )
1171                         {
1172                                 currentDirectoryName = path;
1173                                 
1174                                 PushDirectory( directoryInfo );
1175                                 
1176                                 directoryInfo = new DirectoryInfo( path );
1177                                 
1178                                 if ( sender != dirComboBox )
1179                                         dirComboBox.CurrentPath = path;
1180                                 
1181                                 mwfFileView.UpdateFileView( directoryInfo );
1182                         }
1183                         
1184                         public void ForceDialogEnd( )
1185                         {
1186                                 OnClickOpenSaveButton( this, EventArgs.Empty );
1187                         }
1188                         
1189                         private void PushDirectory( DirectoryInfo di )
1190                         {
1191                                 directoryStack.Push( directoryInfo );
1192                                 backToolBarButton.Enabled = true;
1193                         }
1194                         
1195                         private void PopDirectory( )
1196                         {
1197                                 directoryInfo = (DirectoryInfo)directoryStack.Pop( );
1198                                 
1199                                 currentDirectoryName = directoryInfo.FullName;
1200                                 
1201                                 if ( directoryStack.Count == 0 )
1202                                         backToolBarButton.Enabled = false;
1203                         }
1204                         
1205                         public void ResizeAndRelocateForHelpOrReadOnly( )
1206                         {
1207                                 if ( fileDialog.ShowHelp || fileDialog.ShowReadOnly )
1208                                 {
1209                                         mwfFileView.Size = new Size( 449, 250 );
1210                                         fileNameLabel.Location = new Point( 102, 298 );
1211                                         fileNameComboBox.Location = new Point( 195, 298 );
1212                                         fileTypeLabel.Location = new Point( 102, 324 );
1213                                         fileTypeComboBox.Location = new Point( 195, 324 );
1214                                         openSaveButton.Location = new Point( 475, 298 );
1215                                         cancelButton.Location = new Point( 475, 324 );
1216                                 }
1217                                 else
1218                                 {
1219                                         mwfFileView.Size = new Size( 449, 282 );
1220                                         fileNameLabel.Location = new Point( 102, 330 );
1221                                         fileNameComboBox.Location = new Point( 195, 330 );
1222                                         fileTypeLabel.Location = new Point( 102, 356 );
1223                                         fileTypeComboBox.Location = new Point( 195, 356 );
1224                                         openSaveButton.Location = new Point( 475, 330 );
1225                                         cancelButton.Location = new Point( 475, 356 );
1226                                 }
1227                                 
1228                                 if ( fileDialog.ShowHelp )
1229                                         helpButton.Show( );
1230                                 
1231                                 if ( fileDialog.ShowReadOnly )
1232                                         checkBox.Show( );
1233                         }
1234                         
1235                         internal class PopupButtonPanel : Panel
1236                         {
1237                                 internal class PopupButton : Control
1238                                 {
1239                                         internal enum PopupButtonState
1240                                         { Normal, Down, Up}
1241                                         
1242                                         private Image image = null;
1243                                         private PopupButtonState popupButtonState = PopupButtonState.Normal;
1244                                         private StringFormat text_format = new StringFormat();
1245                                         
1246                                         public PopupButton( )
1247                                         {
1248                                                 text_format.Alignment = StringAlignment.Center;
1249                                                 text_format.LineAlignment = StringAlignment.Far;
1250                                                 
1251                                                 SetStyle( ControlStyles.DoubleBuffer, true );
1252                                                 SetStyle( ControlStyles.AllPaintingInWmPaint, true );
1253                                                 SetStyle( ControlStyles.UserPaint, true );
1254                                         }
1255                                         
1256                                         public Image Image
1257                                         {
1258                                                 set {
1259                                                         image = value;
1260                                                         Refresh( );
1261                                                 }
1262                                                 
1263                                                 get {
1264                                                         return image;
1265                                                 }
1266                                         }
1267                                         
1268                                         public PopupButtonState ButtonState
1269                                         {
1270                                                 set {
1271                                                         popupButtonState = value;
1272                                                         Refresh( );
1273                                                 }
1274                                                 
1275                                                 get {
1276                                                         return popupButtonState;
1277                                                 }
1278                                         }
1279                                         
1280                                         protected override void OnPaint( PaintEventArgs pe )
1281                                         {
1282                                                 Draw( pe );
1283                                                 
1284                                                 base.OnPaint( pe );
1285                                         }
1286                                         
1287                                         private void Draw( PaintEventArgs pe )
1288                                         {
1289                                                 Graphics gr = pe.Graphics;
1290                                                 
1291                                                 gr.FillRectangle( ThemeEngine.Current.ResPool.GetSolidBrush( BackColor ), ClientRectangle );
1292                                                 
1293                                                 // draw image
1294                                                 if ( image != null )
1295                                                 {
1296                                                         int i_x = ( ClientSize.Width - image.Width ) / 2;
1297                                                         int i_y = 4;
1298                                                         gr.DrawImage( image, i_x, i_y );
1299                                                 }
1300                                                 
1301                                                 if ( Text != String.Empty )
1302                                                 {
1303                                                         Rectangle text_rect = Rectangle.Inflate( ClientRectangle, -4, -4 );
1304                                                         
1305                                                         gr.DrawString( Text, Font, ThemeEngine.Current.ResPool.GetSolidBrush( ForeColor ), text_rect, text_format );
1306                                                 }
1307                                                 
1308                                                 switch ( popupButtonState )
1309                                                 {
1310                                                         case PopupButtonState.Up:
1311                                                                 gr.DrawLine( ThemeEngine.Current.ResPool.GetPen( Color.White ), 0, 0, ClientSize.Width - 1, 0 );
1312                                                                 gr.DrawLine( ThemeEngine.Current.ResPool.GetPen( Color.White ), 0, 0, 0, ClientSize.Height - 1 );
1313                                                                 gr.DrawLine( ThemeEngine.Current.ResPool.GetPen( Color.Black ), ClientSize.Width - 1, 0, ClientSize.Width - 1, ClientSize.Height - 1 );
1314                                                                 gr.DrawLine( ThemeEngine.Current.ResPool.GetPen( Color.Black ), 0, ClientSize.Height - 1, ClientSize.Width - 1, ClientSize.Height - 1 );
1315                                                                 break;
1316                                                                 
1317                                                         case PopupButtonState.Down:
1318                                                                 gr.DrawLine( ThemeEngine.Current.ResPool.GetPen( Color.Black ), 0, 0, ClientSize.Width - 1, 0 );
1319                                                                 gr.DrawLine( ThemeEngine.Current.ResPool.GetPen( Color.Black ), 0, 0, 0, ClientSize.Height - 1 );
1320                                                                 gr.DrawLine( ThemeEngine.Current.ResPool.GetPen( Color.White ), ClientSize.Width - 1, 0, ClientSize.Width - 1, ClientSize.Height - 1 );
1321                                                                 gr.DrawLine( ThemeEngine.Current.ResPool.GetPen( Color.White ), 0, ClientSize.Height - 1, ClientSize.Width - 1, ClientSize.Height - 1 );
1322                                                                 break;
1323                                                 }
1324                                         }
1325                                         
1326                                         protected override void OnMouseEnter( EventArgs e )
1327                                         {
1328                                                 if ( popupButtonState != PopupButtonState.Down )
1329                                                         popupButtonState = PopupButtonState.Up;
1330                                                 Refresh( );
1331                                                 base.OnMouseEnter( e );
1332                                         }
1333                                         
1334                                         protected override void OnMouseLeave( EventArgs e )
1335                                         {
1336                                                 if ( popupButtonState != PopupButtonState.Down )
1337                                                         popupButtonState = PopupButtonState.Normal;
1338                                                 Refresh( );
1339                                                 base.OnMouseLeave( e );
1340                                         }
1341                                         
1342                                         protected override void OnClick( EventArgs e )
1343                                         {
1344                                                 popupButtonState = PopupButtonState.Down;
1345                                                 Refresh( );
1346                                                 base.OnClick( e );
1347                                         }
1348                                 }
1349                                 
1350                                 private FileDialogPanel fileDialogPanel;
1351                                 
1352                                 private PopupButton lastOpenButton;
1353                                 private PopupButton desktopButton;
1354                                 private PopupButton homeButton;
1355                                 private PopupButton workplaceButton;
1356                                 private PopupButton networkButton;
1357                                 
1358                                 private PopupButton lastPopupButton = null;
1359                                 
1360                                 private ImageList imageList = new ImageList();
1361                                 
1362                                 public PopupButtonPanel( FileDialogPanel fileDialogPanel )
1363                                 {
1364                                         this.fileDialogPanel = fileDialogPanel;
1365                                         
1366                                         BorderStyle = BorderStyle.Fixed3D;
1367                                         BackColor = Color.FromArgb( 128, 128, 128 );
1368                                         Size = new Size( 85, 336 );
1369                                         
1370                                         // use ImageList to scale the bitmaps
1371                                         imageList.ColorDepth = ColorDepth.Depth32Bit;
1372                                         imageList.ImageSize = new Size( 38, 38 );
1373                                         imageList.Images.Add( (Image)Locale.GetResource( "last_open" ) );
1374                                         //imageList.Images.Add( (Image)Locale.GetResource( "desktop" ) );
1375                                         imageList.Images.Add( MimeIconEngine.GetIconForMimeTypeAndSize( "desktop/desktop", imageList.ImageSize ) );
1376                                         //imageList.Images.Add( (Image)Locale.GetResource( "folder_with_paper" ) );
1377                                         imageList.Images.Add( MimeIconEngine.GetIconForMimeTypeAndSize( "directory/home", imageList.ImageSize ) );
1378                                         imageList.Images.Add( (Image)Locale.GetResource( "monitor-computer" ) );
1379                                         //imageList.Images.Add( (Image)Locale.GetResource( "monitor-planet" ) );
1380                                         imageList.Images.Add( MimeIconEngine.GetIconForMimeTypeAndSize( "network/network", imageList.ImageSize ) );
1381                                         imageList.TransparentColor = Color.Transparent;
1382                                         
1383                                         lastOpenButton = new PopupButton( );
1384                                         desktopButton = new PopupButton( );
1385                                         homeButton = new PopupButton( );
1386                                         workplaceButton = new PopupButton( );
1387                                         networkButton = new PopupButton( );
1388                                         
1389                                         lastOpenButton.Size = new Size( 82, 64 );
1390                                         lastOpenButton.Image = imageList.Images[ 0 ];
1391                                         lastOpenButton.BackColor = BackColor;
1392                                         lastOpenButton.ForeColor = Color.White;
1393                                         lastOpenButton.Location = new Point( 2, 2 );
1394                                         lastOpenButton.Text = "Last Open";
1395                                         lastOpenButton.Click += new EventHandler( OnClickButton );
1396                                         
1397                                         desktopButton.Image = imageList.Images[ 1 ];
1398                                         desktopButton.BackColor = BackColor;
1399                                         desktopButton.ForeColor = Color.White;
1400                                         desktopButton.Size = new Size( 82, 64 );
1401                                         desktopButton.Location = new Point( 2, 66 );
1402                                         desktopButton.Text = "Desktop";
1403                                         desktopButton.Click += new EventHandler( OnClickButton );
1404                                         
1405                                         homeButton.Image = imageList.Images[ 2 ];
1406                                         homeButton.BackColor = BackColor;
1407                                         homeButton.ForeColor = Color.White;
1408                                         homeButton.Size = new Size( 82, 64 );
1409                                         homeButton.Location = new Point( 2, 130 );
1410                                         homeButton.Text = "Home";
1411                                         homeButton.Click += new EventHandler( OnClickButton );
1412                                         
1413                                         workplaceButton.Image = imageList.Images[ 3 ];
1414                                         workplaceButton.BackColor = BackColor;
1415                                         workplaceButton.ForeColor = Color.White;
1416                                         workplaceButton.Size = new Size( 82, 64 );
1417                                         workplaceButton.Location = new Point( 2, 194 );
1418                                         workplaceButton.Text = "Workplace";
1419                                         workplaceButton.Click += new EventHandler( OnClickButton );
1420                                         
1421                                         networkButton.Image = imageList.Images[ 4 ];
1422                                         networkButton.BackColor = BackColor;
1423                                         networkButton.ForeColor = Color.White;
1424                                         networkButton.Size = new Size( 82, 64 );
1425                                         networkButton.Location = new Point( 2, 258 );
1426                                         networkButton.Text = "Network";
1427                                         networkButton.Click += new EventHandler( OnClickButton );
1428                                         
1429                                         Controls.Add( lastOpenButton );
1430                                         Controls.Add( desktopButton );
1431                                         Controls.Add( homeButton );
1432                                         Controls.Add( workplaceButton );
1433                                         Controls.Add( networkButton );
1434                                 }
1435                                 
1436                                 void OnClickButton( object sender, EventArgs e )
1437                                 {
1438                                         if ( lastPopupButton != null && (PopupButton)sender != lastPopupButton )
1439                                                 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1440                                         lastPopupButton = sender as PopupButton;
1441                                         
1442                                         if ( sender == lastOpenButton )
1443                                         {
1444                                                 
1445                                         }
1446                                         else
1447                                         if ( sender == desktopButton )
1448                                         {
1449                                                 fileDialogPanel.ChangeDirectory( this, Environment.GetFolderPath( Environment.SpecialFolder.Desktop ) );
1450                                         }
1451                                         else
1452                                         if ( sender == homeButton )
1453                                         {
1454                                                 fileDialogPanel.ChangeDirectory( this, Environment.GetFolderPath( Environment.SpecialFolder.Personal ) );
1455                                         }
1456                                         else
1457                                         if ( sender == workplaceButton )
1458                                         {
1459 //                                              fileDialogPanel.ChangeDirectory(this, Environment.GetFolderPath( Environment.SpecialFolder.MyComputer ) );
1460                                         }
1461                                         else
1462                                         if ( sender == networkButton )
1463                                         {
1464                                                 
1465                                         }
1466                                 }
1467                         }
1468                 }
1469         }
1470         
1471         internal struct FilterStruct
1472         {
1473                 public string filterName;
1474                 public StringCollection filters;
1475                 
1476                 public FilterStruct( string filterName, string filter )
1477                 {
1478                         this.filterName = filterName;
1479                         
1480                         filters =  new StringCollection( );
1481                         
1482                         SplitFilters( filter );
1483                 }
1484                 
1485                 private void SplitFilters( string filter )
1486                 {
1487                         string[] split = filter.Split( new Char[] {';'} );
1488                         
1489                         filters.AddRange( split );
1490                 }
1491         }
1492         
1493         internal struct FileStruct
1494         {
1495                 public FileStruct( string fullname, FileAttributes attributes )
1496                 {
1497                         this.fullname = fullname;
1498                         this.attributes = attributes;
1499                 }
1500                 
1501                 public string fullname;
1502                 public FileAttributes attributes;
1503         }
1504         
1505         // MWFFileView
1506         internal class MWFFileView : ListView
1507         {
1508 //              private ImageList fileViewSmallImageList = new ImageList();
1509 //              private ImageList fileViewBigImageList = new ImageList();
1510                 
1511                 private ArrayList filterArrayList;
1512                 // store the FileStruct of all files in the current directory
1513                 private Hashtable fileHashtable = new Hashtable();
1514                 
1515                 private bool showHiddenFiles = false;
1516                 
1517                 private EventHandler on_selected_file_changed;
1518                 private EventHandler on_selected_files_changed;
1519                 private EventHandler on_directory_changed;
1520                 private EventHandler on_force_dialog_end;
1521                 
1522                 private string fileName;
1523                 private string fullFileName;
1524                 private string selectedFilesString;
1525                 
1526                 private int filterIndex;
1527                 
1528                 public MWFFileView( )
1529                 {
1530                         SmallImageList = MimeIconEngine.SmallIcons;
1531                         LargeImageList = MimeIconEngine.LargeIcons;
1532                         
1533                         View = View.List;
1534                 }
1535                 
1536                 public ArrayList FilterArrayList
1537                 {
1538                         set {
1539                                 filterArrayList = value;
1540                         }
1541                         
1542                         get {
1543                                 return filterArrayList;
1544                         }
1545                 }
1546                 
1547                 public Hashtable FileHashtable
1548                 {
1549                         set {
1550                                 fileHashtable = value;
1551                         }
1552                         
1553                         get {
1554                                 return fileHashtable;
1555                         }
1556                 }
1557                 
1558                 public bool ShowHiddenFiles
1559                 {
1560                         set {
1561                                 showHiddenFiles = value;
1562                         }
1563                         
1564                         get {
1565                                 return showHiddenFiles;
1566                         }
1567                 }
1568                 
1569                 public string FileName
1570                 {
1571                         set {
1572                                 fileName = value;
1573                         }
1574                         
1575                         get {
1576                                 return fileName;
1577                         }
1578                 }
1579                 
1580                 public string FullFileName
1581                 {
1582                         set {
1583                                 fullFileName = value;
1584                         }
1585                         
1586                         get {
1587                                 return fullFileName;
1588                         }
1589                 }
1590                 
1591                 public int FilterIndex
1592                 {
1593                         set {
1594                                 filterIndex = value;
1595                         }
1596                         
1597                         get {
1598                                 return filterIndex;
1599                         }
1600                 }
1601                 
1602                 public string SelectedFilesString
1603                 {
1604                         set {
1605                                 selectedFilesString = value;
1606                         }
1607                         
1608                         get {
1609                                 return selectedFilesString;
1610                         }
1611                 }
1612                 
1613                 private ArrayList GetFileInfoArrayList( DirectoryInfo directoryInfo )
1614                 {
1615                         ArrayList arrayList = new ArrayList( );
1616                         
1617                         if ( filterArrayList != null && filterArrayList.Count != 0 )
1618                         {
1619                                 FilterStruct fs = (FilterStruct)filterArrayList[ filterIndex - 1 ];
1620                                 
1621                                 foreach ( string s in fs.filters )
1622                                         arrayList.AddRange( directoryInfo.GetFiles( s ) );
1623                         }
1624                         else
1625                                 arrayList.AddRange( directoryInfo.GetFiles( ) );
1626                         
1627                         return arrayList;
1628                 }
1629                 
1630                 public void UpdateFileView( DirectoryInfo inputDirectoryInfo )
1631                 {
1632                         DirectoryInfo directoryInfo = inputDirectoryInfo;
1633                         
1634                         DirectoryInfo[] directoryInfoArray = directoryInfo.GetDirectories( );
1635                         
1636                         ArrayList fileInfoArrayList = GetFileInfoArrayList( directoryInfo );
1637                         
1638                         fileHashtable.Clear( );
1639                         
1640                         BeginUpdate( );
1641                         
1642                         Items.Clear( );
1643                         SelectedItems.Clear( );
1644                         
1645                         foreach ( DirectoryInfo directoryInfoi in directoryInfoArray )
1646                         {
1647                                 if ( !ShowHiddenFiles )
1648                                         if ( directoryInfoi.Name.StartsWith( "." ) || directoryInfoi.Attributes == FileAttributes.Hidden )
1649                                                 continue;
1650                                 
1651                                 FileStruct fileStruct = new FileStruct( );
1652                                 
1653                                 fileStruct.fullname = directoryInfoi.FullName;
1654                                 
1655                                 ListViewItem listViewItem = new ListViewItem( directoryInfoi.Name );
1656                                 
1657                                 int index = MimeIconEngine.GetIconIndexForMimeType( "inode/directory" );
1658                                 
1659                                 listViewItem.ImageIndex = index;
1660                                 
1661                                 listViewItem.SubItems.Add( "" );
1662                                 listViewItem.SubItems.Add( "Directory" );
1663                                 listViewItem.SubItems.Add( directoryInfoi.LastAccessTime.ToShortDateString( ) + " " + directoryInfoi.LastAccessTime.ToShortTimeString( ) );
1664                                 
1665                                 fileStruct.attributes = FileAttributes.Directory;
1666                                 
1667                                 fileHashtable.Add( directoryInfoi.Name, fileStruct );
1668                                 
1669                                 Items.Add( listViewItem );
1670                         }
1671                         
1672                         foreach ( FileInfo fileInfo in fileInfoArrayList )
1673                         {
1674                                 if ( !ShowHiddenFiles )
1675                                         if ( fileInfo.Name.StartsWith( "." )  || fileInfo.Attributes == FileAttributes.Hidden )
1676                                                 continue;
1677                                 
1678                                 FileStruct fileStruct = new FileStruct( );
1679                                 
1680                                 fileStruct.fullname = fileInfo.FullName;
1681                                 
1682                                 ListViewItem listViewItem = new ListViewItem( fileInfo.Name );
1683                                 
1684                                 listViewItem.ImageIndex = MimeIconEngine.GetIconIndexForFile( fileStruct.fullname );
1685                                 
1686                                 long fileLen = 1;
1687                                 if ( fileInfo.Length > 1024 )
1688                                         fileLen = fileInfo.Length / 1024;
1689                                 
1690                                 listViewItem.SubItems.Add( fileLen.ToString( ) + " KB" );
1691                                 listViewItem.SubItems.Add( "File" );
1692                                 listViewItem.SubItems.Add( fileInfo.LastAccessTime.ToShortDateString( ) + " " + fileInfo.LastAccessTime.ToShortTimeString( ) );
1693                                 
1694                                 fileStruct.attributes = FileAttributes.Normal;
1695                                 
1696                                 fileHashtable.Add( fileInfo.Name, fileStruct );
1697                                 
1698                                 Items.Add( listViewItem );
1699                         }
1700                         
1701                         EndUpdate( );
1702                 }
1703                 
1704                 protected override void OnClick( EventArgs e )
1705                 {
1706                         if ( !MultiSelect )
1707                         {
1708                                 if ( SelectedItems.Count > 0 )
1709                                 {
1710                                         ListViewItem listViewItem = SelectedItems[ 0 ];
1711                                         
1712                                         FileStruct fileStruct = (FileStruct)fileHashtable[ listViewItem.Text ];
1713                                         
1714                                         if ( fileStruct.attributes != FileAttributes.Directory )
1715                                         {
1716                                                 fileName = listViewItem.Text;
1717                                                 fullFileName = fileStruct.fullname;
1718                                                 
1719                                                 if ( on_selected_file_changed != null )
1720                                                         on_selected_file_changed( this, EventArgs.Empty );
1721                                         }
1722                                 }
1723                         }
1724                         
1725                         base.OnClick( e );
1726                 }
1727                 
1728                 protected override void OnDoubleClick( EventArgs e )
1729                 {
1730                         if ( SelectedItems.Count > 0 )
1731                         {
1732                                 ListViewItem listViewItem = SelectedItems[ 0 ];
1733                                 
1734                                 FileStruct fileStruct = (FileStruct)fileHashtable[ listViewItem.Text ];
1735                                 
1736                                 if ( fileStruct.attributes == FileAttributes.Directory )
1737                                 {
1738                                         fullFileName = fileStruct.fullname;
1739                                         
1740                                         if ( on_directory_changed != null )
1741                                                 on_directory_changed( this, EventArgs.Empty );
1742                                 }
1743                                 else
1744                                 {
1745                                         fileName = listViewItem.Text;
1746                                         fullFileName = fileStruct.fullname;
1747                                         
1748                                         if ( on_selected_file_changed != null )
1749                                                 on_selected_file_changed( this, EventArgs.Empty );
1750                                         
1751                                         if ( on_force_dialog_end != null )
1752                                                 on_force_dialog_end( this, EventArgs.Empty );
1753                                         
1754                                         return;
1755                                 }
1756                         }
1757                         
1758                         base.OnDoubleClick( e );
1759                 }
1760                 
1761                 protected override void OnSelectedIndexChanged( EventArgs e )
1762                 {
1763                         if ( MultiSelect )
1764                         {
1765                                 if ( SelectedItems.Count > 0 )
1766                                 {
1767                                         selectedFilesString = "";
1768                                         
1769                                         if ( SelectedItems.Count == 1 )
1770                                         {
1771                                                 FileStruct fileStruct = (FileStruct)fileHashtable[ SelectedItems[ 0 ].Text ];
1772                                                 
1773                                                 if ( fileStruct.attributes != FileAttributes.Directory )
1774                                                         selectedFilesString = SelectedItems[ 0 ].Text;
1775                                         }
1776                                         else
1777                                         {
1778                                                 foreach ( ListViewItem lvi in SelectedItems )
1779                                                 {
1780                                                         FileStruct fileStruct = (FileStruct)fileHashtable[ lvi.Text ];
1781                                                         
1782                                                         if ( fileStruct.attributes != FileAttributes.Directory )
1783                                                                 selectedFilesString += "\"" + lvi.Text + "\" ";
1784                                                 }
1785                                         }
1786                                         
1787                                         if ( on_selected_files_changed != null )
1788                                                 on_selected_files_changed( this, EventArgs.Empty );
1789                                 }
1790                         }
1791                         
1792                         base.OnSelectedIndexChanged( e );
1793                 }
1794                 
1795                 public event EventHandler SelectedFileChanged
1796                 {
1797                         add { on_selected_file_changed += value; }
1798                         remove { on_selected_file_changed -= value; }
1799                 }
1800                 
1801                 public event EventHandler SelectedFilesChanged
1802                 {
1803                         add { on_selected_files_changed += value; }
1804                         remove { on_selected_files_changed -= value; }
1805                 }
1806                 
1807                 public event EventHandler DirectoryChanged
1808                 {
1809                         add { on_directory_changed += value; }
1810                         remove { on_directory_changed -= value; }
1811                 }
1812                 
1813                 public event EventHandler ForceDialogEnd
1814                 {
1815                         add { on_force_dialog_end += value; }
1816                         remove { on_force_dialog_end -= value; }
1817                 }
1818         }
1819         
1820         internal class FileFilter
1821         {
1822                 private ArrayList filterArrayList = new ArrayList();
1823                 
1824                 private string filter;
1825                 
1826                 public FileFilter( )
1827                 {}
1828                 
1829                 public FileFilter( string filter )
1830                 {
1831                         this.filter = filter;
1832                         
1833                         SplitFilter( );
1834                 }
1835                 
1836                 public ArrayList FilterArrayList
1837                 {
1838                         set {
1839                                 filterArrayList = value;
1840                         }
1841                         
1842                         get {
1843                                 return filterArrayList;
1844                         }
1845                 }
1846                 
1847                 public string Filter
1848                 {
1849                         set {
1850                                 filter = value;
1851                                 
1852                                 SplitFilter( );
1853                         }
1854                         
1855                         get {
1856                                 return filter;
1857                         }
1858                 }
1859                 
1860                 private void SplitFilter( )
1861                 {
1862                         filterArrayList.Clear( );
1863                         
1864                         if ( filter == null )
1865                                 throw new NullReferenceException( "Filter" );
1866                         
1867                         if ( filter.Length == 0 )
1868                                 return;
1869                         
1870                         string[] filters = filter.Split( new Char[] {'|'} );
1871                         
1872                         if ( ( filters.Length % 2 ) != 0 )
1873                                 throw new ArgumentException( "Filter" );
1874                         
1875                         for ( int i = 0; i < filters.Length; i += 2 )
1876                         {
1877                                 FilterStruct filterStruct = new FilterStruct( filters[ i ], filters[ i + 1 ] );
1878                                 
1879                                 filterArrayList.Add( filterStruct );
1880                         }
1881                 }
1882         }
1883         
1884         internal class DirComboBox : ComboBox
1885         {
1886                 internal class DirComboBoxItem
1887                 {
1888                         private int imageIndex;
1889                         private string name;
1890                         private string path;
1891                         private int xPos;
1892                         
1893                         public DirComboBoxItem( int imageIndex, string name, string path, int xPos )
1894                         {
1895                                 this.imageIndex = imageIndex;
1896                                 this.name = name;
1897                                 this.path = path;
1898                                 this.XPos = xPos;
1899                         }
1900                         
1901                         public int ImageIndex
1902                         {
1903                                 set {
1904                                         imageIndex = value;
1905                                 }
1906                                 
1907                                 get {
1908                                         return imageIndex;
1909                                 }
1910                         }
1911                         
1912                         public string Name
1913                         {
1914                                 set {
1915                                         name = value;
1916                                 }
1917                                 
1918                                 get {
1919                                         return name;
1920                                 }
1921                         }
1922                         
1923                         public string Path
1924                         {
1925                                 set {
1926                                         path = value;
1927                                 }
1928                                 
1929                                 get {
1930                                         return path;
1931                                 }
1932                         }
1933                         
1934                         public int XPos
1935                         {
1936                                 set {
1937                                         xPos = value;
1938                                 }
1939                                 
1940                                 get {
1941                                         return xPos;
1942                                 }
1943                         }
1944                 }
1945                 
1946                 private ImageList imageList = new ImageList();
1947                 
1948                 private string currentPath;
1949                 
1950                 private bool firstTime = true;
1951                 
1952                 private EventHandler on_directory_changed;
1953                 
1954                 public DirComboBox( )
1955                 {
1956                         DrawMode = DrawMode.OwnerDrawFixed;
1957                         
1958                         imageList.ColorDepth = ColorDepth.Depth32Bit;
1959                         imageList.ImageSize = new Size( 16, 16 );
1960                         imageList.Images.Add( MimeIconEngine.GetIconForMimeTypeAndSize( "desktop/desktop", imageList.ImageSize ) );
1961                         imageList.Images.Add( MimeIconEngine.GetIconForMimeTypeAndSize( "directory/home", imageList.ImageSize ) );
1962                         imageList.Images.Add( MimeIconEngine.GetIconForMimeTypeAndSize( "inode/directory", imageList.ImageSize ) );
1963                         imageList.TransparentColor = Color.Transparent;
1964                         
1965                         Items.AddRange( new object[] {
1966                                                new DirComboBoxItem( 0, "Desktop", Environment.GetFolderPath( Environment.SpecialFolder.Desktop ), 0 ),
1967                                                new DirComboBoxItem( 1, "Home", Environment.GetFolderPath( Environment.SpecialFolder.Personal ), 0 )
1968                                        }
1969                                        );
1970                 }
1971                 
1972                 public string CurrentPath
1973                 {
1974                         set {
1975                                 currentPath = value;
1976                                 
1977                                 ShowPath( );
1978                         }
1979                         get {
1980                                 return currentPath;
1981                         }
1982                 }
1983                 
1984                 private void ShowPath( )
1985                 {
1986                         DirectoryInfo di = new DirectoryInfo( currentPath );
1987                         
1988                         Stack dirStack = new Stack( );
1989                         
1990                         dirStack.Push( di );
1991                         
1992                         while ( di.Parent != null )
1993                         {
1994                                 di = di.Parent;
1995                                 dirStack.Push( di );
1996                         }
1997                         
1998                         BeginUpdate( );
1999                         
2000                         Items.Clear( );
2001                         
2002                         Items.AddRange( new object[] {
2003                                                new DirComboBoxItem( 0, "Desktop", Environment.GetFolderPath( Environment.SpecialFolder.Desktop ), 0 ),
2004                                                new DirComboBoxItem( 1, "Home", Environment.GetFolderPath( Environment.SpecialFolder.Personal ), 0 )
2005                                        }
2006                                        );
2007                         
2008                         int sel = -1;
2009                         
2010                         int xPos = -4;
2011                         
2012                         while ( dirStack.Count != 0 )
2013                         {
2014                                 DirectoryInfo dii = (DirectoryInfo)dirStack.Pop( );
2015                                 sel = Items.Add( new DirComboBoxItem( 2, dii.Name, dii.FullName, xPos + 4 ) );
2016                                 xPos += 4;
2017                         }
2018                         
2019                         if ( sel != -1 )
2020                                 SelectedIndex = sel;
2021                         
2022                         EndUpdate( );
2023                 }
2024                 
2025                 protected override void OnDrawItem( DrawItemEventArgs e )
2026                 {
2027                         if ( e.Index == -1 )
2028                                 return;
2029                         
2030                         Bitmap bmp = new Bitmap( e.Bounds.Width, e.Bounds.Height, e.Graphics );
2031                         Graphics gr = Graphics.FromImage( bmp );
2032                         
2033                         DirComboBoxItem dcbi = Items[ e.Index ] as DirComboBoxItem;
2034                         
2035                         Color backColor = e.BackColor;
2036                         Color foreColor = e.ForeColor;
2037                         
2038                         int xPos = dcbi.XPos;
2039                         
2040                         // Bug in ComboBox !!!!!
2041                         // we never receive DrawItemState.ComboBoxEdit
2042                         if ( ( e.State & DrawItemState.ComboBoxEdit ) != 0 )
2043                                 xPos = 0;
2044                         else
2045                         if ( ( e.State & DrawItemState.Selected ) == DrawItemState.Selected )
2046                         {
2047                                 backColor = Color.Blue;
2048                                 foreColor = Color.White;
2049                         }
2050                         
2051                         gr.FillRectangle( ThemeEngine.Current.ResPool.GetSolidBrush( backColor ), new Rectangle( 0, 0, bmp.Width, bmp.Height ) );
2052                         
2053                         gr.DrawString( dcbi.Name, e.Font , ThemeEngine.Current.ResPool.GetSolidBrush( foreColor ), new Point( 24 + xPos, ( bmp.Height - e.Font.Height ) / 2 ) );
2054                         gr.DrawImage( imageList.Images[ dcbi.ImageIndex ], new Rectangle( new Point( xPos + 2, 0 ), new Size( 16, 16 ) ) );
2055                         
2056                         e.Graphics.DrawImage( bmp, e.Bounds.X, e.Bounds.Y );
2057                 }
2058                 
2059                 protected override void OnSelectedIndexChanged( EventArgs e )
2060                 {
2061                         // do not call ChangeDirectory when invoked from FileDialogPanel ctor...
2062                         if ( firstTime )
2063                         {
2064                                 firstTime = false;
2065                                 return;
2066                         }
2067                         
2068                         if ( Items.Count > 0 )
2069                         {
2070                                 DirComboBoxItem dcbi = Items[ SelectedIndex ] as DirComboBoxItem;
2071                                 
2072                                 currentPath = dcbi.Path;
2073                                 
2074                                 if ( on_directory_changed != null )
2075                                         on_directory_changed( this, EventArgs.Empty );
2076                         }
2077                 }
2078                 
2079                 public event EventHandler DirectoryChanged
2080                 {
2081                         add { on_directory_changed += value; }
2082                         remove { on_directory_changed -= value; }
2083                 }
2084         }
2085 }
2086
2087