PageRenderTime 63ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/mcs/class/Managed.Windows.Forms/System.Windows.Forms/FileDialog.cs

https://bitbucket.org/danipen/mono
C# | 5200 lines | 4051 code | 987 blank | 162 comment | 786 complexity | cb101feb47e2c054670851aa0824359d MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0

Large files files are truncated, but you can click here to view the full file

  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) 2006 Alexander Olk
  21. //
  22. // Authors:
  23. //
  24. // Alexander Olk alex.olk@googlemail.com
  25. // Gert Driesen (drieseng@users.sourceforge.net)
  26. // Eric Petit (surfzoid2002@yahoo.fr)
  27. //
  28. // TODO:
  29. // Keyboard shortcuts (DEL, F5, F2)
  30. // ??
  31. using System;
  32. using System.Collections;
  33. using System.Collections.Specialized;
  34. using System.ComponentModel;
  35. using System.Drawing;
  36. using System.IO;
  37. using System.Resources;
  38. using System.Text;
  39. using System.Threading;
  40. using System.Xml;
  41. namespace System.Windows.Forms
  42. {
  43. #region FileDialog
  44. [DefaultProperty ("FileName")]
  45. [DefaultEvent ("FileOk")]
  46. public abstract class FileDialog : CommonDialog
  47. {
  48. protected static readonly object EventFileOk = new object ();
  49. private static int MaxFileNameItems = 10;
  50. internal enum FileDialogType
  51. {
  52. OpenFileDialog,
  53. SaveFileDialog
  54. }
  55. private bool addExtension = true;
  56. private bool checkFileExists;
  57. private bool checkPathExists = true;
  58. private string defaultExt;
  59. private bool dereferenceLinks = true;
  60. private string[] fileNames;
  61. private string filter = "";
  62. private int filterIndex = 1;
  63. private string initialDirectory;
  64. private bool restoreDirectory;
  65. private bool showHelp;
  66. private string title;
  67. private bool validateNames = true;
  68. private bool auto_upgrade_enable = true;
  69. private FileDialogCustomPlacesCollection custom_places;
  70. private bool supportMultiDottedExtensions;
  71. private bool checkForIllegalChars = true;
  72. private Button cancelButton;
  73. private ToolBarButton upToolBarButton;
  74. private PopupButtonPanel popupButtonPanel;
  75. private Button openSaveButton;
  76. private Button helpButton;
  77. private Label fileTypeLabel;
  78. private ToolBarButton menueToolBarButton;
  79. private ContextMenu menueToolBarButtonContextMenu;
  80. private ToolBar smallButtonToolBar;
  81. private DirComboBox dirComboBox;
  82. private ComboBox fileNameComboBox;
  83. private Label fileNameLabel;
  84. private MWFFileView mwfFileView;
  85. private MwfFileViewItemComparer file_view_comparer;
  86. private Label searchSaveLabel;
  87. private ToolBarButton newdirToolBarButton;
  88. private ToolBarButton backToolBarButton;
  89. private ComboBox fileTypeComboBox;
  90. private ImageList imageListTopToolbar;
  91. private CheckBox readonlyCheckBox;
  92. private bool multiSelect;
  93. private string restoreDirectoryString = String.Empty;
  94. internal FileDialogType fileDialogType;
  95. private bool do_not_call_OnSelectedIndexChangedFileTypeComboBox;
  96. private bool showReadOnly;
  97. private bool readOnlyChecked;
  98. internal bool createPrompt;
  99. internal bool overwritePrompt = true;
  100. private FileFilter fileFilter;
  101. private string[] configFileNames = null;
  102. private string lastFolder = String.Empty;
  103. private MWFVFS vfs;
  104. private const string filedialog_string = "FileDialog";
  105. private const string lastfolder_string = "LastFolder";
  106. private const string width_string = "Width";
  107. private const string height_string = "Height";
  108. private const string filenames_string = "FileNames";
  109. private const string x_string = "X";
  110. private const string y_string = "Y";
  111. private readonly char [] wildcard_chars = new char [] { '*', '?' };
  112. private bool disable_form_closed_event;
  113. internal FileDialog ()
  114. {
  115. form = new DialogForm (this);
  116. vfs = new MWFVFS ();
  117. Size formConfigSize = Size.Empty;
  118. Point formConfigLocation = Point.Empty;
  119. object formWidth = MWFConfig.GetValue (filedialog_string, width_string);
  120. object formHeight = MWFConfig.GetValue (filedialog_string, height_string);
  121. if (formHeight != null && formWidth != null)
  122. formConfigSize = new Size ((int)formWidth, (int)formHeight);
  123. object formLocationX = MWFConfig.GetValue (filedialog_string, x_string);
  124. object formLocationY = MWFConfig.GetValue (filedialog_string, y_string);
  125. if (formLocationX != null && formLocationY != null)
  126. formConfigLocation = new Point ((int)formLocationX, (int)formLocationY);
  127. configFileNames = (string[])MWFConfig.GetValue (filedialog_string, filenames_string);
  128. fileTypeComboBox = new ComboBox ();
  129. backToolBarButton = new ToolBarButton ();
  130. newdirToolBarButton = new ToolBarButton ();
  131. searchSaveLabel = new Label ();
  132. mwfFileView = new MWFFileView (vfs);
  133. fileNameLabel = new Label ();
  134. fileNameComboBox = new ComboBox ();
  135. dirComboBox = new DirComboBox (vfs);
  136. smallButtonToolBar = new ToolBar ();
  137. menueToolBarButton = new ToolBarButton ();
  138. fileTypeLabel = new Label ();
  139. openSaveButton = new Button ();
  140. helpButton = new Button ();
  141. popupButtonPanel = new PopupButtonPanel ();
  142. upToolBarButton = new ToolBarButton ();
  143. cancelButton = new Button ();
  144. form.CancelButton = cancelButton;
  145. imageListTopToolbar = new ImageList ();
  146. menueToolBarButtonContextMenu = new ContextMenu ();
  147. readonlyCheckBox = new CheckBox ();
  148. form.SuspendLayout ();
  149. //imageListTopToolbar
  150. imageListTopToolbar.ColorDepth = ColorDepth.Depth32Bit;
  151. imageListTopToolbar.ImageSize = new Size (16, 16); // 16, 16
  152. imageListTopToolbar.Images.Add (ResourceImageLoader.Get ("go-previous.png"));
  153. imageListTopToolbar.Images.Add (ResourceImageLoader.Get ("go-top.png"));
  154. imageListTopToolbar.Images.Add (ResourceImageLoader.Get ("folder-new.png"));
  155. imageListTopToolbar.Images.Add (ResourceImageLoader.Get ("preferences-system-windows.png"));
  156. imageListTopToolbar.TransparentColor = Color.Transparent;
  157. // searchLabel
  158. searchSaveLabel.FlatStyle = FlatStyle.System;
  159. searchSaveLabel.Location = new Point (6, 6);
  160. searchSaveLabel.Size = new Size (86, 22);
  161. searchSaveLabel.TextAlign = ContentAlignment.MiddleRight;
  162. // dirComboBox
  163. dirComboBox.Anchor = ((AnchorStyles)(((AnchorStyles.Top | AnchorStyles.Left) | AnchorStyles.Right)));
  164. dirComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
  165. dirComboBox.Location = new Point (99, 6);
  166. dirComboBox.Size = new Size (261, 22);
  167. dirComboBox.TabIndex = 7;
  168. // smallButtonToolBar
  169. smallButtonToolBar.Anchor = ((AnchorStyles)((AnchorStyles.Top | AnchorStyles.Right)));
  170. smallButtonToolBar.Appearance = ToolBarAppearance.Flat;
  171. smallButtonToolBar.AutoSize = false;
  172. smallButtonToolBar.Buttons.AddRange (new ToolBarButton [] {
  173. backToolBarButton,
  174. upToolBarButton,
  175. newdirToolBarButton,
  176. menueToolBarButton});
  177. smallButtonToolBar.ButtonSize = new Size (24, 24); // 21, 16
  178. smallButtonToolBar.Divider = false;
  179. smallButtonToolBar.Dock = DockStyle.None;
  180. smallButtonToolBar.DropDownArrows = true;
  181. smallButtonToolBar.ImageList = imageListTopToolbar;
  182. smallButtonToolBar.Location = new Point (372, 6);
  183. smallButtonToolBar.ShowToolTips = true;
  184. smallButtonToolBar.Size = new Size (140, 28);
  185. smallButtonToolBar.TabIndex = 8;
  186. smallButtonToolBar.TextAlign = ToolBarTextAlign.Right;
  187. // buttonPanel
  188. popupButtonPanel.Dock = DockStyle.None;
  189. popupButtonPanel.Anchor = ((AnchorStyles)((((AnchorStyles.Top | AnchorStyles.Bottom) | AnchorStyles.Left))));
  190. popupButtonPanel.Location = new Point (6, 35);
  191. popupButtonPanel.Size = new Size (87, 338);
  192. popupButtonPanel.TabIndex = 9;
  193. // mwfFileView
  194. mwfFileView.Anchor = ((AnchorStyles)((((AnchorStyles.Top | AnchorStyles.Bottom) | AnchorStyles.Left) | AnchorStyles.Right)));
  195. mwfFileView.Location = new Point (99, 35);
  196. mwfFileView.Size = new Size (450, 283);
  197. mwfFileView.MultiSelect = false;
  198. mwfFileView.TabIndex = 10;
  199. mwfFileView.RegisterSender (dirComboBox);
  200. mwfFileView.RegisterSender (popupButtonPanel);
  201. // fileNameLabel
  202. fileNameLabel.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Left)));
  203. fileNameLabel.FlatStyle = FlatStyle.System;
  204. fileNameLabel.Location = new Point (101, 326);
  205. fileNameLabel.Size = new Size (70, 21);
  206. fileNameLabel.Text = "File name:";
  207. fileNameLabel.TextAlign = ContentAlignment.MiddleLeft;
  208. // fileNameComboBox
  209. fileNameComboBox.Anchor = ((AnchorStyles)(((AnchorStyles.Bottom | AnchorStyles.Left) | AnchorStyles.Right)));
  210. fileNameComboBox.Location = new Point (195, 326);
  211. fileNameComboBox.Size = new Size (246, 22);
  212. fileNameComboBox.TabIndex = 1;
  213. fileNameComboBox.MaxDropDownItems = MaxFileNameItems;
  214. fileNameComboBox.RestoreContextMenu ();
  215. UpdateRecentFiles ();
  216. // fileTypeLabel
  217. fileTypeLabel.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Left)));
  218. fileTypeLabel.FlatStyle = FlatStyle.System;
  219. fileTypeLabel.Location = new Point (101, 355);
  220. fileTypeLabel.Size = new Size (90, 21);
  221. fileTypeLabel.Text = "Files of type:";
  222. fileTypeLabel.TextAlign = ContentAlignment.MiddleLeft;
  223. // fileTypeComboBox
  224. fileTypeComboBox.Anchor = ((AnchorStyles)(((AnchorStyles.Bottom | AnchorStyles.Left) | AnchorStyles.Right)));
  225. fileTypeComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
  226. fileTypeComboBox.Location = new Point (195, 355);
  227. fileTypeComboBox.Size = new Size (246, 22);
  228. fileTypeComboBox.TabIndex = 2;
  229. // backToolBarButton
  230. backToolBarButton.ImageIndex = 0;
  231. backToolBarButton.Enabled = false;
  232. backToolBarButton.Style = ToolBarButtonStyle.PushButton;
  233. mwfFileView.AddControlToEnableDisableByDirStack (backToolBarButton);
  234. // upToolBarButton
  235. upToolBarButton.ImageIndex = 1;
  236. upToolBarButton.Style = ToolBarButtonStyle.PushButton;
  237. mwfFileView.SetFolderUpToolBarButton (upToolBarButton);
  238. // newdirToolBarButton
  239. newdirToolBarButton.ImageIndex = 2;
  240. newdirToolBarButton.Style = ToolBarButtonStyle.PushButton;
  241. // menueToolBarButton
  242. menueToolBarButton.ImageIndex = 3;
  243. menueToolBarButton.DropDownMenu = menueToolBarButtonContextMenu;
  244. menueToolBarButton.Style = ToolBarButtonStyle.DropDownButton;
  245. // menueToolBarButtonContextMenu
  246. menueToolBarButtonContextMenu.MenuItems.AddRange (mwfFileView.ViewMenuItems);
  247. // openSaveButton
  248. openSaveButton.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Right)));
  249. openSaveButton.FlatStyle = FlatStyle.System;
  250. openSaveButton.Location = new Point (474, 326);
  251. openSaveButton.Size = new Size (75, 23);
  252. openSaveButton.TabIndex = 4;
  253. openSaveButton.FlatStyle = FlatStyle.System;
  254. // cancelButton
  255. cancelButton.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Right)));
  256. cancelButton.FlatStyle = FlatStyle.System;
  257. cancelButton.Location = new Point (474, 353);
  258. cancelButton.Size = new Size (75, 23);
  259. cancelButton.TabIndex = 5;
  260. cancelButton.Text = "Cancel";
  261. cancelButton.FlatStyle = FlatStyle.System;
  262. // helpButton
  263. helpButton.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Right)));
  264. helpButton.FlatStyle = FlatStyle.System;
  265. helpButton.Location = new Point (474, 353);
  266. helpButton.Size = new Size (75, 23);
  267. helpButton.TabIndex = 6;
  268. helpButton.Text = "Help";
  269. helpButton.FlatStyle = FlatStyle.System;
  270. helpButton.Visible = false;
  271. // checkBox
  272. readonlyCheckBox.Anchor = ((AnchorStyles)(((AnchorStyles.Bottom | AnchorStyles.Left) | AnchorStyles.Right)));
  273. readonlyCheckBox.Text = "Open Readonly";
  274. readonlyCheckBox.Location = new Point (195, 350);
  275. readonlyCheckBox.Size = new Size (245, 21);
  276. readonlyCheckBox.TabIndex = 3;
  277. readonlyCheckBox.FlatStyle = FlatStyle.System;
  278. readonlyCheckBox.Visible = false;
  279. form.SizeGripStyle = SizeGripStyle.Show;
  280. form.AcceptButton = openSaveButton;
  281. form.MaximizeBox = true;
  282. form.MinimizeBox = true;
  283. form.FormBorderStyle = FormBorderStyle.Sizable;
  284. form.ClientSize = new Size (555, 385);
  285. form.MinimumSize = form.Size;
  286. form.Controls.Add (smallButtonToolBar);
  287. form.Controls.Add (cancelButton);
  288. form.Controls.Add (openSaveButton);
  289. form.Controls.Add (mwfFileView);
  290. form.Controls.Add (fileTypeLabel);
  291. form.Controls.Add (fileNameLabel);
  292. form.Controls.Add (fileTypeComboBox);
  293. form.Controls.Add (fileNameComboBox);
  294. form.Controls.Add (dirComboBox);
  295. form.Controls.Add (searchSaveLabel);
  296. form.Controls.Add (popupButtonPanel);
  297. form.Controls.Add (helpButton);
  298. form.Controls.Add (readonlyCheckBox);
  299. form.ResumeLayout (true);
  300. if (formConfigSize != Size.Empty) {
  301. form.ClientSize = formConfigSize;
  302. }
  303. if (formConfigLocation != Point.Empty) {
  304. form.Location = formConfigLocation;
  305. }
  306. openSaveButton.Click += new EventHandler (OnClickOpenSaveButton);
  307. cancelButton.Click += new EventHandler (OnClickCancelButton);
  308. helpButton.Click += new EventHandler (OnClickHelpButton);
  309. smallButtonToolBar.ButtonClick += new ToolBarButtonClickEventHandler (OnClickSmallButtonToolBar);
  310. fileTypeComboBox.SelectedIndexChanged += new EventHandler (OnSelectedIndexChangedFileTypeComboBox);
  311. mwfFileView.SelectedFileChanged += new EventHandler (OnSelectedFileChangedFileView);
  312. mwfFileView.ForceDialogEnd += new EventHandler (OnForceDialogEndFileView);
  313. mwfFileView.SelectedFilesChanged += new EventHandler (OnSelectedFilesChangedFileView);
  314. mwfFileView.ColumnClick += new ColumnClickEventHandler(OnColumnClickFileView);
  315. dirComboBox.DirectoryChanged += new EventHandler (OnDirectoryChangedDirComboBox);
  316. popupButtonPanel.DirectoryChanged += new EventHandler (OnDirectoryChangedPopupButtonPanel);
  317. readonlyCheckBox.CheckedChanged += new EventHandler (OnCheckCheckChanged);
  318. form.FormClosed += new FormClosedEventHandler (OnFileDialogFormClosed);
  319. custom_places = new FileDialogCustomPlacesCollection ();
  320. }
  321. [DefaultValue(true)]
  322. public bool AddExtension {
  323. get {
  324. return addExtension;
  325. }
  326. set {
  327. addExtension = value;
  328. }
  329. }
  330. [MonoTODO ("Stub, value not respected")]
  331. [DefaultValue (true)]
  332. public bool AutoUpgradeEnabled {
  333. get { return auto_upgrade_enable; }
  334. set { auto_upgrade_enable = value; }
  335. }
  336. [DefaultValue(false)]
  337. public virtual bool CheckFileExists {
  338. get {
  339. return checkFileExists;
  340. }
  341. set {
  342. checkFileExists = value;
  343. }
  344. }
  345. [DefaultValue(true)]
  346. public bool CheckPathExists {
  347. get {
  348. return checkPathExists;
  349. }
  350. set {
  351. checkPathExists = value;
  352. }
  353. }
  354. [MonoTODO ("Stub, collection not used")]
  355. [Browsable (false)]
  356. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  357. public FileDialogCustomPlacesCollection CustomPlaces {
  358. get { return custom_places; }
  359. }
  360. [DefaultValue("")]
  361. public string DefaultExt {
  362. get {
  363. if (defaultExt == null)
  364. return string.Empty;
  365. return defaultExt;
  366. }
  367. set {
  368. if (value != null && value.Length > 0) {
  369. // remove leading dot
  370. if (value [0] == '.')
  371. value = value.Substring (1);
  372. }
  373. defaultExt = value;
  374. }
  375. }
  376. // in MS.NET it doesn't make a difference if
  377. // DerefenceLinks is true or false
  378. // if the selected file is a link FileDialog
  379. // always returns the link
  380. [DefaultValue(true)]
  381. public bool DereferenceLinks {
  382. get {
  383. return dereferenceLinks;
  384. }
  385. set {
  386. dereferenceLinks = value;
  387. }
  388. }
  389. [DefaultValue("")]
  390. public string FileName {
  391. get {
  392. if (fileNames == null || fileNames.Length == 0)
  393. return string.Empty;
  394. if (fileNames [0].Length == 0)
  395. return string.Empty;
  396. // skip check for illegal characters if the filename was set
  397. // through FileDialog API
  398. if (!checkForIllegalChars)
  399. return fileNames [0];
  400. // ensure filename contains only valid characters
  401. Path.GetFullPath (fileNames [0]);
  402. // but return filename as is
  403. return fileNames [0];
  404. }
  405. set {
  406. if (value != null) {
  407. fileNames = new string [1] { value };
  408. } else {
  409. fileNames = new string [0];
  410. }
  411. // skip check for illegal characters if the filename was set
  412. // through FileDialog API
  413. checkForIllegalChars = false;
  414. }
  415. }
  416. [Browsable(false)]
  417. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  418. public string[] FileNames {
  419. get {
  420. if (fileNames == null || fileNames.Length == 0) {
  421. return new string [0];
  422. }
  423. string[] new_filenames = new string [fileNames.Length];
  424. fileNames.CopyTo (new_filenames, 0);
  425. // skip check for illegal characters if the filename was set
  426. // through FileDialog API
  427. if (!checkForIllegalChars)
  428. return new_filenames;
  429. foreach (string fileName in new_filenames) {
  430. // ensure filename contains only valid characters
  431. Path.GetFullPath (fileName);
  432. }
  433. return new_filenames;
  434. }
  435. }
  436. [DefaultValue("")]
  437. [Localizable(true)]
  438. public string Filter {
  439. get {
  440. return filter;
  441. }
  442. set {
  443. if (value == null) {
  444. filter = "";
  445. if (fileFilter != null)
  446. fileFilter.FilterArrayList.Clear ();
  447. } else {
  448. if (FileFilter.CheckFilter (value)) {
  449. filter = value;
  450. fileFilter = new FileFilter (filter);
  451. } else
  452. throw new ArgumentException ("The provided filter string"
  453. + " is invalid. The filter string should contain a"
  454. + " description of the filter, followed by the "
  455. + " vertical bar (|) and the filter pattern. The"
  456. + " strings for different filtering options should"
  457. + " also be separated by the vertical bar. Example:"
  458. + " Text files (*.txt)|*.txt|All files (*.*)|*.*");
  459. }
  460. UpdateFilters ();
  461. }
  462. }
  463. [DefaultValue(1)]
  464. public int FilterIndex {
  465. get {
  466. return filterIndex;
  467. }
  468. set {
  469. filterIndex = value;
  470. }
  471. }
  472. [DefaultValue("")]
  473. public string InitialDirectory {
  474. get {
  475. if (initialDirectory == null)
  476. return string.Empty;
  477. return initialDirectory;
  478. }
  479. set {
  480. initialDirectory = value;
  481. }
  482. }
  483. [DefaultValue(false)]
  484. public bool RestoreDirectory {
  485. get {
  486. return restoreDirectory;
  487. }
  488. set {
  489. restoreDirectory = value;
  490. }
  491. }
  492. [DefaultValue(false)]
  493. public bool ShowHelp {
  494. get {
  495. return showHelp;
  496. }
  497. set {
  498. showHelp = value;
  499. ResizeAndRelocateForHelpOrReadOnly ();
  500. }
  501. }
  502. [DefaultValue(false)]
  503. public bool SupportMultiDottedExtensions {
  504. get {
  505. return supportMultiDottedExtensions;
  506. }
  507. set {
  508. supportMultiDottedExtensions = value;
  509. }
  510. }
  511. [DefaultValue("")]
  512. [Localizable(true)]
  513. public string Title {
  514. get {
  515. if (title == null)
  516. return string.Empty;
  517. return title;
  518. }
  519. set {
  520. title = value;
  521. }
  522. }
  523. // this one is a hard one ;)
  524. // Win32 filename:
  525. // - up to MAX_PATH characters (windef.h) = 260
  526. // - no trailing dots or spaces
  527. // - case preserving
  528. // - etc...
  529. // NTFS/Posix filename:
  530. // - up to 32,768 Unicode characters
  531. // - trailing periods or spaces
  532. // - case sensitive
  533. // - etc...
  534. [DefaultValue(true)]
  535. public bool ValidateNames {
  536. get {
  537. return validateNames;
  538. }
  539. set {
  540. validateNames = value;
  541. }
  542. }
  543. public override void Reset ()
  544. {
  545. addExtension = true;
  546. checkFileExists = false;
  547. checkPathExists = true;
  548. DefaultExt = null;
  549. dereferenceLinks = true;
  550. FileName = null;
  551. Filter = String.Empty;
  552. FilterIndex = 1;
  553. InitialDirectory = null;
  554. restoreDirectory = false;
  555. SupportMultiDottedExtensions = false;
  556. ShowHelp = false;
  557. Title = null;
  558. validateNames = true;
  559. UpdateFilters ();
  560. }
  561. public override string ToString ()
  562. {
  563. return String.Format("{0}: Title: {1}, FileName: {2}", base.ToString (), Title, FileName);
  564. }
  565. public event CancelEventHandler FileOk {
  566. add { Events.AddHandler (EventFileOk, value); }
  567. remove { Events.RemoveHandler (EventFileOk, value); }
  568. }
  569. protected virtual IntPtr Instance {
  570. get {
  571. if (form == null)
  572. return IntPtr.Zero;
  573. return form.Handle;
  574. }
  575. }
  576. // This is just for internal use with MSs version, so it doesn't need to be implemented
  577. // as it can't really be accessed anyways
  578. protected int Options {
  579. get { return -1; }
  580. }
  581. internal virtual string DialogTitle {
  582. get {
  583. return Title;
  584. }
  585. }
  586. [MonoTODO ("Not implemented, will throw NotImplementedException")]
  587. protected override IntPtr HookProc (IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam)
  588. {
  589. throw new NotImplementedException ();
  590. }
  591. protected void OnFileOk (CancelEventArgs e)
  592. {
  593. CancelEventHandler fo = (CancelEventHandler) Events [EventFileOk];
  594. if (fo != null)
  595. fo (this, e);
  596. }
  597. private void CleanupOnClose ()
  598. {
  599. WriteConfigValues ();
  600. Mime.CleanFileCache ();
  601. disable_form_closed_event = true;
  602. }
  603. protected override bool RunDialog (IntPtr hWndOwner)
  604. {
  605. ReadConfigValues ();
  606. form.Text = DialogTitle;
  607. // avoid using the FileNames property to skip the invalid characters
  608. // check
  609. string fileName = null;
  610. if (fileNames != null && fileNames.Length != 0)
  611. fileName = fileNames [0];
  612. else
  613. fileName = string.Empty;
  614. SelectFilter ();
  615. form.Refresh ();
  616. SetFileAndDirectory (fileName);
  617. fileNameComboBox.Select ();
  618. return true;
  619. }
  620. internal virtual bool ShowReadOnly {
  621. set {
  622. showReadOnly = value;
  623. ResizeAndRelocateForHelpOrReadOnly ();
  624. }
  625. get {
  626. return showReadOnly;
  627. }
  628. }
  629. internal virtual bool ReadOnlyChecked {
  630. set {
  631. readOnlyChecked = value;
  632. readonlyCheckBox.Checked = value;
  633. }
  634. get {
  635. return readOnlyChecked;
  636. }
  637. }
  638. internal bool BMultiSelect {
  639. set {
  640. multiSelect = value;
  641. mwfFileView.MultiSelect = value;
  642. }
  643. get {
  644. return multiSelect;
  645. }
  646. }
  647. internal string OpenSaveButtonText {
  648. set {
  649. openSaveButton.Text = value;
  650. }
  651. }
  652. internal string SearchSaveLabel {
  653. set {
  654. searchSaveLabel.Text = value;
  655. }
  656. }
  657. internal string FileTypeLabel {
  658. set {
  659. fileTypeLabel.Text = value;
  660. }
  661. }
  662. internal string CustomFilter {
  663. get {
  664. string fname = fileNameComboBox.Text;
  665. if (fname.IndexOfAny (wildcard_chars) == -1)
  666. return null;
  667. return fname;
  668. }
  669. }
  670. private void SelectFilter ()
  671. {
  672. int filter_to_select = (filterIndex - 1);
  673. if (mwfFileView.FilterArrayList == null || mwfFileView.FilterArrayList.Count == 0) {
  674. filter_to_select = -1;
  675. } else {
  676. if (filter_to_select < 0 || filter_to_select >= mwfFileView.FilterArrayList.Count)
  677. filter_to_select = 0;
  678. }
  679. do_not_call_OnSelectedIndexChangedFileTypeComboBox = true;
  680. fileTypeComboBox.BeginUpdate ();
  681. fileTypeComboBox.SelectedIndex = filter_to_select;
  682. fileTypeComboBox.EndUpdate ();
  683. do_not_call_OnSelectedIndexChangedFileTypeComboBox = false;
  684. mwfFileView.FilterIndex = filter_to_select + 1;
  685. }
  686. private void SetFileAndDirectory (string fname)
  687. {
  688. if (fname.Length != 0) {
  689. bool rooted = Path.IsPathRooted (fname);
  690. if (!rooted) {
  691. mwfFileView.ChangeDirectory (null, lastFolder);
  692. fileNameComboBox.Text = fname;
  693. } else {
  694. string dirname = Path.GetDirectoryName (fname);
  695. if (dirname != null && dirname.Length > 0 && Directory.Exists (dirname)) {
  696. fileNameComboBox.Text = Path.GetFileName (fname);
  697. mwfFileView.ChangeDirectory (null, dirname);
  698. } else {
  699. fileNameComboBox.Text = fname;
  700. mwfFileView.ChangeDirectory (null, lastFolder);
  701. }
  702. }
  703. } else {
  704. mwfFileView.ChangeDirectory (null, lastFolder);
  705. fileNameComboBox.Text = null;
  706. }
  707. }
  708. void OnClickOpenSaveButton (object sender, EventArgs e)
  709. {
  710. // for filenames typed or selected by user, enable check for
  711. // illegal characters in filename(s)
  712. checkForIllegalChars = true;
  713. if (fileDialogType == FileDialogType.OpenFileDialog) {
  714. ListView.SelectedListViewItemCollection sl = mwfFileView.SelectedItems;
  715. if (sl.Count > 0 && sl [0] != null) {
  716. if (sl.Count == 1) {
  717. FileViewListViewItem item = sl [0] as FileViewListViewItem;
  718. FSEntry fsEntry = item.FSEntry;
  719. if ((fsEntry.Attributes & FileAttributes.Directory) == FileAttributes.Directory) {
  720. mwfFileView.ChangeDirectory (null, fsEntry.FullName, CustomFilter);
  721. return;
  722. }
  723. } else {
  724. foreach (FileViewListViewItem item in sl) {
  725. FSEntry fsEntry = item.FSEntry;
  726. if ((fsEntry.Attributes & FileAttributes.Directory) == FileAttributes.Directory) {
  727. mwfFileView.ChangeDirectory (null, fsEntry.FullName, CustomFilter);
  728. return;
  729. }
  730. }
  731. }
  732. }
  733. }
  734. // Custom filter, typed by the user, ignoring the stored filters
  735. if (fileNameComboBox.Text.IndexOfAny (wildcard_chars) != -1) {
  736. mwfFileView.UpdateFileView (fileNameComboBox.Text);
  737. return;
  738. }
  739. ArrayList files = new ArrayList ();
  740. FileNamesTokenizer tokenizer = new FileNamesTokenizer (
  741. fileNameComboBox.Text, multiSelect);
  742. tokenizer.GetNextFile ();
  743. while (tokenizer.CurrentToken != TokenType.EOF) {
  744. string fileName = tokenizer.TokenText;
  745. string internalfullfilename;
  746. if (!Path.IsPathRooted (fileName)) {
  747. // on unix currentRealFolder for "Recently used files" is null,
  748. // because recently used files don't get saved as links in a directory
  749. // recently used files get saved in a xml file
  750. if (mwfFileView.CurrentRealFolder != null)
  751. fileName = Path.Combine (mwfFileView.CurrentRealFolder, fileName);
  752. else
  753. if (mwfFileView.CurrentFSEntry != null) {
  754. fileName = mwfFileView.CurrentFSEntry.FullName;
  755. }
  756. }
  757. FileInfo fileInfo = new FileInfo (fileName);
  758. if (fileInfo.Exists || fileDialogType == FileDialogType.SaveFileDialog) {
  759. internalfullfilename = fileName;
  760. } else {
  761. DirectoryInfo dirInfo = new DirectoryInfo (fileName);
  762. if (dirInfo.Exists) {
  763. mwfFileView.ChangeDirectory (null, dirInfo.FullName, CustomFilter);
  764. fileNameComboBox.Text = null;
  765. return;
  766. } else {
  767. internalfullfilename = fileName;
  768. }
  769. }
  770. if (addExtension) {
  771. string current_extension = Path.GetExtension (fileName);
  772. if (current_extension.Length == 0) {
  773. string filter_extension = string.Empty;
  774. if (AddFilterExtension (internalfullfilename))
  775. filter_extension = GetExtension (internalfullfilename);
  776. if (filter_extension.Length == 0 && DefaultExt.Length > 0) {
  777. filter_extension = "." + DefaultExt;
  778. if (checkFileExists) {
  779. // ignore DefaultExt if file not exist
  780. if (!File.Exists (internalfullfilename + filter_extension))
  781. filter_extension = string.Empty;
  782. }
  783. }
  784. internalfullfilename += filter_extension;
  785. }
  786. }
  787. if (checkFileExists) {
  788. if (!File.Exists (internalfullfilename)) {
  789. string message = "\"" + internalfullfilename + "\" does not exist. Please verify that you have entered the correct file name.";
  790. MessageBox.Show (message, openSaveButton.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  791. return;
  792. }
  793. }
  794. if (fileDialogType == FileDialogType.SaveFileDialog) {
  795. if (overwritePrompt) {
  796. if (File.Exists (internalfullfilename)) {
  797. string message = "\"" + internalfullfilename + "\" already exists. Do you want to overwrite it?";
  798. DialogResult dr = MessageBox.Show (message, openSaveButton.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
  799. if (dr == DialogResult.Cancel)
  800. return;
  801. }
  802. }
  803. if (createPrompt) {
  804. if (!File.Exists (internalfullfilename)) {
  805. string message = "\"" + internalfullfilename + "\" does not exist. Do you want to create it?";
  806. DialogResult dr = MessageBox.Show (message, openSaveButton.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
  807. if (dr == DialogResult.Cancel)
  808. return;
  809. }
  810. }
  811. }
  812. files.Add (internalfullfilename);
  813. tokenizer.GetNextFile ();
  814. }
  815. if (files.Count > 0) {
  816. fileNames = new string [files.Count];
  817. for (int i = 0; i < files.Count; i++) {
  818. string fileName = (string) files [i];
  819. fileNames [i] = fileName;
  820. mwfFileView.WriteRecentlyUsed (fileName);
  821. if (!File.Exists (fileName))
  822. // ignore files that do not exist
  823. continue;
  824. if (fileNameComboBox.Items.IndexOf (fileName) == -1)
  825. fileNameComboBox.Items.Insert (0, fileName);
  826. }
  827. // remove items above the maximum items that we want to display
  828. while (fileNameComboBox.Items.Count > MaxFileNameItems)
  829. fileNameComboBox.Items.RemoveAt (MaxFileNameItems);
  830. } else {
  831. // If a directory is selected, navigate into it
  832. foreach (FileViewListViewItem item in mwfFileView.SelectedItems) {
  833. FSEntry fsEntry = item.FSEntry;
  834. if ((fsEntry.Attributes & FileAttributes.Directory) == FileAttributes.Directory) {
  835. mwfFileView.ChangeDirectory (null, fsEntry.FullName, CustomFilter);
  836. return;
  837. }
  838. }
  839. return;
  840. }
  841. if (checkPathExists && mwfFileView.CurrentRealFolder != null) {
  842. if (!Directory.Exists (mwfFileView.CurrentRealFolder)) {
  843. string message = "\"" + mwfFileView.CurrentRealFolder + "\" does not exist. Please verify that you have entered the correct directory name.";
  844. MessageBox.Show (message, openSaveButton.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  845. if (InitialDirectory.Length == 0 || !Directory.Exists (InitialDirectory))
  846. mwfFileView.ChangeDirectory (null, lastFolder, CustomFilter);
  847. else
  848. mwfFileView.ChangeDirectory (null, InitialDirectory, CustomFilter);
  849. return;
  850. }
  851. }
  852. if (restoreDirectory) {
  853. lastFolder = restoreDirectoryString;
  854. } else {
  855. lastFolder = mwfFileView.CurrentFolder;
  856. }
  857. // update value of FilterIndex with user-selected filter
  858. filterIndex = fileTypeComboBox.SelectedIndex + 1;
  859. CancelEventArgs cancelEventArgs = new CancelEventArgs ();
  860. OnFileOk (cancelEventArgs);
  861. if (cancelEventArgs.Cancel)
  862. return;
  863. CleanupOnClose ();
  864. form.DialogResult = DialogResult.OK;
  865. }
  866. bool AddFilterExtension (string fileName)
  867. {
  868. if (fileDialogType == FileDialogType.OpenFileDialog) {
  869. if (DefaultExt.Length == 0)
  870. return true;
  871. if (checkFileExists) {
  872. // if CheckFileExists is true, only add filter extension if
  873. // file with DefaultExt does not exist
  874. string fullFileName = fileName + "." + DefaultExt;
  875. return !File.Exists (fullFileName);
  876. } else {
  877. // if CheckFileExists is false, only add filter extension
  878. // if specified file does not exist
  879. return !File.Exists (fileName);
  880. }
  881. }
  882. return true;
  883. }
  884. string GetExtension (string fileName)
  885. {
  886. string filter_extension = String.Empty;
  887. if (fileFilter == null || fileTypeComboBox.SelectedIndex == -1)
  888. return filter_extension;
  889. FilterStruct filterstruct = (FilterStruct) fileFilter.FilterArrayList
  890. [fileTypeComboBox.SelectedIndex];
  891. for (int i = 0; i < filterstruct.filters.Count; i++) {
  892. string extension = filterstruct.filters [i];
  893. if (extension.StartsWith ("*"))
  894. extension = extension.Remove (0, 1);
  895. if (extension.IndexOf ('*') != -1)
  896. continue;
  897. if (!supportMultiDottedExtensions) {
  898. int lastdot = extension.LastIndexOf('.');
  899. if (lastdot > 0) {
  900. if (extension.LastIndexOf('.', lastdot - 1) != -1) {
  901. extension = extension.Remove(0, lastdot);
  902. }
  903. }
  904. }
  905. if (!checkFileExists) {
  906. filter_extension = extension;
  907. break;
  908. }
  909. if (fileDialogType == FileDialogType.SaveFileDialog) {
  910. // when DefaultExt is set, only consider first filter
  911. // extension (and do not check if file exists)
  912. if (DefaultExt.Length > 0) {
  913. filter_extension = extension;
  914. break;
  915. }
  916. }
  917. // MSDN: If the CheckFileExists property is true,
  918. // the dialog box adds the first extension from the
  919. // current file filter that matches an existing file
  920. string fullfilename = fileName + extension;
  921. if (File.Exists (fullfilename)) {
  922. filter_extension = extension;
  923. break;
  924. } else {
  925. if (fileDialogType == FileDialogType.SaveFileDialog) {
  926. // when DefaultExt is set, only consider first filter
  927. // extension
  928. if (DefaultExt.Length > 0) {
  929. filter_extension = extension;
  930. break;
  931. }
  932. }
  933. }
  934. }
  935. return filter_extension;
  936. }
  937. void OnClickCancelButton (object sender, EventArgs e)
  938. {
  939. if (restoreDirectory)
  940. mwfFileView.CurrentFolder = restoreDirectoryString;
  941. CleanupOnClose ();
  942. form.DialogResult = DialogResult.Cancel;
  943. }
  944. void OnClickHelpButton (object sender, EventArgs e)
  945. {
  946. OnHelpRequest (e);
  947. }
  948. void OnClickSmallButtonToolBar (object sender, ToolBarButtonClickEventArgs e)
  949. {
  950. if (e.Button == upToolBarButton) {
  951. mwfFileView.OneDirUp (CustomFilter);
  952. } else
  953. if (e.Button == backToolBarButton) {
  954. mwfFileView.PopDir (CustomFilter);
  955. } else
  956. if (e.Button == newdirToolBarButton) {
  957. mwfFileView.CreateNewFolder ();
  958. }
  959. }
  960. void OnSelectedIndexChangedFileTypeComboBox (object sender, EventArgs e)
  961. {
  962. if (do_not_call_OnSelectedIndexChangedFileTypeComboBox) {
  963. do_not_call_OnSelectedIndexChangedFileTypeComboBox = false;
  964. return;
  965. }
  966. UpdateRecentFiles ();
  967. mwfFileView.FilterIndex = fileTypeComboBox.SelectedIndex + 1;
  968. }
  969. void OnSelectedFileChangedFileView (object sender, EventArgs e)
  970. {
  971. fileNameComboBox.Text = mwfFileView.CurrentFSEntry.Name;
  972. }
  973. void OnSelectedFilesChangedFileView (object sender, EventArgs e)
  974. {
  975. string selectedFiles = mwfFileView.SelectedFilesString;
  976. if (selectedFiles != null && selectedFiles.Length != 0)
  977. fileNameComboBox.Text = selectedFiles;
  978. }
  979. void OnForceDialogEndFileView (object sender, EventArgs e)
  980. {
  981. OnClickOpenSaveButton (this, EventArgs.Empty);
  982. }
  983. void OnDirectoryChangedDirComboBox (object sender, EventArgs e)
  984. {
  985. mwfFileView.ChangeDirectory (sender, dirComboBox.CurrentFolder, CustomFilter);
  986. }
  987. void OnDirectoryChangedPopupButtonPanel (object sender, EventArgs e)
  988. {
  989. mwfFileView.ChangeDirectory (sender, popupButtonPanel.CurrentFolder, CustomFilter);
  990. }
  991. void OnCheckCheckChanged (object sender, EventArgs e)
  992. {
  993. ReadOnlyChecked = readonlyCheckBox.Checked;
  994. }
  995. void OnFileDialogFormClosed (object sender, FormClosedEventArgs e)
  996. {
  997. HandleFormClosedEvent (sender);
  998. }
  999. private void OnColumnClickFileView (object sender, ColumnClickEventArgs e)
  1000. {
  1001. if (file_view_comparer == null)
  1002. file_view_comparer = new MwfFileViewItemComparer (true);
  1003. file_view_comparer.ColumnIndex = e.Column;
  1004. file_view_comparer.Ascendent = !file_view_comparer.Ascendent;
  1005. if (mwfFileView.ListViewItemSorter == null)
  1006. mwfFileView.ListViewItemSorter = file_view_comparer;
  1007. else
  1008. mwfFileView.Sort ();
  1009. }
  1010. void HandleFormClosedEvent (object sender)
  1011. {
  1012. if (!disable_form_closed_event)
  1013. OnClickCancelButton (sender, EventArgs.Empty);
  1014. disable_form_closed_event = false;
  1015. }
  1016. private void UpdateFilters ()
  1017. {
  1018. if (fileFilter == null)
  1019. fileFilter = new FileFilter ();
  1020. ArrayList filters = fileFilter.FilterArrayList;
  1021. fileTypeComboBox.BeginUpdate ();
  1022. fileTypeComboBox.Items.Clear ();
  1023. foreach (FilterStruct fs in filters) {
  1024. fileTypeComboBox.Items.Add (fs.filterName);
  1025. }
  1026. fileTypeComboBox.EndUpdate ();
  1027. mwfFileView.FilterArrayList = filters;
  1028. }
  1029. private void UpdateRecentFiles ()
  1030. {
  1031. fileNameComboBox.Items.Clear ();
  1032. if (configFileNames != null) {
  1033. foreach (string configFileName in configFileNames) {
  1034. if (configFileName == null || configFileName.Trim ().Length == 0)
  1035. continue;
  1036. // add no more than 10 items
  1037. if (fileNameComboBox.Items.Count >= MaxFileNameItems)
  1038. break;
  1039. fileNameComboBox.Items.Add (configFileName);
  1040. }
  1041. }
  1042. }
  1043. private void ResizeAndRelocateForHelpOrReadOnly ()
  1044. {
  1045. form.SuspendLayout ();
  1046. int fx = form.Size.Width - form.MinimumSize.Width;
  1047. int fy = form.Size.Height - form.MinimumSize.Height;
  1048. if (!ShowHelp && !ShowReadOnly)
  1049. fy += 29;
  1050. mwfFileView.Size = new Size (450 + fx, 254 + fy);
  1051. fileNameLabel.Location = new Point (101, 298 + fy);
  1052. fileNameComboBox.Location = new Point (195, 298 + fy);
  1053. fileTypeLabel.Location = new Point (101, 326 + fy);
  1054. fileTypeComboBox.Location = new Point (195, 326 + fy);
  1055. openSaveButton.Location = new Point (474 + fx, 298 + fy);
  1056. cancelButton.Location = new Point (474 + fx, 324 + fy);
  1057. helpButton.Location = new Point (474 + fx, 353 + fy);
  1058. readonlyCheckBox.Location = new Point (195, 350 + fy);
  1059. helpButton.Visible = ShowHelp;
  1060. readonlyCheckBox.Visible = ShowReadOnly;
  1061. form.ResumeLayout ();
  1062. }
  1063. private void WriteConfigValues ()
  1064. {
  1065. MWFConfig.SetValue (filedialog_string, width_string, form.ClientSize.Width);
  1066. MWFConfig.SetValue (filedialog_string, height_string, form.ClientSize.Height);
  1067. MWFConfig.SetValue (filedialog_string, x_string, form.Location.X);
  1068. MWFConfig.SetValue (filedialog_string, y_string, form.Location.Y);
  1069. MWFConfig.SetValue (filedialog_string, lastfolder_string, lastFolder);
  1070. string[] fileNameCBItems = new string [fileNameComboBox.Items.Count];
  1071. fileNameComboBox.Items.CopyTo (fileNameCBItems, 0);
  1072. MWFConfig.SetValue (filedialog_string, filenames_string, fileNameCBItems);
  1073. }
  1074. private void ReadConfigValues ()
  1075. {
  1076. lastFolder = (string)MWFConfig.GetValue (filedialog_string, lastfolder_string);
  1077. if (lastFolder != null && lastFolder.IndexOf ("://") == -1) {
  1078. if (!Directory.Exists (lastFolder)) {
  1079. lastFolder = MWFVFS.DesktopPrefix;
  1080. }
  1081. }
  1082. if (InitialDirectory.Length > 0 && Directory.Exists (InitialDirectory))
  1083. lastFolder = InitialDirectory;
  1084. else
  1085. if (lastFolder == null || lastFolder.Length == 0)
  1086. lastFolder = Environment.CurrentDirectory;
  1087. if (RestoreDirectory)
  1088. restoreDirectoryString = lastFolder;
  1089. }
  1090. class FileNamesTokenizer
  1091. {
  1092. public FileNamesTokenizer (string text, bool allowMultiple)
  1093. {
  1094. _text = text;
  1095. _position = 0;
  1096. _tokenType = TokenType.BOF;
  1097. _allowMultiple = allowMultiple;
  1098. }
  1099. public TokenType CurrentToken {
  1100. get { return _tokenType; }
  1101. }
  1102. public string TokenText {
  1103. get { return _tokenText; }
  1104. }
  1105. public bool AllowMultiple {
  1106. get { return _allowMultiple; }
  1107. }
  1108. private int ReadChar ()
  1109. {
  1110. if (_position < _text.Length) {
  1111. return _text [_position++];
  1112. } else {
  1113. return -1;
  1114. }
  1115. }
  1116. private int PeekChar ()
  1117. {
  1118. if (_position < _text.Length) {
  1119. return _text [_position];
  1120. } else {
  1121. return -1;
  1122. }
  1123. }
  1124. private void SkipWhitespaceAndQuotes ()
  1125. {
  1126. int ch;
  1127. while ((ch = PeekChar ()) != -1) {
  1128. if ((char) ch != '"' && !char.IsWhiteSpace ((char) ch))
  1129. break;
  1130. ReadChar ();
  1131. }
  1132. }
  1133. public void GetNextFile ()
  1134. {
  1135. if (_tokenType == TokenType.EOF)
  1136. throw new Exception ("");
  1137. int ch;
  1138. SkipWhitespaceAndQuotes ();
  1139. if (PeekChar () == -1) {
  1140. _tokenType = TokenType.EOF;
  1141. return;
  1142. }
  1143. _tokenType = TokenType.FileName;
  1144. StringBuilder sb = new StringBuilder ();
  1145. while ((ch = PeekChar ()) != -1) {
  1146. if ((char) ch == '"') {
  1147. ReadChar ();
  1148. if (AllowMultiple)
  1149. break;
  1150. int pos = _position;
  1151. SkipWhitespaceAndQuotes ();
  1152. if (PeekChar () == -1) {
  1153. break;
  1154. }
  1155. _position = ++pos;
  1156. sb.Append ((char) ch);
  1157. } else {
  1158. sb.Append ((char) ReadChar ());
  1159. }
  1160. }
  1161. _tokenText = sb.ToString ();
  1162. }
  1163. private readonly bool _allowMultiple;
  1164. private int _position;
  1165. private readonly string _text;
  1166. private TokenType _tokenType;
  1167. private string _tokenText;
  1168. }
  1169. internal enum TokenType
  1170. {
  1171. BOF,
  1172. EOF,
  1173. FileName,
  1174. }
  1175. }
  1176. #endregion
  1177. #region PopupButtonPanel
  1178. internal class PopupButtonPanel : Control, IUpdateFolder
  1179. {
  1180. #region PopupButton
  1181. internal class PopupButton : Control
  1182. {
  1183. internal enum PopupButtonState
  1184. { Normal, Down, Up}
  1185. private Image image = null;
  1186. private PopupButtonState popupButtonState = PopupButtonState.Normal;
  1187. private StringFormat text_format = new StringFormat();
  1188. private Rectangle text_rect = Rectangle.Empty;
  1189. public PopupButton ()
  1190. {
  1191. text_format.Alignment = StringAlignment.Center;
  1192. text_format.LineAlignment = StringAlignment.Near;
  1193. SetStyle (ControlStyles.DoubleBuffer, true);
  1194. SetStyle (ControlStyles.AllPaintingInWmPaint, true);
  1195. SetStyle (ControlStyles.UserPaint, true);
  1196. SetStyle (ControlStyles.Selectable, false);
  1197. }
  1198. public Image Image {
  1199. set {
  1200. image = value;
  1201. Invalidate ();
  1202. }
  1203. get {
  1204. return image;
  1205. }
  1206. }
  1207. public PopupButtonState ButtonState {
  1208. set {
  1209. popupButtonState = value;
  1210. Invalidate ();
  1211. }
  1212. get {
  1213. return popupButtonState;
  1214. }
  1215. }
  1216. #region UIA Framework Members
  1217. internal void PerformClick ()
  1218. {
  1219. OnClick (EventArgs.Empty);
  1220. }
  1221. #endregion
  1222. protected override void OnPaint (PaintEventArgs pe)
  1223. {
  1224. Draw (pe);
  1225. base.OnPaint (pe);
  1226. }
  1227. private void Draw (PaintEventArgs pe)
  1228. {
  1229. Graphics gr = pe.Graphics;
  1230. gr.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (BackColor), ClientRectangle);
  1231. // draw image
  1232. if (image != null) {
  1233. int i_x = (ClientSize.Width - image.Width) / 2;
  1234. int i_y = 4;
  1235. gr.DrawImage (image, i_x, i_y);
  1236. }
  1237. if (Text != String.Empty) {
  1238. if (text_rect == Rectangle.Empty)
  1239. text_rect = new Rectangle (0, Height - 30, Width, Height - 30);
  1240. gr.DrawString (Text, Font, Brushes.White, text_rect, text_format);
  1241. }
  1242. switch (popupButtonState) {
  1243. case PopupButtonState.Up:
  1244. gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.White), 0, 0, ClientSize.Width - 1, 0);
  1245. gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.White), 0, 0, 0, ClientSize.Height - 1);
  1246. gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.Black), ClientSize.Width - 1, 0, ClientSize.Width - 1, ClientSize.Height - 1);
  1247. gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.Black), 0, ClientSize.Height - 1, ClientSize.Width - 1, ClientSize.Height - 1);
  1248. break;
  1249. case PopupButtonState.Down:
  1250. gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.Black), 0, 0, ClientSize.Width - 1, 0);
  1251. gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.Black), 0, 0, 0, ClientSize.Height - 1);
  1252. gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.White), ClientSize.Width - 1, 0, ClientSize.Width - 1, ClientSize.Height - 1);
  1253. gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.White), 0, ClientSize.Height - 1, ClientSize.Width - 1, ClientSize.Height - 1);
  1254. break;
  1255. }
  1256. }
  1257. protected override void OnMouseEnter (EventArgs e)
  1258. {
  1259. if (popupButtonState != PopupButtonState.Down)
  1260. popupButtonState = PopupButtonState.Up;
  1261. PopupButtonPanel panel = Parent as PopupButtonPanel;
  1262. if (panel.focusButton != null && panel.focusButton.ButtonState == PopupButtonState.Up) {
  1263. panel.focusButton.ButtonState = PopupButtonState.Normal;
  1264. panel.SetFocusButton (null);
  1265. }
  1266. Invalidate ();
  1267. base.OnMouseEnter (e);
  1268. }
  1269. protected override void OnMouseLeave (EventArgs e)
  1270. {
  1271. if (popupButtonState == PopupButtonState.Up)
  1272. popupButtonState = PopupButtonState.Normal;
  1273. Invalidate ();
  1274. base.OnMouseLeave (e);
  1275. }
  1276. protected override void OnClick (EventArgs e)
  1277. {
  1278. popupButtonState = PopupButtonState.Down;
  1279. Invalidate ();
  1280. base.OnClick (e);
  1281. }
  1282. }
  1283. #endregion
  1284. private PopupButton recentlyusedButton;
  1285. private PopupButton desktopButton;
  1286. private PopupButton personalButton;
  1287. private PopupButton mycomputerButton;
  1288. private PopupButton networkButton;
  1289. private PopupButton lastPopupButton = null;
  1290. private PopupButton focusButton = null;
  1291. private string currentPath;
  1292. private int currentFocusIndex;
  1293. public PopupButtonPanel ()
  1294. {
  1295. SuspendLayout ();
  1296. BackColor = Color.FromArgb (128, 128, 128);
  1297. Size = new Size (85, 336);
  1298. InternalBorderStyle = BorderStyle.Fixed3D;
  1299. recentlyusedButton = new PopupButton ();
  1300. desktopButton = new PopupButton ();
  1301. personalButton = new PopupButton ();
  1302. mycomputerButton = new PopupButton ();
  1303. networkButton = new PopupButton ();
  1304. recentlyusedButton.Size = new Size (81, 64);
  1305. recentlyusedButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesRecentDocuments, 32);
  1306. recentlyusedButton.BackColor = BackColor;
  1307. recentlyusedButton.ForeColor = Color.Black;
  1308. recentlyusedButton.Location = new Point (2, 2);
  1309. recentlyusedButton.Text = "Recently\nused";
  1310. recentlyusedButton.Click += new EventHandler (OnClickButton);
  1311. desktopButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesDesktop, 32);
  1312. desktopButton.BackColor = BackColor;
  1313. desktopButton.ForeColor = Color.Black;
  1314. desktopButton.Size = new Size (81, 64);
  1315. desktopButton.Location = new Point (2, 66);
  1316. desktopButton.Text = "Desktop";
  1317. desktopButton.Click += new EventHandler (OnClickButton);
  1318. personalButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesPersonal, 32);
  1319. personalButton.BackColor = BackColor;
  1320. personalButton.ForeColor = Color.Black;
  1321. personalButton.Size = new Size (81, 64);
  1322. personalButton.Location = new Point (2, 130);
  1323. personalButton.Text = "Personal";
  1324. personalButton.Click += new EventHandler (OnClickButton);
  1325. mycomputerButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesMyComputer, 32);
  1326. mycomputerButton.BackColor = BackColor;
  1327. mycomputerButton.ForeColor = Color.Black;
  1328. mycomputerButton.Size = new Size (81, 64);
  1329. mycomputerButton.Location = new Point (2, 194);
  1330. mycomputerButton.Text = "My Computer";
  1331. mycomputerButton.Click += new EventHandler (OnClickButton);
  1332. networkButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesMyNetwork, 32);
  1333. networkButton.BackColor = BackColor;
  1334. networkButton.ForeColor = Color.Black;
  1335. networkButton.Size = new Size (81, 64);
  1336. networkButton.Location = new Point (2, 258);
  1337. networkButton.Text = "My Network";
  1338. networkButton.Click += new EventHandler (OnClickButton);
  1339. Controls.Add (recentlyusedButton);
  1340. Controls.Add (desktopButton);
  1341. Controls.Add (personalButton);
  1342. Controls.Add (mycomputerButton);
  1343. Controls.Add (networkButton);
  1344. ResumeLayout (false);
  1345. KeyDown += new KeyEventHandler (Key_Down);
  1346. SetStyle (ControlStyles.StandardClick, false);
  1347. }
  1348. void OnClickButton (object sender, EventArgs e)
  1349. {
  1350. if (lastPopupButton != null && lastPopupButton != sender as PopupButton)
  1351. lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
  1352. lastPopupButton = sender as PopupButton;
  1353. if (sender == recentlyusedButton) {
  1354. currentPath = MWFVFS.RecentlyUsedPrefix;
  1355. } else
  1356. if (sender == desktopButton) {
  1357. currentPath = MWFVFS.DesktopPrefix;
  1358. } else
  1359. if (sender == personalButton) {
  1360. currentPath = MWFVFS.PersonalPrefix;
  1361. } else
  1362. if (sender == mycomputerButton) {
  1363. currentPath = MWFVFS.MyComputerPrefix;
  1364. } else
  1365. if (sender == networkButton) {
  1366. currentPath = MWFVFS.MyNetworkPrefix;
  1367. }
  1368. EventHandler eh = (EventHandler)(Events [PDirectoryChangedEvent]);
  1369. if (eh != null)
  1370. eh (this, EventArgs.Empty);
  1371. }
  1372. static object UIAFocusedItemChangedEvent = new object ();
  1373. internal event EventHandler UIAFocusedItemChanged {
  1374. add { Events.AddHandler (UIAFocusedItemChangedEvent, value); }
  1375. remove { Events.RemoveHandler (UIAFocusedItemChangedEvent, value); }
  1376. }
  1377. internal void OnUIAFocusedItemChanged ()
  1378. {
  1379. EventHandler eh = (EventHandler) Events [UIAFocusedItemChangedEvent];
  1380. if (eh != null)
  1381. eh (this, EventArgs.Empty);
  1382. }
  1383. internal PopupButton UIAFocusButton {
  1384. get {
  1385. return focusButton;
  1386. }
  1387. }
  1388. public string CurrentFolder {
  1389. set {
  1390. string currentPath = value;
  1391. if (currentPath == MWFVFS.RecentlyUsedPrefix) {
  1392. if (lastPopupButton != recentlyusedButton) {
  1393. if (lastPopupButton != null)
  1394. lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
  1395. recentlyusedButton.ButtonState = PopupButton.PopupButtonState.Down;
  1396. lastPopupButton = recentlyusedButton;
  1397. }
  1398. } else
  1399. if (currentPath == MWFVFS.DesktopPrefix) {
  1400. if (lastPopupButton != desktopButton) {
  1401. if (lastPopupButton != null)
  1402. lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
  1403. desktopButton.ButtonState = PopupButton.PopupButtonState.Down;
  1404. lastPopupButton = desktopButton;
  1405. }
  1406. } else
  1407. if (currentPath == MWFVFS.PersonalPrefix) {
  1408. if (lastPopupButton != personalButton) {
  1409. if (lastPopupButton != null)
  1410. lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
  1411. personalButton.ButtonState = PopupButton.PopupButtonState.Down;
  1412. lastPopupButton = personalButton;
  1413. }
  1414. } else
  1415. if (currentPath == MWFVFS.MyComputerPrefix) {
  1416. if (lastPopupButton != mycomputerButton) {
  1417. if (lastPopupButton != null)
  1418. lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
  1419. mycomputerButton.ButtonState = PopupButton.PopupButtonState.Down;
  1420. lastPopupButton = mycomputerButton;
  1421. }
  1422. } else
  1423. if (currentPath == MWFVFS.MyNetworkPrefix) {
  1424. if (lastPopup

Large files files are truncated, but you can click here to view the full file