From f649f841b919cf2fa5c40f7d8b1bf1dd61300a81 Mon Sep 17 00:00:00 2001 From: mbkma Date: Sun, 31 May 2020 01:04:31 +0200 Subject: Implement navigation through history stack equations using keyboard --- src/math-history-entry.c | 6 ++++++ src/math-history-entry.h | 3 +++ src/math-history.c | 24 ++++++++++++++++++++++++ src/math-history.h | 9 +++++++++ src/math-window.c | 26 ++++++++++++++++++++++++-- 5 files changed, 66 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/math-history-entry.c b/src/math-history-entry.c index 8a6b4ae..8277f88 100644 --- a/src/math-history-entry.c +++ b/src/math-history-entry.c @@ -88,6 +88,12 @@ math_history_entry_insert_entry(MathHistoryEntry *history_entry, const gchar *eq #undef get_widget } +gchar * +math_history_entry_get_equation(MathHistoryEntry *history_entry) +{ + return gtk_widget_get_tooltip_text(history_entry->priv->equation_label); +} + static void math_history_entry_class_init(MathHistoryEntryClass *klass) { diff --git a/src/math-history-entry.h b/src/math-history-entry.h index 53ccc88..2133ade 100644 --- a/src/math-history-entry.h +++ b/src/math-history-entry.h @@ -44,6 +44,9 @@ math_history_entry_new(MathEquation *equation); void math_history_entry_insert_entry(MathHistoryEntry *history_entry, const gchar *equation, const gchar *answer_four_digits, const gchar *answer_nine_digits); +gchar * +math_history_entry_get_equation(MathHistoryEntry *history_entry); + G_END_DECLS #endif /* MATH_HISTORY_ENTRY_VIEW_H */ diff --git a/src/math-history.c b/src/math-history.c index 49d63a2..403a04f 100644 --- a/src/math-history.c +++ b/src/math-history.c @@ -22,6 +22,7 @@ struct MathHistoryPrivate gchar *last_answer; gchar *last_equation; + int current; /* 0 is the first entry, rows-1 the most recent entry */ int rows; GtkWidget *listbox; }; @@ -82,6 +83,7 @@ math_history_insert_entry (MathHistory *history, char *equation, MPNumber *answe history->priv->last_answer = g_strdup(answer_nine_digits); history->priv->last_equation = g_strdup(equation); history->priv->rows++; + history->priv->current = history->priv->rows; g_signal_emit_by_name(history, "row-added"); g_free(answer_four_digits); @@ -94,12 +96,33 @@ math_history_clear(MathHistory *history) GList *children, *iter; history->priv->rows = 0; + history->priv->current = 0; children = gtk_container_get_children(GTK_CONTAINER(history->priv->listbox)); for (iter = children; iter != NULL; iter = g_list_next(iter)) gtk_widget_destroy(GTK_WIDGET(iter->data)); g_list_free(children); } +gpointer +math_history_get_entry_at(MathHistory *history, int index) +{ + if (index >= 0 && index < history->priv->rows) + return MATH_HISTORY_ENTRY(gtk_list_box_get_row_at_index(GTK_LIST_BOX(history->priv->listbox), index)); + return NULL; +} + +void +math_history_set_current(MathHistory *history, int value) +{ + history->priv->current = CLAMP (history->priv->current + value, 0, history->priv->rows - 1); +} + +int +math_history_get_current(MathHistory *history) +{ + return history->priv->current; +} + static void math_history_class_init(MathHistoryClass *klass) { @@ -124,6 +147,7 @@ math_history_init(MathHistory *history) gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(history), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); gtk_scrolled_window_set_placement(GTK_SCROLLED_WINDOW(history), GTK_CORNER_TOP_LEFT); + history->priv->current = 0; history->priv->rows = 0; history->priv->last_answer = g_strdup(""); history->priv->last_equation = g_strdup(""); diff --git a/src/math-history.h b/src/math-history.h index 7c7b079..87dab7a 100644 --- a/src/math-history.h +++ b/src/math-history.h @@ -45,6 +45,15 @@ math_history_new(MathEquation *equation); void math_history_insert_entry(MathHistory *history, char *equation, MPNumber *answer, int number_base); +gpointer +math_history_get_entry_at(MathHistory *history, int index); + +void +math_history_set_current(MathHistory *history, int value); + +int +math_history_get_current(MathHistory *history); + void math_history_clear(MathHistory *history); diff --git a/src/math-window.c b/src/math-window.c index 08ac3e3..e872f4f 100644 --- a/src/math-window.c +++ b/src/math-window.c @@ -202,7 +202,29 @@ key_press_cb(MathWindow *window, GdkEventKey *event) gboolean result; g_signal_emit_by_name(window->priv->display, "key-press-event", event, &result); - if (math_buttons_get_mode (window->priv->buttons) == PROGRAMMING && (event->state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK) { + /* Keyboard navigation for history */ + if ((event->state & GDK_MOD1_MASK) == GDK_MOD1_MASK && (event->keyval == GDK_KEY_Up || event->keyval == GDK_KEY_Down)) + { + switch (event->keyval) + { + case GDK_KEY_Up: + math_history_set_current(window->priv->history, -1); + break; + case GDK_KEY_Down: + math_history_set_current(window->priv->history, 1); + break; + } + + MathHistoryEntry *entry = math_history_get_entry_at(window->priv->history, math_history_get_current(window->priv->history)); + if (entry) + { + gchar *equation_string = math_history_entry_get_equation(entry); + math_equation_set(window->priv->equation, equation_string); + g_free(equation_string); + } + return TRUE; + } + else if (math_buttons_get_mode (window->priv->buttons) == PROGRAMMING && (event->state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK) { switch(event->keyval) { /* Binary */ @@ -540,7 +562,7 @@ static void create_menu(MathWindow* window) menu = add_menu(window->priv->menu_bar, VIEW_MENU_LABEL); window->priv->view_history_menu_item = add_menu_item(menu, gtk_check_menu_item_new_with_mnemonic(VIEW_HISTORY_LABEL), G_CALLBACK(history_check_toggled_cb), window); - gtk_widget_add_accelerator(window->priv->view_history_menu_item, "activate", accel_group, GDK_KEY_H, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE); + gtk_widget_add_accelerator(window->priv->view_history_menu_item, "activate", accel_group, GDK_KEY_H, GDK_CONTROL_MASK | GDK_SHIFT_MASK, GTK_ACCEL_VISIBLE); menu = add_menu(window->priv->menu_bar, HELP_MENU_LABEL); menu_item = add_menu_item(menu, gtk_image_menu_item_new_from_icon("help-browser", HELP_CONTENTS_LABEL, accel_group), G_CALLBACK(help_cb), window); -- cgit v1.2.1