Debian Codegraph @

addons/native_dialog/gtk_filesel.c

raw
1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * GTK native dialog implementation.
12 *
13 * See LICENSE.txt for copyright information.
14 */
15
16 #include <gtk/gtk.h>
17
18 #include "allegro5/allegro.h"
19 #include "allegro5/allegro_native_dialog.h"
20 #include "allegro5/internal/aintern_native_dialog.h"
21 #include "gtk_dialog.h"
22 #include "gtk_xgtk.h"
23
24 typedef struct {
25 ALLEGRO_DISPLAY *display;
26 ALLEGRO_NATIVE_DIALOG *dialog;
27 } GTK_FILE_DIALOG_MESSAGE;
28
29 /* [nd_gtk thread] */
30 static gboolean create_gtk_file_dialog(gpointer data)
31 {
32 GTK_FILE_DIALOG_MESSAGE *msg = data;
33 ALLEGRO_DISPLAY *display = msg->display;
34 ALLEGRO_NATIVE_DIALOG *fd = msg->dialog;
35 bool save = fd->flags & ALLEGRO_FILECHOOSER_SAVE;
36 bool folder = fd->flags & ALLEGRO_FILECHOOSER_FOLDER;
37 gint result;
38
39 GtkWidget *window;
40
41 window =
42 gtk_file_chooser_dialog_new(al_cstr(fd->title),
43 NULL,
44 save ? GTK_FILE_CHOOSER_ACTION_SAVE : folder ? GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER : GTK_FILE_CHOOSER_ACTION_OPEN,
45 "_Cancel", GTK_RESPONSE_CANCEL,
46 save ? "_Save" : "_Open", GTK_RESPONSE_ACCEPT, NULL);
47
48 _al_gtk_make_transient(display, window);
49
50 if (save) {
51 gtk_file_chooser_set_do_overwrite_confirmation
52 (GTK_FILE_CHOOSER(window), true);
53 }
54
55 if (fd->fc_initial_path) {
56 bool is_dir;
57 bool exists;
58 const char *path = al_path_cstr(fd->fc_initial_path, ALLEGRO_NATIVE_PATH_SEP);
59
60 if (al_filename_exists(path)) {
61 exists = true;
62 ALLEGRO_FS_ENTRY *fs = al_create_fs_entry(path);
63 is_dir = al_get_fs_entry_mode(fs) & ALLEGRO_FILEMODE_ISDIR;
64 al_destroy_fs_entry(fs);
65 }
66 else {
67 exists = false;
68 is_dir = false;
69 }
70
71 if (is_dir) {
72 gtk_file_chooser_set_current_folder
73 (GTK_FILE_CHOOSER(window),
74 al_path_cstr(fd->fc_initial_path, ALLEGRO_NATIVE_PATH_SEP));
75 }
76 else if (exists) {
77 gtk_file_chooser_set_filename
78 (GTK_FILE_CHOOSER(window),
79 al_path_cstr(fd->fc_initial_path, ALLEGRO_NATIVE_PATH_SEP));
80 }
81 else {
82 ALLEGRO_PATH *dir_path = al_clone_path(fd->fc_initial_path);
83 if (dir_path) {
84 al_set_path_filename(dir_path, NULL);
85 gtk_file_chooser_set_current_folder
86 (GTK_FILE_CHOOSER(window),
87 al_path_cstr(dir_path, ALLEGRO_NATIVE_PATH_SEP));
88 if (save) {
89 gtk_file_chooser_set_current_name
90 (GTK_FILE_CHOOSER(window),
91 al_get_path_filename(fd->fc_initial_path));
92 }
93 al_destroy_path(dir_path);
94 }
95 }
96 }
97
98 if (fd->flags & ALLEGRO_FILECHOOSER_MULTIPLE)
99 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(window), true);
100
101 for (size_t i = 0; i < _al_vector_size(&fd->fc_patterns); i++)
102 {
103 _AL_PATTERNS_AND_DESC *patterns_and_desc = _al_vector_ref(&fd->fc_patterns, i);
104 const ALLEGRO_USTR *desc = al_ref_info(&patterns_and_desc->desc);
105 GtkFileFilter* filter = gtk_file_filter_new();
106 if (al_ustr_size(desc) > 0) {
107 char *cstr = al_cstr_dup(desc);
108 gtk_file_filter_set_name(filter, cstr);
109 al_free(cstr);
110 }
111 else {
112 gtk_file_filter_set_name(filter, "All supported files");
113 }
114 for (size_t j = 0; j < _al_vector_size(&patterns_and_desc->patterns_vec); j++) {
115 _AL_PATTERN *pattern = _al_vector_ref(&patterns_and_desc->patterns_vec, j);
116 char *cstr = al_cstr_dup(al_ref_info(&pattern->info));
117 if (pattern->is_mime) {
118 gtk_file_filter_add_mime_type(filter, cstr);
119 }
120 else {
121 gtk_file_filter_add_pattern(filter, cstr);
122 }
123 al_free(cstr);
124 }
125 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(window), filter);
126 }
127
128 result = gtk_dialog_run(GTK_DIALOG(window));
129 if (result == GTK_RESPONSE_ACCEPT) {
130 GSList* filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(window));
131 int i;
132 GSList* iter;
133
134 fd->fc_path_count = g_slist_length(filenames);
135 fd->fc_paths = al_malloc(fd->fc_path_count * sizeof(void *));
136 for (i = 0, iter = filenames; i < (int)fd->fc_path_count; i++, iter = g_slist_next(iter)) {
137 fd->fc_paths[i] = al_create_path((const char*)iter->data);
138 g_free(iter->data);
139 }
140 g_slist_free(filenames);
141 }
142
143 gtk_widget_destroy(window);
144
145 ASSERT(fd->async_queue);
146 g_async_queue_push(fd->async_queue, ACK_CLOSED);
147
148 return FALSE;
149 }
150
151 /* [user thread] */
152 bool _al_show_native_file_dialog(ALLEGRO_DISPLAY *display,
153 ALLEGRO_NATIVE_DIALOG *fd)
154 {
155 GTK_FILE_DIALOG_MESSAGE msg;
156
157 if (!_al_gtk_ensure_thread())
158 return false;
159
160 fd->async_queue = g_async_queue_new();
161
162 msg.display = display;
163 msg.dialog = fd;
164 g_timeout_add(0, create_gtk_file_dialog, &msg);
165
166 /* Wait for a signal that the window is closed. */
167 while (g_async_queue_pop(fd->async_queue) != ACK_CLOSED)
168 ;
169 g_async_queue_unref(fd->async_queue);
170 fd->async_queue = NULL;
171 return true;
172 }
173
174 /* vim: set sts=3 sw=3 et: */