summaryrefslogtreecommitdiff
path: root/gmathview/gmathview_mod_stack.c
blob: 7634e325711e8e5a9039e1cf08adf787dd9d43c9 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 * gMathView, mathematic expressions viewing & editing widget
 *
 *  # Modification stack source
 *
 * Author : Guillaume Seguin
 * Email : guillaume@segu.in
 *
 * Copyright (c) 2007 Guillaume Seguin <guillaume@segu.in>
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include <gmathview.h>
#include "gmathview_internal.h"
#include "gmathview_mod_stack.h"

static GMathMod*
gmathmod_new (GMathModType type);

static void
g_math_view_push_to_mod_stack (GMathView *mathview, GMathMod *mod);

static void
g_math_view_destroy_mod_stack (GList *stack);

void
g_math_view_undo (GMathView *mathview)
{
    GMathViewPrivate *priv;
    GMathMod         *mod;

    priv = G_MATH_VIEW_GET_PRIVATE (mathview);

    if (!priv->mod_stack || !priv->mod_stack->data)
        return;

    mod = (GMathMod *) priv->mod_stack->data;

    switch (mod->type)
    {
        case GMathModUpdateParent:
            ((GMathSymbol *) mod->data1)->parent = mod->data2;
            break;
        case GMathModUpdateValue:
            ((GMathSymbol *) mod->data1)->value = mod->data2;
            break;
        case GMathModUpdateRoot:
            priv->formula->root = ((GMathSymbol *) mod->data1);
            break;
    }

    priv->mod_stack = priv->mod_stack->next;
}

void
g_math_view_redo (GMathView *mathview)
{
    GMathViewPrivate *priv;
    GMathMod         *mod;

    priv = G_MATH_VIEW_GET_PRIVATE (mathview);

    if (!priv->mod_stack || !priv->mod_stack->prev)
        return;

    mod = (GMathMod *) priv->mod_stack->prev->data;

    if (!mod)
        return;

    switch (mod->type)
    {
        case GMathModUpdateParent:
            ((GMathSymbol *) mod->data1)->parent = mod->data3;
            break;
        case GMathModUpdateValue:
            ((GMathSymbol *) mod->data1)->value = mod->data3;
            break;
        case GMathModUpdateRoot:
            priv->formula->root = ((GMathSymbol *) mod->data2);
            break;
    }

    priv->mod_stack = priv->mod_stack->prev;
}

gboolean
g_math_view_can_undo (GMathView *mathview)
{
    GMathViewPrivate *priv;

    priv = G_MATH_VIEW_GET_PRIVATE (mathview);

    return (priv->mod_stack && priv->mod_stack->data);
}

gboolean
g_math_view_can_redo (GMathView *mathview)
{
    GMathViewPrivate *priv;

    priv = G_MATH_VIEW_GET_PRIVATE (mathview);

    return (priv->mod_stack && priv->mod_stack->prev);
}

void
g_math_view_update_parent (GMathView *mathview, GMathSymbol *symbol,
                           GMathSymbol *new_parent)
{
    GMathMod *mod;

    mod = gmathmod_new (GMathModUpdateParent);

    mod->data1 = (gpointer) symbol;
    mod->data2 = (gpointer) symbol->parent;
    mod->data3 = (gpointer) new_parent;

    symbol->parent = new_parent;

    g_math_view_push_to_mod_stack (mathview, mod);
}

void
g_math_view_update_value (GMathView *mathview, GMathSymbol *symbol,
                          gpointer new_value)
{
    GMathMod *mod;

    mod = gmathmod_new (GMathModUpdateParent);

    mod->data1 = (gpointer) symbol;
    mod->data2 = (gpointer) symbol->value;
    mod->data3 = (gpointer) new_value;

    symbol->value = new_value;

    g_math_view_push_to_mod_stack (mathview, mod);
}

void
g_math_view_update_root (GMathView *mathview, GMathSymbol *new_root)
{
    GMathMod *mod;
    GMathViewPrivate *priv;

    priv = G_MATH_VIEW_GET_PRIVATE (mathview);
    mod = gmathmod_new (GMathModUpdateRoot);

    mod->data1 = (gpointer) priv->formula->root;
    mod->data2 = (gpointer) new_root;

    priv->formula->root = new_root;

    g_math_view_push_to_mod_stack (mathview, mod);
}

static GMathMod*
gmathmod_new (GMathModType type)
{
    GMathMod *mod = g_new0 (GMathMod, 1);
    mod->type = type;
    return mod;
}

static void
g_math_view_push_to_mod_stack (GMathView *mathview, GMathMod *mod)
{
    GMathViewPrivate *priv;
    GList            *stack;

    priv = G_MATH_VIEW_GET_PRIVATE (mathview);

    if (!priv->mod_stack || !priv->mod_stack->prev)
    {
        priv->mod_stack = g_list_prepend (priv->mod_stack, mod);
        return;
    }

    stack = priv->mod_stack->prev;
    priv->mod_stack->prev = NULL;
    stack->next = NULL;
    priv->mod_stack = g_list_prepend (priv->mod_stack, mod);

    g_math_view_destroy_mod_stack (g_list_first (stack));
}

static void
g_math_view_destroy_mod_stack (GList *stack)
{
    if (!stack)
        return;

    /* FIXME: there's a high probability of mem leak here */

    stack = g_list_remove (stack, stack->data);

    g_math_view_destroy_mod_stack (stack);
}