Mercurial > projects > dynamin
annotate dynamin/gui/file_dialog.d @ 18:836a064828e8
Implement FileDialog/DirectoryDialog with GTK
and start a glib/gdk/gtk binding. Add invoke/invokeNow stubs to fix build on X.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Fri, 24 Jul 2009 00:35:42 -0500 |
parents | e6e201c3a439 |
children | 127b9d99c01c |
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 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
4
diff
changeset
|
106 |
0 | 107 /// Gets or sets the selected filter. An index of 0 is the first one added. |
108 int selectedFilter() { return _selectedFilter; } | |
109 /// ditto | |
110 void selectedFilter(int index) { _selectedFilter = index; } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
4
diff
changeset
|
111 |
0 | 112 /** |
113 * Gets or sets whether more than one file can be selected. | |
114 * The default is true for an OpenFileDialog and false for SaveFileDialog. | |
115 */ | |
116 bool multipleSelection() { return _multipleSelection; } | |
117 /// ditto | |
14
e6e201c3a439
Remove ANSI functions from Windows binding and convert inner tabs to spaces
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
118 void multipleSelection(bool b) { _multipleSelection = b; } |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
4
diff
changeset
|
119 |
0 | 120 /// Gets or sets the text that is displayed in the dialog's title bar. |
121 string text() { return _text; } | |
122 /// ditto | |
123 void text(string str) { _text = str; } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
4
diff
changeset
|
124 |
0 | 125 /** |
126 * Sets the text in the file name text box to the specified string. | |
127 * Example: | |
128 * ----- | |
129 * dialog.initialFileName = "Untitled"; | |
130 * ----- | |
131 */ | |
132 void initialFileName(string str) { | |
133 // TODO: make sure str is not a path? | |
134 _initialFileName = str; | |
135 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
4
diff
changeset
|
136 |
0 | 137 /** |
138 * Sets the directory that the FileDialog shows. If this is null, | |
139 * the default directory is used when the dialog is first shown. | |
140 * After the dialog has been shown, this is set to the directory | |
141 * the user was last looking at. | |
142 */ | |
143 void directory(string str) { | |
144 _directory = str; | |
145 } | |
18
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
146 /// TODO: Should this be selectedDirectory ? |
0 | 147 string directory() { |
148 return _directory; | |
149 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
4
diff
changeset
|
150 |
0 | 151 /** |
152 * Gets the files selected by the user. | |
153 * If the user did not type a file name extension, the correct one | |
154 * will be added according to the selected filter. | |
155 */ | |
156 string[] files() { return _files; } | |
157 /// Gets the first of the files selected by the user. | |
158 string file() { return _files[0]; } | |
4
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
159 |
0 | 160 // TODO: parameters |
4
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
161 // TODO: should showDialog take any parameters? |
0 | 162 // what should happen if no owner is set? |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
4
diff
changeset
|
163 // WinForms sets the owner to the currently active window in the app |
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
4
diff
changeset
|
164 // do the same? or have no owner (annoying, as window can get below)? |
0 | 165 DialogResult showDialog() { |
4
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
166 return backend_showDialog(); |
0 | 167 } |
18
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
168 private void ensureAllFilesFilter() { |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
169 foreach(filter; _filters) |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
170 if(filter.extensions.length == 0) |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
171 return; |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
172 addFilter("All Files (*.*)"); |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
173 } |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
174 private void maybeAddExt(ref string file) { |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
175 auto selFilter = _filters[selectedFilter]; |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
176 |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
177 // return if the "All Files (*.*)" filter is selected |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
178 if(selFilter.extensions.length == 0) |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
179 return; |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
180 |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
181 // return if the file already has an extension from the selected filter |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
182 foreach(ext; selFilter.extensions) |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
183 if(file.downcase().endsWith(ext.downcase())) |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
184 return; |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
185 |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
186 file ~= "." ~ selFilter.extensions[0].downcase(); |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
187 } |
0 | 188 } |
189 | |
190 /// | |
191 class OpenFileDialog : FileDialog { | |
192 this() { | |
193 _multipleSelection = true; | |
194 // different settings | |
4
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
195 fileDialogType = Open; |
0 | 196 } |
197 } | |
198 | |
199 /// | |
200 class SaveFileDialog : FileDialog { | |
201 this() { | |
202 _multipleSelection = false; | |
203 // different settings | |
4
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
204 fileDialogType = Save; |
0 | 205 } |
206 } |