ui.c 20 KB

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