ui.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  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_TextWidth(
  557. LPCWSTR lpszItemText
  558. )
  559. /*++
  560. Purpose:
  561. Calculate the text width of the given text.
  562. Parameters:
  563. [IN] itemText - Pointer to the text.
  564. Return value:
  565. text width.
  566. --*/
  567. {
  568. size_t l = wcslen(lpszItemText);
  569. int j = 0, w = 0;
  570. for (j = 0; j < l; j++)
  571. {
  572. w += PAL_CharWidth(lpszItemText[j]);
  573. }
  574. return w;
  575. }
  576. INT
  577. PAL_MenuTextMaxWidth(
  578. LPCMENUITEM rgMenuItem,
  579. INT nMenuItem
  580. )
  581. /*++
  582. Purpose:
  583. Calculate the maximal text width of all the menu items in number of full width characters.
  584. Parameters:
  585. [IN] rgMenuItem - Pointer to the menu item array.
  586. [IN] nMenuItem - Number of menu items.
  587. Return value:
  588. Maximal text width.
  589. --*/
  590. {
  591. int i, r = 0;
  592. for (i = 0; i < nMenuItem; i++)
  593. {
  594. LPCWSTR itemText = PAL_GetWord(rgMenuItem[i].wNumWord);
  595. int w = (PAL_TextWidth(itemText) + 8) >> 4;
  596. if (r < w)
  597. {
  598. r = w;
  599. }
  600. }
  601. return r;
  602. }
  603. INT
  604. PAL_WordMaxWidth(
  605. INT nFirstWord,
  606. INT nWordNum
  607. )
  608. /*++
  609. Purpose:
  610. Calculate the maximal text width of a specific range of words in number of full width characters.
  611. Parameters:
  612. [IN] nFirstWord - First index of word.
  613. [IN] nWordNum - Number of words.
  614. Return value:
  615. Maximal text width.
  616. --*/
  617. {
  618. int i, r = 0;
  619. for (i = 0; i < nWordNum; i++)
  620. {
  621. LPCWSTR itemText = PAL_GetWord(nFirstWord + i);
  622. int j = 0, l = wcslen(itemText), w = 0;
  623. for (j = 0; j < l; j++)
  624. {
  625. w += PAL_CharWidth(itemText[j]);
  626. }
  627. w = (w + 8) >> 4;
  628. if (r < w)
  629. {
  630. r = w;
  631. }
  632. }
  633. return r;
  634. }
  635. INT
  636. PAL_WordWidth(
  637. INT nWordIndex
  638. )
  639. /*++
  640. Purpose:
  641. Calculate the text width of a specific word.
  642. Parameters:
  643. [IN] nWordNum - Index of the word.
  644. Return value:
  645. Text width.
  646. --*/
  647. {
  648. LPCWSTR itemText = PAL_GetWord(nWordIndex);
  649. int i, l = wcslen(itemText), w = 0;
  650. for (i = 0; i < l; i++)
  651. {
  652. w += PAL_CharWidth(itemText[i]);
  653. }
  654. return (w + 8) >> 4;
  655. }
  656. LPOBJECTDESC
  657. PAL_LoadObjectDesc(
  658. LPCSTR lpszFileName
  659. )
  660. /*++
  661. Purpose:
  662. Load the object description strings from file.
  663. Parameters:
  664. [IN] lpszFileName - the filename to be loaded.
  665. Return value:
  666. Pointer to loaded data, in linked list form. NULL if unable to load.
  667. --*/
  668. {
  669. FILE *fp;
  670. PAL_LARGE char buf[512];
  671. char *p;
  672. LPOBJECTDESC lpDesc = NULL, pNew = NULL;
  673. unsigned int i;
  674. fp = fopen(lpszFileName, "r");
  675. if (fp == NULL)
  676. {
  677. return NULL;
  678. }
  679. //
  680. // Load the description data
  681. //
  682. while (fgets(buf, 512, fp) != NULL)
  683. {
  684. int wlen,strip_count=2;
  685. p = strchr(buf, '=');
  686. if (p == NULL)
  687. {
  688. continue;
  689. }
  690. *p++ = '\0';
  691. while(strip_count--){
  692. if(p[strlen(p)-1]=='\r') p[strlen(p)-1]='\0';
  693. if(p[strlen(p)-1]=='\n') p[strlen(p)-1]='\0';
  694. }
  695. wlen = PAL_MultiByteToWideChar(p, -1, NULL, 0);
  696. pNew = UTIL_calloc(1, sizeof(OBJECTDESC));
  697. sscanf(buf, "%x", &i);
  698. pNew->wObjectID = i;
  699. pNew->lpDesc = (LPWSTR)UTIL_malloc(wlen * sizeof(WCHAR));
  700. PAL_MultiByteToWideChar(p, -1, pNew->lpDesc, wlen);
  701. pNew->next = lpDesc;
  702. lpDesc = pNew;
  703. }
  704. fclose(fp);
  705. return lpDesc;
  706. }
  707. VOID
  708. PAL_FreeObjectDesc(
  709. LPOBJECTDESC lpObjectDesc
  710. )
  711. /*++
  712. Purpose:
  713. Free the object description data.
  714. Parameters:
  715. [IN] lpObjectDesc - the description data to be freed.
  716. Return value:
  717. None.
  718. --*/
  719. {
  720. LPOBJECTDESC p;
  721. while (lpObjectDesc != NULL)
  722. {
  723. p = lpObjectDesc->next;
  724. free(lpObjectDesc->lpDesc);
  725. free(lpObjectDesc);
  726. lpObjectDesc = p;
  727. }
  728. }
  729. LPCWSTR
  730. PAL_GetObjectDesc(
  731. LPOBJECTDESC lpObjectDesc,
  732. WORD wObjectID
  733. )
  734. /*++
  735. Purpose:
  736. Get the object description string from the linked list.
  737. Parameters:
  738. [IN] lpObjectDesc - the description data linked list.
  739. [IN] wObjectID - the object ID.
  740. Return value:
  741. The description string. NULL if the specified object ID
  742. is not found.
  743. --*/
  744. {
  745. while (lpObjectDesc != NULL)
  746. {
  747. if (lpObjectDesc->wObjectID == wObjectID)
  748. {
  749. return lpObjectDesc->lpDesc;
  750. }
  751. lpObjectDesc = lpObjectDesc->next;
  752. }
  753. return NULL;
  754. }