treectrl.cpp 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : wwui *
  23. * *
  24. * $Archive:: /Commando/Code/wwui/treectrl.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 11/08/01 5:56p $*
  29. * *
  30. * $Revision:: 2 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "treectrl.h"
  36. #include "assetmgr.h"
  37. #include "refcount.h"
  38. #include "font3d.h"
  39. #include "mousemgr.h"
  40. #include "ww3d.h"
  41. #include "dialogmgr.h"
  42. #include "dialogbase.h"
  43. #include "stylemgr.h"
  44. ////////////////////////////////////////////////////////////////
  45. // Local constants
  46. ////////////////////////////////////////////////////////////////
  47. static const char *PLUS_IMAGE = "if_treeplus.tga";
  48. static const RectClass PLUS_RECT = RectClass (0, 0, 16, 16);
  49. static const RectClass MINUS_RECT = RectClass (16, 0, 32, 16);
  50. ////////////////////////////////////////////////////////////////
  51. // Static member initialization
  52. ////////////////////////////////////////////////////////////////
  53. TREECTRL_SORT_CALLBACK TreeCtrlClass::CurrentSortCallback = NULL;
  54. TreeCtrlClass * TreeCtrlClass::CurrentSorter = NULL;
  55. uint32 TreeCtrlClass::CurrentSortUserData = 0;
  56. const char * TreeCtrlClass::ICON_FOLDER = "if_treefld.tga";
  57. const char * TreeCtrlClass::ICON_FOLDER_OPEN = "if_treefld_open.tga";
  58. ////////////////////////////////////////////////////////////////
  59. //
  60. // TreeCtrlClass
  61. //
  62. ////////////////////////////////////////////////////////////////
  63. TreeCtrlClass::TreeCtrlClass (void) :
  64. SelectedItem (NULL),
  65. ScrollPos (0),
  66. RowHeight (0),
  67. RowsPerPage (0),
  68. IsScrollBarDisplayed (false)
  69. {
  70. //
  71. // Set the font for the text renderers
  72. //
  73. StyleMgrClass::Assign_Font (&TextRenderer, StyleMgrClass::FONT_LISTS);
  74. StyleMgrClass::Configure_Renderer (&ControlRenderer);
  75. StyleMgrClass::Configure_Renderer (&PlusRenderer);
  76. StyleMgrClass::Configure_Renderer (&IconRenderer);
  77. HilightRenderer.Set_Coordinate_Range (Render2DClass::Get_Screen_Resolution());
  78. StyleMgrClass::Configure_Hilighter (&HilightRenderer);
  79. //
  80. // Load the "plus" texture
  81. //
  82. TextureClass *texture = WW3DAssetManager::Get_Instance ()->Get_Texture (PLUS_IMAGE, TextureClass::MIP_LEVELS_1);
  83. if (texture != NULL) {
  84. PlusRenderer.Set_Texture (texture);
  85. REF_PTR_RELEASE (texture);
  86. }
  87. TextRenderer.Set_Texture_Size_Hint (256);
  88. //
  89. // We don't want the scroll bar getting focus
  90. //
  91. ScrollBarCtrl.Set_Wants_Focus (false);
  92. ScrollBarCtrl.Set_Advise_Sink (this);
  93. ScrollBarCtrl.Set_Is_Embedded (true);
  94. return ;
  95. }
  96. ////////////////////////////////////////////////////////////////
  97. //
  98. // ~TreeCtrlClass
  99. //
  100. ////////////////////////////////////////////////////////////////
  101. TreeCtrlClass::~TreeCtrlClass (void)
  102. {
  103. ScrollBarCtrl.Set_Advise_Sink (NULL);
  104. if (Parent != NULL) {
  105. Parent->Remove_Control (&ScrollBarCtrl);
  106. }
  107. return ;
  108. }
  109. ////////////////////////////////////////////////////////////////
  110. //
  111. // Create_Text_Renderers
  112. //
  113. ////////////////////////////////////////////////////////////////
  114. void
  115. TreeCtrlClass::Create_Text_Renderers (void)
  116. {
  117. //
  118. // Configure the renderers
  119. //
  120. HilightRenderer.Reset ();
  121. TextRenderer.Reset ();
  122. IconRenderer.Reset ();
  123. PlusRenderer.Reset ();
  124. IconMgr.Reset_Renderers ();
  125. float x_pos = ClientRect.Left;
  126. float y_pos = ClientRect.Top;
  127. int row_index = 0;
  128. int level = 0;
  129. //
  130. // Render each row...
  131. //
  132. for (int index = 0; index < ItemList.Count (); index ++) {
  133. TreeItemClass *item = ItemList[index];
  134. if (Render_Item (item, x_pos, y_pos, row_index, level) == false) {
  135. break;
  136. }
  137. }
  138. return ;
  139. }
  140. ////////////////////////////////////////////////////////////////
  141. //
  142. // Render_Item
  143. //
  144. ////////////////////////////////////////////////////////////////
  145. bool
  146. TreeCtrlClass::Render_Item (TreeItemClass *item, float x_pos, float &y_pos, int &row_index, int level)
  147. {
  148. bool retval = true;
  149. if (item == NULL) {
  150. return false;
  151. }
  152. //
  153. // Determine which color to draw the outline in
  154. //
  155. int color = StyleMgrClass::Get_Line_Color ();
  156. if (IsEnabled == false) {
  157. color = StyleMgrClass::Get_Disabled_Line_Color ();
  158. }
  159. const float LINE_START = 16.0F * StyleMgrClass::Get_X_Scale ();
  160. const float LINE_SPACING = 16.0F * StyleMgrClass::Get_X_Scale ();
  161. const float TEXT_SPACING = 4.0F * StyleMgrClass::Get_X_Scale ();
  162. //
  163. // Build the display rectangles
  164. //
  165. RectClass rect;
  166. rect.Left = ClientRect.Left + LINE_START + int(level * LINE_SPACING);
  167. rect.Top = y_pos;
  168. rect.Right = ClientRect.Right;
  169. rect.Bottom = y_pos + RowHeight;
  170. RectClass plus_rect;
  171. plus_rect.Left = int(rect.Left - LINE_SPACING);
  172. plus_rect.Top = int(rect.Center ().Y - (LINE_SPACING * 0.5F));
  173. plus_rect.Right = int((plus_rect.Left + LINE_SPACING));
  174. plus_rect.Bottom = int(rect.Center ().Y + (LINE_SPACING * 0.5F));
  175. RectClass icon_rect;
  176. icon_rect.Left = rect.Left;
  177. icon_rect.Top = rect.Top;
  178. icon_rect.Right = icon_rect.Left + LINE_SPACING;
  179. icon_rect.Bottom = rect.Bottom;
  180. RectClass text_rect;
  181. text_rect.Left = icon_rect.Right + TEXT_SPACING;
  182. text_rect.Top = rect.Top;
  183. text_rect.Right = rect.Right;
  184. text_rect.Bottom = rect.Bottom;
  185. //
  186. // Only draw the entry if its inside the view
  187. //
  188. if (row_index >= ScrollPos && ((y_pos + RowHeight) <= ClientRect.Bottom)) {
  189. //
  190. // Draw the tick to the left of the entry
  191. //
  192. if (level > 0) {
  193. float half_rect = rect.Height () / 2;
  194. Vector2 line_start (icon_rect.Left - (LINE_SPACING * 0.5F), rect.Top + half_rect);
  195. Vector2 line_end (icon_rect.Left, rect.Top + half_rect);
  196. ControlRenderer.Add_Line (line_start, line_end, 1.0F, color);
  197. }
  198. //
  199. // Draw the plus (if necessary)
  200. //
  201. if ((item->Get_Child_Count () > 0 || item->Needs_Children ())) {
  202. RectClass plus_uv_rect = PLUS_RECT;
  203. if (item->Is_Expanded ()) {
  204. plus_uv_rect = MINUS_RECT;
  205. }
  206. plus_uv_rect.Inverse_Scale (Vector2 (32.0F, 32.0F));
  207. PlusRenderer.Add_Quad (plus_rect, plus_uv_rect);
  208. }
  209. //
  210. // Render the icon
  211. //
  212. const char *icon_name = item->Get_Icon ();
  213. if (SelectedItem == item) {
  214. icon_name = item->Get_Selected_Icon ();
  215. }
  216. if (icon_name != NULL && icon_name[0] != 0) {
  217. IconMgr.Render_Icon (icon_rect, icon_name);
  218. }
  219. //
  220. // Draw the text
  221. //
  222. StyleMgrClass::Render_Text (item->Get_Name (), &TextRenderer, text_rect, true, true,
  223. StyleMgrClass::LEFT_JUSTIFY, IsEnabled);
  224. //
  225. // Adjust the width of the rectangle so its the approximate size
  226. // of the text
  227. //
  228. float width = TextRenderer.Get_Text_Extents (item->Get_Name ()).X;
  229. text_rect.Right = text_rect.Left + width + TEXT_SPACING;
  230. //
  231. // Is this the selected item?
  232. //
  233. if (SelectedItem == item) {
  234. if (HasFocus) {
  235. RectClass hilight_rect = text_rect;
  236. hilight_rect.Right -= 1.0F;
  237. hilight_rect.Bottom -= 1.0F;
  238. StyleMgrClass::Render_Hilight (&HilightRenderer, hilight_rect);
  239. }
  240. ControlRenderer.Add_Outline (text_rect, 1.0F, color);
  241. }
  242. //
  243. // Move down one row
  244. //
  245. y_pos += RowHeight;
  246. }
  247. row_index ++;
  248. retval = ((y_pos + RowHeight) <= ClientRect.Bottom);
  249. //
  250. // Should we render the children?
  251. //
  252. if (item->Is_Expanded ()) {
  253. float start_y_pos = y_pos;
  254. int start_row_index = max (row_index, ScrollPos);
  255. int end_row_index = start_row_index;
  256. //
  257. // Render each child item
  258. //
  259. int child_count = item->Get_Child_Count ();
  260. for (int index = 0; index < child_count; index ++) {
  261. end_row_index = row_index + 1;
  262. retval = Render_Item (item->Get_Child (index), x_pos, y_pos, row_index, level + 1);
  263. }
  264. //
  265. // Render the line connecting this item to all its children
  266. //
  267. if ( end_row_index != start_row_index &&
  268. end_row_index >= ScrollPos &&
  269. start_row_index <= (ScrollPos + RowsPerPage))
  270. {
  271. float end_y_pos = start_y_pos + (end_row_index - start_row_index) * RowHeight;
  272. //
  273. // Clip the line start and end to the window
  274. //
  275. end_y_pos = min (end_y_pos, ClientRect.Top + (RowsPerPage * RowHeight));
  276. start_y_pos = max (start_y_pos, ClientRect.Top);
  277. //
  278. // Render the line
  279. //
  280. if (start_y_pos < end_y_pos) {
  281. float icon_x_pos = icon_rect.Center ().X;
  282. Vector2 line_start (icon_x_pos, start_y_pos);
  283. Vector2 line_end (icon_x_pos, end_y_pos - (RowHeight * 0.5F));
  284. ControlRenderer.Add_Line (line_start, line_end, 1.0F, color);
  285. }
  286. }
  287. }
  288. return retval;
  289. }
  290. ////////////////////////////////////////////////////////////////
  291. //
  292. // Set_Scroll_Pos
  293. //
  294. ////////////////////////////////////////////////////////////////
  295. void
  296. TreeCtrlClass::Set_Scroll_Pos (int new_pos)
  297. {
  298. //
  299. // Ensure this scroll position doesn't extend out of bounds
  300. //
  301. int visible_rows = Count_Visible_Rows ();
  302. new_pos = min (visible_rows - RowsPerPage, new_pos);
  303. new_pos = max (new_pos, 0);
  304. //
  305. // Did anything change?
  306. //
  307. if (ScrollPos != new_pos) {
  308. ScrollPos = new_pos;
  309. ScrollBarCtrl.Set_Pos (ScrollPos, false);
  310. Set_Dirty ();
  311. }
  312. return ;
  313. }
  314. ////////////////////////////////////////////////////////////////
  315. //
  316. // Count_Visible_Rows
  317. //
  318. ////////////////////////////////////////////////////////////////
  319. int
  320. TreeCtrlClass::Count_Visible_Rows (void)
  321. {
  322. int total = 0;
  323. //
  324. // Count each row
  325. //
  326. for (int index = 0; index < ItemList.Count (); index ++) {
  327. TreeItemClass *item = ItemList[index];
  328. //
  329. // Count this item and it's children
  330. //
  331. total += Count_Visible_Rows (item);
  332. }
  333. return total;
  334. }
  335. ////////////////////////////////////////////////////////////////
  336. //
  337. // Count_Visible_Rows
  338. //
  339. ////////////////////////////////////////////////////////////////
  340. int
  341. TreeCtrlClass::Count_Visible_Rows (TreeItemClass *item)
  342. {
  343. if (item == NULL) {
  344. return 0;
  345. }
  346. int total = 1;
  347. //
  348. // Should we count the children?
  349. //
  350. if (item->Is_Expanded ()) {
  351. //
  352. // Count each child
  353. //
  354. int child_count = item->Get_Child_Count ();
  355. for (int index = 0; index < child_count; index ++) {
  356. total += Count_Visible_Rows (item->Get_Child (index));
  357. }
  358. }
  359. return total;
  360. }
  361. ////////////////////////////////////////////////////////////////
  362. //
  363. // Create_Control_Renderers
  364. //
  365. ////////////////////////////////////////////////////////////////
  366. void
  367. TreeCtrlClass::Create_Control_Renderers (void)
  368. {
  369. Render2DClass &renderer = ControlRenderer;
  370. //
  371. // Configure this renderer
  372. //
  373. renderer.Reset ();
  374. renderer.Enable_Texturing (false);
  375. //
  376. // Determine which color to draw the outline in
  377. //
  378. int color = StyleMgrClass::Get_Line_Color ();
  379. int bkcolor = StyleMgrClass::Get_Bk_Color ();
  380. if (IsEnabled == false) {
  381. color = StyleMgrClass::Get_Disabled_Line_Color ();
  382. bkcolor = StyleMgrClass::Get_Disabled_Bk_Color ();
  383. }
  384. //
  385. // Draw the outline
  386. //
  387. renderer.Add_Rect (Rect, 1.0F, color, bkcolor);
  388. return ;
  389. }
  390. ////////////////////////////////////////////////////////////////
  391. //
  392. // On_Set_Cursor
  393. //
  394. ////////////////////////////////////////////////////////////////
  395. void
  396. TreeCtrlClass::On_Set_Cursor (const Vector2 &mouse_pos)
  397. {
  398. //
  399. // Change the mouse cursor
  400. //
  401. MouseMgrClass::Set_Cursor (MouseMgrClass::CURSOR_ACTION);
  402. return ;
  403. }
  404. ////////////////////////////////////////////////////////////////
  405. //
  406. // Update_Client_Rect
  407. //
  408. ////////////////////////////////////////////////////////////////
  409. void
  410. TreeCtrlClass::Update_Client_Rect (void)
  411. {
  412. const float BORDER_SIZE_X = 4.0F * StyleMgrClass::Get_X_Scale ();
  413. const float BORDER_SIZE_Y = 4.0F * StyleMgrClass::Get_Y_Scale ();
  414. //
  415. // Set the client area
  416. //
  417. ClientRect = Rect;
  418. ClientRect.Inflate (Vector2 (-BORDER_SIZE_X, -BORDER_SIZE_Y));
  419. //
  420. // Calculate how tall each row should be...
  421. //
  422. const float ROW_SPACING = 4.0F;
  423. float border_height = (ROW_SPACING * StyleMgrClass::Get_Y_Scale ());
  424. RowHeight = (TextRenderer.Get_Text_Extents (L"W").Y + border_height);
  425. //
  426. // Determine how many rows we can fit on a page
  427. //
  428. RowsPerPage = ClientRect.Height () / RowHeight;
  429. ClientRect.Top = int(Rect.Center ().Y - (RowsPerPage * RowHeight * 0.5F));
  430. ClientRect.Bottom = int(Rect.Center ().Y + (RowsPerPage * RowHeight * 0.5F));
  431. //
  432. // Choose an arbitrary width, the scroll bar
  433. // will snap to the only width it supports
  434. //
  435. float width = 10;
  436. //
  437. // Calculate the scroll bar's rectangle
  438. //
  439. RectClass scroll_rect;
  440. scroll_rect.Left = Rect.Right - width;
  441. scroll_rect.Top = Rect.Top;
  442. scroll_rect.Right = Rect.Right;
  443. scroll_rect.Bottom = Rect.Bottom;
  444. //
  445. // Size the scroll bar
  446. //
  447. ScrollBarCtrl.Set_Window_Rect (scroll_rect);
  448. Update_Scroll_Bar_Visibility ();
  449. Set_Dirty ();
  450. return ;
  451. }
  452. ////////////////////////////////////////////////////////////////
  453. //
  454. // On_VScroll
  455. //
  456. ////////////////////////////////////////////////////////////////
  457. void
  458. TreeCtrlClass::On_VScroll (ScrollBarCtrlClass *, int , int new_position)
  459. {
  460. Set_Scroll_Pos (new_position);
  461. return ;
  462. }
  463. ////////////////////////////////////////////////////////////////
  464. //
  465. // Render
  466. //
  467. ////////////////////////////////////////////////////////////////
  468. void
  469. TreeCtrlClass::Render (void)
  470. {
  471. //
  472. // Recreate the renderers (if necessary)
  473. //
  474. if (IsDirty) {
  475. Create_Control_Renderers ();
  476. Create_Text_Renderers ();
  477. }
  478. //
  479. // Render the background and text for the current state
  480. //
  481. IconMgr.Render_Icons ();
  482. TextRenderer.Render ();
  483. ControlRenderer.Render ();
  484. PlusRenderer.Render ();
  485. HilightRenderer.Render ();
  486. DialogControlClass::Render ();
  487. return ;
  488. }
  489. ////////////////////////////////////////////////////////////////
  490. //
  491. // Sort_Children_Alphabetically
  492. //
  493. ////////////////////////////////////////////////////////////////
  494. void
  495. TreeCtrlClass::Sort_Children_Alphabetically (TreeItemClass *parent)
  496. {
  497. Sort_Children (parent, Alphabetic_Sort_Callback, 0);
  498. return ;
  499. }
  500. ////////////////////////////////////////////////////////////////
  501. //
  502. // Sort_Children
  503. //
  504. ////////////////////////////////////////////////////////////////
  505. void
  506. TreeCtrlClass::Sort_Children (TreeItemClass *parent, TREECTRL_SORT_CALLBACK sort_callback, uint32 user_data)
  507. {
  508. CurrentSortCallback = sort_callback;
  509. CurrentSorter = this;
  510. CurrentSortUserData = user_data;
  511. if (parent == NULL) {
  512. //
  513. // Sort the root-level entries
  514. //
  515. if (ItemList.Count () != 0) {
  516. ::qsort (&ItemList[0], ItemList.Count (), sizeof (ItemList[0]), Sort_Callback);
  517. }
  518. } else {
  519. TREE_ITEM_LIST &child_list = parent->Get_Child_List ();
  520. //
  521. // Sort the immediate children of this item
  522. //
  523. if (child_list.Count () != 0) {
  524. ::qsort (&child_list[0], child_list.Count (), sizeof (child_list[0]), Sort_Callback);
  525. }
  526. }
  527. return ;
  528. }
  529. ////////////////////////////////////////////////////////////////
  530. //
  531. // Sort_Callback
  532. //
  533. ////////////////////////////////////////////////////////////////
  534. int __cdecl
  535. TreeCtrlClass::Sort_Callback (const void *elem1, const void *elem2)
  536. {
  537. WWASSERT (elem1 != NULL);
  538. WWASSERT (elem2 != NULL);
  539. TreeItemClass *item1 = *((TreeItemClass **)elem1);
  540. TreeItemClass *item2 = *((TreeItemClass **)elem2);
  541. //
  542. // Pass the sorting off to the user... (NOT THREAD SAFE)
  543. //
  544. return CurrentSortCallback (CurrentSorter, item1, item2, CurrentSortUserData);
  545. }
  546. ////////////////////////////////////////////////////////////////
  547. //
  548. // Alphabetic_Sort_Callback
  549. //
  550. ////////////////////////////////////////////////////////////////
  551. int CALLBACK
  552. TreeCtrlClass::Alphabetic_Sort_Callback
  553. (
  554. TreeCtrlClass * tree_ctrl,
  555. TreeItemClass * item1,
  556. TreeItemClass * item2,
  557. uint32 user_param
  558. )
  559. {
  560. //
  561. // Sort by name
  562. //
  563. const WCHAR *name1 = item1->Get_Name ();
  564. const WCHAR *name2 = item2->Get_Name ();
  565. int result = ::CompareStringW (LOCALE_USER_DEFAULT, NORM_IGNORECASE, name1, -1, name2, -1);
  566. int retval = 0;
  567. if (result == CSTR_LESS_THAN) {
  568. retval = -1;
  569. } else if (result == CSTR_GREATER_THAN) {
  570. retval = 1;
  571. }
  572. return retval;
  573. }
  574. ////////////////////////////////////////////////////////////////
  575. //
  576. // Find_Top_Item
  577. //
  578. ////////////////////////////////////////////////////////////////
  579. TreeItemClass *
  580. TreeCtrlClass::Find_Top_Item (void)
  581. {
  582. TreeItemClass *retval = NULL;
  583. //
  584. // Count visible items until we've hit the current scroll position
  585. //
  586. retval = ItemList.Count () > 0 ? ItemList[0] : NULL;
  587. for (int index = 0; retval != NULL && index < ScrollPos; index ++) {
  588. retval = Find_Next_Visible (retval);
  589. }
  590. return retval;
  591. }
  592. ////////////////////////////////////////////////////////////////
  593. //
  594. // Find_Last_Visible_Item
  595. //
  596. ////////////////////////////////////////////////////////////////
  597. TreeItemClass *
  598. TreeCtrlClass::Find_Last_Visible_Item (void)
  599. {
  600. //
  601. // Determine where to start...
  602. //
  603. TreeItemClass *curr_item = NULL;
  604. if (ItemList.Count () > 0) {
  605. curr_item = ItemList[ItemList.Count () - 1];
  606. }
  607. //
  608. // Keep searching until we stop finding new visible items...
  609. //
  610. while (curr_item != NULL) {
  611. TreeItemClass *next_item = Find_Next_Visible (curr_item);
  612. if (next_item == curr_item) {
  613. break;
  614. }
  615. curr_item = next_item;
  616. }
  617. return curr_item;
  618. }
  619. ////////////////////////////////////////////////////////////////
  620. //
  621. // Hit_Test
  622. //
  623. ////////////////////////////////////////////////////////////////
  624. TreeItemClass *
  625. TreeCtrlClass::Hit_Test (const Vector2 &mouse_pos, HITTYPE &type)
  626. {
  627. TreeItemClass *retval = NULL;
  628. //
  629. // Attempt to locate the first visible item
  630. //
  631. TreeItemClass *hit_item = Find_Top_Item ();
  632. if (hit_item != NULL) {
  633. //
  634. // Determine on which row of the screen the user clicked
  635. //
  636. int row_index = (mouse_pos.Y - ClientRect.Top) / RowHeight;
  637. //
  638. // Now move down that number of items...
  639. //
  640. for (int index = 0; hit_item != NULL && index < row_index; index ++) {
  641. hit_item = Find_Next_Visible (hit_item);
  642. }
  643. const float LINE_START = 16.0F * StyleMgrClass::Get_X_Scale ();
  644. const float LINE_SPACING = 16.0F * StyleMgrClass::Get_X_Scale ();
  645. const float TEXT_SPACING = 4.0F * StyleMgrClass::Get_X_Scale ();
  646. //
  647. // Now determine on what part of the item the user clicked...
  648. //
  649. if (hit_item != NULL) {
  650. int level = hit_item->Get_Indent_Level ();
  651. float icon_left = ClientRect.Left + LINE_START + int(level * LINE_SPACING);
  652. float plus_left = icon_left - LINE_SPACING;
  653. float text_left = icon_left + LINE_SPACING + TEXT_SPACING;
  654. //
  655. // Return the hit type and hit item to the caller
  656. //
  657. if (mouse_pos.X >= plus_left && mouse_pos.X < icon_left) {
  658. type = HIT_PLUS;
  659. retval = hit_item;
  660. } else if (mouse_pos.X >= icon_left && mouse_pos.X < text_left) {
  661. type = HIT_ICON;
  662. retval = hit_item;
  663. } else if (mouse_pos.X >= text_left) {
  664. type = HIT_TEXT;
  665. retval = hit_item;
  666. }
  667. }
  668. }
  669. return retval;
  670. }
  671. ////////////////////////////////////////////////////////////////
  672. //
  673. // On_LButton_Down
  674. //
  675. ////////////////////////////////////////////////////////////////
  676. void
  677. TreeCtrlClass::On_LButton_Down (const Vector2 &mouse_pos)
  678. {
  679. //
  680. // Find which item hit clicked on
  681. //
  682. HITTYPE type;
  683. TreeItemClass *tree_item = Hit_Test (mouse_pos, type);
  684. if (tree_item != NULL) {
  685. SelectedItem = tree_item;
  686. //
  687. // Toggle the expanded state if the user clicked on the
  688. // plus
  689. //
  690. if (type == HIT_PLUS) {
  691. tree_item->Expand (!tree_item->Is_Expanded ());
  692. }
  693. Set_Dirty ();
  694. }
  695. return ;
  696. }
  697. ////////////////////////////////////////////////////////////////
  698. //
  699. // On_LButton_DblClk
  700. //
  701. ////////////////////////////////////////////////////////////////
  702. void
  703. TreeCtrlClass::On_LButton_DblClk (const Vector2 &mouse_pos)
  704. {
  705. //
  706. // Find which item hit clicked on
  707. //
  708. HITTYPE type;
  709. TreeItemClass *tree_item = Hit_Test (mouse_pos, type);
  710. if (tree_item != NULL) {
  711. SelectedItem = tree_item;
  712. //
  713. // Toggle the expanded state if the user clicked on the
  714. // plus
  715. //
  716. tree_item->Expand (!tree_item->Is_Expanded ());
  717. Set_Dirty ();
  718. }
  719. return ;
  720. }
  721. ////////////////////////////////////////////////////////////////
  722. //
  723. // On_LButton_Up
  724. //
  725. ////////////////////////////////////////////////////////////////
  726. void
  727. TreeCtrlClass::On_LButton_Up (const Vector2 &mouse_pos)
  728. {
  729. return ;
  730. }
  731. ////////////////////////////////////////////////////////////////
  732. //
  733. // On_Set_Focus
  734. //
  735. ////////////////////////////////////////////////////////////////
  736. void
  737. TreeCtrlClass::On_Set_Focus (void)
  738. {
  739. Set_Dirty ();
  740. DialogControlClass::On_Set_Focus ();
  741. return ;
  742. }
  743. ////////////////////////////////////////////////////////////////
  744. //
  745. // On_Kill_Focus
  746. //
  747. ////////////////////////////////////////////////////////////////
  748. void
  749. TreeCtrlClass::On_Kill_Focus (DialogControlClass *focus)
  750. {
  751. Set_Dirty ();
  752. DialogControlClass::On_Kill_Focus (focus);
  753. return ;
  754. }
  755. ////////////////////////////////////////////////////////////////
  756. //
  757. // On_Mouse_Wheel
  758. //
  759. ////////////////////////////////////////////////////////////////
  760. void
  761. TreeCtrlClass::On_Mouse_Wheel (int direction)
  762. {
  763. if (direction < 0) {
  764. Set_Scroll_Pos (ScrollPos - 1);
  765. } else {
  766. Set_Scroll_Pos (ScrollPos + 1);
  767. }
  768. Set_Dirty ();
  769. return ;
  770. }
  771. ////////////////////////////////////////////////////////////////
  772. //
  773. // On_Key_Down
  774. //
  775. ////////////////////////////////////////////////////////////////
  776. bool
  777. TreeCtrlClass::On_Key_Down (uint32 key_id, uint32 key_data)
  778. {
  779. bool handled = false;
  780. bool is_dirty = true;
  781. switch (key_id)
  782. {
  783. case VK_HOME:
  784. if (ItemList.Count () > 0) {
  785. SelectedItem = ItemList[0];
  786. Set_Scroll_Pos (0);
  787. }
  788. break;
  789. case VK_END:
  790. if (ItemList.Count () > 0) {
  791. SelectedItem = Find_Last_Visible_Item ();
  792. Ensure_Visible (SelectedItem);
  793. }
  794. break;
  795. case VK_UP:
  796. SelectedItem = Find_Prev_Visible (SelectedItem);
  797. Ensure_Visible (SelectedItem);
  798. Set_Dirty ();
  799. break;
  800. case VK_DOWN:
  801. SelectedItem = Find_Next_Visible (SelectedItem);
  802. Ensure_Visible (SelectedItem);
  803. Set_Dirty ();
  804. break;
  805. case VK_RIGHT:
  806. if (SelectedItem != NULL) {
  807. SelectedItem->Expand (true);
  808. }
  809. break;
  810. case VK_LEFT:
  811. if (SelectedItem != NULL) {
  812. SelectedItem->Expand (false);
  813. }
  814. break;
  815. default:
  816. is_dirty = false;
  817. break;
  818. }
  819. if (is_dirty) {
  820. Set_Dirty ();
  821. }
  822. return handled;
  823. }
  824. ////////////////////////////////////////////////////////////////
  825. //
  826. // On_Create
  827. //
  828. ////////////////////////////////////////////////////////////////
  829. void
  830. TreeCtrlClass::On_Create (void)
  831. {
  832. return ;
  833. }
  834. ////////////////////////////////////////////////////////////////
  835. //
  836. // Ensure_Visible
  837. //
  838. ////////////////////////////////////////////////////////////////
  839. void
  840. TreeCtrlClass::Ensure_Visible (TreeItemClass *item_to_find)
  841. {
  842. if (item_to_find == NULL) {
  843. Set_Scroll_Pos (0);
  844. Set_Dirty ();
  845. return ;
  846. }
  847. //
  848. // Start at the first visible item and keep going until we've found the
  849. // item we're looking for
  850. //
  851. TreeItemClass *item = ItemList.Count () > 0 ? ItemList[0] : NULL;
  852. for (int row_index = 0; item != NULL; row_index ++) {
  853. if (item == item_to_find) {
  854. //
  855. // Determine if this row is outside the visible window
  856. //
  857. if (row_index < ScrollPos) {
  858. Set_Scroll_Pos (row_index);
  859. Set_Dirty ();
  860. } else if (row_index >= ScrollPos + RowsPerPage) {
  861. Set_Scroll_Pos (row_index - (RowsPerPage-1));
  862. Set_Dirty ();
  863. }
  864. break;
  865. }
  866. //
  867. // Advance to the next visible item
  868. //
  869. item = Find_Next_Visible (item);
  870. }
  871. return ;
  872. }
  873. ////////////////////////////////////////////////////////////////
  874. //
  875. // Insert_Item
  876. //
  877. ////////////////////////////////////////////////////////////////
  878. TreeItemClass *
  879. TreeCtrlClass::Insert_Item
  880. (
  881. const WCHAR * name,
  882. const char * icon_name,
  883. const char * selected_icon_name,
  884. TreeItemClass * parent
  885. )
  886. {
  887. //
  888. // Allocate a new tree item
  889. //
  890. TreeItemClass *retval = new TreeItemClass (this);
  891. retval->Set_Name (name);
  892. retval->Set_Icon (icon_name);
  893. retval->Set_Selected_Icon (selected_icon_name);
  894. //
  895. // Now insert this item into the tree
  896. //
  897. if (parent != NULL) {
  898. parent->Add_Child (retval);
  899. retval->Set_Parent (parent);
  900. } else {
  901. ItemList.Add (retval);
  902. //
  903. // Select the first item by default
  904. //
  905. if (ItemList.Count () == 1) {
  906. SelectedItem = retval;
  907. }
  908. }
  909. //
  910. // Check to see if we need a scroll bar or not
  911. //
  912. Update_Scroll_Bar_Visibility ();
  913. Set_Dirty ();
  914. return retval;
  915. }
  916. ////////////////////////////////////////////////////////////////
  917. //
  918. // Delete_Item
  919. //
  920. ////////////////////////////////////////////////////////////////
  921. void
  922. TreeCtrlClass::Delete_Item (TreeItemClass *item)
  923. {
  924. if (item == NULL) {
  925. return ;
  926. }
  927. //
  928. // Remove the selected item if necessary
  929. //
  930. if (SelectedItem == item) {
  931. SelectedItem = NULL;
  932. }
  933. //
  934. // Notify any advise sinks
  935. //
  936. ADVISE_NOTIFY (On_TreeCtrl_Delete_Item (this, Get_ID (), item));
  937. //
  938. // Recursively delete all this item's children
  939. //
  940. int child_count = item->Get_Child_Count ();
  941. for (int index = 0; index < child_count; index ++) {
  942. TreeItemClass *child = item->Get_Child (index);
  943. Delete_Item (child);
  944. }
  945. //
  946. // Now unlink this child from its parent (or the root list)
  947. //
  948. TreeItemClass *parent = item->Get_Parent ();
  949. if (parent != NULL) {
  950. parent->Remove_Child (item);
  951. } else {
  952. //
  953. // Remove this item from the root list
  954. //
  955. index = ItemList.ID (item);
  956. if (index != -1) {
  957. ItemList.Delete (index);
  958. }
  959. }
  960. //
  961. // Free this item
  962. //
  963. delete item;
  964. item = NULL;
  965. //
  966. // Ensure the scroll bar is correctly displayed
  967. //
  968. Update_Scroll_Bar_Visibility ();
  969. Set_Dirty ();
  970. return ;
  971. }
  972. ////////////////////////////////////////////////////////////////
  973. //
  974. // Delete_All_Items
  975. //
  976. ////////////////////////////////////////////////////////////////
  977. void
  978. TreeCtrlClass::Delete_All_Items (void)
  979. {
  980. //
  981. // Note: We count down because the Delete_Item () call will
  982. // delete the entry from the list...
  983. //
  984. int index = ItemList.Count ();
  985. while (index --) {
  986. Delete_Item (ItemList[index]);
  987. }
  988. //
  989. // Ensure the scroll bar is correctly displayed
  990. //
  991. ScrollPos = 0;
  992. SelectedItem = NULL;
  993. Update_Scroll_Bar_Visibility ();
  994. return ;
  995. }
  996. ////////////////////////////////////////////////////////////////
  997. //
  998. // Select_Item
  999. //
  1000. ////////////////////////////////////////////////////////////////
  1001. void
  1002. TreeCtrlClass::Select_Item (TreeItemClass *item)
  1003. {
  1004. //
  1005. // Notify any advise sinks
  1006. //
  1007. ADVISE_NOTIFY (On_TreeCtrl_Sel_Change (this, Get_ID (), SelectedItem, item));
  1008. SelectedItem = item;
  1009. Set_Dirty ();
  1010. return ;
  1011. }
  1012. ////////////////////////////////////////////////////////////////
  1013. //
  1014. // Get_Selected_Item
  1015. //
  1016. ////////////////////////////////////////////////////////////////
  1017. TreeItemClass *
  1018. TreeCtrlClass::Get_Selected_Item (void)
  1019. {
  1020. return SelectedItem;
  1021. }
  1022. ////////////////////////////////////////////////////////////////
  1023. //
  1024. // Get_Next_Sibling
  1025. //
  1026. ////////////////////////////////////////////////////////////////
  1027. TreeItemClass *
  1028. TreeCtrlClass::Get_Next_Sibling (TreeItemClass *item)
  1029. {
  1030. TreeItemClass *retval = NULL;
  1031. if (item == NULL) {
  1032. return NULL;
  1033. }
  1034. //
  1035. // If this item has a parent, then ask it for the sibling...
  1036. //
  1037. TreeItemClass *parent = item->Get_Parent ();
  1038. if (parent != NULL) {
  1039. retval = parent->Get_Next_Child (item);
  1040. } else {
  1041. //
  1042. // Return the next entry in the root item list...
  1043. //
  1044. int index = ItemList.ID (item);
  1045. if (index != -1 && (index + 1) < ItemList.Count ()) {
  1046. retval = ItemList[index + 1];
  1047. }
  1048. }
  1049. return retval;
  1050. }
  1051. ////////////////////////////////////////////////////////////////
  1052. //
  1053. // Get_Prev_Sibling
  1054. //
  1055. ////////////////////////////////////////////////////////////////
  1056. TreeItemClass *
  1057. TreeCtrlClass::Get_Prev_Sibling (TreeItemClass *item)
  1058. {
  1059. TreeItemClass *retval = NULL;
  1060. if (item == NULL) {
  1061. return NULL;
  1062. }
  1063. //
  1064. // If this item has a parent, then ask it for the sibling...
  1065. //
  1066. TreeItemClass *parent = item->Get_Parent ();
  1067. if (parent != NULL) {
  1068. retval = parent->Get_Prev_Child (item);
  1069. } else {
  1070. //
  1071. // Return the previous entry in the root item list...
  1072. //
  1073. int index = ItemList.ID (item);
  1074. if (index > 0) {
  1075. retval = ItemList[index - 1];
  1076. }
  1077. }
  1078. return retval;
  1079. }
  1080. ////////////////////////////////////////////////////////////////
  1081. //
  1082. // Find_Prev_Visible
  1083. //
  1084. ////////////////////////////////////////////////////////////////
  1085. TreeItemClass *
  1086. TreeCtrlClass::Find_Prev_Visible (TreeItemClass *item)
  1087. {
  1088. TreeItemClass *retval = NULL;
  1089. if (item == NULL) {
  1090. if (ItemList.Count () > 0) {
  1091. return ItemList[0];
  1092. }
  1093. return NULL;
  1094. }
  1095. //
  1096. // Simply find the previous sibling of this item.
  1097. // If you can't find the sibling, then return its parent
  1098. //
  1099. retval = Get_Prev_Sibling (item);
  1100. if (retval == NULL) {
  1101. retval = item->Get_Parent ();
  1102. } else {
  1103. while (retval->Is_Expanded ()) {
  1104. //
  1105. // Find the last child for this given item
  1106. //
  1107. TreeItemClass *curr_item = retval->Get_Last_Child ();
  1108. if (curr_item != NULL) {
  1109. retval = curr_item;
  1110. } else {
  1111. break;
  1112. }
  1113. }
  1114. }
  1115. //
  1116. // If we didn't find a previous visible entry, then
  1117. // simply return the item we were given
  1118. //
  1119. if (retval == NULL) {
  1120. retval = item;
  1121. }
  1122. return retval;
  1123. }
  1124. ////////////////////////////////////////////////////////////////
  1125. //
  1126. // Find_Next_Visible
  1127. //
  1128. ////////////////////////////////////////////////////////////////
  1129. TreeItemClass *
  1130. TreeCtrlClass::Find_Next_Visible (TreeItemClass *item)
  1131. {
  1132. TreeItemClass *retval = NULL;
  1133. if (item == NULL) {
  1134. if (ItemList.Count () > 0) {
  1135. return ItemList[0];
  1136. }
  1137. return NULL;
  1138. }
  1139. //
  1140. // First try to get this item's first child...
  1141. //
  1142. if (item->Is_Expanded () && item->Get_Child_Count () > 0) {
  1143. retval = item->Get_Child (0);
  1144. }
  1145. if (retval == NULL) {
  1146. //
  1147. // Try to find this item's next sibling...
  1148. //
  1149. retval = Get_Next_Sibling (item);
  1150. if (retval == NULL) {
  1151. //
  1152. // Loop outwards through each parent until
  1153. // we can find the next sibling of any parent...
  1154. //
  1155. TreeItemClass *curr_item = item;
  1156. while (curr_item != NULL && retval == NULL) {
  1157. curr_item = curr_item->Get_Parent ();
  1158. if (curr_item != NULL) {
  1159. retval = Get_Next_Sibling (curr_item);
  1160. }
  1161. }
  1162. }
  1163. }
  1164. //
  1165. // If we didn't find a previous visible entry, then
  1166. // simply return the item we were given
  1167. //
  1168. if (retval == NULL) {
  1169. retval = item;
  1170. }
  1171. return retval;
  1172. }
  1173. ////////////////////////////////////////////////////////////////
  1174. //
  1175. // Update_Scroll_Bar_Visibility
  1176. //
  1177. ////////////////////////////////////////////////////////////////
  1178. void
  1179. TreeCtrlClass::Update_Scroll_Bar_Visibility (void)
  1180. {
  1181. int visible_rows = Count_Visible_Rows ();
  1182. //
  1183. // Determine if we have more entries then we can
  1184. // display on one page
  1185. //
  1186. bool needs_scrollbar = (visible_rows > RowsPerPage);
  1187. float new_right = 0;
  1188. if (needs_scrollbar != IsScrollBarDisplayed) {
  1189. //
  1190. // Do we need to show a scroll bar?
  1191. //
  1192. if (needs_scrollbar) {
  1193. new_right = ScrollBarCtrl.Get_Window_Rect ().Left;
  1194. IsScrollBarDisplayed = true;
  1195. } else if (Parent != NULL) {
  1196. new_right = ScrollBarCtrl.Get_Window_Rect ().Right;
  1197. IsScrollBarDisplayed = false;
  1198. }
  1199. const float BORDER_SIZE_X = 4.0F * StyleMgrClass::Get_X_Scale ();
  1200. //
  1201. // Reset our window size (as necessary)
  1202. //
  1203. Rect.Right = new_right;
  1204. ClientRect.Left = Rect.Left + BORDER_SIZE_X;
  1205. ClientRect.Right = Rect.Right - BORDER_SIZE_X;
  1206. //
  1207. // Add or remove the scrollbar (as necessary)
  1208. //
  1209. if (IsScrollBarDisplayed) {
  1210. Parent->Add_Control (&ScrollBarCtrl);
  1211. } else {
  1212. Parent->Remove_Control (&ScrollBarCtrl);
  1213. }
  1214. }
  1215. //
  1216. // Configure the scroll-bar's ranges
  1217. //
  1218. ScrollBarCtrl.Set_Page_Size (RowsPerPage);
  1219. ScrollBarCtrl.Set_Range (0, visible_rows - RowsPerPage);
  1220. ScrollBarCtrl.Set_Pos (ScrollPos);
  1221. ScrollPos = ScrollBarCtrl.Get_Pos ();
  1222. return ;
  1223. }
  1224. ////////////////////////////////////////////////////////////////
  1225. //
  1226. // On_Expanded
  1227. //
  1228. ////////////////////////////////////////////////////////////////
  1229. void
  1230. TreeCtrlClass::On_Expanded (TreeItemClass *item)
  1231. {
  1232. //
  1233. // Notify any advise sinks
  1234. //
  1235. ADVISE_NOTIFY (On_TreeCtrl_Expand_Item (this, Get_ID (), item));
  1236. //
  1237. // Check to see if this item needs its children filled in. If so,
  1238. // then notify the advise sinks
  1239. //
  1240. if (item != NULL && item->Is_Expanded () && item->Needs_Children ()) {
  1241. ADVISE_NOTIFY (On_TreeCtrl_Needs_Children (this, Get_ID (), item));
  1242. }
  1243. return ;
  1244. }
  1245. //********************************************************************************//
  1246. //
  1247. // Start of TreeItemClass
  1248. //
  1249. //********************************************************************************//
  1250. ////////////////////////////////////////////////////////////////
  1251. //
  1252. // Remove_Child
  1253. //
  1254. ////////////////////////////////////////////////////////////////
  1255. void
  1256. TreeItemClass::Remove_Child (TreeItemClass *child)
  1257. {
  1258. int index = ChildList.ID (child);
  1259. if (index != -1) {
  1260. ChildList.Delete (index);
  1261. }
  1262. return ;
  1263. }
  1264. ////////////////////////////////////////////////////////////////
  1265. //
  1266. // Expand
  1267. //
  1268. ////////////////////////////////////////////////////////////////
  1269. void
  1270. TreeItemClass::Expand (bool onoff)
  1271. {
  1272. IsExpanded = onoff;
  1273. TreeCtrl->On_Expanded (this);
  1274. TreeCtrl->Select_Item (this);
  1275. TreeCtrl->Update_Scroll_Bar_Visibility ();
  1276. TreeCtrl->Set_Dirty ();
  1277. return ;
  1278. }
  1279. ////////////////////////////////////////////////////////////////
  1280. //
  1281. // Set_Name
  1282. //
  1283. ////////////////////////////////////////////////////////////////
  1284. void
  1285. TreeItemClass::Set_Name (const WCHAR *name)
  1286. {
  1287. Name = name;
  1288. TreeCtrl->Set_Dirty ();
  1289. return ;
  1290. }
  1291. ////////////////////////////////////////////////////////////////
  1292. //
  1293. // Set_Icon
  1294. //
  1295. ////////////////////////////////////////////////////////////////
  1296. void
  1297. TreeItemClass::Set_Icon (const char *texture_name)
  1298. {
  1299. IconName = texture_name;
  1300. //
  1301. // Add this icon to our manager
  1302. //
  1303. if (texture_name != NULL) {
  1304. TreeCtrl->IconMgr.Add_Icon (texture_name);
  1305. }
  1306. TreeCtrl->Set_Dirty ();
  1307. return ;
  1308. }
  1309. ////////////////////////////////////////////////////////////////
  1310. //
  1311. // Set_Selected_Icon
  1312. //
  1313. ////////////////////////////////////////////////////////////////
  1314. void
  1315. TreeItemClass::Set_Selected_Icon (const char *texture_name)
  1316. {
  1317. SelectedIconName = texture_name;
  1318. //
  1319. // Add this icon to our manager
  1320. //
  1321. if (texture_name != NULL) {
  1322. TreeCtrl->IconMgr.Add_Icon (texture_name);
  1323. }
  1324. TreeCtrl->Set_Dirty ();
  1325. return ;
  1326. }
  1327. ////////////////////////////////////////////////////////////////
  1328. //
  1329. // Get_Prev_Child
  1330. //
  1331. ////////////////////////////////////////////////////////////////
  1332. TreeItemClass *
  1333. TreeItemClass::Get_Prev_Child (TreeItemClass *child)
  1334. {
  1335. TreeItemClass *retval = NULL;
  1336. if (child == NULL) {
  1337. return NULL;
  1338. }
  1339. //
  1340. // Return the previous entry in our list...
  1341. //
  1342. int index = ChildList.ID (child);
  1343. if (index > 0) {
  1344. retval = ChildList[index - 1];
  1345. }
  1346. return retval;
  1347. }
  1348. ////////////////////////////////////////////////////////////////
  1349. //
  1350. // Get_Prev_Child
  1351. //
  1352. ////////////////////////////////////////////////////////////////
  1353. TreeItemClass *
  1354. TreeItemClass::Get_Next_Child (TreeItemClass *child)
  1355. {
  1356. TreeItemClass *retval = NULL;
  1357. if (child == NULL) {
  1358. return NULL;
  1359. }
  1360. //
  1361. // Return the next entry in our list...
  1362. //
  1363. int index = ChildList.ID (child);
  1364. if (index != -1 && (index + 1) < ChildList.Count ()) {
  1365. retval = ChildList[index + 1];
  1366. }
  1367. return retval;
  1368. }
  1369. ////////////////////////////////////////////////////////////////
  1370. //
  1371. // Get_Indent_Level
  1372. //
  1373. ////////////////////////////////////////////////////////////////
  1374. int
  1375. TreeItemClass::Get_Indent_Level (void)
  1376. {
  1377. int retval = 0;
  1378. //
  1379. // Count the number of parents this item has
  1380. //
  1381. TreeItemClass *curr_item = this;
  1382. while ((curr_item = curr_item->Get_Parent ()) != NULL) {
  1383. retval ++;
  1384. }
  1385. return retval;
  1386. }
  1387. ////////////////////////////////////////////////////////////////
  1388. //
  1389. // Get_Last_Child
  1390. //
  1391. ////////////////////////////////////////////////////////////////
  1392. TreeItemClass *
  1393. TreeItemClass::Get_Last_Child (void)
  1394. {
  1395. TreeItemClass *retval = NULL;
  1396. if (ChildList.Count () > 0) {
  1397. retval = ChildList[ChildList.Count () - 1];
  1398. }
  1399. return retval;
  1400. }
  1401. ////////////////////////////////////////////////////////////////
  1402. //
  1403. // Set_Needs_Children
  1404. //
  1405. ////////////////////////////////////////////////////////////////
  1406. void
  1407. TreeItemClass::Set_Needs_Children (bool onoff)
  1408. {
  1409. NeedsChildren = onoff;
  1410. TreeCtrl->Set_Dirty ();
  1411. return ;
  1412. }