Mercurial > projects > dynamin
annotate dynamin/gui/file_dialog.d @ 4:fc2420d39e3c
Separate out Windows backend of FileDialog.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Sat, 20 Jun 2009 10:23:31 -0500 |
parents | aa4efef0f0b1 |
children | 4029d5af7542 |
rev | line source |
---|---|
0 | 1 // Written in the D programming language |
2 // www.digitalmars.com/d/ | |
3 | |
4 /* | |
5 * The contents of this file are subject to the Mozilla Public License Version | |
6 * 1.1 (the "License"); you may not use this file except in compliance with | |
7 * the License. You may obtain a copy of the License at | |
8 * http://www.mozilla.org/MPL/ | |
9 * | |
10 * Software distributed under the License is distributed on an "AS IS" basis, | |
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | |
12 * for the specific language governing rights and limitations under the | |
13 * License. | |
14 * | |
15 * The Original Code is the Dynamin library. | |
16 * | |
17 * The Initial Developer of the Original Code is Jordan Miner. | |
18 * Portions created by the Initial Developer are Copyright (C) 2006-2009 | |
19 * the Initial Developer. All Rights Reserved. | |
20 * | |
21 * Contributor(s): | |
22 * Jordan Miner <jminer7@gmail.com> | |
23 * | |
24 */ | |
25 | |
26 module dynamin.gui.file_dialog; | |
27 | |
28 import dynamin.all_core; | |
4
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
29 import dynamin.gui_backend; |
0 | 30 |
31 // not used by programs | |
32 struct FileDialogFilter { | |
33 string name; | |
34 string[] extensions; | |
35 // ignored for now | |
36 bool delegate(string fileName) shouldShow; | |
37 } | |
38 | |
39 /** | |
40 * As this is an abstract class, use OpenFileDialog or SaveFileDialog instead. | |
41 * | |
42 * TODO: figure out a way to allow the user to type in a custom filter? | |
43 * TODO: on Linux, use a GTK dialog if available, otherwise use a custom one. | |
44 * | |
45 * The appearance of a file dialog with Windows Classic: | |
46 * | |
47 * $(IMAGE ../web/example_file_dialog.png) | |
48 */ | |
49 abstract class FileDialog { | |
4
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
50 private: |
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
51 mixin FileDialogBackend; |
0 | 52 protected: |
53 bool _multipleSelection; | |
54 string _initialFileName; | |
55 string _text; | |
56 string _directory; | |
57 string[] _files; | |
58 FileDialogFilter[] _filters; | |
59 int _selectedFilter; | |
4
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
60 |
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
61 uint fileDialogType; |
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
62 enum { |
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
63 Open, Save |
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
64 } |
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
65 invariant() { |
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
66 assert(fileDialogType == Open || fileDialogType == Save); |
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
67 } |
0 | 68 public: |
69 /** | |
70 * Adds a filter that only shows files with the specified extensions. | |
71 * Note that the "All Files (*.*)" filter is added automatically when the | |
72 * dialog is shown if not added previously because I don't like when | |
73 * programs don't let me have control over what extension to use and | |
74 * don't let me be able to see all the files. | |
75 * Examples: | |
76 * ----- | |
77 * dialog.addFilter("All Files (*.*)"); | |
78 * dialog.addFilter("Cascading Style Sheets (*.css)", "css"); | |
79 * dialog.addFilter("Web Pages (*.html, *.htm)", "html", "htm"); | |
80 * ----- | |
81 */ | |
82 void addFilter(string name, string[] exts...) { | |
83 FileDialogFilter filter; | |
84 filter.name = name; | |
85 filter.extensions = exts; | |
86 | |
87 _filters.length = _filters.length + 1; | |
88 _filters[_filters.length-1] = filter; | |
89 } | |
90 /** | |
91 * Adds a filter that only shows files with which the specified delegate | |
92 * returns true for. | |
93 * BUG: not implemented | |
94 */ | |
95 void addFilter(string name, string ext, bool delegate(string fileName) shouldShow) { | |
96 // TODO: | |
97 throw new Exception("addFilter(string, string, delegate) not implemented"); | |
98 FileDialogFilter filter; | |
99 filter.name = name; | |
100 filter.extensions = [ext]; | |
101 filter.shouldShow = shouldShow; | |
102 | |
103 _filters.length = _filters.length + 1; | |
104 _filters[_filters.length-1] = filter; | |
105 } | |
106 /// Gets or sets the selected filter. An index of 0 is the first one added. | |
107 int selectedFilter() { return _selectedFilter; } | |
108 /// ditto | |
109 void selectedFilter(int index) { _selectedFilter = index; } | |
110 /** | |
111 * Gets or sets whether more than one file can be selected. | |
112 * The default is true for an OpenFileDialog and false for SaveFileDialog. | |
113 */ | |
114 bool multipleSelection() { return _multipleSelection; } | |
115 /// ditto | |
116 void multipleSelection(bool b) { _multipleSelection = b; } | |
117 /// Gets or sets the text that is displayed in the dialog's title bar. | |
118 string text() { return _text; } | |
119 /// ditto | |
120 void text(string str) { _text = str; } | |
121 /** | |
122 * Sets the text in the file name text box to the specified string. | |
123 * Example: | |
124 * ----- | |
125 * dialog.initialFileName = "Untitled"; | |
126 * ----- | |
127 */ | |
128 void initialFileName(string str) { | |
129 // TODO: make sure str is not a path? | |
130 _initialFileName = str; | |
131 } | |
132 /** | |
133 * Sets the directory that the FileDialog shows. If this is null, | |
134 * the default directory is used when the dialog is first shown. | |
135 * After the dialog has been shown, this is set to the directory | |
136 * the user was last looking at. | |
137 */ | |
138 void directory(string str) { | |
139 _directory = str; | |
140 } | |
141 /// TODO: Should this be SelectedDirectory ? | |
142 string directory() { | |
143 return _directory; | |
144 } | |
145 /** | |
146 * Gets the files selected by the user. | |
147 * If the user did not type a file name extension, the correct one | |
148 * will be added according to the selected filter. | |
149 */ | |
150 string[] files() { return _files; } | |
151 /// Gets the first of the files selected by the user. | |
152 string file() { return _files[0]; } | |
4
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
153 |
0 | 154 // TODO: parameters |
4
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
155 // TODO: should showDialog take any parameters? |
0 | 156 // what should happen if no owner is set? |
157 // Windows Forms sets the owner to the currently active window in the application | |
158 // do the same? or have no owner (really annoying, as window can get below)? | |
159 DialogResult showDialog() { | |
4
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
160 return backend_showDialog(); |
0 | 161 } |
162 } | |
163 | |
164 /// | |
165 class OpenFileDialog : FileDialog { | |
166 this() { | |
167 _multipleSelection = true; | |
168 // different settings | |
4
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
169 fileDialogType = Open; |
0 | 170 } |
171 } | |
172 | |
173 /// | |
174 class SaveFileDialog : FileDialog { | |
175 this() { | |
176 _multipleSelection = false; | |
177 // different settings | |
4
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
178 fileDialogType = Save; |
0 | 179 } |
180 } |