Shows a preview of the graphics settings
[crack-attack.git] / src / gtk-gui / callbacks.cxx
blob3d2c36350dfc4d54f59158cf383db9c2c03d86e9
1 /*
2 * callbacks.cxx
3 * Andrew Sayman - 10/11/04
5 * Copyright (C) 2000 Daniel Nelson
6 * Copyright (C) 2004 Andrew Sayman
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * Daniel Nelson - aluminumangel.org
23 * 174 W. 18th Ave.
24 * Columbus, OH 43210
27 #ifdef HAVE_CONFIG_H
28 # include <config.h>
29 #endif
31 #include <gtk/gtk.h>
32 #include <glib.h>
33 #include "../Mode.h"
34 #include "../Attack.h"
35 #include "callbacks.h"
36 #include "interface.h"
37 #include "support.h"
38 #include "prefs.h"
39 #include "persist.h"
40 #include "modeparser.h"
42 static GtkWidget *fraClient, *fraSingle, *fraServer;
43 static GtkWindow *window = NULL;
44 static int mode = CM_SOLO;
45 static gboolean MS_RUNNING = FALSE;
46 static pid_t running_process = 0;
48 extern int glut_argc;
49 extern char **glut_argv;
51 void turn_game_prefs_off () {
52 GAME_SINGLE = GAME_SERVER = GAME_CLIENT = GAME_EXTREME = FALSE;
55 void lookup_widgets(GtkToggleButton * gtb) {
56 fraClient = lookup_widget(GTK_WIDGET(gtb), "fraClient");
57 fraSingle = lookup_widget(GTK_WIDGET(gtb), "fraSingle");
58 fraServer = lookup_widget(GTK_WIDGET(gtb), "fraServer");
61 void turn_sensitive_off() {
62 gtk_widget_set_sensitive (fraClient, FALSE);
63 gtk_widget_set_sensitive (fraSingle, FALSE);
64 gtk_widget_set_sensitive (fraServer, FALSE);
67 void toggle_sensitive (GtkToggleButton* gtb, gint selector) {
68 gtk_widget_set_sensitive (fraClient, FALSE);
69 gtk_widget_set_sensitive (fraSingle, FALSE);
70 gtk_widget_set_sensitive (fraServer, FALSE);
71 switch (selector) {
72 case 1: gtk_widget_set_sensitive (fraClient, TRUE);
73 break;
74 case 2: gtk_widget_set_sensitive (fraServer, TRUE);
75 break;
79 void prepare_for_actions (GtkToggleButton *gtb) {
80 lookup_widgets(gtb);
81 turn_game_prefs_off();
82 turn_sensitive_off();
85 static void
86 game_end (GPid pid, gint status, gpointer data) {
87 #ifdef DEVELOPMENT
88 g_print("game_end called!\n");
89 #endif
90 MS_RUNNING = FALSE;
91 running_process = 0;
92 if (window) {
93 gtk_widget_show(GTK_WIDGET(window));
95 g_spawn_close_pid (pid);
98 void
99 ca_error_dialog (const char *message)
101 GtkWidget *dialog = NULL;
102 dialog = gtk_message_dialog_new (window,
103 GTK_DIALOG_DESTROY_WITH_PARENT,
104 GTK_MESSAGE_ERROR,
105 GTK_BUTTONS_CLOSE,
106 message);
107 gtk_dialog_run (GTK_DIALOG (dialog));
108 gtk_widget_destroy (dialog);
111 void
112 on_btnStart_clicked (GtkButton *button,
113 gpointer user_data)
115 window =
116 GTK_WINDOW(lookup_widget(GTK_WIDGET(button),"winCrackAttackSplash"));
117 if (MS_RUNNING) {
118 //Game is running, display an error message to the user
119 ca_error_dialog("Error: crack-attack is already running");
120 return;
123 // Save the gui data when the game starts.
124 gui_data_save(GTK_WIDGET(button));
126 // Set the mode based on all the widget values...
127 mode = generate_mode(GTK_WIDGET(button));
129 #ifdef DEVELOPMENT
130 g_print("Looking for location: %s\n", GC_BINARY_LOCATION);
131 #endif
132 gtk_widget_hide(GTK_WIDGET(window));
133 GError *err = NULL;
134 GPid pid;
135 gtk_widget_hide(GTK_WIDGET(window));
136 GSpawnFlags flags = (GSpawnFlags) (G_SPAWN_LEAVE_DESCRIPTORS_OPEN |
137 G_SPAWN_DO_NOT_REAP_CHILD);
139 gchar **args =
140 generate_array(mode, GC_BINARY_LOCATION, GTK_WIDGET(button));
142 gboolean ret =
143 g_spawn_async(NULL, args, NULL, flags, NULL, NULL, &pid, &err);
145 MS_RUNNING = TRUE;
146 g_child_watch_add(pid, (GChildWatchFunc) game_end, NULL);
147 g_free(args);
148 if (!ret) {
149 if (err) ca_error_dialog(err->message);
153 gboolean
154 on_winCrackAttackSplash_delete_event (GtkWindow *window,
155 gpointer user_data)
157 return FALSE;
160 void
161 on_cmbQuality_changed (GtkComboBox *cmb,
162 gpointer user_data)
164 gint tmp = gtk_combo_box_get_active(cmb);
165 GtkImage * imgLogo = GTK_IMAGE(lookup_widget(GTK_WIDGET(cmb), "imgLogo"));
166 gchar *filename = NULL;
167 switch (tmp) {
168 case 0: {
169 filename = find_pixmap_file("preview_normal.tga");
170 break;
172 case 1: {
173 filename = find_pixmap_file("preview_reduced.tga");
174 break;
176 case 2: {
177 filename = find_pixmap_file("preview_extremely_reduced.tga");
178 break;
180 default: {
181 filename = find_pixmap_file("preview_normal.tga");
184 gtk_image_set_from_file(imgLogo, filename);