/* 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
* license along with this library; if not, write to the
* free software foundation, inc., 59 temple place - suite 330,
* boston, ma 02111-1307, usa.
*/
#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 };
gtype
tictactoe_get_type ()
{
static gtype ttt_type = 0;
if (!ttt_type)
{
static const gtypeinfo ttt_info =
{
sizeof (tictactoeclass),
null,
null,
(gclassinitfunc) tictactoe_class_init,
null,
null,
sizeof (tictactoe),
0,
(ginstanceinitfunc) tictactoe_init,
};
ttt_type = g_type_register_static (gtk_type_vbox, "tictactoe", &ttt_info, 0);
}
return ttt_type;
}
static void
tictactoe_class_init (tictactoeclass *class)
{
gtkobjectclass *object_class;
object_class = (gtkobjectclass*) class;
tictactoe_signals[tictactoe_signal] = g_signal_new ("tictactoe",
g_type_from_class (object_class),
g_signal_run_first,
0,
g_cclosure_marshal_void__void,
g_type_none, 0, null);
class->tictactoe = null;
}
static void
tictactoe_init (tictactoe *ttt)
{
gtkwidget *table;
gint i,j;
table = gtk_table_new (3, 3, true);
gtk_container_add (gtk_container (ttt), table);
gtk_widget_show (table);
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
{
ttt->buttons[i][j] = gtk_toggle_button_new ();
i, i+1, j, j+1);
g_signal_connect (g_object (ttt->buttons[i][j]), "toggled",
g_callback (tictactoe_toggle), (gpointer) ttt);
gtk_widget_set_size_request (ttt->buttons[i][j], 20, 20);
gtk_widget_show (ttt->buttons[i][j]);
}
}
gtkwidget*
tictactoe_new ()
{
return gtk_widget (g_object_new (tictactoe_get_type (), null));
}
tictactoe_clear (tictactoe *ttt)
{
int i,j;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
{
null, ttt);
gtk_toggle_button_set_active (gtk_toggle_button (ttt->buttons[i][j]),
false);
null, ttt);
}
}
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;
for (k = 0; k < 8; k++)
{
success = true;
found = false;
for (i = 0; i < 3; i++)
{
gtk_toggle_button (ttt->buttons[rwins[k][i]][cwins[k][i]])->active;
found = found ||
ttt->buttons[rwins[k][i]][cwins[k][i]] == widget;
}
if (success && found)
{
tictactoe_signals[tictactoe_signal], 0);
break;
}
}
}
|