📄 ui_tabcomp.c
字号:
while(list) { gchar *file = list->data; if (strncmp(entry_file, file, l) == 0) { poss = g_list_prepend(poss, file); } list = list->next; } if (poss) { if (!poss->next) { gchar *file = poss->data; gchar *buf; buf = g_strconcat(entry_dir, "/", file, NULL); if (isdir(buf)) { g_free(buf); buf = g_strconcat(entry_dir, "/", file, "/", NULL); } gtk_entry_set_text(GTK_ENTRY(td->entry), buf); gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf)); g_free(buf); g_list_free(poss); g_free(entry_dir); return TRUE; } else { gint c = strlen(entry_file); gint done = FALSE; gchar *test_file = poss->data; while (!done) { list = poss; if (!list) done = TRUE; while(list && !done) { gchar *file = list->data; if (strlen(file) < c || strncmp(test_file, file, c) != 0) { done = TRUE; } list = list->next; } c++; } c -= 2; if (c > 0) { gchar *file; gchar *buf; file = g_strdup(test_file); file[c] = '\0'; buf = g_strconcat(entry_dir, "/", file, NULL); gtk_entry_set_text(GTK_ENTRY(td->entry), buf); gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));#ifdef TAB_COMPLETION_ENABLE_POPUP_MENU poss = g_list_sort(poss, simple_sort); tab_completion_popup_list(td, poss);#endif g_free(file); g_free(buf); g_list_free(poss); g_free(entry_dir); return TRUE; } } g_list_free(poss); } } g_free(entry_dir); return FALSE;}static gint tab_completion_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data){ TabCompData *td = data; gint stop_signal = FALSE; switch (event->keyval) { case GDK_Tab: if (!(event->state & GDK_CONTROL_MASK)) { if (tab_completion_do(td)) { tab_completion_emit_tab_signal(td); } stop_signal = TRUE; } break; case GDK_Return: case GDK_KP_Enter: if (td->fd_button && (event->state & GDK_CONTROL_MASK)) { tab_completion_select_show(td); stop_signal = TRUE; } else if (tab_completion_emit_enter_signal(td)) { stop_signal = TRUE; } break; default: break; } if (stop_signal) g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event"); return (stop_signal);}static void tab_completion_button_pressed(GtkWidget *widget, gpointer data){ TabCompData *td; GtkWidget *entry = data; td = g_object_get_data(G_OBJECT(entry), "tab_completion_data"); if (!td) return; if (!GTK_WIDGET_HAS_FOCUS(entry)) { gtk_widget_grab_focus(entry); } if (tab_completion_do(td)) { tab_completion_emit_tab_signal(td); }}static void tab_completion_button_size_allocate(GtkWidget *button, GtkAllocation *allocation, gpointer data){ GtkWidget *parent = data; if (allocation->height > parent->allocation.height) { GtkAllocation button_allocation; button_allocation = button->allocation; button_allocation.height = parent->allocation.height; button_allocation.y = parent->allocation.y + (parent->allocation.height - parent->allocation.height) / 2; gtk_widget_size_allocate(button, &button_allocation); }}static GtkWidget *tab_completion_create_complete_button(GtkWidget *entry, GtkWidget *parent){ GtkWidget *button; GtkWidget *icon; GdkPixbuf *pixbuf; button = gtk_button_new(); GTK_WIDGET_UNSET_FLAGS(button, GTK_CAN_FOCUS); g_signal_connect(G_OBJECT(button), "size_allocate", G_CALLBACK(tab_completion_button_size_allocate), parent); g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(tab_completion_button_pressed), entry); pixbuf = gdk_pixbuf_new_from_inline(-1, icon_tabcomp, FALSE, NULL); icon = gtk_image_new_from_pixbuf(pixbuf); gdk_pixbuf_unref(pixbuf); gtk_container_add(GTK_CONTAINER(button), icon); gtk_widget_show(icon); return button;}/* *---------------------------------------------------------------------------- * public interface *---------------------------------------------------------------------------- */GtkWidget *tab_completion_new_with_history(GtkWidget **entry, const gchar *text, const gchar *history_key, gint max_levels, void (*enter_func)(const gchar *, gpointer), gpointer data){ GtkWidget *box; GtkWidget *combo; GtkWidget *combo_entry; GtkWidget *button; GList *work; TabCompData *td; gint n = 0; box = gtk_hbox_new(FALSE, 0); combo = gtk_combo_box_entry_new_text(); gtk_box_pack_start(GTK_BOX(box), combo, TRUE, TRUE, 0); gtk_widget_show(combo); combo_entry = GTK_BIN(combo)->child;#if 0 gtk_combo_set_case_sensitive(GTK_COMBO(combo), TRUE); gtk_combo_set_use_arrows(GTK_COMBO(combo), FALSE);#endif button = tab_completion_create_complete_button(combo_entry, combo); gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 0); gtk_widget_show(button); tab_completion_add_to_entry(combo_entry, enter_func, data); td = g_object_get_data(G_OBJECT(combo_entry), "tab_completion_data"); if (!td) return NULL; /* this should never happen! */ td->combo = combo; td->has_history = TRUE; td->history_key = g_strdup(history_key); td->history_levels = max_levels; work = history_list_get_by_key(td->history_key); work = history_list_get_by_key(history_key); while (work) { gtk_combo_box_append_text(GTK_COMBO_BOX(combo), (gchar *)work->data); work = work->next; n++; } if (text) { gtk_entry_set_text(GTK_ENTRY(combo_entry), text); } else if (n > 0) { gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0); } if (entry) *entry = combo_entry; return box;}const gchar *tab_completion_set_to_last_history(GtkWidget *entry){ TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data"); const gchar *buf; if (!td || !td->has_history) return NULL; buf = history_list_find_last_path_by_key(td->history_key); if (buf) { gtk_entry_set_text(GTK_ENTRY(td->entry), buf); } return buf;}void tab_completion_append_to_history(GtkWidget *entry, const gchar *path){ TabCompData *td; GtkTreeModel *store; GList *work; td = g_object_get_data(G_OBJECT(entry), "tab_completion_data"); if (!path) return; if (!td || !td->has_history) return; history_list_add_to_key(td->history_key, path, td->history_levels); gtk_combo_box_set_active(GTK_COMBO_BOX(td->combo), -1); store = gtk_combo_box_get_model(GTK_COMBO_BOX(td->combo)); gtk_list_store_clear(GTK_LIST_STORE(store)); work = history_list_get_by_key(td->history_key); while (work) { gtk_combo_box_append_text(GTK_COMBO_BOX(td->combo), (gchar *)work->data); work = work->next; }}GtkWidget *tab_completion_new(GtkWidget **entry, const gchar *text, void (*enter_func)(const gchar *, gpointer), gpointer data){ GtkWidget *hbox; GtkWidget *button; GtkWidget *newentry; hbox = gtk_hbox_new(FALSE, 0); newentry = gtk_entry_new(); if (text) gtk_entry_set_text(GTK_ENTRY(newentry), text); gtk_box_pack_start(GTK_BOX(hbox), newentry, TRUE, TRUE, 0); gtk_widget_show(newentry); button = tab_completion_create_complete_button(newentry, newentry); gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0); gtk_widget_show(button); tab_completion_add_to_entry(newentry, enter_func, data); if (entry) *entry = newentry; return hbox;}void tab_completion_add_to_entry(GtkWidget *entry, void (*enter_func)(const gchar *, gpointer), gpointer data){ TabCompData *td; if (!entry) { printf("Tab completion error: entry != NULL\n"); return; } td = g_new0(TabCompData, 1); td->entry = entry; td->dir_path = NULL; td->file_list = NULL; td->enter_func = enter_func; td->enter_data = data; td->tab_func = NULL; td->tab_data = NULL; td->has_history = FALSE; td->history_key = NULL; td->history_levels = 0; g_object_set_data(G_OBJECT(td->entry), "tab_completion_data", td); g_signal_connect(G_OBJECT(entry), "key_press_event", G_CALLBACK(tab_completion_key_pressed), td); g_signal_connect(G_OBJECT(entry), "destroy", G_CALLBACK(tab_completion_destroy), td);}void tab_completion_add_tab_func(GtkWidget *entry, void (*tab_func)(const gchar *, gpointer), gpointer data){ TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data"); if (!td) return; td->tab_func = tab_func; td->tab_data = data;}gchar *remove_trailing_slash(const gchar *path){ gchar *ret; gint l; if (!path) return NULL; ret = g_strdup(path); l = strlen(ret); if (l > 1 && ret[l - 1] == '/') ret[l - 1] = '\0'; return ret;}static void tab_completion_select_cancel_cb(FileDialog *fd, gpointer data){ TabCompData *td = data; td->fd = NULL; file_dialog_close(fd);}static void tab_completion_select_ok_cb(FileDialog *fd, gpointer data){ TabCompData *td = data; gtk_entry_set_text(GTK_ENTRY(td->entry), gtk_entry_get_text(GTK_ENTRY(fd->entry))); tab_completion_select_cancel_cb(fd, data); tab_completion_emit_enter_signal(td);}static void tab_completion_select_show(TabCompData *td){ const gchar *title; const gchar *path; if (td->fd) { gtk_window_present(GTK_WINDOW(GENERIC_DIALOG(td->fd)->dialog)); return; } title = (td->fd_title) ? td->fd_title : _("Select path"); td->fd = file_dialog_new(title, PACKAGE, "select_path", td->entry, tab_completion_select_cancel_cb, td); file_dialog_add_button(td->fd, GTK_STOCK_OK, NULL, tab_completion_select_ok_cb, TRUE); generic_dialog_add_message(GENERIC_DIALOG(td->fd), NULL, title, NULL); path = gtk_entry_get_text(GTK_ENTRY(td->entry)); if (strlen(path) == 0) path = NULL; if (td->fd_folders_only) { file_dialog_add_path_widgets(td->fd, NULL, path, td->history_key, NULL, NULL); } else { file_dialog_add_path_widgets(td->fd, NULL, path, td->history_key, "*", _("All files")); } gtk_widget_show(GENERIC_DIALOG(td->fd)->dialog);}static void tab_completion_select_pressed(GtkWidget *widget, gpointer data){ TabCompData *td = data; tab_completion_select_show(td);}void tab_completion_add_select_button(GtkWidget *entry, const gchar *title, gint folders_only){ TabCompData *td; GtkWidget *parent; GtkWidget *hbox; td = g_object_get_data(G_OBJECT(entry), "tab_completion_data"); if (!td) return; g_free(td->fd_title); td->fd_title = g_strdup(title); td->fd_folders_only = folders_only; if (td->fd_button) return; parent = (td->combo) ? td->combo : td->entry; hbox = gtk_widget_get_parent(parent); if (!GTK_IS_BOX(hbox)) return; td->fd_button = gtk_button_new_with_label("..."); g_signal_connect(G_OBJECT(td->fd_button), "size_allocate", G_CALLBACK(tab_completion_button_size_allocate), parent); g_signal_connect(G_OBJECT(td->fd_button), "clicked", G_CALLBACK(tab_completion_select_pressed), td); gtk_box_pack_start(GTK_BOX(hbox), td->fd_button, FALSE, FALSE, 0); gtk_widget_show(td->fd_button);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -