ui.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  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. LPMENUITEM 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,
  343. bColor, TRUE, TRUE);
  344. }
  345. if (lpfnMenuItemChanged != NULL)
  346. {
  347. (*lpfnMenuItemChanged)(rgMenuItem[wDefaultItem].wValue);
  348. }
  349. while (TRUE)
  350. {
  351. PAL_ClearKeyState();
  352. //
  353. // Redraw the selected item if needed.
  354. //
  355. if (rgMenuItem[wCurrentItem].fEnabled)
  356. {
  357. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  358. rgMenuItem[wCurrentItem].pos, MENUITEM_COLOR_SELECTED, FALSE, TRUE);
  359. }
  360. PAL_ProcessEvent();
  361. if (g_InputState.dwKeyPress & (kKeyDown | kKeyRight))
  362. {
  363. //
  364. // User pressed the down or right arrow key
  365. //
  366. if (rgMenuItem[wCurrentItem].fEnabled)
  367. {
  368. //
  369. // Dehighlight the unselected item.
  370. //
  371. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  372. rgMenuItem[wCurrentItem].pos, bLabelColor, FALSE, TRUE);
  373. }
  374. else
  375. {
  376. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  377. rgMenuItem[wCurrentItem].pos, MENUITEM_COLOR_INACTIVE, FALSE, TRUE);
  378. }
  379. wCurrentItem++;
  380. if (wCurrentItem >= nMenuItem)
  381. {
  382. wCurrentItem = 0;
  383. }
  384. //
  385. // Highlight the selected item.
  386. //
  387. if (rgMenuItem[wCurrentItem].fEnabled)
  388. {
  389. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  390. rgMenuItem[wCurrentItem].pos, MENUITEM_COLOR_SELECTED, FALSE, TRUE);
  391. }
  392. else
  393. {
  394. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  395. rgMenuItem[wCurrentItem].pos, MENUITEM_COLOR_SELECTED_INACTIVE, FALSE, TRUE);
  396. }
  397. if (lpfnMenuItemChanged != NULL)
  398. {
  399. (*lpfnMenuItemChanged)(rgMenuItem[wCurrentItem].wValue);
  400. }
  401. }
  402. else if (g_InputState.dwKeyPress & (kKeyUp | kKeyLeft))
  403. {
  404. //
  405. // User pressed the up or left arrow key
  406. //
  407. if (rgMenuItem[wCurrentItem].fEnabled)
  408. {
  409. //
  410. // Dehighlight the unselected item.
  411. //
  412. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  413. rgMenuItem[wCurrentItem].pos, bLabelColor, FALSE, TRUE);
  414. }
  415. else
  416. {
  417. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  418. rgMenuItem[wCurrentItem].pos, MENUITEM_COLOR_INACTIVE, FALSE, TRUE);
  419. }
  420. if (wCurrentItem > 0)
  421. {
  422. wCurrentItem--;
  423. }
  424. else
  425. {
  426. wCurrentItem = nMenuItem - 1;
  427. }
  428. //
  429. // Highlight the selected item.
  430. //
  431. if (rgMenuItem[wCurrentItem].fEnabled)
  432. {
  433. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  434. rgMenuItem[wCurrentItem].pos, MENUITEM_COLOR_SELECTED, FALSE, TRUE);
  435. }
  436. else
  437. {
  438. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  439. rgMenuItem[wCurrentItem].pos, MENUITEM_COLOR_SELECTED_INACTIVE, FALSE, TRUE);
  440. }
  441. if (lpfnMenuItemChanged != NULL)
  442. {
  443. (*lpfnMenuItemChanged)(rgMenuItem[wCurrentItem].wValue);
  444. }
  445. }
  446. else if (g_InputState.dwKeyPress & kKeyMenu)
  447. {
  448. //
  449. // User cancelled
  450. //
  451. if (rgMenuItem[wCurrentItem].fEnabled)
  452. {
  453. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  454. rgMenuItem[wCurrentItem].pos, bLabelColor, FALSE, TRUE);
  455. }
  456. else
  457. {
  458. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  459. rgMenuItem[wCurrentItem].pos, MENUITEM_COLOR_INACTIVE, FALSE, TRUE);
  460. }
  461. break;
  462. }
  463. else if (g_InputState.dwKeyPress & kKeySearch)
  464. {
  465. //
  466. // User pressed Enter
  467. //
  468. if (rgMenuItem[wCurrentItem].fEnabled)
  469. {
  470. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  471. rgMenuItem[wCurrentItem].pos, MENUITEM_COLOR_CONFIRMED, FALSE, TRUE);
  472. return rgMenuItem[wCurrentItem].wValue;
  473. }
  474. }
  475. //
  476. // Use delay function to avoid high CPU usage.
  477. //
  478. SDL_Delay(50);
  479. }
  480. return MENUITEM_VALUE_CANCELLED;
  481. }
  482. VOID
  483. PAL_DrawNumber(
  484. UINT iNum,
  485. UINT nLength,
  486. PAL_POS pos,
  487. NUMCOLOR color,
  488. NUMALIGN align
  489. )
  490. /*++
  491. Purpose:
  492. Draw the specified number with the bitmaps in the UI sprite.
  493. Parameters:
  494. [IN] iNum - the number to be drawn.
  495. [IN] nLength - max. length of the number.
  496. [IN] pos - position on the screen.
  497. [IN] color - color of the number (yellow or blue).
  498. [IN] align - align mode of the number.
  499. Return value:
  500. None.
  501. --*/
  502. {
  503. UINT nActualLength, i;
  504. int x, y;
  505. LPCBITMAPRLE rglpBitmap[10];
  506. //
  507. // Get the bitmaps. Blue starts from 29, Cyan from 56, Yellow from 19.
  508. //
  509. x = (color == kNumColorBlue) ? 29 : ((color == kNumColorCyan) ? 56 : 19);
  510. for (i = 0; i < 10; i++)
  511. {
  512. rglpBitmap[i] = PAL_SpriteGetFrame(gpSpriteUI, (UINT)x + i);
  513. }
  514. i = iNum;
  515. nActualLength = 0;
  516. //
  517. // Calculate the actual length of the number.
  518. //
  519. while (i > 0)
  520. {
  521. i /= 10;
  522. nActualLength++;
  523. }
  524. if (nActualLength > nLength)
  525. {
  526. nActualLength = nLength;
  527. }
  528. else if (nActualLength == 0)
  529. {
  530. nActualLength = 1;
  531. }
  532. x = PAL_X(pos) - 6;
  533. y = PAL_Y(pos);
  534. switch (align)
  535. {
  536. case kNumAlignLeft:
  537. x += 6 * nActualLength;
  538. break;
  539. case kNumAlignMid:
  540. x += 3 * (nLength + nActualLength);
  541. break;
  542. case kNumAlignRight:
  543. x += 6 * nLength;
  544. break;
  545. }
  546. //
  547. // Draw the number.
  548. //
  549. while (nActualLength-- > 0)
  550. {
  551. PAL_RLEBlitToSurface(rglpBitmap[iNum % 10], gpScreen, PAL_XY(x, y));
  552. x -= 6;
  553. iNum /= 10;
  554. }
  555. }
  556. INT
  557. PAL_MenuTextMaxWidth(
  558. LPMENUITEM rgMenuItem,
  559. INT nMenuItem
  560. )
  561. /*++
  562. Purpose:
  563. Calculate the maximal text width of all the menu items in number of full width characters.
  564. Parameters:
  565. [IN] rgMenuItem - Pointer to the menu item array.
  566. [IN] nMenuItem - Number of menu items.
  567. Return value:
  568. Maximal text width.
  569. --*/
  570. {
  571. int i, r = 0;
  572. for (i = 0; i < nMenuItem; i++)
  573. {
  574. LPCWSTR itemText = PAL_GetWord(rgMenuItem[i].wNumWord);
  575. int j = 0, l = wcslen(itemText), w = 0;
  576. for (j = 0; j < l; j++)
  577. {
  578. w += PAL_CharWidth(itemText[j]);
  579. }
  580. w = (w + 8) >> 4;
  581. if (r < w)
  582. {
  583. r = w;
  584. }
  585. }
  586. return r;
  587. }
  588. INT
  589. PAL_WordMaxWidth(
  590. INT nFirstWord,
  591. INT nWordNum
  592. )
  593. /*++
  594. Purpose:
  595. Calculate the maximal text width of a specific range of words in number of full width characters.
  596. Parameters:
  597. [IN] nFirstWord - First index of word.
  598. [IN] nWordNum - Number of words.
  599. Return value:
  600. Maximal text width.
  601. --*/
  602. {
  603. int i, r = 0;
  604. for (i = 0; i < nWordNum; i++)
  605. {
  606. LPCWSTR itemText = PAL_GetWord(nFirstWord + i);
  607. int j = 0, l = wcslen(itemText), w = 0;
  608. for (j = 0; j < l; j++)
  609. {
  610. w += PAL_CharWidth(itemText[j]);
  611. }
  612. w = (w + 8) >> 4;
  613. if (r < w)
  614. {
  615. r = w;
  616. }
  617. }
  618. return r;
  619. }
  620. INT
  621. PAL_WordWidth(
  622. INT nWordIndex
  623. )
  624. /*++
  625. Purpose:
  626. Calculate the text width of a specific word.
  627. Parameters:
  628. [IN] nWordNum - Index of the word.
  629. Return value:
  630. Text width.
  631. --*/
  632. {
  633. LPCWSTR itemText = PAL_GetWord(nWordIndex);
  634. int i, l = wcslen(itemText), w = 0;
  635. for (i = 0; i < l; i++)
  636. {
  637. w += PAL_CharWidth(itemText[i]);
  638. }
  639. return (w + 8) >> 4;
  640. }
  641. LPOBJECTDESC
  642. PAL_LoadObjectDesc(
  643. LPCSTR lpszFileName
  644. )
  645. /*++
  646. Purpose:
  647. Load the object description strings from file.
  648. Parameters:
  649. [IN] lpszFileName - the filename to be loaded.
  650. Return value:
  651. Pointer to loaded data, in linked list form. NULL if unable to load.
  652. --*/
  653. {
  654. FILE *fp;
  655. PAL_LARGE char buf[512];
  656. char *p;
  657. LPOBJECTDESC lpDesc = NULL, pNew = NULL;
  658. unsigned int i;
  659. fp = fopen(lpszFileName, "r");
  660. if (fp == NULL)
  661. {
  662. return NULL;
  663. }
  664. //
  665. // Load the description data
  666. //
  667. while (fgets(buf, 512, fp) != NULL)
  668. {
  669. # ifdef PAL_UNICODE
  670. int wlen;
  671. # endif
  672. p = strchr(buf, '=');
  673. if (p == NULL)
  674. {
  675. continue;
  676. }
  677. *p = '\0';
  678. p++;
  679. # ifdef PAL_UNICODE
  680. wlen = PAL_MultiByteToWideChar(p, -1, NULL, 0);
  681. # endif
  682. pNew = UTIL_calloc(1, sizeof(OBJECTDESC));
  683. sscanf(buf, "%x", &i);
  684. pNew->wObjectID = i;
  685. # ifdef PAL_UNICODE
  686. pNew->lpDesc = (LPWSTR)malloc(wlen * sizeof(WCHAR));
  687. PAL_MultiByteToWideChar(p, -1, pNew->lpDesc, wlen);
  688. # else
  689. pNew->lpDesc = strdup(p);
  690. # endif
  691. pNew->next = lpDesc;
  692. lpDesc = pNew;
  693. }
  694. fclose(fp);
  695. return lpDesc;
  696. }
  697. VOID
  698. PAL_FreeObjectDesc(
  699. LPOBJECTDESC lpObjectDesc
  700. )
  701. /*++
  702. Purpose:
  703. Free the object description data.
  704. Parameters:
  705. [IN] lpObjectDesc - the description data to be freed.
  706. Return value:
  707. None.
  708. --*/
  709. {
  710. LPOBJECTDESC p;
  711. while (lpObjectDesc != NULL)
  712. {
  713. p = lpObjectDesc->next;
  714. free(lpObjectDesc->lpDesc);
  715. free(lpObjectDesc);
  716. lpObjectDesc = p;
  717. }
  718. }
  719. #ifdef PAL_UNICODE
  720. LPCWSTR
  721. #else
  722. LPCSTR
  723. #endif
  724. PAL_GetObjectDesc(
  725. LPOBJECTDESC lpObjectDesc,
  726. WORD wObjectID
  727. )
  728. /*++
  729. Purpose:
  730. Get the object description string from the linked list.
  731. Parameters:
  732. [IN] lpObjectDesc - the description data linked list.
  733. [IN] wObjectID - the object ID.
  734. Return value:
  735. The description string. NULL if the specified object ID
  736. is not found.
  737. --*/
  738. {
  739. while (lpObjectDesc != NULL)
  740. {
  741. if (lpObjectDesc->wObjectID == wObjectID)
  742. {
  743. return lpObjectDesc->lpDesc;
  744. }
  745. lpObjectDesc = lpObjectDesc->next;
  746. }
  747. return NULL;
  748. }