]> Pileus Git - ~andy/gtk/blob - examples/tictactoe/tictactoe.c
update packaging script for current tutorial
[~andy/gtk] / examples / tictactoe / tictactoe.c
1
2 /* GTK - The GIMP Toolkit
3  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4  *
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.
9  *
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.
14  *
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.
19  */
20 #include "gtk/gtksignal.h"
21 #include "gtk/gtktable.h"
22 #include "gtk/gtktogglebutton.h"
23 #include "tictactoe.h"
24
25 enum {
26   TICTACTOE_SIGNAL,
27   LAST_SIGNAL
28 };
29
30 static void tictactoe_class_init          (TictactoeClass *klass);
31 static void tictactoe_init                (Tictactoe      *ttt);
32 static void tictactoe_toggle              (GtkWidget *widget, Tictactoe *ttt);
33
34 static gint tictactoe_signals[LAST_SIGNAL] = { 0 };
35
36 GType
37 tictactoe_get_type ()
38 {
39   static GType ttt_type = 0;
40
41   if (!ttt_type)
42     {
43       static const GTypeInfo ttt_info =
44       {
45         sizeof (TictactoeClass),
46         NULL,
47         NULL,
48         (GClassInitFunc) tictactoe_class_init,
49         NULL,
50         NULL,
51         sizeof (Tictactoe),
52         0,
53         (GInstanceInitFunc) tictactoe_init,
54       };
55
56       ttt_type = g_type_register_static (GTK_TYPE_VBOX, "Tictactoe", &ttt_info, 0);
57     }
58
59   return ttt_type;
60 }
61
62 static void
63 tictactoe_class_init (TictactoeClass *class)
64 {
65   GtkObjectClass *object_class;
66
67   object_class = (GtkObjectClass*) class;
68   
69   tictactoe_signals[TICTACTOE_SIGNAL] = g_signal_new ("tictactoe",
70                                          G_TYPE_FROM_CLASS (object_class),
71                                          G_SIGNAL_RUN_FIRST,
72                                          0,
73                                          NULL, 
74                                          NULL,                
75                                          g_cclosure_marshal_VOID__VOID,
76                                          G_TYPE_NONE, 0, NULL);
77
78
79   class->tictactoe = NULL;
80 }
81
82 static void
83 tictactoe_init (Tictactoe *ttt)
84 {
85   GtkWidget *table;
86   gint i,j;
87   
88   table = gtk_table_new (3, 3, TRUE);
89   gtk_container_add (GTK_CONTAINER (ttt), table);
90   gtk_widget_show (table);
91
92   for (i = 0; i < 3; i++)
93     for (j = 0; j < 3; j++)
94       {
95         ttt->buttons[i][j] = gtk_toggle_button_new ();
96         gtk_table_attach_defaults (GTK_TABLE (table), ttt->buttons[i][j], 
97                                    i, i+1, j, j+1);
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]);
102       }
103 }
104
105 GtkWidget*
106 tictactoe_new ()
107 {
108   return GTK_WIDGET (g_object_new (tictactoe_get_type (), NULL));
109 }
110
111 void           
112 tictactoe_clear (Tictactoe *ttt)
113 {
114   int i,j;
115
116   for (i = 0; i < 3; i++)
117     for (j = 0; j < 3; j++)
118       {
119         g_signal_handlers_block_by_func (G_OBJECT (ttt->buttons[i][j]), 
120                                          NULL, ttt);
121         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ttt->buttons[i][j]),
122                                       FALSE);
123         g_signal_handlers_unblock_by_func (G_OBJECT (ttt->buttons[i][j]), 
124                                            NULL, ttt);
125       }
126 }
127
128 static void
129 tictactoe_toggle (GtkWidget *widget, Tictactoe *ttt)
130 {
131   int i,k;
132
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 } };
139
140   int success, found;
141
142   for (k = 0; k < 8; k++)
143     {
144       success = TRUE;
145       found = FALSE;
146
147       for (i = 0; i < 3; i++)
148         {
149           success = success && 
150             GTK_TOGGLE_BUTTON (ttt->buttons[rwins[k][i]][cwins[k][i]])->active;
151           found = found ||
152             ttt->buttons[rwins[k][i]][cwins[k][i]] == widget;
153         }
154       
155       if (success && found)
156         {
157           g_signal_emit (G_OBJECT (ttt), 
158                          tictactoe_signals[TICTACTOE_SIGNAL], 0);
159           break;
160         }
161     }
162 }
163