gliv-1.7
[gliv.git] / src / history.c
blobd0f05c03215e4771ec015026a70ad66759ec15db
1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 * See the COPYING file for license information.
18 * Guillaume Chazarain <gfc@altern.org>
21 /*********************
22 * History handling. *
23 *********************/
25 #include "gliv.h"
27 extern options_struct *options;
29 static GList *list;
30 static GList *node; /* Currrent position in the list. */
32 static gint count; /* Number of elements in the list. */
33 static gboolean used; /* Whether the history has been initialized. */
35 void init_history(void)
37 if (options->history_size != 0) {
38 node = list = NULL;
39 count = 0;
40 used = TRUE;
41 } else
42 used = FALSE;
45 /* Returns item->next. */
46 static GList *free_item(GList * item)
48 GList *next;
50 g_free(item->data);
51 next = item->next;
52 count--;
54 g_list_delete_link(list, item);
55 return next;
58 static void free_list(GList * item)
60 while (item != NULL)
61 item = free_item(item);
64 static void clean_list(void)
66 while (count > options->history_size) {
67 if (node == list)
68 node = node->next;
69 list = free_item(list);
72 list->prev = NULL;
75 void append_history(void)
77 GList *item;
79 if (used == FALSE || options->history_size == 0)
80 return;
82 item = g_list_alloc();
83 item->data = g_new(gfloat, 8);
85 matrix_cpy(item->data, NULL);
87 item->prev = node;
88 item->next = NULL;
90 if (list == NULL)
91 list = item;
92 else {
93 if (node->next != NULL)
94 free_list(node->next);
96 node->next = item;
99 node = item;
100 count++;
102 if (options->history_size > 0)
103 clean_list();
106 static void set_state(GList * item)
108 if (item != NULL && item->data != NULL) {
109 node = item;
110 matrix_cpy(NULL, item->data);
111 refresh(REFRESH_IMAGE | REFRESH_STATUS);
115 void undo(void)
117 set_state(g_list_previous(node));
120 void redo(void)
122 set_state(g_list_next(node));
125 void clear_history(void)
127 if (list != NULL) {
128 free_list(list);
129 init_history();
130 append_history();