Support for g++-4.1.
[gf1.git] / callbacks.cxx
blob0bf5a03175ac72b66fad20388b9c3d0e2d6f67ca
1 /*
2 ** $Id$
3 */
4 /*
5 ** Copyright (C) 1998 Kurt Van den Branden
6 **
7 ** This program is free software; you can redistribute it and/or modify
8 ** it under the terms of the GNU General Public License as published by
9 ** the Free Software Foundation; either version 2 of the License, or
10 ** (at your option) any later version.
11 **
12 ** This program is distributed in the hope that it will be useful,
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ** GNU General Public License for more details.
16 **
17 ** You should have received a copy of the GNU General Public License
18 ** along with this program; if not, write to the Free Software
19 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <stdio.h>
23 #include <time.h>
24 #include <FL/Fl.H>
25 #include <FL/fl_ask.H>
27 #ifdef NEWFILECHOOSER
28 #include <FileChooser.h>
29 #else
30 #include <FL/fl_file_chooser.H>
31 #endif
33 #include "callbacks.h"
34 #include "thegame.h"
35 #include "helptext.h"
37 extern char versiontext[];
38 int interrupt_computer = 0;
39 char currentfilename[100] = "";
41 void show_about (int flag)
43 Fl_Window * aboutw;
45 aboutw = make_aboutwindow ();
46 box_versiontext->label (versiontext);
47 aboutw->show();
49 if (flag)
51 Fl::add_timeout (5.0, remove_about, (void *) aboutw);
52 return;
55 return;
58 void remove_about (void * aboutwindow)
60 Fl_Window * aboutw = (Fl_Window *) aboutwindow;
62 Fl::remove_timeout (remove_about, aboutwindow);
63 delete aboutw;
65 return;
69 void gf1_alert (char * message)
71 Fl_Widget * icon;
73 icon = fl_message_icon ();
74 icon->box (FL_ENGRAVED_BOX);
75 icon->color (FL_GRAY);
77 fl_alert (message);
79 return;
84 ** returns:
85 ** 1 : yes was pressed
86 ** 0 : no was pressed
88 ** position of the window will be just under the gameboard
90 int gf1_question (char * line1, char * line2)
92 Fl_Window * questionw;
93 int theend = 0,
94 value;
95 Fl_Widget * mainwin;
97 questionw = make_questionwindow ();
98 mainwin = gameboard->parent ();
99 questionw->position (mainwin->x() + gameboard->x(),
100 mainwin->y() + mainwin->h() - questionw->h());
101 qtext1->label (line1);
102 qtext2->label (line2);
104 questionw->show();
106 while (!theend)
108 Fl::wait();
110 Fl_Widget *x;
111 while ((x = Fl::readqueue()))
113 if (x == button_qyes)
115 value = 1;
116 theend = 1;
118 else if (x == button_qno)
120 value = 0;
121 theend = 1;
126 delete questionw;
128 return (value);
133 ** returns:
134 ** 2 : save was pressed
135 ** 1 : yes was pressed
136 ** 0 : no was pressed
138 ** position of the window will be just under the gameboard
140 int gf1_questionsave (char * line1, char * line2)
142 Fl_Window * questionw;
143 int theend = 0,
144 value;
145 Fl_Widget * mainwin;
147 questionw = make_questionsavewindow ();
148 mainwin = gameboard->parent ();
149 questionw->position (mainwin->x() + gameboard->x(),
150 mainwin->y() + mainwin->h() - questionw->h());
151 qstext1->label (line1);
152 qstext2->label (line2);
154 questionw->show();
156 while (!theend)
158 Fl::wait();
160 Fl_Widget *x;
161 while ((x = Fl::readqueue()))
163 if (x == button_qsyes)
165 value = 1;
166 theend = 1;
168 else if (x == button_qsno)
170 value = 0;
171 theend = 1;
173 else if (x == button_qssave)
175 value = 2;
176 theend = 1;
181 delete questionw;
183 return (value);
187 void changeinterface (int flag, gamestruct * curgame)
189 listheader * loglines;
190 char * line,
191 tempstr[100];
193 if (flag == INTERFACE_PLAY)
195 /* interface items for playing */
196 count_lostwhite->hide();
197 count_lostblack->hide();
198 choice_piecetype->hide();
199 menu_piecetype->hide();
201 text_to->show();
202 text_from->show();
203 button_execute->show();
205 /* the browser widget for logging */
206 logbrowser->clear();
207 if (curgame != NULL)
209 logbrowser->color (FL_WHITE);
210 loglines = logtobrowser (curgame->movelog);
211 while ((line = (char *) llrembynr (loglines, 1)) != NULL)
213 logbrowser->add (line);
214 free (line);
216 free (loglines);
218 else
219 logbrowser->color (FL_GRAY);
220 logbrowser->bottomline (logbrowser->size());
222 /* player names and timers */
223 if (curgame != NULL)
225 whitename->value (curgame->player[0].name);
226 blackname->value (curgame->player[1].name);
227 if (curgame->player[0].fulltime == -1)
229 whitetimer->value ("-:--:--");
230 blacktimer->value ("-:--:--");
232 else
234 timertostr (curgame->player[0].timer, tempstr);
235 whitetimer->value (tempstr);
236 timertostr (curgame->player[1].timer, tempstr);
237 blacktimer->value (tempstr);
239 /* background color for player-names shows whose turn it is */
240 if (curgame->state == GAME_GO)
242 if (b_next_piece (curgame->boards[0]) == 'o')
244 whitename->color (FL_RED);
245 blackname->color (FL_GRAY);
247 else
249 whitename->color (FL_GRAY);
250 blackname->color (FL_RED);
253 else
255 whitename->color (FL_GRAY);
256 blackname->color (FL_GRAY);
258 whitename->redraw ();
259 blackname->redraw ();
261 else
263 whitename->value ("");
264 blackname->value ("");
265 whitetimer->value ("");
266 blacktimer->value ("");
267 whitename->color (FL_GRAY);
268 blackname->color (FL_GRAY);
269 whitename->redraw ();
270 blackname->redraw ();
273 /* menu items for playing */
274 menu_new->activate();
275 menu_loadgame->activate();
276 if (curgame != NULL)
278 menu_savegame->activate();
279 menu_savelog->activate();
281 ** show the undo-button when there is a board to restore and
282 ** when we are not playing a timed game
284 if ((curgame->boards[2] != NULL) &&
285 (curgame->player[0].fulltime == -1) &
286 (curgame->state == GAME_GO))
287 menu_undo->activate();
288 else
289 menu_undo->deactivate();
291 else
293 menu_savegame->deactivate();
294 menu_savelog->deactivate();
295 menu_undo->deactivate();
297 menu_edit->activate();
298 menu_start->deactivate();
299 menu_compumove->deactivate();
300 menu_clear->deactivate();
301 menu_restore->deactivate();
302 menu_loadboard->deactivate();
304 if (curgame == NULL)
305 menu_saveboard->deactivate();
306 else
307 menu_saveboard->activate();
308 #ifdef HAVEGD
309 if (curgame == NULL)
310 menu_makedrawing->deactivate();
311 else
312 menu_makedrawing->activate();
313 #else
314 menu_makedrawing->deactivate();
315 #endif
317 /* state of the gameboard */
318 if ((curgame == NULL) ||
319 (curgame->state == GAME_STOP))
321 gameboard->setstate (BOARD_NONE);
323 else
325 gameboard->setstate (BOARD_PLAY);
328 else
330 /* interface items for editing */
331 text_to->hide();
332 text_from->hide();
333 button_execute->hide();
335 count_lostwhite->show();
336 count_lostblack->show();
337 choice_piecetype->show();
338 menu_piecetype->show();
340 /* the browser widget for logging */
341 logbrowser->clear();
342 logbrowser->color (FL_GRAY);
344 /* player names and timers */
345 whitename->value ("");
346 blackname->value ("");
347 whitetimer->value ("");
348 blacktimer->value ("");
349 whitename->color (FL_GRAY);
350 blackname->color (FL_GRAY);
351 whitename->redraw ();
352 blackname->redraw ();
354 /* menu items for editing */
355 menu_new->deactivate();
356 menu_loadgame->deactivate();
357 menu_savegame->deactivate();
358 menu_savelog->deactivate();
359 menu_edit->deactivate();
360 menu_start->activate();
361 menu_compumove->activate();
362 menu_clear->activate();
363 menu_restore->activate();
364 menu_loadboard->activate();
365 menu_saveboard->activate();
366 menu_undo->deactivate();
368 #ifdef HAVEGD
369 menu_makedrawing->activate();
370 #else
371 menu_makedrawing->deactivate();
372 #endif
374 /* state of the gameboard */
375 gameboard->setstate (BOARD_EDIT);
378 /* always allowed menu items */
379 menu_setup->activate();
380 menu_exit->activate();
381 menu_help->activate();
382 menu_about->activate();
384 /* problems with the UNDO-button if I don't do this */
385 menubar->redraw ();
387 return;
391 void setlostcounters (void)
393 board * tempboard;
395 tempboard = gameboard->getboard ();
397 if (tempboard == NULL)
399 count_lostwhite->value (0);
400 count_lostblack->value (0);
402 else
404 count_lostwhite->value (b_white_lost (tempboard));
405 count_lostblack->value (b_black_lost (tempboard));
408 return;
412 void stop_thinking (Fl_Button * stopbutton, void * data)
414 interrupt_computer = 1;
416 return;
420 Fl_Window * create_thinkwindow (void)
422 Fl_Window * thinkwindow;
423 Fl_Widget * mainwin;
425 thinkwindow = make_thinkwindow ();
426 mainwin = logbrowser->parent ();
427 thinkwindow->position (logbrowser->x() + mainwin->x() + 10,
428 logbrowser->y() + mainwin->y() + 10);
429 thinkwindow->show ();
430 Fl::check();
432 return (thinkwindow);
436 void timertostr (float newtimer, char * tempstr)
438 int thetimer = (int) newtimer;
440 if (newtimer <= 0.0)
442 sprintf (tempstr, "YOULOSE");
444 else
446 sprintf (tempstr, "%d:%02d:%02d",
447 thetimer / 3600,
448 (thetimer % 3600) / 60,
449 thetimer % 60);
452 return;
456 Fl_Window * gf1_help (void)
458 int i;
459 Fl_Window * helpw;
461 helpw = make_helpwindow ();
463 for (i = 0; i < NRHELPLINES; i++)
465 browser_help->add (helplines[i]);
467 browser_help->topline (1);
469 for (i = 0; i < NRSECTIONLINES; i++)
471 browser_sections->add (sectionlines[i].line);
473 browser_sections->topline (1);
475 return (helpw);
478 void showhelpsection (void)
480 int val = browser_sections->value ();
481 if (val != 0)
483 browser_help->topline (sectionlines[val-1].offset);
486 return;
490 void setcurrentfilename (const char * str)
492 char tempstr[100],
493 * pos;
495 strcpy (currentfilename, str);
496 if (currentfilename[0] == '\0')
498 sprintf (tempstr, "Gipf for One");
500 else
502 #ifdef MSWIN
503 if ((pos = strrchr (currentfilename, '\\')) != NULL)
504 #else
505 if ((pos = strrchr (currentfilename, '/')) != NULL)
506 #endif
507 pos++;
508 else
509 pos = currentfilename;
510 sprintf (tempstr, "Gipf for One (%s)", pos);
513 gf1_window->label (tempstr);
514 return;
517 char * getcurrentfilename (void)
519 return (currentfilename);
522 const char * file_chooser (char * windowtitle, char * pattern, const char * path)
524 const char * filename = NULL;
526 #ifdef NEWFILECHOOSER
527 FileIcon::load_system_icons();
529 FileChooser fc(path, pattern, FileChooser::CREATE, windowtitle);
531 fc.show();
532 while (fc.visible())
533 Fl::wait();
535 if (fc.count())
536 filename = fc.value();
537 #else
538 filename = fl_file_chooser (windowtitle, pattern, path);
539 #endif
541 return (filename);