summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohannes Unruh <[email protected]>2020-03-23 00:03:52 +0100
committerRobert Antoni Buj Gelonch <[email protected]>2020-04-07 23:27:35 +0200
commit7f43d244e969162ca92d6575085b4196f0c2e19f (patch)
treefffc9391bbe294ea501bd829fd57cba977d5abcf /src
parentd92c6f685a8622905d9ca50479cf19454a5959f5 (diff)
downloadmate-calc-7f43d244e969162ca92d6575085b4196f0c2e19f.tar.bz2
mate-calc-7f43d244e969162ca92d6575085b4196f0c2e19f.tar.xz
history-view: simplify code and add clear option
Diffstat (limited to 'src')
-rw-r--r--src/math-history-entry.c74
-rw-r--r--src/math-history-entry.h2
-rw-r--r--src/math-history.c80
-rw-r--r--src/math-history.h3
-rw-r--r--src/math-window.c7
5 files changed, 84 insertions, 82 deletions
diff --git a/src/math-history-entry.c b/src/math-history-entry.c
index 6948eac..0d8940a 100644
--- a/src/math-history-entry.c
+++ b/src/math-history-entry.c
@@ -20,13 +20,8 @@ struct MathHistoryEntryPrivate
{
MathEquation *equation;
- MPNumber *answer; /* Stores answer in MPNumber object */
- char *equation_string; /* Stores equation to be entered in history-entry */
- char *answer_string; /* Stores answer to be entered in history-entry */
GtkWidget *equation_label;
GtkWidget *answer_label;
- GtkWidget *equation_eventbox;
- GtkWidget *answer_eventbox;
};
G_DEFINE_TYPE_WITH_PRIVATE (MathHistoryEntry, math_history_entry, GTK_TYPE_LIST_BOX_ROW);
@@ -43,58 +38,35 @@ math_history_entry_new(MathEquation *equation)
return history_entry;
}
-void answer_clicked_cb(GtkWidget *widget, GdkEventButton *eventbutton, gpointer data);
+void answer_clicked_cb(GtkWidget *widget, GdkEventButton *eventbutton, MathHistoryEntry *history_entry);
G_MODULE_EXPORT
void
-answer_clicked_cb (GtkWidget *widget, GdkEventButton *eventbutton, gpointer data)
-{ /* Callback function for button-press-event on answer_eventbox */
- GtkEventBox *event = (GtkEventBox*) widget;
- MathHistoryEntry *history_entry = MATH_HISTORY_ENTRY(data);
- if (event != NULL)
- {
- if (GTK_IS_BIN(event))
- {
- GtkWidget *answer_label = gtk_bin_get_child(GTK_BIN(event));
- char *answer = gtk_widget_get_tooltip_text(answer_label);
- if (answer != NULL)
- math_equation_insert(history_entry->priv->equation, answer);
- }
- }
+answer_clicked_cb (GtkWidget *widget, GdkEventButton *eventbutton, MathHistoryEntry *history_entry)
+{
+ const gchar *answer = gtk_widget_get_tooltip_text(history_entry->priv->answer_label);
+
+ if (answer != NULL)
+ math_equation_insert(history_entry->priv->equation, answer);
}
-void equation_clicked_cb(GtkWidget *widget, GdkEventButton *eventbutton, gpointer data);
+
+void equation_clicked_cb(GtkWidget *widget, GdkEventButton *eventbutton, MathHistoryEntry *history_entry);
G_MODULE_EXPORT
void
-equation_clicked_cb (GtkWidget *widget, GdkEventButton *eventbutton, gpointer data)
-{ /* Callback function for button-press-event on equation_eventbox */
- GtkEventBox *event = (GtkEventBox*) widget;
- MathHistoryEntry *history_entry = MATH_HISTORY_ENTRY(data);
- if (event != NULL)
- {
- if (GTK_IS_BIN(event))
- {
- GtkLabel *equation_label = (GtkLabel*) gtk_bin_get_child(GTK_BIN(event));
- const char *equation = gtk_label_get_text(equation_label);
- if (equation != NULL)
- math_equation_set(history_entry->priv->equation, equation);
- }
- }
+equation_clicked_cb (GtkWidget *widget, GdkEventButton *eventbutton, MathHistoryEntry *history_entry)
+{
+ const gchar *equation = gtk_label_get_text(GTK_LABEL(history_entry->priv->equation_label));
+
+ if (equation != NULL)
+ math_equation_set(history_entry->priv->equation, equation);
}
void
-math_history_entry_insert_entry(MathHistoryEntry *history_entry, char *equation, MPNumber *number, int number_base)
+math_history_entry_insert_entry(MathHistoryEntry *history_entry, const gchar *equation, const gchar *answer_four_digits, const gchar *answer_nine_digits)
{
GtkBuilder *builder = NULL;
GtkWidget *grid;
GError *error = NULL;
- char *final_answer;
- MpSerializer *serializer_nine = mp_serializer_new(MP_DISPLAY_FORMAT_AUTOMATIC, number_base, 9);
- MpSerializer *serializer_four = mp_serializer_new(MP_DISPLAY_FORMAT_AUTOMATIC, number_base, 4);
- history_entry->priv->answer = number;
- history_entry->priv->equation_string = equation;
- history_entry->priv->answer_string = mp_serializer_to_string(serializer_nine, history_entry->priv->answer);
- final_answer = mp_serializer_to_string(serializer_four, history_entry->priv->answer);
-
builder = gtk_builder_new();
if(gtk_builder_add_from_resource (builder, UI_HISTORY_ENTRY_RESOURCE_PATH, &error) == 0)
{
@@ -106,14 +78,12 @@ math_history_entry_insert_entry(MathHistoryEntry *history_entry, char *equation,
gtk_container_add(GTK_CONTAINER(history_entry), grid);
history_entry->priv->equation_label = GET_WIDGET(builder, "equation_label");
history_entry->priv->answer_label = GET_WIDGET(builder, "answer_label");
- history_entry->priv->equation_eventbox = GET_WIDGET(builder, "equation_eventbox");
- history_entry->priv->answer_eventbox = GET_WIDGET(builder, "answer_eventbox");
- gtk_widget_set_tooltip_text(history_entry->priv->equation_label, history_entry->priv->equation_string);
- gtk_widget_set_tooltip_text(history_entry->priv->answer_label, history_entry->priv->answer_string);
- gtk_label_set_text(GTK_LABEL(history_entry->priv->equation_label), history_entry->priv->equation_string);
- gtk_label_set_text(GTK_LABEL(history_entry->priv->answer_label), final_answer);
+ 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_free(final_answer);
+ g_object_unref(builder);
}
static void
@@ -125,6 +95,4 @@ static void
math_history_entry_init(MathHistoryEntry *history_entry)
{
history_entry->priv = math_history_entry_get_instance_private(history_entry);
- history_entry->priv->equation_string = "";
- history_entry->priv->answer_string = "";
}
diff --git a/src/math-history-entry.h b/src/math-history-entry.h
index 512b551..53ccc88 100644
--- a/src/math-history-entry.h
+++ b/src/math-history-entry.h
@@ -42,7 +42,7 @@ MathHistoryEntry *
math_history_entry_new(MathEquation *equation);
void
-math_history_entry_insert_entry(MathHistoryEntry *history_entry, char *equation, MPNumber *number, int number_base);
+math_history_entry_insert_entry(MathHistoryEntry *history_entry, const gchar *equation, const gchar *answer_four_digits, const gchar *answer_nine_digits);
G_END_DECLS
diff --git a/src/math-history.c b/src/math-history.c
index e350ef8..49d63a2 100644
--- a/src/math-history.c
+++ b/src/math-history.c
@@ -20,9 +20,9 @@ struct MathHistoryPrivate
{
MathEquation *equation;
- MathHistoryEntry *entry;
- char *prev_equation;
- int items_count; /* Number of entries in history listbox */
+ gchar *last_answer;
+ gchar *last_equation;
+ int rows;
GtkWidget *listbox;
};
@@ -50,33 +50,54 @@ scroll_bottom_cb(MathHistory *history, gpointer data)
- gtk_adjustment_get_page_size (adjustment));
}
-static gboolean
-check_history(MathHistory *history, char *equation)
-{ /* Checks if the last inserted calculation is the same as the current calculation to be inserted in history */
- if (history->priv->items_count >= 1 && g_strcmp0(equation, history->priv->prev_equation)==0)
- return TRUE; /* returns true if last entered equation is the same as the current equation */
- else
- return FALSE;
-}
-
void
math_history_insert_entry (MathHistory *history, char *equation, MPNumber *answer, int number_base)
-{ /* Inserts a new entry into the history listbox */
- history->priv->entry = math_history_entry_new(history->priv->equation);
- gboolean check = check_history (history, equation);
- history->priv->prev_equation = g_strdup(equation);
- if (!check)
+{
+ 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)
{
- math_history_entry_insert_entry(history->priv->entry, equation, answer, number_base);
- if (history->priv->entry != NULL)
- {
- gtk_list_box_insert(GTK_LIST_BOX(history->priv->listbox), GTK_WIDGET(history->priv->entry), -1);
- gtk_widget_set_can_focus(GTK_WIDGET(history->priv->entry), FALSE);
- gtk_widget_show_all(GTK_WIDGET(history->priv->entry));
- history->priv->items_count++;
- }
+ g_free(answer_four_digits);
+ g_free(answer_nine_digits);
+ return;
}
+
+ math_history_entry_insert_entry(entry, equation, answer_four_digits, answer_nine_digits);
+
+ 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++;
g_signal_emit_by_name(history, "row-added");
+
+ g_free(answer_four_digits);
+ g_free(answer_nine_digits);
+}
+
+void
+math_history_clear(MathHistory *history)
+{
+ GList *children, *iter;
+
+ history->priv->rows = 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);
}
static void
@@ -98,13 +119,16 @@ static void
math_history_init(MathHistory *history)
{
history->priv = math_history_get_instance_private(history);
+
gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(history), GTK_SHADOW_IN);
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->items_count = 0;
- history->priv->prev_equation = "";
- history->priv->listbox = gtk_list_box_new();
+ history->priv->rows = 0;
+ history->priv->last_answer = g_strdup("");
+ history->priv->last_equation = g_strdup("");
+ history->priv->listbox = gtk_list_box_new();
+
gtk_list_box_set_selection_mode(GTK_LIST_BOX(history->priv->listbox), GTK_SELECTION_NONE);
gtk_widget_show(GTK_WIDGET(history->priv->listbox));
diff --git a/src/math-history.h b/src/math-history.h
index 6301dd6..7c7b079 100644
--- a/src/math-history.h
+++ b/src/math-history.h
@@ -45,6 +45,9 @@ math_history_new(MathEquation *equation);
void
math_history_insert_entry(MathHistory *history, char *equation, MPNumber *answer, int number_base);
+void
+math_history_clear(MathHistory *history);
+
G_END_DECLS
#endif /* MATH_HISTORY_VIEW_H */
diff --git a/src/math-window.c b/src/math-window.c
index 87ac779..08ac3e3 100644
--- a/src/math-window.c
+++ b/src/math-window.c
@@ -180,6 +180,11 @@ static void show_history_cb(MathWindow *window, GParamSpec *spec)
g_settings_set_boolean(g_settings_var, "show-history", math_window_get_show_history(window));
}
+static void clear_history_cb(GtkMenuItem *menu, MathWindow *window)
+{
+ math_history_clear(window->priv->history);
+}
+
static void show_preferences_cb(GtkMenuItem *menu, MathWindow *window)
{
if (!window->priv->preferences_dialog)
@@ -514,6 +519,8 @@ static void create_menu(MathWindow* window)
gtk_widget_add_accelerator(menu_item, "activate", accel_group, GDK_KEY_Z, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
menu_item = add_menu_item(menu, gtk_image_menu_item_new_from_icon("edit-redo",_("_Redo"), accel_group), G_CALLBACK(redo_cb), window);
gtk_widget_add_accelerator(menu_item, "activate", accel_group, GDK_KEY_Z, GDK_CONTROL_MASK | GDK_SHIFT_MASK, GTK_ACCEL_VISIBLE);
+ menu_item = add_menu_item(menu, gtk_image_menu_item_new_from_icon("edit-clear",_("_Clear History"), accel_group), G_CALLBACK(clear_history_cb), window);
+ gtk_widget_add_accelerator(menu_item, "activate", accel_group, GDK_KEY_Delete, GDK_CONTROL_MASK | GDK_SHIFT_MASK, GTK_ACCEL_VISIBLE);
add_menu_item(menu, gtk_separator_menu_item_new(), NULL, NULL);
add_menu_item(menu, gtk_image_menu_item_new_from_icon("preferences-desktop",_("_Preferences"), accel_group), G_CALLBACK(show_preferences_cb), window);
add_menu_item(menu, gtk_separator_menu_item_new(), NULL, NULL);