Copyright updates. I hope that I didn't miss anybody.
[crack-attack.git] / src / gtk-gui / persist.cxx
blob9b1a398501b81ffc59e20ab1f08886689bc69865
1 /*
2 * persist.cxx
3 * Kevin Webb - 3/21/05
5 * Copyright (C) 2005 Kevin Webb
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include <gtk/gtk.h>
22 #include <fstream>
23 #include <cstring>
25 #include "../TextureLoader.h"
26 #include "persist.h"
27 #include "callbacks.h"
28 #include "interface.h"
29 #include "support.h"
32 int
33 gui_get_dimensions (GtkButton *button)
36 GtkWidget *optResolutions;
37 int resolution;
39 optResolutions = lookup_widget(GTK_WIDGET(button), "optResolutions");
40 if (optResolutions) {
41 resolution = (int) gtk_option_menu_get_history((GtkOptionMenu *) optResolutions);
44 // These resolution values should proabably be constants somewhere, rather
45 // than hardcoding them in two places...
46 switch(resolution) {
47 case 1:
48 return GC_RESOLUTION_1;
49 case 2:
50 return GC_RESOLUTION_2;
51 case 3:
52 return GC_RESOLUTION_3;
53 case 4:
54 return GC_RESOLUTION_4;
55 default:
56 return GC_RESOLUTION_0;
61 void
62 gui_data_save (GtkButton *button)
65 char file_name[256];
66 GtkEntry *entPlayerName;
67 GtkWidget *optResolutions;
68 GtkWidget *cbtnLowGraphics;
69 GtkWidget *cbtnReallyLowGraphics;
70 gchar *tmp = NULL;
71 char player_name[GC_PLAYER_NAME_LENGTH];
72 int resolution;
73 gboolean low;
74 gboolean reallylow;
76 // Get the current name in the name text box.
77 entPlayerName = GTK_ENTRY(lookup_widget(GTK_WIDGET(button), "entPlayerName"));
78 if (entPlayerName) {
79 tmp = (gchar *) gtk_entry_get_text(entPlayerName);
80 if (tmp) {
81 if (strlen(tmp) == 0) {
82 gtk_entry_set_text(entPlayerName, g_get_user_name());
83 return;
85 g_strlcpy(player_name, tmp, 256);
89 // Get the currently selected resolution.
90 optResolutions = lookup_widget(GTK_WIDGET(button), "optResolutions");
91 if (optResolutions) {
92 resolution = (int) gtk_option_menu_get_history((GtkOptionMenu *) optResolutions);
95 // Get the status of the low and really low graphics check boxes.
96 cbtnLowGraphics = lookup_widget(GTK_WIDGET(button), "cbtnLowGraphics");
97 cbtnReallyLowGraphics = lookup_widget(GTK_WIDGET(button), "cbtnReallyLowGraphics");
98 if (cbtnLowGraphics && cbtnReallyLowGraphics) {
99 low = gtk_toggle_button_get_active((GtkToggleButton *) cbtnLowGraphics);
100 reallylow = gtk_toggle_button_get_active((GtkToggleButton *) cbtnReallyLowGraphics);
103 // Generate the file name and write the data,
104 TextureLoader::buildLocalDataFileName(GC_GUI_FILE_NAME, file_name);
106 ofstream file(file_name);
107 if (file.fail()) {
108 cerr << "Unable to write gui data to file!\n";
109 return;
112 file << player_name << endl;
113 file << resolution << endl;
114 file << low << endl;
115 file << reallylow << endl;
117 file.close();
121 void
122 gui_data_read (GtkWidget *widget)
125 char buffer[256];
126 char file_name[256];
127 GtkEntry *entPlayerName;
128 GtkWidget *optResolutions;
129 GtkWidget *cbtnLowGraphics;
130 GtkWidget *cbtnReallyLowGraphics;
131 char player_name[GC_PLAYER_NAME_LENGTH];
132 int resolution;
133 gboolean low;
134 gboolean reallylow;
136 // Generate the name of the file and read the data.
137 TextureLoader::buildLocalDataFileName(GC_GUI_FILE_NAME, file_name);
139 ifstream file(file_name);
140 if (file.fail()) {
141 #ifdef DEVELOPMENT
142 cerr << "Unable to find gui data file!\n";
143 #endif
144 return;
147 if (!file.eof()) {
148 file.getline(buffer, 256);
149 strcpy(player_name, buffer);
151 file.getline(buffer, 256);
152 resolution = atoi(buffer);
154 if (resolution < 0 || resolution > 4) {
155 resolution = 0;
158 file.getline(buffer, 256);
159 low = atoi(buffer);
161 file.getline(buffer, 256);
162 reallylow = atoi(buffer);
164 } else {
165 file.close();
166 return;
168 file.close();
170 // Set the name.
171 entPlayerName = GTK_ENTRY(lookup_widget(GTK_WIDGET(widget), "entPlayerName"));
172 if (entPlayerName) {
173 gtk_entry_set_text (GTK_ENTRY (entPlayerName), player_name);
176 /* Set the resolution in the option box. This does not actually change the
177 * value of the height and width. That is done with the gui_get_dimensions
178 * function before the game is launched...
180 optResolutions = lookup_widget(GTK_WIDGET(widget), "optResolutions");
181 if (optResolutions) {
182 gtk_option_menu_set_history((GtkOptionMenu *) optResolutions, (guint) resolution);
185 // Set the low and really low graphics check boxes.
186 cbtnLowGraphics = lookup_widget(GTK_WIDGET(widget), "cbtnLowGraphics");
187 cbtnReallyLowGraphics = lookup_widget(GTK_WIDGET(widget), "cbtnReallyLowGraphics");
188 if (cbtnLowGraphics && cbtnReallyLowGraphics) {
189 gtk_toggle_button_set_active((GtkToggleButton *) cbtnLowGraphics, low);
190 gtk_toggle_button_set_active((GtkToggleButton *) cbtnReallyLowGraphics, reallylow);