Flu_Tree_Browser.cpp 107 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793
  1. // $Id: Flu_Tree_Browser.cpp,v 1.134 2005/03/14 15:06:19 jbryan Exp $
  2. /***************************************************************
  3. * FLU - FLTK Utility Widgets
  4. * Copyright (C) 2002 Ohio Supercomputer Center, Ohio State University
  5. *
  6. * This file and its content is protected by a software license.
  7. * You should have received a copy of this license with this file.
  8. * If not, please contact the Ohio Supercomputer Center immediately:
  9. * Attn: Jason Bryan Re: FLU 1224 Kinnear Rd, Columbus, Ohio 43212
  10. *
  11. ***************************************************************/
  12. #include <FL/Fl.H>
  13. #include <FL/fl_draw.H>
  14. #include <FL/math.h>
  15. #include <stdlib.h>
  16. #include "FLU/Flu_Tree_Browser.H"
  17. #include "FLU/flu_pixmaps.h"
  18. #define MAX( x, y ) ( (x)>(y) ? (x) : (y) )
  19. #define MIN( x, y ) ( (x)<(y) ? (x) : (y) )
  20. #define ABS( x ) ( (x)>0 ? (x) : -(x) )
  21. #define LERP( t, x0, x1 ) ( (x0) + (t)*( (x1) - (x0) ) )
  22. bool Flu_Tree_Browser::USE_FLU_WIDGET_CALLBACK = false;
  23. #ifdef USE_FLU_DND
  24. Flu_Tree_Browser::DND_Object::DND_Object() : Flu_DND( "DND_Object" )
  25. {
  26. }
  27. #endif
  28. Flu_Tree_Browser::IntStack::IntStack()
  29. {
  30. _list = NULL;
  31. _size = _bufferSize = 0;
  32. }
  33. Flu_Tree_Browser::IntStack::IntStack( const Flu_Tree_Browser::IntStack& s )
  34. {
  35. _list = NULL;
  36. _size = _bufferSize = 0;
  37. *this = s;
  38. }
  39. Flu_Tree_Browser::IntStack::~IntStack()
  40. {
  41. clear();
  42. }
  43. Flu_Tree_Browser::IntStack& Flu_Tree_Browser::IntStack::operator =( const Flu_Tree_Browser::IntStack& s )
  44. {
  45. clear();
  46. if( s._size )
  47. {
  48. _list = (int*)malloc( s._size*sizeof(int) );
  49. memcpy( _list, s._list, s._size*sizeof(int) );
  50. _size = _bufferSize = s._size;
  51. }
  52. return *this;
  53. }
  54. void Flu_Tree_Browser::IntStack::push( int i )
  55. {
  56. if( _size == _bufferSize )
  57. {
  58. // allocate a new list
  59. _bufferSize += 4;
  60. int *newList = (int*)malloc( _bufferSize*sizeof(int) );
  61. // copy the old list
  62. if( _size > 0 )
  63. memcpy( newList, _list, _size*sizeof(int) );
  64. if( _list )
  65. free( _list );
  66. _list = newList;
  67. }
  68. // add the new item
  69. _list[_size] = i;
  70. _size++;
  71. }
  72. int Flu_Tree_Browser::IntStack::pop()
  73. {
  74. if( _size == 0 )
  75. return 0;
  76. int val = _list[_size];
  77. _size--;
  78. return val;
  79. }
  80. void Flu_Tree_Browser::IntStack::clear()
  81. {
  82. if( _list )
  83. free( _list );
  84. _list = NULL;
  85. _size = _bufferSize = 0;
  86. }
  87. Flu_Tree_Browser::NodeList::NodeList()
  88. {
  89. _nodes = NULL;
  90. _nNodes = _size = 0;
  91. }
  92. Flu_Tree_Browser::NodeList::~NodeList()
  93. {
  94. clear();
  95. }
  96. typedef Flu_Tree_Browser::Node* NodeP;
  97. bool Flu_Tree_Browser::NodeList::search( const char *n, int &index )
  98. {
  99. index = _nNodes;
  100. if( _nNodes == 0 )
  101. return false;
  102. // we know we have at least one node. so use it to get the RData struct to find out what
  103. // the insertion mode is
  104. int iMode = _nodes[0]->tree->insertion_mode();
  105. if( iMode == FLU_INSERT_SORTED || iMode == FLU_INSERT_SORTED_REVERSE )
  106. return( binSearch( n, index ) );
  107. else
  108. return( linSearch( n, index ) );
  109. }
  110. bool Flu_Tree_Browser::NodeList::search( Node *n, int &index )
  111. {
  112. index = _nNodes;
  113. if( _nNodes == 0 )
  114. return false;
  115. // we know we have at least one node. so use it to get the RData struct to find out what
  116. // the insertion mode is
  117. int iMode = _nodes[0]->tree->insertion_mode();
  118. if( iMode == FLU_INSERT_SORTED || iMode == FLU_INSERT_SORTED_REVERSE )
  119. return( binSearch( n, index ) );
  120. else
  121. return( linSearch( n, index ) );
  122. }
  123. bool Flu_Tree_Browser::NodeList::linSearch( const char *n, int &index )
  124. {
  125. index = _nNodes;
  126. for( int i = 0; i < _nNodes; i++ )
  127. {
  128. if( strcmp( n, _nodes[i]->label() ) == 0 )
  129. {
  130. index = i;
  131. return true;
  132. }
  133. }
  134. return false;
  135. }
  136. bool Flu_Tree_Browser::NodeList::linSearch( Node *n, int &index )
  137. {
  138. index = _nNodes;
  139. for( int i = 0; i < _nNodes; i++ )
  140. {
  141. if( n == _nodes[i] )
  142. {
  143. index = i;
  144. return true;
  145. }
  146. }
  147. return false;
  148. }
  149. bool Flu_Tree_Browser::NodeList::binSearch( Node *n, int &index )
  150. {
  151. if( binSearch( n->label(), index ) )
  152. {
  153. // the search found the first node with the label. since there are identical entries
  154. // allowed, it may not be the actual node we want. therefore search forward until we find it
  155. for( ; index < _nNodes; index++ )
  156. if( _nodes[index] == n )
  157. return true;
  158. return false;
  159. }
  160. else
  161. return false;
  162. }
  163. bool Flu_Tree_Browser::NodeList::binSearch( const char *n, int &index )
  164. {
  165. // do a binary search for a child with name == "n"
  166. // return true if the child is found, and store its index in "index"
  167. // return false if the child is not found, and store the index it would
  168. // be in in "index"
  169. // special case: no nodes
  170. if( _nNodes == 0 )
  171. {
  172. index = 0;
  173. return false;
  174. }
  175. // we know we have at least one node. so use it to get the RData struct to find out what
  176. // the insertion mode is
  177. int iMode = _nodes[0]->tree->insertion_mode();
  178. // special case: 1 node
  179. if( _nNodes == 1 )
  180. {
  181. int val = strcmp( n, _nodes[0]->label() );
  182. if( iMode == FLU_INSERT_SORTED_REVERSE )
  183. val *= -1;
  184. if( val == 0 )
  185. {
  186. index = 0;
  187. return true;
  188. }
  189. else if( val < 0 )
  190. index = 0;
  191. else
  192. index = 1;
  193. return false;
  194. }
  195. int first = 0, last = _nNodes - 1;
  196. int val1, val2, mVal;
  197. for(;;)
  198. {
  199. // the range is down to 2 nodes
  200. if( last == first + 1 )
  201. {
  202. val1 = strcmp( n, _nodes[first]->label() );
  203. if( iMode == FLU_INSERT_SORTED_REVERSE )
  204. val1 = -val1;
  205. if( val1 < 0 )
  206. {
  207. index = first;
  208. return false;
  209. }
  210. else if( val1 == 0 )
  211. {
  212. index = first;
  213. break;
  214. }
  215. val2 = strcmp( n, _nodes[last]->label() );
  216. if( iMode == FLU_INSERT_SORTED_REVERSE )
  217. val2 = -val2;
  218. if( val2 < 0 )
  219. {
  220. index = last;
  221. return false;
  222. }
  223. else if( val2 == 0 )
  224. {
  225. index = last;
  226. break;
  227. }
  228. else
  229. {
  230. index = last+1;
  231. return false;
  232. }
  233. }
  234. // pick which half of the array to search next
  235. int midpoint = first + ((last-first)>>1);
  236. mVal = strcmp( n, _nodes[midpoint]->label() );
  237. if( iMode == FLU_INSERT_SORTED_REVERSE )
  238. mVal = -mVal;
  239. if( mVal < 0 )
  240. last = midpoint;
  241. else if( mVal > 0 )
  242. first = midpoint;
  243. else
  244. {
  245. index = midpoint;
  246. break;
  247. }
  248. }
  249. // we found *a* node equal to "n", now find the first node equal to "n"
  250. // by searching until we hit a node not equal to "n"
  251. for( first = index; first > 0; first-- )
  252. if( strcmp( n, _nodes[first-1]->label() ) != 0 )
  253. break;
  254. index = first;
  255. return true;
  256. }
  257. int Flu_Tree_Browser::NodeList::compareNodes( const void *arg1, const void* arg2 )
  258. {
  259. Flu_Tree_Browser::Node *n1 = *((Flu_Tree_Browser::Node**)arg1), *n2 = *((Flu_Tree_Browser::Node**)arg2);
  260. return strcmp( n1->text.c_str(), n2->text.c_str() );
  261. }
  262. int Flu_Tree_Browser::NodeList::reverseCompareNodes( const void *arg1, const void* arg2 )
  263. {
  264. Flu_Tree_Browser::Node *n1 = *((Flu_Tree_Browser::Node**)arg1), *n2 = *((Flu_Tree_Browser::Node**)arg2);
  265. return( -strcmp( n1->text.c_str(), n2->text.c_str() ) );
  266. }
  267. void Flu_Tree_Browser::NodeList::sort()
  268. {
  269. if( _nNodes )
  270. {
  271. // we know we have at least one node. so use it to get the RData struct to find out what
  272. // the insertion mode is
  273. int iMode = _nodes[0]->tree->insertion_mode();
  274. if( iMode == FLU_INSERT_SORTED )
  275. qsort( _nodes, _nNodes, sizeof(Node*), compareNodes );
  276. else if( iMode == FLU_INSERT_SORTED_REVERSE )
  277. qsort( _nodes, _nNodes, sizeof(Node*), reverseCompareNodes );
  278. }
  279. }
  280. Flu_Tree_Browser::Node* Flu_Tree_Browser::Node::insert( const char* fullpath, int pos )
  281. {
  282. // insert the new node at the back of the tree
  283. int imode = tree->insertion_mode();
  284. tree->insertion_mode( FLU_INSERT_BACK );
  285. Node *n = add( fullpath );
  286. tree->insertion_mode( imode );
  287. if( !n ) return NULL;
  288. // find the node at position "pos" and
  289. // move the new node before it, so it takes over position "pos"
  290. if( pos < 0 ) pos = 0;
  291. if( pos >= children() ) pos = children()-1;
  292. move( n, MOVE_BEFORE, child(pos) );
  293. return n;
  294. }
  295. Flu_Tree_Browser::Node* Flu_Tree_Browser::Node::insert_branch( const char* fullpath, int pos )
  296. {
  297. FluSimpleString p( fullpath );
  298. if( p.size() && p[p.size()-1] != '/' && p[p.size()-1] != '\\' ) p += "/";
  299. return insert( p.c_str(), pos );
  300. }
  301. Flu_Tree_Browser::Node* Flu_Tree_Browser::Node::insert_leaf( const char* fullpath, int pos )
  302. {
  303. FluSimpleString p( fullpath );
  304. if( p.size() && ( p[p.size()-1] == '/' || p[p.size()-1] == '\\' ) ) p[p.size()-1] = '\0';
  305. return insert( p.c_str(), pos );
  306. }
  307. bool Flu_Tree_Browser::Node::move( int pos )
  308. {
  309. // get this node's position
  310. int i = index();
  311. if( i == -1 ) return false;
  312. // get the node in our parent at index "pos"
  313. if( !parent() ) return false;
  314. if( pos < 0 ) pos = 0;
  315. if( pos >= parent()->children() ) pos = parent()->children()-1;
  316. Node *n = parent()->child( pos );
  317. // move this node to be before its sibling, so it takes over position "pos"
  318. return move( this, MOVE_BEFORE, n );
  319. }
  320. bool Flu_Tree_Browser::Node::swap( Node* n1, Node* n2 )
  321. {
  322. if( n1->tree != n2->tree ) return false;
  323. Node *p1 = n1->parent(), *p2 = n2->parent();
  324. if( !p1 || !p2 ) return false;
  325. int i, index1 = -1, index2 = -1;
  326. for( i = 0; i < p1->children(); i++ )
  327. {
  328. if( p1->child(i) == n1 )
  329. {
  330. index1 = i;
  331. break;
  332. }
  333. }
  334. if( index1 == -1 ) return false;
  335. for( i = 0; i < p2->children(); i++ )
  336. {
  337. if( p2->child(i) == n2 )
  338. {
  339. index2 = i;
  340. break;
  341. }
  342. }
  343. if( index2 == -1 ) return false;
  344. p1->_children._nodes[index1] = n2;
  345. p2->_children._nodes[index2] = n1;
  346. return true;
  347. }
  348. bool Flu_Tree_Browser::Node::move( Node* n1, int where, Node* n2 )
  349. {
  350. if( isMoveValid( n1, where, n2 ) )
  351. return( NodeList::move( n1, where, n2 ) );
  352. else
  353. return false;
  354. }
  355. void Flu_Tree_Browser::Node::sort()
  356. {
  357. _children.sort();
  358. for( int i = 0; i < _children.size(); i++ )
  359. _children.child(i)->sort();
  360. // since the position of each child changed in the tree,
  361. // any children with widgets should have each widget's position in their group
  362. // also change, to make event delivery match what is visible
  363. sort_widgets();
  364. }
  365. void Flu_Tree_Browser :: Node :: sort_widgets()
  366. {
  367. if( !_group || (_group->children() == 0) )
  368. return;
  369. int i;
  370. // take all the child widgets out of their group
  371. for( i = _group->children()-1; i >= 0; i-- )
  372. _group->remove( *(_group->child(i)) );
  373. // now add them back in the right order
  374. WidgetInfo *w;
  375. for( i = 0; i < _children.size(); i++ )
  376. {
  377. w = _children.child(i)->_widget;
  378. if( w && w->w )
  379. _group->add( w->w );
  380. }
  381. }
  382. bool Flu_Tree_Browser::Node::is_ancestor( Node* n )
  383. {
  384. Node *p = parent();
  385. while( p )
  386. {
  387. if( p == n )
  388. return true;
  389. else
  390. p = p->parent();
  391. }
  392. return false;
  393. }
  394. bool Flu_Tree_Browser::Node::is_descendent( Node* n )
  395. {
  396. return n->is_ancestor( this );
  397. }
  398. bool Flu_Tree_Browser::NodeList::move( Node* n1, int where, Node* n2 )
  399. {
  400. if( !n1 || !n2 )
  401. return false;
  402. if( n1->tree )
  403. n1->tree->redraw();
  404. if( n2->tree )
  405. n2->tree->redraw();
  406. // try to move n1 to the first child position of n2
  407. if( where == MOVE_INSIDE )
  408. {
  409. if( !n2->is_branch() )
  410. return false;
  411. // get the parent of n1
  412. Node* p1 = n1->parent();
  413. if( p1 )
  414. // remove n1 from its parent's list
  415. p1->_children.erase( n1 );
  416. // insert into n2
  417. int iMode = n1->tree->insertion_mode();
  418. if( iMode == FLU_INSERT_SORTED || iMode == FLU_INSERT_SORTED_REVERSE )
  419. n2->_children.add( n1 );
  420. else
  421. n2->_children.add( n1, 0 );
  422. // update the parent of n1
  423. n1->_parent = n2;
  424. return true;
  425. }
  426. // find the position of n2 in its parent's list
  427. Node* p2 = n2->parent();
  428. if( !p2 )
  429. return false;
  430. int index = 0, removed = -1;
  431. if( p2->_children.search( n2, index ) )
  432. {
  433. // get the parent of n1
  434. Node* p1 = n1->parent();
  435. if( p1 )
  436. // remove n1 from its parent's list. remember the position it was removed from
  437. removed = p1->_children.erase( n1 );
  438. // if n1 and n2 have the same parent, and if n1 came before the spot where
  439. // n2 will be inserted, then our indexing is off by one because n1 has been removed
  440. if( p1 == p2 && removed <= index )
  441. index--;
  442. if( where == MOVE_AFTER )
  443. index++;
  444. // insert n1 at the proper position
  445. p2->_children.add( n1, index );
  446. // update the parent of n1
  447. n1->_parent = p2;
  448. }
  449. return true;
  450. }
  451. void Flu_Tree_Browser::NodeList::add( Node* n, int position )
  452. {
  453. int i, index;
  454. int mode = n->tree->insertion_mode();
  455. // if the list is out of room, allocate a new one that's bigger
  456. if( _nNodes == _size )
  457. {
  458. int newSize = ( _size == 0 ) ? 1 : _size*2; // double the size of the old list (same behavior as STL vector)
  459. // allocate the new list
  460. Node** newNodes = new NodeP[ newSize ];
  461. // copy the old list to the new list
  462. memcpy( newNodes, _nodes, _nNodes*sizeof(Node*) );
  463. // delete the old list and replace it with the new list
  464. delete[] _nodes;
  465. //n->tree->rdata.cbNode = NULL;
  466. _nodes = newNodes;
  467. _size = newSize;
  468. }
  469. if( position >= 0 )
  470. {
  471. if( position > _nNodes )
  472. index = _nNodes;
  473. else
  474. index = position;
  475. }
  476. else if( mode == FLU_INSERT_SORTED || mode == FLU_INSERT_SORTED_REVERSE )
  477. {
  478. // search through the list until we find where to insert the node
  479. binSearch( n->label(), index );
  480. }
  481. else if( mode == FLU_INSERT_FRONT )
  482. {
  483. index = 0;
  484. }
  485. else if( mode == FLU_INSERT_BACK )
  486. {
  487. index = _nNodes;
  488. }
  489. else
  490. return;
  491. // shift all entries from the new insertion point down one spot
  492. // to make room for the new node
  493. for( i = _nNodes - 1; i >= index; i-- )
  494. _nodes[i+1] = _nodes[i];
  495. // add the new node
  496. _nodes[index] = n;
  497. _nNodes++;
  498. }
  499. int Flu_Tree_Browser::NodeList::erase( Node *n )
  500. {
  501. if( n == NULL )
  502. return -1;
  503. int index;
  504. if( search( n, index ) )
  505. {
  506. // move all the others down one spot to remove the node
  507. for( int i = index; i < _nNodes-1; i++ )
  508. _nodes[i] = _nodes[i+1];
  509. _nNodes--;
  510. return index;
  511. }
  512. return -1;
  513. }
  514. int Flu_Tree_Browser::NodeList::erase( const char* n )
  515. {
  516. if( _nNodes == 0 )
  517. return -1;
  518. int index;
  519. if( search( n, index ) )
  520. {
  521. // move all the others down one spot to remove the node
  522. for( int i = index; i < _nNodes-1; i++ )
  523. _nodes[i] = _nodes[i+1];
  524. _nNodes--;
  525. return index;
  526. }
  527. return -1;
  528. }
  529. void Flu_Tree_Browser::NodeList::erase( int n )
  530. {
  531. // make sure n is in range
  532. if( ( n < 0 ) || ( n >= _nNodes ) )
  533. return;
  534. // move all the others down one spot to remove the node
  535. for( int i = n; i < _nNodes-1; i++ )
  536. _nodes[i] = _nodes[i+1];
  537. _nNodes--;
  538. }
  539. void Flu_Tree_Browser::NodeList::clear()
  540. {
  541. if( _nodes )
  542. {
  543. //if( _nNodes )
  544. //if( _nodes[0] )
  545. // _nodes[0]->tree->rdata.cbNode = NULL;
  546. delete[] _nodes;
  547. }
  548. _nodes = NULL;
  549. _nNodes = _size = 0;
  550. }
  551. int Flu_Tree_Browser::NodeList::findNum( const char *n )
  552. {
  553. if( ( _nNodes == 0 ) || ( n == 0 ) )
  554. return 0;
  555. // see if there is a first node equal to "n"
  556. int index, last;
  557. if( !search( n, index ) )
  558. return 0;
  559. // now search forward until we hit a node not equal to "n"
  560. for( last = index; last < _nNodes-1; last++ )
  561. if( strcmp( n, _nodes[last+1]->label() ) != 0 )
  562. break;
  563. return last - index + 1;
  564. }
  565. Flu_Tree_Browser::Node* Flu_Tree_Browser::NodeList::find( const char* n, int which )
  566. {
  567. if( ( _nNodes == 0 ) || ( n == 0 ) || ( which == 0 ) )
  568. return NULL;
  569. // see if there is a first node equal to "n"
  570. int index, first;
  571. if( !search( n, first ) )
  572. return NULL;
  573. // now search forward and try to find the which'th node named "n"
  574. int total = 0;
  575. for( index = first; index < _nNodes; index++ )
  576. {
  577. if( strcmp( n, _nodes[index]->label() ) == 0 )
  578. {
  579. total++;
  580. if( total == which )
  581. break;
  582. }
  583. else
  584. break;
  585. }
  586. if( total != which )
  587. return NULL;
  588. return _nodes[index];
  589. }
  590. #define SCROLL_SIZE 15
  591. Flu_Tree_Browser::Flu_Tree_Browser( int x, int y, int w, int h, const char *l )
  592. : Fl_Group( x, y, w, h )
  593. #ifdef USE_FLU_DND
  594. , Flu_DND( "Flu_Tree_Browser" )
  595. #endif
  596. {
  597. //lastEvent = -1;
  598. clearing = false;
  599. autoScrollX = autoScrollY = 0.0f;
  600. #ifdef USE_FLU_DND
  601. dnd_allow_type( "Flu_Tree_Browser" );
  602. dnd_allow_type( "DND_Object" );
  603. dnd_allow_text( true );
  604. #endif
  605. // add some widgets
  606. _box = new Fl_Group( x, y, w-SCROLL_SIZE, h-SCROLL_SIZE );
  607. //_box->resizable( NULL );
  608. _box->end();
  609. //_box->set_output();
  610. scrollV = new Fl_Scrollbar( x+w-SCROLL_SIZE, y, SCROLL_SIZE, h-SCROLL_SIZE );
  611. scrollV->type( FL_VERTICAL );
  612. scrollV->callback( _scrollCB, this );
  613. scrollV->value( 0, 1, 0, 0 );
  614. scrollH = new Fl_Scrollbar( x, y+h-SCROLL_SIZE, w-SCROLL_SIZE, SCROLL_SIZE );
  615. scrollH->type( FL_HORIZONTAL );
  616. scrollH->callback( _scrollCB, this );
  617. scrollH->value( 0, 1, 0, 0 );
  618. scrollBox = new Fl_Group( x+w-SCROLL_SIZE, y+h-SCROLL_SIZE, SCROLL_SIZE, SCROLL_SIZE );
  619. scrollBox->box( FL_UP_BOX );
  620. scrollBox->end();
  621. resizable( _box );
  622. // set up the recursive data structure
  623. memset( &rdata, 0, sizeof(rdata) );
  624. rdata.root = &root;
  625. root.tree = this;
  626. rdata.cbNode = NULL;
  627. rdata.cbReason = FLU_NOTHING;
  628. rdata.tree = this;
  629. rdata.dragging = false;
  630. rdata.forceResize = true;
  631. rdata.shiftSelect = false;
  632. rdata.shiftSelectAll = false;
  633. rdata.nextId = 1;
  634. rdata.searchIndex = 1;
  635. rdata.defaultCollapseIcons[0] = new Fl_Pixmap( (char*const*)plus_xpm );
  636. rdata.defaultCollapseIcons[1] = new Fl_Pixmap( (char*const*)minus_xpm );
  637. rdata.defaultBranchIcons[0] = new Fl_Pixmap( (char*const*)folder_closed_xpm );
  638. rdata.defaultBranchIcons[1] = new Fl_Pixmap( (char*const*)folder_open_xpm );
  639. end();
  640. // set the default values for the tree
  641. selection_follows_hilight( false );
  642. select_under_mouse( false );
  643. open_without_children( true );
  644. auto_branches( false );
  645. animate( false );
  646. collapse_time( 0.1f );
  647. double_click_opens( true );
  648. move_only_same_group( false );
  649. frame_rate( 100.0f );
  650. allow_leaf_duplication( true );
  651. allow_branch_duplication( false );
  652. shaded_entry_colors( FL_WHITE, FL_WHITE );
  653. collapse_icons( NULL, NULL );
  654. //branch_icons( NULL, NULL );
  655. rdata.branchIcons[0] = rdata.defaultBranchIcons[0];
  656. rdata.branchIcons[1] = rdata.defaultBranchIcons[1];
  657. leaf_icon( NULL );
  658. branch_text( FL_BLACK, FL_HELVETICA_BOLD, 12 );
  659. leaf_text( FL_BLACK, FL_HELVETICA, 12 );
  660. //callback( NULL );
  661. when( FL_WHEN_CHANGED );
  662. color( FL_WHITE );
  663. selection_color( FL_SELECTION_COLOR );
  664. box( FL_FLAT_BOX );
  665. connector_style( FL_DARK2, FL_DOT );
  666. selection_mode( FLU_MULTI_SELECT );
  667. selection_drag_mode( FLU_DRAG_TO_SELECT );
  668. insertion_mode( FLU_INSERT_SORTED );
  669. show_connectors( true );
  670. show_root( true );
  671. show_leaves( true );
  672. show_branches( true );
  673. open_on_select( false );
  674. //root_always_open( false );
  675. horizontal_gap( 2 );
  676. vertical_gap( 0 );
  677. widget_gap( 2 );
  678. set_root( l );
  679. resize( x, y, w, h );
  680. }
  681. Flu_Tree_Browser::~Flu_Tree_Browser()
  682. {
  683. Fl::remove_timeout( _timerRedrawCB, this );
  684. Fl::remove_timeout( _timerScrollCB, this );
  685. delete rdata.defaultCollapseIcons[0];
  686. delete rdata.defaultCollapseIcons[1];
  687. delete rdata.defaultBranchIcons[0];
  688. delete rdata.defaultBranchIcons[1];
  689. }
  690. void Flu_Tree_Browser::auto_branches( bool b )
  691. {
  692. rdata.autoBranches = b;
  693. }
  694. void Flu_Tree_Browser::collapse_icons( Fl_Image *closed, Fl_Image *open )
  695. {
  696. if( closed )
  697. rdata.collapseIcons[0] = closed;
  698. else
  699. rdata.collapseIcons[0] = rdata.defaultCollapseIcons[0];
  700. if( open )
  701. rdata.collapseIcons[1] = open;
  702. else
  703. rdata.collapseIcons[1] = rdata.defaultCollapseIcons[1];
  704. }
  705. void Flu_Tree_Browser::branch_icons( Fl_Image *closed, Fl_Image *open )
  706. {
  707. //if( closed )
  708. rdata.branchIcons[0] = closed;
  709. //else
  710. //rdata.branchIcons[0] = rdata.defaultBranchIcons[0];
  711. //if( open )
  712. rdata.branchIcons[1] = open;
  713. //else
  714. //rdata.branchIcons[1] = rdata.defaultBranchIcons[1];
  715. }
  716. void Flu_Tree_Browser::set_default_branch_icons()
  717. {
  718. rdata.branchIcons[0] = rdata.defaultBranchIcons[0];
  719. rdata.branchIcons[1] = rdata.defaultBranchIcons[1];
  720. }
  721. void Flu_Tree_Browser::leaf_icon( Fl_Image *icon )
  722. {
  723. rdata.leafIcon = icon;
  724. }
  725. bool Flu_Tree_Browser::inside_entry_area( int x, int y )
  726. {
  727. if( scrollH->visible() && scrollV->visible() )
  728. return( x > _box->x() && y > _box->y() &&
  729. x < (_box->x()+_box->w()-scrollV->w()) &&
  730. y < (_box->y()+_box->h()-scrollH->h()) );
  731. else if( !scrollH->visible() && !scrollV->visible() )
  732. return( x > _box->x() && y > _box->y() &&
  733. x < (_box->x()+_box->w()) &&
  734. y < (_box->y()+_box->h()) );
  735. else if( scrollH->visible() )
  736. return( x > _box->x() && y > _box->y() &&
  737. x < (_box->x()+_box->w()) &&
  738. y < (_box->y()+_box->h()-scrollH->h()) );
  739. else
  740. return( x > _box->x() && y > _box->y() &&
  741. x < (_box->x()+_box->w()-scrollV->w()) &&
  742. y < (_box->y()+_box->h()) );
  743. }
  744. void Flu_Tree_Browser::resize( int X, int Y, int W, int H )
  745. {
  746. Fl_Group::resize( X, Y, W, H );
  747. int dx = Fl::box_dx(box()), dy = Fl::box_dy(box()), dw = Fl::box_dw(box()), dh = Fl::box_dh(box());
  748. rdata.x = X+dx;
  749. rdata.y = Y+dy;
  750. rdata.totalW = rdata.x;
  751. root.recurse( rdata, Node::MEASURE );
  752. rdata.totalW -= X-dx;
  753. rdata.totalH = rdata.y - Y-dy;
  754. // if the size of the tree is bigger than the window, turn on the scrollbars
  755. bool hOn = false, vOn = false;
  756. if( rdata.totalW > W-dw )
  757. hOn = true;
  758. if( rdata.totalH > H-dh )
  759. vOn = true;
  760. // check if turning on one scrollbar actually forces the other to turn on
  761. if( hOn && ( rdata.totalH > H-SCROLL_SIZE ) )
  762. vOn = true;
  763. if( vOn && ( rdata.totalW > W-SCROLL_SIZE ) )
  764. hOn = true;
  765. // now resize the other kids depending on the state of the scrollbars
  766. _box->resize( X, Y, W, H );
  767. if( hOn && vOn ) // both scrollbars on
  768. {
  769. scrollH->resize( X+dx, Y+H-SCROLL_SIZE-dy, W-SCROLL_SIZE-dw, SCROLL_SIZE );
  770. scrollH->show();
  771. scrollV->resize( X+W-SCROLL_SIZE-dx, Y+dy, SCROLL_SIZE, H-SCROLL_SIZE-dh );
  772. scrollV->show();
  773. scrollBox->resize( X+W-SCROLL_SIZE-dx, Y+H-SCROLL_SIZE-dy, SCROLL_SIZE, SCROLL_SIZE );
  774. scrollBox->show();
  775. // set the scrollbar sizes and values
  776. int hDelta = rdata.totalW - W+dw + SCROLL_SIZE, scrollHW = scrollH->w()-SCROLL_SIZE-SCROLL_SIZE;
  777. hDelta = MAX( hDelta, 0 );
  778. scrollH->value( MIN( scrollH->value(), hDelta ), 1, 0, hDelta );
  779. scrollH->slider_size( MAX( (float)SCROLL_SIZE/float(scrollHW), float(scrollHW-hDelta)/float(scrollHW) ) );
  780. int vDelta = rdata.totalH - H+dh + SCROLL_SIZE, scrollVH = scrollV->h()-SCROLL_SIZE-SCROLL_SIZE;
  781. vDelta = MAX( vDelta, 0 );
  782. scrollV->value( MIN( scrollV->value(), vDelta ), 1, 0, vDelta );
  783. scrollV->slider_size( MAX( (float)SCROLL_SIZE/float(scrollVH), float(scrollVH-vDelta)/float(scrollVH) ) );
  784. _box->resize( X, Y, W-SCROLL_SIZE, H-SCROLL_SIZE );
  785. }
  786. else if( !hOn && !vOn ) // neither on
  787. {
  788. scrollH->hide();
  789. scrollV->hide();
  790. scrollBox->hide();
  791. }
  792. else if( hOn ) // just horizontal on
  793. {
  794. scrollH->resize( X+dx, Y+H-SCROLL_SIZE-dy, W-dw, SCROLL_SIZE );
  795. scrollH->show();
  796. scrollV->hide();
  797. scrollBox->hide();
  798. // set the scrollbar size and value
  799. int hDelta = rdata.totalW - W+dw, scrollHW = scrollH->w()-SCROLL_SIZE-SCROLL_SIZE;
  800. hDelta = MAX( hDelta, 0 );
  801. scrollH->value( MIN( scrollH->value(), hDelta ), 1, 0, hDelta );
  802. scrollH->slider_size( MAX( (float)SCROLL_SIZE/float(scrollHW), float(scrollHW-hDelta)/float(scrollHW) ) );
  803. _box->resize( X, Y, W, H-SCROLL_SIZE );
  804. }
  805. else if( vOn ) // just vertical on
  806. {
  807. scrollH->hide();
  808. scrollV->resize( X+W-SCROLL_SIZE-dx, Y+dy, SCROLL_SIZE, H-dh );
  809. scrollV->show();
  810. scrollBox->hide();
  811. // set the scrollbar size and value
  812. int vDelta = rdata.totalH - H+dh, scrollVH = scrollV->h()-SCROLL_SIZE-SCROLL_SIZE;
  813. vDelta = MAX( vDelta, 0 );
  814. scrollV->value( MIN( scrollV->value(), vDelta ), 1, 0, vDelta );
  815. scrollV->slider_size( MAX( (float)SCROLL_SIZE/float(scrollVH), float(scrollVH-vDelta)/float(scrollVH) ) );
  816. _box->resize( X, Y, W-SCROLL_SIZE, H );
  817. }
  818. rdata.browserX = _box->x() + dx;
  819. rdata.browserY = _box->y() + dy;
  820. rdata.browserW = _box->w() - dw;
  821. rdata.browserH = _box->h() - dh;
  822. redraw();
  823. rdata.forceResize = true; // weird hack to get the scrollbars to turn on right the first time
  824. }
  825. void Flu_Tree_Browser::on_dnd_leave()
  826. {
  827. #ifdef USE_FLU_DND
  828. rdata.isMoveValid = false;
  829. redraw();
  830. // X
  831. if( scrollH->visible() )
  832. {
  833. float max = 0.01f * (scrollH->maximum() - scrollH->minimum());
  834. if( max < 10.0f ) max = 10.0f;
  835. if( autoScrollX > 0.0f )
  836. autoScrollX = max;
  837. else
  838. autoScrollX = -max;
  839. }
  840. // Y
  841. if( scrollV->visible() )
  842. {
  843. float max = 0.01f * (scrollV->maximum() - scrollV->minimum());
  844. if( max < 10.0f ) max = 10.0f;
  845. if( autoScrollY > 0.0f )
  846. autoScrollY = max;
  847. else
  848. autoScrollY = -max;
  849. }
  850. #endif
  851. }
  852. bool Flu_Tree_Browser::on_dnd_drag( int X, int Y )
  853. {
  854. #ifdef USE_FLU_DND
  855. rdata.dragging = true;
  856. autoScrollX = autoScrollY = 0.0f;
  857. if( scrollH->visible() )
  858. {
  859. // auto-scroll the horizontal scrollbars based on how close to the left or right of the browser the mouse is
  860. float min = 1.0f, max = 0.01f * (scrollH->maximum() - scrollH->minimum());
  861. if( max < 10.0f ) max = 10.0f;
  862. if( X < (x()+30) ) // left
  863. {
  864. float t = float((x()+30) - X) / 30.0f;
  865. autoScrollX = -LERP( t*t, min, max );
  866. if( !scrolledTimerOn )
  867. Fl::add_timeout( 0.0, _timerScrollCB, this );
  868. }
  869. else if( X > (x()+w()-30) ) // right
  870. {
  871. float t = float(X - (x()+w()-30)) / 30.0f;
  872. autoScrollX = LERP( t*t, min, max );
  873. if( !scrolledTimerOn )
  874. Fl::add_timeout( 0.0, _timerScrollCB, this );
  875. }
  876. }
  877. if( scrollV->visible() )
  878. {
  879. // auto-scroll the vertical scrollbars based on how close to the top or bottom of the browser the mouse is
  880. float min = 1.0f, max = 0.01f * (scrollV->maximum() - scrollV->minimum());
  881. if( max < 10.0f ) max = 10.0f;
  882. if( Y < (y()+30) ) // top
  883. {
  884. float t = float((y()+30) - Y) / 30.0f;
  885. autoScrollY = -LERP( t*t, min, max );
  886. if( !scrolledTimerOn )
  887. Fl::add_timeout( 0.0, _timerScrollCB, this );
  888. }
  889. else if( Y > (y()+h()-30) ) // bottom
  890. {
  891. float t = float(Y - (y()+h()-30)) / 30.0f;
  892. autoScrollY = LERP( t*t, min, max );
  893. if( !scrolledTimerOn )
  894. Fl::add_timeout( 0.0, _timerScrollCB, this );
  895. }
  896. }
  897. if( autoScrollX == 0.0f && autoScrollY == 0.0f )
  898. {
  899. Fl::remove_timeout( _timerScrollCB, this );
  900. scrolledTimerOn = false;
  901. }
  902. int dx = Fl::box_dx(box()), dy = Fl::box_dy(box());
  903. rdata.x = x()+dx;
  904. rdata.y = y()+dy;
  905. if( scrollH->visible() )
  906. rdata.x -= scrollH->value();
  907. if( scrollV->visible() )
  908. rdata.y -= scrollV->value();
  909. rdata.delta = 0;
  910. root.recurse( rdata, Node::HANDLE, FL_DND_DRAG );
  911. rdata.isMoveValid = Fl::event_inside( this ) && Node::isMoveValid( rdata.grabbed, rdata.dragWhere, rdata.dragNode );
  912. redraw();
  913. Fl::flush();
  914. if( rdata.isMoveValid )
  915. return true;
  916. else
  917. #endif
  918. return false;
  919. }
  920. void Flu_Tree_Browser::on_dnd_release()
  921. {
  922. #ifdef USE_FLU_DND
  923. Fl::remove_timeout( _timerScrollCB, this );
  924. scrolledTimerOn = false;
  925. redraw();
  926. Fl::flush();
  927. #endif
  928. }
  929. void Flu_Tree_Browser::on_dnd_drop( const Flu_DND_Event *e )
  930. {
  931. #ifdef USE_FLU_DND
  932. bool newNode = false;
  933. if( !rdata.isMoveValid )
  934. rdata.grabbed = 0;
  935. else if( e->event_is_text() && rdata.dnd )
  936. {
  937. // create a new node with the text as the name and make it the grabbed node
  938. rdata.grabbed = new Node( true, e->text(), NULL, rdata, NULL, true );
  939. if( rdata.grabbed )
  940. newNode = true;
  941. }
  942. else
  943. {
  944. if( e->is_data_type( "Flu_Tree_Browser" ) )
  945. {
  946. if( rdata.moveOnlySameGroup && ( rdata.grabbed->parent() != rdata.dragNode->parent() ) )
  947. rdata.grabbed = NULL;
  948. }
  949. else if( e->is_data_type( "DND_Object" ) && rdata.dnd )
  950. {
  951. // create a new node with the text as the name and make it the grabbed node
  952. DND_Object *o = (DND_Object*)e->data();
  953. rdata.grabbed = new Node( true, o->name(), NULL, rdata, NULL, true );
  954. if( rdata.grabbed )
  955. {
  956. rdata.grabbed->user_data( e->data() );
  957. newNode = true;
  958. }
  959. }
  960. else
  961. rdata.grabbed = NULL;
  962. }
  963. // select only the new/moved node
  964. root.unselect_all( rdata.grabbed );
  965. set_hilighted( rdata.grabbed );
  966. if( rdata.grabbed )
  967. {
  968. rdata.grabbed->select( true );
  969. // move the node
  970. if( NodeList::move( rdata.grabbed, rdata.dragWhere, rdata.dragNode ) )
  971. {
  972. if( newNode )
  973. rdata.grabbed->do_callback( FLU_NEW_NODE );
  974. else
  975. rdata.grabbed->do_callback( FLU_MOVED_NODE );
  976. }
  977. rdata.forceResize = true;
  978. }
  979. Fl::focus(this);
  980. rdata.dragging = false;
  981. rdata.grabbed = 0;
  982. rdata.dragNode = 0;
  983. Fl::remove_timeout( _timerScrollCB, this );
  984. scrolledTimerOn = false;
  985. redraw();
  986. #endif
  987. }
  988. int Flu_Tree_Browser::handle( int event )
  989. {
  990. #ifdef USE_FLU_DND
  991. if( dnd_handle( event ) )
  992. return 1;
  993. #endif
  994. if( event == FL_NO_EVENT )//|| event == FL_MOVE )
  995. return 0;
  996. if( event == FL_FOCUS )//&& rdata.lastHilighted )
  997. {
  998. //set_hilighted( rdata.lastHilighted );
  999. //lastEvent = event;
  1000. //Fl_Group::handle( event );
  1001. redraw();
  1002. return 1;
  1003. }
  1004. if( event == FL_UNFOCUS )
  1005. {
  1006. //if( lastEvent != FL_LEAVE )
  1007. //{
  1008. //rdata.lastHilighted = rdata.hilighted;
  1009. //}
  1010. //set_hilighted( NULL );
  1011. //lastEvent = event;
  1012. Fl_Group::handle( event );
  1013. redraw();
  1014. return 1;
  1015. }
  1016. if( !rdata.dragging && !( event == FL_MOVE && rdata.selectUnderMouse ) )
  1017. {
  1018. if( ! (event == FL_MOVE || event == FL_ENTER || event == FL_LEAVE ) )
  1019. _box->redraw();
  1020. if( Fl_Group::handle( event ) )
  1021. {
  1022. //if( event == FL_KEYDOWN || event == FL_KEYUP )
  1023. // redraw();
  1024. return 1;
  1025. }
  1026. //if (scrollV && Fl::event_inside(scrollV) && scrollV->handle(event)) return 1;
  1027. //if (scrollH && Fl::event_inside(scrollH) && scrollH->handle(event)) return 1;
  1028. }
  1029. if( event == FL_RELEASE )
  1030. {
  1031. //Fl::focus(this);
  1032. rdata.dragging = false;
  1033. rdata.grabbed = 0;
  1034. rdata.dragNode = 0;
  1035. //redraw();
  1036. }
  1037. int dx = Fl::box_dx(box()), dy = Fl::box_dy(box());
  1038. // set some initial values for the recursive data structure
  1039. // account for the scrollbar positions
  1040. rdata.x = x()+dx;
  1041. rdata.y = y()+dy;
  1042. if( scrollH->visible() )
  1043. rdata.x -= scrollH->value();
  1044. if( scrollV->visible() )
  1045. rdata.y -= scrollV->value();
  1046. rdata.previous = NULL;
  1047. rdata.delta = 0;
  1048. rdata.visibilityChanged = false;
  1049. // catch cursor keys for moving the hilighted entry or selecting all entries
  1050. if( event == FL_KEYDOWN )
  1051. {
  1052. int key = Fl::event_key();
  1053. // move hilighted entry up
  1054. if( key == FL_Up )
  1055. {
  1056. rdata.delta = -1;
  1057. Fl::focus(this);
  1058. //redraw();
  1059. }
  1060. // move hilighted entry down
  1061. else if( key == FL_Down )
  1062. {
  1063. rdata.delta = 1;
  1064. Fl::focus(this);
  1065. //redraw();
  1066. }
  1067. // select all
  1068. else if( Fl::event_state(FL_CTRL) && key == 'a' )
  1069. {
  1070. select_all();
  1071. Fl::focus(this);
  1072. redraw();
  1073. return 1;
  1074. }
  1075. // check for the Home key
  1076. else if( key == FL_Home )
  1077. {
  1078. // set the hilighted entry to be the first entry
  1079. if( rdata.showRoot || ( rdata.root->_children.size() == 0 ) )
  1080. set_hilighted( rdata.root );
  1081. else if( rdata.root->_children.size() > 0 )
  1082. set_hilighted( rdata.root->_children.child(0) );
  1083. //redraw();
  1084. }
  1085. // check for the End key
  1086. else if( key == FL_End )
  1087. {
  1088. // set the hilighted entry to be the last visible entry
  1089. if( rdata.showRoot && ( rdata.root->_children.size() == 0 ) )
  1090. set_hilighted( rdata.root );
  1091. else
  1092. {
  1093. // find the last node by repeatedly looking for the last child until there are no more branches
  1094. Node *n = &root;
  1095. while( n->_children.size() && n->open() )
  1096. n = n->_children.child( n->_children.size()-1 );
  1097. set_hilighted( n );
  1098. }
  1099. //redraw();
  1100. }
  1101. }
  1102. // pass the event down the tree
  1103. int val = root.recurse( rdata, Node::HANDLE, event );
  1104. if( val )
  1105. {
  1106. //redraw();
  1107. if( rdata.visibilityChanged )
  1108. {
  1109. root.determineVisibility();
  1110. scrollV->redraw();
  1111. scrollH->redraw();
  1112. }
  1113. if( val == 1 )
  1114. return 1;
  1115. }
  1116. // special case: if multi-select or single-select and user clicks on no items, unselect all items
  1117. else if( (rdata.selectionMode != FLU_NO_SELECT) && (event == FL_PUSH) && (!Fl::event_state(FL_CTRL)) )
  1118. {
  1119. unselect_all();
  1120. set_hilighted( NULL );
  1121. rdata.forceResize = true;
  1122. redraw();
  1123. return 1;
  1124. }
  1125. if( event == FL_SHOW || event == FL_HIDE )
  1126. root.determineVisibility();
  1127. return Fl_Group::handle( event );
  1128. //return 0;
  1129. }
  1130. void Flu_Tree_Browser::insertion_mode( int m )
  1131. {
  1132. rdata.insertionMode = m;
  1133. root.sort();
  1134. }
  1135. void Flu_Tree_Browser::set_hilighted( Flu_Tree_Browser::Node* n )
  1136. {
  1137. if( rdata.hilighted == n && when() != FL_WHEN_NOT_CHANGED )
  1138. return;
  1139. if( rdata.hilighted )
  1140. rdata.hilighted->do_callback( FLU_UNHILIGHTED );
  1141. rdata.hilighted = n;
  1142. if( rdata.hilighted )
  1143. rdata.hilighted->do_callback( FLU_HILIGHTED );
  1144. if( rdata.hilighted )
  1145. {
  1146. if( rdata.selectionFollowsHilight )
  1147. {
  1148. if( rdata.selectionMode == FLU_SINGLE_SELECT )
  1149. unselect_all();
  1150. rdata.hilighted->select( true );
  1151. }
  1152. int extraH = scrollH->visible() ? scrollH->h() : 0;
  1153. // if the hilighted entry is below the visible bounds of the browser, move the vertical scrollbar
  1154. // so the hilighted entry is the last visible entry
  1155. if( rdata.hilighted->currentY-y()+rdata.hilighted->currentH > scrollV->value()+h()-extraH )
  1156. ((Fl_Valuator*)scrollV)->value( rdata.hilighted->currentY-y() - h()+extraH + rdata.hilighted->currentH );
  1157. // if the hilighted entry is above the visible bounds of the browser, move the vertical scrollbar
  1158. // so the hilighted entry is the first visible entry
  1159. if( rdata.hilighted->currentY-y() < scrollV->value() )
  1160. ((Fl_Valuator*)scrollV)->value( rdata.hilighted->currentY-y() );
  1161. }
  1162. redraw();
  1163. }
  1164. int Flu_Tree_Browser::num_selected()
  1165. {
  1166. return root.recurse( rdata, Node::COUNT_SELECTED );
  1167. }
  1168. int Flu_Tree_Browser::Node::num_selected()
  1169. {
  1170. return recurse( tree->rdata, COUNT_SELECTED );
  1171. }
  1172. Flu_Tree_Browser::Node* Flu_Tree_Browser::get_selected( int index )
  1173. {
  1174. return root.get_selected( index );
  1175. }
  1176. Flu_Tree_Browser::Node* Flu_Tree_Browser::Node::get_selected( int index )
  1177. {
  1178. tree->rdata.counter = 0;
  1179. tree->rdata.searchIndex = index;
  1180. Node *n = modify( 0, GET_SELECTED, tree->rdata );
  1181. tree->rdata.searchIndex = 1;
  1182. return n;
  1183. }
  1184. Flu_Tree_Browser::Node::Node( const char *lbl )
  1185. {
  1186. flags = 0;
  1187. userData = 0;
  1188. _parent = 0;
  1189. _widget = 0;
  1190. _group = 0;
  1191. tree = 0;
  1192. SET(ACTIVE);
  1193. CLEAR(LEAF);
  1194. _id = 0;
  1195. CLEAR(ALWAYS_OPEN);
  1196. SET(COLLAPSED);
  1197. SET(MOVABLE);
  1198. SET(DROPPABLE);
  1199. currentY = currentH = 0;
  1200. totalChildH = 0;
  1201. CLEAR(SELECTED);
  1202. CLEAR(EXPAND_TO_WIDTH);
  1203. SET(SHOW_LABEL);
  1204. if( lbl == 0 )
  1205. text = "";
  1206. else
  1207. text = lbl;
  1208. cIcon[0] = cIcon[1] = bIcon[0] = bIcon[1] = lIcon = 0;
  1209. }
  1210. Flu_Tree_Browser::Node::Node( bool l, const char* n, Node *p, RData &rdata, Fl_Widget *w, bool showLbl )
  1211. {
  1212. _group = 0;
  1213. flags = 0;
  1214. userData = 0;
  1215. SET(LEAF,l);
  1216. text = n;
  1217. _id = 0;
  1218. SET(ACTIVE);
  1219. _parent = p;
  1220. CLEAR(ALWAYS_OPEN);
  1221. SET(COLLAPSED);
  1222. CLEAR(SELECTED);
  1223. CLEAR(EXPAND_TO_WIDTH);
  1224. SET(MOVABLE);
  1225. SET(DROPPABLE);
  1226. _widget = 0;
  1227. totalChildH = 0;
  1228. currentY = currentH = 0;
  1229. cIcon[0] = cIcon[1] = bIcon[0] = bIcon[1] = lIcon = 0;
  1230. SET( SHOW_LABEL, showLbl );
  1231. tree = rdata.tree;
  1232. initType();
  1233. _id = rdata.nextId++;
  1234. widget( w );
  1235. }
  1236. void Flu_Tree_Browser::Node::initType()
  1237. {
  1238. if( is_leaf() )
  1239. {
  1240. lIcon = tree->rdata.leafIcon;
  1241. textColor = tree->rdata.defLeafColor;
  1242. textFont = tree->rdata.defLeafFont;
  1243. textSize = tree->rdata.defLeafSize;
  1244. }
  1245. else
  1246. {
  1247. cIcon[0] = tree->rdata.collapseIcons[0];
  1248. cIcon[1] = tree->rdata.collapseIcons[1];
  1249. bIcon[0] = tree->rdata.branchIcons[0];
  1250. bIcon[1] = tree->rdata.branchIcons[1];
  1251. textColor = tree->rdata.defBranchColor;
  1252. textFont = tree->rdata.defBranchFont;
  1253. textSize = tree->rdata.defBranchSize;
  1254. }
  1255. }
  1256. Flu_Tree_Browser::Node::~Node()
  1257. {
  1258. // if this node is in a tree, make sure it isn't holding a reference to us
  1259. if( tree )
  1260. {
  1261. if( tree->rdata.hilighted == this ) tree->rdata.hilighted = NULL;
  1262. //if( tree->rdata.lastHilighted == this ) tree->rdata.lastHilighted = NULL;
  1263. if( tree->rdata.grabbed == this ) tree->rdata.grabbed = NULL;
  1264. if( tree->rdata.dragNode == this ) tree->rdata.dragNode = NULL;
  1265. }
  1266. clear();
  1267. }
  1268. Flu_Tree_Browser::Node* Flu_Tree_Browser::Node::first()
  1269. {
  1270. return this;
  1271. }
  1272. Flu_Tree_Browser::Node* Flu_Tree_Browser::Node::first_branch()
  1273. {
  1274. Node *n = first();
  1275. while( n )
  1276. {
  1277. if( n->is_branch() )
  1278. return n;
  1279. else
  1280. n = n->next();
  1281. }
  1282. return NULL;
  1283. }
  1284. Flu_Tree_Browser::Node* Flu_Tree_Browser::Node::first_leaf()
  1285. {
  1286. Node *n = first();
  1287. while( n )
  1288. {
  1289. if( n->is_leaf() )
  1290. return n;
  1291. else
  1292. n = n->next();
  1293. }
  1294. return NULL;
  1295. }
  1296. Flu_Tree_Browser::Node* Flu_Tree_Browser::Node::last()
  1297. {
  1298. if( children() == 0 )
  1299. return this;
  1300. else
  1301. return( child( children() - 1 )->last() );
  1302. }
  1303. Flu_Tree_Browser::Node* Flu_Tree_Browser::Node::last_branch()
  1304. {
  1305. Node *n = last();
  1306. while( n )
  1307. {
  1308. if( n->is_branch() )
  1309. return n;
  1310. else
  1311. n = n->previous();
  1312. }
  1313. return NULL;
  1314. }
  1315. Flu_Tree_Browser::Node* Flu_Tree_Browser::Node::last_leaf()
  1316. {
  1317. Node *n = last();
  1318. while( n )
  1319. {
  1320. if( n->is_leaf() )
  1321. return n;
  1322. else
  1323. n = n->previous();
  1324. }
  1325. return NULL;
  1326. }
  1327. Flu_Tree_Browser::Node* Flu_Tree_Browser::Node::next_sibling()
  1328. {
  1329. if( is_root() )
  1330. return NULL;
  1331. int index;
  1332. for( index = 0; index < _parent->children(); index++ )
  1333. if( _parent->child(index) == this )
  1334. break;
  1335. // if we are the last child of our parent, then we have no next sibling
  1336. if( index == _parent->children()-1 )
  1337. return NULL;
  1338. // otherwise return our next sibling
  1339. else
  1340. return( _parent->child(index+1) );
  1341. }
  1342. Flu_Tree_Browser::Node* Flu_Tree_Browser::Node::previous_sibling()
  1343. {
  1344. if( is_root() )
  1345. return NULL;
  1346. int index;
  1347. for( index = 0; index < _parent->children(); index++ )
  1348. if( _parent->child(index) == this )
  1349. break;
  1350. // if we are the first child of our parent, then we have no previous sibling
  1351. if( index == 0 )
  1352. return NULL;
  1353. // otherwise return our previous sibling
  1354. else
  1355. return( _parent->child(index-1) );
  1356. }
  1357. int Flu_Tree_Browser::Node::index() const
  1358. {
  1359. if( is_root() )
  1360. return -1;
  1361. int index;
  1362. for( index = 0; index < _parent->children(); index++ )
  1363. if( _parent->child(index) == this )
  1364. return index;
  1365. return -1;
  1366. }
  1367. Flu_Tree_Browser::Node* Flu_Tree_Browser::Node::next()
  1368. {
  1369. // take care of the root node as a special case
  1370. if( is_root() )
  1371. {
  1372. if( children() )
  1373. return child(0);
  1374. else
  1375. return NULL;
  1376. }
  1377. // if we are a branch, then the next node is our first child, unless we don't have any children
  1378. if( is_branch() && _children.size() )
  1379. return _children.child(0);
  1380. else
  1381. {
  1382. // otherwise, the next node is our next sibling. if there is no next sibling (because we
  1383. // are the last child of our parent), then the next node is the next sibling of our parent (and so on...)
  1384. Node *p = parent(), *n = next_sibling();
  1385. while( p )
  1386. {
  1387. if( n )
  1388. return n;
  1389. else
  1390. {
  1391. n = p->next_sibling();
  1392. p = p->parent();
  1393. }
  1394. }
  1395. return NULL;
  1396. }
  1397. }
  1398. Flu_Tree_Browser::Node* Flu_Tree_Browser::Node::next_branch()
  1399. {
  1400. Node *n = next();
  1401. while( n )
  1402. {
  1403. if( n->is_branch() )
  1404. return n;
  1405. else
  1406. n = n->next();
  1407. }
  1408. return NULL;
  1409. }
  1410. Flu_Tree_Browser::Node* Flu_Tree_Browser::Node::next_leaf()
  1411. {
  1412. Node *n = next();
  1413. while( n )
  1414. {
  1415. if( n->is_leaf() )
  1416. return n;
  1417. else
  1418. n = n->next();
  1419. }
  1420. return NULL;
  1421. }
  1422. Flu_Tree_Browser::Node* Flu_Tree_Browser::Node::previous()
  1423. {
  1424. // take care of the root node as a special case
  1425. if( is_root() )
  1426. return NULL;
  1427. // the previous node is either our parent's
  1428. // previous sibling (if that sibling exists and is a leaf or a branch with no children),
  1429. // or the last child of our parent's previous sibling (if that sibling exists and is
  1430. // a branch with children). if there is no previous sibling, then the previous node
  1431. // is our parent
  1432. Node *n = previous_sibling();
  1433. if( !n )
  1434. return _parent;
  1435. else
  1436. {
  1437. if( n->is_leaf() ) // is leaf, so that is the previous node
  1438. return n;
  1439. else if( n->children() ) // is branch with some children, so previous node is last child
  1440. return( n->last() );
  1441. else // is branch with no children, so that is the previous node
  1442. return n;
  1443. }
  1444. }
  1445. Flu_Tree_Browser::Node* Flu_Tree_Browser::Node::previous_branch()
  1446. {
  1447. Node *n = previous();
  1448. while( n )
  1449. {
  1450. if( n->is_branch() )
  1451. return n;
  1452. else
  1453. n = n->previous();
  1454. }
  1455. return NULL;
  1456. }
  1457. Flu_Tree_Browser::Node* Flu_Tree_Browser::Node::previous_leaf()
  1458. {
  1459. Node *n = previous();
  1460. while( n )
  1461. {
  1462. if( n->is_leaf() )
  1463. return n;
  1464. else
  1465. n = n->previous();
  1466. }
  1467. return NULL;
  1468. }
  1469. void Flu_Tree_Browser::Node::determineVisibility( bool parentVisible )
  1470. {
  1471. if( _widget )
  1472. {
  1473. if( parentVisible )
  1474. _widget->w->show();
  1475. else
  1476. _widget->w->hide();
  1477. }
  1478. for( int i = 0; i < _children.size(); i++ )
  1479. _children.child(i)->determineVisibility( parentVisible && open() );
  1480. }
  1481. Flu_Tree_Browser::Node* Flu_Tree_Browser::Node::child( int i ) const
  1482. {
  1483. if( i < 0 || i >= _children.size() )
  1484. return 0;
  1485. else
  1486. return _children.child(i);
  1487. }
  1488. void Flu_Tree_Browser::Node::clear()
  1489. {
  1490. tree->clearing = true;
  1491. widget(NULL);
  1492. for( int i = 0; i < _children.size(); i++ )
  1493. {
  1494. //if( tree->rdata.cbNode == _children.child(i) )
  1495. //tree->rdata.cbNode = NULL;
  1496. delete _children.child(i);
  1497. }
  1498. _children.clear();
  1499. if( _group )
  1500. {
  1501. if( _group->parent() )
  1502. _group->parent()->remove( *_group );
  1503. while( _group->children() )
  1504. _group->remove( *_group->child(0) );
  1505. delete _group;
  1506. _group = NULL;
  1507. }
  1508. tree->clearing = false;
  1509. }
  1510. void Flu_Tree_Browser::Node::print( int spaces )
  1511. {
  1512. for( int s = 0; s < spaces; s++ )
  1513. printf( " " );
  1514. if( is_leaf() )
  1515. printf( " %s\n", text.c_str() );
  1516. else
  1517. printf( "[%s]\n", text.c_str() );
  1518. for( int i = 0; i < _children.size(); i++ )
  1519. _children.child(i)->print( spaces+2 );
  1520. }
  1521. void Flu_Tree_Browser::draw()
  1522. {
  1523. if( rdata.forceResize )
  1524. {
  1525. resize( x(), y(), w(), h() );
  1526. rdata.forceResize = false;
  1527. }
  1528. // draw the background color
  1529. //fl_draw_box( _box->box(), _box->x(), _box->y(), _box->w(), _box->h(), _box->color() );
  1530. fl_draw_box( box(), x(), y(), w(), h(), color() );
  1531. int dx = Fl::box_dx(box()), dy = Fl::box_dy(box()),
  1532. dw = Fl::box_dw(box()), dh = Fl::box_dh(box());
  1533. // set up the recursive data structure
  1534. rdata.x = x()+dx;
  1535. rdata.y = y()+dy;
  1536. // account for the positions of the scrollbars
  1537. if( scrollH->visible() )
  1538. rdata.x -= scrollH->value();
  1539. if( scrollV->visible() )
  1540. rdata.y -= scrollV->value();
  1541. rdata.last = true;
  1542. rdata.bgColor = _box->color();
  1543. rdata.shadedIndex = 0;
  1544. // pick the connector line and selection colors depending on the active state
  1545. if( active() )
  1546. {
  1547. rdata.lineColor = rdata.defLineColor;
  1548. rdata.selectionColor = rdata.defSelectionColor;
  1549. }
  1550. else
  1551. {
  1552. rdata.lineColor = fl_inactive( rdata.defLineColor );
  1553. rdata.selectionColor = fl_inactive( rdata.defSelectionColor );
  1554. }
  1555. // draw the tree
  1556. fl_push_clip( x()+dx, y()+dy, w()-dw, h()-dh );
  1557. root.recurse( rdata, Node::DRAW );
  1558. // if dragging to move, draw a bar showing where the dragged node will be inserted
  1559. #ifdef USE_FLU_DND
  1560. if( dnd_is_dragging() && rdata.isMoveValid && rdata.dragging )
  1561. {
  1562. bool drawLine = false;
  1563. if( dnd_event_is_text() )
  1564. drawLine = true;
  1565. else if( dnd_is_data_type( "Flu_Tree_Browser" ) )
  1566. {
  1567. if( !rdata.moveOnlySameGroup || ( rdata.grabbed->parent() == rdata.dragNode->parent() ) )
  1568. drawLine = true;
  1569. }
  1570. else if( dnd_is_data_type( "DND_Object" ) )
  1571. drawLine = true;
  1572. if( drawLine && rdata.dragWhere != MOVE_INSIDE )
  1573. {
  1574. fl_color( FL_RED );
  1575. fl_line_style( FL_SOLID, 2 );
  1576. fl_line( _box->x(), rdata.dragPos, _box->x()+_box->w(), rdata.dragPos );
  1577. fl_line_style( 0 );
  1578. }
  1579. }
  1580. #endif
  1581. fl_pop_clip();
  1582. // draw the kids
  1583. draw_child( *scrollBox );
  1584. draw_child( *scrollH );
  1585. draw_child( *scrollV );
  1586. // draw the box last so it's on top
  1587. //fl_draw_box( _box->box(), _box->x(), _box->y(), _box->w(), _box->h(), _box->color() );
  1588. }
  1589. inline void draw_T( int x, int y, int w, int h )
  1590. {
  1591. int w2 = w >> 1;
  1592. int h2 = h >> 1;
  1593. fl_line( x+w2, y, x+w2, y+h );
  1594. fl_line( x+w2, y+h2, x+w, y+h2 );
  1595. }
  1596. inline void draw_L( int x, int y, int w, int h )
  1597. {
  1598. int w2 = w >> 1;
  1599. int h2 = h >> 1;
  1600. fl_line( x+w2, y, x+w2, y+h2 );
  1601. fl_line( x+w2, y+h2, x+w, y+h2 );
  1602. }
  1603. inline void draw_Lflip( int x, int y, int w, int h )
  1604. {
  1605. int w2 = w >> 1;
  1606. int h2 = h >> 1;
  1607. fl_line( x+w2, y+h, x+w2, y+h2 );
  1608. fl_line( x+w2, y+h2, x, y+h2 );
  1609. }
  1610. inline void draw_Lflop( int x, int y, int w, int h )
  1611. {
  1612. int w2 = w >> 1;
  1613. int h2 = h >> 1;
  1614. fl_line( x+w2, y+h, x+w2, y+h2 );
  1615. fl_line( x+w2, y+h2, x+w, y+h2 );
  1616. }
  1617. inline void draw_Ldash( int x, int y, int w, int h )
  1618. {
  1619. w = w >> 1;
  1620. h = h >> 1;
  1621. fl_line( x, y+h, x+w, y+h );
  1622. }
  1623. inline void draw_vert_dash( int x, int y, int w, int h )
  1624. {
  1625. w = w >> 1;
  1626. fl_line( x+w, y+(h>>1), x+w, y+h );
  1627. }
  1628. inline void draw_Rdash( int x, int y, int w, int h )
  1629. {
  1630. h = h >> 1;
  1631. fl_line( x+w, y+h, x+(w>>1), y+h );
  1632. }
  1633. void Flu_Tree_Browser::Node::draw( RData &rdata, bool measure )
  1634. {
  1635. int which = open(); // i.e. which icon: open or closed?
  1636. bool skipCollapser = is_root() && rdata.showRoot && ( CHECK(ALWAYS_OPEN) || rdata.allBranchesAlwaysOpen );
  1637. int halfHGap = rdata.hGap >> 1, halfVGap = rdata.vGap >> 1;
  1638. bool doDraw = !measure;
  1639. int X = rdata.x;
  1640. int Y = rdata.y;
  1641. Fl_Color bgColor = rdata.shadedColors[rdata.shadedIndex], tColor = textColor, hilightColor = rdata.selectionColor;
  1642. // pick the text color depending on the active state
  1643. if( !rdata.tree->active() || !CHECK(ACTIVE))
  1644. tColor = fl_inactive( tColor );
  1645. if( doDraw )
  1646. {
  1647. // draw the background for the entry using the entry background color
  1648. fl_draw_box( FL_FLAT_BOX, rdata.browserX, Y, rdata.browserW, currentH, bgColor );
  1649. // if dragging to the inside of a branch, hilight that branch
  1650. #ifdef USE_FLU_DND
  1651. if( tree->dnd_is_dragging() && rdata.isMoveValid && rdata.dragWhere == MOVE_INSIDE && rdata.dragNode == this )
  1652. {
  1653. bgColor = FL_RED;
  1654. tColor = fl_contrast( tColor, bgColor );
  1655. hilightColor = rdata.bgColor;
  1656. fl_draw_box( FL_FLAT_BOX, rdata.browserX, Y, rdata.browserW, currentH, bgColor );
  1657. }
  1658. // if selected, draw a filled selection box and reverse the normal draw colors
  1659. else if( CHECK(SELECTED) )
  1660. #else
  1661. if( CHECK(SELECTED) )
  1662. #endif
  1663. {
  1664. bgColor = rdata.selectionColor;
  1665. tColor = fl_contrast( tColor, bgColor );
  1666. hilightColor = rdata.bgColor;
  1667. fl_draw_box( FL_FLAT_BOX, rdata.browserX, Y, rdata.browserW, currentH, bgColor );
  1668. }
  1669. fl_color( rdata.lineColor );
  1670. fl_line_style( rdata.lineStyle, rdata.lineWidth );
  1671. }
  1672. if( is_leaf() ) // draw leaves one way...
  1673. {
  1674. // draw the connectors
  1675. if( doDraw && rdata.showConnectors && rdata.showBranches )
  1676. {
  1677. if( parent()->is_root() && !rdata.showRoot && rdata.first )
  1678. {
  1679. if( rdata.last )
  1680. draw_Rdash( X-halfHGap, Y-halfVGap, rdata.branchIconW+rdata.hGap, currentH+rdata.vGap );
  1681. else
  1682. draw_Lflop( X-halfHGap, Y-halfVGap, rdata.branchIconW+rdata.hGap, currentH+rdata.vGap );
  1683. }
  1684. else if( rdata.last )
  1685. draw_L( X-halfHGap, Y-halfVGap, rdata.branchIconW+rdata.hGap, currentH+rdata.vGap );
  1686. else
  1687. draw_T( X-halfHGap, Y-halfVGap, rdata.branchIconW+rdata.hGap, currentH+rdata.vGap );
  1688. }
  1689. // account for leaf icon spacing
  1690. if( rdata.showBranches )
  1691. {
  1692. if( lIcon )
  1693. X += rdata.collapseIcons[which]->w() + rdata.hGap;
  1694. else
  1695. X += rdata.collapseIcons[which]->w() + rdata.wGap;
  1696. }
  1697. else
  1698. X += rdata.wGap;
  1699. // draw some more connectors
  1700. if( doDraw && rdata.showConnectors && lIcon && rdata.showBranches )
  1701. draw_Ldash( X-halfHGap, Y-halfVGap, lIcon->w()+rdata.hGap, currentH+rdata.vGap );
  1702. // draw the leaf icon
  1703. if( lIcon && !CHECK(ICON_AT_END) )
  1704. {
  1705. if( doDraw )
  1706. lIcon->draw( X, Y+(currentH>>1)-(lIcon->h()>>1) );
  1707. X += lIcon->w() + rdata.wGap;
  1708. }
  1709. }
  1710. else // ...and branches another
  1711. {
  1712. // force the root to the left if it has no visible children
  1713. if( is_root() && !CHECK(SOME_VISIBLE_CHILDREN) )
  1714. {
  1715. skipCollapser = true;
  1716. which = 0;
  1717. }
  1718. if( !CHECK(SOME_VISIBLE_CHILDREN) && !rdata.showLeaves )
  1719. which = 0;
  1720. // draw the connectors
  1721. if( doDraw && !skipCollapser && rdata.showConnectors && rdata.showBranches )
  1722. {
  1723. if( is_root() )
  1724. {
  1725. if( CHECK(SOME_VISIBLE_CHILDREN) )
  1726. draw_Rdash( X-halfHGap, Y-halfVGap, rdata.collapseIcons[which]->w()+4+rdata.hGap, currentH+rdata.vGap );
  1727. }
  1728. else if( parent()->is_root() && !rdata.showRoot && rdata.first )
  1729. {
  1730. if( rdata.last )
  1731. draw_Rdash( X-halfHGap, Y-halfVGap, rdata.branchIconW+rdata.hGap, currentH+rdata.vGap );
  1732. else
  1733. draw_Lflop( X-halfHGap, Y-halfVGap, rdata.branchIconW+rdata.hGap, currentH+rdata.vGap );
  1734. }
  1735. else if( rdata.last )
  1736. draw_L( X-halfHGap, Y-halfVGap, rdata.branchIconW+rdata.hGap, currentH+rdata.vGap );
  1737. else
  1738. draw_T( X-halfHGap, Y-halfVGap, rdata.branchIconW+rdata.hGap, currentH+rdata.vGap );
  1739. }
  1740. // draw the collapsed icons
  1741. if( doDraw && !skipCollapser && !CHECK(ALWAYS_OPEN) && !rdata.allBranchesAlwaysOpen )
  1742. {
  1743. if( CHECK(SOME_VISIBLE_CHILDREN) || rdata.showLeaves )
  1744. {
  1745. if( !rdata.openWOChildren && !CHECK(SOME_VISIBLE_CHILDREN) )
  1746. which = 0;
  1747. if( rdata.openWOChildren || CHECK(SOME_VISIBLE_CHILDREN) )
  1748. {
  1749. if( _parent==0 )
  1750. cIcon[which]->draw( X, Y+(currentH>>1)-(cIcon[which]->h()>>1) );
  1751. else
  1752. cIcon[which]->draw( X+(rdata.branchIconW>>1)-(cIcon[which]->w()>>1), Y+(currentH>>1)-(cIcon[which]->h()>>1) );
  1753. }
  1754. }
  1755. }
  1756. if( !skipCollapser )
  1757. {
  1758. X += cIcon[which]->w();
  1759. if( bIcon[which] )
  1760. X += rdata.hGap;
  1761. else
  1762. X += rdata.wGap;
  1763. }
  1764. // draw some more connectors
  1765. if( doDraw && rdata.showConnectors && rdata.showBranches )
  1766. {
  1767. int hGap = rdata.hGap;
  1768. if( bIcon[which] )
  1769. hGap += bIcon[which]->w();
  1770. if( skipCollapser && CHECK(SOME_VISIBLE_CHILDREN) )
  1771. draw_vert_dash( X-halfHGap, Y-halfVGap, hGap, currentH+rdata.vGap );
  1772. else if( !which || !CHECK(SOME_VISIBLE_CHILDREN) )
  1773. draw_Ldash( X-halfHGap, Y-halfVGap, hGap, currentH+rdata.vGap );
  1774. else
  1775. draw_Lflip( X-halfHGap, Y-halfVGap, hGap, currentH+rdata.vGap );
  1776. }
  1777. // draw the branch icon
  1778. if( bIcon[which] )
  1779. {
  1780. if( doDraw )
  1781. bIcon[which]->draw( X, Y+(currentH>>1)-(bIcon[which]->h()>>1) );
  1782. X += bIcon[which]->w() + rdata.wGap;
  1783. }
  1784. else
  1785. X += rdata.wGap;
  1786. }
  1787. if( doDraw )
  1788. fl_line_style( 0 );
  1789. // draw the entry
  1790. if( CHECK(SHOW_LABEL) && !CHECK(SWAP_LABEL_AND_WIDGET) )
  1791. {
  1792. if( doDraw )
  1793. {
  1794. fl_draw_box( FL_FLAT_BOX, X, Y+(currentH>>1)-(textH>>1), textW, textH, bgColor );
  1795. fl_color( tColor );
  1796. fl_font( textFont, textSize );
  1797. fl_draw( text.c_str(), X, Y+(currentH>>1)-(textH>>1), textW, textH, FL_ALIGN_LEFT );
  1798. }
  1799. X += textW;
  1800. }
  1801. if( _widget )
  1802. {
  1803. int widgetW = _widget->w->w();
  1804. int widgetH = _widget->w->h();
  1805. if( doDraw )
  1806. {
  1807. if( CHECK(AUTO_COLOR) )
  1808. _widget->w->color( bgColor );
  1809. if( CHECK(AUTO_LABEL_COLOR) )
  1810. _widget->w->labelcolor( tColor );
  1811. if( CHECK(AUTO_LABEL) )
  1812. _widget->w->label( text.c_str() );
  1813. _widget->w->redraw();
  1814. _widget->w->position( X, Y+(currentH>>1)-(widgetH>>1) );
  1815. if( CHECK(EXPAND_TO_WIDTH) )
  1816. _widget->w->size( MAX( _widget->defaultW, rdata.browserW - (X-rdata.browserX) ), _widget->w->h() );
  1817. _widget->w->draw();
  1818. }
  1819. if( CHECK(EXPAND_TO_WIDTH) )
  1820. {
  1821. if( _widget->w->w() == _widget->defaultW )
  1822. X += _widget->defaultW;
  1823. }
  1824. else
  1825. X += widgetW;
  1826. }
  1827. if( CHECK(SHOW_LABEL) && CHECK(SWAP_LABEL_AND_WIDGET) )
  1828. {
  1829. if( doDraw )
  1830. {
  1831. fl_draw_box( FL_FLAT_BOX, X, Y+(currentH>>1)-(textH>>1), textW, textH, bgColor );
  1832. fl_color( tColor );
  1833. fl_font( textFont, textSize );
  1834. fl_draw( text.c_str(), X, Y+(currentH>>1)-(textH>>1), textW, textH, FL_ALIGN_LEFT );
  1835. }
  1836. X += textW;
  1837. }
  1838. // draw the leaf icon to the right of the label and widget
  1839. if( is_leaf() && lIcon && CHECK(ICON_AT_END) )
  1840. {
  1841. if( doDraw )
  1842. lIcon->draw( X, Y+(currentH>>1)-(lIcon->h()>>1) );
  1843. X += lIcon->w() + rdata.wGap;
  1844. }
  1845. // if hilighted, draw a box outlining the entry
  1846. if( Fl::focus() == tree && rdata.hilighted == this && doDraw )
  1847. {
  1848. fl_color( hilightColor );
  1849. //fl_line_style( FL_DOT, 1 );
  1850. fl_line_style( FL_SOLID, 3 );
  1851. fl_rect( rdata.browserX, Y, rdata.browserW, currentH, hilightColor );
  1852. fl_line_style( 0 );
  1853. }
  1854. rdata.totalW = MAX( rdata.totalW, X );
  1855. }
  1856. void Flu_Tree_Browser::Node::select( bool b )
  1857. {
  1858. if( (CHECK(SELECTED)==b) && (tree->when() != FL_WHEN_NOT_CHANGED) )
  1859. return;
  1860. SET(SELECTED,b);
  1861. tree->set_changed();
  1862. tree->redraw();
  1863. if( tree->when() == FL_WHEN_RELEASE )
  1864. return;
  1865. if( b )
  1866. do_callback( FLU_SELECTED );
  1867. else
  1868. do_callback( FLU_UNSELECTED );
  1869. }
  1870. void Flu_Tree_Browser::timerScrollCB()
  1871. {
  1872. bool doRedraw = false;
  1873. float val = scrollV->value() + autoScrollY;
  1874. if( val < 0.0f )
  1875. val = 0.0f;
  1876. if( val > scrollV->maximum() )
  1877. val = scrollV->maximum();
  1878. doRedraw |= ( val != scrollV->value() );
  1879. ((Fl_Valuator*)scrollV)->value( val );
  1880. val = scrollH->value() + autoScrollX;
  1881. if( val < 0.0f )
  1882. val = 0.0f;
  1883. if( val > scrollH->maximum() )
  1884. val = scrollH->maximum();
  1885. doRedraw |= ( val != scrollH->value() );
  1886. ((Fl_Valuator*)scrollH)->value( val );
  1887. Fl::repeat_timeout( 0.02, _timerScrollCB, this );
  1888. scrolledTimerOn = true;
  1889. if( doRedraw )
  1890. redraw();
  1891. }
  1892. void Flu_Tree_Browser::timerRedrawCB()
  1893. {
  1894. if( rdata.animating )
  1895. Fl::repeat_timeout( 1.0f/rdata.fps, _timerRedrawCB, this );
  1896. redraw();
  1897. }
  1898. void Flu_Tree_Browser::Node::open( bool b )
  1899. {
  1900. if( is_leaf() )
  1901. return;
  1902. if( CHECK(ALWAYS_OPEN) || tree->rdata.allBranchesAlwaysOpen )
  1903. return;
  1904. if( (open() == b) && (tree->when() != FL_WHEN_NOT_CHANGED) )
  1905. return;
  1906. tree->rdata.justOpenedClosed = true;
  1907. SET(COLLAPSED,!b);
  1908. if( tree->rdata.animate && _children.size() )
  1909. {
  1910. // if we aren't yet animating a node, animate it!
  1911. if( !tree->rdata.animating && !tree->rdata.animatedNode )
  1912. {
  1913. // if we don't know how high all the children are, find out
  1914. // (this only happens once per node, the first time it is opened)
  1915. if( totalChildH == 0 )
  1916. {
  1917. RData r = tree->rdata;
  1918. r.x = r.y = r.totalW = 0;
  1919. recurse( r, Node::MEASURE_THIS_OPEN );
  1920. }
  1921. // set the initial offset based on whether the branch is open or closed
  1922. tree->rdata.animationOffset = b ? -totalChildH : -1;
  1923. // the delta is how much to change the offset each frame
  1924. tree->rdata.animationDelta = totalChildH / ( tree->rdata.collapseTime * tree->rdata.fps );
  1925. tree->rdata.animationDelta = b ? tree->rdata.animationDelta : -tree->rdata.animationDelta;
  1926. tree->rdata.animating = true;
  1927. tree->rdata.animatedNode = this;
  1928. Fl::add_timeout( 1.0f/tree->rdata.fps, _timerRedrawCB, tree );
  1929. }
  1930. // otherwise reverse the direction of the animation, only if we are animating this node
  1931. else if( tree->rdata.animating && tree->rdata.animatedNode==this )
  1932. {
  1933. if( b ^ (tree->rdata.animationDelta>0) )
  1934. tree->rdata.animationDelta = -tree->rdata.animationDelta;
  1935. }
  1936. }
  1937. if( open() && (_parent != 0) ) // root node doesn't count as a single open branch
  1938. {
  1939. if( ( tree->rdata.lastOpenBranch != this ) && tree->rdata.singleBranchOpen )
  1940. tree->rdata.lastOpenBranch->close();
  1941. tree->rdata.lastOpenBranch = this;
  1942. }
  1943. tree->rdata.forceResize = true;
  1944. tree->rdata.visibilityChanged = true;
  1945. if( b )
  1946. do_callback( FLU_OPENED );
  1947. else
  1948. do_callback( FLU_CLOSED );
  1949. }
  1950. void Flu_Tree_Browser::Node::active( bool b )
  1951. {
  1952. if( CHECK(ACTIVE) == b && tree->when() != FL_WHEN_NOT_CHANGED )
  1953. return;
  1954. SET( ACTIVE, b );
  1955. if( _widget )
  1956. {
  1957. if( b )
  1958. _widget->w->activate();
  1959. else
  1960. _widget->w->deactivate();
  1961. }
  1962. if( !CHECK(ACTIVE) )
  1963. {
  1964. if( tree->rdata.hilighted == this )
  1965. tree->set_hilighted( NULL );
  1966. select( false );
  1967. open( false );
  1968. }
  1969. }
  1970. void Flu_Tree_Browser::Node::unselect_all( Node* except )
  1971. {
  1972. if( this != except )
  1973. select( false );
  1974. for( int i = 0; i < _children.size(); i++ )
  1975. _children.child(i)->unselect_all( except );
  1976. }
  1977. void Flu_Tree_Browser::Node::select_all()
  1978. {
  1979. select( true );
  1980. for( int i = 0; i < _children.size(); i++ )
  1981. _children.child(i)->select_all();
  1982. }
  1983. bool Flu_Tree_Browser::Node::isMoveValid( Node* &n1, int &where, Node* &n2 )
  1984. {
  1985. // if n1 is NULL, then check it as if it were a node being moved from another tree
  1986. if( n2 == NULL )
  1987. return false;
  1988. // check the validity of the move:
  1989. // 1) the source and destination nodes can't be the same
  1990. // 2) you can't move before the root node
  1991. // 3) you can't move an unmovable node or move a branch node such that it would become a descendent of itself
  1992. // 4) if moving only within the same group, check that the parents are the same
  1993. // 5) if moving into a sorted tree, the destination node MUST be a branch
  1994. // 6) a move AFTER an OPEN branch is a move BEFORE its first child
  1995. // 7) you can't move a node into a non-droppable branch node
  1996. if( n1 == n2 )
  1997. return false;
  1998. if( where==MOVE_BEFORE && n2->is_root() )
  1999. return false;
  2000. if( n1 )
  2001. {
  2002. if( !n1->movable() )
  2003. return false;
  2004. if( n1->is_branch() )
  2005. if( n1->is_descendent( n2 ) )
  2006. return false;
  2007. }
  2008. bool sameGroup = n2->tree->move_only_same_group();
  2009. if( sameGroup && n1 )
  2010. {
  2011. if( n1->parent() != n2->parent() || where==MOVE_INSIDE )
  2012. return false;
  2013. }
  2014. int iMode = n2->tree->insertion_mode();
  2015. if( iMode == FLU_INSERT_SORTED || iMode == FLU_INSERT_SORTED_REVERSE )
  2016. {
  2017. if( n2->is_branch() )
  2018. {
  2019. where = MOVE_INSIDE;
  2020. return true;
  2021. }
  2022. else
  2023. return false;
  2024. }
  2025. if( where==MOVE_AFTER && n2->is_branch() && n2->open() )
  2026. {
  2027. // can't move inside a branch if within the same group, unless the first node is dragged
  2028. // from outside the tree (in which case n1 is NULL)
  2029. if( sameGroup && n1 )
  2030. {
  2031. if( n2->_children.size() > 0 )
  2032. return false;
  2033. }
  2034. else if( n2->_children.size() > 0 )
  2035. {
  2036. where = MOVE_BEFORE;
  2037. n2 = n2->_children.child(0);
  2038. }
  2039. else
  2040. where = MOVE_INSIDE;
  2041. }
  2042. if( where==MOVE_INSIDE )
  2043. {
  2044. if( !n2->droppable() )
  2045. return false;
  2046. }
  2047. else if( n2->parent() )
  2048. if( !n2->parent()->droppable() )
  2049. return false;
  2050. return true;
  2051. }
  2052. int Flu_Tree_Browser::Node::recurse( RData &rdata, int type, int event )
  2053. {
  2054. int i;
  2055. if( tree->clearing ) return 0;
  2056. if( is_root() )
  2057. rdata.first = true;
  2058. if( type == COUNT_SELECTED )
  2059. {
  2060. if( is_leaf() )
  2061. return (int)CHECK(SELECTED);
  2062. else
  2063. {
  2064. int total = (int)CHECK(SELECTED);
  2065. for( i = 0; i < _children.size(); i++ )
  2066. total += _children.child(i)->recurse( rdata, type, event );
  2067. return total;
  2068. }
  2069. }
  2070. // see if this entry is even visible
  2071. if( rdata.y > rdata.browserY+rdata.browserH )
  2072. {
  2073. if( type == DRAW )
  2074. return 1;
  2075. else if( type == HANDLE ){
  2076. if( !((event == FL_KEYDOWN) && Fl::event_key(FL_Down)) ) //to allow cursor down work
  2077. return 0;
  2078. }
  2079. }
  2080. int which = open();
  2081. bool skipEntry = ( is_root() && !rdata.showRoot ) || ( is_leaf() && !rdata.showLeaves ) || ( is_branch() && !rdata.showBranches );
  2082. bool skipCollapser = is_root() && rdata.showRoot && ( CHECK(ALWAYS_OPEN) || rdata.allBranchesAlwaysOpen );
  2083. // find the size of the entry label
  2084. if( (type == MEASURE) || (type == MEASURE_THIS_OPEN) )
  2085. {
  2086. if( CHECK(SHOW_LABEL) )
  2087. {
  2088. int W = 0, H;
  2089. fl_font( textFont, textSize );
  2090. fl_measure( text.c_str(), W, H );
  2091. W += 4;
  2092. H += 4; // hack - it looks better
  2093. textW = W;
  2094. textH = H;
  2095. }
  2096. else
  2097. {
  2098. textW = textH = 0;
  2099. }
  2100. // remember vertically where this node is w.r.t the browser
  2101. currentY = rdata.y;
  2102. currentH = textH;
  2103. // find the total size of the entry, depending on if there's a widget
  2104. if( _widget )
  2105. currentH = MAX( _widget->w->h(), currentH );
  2106. // find the total height of this entry by taking the max height of the entry and icons
  2107. if( is_leaf() )
  2108. {
  2109. if( lIcon )
  2110. currentH = MAX( currentH, lIcon->h() );
  2111. }
  2112. else
  2113. {
  2114. currentH = MAX( currentH, cIcon[which]->h() );
  2115. if( bIcon[which] )
  2116. currentH = MAX( currentH, bIcon[which]->h() );
  2117. }
  2118. }
  2119. bool skipAhead = ((rdata.y + currentH) < rdata.browserY) &&
  2120. !((event == FL_KEYDOWN) && Fl::event_key(FL_Up)); //to allow cursor up work
  2121. // process the entry
  2122. switch( type )
  2123. {
  2124. case DRAW:
  2125. {
  2126. if( skipEntry || skipAhead ) break;
  2127. draw( rdata, false );
  2128. // draw any vertical connectors connecting our parents, grandparents, etc.,
  2129. if( rdata.showBranches )
  2130. {
  2131. int d = depth()-1;
  2132. for( i = 0; i < rdata.branchConnectors.size(); i++ )
  2133. {
  2134. if( i != d )
  2135. {
  2136. fl_color( rdata.lineColor );
  2137. fl_line_style( rdata.lineStyle, rdata.lineWidth );
  2138. fl_line( rdata.branchConnectors[i], rdata.y, rdata.branchConnectors[i], rdata.y+currentH );
  2139. fl_line_style( 0 );
  2140. }
  2141. }
  2142. }
  2143. rdata.shadedIndex = 1 - rdata.shadedIndex; // toggle the even/odd entry for shading
  2144. }
  2145. break;
  2146. case MEASURE:
  2147. if( is_leaf() )
  2148. CLEAR( SOME_VISIBLE_CHILDREN );
  2149. else
  2150. {
  2151. // find out whether the branch has any children that could be visible
  2152. bool someVisibleChildren = rdata.showLeaves && ( _children.size() > 0 );
  2153. for( i = 0; i < _children.size(); i++ )
  2154. {
  2155. if( _children.child(i)->is_branch() )
  2156. {
  2157. someVisibleChildren = true;
  2158. break;
  2159. }
  2160. }
  2161. SET( SOME_VISIBLE_CHILDREN, someVisibleChildren );
  2162. }
  2163. case MEASURE_THIS_OPEN:
  2164. if( skipEntry ) break;
  2165. draw( rdata, true );
  2166. break;
  2167. case HANDLE:
  2168. {
  2169. if( skipEntry || skipAhead || !CHECK(ACTIVE) ) break;
  2170. if( event != FL_DRAG && event != FL_NO_EVENT )
  2171. rdata.justOpenedClosed = false;
  2172. // if we are trying to select all entries between 2 widgets due to a shift-select...
  2173. if( rdata.shiftSelect )
  2174. {
  2175. if( (rdata.hilighted == this) || (rdata.grabbed == this) )
  2176. {
  2177. if( !rdata.shiftSelectAll )
  2178. {
  2179. rdata.shiftSelectAll = true;
  2180. select( true );
  2181. if( is_branch() && rdata.openOnSelect )
  2182. {
  2183. open( true );
  2184. }
  2185. }
  2186. else
  2187. {
  2188. rdata.shiftSelect = false;
  2189. rdata.shiftSelectAll = false;
  2190. rdata.grabbed = 0;
  2191. select( true );
  2192. if( is_branch() && rdata.openOnSelect )
  2193. {
  2194. open( true );
  2195. }
  2196. }
  2197. }
  2198. else if( rdata.shiftSelectAll )
  2199. {
  2200. select( true );
  2201. if( is_branch() && rdata.openOnSelect )
  2202. {
  2203. open( true );
  2204. }
  2205. }
  2206. break;
  2207. }
  2208. // check for the keyboard event
  2209. if( event == FL_KEYDOWN )
  2210. {
  2211. int key = Fl::event_key();
  2212. // check for the spacebar selecting this entry
  2213. if( ((key == ' ') || (key == FL_Enter) || (key == FL_KP_Enter))
  2214. && rdata.hilighted == this )
  2215. {
  2216. if( Fl::event_state(FL_CTRL) )
  2217. select( !CHECK(SELECTED) );
  2218. else
  2219. {
  2220. rdata.root->unselect_all( this );
  2221. select( true );
  2222. }
  2223. if( is_branch() && rdata.openOnSelect )
  2224. {
  2225. open( true );
  2226. }
  2227. return 1;
  2228. }
  2229. /*
  2230. // check for the enter key opening/closing this entry
  2231. else if( (key == FL_Enter) && (rdata.hilighted == this) )
  2232. {
  2233. open( !open() );
  2234. return 1;
  2235. }
  2236. */
  2237. // check for the left/right cursor keys opening/closing this entry
  2238. else if( (key == FL_Left) && (rdata.hilighted == this) )
  2239. {
  2240. open( false );
  2241. return 1;
  2242. }
  2243. else if( (key == FL_Right) && (rdata.hilighted == this) )
  2244. {
  2245. open( true );
  2246. return 1;
  2247. }
  2248. }
  2249. // check for the "up" cursor key moving the hilighted entry
  2250. if( rdata.delta == -1 && rdata.hilighted == this && rdata.previous != NULL )
  2251. {
  2252. tree->set_hilighted( rdata.previous );
  2253. rdata.delta = 0;
  2254. return 1;
  2255. }
  2256. // check for the "down" cursor key moving the hilighted entry
  2257. if( rdata.delta == 1 && rdata.hilighted == rdata.previous )
  2258. {
  2259. tree->set_hilighted( this );
  2260. rdata.delta = 0;
  2261. return 1;
  2262. }
  2263. rdata.previous = this;
  2264. // the event is not ours to use
  2265. //if( _widget && !rdata.dragging )
  2266. //if( Fl::event_inside( _widget->w ) )
  2267. // return 2;
  2268. bool inExpander = false;
  2269. if( is_branch() )
  2270. {
  2271. int which = open();
  2272. if( _parent==0 )
  2273. inExpander = Fl::event_inside( rdata.x, rdata.y+(currentH>>1)-(cIcon[which]->h()>>1),
  2274. cIcon[which]->w(), cIcon[which]->h() );
  2275. else
  2276. inExpander = Fl::event_inside( rdata.x+(rdata.branchIconW>>1)-(cIcon[which]->w()>>1),
  2277. rdata.y+(currentH>>1)-(cIcon[which]->h()>>1),
  2278. cIcon[which]->w(), cIcon[which]->h() );
  2279. }
  2280. if( event == FL_PUSH )
  2281. {
  2282. // check for expand/collapse
  2283. if( Fl::event_button() == FL_LEFT_MOUSE && inExpander )
  2284. {
  2285. if( rdata.openWOChildren || CHECK(SOME_VISIBLE_CHILDREN) )
  2286. {
  2287. open( !open() );
  2288. rdata.dragging = false;
  2289. rdata.dragNode = 0;
  2290. return 1;
  2291. }
  2292. }
  2293. }
  2294. if( event == FL_DRAG && rdata.justOpenedClosed )
  2295. return 0;
  2296. // if no selections, return
  2297. if( rdata.selectionMode == FLU_NO_SELECT )
  2298. break;
  2299. // if the event is not inside us, return
  2300. if( !Fl::event_inside( rdata.browserX, rdata.y, rdata.browserW, currentH ) )
  2301. break;
  2302. #ifdef USE_FLU_DND
  2303. // check for grabbing of a node for DND
  2304. if( event == FL_DRAG && rdata.selectionDragMode == FLU_DRAG_TO_MOVE && !is_root() && rdata.grabbed &&
  2305. //rdata.insertionMode!=FLU_INSERT_SORTED && rdata.insertionMode!=FLU_INSERT_SORTED_REVERSE &&
  2306. !tree->dnd_is_dragging() && !rdata.justOpenedClosed && CHECK(MOVABLE) )
  2307. {
  2308. tree->dnd_grab( this, "Flu_Tree_Browser" );
  2309. return 1;
  2310. }
  2311. // dragging to move a node
  2312. if( event == FL_DND_DRAG )
  2313. {
  2314. rdata.dragNode = this; // remember which node to move the grabbed node before/after
  2315. if( is_root() )
  2316. {
  2317. rdata.dragWhere = MOVE_AFTER;
  2318. rdata.dragPos = rdata.y + currentH;
  2319. }
  2320. else
  2321. {
  2322. // if this is a leaf or an open branch, then can only move before or after
  2323. // otherwise can move inside
  2324. if( is_branch() && !open() )
  2325. {
  2326. int t = MAX( currentH / 3, 1 );
  2327. if( (Fl::event_y()-rdata.y) <= t )
  2328. rdata.dragWhere = MOVE_BEFORE;
  2329. else if( (Fl::event_y()-rdata.y) <= (t<<1) )
  2330. rdata.dragWhere = MOVE_INSIDE;
  2331. else
  2332. rdata.dragWhere = MOVE_AFTER;
  2333. }
  2334. else
  2335. {
  2336. if( (Fl::event_y()-rdata.y) <= (currentH>>1) )
  2337. rdata.dragWhere = MOVE_BEFORE;
  2338. else
  2339. rdata.dragWhere = MOVE_AFTER;
  2340. }
  2341. // where to draw the insertion position?
  2342. if( rdata.dragWhere == MOVE_BEFORE || rdata.dragWhere == MOVE_INSIDE )
  2343. rdata.dragPos = rdata.y;
  2344. else
  2345. rdata.dragPos = rdata.y + currentH;
  2346. }
  2347. return 1;
  2348. }
  2349. #endif
  2350. //if( _widget && _widget->w && Fl::event_inside(_widget->w) && _widget->w->handle(event))
  2351. //return 1;
  2352. // single selection
  2353. if( rdata.selectionMode == FLU_SINGLE_SELECT )
  2354. {
  2355. if( event == FL_MOVE && rdata.selectUnderMouse )
  2356. {
  2357. //select_only();
  2358. rdata.root->unselect_all( this );
  2359. SET(SELECTED,true);
  2360. tree->redraw();
  2361. }
  2362. else if( event == FL_PUSH )
  2363. {
  2364. //rdata.dragging = true;
  2365. rdata.grabbed = this;
  2366. if( rdata.selectUnderMouse )
  2367. rdata.root->unselect_all();
  2368. else
  2369. rdata.root->unselect_all( this );
  2370. tree->set_hilighted( this );
  2371. if( Fl::event_state(FL_CTRL) )
  2372. select( !CHECK(SELECTED) );
  2373. else
  2374. select( true );
  2375. if( is_leaf() )
  2376. {
  2377. if( Fl::event_clicks() > 0 )
  2378. {
  2379. Fl::event_clicks(0);
  2380. do_callback( FLU_DOUBLE_CLICK );
  2381. }
  2382. }
  2383. else
  2384. {
  2385. if( Fl::event_clicks() > 0 )
  2386. {
  2387. Fl::event_clicks(0);
  2388. if( rdata.doubleClickToOpen )
  2389. {
  2390. if( rdata.openWOChildren || CHECK(SOME_VISIBLE_CHILDREN) )
  2391. open( !open() );
  2392. }
  2393. else
  2394. do_callback( FLU_DOUBLE_CLICK );
  2395. }
  2396. else if( rdata.openOnSelect )
  2397. {
  2398. open( true );
  2399. }
  2400. }
  2401. Fl::focus(tree);
  2402. return 1;
  2403. }
  2404. else if( event == FL_DRAG )
  2405. {
  2406. if( rdata.selectionDragMode == FLU_DRAG_IGNORE )
  2407. return 1;
  2408. rdata.dragging = true;
  2409. //if( ( rdata.selectionDragMode == FLU_DRAG_IGNORE || rdata.selectionDragMode == FLU_DRAG_TO_MOVE) && ( tree->insertion_mode() == FLU_INSERT_FRONT || tree->insertion_mode() == FLU_INSERT_BACK ) )
  2410. //return 1;
  2411. rdata.root->unselect_all( this );
  2412. tree->set_hilighted( this );
  2413. select( true );
  2414. return 1;
  2415. }
  2416. else if( event == FL_RELEASE && tree->when() == FL_WHEN_RELEASE && selected() && !inExpander )
  2417. {
  2418. do_callback( FLU_SELECTED );
  2419. return 1;
  2420. }
  2421. }
  2422. // multiple selection
  2423. else if( rdata.selectionMode == FLU_MULTI_SELECT )
  2424. {
  2425. if( event == FL_PUSH )
  2426. {
  2427. //rdata.dragging = true;
  2428. rdata.grabbed = this;
  2429. if( Fl::event_state(FL_CTRL) )
  2430. {
  2431. select( !CHECK(SELECTED) );
  2432. tree->set_hilighted( this );
  2433. }
  2434. else if( Fl::event_state(FL_SHIFT) )
  2435. {
  2436. // select everything from the last selected entry to this one
  2437. if( rdata.hilighted == this )
  2438. {
  2439. select( true );
  2440. if( is_branch() )
  2441. {
  2442. if( Fl::event_clicks() > 0 )
  2443. {
  2444. Fl::event_clicks(0);
  2445. if( rdata.doubleClickToOpen )
  2446. {
  2447. if( rdata.openWOChildren || CHECK(SOME_VISIBLE_CHILDREN) )
  2448. open( !open() );
  2449. }
  2450. else
  2451. do_callback( FLU_DOUBLE_CLICK );
  2452. }
  2453. else if( rdata.openOnSelect )
  2454. {
  2455. open( !open() );
  2456. }
  2457. }
  2458. }
  2459. else
  2460. {
  2461. rdata.shiftSelectAll = false;
  2462. rdata.shiftSelect = true;
  2463. rdata.grabbed = this;
  2464. rdata.root->recurse( rdata, HANDLE, 0 );
  2465. tree->set_hilighted( this );
  2466. }
  2467. }
  2468. else
  2469. {
  2470. rdata.root->unselect_all( this );
  2471. select( true );
  2472. if( is_leaf() )
  2473. {
  2474. if( Fl::event_clicks() > 0 )
  2475. {
  2476. Fl::event_clicks(0);
  2477. do_callback( FLU_DOUBLE_CLICK );
  2478. }
  2479. }
  2480. else
  2481. {
  2482. if( Fl::event_clicks() > 0 )
  2483. {
  2484. Fl::event_clicks(0);
  2485. if( rdata.doubleClickToOpen )
  2486. {
  2487. if( rdata.openWOChildren || CHECK(SOME_VISIBLE_CHILDREN) )
  2488. open( !open() );
  2489. }
  2490. else
  2491. do_callback( FLU_DOUBLE_CLICK );
  2492. }
  2493. else if( rdata.openOnSelect )
  2494. {
  2495. open( true );
  2496. }
  2497. }
  2498. tree->set_hilighted( this );
  2499. }
  2500. Fl::focus(tree);
  2501. return 1;
  2502. }
  2503. else if( event == FL_DRAG )
  2504. {
  2505. if( rdata.selectionDragMode == FLU_DRAG_IGNORE )
  2506. return 1;
  2507. rdata.dragging = true;
  2508. //if( ( rdata.selectionDragMode == FLU_DRAG_IGNORE || rdata.selectionDragMode == FLU_DRAG_TO_MOVE) && ( tree->insertion_mode() == FLU_INSERT_FRONT || tree->insertion_mode() == FLU_INSERT_BACK ) )
  2509. //return 1;
  2510. select( true );
  2511. tree->set_hilighted( this );
  2512. return 1;
  2513. }
  2514. else if( event == FL_RELEASE && tree->when() == FL_WHEN_RELEASE && selected() && !inExpander )
  2515. {
  2516. do_callback( FLU_SELECTED );
  2517. return 1;
  2518. }
  2519. }
  2520. }
  2521. break;
  2522. }
  2523. // advance the counters vertically to the next entry
  2524. if( !skipEntry )
  2525. rdata.y += currentH + rdata.vGap;
  2526. if( !is_root() && rdata.first && !skipEntry )
  2527. rdata.first = false;
  2528. // if we're a leaf, no need to process further
  2529. if( is_leaf() )
  2530. return 0;
  2531. // should we bail out already if we're done processing?
  2532. if( closed() && !skipEntry && !skipCollapser && tree->rdata.animatedNode!=this && ( type != MEASURE_THIS_OPEN ) )
  2533. return 0;
  2534. if( !CHECK(SOME_VISIBLE_CHILDREN) )
  2535. return 0;
  2536. // advance the counters horizontally to the next entry
  2537. if( rdata.showBranches )
  2538. {
  2539. if( !skipEntry && !skipCollapser )
  2540. rdata.x += cIcon[which]->w() + rdata.hGap;
  2541. }
  2542. rdata.totalW = MAX( rdata.totalW, rdata.x );
  2543. // the branchIconW is the width of the branch icon at this level
  2544. // it is used to center all children icons under the branch icon
  2545. int lastBranchIconW = rdata.branchIconW;
  2546. if( rdata.showBranches )
  2547. {
  2548. if( bIcon[which] )
  2549. rdata.branchIconW = bIcon[which]->w();
  2550. else
  2551. rdata.branchIconW = cIcon[which]->w();
  2552. }
  2553. else
  2554. rdata.branchIconW = 0;
  2555. // update the animation
  2556. if( tree->rdata.animatedNode==this && ( type == DRAW ) )
  2557. {
  2558. // check for termination (if opening)
  2559. if( (rdata.animationOffset+rdata.animationDelta) >= 0.0f )
  2560. {
  2561. tree->rdata.animatedNode = NULL;
  2562. rdata.animating = false;
  2563. tree->rdata.forceResize = true;
  2564. Fl::remove_timeout( _timerRedrawCB, tree );
  2565. }
  2566. else
  2567. {
  2568. // update the offset
  2569. rdata.animationOffset += rdata.animationDelta;
  2570. fl_push_clip( rdata.browserX, rdata.y, rdata.browserW, rdata.browserH );
  2571. rdata.y += (int)rdata.animationOffset;
  2572. }
  2573. }
  2574. if( ( type == MEASURE ) || ( type == MEASURE_THIS_OPEN ) )
  2575. totalChildH = rdata.y;
  2576. // process all children
  2577. int val;
  2578. int tempW = rdata.branchIconW >> 1;
  2579. for( i = 0; i < _children.size(); i++ )
  2580. {
  2581. // prepare the recursive data structure for the next level
  2582. if( i == 0 )
  2583. rdata.first = true;
  2584. rdata.last = (i == _children.size()-1 );
  2585. // if child "i" is not the last child,
  2586. // then there is a long connector that needs drawn between this node and the last child.
  2587. // push the horizontal position of the connector onto the stack
  2588. if( (type == DRAW) && rdata.showConnectors && ( i < _children.size()-1 ) )
  2589. {
  2590. rdata.branchConnectors.push( rdata.x+tempW );
  2591. val = _children.child(i)->recurse( rdata, type, event );
  2592. rdata.branchConnectors.pop();
  2593. }
  2594. else
  2595. val = _children.child(i)->recurse( rdata, type, event );
  2596. if( val )
  2597. return val;
  2598. }
  2599. // set the branch icon width back to what it was before we changed it
  2600. rdata.branchIconW = lastBranchIconW;
  2601. if( ( type == MEASURE ) || ( type == MEASURE_THIS_OPEN ) )
  2602. totalChildH = rdata.y - totalChildH;
  2603. // update the animation
  2604. if( tree->rdata.animatedNode==this && ( type == DRAW ) )
  2605. {
  2606. fl_pop_clip();
  2607. // check for termination (if closing)
  2608. if( rdata.animationOffset <= (float)(-totalChildH) )
  2609. {
  2610. tree->rdata.animatedNode = NULL;
  2611. rdata.animating = false;
  2612. tree->rdata.forceResize = true;
  2613. Fl::remove_timeout( _timerRedrawCB, tree );
  2614. }
  2615. }
  2616. // move back horizontally from the last entry
  2617. if( rdata.showBranches )
  2618. {
  2619. if( !skipEntry && !skipCollapser )
  2620. rdata.x -= cIcon[which]->w() + rdata.hGap;
  2621. }
  2622. return 0;
  2623. }
  2624. void Flu_Tree_Browser::print()
  2625. {
  2626. root.print();
  2627. }
  2628. void Flu_Tree_Browser::clear()
  2629. {
  2630. root.clear();
  2631. root.text = "";
  2632. while( _box->children() )
  2633. _box->remove( *_box->child(0) );
  2634. rdata.cbNode = NULL;
  2635. rdata.cbReason = FLU_NOTHING;
  2636. rdata.hilighted = NULL;
  2637. rdata.dragging = false;
  2638. rdata.forceResize = true;
  2639. rdata.lastOpenBranch = NULL;
  2640. rdata.shiftSelect = false;
  2641. rdata.shiftSelectAll = false;
  2642. rdata.nextId = 1;
  2643. rdata.searchIndex = 1;
  2644. }
  2645. Flu_Tree_Browser::Node* Flu_Tree_Browser::set_root( const char *label, Fl_Widget *w, bool showLabel )
  2646. {
  2647. if( label == 0 )
  2648. label = "";
  2649. root.text = label;
  2650. root.widget( w );
  2651. root.SET(Node::SHOW_LABEL,showLabel);
  2652. root.cIcon[0] = rdata.collapseIcons[0];
  2653. root.cIcon[1] = rdata.collapseIcons[1];
  2654. root.bIcon[0] = rdata.branchIcons[0];
  2655. root.bIcon[1] = rdata.branchIcons[1];
  2656. root.textColor = rdata.defBranchColor;
  2657. root.textFont = rdata.defBranchFont;
  2658. root.textSize = rdata.defBranchSize;
  2659. rdata.forceResize = true;
  2660. return &root;
  2661. }
  2662. Flu_Tree_Browser::Node* Flu_Tree_Browser::add( const char* fullpath, Fl_Widget *w, bool showLabel )
  2663. {
  2664. return( root.modify( fullpath, Node::ADD, rdata, w, showLabel ) );
  2665. }
  2666. Flu_Tree_Browser::Node* Flu_Tree_Browser::add( const char* path, const char* text, Fl_Widget *w, bool showLabel )
  2667. {
  2668. // if the path does not end in '/', add it
  2669. FluSimpleString s = path;
  2670. if( path[strlen(path)-1] != '/' )
  2671. s += "/";
  2672. s += text;
  2673. return add( s.c_str(), w, showLabel );
  2674. }
  2675. Flu_Tree_Browser::Node* Flu_Tree_Browser::add_branch( const char* fullpath, Fl_Widget *w, bool showLabel )
  2676. {
  2677. FluSimpleString p( fullpath );
  2678. if( p.size() && p[p.size()-1] != '/' && p[p.size()-1] != '\\' ) p += "/";
  2679. return add( p.c_str(), w, showLabel );
  2680. }
  2681. Flu_Tree_Browser::Node* Flu_Tree_Browser::add_branch( const char* path, const char* name, Fl_Widget *w, bool showLabel )
  2682. {
  2683. FluSimpleString p( name );
  2684. if( p.size() && p[p.size()-1] != '/' && p[p.size()-1] != '\\' ) p += "/";
  2685. return add( path, p.c_str(), w, showLabel );
  2686. }
  2687. Flu_Tree_Browser::Node* Flu_Tree_Browser::add_leaf( const char* fullpath, Fl_Widget *w, bool showLabel )
  2688. {
  2689. FluSimpleString p( fullpath );
  2690. if( p.size() && ( p[p.size()-1] == '/' || p[p.size()-1] == '\\' ) ) p[p.size()-1] = '\0';
  2691. return add( p.c_str(), w, showLabel );
  2692. }
  2693. Flu_Tree_Browser::Node* Flu_Tree_Browser::add_leaf( const char* path, const char* name, Fl_Widget *w, bool showLabel )
  2694. {
  2695. FluSimpleString p( name );
  2696. if( p.size() && ( p[p.size()-1] == '/' || p[p.size()-1] == '\\' ) ) p[p.size()-1] = '\0';
  2697. return add( path, p.c_str(), w, showLabel );
  2698. }
  2699. unsigned int Flu_Tree_Browser::remove( const char *fullpath )
  2700. {
  2701. return( (ptrdiff_t)root.modify( fullpath, Node::REMOVE, rdata ) );
  2702. }
  2703. unsigned int Flu_Tree_Browser::remove( const char *path, const char *text )
  2704. {
  2705. // if the path does not end in '/', add it
  2706. FluSimpleString s = path;
  2707. if( path[strlen(path)-1] != '/' )
  2708. s += "/";
  2709. s += text;
  2710. return remove( s.c_str() );
  2711. }
  2712. unsigned int Flu_Tree_Browser::remove( unsigned int id )
  2713. {
  2714. return root.remove( id );
  2715. }
  2716. unsigned int Flu_Tree_Browser::Node::remove( unsigned int id )
  2717. {
  2718. if( id == 0 )
  2719. return 0;
  2720. for( int i = 0; i < _children.size(); i++ )
  2721. {
  2722. Node *n = _children.child(i);
  2723. if( n->id() == id )
  2724. {
  2725. _children.erase( i );
  2726. tree->rdata.forceResize = true;
  2727. //if( tree->rdata.cbNode == n )
  2728. //tree->rdata.cbNode = NULL;
  2729. delete n;
  2730. if( tree->rdata.autoBranches )
  2731. initType();
  2732. tree->redraw();
  2733. return id;
  2734. }
  2735. else if( n->remove( id ) )
  2736. return id;
  2737. }
  2738. return 0;
  2739. }
  2740. unsigned int Flu_Tree_Browser::remove( Fl_Widget *w )
  2741. {
  2742. return root.remove( w );
  2743. }
  2744. unsigned int Flu_Tree_Browser::Node::remove( Fl_Widget *w )
  2745. {
  2746. if( !w )
  2747. return 0;
  2748. for( int i = 0; i < _children.size(); i++ )
  2749. {
  2750. Node *n = _children.child(i);
  2751. if( n->_widget )
  2752. {
  2753. if( n->_widget->w == w )
  2754. {
  2755. int id = n->id();
  2756. _children.erase( i );
  2757. tree->rdata.forceResize = true;
  2758. //if( tree->rdata.cbNode == n )
  2759. //tree->rdata.cbNode = NULL;
  2760. delete n;
  2761. if( tree->rdata.autoBranches )
  2762. initType();
  2763. tree->redraw();
  2764. return id;
  2765. }
  2766. }
  2767. int id = n->remove( w );
  2768. if( id )
  2769. return id;
  2770. }
  2771. return 0;
  2772. }
  2773. int Flu_Tree_Browser::find_number( const char *fullpath )
  2774. {
  2775. rdata.counter = 0;
  2776. root.modify( fullpath, Node::FIND_NUMBER, rdata );
  2777. return rdata.counter;
  2778. }
  2779. int Flu_Tree_Browser::find_number( const char *path, const char *text )
  2780. {
  2781. // if the path does not end in '/', add it
  2782. FluSimpleString s = path;
  2783. if( path[strlen(path)-1] != '/' )
  2784. s += "/";
  2785. s += text;
  2786. return find_number( s.c_str() );
  2787. }
  2788. Flu_Tree_Browser::Node* Flu_Tree_Browser::find_next( const char *fullpath, Node* startNode )
  2789. {
  2790. // degenerate case: root node
  2791. if( strcmp( fullpath, "/" ) == 0 )
  2792. return &root;
  2793. rdata.previous = startNode;
  2794. return( root.modify( fullpath, Node::FIND, rdata ) );
  2795. }
  2796. Flu_Tree_Browser::Node* Flu_Tree_Browser::find_next( const char *path, const char *text )
  2797. {
  2798. // if the path does not end in '/', add it
  2799. FluSimpleString s = path;
  2800. if( path[strlen(path)-1] != '/' )
  2801. s += "/";
  2802. s += text;
  2803. return find_next( s.c_str() );
  2804. }
  2805. Flu_Tree_Browser::Node* Flu_Tree_Browser::find( const char *path, const char *text )
  2806. {
  2807. // if the path does not end in '/', add it
  2808. FluSimpleString s = path;
  2809. if( path[strlen(path)-1] != '/' )
  2810. s += "/";
  2811. s += text;
  2812. return find( s.c_str() );
  2813. }
  2814. Flu_Tree_Browser::Node* Flu_Tree_Browser::find( unsigned int id )
  2815. {
  2816. return root.find( id );
  2817. }
  2818. Flu_Tree_Browser::Node* Flu_Tree_Browser::Node::find( unsigned int id )
  2819. {
  2820. if( id == 0 )
  2821. return NULL;
  2822. if( _id == id )
  2823. return this;
  2824. for( int i = 0; i < _children.size(); i++ )
  2825. {
  2826. Node *n = _children.child(i)->find( id );
  2827. if( n )
  2828. return n;
  2829. }
  2830. return NULL;
  2831. }
  2832. Flu_Tree_Browser::Node* Flu_Tree_Browser::find( Fl_Widget *w )
  2833. {
  2834. return root.find( w );
  2835. }
  2836. Flu_Tree_Browser::Node* Flu_Tree_Browser::Node::find( Fl_Widget *w )
  2837. {
  2838. if( _widget )
  2839. if( _widget->w == w )
  2840. return this;
  2841. for( int i = 0; i < _children.size(); i++ )
  2842. {
  2843. Node *n = _children.child(i)->find( w );
  2844. if( n )
  2845. return n;
  2846. }
  2847. return NULL;
  2848. }
  2849. Flu_Tree_Browser::Node* Flu_Tree_Browser::find_by_user_data( void *user_data )
  2850. {
  2851. return root.find_by_user_data( user_data );
  2852. }
  2853. Flu_Tree_Browser::Node* Flu_Tree_Browser::Node::find_by_user_data( void *user_data )
  2854. {
  2855. if( userData == user_data ) return this;
  2856. for( int i = 0; i < _children.size(); i++ )
  2857. {
  2858. Node *n = _children.child(i)->find_by_user_data( user_data );
  2859. if( n )
  2860. return n;
  2861. }
  2862. return NULL;
  2863. }
  2864. bool Flu_Tree_Browser::Node::findPath( unsigned int id, RData &rdata )
  2865. {
  2866. if( _id == id )
  2867. {
  2868. if( is_leaf() )
  2869. rdata.path += text;
  2870. else
  2871. {
  2872. rdata.path += text;
  2873. rdata.path += "/";
  2874. }
  2875. return true;
  2876. }
  2877. if( is_leaf() )
  2878. return false;
  2879. char *oldPath = strdup( rdata.path.c_str() );
  2880. if( _parent != 0 )
  2881. {
  2882. rdata.path += text;
  2883. rdata.path += "/";
  2884. }
  2885. for( int i = 0; i < _children.size(); i++ )
  2886. {
  2887. if( _children.child(i)->findPath( id, rdata ) )
  2888. {
  2889. free( oldPath );
  2890. return true;
  2891. }
  2892. }
  2893. rdata.path = oldPath;
  2894. free( oldPath );
  2895. return false;
  2896. }
  2897. bool Flu_Tree_Browser::Node::findPath( Fl_Widget *w, RData &rdata )
  2898. {
  2899. if( _widget )
  2900. if( _widget->w == w )
  2901. {
  2902. if( is_leaf() )
  2903. rdata.path += text;
  2904. else
  2905. {
  2906. rdata.path += text;
  2907. rdata.path += "/";
  2908. }
  2909. return true;
  2910. }
  2911. if( is_leaf() )
  2912. return false;
  2913. char *oldPath = strdup( rdata.path.c_str() );
  2914. if( _parent != 0 )
  2915. {
  2916. rdata.path += text;
  2917. rdata.path += "/";
  2918. }
  2919. for( int i = 0; i < _children.size(); i++ )
  2920. {
  2921. if( _children.child(i)->findPath( w, rdata ) )
  2922. {
  2923. free( oldPath );
  2924. return true;
  2925. }
  2926. }
  2927. rdata.path = oldPath;
  2928. free( oldPath );
  2929. return false;
  2930. }
  2931. const char* Flu_Tree_Browser::find_path( unsigned int id )
  2932. {
  2933. // degenerate case: the root is always id==0
  2934. if( id == 0 )
  2935. return "/";
  2936. rdata.path = "/";
  2937. if( root.findPath( id, rdata ) )
  2938. return rdata.path.c_str();
  2939. else
  2940. return "";
  2941. }
  2942. const char* Flu_Tree_Browser::find_path( Fl_Widget *w )
  2943. {
  2944. rdata.path = "/";
  2945. if( root.findPath( w, rdata ) )
  2946. return rdata.path.c_str();
  2947. else
  2948. return "";
  2949. }
  2950. static char* remove_escape_chars( const char *str )
  2951. {
  2952. // remove any escape characters
  2953. char *text = strdup( str );
  2954. int tIndex = 0;
  2955. for( int pIndex = 0; pIndex < (int)strlen( str ); pIndex++ )
  2956. {
  2957. if( str[pIndex] != '\\' )
  2958. text[tIndex++] = str[pIndex];
  2959. }
  2960. text[tIndex] = '\0';
  2961. return text;
  2962. }
  2963. void Flu_Tree_Browser::Node::do_callback( int reason )
  2964. {
  2965. //if( tree->rdata.when == FL_WHEN_NEVER )
  2966. if( tree->when() == FL_WHEN_NEVER )
  2967. return;
  2968. //if( tree->rdata.cb )
  2969. {
  2970. tree->rdata.cbReason = reason;
  2971. tree->rdata.cbNode = this;
  2972. //tree->rdata.cb( tree, tree->rdata.cbd );
  2973. ((Fl_Widget*)tree)->do_callback();
  2974. }
  2975. }
  2976. unsigned short Flu_Tree_Browser::Node::depth() const
  2977. {
  2978. int d = 0;
  2979. Node *p = _parent;
  2980. while( p )
  2981. {
  2982. d++;
  2983. p = p->_parent;
  2984. }
  2985. return d;
  2986. }
  2987. Flu_Tree_Browser::Node* Flu_Tree_Browser::Node::add_branch( const char* fullpath, Fl_Widget *w, bool showLabel )
  2988. {
  2989. FluSimpleString p( fullpath );
  2990. if( p.size() && p[p.size()-1] != '/' && p[p.size()-1] != '\\' ) p += "/";
  2991. return add( p.c_str(), w, showLabel );
  2992. }
  2993. Flu_Tree_Browser::Node* Flu_Tree_Browser::Node::add_leaf( const char* fullpath, Fl_Widget *w, bool showLabel )
  2994. {
  2995. FluSimpleString p( fullpath );
  2996. if( p.size() && ( p[p.size()-1] == '/' || p[p.size()-1] == '\\' ) ) p[p.size()-1] = '\0';
  2997. return add( p.c_str(), w, showLabel );
  2998. }
  2999. Flu_Tree_Browser::Node* Flu_Tree_Browser::Node::add( const char* path, const char* name, Fl_Widget *w, bool showLabel )
  3000. {
  3001. FluSimpleString p( path );
  3002. if( p.size() && p[p.size()-1] != '/' && p[p.size()-1] != '\\' ) p += "/";
  3003. p += name;
  3004. return add( p.c_str(), w, showLabel );
  3005. }
  3006. Flu_Tree_Browser::Node* Flu_Tree_Browser::Node::add_branch( const char* path, const char* name, Fl_Widget *w, bool showLabel )
  3007. {
  3008. FluSimpleString p( path );
  3009. if( p.size() && p[p.size()-1] != '/' && p[p.size()-1] != '\\' ) p += "/";
  3010. p += name;
  3011. return add_branch( p.c_str(), w, showLabel );
  3012. }
  3013. Flu_Tree_Browser::Node* Flu_Tree_Browser::Node::add_leaf( const char* path, const char* name, Fl_Widget *w, bool showLabel )
  3014. {
  3015. FluSimpleString p( path );
  3016. if( p.size() && p[p.size()-1] != '/' && p[p.size()-1] != '\\' ) p += "/";
  3017. p += name;
  3018. return add_leaf( p.c_str(), w, showLabel );
  3019. }
  3020. Flu_Tree_Browser::Node* Flu_Tree_Browser::Node::modify( const char* path, int what, RData &rdata, Fl_Widget *w, bool showLabel )
  3021. {
  3022. // find the selected entry at rdata.searchIndex among all selected entries
  3023. if( what == GET_SELECTED )
  3024. {
  3025. if( CHECK(SELECTED) )
  3026. {
  3027. rdata.counter++;
  3028. if( rdata.counter == rdata.searchIndex )
  3029. return this;
  3030. }
  3031. for( int i = 0; i < _children.size(); i++ )
  3032. {
  3033. Node *n = _children.child(i)->modify( path, what, rdata, w );
  3034. if( n )
  3035. return n;
  3036. }
  3037. return NULL;
  3038. }
  3039. // trivial test for a bogus empty path
  3040. if( path == 0 )
  3041. return NULL;
  3042. // if the path starts with '/', skip the '/'
  3043. if( path[0] == '/' )
  3044. path++;
  3045. // trivial test for a bogus empty path
  3046. if( path[0] == '\0' )
  3047. return NULL;
  3048. const char *remainingPath;
  3049. char *nodeName;
  3050. bool lastNode, branchNode;
  3051. Node *retNode = NULL;
  3052. ///////////// extract the next node name from the path ///////////////////
  3053. // find the next '/' that is not preceded by the escape character '\'
  3054. const char *slash = strchr( path, '/' );
  3055. for(;;)
  3056. {
  3057. // find the next '/'
  3058. if( slash == NULL ) // there isn't one, so we're done
  3059. break;
  3060. // test for escape character
  3061. else if( slash[-1] == '\\' ) // path[0] can never be '/', so this is a safe test
  3062. slash = strchr( slash+1, '/' );
  3063. // we have it
  3064. else
  3065. break;
  3066. }
  3067. // if there is no slash, then the node name is the path and it is a leaf and the last node in the path
  3068. if( slash == NULL )
  3069. {
  3070. branchNode = false;
  3071. char *name = strdup( path ); // copy the path
  3072. nodeName = remove_escape_chars( name ); // remove the escape characters
  3073. free( name );
  3074. lastNode = true;
  3075. remainingPath = NULL;
  3076. }
  3077. // otherwise the node name is the path up to the slash, it is also a branch and may not be the last node in the path
  3078. else
  3079. {
  3080. branchNode = true;
  3081. char *name = (char*)malloc( slash-path+1 );
  3082. strncpy( name, path, slash-path );
  3083. name[slash-path] = '\0';
  3084. nodeName = remove_escape_chars( name ); // remove the escape characters
  3085. free( name );
  3086. lastNode = ( slash[1] == '\0' ); // this is the last node if there is nothing after the slash
  3087. if( lastNode )
  3088. {
  3089. //if( rdata.autoBranches )
  3090. //branchNode = false;
  3091. remainingPath = NULL;
  3092. }
  3093. else
  3094. remainingPath = slash+1;
  3095. }
  3096. ///////////// process the node ///////////////////
  3097. switch( what )
  3098. {
  3099. case ADD:
  3100. {
  3101. // if the new node is a leaf node, add the string as a leaf and return
  3102. if( !branchNode )
  3103. {
  3104. // is there already a node with this name?
  3105. Node *n = _children.find( nodeName );
  3106. if( n )
  3107. {
  3108. // if that node is a branch node, we can't add a new one with the same name
  3109. if( n->is_branch() )
  3110. break;
  3111. // if we are not allowed to add multiple nodes with the same name,
  3112. // then just return
  3113. if( !rdata.allowLeafDuplication )
  3114. break;
  3115. }
  3116. // add a new node
  3117. retNode = new Node( true, nodeName, this, rdata, w, showLabel );
  3118. _children.add( retNode );
  3119. rdata.forceResize = true;
  3120. rdata.visibilityChanged = true;
  3121. if( tree->rdata.autoBranches )
  3122. initType();
  3123. }
  3124. // otherwise make sure the node name exists as a branch and recurse on it
  3125. else
  3126. {
  3127. // if there is already a node with this name, just use it
  3128. Node *n = NULL;
  3129. n = _children.find( nodeName );
  3130. if( n )
  3131. {
  3132. // make sure it is a branch
  3133. if( n->is_leaf() )
  3134. break;
  3135. if( rdata.allowBranchDuplication )
  3136. n = NULL;
  3137. }
  3138. // else add a new node
  3139. if( n == NULL )
  3140. {
  3141. // only add the widget for the last node
  3142. n = new Node( false, nodeName, this, rdata, lastNode?w:NULL, lastNode?showLabel:true );
  3143. _children.add( n );
  3144. rdata.forceResize = true;
  3145. rdata.visibilityChanged = true;
  3146. }
  3147. if( tree->rdata.autoBranches )
  3148. initType();
  3149. // recurse on the remainder of the path, if not the last node
  3150. if( lastNode )
  3151. retNode = n;
  3152. else
  3153. retNode = n->modify( remainingPath, what, rdata, w, showLabel );
  3154. }
  3155. tree->redraw();
  3156. }
  3157. break;
  3158. case REMOVE:
  3159. {
  3160. // try to find the indicated node. if we can't find it, just return
  3161. Node *n = _children.find( nodeName );
  3162. if( !n )
  3163. break;
  3164. // if this is the last node, remove it.
  3165. if( lastNode )
  3166. {
  3167. ptrdiff_t ID = n->id();
  3168. _children.erase( n );
  3169. //if( tree->rdata.cbNode == n )
  3170. //tree->rdata.cbNode = NULL;
  3171. delete n;
  3172. retNode = (Node*)ID; // non-null return value means remove was successful
  3173. rdata.forceResize = true;
  3174. rdata.visibilityChanged = true;
  3175. if( tree->rdata.autoBranches )
  3176. initType();
  3177. tree->redraw();
  3178. }
  3179. // otherwise recurse on the remainder of the path
  3180. else
  3181. retNode = n->modify( remainingPath, what, rdata, w, showLabel );
  3182. }
  3183. break;
  3184. case FIND:
  3185. {
  3186. // if this node equals the starting node for a find_next,
  3187. // then by clearing rdata.previous we flag that we are allowed to return the next match
  3188. if( rdata.previous == this )
  3189. rdata.previous = NULL;
  3190. Node *n = NULL;
  3191. if( !lastNode )
  3192. {
  3193. // if, according to the path, this is not the last node, then just recursively
  3194. // search for the named node
  3195. n = _children.find( nodeName );
  3196. if( !n )
  3197. break;
  3198. retNode = n->modify( remainingPath, what, rdata, w, showLabel );
  3199. }
  3200. else
  3201. {
  3202. // otherwise, according to the path, this is the last node (i.e. a leaf).
  3203. // since only leaves can have multiple identical entries,
  3204. // try to find the indicated node, accounting for the possibility
  3205. // that it may not be the one we're after
  3206. int next = 1;
  3207. for(;;)
  3208. {
  3209. // look for the named node
  3210. n = _children.find( nodeName, next++ );
  3211. // if we can't find it, just return, because it's not here
  3212. if( !n )
  3213. break;
  3214. // we are only allowed to return a match if the previous node is NULL,
  3215. // indicating we have passed the starting node for a find_next
  3216. if( rdata.previous == NULL )
  3217. {
  3218. retNode = n;
  3219. break;
  3220. }
  3221. // if the found node equals the starting node for a find_next,
  3222. // then by clearing rdata.previous we flag that we are allowed to return the next match
  3223. if( rdata.previous == n )
  3224. rdata.previous = NULL;
  3225. }
  3226. }
  3227. }
  3228. break;
  3229. case FIND_NUMBER:
  3230. {
  3231. if( lastNode ) // can only match multiple leaves if the path says this is the last node
  3232. {
  3233. rdata.counter += _children.findNum( nodeName );
  3234. }
  3235. else // otherwise recurse down the remaining path
  3236. {
  3237. Node *n = _children.find( nodeName );
  3238. n->modify( remainingPath, what, rdata, w, showLabel );
  3239. }
  3240. }
  3241. break;
  3242. }
  3243. free( nodeName );
  3244. return retNode;
  3245. }
  3246. void Flu_Tree_Browser::Node::widgetCB()
  3247. {
  3248. if( _widget )
  3249. {
  3250. if( _widget->CB )
  3251. _widget->CB( _widget->w, _widget->CBData );
  3252. }
  3253. do_callback( FLU_WIDGET_CALLBACK );
  3254. }
  3255. void Flu_Tree_Browser::Node::widget( Fl_Widget *w )
  3256. {
  3257. tree->rdata.forceResize = true;
  3258. // delete existing widget
  3259. if( _widget )
  3260. {
  3261. Fl_Group *p = _widget->w->parent();
  3262. if( p )
  3263. p->remove( *(_widget->w) );
  3264. delete _widget->w;
  3265. delete _widget;
  3266. _widget = NULL;
  3267. }
  3268. if( !w )
  3269. return;
  3270. // if the widget is already in the tree, then it will be in another node.
  3271. // we need to make sure that the node gets rid of its reference too
  3272. Node *n = tree->first();
  3273. while( n )
  3274. {
  3275. if( n->_widget && n->_widget->w == w )
  3276. {
  3277. n->_widget->w = 0;
  3278. delete n->_widget;
  3279. n->_widget = 0;
  3280. }
  3281. n = n->next();
  3282. }
  3283. // allocate new widget struct and initialize
  3284. _widget = new WidgetInfo;
  3285. _widget->w = w;
  3286. _widget->defaultW = _widget->w->w();
  3287. if( USE_FLU_WIDGET_CALLBACK )
  3288. {
  3289. _widget->CB = _widget->w->callback();
  3290. _widget->CBData = _widget->w->user_data();
  3291. _widget->w->callback( _widgetCB, this );
  3292. }
  3293. // remove the widget from its parent
  3294. {
  3295. Fl_Group *p = w->parent();
  3296. if( p )
  3297. p->remove( *w );
  3298. }
  3299. // add the widget to the parent group of this node
  3300. Node *p;
  3301. if( is_root() )
  3302. {
  3303. //tree->_box->add( w );
  3304. p = this;
  3305. }
  3306. else
  3307. {
  3308. //Node *p = parent();
  3309. p = parent();
  3310. }
  3311. if( !p->_group )
  3312. {
  3313. p->_group = new Fl_Group( tree->_box->x(), tree->_box->y(), tree->_box->w(), tree->_box->h() );
  3314. p->_group->end();
  3315. p->_group->resizable( NULL );
  3316. tree->_box->add( p->_group );
  3317. }
  3318. p->_group->add( w );
  3319. sort_widgets();
  3320. }
  3321. void Flu_Tree_Browser::Node::branch_icons( Fl_Image *closed, Fl_Image *open )
  3322. {
  3323. if( is_branch() )
  3324. {
  3325. bIcon[0] = closed;
  3326. bIcon[1] = open;
  3327. tree->rdata.forceResize = true;
  3328. }
  3329. }
  3330. void Flu_Tree_Browser::Node::collapse_icons( Fl_Image *closed, Fl_Image *open )
  3331. {
  3332. if( is_branch() )
  3333. {
  3334. if( !closed || !open )
  3335. {
  3336. cIcon[0] = tree->rdata.defaultCollapseIcons[0];
  3337. cIcon[1] = tree->rdata.defaultCollapseIcons[1];
  3338. }
  3339. else
  3340. {
  3341. cIcon[0] = closed;
  3342. cIcon[1] = open;
  3343. }
  3344. tree->rdata.forceResize = true;
  3345. }
  3346. }
  3347. void Flu_Tree_Browser::Node::leaf_icon( Fl_Image *icon )
  3348. {
  3349. if( is_leaf() )
  3350. {
  3351. lIcon = icon;
  3352. tree->rdata.forceResize = true;
  3353. }
  3354. }
  3355. bool Flu_Tree_Browser::Node::is_branch() const
  3356. {
  3357. if( tree->rdata.autoBranches )
  3358. return( _children.size() != 0 );
  3359. else
  3360. return !CHECK(LEAF);
  3361. }
  3362. bool Flu_Tree_Browser::Node::is_leaf() const
  3363. {
  3364. if( tree->rdata.autoBranches )
  3365. return( _children.size() == 0 && !is_root() );
  3366. else
  3367. return CHECK(LEAF);
  3368. }