Initial import, 0.1 + pk 0.2.4-B1
[unix-history] / gnu / games / chess / Xchess / button.c
CommitLineData
15637ed4
RG
1
2/* This file contains code for X-CHESS.
3 Copyright (C) 1986 Free Software Foundation, Inc.
4
5This file is part of X-CHESS.
6
7X-CHESS is distributed in the hope that it will be useful,
8but WITHOUT ANY WARRANTY. No author or distributor
9accepts responsibility to anyone for the consequences of using it
10or for whether it serves any particular purpose or works at all,
11unless he says so in writing. Refer to the X-CHESS General Public
12License for full details.
13
14Everyone is granted permission to copy, modify and redistribute
15X-CHESS, but only under the conditions described in the
16X-CHESS General Public License. A copy of this license is
17supposed to have been given to you along with X-CHESS so you
18can know your rights and responsibilities. It should be in a
19file named COPYING. Among other things, the copyright notice
20and this notice must be preserved on all copies. */
21
22
23/* RCS Info: $Revision: 1.4 $ on $Date: 86/11/26 12:09:41 $
24 * $Source: /users/faustus/xchess/RCS/button.c,v $
25 * Copyright (c) 1986 Wayne A. Christopher, U. C. Berkeley CAD Group
26 * Permission is granted to do anything with this code except sell it
27 * or remove this message.
28 *
29 * Do stuff with the buttons.
30 * The configuration we're using is: Draw Back Pause
31 * Resign Fwd Flip
32 * Reset Save Easy (Switch)
33 */
34
35#include "xchess.h"
36
37typedef enum choice { NOCHOICE, DRAW, RESIGN, REPLAY, SWITCH, FORE, SAVE,
38 STOP, FLIP, RESTART, EASY } choice;
39
40static struct but {
41 char *label;
42 int x, y;
43 int width, height;
44 choice which;
45} buts[] = {
46 { "Draw", 0, 20, 108, 29, DRAW } ,
47 { "Back", 109, 20, 108, 29, REPLAY } ,
48 { "Pause", 219, 20, 108, 29, STOP } ,
49 { "Resign", 0, 50, 108, 29, RESIGN } ,
50 { "Fwd", 109, 50, 108, 29, FORE } ,
51 { "Flip", 219, 50, 108, 29, FLIP } ,
52 { "Reset", 0, 80, 108, 29, RESTART } ,
53 { "Save", 109, 80, 108, 29, SAVE } ,
54#define EASY_OFFSET 8
55 { "Switch", 219, 80, 108, 29, SWITCH }
56/* { "NoEasy", 219, 80, 108, 29, EASY }*/
57} ;
58static int easy = 1;
59
60void
61button_draw(win)
62 windata *win;
63{
64 int i, x, numbuts = sizeof (buts) / sizeof (struct but);
65
66 XSetState(win->display, DefaultGC(win->display, 0),
67 win->border.pixel, WhitePixel(win->display, 0),
68 GXcopy, AllPlanes);
69 XSetLineAttributes(win->display, DefaultGC(win->display, 0),
70 BORDER_WIDTH, LineSolid, CapButt,
71 JoinMiter);
72
73 XDrawLine(win->display, win->buttonwin,
74 DefaultGC(win->display, 0),
75 0, 29, BUTTON_WIDTH, 29);
76 XDrawLine(win->display, win->buttonwin,
77 DefaultGC(win->display, 0),
78 0, 60, BUTTON_WIDTH, 60);
79 XDrawLine(win->display, win->buttonwin,
80 DefaultGC(win->display, 0),
81 108, 0, 108, BUTTON_HEIGHT);
82 XDrawLine(win->display, win->buttonwin,
83 DefaultGC(win->display, 0),
84 219, 0, 219, BUTTON_HEIGHT);
85
86 XSetFont(win->display, DefaultGC(win->display, 0), win->large->fid);
87 XSetForeground(win->display, DefaultGC(win->display, 0),
88 win->textcolor.pixel);
89 XSetBackground(win->display, DefaultGC(win->display, 0),
90 win->textback.pixel);
91
92 for (i = 0; i < numbuts; i++) {
93 x = (buts[i].width -
94 XTextWidth(win->large, buts[i].label,
95 strlen(buts[i].label))) / 2;
96
97 XDrawImageString(win->display, win->buttonwin,
98 DefaultGC(win->display, 0),
99 buts[i].x + x, buts[i].y, buts[i].label,
100 strlen(buts[i].label));
101 }
102 return;
103}
104
105void
106button_service(win, event)
107 windata *win;
108 XEvent *event;
109{
110 XKeyEvent *ev = &event->xkey;
111 choice c;
112 int i, numbuts = sizeof (buts) / sizeof (struct but);
113 char *s;
114
115 ev->y += 15;
116 for (i = 0; i < numbuts; i++)
117 if ((ev->x >= buts[i].x) && (ev->x <= buts[i].x +
118 buts[i].width) && (ev->y >= buts[i].y) &&
119 (ev->y <= buts[i].y + buts[i].height)) {
120 c = buts[i].which;
121 break;
122 }
123 if ((i == numbuts) || (c == NOCHOICE)) {
124 message_add(win, "Bad choice.\n", true);
125 return;
126 }
127
128 if (loading_flag && (c != STOP)) {
129 message_add(win, "You can only use PAUSE now\n", true);
130 return;
131 }
132
133 switch (c) {
134 case DRAW:
135 if (!oneboard) {
136 message_add(win, "Just a sec...\n", false);
137 if (!pop_question(((win == win1) ? win2 : win1),
138"The other player wants\nto call the game a draw.\nDo you agree?\n")) {
139 message_add(win,
140 "The other player declines the draw\n", false);
141 return;
142 }
143 }
144 message_add(win1, "Draw agreed.\n", false);
145 if (!oneboard)
146 message_add(win2, "Draw agreed.\n", false);
147 cleanup("Draw agreed.");
148 break;
149
150 case RESIGN:
151 if (!pop_question(win, "Are you sure\nyou want to resign?"))
152 return;
153 if ((oneboard && !progflag) || (nexttomove == win->color)) {
154 if (nexttomove == WHITE)
155 s = "White resigns.";
156 else
157 s = "Black resigns.";
158 if (oneboard) {
159 message_add(win, s, false);
160 message_add(win, "\n", false);
161 } else {
162 message_add(win1, s, false);
163 message_add(win, "\n", false);
164 message_add(win2, s, false);
165 message_add(win, "\n", false);
166 }
167 sleep(5);
168 cleanup(s);
169 } else {
170 message_add(win, "It's not your turn.\n", true);
171 }
172 break;
173
174 case REPLAY:
175 if (!oneboard) {
176 message_add(win, "Just a sec...\n", false);
177 if (!pop_question(((win == win1) ? win2 : win1),
178"The other player wants\nto take back his last move.\nDo you let him?\n")) {
179 message_add(win,
180 "The other player refuses...\n", false);
181 return;
182 }
183 }
184 if (!moves) {
185 message_add(win, "Can't back up...\n", true);
186 break;
187 }
188 message_add(win1, "Replaying...\n", false);
189 if (!oneboard)
190 message_add(win2, "Replaying...\n", false);
191 replay();
192 if (progflag)
193 replay();
194 break;
195
196 case FORE:
197 if (!oneboard) {
198 message_add(win, "Just a sec...\n", false);
199 if (!pop_question(((win == win1) ? win2 : win1),
200"The other player wants\nto do a 'fore'.\nIs that ok with you?\n")) {
201 message_add(win,
202 "The other player refuses...\n", false);
203 return;
204 }
205 }
206 if (!foremoves) {
207 message_add(win, "Can't go forward...\n", true);
208 break;
209 }
210 message_add(win1, "Moving forward...\n", false);
211 if (!oneboard)
212 message_add(win2, "Moving forward...\n", false);
213 forward();
214 break;
215
216 case SWITCH:
217 message_add(win, "You can't switch yet.\n", false);
218 break;
219
220 case SAVE:
221 if (saveflag) {
222 message_add(win,
223 "Game is already being logged in file '", true);
224 message_add(win, record_file, true);
225 message_add(win, "'.\n", true);
226 } else {
227 message_add(win, "Saving game to file '", false);
228 message_add(win, record_file, false);
229 message_add(win, "'.\n", false);
230 record_save();
231 }
232 break;
233
234 case STOP:
235 if (loading_flag) {
236 loading_paused = (loading_paused ? false : true);
237 message_add(win, loading_paused ?
238 "Stopped.\nHit 'Pause' again to restart.\n" :
239 "Restarted.\n", false);
240 } else if (clock_started) {
241 if (!oneboard) {
242 message_add(win, "Just a sec...\n", false);
243 if (!pop_question(((win == win1) ? win2 : win1),
244"The other player wants\nto stop the clock.\nDo you let him?\n")) {
245 message_add(win,
246 "The other player refuses to pause.\n",
247 false);
248 return;
249 }
250 }
251 message_add(win1,
252 "Clock stopped.\nHit 'Pause' again to restart.\n",
253 false);
254 if (!oneboard)
255 message_add(win2,
256 "Clock stopped.\nHit 'Pause' again to restart.\n",
257 false);
258 clock_started = false;
259 } else {
260 if (!oneboard) {
261 message_add(win, "Just a sec...\n", false);
262 if (!pop_question(((win == win1) ? win2 : win1),
263"The other player wants\nto start the clock again.\nIs that ok?\n")) {
264 message_add(win,
265 "The other player refuses to resume.\n",
266 false);
267 return;
268 }
269 }
270 message_add(win1, "Clock restarted.\n", false);
271 if (!oneboard)
272 message_add(win2, "Clock restarted.\n", false);
273 clock_started = true;
274 }
275 break;
276
277 case FLIP:
278 message_add(win, "Flipping window...\n", false);
279 win->flipped = win->flipped ? false : true;
280 win_redraw(win, (XEvent *) NULL);
281 break;
282
283 case RESTART:
284 if (!oneboard) {
285 message_add(win, "Just a sec...\n", false);
286 if (!pop_question(((win == win1) ? win2 : win1),
287"The other player wants\nto restart the game.\nDo you agree?\n")) {
288 message_add(win,
289 "The other player refuses to reset\n", false);
290 return;
291 }
292 }
293 message_add(win, "Restarting game.\n", false);
294 restart();
295 break;
296 case EASY:
297 if (oneboard) {
298 int x;
299 if (easy)
300 easy = 0;
301 else
302 easy = 1;
303
304 if (easy)
305 buts[EASY_OFFSET].label = " Easy ";
306 else
307 buts[EASY_OFFSET].label = "NoEasy";
308
309 program_easy(easy);
310
311 x = (buts[EASY_OFFSET].width -
312 XTextWidth(win->large,
313 buts[EASY_OFFSET].label,
314 strlen(buts[EASY_OFFSET].label))) / 2;
315
316 XSetFont(win->display, DefaultGC(win->display,
317 0), win->large->fid);
318 XSetForeground(win->display,
319 DefaultGC(win->display, 0),
320 win->textcolor.pixel);
321 XSetBackground(win->display,
322 DefaultGC(win->display, 0),
323 win->textback.pixel);
324
325 XDrawImageString(win->display,
326 win->buttonwin,
327 DefaultGC(win->display, 0),
328 buts[EASY_OFFSET].x + x,
329 buts[EASY_OFFSET].y,
330 buts[EASY_OFFSET].label,
331 strlen(buts[EASY_OFFSET].label));
332 }
333 break;
334 }
335 return;
336}
337