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