Add overlay_show to reduce repetition.

This commit is contained in:
Simon Gomizelj 2012-06-03 23:37:15 -04:00
parent fdd413c854
commit 63293797f3
1 changed files with 27 additions and 29 deletions

View File

@ -20,6 +20,7 @@
enum overlay_mode { enum overlay_mode {
OVERLAY_HIDDEN, OVERLAY_HIDDEN,
OVERLAY_SEARCH, OVERLAY_SEARCH,
OVERLAY_RSEARCH,
OVERLAY_COMPLETION OVERLAY_COMPLETION
}; };
@ -28,7 +29,6 @@ typedef struct search_panel_info {
GtkWidget *entry; GtkWidget *entry;
GtkBin *panel; GtkBin *panel;
enum overlay_mode mode; enum overlay_mode mode;
bool reverse;
} search_panel_info; } search_panel_info;
static gboolean always_selected() { static gboolean always_selected() {
@ -103,7 +103,11 @@ static gboolean entry_key_press_cb(GtkEntry *entry, GdkEventKey *event, search_p
switch (info->mode) { switch (info->mode) {
case OVERLAY_SEARCH: case OVERLAY_SEARCH:
search(VTE_TERMINAL(info->vte), text, info->reverse); search(VTE_TERMINAL(info->vte), text, false);
ret = TRUE;
break;
case OVERLAY_RSEARCH:
search(VTE_TERMINAL(info->vte), text, true);
ret = TRUE; ret = TRUE;
break; break;
case OVERLAY_COMPLETION: case OVERLAY_COMPLETION:
@ -124,6 +128,24 @@ static gboolean entry_key_press_cb(GtkEntry *entry, GdkEventKey *event, search_p
return ret; return ret;
} }
static void overlay_show(search_panel_info *info, enum overlay_mode mode, bool complete) {
if (complete) {
GtkEntryCompletion *completion = gtk_entry_completion_new();
gtk_entry_set_completion(GTK_ENTRY(info->entry), completion);
g_object_unref(completion);
GtkTreeModel *completion_model = create_completion_model(VTE_TERMINAL(info->vte));
gtk_entry_completion_set_model(completion, completion_model);
g_object_unref(completion_model);
gtk_entry_completion_set_text_column(completion, 0);
}
info->mode = mode;
gtk_widget_show(GTK_WIDGET(info->panel));
gtk_widget_grab_focus(info->entry);
}
static gboolean key_press_cb(VteTerminal *vte, GdkEventKey *event, search_panel_info *info) { static gboolean key_press_cb(VteTerminal *vte, GdkEventKey *event, search_panel_info *info) {
const guint modifiers = event->state & gtk_accelerator_get_default_mod_mask(); const guint modifiers = event->state & gtk_accelerator_get_default_mod_mask();
if (modifiers == (GDK_CONTROL_MASK|GDK_SHIFT_MASK)) { if (modifiers == (GDK_CONTROL_MASK|GDK_SHIFT_MASK)) {
@ -143,16 +165,10 @@ static gboolean key_press_cb(VteTerminal *vte, GdkEventKey *event, search_panel_
vte_terminal_copy_primary(vte); vte_terminal_copy_primary(vte);
return TRUE; return TRUE;
case KEY(KEY_SEARCH): case KEY(KEY_SEARCH):
info->mode = OVERLAY_SEARCH; overlay_show(info, OVERLAY_SEARCH, true);
info->reverse = false;
gtk_widget_show(GTK_WIDGET(info->panel));
gtk_widget_grab_focus(info->entry);
return TRUE; return TRUE;
case KEY(KEY_RSEARCH): case KEY(KEY_RSEARCH):
info->mode = OVERLAY_SEARCH; overlay_show(info, OVERLAY_RSEARCH, true);
info->reverse = true;
gtk_widget_show(GTK_WIDGET(info->panel));
gtk_widget_grab_focus(info->entry);
return TRUE; return TRUE;
case KEY(KEY_URL): case KEY(KEY_URL):
search(vte, url_regex, false); search(vte, url_regex, false);
@ -162,24 +178,7 @@ static gboolean key_press_cb(VteTerminal *vte, GdkEventKey *event, search_panel_
return TRUE; return TRUE;
} }
} else if (modifiers == GDK_CONTROL_MASK && event->keyval == GDK_KEY_Tab) { } else if (modifiers == GDK_CONTROL_MASK && event->keyval == GDK_KEY_Tab) {
// Create the completion object overlay_show(info, OVERLAY_COMPLETION, true);
GtkEntryCompletion *completion = gtk_entry_completion_new();
// Assign the completion to the entry
gtk_entry_set_completion(GTK_ENTRY(info->entry), completion);
g_object_unref(completion);
// Create a tree model and use it as the completion model
GtkTreeModel *completion_model = create_completion_model(vte);
gtk_entry_completion_set_model(completion, completion_model);
g_object_unref(completion_model);
// Use model column 0 as the text column
gtk_entry_completion_set_text_column(completion, 0);
info->mode = OVERLAY_COMPLETION;
gtk_widget_show(GTK_WIDGET(info->panel));
gtk_widget_grab_focus(info->entry);
return TRUE; return TRUE;
} }
if (modifiers == GDK_CONTROL_MASK && event->keyval == GDK_KEY_Tab) { if (modifiers == GDK_CONTROL_MASK && event->keyval == GDK_KEY_Tab) {
@ -343,7 +342,6 @@ int main(int argc, char **argv) {
.entry = entry, .entry = entry,
.panel = GTK_BIN(alignment), .panel = GTK_BIN(alignment),
.mode = OVERLAY_HIDDEN, .mode = OVERLAY_HIDDEN,
.reverse = false
}; };
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL); g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);