Mercurial > geeqie.yaz
annotate src/ui_tabcomp.c @ 825:ee33d2ddb661
Optionnally display directory's date in list view.
It can be set through Preferences > Advanced > Behavior and
is saved to rc file as layout.show_directory_date option.
author | zas_ |
---|---|
date | Fri, 13 Jun 2008 11:11:57 +0000 |
parents | e6ebae313d46 |
children | c414002a1f27 |
rev | line source |
---|---|
9 | 1 /* |
2 * (SLIK) SimpLIstic sKin functions | |
69
31759d770628
Fri Oct 13 10:27:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
12
diff
changeset
|
3 * (C) 2006 John Ellis |
475 | 4 * Copyright (C) 2008 The Geeqie Team |
9 | 5 * |
6 * Author: John Ellis | |
7 * | |
8 * This software is released under the GNU General Public License (GNU GPL). | |
9 * Please read the included file COPYING for more information. | |
10 * This software comes with no warranty of any kind, use at your own risk! | |
11 */ | |
12 | |
13 #ifdef HAVE_CONFIG_H | |
14 # include "config.h" | |
15 #endif | |
16 #include "intl.h" | |
17 | |
18 #include <stdio.h> | |
19 #include <stdlib.h> | |
20 #include <string.h> | |
21 #include <unistd.h> | |
22 #include <sys/types.h> | |
23 #include <dirent.h> | |
24 | |
25 #include <gdk/gdk.h> | |
26 #include <gtk/gtk.h> | |
27 #include <gdk-pixbuf/gdk-pixbuf.h> | |
28 | |
281 | 29 #include "main.h" |
9 | 30 #include "ui_tabcomp.h" |
31 | |
32 #include "ui_bookmark.h" | |
33 #include "ui_fileops.h" | |
34 #include "ui_spinner.h" | |
35 #include "ui_utildlg.h" | |
36 | |
37 #include <gdk/gdkkeysyms.h> /* for key values */ | |
38 | |
39 | |
40 /* define this to enable a pop-up menu that shows possible matches | |
41 * #define TAB_COMPLETION_ENABLE_POPUP_MENU | |
42 */ | |
43 #define TAB_COMPLETION_ENABLE_POPUP_MENU 1 | |
44 #define TAB_COMP_POPUP_MAX 500 | |
45 | |
46 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU | |
47 #include "ui_menu.h" | |
48 #endif | |
49 | |
50 | |
51 /* ---------------------------------------------------------------- | |
52 Tab completion routines, can be connected to any gtkentry widget | |
53 using the tab_completion_add_to_entry() function. | |
726 | 54 Use remove_trailing_slash() to strip the trailing G_DIR_SEPARATOR. |
9 | 55 ----------------------------------------------------------------*/ |
56 | |
57 typedef struct _TabCompData TabCompData; | |
58 struct _TabCompData | |
59 { | |
60 GtkWidget *entry; | |
61 gchar *dir_path; | |
62 GList *file_list; | |
63 void (*enter_func)(const gchar *, gpointer); | |
64 void (*tab_func)(const gchar *, gpointer); | |
65 gpointer enter_data; | |
66 gpointer tab_data; | |
67 | |
68 GtkWidget *combo; | |
69 gint has_history; | |
70 gchar *history_key; | |
71 gint history_levels; | |
72 | |
73 FileDialog *fd; | |
74 gchar *fd_title; | |
75 gint fd_folders_only; | |
76 GtkWidget *fd_button; | |
77 }; | |
78 | |
79 | |
80 static void tab_completion_select_show(TabCompData *td); | |
81 | |
82 static void tab_completion_free_list(TabCompData *td) | |
83 { | |
84 GList *list; | |
85 | |
86 g_free(td->dir_path); | |
87 td->dir_path = NULL; | |
88 | |
89 list = td->file_list; | |
90 | |
516 | 91 while (list) |
9 | 92 { |
93 g_free(list->data); | |
94 list = list->next; | |
95 } | |
96 | |
97 g_list_free(td->file_list); | |
98 td->file_list = NULL; | |
99 } | |
100 | |
101 static void tab_completion_read_dir(TabCompData *td, const gchar *path) | |
102 { | |
442 | 103 DIR *dp; |
104 struct dirent *dir; | |
105 GList *list = NULL; | |
9 | 106 gchar *pathl; |
107 | |
108 tab_completion_free_list(td); | |
109 | |
110 pathl = path_from_utf8(path); | |
442 | 111 dp = opendir(pathl); |
9 | 112 g_free(pathl); |
113 if (!dp) | |
442 | 114 { |
115 /* dir not found */ | |
116 return; | |
117 } | |
118 while ((dir = readdir(dp)) != NULL) | |
119 { | |
69
31759d770628
Fri Oct 13 10:27:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
12
diff
changeset
|
120 gchar *name = dir->d_name; |
31759d770628
Fri Oct 13 10:27:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
12
diff
changeset
|
121 if (strcmp(name, ".") != 0 && strcmp(name, "..") != 0) |
31759d770628
Fri Oct 13 10:27:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
12
diff
changeset
|
122 { |
31759d770628
Fri Oct 13 10:27:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
12
diff
changeset
|
123 list = g_list_prepend(list, path_to_utf8(name)); |
31759d770628
Fri Oct 13 10:27:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
12
diff
changeset
|
124 } |
9 | 125 } |
442 | 126 closedir(dp); |
9 | 127 |
128 td->dir_path = g_strdup(path); | |
129 td->file_list = list; | |
130 } | |
131 | |
132 static void tab_completion_destroy(GtkWidget *widget, gpointer data) | |
133 { | |
134 TabCompData *td = data; | |
135 | |
136 tab_completion_free_list(td); | |
137 g_free(td->history_key); | |
138 | |
139 if (td->fd) file_dialog_close(td->fd); | |
140 g_free(td->fd_title); | |
141 | |
142 g_free(td); | |
143 } | |
144 | |
145 static gint tab_completion_emit_enter_signal(TabCompData *td) | |
146 { | |
147 gchar *text; | |
148 if (!td->enter_func) return FALSE; | |
149 | |
150 text = g_strdup(gtk_entry_get_text(GTK_ENTRY(td->entry))); | |
151 | |
152 if (text[0] == '~') | |
153 { | |
154 gchar *t = text; | |
720
d8a88f279aca
Use expand_tilde() instead of simple concatenation, it allows correct expansion
zas_
parents:
673
diff
changeset
|
155 text = expand_tilde(text); |
9 | 156 g_free(t); |
157 } | |
158 | |
159 td->enter_func(text, td->enter_data); | |
160 g_free(text); | |
161 | |
162 return TRUE; | |
163 } | |
164 | |
165 static void tab_completion_emit_tab_signal(TabCompData *td) | |
166 { | |
167 gchar *text; | |
168 if (!td->tab_func) return; | |
169 | |
170 text = g_strdup(gtk_entry_get_text(GTK_ENTRY(td->entry))); | |
171 | |
172 if (text[0] == '~') | |
173 { | |
174 gchar *t = text; | |
720
d8a88f279aca
Use expand_tilde() instead of simple concatenation, it allows correct expansion
zas_
parents:
673
diff
changeset
|
175 text = expand_tilde(text); |
9 | 176 g_free(t); |
177 } | |
178 | |
179 td->tab_func(text, td->tab_data); | |
180 g_free(text); | |
181 } | |
182 | |
183 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU | |
184 | |
185 static gint tab_completion_popup_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data) | |
186 { | |
187 TabCompData *td = data; | |
188 | |
189 if (event->keyval == GDK_Tab || | |
190 event->keyval == GDK_BackSpace || | |
191 (event->keyval >= 0x20 && event->keyval <= 0xFF) ) | |
192 { | |
193 if (event->keyval >= 0x20 && event->keyval <= 0xFF) | |
194 { | |
195 gchar buf[2]; | |
196 gint p = -1; | |
197 | |
198 buf[0] = event->keyval; | |
199 buf[1] = '\0'; | |
200 gtk_editable_insert_text(GTK_EDITABLE(td->entry), buf, 1, &p); | |
201 gtk_editable_set_position(GTK_EDITABLE(td->entry), -1); | |
202 } | |
203 | |
204 /*close the menu */ | |
205 gtk_menu_popdown(GTK_MENU(widget)); | |
206 /* doing this does not emit the "selection done" signal, unref it ourselves */ | |
207 gtk_widget_unref(widget); | |
442 | 208 |
9 | 209 return TRUE; |
210 } | |
211 | |
212 return FALSE; | |
213 } | |
214 | |
215 static void tab_completion_popup_cb(GtkWidget *widget, gpointer data) | |
216 { | |
217 gchar *name = data; | |
218 TabCompData *td; | |
219 gchar *buf; | |
220 | |
221 td = g_object_get_data(G_OBJECT(widget), "tab_completion_data"); | |
222 if (!td) return; | |
223 | |
721
b736a2e3129b
tab_completion_popup_cb(): use g_build_filename() and simplify code.
zas_
parents:
720
diff
changeset
|
224 buf = g_build_filename(td->dir_path, name, NULL); |
9 | 225 gtk_entry_set_text(GTK_ENTRY(td->entry), buf); |
226 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf)); | |
227 g_free(buf); | |
228 | |
229 tab_completion_emit_tab_signal(td); | |
230 } | |
231 | |
232 static void tab_completion_popup_pos_cb(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer data) | |
233 { | |
234 TabCompData *td = data; | |
235 gint height; | |
236 PangoLayout *layout; | |
237 PangoRectangle strong_pos, weak_pos; | |
238 gint length; | |
239 gint xoffset, yoffset; | |
12
147f4c4b9025
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
240 GtkRequisition req; |
147f4c4b9025
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
241 GdkScreen *screen; |
147f4c4b9025
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
242 gint monitor_num; |
147f4c4b9025
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
243 GdkRectangle monitor; |
9 | 244 |
245 gdk_window_get_origin(td->entry->window, x, y); | |
246 | |
12
147f4c4b9025
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
247 screen = gtk_widget_get_screen(GTK_WIDGET(menu)); |
147f4c4b9025
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
248 monitor_num = gdk_screen_get_monitor_at_window(screen, td->entry->window); |
147f4c4b9025
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
249 gdk_screen_get_monitor_geometry(screen, monitor_num, &monitor); |
147f4c4b9025
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
250 |
147f4c4b9025
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
251 gtk_widget_size_request(GTK_WIDGET(menu), &req); |
9 | 252 |
253 length = strlen(gtk_entry_get_text(GTK_ENTRY(td->entry))); | |
254 gtk_entry_get_layout_offsets(GTK_ENTRY(td->entry), &xoffset, &yoffset); | |
255 | |
256 layout = gtk_entry_get_layout(GTK_ENTRY(td->entry)); | |
257 pango_layout_get_cursor_pos(layout, length, &strong_pos, &weak_pos); | |
12
147f4c4b9025
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
258 |
9 | 259 *x += strong_pos.x / PANGO_SCALE + xoffset; |
12
147f4c4b9025
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
260 |
147f4c4b9025
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
261 height = MIN(td->entry->requisition.height, td->entry->allocation.height); |
147f4c4b9025
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
262 |
147f4c4b9025
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
263 if (req.height > monitor.y + monitor.height - *y - height && |
147f4c4b9025
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
264 *y - monitor.y > monitor.y + monitor.height - *y) |
147f4c4b9025
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
265 { |
147f4c4b9025
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
266 height = MIN(*y - monitor.y, req.height); |
147f4c4b9025
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
267 gtk_widget_set_size_request(GTK_WIDGET(menu), -1, height); |
147f4c4b9025
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
268 *y -= height; |
147f4c4b9025
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
269 } |
147f4c4b9025
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
270 else |
147f4c4b9025
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
271 { |
147f4c4b9025
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
272 *y += height; |
147f4c4b9025
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
273 } |
9 | 274 } |
275 | |
276 static void tab_completion_popup_list(TabCompData *td, GList *list) | |
277 { | |
278 GtkWidget *menu; | |
279 GList *work; | |
280 GdkEvent *event; | |
281 guint32 etime; | |
282 gint ebutton; | |
283 gint count = 0; | |
284 | |
285 if (!list) return; | |
286 | |
287 #if 0 | |
288 /* | |
289 * well, the menu would be too long anyway... | |
290 * (listing /dev causes gtk+ window allocation errors, -> too big a window) | |
291 * this is why menu popups are disabled, this really should be a popup scrollable listview. | |
292 */ | |
293 if (g_list_length(list) > 200) return; | |
294 #endif | |
295 | |
296 menu = popup_menu_short_lived(); | |
297 | |
298 work = list; | |
299 while (work && count < TAB_COMP_POPUP_MAX) | |
300 { | |
301 gchar *name = work->data; | |
302 GtkWidget *item; | |
303 | |
304 item = menu_item_add_simple(menu, name, G_CALLBACK(tab_completion_popup_cb), name); | |
305 g_object_set_data(G_OBJECT(item), "tab_completion_data", td); | |
306 | |
307 work = work->next; | |
308 count++; | |
309 } | |
310 | |
311 g_signal_connect(G_OBJECT(menu), "key_press_event", | |
312 G_CALLBACK(tab_completion_popup_key_press), td); | |
313 | |
314 /* peek at the current event to get the time, etc. */ | |
315 event = gtk_get_current_event(); | |
316 | |
317 if (event && event->type == GDK_BUTTON_RELEASE) | |
318 { | |
319 ebutton = event->button.button; | |
320 } | |
321 else | |
322 { | |
323 ebutton = 0; | |
324 } | |
325 | |
326 if (event) | |
327 { | |
328 etime = gdk_event_get_time(event); | |
329 gdk_event_free(event); | |
330 } | |
331 else | |
332 { | |
333 etime = 0; | |
334 } | |
335 | |
336 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, | |
337 tab_completion_popup_pos_cb, td, ebutton, etime); | |
338 } | |
339 | |
340 #ifndef CASE_SORT | |
341 #define CASE_SORT strcmp | |
342 #endif | |
343 | |
344 static gint simple_sort(gconstpointer a, gconstpointer b) | |
345 { | |
442 | 346 return CASE_SORT((gchar *)a, (gchar *)b); |
9 | 347 } |
348 | |
349 #endif | |
350 | |
351 static gint tab_completion_do(TabCompData *td) | |
352 { | |
353 const gchar *entry_text = gtk_entry_get_text(GTK_ENTRY(td->entry)); | |
354 const gchar *entry_file; | |
355 gchar *entry_dir; | |
356 gchar *ptr; | |
357 gint home_exp = FALSE; | |
358 | |
359 /* home dir expansion */ | |
360 if (entry_text[0] == '~') | |
361 { | |
720
d8a88f279aca
Use expand_tilde() instead of simple concatenation, it allows correct expansion
zas_
parents:
673
diff
changeset
|
362 entry_dir = expand_tilde(entry_text); |
9 | 363 home_exp = TRUE; |
364 } | |
365 else | |
366 { | |
367 entry_dir = g_strdup(entry_text); | |
368 } | |
369 | |
370 entry_file = filename_from_path(entry_text); | |
371 | |
372 if (isfile(entry_dir)) | |
373 { | |
374 if (home_exp) | |
375 { | |
376 gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir); | |
377 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(entry_dir)); | |
378 } | |
379 g_free(entry_dir); | |
380 return home_exp; | |
381 } | |
722
9a145206ec2c
tab_completion_do(): use g_build_filename(), G_DIR_SEPARATOR, G_DIR_SEPARATOR_S.
zas_
parents:
721
diff
changeset
|
382 |
9 | 383 if (isdir(entry_dir) && strcmp(entry_file, ".") != 0 && strcmp(entry_file, "..") != 0) |
384 { | |
385 ptr = entry_dir + strlen(entry_dir) - 1; | |
722
9a145206ec2c
tab_completion_do(): use g_build_filename(), G_DIR_SEPARATOR, G_DIR_SEPARATOR_S.
zas_
parents:
721
diff
changeset
|
386 if (ptr[0] == G_DIR_SEPARATOR) |
9 | 387 { |
388 if (home_exp) | |
389 { | |
390 gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir); | |
391 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(entry_dir)); | |
392 } | |
393 | |
394 tab_completion_read_dir(td, entry_dir); | |
395 td->file_list = g_list_sort(td->file_list, simple_sort); | |
396 if (td->file_list && !td->file_list->next) | |
397 { | |
398 gchar *buf; | |
399 const gchar *file; | |
400 | |
401 file = td->file_list->data; | |
722
9a145206ec2c
tab_completion_do(): use g_build_filename(), G_DIR_SEPARATOR, G_DIR_SEPARATOR_S.
zas_
parents:
721
diff
changeset
|
402 buf = g_build_filename(entry_dir, file, NULL); |
9 | 403 if (isdir(buf)) |
404 { | |
722
9a145206ec2c
tab_completion_do(): use g_build_filename(), G_DIR_SEPARATOR, G_DIR_SEPARATOR_S.
zas_
parents:
721
diff
changeset
|
405 gchar *tmp = g_strconcat(buf, G_DIR_SEPARATOR_S, NULL); |
9 | 406 g_free(buf); |
722
9a145206ec2c
tab_completion_do(): use g_build_filename(), G_DIR_SEPARATOR, G_DIR_SEPARATOR_S.
zas_
parents:
721
diff
changeset
|
407 buf = tmp; |
9 | 408 } |
409 gtk_entry_set_text(GTK_ENTRY(td->entry), buf); | |
410 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf)); | |
411 g_free(buf); | |
412 } | |
413 | |
414 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU | |
415 | |
416 else | |
417 { | |
418 tab_completion_popup_list(td, td->file_list); | |
419 } | |
420 #endif | |
421 | |
422 g_free(entry_dir); | |
423 return home_exp; | |
424 } | |
425 else | |
426 { | |
722
9a145206ec2c
tab_completion_do(): use g_build_filename(), G_DIR_SEPARATOR, G_DIR_SEPARATOR_S.
zas_
parents:
721
diff
changeset
|
427 gchar *buf = g_strconcat(entry_dir, G_DIR_SEPARATOR_S, NULL); |
9 | 428 gtk_entry_set_text(GTK_ENTRY(td->entry), buf); |
429 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf)); | |
430 g_free(buf); | |
431 g_free(entry_dir); | |
432 return TRUE; | |
433 } | |
434 } | |
435 | |
436 ptr = (gchar *)filename_from_path(entry_dir); | |
437 if (ptr > entry_dir) ptr--; | |
438 ptr[0] = '\0'; | |
439 | |
440 if (strlen(entry_dir) == 0) | |
441 { | |
442 g_free(entry_dir); | |
722
9a145206ec2c
tab_completion_do(): use g_build_filename(), G_DIR_SEPARATOR, G_DIR_SEPARATOR_S.
zas_
parents:
721
diff
changeset
|
443 entry_dir = g_strdup(G_DIR_SEPARATOR_S); /* FIXME: win32 */ |
9 | 444 } |
445 | |
446 if (isdir(entry_dir)) | |
447 { | |
448 GList *list; | |
449 GList *poss = NULL; | |
450 gint l = strlen(entry_file); | |
451 | |
452 if (!td->dir_path || !td->file_list || strcmp(td->dir_path, entry_dir) != 0) | |
453 { | |
454 tab_completion_read_dir(td, entry_dir); | |
455 } | |
456 | |
722
9a145206ec2c
tab_completion_do(): use g_build_filename(), G_DIR_SEPARATOR, G_DIR_SEPARATOR_S.
zas_
parents:
721
diff
changeset
|
457 if (strcmp(entry_dir, G_DIR_SEPARATOR_S) == 0) entry_dir[0] = '\0'; /* FIXME: win32 */ |
9 | 458 |
459 list = td->file_list; | |
516 | 460 while (list) |
9 | 461 { |
462 gchar *file = list->data; | |
463 if (strncmp(entry_file, file, l) == 0) | |
464 { | |
465 poss = g_list_prepend(poss, file); | |
466 } | |
467 list = list->next; | |
468 } | |
469 | |
470 if (poss) | |
471 { | |
472 if (!poss->next) | |
473 { | |
474 gchar *file = poss->data; | |
475 gchar *buf; | |
476 | |
722
9a145206ec2c
tab_completion_do(): use g_build_filename(), G_DIR_SEPARATOR, G_DIR_SEPARATOR_S.
zas_
parents:
721
diff
changeset
|
477 buf = g_build_filename(entry_dir, file, NULL); |
9 | 478 |
479 if (isdir(buf)) | |
480 { | |
722
9a145206ec2c
tab_completion_do(): use g_build_filename(), G_DIR_SEPARATOR, G_DIR_SEPARATOR_S.
zas_
parents:
721
diff
changeset
|
481 gchar *tmp = g_strconcat(buf, G_DIR_SEPARATOR_S, NULL); |
9 | 482 g_free(buf); |
722
9a145206ec2c
tab_completion_do(): use g_build_filename(), G_DIR_SEPARATOR, G_DIR_SEPARATOR_S.
zas_
parents:
721
diff
changeset
|
483 buf = tmp; |
9 | 484 } |
485 gtk_entry_set_text(GTK_ENTRY(td->entry), buf); | |
486 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf)); | |
487 g_free(buf); | |
488 g_list_free(poss); | |
489 g_free(entry_dir); | |
490 return TRUE; | |
491 } | |
492 else | |
493 { | |
734
e6ebae313d46
Fix up some types, make some signed vs unsigned warnings quiet.
zas_
parents:
726
diff
changeset
|
494 gsize c = strlen(entry_file); |
9 | 495 gint done = FALSE; |
496 gchar *test_file = poss->data; | |
497 | |
498 while (!done) | |
499 { | |
500 list = poss; | |
501 if (!list) done = TRUE; | |
516 | 502 while (list && !done) |
9 | 503 { |
504 gchar *file = list->data; | |
505 if (strlen(file) < c || strncmp(test_file, file, c) != 0) | |
506 { | |
507 done = TRUE; | |
508 } | |
509 list = list->next; | |
510 } | |
511 c++; | |
512 } | |
513 c -= 2; | |
514 if (c > 0) | |
515 { | |
516 gchar *file; | |
517 gchar *buf; | |
518 file = g_strdup(test_file); | |
519 file[c] = '\0'; | |
722
9a145206ec2c
tab_completion_do(): use g_build_filename(), G_DIR_SEPARATOR, G_DIR_SEPARATOR_S.
zas_
parents:
721
diff
changeset
|
520 buf = g_build_filename(entry_dir, file, NULL); |
9 | 521 gtk_entry_set_text(GTK_ENTRY(td->entry), buf); |
522 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf)); | |
523 | |
524 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU | |
525 | |
526 poss = g_list_sort(poss, simple_sort); | |
527 tab_completion_popup_list(td, poss); | |
528 | |
529 #endif | |
530 | |
531 g_free(file); | |
532 g_free(buf); | |
533 g_list_free(poss); | |
534 g_free(entry_dir); | |
535 return TRUE; | |
536 } | |
537 } | |
538 g_list_free(poss); | |
539 } | |
540 } | |
541 | |
542 g_free(entry_dir); | |
543 | |
544 return FALSE; | |
545 } | |
546 | |
547 static gint tab_completion_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data) | |
548 { | |
549 TabCompData *td = data; | |
550 gint stop_signal = FALSE; | |
551 | |
552 switch (event->keyval) | |
553 { | |
442 | 554 case GDK_Tab: |
9 | 555 if (!(event->state & GDK_CONTROL_MASK)) |
556 { | |
557 if (tab_completion_do(td)) | |
558 { | |
559 tab_completion_emit_tab_signal(td); | |
560 } | |
561 stop_signal = TRUE; | |
562 } | |
563 break; | |
564 case GDK_Return: case GDK_KP_Enter: | |
565 if (td->fd_button && | |
566 (event->state & GDK_CONTROL_MASK)) | |
567 { | |
568 tab_completion_select_show(td); | |
569 stop_signal = TRUE; | |
570 } | |
571 else if (tab_completion_emit_enter_signal(td)) | |
572 { | |
573 stop_signal = TRUE; | |
574 } | |
575 break; | |
576 default: | |
577 break; | |
578 } | |
579 | |
580 if (stop_signal) g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event"); | |
581 | |
582 return (stop_signal); | |
583 } | |
584 | |
585 static void tab_completion_button_pressed(GtkWidget *widget, gpointer data) | |
586 { | |
587 TabCompData *td; | |
588 GtkWidget *entry = data; | |
589 | |
590 td = g_object_get_data(G_OBJECT(entry), "tab_completion_data"); | |
591 | |
592 if (!td) return; | |
593 | |
594 if (!GTK_WIDGET_HAS_FOCUS(entry)) | |
595 { | |
596 gtk_widget_grab_focus(entry); | |
597 } | |
598 | |
599 if (tab_completion_do(td)) | |
600 { | |
601 tab_completion_emit_tab_signal(td); | |
602 } | |
603 } | |
604 | |
605 static void tab_completion_button_size_allocate(GtkWidget *button, GtkAllocation *allocation, gpointer data) | |
606 { | |
607 GtkWidget *parent = data; | |
608 | |
609 if (allocation->height > parent->allocation.height) | |
610 { | |
611 GtkAllocation button_allocation; | |
612 | |
613 button_allocation = button->allocation; | |
614 button_allocation.height = parent->allocation.height; | |
615 button_allocation.y = parent->allocation.y + | |
616 (parent->allocation.height - parent->allocation.height) / 2; | |
617 gtk_widget_size_allocate(button, &button_allocation); | |
618 } | |
619 } | |
620 | |
621 static GtkWidget *tab_completion_create_complete_button(GtkWidget *entry, GtkWidget *parent) | |
622 { | |
623 GtkWidget *button; | |
624 GtkWidget *icon; | |
625 GdkPixbuf *pixbuf; | |
626 | |
627 button = gtk_button_new(); | |
628 GTK_WIDGET_UNSET_FLAGS(button, GTK_CAN_FOCUS); | |
629 g_signal_connect(G_OBJECT(button), "size_allocate", | |
630 G_CALLBACK(tab_completion_button_size_allocate), parent); | |
631 g_signal_connect(G_OBJECT(button), "clicked", | |
632 G_CALLBACK(tab_completion_button_pressed), entry); | |
633 | |
634 pixbuf = gdk_pixbuf_new_from_inline(-1, icon_tabcomp, FALSE, NULL); | |
635 icon = gtk_image_new_from_pixbuf(pixbuf); | |
636 gdk_pixbuf_unref(pixbuf); | |
637 gtk_container_add(GTK_CONTAINER(button), icon); | |
638 gtk_widget_show(icon); | |
639 | |
640 return button; | |
641 } | |
642 | |
643 /* | |
644 *---------------------------------------------------------------------------- | |
645 * public interface | |
646 *---------------------------------------------------------------------------- | |
647 */ | |
648 | |
649 GtkWidget *tab_completion_new_with_history(GtkWidget **entry, const gchar *text, | |
650 const gchar *history_key, gint max_levels, | |
651 void (*enter_func)(const gchar *, gpointer), gpointer data) | |
652 { | |
653 GtkWidget *box; | |
654 GtkWidget *combo; | |
655 GtkWidget *combo_entry; | |
656 GtkWidget *button; | |
657 GList *work; | |
658 TabCompData *td; | |
659 gint n = 0; | |
660 | |
661 box = gtk_hbox_new(FALSE, 0); | |
662 | |
663 combo = gtk_combo_box_entry_new_text(); | |
664 gtk_box_pack_start(GTK_BOX(box), combo, TRUE, TRUE, 0); | |
665 gtk_widget_show(combo); | |
666 | |
667 combo_entry = GTK_BIN(combo)->child; | |
668 #if 0 | |
669 gtk_combo_set_case_sensitive(GTK_COMBO(combo), TRUE); | |
670 gtk_combo_set_use_arrows(GTK_COMBO(combo), FALSE); | |
671 #endif | |
672 | |
673 button = tab_completion_create_complete_button(combo_entry, combo); | |
674 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 0); | |
675 gtk_widget_show(button); | |
676 | |
677 tab_completion_add_to_entry(combo_entry, enter_func, data); | |
678 | |
679 td = g_object_get_data(G_OBJECT(combo_entry), "tab_completion_data"); | |
680 if (!td) return NULL; /* this should never happen! */ | |
681 | |
682 td->combo = combo; | |
683 td->has_history = TRUE; | |
684 td->history_key = g_strdup(history_key); | |
685 td->history_levels = max_levels; | |
686 | |
687 work = history_list_get_by_key(td->history_key); | |
688 | |
689 work = history_list_get_by_key(history_key); | |
690 while (work) | |
691 { | |
692 gtk_combo_box_append_text(GTK_COMBO_BOX(combo), (gchar *)work->data); | |
693 work = work->next; | |
694 n++; | |
695 } | |
696 | |
697 if (text) | |
698 { | |
699 gtk_entry_set_text(GTK_ENTRY(combo_entry), text); | |
700 } | |
701 else if (n > 0) | |
702 { | |
703 gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0); | |
704 } | |
705 | |
706 if (entry) *entry = combo_entry; | |
707 return box; | |
708 } | |
709 | |
710 const gchar *tab_completion_set_to_last_history(GtkWidget *entry) | |
711 { | |
712 TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data"); | |
713 const gchar *buf; | |
714 | |
715 if (!td || !td->has_history) return NULL; | |
716 | |
717 buf = history_list_find_last_path_by_key(td->history_key); | |
718 if (buf) | |
719 { | |
720 gtk_entry_set_text(GTK_ENTRY(td->entry), buf); | |
721 } | |
722 | |
723 return buf; | |
724 } | |
725 | |
726 void tab_completion_append_to_history(GtkWidget *entry, const gchar *path) | |
727 { | |
728 TabCompData *td; | |
729 GtkTreeModel *store; | |
730 GList *work; | |
731 | |
732 td = g_object_get_data(G_OBJECT(entry), "tab_completion_data"); | |
733 | |
734 if (!path) return; | |
735 | |
736 if (!td || !td->has_history) return; | |
737 | |
738 history_list_add_to_key(td->history_key, path, td->history_levels); | |
739 | |
740 gtk_combo_box_set_active(GTK_COMBO_BOX(td->combo), -1); | |
741 | |
742 store = gtk_combo_box_get_model(GTK_COMBO_BOX(td->combo)); | |
743 gtk_list_store_clear(GTK_LIST_STORE(store)); | |
744 | |
745 work = history_list_get_by_key(td->history_key); | |
746 while (work) | |
747 { | |
748 gtk_combo_box_append_text(GTK_COMBO_BOX(td->combo), (gchar *)work->data); | |
749 work = work->next; | |
750 } | |
751 } | |
752 | |
753 GtkWidget *tab_completion_new(GtkWidget **entry, const gchar *text, | |
754 void (*enter_func)(const gchar *, gpointer), gpointer data) | |
755 { | |
756 GtkWidget *hbox; | |
757 GtkWidget *button; | |
758 GtkWidget *newentry; | |
759 | |
760 hbox = gtk_hbox_new(FALSE, 0); | |
761 | |
762 newentry = gtk_entry_new(); | |
763 if (text) gtk_entry_set_text(GTK_ENTRY(newentry), text); | |
764 gtk_box_pack_start(GTK_BOX(hbox), newentry, TRUE, TRUE, 0); | |
765 gtk_widget_show(newentry); | |
766 | |
767 button = tab_completion_create_complete_button(newentry, newentry); | |
768 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0); | |
769 gtk_widget_show(button); | |
770 | |
771 tab_completion_add_to_entry(newentry, enter_func, data); | |
772 | |
773 if (entry) *entry = newentry; | |
774 return hbox; | |
775 } | |
776 | |
777 void tab_completion_add_to_entry(GtkWidget *entry, void (*enter_func)(const gchar *, gpointer), gpointer data) | |
778 { | |
779 TabCompData *td; | |
780 if (!entry) | |
781 { | |
673
fbebf5cf4a55
Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents:
516
diff
changeset
|
782 log_printf("Tab completion error: entry != NULL\n"); |
9 | 783 return; |
784 } | |
785 | |
786 td = g_new0(TabCompData, 1); | |
787 td->entry = entry; | |
788 td->dir_path = NULL; | |
789 td->file_list = NULL; | |
790 td->enter_func = enter_func; | |
791 td->enter_data = data; | |
792 td->tab_func = NULL; | |
793 td->tab_data = NULL; | |
794 | |
795 td->has_history = FALSE; | |
796 td->history_key = NULL; | |
797 td->history_levels = 0; | |
798 | |
799 g_object_set_data(G_OBJECT(td->entry), "tab_completion_data", td); | |
800 | |
801 g_signal_connect(G_OBJECT(entry), "key_press_event", | |
802 G_CALLBACK(tab_completion_key_pressed), td); | |
803 g_signal_connect(G_OBJECT(entry), "destroy", | |
804 G_CALLBACK(tab_completion_destroy), td); | |
805 } | |
806 | |
807 void tab_completion_add_tab_func(GtkWidget *entry, void (*tab_func)(const gchar *, gpointer), gpointer data) | |
808 { | |
809 TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data"); | |
810 | |
811 if (!td) return; | |
812 | |
813 td->tab_func = tab_func; | |
814 td->tab_data = data; | |
815 } | |
816 | |
817 gchar *remove_trailing_slash(const gchar *path) | |
818 { | |
819 gint l; | |
260
249a9a6cd27f
Improve remove_trailing_slash() so it allocates no more than
zas_
parents:
254
diff
changeset
|
820 |
9 | 821 if (!path) return NULL; |
822 | |
260
249a9a6cd27f
Improve remove_trailing_slash() so it allocates no more than
zas_
parents:
254
diff
changeset
|
823 l = strlen(path); |
726 | 824 while (l > 1 && path[l - 1] == G_DIR_SEPARATOR) l--; |
9 | 825 |
260
249a9a6cd27f
Improve remove_trailing_slash() so it allocates no more than
zas_
parents:
254
diff
changeset
|
826 return g_strndup(path, l); |
9 | 827 } |
828 | |
829 static void tab_completion_select_cancel_cb(FileDialog *fd, gpointer data) | |
830 { | |
831 TabCompData *td = data; | |
832 | |
833 td->fd = NULL; | |
834 file_dialog_close(fd); | |
835 } | |
836 | |
837 static void tab_completion_select_ok_cb(FileDialog *fd, gpointer data) | |
838 { | |
839 TabCompData *td = data; | |
840 | |
841 gtk_entry_set_text(GTK_ENTRY(td->entry), gtk_entry_get_text(GTK_ENTRY(fd->entry))); | |
842 | |
843 tab_completion_select_cancel_cb(fd, data); | |
844 | |
845 tab_completion_emit_enter_signal(td); | |
846 } | |
847 | |
848 static void tab_completion_select_show(TabCompData *td) | |
849 { | |
850 const gchar *title; | |
851 const gchar *path; | |
852 | |
853 if (td->fd) | |
854 { | |
855 gtk_window_present(GTK_WINDOW(GENERIC_DIALOG(td->fd)->dialog)); | |
856 return; | |
857 } | |
858 | |
859 title = (td->fd_title) ? td->fd_title : _("Select path"); | |
254
9faf34f047b1
Make the wmclass value unique among the code by defining
zas_
parents:
138
diff
changeset
|
860 td->fd = file_dialog_new(title, GQ_WMCLASS, "select_path", td->entry, |
9 | 861 tab_completion_select_cancel_cb, td); |
862 file_dialog_add_button(td->fd, GTK_STOCK_OK, NULL, | |
863 tab_completion_select_ok_cb, TRUE); | |
864 | |
865 generic_dialog_add_message(GENERIC_DIALOG(td->fd), NULL, title, NULL); | |
866 | |
867 path = gtk_entry_get_text(GTK_ENTRY(td->entry)); | |
868 if (strlen(path) == 0) path = NULL; | |
869 if (td->fd_folders_only) | |
870 { | |
871 file_dialog_add_path_widgets(td->fd, NULL, path, td->history_key, NULL, NULL); | |
872 } | |
873 else | |
874 { | |
875 file_dialog_add_path_widgets(td->fd, NULL, path, td->history_key, "*", _("All files")); | |
876 } | |
877 | |
878 gtk_widget_show(GENERIC_DIALOG(td->fd)->dialog); | |
879 } | |
880 | |
881 static void tab_completion_select_pressed(GtkWidget *widget, gpointer data) | |
882 { | |
883 TabCompData *td = data; | |
884 | |
885 tab_completion_select_show(td); | |
886 } | |
887 | |
888 void tab_completion_add_select_button(GtkWidget *entry, const gchar *title, gint folders_only) | |
889 { | |
890 TabCompData *td; | |
891 GtkWidget *parent; | |
892 GtkWidget *hbox; | |
893 | |
894 td = g_object_get_data(G_OBJECT(entry), "tab_completion_data"); | |
895 | |
896 if (!td) return; | |
897 | |
898 g_free(td->fd_title); | |
899 td->fd_title = g_strdup(title); | |
900 td->fd_folders_only = folders_only; | |
901 | |
902 if (td->fd_button) return; | |
903 | |
904 parent = (td->combo) ? td->combo : td->entry; | |
905 | |
906 hbox = gtk_widget_get_parent(parent); | |
907 if (!GTK_IS_BOX(hbox)) return; | |
908 | |
909 td->fd_button = gtk_button_new_with_label("..."); | |
910 g_signal_connect(G_OBJECT(td->fd_button), "size_allocate", | |
911 G_CALLBACK(tab_completion_button_size_allocate), parent); | |
912 g_signal_connect(G_OBJECT(td->fd_button), "clicked", | |
913 G_CALLBACK(tab_completion_select_pressed), td); | |
914 | |
915 gtk_box_pack_start(GTK_BOX(hbox), td->fd_button, FALSE, FALSE, 0); | |
916 | |
917 gtk_widget_show(td->fd_button); | |
918 } |