]> Pileus Git - ~andy/gtk/blob - examples/tictactoe/tictactoe.c
- re-write the GtkProgressBar section to the 1.1 API. - add an Appendix
[~andy/gtk] / examples / tictactoe / tictactoe.c
1 /* example-start tictactoe tictactoe.c */
2
3 /* GTK - The GIMP Toolkit
4  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
5  *
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.
10  *
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.
15  *
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.
20  */
21 #include "gtk/gtksignal.h"
22 #include "gtk/gtktable.h"
23 #include "gtk/gtktogglebutton.h"
24 #include "tictactoe.h"
25
26 enum {
27   TICTACTOE_SIGNAL,
28   LAST_SIGNAL
29 };
30
31 static void tictactoe_class_init          (TictactoeClass *klass);
32 static void tictactoe_init                (Tictactoe      *ttt);
33 static void tictactoe_toggle              (GtkWidget *widget, Tictactoe *ttt);
34
35 static gint tictactoe_signals[LAST_SIGNAL] = { 0 };
36
37 guint
38 tictactoe_get_type ()
39 {
40   static guint ttt_type = 0;
41
42   if (!ttt_type)
43     {
44       GtkTypeInfo ttt_info =
45       {
46         "Tictactoe",
47         sizeof (Tictactoe),
48         sizeof (TictactoeClass),
49         (GtkClassInitFunc) tictactoe_class_init,
50         (GtkObjectInitFunc) tictactoe_init,
51         (GtkArgSetFunc) NULL,
52         (GtkArgGetFunc) NULL
53       };
54
55       ttt_type = gtk_type_unique (gtk_vbox_get_type (), &ttt_info);
56     }
57
58   return ttt_type;
59 }
60
61 static void
62 tictactoe_class_init (TictactoeClass *class)
63 {
64   GtkObjectClass *object_class;
65
66   object_class = (GtkObjectClass*) class;
67   
68   tictactoe_signals[TICTACTOE_SIGNAL] = gtk_signal_new ("tictactoe",
69                                          GTK_RUN_FIRST,
70                                          object_class->type,
71                                          GTK_SIGNAL_OFFSET (TictactoeClass, tictactoe),
72                                          gtk_signal_default_marshaller, GTK_TYPE_NONE, 0);
73
74
75   gtk_object_class_add_signals (object_class, tictactoe_signals, LAST_SIGNAL);
76
77   class->tictactoe = NULL;
78 }
79
80 static void
81 tictactoe_init (Tictactoe *ttt)
82 {
83   GtkWidget *table;
84   gint i,j;
85   
86   table = gtk_table_new (3, 3, TRUE);
87   gtk_container_add (GTK_CONTAINER(ttt), table);
88   gtk_widget_show (table);
89
90   for (i=0;i<3; i++)
91     for (j=0;j<3; j++)
92       {
93         ttt->buttons[i][j] = gtk_toggle_button_new ();
94         gtk_table_attach_defaults (GTK_TABLE(table), ttt->buttons[i][j], 
95                                    i, i+1, j, j+1);
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]);
100       }
101 }
102
103 GtkWidget*
104 tictactoe_new ()
105 {
106   return GTK_WIDGET ( gtk_type_new (tictactoe_get_type ()));
107 }
108
109 void           
110 tictactoe_clear (Tictactoe *ttt)
111 {
112   int i,j;
113
114   for (i=0;i<3;i++)
115     for (j=0;j<3;j++)
116       {
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]),
119                                      FALSE);
120         gtk_signal_handler_unblock_by_data (GTK_OBJECT(ttt->buttons[i][j]), ttt);
121       }
122 }
123
124 static void
125 tictactoe_toggle (GtkWidget *widget, Tictactoe *ttt)
126 {
127   int i,k;
128
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 } };
135
136   int success, found;
137
138   for (k=0; k<8; k++)
139     {
140       success = TRUE;
141       found = FALSE;
142
143       for (i=0;i<3;i++)
144         {
145           success = success && 
146             GTK_TOGGLE_BUTTON(ttt->buttons[rwins[k][i]][cwins[k][i]])->active;
147           found = found ||
148             ttt->buttons[rwins[k][i]][cwins[k][i]] == widget;
149         }
150       
151       if (success && found)
152         {
153           gtk_signal_emit (GTK_OBJECT (ttt), 
154                            tictactoe_signals[TICTACTOE_SIGNAL]);
155           break;
156         }
157     }
158 }
159
160 /* example-end */