popup.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.2 $ on $Date: 86/11/26 12:10:38 $
  18. * $Source: /users/faustus/xchess/RCS/popup.c,v $
  19. * Copyright (c) 1986 Wayne A. Christopher, U. C. Berkeley CAD Group
  20. * faustus@cad.berkeley.edu, ucbvax!faustus
  21. * Permission is granted to modify and re-distribute this code in any manner
  22. * as long as this notice is preserved. All standard disclaimers apply.
  23. *
  24. * A simple pop-up menu system.
  25. */
  26. #include "xchess.h"
  27. /* Open a small window with some text in it and two buttons -- yes and no.
  28. * Use black and white pixel, and the medium font.
  29. */
  30. bool
  31. pop_question(win, text)
  32. windata *win;
  33. char *text;
  34. {
  35. char *s, *t;
  36. int nlines = 1, ncols = 0, i = 0, j;
  37. int x, y;
  38. Window w;
  39. bool ch;
  40. XEvent ev;
  41. Pixmap btile, ttile;
  42. for (s = text; *s; s++) {
  43. if ((*s == '\n') && s[1])
  44. nlines++;
  45. if ((*s == '\n') || !s[1]) {
  46. if (i > ncols)
  47. ncols = i;
  48. i = 0;
  49. } else
  50. i++;
  51. }
  52. if (ncols < 12)
  53. ncols = 12;
  54. nlines += 4;
  55. ncols += 4;
  56. x = (BASE_WIDTH - ncols * win->medium->width) / 2;
  57. y = (BASE_HEIGHT - nlines * win->medium->height) / 2;
  58. XSync(0);
  59. XSetDisplay(win->display);
  60. btile = XMakeTile(win->border.pixel);
  61. ttile = XMakeTile(win->textback.pixel);
  62. w = XCreateWindow(win->basewin, x, y, ncols *
  63. win->medium->width, nlines * win->medium->height,
  64. BORDER_WIDTH, btile, ttile);
  65. XMapWindow(w);
  66. for (i = 0, s = text; i < nlines - 4; i++) {
  67. for (t = s, j = 0; *t && (*t != '\n'); t++, j++)
  68. ;
  69. XText(w, (ncols - j) / 2 * win->medium->width, (i + 1) *
  70. win->medium->height, s, j, win->medium->id,
  71. win->textcolor.pixel, win->textback.pixel);
  72. s = t + 1;
  73. }
  74. XText(w, (ncols - 8) * win->medium->width / 4, (nlines - 2) *
  75. win->medium->height, "YES", 3, win->medium->id,
  76. win->textcolor.pixel, win->textback.pixel);
  77. XText(w, (ncols - 4) * win->medium->width * 3 / 4, (nlines - 2) *
  78. win->medium->height, "NO", 2, win->medium->id,
  79. win->textcolor.pixel, win->textback.pixel);
  80. XSelectInput(w, ButtonPressed);
  81. XWindowEvent(w, ButtonPressed, &ev);
  82. x = ((XKeyOrButtonEvent *) &ev)->x;
  83. y = ((XKeyOrButtonEvent *) &ev)->y;
  84. if (x > ncols * win->medium->width / 2)
  85. ch = false;
  86. else
  87. ch = true;
  88. XFreePixmap(btile);
  89. XFreePixmap(ttile);
  90. XDestroyWindow(w);
  91. return (ch);
  92. }