1998-12-10 17:31:04 +00:00
|
|
|
|
1998-03-23 06:36:09 +00:00
|
|
|
/* GTK - The GIMP Toolkit
|
|
|
|
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
1998-04-13 02:02:47 +00:00
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
1998-03-23 06:36:09 +00:00
|
|
|
*/
|
|
|
|
#include "gtk/gtksignal.h"
|
|
|
|
#include "gtk/gtktable.h"
|
|
|
|
#include "gtk/gtktogglebutton.h"
|
|
|
|
#include "tictactoe.h"
|
|
|
|
|
|
|
|
enum {
|
|
|
|
TICTACTOE_SIGNAL,
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
|
|
|
static void tictactoe_class_init (TictactoeClass *klass);
|
|
|
|
static void tictactoe_init (Tictactoe *ttt);
|
|
|
|
static void tictactoe_toggle (GtkWidget *widget, Tictactoe *ttt);
|
|
|
|
|
|
|
|
static gint tictactoe_signals[LAST_SIGNAL] = { 0 };
|
|
|
|
|
2002-02-16 23:52:30 +00:00
|
|
|
GType
|
1998-03-23 06:36:09 +00:00
|
|
|
tictactoe_get_type ()
|
|
|
|
{
|
2002-02-16 23:52:30 +00:00
|
|
|
static GType ttt_type = 0;
|
1998-03-23 06:36:09 +00:00
|
|
|
|
|
|
|
if (!ttt_type)
|
|
|
|
{
|
2002-02-16 23:52:30 +00:00
|
|
|
static const GTypeInfo ttt_info =
|
1998-03-23 06:36:09 +00:00
|
|
|
{
|
|
|
|
sizeof (TictactoeClass),
|
2002-02-16 23:52:30 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
(GClassInitFunc) tictactoe_class_init,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
sizeof (Tictactoe),
|
|
|
|
0,
|
|
|
|
(GInstanceInitFunc) tictactoe_init,
|
1998-03-23 06:36:09 +00:00
|
|
|
};
|
|
|
|
|
2002-02-16 23:52:30 +00:00
|
|
|
ttt_type = g_type_register_static (GTK_TYPE_VBOX, "Tictactoe", &ttt_info, 0);
|
1998-03-23 06:36:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ttt_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tictactoe_class_init (TictactoeClass *class)
|
|
|
|
{
|
|
|
|
GtkObjectClass *object_class;
|
|
|
|
|
|
|
|
object_class = (GtkObjectClass*) class;
|
|
|
|
|
2002-02-16 23:52:30 +00:00
|
|
|
tictactoe_signals[TICTACTOE_SIGNAL] = g_signal_new ("tictactoe",
|
|
|
|
G_TYPE_FROM_CLASS (object_class),
|
|
|
|
G_SIGNAL_RUN_FIRST,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
g_cclosure_marshal_VOID__VOID,
|
|
|
|
G_TYPE_NONE, 0, NULL);
|
1998-03-23 06:36:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
class->tictactoe = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tictactoe_init (Tictactoe *ttt)
|
|
|
|
{
|
|
|
|
GtkWidget *table;
|
|
|
|
gint i,j;
|
|
|
|
|
|
|
|
table = gtk_table_new (3, 3, TRUE);
|
2002-02-19 01:25:26 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (ttt), table);
|
1998-03-23 06:36:09 +00:00
|
|
|
gtk_widget_show (table);
|
|
|
|
|
2002-02-19 01:25:26 +00:00
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
for (j = 0; j < 3; j++)
|
1998-03-23 06:36:09 +00:00
|
|
|
{
|
|
|
|
ttt->buttons[i][j] = gtk_toggle_button_new ();
|
2002-02-19 01:25:26 +00:00
|
|
|
gtk_table_attach_defaults (GTK_TABLE (table), ttt->buttons[i][j],
|
1998-03-23 06:36:09 +00:00
|
|
|
i, i+1, j, j+1);
|
2002-02-19 19:47:16 +00:00
|
|
|
g_signal_connect (G_OBJECT (ttt->buttons[i][j]), "toggled",
|
|
|
|
G_CALLBACK (tictactoe_toggle), ttt);
|
2002-02-16 23:52:30 +00:00
|
|
|
gtk_widget_set_size_request (ttt->buttons[i][j], 20, 20);
|
1998-03-23 06:36:09 +00:00
|
|
|
gtk_widget_show (ttt->buttons[i][j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkWidget*
|
|
|
|
tictactoe_new ()
|
|
|
|
{
|
2002-02-16 23:52:30 +00:00
|
|
|
return GTK_WIDGET (g_object_new (tictactoe_get_type (), NULL));
|
1998-03-23 06:36:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tictactoe_clear (Tictactoe *ttt)
|
|
|
|
{
|
|
|
|
int i,j;
|
|
|
|
|
2002-02-19 01:25:26 +00:00
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
for (j = 0; j < 3; j++)
|
1998-03-23 06:36:09 +00:00
|
|
|
{
|
2002-02-19 19:47:16 +00:00
|
|
|
g_signal_handlers_block_by_func (G_OBJECT (ttt->buttons[i][j]),
|
|
|
|
NULL, ttt);
|
1999-01-28 10:35:40 +00:00
|
|
|
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ttt->buttons[i][j]),
|
2002-02-19 01:25:26 +00:00
|
|
|
FALSE);
|
2002-02-19 19:47:16 +00:00
|
|
|
g_signal_handlers_unblock_by_func (G_OBJECT (ttt->buttons[i][j]),
|
|
|
|
NULL, ttt);
|
1998-03-23 06:36:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tictactoe_toggle (GtkWidget *widget, Tictactoe *ttt)
|
|
|
|
{
|
|
|
|
int i,k;
|
|
|
|
|
|
|
|
static int rwins[8][3] = { { 0, 0, 0 }, { 1, 1, 1 }, { 2, 2, 2 },
|
|
|
|
{ 0, 1, 2 }, { 0, 1, 2 }, { 0, 1, 2 },
|
|
|
|
{ 0, 1, 2 }, { 0, 1, 2 } };
|
|
|
|
static int cwins[8][3] = { { 0, 1, 2 }, { 0, 1, 2 }, { 0, 1, 2 },
|
|
|
|
{ 0, 0, 0 }, { 1, 1, 1 }, { 2, 2, 2 },
|
|
|
|
{ 0, 1, 2 }, { 2, 1, 0 } };
|
|
|
|
|
|
|
|
int success, found;
|
|
|
|
|
2002-02-19 01:25:26 +00:00
|
|
|
for (k = 0; k < 8; k++)
|
1998-03-23 06:36:09 +00:00
|
|
|
{
|
|
|
|
success = TRUE;
|
|
|
|
found = FALSE;
|
|
|
|
|
2002-02-19 01:25:26 +00:00
|
|
|
for (i = 0; i < 3; i++)
|
1998-03-23 06:36:09 +00:00
|
|
|
{
|
|
|
|
success = success &&
|
2002-02-19 01:25:26 +00:00
|
|
|
GTK_TOGGLE_BUTTON (ttt->buttons[rwins[k][i]][cwins[k][i]])->active;
|
1998-03-23 06:36:09 +00:00
|
|
|
found = found ||
|
|
|
|
ttt->buttons[rwins[k][i]][cwins[k][i]] == widget;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (success && found)
|
|
|
|
{
|
2002-02-19 19:47:16 +00:00
|
|
|
g_signal_emit (G_OBJECT (ttt),
|
2002-02-19 01:25:26 +00:00
|
|
|
tictactoe_signals[TICTACTOE_SIGNAL], 0);
|
1998-03-23 06:36:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1998-12-10 17:31:04 +00:00
|
|
|
|