1 /* example-start tictactoe tictactoe.c */
3 /* GTK - The GIMP Toolkit
4 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
21 #include "gtk/gtksignal.h"
22 #include "gtk/gtktable.h"
23 #include "gtk/gtktogglebutton.h"
24 #include "tictactoe.h"
31 static void tictactoe_class_init (TictactoeClass *klass);
32 static void tictactoe_init (Tictactoe *ttt);
33 static void tictactoe_toggle (GtkWidget *widget, Tictactoe *ttt);
35 static gint tictactoe_signals[LAST_SIGNAL] = { 0 };
40 static guint ttt_type = 0;
44 GtkTypeInfo ttt_info =
48 sizeof (TictactoeClass),
49 (GtkClassInitFunc) tictactoe_class_init,
50 (GtkObjectInitFunc) tictactoe_init,
55 ttt_type = gtk_type_unique (gtk_vbox_get_type (), &ttt_info);
62 tictactoe_class_init (TictactoeClass *class)
64 GtkObjectClass *object_class;
66 object_class = (GtkObjectClass*) class;
68 tictactoe_signals[TICTACTOE_SIGNAL] = gtk_signal_new ("tictactoe",
71 GTK_SIGNAL_OFFSET (TictactoeClass, tictactoe),
72 gtk_signal_default_marshaller, GTK_TYPE_NONE, 0);
75 gtk_object_class_add_signals (object_class, tictactoe_signals, LAST_SIGNAL);
77 class->tictactoe = NULL;
81 tictactoe_init (Tictactoe *ttt)
86 table = gtk_table_new (3, 3, TRUE);
87 gtk_container_add (GTK_CONTAINER(ttt), table);
88 gtk_widget_show (table);
93 ttt->buttons[i][j] = gtk_toggle_button_new ();
94 gtk_table_attach_defaults (GTK_TABLE(table), ttt->buttons[i][j],
96 gtk_signal_connect (GTK_OBJECT (ttt->buttons[i][j]), "toggled",
97 GTK_SIGNAL_FUNC (tictactoe_toggle), ttt);
98 gtk_widget_set_usize (ttt->buttons[i][j], 20, 20);
99 gtk_widget_show (ttt->buttons[i][j]);
106 return GTK_WIDGET ( gtk_type_new (tictactoe_get_type ()));
110 tictactoe_clear (Tictactoe *ttt)
117 gtk_signal_handler_block_by_data (GTK_OBJECT(ttt->buttons[i][j]), ttt);
118 gtk_toggle_button_set_state (GTK_TOGGLE_BUTTON (ttt->buttons[i][j]),
120 gtk_signal_handler_unblock_by_data (GTK_OBJECT(ttt->buttons[i][j]), ttt);
125 tictactoe_toggle (GtkWidget *widget, Tictactoe *ttt)
129 static int rwins[8][3] = { { 0, 0, 0 }, { 1, 1, 1 }, { 2, 2, 2 },
130 { 0, 1, 2 }, { 0, 1, 2 }, { 0, 1, 2 },
131 { 0, 1, 2 }, { 0, 1, 2 } };
132 static int cwins[8][3] = { { 0, 1, 2 }, { 0, 1, 2 }, { 0, 1, 2 },
133 { 0, 0, 0 }, { 1, 1, 1 }, { 2, 2, 2 },
134 { 0, 1, 2 }, { 2, 1, 0 } };
146 GTK_TOGGLE_BUTTON(ttt->buttons[rwins[k][i]][cwins[k][i]])->active;
148 ttt->buttons[rwins[k][i]][cwins[k][i]] == widget;
151 if (success && found)
153 gtk_signal_emit (GTK_OBJECT (ttt),
154 tictactoe_signals[TICTACTOE_SIGNAL]);