Don't close the window when the user refused to quit.
[gliv.git] / src / history.c
blob524fa3ada0cff666b44f28d1e84992d2b8790f40
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 <guichaz@yahoo.fr>
21 /********************
22 * History handling *
23 ********************/
25 #include "gliv.h"
26 #include "history.h"
27 #include "options.h"
28 #include "rendering.h"
29 #include "matrix.h"
31 extern options_struct *options;
33 static GList *list = NULL;
34 static GList *node = NULL; /* Currrent position in the list. */
36 static gint count = 0; /* Number of elements in the list. */
38 /* Returns item->next. */
39 static GList *free_item(GList * item)
41 GList *next;
43 g_free(item->data);
44 next = item->next;
45 count--;
47 g_list_delete_link(list, item);
48 return next;
51 /* Free the elements from there to the end. */
52 static void free_list(GList * item)
54 while (item != NULL)
55 item = free_item(item);
58 /* Keep the list from being larger than options->history_size. */
59 void clean_history(void)
61 if (options->history_size < 0)
62 /* Unlimited history. */
63 return;
65 while (count > options->history_size) {
66 if (node == list)
67 node = node->next;
68 list = free_item(list);
71 if (list != NULL)
72 list->prev = NULL;
75 /* Add the current configuration to the end of the list. */
76 void append_history(void)
78 GList *item;
80 if (options->history_size == 0)
81 /* No history. */
82 return;
84 item = g_list_alloc();
86 /* Fill the data. */
87 item->data = g_new(gfloat, 8);
88 matrix_cpy(item->data, NULL);
90 /* We are at the end of the list. */
91 item->prev = node;
92 item->next = NULL;
94 if (list == NULL)
95 /* This is the first element. */
96 list = item;
97 else {
98 if (node->next != NULL)
99 /* We discard what was after us. */
100 free_list(node->next);
102 node->next = item;
105 node = item;
106 count++;
107 clean_history();
110 static void set_state(GList * item)
112 if (item != NULL && item->data != NULL) {
113 node = item;
114 matrix_cpy(NULL, item->data);
115 refresh(REFRESH_IMAGE | REFRESH_STATUS);
119 void undo(void)
121 set_state(g_list_previous(node));
124 void redo(void)
126 set_state(g_list_next(node));
129 /* Restart from scratch. */
130 void clear_history(void)
132 if (list != NULL) {
133 free_list(list);
134 list = node = NULL;
135 append_history();