ui.c 20 KB

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