Mercurial > projects > dynamin
annotate dynamin/gui/file_dialog.d @ 106:acdbb30fee7e
Port to D2.
Most of the effort was dealing with immutable and const.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Mon, 17 Dec 2012 23:41:50 -0600 |
parents | 73060bc3f004 |
children |
rev | line source |
---|---|
0 | 1 |
2 /* | |
103
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
46
diff
changeset
|
3 * Copyright Jordan Miner |
0 | 4 * |
103
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
46
diff
changeset
|
5 * This Source Code Form is subject to the terms of the Mozilla Public |
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
46
diff
changeset
|
6 * License, v. 2.0. If a copy of the MPL was not distributed with this |
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
46
diff
changeset
|
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
0 | 8 * |
9 */ | |
10 | |
11 module dynamin.gui.file_dialog; | |
12 | |
13 import dynamin.all_core; | |
4
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
14 import dynamin.gui_backend; |
0 | 15 |
16 // not used by programs | |
17 struct FileDialogFilter { | |
18 string name; | |
19 string[] extensions; | |
20 // ignored for now | |
106 | 21 bool delegate(cstring fileName) shouldShow; |
0 | 22 } |
23 | |
24 /** | |
25 * As this is an abstract class, use OpenFileDialog or SaveFileDialog instead. | |
26 * | |
27 * TODO: figure out a way to allow the user to type in a custom filter? | |
28 * TODO: on Linux, use a GTK dialog if available, otherwise use a custom one. | |
29 * | |
30 * The appearance of a file dialog with Windows Classic: | |
31 * | |
32 * $(IMAGE ../web/example_file_dialog.png) | |
33 */ | |
34 abstract class FileDialog { | |
4
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
35 private: |
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
36 mixin FileDialogBackend; |
0 | 37 protected: |
38 bool _multipleSelection; | |
39 string _initialFileName; | |
40 string _text; | |
46
127b9d99c01c
Rename uses of 'directory' to 'folder' in method names and comments.
Jordan Miner <jminer7@gmail.com>
parents:
18
diff
changeset
|
41 string _folder; |
0 | 42 string[] _files; |
43 FileDialogFilter[] _filters; | |
44 int _selectedFilter; | |
4
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
45 |
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
46 uint fileDialogType; |
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
47 enum { |
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
48 Open, Save |
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
49 } |
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
50 invariant() { |
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
51 assert(fileDialogType == Open || fileDialogType == Save); |
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
52 } |
0 | 53 public: |
54 /** | |
55 * Adds a filter that only shows files with the specified extensions. | |
56 * Note that the "All Files (*.*)" filter is added automatically when the | |
57 * dialog is shown if not added previously because I don't like when | |
58 * programs don't let me have control over what extension to use and | |
59 * don't let me be able to see all the files. | |
60 * Examples: | |
61 * ----- | |
62 * dialog.addFilter("All Files (*.*)"); | |
63 * dialog.addFilter("Cascading Style Sheets (*.css)", "css"); | |
64 * dialog.addFilter("Web Pages (*.html, *.htm)", "html", "htm"); | |
65 * ----- | |
66 */ | |
67 void addFilter(string name, string[] exts...) { | |
68 FileDialogFilter filter; | |
69 filter.name = name; | |
70 filter.extensions = exts; | |
71 | |
72 _filters.length = _filters.length + 1; | |
73 _filters[_filters.length-1] = filter; | |
74 } | |
75 /** | |
76 * Adds a filter that only shows files with which the specified delegate | |
77 * returns true for. | |
78 * BUG: not implemented | |
79 */ | |
106 | 80 void addFilter(string name, string ext, bool delegate(cstring fileName) shouldShow) { |
0 | 81 // TODO: |
82 throw new Exception("addFilter(string, string, delegate) not implemented"); | |
83 FileDialogFilter filter; | |
84 filter.name = name; | |
85 filter.extensions = [ext]; | |
86 filter.shouldShow = shouldShow; | |
87 | |
88 _filters.length = _filters.length + 1; | |
89 _filters[_filters.length-1] = filter; | |
90 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
4
diff
changeset
|
91 |
0 | 92 /// Gets or sets the selected filter. An index of 0 is the first one added. |
93 int selectedFilter() { return _selectedFilter; } | |
94 /// ditto | |
95 void selectedFilter(int index) { _selectedFilter = index; } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
4
diff
changeset
|
96 |
0 | 97 /** |
98 * Gets or sets whether more than one file can be selected. | |
99 * The default is true for an OpenFileDialog and false for SaveFileDialog. | |
100 */ | |
101 bool multipleSelection() { return _multipleSelection; } | |
102 /// ditto | |
14
e6e201c3a439
Remove ANSI functions from Windows binding and convert inner tabs to spaces
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
103 void multipleSelection(bool b) { _multipleSelection = b; } |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
4
diff
changeset
|
104 |
0 | 105 /// Gets or sets the text that is displayed in the dialog's title bar. |
106 string text() { return _text; } | |
107 /// ditto | |
108 void text(string str) { _text = str; } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
4
diff
changeset
|
109 |
0 | 110 /** |
111 * Sets the text in the file name text box to the specified string. | |
112 * Example: | |
113 * ----- | |
114 * dialog.initialFileName = "Untitled"; | |
115 * ----- | |
116 */ | |
117 void initialFileName(string str) { | |
118 // TODO: make sure str is not a path? | |
119 _initialFileName = str; | |
120 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
4
diff
changeset
|
121 |
0 | 122 /** |
46
127b9d99c01c
Rename uses of 'directory' to 'folder' in method names and comments.
Jordan Miner <jminer7@gmail.com>
parents:
18
diff
changeset
|
123 * Sets the folder that the FileDialog shows. If this is null, |
127b9d99c01c
Rename uses of 'directory' to 'folder' in method names and comments.
Jordan Miner <jminer7@gmail.com>
parents:
18
diff
changeset
|
124 * the default folder is used when the dialog is first shown. |
127b9d99c01c
Rename uses of 'directory' to 'folder' in method names and comments.
Jordan Miner <jminer7@gmail.com>
parents:
18
diff
changeset
|
125 * After the dialog has been shown, this is set to the folder |
0 | 126 * the user was last looking at. |
127 */ | |
46
127b9d99c01c
Rename uses of 'directory' to 'folder' in method names and comments.
Jordan Miner <jminer7@gmail.com>
parents:
18
diff
changeset
|
128 void folder(string str) { |
127b9d99c01c
Rename uses of 'directory' to 'folder' in method names and comments.
Jordan Miner <jminer7@gmail.com>
parents:
18
diff
changeset
|
129 _folder = str; |
0 | 130 } |
46
127b9d99c01c
Rename uses of 'directory' to 'folder' in method names and comments.
Jordan Miner <jminer7@gmail.com>
parents:
18
diff
changeset
|
131 /// TODO: Should this be selectedFolder ? |
127b9d99c01c
Rename uses of 'directory' to 'folder' in method names and comments.
Jordan Miner <jminer7@gmail.com>
parents:
18
diff
changeset
|
132 string folder() { |
127b9d99c01c
Rename uses of 'directory' to 'folder' in method names and comments.
Jordan Miner <jminer7@gmail.com>
parents:
18
diff
changeset
|
133 return _folder; |
0 | 134 } |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
4
diff
changeset
|
135 |
0 | 136 /** |
137 * Gets the files selected by the user. | |
138 * If the user did not type a file name extension, the correct one | |
139 * will be added according to the selected filter. | |
140 */ | |
141 string[] files() { return _files; } | |
142 /// Gets the first of the files selected by the user. | |
143 string file() { return _files[0]; } | |
4
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
144 |
0 | 145 // TODO: parameters |
4
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
146 // TODO: should showDialog take any parameters? |
0 | 147 // 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
|
148 // 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
|
149 // do the same? or have no owner (annoying, as window can get below)? |
0 | 150 DialogResult showDialog() { |
4
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
151 return backend_showDialog(); |
0 | 152 } |
18
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
153 private void ensureAllFilesFilter() { |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
154 foreach(filter; _filters) |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
155 if(filter.extensions.length == 0) |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
156 return; |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
157 addFilter("All Files (*.*)"); |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
158 } |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
159 private void maybeAddExt(ref string file) { |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
160 auto selFilter = _filters[selectedFilter]; |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
161 |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
162 // return if the "All Files (*.*)" filter is selected |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
163 if(selFilter.extensions.length == 0) |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
164 return; |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
165 |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
166 // 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
|
167 foreach(ext; selFilter.extensions) |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
168 if(file.downcase().endsWith(ext.downcase())) |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
169 return; |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
170 |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
171 file ~= "." ~ selFilter.extensions[0].downcase(); |
836a064828e8
Implement FileDialog/DirectoryDialog with GTK
Jordan Miner <jminer7@gmail.com>
parents:
14
diff
changeset
|
172 } |
0 | 173 } |
174 | |
175 /// | |
176 class OpenFileDialog : FileDialog { | |
177 this() { | |
178 _multipleSelection = true; | |
179 // different settings | |
4
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
180 fileDialogType = Open; |
0 | 181 } |
182 } | |
183 | |
184 /// | |
185 class SaveFileDialog : FileDialog { | |
186 this() { | |
187 _multipleSelection = false; | |
188 // different settings | |
4
fc2420d39e3c
Separate out Windows backend of FileDialog.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
189 fileDialogType = Save; |
0 | 190 } |
191 } |