2 /* GTK - The GIMP Toolkit
3 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 #include "gtk/gtksignal.h"
21 #include "gtk/gtktable.h"
22 #include "gtk/gtktogglebutton.h"
23 #include "tictactoe.h"
30 static void tictactoe_class_init (TictactoeClass *klass);
31 static void tictactoe_init (Tictactoe *ttt);
32 static void tictactoe_toggle (GtkWidget *widget, Tictactoe *ttt);
34 static gint tictactoe_signals[LAST_SIGNAL] = { 0 };
39 static GType ttt_type = 0;
43 static const GTypeInfo ttt_info =
45 sizeof (TictactoeClass),
48 (GClassInitFunc) tictactoe_class_init,
53 (GInstanceInitFunc) tictactoe_init,
56 ttt_type = g_type_register_static (GTK_TYPE_VBOX, "Tictactoe", &ttt_info, 0);
63 tictactoe_class_init (TictactoeClass *class)
65 GtkObjectClass *object_class;
67 object_class = (GtkObjectClass*) class;
69 tictactoe_signals[TICTACTOE_SIGNAL] = g_signal_new ("tictactoe",
70 G_TYPE_FROM_CLASS (object_class),
75 g_cclosure_marshal_VOID__VOID,
76 G_TYPE_NONE, 0, NULL);
79 class->tictactoe = NULL;
83 tictactoe_init (Tictactoe *ttt)
88 table = gtk_table_new (3, 3, TRUE);
89 gtk_container_add (GTK_CONTAINER (ttt), table);
90 gtk_widget_show (table);
92 for (i = 0; i < 3; i++)
93 for (j = 0; j < 3; j++)
95 ttt->buttons[i][j] = gtk_toggle_button_new ();
96 gtk_table_attach_defaults (GTK_TABLE (table), ttt->buttons[i][j],
98 g_signal_connect (G_OBJECT (ttt->buttons[i][j]), "toggled",
99 G_CALLBACK (tictactoe_toggle), (gpointer) ttt);
100 gtk_widget_set_size_request (ttt->buttons[i][j], 20, 20);
101 gtk_widget_show (ttt->buttons[i][j]);
108 return GTK_WIDGET (g_object_new (tictactoe_get_type (), NULL));
112 tictactoe_clear (Tictactoe *ttt)
116 for (i = 0; i < 3; i++)
117 for (j = 0; j < 3; j++)
119 g_signal_handlers_block_by_func (G_OBJECT (ttt->buttons[i][j]),
121 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ttt->buttons[i][j]),
123 g_signal_handlers_unblock_by_func (G_OBJECT (ttt->buttons[i][j]),
129 tictactoe_toggle (GtkWidget *widget, Tictactoe *ttt)
133 static int rwins[8][3] = { { 0, 0, 0 }, { 1, 1, 1 }, { 2, 2, 2 },
134 { 0, 1, 2 }, { 0, 1, 2 }, { 0, 1, 2 },
135 { 0, 1, 2 }, { 0, 1, 2 } };
136 static int cwins[8][3] = { { 0, 1, 2 }, { 0, 1, 2 }, { 0, 1, 2 },
137 { 0, 0, 0 }, { 1, 1, 1 }, { 2, 2, 2 },
138 { 0, 1, 2 }, { 2, 1, 0 } };
142 for (k = 0; k < 8; k++)
147 for (i = 0; i < 3; i++)
150 GTK_TOGGLE_BUTTON (ttt->buttons[rwins[k][i]][cwins[k][i]])->active;
152 ttt->buttons[rwins[k][i]][cwins[k][i]] == widget;
155 if (success && found)
157 g_signal_emit (G_OBJECT (ttt),
158 tictactoe_signals[TICTACTOE_SIGNAL], 0);