ui.c 19 KB

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