From e3ea3965890dcbccc9a7d2958472f0ed46b872fc Mon Sep 17 00:00:00 2001 From: mbkma Date: Sat, 20 Feb 2021 09:45:59 +0100 Subject: history view follows preferences --- src/math-equation.c | 22 +++++++++++++++++----- src/math-history-entry.c | 47 ++++++++++++++++++++++++++++++++++------------- src/math-history-entry.h | 5 ++++- src/math-history.c | 41 +++++++++++++++++++---------------------- src/math-history.h | 5 ++++- src/math-window.c | 15 ++++++++++++--- 6 files changed, 90 insertions(+), 45 deletions(-) (limited to 'src') diff --git a/src/math-equation.c b/src/math-equation.c index 6a0a37a..d332718 100644 --- a/src/math-equation.c +++ b/src/math-equation.c @@ -288,6 +288,8 @@ reformat_display(MathEquation *equation) /* Add/remove thousands separators */ reformat_separators(equation); + + g_signal_emit_by_name(equation, "display-changed"); } @@ -980,8 +982,7 @@ math_equation_set_with_history(MathEquation *equation, const gchar *text) /* Notify history */ state = get_current_state(equation); - g_signal_emit_by_name(equation, "history", state->expression, &x, - mp_serializer_get_base(equation->priv->serializer)); + g_signal_emit_by_name(equation, "history", state->expression, &x); free_state(state); mp_clear(&x); @@ -1000,7 +1001,7 @@ math_equation_set_number(MathEquation *equation, const MPNumber *x) /* Notify history */ state = get_current_state(equation); - g_signal_emit_by_name(equation, "history", state->expression, x, mp_serializer_get_base(equation->priv->serializer)); + g_signal_emit_by_name(equation, "history", state->expression, x); /* Show the number in the user chosen format */ text = mp_serializer_to_string(equation->priv->serializer, x); @@ -1846,7 +1847,7 @@ math_equation_class_init(MathEquationClass *klass) MP_TYPE_SERIALIZER, G_PARAM_READABLE)); - GType param_types[3] = {G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_INT}; + GType param_types[2] = {G_TYPE_STRING, G_TYPE_POINTER}; g_signal_newv("history", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST, @@ -1855,8 +1856,19 @@ math_equation_class_init(MathEquationClass *klass) NULL, NULL, G_TYPE_NONE, - 3, + 2, param_types); + + g_signal_new ("display-changed", + G_TYPE_FROM_CLASS(klass), + G_SIGNAL_RUN_LAST, + 0, + NULL, + NULL, + NULL, + G_TYPE_NONE, + 0, + NULL); } diff --git a/src/math-history-entry.c b/src/math-history-entry.c index 8277f88..f3f0f5b 100644 --- a/src/math-history-entry.c +++ b/src/math-history-entry.c @@ -20,6 +20,7 @@ struct MathHistoryEntryPrivate { MathEquation *equation; + MPNumber *number; GtkWidget *equation_label; GtkWidget *answer_label; }; @@ -59,33 +60,39 @@ equation_clicked_cb (GtkWidget *widget, GdkEventButton *eventbutton, MathHistory } void -math_history_entry_insert_entry(MathHistoryEntry *history_entry, const gchar *equation, const gchar *answer_four_digits, const gchar *answer_nine_digits) +math_history_entry_insert_entry(MathHistoryEntry *history_entry, const gchar *equation, MPNumber *answer, MpSerializer *serializer) { #define get_widget(x) GTK_WIDGET(gtk_builder_get_object(builder, x)) - GtkBuilder *builder = NULL; + GtkBuilder *builder; GtkWidget *grid; - GError *error = NULL; - - builder = gtk_builder_new(); - if(gtk_builder_add_from_resource (builder, UI_HISTORY_ENTRY_RESOURCE_PATH, &error) == 0) - { - g_warning("Error loading history-entry UI: %s", error->message); - g_clear_error(&error); - return; - } + + mp_set_from_mp(answer, history_entry->priv->number); + + builder = gtk_builder_new_from_resource(UI_HISTORY_ENTRY_RESOURCE_PATH); grid = get_widget("grid"); gtk_container_add(GTK_CONTAINER(history_entry), grid); history_entry->priv->equation_label = get_widget("equation_label"); history_entry->priv->answer_label = get_widget("answer_label"); gtk_widget_set_tooltip_text(history_entry->priv->equation_label, equation); - gtk_widget_set_tooltip_text(history_entry->priv->answer_label, answer_nine_digits); gtk_label_set_text(GTK_LABEL(history_entry->priv->equation_label), equation); - gtk_label_set_text(GTK_LABEL(history_entry->priv->answer_label), answer_four_digits); gtk_builder_connect_signals(builder, history_entry); g_object_unref(builder); #undef get_widget + + math_history_entry_redisplay(history_entry, serializer); +} + +void +math_history_entry_redisplay(MathHistoryEntry *history_entry, MpSerializer *serializer) +{ + gchar *answer = mp_serializer_to_string(serializer, history_entry->priv->number); + + gtk_widget_set_tooltip_text(history_entry->priv->answer_label, answer); + gtk_label_set_text(GTK_LABEL(history_entry->priv->answer_label), answer); + + g_free(answer); } gchar * @@ -94,13 +101,27 @@ math_history_entry_get_equation(MathHistoryEntry *history_entry) return gtk_widget_get_tooltip_text(history_entry->priv->equation_label); } +static void +math_history_entry_finalize(GObject *object) +{ + MathHistoryEntry *self = MATH_HISTORY_ENTRY (object); + + mp_free(self->priv->number); + + G_OBJECT_CLASS (math_history_entry_parent_class)->finalize (object); +} + static void math_history_entry_class_init(MathHistoryEntryClass *klass) { + GObjectClass *object_class = G_OBJECT_CLASS(klass); + + object_class->finalize = math_history_entry_finalize; } static void math_history_entry_init(MathHistoryEntry *history_entry) { history_entry->priv = math_history_entry_get_instance_private(history_entry); + history_entry->priv->number = mp_new_ptr(); } diff --git a/src/math-history-entry.h b/src/math-history-entry.h index 2133ade..f46f15f 100644 --- a/src/math-history-entry.h +++ b/src/math-history-entry.h @@ -42,7 +42,10 @@ MathHistoryEntry * 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); +math_history_entry_insert_entry(MathHistoryEntry *history_entry, const gchar *equation, MPNumber *answer, MpSerializer *serializer); + +void +math_history_entry_redisplay(MathHistoryEntry *history_entry, MpSerializer *serializer); gchar * math_history_entry_get_equation(MathHistoryEntry *history_entry); diff --git a/src/math-history.c b/src/math-history.c index 403a04f..55bad21 100644 --- a/src/math-history.c +++ b/src/math-history.c @@ -20,7 +20,7 @@ struct MathHistoryPrivate { MathEquation *equation; - gchar *last_answer; + MpSerializer *serializer; gchar *last_equation; int current; /* 0 is the first entry, rows-1 the most recent entry */ int rows; @@ -52,42 +52,27 @@ scroll_bottom_cb(MathHistory *history, gpointer data) } void -math_history_insert_entry (MathHistory *history, char *equation, MPNumber *answer, int number_base) +math_history_insert_entry (MathHistory *history, char *equation, MPNumber *answer) { - MathHistoryEntry *entry = math_history_entry_new(history->priv->equation); - - MpSerializer *serializer_four = mp_serializer_new(MP_DISPLAY_FORMAT_AUTOMATIC, number_base, 4); - MpSerializer *serializer_nine = mp_serializer_new(MP_DISPLAY_FORMAT_AUTOMATIC, number_base, 9); - - gchar *answer_four_digits = mp_serializer_to_string(serializer_four, answer); - gchar *answer_nine_digits = mp_serializer_to_string(serializer_nine, answer); - - if (g_strcmp0(history->priv->last_answer, answer_nine_digits) == 0 && - g_strcmp0(history->priv->last_equation, equation) == 0 && - history->priv->rows >= 1) + if (g_strcmp0(history->priv->last_equation, equation) == 0 && history->priv->rows >= 1) { - g_free(answer_four_digits); - g_free(answer_nine_digits); return; } - math_history_entry_insert_entry(entry, equation, answer_four_digits, answer_nine_digits); + MathHistoryEntry *entry = math_history_entry_new(history->priv->equation); + + math_history_entry_insert_entry(entry, equation, answer, history->priv->serializer); gtk_list_box_insert(GTK_LIST_BOX(history->priv->listbox), GTK_WIDGET(entry), -1); gtk_widget_set_can_focus(GTK_WIDGET(entry), FALSE); gtk_widget_show(GTK_WIDGET(entry)); - g_free(history->priv->last_answer); g_free(history->priv->last_equation); - 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); - g_free(answer_nine_digits); } void @@ -123,6 +108,18 @@ math_history_get_current(MathHistory *history) return history->priv->current; } +void +math_history_set_serializer(MathHistory *history, MpSerializer *serializer) +{ + history->priv->serializer = serializer; + + GList *children, *iter; + children = gtk_container_get_children(GTK_CONTAINER(history->priv->listbox)); + for (iter = children; iter != NULL; iter = g_list_next(iter)) + math_history_entry_redisplay(MATH_HISTORY_ENTRY(iter->data), serializer); + g_list_free(children); +} + static void math_history_class_init(MathHistoryClass *klass) { @@ -147,9 +144,9 @@ 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->serializer = NULL; history->priv->current = 0; history->priv->rows = 0; - history->priv->last_answer = g_strdup(""); history->priv->last_equation = g_strdup(""); history->priv->listbox = gtk_list_box_new(); diff --git a/src/math-history.h b/src/math-history.h index 87dab7a..dc9349e 100644 --- a/src/math-history.h +++ b/src/math-history.h @@ -43,7 +43,7 @@ MathHistory * math_history_new(MathEquation *equation); void -math_history_insert_entry(MathHistory *history, char *equation, MPNumber *answer, int number_base); +math_history_insert_entry(MathHistory *history, char *equation, MPNumber *answer); gpointer math_history_get_entry_at(MathHistory *history, int index); @@ -57,6 +57,9 @@ math_history_get_current(MathHistory *history); void math_history_clear(MathHistory *history); +void +math_history_set_serializer(MathHistory *history, MpSerializer *serializer); + G_END_DECLS #endif /* MATH_HISTORY_VIEW_H */ diff --git a/src/math-window.c b/src/math-window.c index be4f02e..8d6f79d 100644 --- a/src/math-window.c +++ b/src/math-window.c @@ -444,10 +444,17 @@ static GtkWidget *add_menu(GtkWidget *menu_bar, const gchar *name) } static void -update_history_cb (MathEquation *equation, char *answer, MPNumber *number, int number_base, gpointer data) +update_history_cb (MathEquation *equation, char *equation_string, MPNumber *answer, MathWindow *window) { /* Recieves signal emitted by a MathEquation object for updating history */ - MathWindow *window = MATH_WINDOW(data); - math_history_insert_entry (window->priv->history, answer, number, number_base); /* Sends current equation and answer for updating History-View */ + math_history_insert_entry (window->priv->history, equation_string, answer); /* Sends current equation string and answer for updating History-View */ +} + +static void +history_set_serializer_cb(MathEquation *equation, MathWindow *window) +{ + MpSerializer *serializer = math_equation_get_serializer(equation); + + math_history_set_serializer(window->priv->history, serializer); } static void quit_cb(GtkWidget* widget, MathWindow* window) @@ -599,6 +606,8 @@ create_gui(MathWindow *window) g_signal_connect(window->priv->equation, "history", G_CALLBACK(update_history_cb), window); g_signal_connect(window, "notify::show-history", G_CALLBACK(show_history_cb), NULL); show_history_cb(window, NULL); + g_signal_connect(window->priv->equation, "display-changed", G_CALLBACK(history_set_serializer_cb), window); + history_set_serializer_cb(window->priv->equation, window); gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(window->priv->history), TRUE, TRUE, 0); scrolled_window = gtk_scrolled_window_new(NULL, NULL); -- cgit v1.2.1