PageRenderTime 57ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/tools/ddfinwad/win32gui/gui_ctrl.c

#
C | 759 lines | 488 code | 151 blank | 120 comment | 101 complexity | 252c18905830751d857c4a7e5971985e MD5 | raw file
Possible License(s): GPL-2.0
  1. //----------------------------------------------------------------------------
  2. // WIN32 GUI Controls
  3. //----------------------------------------------------------------------------
  4. //
  5. // Copyright (c) 2000 Andy Baker
  6. //
  7. // This program is free software; you can redistribute it and/or
  8. // modify it under the terms of the GNU General Public License
  9. // as published by the Free Software Foundation; either version 2
  10. // of the License, or (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. //----------------------------------------------------------------------------
  18. //
  19. #include "gui.h"
  20. #define MAXFILENAME 512
  21. //
  22. // BUTTON
  23. //
  24. typedef struct
  25. {
  26. HWND buttonhwnd;
  27. void (*action)(HWND wnd, DWORD msg); // Action Procedure
  28. }
  29. _guibutt_t;
  30. //
  31. // EDIT BOX
  32. //
  33. typedef struct
  34. {
  35. HWND boxhwnd; // Box Handle
  36. void (*action)(HWND wnd, DWORD msg); // Action Procedure
  37. }
  38. _editbox_t;
  39. //
  40. // PROGRESS BAR
  41. //
  42. typedef struct
  43. {
  44. HWND barhwnd;
  45. int minrange;
  46. int maxrange;
  47. }
  48. _guipgrsbar_t;
  49. //
  50. // TEXT BAR
  51. //
  52. typedef struct
  53. {
  54. HWND txthwnd;
  55. }
  56. _guitext_t;
  57. // Button List
  58. static _guibutt_t **guibuttlist;
  59. static int numofguibutts = 0;
  60. // EditBox List
  61. static _editbox_t **editboxlist;
  62. static int numofeditboxs = 0;
  63. // ProgressBar List
  64. static _guipgrsbar_t **pgrsbarlist;
  65. static int numofpgrsbars = 0;
  66. // Text List
  67. static _guitext_t **txtlist;
  68. static int numoftxts = 0;
  69. // ============================== BUTTON ==============================
  70. //
  71. // I_GUICreateButton
  72. //
  73. int I_GUICreateButton(guibutton_t* button)
  74. {
  75. _guibutt_t **oldguibuttlist;
  76. HWND butthandle;
  77. int i;
  78. if (numofguibutts && guibuttlist)
  79. {
  80. i=0;
  81. while (i<numofguibutts && guibuttlist[i])
  82. i++;
  83. if (i==numofguibutts)
  84. {
  85. oldguibuttlist = guibuttlist;
  86. guibuttlist = malloc(sizeof(_guibutt_t*)*(numofguibutts+1));
  87. if (!guibuttlist)
  88. return 0;
  89. memset(guibuttlist, 0, sizeof(_guibutt_t*)*(numofguibutts+1));
  90. memcpy(guibuttlist, oldguibuttlist, sizeof(_guibutt_t*)*(numofguibutts));
  91. free(oldguibuttlist);
  92. i = numofguibutts;
  93. numofguibutts++;
  94. }
  95. }
  96. else
  97. {
  98. guibuttlist = malloc(sizeof(_guibutt_t*));
  99. if (!guibuttlist)
  100. return 0;
  101. numofguibutts = 1;
  102. i = 0;
  103. }
  104. guibuttlist[i] = malloc(sizeof(_guibutt_t));
  105. if (!guibuttlist[i])
  106. return 0;
  107. butthandle = CreateWindowEx(0, "BUTTON",
  108. (LPSTR) NULL,
  109. BS_VCENTER|BS_DEFPUSHBUTTON|WS_CHILD|WS_VISIBLE,
  110. button->x,
  111. button->y,
  112. button->width,
  113. button->height,
  114. button->parentwindow,
  115. (HMENU) NULL,
  116. button->parentinst,
  117. NULL);
  118. guibuttlist[i]->action = button->action;
  119. if (button->text)
  120. SetWindowText(butthandle, button->text);
  121. guibuttlist[i]->buttonhwnd = butthandle;
  122. return i;
  123. }
  124. //
  125. // I_GUIButtonMsghandler
  126. //
  127. boolean_g I_GUIButtonMsghandler(HWND window, DWORD message)
  128. {
  129. int i;
  130. i=0;
  131. while (i<numofguibutts)
  132. {
  133. if (guibuttlist[i] && (guibuttlist[i]->buttonhwnd == window))
  134. {
  135. if (guibuttlist[i]->action)
  136. guibuttlist[i]->action(window, message);
  137. return true;
  138. }
  139. i++;
  140. }
  141. return false;
  142. }
  143. //
  144. // I_GUIDestroyButton
  145. //
  146. void I_GUIDestroyButton(int handle)
  147. {
  148. if (handle<0 || handle>=numofguibutts)
  149. return;
  150. if (!guibuttlist[handle])
  151. return;
  152. DestroyWindow(guibuttlist[handle]->buttonhwnd);
  153. free(guibuttlist[handle]);
  154. guibuttlist[handle] = NULL;
  155. }
  156. // ============================== EDIT BOX ==============================
  157. //
  158. // I_GUICreateEditbox
  159. //
  160. int I_GUICreateEditbox(editbox_t* editbox)
  161. {
  162. _editbox_t **oldeditboxlist;
  163. HWND boxhandle;
  164. int i;
  165. if (numofeditboxs && editboxlist)
  166. {
  167. i=0;
  168. while (i<numofeditboxs && editboxlist[i])
  169. i++;
  170. if (i==numofeditboxs)
  171. {
  172. oldeditboxlist = editboxlist;
  173. editboxlist = malloc(sizeof(_editbox_t*)*(numofeditboxs+1));
  174. if (!editboxlist)
  175. return 0;
  176. memset(editboxlist, 0, sizeof(_editbox_t*)*(numofeditboxs+1));
  177. memcpy(editboxlist, oldeditboxlist, sizeof(_editbox_t*)*(numofeditboxs));
  178. free(oldeditboxlist);
  179. i = numofeditboxs;
  180. numofeditboxs++;
  181. }
  182. }
  183. else
  184. {
  185. editboxlist = malloc(sizeof(_editbox_t*));
  186. if (!editboxlist)
  187. return 0;
  188. numofeditboxs = 1;
  189. i = 0;
  190. }
  191. editboxlist[i] = malloc(sizeof(_editbox_t));
  192. if (!editboxlist[i])
  193. return 0;
  194. boxhandle = CreateWindowEx(0, "EDIT",
  195. (LPSTR) NULL,
  196. WS_BORDER|WS_CHILD|WS_VISIBLE,
  197. editbox->x,
  198. editbox->y,
  199. editbox->width,
  200. editbox->height,
  201. editbox->parentwindow,
  202. (HMENU) NULL,
  203. editbox->parentinst,
  204. NULL);
  205. editboxlist[i]->action = editbox->action;
  206. editboxlist[i]->boxhwnd = boxhandle;
  207. return i;
  208. }
  209. //
  210. // I_GUIEnableEditbox
  211. //
  212. boolean_g I_GUIEnableEditbox(int handle, boolean_g enable)
  213. {
  214. if (handle<0 || handle>=numofeditboxs)
  215. return false;
  216. if (!editboxlist[handle])
  217. return false;
  218. if (enable)
  219. EnableWindow(editboxlist[handle]->boxhwnd, TRUE);
  220. else
  221. EnableWindow(editboxlist[handle]->boxhwnd, FALSE);
  222. return true;
  223. }
  224. //
  225. // I_GUISetReadOnlyEditbox
  226. //
  227. boolean_g I_GUISetReadOnlyEditbox(int handle, boolean_g enable)
  228. {
  229. if (handle<0 || handle>=numofeditboxs)
  230. return false;
  231. if (!editboxlist[handle])
  232. return false;
  233. if (enable)
  234. SendMessage(editboxlist[handle]->boxhwnd, EM_SETREADONLY, (WPARAM)(BOOL)TRUE, 0L);
  235. else
  236. SendMessage(editboxlist[handle]->boxhwnd, EM_SETREADONLY, (WPARAM)(BOOL)FALSE, 0L);
  237. return true;
  238. }
  239. //
  240. // I_GUIGetEditboxText
  241. //
  242. boolean_g I_GUIGetEditboxText(int handle, char *str, int strsize)
  243. {
  244. if (handle<0 || handle>=numofeditboxs)
  245. return false;
  246. if (!editboxlist[handle])
  247. return false;
  248. // No I don't like this either: Windozes for you
  249. *(int*)str = strsize;
  250. SendMessage(editboxlist[handle]->boxhwnd, EM_GETLINE, 0, (LPARAM)str);
  251. return true;
  252. }
  253. //
  254. // I_GUISetEditboxText
  255. //
  256. boolean_g I_GUISetEditboxText(int handle, const char *str)
  257. {
  258. if (handle<0 || handle>=numofeditboxs)
  259. return false;
  260. if (!editboxlist[handle])
  261. return false;
  262. SendMessage(editboxlist[handle]->boxhwnd, WM_SETTEXT, 0, (LPARAM)str);
  263. return true;
  264. }
  265. //
  266. // I_GUIEditboxMsghandler
  267. //
  268. boolean_g I_GUIEditboxMsghandler(HWND window, DWORD message)
  269. {
  270. int i;
  271. i=0;
  272. while (i<numofeditboxs)
  273. {
  274. if (editboxlist[i] && (editboxlist[i]->boxhwnd == window))
  275. {
  276. if (editboxlist[i]->action)
  277. editboxlist[i]->action(window, message);
  278. return true;
  279. }
  280. i++;
  281. }
  282. return false;
  283. }
  284. //
  285. // I_GUIDestroyEditbox
  286. //
  287. void I_GUIDestroyEditbox(int handle)
  288. {
  289. if (handle<0 || handle>=numofeditboxs)
  290. return;
  291. if (!editboxlist[handle])
  292. return;
  293. DestroyWindow(editboxlist[handle]->boxhwnd);
  294. free(editboxlist[handle]);
  295. editboxlist[handle] = NULL;
  296. }
  297. // ======================== OPENFILE DIALOG ========================
  298. //
  299. // I_GUIOpenFileDialog
  300. //
  301. const char *I_GUIOpenFileDialog(filedialog_t *filedlg)
  302. {
  303. OPENFILENAME opendialog;
  304. char *filename;
  305. char *filter;
  306. int fpos, fnpos;
  307. int flen, fnlen;
  308. int filterlen, filterpos;
  309. int i;
  310. if (!filedlg)
  311. return NULL;
  312. // Construct filter
  313. flen = strlen(filedlg->filters);
  314. fnlen = strlen(filedlg->filternames);
  315. filterlen = fnlen+flen+2;
  316. filter = malloc(sizeof(char)*(filterlen+1)); // include treble NULL
  317. if (!filter)
  318. return NULL;
  319. memset(filter, 0, sizeof(char)*(filterlen+1));
  320. filterpos = 0; // Construct Filter Position
  321. fpos = 0; // Filters Position
  322. fnpos = 0; // Filter Names Position
  323. // Add Filter Name
  324. while (filterpos<filterlen)
  325. {
  326. while (filedlg->filternames[fnpos] != ',' &&
  327. filedlg->filternames[fnpos] != '\0')
  328. {
  329. filter[filterpos] = filedlg->filternames[fnpos];
  330. filterpos++;
  331. fnpos++;
  332. }
  333. // Skip Comma
  334. if (filedlg->filternames[fnpos] == ',')
  335. fnpos++;
  336. // Add NULL Split
  337. filter[filterpos] = '\0';
  338. filterpos++;
  339. // Add Filter Type
  340. while (filedlg->filters[fpos] != ',' && filedlg->filters[fpos] != '\0')
  341. {
  342. filter[filterpos] = filedlg->filters[fpos];
  343. filterpos++;
  344. fpos++;
  345. }
  346. // Skip Comma
  347. if (filedlg->filters[fpos] == ',')
  348. fpos++;
  349. // Add NULL Split
  350. filter[filterpos] = '\0';
  351. filterpos++;
  352. }
  353. filename = malloc(sizeof(char)*(MAXFILENAME+1));
  354. if (!filename)
  355. return NULL;
  356. memset(filename, 0, sizeof(char)*(MAXFILENAME+1));
  357. opendialog.lStructSize = OPENFILENAME_SIZE_VERSION_400;
  358. opendialog.hwndOwner = filedlg->parentwindow;
  359. opendialog.hInstance = NULL;
  360. opendialog.lpstrFilter = filter;
  361. opendialog.lpstrCustomFilter = (LPTSTR) NULL;
  362. opendialog.nMaxCustFilter = 0L;
  363. opendialog.nFilterIndex = 1L;
  364. opendialog.lpstrFile = filename;
  365. opendialog.nMaxFile = sizeof(char)*MAXFILENAME;
  366. opendialog.lpstrFileTitle = NULL;
  367. opendialog.nMaxFileTitle = 0;
  368. opendialog.lpstrInitialDir = filedlg->startdir;
  369. opendialog.lpstrTitle = filedlg->title;
  370. opendialog.nFileOffset = 0;
  371. opendialog.nFileExtension = 0;
  372. opendialog.lpstrDefExt = NULL; // No default extension
  373. opendialog.lCustData = 0;
  374. opendialog.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST |
  375. OFN_HIDEREADONLY | OFN_LONGNAMES;
  376. if (!GetOpenFileName(&opendialog))
  377. return NULL;
  378. return filename;
  379. }
  380. // ======================== PROGRESS BAR ========================
  381. //
  382. // I_GUICreateBar
  383. //
  384. int I_GUICreateBar(progressbar_t* prgrbar)
  385. {
  386. _guipgrsbar_t **oldpgrsbarlist;
  387. HWND barhandle;
  388. COLORREF crf;
  389. int i;
  390. if (numofpgrsbars && pgrsbarlist)
  391. {
  392. i=0;
  393. while (i<numofpgrsbars && pgrsbarlist[i])
  394. i++;
  395. if (i==numofpgrsbars)
  396. {
  397. oldpgrsbarlist = pgrsbarlist;
  398. pgrsbarlist = malloc(sizeof(_guipgrsbar_t*)*(numofpgrsbars+1));
  399. if (!pgrsbarlist)
  400. return 0;
  401. memset(pgrsbarlist, 0, sizeof(_guipgrsbar_t*)*(numofpgrsbars+1));
  402. memcpy(pgrsbarlist, oldpgrsbarlist, sizeof(_guipgrsbar_t*)*(numofpgrsbars));
  403. free(oldpgrsbarlist);
  404. i = numofpgrsbars;
  405. numofpgrsbars++;
  406. }
  407. }
  408. else
  409. {
  410. pgrsbarlist = malloc(sizeof(_guipgrsbar_t*));
  411. if (!pgrsbarlist)
  412. return 0;
  413. numofpgrsbars = 1;
  414. i = 0;
  415. }
  416. pgrsbarlist[i] = malloc(sizeof(_guipgrsbar_t));
  417. if (!pgrsbarlist[i])
  418. return 0;
  419. barhandle = CreateWindowEx(0, PROGRESS_CLASS,
  420. (LPSTR) NULL,
  421. PBS_SMOOTH|WS_CHILD|WS_VISIBLE,
  422. prgrbar->x,
  423. prgrbar->y,
  424. prgrbar->width,
  425. prgrbar->height,
  426. prgrbar->parentwindow,
  427. (HMENU) NULL,
  428. prgrbar->parentinst,
  429. NULL);
  430. if (!barhandle)
  431. {
  432. //
  433. // No need to make the pointer table any smaller - just
  434. // free the bar struct
  435. //
  436. free(pgrsbarlist[i]);
  437. return 0;
  438. }
  439. // Set the range
  440. SendMessage(barhandle,
  441. PBM_SETRANGE32,
  442. prgrbar->minrange,
  443. prgrbar->maxrange);
  444. // Set the initial position
  445. SendMessage(barhandle, PBM_SETPOS, prgrbar->initpos, 0);
  446. // Set the step
  447. SendMessage(barhandle, PBM_SETSTEP, prgrbar->step, 0);
  448. // Set the bar colour
  449. crf = RGB(prgrbar->barcolour.red,
  450. prgrbar->barcolour.green,
  451. prgrbar->barcolour.blue);
  452. SendMessage(barhandle, PBM_SETBARCOLOR, 0, crf);
  453. // Set the background colour
  454. crf = RGB(prgrbar->backcolour.red,
  455. prgrbar->backcolour.green,
  456. prgrbar->backcolour.blue);
  457. SendMessage(barhandle, PBM_SETBKCOLOR, 0, crf);
  458. // Setup structure
  459. pgrsbarlist[i]->barhwnd = barhandle;
  460. pgrsbarlist[i]->minrange = prgrbar->minrange;
  461. pgrsbarlist[i]->maxrange = prgrbar->maxrange;
  462. return i;
  463. }
  464. //
  465. // I_GUIStepBar
  466. //
  467. boolean_g I_GUIStepBar(int handle)
  468. {
  469. if (handle<0 || handle>=numofpgrsbars)
  470. return false;
  471. if (!pgrsbarlist[handle])
  472. return false;
  473. SendMessage(pgrsbarlist[handle]->barhwnd, PBM_STEPIT, 0, 0);
  474. return true;
  475. }
  476. //
  477. // I_GUIUpdateBar
  478. //
  479. boolean_g I_GUIUpdateBar(int handle, int pos)
  480. {
  481. if (handle<0 || handle>=numofpgrsbars)
  482. return false;
  483. if (!pgrsbarlist[handle])
  484. return false;
  485. if (pgrsbarlist[handle]->minrange>pos || pgrsbarlist[handle]->maxrange<pos)
  486. return false;
  487. SendMessage(pgrsbarlist[handle]->barhwnd, PBM_SETPOS, pos, 0);
  488. return true;
  489. }
  490. //
  491. // I_GUIUpdateBarRange
  492. //
  493. boolean_g I_GUIUpdateBarRange(int handle, int minr, int maxr)
  494. {
  495. if (handle<0 || handle>=numofpgrsbars)
  496. return false;
  497. if (!pgrsbarlist[handle])
  498. return false;
  499. pgrsbarlist[handle]->minrange = minr;
  500. pgrsbarlist[handle]->maxrange = maxr;
  501. // Set the range
  502. SendMessage(pgrsbarlist[handle]->barhwnd,
  503. PBM_SETRANGE32,
  504. pgrsbarlist[handle]->minrange,
  505. pgrsbarlist[handle]->maxrange);
  506. return true;
  507. }
  508. //
  509. // I_GUIDestroyBar
  510. //
  511. void I_GUIDestroyBar(int handle)
  512. {
  513. if (handle<0 || handle>=numofpgrsbars)
  514. return;
  515. if (!pgrsbarlist[handle])
  516. return;
  517. DestroyWindow(pgrsbarlist[handle]->barhwnd);
  518. free(pgrsbarlist[handle]);
  519. pgrsbarlist[handle] = NULL;
  520. }
  521. // ======================== TEXT ========================
  522. //
  523. // I_GUICreateText
  524. //
  525. int I_GUICreateText(guitext_t* gtext)
  526. {
  527. _guitext_t **oldtxtlist;
  528. HWND txthandle;
  529. COLORREF crf;
  530. int i;
  531. if (numoftxts && txtlist)
  532. {
  533. i=0;
  534. while (i<numoftxts && txtlist[i])
  535. i++;
  536. if (i==numoftxts)
  537. {
  538. oldtxtlist = txtlist;
  539. txtlist = malloc(sizeof(_guitext_t*)*(numoftxts+1));
  540. if (!txtlist)
  541. return 0;
  542. memset(txtlist, 0, sizeof(_guitext_t*)*(numoftxts+1));
  543. memcpy(txtlist, oldtxtlist, sizeof(_guitext_t*)*(numoftxts));
  544. free(oldtxtlist);
  545. i = numoftxts;
  546. numoftxts++;
  547. }
  548. }
  549. else
  550. {
  551. txtlist = malloc(sizeof(_guitext_t*));
  552. if (!txtlist)
  553. return 0;
  554. numoftxts = 1;
  555. i = 0;
  556. }
  557. txtlist[i] = malloc(sizeof(_guitext_t));
  558. if (!txtlist[i])
  559. return 0;
  560. txthandle = CreateWindowEx(0, "STATIC",
  561. (LPSTR)gtext->text,
  562. SS_LEFT|WS_CHILD|WS_VISIBLE,
  563. gtext->x,
  564. gtext->y,
  565. gtext->width,
  566. gtext->height,
  567. gtext->parentwindow,
  568. (HMENU) NULL,
  569. gtext->parentinst,
  570. NULL);
  571. if (!txthandle)
  572. {
  573. //
  574. // No need to make the pointer table any smaller - just
  575. // free the text struct
  576. //
  577. free(txtlist[i]);
  578. return 0;
  579. }
  580. // Setup structure
  581. txtlist[i]->txthwnd = txthandle;
  582. return i;
  583. }
  584. //
  585. // I_GUISetText
  586. //
  587. boolean_g I_GUISetText(int handle, const char *str)
  588. {
  589. if (handle<0 || handle>=numoftxts)
  590. return false;
  591. if (!txtlist[handle])
  592. return false;
  593. SendMessage(txtlist[handle]->txthwnd, WM_SETTEXT, 0, (LPARAM)str);
  594. return true;
  595. }
  596. //
  597. // I_GUIDestroyText
  598. //
  599. void I_GUIDestroyText(int handle)
  600. {
  601. if (handle<0 || handle>=numoftxts)
  602. return;
  603. if (!txtlist[handle])
  604. return;
  605. DestroyWindow(txtlist[handle]->txthwnd);
  606. free(txtlist[handle]);
  607. txtlist[handle] = NULL;
  608. }