1/*
2 * Copyright (c) 2009, ETH Zurich.
3 * All rights reserved.
4 *
5 * This file is distributed under the terms in the attached LICENSE file.
6 * If you do not find this file, copies can be found by writing to:
7 * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
8 */
9
10using System;
11using System.Collections.Generic;
12using System.Text;
13using System.Windows.Forms;
14
15namespace Aquarium
16{
17    /// <summary>
18    /// This is a commonly copied file which makes it easy to add file load
19    /// and save dialogs to Avalon applications.  It does this using the
20    /// standard Windows *FORMS* dialogs.  Therefore this code must be in
21    /// a separate file with a separate set of "using" directives at the top
22    /// and you must also add System.Windows.Forms to the references for your
23    /// project.
24    /// </summary>
25    static class Dialogs
26    {
27        /// <summary>
28        /// Dialog box for user to select file to open.
29        /// </summary>
30        /// <param name="extension">Just the extension e.g. "foo"</param>
31        /// <param name="filter">Description of filter e.g. "Foo files|*.foo"</param>
32        /// <param name="filename">Out parameter filename chosen by user</param>
33        /// <param name="addAll">Permit user to change to show *.*</param>
34        /// <returns>true if user selected OK, false for Cancel</returns>
35        public static bool OpenThese(string extension, string filter,
36            out string filename, bool addAll)
37        {
38            filename = null;
39            OpenFileDialog dlg = new OpenFileDialog();
40            if (addAll)
41                filter = filter + "|All files|*.*";
42            dlg.Filter = filter;
43            dlg.DefaultExt = extension;
44            dlg.Multiselect = false;
45            dlg.CheckFileExists = true;
46            dlg.SupportMultiDottedExtensions = true;
47            DialogResult result = dlg.ShowDialog();
48            if (result == DialogResult.OK)
49            {
50                filename = dlg.FileName;
51                return true;
52            }
53            return false;
54        }
55
56        /// <summary>
57        /// Dialog box for user to select file to save.
58        /// </summary>
59        /// <param name="extension">Just the extension e.g. "foo"</param>
60        /// <param name="filter">Description of filter e.g. "Foo files|*.foo"</param>
61        /// <param name="filename">Out parameter filename chosen by user</param>
62        /// <param name="addAll">Permit user to change to show *.*</param>
63        /// <returns>true if user selected OK, false for Cancel</returns>
64        public static bool SaveThese(string extension, string filter,
65            out string filename, bool addAll)
66        {
67            filename = null;
68            SaveFileDialog dlg = new SaveFileDialog();
69            if (addAll)
70                filter = filter + "|All files|*.*";
71            dlg.Filter = filter;
72            dlg.DefaultExt = extension;
73            dlg.CheckFileExists = false;
74            dlg.SupportMultiDottedExtensions = true;
75            DialogResult result = dlg.ShowDialog();
76            if (result == DialogResult.OK)
77            {
78                filename = dlg.FileName;
79                return true;
80            }
81            return false;
82        }
83
84        /// <summary>
85        /// Dialog box for user to select file to open.  All also permitted.
86        /// </summary>
87        /// <param name="extension">Just the extension e.g. "foo"</param>
88        /// <param name="filter">Description of filter e.g. "Foo files|*.foo"</param>
89        /// <param name="filename">Out parameter filename chosen by user</param>
90        /// <returns>true if user selected OK, false for Cancel</returns>
91        public static bool OpenThese(string extension, string filter,
92            out string filename)
93        {
94            return OpenThese(extension, filter, out filename, true);
95        }
96
97
98        /// <summary>
99        /// Dialog box for user to select file to save.  All also permitted.
100        /// </summary>
101        /// <param name="extension">Just the extension e.g. "foo"</param>
102        /// <param name="filter">Description of filter e.g. "Foo files|*.foo"</param>
103        /// <param name="filename">Out parameter filename chosen by user</param>
104        /// <returns>true if user selected OK, false for Cancel</returns>
105        public static bool SaveThese(string extension, string filter,
106            out string filename)
107        {
108            return SaveThese(extension, filter, out filename, true);
109        }
110    } // static class Dialogs
111} // namespace
112