comparison dynamin/gui/x_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
children 127b9d99c01c
comparison
equal deleted inserted replaced
17:ef81af74a306 18:836a064828e8
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) 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.x_file_dialog;
27
28 public import Utf = tango.text.convert.Utf;
29 public import dynamin.c.glib;
30 public import dynamin.c.gtk;
31
32 template FileDialogBackend() {
33 DialogResult backend_showDialog() {
34 // gdk_x11_get_server_time (GdkWindow *window)
35 // could be used in clipboard
36
37 string title = text ? text : (fileDialogType == Open ? "Open" : "Save");
38 auto dialog = gtk_file_chooser_dialog_new(toCharPtr(title), null,
39 fileDialogType == Open ?
40 GTK_FILE_CHOOSER_ACTION_OPEN : GTK_FILE_CHOOSER_ACTION_SAVE,
41 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
42 fileDialogType == Open ? GTK_STOCK_OPEN : GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, null);
43
44 ensureAllFilesFilter();
45 foreach(i, filter; _filters) {
46 if(filter.shouldShow) // TODO:
47 continue;
48 auto gfilter = gtk_file_filter_new();
49 gtk_file_filter_set_name(gfilter, toCharPtr(filter.name));
50
51 if(filter.extensions.length == 0)
52 gtk_file_filter_add_pattern(gfilter, "*");
53 else foreach(ext; filter.extensions)
54 gtk_file_filter_add_pattern(gfilter, toCharPtr("*."~ext));
55
56 gtk_file_chooser_add_filter(dialog, gfilter);
57 if(selectedFilter == i)
58 gtk_file_chooser_set_filter(dialog, gfilter);
59 }
60
61 if(fileDialogType == Open)
62 gtk_file_chooser_set_select_multiple(dialog, multipleSelection);
63 gtk_file_chooser_set_do_overwrite_confirmation(dialog, true);
64 if(_directory)
65 gtk_file_chooser_set_current_folder(dialog, toCharPtr(_directory));
66 if(_initialFileName)
67 gtk_file_chooser_set_current_name(dialog, toCharPtr(_initialFileName));
68
69 scope(exit) {
70 gtk_widget_destroy(dialog);
71 while(gtk_events_pending())
72 gtk_main_iteration();
73 }
74 if(gtk_dialog_run(dialog) == GTK_RESPONSE_ACCEPT) {
75 auto gfilters = gtk_file_chooser_list_filters(dialog);
76 _selectedFilter = g_slist_index(gfilters,
77 gtk_file_chooser_get_filter(dialog));
78 g_slist_free(gfilters);
79
80 auto list = gtk_file_chooser_get_filenames(dialog);
81 _files = new string[g_slist_length(list)];
82 for(int i = 0; i < _files.length; ++i) {
83 auto d = cast(char*)list.data;
84 _files[i] = d[0..strlen(d)].dup;
85 maybeAddExt(_files[i]);
86 g_free(list.data);
87 list = g_slist_next(list);
88 }
89 g_slist_free(list);
90
91 char* fold = gtk_file_chooser_get_current_folder(dialog);
92 _directory = fold[0..strlen(fold)].dup;
93 g_free(fold);
94
95 return DialogResult.OK;
96 }
97 return DialogResult.Cancel;
98 }
99 }
100