MSGBOX.CPP 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. /* $Header: /CounterStrike/MSGBOX.CPP 1 3/03/97 10:25a Joe_bostic $ */
  15. /***********************************************************************************************
  16. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  17. ***********************************************************************************************
  18. * *
  19. * Project Name : Command & Conquer *
  20. * *
  21. * File Name : OPTIONS.CPP *
  22. * *
  23. * Programmer : Joe L. Bostic *
  24. * *
  25. * Start Date : June 8, 1994 *
  26. * *
  27. * Last Update : August 24, 1995 [JLB] *
  28. * *
  29. *---------------------------------------------------------------------------------------------*
  30. * Functions: *
  31. * WWMessageBox::Process -- Handles all the options graphic interface. *
  32. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  33. #include "function.h"
  34. #include "msgbox.h"
  35. #include "gadget.h"
  36. #ifdef FIXIT_VERSION_3
  37. bool cancel_current_msgbox = false;
  38. #endif
  39. /***********************************************************************************************
  40. * WWMessageBox::Process -- pops up a message with yes/no, etc *
  41. * *
  42. * This function displays a dialog box with a one-line message, and *
  43. * up to two buttons. The 2nd button is optional. The buttons default *
  44. * to "OK" and nothing, respectively. The hotkeys for the buttons are *
  45. * RETURN and ESCAPE. *
  46. * *
  47. * INPUT: *
  48. * msg message to display *
  49. * b1txt text for 1st button *
  50. * b2txt text for 2nd button; NULL = no 2nd button *
  51. * *
  52. * OUTPUT: *
  53. * # of button selected (0 = 1st) *
  54. * *
  55. * WARNINGS: *
  56. * 'msg' text must be <= 38 chars *
  57. * 'b1txt' and 'b2txt' must each be <= 18 chars *
  58. * *
  59. * HISTORY: *
  60. * 11/08/1994 BR : Created. *
  61. * 05/18/1995 JLB : Uses new font and dialog style. *
  62. * 08/24/1995 JLB : Handles three buttons. *
  63. *=============================================================================================*/
  64. #define BUTTON_1 1
  65. #define BUTTON_2 2
  66. #define BUTTON_3 3
  67. #define BUTTON_FLAG 0x8000
  68. int WWMessageBox::Process(const char * msg, const char * b1txt, const char * b2txt, const char * b3txt, bool preserve)
  69. {
  70. #define BUFFSIZE (511)
  71. char buffer[BUFFSIZE];
  72. int retval;
  73. bool process; // loop while true
  74. int selection;
  75. bool pressed;
  76. int curbutton;
  77. TextButtonClass * buttons[3];
  78. void * back;
  79. BOOL display; // display level
  80. int realval[5];
  81. #ifndef WIN32
  82. int preservex,preservey,preservew,preserveh;
  83. #endif
  84. #ifdef WIN32
  85. GraphicBufferClass seen_buff_save(VisiblePage.Get_Width(), VisiblePage.Get_Height(), (void*)NULL);
  86. #endif
  87. if (b1txt != NULL && *b1txt == '\0') b1txt = NULL;
  88. if (b2txt != NULL && *b2txt == '\0') b2txt = NULL;
  89. if (b3txt != NULL && *b3txt == '\0') b3txt = NULL;
  90. //PG_TO_FIX
  91. //Fancy_Text_Print(TXT_NONE, 0, 0, TBLACK, TBLACK, TPF_TEXT);
  92. /*
  93. ** Examine the optional button parameters. Fetch the width and starting
  94. ** characters for each.
  95. */
  96. int bwidth, bheight; // button width and height
  97. int numbuttons = 0;
  98. if (b1txt != NULL) {
  99. /*
  100. ** Build the button list.
  101. */
  102. bheight = FontHeight + FontYSpacing + (2 * RESFACTOR);
  103. bwidth = max((String_Pixel_Width(b1txt) + (8 * RESFACTOR)), (30 * RESFACTOR));
  104. if (b2txt != NULL) {
  105. numbuttons = 2;
  106. bwidth = max(((int)String_Pixel_Width( b2txt ) + (8 * RESFACTOR)), bwidth);
  107. if (b3txt != NULL) {
  108. numbuttons = 3;
  109. }
  110. } else {
  111. numbuttons = 1;
  112. }
  113. }
  114. /*
  115. ** Determine the dimensions of the text to be used for the dialog box.
  116. ** These dimensions will control how the dialog box looks.
  117. */
  118. buffer[BUFFSIZE-1] = 0;
  119. strncpy(buffer, msg, BUFFSIZE-1);
  120. //PG_TO_FIX
  121. //Fancy_Text_Print(TXT_NONE, 0, 0, TBLACK, TBLACK, TPF_TEXT);
  122. int width;
  123. int height;
  124. int lines = Format_Window_String(buffer, 255 * RESFACTOR, width, height);
  125. TextPrintType tpf = TPF_TEXT;
  126. width = max(width, (90 * RESFACTOR));
  127. width += 40 * RESFACTOR;
  128. height += (numbuttons == 0) ? (40 * RESFACTOR) : (60 * RESFACTOR);
  129. int x = (SeenBuff.Get_Width() - width) / 2;
  130. int y = (SeenBuff.Get_Height() - height) / 2;
  131. int printx = x + (20 * RESFACTOR);
  132. /*
  133. ** Special hack to center a one line dialog box text.
  134. */
  135. if (lines == 1) {
  136. printx = x + width/2;
  137. tpf = tpf | TPF_CENTER;
  138. }
  139. /*
  140. ** Other inits.
  141. */
  142. Set_Logic_Page(SeenBuff);
  143. #ifdef WIN32
  144. VisiblePage.Blit(seen_buff_save);
  145. #endif
  146. /*
  147. ** Initialize the button structures. All are initialized, even though one (or none) may
  148. ** actually be added to the button list.
  149. */
  150. //DOS BUILD GERMAN BUTTONS NEED TO ONE ON TOP OF THE OTHER VG 11/6/96
  151. TextButtonClass button1(BUTTON_1, b1txt, TPF_BUTTON,
  152. x + ((numbuttons == 1) ? ((width - bwidth) >> 1) : (20 * RESFACTOR)), y + height - (bheight + (15 * RESFACTOR)), bwidth);
  153. /*
  154. ** Center button.
  155. */
  156. TextButtonClass button2(BUTTON_2, b2txt, TPF_BUTTON,
  157. x + width - (bwidth + (20 * RESFACTOR)), y + height - (bheight + (15 * RESFACTOR)), bwidth);
  158. /*
  159. ** Right button.
  160. */
  161. TextButtonClass button3(BUTTON_3, b3txt, TPF_BUTTON, 0, y + height - (bheight + (15 * RESFACTOR)));
  162. button3.X = x + ((width - button3.Width) >> 1);
  163. TextButtonClass * buttonlist = 0;
  164. curbutton = 0;
  165. /*
  166. ** Add and initialize the buttons to the button list.
  167. */
  168. memset(buttons, '\0', sizeof(buttons));
  169. if (numbuttons > 0) {
  170. buttonlist = &button1;
  171. buttons[0] = &button1;
  172. realval[0] = BUTTON_1;
  173. if (numbuttons > 2) {
  174. button3.Add(*buttonlist);
  175. buttons[1] = &button3;
  176. realval[1] = BUTTON_3;
  177. button2.Add(*buttonlist);
  178. buttons[2] = &button2;
  179. realval[2] = BUTTON_2;
  180. buttons[curbutton]->Turn_On();
  181. } else {
  182. if (numbuttons == 2) {
  183. button2.Add(*buttonlist);
  184. buttons[1] = &button2;
  185. realval[1] = BUTTON_2;
  186. buttons[curbutton]->Turn_On();
  187. }
  188. }
  189. }
  190. /*
  191. ** Draw the dialog.
  192. */
  193. Hide_Mouse();
  194. if (preserve) {
  195. #ifndef WIN32
  196. preservex = max(0, x-4);
  197. preservey = max(0, y-4);
  198. preservew = min(width+8, 320-preservex);
  199. preserveh = min(height+8, 200-preservey);
  200. back = new char[preservew * preserveh];
  201. SeenBuff.To_Buffer(preservex, preservey, preservew, preserveh, back, preservew * preserveh);
  202. #else
  203. back = new char[width * height];
  204. SeenBuff.To_Buffer(x, y, width, height, back, width * height);
  205. #endif
  206. }
  207. Dialog_Box(x, y, width, height);
  208. Draw_Caption(Caption, x, y, width);
  209. /*
  210. ** Draw the body of the message.
  211. */
  212. Fancy_Text_Print(buffer, printx, y + 20*RESFACTOR, GadgetClass::Get_Color_Scheme(), TBLACK, tpf);
  213. /*
  214. ** Redraw the buttons.
  215. */
  216. if (buttonlist) {
  217. buttonlist->Draw_All();
  218. }
  219. Show_Mouse();
  220. /*
  221. ** Main Processing Loop.
  222. */
  223. if (buttonlist) {
  224. process = true;
  225. pressed = false;
  226. while (process) {
  227. #ifdef WIN32
  228. /*
  229. ** If we have just received input focus again after running in the background then
  230. ** we need to redraw.
  231. */
  232. if (AllSurfaces.SurfacesRestored) {
  233. AllSurfaces.SurfacesRestored = false;
  234. seen_buff_save.Blit(VisiblePage);
  235. display = true;
  236. }
  237. #endif
  238. if (display) {
  239. display = false;
  240. Hide_Mouse();
  241. Dialog_Box(x, y, width, height);
  242. Draw_Caption(Caption, x, y, width);
  243. /*
  244. ** Draw the body of the message.
  245. */
  246. Fancy_Text_Print(buffer, printx, y + 20*RESFACTOR, GadgetClass::Get_Color_Scheme(), TBLACK, tpf);
  247. /*
  248. ** Redraw the buttons.
  249. */
  250. if (buttonlist) {
  251. buttonlist->Draw_All();
  252. }
  253. Show_Mouse();
  254. }
  255. /*
  256. ** Invoke game callback.
  257. */
  258. Call_Back();
  259. /*
  260. ** Fetch and process input.
  261. */
  262. KeyNumType input = buttonlist->Input();
  263. #ifdef FIXIT_VERSION_3
  264. // I really hate to do this, but... ajw
  265. if( cancel_current_msgbox )
  266. {
  267. cancel_current_msgbox = false;
  268. input = KN_ESC;
  269. }
  270. #endif
  271. switch (input) {
  272. case (KN_ESC):
  273. selection = realval[numbuttons-1];
  274. pressed = true;
  275. #ifdef NEVER
  276. if (numbuttons > 2) {
  277. selection = realval[1];
  278. pressed = true;
  279. } else {
  280. selection = realval[2];
  281. pressed = true;
  282. }
  283. #endif
  284. break;
  285. case (BUTTON_1|BUTTON_FLAG):
  286. selection = realval[0];
  287. pressed = true;
  288. break;
  289. case (BUTTON_2|BUTTON_FLAG):
  290. if (numbuttons > 2) {
  291. selection = realval[2];
  292. } else {
  293. selection = realval[1];
  294. }
  295. pressed = true;
  296. break;
  297. case (BUTTON_3|BUTTON_FLAG):
  298. selection = realval[1];
  299. pressed = true;
  300. break;
  301. case (KN_LEFT):
  302. if (numbuttons > 1) {
  303. buttons[curbutton]->Turn_Off();
  304. buttons[curbutton]->Flag_To_Redraw();
  305. curbutton--;
  306. if (curbutton < 0) {
  307. curbutton = numbuttons - 1;
  308. }
  309. buttons[curbutton]->Turn_On();
  310. buttons[curbutton]->Flag_To_Redraw();
  311. }
  312. break;
  313. case (KN_RIGHT):
  314. if (numbuttons > 1) {
  315. buttons[curbutton]->Turn_Off();
  316. buttons[curbutton]->Flag_To_Redraw();
  317. curbutton++;
  318. if (curbutton > (numbuttons - 1) ) {
  319. curbutton = 0;
  320. }
  321. buttons[curbutton]->Turn_On();
  322. buttons[curbutton]->Flag_To_Redraw();
  323. }
  324. break;
  325. case (KN_RETURN):
  326. selection = realval[curbutton];
  327. pressed = true;
  328. break;
  329. /*
  330. ** Check 'input' to see if it's the 1st char of button text
  331. */
  332. default:
  333. break;
  334. }
  335. if (pressed) {
  336. TextButtonClass * toggle;
  337. /*
  338. ** Turn all the buttons off.
  339. */
  340. toggle = (TextButtonClass *)buttonlist->Extract_Gadget(BUTTON_1);
  341. if (toggle != NULL) {
  342. toggle->Turn_Off();
  343. toggle->IsPressed = false;
  344. }
  345. toggle = (TextButtonClass *)buttonlist->Extract_Gadget(BUTTON_2);
  346. if (toggle != NULL) {
  347. toggle->Turn_Off();
  348. toggle->IsPressed = false;
  349. }
  350. toggle = (TextButtonClass *)buttonlist->Extract_Gadget(BUTTON_3);
  351. if (toggle != NULL) {
  352. toggle->Turn_Off();
  353. toggle->IsPressed = false;
  354. }
  355. /*
  356. ** Turn on and depress the button that was selected.
  357. */
  358. if (selection == BUTTON_1 || selection == BUTTON_2 || selection == BUTTON_3) {
  359. TextButtonClass * toggle = (TextButtonClass *)buttonlist->Extract_Gadget(selection);
  360. if (toggle != NULL) {
  361. toggle->Turn_On();
  362. // toggle->IsOn = true;
  363. toggle->IsPressed = true;
  364. }
  365. }
  366. Hide_Mouse();
  367. buttonlist->Draw_All(true);
  368. Show_Mouse();
  369. switch (selection) {
  370. case (BUTTON_1):
  371. retval = 0;
  372. process = false;
  373. break;
  374. case (BUTTON_2):
  375. retval = 1;
  376. process = false;
  377. break;
  378. case BUTTON_3:
  379. retval = 2;
  380. process = false;
  381. break;
  382. }
  383. pressed = false;
  384. }
  385. }
  386. } else {
  387. Keyboard->Clear();
  388. }
  389. /*
  390. ** Restore the screen if necessary.
  391. */
  392. if (preserve) {
  393. Hide_Mouse();
  394. if (SeenBuff.Lock()) {
  395. #ifdef WIN32
  396. Buffer_To_Page(x, y, width, height, back, &SeenBuff);
  397. #else
  398. MCGA_Buffer_To_Page(preservex, preservey, preservew, preserveh, back, &SeenBuff);
  399. #endif
  400. }
  401. SeenBuff.Unlock();
  402. delete[] back;
  403. back = NULL;
  404. Show_Mouse();
  405. }
  406. return(retval);
  407. }
  408. /***********************************************************************************************
  409. * WWMessageBox::Process -- this one takes integer text arguments *
  410. * *
  411. * INPUT: *
  412. * msg message to display *
  413. * b1txt text for 1st button *
  414. * b2txt text for 2nd button; NULL = no 2nd button *
  415. * *
  416. * OUTPUT: *
  417. * # of button selected (0 = 1st) *
  418. * *
  419. * WARNINGS: *
  420. * 'msg' text must be <= 38 chars *
  421. * 'b1txt' and 'b2txt' must each be <= 18 chars *
  422. * *
  423. * HISTORY: *
  424. * 12/12/1994 BR : Created. *
  425. * 06/18/1995 JLB : Simplified. *
  426. *=============================================================================================*/
  427. int WWMessageBox::Process(int msg, int b1txt, int b2txt, int b3txt, bool preserve)
  428. {
  429. return(Process(Text_String(msg), b1txt, b2txt, b3txt, preserve));
  430. }
  431. /***********************************************************************************************
  432. * WWMessageBox::Process -- Displays message box. *
  433. * *
  434. * This routine will display a message box and wait for player input. It varies from the *
  435. * other message box functions only in the type of parameters it takes. *
  436. * *
  437. * INPUT: msg -- Pointer to text string for the message itself. *
  438. * *
  439. * b1txt -- Text number for the "ok" button. *
  440. * *
  441. * b2txt -- Text number for the "cancel" button. *
  442. * *
  443. * OUTPUT: Returns with the button selected. "true" if "OK" was pressed, and "false" if *
  444. * "CANCEL" was pressed. *
  445. * *
  446. * WARNINGS: none *
  447. * *
  448. * HISTORY: *
  449. * 06/18/1995 JLB : Created. *
  450. *=============================================================================================*/
  451. int WWMessageBox::Process(char const * msg, int b1txt, int b2txt, int b3txt, bool preserve)
  452. {
  453. return(Process(msg, Text_String(b1txt), Text_String(b2txt), Text_String(b3txt), preserve));
  454. }