button.c 7.5 KB

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