You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

297 lines
8.0 KiB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "x11.h"
  4. #include "x11-athena.h"
  5. #include <X11/Xaw/XawInit.h>
  6. #include <X11/Xaw/Label.h>
  7. #include <X11/Xaw/Box.h>
  8. #include <X11/Xaw/MenuButton.h>
  9. #include <X11/Xaw/SimpleMenu.h>
  10. #include <X11/Xaw/SmeBSB.h>
  11. #include <X11/Xaw/SmeLine.h>
  12. #include <X11/Xaw/AsciiText.h>
  13. #include "util.h"
  14. #include "Game.h"
  15. #include "UI.h"
  16. static Widget base, menubar, field;
  17. static Widget dialogs[DIALOG_MAX + 1];
  18. static Widget pausebutton;
  19. static void
  20. x11_athena_popup_dialog(int index) {
  21. x11_popup(dialogs[index]);
  22. }
  23. static void
  24. popup(Widget w, XtPointer client_data, XtPointer call_data) {
  25. UNUSED(w);
  26. UNUSED(call_data);
  27. x11_athena_popup_dialog((int)client_data);
  28. }
  29. static Widget
  30. new_menu_item(Widget pshell, int dialog) {
  31. Widget menu_item = XtCreateManagedWidget(UI_menu_string(dialog),
  32. smeBSBObjectClass,
  33. pshell, NULL, 0);
  34. XtAddCallback(menu_item, XtNcallback, popup, (void *) dialog);
  35. return menu_item;
  36. }
  37. static Widget
  38. CreateMenuBar(const char *name, Widget parent) {
  39. Widget menubar, pshell;
  40. Widget game_menu, info_menu;
  41. menubar = XtVaCreateManagedWidget(name, boxWidgetClass, parent,
  42. XtNborderWidth, 0,
  43. XtNorientation, XtEhorizontal, NULL);
  44. game_menu = XtCreateManagedWidget("Game", menuButtonWidgetClass,
  45. menubar, NULL, 0);
  46. pshell = XtCreatePopupShell("menu", simpleMenuWidgetClass,
  47. game_menu, NULL, 0);
  48. new_menu_item(pshell, DIALOG_NEWGAME);
  49. pausebutton = new_menu_item(pshell, DIALOG_PAUSEGAME);
  50. new_menu_item(pshell, DIALOG_WARPLEVEL);
  51. new_menu_item(pshell, DIALOG_HIGHSCORE);
  52. new_menu_item(pshell, DIALOG_QUITGAME);
  53. info_menu = XtCreateManagedWidget("Info", menuButtonWidgetClass,
  54. menubar, NULL, 0);
  55. pshell = XtCreatePopupShell("menu", simpleMenuWidgetClass,
  56. info_menu, NULL, 0);
  57. new_menu_item(pshell, DIALOG_STORY);
  58. new_menu_item(pshell, DIALOG_RULES);
  59. new_menu_item(pshell, DIALOG_ABOUT);
  60. return menubar;
  61. }
  62. static void
  63. close_window (Widget w, XtPointer client_data, XtPointer call_data) {
  64. UNUSED(client_data);
  65. UNUSED(call_data);
  66. XtPopdown(XtParent(XtParent(w)));
  67. }
  68. static Widget
  69. CreateRowCol(const char *name, Widget parent) {
  70. return XtCreateManagedWidget(name, boxWidgetClass, parent, NULL, 0);
  71. }
  72. static void
  73. CreatePixmapBox(int index, Widget parent, Pixmap logo, Pixmap pixmap) {
  74. Widget base, pshell, button;
  75. const char *text = UI_dialog_string(index);
  76. pshell = XtCreatePopupShell(UI_menu_string(index),
  77. transientShellWidgetClass, parent, NULL, 0);
  78. base = CreateRowCol("", pshell);
  79. XtVaCreateManagedWidget("", labelWidgetClass, base,
  80. XtNbitmap, logo, XtNborderWidth, 0, NULL);
  81. if (pixmap)
  82. XtVaCreateManagedWidget("", labelWidgetClass, base,
  83. XtNbitmap, pixmap, XtNborderWidth,
  84. 0, NULL);
  85. if (text != NULL)
  86. XtVaCreateManagedWidget("", labelWidgetClass, base,
  87. XtNlabel, text, XtNborderWidth, 0, NULL);
  88. button = XtVaCreateManagedWidget("OK", commandWidgetClass, base, NULL);
  89. XtAddCallback(button, XtNcallback, close_window, NULL);
  90. dialogs[index] = base;
  91. }
  92. static void
  93. CreateEnterText(int index, Widget parent, XtCallbackProc callback) {
  94. Widget base, pshell, button, textfield;
  95. pshell = XtCreatePopupShell(UI_menu_string(index),
  96. transientShellWidgetClass, parent, NULL, 0);
  97. base = CreateRowCol("", pshell);
  98. XtVaCreateManagedWidget("", labelWidgetClass, base,
  99. XtNlabel, UI_dialog_string(index), XtNborderWidth, 0, NULL);
  100. textfield = XtVaCreateManagedWidget("", asciiTextWidgetClass, base,
  101. XtNeditType, XawtextEdit, XtNstring, "", XtNwidth, 200, NULL);
  102. button = XtVaCreateManagedWidget("OK", commandWidgetClass, base, NULL);
  103. XtAddCallback(button, XtNcallback, callback, textfield);
  104. XtAddCallback(button, XtNcallback, close_window, NULL);
  105. button = XtVaCreateManagedWidget("Cancel", commandWidgetClass, base,
  106. NULL);
  107. XtAddCallback(button, XtNcallback, close_window, NULL);
  108. dialogs[index] = base;
  109. }
  110. static void
  111. CreateDialog(int index, Widget parent, int hascancel, Pixmap icon,
  112. const char *buttonlabel, XtCallbackProc callback)
  113. {
  114. Widget base, pshell, button;
  115. const char *text = UI_dialog_string(index);
  116. char ttext[16];
  117. pshell = XtCreatePopupShell(UI_menu_string(index),
  118. transientShellWidgetClass, parent, NULL, 0);
  119. base = CreateRowCol("base", pshell);
  120. if (icon)
  121. XtVaCreateManagedWidget("", labelWidgetClass, base,
  122. XtNbitmap, icon,
  123. XtNborderWidth, 0, NULL);
  124. if (text != NULL && strlen(text) < sizeof(ttext)) {
  125. sprintf(ttext, "%-*s", (int)sizeof(ttext) - 1, text);
  126. text = ttext;
  127. }
  128. XtVaCreateManagedWidget("label", labelWidgetClass, base,
  129. XtNlabel, text, XtNborderWidth, 0, NULL);
  130. if (!buttonlabel)
  131. buttonlabel="OK";
  132. button = XtVaCreateManagedWidget(buttonlabel, commandWidgetClass,
  133. base, NULL);
  134. if (callback)
  135. XtAddCallback(button, XtNcallback, callback, NULL);
  136. XtAddCallback(button, XtNcallback, close_window, NULL);
  137. if (hascancel) {
  138. button = XtVaCreateManagedWidget("Cancel", commandWidgetClass,
  139. base, NULL);
  140. XtAddCallback(button, XtNcallback, close_window, NULL);
  141. }
  142. dialogs[index] = base;
  143. }
  144. static Widget
  145. CreateDrawingArea(const char *name, Widget parent, int width, int height) {
  146. return XtVaCreateManagedWidget(name, coreWidgetClass, parent, XtNwidth,
  147. width, XtNheight, height, NULL);
  148. }
  149. static void
  150. x11_athena_update_dialog(int index, const char *str) {
  151. WidgetList t;
  152. XtVaGetValues(dialogs[index], XtNchildren, &t, NULL);
  153. XtVaSetValues(t[0], XtNlabel, str, NULL);
  154. }
  155. static void
  156. x11_athena_make_main_window(int size) {
  157. x11_setup_resources();
  158. base = XtVaCreateManagedWidget("base", boxWidgetClass, x11_toplevel(),
  159. XtNhSpace, 0, XtNvSpace, 0, NULL);
  160. menubar = CreateMenuBar("menubar", base);
  161. field = CreateDrawingArea("field", base, size, size);
  162. x11_setup();
  163. x11_set_drawingarea(field, size);
  164. x11_add_event_handlers(field);
  165. }
  166. static void
  167. new_game(Widget w, XtPointer client_data, XtPointer call_data) {
  168. UNUSED(w);
  169. UNUSED(client_data);
  170. UNUSED(call_data);
  171. Game_start(1);
  172. }
  173. static void
  174. quit_game(Widget w, XtPointer client_data, XtPointer call_data) {
  175. UNUSED(w);
  176. UNUSED(client_data);
  177. UNUSED(call_data);
  178. Game_quit();
  179. }
  180. static void
  181. warp_apply(Widget w, XtPointer client_data, XtPointer call_data) {
  182. char *str;
  183. char *endp;
  184. int newlevel;
  185. Widget text = (Widget) client_data;
  186. UNUSED(w);
  187. UNUSED(call_data);
  188. XtVaGetValues(text, XtNstring, &str, NULL);
  189. newlevel = strtol(str, &endp, 10);
  190. if (*endp != '\0')
  191. return;
  192. Game_warp_to_level(newlevel);
  193. }
  194. static void
  195. enter_name(Widget w, XtPointer client_data, XtPointer call_data) {
  196. char *str;
  197. Widget text = (Widget) client_data;
  198. UNUSED(w);
  199. UNUSED(call_data);
  200. XtVaGetValues(text, XtNstring, &str, NULL);
  201. Game_add_high_score(str);
  202. }
  203. static void
  204. x11_athena_create_dialogs(Picture *logo, Picture *icon, Picture *about) {
  205. CreateDialog(DIALOG_NEWGAME, base, 1, (Pixmap)NULL, NULL, new_game);
  206. CreateDialog(DIALOG_PAUSEGAME, base, 0, icon->pix, "Continue", NULL);
  207. CreateEnterText(DIALOG_WARPLEVEL, base, warp_apply);
  208. CreateDialog(DIALOG_HIGHSCORE, base, 0, (Pixmap)NULL, NULL, NULL);
  209. CreateDialog(DIALOG_QUITGAME, base, 1, (Pixmap)NULL, NULL, quit_game);
  210. CreatePixmapBox(DIALOG_STORY, base, logo->pix, (Pixmap)NULL);
  211. CreatePixmapBox(DIALOG_RULES, base, logo->pix, (Pixmap)NULL);
  212. CreatePixmapBox(DIALOG_ABOUT, base, logo->pix, about->pix);
  213. CreateDialog(DIALOG_SCORE, base, 0, (Pixmap)NULL, NULL, NULL);
  214. CreateDialog(DIALOG_ENDGAME, base, 0, (Pixmap)NULL, "Nuts!", NULL);
  215. CreateEnterText(DIALOG_ENTERNAME, base, enter_name);
  216. }
  217. static void
  218. x11_athena_set_pausebutton(int active) {
  219. if (pausebutton != NULL)
  220. XtSetSensitive(pausebutton, active);
  221. }
  222. static struct UI_methods x11_athena_methods = {
  223. x11_set_cursor,
  224. x11_load_cursor,
  225. x11_load_picture,
  226. x11_set_icon,
  227. x11_picture_width,
  228. x11_picture_height,
  229. x11_graphics_init,
  230. x11_clear_window,
  231. x11_refresh_window,
  232. x11_draw_image,
  233. x11_draw_line,
  234. x11_draw_string,
  235. x11_start_timer,
  236. x11_stop_timer,
  237. x11_timer_active,
  238. x11_athena_popup_dialog,
  239. x11_main_loop,
  240. x11_initialize,
  241. x11_athena_make_main_window,
  242. x11_athena_create_dialogs,
  243. x11_athena_set_pausebutton,
  244. x11_athena_update_dialog,
  245. };
  246. void
  247. x11_athena_setmethods(UI_methods **methodsp) {
  248. *methodsp = &x11_athena_methods;
  249. }