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 guint tictactoe_signals[LAST_SIGNAL] = { 0 };
37
tictactoe_get_type (void)
39
static GType ttt_type = 0;
43
static const GTypeInfo ttt_info =
45
sizeof (TictactoeClass),
47
NULL, /* base_finalize */
48
(GClassInitFunc) tictactoe_class_init,
49
NULL, /* class_finalize */
50
NULL, /* class_data */
53
(GInstanceInitFunc) tictactoe_init,
56
ttt_type = g_type_register_static (GTK_TYPE_TABLE, "Tictactoe", &ttt_info, 0);
63
tictactoe_class_init (TictactoeClass *klass)
66
tictactoe_signals[TICTACTOE_SIGNAL] = g_signal_new ("tictactoe",
67
G_TYPE_FROM_CLASS (klass),
68
G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
69
G_STRUCT_OFFSET (TictactoeClass, tictactoe),
72
g_cclosure_marshal_VOID__VOID,
79
tictactoe_init (Tictactoe *ttt)
83
gtk_table_resize (GTK_TABLE (ttt), 3, 3);
84
gtk_table_set_homogeneous (GTK_TABLE (ttt), TRUE);
88
ttt->buttons[i][j] = gtk_toggle_button_new ();
89
gtk_table_attach_defaults (GTK_TABLE (ttt), ttt->buttons[i][j],
91
g_signal_connect (G_OBJECT (ttt->buttons[i][j]), "toggled",
92
G_CALLBACK (tictactoe_toggle), (gpointer) ttt);
93
gtk_widget_set_size_request (ttt->buttons[i][j], 20, 20);
94
gtk_widget_show (ttt->buttons[i][j]);
101
return GTK_WIDGET (g_object_new (tictactoe_get_type (), NULL));
105
tictactoe_clear (Tictactoe *ttt)
109
for (i = 0; i<3; i++)
110
for (j = 0; j<3; j++)
112
g_signal_handlers_block_matched (G_OBJECT (ttt->buttons[i][j]),
114
0, 0, NULL, NULL, ttt);
115
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ttt->buttons[i][j]),
117
g_signal_handlers_unblock_matched (G_OBJECT (ttt->buttons[i][j]),
119
0, 0, NULL, NULL, ttt);
124
tictactoe_toggle (GtkWidget *widget, Tictactoe *ttt)
128
static int rwins[8][3] = { { 0, 0, 0 }, { 1, 1, 1 }, { 2, 2, 2 },
129
{ 0, 1, 2 }, { 0, 1, 2 }, { 0, 1, 2 },
130
{ 0, 1, 2 }, { 0, 1, 2 } };
131
static int cwins[8][3] = { { 0, 1, 2 }, { 0, 1, 2 }, { 0, 1, 2 },
132
{ 0, 0, 0 }, { 1, 1, 1 }, { 2, 2, 2 },
133
{ 0, 1, 2 }, { 2, 1, 0 } };
137
for (k = 0; k<8; k++)
142
for (i = 0; i<3; i++)
145
GTK_TOGGLE_BUTTON (ttt->buttons[rwins[k][i]][cwins[k][i]])->active;
147
ttt->buttons[rwins[k][i]][cwins[k][i]] == widget;
150
if (success && found)
152
g_signal_emit (G_OBJECT (ttt),
153
tictactoe_signals[TICTACTOE_SIGNAL], 0);