ui.c 19 KB

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