ICONLIST.CPP 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. #ifdef WOLAPI_INTEGRATION
  15. // Iconlist.cpp - created by ajw 07/07/98
  16. // IconListClass is ListClass plus the option to include an icon on each line entry,
  17. // the option to have the class maintain its own copies of strings passed to it
  18. // for display, and the option to limit the maximum number of these strings that are
  19. // kept (entries are removed from the top when this maximum is reached).
  20. // Also added: multiple item selection capability. Note that the old selection code
  21. // runs as normal, but it simply not used when it comes time to display.
  22. // Also added: if mem. allocation is being done by this, the ability to break new items
  23. // into multiple lines of text is enabled.
  24. // Also added: extra data can be invisibly stored with each item, if memory allocation is
  25. // being done by this.
  26. // Extra data included 3 item preceding icons, 1 fixed position icon, an extra string,
  27. // an extra void pointer, and a color remapping value.
  28. #include "iconlist.h"
  29. #include "dibapi.h"
  30. int Format_Window_String_New( const char* string, int maxlinelen, int& width, int& height, char* szReturn, int iExtraChars );
  31. void CC_Draw_DIB( const char* pDIB, int xDest, int yDest, int iWidth, WindowNumberType window );
  32. //***********************************************************************************************
  33. IconListClass::IconListClass( int id, int x, int y, int w, int h, TextPrintType flags, void const * up, void const * down,
  34. bool bResponsibleForStringAlloc, int iSelectionType, int iMaxItemsSaved ) :
  35. ListClass( id, x, y, w, h, flags, up, down )
  36. {
  37. // If bResponsibleForStringAlloc, COPIES of strings are stored in the list. Deletion is
  38. // handled by this class. Icons are different - the caller is responsible for what's on
  39. // the other end of the pointer.
  40. bDoAlloc = bResponsibleForStringAlloc;
  41. // iSelectionType = 0 for no selection shown, 1 for normal ListClass selection, 2 for n multiple selections
  42. if( iSelectionType < 0 || iSelectionType > 2 ) iSelectionType = 1;
  43. iSelectType = iSelectionType;
  44. // If iMaxItemsSaved is 0, there is no limit to the number of text lines. The list can grow forever.
  45. // Otherwise items are deleted from the head of the list when the maximum is passed.
  46. // iMaxItemsSaved only applies when bResponsibleForStringAlloc.
  47. iMaxItems = iMaxItemsSaved;
  48. }
  49. //***********************************************************************************************
  50. IconListClass::~IconListClass( void )
  51. {
  52. // Delete the IconList_ItemExtras structs created to hold extra info on each item.
  53. for( int i = 0; i < ExtrasList.Count(); i++ )
  54. delete (IconList_ItemExtras*)ExtrasList[ i ];
  55. if( bDoAlloc )
  56. {
  57. // Delete all alloc'ed strings.
  58. for( int i = 0; i < List.Count(); i++ )
  59. delete [] (char*)List[i];
  60. }
  61. }
  62. /***********************************************************************************************
  63. * IconListClass::Add_Item -- Adds an item to the list box. *
  64. * *
  65. * This will add the specified string to the list box. The string is added to the end *
  66. * of the list. *
  67. * *
  68. * INPUT: text -- Pointer to the string to add to the list box. *
  69. * pIcon -- Pointer to the shape to add.
  70. * IconKind -- Indicates what type of image pIcon points to.
  71. * szExtraDataString -- Extra string data that gets copied and stored along with item.
  72. * szExtraDataPtr -- Extra data that gets stored along with item.
  73. * pColorRemap -- Points to a color remapping used when drawing the item.
  74. *
  75. * OUTPUT: Returns new item index. *
  76. * WARNINGS: none *
  77. * HISTORY: 07/07/1998 ajw : Created. *
  78. *=============================================================================================*/
  79. int IconListClass::Add_Item(char const * text)
  80. {
  81. return Add_Item( text, NULL, NULL, ICON_SHAPE );
  82. }
  83. int IconListClass::Add_Item( const char* text, const char* szHelp,
  84. void* pIcon0, ICONKIND IconKind0, const char* szExtraDataString /* = NULL */,
  85. void* pvExtraDataPtr /* = NULL */, RemapControlType* pColorRemap /* = NULL */,
  86. void* pIcon1 /* = NULL */, ICONKIND IconKind1 /* = ICON_SHAPE */,
  87. void* pIcon2 /* = NULL */, ICONKIND IconKind2 /* = ICON_SHAPE */,
  88. void* pFixedIcon /* = NULL */, ICONKIND FixedIconKind /* = ICON_SHAPE */, int iXFixedIcon /* = 0 */, int iYFixedIcon /* = 0 */, int iFixedIconWidth /* = -1 */ )
  89. {
  90. if( text )
  91. {
  92. if( bDoAlloc )
  93. {
  94. int iRetVal;
  95. char* szText = new char[ strlen( text ) + 51 ]; // 50 extra chars added for line breaks later.
  96. strcpy( szText, text );
  97. int iWidthMax, iHeight;
  98. // Stupid usage of globals for font stuff... <grumble>
  99. if( TextFlags == TPF_TYPE )
  100. {
  101. void* pFontBefore = Set_Font( TypeFontPtr );
  102. DWORD FontXSpacingBefore = FontXSpacing;
  103. FontXSpacing = -2;
  104. int iWidthToClipAt = IsScrollActive ? Width : Width - UpGadget.Width;
  105. // This call will place '\r's in the string where line breaks should occur.
  106. Format_Window_String_New( text, iWidthToClipAt, iWidthMax, iHeight, szText, 50 );
  107. Set_Font( pFontBefore );
  108. FontXSpacing = FontXSpacingBefore; // Just in case it matters... Doubt it.
  109. }
  110. else
  111. {
  112. // Currently never called. Test well if you use IconList with a font other than TPF_TYPE,
  113. // as the character spacing globals get set weirdly, I've found.
  114. int iWidthToClipAt = IsScrollActive ? Width : Width - UpGadget.Width;
  115. // This call will place '\r's in the string where line breaks should occur.
  116. Format_Window_String_New( text, iWidthToClipAt, iWidthMax, iHeight, szText, 50 );
  117. }
  118. // Each break character causes a line to be added to list.
  119. char szBreakchars[] = "\r\n\v\f";
  120. char* szToken;
  121. char* szNextChar = szText;
  122. szToken = strtok( szText, szBreakchars );
  123. while( szToken )
  124. {
  125. while( szNextChar < szToken )
  126. {
  127. // We expected szToken to begin at szNextChar. Since it doesn't, extra break
  128. // characters must have been removed by strtok as they were adjacent. We want
  129. // a line break for every break character, so add lines for each space that
  130. // szNextChar is off by.
  131. szNextChar++;
  132. Add_Item_Detail( " ", szHelp, pIcon0, IconKind0, szExtraDataString, pvExtraDataPtr, pColorRemap, pIcon1, IconKind1, pIcon2, IconKind2, pFixedIcon, FixedIconKind, iXFixedIcon, iYFixedIcon, iFixedIconWidth );
  133. }
  134. iRetVal = Add_Item_Detail( szToken, szHelp, pIcon0, IconKind0, szExtraDataString, pvExtraDataPtr, pColorRemap, pIcon1, IconKind1, pIcon2, IconKind2, pFixedIcon, FixedIconKind, iXFixedIcon, iYFixedIcon, iFixedIconWidth );
  135. // Expect next token two chars after the end of this one.
  136. szNextChar = szToken + strlen( szToken ) + 1;
  137. // Get next token.
  138. szToken = strtok( NULL, szBreakchars );
  139. }
  140. delete [] szText;
  141. return iRetVal; // Last value returned by ListClass::Add_Item
  142. }
  143. else
  144. {
  145. // Add one item to list.
  146. IconList_ItemExtras* pItemExtra = new IconList_ItemExtras;
  147. pItemExtra->bMultiSelected = false;
  148. pItemExtra->pIcon[0] = pIcon0;
  149. pItemExtra->IconKind[0] = IconKind0;
  150. pItemExtra->pIcon[1] = pIcon1;
  151. pItemExtra->IconKind[1] = IconKind1;
  152. pItemExtra->pIcon[2] = pIcon2;
  153. pItemExtra->IconKind[2] = IconKind2;
  154. pItemExtra->FixedIcon.pIcon = pFixedIcon;
  155. pItemExtra->FixedIcon.IconKind = FixedIconKind;
  156. pItemExtra->FixedIcon.xOffset = iXFixedIcon;
  157. pItemExtra->FixedIcon.yOffset = iYFixedIcon;
  158. pItemExtra->FixedIcon.iWidth = iFixedIconWidth;
  159. pItemExtra->pvExtraData = pvExtraDataPtr;
  160. pItemExtra->pColorRemap = pColorRemap;
  161. if( szHelp )
  162. {
  163. // Copy help into new help string.
  164. pItemExtra->szHelp = new char[ strlen( szHelp ) + 1 ];
  165. strcpy( pItemExtra->szHelp, szHelp );
  166. }
  167. if( szExtraDataString )
  168. {
  169. // Copy special data string into new extradata string.
  170. pItemExtra->szExtraData = new char[ strlen( szExtraDataString ) + 1 ];
  171. strcpy( pItemExtra->szExtraData, szExtraDataString );
  172. }
  173. ExtrasList.Add( pItemExtra );
  174. return ListClass::Add_Item( text );
  175. }
  176. }
  177. else
  178. {
  179. // (no text for new item)
  180. if( pIcon0 || pIcon1 || pIcon2 )
  181. {
  182. // Note: Cannot add an entry without text unless string allocation is being handled by me.
  183. // Otherwise, because we want the icon to show up, create a blank entry for the ListClass.
  184. if( bDoAlloc )
  185. {
  186. IconList_ItemExtras* pItemExtra = new IconList_ItemExtras;
  187. pItemExtra->bMultiSelected = false;
  188. pItemExtra->pIcon[0] = pIcon0;
  189. pItemExtra->IconKind[0] = IconKind0;
  190. pItemExtra->pIcon[1] = pIcon1;
  191. pItemExtra->IconKind[1] = IconKind1;
  192. pItemExtra->pIcon[2] = pIcon2;
  193. pItemExtra->IconKind[2] = IconKind2;
  194. pItemExtra->FixedIcon.pIcon = pFixedIcon;
  195. pItemExtra->FixedIcon.IconKind = FixedIconKind;
  196. pItemExtra->FixedIcon.xOffset = iXFixedIcon;
  197. pItemExtra->FixedIcon.yOffset = iYFixedIcon;
  198. pItemExtra->FixedIcon.iWidth = iFixedIconWidth;
  199. pItemExtra->pvExtraData = pvExtraDataPtr;
  200. pItemExtra->pColorRemap = pColorRemap;
  201. if( szHelp )
  202. {
  203. // Copy help into new help string.
  204. pItemExtra->szHelp = new char[ strlen( szHelp ) + 1 ];
  205. strcpy( pItemExtra->szHelp, szHelp );
  206. }
  207. if( szExtraDataString )
  208. {
  209. // Copy special data string into new extradata string.
  210. pItemExtra->szExtraData = new char[ strlen( szExtraDataString ) + 1 ];
  211. strcpy( pItemExtra->szExtraData, szExtraDataString );
  212. }
  213. ExtrasList.Add( pItemExtra );
  214. if( iMaxItems && List.Count() == iMaxItems )
  215. {
  216. // Delete head of list.
  217. Remove_Item( 0 );
  218. }
  219. // Create new string, essentially blank.
  220. char* szText = new char[2];
  221. strcpy( szText, " " );
  222. return ListClass::Add_Item( szText );
  223. }
  224. else
  225. // Cannot add entry, as text is blank and ListClass::Add_Item will do nothing.
  226. // The Icon we want will not show up.
  227. return List.Count() - 1;
  228. }
  229. else
  230. return ListClass::Add_Item( text );
  231. }
  232. }
  233. //***********************************************************************************************
  234. int IconListClass::Add_Item_Detail( const char* szToken, const char* szHelp,
  235. void* pIcon0, ICONKIND IconKind0, const char* szExtraDataString,
  236. void* pvExtraData, RemapControlType* pColorRemap,
  237. void* pIcon1, ICONKIND IconKind1,
  238. void* pIcon2, ICONKIND IconKind2,
  239. void* pFixedIcon, ICONKIND FixedIconKind, int iXFixedIcon, int iYFixedIcon, int iFixedIconWidth )
  240. {
  241. // Broken out of above function as it is repeated.
  242. // Add one item to list.
  243. // Too many entries?
  244. if( iMaxItems && List.Count() == iMaxItems )
  245. {
  246. // Delete head of list.
  247. Remove_Item( 0 );
  248. }
  249. // Create icon entry.
  250. IconList_ItemExtras* pItemExtra = new IconList_ItemExtras;
  251. pItemExtra->bMultiSelected = false;
  252. pItemExtra->pIcon[0] = pIcon0; // ajw - Question: repeat the icon for each entry? make it optional?
  253. pItemExtra->IconKind[0] = IconKind0;
  254. pItemExtra->pIcon[1] = pIcon1;
  255. pItemExtra->IconKind[1] = IconKind1;
  256. pItemExtra->pIcon[2] = pIcon2;
  257. pItemExtra->IconKind[2] = IconKind2;
  258. pItemExtra->FixedIcon.pIcon = pFixedIcon;
  259. pItemExtra->FixedIcon.IconKind = FixedIconKind;
  260. pItemExtra->FixedIcon.xOffset = iXFixedIcon;
  261. pItemExtra->FixedIcon.yOffset = iYFixedIcon;
  262. pItemExtra->FixedIcon.iWidth = iFixedIconWidth;
  263. pItemExtra->pvExtraData = pvExtraData;
  264. pItemExtra->pColorRemap = pColorRemap;
  265. if( szHelp )
  266. {
  267. // Copy help into new help string.
  268. pItemExtra->szHelp = new char[ strlen( szHelp ) + 1 ];
  269. strcpy( pItemExtra->szHelp, szHelp );
  270. }
  271. if( szExtraDataString )
  272. {
  273. // Copy special data string into new extradata string.
  274. pItemExtra->szExtraData = new char[ strlen( szExtraDataString ) + 1 ];
  275. strcpy( pItemExtra->szExtraData, szExtraDataString );
  276. }
  277. ExtrasList.Add( pItemExtra );
  278. // Create text entry.
  279. // Copy text to new string.
  280. char* szTextBit = new char[ strlen( szToken ) + 1 ];
  281. strcpy( szTextBit, szToken );
  282. return ListClass::Add_Item( szTextBit );
  283. }
  284. //***********************************************************************************************
  285. int IconListClass::Add_Item( int text )
  286. {
  287. return Add_Item( Text_String(text), NULL, NULL, ICON_SHAPE );
  288. }
  289. //***********************************************************************************************
  290. int IconListClass::Add_Item( int text, const char* szHelp,
  291. void* pIcon0, ICONKIND IconKind0, const char* szExtraDataString /* = NULL */,
  292. void* pvExtraDataPtr /* = NULL */, RemapControlType* pColorRemap /* = NULL */,
  293. void* pIcon1 /* = NULL */, ICONKIND IconKind1 /* = ICON_SHAPE */,
  294. void* pIcon2 /* = NULL */, ICONKIND IconKind2 /* = ICON_SHAPE */,
  295. void* pFixedIcon /* = NULL */, ICONKIND FixedIconKind /* = ICON_SHAPE */, int iXFixedIcon /* = 0 */, int iYFixedIcon /* = 0 */, int iFixedIconWidth /* = -1 */ )
  296. {
  297. return Add_Item( Text_String(text), szHelp, pIcon0, IconKind0, szExtraDataString, pvExtraDataPtr, pColorRemap,
  298. pIcon1, IconKind1, pIcon2, IconKind2, pFixedIcon, FixedIconKind, iXFixedIcon, iYFixedIcon, iFixedIconWidth );
  299. }
  300. //***********************************************************************************************
  301. void IconListClass::Remove_Item( char const * text )
  302. {
  303. if( text )
  304. Remove_Item( List.ID(text) );
  305. }
  306. //***********************************************************************************************
  307. void IconListClass::Remove_Item( int index )
  308. {
  309. if( (unsigned)index < List.Count() )
  310. {
  311. delete (IconList_ItemExtras*)ExtrasList[ index ];
  312. ExtrasList.Delete( index );
  313. if( bDoAlloc )
  314. // Delete alloc'ed string.
  315. delete [] (char*)List[index];
  316. ListClass::Remove_Item( index );
  317. // I should probably put this in ListClass:Remove_Item(), as it seems clearly to be
  318. // missing, but I want to only affect my own new code, to not introduce possible bugs.
  319. // Shift the selected index if appropriate...
  320. if( SelectedIndex >= index )
  321. {
  322. SelectedIndex--;
  323. if( SelectedIndex < 0 )
  324. SelectedIndex = 0;
  325. }
  326. }
  327. }
  328. /***********************************************************************************************
  329. * IconListClass::Draw_Entry -- Calls ListClass::Draw_Entry, then adds icon. *
  330. * *
  331. * This routine is called by the Draw_Me function when it desired to redraw a particular *
  332. * text line in the list box. *
  333. * *
  334. * INPUT: index -- The index of the list entry to draw. This index is based on the *
  335. * total list and NOT the current visible view page. *
  336. * *
  337. * x,y -- Pixel coordinates for the upper left corner of the text entry. *
  338. * *
  339. * width -- The maximum width that the text may draw over. It is expected that *
  340. * this drawing routine entirely fills this length. *
  341. * *
  342. * selected -- bool; Is this a selected (highlighted) listbox entry? *
  343. * *
  344. * OUTPUT: none *
  345. * WARNINGS: none *
  346. * HISTORY: *
  347. * 07/07/1998 ajw: Created. *
  348. *=============================================================================================*/
  349. #define PREICONGAP 1
  350. #define ICONTEXTGAP 2
  351. void IconListClass::Draw_Entry( int index, int x, int y, int width, int selected )
  352. {
  353. IconList_ItemExtras* pExtras = (IconList_ItemExtras*)ExtrasList[ index ];
  354. int xText = x;
  355. // ajw If I end up needing to use SHAPEs for icons, figure out shape width here and offset x.
  356. bool bIconsPresent = false;
  357. for( int iIcon = 0; iIcon != 3; iIcon++ )
  358. if( pExtras->pIcon[ iIcon ] && pExtras->IconKind[ iIcon ] == ICON_DIB )
  359. {
  360. // Push text over to accommodate icon.
  361. int iWidthIcon = PREICONGAP + DIBWidth( (char*)pExtras->pIcon[ iIcon ] );
  362. xText += iWidthIcon;
  363. width -= iWidthIcon;
  364. bIconsPresent = true;
  365. }
  366. if( bIconsPresent )
  367. {
  368. xText += ICONTEXTGAP;
  369. width -= ICONTEXTGAP;
  370. }
  371. RemapControlType* pRemap = pExtras->pColorRemap;
  372. if( !pRemap )
  373. {
  374. // Tabs hack. If there are icons, and a tab, push back the FIRST tab appropriately.
  375. // (Ignore others. This is a hack because having more than one tab will now break this.)
  376. // See local version of this same hack, below.
  377. int TempTabs;
  378. const int* TabsSave;
  379. if( Tabs )
  380. {
  381. TempTabs = *Tabs - ( xText - x );
  382. TabsSave = Tabs;
  383. Tabs = &TempTabs;
  384. }
  385. switch( iSelectType )
  386. {
  387. case 0:
  388. // Don't draw any items selected (even if they are, really, in ListClass).
  389. ListClass::Draw_Entry( index, xText, y, width, false );
  390. break;
  391. case 1:
  392. ListClass::Draw_Entry( index, xText, y, width, selected );
  393. break;
  394. case 2:
  395. // Ignore 'selected' parameter. We use our own records.
  396. ListClass::Draw_Entry( index, xText, y, width, pExtras->bMultiSelected );
  397. break;
  398. }
  399. // Restore Tabs.
  400. if( Tabs )
  401. Tabs = TabsSave;
  402. }
  403. else
  404. {
  405. // Use different color remapping.
  406. // This is largely copied straight from ListClass::Draw_Entry()...
  407. TextPrintType flags = TextFlags;
  408. bool bShowSelected;
  409. switch( iSelectType )
  410. {
  411. case 0:
  412. bShowSelected = false;
  413. break;
  414. case 1:
  415. bShowSelected = selected;
  416. break;
  417. case 2:
  418. bShowSelected = pExtras->bMultiSelected;
  419. break;
  420. }
  421. if( bShowSelected )
  422. {
  423. flags = flags | TPF_BRIGHT_COLOR;
  424. LogicPage->Fill_Rect( xText, y, xText + width - 1, y + LineHeight - 1, pRemap->Shadow );
  425. }
  426. else
  427. {
  428. if (!(flags & TPF_USE_GRAD_PAL))
  429. {
  430. flags = flags | TPF_MEDIUM_COLOR;
  431. }
  432. }
  433. // Tabs hack. If there are icons, and a tab, push back the FIRST tab appropriately.
  434. // (Ignore others. This is a hack because having more than one tab will now break this.)
  435. if( Tabs )
  436. {
  437. int tab = *Tabs - ( xText - x );
  438. Conquer_Clip_Text_Print( List[index], xText, y, pRemap, TBLACK, flags, width, &tab );
  439. }
  440. else
  441. Conquer_Clip_Text_Print( List[index], xText, y, pRemap, TBLACK, flags, width, NULL );
  442. }
  443. // Draw fixed position icon.
  444. if( pExtras->FixedIcon.pIcon )
  445. {
  446. if( pExtras->FixedIcon.IconKind == ICON_SHAPE )
  447. CC_Draw_Shape( pExtras->FixedIcon.pIcon, 0, x + pExtras->FixedIcon.xOffset, y + pExtras->FixedIcon.yOffset, WINDOW_MAIN, SHAPE_NORMAL );
  448. // Put similar code in here for shapes if used...
  449. else
  450. CC_Draw_DIB( (char*)pExtras->FixedIcon.pIcon, x + pExtras->FixedIcon.xOffset, y + pExtras->FixedIcon.yOffset, pExtras->FixedIcon.iWidth, WINDOW_MAIN );
  451. }
  452. // Draw variable position left-of-text icons.
  453. for( iIcon = 0; iIcon != 3; iIcon++ )
  454. {
  455. if( pExtras->pIcon[ iIcon ] )
  456. {
  457. x += PREICONGAP;
  458. if( pExtras->IconKind[ iIcon ] == ICON_SHAPE )
  459. CC_Draw_Shape( pExtras->pIcon[ iIcon ], 0, x, y, WINDOW_MAIN, SHAPE_NORMAL );
  460. // Put similar code in here for shapes if used...
  461. else
  462. {
  463. CC_Draw_DIB( (char*)pExtras->pIcon[ iIcon ], x, y, 9999, WINDOW_MAIN );
  464. x += DIBWidth( (char*)pExtras->pIcon[ iIcon ] );
  465. }
  466. }
  467. }
  468. }
  469. //***********************************************************************************************
  470. int IconListClass::Action(unsigned flags, KeyNumType & key)
  471. {
  472. // Overriding of function is for the sake of MultiSelecting only.
  473. if( iSelectType == 2 )
  474. {
  475. if( !( flags & LEFTRELEASE ) )
  476. {
  477. if( !( flags & KEYBOARD ) )
  478. {
  479. int index = Get_Mouse_Y() - (Y+1);
  480. index = index / LineHeight;
  481. int iSelected = CurrentTopIndex + index;
  482. iSelected = min( iSelected, List.Count() - 1 );
  483. if( iSelected >= 0 )
  484. ((IconList_ItemExtras*)ExtrasList[ iSelected ])->bMultiSelected =
  485. !((IconList_ItemExtras*)ExtrasList[ iSelected ])->bMultiSelected;
  486. }
  487. }
  488. }
  489. return ListClass::Action( flags, key );
  490. }
  491. //***********************************************************************************************
  492. // * IconListClass::Show_Last_Item -- Scrolls listbox down to ensure that last entry is visible.
  493. // ajw 07/09/98
  494. void IconListClass::Show_Last_Item()
  495. {
  496. int iItemLast = List.Count() - 1;
  497. if( iItemLast - LineCount + 1 != CurrentTopIndex )
  498. {
  499. Flag_To_Redraw();
  500. Set_View_Index( iItemLast - LineCount + 1 );
  501. }
  502. }
  503. //***********************************************************************************************
  504. bool IconListClass::bItemIsMultiSelected( int index ) const
  505. {
  506. if( index < ExtrasList.Count() && index > -1 )
  507. return ( (IconList_ItemExtras*)ExtrasList[ index ] )->bMultiSelected;
  508. else
  509. return false;
  510. }
  511. //***********************************************************************************************
  512. void IconListClass::MultiSelect( int index, bool bSelect )
  513. {
  514. if( index < ExtrasList.Count() && index > -1 )
  515. ( (IconList_ItemExtras*)ExtrasList[ index ] )->bMultiSelected = bSelect;
  516. }
  517. //***********************************************************************************************
  518. const char* IconListClass::Get_Item_ExtraDataString( int index ) const
  519. {
  520. // Returns const pointer to the hidden "extra data" string that can be associated with each item.
  521. // This is NULL if no extra data was assigned.
  522. if( index < ExtrasList.Count() && index > -1 )
  523. {
  524. return ( (IconList_ItemExtras*)ExtrasList[ index ] )->szExtraData;
  525. }
  526. return NULL;
  527. }
  528. //***********************************************************************************************
  529. void IconListClass::Set_Item_ExtraDataString( int index, const char* szNewString )
  530. {
  531. if( index < ExtrasList.Count() && index > -1 )
  532. {
  533. IconList_ItemExtras* pItemExtra = (IconList_ItemExtras*)ExtrasList[ index ];
  534. if( pItemExtra->szExtraData )
  535. {
  536. // Delete the existing string.
  537. delete [] pItemExtra->szExtraData;
  538. }
  539. if( szNewString )
  540. {
  541. // Copy special data string into new extradata string.
  542. pItemExtra->szExtraData = new char[ strlen( szNewString ) + 1 ];
  543. strcpy( pItemExtra->szExtraData, szNewString );
  544. }
  545. else
  546. pItemExtra->szExtraData = NULL;
  547. }
  548. }
  549. //***********************************************************************************************
  550. void* IconListClass::Get_Item_ExtraDataPtr( int index ) const
  551. {
  552. // Returns the hidden "extra data" void pointer that can be associated with each item.
  553. // This is NULL if no value was assigned.
  554. if( index < ExtrasList.Count() && index > -1 )
  555. return ( (IconList_ItemExtras*)ExtrasList[ index ] )->pvExtraData;
  556. else
  557. return NULL;
  558. }
  559. //***********************************************************************************************
  560. void IconListClass::Set_Item_ExtraDataPtr( int index, void* pNewValue )
  561. {
  562. // Sets the hidden "extra data" void pointer that can be associated with each item.
  563. if( index < ExtrasList.Count() && index > -1 )
  564. ( (IconList_ItemExtras*)ExtrasList[ index ] )->pvExtraData = pNewValue;
  565. }
  566. //***********************************************************************************************
  567. const IconList_ItemExtras* IconListClass::Get_ItemExtras( int index ) const
  568. {
  569. if( index < ExtrasList.Count() && index > -1 )
  570. return (IconList_ItemExtras*)ExtrasList[ index ];
  571. else
  572. return NULL;
  573. }
  574. //***********************************************************************************************
  575. const char* IconListClass::Get_Item_Help( int index ) const
  576. {
  577. // Returns pointer to the string allocated for tooltip help.
  578. if( index < ExtrasList.Count() && index > -1 )
  579. return ( (IconList_ItemExtras*)ExtrasList[ index ] )->szHelp;
  580. else
  581. return NULL;
  582. }
  583. //***********************************************************************************************
  584. void IconListClass::Clear()
  585. {
  586. // Removes all items from list.
  587. // Delete the IconList_ItemExtras structs created to hold extra info on each item.
  588. for( int i = 0; i < ExtrasList.Count(); i++ )
  589. delete (IconList_ItemExtras*)ExtrasList[ i ];
  590. ExtrasList.Clear();
  591. if( bDoAlloc )
  592. {
  593. // Delete all alloc'ed strings.
  594. for( int i = 0; i < List.Count(); i++ )
  595. delete [] (char*)List[i];
  596. }
  597. List.Clear();
  598. Remove_Scroll_Bar();
  599. CurrentTopIndex = 0;
  600. }
  601. //***********************************************************************************************
  602. RemapControlType* IconListClass::Get_Item_Color( int index )
  603. {
  604. if( index < ExtrasList.Count() && index > -1 )
  605. return ( (IconList_ItemExtras*)ExtrasList[ index ] )->pColorRemap;
  606. else
  607. return NULL;
  608. }
  609. //***********************************************************************************************
  610. void IconListClass::Set_Item_Color( int index, RemapControlType* pColorRemap )
  611. {
  612. if( index < ExtrasList.Count() && index > -1 )
  613. ( (IconList_ItemExtras*)ExtrasList[ index ] )->pColorRemap = pColorRemap;
  614. }
  615. //***********************************************************************************************
  616. int IconListClass::Find( const char* szItemToFind )
  617. {
  618. // Returns -1 if szItemToFind is not found as the text BEGINNING one of the list entries, else index of item.
  619. // Compare is case-sensitive.
  620. for( int i = 0; i < List.Count(); i++ )
  621. {
  622. if( strncmp( List[ i ], szItemToFind, strlen( szItemToFind ) ) == 0 )
  623. return i;
  624. }
  625. return -1;
  626. }
  627. //***********************************************************************************************
  628. int IconListClass::FindColor( RemapControlType* pColorRemap )
  629. {
  630. // Returns -1 if no items of specified color are found, else first index. Assumes colorptr == colorptr is a valid equality test.
  631. for( int i = 0; i < List.Count(); i++ )
  632. {
  633. if( Get_Item_Color( i ) == pColorRemap )
  634. return i;
  635. }
  636. return -1;
  637. }
  638. //***********************************************************************************************
  639. bool IconListClass::Set_Item( unsigned int index, const char* szText )
  640. {
  641. // Resets the text string allocated for an item.
  642. if( !bDoAlloc || index >= List.Count() )
  643. return false;
  644. // Delete alloc'ed string.
  645. delete [] (char*)List[ index ];
  646. // Copy text to new string.
  647. char* szTextNew = new char[ strlen( szText ) + 1 ];
  648. strcpy( szTextNew, szText );
  649. // Reassign List's ptr.
  650. List[ index ] = szTextNew;
  651. return true;
  652. }
  653. //***********************************************************************************************
  654. bool IconListClass::Set_Icon( unsigned int index, unsigned int iIconNumber, void* pIcon, ICONKIND IconKind )
  655. {
  656. if( index >= List.Count() )
  657. return false;
  658. // Sets one of the left-aligned icons.
  659. ( (IconList_ItemExtras*)ExtrasList[ index ] )->pIcon[ iIconNumber ] = pIcon;
  660. ( (IconList_ItemExtras*)ExtrasList[ index ] )->IconKind[ iIconNumber ] = IconKind;
  661. return true;
  662. }
  663. //***********************************************************************************************
  664. int IconListClass::GetRealWidth() // sigh
  665. {
  666. if( IsScrollActive )
  667. return Width + ScrollGadget.Width;
  668. return Width;
  669. }
  670. //***********************************************************************************************
  671. void IconListClass::Resize( int x, int y, int w, int h )
  672. {
  673. Remove_Scroll_Bar(); // If there is one.
  674. X = x;
  675. Y = y;
  676. Width = w;
  677. Height = h;
  678. Set_Position( x, y );
  679. LineCount = (h-1) / LineHeight;
  680. if (List.Count() > LineCount) {
  681. Add_Scroll_Bar();
  682. }
  683. Flag_To_Redraw();
  684. }
  685. //***********************************************************************************************
  686. int IconListClass::IndexUnderMouse()
  687. {
  688. // Returns index of line that mouse is currently over, or -1 for mouse not hitting valid index.
  689. // Assumes that x position of mouse is already known to be over the iconlist.
  690. int index = Get_Mouse_Y() - (Y+1);
  691. index = index / LineHeight + CurrentTopIndex;
  692. if( index > List.Count() - 1 || index < 0 )
  693. return -1;
  694. return index;
  695. }
  696. //***********************************************************************************************
  697. int IconListClass::OffsetToIndex( int iIndex, int y )
  698. {
  699. // Finds the current offset of item iIndex from the current top view index, in pixels, and add it to y.
  700. return y + ( iIndex - CurrentTopIndex ) * LineHeight;
  701. }
  702. //***********************************************************************************************
  703. //***********************************************************************************************
  704. // * Format_Window_String_New
  705. // Functions like Format_Window_String except it fixes an infinite loop bug that occurred when strings
  706. // lacked suitable break points, eliminates the '@' as an escape character, and operates differently
  707. // in that it leaves the original string along, writing results instead to a second string parameter,
  708. // that is iExtraChars longer than the original string. This is all a big hack so that I can insert
  709. // extra break characters when a break in a long single word has to be made.
  710. // Hey - it's better than an infinite loop that forces you to reset your machine, as in the original code...
  711. int Format_Window_String_New( const char* string, int maxlinelen, int& width, int& height, char* szReturn, int iExtraChars )
  712. {
  713. int linelen;
  714. int lines = 0;
  715. width = 0;
  716. height = 0;
  717. // In no string was passed in, then there are no lines.
  718. if (!string) return(0);
  719. // While there are more letters left divide the line up.
  720. while (*string) {
  721. linelen = 0;
  722. height += FontHeight + FontYSpacing;
  723. lines++;
  724. // While the current line is less then the max length...
  725. *szReturn = *string;
  726. linelen += Char_Pixel_Width( *string );
  727. while ( linelen < maxlinelen && *string != '\r' && *string != '\0' )
  728. {
  729. *++szReturn = *++string;
  730. linelen += Char_Pixel_Width( *string );
  731. }
  732. // if the line is too long...
  733. if (linelen >= maxlinelen)
  734. {
  735. /*
  736. ** Back up to an appropriate location to break.
  737. */
  738. const char* stringOverEnd = string;
  739. while( linelen > 0 && *string != ' ' && *string != '\r' && *string != '\0' )
  740. {
  741. linelen -= Char_Pixel_Width(*string--);
  742. }
  743. if( linelen <= 0 )
  744. {
  745. // We could not find a nice break point.
  746. // Go back one char from over-the-end point and add in a break there.
  747. string = stringOverEnd - 1;
  748. if( iExtraChars > 0 )
  749. iExtraChars--; // One less to make use of later.
  750. else
  751. // We've used up all our extras characters.
  752. // Put in a break below by wiping out a valid char here.
  753. szReturn--;
  754. }
  755. else
  756. {
  757. // Back up szReturn to same location.
  758. szReturn -= ( stringOverEnd - string );
  759. }
  760. }
  761. /*
  762. ** Record the largest width of the worst case string.
  763. */
  764. if (linelen > width) {
  765. width = linelen;
  766. }
  767. /*
  768. ** Force a break at the end of the line.
  769. */
  770. if (*string) {
  771. *szReturn++ = '\r';
  772. string++;
  773. }
  774. }
  775. return(lines);
  776. }
  777. //***********************************************************************************************
  778. void CC_Draw_DIB( const char* pDIB, int xDest, int yDest, int iWidth, WindowNumberType window )
  779. {
  780. // A very basic DIB drawing routine. No clipping. No edge of window overrun checking.
  781. // If iWidth is too large, default width of dib is used.
  782. // If iWidth is negative, dib isn't drawn.
  783. if( pDIB && iWidth >= 0 )
  784. {
  785. int iWidthDIB = DIBWidth( pDIB );
  786. int iHeight = DIBHeight( pDIB );
  787. const char* pBits = FindDIBBits( pDIB );
  788. int iSrcPitch = ( iWidthDIB + 3 ) & ~3;
  789. if( iWidth > iWidthDIB )
  790. iWidth = iWidthDIB;
  791. GraphicViewPortClass draw_window( LogicPage->Get_Graphic_Buffer(),
  792. WindowList[window][WINDOWX] + LogicPage->Get_XPos(),
  793. WindowList[window][WINDOWY] + LogicPage->Get_YPos(),
  794. WindowList[window][WINDOWWIDTH],
  795. WindowList[window][WINDOWHEIGHT] );
  796. if( draw_window.Lock() )
  797. {
  798. int iDestPitch = draw_window.Get_Pitch() + draw_window.Get_Width(); // Meaning of "Pitch" in this class seems to mean the eol skip.
  799. char* pLineDest = (char*)draw_window.Get_Offset() + xDest + ( yDest + iHeight - 1 ) * iDestPitch;
  800. const char* pLineSrc = pBits;
  801. for( int y = 0; y != iHeight; y++ )
  802. {
  803. char* pDest = pLineDest;
  804. const char* pSrc = pLineSrc;
  805. for( int x = 0; x != iWidth; x++ )
  806. {
  807. *pDest++ = *pSrc++;
  808. }
  809. pLineDest -= iDestPitch;
  810. pLineSrc += iSrcPitch;
  811. }
  812. draw_window.Unlock();
  813. }
  814. }
  815. // else
  816. // debugprint( "CC_Draw_DIB bad case ------------ pDib %i, iWidth %i\n", pDIB, iWidth );
  817. }
  818. #endif