10 #include <gdk/gdkkeysyms.h> /* for key values */
12 #include "tabcomp.xpm"
14 /* ----------------------------------------------------------------
15 Tab completion routines, can be connected to any gtkentry widget
16 using the tab_completion_add_to_entry() function.
17 Use remove_trailing_slash() to strip the trailing '/'.
18 ----------------------------------------------------------------*/
20 typedef struct _TabCompData TabCompData;
26 void (*enter_func)(gchar *, gpointer);
27 void (*tab_func)(gchar *, gpointer);
37 typedef struct _HistoryData HistoryData;
44 static GList *history_list = NULL;
46 static void tab_completion_free_list(TabCompData *td);
47 static void tab_completion_read_dir(TabCompData *td, gchar *path);
48 static void tab_completion_destroy(GtkWidget *widget, gpointer data);
49 static void tab_completion_emit_enter_signal(TabCompData *td);
50 static void tab_completion_emit_tab_signal(TabCompData *td);
51 static gint tab_completion_do(TabCompData *td);
52 static gint tab_completion_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data);
53 static void tab_completion_button_pressed(GtkWidget *widget, gpointer data);
54 static GtkWidget *tab_completion_create_complete_button(GtkWidget *window, GtkWidget *entry);
56 static void history_list_free(HistoryData *hd);
57 static HistoryData *history_list_find_by_key(const gchar* key);
58 static gchar *history_list_find_last_path_by_key(const gchar* key);
59 static void history_list_free_key(const gchar *key);
60 static void history_list_add_to_key(const gchar *key, const gchar *path, gint max);
62 static void history_list_free(HistoryData *hd)
79 static HistoryData *history_list_find_by_key(const gchar* key)
81 GList *work = history_list;
84 HistoryData *hd = work->data;
85 if (strcmp(hd->key, key) == 0) return hd;
91 static gchar *history_list_find_last_path_by_key(const gchar* key)
94 hd = history_list_find_by_key(key);
95 if (!hd || !hd->list) return NULL;
97 return hd->list->data;
100 static void history_list_free_key(const gchar *key)
103 hd = history_list_find_by_key(key);
106 history_list = g_list_remove(history_list, hd);
107 history_list_free(hd);
110 static void history_list_add_to_key(const gchar *key, const gchar *path, gint max)
115 hd = history_list_find_by_key(key);
118 hd = g_new(HistoryData, 1);
119 hd->key = g_strdup(key);
121 history_list = g_list_prepend(history_list, hd);
124 /* if already in the list, simply move it to the top */
128 gchar *buf = work->data;
130 if (strcmp(buf, path) == 0)
132 hd->list = g_list_remove(hd->list, buf);
133 hd->list = g_list_prepend(hd->list, buf);
138 hd->list = g_list_prepend(hd->list, g_strdup(path));
142 while(hd->list && g_list_length(hd->list) > max)
144 GList *work = g_list_last(hd->list);
145 gchar *buf = work->data;
146 hd->list = g_list_remove(hd->list, buf);
152 static void tab_completion_free_list(TabCompData *td)
156 g_free(td->dir_path);
159 list = td->file_list;
167 g_list_free(td->file_list);
168 td->file_list = NULL;
171 static void tab_completion_read_dir(TabCompData *td, gchar *path)
177 tab_completion_free_list(td);
179 if((dp = opendir(path))==NULL)
184 while ((dir = readdir(dp)) != NULL)
186 /* skips removed files */
189 gchar *name = dir->d_name;
190 if (strcmp(name, ".") != 0 && strcmp(name, "..") != 0)
192 list = g_list_prepend(list, g_strdup(name));
198 td->dir_path = g_strdup(path);
199 td->file_list = list;
202 static void tab_completion_destroy(GtkWidget *widget, gpointer data)
204 TabCompData *td = data;
205 tab_completion_free_list(td);
206 g_free(td->history_key);
210 static void tab_completion_emit_enter_signal(TabCompData *td)
213 if (!td->enter_func) return;
215 text = g_strdup(gtk_entry_get_text(GTK_ENTRY(td->entry)));
220 text = g_strconcat(homedir(), t + 1, NULL);
224 td->enter_func(text, td->enter_data);
228 static void tab_completion_emit_tab_signal(TabCompData *td)
231 if (!td->tab_func) return;
233 text = g_strdup(gtk_entry_get_text(GTK_ENTRY(td->entry)));
238 text = g_strconcat(homedir(), t + 1, NULL);
242 td->tab_func(text, td->tab_data);
246 static gint tab_completion_do(TabCompData *td)
248 gchar *entry_text = gtk_entry_get_text(GTK_ENTRY(td->entry));
252 gint home_exp = FALSE;
254 /* home dir expansion */
255 if (entry_text[0] == '~')
257 entry_dir = g_strconcat(homedir(), entry_text + 1, NULL);
262 entry_dir = g_strdup(entry_text);
265 entry_file = filename_from_path(entry_text);
267 if (isfile(entry_dir))
271 gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
272 gtk_entry_set_position(GTK_ENTRY(td->entry), strlen(entry_dir));
277 if (isdir(entry_dir) && strcmp(entry_file, ".") != 0 && strcmp(entry_file, "..") != 0)
279 ptr = entry_dir + strlen(entry_dir) - 1;
284 gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
285 gtk_entry_set_position(GTK_ENTRY(td->entry), strlen(entry_dir));
292 gchar *buf = g_strconcat(entry_dir, "/", NULL);
293 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
294 gtk_entry_set_position(GTK_ENTRY(td->entry), strlen(buf));
301 ptr = filename_from_path(entry_dir);
302 if (ptr > entry_dir) ptr--;
305 if (strlen(entry_dir) == 0)
308 entry_dir = g_strdup("/");
311 if (isdir(entry_dir))
315 gint l = strlen(entry_file);
317 if (!td->dir_path || !td->file_list || strcmp(td->dir_path, entry_dir) != 0)
319 tab_completion_read_dir(td, entry_dir);
322 if (strcmp(entry_dir, "/") == 0) entry_dir[0] = '\0';
324 list = td->file_list;
327 gchar *file = list->data;
328 if (strncmp(entry_file, file, l) == 0)
330 poss = g_list_prepend(poss, file);
339 gchar *file = poss->data;
342 buf = g_strconcat(entry_dir, "/", file, NULL);
347 buf = g_strconcat(entry_dir, "/", file, "/", NULL);
349 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
350 gtk_entry_set_position(GTK_ENTRY(td->entry), strlen(buf));
358 gint c = strlen(entry_file);
360 gchar *test_file = poss->data;
365 if (!list) done = TRUE;
368 gchar *file = list->data;
369 if (strlen(file) < c || strncmp(test_file, file, c) != 0)
382 file = g_strdup(test_file);
384 buf = g_strconcat(entry_dir, "/", file, NULL);
385 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
386 gtk_entry_set_position(GTK_ENTRY(td->entry), strlen(buf));
403 static gint tab_completion_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
405 TabCompData *td = data;
406 gint stop_signal = FALSE;
408 switch (event->keyval)
411 if (tab_completion_do(td))
413 tab_completion_emit_tab_signal(td);
418 tab_completion_emit_enter_signal(td);
427 if (stop_signal) gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), "key_press_event");
434 static void tab_completion_button_pressed(GtkWidget *widget, gpointer data)
437 GtkWidget *entry = data;
439 td = gtk_object_get_data(GTK_OBJECT(entry), "tab_completion_data");
443 if (!GTK_WIDGET_HAS_FOCUS(entry))
445 gtk_widget_grab_focus(entry);
448 if (tab_completion_do(td))
450 tab_completion_emit_tab_signal(td);
454 static GtkWidget *tab_completion_create_complete_button(GtkWidget *window, GtkWidget *entry)
458 GdkPixmap *pixmap = NULL;
459 GdkBitmap *mask = NULL;
462 style = gtk_widget_get_style(window);
463 pixmap = gdk_pixmap_create_from_xpm_d(window->window, &mask,
464 &style->bg[GTK_STATE_NORMAL], (gchar **)tabcomp_xpm);
466 button = gtk_button_new();
467 GTK_WIDGET_UNSET_FLAGS(button, GTK_CAN_FOCUS);
468 gtk_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) tab_completion_button_pressed, entry);
470 icon = gtk_pixmap_new(pixmap, mask);
471 gtk_container_add(GTK_CONTAINER(button), icon);
472 gtk_widget_show(icon);
478 *----------------------------------------------------------------------------
480 *----------------------------------------------------------------------------
483 GtkWidget *tab_completion_new_with_history(GtkWidget **entry, GtkWidget *window, gchar *text,
484 const gchar *history_key, gint max_levels,
485 void (*enter_func)(gchar *, gpointer), gpointer data)
492 combo = gtk_combo_new();
493 gtk_combo_set_use_arrows(GTK_COMBO(combo), FALSE);
495 button = tab_completion_create_complete_button(window, GTK_COMBO(combo)->entry);
496 gtk_box_pack_start(GTK_BOX(combo), button, FALSE, FALSE, 0);
497 gtk_box_reorder_child(GTK_BOX(combo), button, 1);
498 gtk_widget_show(button);
500 tab_completion_add_to_entry(GTK_COMBO(combo)->entry, enter_func, data);
502 td = gtk_object_get_data(GTK_OBJECT(GTK_COMBO(combo)->entry), "tab_completion_data");
503 if (!td) return; /* this should never happen! */
506 td->has_history = TRUE;
507 td->history_key = g_strdup(history_key);
508 td->history_levels = max_levels;
510 hd = history_list_find_by_key(td->history_key);
513 gtk_combo_set_popdown_strings(GTK_COMBO(combo), hd->list);
516 if (text) gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo)->entry), text);
518 if (entry) *entry = GTK_COMBO(combo)->entry;
522 gchar *tab_completion_set_to_last_history(GtkWidget *entry)
524 TabCompData *td = gtk_object_get_data(GTK_OBJECT(entry), "tab_completion_data");
527 if (!td || !td->has_history) return NULL;
529 buf = history_list_find_last_path_by_key(td->history_key);
532 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
538 void tab_completion_append_to_history(GtkWidget *entry, gchar *path)
540 TabCompData *td = gtk_object_get_data(GTK_OBJECT(entry), "tab_completion_data");
543 if (!td || !td->has_history) return;
545 history_list_add_to_key(td->history_key, path, td->history_levels);
546 hd = history_list_find_by_key(td->history_key);
549 gtk_combo_set_popdown_strings(GTK_COMBO(td->combo), hd->list);
553 GtkWidget *tab_completion_new(GtkWidget **entry, GtkWidget *window, gchar *text,
554 void (*enter_func)(gchar *, gpointer), gpointer data)
560 hbox = gtk_hbox_new(FALSE, 0);
562 newentry = gtk_entry_new();
563 if (text) gtk_entry_set_text(GTK_ENTRY(newentry), text);
564 gtk_box_pack_start(GTK_BOX(hbox), newentry, TRUE, TRUE, 0);
565 gtk_widget_show(newentry);
567 button = tab_completion_create_complete_button(window, newentry);
568 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
569 gtk_widget_show(button);
571 tab_completion_add_to_entry(newentry, enter_func, data);
573 if (entry) *entry = newentry;
577 void tab_completion_add_to_entry(GtkWidget *entry, void (*enter_func)(gchar *, gpointer), gpointer data)
582 printf("Tab completion error: entry != NULL\n");
586 td = g_new0(TabCompData, 1);
589 td->file_list = NULL;
590 td->enter_func = enter_func;
591 td->enter_data = data;
595 td->has_history = FALSE;
596 td->history_key = NULL;
597 td->history_levels = 0;
599 gtk_object_set_data(GTK_OBJECT(td->entry), "tab_completion_data", td);
601 gtk_signal_connect(GTK_OBJECT(entry), "key_press_event",
602 (GtkSignalFunc) tab_completion_key_pressed, td);
603 gtk_signal_connect(GTK_OBJECT(entry), "destroy",
604 (GtkSignalFunc) tab_completion_destroy, td);
607 void tab_completion_add_tab_func(GtkWidget *entry, void (*tab_func)(gchar *, gpointer), gpointer data)
609 TabCompData *td = gtk_object_get_data(GTK_OBJECT(entry), "tab_completion_data");
613 td->tab_func = tab_func;
617 gchar *remove_trailing_slash(gchar *path)
621 if (!path) return NULL;
623 ret = g_strdup(path);
625 if (l > 1 && ret[l - 1] == '/') ret[l - 1] = '\0';