summaryrefslogtreecommitdiff
path: root/src/math-history-entry.c
blob: f3f0f5b23fa9e523e046cb3172ee4bb5e4b0b166 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * Copyright (C) 2020 MATE developers
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 2 of the License, or (at your option) any later
 * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
 * license.
 *
 *
 */

#include <string.h>
#include <glib/gi18n.h>
#include <gdk/gdkkeysyms.h>

#include "math-history-entry.h"

struct MathHistoryEntryPrivate
{
    MathEquation *equation;

    MPNumber *number;
    GtkWidget *equation_label;
    GtkWidget *answer_label;
};

G_DEFINE_TYPE_WITH_PRIVATE (MathHistoryEntry, math_history_entry, GTK_TYPE_LIST_BOX_ROW);

#define UI_HISTORY_ENTRY_RESOURCE_PATH "/org/mate/calculator/ui/history-entry.ui"

MathHistoryEntry *
math_history_entry_new(MathEquation *equation)
{
    MathHistoryEntry *history_entry = g_object_new(math_history_entry_get_type(), NULL);
    history_entry->priv->equation = g_object_ref(equation);
    return history_entry;
}

void answer_clicked_cb(GtkWidget *widget, GdkEventButton *eventbutton, MathHistoryEntry *history_entry);
G_MODULE_EXPORT
void
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, MathHistoryEntry *history_entry);
G_MODULE_EXPORT
void
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, const gchar *equation, MPNumber *answer, MpSerializer *serializer)
{
    #define get_widget(x) GTK_WIDGET(gtk_builder_get_object(builder, x))

    GtkBuilder *builder;
    GtkWidget *grid;

    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_label_set_text(GTK_LABEL(history_entry->priv->equation_label), equation);
    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 *
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();
}