ui.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. /* -*- mode: c; tab-width: 4; c-basic-offset: 4; c-file-style: "linux" -*- */
  2. //
  3. // Copyright (c) 2009-2011, Wei Mingzhi <whistler_wmz@users.sf.net>.
  4. // Copyright (c) 2011-2017, SDLPAL development team.
  5. // All rights reserved.
  6. //
  7. // This file is part of SDLPAL.
  8. //
  9. // SDLPAL is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU General Public License as published by
  11. // the Free Software Foundation, either version 3 of the License, or
  12. // (at your option) any later version.
  13. //
  14. // This program is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. // GNU General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public License
  20. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. //
  22. #include "main.h"
  23. LPSPRITE gpSpriteUI = NULL;
  24. static LPBOX
  25. PAL_CreateBoxInternal(
  26. const SDL_Rect *rect
  27. )
  28. {
  29. LPBOX lpBox = (LPBOX)calloc(1, sizeof(BOX));
  30. if (lpBox == NULL)
  31. {
  32. return NULL;
  33. }
  34. lpBox->pos = PAL_XY(rect->x, rect->y);
  35. lpBox->lpSavedArea = VIDEO_DuplicateSurface(gpScreen, rect);
  36. lpBox->wHeight = (WORD)rect->w;
  37. lpBox->wWidth = (WORD)rect->h;
  38. if (lpBox->lpSavedArea == NULL)
  39. {
  40. free(lpBox);
  41. return NULL;
  42. }
  43. return lpBox;
  44. }
  45. INT
  46. PAL_InitUI(
  47. VOID
  48. )
  49. /*++
  50. Purpose:
  51. Initialze the UI subsystem.
  52. Parameters:
  53. None.
  54. Return value:
  55. 0 = success, -1 = fail.
  56. --*/
  57. {
  58. int iSize;
  59. //
  60. // Load the UI sprite.
  61. //
  62. iSize = PAL_MKFGetChunkSize(CHUNKNUM_SPRITEUI, gpGlobals->f.fpDATA);
  63. if (iSize < 0)
  64. {
  65. return -1;
  66. }
  67. gpSpriteUI = (LPSPRITE)calloc(1, iSize);
  68. if (gpSpriteUI == NULL)
  69. {
  70. return -1;
  71. }
  72. PAL_MKFReadChunk(gpSpriteUI, iSize, CHUNKNUM_SPRITEUI, gpGlobals->f.fpDATA);
  73. return 0;
  74. }
  75. VOID
  76. PAL_FreeUI(
  77. VOID
  78. )
  79. /*++
  80. Purpose:
  81. Shutdown the UI subsystem.
  82. Parameters:
  83. None.
  84. Return value:
  85. None.
  86. --*/
  87. {
  88. if (gpSpriteUI != NULL)
  89. {
  90. free(gpSpriteUI);
  91. gpSpriteUI = NULL;
  92. }
  93. }
  94. LPBOX
  95. PAL_CreateBox(
  96. PAL_POS pos,
  97. INT nRows,
  98. INT nColumns,
  99. INT iStyle,
  100. BOOL fSaveScreen
  101. )
  102. {
  103. return PAL_CreateBoxWithShadow( pos, nRows, nColumns, iStyle, fSaveScreen, 6 );
  104. }
  105. LPBOX
  106. PAL_CreateBoxWithShadow(
  107. PAL_POS pos,
  108. INT nRows,
  109. INT nColumns,
  110. INT iStyle,
  111. BOOL fSaveScreen,
  112. INT nShadowOffset
  113. )
  114. /*++
  115. Purpose:
  116. Create a box on the screen.
  117. Parameters:
  118. [IN] pos - position of the box.
  119. [IN] nRows - number of rows of the box.
  120. [IN] nColumns - number of columns of the box.
  121. [IN] iStyle - style of the box (0 or 1).
  122. [IN] fSaveScreen - whether save the used screen area or not.
  123. Return value:
  124. Pointer to a BOX structure. NULL if failed.
  125. If fSaveScreen is false, then always returns NULL.
  126. --*/
  127. {
  128. int i, j, x, m, n;
  129. LPCBITMAPRLE rglpBorderBitmap[3][3];
  130. LPBOX lpBox = NULL;
  131. SDL_Rect rect;
  132. //
  133. // Get the bitmaps
  134. //
  135. for (i = 0; i < 3; i++)
  136. {
  137. for (j = 0; j < 3; j++)
  138. {
  139. rglpBorderBitmap[i][j] = PAL_SpriteGetFrame(gpSpriteUI, i * 3 + j + iStyle * 9);
  140. }
  141. }
  142. rect.x = PAL_X(pos);
  143. rect.y = PAL_Y(pos);
  144. rect.w = 0;
  145. rect.h = 0;
  146. //
  147. // Get the total width and total height of the box
  148. //
  149. for (i = 0; i < 3; i++)
  150. {
  151. if (i == 1)
  152. {
  153. rect.w += PAL_RLEGetWidth(rglpBorderBitmap[0][i]) * nColumns;
  154. rect.h += PAL_RLEGetHeight(rglpBorderBitmap[i][0]) * nRows;
  155. }
  156. else
  157. {
  158. rect.w += PAL_RLEGetWidth(rglpBorderBitmap[0][i]);
  159. rect.h += PAL_RLEGetHeight(rglpBorderBitmap[i][0]);
  160. }
  161. }
  162. // Include shadow
  163. rect.w += nShadowOffset;
  164. rect.h += nShadowOffset;
  165. if (fSaveScreen)
  166. {
  167. //
  168. // Save the used part of the screen
  169. //
  170. lpBox = PAL_CreateBoxInternal(&rect);
  171. }
  172. //
  173. // Border takes 2 additional rows and columns...
  174. //
  175. nRows += 2;
  176. nColumns += 2;
  177. //
  178. // Draw the box
  179. //
  180. for (i = 0; i < nRows; i++)
  181. {
  182. x = rect.x;
  183. m = (i == 0) ? 0 : ((i == nRows - 1) ? 2 : 1);
  184. for (j = 0; j < nColumns; j++)
  185. {
  186. n = (j == 0) ? 0 : ((j == nColumns - 1) ? 2 : 1);
  187. PAL_RLEBlitToSurfaceWithShadow(rglpBorderBitmap[m][n], gpScreen, PAL_XY(x+nShadowOffset, rect.y+nShadowOffset),TRUE);
  188. PAL_RLEBlitToSurface(rglpBorderBitmap[m][n], gpScreen, PAL_XY(x, rect.y));
  189. x += PAL_RLEGetWidth(rglpBorderBitmap[m][n]);
  190. }
  191. rect.y += PAL_RLEGetHeight(rglpBorderBitmap[m][0]);
  192. }
  193. return lpBox;
  194. }
  195. LPBOX
  196. PAL_CreateSingleLineBox(
  197. PAL_POS pos,
  198. INT nLen,
  199. BOOL fSaveScreen
  200. )
  201. {
  202. return PAL_CreateSingleLineBoxWithShadow(pos, nLen, fSaveScreen, 6);
  203. }
  204. LPBOX
  205. PAL_CreateSingleLineBoxWithShadow(
  206. PAL_POS pos,
  207. INT nLen,
  208. BOOL fSaveScreen,
  209. INT nShadowOffset
  210. )
  211. /*++
  212. Purpose:
  213. Create a single-line box on the screen.
  214. Parameters:
  215. [IN] pos - position of the box.
  216. [IN] nLen - length of the box.
  217. [IN] fSaveScreen - whether save the used screen area or not.
  218. Return value:
  219. Pointer to a BOX structure. NULL if failed.
  220. If fSaveScreen is false, then always returns NULL.
  221. --*/
  222. {
  223. static const int iNumLeftSprite = 44;
  224. static const int iNumMidSprite = 45;
  225. static const int iNumRightSprite = 46;
  226. LPCBITMAPRLE lpBitmapLeft;
  227. LPCBITMAPRLE lpBitmapMid;
  228. LPCBITMAPRLE lpBitmapRight;
  229. SDL_Rect rect;
  230. LPBOX lpBox = NULL;
  231. int i;
  232. int xSaved;
  233. //
  234. // Get the bitmaps
  235. //
  236. lpBitmapLeft = PAL_SpriteGetFrame(gpSpriteUI, iNumLeftSprite);
  237. lpBitmapMid = PAL_SpriteGetFrame(gpSpriteUI, iNumMidSprite);
  238. lpBitmapRight = PAL_SpriteGetFrame(gpSpriteUI, iNumRightSprite);
  239. rect.x = PAL_X(pos);
  240. rect.y = PAL_Y(pos);
  241. //
  242. // Get the total width and total height of the box
  243. //
  244. rect.w = PAL_RLEGetWidth(lpBitmapLeft) + PAL_RLEGetWidth(lpBitmapRight);
  245. rect.w += PAL_RLEGetWidth(lpBitmapMid) * nLen;
  246. rect.h = PAL_RLEGetHeight(lpBitmapLeft);
  247. // Include shadow
  248. rect.w += nShadowOffset;
  249. rect.h += nShadowOffset;
  250. if (fSaveScreen)
  251. {
  252. //
  253. // Save the used part of the screen
  254. //
  255. lpBox = PAL_CreateBoxInternal(&rect);
  256. }
  257. xSaved = rect.x;
  258. //
  259. // Draw the shadow
  260. //
  261. PAL_RLEBlitToSurfaceWithShadow(lpBitmapLeft, gpScreen, PAL_XY(rect.x+nShadowOffset, rect.y+nShadowOffset), TRUE);
  262. rect.x += PAL_RLEGetWidth(lpBitmapLeft);
  263. for (i = 0; i < nLen; i++)
  264. {
  265. PAL_RLEBlitToSurfaceWithShadow(lpBitmapMid, gpScreen, PAL_XY(rect.x+nShadowOffset, rect.y+nShadowOffset), TRUE);
  266. rect.x += PAL_RLEGetWidth(lpBitmapMid);
  267. }
  268. PAL_RLEBlitToSurfaceWithShadow(lpBitmapRight, gpScreen, PAL_XY(rect.x+nShadowOffset, rect.y+nShadowOffset), TRUE);
  269. rect.x = xSaved;
  270. //
  271. // Draw the box
  272. //
  273. PAL_RLEBlitToSurface(lpBitmapLeft, gpScreen, pos);
  274. rect.x += PAL_RLEGetWidth(lpBitmapLeft);
  275. for (i = 0; i < nLen; i++)
  276. {
  277. PAL_RLEBlitToSurface(lpBitmapMid, gpScreen, PAL_XY(rect.x, rect.y));
  278. rect.x += PAL_RLEGetWidth(lpBitmapMid);
  279. }
  280. PAL_RLEBlitToSurface(lpBitmapRight, gpScreen, PAL_XY(rect.x, rect.y));
  281. return lpBox;
  282. }
  283. VOID
  284. PAL_DeleteBox(
  285. LPBOX lpBox
  286. )
  287. /*++
  288. Purpose:
  289. Delete a box and restore the saved part of the screen.
  290. Parameters:
  291. [IN] lpBox - pointer to the BOX struct.
  292. Return value:
  293. None.
  294. --*/
  295. {
  296. SDL_Rect rect;
  297. //
  298. // Check for NULL pointer.
  299. //
  300. if (lpBox == NULL)
  301. {
  302. return;
  303. }
  304. //
  305. // Restore the saved screen part
  306. //
  307. rect.x = PAL_X(lpBox->pos);
  308. rect.y = PAL_Y(lpBox->pos);
  309. rect.w = lpBox->wWidth;
  310. rect.h = lpBox->wHeight;
  311. VIDEO_CopySurface(lpBox->lpSavedArea, NULL, gpScreen, &rect);
  312. //
  313. // Free the memory used by the box
  314. //
  315. VIDEO_FreeSurface(lpBox->lpSavedArea);
  316. free(lpBox);
  317. }
  318. WORD
  319. PAL_ReadMenu(
  320. LPITEMCHANGED_CALLBACK lpfnMenuItemChanged,
  321. LPCMENUITEM rgMenuItem,
  322. INT nMenuItem,
  323. WORD wDefaultItem,
  324. BYTE bLabelColor
  325. )
  326. /*++
  327. Purpose:
  328. Execute a menu.
  329. Parameters:
  330. [IN] lpfnMenuItemChanged - Callback function which is called when user
  331. changed the current menu item.
  332. [IN] rgMenuItem - Array of the menu items.
  333. [IN] nMenuItem - Number of menu items.
  334. [IN] wDefaultItem - default item index.
  335. [IN] bLabelColor - color of the labels.
  336. Return value:
  337. Return value of the selected menu item. MENUITEM_VALUE_CANCELLED if cancelled.
  338. --*/
  339. {
  340. int i;
  341. WORD wCurrentItem = (wDefaultItem < nMenuItem) ? wDefaultItem : 0;
  342. //
  343. // Draw all the menu texts.
  344. //
  345. for (i = 0; i < nMenuItem; i++)
  346. {
  347. BYTE bColor = bLabelColor;
  348. if (!rgMenuItem[i].fEnabled)
  349. {
  350. if (i == wCurrentItem)
  351. {
  352. bColor = MENUITEM_COLOR_SELECTED_INACTIVE;
  353. }
  354. else
  355. {
  356. bColor = MENUITEM_COLOR_INACTIVE;
  357. }
  358. }
  359. PAL_DrawText(PAL_GetWord(rgMenuItem[i].wNumWord), rgMenuItem[i].pos, bColor, TRUE, TRUE, FALSE);
  360. }
  361. if (lpfnMenuItemChanged != NULL)
  362. {
  363. (*lpfnMenuItemChanged)(rgMenuItem[wDefaultItem].wValue);
  364. }
  365. while (TRUE)
  366. {
  367. PAL_ClearKeyState();
  368. //
  369. // Redraw the selected item if needed.
  370. //
  371. if (rgMenuItem[wCurrentItem].fEnabled)
  372. {
  373. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  374. rgMenuItem[wCurrentItem].pos, MENUITEM_COLOR_SELECTED, FALSE, TRUE, FALSE);
  375. }
  376. PAL_ProcessEvent();
  377. if (g_InputState.dwKeyPress & (kKeyDown | kKeyRight))
  378. {
  379. //
  380. // User pressed the down or right arrow key
  381. //
  382. if (rgMenuItem[wCurrentItem].fEnabled)
  383. {
  384. //
  385. // Dehighlight the unselected item.
  386. //
  387. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  388. rgMenuItem[wCurrentItem].pos, bLabelColor, FALSE, TRUE, FALSE);
  389. }
  390. else
  391. {
  392. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  393. rgMenuItem[wCurrentItem].pos, MENUITEM_COLOR_INACTIVE, FALSE, TRUE, FALSE);
  394. }
  395. wCurrentItem++;
  396. if (wCurrentItem >= nMenuItem)
  397. {
  398. wCurrentItem = 0;
  399. }
  400. //
  401. // Highlight the selected item.
  402. //
  403. if (rgMenuItem[wCurrentItem].fEnabled)
  404. {
  405. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  406. rgMenuItem[wCurrentItem].pos, MENUITEM_COLOR_SELECTED, FALSE, TRUE, FALSE);
  407. }
  408. else
  409. {
  410. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  411. rgMenuItem[wCurrentItem].pos, MENUITEM_COLOR_SELECTED_INACTIVE, FALSE, TRUE, FALSE);
  412. }
  413. if (lpfnMenuItemChanged != NULL)
  414. {
  415. (*lpfnMenuItemChanged)(rgMenuItem[wCurrentItem].wValue);
  416. }
  417. }
  418. else if (g_InputState.dwKeyPress & (kKeyUp | kKeyLeft))
  419. {
  420. //
  421. // User pressed the up or left arrow key
  422. //
  423. if (rgMenuItem[wCurrentItem].fEnabled)
  424. {
  425. //
  426. // Dehighlight the unselected item.
  427. //
  428. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  429. rgMenuItem[wCurrentItem].pos, bLabelColor, FALSE, TRUE, FALSE);
  430. }
  431. else
  432. {
  433. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  434. rgMenuItem[wCurrentItem].pos, MENUITEM_COLOR_INACTIVE, FALSE, TRUE, FALSE);
  435. }
  436. if (wCurrentItem > 0)
  437. {
  438. wCurrentItem--;
  439. }
  440. else
  441. {
  442. wCurrentItem = nMenuItem - 1;
  443. }
  444. //
  445. // Highlight the selected item.
  446. //
  447. if (rgMenuItem[wCurrentItem].fEnabled)
  448. {
  449. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  450. rgMenuItem[wCurrentItem].pos, MENUITEM_COLOR_SELECTED, FALSE, TRUE, FALSE);
  451. }
  452. else
  453. {
  454. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  455. rgMenuItem[wCurrentItem].pos, MENUITEM_COLOR_SELECTED_INACTIVE, FALSE, TRUE, FALSE);
  456. }
  457. if (lpfnMenuItemChanged != NULL)
  458. {
  459. (*lpfnMenuItemChanged)(rgMenuItem[wCurrentItem].wValue);
  460. }
  461. }
  462. else if (g_InputState.dwKeyPress & kKeyMenu)
  463. {
  464. //
  465. // User cancelled
  466. //
  467. if (rgMenuItem[wCurrentItem].fEnabled)
  468. {
  469. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  470. rgMenuItem[wCurrentItem].pos, bLabelColor, FALSE, TRUE, FALSE);
  471. }
  472. else
  473. {
  474. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  475. rgMenuItem[wCurrentItem].pos, MENUITEM_COLOR_INACTIVE, FALSE, TRUE, FALSE);
  476. }
  477. break;
  478. }
  479. else if (g_InputState.dwKeyPress & kKeySearch)
  480. {
  481. //
  482. // User pressed Enter
  483. //
  484. if (rgMenuItem[wCurrentItem].fEnabled)
  485. {
  486. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  487. rgMenuItem[wCurrentItem].pos, MENUITEM_COLOR_CONFIRMED, FALSE, TRUE, FALSE);
  488. return rgMenuItem[wCurrentItem].wValue;
  489. }
  490. }
  491. //
  492. // Use delay function to avoid high CPU usage.
  493. //
  494. SDL_Delay(50);
  495. }
  496. return MENUITEM_VALUE_CANCELLED;
  497. }
  498. VOID
  499. PAL_DrawNumber(
  500. UINT iNum,
  501. UINT nLength,
  502. PAL_POS pos,
  503. NUMCOLOR color,
  504. NUMALIGN align
  505. )
  506. /*++
  507. Purpose:
  508. Draw the specified number with the bitmaps in the UI sprite.
  509. Parameters:
  510. [IN] iNum - the number to be drawn.
  511. [IN] nLength - max. length of the number.
  512. [IN] pos - position on the screen.
  513. [IN] color - color of the number (yellow or blue).
  514. [IN] align - align mode of the number.
  515. Return value:
  516. None.
  517. --*/
  518. {
  519. UINT nActualLength, i;
  520. int x, y;
  521. LPCBITMAPRLE rglpBitmap[10];
  522. //
  523. // Get the bitmaps. Blue starts from 29, Cyan from 56, Yellow from 19.
  524. //
  525. x = (color == kNumColorBlue) ? 29 : ((color == kNumColorCyan) ? 56 : 19);
  526. for (i = 0; i < 10; i++)
  527. {
  528. rglpBitmap[i] = PAL_SpriteGetFrame(gpSpriteUI, (UINT)x + i);
  529. }
  530. i = iNum;
  531. nActualLength = 0;
  532. //
  533. // Calculate the actual length of the number.
  534. //
  535. while (i > 0)
  536. {
  537. i /= 10;
  538. nActualLength++;
  539. }
  540. if (nActualLength > nLength)
  541. {
  542. nActualLength = nLength;
  543. }
  544. else if (nActualLength == 0)
  545. {
  546. nActualLength = 1;
  547. }
  548. x = PAL_X(pos) - 6;
  549. y = PAL_Y(pos);
  550. switch (align)
  551. {
  552. case kNumAlignLeft:
  553. x += 6 * nActualLength;
  554. break;
  555. case kNumAlignMid:
  556. x += 3 * (nLength + nActualLength);
  557. break;
  558. case kNumAlignRight:
  559. x += 6 * nLength;
  560. break;
  561. }
  562. //
  563. // Draw the number.
  564. //
  565. while (nActualLength-- > 0)
  566. {
  567. PAL_RLEBlitToSurface(rglpBitmap[iNum % 10], gpScreen, PAL_XY(x, y));
  568. x -= 6;
  569. iNum /= 10;
  570. }
  571. }
  572. /*++
  573. Purpose:
  574. Calculate the text width of the given text.
  575. Parameters:
  576. [IN] itemText - Pointer to the text.
  577. Return value:
  578. text width.
  579. --*/
  580. INT
  581. PAL_TextWidth(
  582. LPCWSTR lpszItemText
  583. )
  584. {
  585. size_t l = wcslen(lpszItemText), j = 0, w = 0;
  586. for (j = 0; j < l; j++)
  587. {
  588. w += PAL_CharWidth(lpszItemText[j]);
  589. }
  590. return w;
  591. }
  592. INT
  593. PAL_MenuTextMaxWidth(
  594. LPCMENUITEM rgMenuItem,
  595. INT nMenuItem
  596. )
  597. /*++
  598. Purpose:
  599. Calculate the maximal text width of all the menu items in number of full width characters.
  600. Parameters:
  601. [IN] rgMenuItem - Pointer to the menu item array.
  602. [IN] nMenuItem - Number of menu items.
  603. Return value:
  604. Maximal text width.
  605. --*/
  606. {
  607. int i, r = 0;
  608. for (i = 0; i < nMenuItem; i++)
  609. {
  610. LPCWSTR itemText = PAL_GetWord(rgMenuItem[i].wNumWord);
  611. int w = (PAL_TextWidth(itemText) + 8) >> 4;
  612. if (r < w)
  613. {
  614. r = w;
  615. }
  616. }
  617. return r;
  618. }
  619. INT
  620. PAL_WordMaxWidth(
  621. INT nFirstWord,
  622. INT nWordNum
  623. )
  624. /*++
  625. Purpose:
  626. Calculate the maximal text width of a specific range of words in number of full width characters.
  627. Parameters:
  628. [IN] nFirstWord - First index of word.
  629. [IN] nWordNum - Number of words.
  630. Return value:
  631. Maximal text width.
  632. --*/
  633. {
  634. int i, r = 0;
  635. for (i = 0; i < nWordNum; i++)
  636. {
  637. LPCWSTR itemText = PAL_GetWord(nFirstWord + i);
  638. int j = 0, l = wcslen(itemText), w = 0;
  639. for (j = 0; j < l; j++)
  640. {
  641. w += PAL_CharWidth(itemText[j]);
  642. }
  643. w = (w + 8) >> 4;
  644. if (r < w)
  645. {
  646. r = w;
  647. }
  648. }
  649. return r;
  650. }
  651. INT
  652. PAL_WordWidth(
  653. INT nWordIndex
  654. )
  655. /*++
  656. Purpose:
  657. Calculate the text width of a specific word.
  658. Parameters:
  659. [IN] nWordNum - Index of the word.
  660. Return value:
  661. Text width.
  662. --*/
  663. {
  664. LPCWSTR itemText = PAL_GetWord(nWordIndex);
  665. int i, l = wcslen(itemText), w = 0;
  666. for (i = 0; i < l; i++)
  667. {
  668. w += PAL_CharWidth(itemText[i]);
  669. }
  670. return (w + 8) >> 4;
  671. }
  672. LPOBJECTDESC
  673. PAL_LoadObjectDesc(
  674. LPCSTR lpszFileName
  675. )
  676. /*++
  677. Purpose:
  678. Load the object description strings from file.
  679. Parameters:
  680. [IN] lpszFileName - the filename to be loaded.
  681. Return value:
  682. Pointer to loaded data, in linked list form. NULL if unable to load.
  683. --*/
  684. {
  685. FILE *fp;
  686. PAL_LARGE char buf[512];
  687. char *p;
  688. LPOBJECTDESC lpDesc = NULL, pNew = NULL;
  689. unsigned int i;
  690. fp = fopen(lpszFileName, "r");
  691. if (fp == NULL)
  692. {
  693. return NULL;
  694. }
  695. //
  696. // Load the description data
  697. //
  698. while (fgets(buf, 512, fp) != NULL)
  699. {
  700. int wlen,strip_count=2;
  701. p = strchr(buf, '=');
  702. if (p == NULL)
  703. {
  704. continue;
  705. }
  706. *p++ = '\0';
  707. while(strip_count--){
  708. if(p[strlen(p)-1]=='\r') p[strlen(p)-1]='\0';
  709. if(p[strlen(p)-1]=='\n') p[strlen(p)-1]='\0';
  710. }
  711. wlen = PAL_MultiByteToWideChar(p, -1, NULL, 0);
  712. pNew = UTIL_calloc(1, sizeof(OBJECTDESC));
  713. sscanf(buf, "%x", &i);
  714. pNew->wObjectID = i;
  715. pNew->lpDesc = (LPWSTR)UTIL_malloc(wlen * sizeof(WCHAR));
  716. PAL_MultiByteToWideChar(p, -1, pNew->lpDesc, wlen);
  717. pNew->next = lpDesc;
  718. lpDesc = pNew;
  719. }
  720. fclose(fp);
  721. return lpDesc;
  722. }
  723. VOID
  724. PAL_FreeObjectDesc(
  725. LPOBJECTDESC lpObjectDesc
  726. )
  727. /*++
  728. Purpose:
  729. Free the object description data.
  730. Parameters:
  731. [IN] lpObjectDesc - the description data to be freed.
  732. Return value:
  733. None.
  734. --*/
  735. {
  736. LPOBJECTDESC p;
  737. while (lpObjectDesc != NULL)
  738. {
  739. p = lpObjectDesc->next;
  740. free(lpObjectDesc->lpDesc);
  741. free(lpObjectDesc);
  742. lpObjectDesc = p;
  743. }
  744. }
  745. LPCWSTR
  746. PAL_GetObjectDesc(
  747. LPOBJECTDESC lpObjectDesc,
  748. WORD wObjectID
  749. )
  750. /*++
  751. Purpose:
  752. Get the object description string from the linked list.
  753. Parameters:
  754. [IN] lpObjectDesc - the description data linked list.
  755. [IN] wObjectID - the object ID.
  756. Return value:
  757. The description string. NULL if the specified object ID
  758. is not found.
  759. --*/
  760. {
  761. while (lpObjectDesc != NULL)
  762. {
  763. if (lpObjectDesc->wObjectID == wObjectID)
  764. {
  765. return lpObjectDesc->lpDesc;
  766. }
  767. lpObjectDesc = lpObjectDesc->next;
  768. }
  769. return NULL;
  770. }