Flu_File_Chooser.cpp 109 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258
  1. // $Id: Flu_File_Chooser.cpp,v 1.103 2005/03/14 15:31:45 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 <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <ctype.h>
  16. #include <time.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #define FLU_USE_REGISTRY
  20. #ifdef WIN32
  21. #include <windows.h>
  22. #include <shlobj.h>
  23. #include <shellapi.h>
  24. #include <lmcons.h>
  25. #endif
  26. #if defined WIN32 && !defined CYGWIN
  27. #include <direct.h>
  28. #else
  29. #include <unistd.h>
  30. #endif
  31. #include <FL/Fl.H>
  32. #include <FL/fl_draw.H>
  33. #include <FL/fl_ask.H>
  34. #include <FL/math.h>
  35. #include <FL/filename.H>
  36. #include <FL/Fl_Pixmap.H>
  37. #include <FL/Fl_Scroll.H>
  38. #include <FL/Fl_Shared_Image.H>
  39. #include "FLU/flu_pixmaps.h"
  40. #include "FLU/Flu_Label.H"
  41. #include "FLU/Flu_Tree_Browser.H"
  42. #include "FLU/Flu_Separator.H"
  43. #include "FLU/Flu_Enumerations.H"
  44. #include "FLU/Flu_File_Chooser.H"
  45. #include "FLU/flu_file_chooser_pixmaps.h"
  46. #include "FLU/flu_pixmaps.h"
  47. // set default language strings
  48. FluSimpleString Flu_File_Chooser::favoritesTxt = "Favorites";
  49. #ifdef WIN32
  50. FluSimpleString Flu_File_Chooser::myComputerTxt = "My Computer";
  51. FluSimpleString Flu_File_Chooser::myDocumentsTxt = "My Documents";
  52. FluSimpleString Flu_File_Chooser::desktopTxt = "Desktop";
  53. #else
  54. FluSimpleString Flu_File_Chooser::myComputerTxt = "Home";
  55. FluSimpleString Flu_File_Chooser::myDocumentsTxt = "Temporary";
  56. FluSimpleString Flu_File_Chooser::desktopTxt = "Desktop";
  57. #endif
  58. FluSimpleString Flu_File_Chooser::detailTxt[4] = { "Name", "Size", "Date", "Type" };
  59. FluSimpleString Flu_File_Chooser::contextMenuTxt[3] = { "New Folder", "Rename", "Delete" };
  60. FluSimpleString Flu_File_Chooser::diskTypesTxt[6] = { "Floppy Disk", "Removable Disk",
  61. "Local Disk", "Compact Disk",
  62. "Network Disk", "RAM Disk" };
  63. FluSimpleString Flu_File_Chooser::filenameTxt = "Filename";
  64. FluSimpleString Flu_File_Chooser::okTxt = "Ok";
  65. FluSimpleString Flu_File_Chooser::cancelTxt = "Cancel";
  66. FluSimpleString Flu_File_Chooser::locationTxt = "Location";
  67. FluSimpleString Flu_File_Chooser::showHiddenTxt = "Show Hidden Files";
  68. FluSimpleString Flu_File_Chooser::fileTypesTxt = "File Types";
  69. FluSimpleString Flu_File_Chooser::directoryTxt = "Directory";
  70. FluSimpleString Flu_File_Chooser::allFilesTxt = "All Files (*)";
  71. FluSimpleString Flu_File_Chooser::defaultFolderNameTxt = "New Folder";
  72. FluSimpleString Flu_File_Chooser::backTTxt = "Go back one directory in the history";
  73. FluSimpleString Flu_File_Chooser::forwardTTxt = "Go forward one directory in the history";
  74. FluSimpleString Flu_File_Chooser::upTTxt = "Go to the parent directory";
  75. FluSimpleString Flu_File_Chooser::reloadTTxt = "Refresh this directory";
  76. FluSimpleString Flu_File_Chooser::trashTTxt = "Delete file(s)";
  77. FluSimpleString Flu_File_Chooser::newDirTTxt = "Create new directory";
  78. FluSimpleString Flu_File_Chooser::addFavoriteTTxt = "Add this directory to my favorites";
  79. FluSimpleString Flu_File_Chooser::previewTTxt = "Preview files";
  80. FluSimpleString Flu_File_Chooser::listTTxt = "View as list";
  81. FluSimpleString Flu_File_Chooser::wideListTTxt = "View as wide list";
  82. FluSimpleString Flu_File_Chooser::detailTTxt = "View details";
  83. FluSimpleString Flu_File_Chooser::createFolderErrTxt = "Could not create directory '%s'. You may not have permission to perform this operation.";
  84. FluSimpleString Flu_File_Chooser::deleteFileErrTxt = "An error ocurred while trying to delete '%s'.";
  85. FluSimpleString Flu_File_Chooser::fileExistsErrTxt = "File '%s' already exists!";
  86. FluSimpleString Flu_File_Chooser::renameErrTxt = "Unable to rename '%s' to '%s'";
  87. // just a string that no file could probably ever be called
  88. #define FAVORITES_UNIQUE_STRING "\t!@#$%^&*(Favorites)-=+"
  89. #define DEFAULT_ENTRY_WIDTH 235
  90. Fl_Pixmap up_folder_img( (char*const*)big_folder_up_xpm ),
  91. trash( (char*const*)trash_xpm ),
  92. new_folder( (char*const*)big_folder_new_xpm ),
  93. reload( (char*const*)reload_xpm ),
  94. preview_img( (char*const*)monalisa_xpm ),
  95. file_list_img( (char*const*)filelist_xpm ),
  96. file_listwide_img( (char*const*)filelistwide_xpm ),
  97. fileDetails( (char*const*)filedetails_xpm ),
  98. add_to_favorite_folder( (char*const*)folder_favorite_xpm ),
  99. home( (char*const*)bighome_xpm ),
  100. favorites( (char*const*)bigfavorites_xpm ),
  101. desktop( (char*const*)desktop_xpm ),
  102. folder_closed( (char*const*)folder_closed_xpm ),
  103. default_file( (char*const*)textdoc_xpm ),
  104. my_computer( (char*const*)my_computer_xpm ),
  105. computer( (char*const*)computer_xpm ),
  106. disk_drive( (char*const*)disk_drive_xpm ),
  107. cd_drive( (char*const*)cd_drive_xpm ),
  108. floppy_drive( (char*const*)floppy_drive_xpm ),
  109. removable_drive( (char*const*)removable_drive_xpm ),
  110. ram_drive( (char*const*)ram_drive_xpm ),
  111. network_drive( (char*const*)network_drive_xpm ),
  112. documents( (char*const*)filled_folder_xpm ),
  113. littlehome( (char*const*)home_xpm ),
  114. little_favorites( (char*const*)mini_folder_favorites_xpm ),
  115. little_desktop( (char*const*)mini_desktop_xpm ),
  116. bigdocuments( (char*const*)bigdocuments_xpm ),
  117. bigtemporary( (char*const*)bigtemporary_xpm );
  118. #define streq(a,b) (strcmp(a,b)==0)
  119. Flu_File_Chooser::FileTypeInfo* Flu_File_Chooser::types = NULL;
  120. int Flu_File_Chooser::numTypes = 0;
  121. int Flu_File_Chooser::typeArraySize = 0;
  122. Flu_File_Chooser::ContextHandlerVector Flu_File_Chooser::contextHandlers;
  123. Flu_File_Chooser::PreviewHandlerVector Flu_File_Chooser::previewHandlers;
  124. Flu_File_Chooser::ImgTxtPreview* Flu_File_Chooser::imgTxtPreview = 0;
  125. int (*Flu_File_Chooser::customSort)(const char*,const char*) = 0;
  126. FluSimpleString Flu_File_Chooser::dArrow[4];
  127. FluSimpleString Flu_File_Chooser::uArrow[4];
  128. #ifdef WIN32
  129. // Internationalized windows folder name access
  130. // Fix suggested by Fabien Costantini
  131. /*
  132. CSIDL_DESKTOPDIRECTORY -- desktop
  133. CSIDL_PERSONAL -- my documents
  134. CSIDL_PERSONAL and strip back to last "/" -> home
  135. */
  136. static FluSimpleString flu_get_special_folder( int csidl )
  137. {
  138. static char path[MAX_PATH+1];
  139. #ifdef FLU_USE_REGISTRY
  140. HKEY key;
  141. DWORD size = MAX_PATH;
  142. const char *keyQuery = "";
  143. switch( csidl )
  144. {
  145. case CSIDL_DESKTOPDIRECTORY: keyQuery = "Desktop"; break;
  146. case CSIDL_PERSONAL: keyQuery = "Personal"; break;
  147. }
  148. if( RegOpenKeyEx( HKEY_CURRENT_USER,
  149. "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
  150. 0, KEY_QUERY_VALUE, &key ) != ERROR_SUCCESS )
  151. return "";
  152. if( RegQueryValueEx( key, keyQuery, 0, 0, (LPBYTE)path, &size ) != ERROR_SUCCESS )
  153. return "";
  154. RegCloseKey( key );
  155. return path;
  156. #else
  157. path[0] = '\0';
  158. if( SUCCEEDED( SHGetSpecialFolderPath( NULL, path, csidl, FALSE ) ) )
  159. //if( SUCCEEDED( SHGetFolderPath( NULL, csidl, NULL, 0, path ) ) )
  160. {
  161. int len = strlen(path);
  162. if( len > 0 && path[len-1] != '/' && path[len-1] != '\\' )
  163. strcat( path, "/" );
  164. return path;
  165. }
  166. return "";
  167. #endif
  168. }
  169. #endif
  170. // taken explicitly from fltk/src/filename_match.cxx
  171. // and changed to support case-sensitive matching
  172. static int flu_filename_match(const char *s, const char *p)
  173. {
  174. int matched;
  175. for (;;) {
  176. switch(*p++) {
  177. case '?' : // match any single character
  178. if (!*s++) return 0;
  179. break;
  180. case '*' : // match 0-n of any characters
  181. if (!*p) return 1; // do trailing * quickly
  182. while (!flu_filename_match(s, p)) if (!*s++) return 0;
  183. return 1;
  184. case '[': { // match one character in set of form [abc-d] or [^a-b]
  185. if (!*s) return 0;
  186. int reverse = (*p=='^' || *p=='!'); if (reverse) p++;
  187. matched = 0;
  188. char last = 0;
  189. while (*p) {
  190. if (*p=='-' && last) {
  191. if (*s <= *++p && *s >= last ) matched = 1;
  192. last = 0;
  193. } else {
  194. if (*s == *p) matched = 1;
  195. }
  196. last = *p++;
  197. if (*p==']') break;
  198. }
  199. if (matched == reverse) return 0;
  200. s++; p++;}
  201. break;
  202. case '{' : // {pattern1|pattern2|pattern3}
  203. NEXTCASE:
  204. if (flu_filename_match(s,p)) return 1;
  205. for (matched = 0;;) {
  206. switch (*p++) {
  207. case '\\': if (*p) p++; break;
  208. case '{': matched++; break;
  209. case '}': if (!matched--) return 0; break;
  210. case '|': case ',': if (matched==0) goto NEXTCASE;
  211. case 0: return 0;
  212. }
  213. }
  214. case '|': // skip rest of |pattern|pattern} when called recursively
  215. case ',':
  216. for (matched = 0; *p && matched >= 0;) {
  217. switch (*p++) {
  218. case '\\': if (*p) p++; break;
  219. case '{': matched++; break;
  220. case '}': matched--; break;
  221. }
  222. }
  223. break;
  224. case '}':
  225. break;
  226. case 0: // end of pattern
  227. return !*s;
  228. case '\\': // quote next character
  229. if (*p) p++;
  230. default:
  231. #ifdef WIN32
  232. if (tolower(*s) != tolower(*(p-1))) return 0;
  233. #else
  234. if( *s != *(p-1) ) return 0;
  235. #endif
  236. s++;
  237. break;
  238. }
  239. }
  240. }
  241. void Flu_File_Chooser :: add_context_handler( int type, const char *ext, const char *name,
  242. void (*cb)(const char*,int,void*), void *cbd )
  243. {
  244. if( cb == NULL )
  245. return;
  246. ContextHandler h;
  247. h.ext = ext ? ext : "";
  248. h.ext.downcase();
  249. h.type = type;
  250. h.name = name;
  251. h.callback = cb;
  252. h.callbackData = cbd;
  253. Flu_File_Chooser::contextHandlers.add( h );
  254. }
  255. void Flu_File_Chooser :: add_preview_handler( PreviewWidgetBase *w )
  256. {
  257. if( w == NULL )
  258. return;
  259. Flu_File_Chooser::previewHandlers.add( w );
  260. }
  261. // extensions == NULL implies directories
  262. void Flu_File_Chooser :: add_type( const char *extensions, const char *short_description, Fl_Image *icon )
  263. {
  264. FluSimpleString ext;
  265. if( extensions )
  266. ext = extensions;
  267. else
  268. ext = "\t"; // indicates a directory
  269. ext.upcase();
  270. // are we overwriting an existing type?
  271. for( int i = 0; i < numTypes; i++ )
  272. {
  273. if( types[i].extensions == ext )
  274. {
  275. types[i].icon = icon;
  276. types[i].type = short_description;
  277. return;
  278. }
  279. }
  280. if( numTypes == typeArraySize )
  281. {
  282. int newSize = ( typeArraySize == 0 ) ? 1 : typeArraySize*2; // double the size of the old list (same behavior as STL vector)
  283. // allocate the new list
  284. FileTypeInfo* newTypes = new FileTypeInfo[ newSize ];
  285. // copy the old list to the new list
  286. for( int i = 0; i < numTypes; i++ )
  287. {
  288. newTypes[i].icon = types[i].icon;
  289. newTypes[i].extensions = types[i].extensions;
  290. newTypes[i].type = types[i].type;
  291. }
  292. // delete the old list and replace it with the new list
  293. delete[] types;
  294. types = newTypes;
  295. typeArraySize = newSize;
  296. }
  297. types[numTypes].icon = icon;
  298. types[numTypes].extensions = ext;
  299. types[numTypes].type = short_description;
  300. numTypes++;
  301. }
  302. Flu_File_Chooser::FileTypeInfo* Flu_File_Chooser :: find_type( const char *extension )
  303. {
  304. FluSimpleString ext;
  305. if( extension )
  306. ext = extension;
  307. else
  308. ext = "\t"; // indicates a directory
  309. ext.upcase();
  310. // lookup the type based on the extension
  311. for( int i = 0; i < numTypes; i++ )
  312. {
  313. // check extension against every token
  314. FluSimpleString e = types[i].extensions;
  315. char *tok = strtok( (char*)e.c_str(), " ," );
  316. while( tok )
  317. {
  318. if( ext == tok )
  319. return &(types[i]);
  320. tok = strtok( NULL, " ," );
  321. }
  322. }
  323. return NULL;
  324. }
  325. Flu_File_Chooser :: Flu_File_Chooser( const char *pathname, const char *pat, int type, const char *title )
  326. : Fl_Double_Window( 600, 400, title ),
  327. filename( 70, h()-60, w()-70-85-10, 25, "", this ),
  328. ok( w()-90, h()-60, 85, 25 ),
  329. cancel( w()-90, h()-30, 85, 25 ),
  330. entryPopup( 0, 0, 0, 0 )
  331. {
  332. int oldNormalSize = FL_NORMAL_SIZE;
  333. FL_NORMAL_SIZE = 12;
  334. fl_register_images();
  335. _callback = 0;
  336. _userdata = 0;
  337. Fl_Double_Window::callback( _hideCB, this );
  338. Fl_Double_Window::size_range( 600, 400 );
  339. Fl_Group *g;
  340. filename.label( filenameTxt.c_str() );
  341. ok.label( okTxt.c_str() );
  342. ok.labelsize( FL_NORMAL_SIZE );
  343. cancel.label( cancelTxt.c_str() );
  344. cancel.labelsize( FL_NORMAL_SIZE );
  345. add_type( NULL, directoryTxt.c_str(), &folder_closed );
  346. for( int j = 0; j < 4; j++ )
  347. {
  348. dArrow[j] = "@-12DnArrow " + detailTxt[j];
  349. uArrow[j] = "@-18UpArrow " + detailTxt[j];
  350. }
  351. history = currentHist = NULL;
  352. walkingHistory = false;
  353. fileEditing = false;
  354. #ifdef WIN32
  355. refreshDrives = true;
  356. caseSort = false;
  357. #else
  358. caseSort = true;
  359. #endif
  360. // determine the system paths for the user's home area, desktop, documents, app data, etc
  361. #ifdef WIN32
  362. userDesktop = flu_get_special_folder( CSIDL_DESKTOPDIRECTORY );
  363. userDocs = flu_get_special_folder( CSIDL_PERSONAL );
  364. // get home area by stripping off to the last '/' from docs
  365. userHome = userDocs;
  366. {
  367. for( int i = userHome.size()-1; i > 0; i-- )
  368. {
  369. if( userHome[i] == '/' )
  370. {
  371. userHome[i] = '\0';
  372. break;
  373. }
  374. }
  375. }
  376. // construct the user desktop path
  377. //userDesktop = userHome + "/" + desktopTxt;
  378. win2unix( userDesktop );
  379. win2unix( userDocs );
  380. // make sure they don't end in '/'
  381. if( userDesktop[userDesktop.size()-1] == '/' )
  382. userDesktop[userDesktop.size()-1] = '\0';
  383. if( userDocs[userDocs.size()-1] == '/' )
  384. userDocs[userDocs.size()-1] = '\0';
  385. // get the actual name of the "My Documents" folder by pulling off the last name in the field
  386. // we do this because the actual name may vary from country to country
  387. {
  388. int slash = userDesktop.rfind( '/' );
  389. if( slash != -1 )
  390. desktopTxt = userDesktop.c_str() + slash + 1;
  391. slash = userDocs.rfind( '/' );
  392. if( slash != -1 )
  393. myDocumentsTxt = userDocs.c_str() + slash + 1;
  394. }
  395. // make sure they end in '/'
  396. userHome += "/";
  397. userDesktop += "/";
  398. userDocs += "/";
  399. #else
  400. {
  401. char buf[1024];
  402. fl_filename_expand( buf, 1024, "~/" );
  403. userHome = buf;
  404. userDesktop = userHome + "/" + desktopTxt + "/";
  405. userDocs = "/tmp/";
  406. }
  407. #endif
  408. configFilename = userHome + ".Flu_File_Chooser.favorites";
  409. selectionType = type;
  410. filenameEnterCallback = filenameTabCallback = false;
  411. sortMethod = SORT_NAME;
  412. lastSelected = NULL;
  413. filename.labelsize( 12 );
  414. filename.when( FL_WHEN_ENTER_KEY_ALWAYS );
  415. filename.callback( _filenameCB, this );
  416. filename.value( "" );
  417. Fl_Group *quickIcons = new Fl_Group( 5, 5, 100, h()-10-60 );
  418. quickIcons->box( FL_DOWN_BOX );
  419. quickIcons->color( FL_DARK3 );
  420. Flu_Button *desktopBtn = new Flu_Button( 30, 18, 50, 48 );
  421. desktopBtn->box( FL_FLAT_BOX );
  422. desktopBtn->image( desktop );
  423. desktopBtn->enter_box( FL_THIN_UP_BOX );
  424. desktopBtn->color( FL_DARK3 );
  425. desktopBtn->callback( _desktopCB, this );
  426. {
  427. Flu_Label *l = new Flu_Label( 5, 62, 100, 20, desktopTxt.c_str() );
  428. l->labelcolor( FL_WHITE );
  429. l->align( FL_ALIGN_CENTER );
  430. }
  431. Flu_Button *homeBtn = new Flu_Button( 30, 98, 50, 48 );
  432. homeBtn->box( FL_FLAT_BOX );
  433. homeBtn->enter_box( FL_THIN_UP_BOX );
  434. homeBtn->color( FL_DARK3 );
  435. homeBtn->callback( _homeCB, this );
  436. {
  437. #ifdef WIN32
  438. Flu_Label *l = new Flu_Label( 5, 142, 100, 20, myComputerTxt.c_str() );
  439. homeBtn->image( my_computer );
  440. #else
  441. Flu_Label *l = new Flu_Label( 5, 142, 100, 20, myComputerTxt.c_str() );
  442. homeBtn->image( home );
  443. #endif
  444. l->labelcolor( FL_WHITE );
  445. l->align( FL_ALIGN_CENTER );
  446. }
  447. Flu_Button *documentsBtn = new Flu_Button( 30, 178, 50, 48 );
  448. documentsBtn->box( FL_FLAT_BOX );
  449. documentsBtn->enter_box( FL_THIN_UP_BOX );
  450. documentsBtn->labelcolor( FL_WHITE );
  451. documentsBtn->color( FL_DARK3 );
  452. documentsBtn->callback( _documentsCB, this );
  453. {
  454. #ifdef WIN32
  455. Flu_Label *l = new Flu_Label( 5, 222, 100, 20, myDocumentsTxt.c_str() );
  456. documentsBtn->image( &bigdocuments );
  457. #else
  458. Flu_Label *l = new Flu_Label( 5, 222, 100, 20, myDocumentsTxt.c_str() );
  459. documentsBtn->image( &bigtemporary );
  460. #endif
  461. l->labelcolor( FL_WHITE );
  462. l->align( FL_ALIGN_CENTER );
  463. }
  464. Flu_Button *favoritesBtn = new Flu_Button( 30, 258, 50, 48 );
  465. favoritesBtn->box( FL_FLAT_BOX );
  466. favoritesBtn->image( favorites );
  467. favoritesBtn->enter_box( FL_THIN_UP_BOX );
  468. favoritesBtn->color( FL_DARK3 );
  469. favoritesBtn->callback( _favoritesCB, this );
  470. {
  471. Flu_Label *l = new Flu_Label( 5, 302, 100, 20, favoritesTxt.c_str() );
  472. l->labelcolor( FL_WHITE );
  473. l->align( FL_ALIGN_CENTER );
  474. }
  475. favoritesList = new Fl_Browser( 0, 0, 0, 0 );
  476. favoritesList->hide();
  477. {
  478. Fl_Group* dummy = new Fl_Group( 5, h()-10-61, 100, 1 );
  479. quickIcons->resizable( dummy );
  480. }
  481. quickIcons->end();
  482. Fl_Group *dummy = new Fl_Group( 110, 0, w()-110, 70 );
  483. locationQuickJump = new Fl_Group( 166, 5, w()-171, 8 );
  484. locationQuickJump->box( FL_NO_BOX );
  485. locationQuickJump->end();
  486. location = new Flu_Combo_Tree( 166, 15, w()-171, 22, locationTxt.c_str() );
  487. location->editable( false );
  488. location->pop_height( 200 );
  489. location->tree.all_branches_always_open( true );
  490. #ifdef WIN32
  491. location->tree.show_root( false );
  492. #endif
  493. location->tree.show_connectors( false );
  494. location->tree.horizontal_gap( -10 );
  495. location->tree.show_leaves( false );
  496. location->callback( _locationCB, this );
  497. ////////////////////////////////////////////////////////////////
  498. g = new Fl_Group( 110, 40, w()-110, 30 ); // group enclosing all the buttons at top
  499. hiddenFiles = new Fl_Check_Button( 110, 43, 130, 25, showHiddenTxt.c_str() );
  500. hiddenFiles->callback( reloadCB, this );
  501. #ifdef WIN32
  502. hiddenFiles->hide();
  503. #endif
  504. backBtn = new Flu_Button( 285, 43, 25, 25, "@<-" );
  505. backBtn->labelcolor( fl_rgb_color( 80, 180, 200 ) );
  506. backBtn->labelsize( 16 );
  507. backBtn->box( FL_FLAT_BOX );
  508. backBtn->enter_box( FL_THIN_UP_BOX );
  509. backBtn->callback( _backCB, this );
  510. backBtn->tooltip( backTTxt.c_str() );
  511. forwardBtn = new Flu_Button( 310, 43, 25, 25, "@->" );
  512. forwardBtn->labelcolor( fl_rgb_color( 80, 180, 200 ) );
  513. forwardBtn->labelsize( 16 );
  514. forwardBtn->box( FL_FLAT_BOX );
  515. forwardBtn->enter_box( FL_THIN_UP_BOX );
  516. forwardBtn->callback( _forwardCB, this );
  517. forwardBtn->tooltip( forwardTTxt.c_str() );
  518. upDirBtn = new Flu_Button( 335, 43, 25, 25 );
  519. upDirBtn->image( up_folder_img );
  520. upDirBtn->box( FL_FLAT_BOX );
  521. upDirBtn->enter_box( FL_THIN_UP_BOX );
  522. upDirBtn->callback( upDirCB, this );
  523. upDirBtn->tooltip( upTTxt.c_str() );
  524. reloadBtn = new Flu_Button( 360, 43, 25, 25 );
  525. reloadBtn->image( reload );
  526. reloadBtn->box( FL_FLAT_BOX );
  527. reloadBtn->enter_box( FL_THIN_UP_BOX );
  528. reloadBtn->callback( reloadCB, this );
  529. reloadBtn->tooltip( reloadTTxt.c_str() );
  530. {
  531. Flu_Separator *sep = new Flu_Separator( 385, 42, 10, 28 );
  532. sep->type( Flu_Separator::VERTICAL );
  533. sep->box( FL_ENGRAVED_BOX );
  534. }
  535. trashBtn = new Flu_Button( 395, 43, 25, 25 );
  536. trashBtn->image( trash );
  537. trashBtn->box( FL_FLAT_BOX );
  538. trashBtn->enter_box( FL_THIN_UP_BOX );
  539. trashBtn->callback( _trashCB, this );
  540. trashBtn->tooltip( trashTTxt.c_str() );
  541. newDirBtn = new Flu_Button( 420, 43, 25, 25 );
  542. newDirBtn->image( new_folder );
  543. newDirBtn->box( FL_FLAT_BOX );
  544. newDirBtn->enter_box( FL_THIN_UP_BOX );
  545. newDirBtn->callback( _newFolderCB, this );
  546. newDirBtn->tooltip( newDirTTxt.c_str() );
  547. addFavoriteBtn = new Flu_Button( 445, 43, 25, 25 );
  548. addFavoriteBtn->image( add_to_favorite_folder );
  549. addFavoriteBtn->box( FL_FLAT_BOX );
  550. addFavoriteBtn->enter_box( FL_THIN_UP_BOX );
  551. addFavoriteBtn->callback( _addToFavoritesCB, this );
  552. addFavoriteBtn->tooltip( addFavoriteTTxt.c_str() );
  553. {
  554. Flu_Separator *sep = new Flu_Separator( 470, 42, 10, 28 );
  555. sep->type( Flu_Separator::VERTICAL );
  556. sep->box( FL_ENGRAVED_BOX );
  557. }
  558. previewBtn = new Flu_Button( 482, 43, 23, 25 );
  559. previewBtn->type( FL_TOGGLE_BUTTON );
  560. previewBtn->image( preview_img );
  561. previewBtn->callback( _previewCB, this );
  562. previewBtn->tooltip( previewTTxt.c_str() );
  563. {
  564. Fl_Group *g2 = new Fl_Group( 511, 43, 81, 25 );
  565. fileListBtn = new Flu_Button( 511, 43, 25, 25 );
  566. fileListBtn->type( FL_RADIO_BUTTON );
  567. fileListBtn->value(1);
  568. fileListBtn->callback( _listModeCB, this );
  569. fileListBtn->image( file_list_img );
  570. fileListBtn->tooltip( listTTxt.c_str() );
  571. fileListWideBtn = new Flu_Button( 540, 43, 25, 25 );
  572. fileListWideBtn->type( FL_RADIO_BUTTON );
  573. fileListWideBtn->callback( _listModeCB, this );
  574. fileListWideBtn->image( file_listwide_img );
  575. fileListWideBtn->tooltip( wideListTTxt.c_str() );
  576. fileDetailsBtn = new Flu_Button( 569, 43, 25, 25 );
  577. fileDetailsBtn->type( FL_RADIO_BUTTON );
  578. fileDetailsBtn->image( fileDetails );
  579. fileDetailsBtn->callback( _listModeCB, this );
  580. fileDetailsBtn->tooltip( detailTTxt.c_str() );
  581. g2->end();
  582. }
  583. g->resizable( hiddenFiles );
  584. g->end();
  585. dummy->resizable( location );
  586. dummy->end();
  587. ////////////////////////////////////////////////////////////////
  588. previewTile = new PreviewTile( 110, 70, w()-110-5, h()-80-40-15, this );
  589. fileGroup = new Fl_Group( 110, 70, w()-120-5, h()-80-40-15 );
  590. {
  591. fileGroup->box( FL_DOWN_FRAME );
  592. filelist = new FileList( fileGroup->x()+2, fileGroup->y()+2, fileGroup->w()-4, fileGroup->h()-4, this );
  593. filelist->box( FL_FLAT_BOX );
  594. filelist->color( FL_WHITE );
  595. filelist->type( FL_HORIZONTAL );
  596. filelist->spacing( 4, 1 );
  597. filelist->scrollbar.linesize( DEFAULT_ENTRY_WIDTH+4 );
  598. filelist->end();
  599. fileDetailsGroup = new Fl_Group( fileGroup->x()+2, fileGroup->y()+2, fileGroup->w()-4, fileGroup->h()-4 );
  600. filecolumns = new FileColumns( fileGroup->x()+2, fileGroup->y()+2, fileGroup->w()-4, 20, this );
  601. filescroll = new Fl_Scroll( fileGroup->x()+2, fileGroup->y()+22, fileGroup->w()-4, fileGroup->h()-20-4 );
  602. filescroll->color( FL_WHITE );
  603. filescroll->scrollbar.linesize( 20 );
  604. filescroll->box( FL_FLAT_BOX );
  605. filescroll->type( Fl_Scroll::VERTICAL );
  606. {
  607. filedetails = new FileDetails( fileGroup->x()+2, fileGroup->y()+22, fileGroup->w()-4, fileGroup->h()-20-4, this );
  608. filedetails->end();
  609. }
  610. filescroll->end();
  611. fileDetailsGroup->end();
  612. fileDetailsGroup->resizable( filescroll );
  613. fileGroup->resizable( filelist );
  614. }
  615. fileGroup->end();
  616. previewGroup = new PreviewGroup( fileGroup->x()+fileGroup->w(), fileGroup->y(), previewTile->w()-fileGroup->w(), fileGroup->h(), this );
  617. previewGroup->end();
  618. {
  619. Fl_Box *b = new Fl_Box( previewTile->x()+250, previewTile->y(), previewTile->w()-350, previewTile->h() );
  620. previewTile->add_resizable( *b );
  621. }
  622. previewTile->end();
  623. previewTile->position( previewGroup->x(), previewGroup->y(), previewTile->x()+previewTile->w(), previewGroup->y() );
  624. previewTile->last = previewTile->x()+previewTile->w()-200;
  625. resizable( previewTile );
  626. filePattern = new Flu_Combo_List( 70, h()-30, w()-70-85-10, 25, fileTypesTxt.c_str() );
  627. filePattern->editable( false );
  628. filePattern->callback( reloadCB, this );
  629. filePattern->pop_height( 200 );
  630. ok.callback( _okCB, this );
  631. cancel.callback( _cancelCB, this );
  632. {
  633. g = new Fl_Group( 0, h()-60, w(), 30 );
  634. g->end();
  635. g->add( filename );
  636. g->add( ok );
  637. g->resizable( filename );
  638. g = new Fl_Group( 0, h()-30, w(), 30 );
  639. g->end();
  640. g->add( filePattern );
  641. g->add( cancel );
  642. g->resizable( filePattern );
  643. }
  644. end();
  645. FL_NORMAL_SIZE = oldNormalSize;
  646. char buf[1024];
  647. // try to load the favorites
  648. {
  649. FILE *f = fopen( configFilename.c_str(), "r" );
  650. if( f )
  651. {
  652. buf[0] = '\0';
  653. while( !feof(f) )
  654. {
  655. fgets( buf, 1024, f );
  656. char *newline = strrchr( buf, '\n' );
  657. if( newline )
  658. *newline = '\0';
  659. if( strlen( buf ) > 0 )
  660. {
  661. // eliminate duplicates
  662. bool duplicate = false;
  663. for( int i = 1; i <= favoritesList->size(); i++ )
  664. {
  665. if( streq( buf, favoritesList->text(i) ) )
  666. {
  667. duplicate = true;
  668. break;
  669. }
  670. }
  671. if( !duplicate )
  672. favoritesList->add( buf );
  673. }
  674. }
  675. fclose( f );
  676. }
  677. }
  678. if( !imgTxtPreview )
  679. {
  680. imgTxtPreview = new ImgTxtPreview();
  681. // make the text previewer the first one
  682. Flu_File_Chooser::previewHandlers.insert( 0, imgTxtPreview );
  683. }
  684. pattern( pat );
  685. default_file_icon( &default_file );
  686. cd( NULL ); // prime with the current directory
  687. clear_history();
  688. cd( pathname );
  689. // if pathname does not start with "/" or "~", set the filename to it
  690. if( pathname && pathname[0] != '/' && pathname[1] != ':' && pathname[0] != '~' )
  691. filename.value( pathname );
  692. }
  693. Flu_File_Chooser :: ~Flu_File_Chooser()
  694. {
  695. //Fl::remove_timeout( Entry::_editCB );
  696. Fl::remove_timeout( Flu_File_Chooser::delayedCdCB );
  697. Fl::remove_timeout( Flu_File_Chooser::selectCB );
  698. for( int i = 0; i < locationQuickJump->children(); i++ )
  699. free( (void*)locationQuickJump->child(i)->label() );
  700. filelist->clear();
  701. filedetails->clear();
  702. clear_history();
  703. }
  704. void Flu_File_Chooser :: hideCB()
  705. {
  706. // the user hid the browser by pushing the "X"
  707. // this is the same as cancel
  708. cancelCB();
  709. }
  710. void Flu_File_Chooser :: cancelCB()
  711. {
  712. filename.value("");
  713. filename.position( filename.size(), filename.size() );
  714. unselect_all();
  715. do_callback();
  716. hide();
  717. }
  718. void Flu_File_Chooser :: do_callback()
  719. {
  720. if( _callback )
  721. _callback( this, _userdata );
  722. }
  723. void Flu_File_Chooser :: pattern( const char *p )
  724. {
  725. // just like in Fl_File_Chooser, we accept tab, |, and ; delimited strings like this:
  726. // "Description (patterns)" or just "patterns" where patterns is
  727. // of the form *.xxx or *.{xxx,yyy,zzz}}
  728. rawPattern = p;
  729. // clear out the old
  730. filePattern->list.clear();
  731. filePattern->input.value( "" );
  732. patterns.clear();
  733. if( p == 0 )
  734. p = "*";
  735. else if( p[0] == '\0' )
  736. p = "*";
  737. FluSimpleString pat = p, pattern;
  738. bool addedAll = false;
  739. const char *next = strtok( (char*)pat.c_str(), "\t|;" );
  740. const char *start;
  741. while( next )
  742. {
  743. if( next[0] == '\0' )
  744. break;
  745. // eat whitespace
  746. while( isspace( *next ) )
  747. next++;
  748. // degenerate check
  749. if( strcmp( next, "*" ) == 0 )
  750. {
  751. addedAll = true;
  752. filePattern->list.add( allFilesTxt.c_str() );
  753. patterns.add( "*" );
  754. next = strtok( NULL, "\t|;" );
  755. continue;
  756. }
  757. // extract the patterns from the substring
  758. if( next[0] != '*' ) // starts with description
  759. {
  760. // the pattern starts after the first '('
  761. start = strchr( next, '(' );
  762. if( !start ) // error: couldn't find the '('
  763. {
  764. next = strtok( NULL, "\t|;" );
  765. continue;
  766. }
  767. start++; // skip the '('
  768. }
  769. else
  770. start = next;
  771. if( start[0] != '*' )
  772. {
  773. next = strtok( NULL, "\t|;" );
  774. continue;
  775. }
  776. start++; // skip the '*'
  777. if( start[0] != '.' )
  778. {
  779. next = strtok( NULL, "\t|;" );
  780. continue;
  781. }
  782. start++; // skip the '.'
  783. if( start[0] == '{' )
  784. {
  785. // the pattern is between '{' and '}'
  786. pattern = start+1;
  787. }
  788. else
  789. pattern = start;
  790. // remove the last '}'
  791. int brace = pattern.find( '}' );
  792. if( brace != -1 )
  793. pattern[brace] = '\0';
  794. // remove the last ')'
  795. int paren = pattern.find( ')' );
  796. if( paren != -1 )
  797. pattern[paren] = '\0';
  798. if( pattern.size() )
  799. {
  800. // add the whole string to the list
  801. filePattern->list.add( next );
  802. patterns.add( pattern );
  803. }
  804. // advance to the pattern token
  805. next = strtok( NULL, "\t|;" );
  806. }
  807. // add all files
  808. if( !addedAll )
  809. {
  810. filePattern->list.add( allFilesTxt.c_str() );
  811. patterns.add( "*" );
  812. }
  813. // choose the first added item
  814. filePattern->value( filePattern->list.text(1) );
  815. }
  816. int Flu_File_Chooser :: handle( int event )
  817. {
  818. if( Fl_Double_Window::callback() != _hideCB )
  819. {
  820. _callback = Fl_Double_Window::callback();
  821. _userdata = Fl_Double_Window::user_data();
  822. Fl_Double_Window::callback( _hideCB, this );
  823. }
  824. if( Fl_Double_Window::handle( event ) )
  825. return 1;
  826. else if( event == FL_KEYDOWN && Fl::event_key(FL_Escape) )
  827. {
  828. cancel.do_callback();
  829. return 1;
  830. }
  831. else if( event == FL_KEYDOWN && Fl::event_key('a') && Fl::event_state(FL_CTRL) )
  832. {
  833. select_all();
  834. return 1;
  835. }
  836. else
  837. return 0;
  838. }
  839. void Flu_File_Chooser :: newFolderCB()
  840. {
  841. // start with the name "New Folder". while the name exists, keep appending a number (1..2..etc)
  842. FluSimpleString newName = defaultFolderNameTxt.c_str(), path = currentDir + newName;
  843. int count = 1;
  844. int i;
  845. for(;;)
  846. {
  847. bool found = false;
  848. // see if any entry already has that name
  849. Fl_Group *g = getEntryGroup();
  850. for( i = 0; i < g->children(); i++ )
  851. {
  852. if( ((Entry*)g->child(i))->filename == newName )
  853. {
  854. found = true;
  855. break;
  856. }
  857. }
  858. // since an entry already exists, change the name and try again
  859. if( found )
  860. {
  861. char buf[16];
  862. sprintf( buf, "%d", count++ );
  863. newName = defaultFolderNameTxt.c_str() + FluSimpleString(buf);
  864. path = currentDir + newName;
  865. }
  866. else
  867. break;
  868. }
  869. // try to create the folder
  870. #if ( defined WIN32 || defined MINGW ) && !defined CYGWIN
  871. if( mkdir( path.c_str() ) != 0 )
  872. #else
  873. if( mkdir( path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH ) != 0 )
  874. #endif
  875. {
  876. fl_alert( createFolderErrTxt.c_str(), newName.c_str() );
  877. return;
  878. }
  879. // create a new entry with the name of the new folder. add to either the list or the details
  880. Entry *entry = new Entry( newName.c_str(), ENTRY_DIR, fileDetailsBtn->value(), this );
  881. if( !fileDetailsBtn->value() )
  882. filelist->add( *entry );
  883. else
  884. filedetails->add( *entry );
  885. // switch that entry to input mode and scroll the browser to it
  886. entry->editCB();
  887. /*
  888. entry->editMode = 2;
  889. entry->value( entry->filename.c_str() );
  890. entry->take_focus();
  891. entry->position( 0, entry->filename.size() );
  892. entry->redraw();
  893. */
  894. if( !fileDetailsBtn->value() )
  895. filelist->scroll_to( entry );
  896. else
  897. filedetails->scroll_to( entry );
  898. }
  899. void Flu_File_Chooser :: recursiveScan( const char *dir, FluStringVector *files )
  900. {
  901. dirent **e;
  902. char *name;
  903. FluSimpleString fullpath;
  904. int num = fl_filename_list( dir, &e );
  905. for( int i = 0; i < num; i++ )
  906. {
  907. name = e[i]->d_name;
  908. // if 'name' ends in '/' or '\', remove it
  909. if( name[strlen(name)-1] == '/' || name[strlen(name)-1] == '\\' )
  910. name[strlen(name)-1] = '\0';
  911. // ignore the "." and ".." names
  912. if( strcmp( name, "." ) == 0 || strcmp( name, ".." ) == 0 )
  913. continue;
  914. // file or directory?
  915. fullpath = dir;
  916. fullpath += "/";
  917. fullpath += name;
  918. if( fl_filename_isdir( fullpath.c_str() ) != 0 )
  919. recursiveScan( fullpath.c_str(), files );
  920. files->add( fullpath );
  921. }
  922. files->add( dir );
  923. }
  924. void Flu_File_Chooser :: trashCB( bool recycle )
  925. {
  926. // linux doesn't have a recycle bin
  927. #ifndef WIN32
  928. recycle = false;
  929. #endif
  930. bool inFavorites = ( currentDir == FAVORITES_UNIQUE_STRING );
  931. if( inFavorites )
  932. recycle = false;
  933. // see how many files are selected
  934. FluSimpleString name;
  935. int selected = 0;
  936. int i;
  937. const char *first = "";
  938. Fl_Group *g = getEntryGroup();
  939. for( i = 0; i < g->children(); i++ )
  940. {
  941. if( ((Entry*)g->child(i))->selected )
  942. {
  943. if( selected == 0 )
  944. first = ((Entry*)g->child(i))->filename.c_str();
  945. selected++;
  946. }
  947. }
  948. if( selected )
  949. {
  950. if( selected == 1 )
  951. {
  952. if( recycle )
  953. {
  954. if( !fl_ask( "Really send '%s' to the Recycle Bin?", first ) )
  955. return;
  956. }
  957. else
  958. {
  959. if( !fl_ask( "Really delete '%s'?", first ) )
  960. return;
  961. }
  962. }
  963. else
  964. {
  965. if( recycle )
  966. {
  967. if( !fl_ask( "Really send these %d files to the Recycle Bin?", selected ) )
  968. return;
  969. }
  970. else
  971. {
  972. if( !fl_ask( "Really delete these %d files?", selected ) )
  973. return;
  974. }
  975. }
  976. if( inFavorites )
  977. {
  978. for( i = 0; i < g->children(); )
  979. {
  980. Entry *e = ((Entry*)g->child(i));
  981. if( e->selected )
  982. {
  983. favoritesList->remove(i+1);
  984. g->remove( *e );
  985. delete e;
  986. }
  987. else
  988. i++;
  989. }
  990. // save the favorites
  991. FILE *f = fopen( configFilename.c_str(), "w" );
  992. if( f )
  993. {
  994. for( i = 1; i <= favoritesList->size(); i++ )
  995. fprintf( f, "%s\n", favoritesList->text(i) );
  996. fclose( f );
  997. }
  998. cd( FAVORITES_UNIQUE_STRING );
  999. return;
  1000. }
  1001. #ifdef WIN32
  1002. SHFILEOPSTRUCT fileop;
  1003. memset( &fileop, 0, sizeof(SHFILEOPSTRUCT) );
  1004. fileop.fFlags = FOF_SILENT | FOF_NOERRORUI | FOF_NOCONFIRMATION;
  1005. if( recycle )
  1006. fileop.fFlags |= FOF_ALLOWUNDO;
  1007. fileop.wFunc = FO_DELETE;
  1008. fileop.pTo = NULL;
  1009. #endif
  1010. for( i = 0; i < g->children(); i++ )
  1011. {
  1012. if( ((Entry*)g->child(i))->selected )
  1013. {
  1014. int result = 0;
  1015. name = currentDir + ((Entry*)g->child(i))->filename;
  1016. // if directory, recursively remove
  1017. if( ((Entry*)g->child(i))->type == ENTRY_DIR )
  1018. {
  1019. // if we are recycling in windows, then the recursive part happens automatically
  1020. #ifdef WIN32
  1021. if( !recycle )
  1022. #endif
  1023. {
  1024. Fl_Group::current(0);
  1025. Fl_Window *win = new Fl_Window( 200, 100, "Notice" );
  1026. Flu_Label *label = new Flu_Label( 30, 30, 150, 30, "Preparing to delete..." );
  1027. win->end();
  1028. win->show();
  1029. Fl::check();
  1030. // recursively build a list of all files that will be deleted
  1031. FluStringVector files;
  1032. recursiveScan( name.c_str(), &files );
  1033. // delete all the files
  1034. label->label( "Deleting files..." );
  1035. for( unsigned int i = 0; i < files.size(); i++ )
  1036. {
  1037. if( ::remove( files[i].c_str() ) != 0 )
  1038. {
  1039. win->hide();
  1040. delete win;
  1041. cd( "./" );
  1042. return;
  1043. }
  1044. }
  1045. win->hide();
  1046. delete win;
  1047. Fl::check();
  1048. continue;
  1049. }
  1050. }
  1051. #ifdef WIN32
  1052. // this moves files to the recycle bin, depending on the value of 'recycle'
  1053. {
  1054. int len = name.size();
  1055. char *buf = (char*)malloc( len+2 );
  1056. strcpy( buf, name.c_str() );
  1057. buf[len+1] = '\0'; // have to have 2 '\0' at the end
  1058. fileop.pFrom = buf;
  1059. result = SHFileOperation( &fileop );
  1060. free( buf );
  1061. }
  1062. #else
  1063. result = ::remove( name.c_str() );
  1064. #endif
  1065. // if remove fails, report an error
  1066. if( result != 0 )
  1067. {
  1068. fl_alert( deleteFileErrTxt.c_str(), name.c_str() );
  1069. cd( "./" );
  1070. return;
  1071. }
  1072. }
  1073. }
  1074. // refresh this directory
  1075. cd( "./" );
  1076. }
  1077. }
  1078. void Flu_File_Chooser :: updateLocationQJ()
  1079. {
  1080. const char *path = location->value();
  1081. for( int i = 0; i < locationQuickJump->children(); i++ )
  1082. free( (void*)locationQuickJump->child(i)->label() );
  1083. locationQuickJump->clear();
  1084. fl_font( location->input.textfont(), location->input.textsize() );
  1085. const char *next = path;
  1086. const char *slash = strchr( next, '/' );
  1087. char *blank = strdup( path );
  1088. int offset = 0;
  1089. while( slash )
  1090. {
  1091. memset( blank, 0, strlen(path) );
  1092. slash++;
  1093. memcpy( blank, next, slash-next );
  1094. int w = 0, h = 0;
  1095. fl_measure( blank, w, h );
  1096. if( blank[0] == '/' )
  1097. w += Fl::box_dx( location->box() );
  1098. memset( blank, 0, strlen(path) );
  1099. memcpy( blank, path, slash-path );
  1100. Fl_Button *b = new Fl_Button( locationQuickJump->x()+offset, locationQuickJump->y(), w, locationQuickJump->h(), strdup(blank) );
  1101. b->labeltype( FL_NO_LABEL );
  1102. b->callback( _locationQJCB, this );
  1103. b->tooltip( b->label() );
  1104. offset += w;
  1105. locationQuickJump->add( b );
  1106. next = slash;
  1107. slash = strchr( next, '/' );
  1108. }
  1109. Fl_Button *b = new Fl_Button( locationQuickJump->x()+offset, locationQuickJump->y(), 1, locationQuickJump->h(), strdup("") );
  1110. b->box( FL_NO_BOX );
  1111. b->labeltype( FL_NO_LABEL );
  1112. locationQuickJump->add( b );
  1113. locationQuickJump->resizable( b );
  1114. free( blank );
  1115. }
  1116. void Flu_File_Chooser :: favoritesCB()
  1117. {
  1118. cd( FAVORITES_UNIQUE_STRING );
  1119. }
  1120. void Flu_File_Chooser :: myComputerCB()
  1121. {
  1122. cd( "/" );
  1123. }
  1124. void Flu_File_Chooser :: documentsCB()
  1125. {
  1126. cd( userDocs.c_str() );
  1127. }
  1128. Flu_File_Chooser :: FileInput :: FileInput( int x, int y, int w, int h, const char *l, Flu_File_Chooser *c )
  1129. : Fl_Input( x, y, w, h, l )
  1130. {
  1131. chooser = c;
  1132. }
  1133. Flu_File_Chooser :: FileInput :: ~FileInput()
  1134. {
  1135. }
  1136. int Flu_File_Chooser :: FileInput :: handle( int event )
  1137. {
  1138. if( event == FL_KEYDOWN )
  1139. {
  1140. if( Fl::event_key(FL_Tab) )
  1141. {
  1142. chooser->filenameTabCallback = true;
  1143. FluSimpleString v(value());
  1144. #ifdef WIN32
  1145. // turn "C:" into "C:/"
  1146. if( v.size() >= 2 )
  1147. if( v[1] == ':' && v[2] == '\0' )
  1148. {
  1149. v += "/";
  1150. value( v.c_str() );
  1151. position( size(), size() );
  1152. }
  1153. #endif
  1154. chooser->delay_cd( v + "*" );
  1155. return 1;
  1156. }
  1157. else if( Fl::event_key(FL_Left) )
  1158. {
  1159. if( Fl_Input::position() == 0 )
  1160. return 1;
  1161. else
  1162. return Fl_Input::handle( event );
  1163. }
  1164. else if( Fl::event_key(FL_Right) )
  1165. {
  1166. if( Fl_Input::position() == (int)strlen(Fl_Input::value()) )
  1167. return 1;
  1168. else
  1169. return Fl_Input::handle( event );
  1170. }
  1171. else if( Fl::event_key(FL_Up) || Fl::event_key(FL_Down) )
  1172. {
  1173. chooser->getEntryContainer()->take_focus();
  1174. if( !chooser->lastSelected )
  1175. {
  1176. if( chooser->getEntryGroup()->children() )
  1177. {
  1178. Flu_File_Chooser::Entry *e = (Flu_File_Chooser::Entry*)chooser->getEntryGroup()->child(0);
  1179. e->selected = true;
  1180. chooser->lastSelected = e;
  1181. e->redraw();
  1182. }
  1183. }
  1184. return chooser->getEntryContainer()->handle( event );
  1185. }
  1186. }
  1187. return Fl_Input::handle( event );
  1188. }
  1189. Flu_File_Chooser :: PreviewTile :: PreviewTile( int x, int y, int w, int h, Flu_File_Chooser *c )
  1190. : Fl_Tile( x, y, w, h )
  1191. {
  1192. chooser = c;
  1193. }
  1194. int Flu_File_Chooser :: PreviewTile :: handle( int event )
  1195. {
  1196. // if we're not in preview mode, then the user isn't allowed to resize the tile
  1197. if( !chooser->previewBtn->value() )
  1198. return Fl_Group::handle( event );
  1199. if( event == FL_DRAG )
  1200. {
  1201. // the user is probably dragging to resize the columns
  1202. // update the sizes for each entry
  1203. chooser->updateEntrySizes();
  1204. chooser->redraw();
  1205. }
  1206. return Fl_Tile::handle(event);
  1207. }
  1208. Flu_File_Chooser :: PreviewWidgetBase :: PreviewWidgetBase()
  1209. : Fl_Group( 0, 0, 0, 0 )
  1210. {
  1211. }
  1212. Flu_File_Chooser :: PreviewWidgetBase :: ~PreviewWidgetBase()
  1213. {
  1214. }
  1215. Flu_File_Chooser :: PreviewGroup :: PreviewGroup( int x, int y, int w, int h, Flu_File_Chooser *c )
  1216. : Fl_Group( x, y, w, h )
  1217. {
  1218. box( FL_DOWN_BOX );
  1219. align( FL_ALIGN_CENTER | FL_ALIGN_CLIP );
  1220. labelsize( 60 );
  1221. labelfont( FL_HELVETICA );
  1222. chooser = c;
  1223. handled = 0;
  1224. lastFile = "";
  1225. }
  1226. void Flu_File_Chooser :: PreviewGroup :: draw()
  1227. {
  1228. if( !chooser->previewBtn->value() )
  1229. return;
  1230. if( file.size() == 0 )
  1231. return;
  1232. FILE *f = fopen( file.c_str(), "rb" );
  1233. if( !f )
  1234. {
  1235. label( "" );
  1236. Fl_Group::draw();
  1237. return;
  1238. }
  1239. fclose( f );
  1240. if( lastFile != file )
  1241. {
  1242. lastFile = file;
  1243. handled = 0;
  1244. PreviewWidgetBase *next;
  1245. for( int i = chooser->previewHandlers.size()-1; i >= 0; i-- )
  1246. {
  1247. next = chooser->previewHandlers[i];
  1248. next->hide();
  1249. if( !handled )
  1250. {
  1251. Fl_Group *p = next->parent();
  1252. Fl_Group::add( next );
  1253. if( next->preview( file.c_str() ) != 0 )
  1254. {
  1255. handled = next;
  1256. }
  1257. Fl_Group::remove( *next );
  1258. if( p )
  1259. p->add( next );
  1260. }
  1261. }
  1262. }
  1263. if( handled == 0 )
  1264. {
  1265. label( "?" );
  1266. Fl_Group::draw();
  1267. }
  1268. else
  1269. {
  1270. label( "" );
  1271. Fl_Group *p = handled->parent();
  1272. handled->show();
  1273. Fl_Group::add( handled );
  1274. handled->resize( x()+Fl::box_dx(box()), y()+Fl::box_dy(box()),
  1275. w()-Fl::box_dw(box()), h()-Fl::box_dh(box()) );
  1276. Fl_Group::draw();
  1277. Fl_Group::remove( *handled );
  1278. handled->hide();
  1279. if( p )
  1280. p->add( handled );
  1281. }
  1282. }
  1283. // adapted from Fl_File_Chooser2.cxx : update_preview()
  1284. int Flu_File_Chooser :: ImgTxtPreview :: preview( const char *filename )
  1285. {
  1286. Fl_Shared_Image *img, // New image
  1287. *oldimg; // Old image
  1288. int pbw, pbh; // Width and height of preview box
  1289. int w, h; // Width and height of preview image
  1290. window()->cursor( FL_CURSOR_WAIT );
  1291. Fl::check();
  1292. img = Fl_Shared_Image::get( filename );
  1293. if( img )
  1294. {
  1295. window()->cursor( FL_CURSOR_DEFAULT );
  1296. Fl::check();
  1297. }
  1298. oldimg = (Fl_Shared_Image*)image();
  1299. if( oldimg )
  1300. oldimg->release();
  1301. image(0);
  1302. if( !img )
  1303. {
  1304. // Try reading the first 1k of data for a label...
  1305. FILE *f = fopen( filename, "rb" );
  1306. if( f )
  1307. {
  1308. int bytes = fread( previewTxt, 1, sizeof(previewTxt) - 1, f );
  1309. previewTxt[bytes] = '\0';
  1310. fclose( f );
  1311. }
  1312. else
  1313. return 0;
  1314. window()->cursor( FL_CURSOR_DEFAULT );
  1315. Fl::check();
  1316. // Scan the buffer for printable chars...
  1317. unsigned char *ptr;
  1318. for( ptr = previewTxt; *ptr && (isprint(*ptr) || isspace(*ptr)); ptr++ ) {}
  1319. if( *ptr || ptr == previewTxt )
  1320. {
  1321. // Non-printable file - can't handle
  1322. return 0;
  1323. }
  1324. else
  1325. {
  1326. // Show the first 1k of text...
  1327. label( (const char*)previewTxt );
  1328. align((Fl_Align)(FL_ALIGN_CLIP | FL_ALIGN_INSIDE | FL_ALIGN_LEFT | FL_ALIGN_TOP));
  1329. labelsize( 12 );
  1330. labelfont( FL_COURIER );
  1331. }
  1332. }
  1333. else if( img->w() > 0 && img->h() > 0 )
  1334. {
  1335. pbw = this->w() - 20;
  1336. pbh = this->h() - 20;
  1337. pbw = (pbw < 10) ? 10 : pbw;
  1338. pbh = (pbh < 10) ? 10 : pbh;
  1339. if( img->w() > pbw || img->h() > pbh )
  1340. {
  1341. w = pbw;
  1342. h = int(float(w*img->h()) / float(img->w()));
  1343. if( h > pbh )
  1344. {
  1345. h = pbh;
  1346. w = int(float(h*img->w()) / float(img->h()));
  1347. }
  1348. oldimg = (Fl_Shared_Image *)img->copy(w, h);
  1349. image((Fl_Image *)oldimg);
  1350. img->release();
  1351. }
  1352. else
  1353. image((Fl_Image *)img);
  1354. align( FL_ALIGN_CLIP );
  1355. label(0);
  1356. }
  1357. redraw();
  1358. return 1;
  1359. }
  1360. void Flu_File_Chooser :: previewCB()
  1361. {
  1362. if( previewBtn->value() )
  1363. {
  1364. fileGroup->resize( fileGroup->x(), fileGroup->y(), previewTile->last-fileGroup->x(), fileGroup->h() );
  1365. previewGroup->resize( previewTile->last, previewGroup->y(), previewTile->w()-fileGroup->w(), previewGroup->h() );
  1366. previewGroup->show();
  1367. }
  1368. else
  1369. {
  1370. previewTile->last = previewGroup->x();
  1371. fileGroup->resize( fileGroup->x(), fileGroup->y(), previewTile->w(), fileGroup->h() );
  1372. previewGroup->resize( previewTile->x()+previewTile->w(), previewGroup->y(), 0, previewGroup->h() );
  1373. previewGroup->hide();
  1374. }
  1375. previewGroup->redraw();
  1376. previewTile->init_sizes();
  1377. fileDetailsGroup->parent()->init_sizes();
  1378. updateEntrySizes();
  1379. redraw();
  1380. if( previewBtn->value() )
  1381. {
  1382. Fl::check();
  1383. previewGroup->redraw();
  1384. }
  1385. }
  1386. void Flu_File_Chooser :: sortCB( Fl_Widget *w )
  1387. {
  1388. // if the sort method is already selected, toggle the REVERSE bit
  1389. if( w == detailNameBtn )
  1390. {
  1391. if( sortMethod & SORT_NAME )
  1392. sortMethod ^= SORT_REVERSE;
  1393. else
  1394. sortMethod = SORT_NAME;
  1395. }
  1396. else if( w == detailSizeBtn )
  1397. {
  1398. if( sortMethod & SORT_SIZE )
  1399. sortMethod ^= SORT_REVERSE;
  1400. else
  1401. sortMethod = SORT_SIZE;
  1402. }
  1403. else if( w == detailDateBtn )
  1404. {
  1405. if( sortMethod & SORT_DATE )
  1406. sortMethod ^= SORT_REVERSE;
  1407. else
  1408. sortMethod = SORT_DATE;
  1409. }
  1410. else if( w == detailTypeBtn )
  1411. {
  1412. if( sortMethod & SORT_TYPE )
  1413. sortMethod ^= SORT_REVERSE;
  1414. else
  1415. sortMethod = SORT_TYPE;
  1416. }
  1417. bool reverse = ( sortMethod & SORT_REVERSE );
  1418. detailNameBtn->label( detailTxt[0].c_str() );
  1419. detailSizeBtn->label( detailTxt[1].c_str() );
  1420. detailDateBtn->label( detailTxt[2].c_str() );
  1421. detailTypeBtn->label( detailTxt[3].c_str() );
  1422. switch( sortMethod & ~SORT_REVERSE )
  1423. {
  1424. case SORT_NAME: detailNameBtn->label( reverse ? dArrow[0].c_str() : uArrow[0].c_str() ); break;
  1425. case SORT_SIZE: detailSizeBtn->label( reverse ? dArrow[1].c_str() : uArrow[1].c_str() ); break;
  1426. case SORT_DATE: detailDateBtn->label( reverse ? dArrow[2].c_str() : uArrow[2].c_str() ); break;
  1427. case SORT_TYPE: detailTypeBtn->label( reverse ? dArrow[3].c_str() : uArrow[3].c_str() ); break;
  1428. }
  1429. filelist->sort();
  1430. filedetails->sort();
  1431. }
  1432. Flu_File_Chooser :: CBTile :: CBTile( int x, int y, int w, int h, Flu_File_Chooser *c )
  1433. : Fl_Tile( x, y, w, h )
  1434. {
  1435. chooser = c;
  1436. }
  1437. int Flu_File_Chooser :: CBTile :: handle( int event )
  1438. {
  1439. if( event == FL_DRAG )
  1440. {
  1441. // the user is probably dragging to resize the columns
  1442. // update the sizes for each entry
  1443. chooser->updateEntrySizes();
  1444. chooser->redraw();
  1445. }
  1446. return Fl_Tile::handle(event);
  1447. }
  1448. Flu_File_Chooser :: FileColumns :: FileColumns( int x, int y, int w, int h, Flu_File_Chooser *c )
  1449. : Fl_Tile( x, y, w, h )
  1450. {
  1451. chooser = c;
  1452. W1 = int(float(w)*0.35f);
  1453. W2 = int(float(w)*0.20f);
  1454. W3 = int(float(w)*0.15f);
  1455. W4 = w-W1-W2-W3;
  1456. Fl_Box *box = new Fl_Box( x+50, y, w-200, h );
  1457. add_resizable( *box );
  1458. c->detailNameBtn = new Flu_Button( x, y, W1, h, detailTxt[0].c_str() );
  1459. c->detailNameBtn->align( FL_ALIGN_CLIP );
  1460. c->detailNameBtn->callback( Flu_File_Chooser::_sortCB, c );
  1461. {
  1462. CBTile *tile = new CBTile( x+W1, y, W2+W3+W4, h, c );
  1463. Fl_Box *box = new Fl_Box( tile->x()+50, tile->y(), tile->w()-150, tile->h() );
  1464. tile->add_resizable( *box );
  1465. c->detailTypeBtn = new Flu_Button( x+W1, y, W2, h, detailTxt[3].c_str() );
  1466. c->detailTypeBtn->align( FL_ALIGN_CLIP );
  1467. c->detailTypeBtn->callback( Flu_File_Chooser::_sortCB, c );
  1468. {
  1469. CBTile *tile = new CBTile( x+W1+W2, y, W3+W4, h, c );
  1470. Fl_Box *box = new Fl_Box( tile->x()+50, tile->y(), tile->w()-100, tile->h() );
  1471. tile->add_resizable( *box );
  1472. c->detailSizeBtn = new Flu_Button( x+W1+W2, y, W3, h, detailTxt[1].c_str() );
  1473. c->detailSizeBtn->align( FL_ALIGN_CLIP );
  1474. c->detailSizeBtn->callback( Flu_File_Chooser::_sortCB, c );
  1475. c->detailDateBtn = new Flu_Button( x+W1+W2+W3, y, W4, h, detailTxt[2].c_str() );
  1476. c->detailDateBtn->align( FL_ALIGN_CLIP );
  1477. c->detailDateBtn->callback( Flu_File_Chooser::_sortCB, c );
  1478. tile->end();
  1479. }
  1480. tile->end();
  1481. }
  1482. end();
  1483. }
  1484. Flu_File_Chooser :: FileColumns :: ~FileColumns()
  1485. {
  1486. }
  1487. void Flu_File_Chooser :: FileColumns :: resize( int x, int y, int w, int h )
  1488. {
  1489. // TODO resize the buttons/tiles according to their stored relative sizes
  1490. Fl_Tile::resize( x, y, w, h );
  1491. }
  1492. int Flu_File_Chooser :: FileColumns :: handle( int event )
  1493. {
  1494. if( event == FL_DRAG )
  1495. {
  1496. // the user is probably dragging to resize the columns
  1497. // update the sizes for each entry
  1498. chooser->updateEntrySizes();
  1499. chooser->redraw();
  1500. }
  1501. return Fl_Tile::handle(event);
  1502. }
  1503. void Flu_File_Chooser :: filenameCB()
  1504. {
  1505. filenameEnterCallback = true;
  1506. //cd( filename.value() );
  1507. okCB();
  1508. }
  1509. inline bool _isProbablyAPattern( const char *s )
  1510. {
  1511. return( strpbrk( s, "*;|[]?" ) != NULL );
  1512. }
  1513. void Flu_File_Chooser :: okCB()
  1514. {
  1515. // if exactly one directory is selected and we are not choosing directories,
  1516. // cd to that directory.
  1517. if( !( selectionType & DIRECTORY ) && !( selectionType & STDFILE ) )
  1518. {
  1519. Fl_Group *g = getEntryGroup();
  1520. FluSimpleString dir;
  1521. int count = 0;
  1522. for( int i = 0; i < g->children(); i++ )
  1523. {
  1524. if( ((Flu_File_Chooser::Entry*)g->child(i))->selected )
  1525. {
  1526. count++;
  1527. dir = ((Flu_File_Chooser::Entry*)g->child(i))->filename;
  1528. }
  1529. }
  1530. if( count == 1 )
  1531. {
  1532. FluSimpleString path = currentDir + dir;
  1533. if( fl_filename_isdir( path.c_str() ) )
  1534. {
  1535. cd( dir.c_str() );
  1536. return;
  1537. }
  1538. }
  1539. }
  1540. // only hide if the filename is not blank or the user is choosing directories,
  1541. // in which case use the current directory
  1542. if( selectionType & DIRECTORY ||
  1543. ( (selectionType & STDFILE) && fl_filename_isdir( (currentDir+filename.value()).c_str() ) )
  1544. )
  1545. {
  1546. #ifdef WIN32
  1547. if( myComputerTxt == filename.value() )
  1548. {
  1549. myComputerCB();
  1550. return;
  1551. }
  1552. #endif
  1553. if( !(selectionType & MULTI ) )
  1554. {
  1555. if( strlen( filename.value() ) != 0 )
  1556. cd( filename.value() );
  1557. filename.value( currentDir.c_str() );
  1558. filename.position( filename.size(), filename.size() );
  1559. }
  1560. do_callback();
  1561. hide();
  1562. }
  1563. else
  1564. {
  1565. if( strlen( filename.value() ) != 0 )
  1566. {
  1567. if( _isProbablyAPattern( filename.value() ) )
  1568. {
  1569. cd( filename.value() );
  1570. return;
  1571. }
  1572. #ifdef WIN32
  1573. if( filename.value()[1] == ':' )
  1574. #else
  1575. if( filename.value()[0] == '/' )
  1576. #endif
  1577. if( fl_filename_isdir( filename.value() ) )
  1578. {
  1579. filename.value( "" );
  1580. return;
  1581. }
  1582. // prepend the path
  1583. FluSimpleString path = currentDir + filename.value();
  1584. filename.value( path.c_str() );
  1585. filename.position( filename.size(), filename.size() );
  1586. do_callback();
  1587. hide();
  1588. }
  1589. }
  1590. }
  1591. void Flu_File_Chooser :: homeCB()
  1592. {
  1593. #ifdef WIN32
  1594. cd( "/" );
  1595. #else
  1596. cd( userHome.c_str() );
  1597. #endif
  1598. }
  1599. void Flu_File_Chooser :: desktopCB()
  1600. {
  1601. cd( userDesktop.c_str() );
  1602. }
  1603. #define QSCANL( field ) \
  1604. while( ((Flu_File_Chooser::Entry*)array[left])->field < \
  1605. ((Flu_File_Chooser::Entry*)array[pivot])->field ) left++
  1606. #define QSCANR( field ) \
  1607. while( ((Flu_File_Chooser::Entry*)array[right])->field > \
  1608. ((Flu_File_Chooser::Entry*)array[pivot])->field ) right--
  1609. #define RQSCANL( field ) \
  1610. while( ((Flu_File_Chooser::Entry*)array[left])->field > \
  1611. ((Flu_File_Chooser::Entry*)array[pivot])->field ) left++
  1612. #define RQSCANR( field ) \
  1613. while( ((Flu_File_Chooser::Entry*)array[right])->field < \
  1614. ((Flu_File_Chooser::Entry*)array[pivot])->field ) right--
  1615. #define CASE_QSCANL( field ) \
  1616. while( casecompare( ((Flu_File_Chooser::Entry*)array[left])->field, \
  1617. ((Flu_File_Chooser::Entry*)array[pivot])->field ) < 0 ) left++
  1618. #define CASE_QSCANR( field ) \
  1619. while( casecompare( ((Flu_File_Chooser::Entry*)array[right])->field, \
  1620. ((Flu_File_Chooser::Entry*)array[pivot])->field ) > 0 ) right--
  1621. #define CASE_RQSCANL( field ) \
  1622. while( casecompare( ((Flu_File_Chooser::Entry*)array[left])->field, \
  1623. ((Flu_File_Chooser::Entry*)array[pivot])->field ) > 0 ) left++
  1624. #define CASE_RQSCANR( field ) \
  1625. while( casecompare( ((Flu_File_Chooser::Entry*)array[right])->field, \
  1626. ((Flu_File_Chooser::Entry*)array[pivot])->field ) < 0 ) right--
  1627. #define CUSTOM_QSCANL( field ) \
  1628. while( customSort( ((Flu_File_Chooser::Entry*)array[left])->field, \
  1629. ((Flu_File_Chooser::Entry*)array[pivot])->field ) < 0 ) left++
  1630. #define CUSTOM_QSCANR( field ) \
  1631. while( customSort( ((Flu_File_Chooser::Entry*)array[right])->field, \
  1632. ((Flu_File_Chooser::Entry*)array[pivot])->field ) > 0 ) right--
  1633. #define CUSTOM_RQSCANL( field ) \
  1634. while( customSort( ((Flu_File_Chooser::Entry*)array[left])->field, \
  1635. ((Flu_File_Chooser::Entry*)array[pivot])->field ) > 0 ) left++
  1636. #define CUSTOM_RQSCANR( field ) \
  1637. while( customSort( ((Flu_File_Chooser::Entry*)array[right])->field, \
  1638. ((Flu_File_Chooser::Entry*)array[pivot])->field ) < 0 ) right--
  1639. void Flu_File_Chooser :: _qSort( int how, bool caseSort, Fl_Widget **array, int low, int high )
  1640. {
  1641. int left, right, pivot;
  1642. Fl_Widget *temp;
  1643. bool reverse = ( how & SORT_REVERSE );
  1644. if( high > low )
  1645. {
  1646. left = low;
  1647. right = high;
  1648. pivot = low;
  1649. while( right >= left )
  1650. {
  1651. switch( how & ~SORT_REVERSE )
  1652. {
  1653. case SORT_NAME:
  1654. if( reverse )
  1655. {
  1656. if( customSort )
  1657. {
  1658. CUSTOM_RQSCANL( filename.c_str() );
  1659. CUSTOM_RQSCANR( filename.c_str() );
  1660. }
  1661. else if( !caseSort )
  1662. {
  1663. CASE_RQSCANL( filename );
  1664. CASE_RQSCANR( filename );
  1665. }
  1666. else
  1667. {
  1668. RQSCANL( filename );
  1669. RQSCANR( filename );
  1670. }
  1671. }
  1672. else
  1673. {
  1674. if( customSort )
  1675. {
  1676. CUSTOM_QSCANL( filename.c_str() );
  1677. CUSTOM_QSCANR( filename.c_str() );
  1678. }
  1679. else if( !caseSort )
  1680. {
  1681. CASE_QSCANL( filename );
  1682. CASE_QSCANR( filename );
  1683. }
  1684. else
  1685. {
  1686. QSCANL( filename );
  1687. QSCANR( filename );
  1688. }
  1689. }
  1690. break;
  1691. case SORT_SIZE:
  1692. if( reverse )
  1693. {
  1694. RQSCANL( isize );
  1695. RQSCANR( isize );
  1696. }
  1697. else
  1698. {
  1699. QSCANL( isize );
  1700. QSCANR( isize );
  1701. }
  1702. break;
  1703. case SORT_DATE:
  1704. if( reverse )
  1705. {
  1706. RQSCANL( idate );
  1707. RQSCANR( idate );
  1708. }
  1709. else
  1710. {
  1711. QSCANL( idate );
  1712. QSCANR( idate );
  1713. }
  1714. break;
  1715. case SORT_TYPE:
  1716. if( reverse )
  1717. {
  1718. RQSCANL( description );
  1719. RQSCANR( description );
  1720. }
  1721. else
  1722. {
  1723. QSCANL( description );
  1724. QSCANR( description );
  1725. }
  1726. break;
  1727. }
  1728. if( left > right )
  1729. break;
  1730. temp = array[left];
  1731. array[left] = array[right];
  1732. array[right] = temp;
  1733. left++;
  1734. right--;
  1735. }
  1736. _qSort( how, caseSort, array, low, right );
  1737. _qSort( how, caseSort, array, left, high );
  1738. }
  1739. }
  1740. Flu_File_Chooser :: FileList :: FileList( int x, int y, int w, int h, Flu_File_Chooser *c )
  1741. : Flu_Wrap_Group( x, y, w, h )
  1742. {
  1743. chooser = c;
  1744. numDirs = 0;
  1745. }
  1746. Flu_File_Chooser :: FileList :: ~FileList()
  1747. {
  1748. }
  1749. void Flu_File_Chooser :: FileList :: sort( int n )
  1750. {
  1751. if( n != -1 )
  1752. numDirs = n;
  1753. if( children() == 0 )
  1754. return;
  1755. // the directories are already first. sort the directories then the names lexigraphically
  1756. Flu_File_Chooser::_qSort( chooser->sortMethod, chooser->caseSort, (Fl_Widget**)array(), 0, numDirs-1 );
  1757. Flu_File_Chooser::_qSort( chooser->sortMethod, chooser->caseSort, (Fl_Widget**)array(), numDirs, children()-1 );
  1758. chooser->redraw();
  1759. }
  1760. int Flu_File_Chooser :: FileList :: handle( int event )
  1761. {
  1762. if( event == FL_FOCUS || event == FL_UNFOCUS )
  1763. return 1;
  1764. if( Flu_Wrap_Group::handle( event ) )
  1765. return 1;
  1766. // if push on no file, unselect all files and turn off editing mode
  1767. if( event == FL_PUSH && !Fl::event_key( FL_SHIFT ) && !Fl::event_key( FL_CTRL ) )
  1768. {
  1769. chooser->unselect_all();
  1770. chooser->filename.value( "" );
  1771. chooser->filename.position( chooser->filename.size(), chooser->filename.size() );
  1772. if( Fl::event_button3() )
  1773. return chooser->popupContextMenu( NULL );
  1774. return 1;
  1775. }
  1776. else if( event == FL_KEYDOWN )
  1777. {
  1778. if( Fl::event_key( FL_Delete ) )
  1779. {
  1780. // recycle by default, unless the shift key is held down
  1781. chooser->trashCB( !Fl::event_state( FL_SHIFT ) );
  1782. return 1;
  1783. }
  1784. Flu_File_Chooser::Entry *e = chooser->lastSelected;
  1785. if( !e )
  1786. {
  1787. for( int i = 0; i < children(); i++ )
  1788. if( ((Flu_File_Chooser::Entry*)child(i))->selected )
  1789. {
  1790. e = (Flu_File_Chooser::Entry*)child(i);
  1791. break;
  1792. }
  1793. }
  1794. if( e )
  1795. {
  1796. switch( Fl::event_key() )
  1797. {
  1798. case FL_Up: e = (Flu_File_Chooser::Entry*)previous( e );
  1799. if( !e && children() ) e = (Flu_File_Chooser::Entry*)child(0); break;
  1800. case FL_Down: e = (Flu_File_Chooser::Entry*)next( e );
  1801. if( !e && children() ) e = (Flu_File_Chooser::Entry*)child(children()-1); break;
  1802. case FL_Left: e = (Flu_File_Chooser::Entry*)left( e ); break;
  1803. case FL_Right: e = (Flu_File_Chooser::Entry*)right( e ); break;
  1804. case FL_Home: if( children() ) e = (Flu_File_Chooser::Entry*)child(0); break;
  1805. case FL_End: if( children() ) e = (Flu_File_Chooser::Entry*)child(children()-1); break;
  1806. case FL_Enter:
  1807. chooser->filenameEnterCallback = true;
  1808. //chooser->cd( e->filename.c_str() );
  1809. chooser->okCB();
  1810. return 1;
  1811. case ' ':
  1812. chooser->cd( e->filename.c_str() );
  1813. return 1;
  1814. default: e = 0; break;
  1815. }
  1816. if( e )
  1817. {
  1818. chooser->unselect_all();
  1819. e->selected = true;
  1820. chooser->lastSelected = e;
  1821. chooser->filename.value( e->filename.c_str() );
  1822. chooser->filename.position( chooser->filename.size(), chooser->filename.size() );
  1823. chooser->redraw();
  1824. if( e->type == ENTRY_FILE )
  1825. chooser->previewGroup->file = chooser->currentDir + e->filename;
  1826. scroll_to( e );
  1827. return 1;
  1828. }
  1829. }
  1830. }
  1831. return 0;
  1832. }
  1833. Flu_File_Chooser :: FileDetails :: FileDetails( int x, int y, int w, int h, Flu_File_Chooser *c )
  1834. : Fl_Pack( x, y, w, h )
  1835. {
  1836. chooser = c;
  1837. numDirs = 0;
  1838. }
  1839. Flu_File_Chooser :: FileDetails :: ~FileDetails()
  1840. {
  1841. }
  1842. void Flu_File_Chooser :: FileDetails :: scroll_to( Fl_Widget *w )
  1843. {
  1844. // we know all the widgets are the same height
  1845. // so just find this widget and scroll to the accumulated height
  1846. int H = 0;
  1847. for( int i = 0; i < children(); i++ )
  1848. {
  1849. if( child(i) == w )
  1850. {
  1851. if( H > (int)chooser->filescroll->scrollbar.maximum() )
  1852. H = (int)chooser->filescroll->scrollbar.maximum();
  1853. chooser->filescroll->position( 0, H );
  1854. return;
  1855. }
  1856. H += w->h();
  1857. }
  1858. }
  1859. void Flu_File_Chooser :: FileDetails :: sort( int n )
  1860. {
  1861. if( n != -1 )
  1862. numDirs = n;
  1863. if( children() == 0 )
  1864. return;
  1865. // the directories are already first. sort the directories then the names lexigraphically
  1866. Flu_File_Chooser::_qSort( chooser->sortMethod, chooser->caseSort, (Fl_Widget**)array(), 0, numDirs-1 );
  1867. Flu_File_Chooser::_qSort( chooser->sortMethod, chooser->caseSort, (Fl_Widget**)array(), numDirs, children()-1 );
  1868. chooser->redraw();
  1869. }
  1870. Fl_Widget* Flu_File_Chooser :: FileDetails :: next( Fl_Widget* w )
  1871. {
  1872. for( int i = 0; i < children()-1; i++ )
  1873. {
  1874. if( w == child(i) )
  1875. return child(i+1);
  1876. }
  1877. return NULL;
  1878. }
  1879. Fl_Widget* Flu_File_Chooser :: FileDetails :: previous( Fl_Widget* w )
  1880. {
  1881. for( int i = 1; i < children(); i++ )
  1882. {
  1883. if( w == child(i) )
  1884. return child(i-1);
  1885. }
  1886. return NULL;
  1887. }
  1888. int Flu_File_Chooser :: FileDetails :: handle( int event )
  1889. {
  1890. if( event == FL_FOCUS || event == FL_UNFOCUS )
  1891. return 1;
  1892. if( Fl_Pack::handle( event ) )
  1893. return 1;
  1894. else if( event == FL_PUSH )
  1895. return 1;
  1896. else if( event == FL_KEYDOWN )
  1897. {
  1898. if( Fl::event_key( FL_Delete ) )
  1899. {
  1900. // recycle by default, unless the shift key is held down
  1901. chooser->trashCB( !Fl::event_state( FL_SHIFT ) );
  1902. return 1;
  1903. }
  1904. Flu_File_Chooser::Entry *e = chooser->lastSelected;
  1905. if( !e )
  1906. {
  1907. for( int i = 0; i < children(); i++ )
  1908. if( ((Flu_File_Chooser::Entry*)child(i))->selected )
  1909. {
  1910. e = (Flu_File_Chooser::Entry*)child(i);
  1911. break;
  1912. }
  1913. }
  1914. if( e )
  1915. {
  1916. switch( Fl::event_key() )
  1917. {
  1918. case FL_Up: e = (Flu_File_Chooser::Entry*)previous( e );
  1919. if( !e && children() ) e = (Flu_File_Chooser::Entry*)child(0); break;
  1920. case FL_Down: e = (Flu_File_Chooser::Entry*)next( e );
  1921. if( !e && children() ) e = (Flu_File_Chooser::Entry*)child(children()-1); break;
  1922. case FL_Home: if( children() ) e = (Flu_File_Chooser::Entry*)child(0); break;
  1923. case FL_End: if( children() ) e = (Flu_File_Chooser::Entry*)child(children()-1); break;
  1924. case FL_Enter:
  1925. chooser->filenameEnterCallback = true;
  1926. //chooser->cd( e->filename.c_str() );
  1927. chooser->okCB();
  1928. return 1;
  1929. case ' ':
  1930. chooser->cd( e->filename.c_str() );
  1931. return 1;
  1932. default: e = 0; break;
  1933. }
  1934. if( e )
  1935. {
  1936. chooser->unselect_all();
  1937. e->selected = true;
  1938. chooser->lastSelected = e;
  1939. chooser->filename.value( e->filename.c_str() );
  1940. chooser->filename.position( chooser->filename.size(), chooser->filename.size() );
  1941. chooser->redraw();
  1942. scroll_to( e );
  1943. return 1;
  1944. }
  1945. }
  1946. }
  1947. return 0;
  1948. }
  1949. Flu_File_Chooser :: Entry :: Entry( const char* name, int t, bool d, Flu_File_Chooser *c )
  1950. : Fl_Input( 0, 0, 0, 0 )
  1951. {
  1952. resize( 0, 0, DEFAULT_ENTRY_WIDTH, 20 );
  1953. textsize( 12 );
  1954. box( FL_BORDER_BOX );
  1955. when( FL_WHEN_RELEASE_ALWAYS | FL_WHEN_ENTER_KEY_ALWAYS );
  1956. callback( _inputCB, this );
  1957. filename = name;
  1958. selected = false;
  1959. chooser = c;
  1960. details = d;
  1961. type = t;
  1962. icon = NULL;
  1963. editMode = 0;
  1964. description = "";
  1965. if( type == ENTRY_FILE && (c->selectionType & DEACTIVATE_FILES) )
  1966. {
  1967. textcolor( FL_GRAY );
  1968. deactivate();
  1969. }
  1970. updateSize();
  1971. updateIcon();
  1972. }
  1973. void Flu_File_Chooser :: Entry :: updateIcon()
  1974. {
  1975. Flu_File_Chooser::FileTypeInfo *tt = NULL;
  1976. if( type==ENTRY_MYCOMPUTER )
  1977. {
  1978. icon = &computer;
  1979. description = myComputerTxt;
  1980. }
  1981. else if( type==ENTRY_MYDOCUMENTS )
  1982. {
  1983. icon = &documents;
  1984. description = myDocumentsTxt;
  1985. }
  1986. else if( type==ENTRY_DRIVE )
  1987. {
  1988. //icon = &disk_drive;
  1989. //description = "";
  1990. }
  1991. else if( type==ENTRY_DIR || type==ENTRY_FAVORITE )
  1992. tt = Flu_File_Chooser::find_type( NULL );
  1993. else
  1994. {
  1995. const char *dot = strrchr( filename.c_str(), '.' );
  1996. if( dot )
  1997. {
  1998. tt = Flu_File_Chooser::find_type( dot+1 );
  1999. if( !tt )
  2000. description = dot+1;
  2001. }
  2002. }
  2003. if( tt )
  2004. {
  2005. icon = tt->icon;
  2006. description = tt->type;
  2007. }
  2008. // if there is no icon, assign a default one
  2009. if( !icon && type==ENTRY_FILE && !(chooser->selectionType & DEACTIVATE_FILES) )
  2010. icon = chooser->defaultFileIcon;
  2011. if( type==ENTRY_FAVORITE )
  2012. icon = &little_favorites;
  2013. toolTip = detailTxt[0] + ": " + filename;
  2014. if( type == ENTRY_FILE )
  2015. toolTip += "\n" + detailTxt[1] +": " + filesize;
  2016. toolTip += "\n" + detailTxt[3] + ": " + description;
  2017. tooltip( toolTip.c_str() );
  2018. redraw();
  2019. }
  2020. void Flu_File_Chooser :: resize( int x, int y, int w, int h )
  2021. {
  2022. Fl_Double_Window::resize( x, y, w, h );
  2023. if( fileListWideBtn->value() )
  2024. filelist->scrollbar.linesize( filelist->w() );
  2025. else if( fileListBtn->value() )
  2026. filelist->scrollbar.linesize( DEFAULT_ENTRY_WIDTH+4 );
  2027. // round position to nearest multiple of the linesize
  2028. ((Fl_Valuator*)&(filelist->scrollbar))->value( filelist->w()*(filelist->scrollbar.value()/filelist->w()) );
  2029. for( int i = 0; i < filelist->children(); i++ )
  2030. ((Entry*)filelist->child(i))->updateSize();
  2031. }
  2032. void Flu_File_Chooser :: listModeCB()
  2033. {
  2034. bool listMode = !fileDetailsBtn->value() || ( currentDir == FAVORITES_UNIQUE_STRING );
  2035. if( listMode )
  2036. {
  2037. while( filedetails->children() )
  2038. filelist->add( filedetails->child(0) );
  2039. }
  2040. else
  2041. {
  2042. while( filelist->children() )
  2043. filedetails->add( filelist->child(0) );
  2044. }
  2045. resize( x(), y(), w(), h() );
  2046. updateEntrySizes();
  2047. if( listMode )
  2048. {
  2049. fileDetailsGroup->hide();
  2050. filelist->show();
  2051. filelist->redraw();
  2052. filelist->parent()->resizable( filelist );
  2053. }
  2054. else
  2055. {
  2056. filelist->hide();
  2057. fileDetailsGroup->show();
  2058. fileDetailsGroup->parent()->resizable( fileDetailsGroup );
  2059. }
  2060. //redraw();
  2061. }
  2062. void Flu_File_Chooser :: Entry :: updateSize()
  2063. {
  2064. if( type==ENTRY_FAVORITE || chooser->fileListWideBtn->value() )
  2065. {
  2066. resize( x(), y(), chooser->filelist->w()-4, 20 );
  2067. }
  2068. else
  2069. resize( x(), y(), DEFAULT_ENTRY_WIDTH, 20 );
  2070. details = chooser->fileDetailsBtn->value() && ( type != ENTRY_FAVORITE );
  2071. if( details )
  2072. {
  2073. nameW = chooser->detailNameBtn->w();
  2074. typeW = chooser->detailTypeBtn->w();
  2075. sizeW = chooser->detailSizeBtn->w();
  2076. dateW = chooser->detailDateBtn->w();
  2077. resize( x(), y(), chooser->filedetails->w(), 20 );
  2078. }
  2079. else
  2080. nameW = w();
  2081. // how big is the icon?
  2082. int iW = 0, iH = 0;
  2083. if( icon )
  2084. {
  2085. iW = icon->w()+2;
  2086. iH = icon->h();
  2087. }
  2088. fl_font( textfont(), textsize() );
  2089. // measure the name and see if we need a truncated version
  2090. int W = 0, H = 0;
  2091. fl_measure( filename.c_str(), W, H );
  2092. if( W > nameW-iW )
  2093. {
  2094. // progressively strip characters off the end of the name until
  2095. // it fits with "..." at the end
  2096. if( altname[0] != '\0' )
  2097. shortname = altname;
  2098. else
  2099. shortname = filename;
  2100. int len = shortname.size();
  2101. while( W > (nameW-iW) && len > 3 )
  2102. {
  2103. shortname[len-3] = '.';
  2104. shortname[len-2] = '.';
  2105. shortname[len-1] = '.';
  2106. shortname[len] = '\0';
  2107. len--;
  2108. W = 0;
  2109. fl_measure( shortname.c_str(), W, H );
  2110. }
  2111. }
  2112. else
  2113. shortname = "";
  2114. // measure the description and see if we need a truncated version
  2115. shortDescription = "";
  2116. if( details )
  2117. {
  2118. W = 0; H = 0;
  2119. fl_measure( description.c_str(), W, H );
  2120. if( W > typeW-4 )
  2121. {
  2122. // progressively strip characters off the end of the description until
  2123. // it fits with "..." at the end
  2124. shortDescription = description;
  2125. int len = shortDescription.size();
  2126. while( W > typeW-4 && len > 3 )
  2127. {
  2128. shortDescription[len-3] = '.';
  2129. shortDescription[len-2] = '.';
  2130. shortDescription[len-1] = '.';
  2131. shortDescription[len] = '\0';
  2132. len--;
  2133. W = 0;
  2134. fl_measure( shortDescription.c_str(), W, H );
  2135. }
  2136. }
  2137. }
  2138. redraw();
  2139. }
  2140. Flu_File_Chooser :: Entry :: ~Entry()
  2141. {
  2142. }
  2143. void Flu_File_Chooser :: Entry :: inputCB()
  2144. {
  2145. redraw();
  2146. // if the user tried to change the string to nothing, restore the original name and turn off edit mode
  2147. if( strlen( value() ) == 0 )
  2148. {
  2149. editMode = 0;
  2150. return;
  2151. }
  2152. // if input text is different from filename, try to change the filename
  2153. if( strcmp( value(), filename.c_str() ) != 0 )
  2154. {
  2155. // build the total old filename and new filename
  2156. FluSimpleString oldName = chooser->currentDir + filename,
  2157. newName = chooser->currentDir + value();
  2158. // see if new name already exists
  2159. struct stat s;
  2160. int result = ::stat( newName.c_str(), &s );
  2161. if( result == 0 )
  2162. {
  2163. fl_alert( fileExistsErrTxt.c_str(), newName.c_str() );
  2164. return; // leave editing on
  2165. }
  2166. if( rename( oldName.c_str(), newName.c_str() ) == -1 )
  2167. {
  2168. fl_alert( renameErrTxt.c_str(), oldName.c_str(), newName.c_str() );
  2169. //return; // leave editing on
  2170. }
  2171. else
  2172. {
  2173. filename = value();
  2174. updateSize();
  2175. updateIcon();
  2176. }
  2177. // QUESTION: should we set the chooser filename to the modified name?
  2178. //chooser->filename.value( filename.c_str() );
  2179. }
  2180. // only turn off editing if we have a successful name change
  2181. editMode = 0;
  2182. }
  2183. Fl_Group* Flu_File_Chooser :: getEntryGroup()
  2184. {
  2185. return (!fileDetailsBtn->value() || currentDir == FAVORITES_UNIQUE_STRING ) ? &(filelist->group) : filedetails;
  2186. }
  2187. Fl_Group* Flu_File_Chooser :: getEntryContainer()
  2188. {
  2189. return (!fileDetailsBtn->value() || currentDir == FAVORITES_UNIQUE_STRING ) ? (Fl_Group*)filelist : filedetails;
  2190. }
  2191. int Flu_File_Chooser :: Entry :: handle( int event )
  2192. {
  2193. if( editMode )
  2194. {
  2195. // if user hits 'Escape' while in edit mode, restore the original name and turn off edit mode
  2196. if( event == FL_KEYDOWN && Fl::event_key( FL_Escape ) )
  2197. {
  2198. editMode = 0;
  2199. redraw();
  2200. if( selected )
  2201. chooser->trashBtn->activate();
  2202. return 1;
  2203. }
  2204. return Fl_Input::handle( event );
  2205. }
  2206. if( event == FL_FOCUS || event == FL_UNFOCUS )
  2207. return 1;
  2208. if( event == FL_ENTER || event == FL_LEAVE )
  2209. return 1;
  2210. Fl_Group *g = chooser->getEntryGroup();
  2211. if( event == FL_PUSH )
  2212. {
  2213. if( Fl::event_clicks() > 0 )
  2214. {
  2215. Fl::event_clicks(0);
  2216. // double-clicking a favorite cd's to it
  2217. if( type == ENTRY_FAVORITE )
  2218. {
  2219. chooser->delay_cd( filename );
  2220. }
  2221. // double-clicking a directory cd's to it
  2222. else if( type != ENTRY_FILE )
  2223. {
  2224. #ifdef WIN32
  2225. if( filename[1] == ':' )
  2226. chooser->delay_cd( filename );
  2227. else
  2228. #endif
  2229. chooser->delay_cd( chooser->currentDir + filename + "/" );
  2230. }
  2231. // double-clicking a file chooses it if we are in file selection mode
  2232. else if( !(chooser->selectionType & DIRECTORY) || (chooser->selectionType & STDFILE) )
  2233. {
  2234. Fl::add_timeout( 0.0f, Flu_File_Chooser::selectCB, chooser );
  2235. }
  2236. if( selected )
  2237. chooser->trashBtn->activate();
  2238. return 1;
  2239. }
  2240. /*
  2241. if( selected && !Fl::event_button3() && !Fl::event_state(FL_CTRL) && !Fl::event_state(FL_SHIFT) )
  2242. {
  2243. // only allow editing of certain files and directories
  2244. if( chooser->fileEditing && ( type == ENTRY_FILE || type == ENTRY_DIR ) )
  2245. {
  2246. // if already selected, switch to input mode
  2247. Fl::add_timeout( 1.0, _editCB, this );
  2248. return 1;
  2249. }
  2250. }
  2251. else*/
  2252. if( chooser->selectionType & MULTI )
  2253. {
  2254. if( Fl::event_state(FL_CTRL) )
  2255. {
  2256. selected = !selected; // toggle this item
  2257. chooser->lastSelected = this;
  2258. if( type == ENTRY_FILE )
  2259. chooser->previewGroup->file = chooser->currentDir + filename;
  2260. chooser->redraw();
  2261. chooser->getEntryContainer()->take_focus();
  2262. }
  2263. else if( Fl::event_state(FL_SHIFT) )
  2264. {
  2265. // toggle all items from the last selected item to this one
  2266. if( chooser->lastSelected == NULL )
  2267. {
  2268. selected = true;
  2269. chooser->lastSelected = this;
  2270. if( type == ENTRY_FILE )
  2271. chooser->previewGroup->file = chooser->currentDir + filename;
  2272. chooser->redraw();
  2273. chooser->getEntryContainer()->take_focus();
  2274. }
  2275. else
  2276. {
  2277. // get the index of the last selected item and this item
  2278. int lastindex = -1, thisindex = -1;
  2279. int i;
  2280. for( i = 0; i < g->children(); i++ )
  2281. {
  2282. if( g->child(i) == chooser->lastSelected )
  2283. lastindex = i;
  2284. if( g->child(i) == this )
  2285. thisindex = i;
  2286. if( lastindex >= 0 && thisindex >= 0 )
  2287. break;
  2288. }
  2289. if( lastindex >= 0 && thisindex >= 0 )
  2290. {
  2291. // loop from this item to the last item, toggling each item except the last
  2292. int inc;
  2293. if( thisindex > lastindex )
  2294. inc = -1;
  2295. else
  2296. inc = 1;
  2297. Entry *e;
  2298. for( i = thisindex; i != lastindex; i += inc )
  2299. {
  2300. e = (Entry*)g->child(i);
  2301. e->selected = !e->selected;
  2302. e->redraw();
  2303. }
  2304. chooser->lastSelected = this;
  2305. if( type == ENTRY_FILE )
  2306. chooser->previewGroup->file = chooser->currentDir + filename;
  2307. chooser->redraw();
  2308. chooser->getEntryContainer()->take_focus();
  2309. }
  2310. }
  2311. }
  2312. else
  2313. {
  2314. chooser->unselect_all();
  2315. selected = true;
  2316. chooser->lastSelected = this;
  2317. if( type == ENTRY_FILE )
  2318. chooser->previewGroup->file = chooser->currentDir + filename;
  2319. chooser->redraw();
  2320. chooser->getEntryContainer()->take_focus();
  2321. }
  2322. if( !((chooser->selectionType & Flu_File_Chooser::DIRECTORY) ||
  2323. (chooser->selectionType & Flu_File_Chooser::STDFILE)) &&
  2324. ( Fl::event_state(FL_CTRL) || Fl::event_state(FL_SHIFT) ) )
  2325. {
  2326. // if we are only choosing multiple files, don't allow a directory
  2327. // to be selected
  2328. Fl_Group *g = chooser->getEntryGroup();
  2329. for( int i = 0; i < g->children(); i++ )
  2330. {
  2331. Entry *e = (Entry*)g->child(i);
  2332. if( e->type == ENTRY_DIR )
  2333. e->selected = false;
  2334. }
  2335. }
  2336. }
  2337. else
  2338. {
  2339. chooser->unselect_all();
  2340. selected = true;
  2341. chooser->lastSelected = this;
  2342. if( type == ENTRY_FILE )
  2343. chooser->previewGroup->file = chooser->currentDir + filename;
  2344. chooser->redraw();
  2345. chooser->getEntryContainer()->take_focus();
  2346. }
  2347. //g->take_focus();
  2348. redraw();
  2349. if( selected )
  2350. chooser->trashBtn->activate();
  2351. if( Fl::event_button3() )
  2352. return chooser->popupContextMenu( this );
  2353. // don't put the filename into the box if we are a directory but we are not choosing directories
  2354. // or if we are in SAVING mode
  2355. if( (chooser->selectionType & Flu_File_Chooser::DIRECTORY) ||
  2356. (chooser->selectionType & Flu_File_Chooser::STDFILE) ||
  2357. type==ENTRY_FILE )
  2358. chooser->filename.value( filename.c_str() );
  2359. else if( !(chooser->selectionType & Flu_File_Chooser::SAVING ) )
  2360. chooser->filename.value( "" );
  2361. chooser->filename.position( chooser->filename.size(), chooser->filename.size() );
  2362. return 1;
  2363. }
  2364. else if( event == FL_DRAG )
  2365. {
  2366. if( chooser->selectionType & MULTI )
  2367. {
  2368. // toggle all items from the last selected item to this one
  2369. if( chooser->lastSelected != NULL )
  2370. {
  2371. selected = true;
  2372. // get the index of the last selected item and this item
  2373. int lastindex = -1, thisindex = -1;
  2374. int i;
  2375. for( i = 0; i < g->children(); i++ )
  2376. {
  2377. if( g->child(i) == chooser->lastSelected )
  2378. lastindex = i;
  2379. if( g->child(i) == this )
  2380. thisindex = i;
  2381. if( lastindex >= 0 && thisindex >= 0 )
  2382. break;
  2383. }
  2384. if( lastindex >= 0 && thisindex >= 0 )
  2385. {
  2386. // loop from this item to the last item, toggling each item except the last
  2387. int inc;
  2388. if( thisindex > lastindex )
  2389. inc = -1;
  2390. else
  2391. inc = 1;
  2392. Entry *e;
  2393. for( i = thisindex; i != lastindex; i += inc )
  2394. {
  2395. e = (Entry*)g->child(i);
  2396. e->selected = !e->selected;
  2397. e->redraw();
  2398. }
  2399. chooser->lastSelected = this;
  2400. if( type == ENTRY_FILE )
  2401. chooser->previewGroup->file = chooser->currentDir + filename;
  2402. chooser->redraw();
  2403. }
  2404. redraw();
  2405. chooser->getEntryContainer()->take_focus();
  2406. if( selected )
  2407. chooser->trashBtn->activate();
  2408. return 1;
  2409. }
  2410. }
  2411. }
  2412. return Fl_Widget::handle(event);
  2413. }
  2414. void Flu_File_Chooser :: Entry :: editCB()
  2415. {
  2416. // if already selected, switch to input mode
  2417. editMode = 2;
  2418. value( filename.c_str() );
  2419. take_focus();
  2420. // select the text up to but not including the extension
  2421. const char *dot = strrchr( filename.c_str(), '.' );
  2422. if( dot )
  2423. position( 0, dot-filename.c_str() );
  2424. else
  2425. position( 0, filename.size() );
  2426. chooser->trashBtn->deactivate();
  2427. redraw();
  2428. }
  2429. int Flu_File_Chooser :: popupContextMenu( Entry *entry )
  2430. {
  2431. int type = entry ? entry->type : ENTRY_NONE;
  2432. char *filename = entry ? (char*)entry->filename.c_str() : NULL;
  2433. char *ext = NULL;
  2434. if( filename )
  2435. ext = strrchr( filename, '.' );
  2436. if( ext )
  2437. {
  2438. ext = strdup( ext+1 ); // skip the '.'
  2439. for( unsigned int i = 0; i < strlen(ext); i++ )
  2440. ext[i] = tolower( ext[i] );
  2441. }
  2442. enum { ACTION_NEW_FOLDER = -1, ACTION_RENAME = -2, ACTION_DELETE = -3 };
  2443. entryPopup.clear();
  2444. switch( type )
  2445. {
  2446. case ENTRY_NONE: // right click on nothing
  2447. entryPopup.add( contextMenuTxt[0].c_str(), 0, 0, (void*)ACTION_NEW_FOLDER );
  2448. break;
  2449. case ENTRY_DIR:
  2450. entryPopup.add( contextMenuTxt[1].c_str(), 0, 0, (void*)ACTION_RENAME );
  2451. entryPopup.add( contextMenuTxt[2].c_str(), 0, 0, (void*)ACTION_DELETE );
  2452. break;
  2453. case ENTRY_FILE:
  2454. entryPopup.add( contextMenuTxt[1].c_str(), 0, 0, (void*)ACTION_RENAME );
  2455. entryPopup.add( contextMenuTxt[2].c_str(), 0, 0, (void*)ACTION_DELETE );
  2456. break;
  2457. case ENTRY_FAVORITE:
  2458. entryPopup.add( contextMenuTxt[2].c_str(), 0, 0, (void*)ACTION_DELETE );
  2459. break;
  2460. case ENTRY_DRIVE:
  2461. break;
  2462. case ENTRY_MYDOCUMENTS:
  2463. break;
  2464. case ENTRY_MYCOMPUTER:
  2465. break;
  2466. }
  2467. // add the programmable context handlers
  2468. for( unsigned int i = 0; i < contextHandlers.size(); i++ )
  2469. {
  2470. if( !(contextHandlers[i].type & type) )
  2471. continue;
  2472. if( type == ENTRY_FILE )
  2473. if( contextHandlers[i].ext.size() && contextHandlers[i].ext != ext )
  2474. continue;
  2475. entryPopup.add( contextHandlers[i].name.c_str(), 0, 0, (void*)i );
  2476. }
  2477. if( ext )
  2478. free( ext );
  2479. entryPopup.position( Fl::event_x(), Fl::event_y() );
  2480. const Fl_Menu_Item *selection = entryPopup.popup();
  2481. if( selection )
  2482. {
  2483. int handler = (int)selection->user_data();
  2484. switch( handler )
  2485. {
  2486. case ACTION_NEW_FOLDER:
  2487. newFolderCB();
  2488. break;
  2489. case ACTION_RENAME:
  2490. entry->editCB();
  2491. /*
  2492. entry->editMode = 2;
  2493. entry->value( entry->filename.c_str() );
  2494. entry->take_focus();
  2495. entry->position( 0, entry->filename.size() );
  2496. trashBtn->deactivate();
  2497. */
  2498. break;
  2499. case ACTION_DELETE:
  2500. // recycle by default, unless the shift key is held down
  2501. trashCB( !Fl::event_state( FL_SHIFT ) );
  2502. break;
  2503. default:
  2504. contextHandlers[handler].callback( filename, type, contextHandlers[handler].callbackData );
  2505. break;
  2506. }
  2507. }
  2508. else
  2509. return handle( FL_PUSH );
  2510. return 1;
  2511. }
  2512. void Flu_File_Chooser :: Entry :: draw()
  2513. {
  2514. if( editMode )
  2515. {
  2516. if( editMode == 2 )
  2517. {
  2518. editMode--;
  2519. fl_draw_box( FL_FLAT_BOX, x(), y(), w(), h(), FL_WHITE );
  2520. redraw();
  2521. }
  2522. Fl_Input::draw();
  2523. return;
  2524. }
  2525. if( selected )
  2526. {
  2527. fl_draw_box( FL_FLAT_BOX, x(), y(), w(), h(), FL_SELECTION_COLOR );
  2528. fl_color( FL_WHITE );
  2529. }
  2530. else
  2531. {
  2532. fl_draw_box( FL_FLAT_BOX, x(), y(), w(), h(), FL_WHITE );
  2533. fl_color( FL_BLACK );
  2534. }
  2535. int X = x()+4;
  2536. if( icon )
  2537. {
  2538. icon->draw( X, y()+h()/2-icon->h()/2 );
  2539. X += icon->w()+2;
  2540. }
  2541. fl_font( textfont(), textsize() );
  2542. //fl_color( textcolor() );
  2543. if( shortname[0] != '\0' )
  2544. fl_draw( shortname.c_str(), X, y(), nameW, h(), FL_ALIGN_LEFT );
  2545. else if( altname[0] != '\0' )
  2546. fl_draw( altname.c_str(), X, y(), nameW, h(), FL_ALIGN_LEFT );
  2547. else
  2548. fl_draw( filename.c_str(), X, y(), nameW, h(), FL_ALIGN_LEFT );
  2549. X = x()+4 + nameW;
  2550. if( details )
  2551. {
  2552. if( shortDescription[0] != '\0' )
  2553. fl_draw( shortDescription.c_str(), X, y(), typeW-4, h(), Fl_Align(FL_ALIGN_LEFT | FL_ALIGN_CLIP) );
  2554. else
  2555. fl_draw( description.c_str(), X, y(), typeW-4, h(), Fl_Align(FL_ALIGN_LEFT | FL_ALIGN_CLIP) );
  2556. X += typeW;
  2557. fl_draw( filesize.c_str(), X, y(), sizeW-4, h(), Fl_Align(FL_ALIGN_RIGHT | FL_ALIGN_CLIP) );
  2558. X += sizeW+4;
  2559. fl_draw( date.c_str(), X, y(), dateW-4, h(), Fl_Align(FL_ALIGN_LEFT | FL_ALIGN_CLIP) );
  2560. }
  2561. }
  2562. void Flu_File_Chooser :: unselect_all()
  2563. {
  2564. Fl_Group *g = getEntryGroup();
  2565. Entry *e;
  2566. for( int i = 0; i < g->children(); i++ )
  2567. {
  2568. e = ((Entry*)g->child(i));
  2569. e->selected = false;
  2570. e->editMode = 0;
  2571. }
  2572. lastSelected = 0;
  2573. previewGroup->file = "";
  2574. previewGroup->redraw();
  2575. trashBtn->deactivate();
  2576. redraw();
  2577. }
  2578. void Flu_File_Chooser :: select_all()
  2579. {
  2580. if( !( selectionType & MULTI ) )
  2581. return;
  2582. Fl_Group *g = getEntryGroup();
  2583. Entry *e;
  2584. previewGroup->file = "";
  2585. for( int i = 0; i < g->children(); i++ )
  2586. {
  2587. e = ((Entry*)g->child(i));
  2588. e->selected = true;
  2589. e->editMode = 0;
  2590. previewGroup->file = e->filename;
  2591. filename.value( e->filename.c_str() );
  2592. }
  2593. lastSelected = 0;
  2594. previewGroup->redraw();
  2595. trashBtn->deactivate();
  2596. redraw();
  2597. }
  2598. void Flu_File_Chooser :: updateEntrySizes()
  2599. {
  2600. filecolumns->W1 = detailNameBtn->w();
  2601. filecolumns->W2 = detailTypeBtn->w();
  2602. filecolumns->W3 = detailSizeBtn->w();
  2603. filecolumns->W4 = detailDateBtn->w();
  2604. // update the size of each entry because the user changed the size of each column
  2605. filedetails->resize( filedetails->x(), filedetails->y(), filescroll->w(), filedetails->h() );
  2606. int i;
  2607. for( i = 0; i < filedetails->children(); i++ )
  2608. ((Entry*)filedetails->child(i))->updateSize();
  2609. for( i = 0; i < filelist->children(); i++ )
  2610. ((Entry*)filelist->child(i))->updateSize();
  2611. }
  2612. const char* Flu_File_Chooser :: value()
  2613. {
  2614. if( filename.size() == 0 )
  2615. return NULL;
  2616. else
  2617. {
  2618. #ifdef WIN32
  2619. // on windows, be sure the drive letter is lowercase for
  2620. // compatibility with fl_filename_relative()
  2621. if( filename.size() > 1 && filename.value()[1] == ':' )
  2622. ((char*)(filename.value()))[0] = tolower( filename.value()[0] );
  2623. #endif
  2624. return filename.value();
  2625. }
  2626. }
  2627. int Flu_File_Chooser :: count()
  2628. {
  2629. if( selectionType & MULTI )
  2630. {
  2631. int n = 0;
  2632. Fl_Group *g = getEntryGroup();
  2633. for( int i = 0; i < g->children(); i++ )
  2634. {
  2635. #ifdef WIN32
  2636. if( ((Entry*)g->child(i))->filename == myComputerTxt )
  2637. continue;
  2638. #endif
  2639. if( ((Entry*)g->child(i))->selected )
  2640. n++;
  2641. }
  2642. return n;
  2643. }
  2644. else
  2645. return (strlen(filename.value())==0)? 0 : 1;
  2646. }
  2647. void Flu_File_Chooser :: value( const char *v )
  2648. {
  2649. cd( v );
  2650. if( !v )
  2651. return;
  2652. // try to find the file and select it
  2653. const char *slash = strrchr( v, '/' );
  2654. if( slash )
  2655. slash++;
  2656. else
  2657. {
  2658. slash = strrchr( v, '\\' );
  2659. if( slash )
  2660. slash++;
  2661. else
  2662. slash = v;
  2663. }
  2664. filename.value( slash );
  2665. filename.position( filename.size(), filename.size() );
  2666. Fl_Group *g = getEntryGroup();
  2667. for( int i = 0; i < g->children(); i++ )
  2668. {
  2669. if( ((Entry*)g->child(i))->filename == slash )
  2670. {
  2671. ((Entry*)g->child(i))->selected = true;
  2672. filelist->scroll_to( (Entry*)g->child(i) );
  2673. filedetails->scroll_to( (Entry*)g->child(i) );
  2674. redraw();
  2675. return;
  2676. }
  2677. }
  2678. }
  2679. const char* Flu_File_Chooser :: value( int n )
  2680. {
  2681. Fl_Group *g = getEntryGroup();
  2682. for( int i = 0; i < g->children(); i++ )
  2683. {
  2684. #ifdef WIN32
  2685. if( ((Entry*)g->child(i))->filename == myComputerTxt )
  2686. continue;
  2687. #endif
  2688. if( ((Entry*)g->child(i))->selected )
  2689. {
  2690. n--;
  2691. if( n == 0 )
  2692. {
  2693. FluSimpleString s = currentDir + ((Entry*)g->child(i))->filename;
  2694. filename.value( s.c_str() );
  2695. filename.position( filename.size(), filename.size() );
  2696. return value();
  2697. }
  2698. }
  2699. }
  2700. return "";
  2701. }
  2702. void Flu_File_Chooser :: reloadCB()
  2703. {
  2704. #ifdef WIN32
  2705. refreshDrives = true;
  2706. #endif
  2707. cd( currentDir.c_str() );
  2708. }
  2709. void Flu_File_Chooser :: addToFavoritesCB()
  2710. {
  2711. // eliminate duplicates
  2712. bool duplicate = false;
  2713. for( int i = 1; i <= favoritesList->size(); i++ )
  2714. {
  2715. if( streq( currentDir.c_str(), favoritesList->text(i) ) )
  2716. {
  2717. duplicate = true;
  2718. break;
  2719. }
  2720. }
  2721. if( !duplicate )
  2722. favoritesList->add( currentDir.c_str() );
  2723. // save the favorites
  2724. FILE *f = fopen( configFilename.c_str(), "w" );
  2725. if( f )
  2726. {
  2727. for( int i = 1; i <= favoritesList->size(); i++ )
  2728. fprintf( f, "%s\n", favoritesList->text(i) );
  2729. fclose( f );
  2730. }
  2731. }
  2732. FluSimpleString Flu_File_Chooser :: formatDate( const char *d )
  2733. {
  2734. if( d == 0 )
  2735. {
  2736. FluSimpleString s;
  2737. return s;
  2738. }
  2739. // convert style "Wed Mar 19 07:23:11 2003" to "MM/DD/YY HH:MM AM|PM"
  2740. int month, day, year, hour, minute, second;
  2741. bool pm;
  2742. char MM[16], dummy[64];
  2743. sscanf( d, "%s %s %d %d:%d:%d %d", dummy, MM, &day, &hour, &minute, &second, &year );
  2744. pm = ( hour >= 12 );
  2745. if( hour == 0 )
  2746. hour = 12;
  2747. if( hour >= 13 )
  2748. hour -= 12;
  2749. if( strcmp(MM,"Jan")==0 ) month = 1;
  2750. else if( strcmp(MM,"Feb")==0 ) month = 2;
  2751. else if( strcmp(MM,"Mar")==0 ) month = 3;
  2752. else if( strcmp(MM,"Apr")==0 ) month = 4;
  2753. else if( strcmp(MM,"May")==0 ) month = 5;
  2754. else if( strcmp(MM,"Jun")==0 ) month = 6;
  2755. else if( strcmp(MM,"Jul")==0 ) month = 7;
  2756. else if( strcmp(MM,"Aug")==0 ) month = 8;
  2757. else if( strcmp(MM,"Sep")==0 ) month = 9;
  2758. else if( strcmp(MM,"Oct")==0 ) month = 10;
  2759. else if( strcmp(MM,"Nov")==0 ) month = 11;
  2760. else month = 12;
  2761. sprintf( dummy, "%d/%d/%02d %d:%02d %s", month, day, year, hour, minute, pm?"PM":"AM" );
  2762. FluSimpleString formatted = dummy;
  2763. return formatted;
  2764. }
  2765. void Flu_File_Chooser :: win2unix( FluSimpleString &s )
  2766. {
  2767. int len = s.size();
  2768. for( int i = 0; i < len; i++ )
  2769. if( s[i] == '\\' )
  2770. s[i] = '/';
  2771. }
  2772. void Flu_File_Chooser :: cleanupPath( FluSimpleString &s )
  2773. {
  2774. // convert all '\' to '/'
  2775. win2unix( s );
  2776. FluSimpleString newS(s.size()+1);
  2777. int oldPos, newPos;
  2778. for( oldPos = 0, newPos = 0; oldPos < s.size(); oldPos++ )
  2779. {
  2780. // remove "./"
  2781. if( s[oldPos] == '.' && s[oldPos+1] == '/' )
  2782. oldPos += 2;
  2783. // convert "//" to "/"
  2784. else if( s[oldPos] == '/' && s[oldPos+1] == '/' )
  2785. oldPos++;
  2786. #ifdef WIN32
  2787. // convert "c:" to "C:"
  2788. else if( s[oldPos+1] == ':' )
  2789. s[oldPos] = toupper( s[oldPos] );
  2790. #endif
  2791. // remove "../" by removing everything back to the last "/"
  2792. if( oldPos+2 < s.size() ) // bounds check
  2793. {
  2794. if( s[oldPos] == '.' && s[oldPos+1] == '.' && s[oldPos+2] == '/' && newS != "/" )
  2795. {
  2796. // erase the last character, which should be a '/'
  2797. newPos--;
  2798. newS[newPos] = '\0';
  2799. // look for the previous '/'
  2800. const char *lastSlash = strrchr( newS.c_str(), '/' );
  2801. // make the new string position after the slash
  2802. newPos = (lastSlash-newS.c_str())+1;
  2803. oldPos += 3;
  2804. }
  2805. }
  2806. newS[newPos] = s[oldPos];
  2807. newPos++;
  2808. }
  2809. newS[newPos] = '\0';
  2810. s = newS;
  2811. }
  2812. void Flu_File_Chooser :: delay_cd( FluSimpleString path )
  2813. {
  2814. delayedCd = path;
  2815. Fl::add_timeout( 0.0f, Flu_File_Chooser::delayedCdCB, this );
  2816. }
  2817. void Flu_File_Chooser :: backCB()
  2818. {
  2819. if( !currentHist ) return;
  2820. if( currentHist->last )
  2821. {
  2822. currentHist = currentHist->last;
  2823. walkingHistory = true;
  2824. delay_cd( currentHist->path );
  2825. }
  2826. }
  2827. void Flu_File_Chooser :: forwardCB()
  2828. {
  2829. if( !currentHist ) return;
  2830. if( currentHist->next )
  2831. {
  2832. currentHist = currentHist->next;
  2833. walkingHistory = true;
  2834. delay_cd( currentHist->path );
  2835. }
  2836. }
  2837. bool Flu_File_Chooser :: correctPath( FluSimpleString &path )
  2838. {
  2839. // the path may or may not be an alias, needing corrected
  2840. #ifdef WIN32
  2841. // point to the correct desktop
  2842. if( path == "/"+desktopTxt+"/" )
  2843. {
  2844. path = userDesktop;
  2845. return true;
  2846. }
  2847. else if( path == userDesktop )
  2848. return true;
  2849. else if( path == "/"+desktopTxt+"/"+myComputerTxt+"/" ||
  2850. path == userDesktop+myComputerTxt+"/" )
  2851. path = "/";
  2852. else if( path == "/"+desktopTxt+"/"+myDocumentsTxt+"/" ||
  2853. path == userDesktop+myDocumentsTxt+"/" )
  2854. path = userDocs;
  2855. #endif
  2856. return false;
  2857. }
  2858. void Flu_File_Chooser :: locationCB( const char *path )
  2859. {
  2860. #ifdef WIN32
  2861. FluSimpleString p = path;
  2862. if( p == "/"+favoritesTxt+"/" )
  2863. favoritesCB();
  2864. else if( p == "/"+desktopTxt+"/"+myComputerTxt+"/" )
  2865. myComputerCB();
  2866. else if( p == "/"+desktopTxt+"/"+myDocumentsTxt+"/" )
  2867. documentsCB();
  2868. else if( p == "/"+desktopTxt+"/" )
  2869. desktopCB();
  2870. // if the path leads off with "/Desktop/My Computer", then strip that part off and cd
  2871. // to the remaining
  2872. else
  2873. {
  2874. FluSimpleString s = "/"+desktopTxt+"/"+myComputerTxt+"/";
  2875. if( strstr( path, s.c_str() ) == path )
  2876. {
  2877. // seach for '(' and if present, extract the drive name and cd to it
  2878. char *paren = strrchr( path, '(' );
  2879. if( paren )
  2880. {
  2881. char drive[] = "A:/";
  2882. drive[0] = toupper(paren[1]);
  2883. cd( drive );
  2884. }
  2885. else
  2886. {
  2887. cd( path+21 );
  2888. }
  2889. }
  2890. }
  2891. #else
  2892. cd( path );
  2893. #endif
  2894. updateLocationQJ();
  2895. }
  2896. void Flu_File_Chooser :: buildLocationCombo()
  2897. {
  2898. // add all filesystems
  2899. location->tree.clear();
  2900. #ifdef WIN32
  2901. FluSimpleString s;
  2902. char volumeName[1024];
  2903. Flu_Tree_Browser::Node *n;
  2904. s = "/"+desktopTxt+"/";
  2905. n = location->tree.add( s.c_str() ); n->branch_icon( &little_desktop );
  2906. s = "/"+desktopTxt+"/"+myDocumentsTxt+"/";
  2907. n = location->tree.add( s.c_str() ); n->branch_icon( &documents );
  2908. s = "/"+desktopTxt+"/"+myComputerTxt+"/";
  2909. n = location->tree.add( s.c_str() ); n->branch_icon( &computer );
  2910. // get the location and add them
  2911. {
  2912. if( refreshDrives )
  2913. driveMask = GetLogicalDrives();
  2914. DWORD mask = driveMask;
  2915. for( int i = 0; i < 26; i++ )
  2916. {
  2917. drives[i] = "";
  2918. driveIcons[i] = &disk_drive;
  2919. if( mask & 1 )
  2920. {
  2921. s = "/"+desktopTxt+"/"+myComputerTxt+"/";
  2922. char drive[] = "A:";
  2923. char windrive[] = "A:\\";
  2924. windrive[0] = drive[0] = 'A' + i;
  2925. DWORD type;
  2926. if( refreshDrives )
  2927. {
  2928. volumeName[0] = '\0';
  2929. type = driveTypes[i] = GetDriveType( windrive );
  2930. if( type != DRIVE_REMOVABLE && type != DRIVE_REMOTE )
  2931. GetVolumeInformation( windrive, volumeName, 1024, NULL, NULL, NULL, NULL, 0 );
  2932. volumeNames[i] = volumeName;
  2933. }
  2934. else
  2935. {
  2936. strncpy( volumeName, volumeNames[i].c_str(), 1024 );
  2937. type = driveTypes[i];
  2938. }
  2939. //s += volume
  2940. const char *disk = "Disk";
  2941. switch( type )
  2942. {
  2943. case DRIVE_REMOVABLE:
  2944. disk = strlen(volumeName)?volumeName: ( 1 < 2 ? diskTypesTxt[0].c_str() : diskTypesTxt[1].c_str() );
  2945. driveIcons[i] = &floppy_drive;
  2946. break;
  2947. case DRIVE_FIXED:
  2948. disk = strlen(volumeName)?volumeName:diskTypesTxt[2].c_str();
  2949. //driveIcons[i] = &disk_drive;
  2950. break;
  2951. case DRIVE_CDROM:
  2952. disk = strlen(volumeName)?volumeName:diskTypesTxt[3].c_str();
  2953. driveIcons[i] = &cd_drive;
  2954. break;
  2955. case DRIVE_REMOTE:
  2956. disk = strlen(volumeName)?volumeName:diskTypesTxt[4].c_str();
  2957. driveIcons[i] = &network_drive;
  2958. break;
  2959. case DRIVE_RAMDISK:
  2960. disk = strlen(volumeName)?volumeName:diskTypesTxt[5].c_str();
  2961. driveIcons[i] = &ram_drive;
  2962. break;
  2963. }
  2964. drives[i] = FluSimpleString(disk) + " (" + FluSimpleString(drive) + ")/";
  2965. s += drives[i];
  2966. n = location->tree.add( s.c_str() ); n->branch_icon( driveIcons[i] );
  2967. // erase the trailing '/' to make things look nicer
  2968. drives[i][ drives[i].size()-1 ] = '\0';
  2969. }
  2970. mask >>= 1;
  2971. }
  2972. }
  2973. s = favoritesTxt+"/";
  2974. n = location->tree.add( s.c_str() ); n->branch_icon( &little_favorites );
  2975. refreshDrives = false;
  2976. #elif defined __APPLE__
  2977. location->tree.label( "/" );
  2978. // get all volume mount points and add to the location combobox
  2979. dirent **e;
  2980. char *name;
  2981. int num = fl_filename_list( "/Volumes/", &e );
  2982. if( num > 0 )
  2983. {
  2984. int i;
  2985. for( i = 0; i < num; i++ )
  2986. {
  2987. name = e[i]->d_name;
  2988. // ignore the "." and ".." names
  2989. if( strcmp( name, "." ) == 0 || strcmp( name, ".." ) == 0 ||
  2990. strcmp( name, "./" ) == 0 || strcmp( name, "../" ) == 0 ||
  2991. strcmp( name, ".\\" ) == 0 || strcmp( name, "..\\" ) == 0 )
  2992. continue;
  2993. // if 'name' ends in '/', remove it
  2994. if( name[strlen(name)-1] == '/' )
  2995. name[strlen(name)-1] = '\0';
  2996. FluSimpleString fullpath = "/Volumes/";
  2997. fullpath += name;
  2998. fullpath += "/";
  2999. location->tree.add( fullpath.c_str() );
  3000. }
  3001. }
  3002. #else
  3003. location->tree.label( "/" );
  3004. // get all mount points and add to the location combobox
  3005. FILE *fstab; // /etc/mtab or /etc/mnttab file
  3006. char dummy[256], mountPoint[256], line[1024]; // Input line
  3007. FluSimpleString mount;
  3008. fstab = fopen( "/etc/fstab", "r" ); // Otherwise fallback to full list
  3009. if( fstab )
  3010. {
  3011. while( fgets( line, 1024, fstab ) )
  3012. {
  3013. if( line[0] == '#' || line[0] == '\n' )
  3014. continue;
  3015. // in fstab, mount point is second full string
  3016. sscanf( line, "%s %s", dummy, mountPoint );
  3017. mount = mountPoint;
  3018. // cull some stuff
  3019. if( mount[0] != '/' ) continue;
  3020. if( mount == "/" ) continue;
  3021. if( mount == "/boot" ) continue;
  3022. if( mount == "/proc" ) continue;
  3023. // now add the mount point
  3024. mount += "/";
  3025. location->tree.add( mount.c_str() );
  3026. }
  3027. fclose( fstab );
  3028. }
  3029. #endif
  3030. }
  3031. void Flu_File_Chooser :: clear_history()
  3032. {
  3033. currentHist = history;
  3034. while( currentHist )
  3035. {
  3036. History *next = currentHist->next;
  3037. delete currentHist;
  3038. currentHist = next;
  3039. }
  3040. currentHist = history = NULL;
  3041. backBtn->deactivate();
  3042. forwardBtn->deactivate();
  3043. }
  3044. void Flu_File_Chooser :: addToHistory()
  3045. {
  3046. // remember history
  3047. // only store this path in the history if it is not the current directory
  3048. if( currentDir.size() && !walkingHistory )
  3049. {
  3050. if( history == NULL )
  3051. {
  3052. history = new History;
  3053. currentHist = history;
  3054. currentHist->path = currentDir;
  3055. }
  3056. else if( currentHist->path != currentDir )
  3057. {
  3058. // since we are adding a new path, delete everything after this path
  3059. History *h = currentHist->next;
  3060. while( h )
  3061. {
  3062. History *next = h->next;
  3063. delete h;
  3064. h = next;
  3065. }
  3066. currentHist->next = new History;
  3067. currentHist->next->last = currentHist;
  3068. currentHist = currentHist->next;
  3069. currentHist->path = currentDir;
  3070. }
  3071. History * h = history;
  3072. while( h )
  3073. h = h->next;
  3074. }
  3075. walkingHistory = false;
  3076. if( currentHist )
  3077. {
  3078. if( currentHist->last )
  3079. backBtn->activate();
  3080. else
  3081. backBtn->deactivate();
  3082. if( currentHist->next )
  3083. forwardBtn->activate();
  3084. else
  3085. forwardBtn->deactivate();
  3086. }
  3087. }
  3088. // treating the string as a '|' or ';' delimited sequence of patterns, strip them out and place in patterns
  3089. // return whether it is likely that "s" represents a regexp file-matching pattern
  3090. bool Flu_File_Chooser :: stripPatterns( FluSimpleString s, FluStringVector* patterns )
  3091. {
  3092. if( s.size() == 0 )
  3093. return false;
  3094. char *tok = strtok( (char*)s.c_str(), "|;" );
  3095. int tokens = 0;
  3096. while( tok )
  3097. {
  3098. tokens++;
  3099. if( tok[0] == ' ' )
  3100. tok++; // skip whitespace
  3101. patterns->add( tok );
  3102. tok = strtok( NULL, "|;" );
  3103. }
  3104. // if there is just a single token and it looks like it's not a pattern,
  3105. // then it is probably JUST a filename, in which case it should not be
  3106. // treated as a pattern
  3107. if( _isProbablyAPattern( s.c_str() ) )
  3108. return true;
  3109. else if( tokens == 1 )
  3110. {
  3111. patterns->clear();
  3112. return false;
  3113. }
  3114. else
  3115. return true;
  3116. }
  3117. void Flu_File_Chooser :: cd( const char *path )
  3118. {
  3119. Entry *entry;
  3120. char cwd[1024];
  3121. if( !path || path[0] == '\0' )
  3122. {
  3123. path = getcwd( cwd, 1024 );
  3124. if( !path )
  3125. path = "./";
  3126. }
  3127. if( path[0] == '~' )
  3128. {
  3129. if( path[1] == '/' || path[1] == '\\' )
  3130. sprintf( cwd, "%s%s", userHome.c_str(), path+2 );
  3131. else
  3132. sprintf( cwd, "%s%s", userHome.c_str(), path+1 );
  3133. path = cwd;
  3134. }
  3135. lastSelected = 0;
  3136. previewGroup->file = "";
  3137. previewGroup->redraw();
  3138. filelist->scroll_to_beginning();
  3139. filescroll->position( 0, 0 );
  3140. bool listMode = !fileDetailsBtn->value() || streq( path, FAVORITES_UNIQUE_STRING );
  3141. #ifdef WIN32
  3142. // refresh the drives if viewing "My Computer"
  3143. if( strcmp( path, "/" ) == 0 )
  3144. refreshDrives = true;
  3145. #endif
  3146. buildLocationCombo();
  3147. filename.take_focus();
  3148. trashBtn->deactivate();
  3149. reloadBtn->activate();
  3150. newDirBtn->activate();
  3151. previewBtn->activate();
  3152. hiddenFiles->activate();
  3153. addFavoriteBtn->activate();
  3154. resize( x(), y(), w(), h() );
  3155. if( listMode )
  3156. {
  3157. //filecolumns->hide();
  3158. //filescroll->hide();
  3159. fileDetailsGroup->hide();
  3160. filelist->show();
  3161. filelist->parent()->resizable( filelist );
  3162. }
  3163. else
  3164. {
  3165. filelist->hide();
  3166. //filecolumns->show();
  3167. //filescroll->show();
  3168. //filescroll->parent()->resizable( filescroll );
  3169. fileDetailsGroup->show();
  3170. fileDetailsGroup->parent()->resizable( fileDetailsGroup );
  3171. //updateEntrySizes();
  3172. }
  3173. FluSimpleString currentFile = filename.value();
  3174. filescroll->position( 0, 0 );
  3175. //Fl::focus( &filename );
  3176. upDirBtn->activate();
  3177. ok.activate();
  3178. // check for favorites
  3179. if( streq( path, FAVORITES_UNIQUE_STRING ) )
  3180. {
  3181. currentDir = FAVORITES_UNIQUE_STRING;
  3182. addToHistory();
  3183. newDirBtn->deactivate();
  3184. previewBtn->deactivate();
  3185. reloadBtn->deactivate();
  3186. addFavoriteBtn->deactivate();
  3187. hiddenFiles->deactivate();
  3188. location->input.value( favoritesTxt.c_str() );
  3189. updateLocationQJ();
  3190. filelist->clear();
  3191. filedetails->clear();
  3192. if( listMode )
  3193. filelist->begin();
  3194. else
  3195. filedetails->begin();
  3196. for( int i = 1; i <= favoritesList->size(); i++ )
  3197. {
  3198. entry = new Entry( favoritesList->text(i), ENTRY_FAVORITE, false/*fileDetailsBtn->value()*/, this );
  3199. entry->updateSize();
  3200. entry->updateIcon();
  3201. }
  3202. if( listMode )
  3203. filelist->end();
  3204. else
  3205. filedetails->end();
  3206. redraw();
  3207. ok.deactivate();
  3208. return;
  3209. }
  3210. // check for the current directory
  3211. else if( streq( path, "." ) || streq( path, "./" ) || streq( path, ".\\" ) )
  3212. {
  3213. // do nothing. just rescan this directory
  3214. }
  3215. // check for parent directory
  3216. else if( streq( path, ".." ) || streq( path, "../" ) || streq( path, "..\\" ) )
  3217. {
  3218. // if we are viewing the favorites and want to go back a directory, go to the previous directory
  3219. if( currentDir == FAVORITES_UNIQUE_STRING )
  3220. {
  3221. backCB();
  3222. return;
  3223. }
  3224. #ifdef WIN32
  3225. // if we are at the desktop already, then we cannot go back any further
  3226. //if( currentDir == "/Desktop/" )
  3227. //{
  3228. // do nothing
  3229. //}
  3230. //else if( currentDir == userHome+"Desktop/" )
  3231. //currentDir = userHome;
  3232. // if we are viewing "My Computer" and want to go back a directory, go to the desktop
  3233. if( currentDir == "/" )
  3234. {
  3235. //currentDir = userDesktop;//userHome + "Desktop";
  3236. // do nothing
  3237. }
  3238. // if we are at a top level drive, go to "My Computer" (i.e. "/")
  3239. else if( currentDir[1] == ':' && currentDir[3] == '\0' )
  3240. currentDir = "/";
  3241. else
  3242. #else
  3243. // if the current directory is already as far back as we can go, ignore
  3244. if( currentDir != "/" )
  3245. #endif
  3246. {
  3247. // strip everything off the end to the next "/"
  3248. int end = currentDir.size()-1;
  3249. currentDir[end] = '\0';
  3250. while( currentDir[end] != '/' )
  3251. {
  3252. currentDir[end] = '\0';
  3253. end--;
  3254. }
  3255. }
  3256. }
  3257. // check for absolute path
  3258. #ifdef WIN32
  3259. else if( path[1] == ':' || path[0] == '/' )
  3260. #else
  3261. else if( path[0] == '/' )
  3262. #endif
  3263. {
  3264. currentDir = path;
  3265. }
  3266. // else relative path
  3267. else
  3268. {
  3269. // concatenate currentDir with path to make an absolute path
  3270. currentDir += path;
  3271. }
  3272. int numDirs = 0, numFiles = 0;
  3273. filelist->clear();
  3274. filedetails->clear();
  3275. cleanupPath( currentDir );
  3276. #ifdef WIN32
  3277. bool isTopDesktop = ( currentDir == ("/"+desktopTxt+"/") );
  3278. bool isDesktop = correctPath( currentDir );
  3279. if( isTopDesktop )
  3280. upDirBtn->deactivate();
  3281. #else
  3282. if( currentDir == "/" )
  3283. upDirBtn->deactivate();
  3284. #endif
  3285. #ifdef WIN32
  3286. bool root = false;
  3287. // check for my computer
  3288. if( currentDir == "/" )
  3289. {
  3290. ok.deactivate();
  3291. root = true;
  3292. if( listMode )
  3293. filelist->begin();
  3294. else
  3295. filedetails->begin();
  3296. for( int i = 0; i < 26; i++ )
  3297. {
  3298. if( drives[i][0] != '\0' )
  3299. {
  3300. char drive[] = "A:/";
  3301. drive[0] = 'A' + i;
  3302. entry = new Entry( drive, ENTRY_DRIVE, fileDetailsBtn->value(), this );
  3303. switch( driveTypes[i] )
  3304. {
  3305. case DRIVE_REMOVABLE: entry->description = diskTypesTxt[0].c_str(); break;
  3306. case DRIVE_FIXED: entry->description = diskTypesTxt[2].c_str(); break;
  3307. case DRIVE_CDROM: entry->description = diskTypesTxt[3].c_str(); break;
  3308. case DRIVE_REMOTE: entry->description = diskTypesTxt[4].c_str(); break;
  3309. case DRIVE_RAMDISK: entry->description = diskTypesTxt[5].c_str(); break;
  3310. }
  3311. entry->icon = driveIcons[i];
  3312. entry->altname = drives[i];
  3313. entry->updateSize();
  3314. entry->updateIcon();
  3315. }
  3316. }
  3317. if( listMode )
  3318. filelist->end();
  3319. else
  3320. filedetails->end();
  3321. redraw();
  3322. }
  3323. // check for desktop. if so, add My Computer and My Documents
  3324. else if( isDesktop )
  3325. {
  3326. if( listMode )
  3327. filelist->begin();
  3328. else
  3329. filedetails->begin();
  3330. entry = new Entry( myDocumentsTxt.c_str(), ENTRY_MYDOCUMENTS, fileDetailsBtn->value(), this );
  3331. entry->updateSize();
  3332. entry->updateIcon();
  3333. entry = new Entry( myComputerTxt.c_str(), ENTRY_MYCOMPUTER, fileDetailsBtn->value(), this );
  3334. entry->updateSize();
  3335. entry->updateIcon();
  3336. if( listMode )
  3337. filelist->end();
  3338. else
  3339. filedetails->end();
  3340. numDirs += 2;
  3341. }
  3342. #endif
  3343. // see if currentDir is in fact a directory
  3344. // if so, make sure there is a trailing "/" and we're done
  3345. if( fl_filename_isdir( currentDir.c_str() ) || currentDir=="/" )
  3346. {
  3347. if( currentDir[strlen(currentDir.c_str())-1] != '/' )
  3348. currentDir += "/";
  3349. #ifdef WIN32
  3350. if( filename.value()[1] != ':' )
  3351. #else
  3352. if( filename.value()[0] != '/' )
  3353. #endif
  3354. {
  3355. if( !(selectionType & SAVING ) )
  3356. filename.value( "" );
  3357. }
  3358. if( !(selectionType & SAVING ) )
  3359. currentFile = "";
  3360. }
  3361. // now we have the current directory and possibly a file at the end
  3362. // try to split into path and file
  3363. if( currentDir[currentDir.size()-1] != '/' )
  3364. {
  3365. char *lastSlash = (char *)strrchr( currentDir.c_str(), '/' );
  3366. if( lastSlash )
  3367. {
  3368. currentFile = lastSlash+1;
  3369. lastSlash[1] = '\0';
  3370. }
  3371. }
  3372. // make sure currentDir ends in '/'
  3373. if( currentDir[currentDir.size()-1] != '/' )
  3374. currentDir += "/";
  3375. #ifdef WIN32
  3376. {
  3377. FluSimpleString tmp = currentDir;
  3378. if( isTopDesktop )
  3379. currentDir = "/"+desktopTxt+"/";
  3380. addToHistory();
  3381. if( isTopDesktop )
  3382. currentDir = tmp;
  3383. }
  3384. #else
  3385. addToHistory();
  3386. #endif
  3387. delayedCd = "./";
  3388. #ifdef WIN32
  3389. // set the location input value
  3390. // check for drives
  3391. if( currentDir[1] == ':' && currentDir[3] == '\0' )
  3392. {
  3393. location->input.value( currentDir.c_str() );
  3394. }
  3395. else if( currentDir == "/" )
  3396. location->input.value( myComputerTxt.c_str() );
  3397. else
  3398. #endif
  3399. {
  3400. location->input.value( currentDir.c_str() );
  3401. #ifdef WIN32
  3402. FluSimpleString treePath = "/"+desktopTxt+"/"+myComputerTxt+"/"+currentDir;
  3403. Flu_Tree_Browser::Node *n = location->tree.add( treePath.c_str() );
  3404. if( currentDir == (userHome+desktopTxt+"/") )
  3405. n->branch_icon( &little_desktop );
  3406. if( currentDir == (userHome+myDocumentsTxt+"/") )
  3407. n->branch_icon( &documents );
  3408. #else
  3409. location->tree.add( currentDir.c_str() );
  3410. #endif
  3411. }
  3412. updateLocationQJ();
  3413. #ifdef WIN32
  3414. if( root )
  3415. return;
  3416. #endif
  3417. FluSimpleString pathbase, fullpath;
  3418. bool isDir, isCurrentFile = false;
  3419. const char *lastAddedFile = NULL, *lastAddedDir = NULL;
  3420. pathbase = currentDir;
  3421. // take the current pattern and make a list of filter pattern strings
  3422. FluStringVector currentPatterns;
  3423. {
  3424. FluSimpleString pat = patterns[filePattern->list.value()-1];
  3425. while( pat.size() )
  3426. {
  3427. int p = pat.find( ',' );
  3428. if( p == -1 )
  3429. {
  3430. if( pat != "*" )
  3431. pat = "*." + pat;
  3432. currentPatterns.add( pat );
  3433. break;
  3434. }
  3435. else
  3436. {
  3437. FluSimpleString s = pat.c_str() + p + 1;
  3438. pat[p] = '\0';
  3439. if( pat != "*" )
  3440. pat = "*." + pat;
  3441. currentPatterns.add( pat );
  3442. pat = s;
  3443. }
  3444. }
  3445. }
  3446. // add any user-defined patterns
  3447. FluStringVector userPatterns;
  3448. // if the user just hit <Tab> but the filename input area is empty,
  3449. // then use the current patterns
  3450. if( !filenameTabCallback || currentFile != "*" )
  3451. stripPatterns( currentFile, &userPatterns );
  3452. // read the directory
  3453. dirent **e;
  3454. char *name;
  3455. int num = fl_filename_list( pathbase.c_str(), &e );
  3456. if( num > 0 )
  3457. {
  3458. int i;
  3459. for( i = 0; i < num; i++ )
  3460. {
  3461. name = e[i]->d_name;
  3462. // ignore the "." and ".." names
  3463. if( strcmp( name, "." ) == 0 || strcmp( name, ".." ) == 0 ||
  3464. strcmp( name, "./" ) == 0 || strcmp( name, "../" ) == 0 ||
  3465. strcmp( name, ".\\" ) == 0 || strcmp( name, "..\\" ) == 0 )
  3466. continue;
  3467. // if 'name' ends in '/', remove it
  3468. if( name[strlen(name)-1] == '/' )
  3469. name[strlen(name)-1] = '\0';
  3470. // file or directory?
  3471. fullpath = pathbase + name;
  3472. isDir = ( fl_filename_isdir( fullpath.c_str() ) != 0 );
  3473. // was this file specified explicitly?
  3474. isCurrentFile = ( currentFile == name );
  3475. #ifndef WIN32
  3476. // filter hidden files
  3477. if( !isCurrentFile && !hiddenFiles->value() && ( name[0] == '.' ) )
  3478. continue;
  3479. #endif
  3480. // only directories?
  3481. if( (selectionType & DIRECTORY) &&
  3482. !isDir &&
  3483. !(selectionType & STDFILE) &&
  3484. !(selectionType & DEACTIVATE_FILES) )
  3485. continue;
  3486. //if( !isDir /*!isCurrentFile*/ )
  3487. {
  3488. // filter according to the user pattern in the filename input
  3489. if( userPatterns.size() )
  3490. {
  3491. bool cull = true;
  3492. for( unsigned int i = 0; i < userPatterns.size(); i++ )
  3493. {
  3494. if( flu_filename_match( name, userPatterns[i].c_str() ) != 0 )
  3495. {
  3496. cull = false;
  3497. break;
  3498. }
  3499. }
  3500. if( cull )
  3501. {
  3502. // only filter directories if someone just hit <TAB>
  3503. if( !isDir || ( isDir && filenameTabCallback ) )
  3504. continue;
  3505. }
  3506. }
  3507. // filter files according to the current pattern
  3508. else
  3509. {
  3510. bool cull = true;
  3511. for( unsigned int i = 0; i < currentPatterns.size(); i++ )
  3512. {
  3513. if( flu_filename_match( name, currentPatterns[i].c_str() ) != 0 )
  3514. {
  3515. cull = false;
  3516. break;
  3517. }
  3518. }
  3519. if( cull )
  3520. {
  3521. // only filter directories if someone just hit <TAB>
  3522. if( !isDir || ( isDir && filenameTabCallback ) )
  3523. continue;
  3524. }
  3525. }
  3526. }
  3527. // add directories at the beginning, and files at the end
  3528. entry = new Entry( name, isDir?ENTRY_DIR:ENTRY_FILE, fileDetailsBtn->value(), this );
  3529. if( isDir )
  3530. {
  3531. if( listMode )
  3532. filelist->insert( *entry, 0 );
  3533. else
  3534. filedetails->insert( *entry, 0 );
  3535. numDirs++;
  3536. lastAddedDir = entry->filename.c_str();
  3537. }
  3538. else
  3539. {
  3540. if( listMode )
  3541. filelist->add( entry );
  3542. else
  3543. filedetails->add( entry );
  3544. numFiles++;
  3545. lastAddedFile = entry->filename.c_str();
  3546. }
  3547. // get some information about the file
  3548. struct stat s;
  3549. ::stat( fullpath.c_str(), &s );
  3550. // store size as human readable and sortable integer
  3551. entry->isize = s.st_size;
  3552. if( isDir && entry->isize == 0 )
  3553. entry->filesize = "";
  3554. else
  3555. {
  3556. char buf[32];
  3557. /*
  3558. if( (entry->isize >> 40) > 0 ) // terrabytes
  3559. {
  3560. double TB = double(entry->isize)/double(1<<40);
  3561. sprintf( buf, "%.1f TB", TB );
  3562. }
  3563. */
  3564. if( (entry->isize >> 30) > 0 ) // gigabytes
  3565. {
  3566. double GB = double(entry->isize)/double(1<<30);
  3567. sprintf( buf, "%.1f GB", GB );
  3568. }
  3569. else if( (entry->isize >> 20) > 0 ) // megabytes
  3570. {
  3571. double MB = double(entry->isize)/double(1<<20);
  3572. sprintf( buf, "%.1f MB", MB );
  3573. }
  3574. else if( (entry->isize >> 10) > 0 ) // kilabytes
  3575. {
  3576. double KB = double(entry->isize)/double(1<<10);
  3577. sprintf( buf, "%.1f KB", KB );
  3578. }
  3579. else // bytes
  3580. {
  3581. sprintf( buf, "%d bytes", (int)entry->isize );
  3582. }
  3583. entry->filesize = buf;
  3584. }
  3585. // store date as human readable and sortable integer
  3586. entry->date = formatDate( ctime( &s.st_mtime ) );//ctime( &s.st_mtime );
  3587. entry->idate = s.st_mtime;
  3588. // convert the permissions into UNIX style rwx-rwx-rwx (user-group-others)
  3589. /*
  3590. unsigned int p = s.st_mode;
  3591. entry->pU = bool(p&S_IRUSR)<<2 | bool(p&S_IWUSR)<<1 | bool(p&S_IXUSR);
  3592. entry->pG = bool(p&S_IRGRP)<<2 | bool(p&S_IWGRP)<<1 | bool(p&S_IXGRP);
  3593. entry->pO = bool(p&S_IROTH)<<2 | bool(p&S_IWOTH)<<1 | bool(p&S_IXOTH);
  3594. char* perms[8] = { "---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx" };
  3595. entry->permissions = perms[entry->pU];
  3596. entry->permissions += perms[entry->pG];
  3597. entry->permissions += perms[entry->pO];
  3598. */
  3599. entry->updateSize();
  3600. entry->updateIcon();
  3601. if( isCurrentFile )
  3602. {
  3603. filename.value( name );
  3604. entry->selected = true;
  3605. lastSelected = entry;
  3606. if( entry->type == ENTRY_FILE )
  3607. previewGroup->file = currentDir + name;
  3608. previewGroup->redraw();
  3609. filelist->scroll_to( entry );
  3610. filedetails->scroll_to( entry );
  3611. //break;
  3612. }
  3613. }
  3614. for( i = 0; i < num; i++ )
  3615. free((void*)(e[i]));
  3616. free((void*)e);
  3617. }
  3618. // sort the files: directories first, then files
  3619. if( listMode )
  3620. filelist->sort( numDirs );
  3621. else
  3622. filedetails->sort( numDirs );
  3623. // see if the user pushed <Tab> in the filename input field
  3624. if( filenameTabCallback )
  3625. {
  3626. filenameTabCallback = false;
  3627. FluSimpleString prefix = commonStr();
  3628. if( numDirs == 1 &&
  3629. currentFile == (FluSimpleString(lastAddedDir)+"*") )
  3630. {
  3631. delay_cd( lastAddedDir );
  3632. }
  3633. if( numDirs == 1 && numFiles == 0 )
  3634. {
  3635. #ifdef WIN32
  3636. if( filename.value()[1] == ':' )
  3637. #else
  3638. if( filename.value()[0] == '/' )
  3639. #endif
  3640. {
  3641. FluSimpleString s = currentDir + lastAddedDir + "/";
  3642. filename.value( s.c_str() );
  3643. }
  3644. else
  3645. filename.value( lastAddedDir );
  3646. }
  3647. else if( numFiles == 1 && numDirs == 0 )
  3648. {
  3649. #ifdef WIN32
  3650. if( filename.value()[1] == ':' )
  3651. #else
  3652. if( filename.value()[0] == '/' )
  3653. #endif
  3654. {
  3655. FluSimpleString s = currentDir + lastAddedFile;
  3656. filename.value( s.c_str() );
  3657. }
  3658. else
  3659. filename.value( lastAddedFile );
  3660. }
  3661. else if( prefix.size() >= currentFile.size() )
  3662. {
  3663. #ifdef WIN32
  3664. if( filename.value()[1] == ':' )
  3665. #else
  3666. if( filename.value()[0] == '/' )
  3667. #endif
  3668. {
  3669. FluSimpleString s = currentDir + prefix;
  3670. filename.value( s.c_str() );
  3671. }
  3672. else
  3673. filename.value( prefix.c_str() );
  3674. }
  3675. if( currentFile == "*" &&
  3676. #ifdef WIN32
  3677. filename.value()[1] != ':' )
  3678. #else
  3679. filename.value()[0] != '/' )
  3680. #endif
  3681. {
  3682. filename.value( "" );
  3683. }
  3684. }
  3685. // see if the user pushed <Enter> in the filename input field
  3686. if( filenameEnterCallback )
  3687. {
  3688. filenameEnterCallback = false;
  3689. #ifdef WIN32
  3690. if( filename.value()[1] == ':' )
  3691. #else
  3692. if( filename.value()[0] == '/' )
  3693. #endif
  3694. filename.value( "" );
  3695. //if( isCurrentFile && numFiles == 1 )
  3696. if( !_isProbablyAPattern( filename.value() ) )
  3697. okCB();
  3698. }
  3699. if( _isProbablyAPattern( filename.value() ) )
  3700. filename.position( 0, filename.size() );
  3701. else
  3702. filename.position( filename.size(), filename.size() );
  3703. filename.take_focus();
  3704. redraw();
  3705. }
  3706. // find the prefix string that is common to all entries in the list
  3707. FluSimpleString Flu_File_Chooser :: commonStr()
  3708. {
  3709. FluSimpleString common;
  3710. int index = 0;
  3711. const char* name;
  3712. int len, i;
  3713. Fl_Group *g = getEntryGroup();
  3714. for(;;)
  3715. {
  3716. bool allSkipped = true;
  3717. for( i = 0; i < g->children(); i++ )
  3718. {
  3719. name = ((Entry*)g->child(i))->filename.c_str();
  3720. len = strlen( name );
  3721. if( index >= len )
  3722. continue;
  3723. allSkipped = false;
  3724. if( i == 0 )
  3725. common.push_back( name[index] );
  3726. else if( toupper(common[index]) != toupper(name[index]) )
  3727. {
  3728. common[index] = '\0';
  3729. return common;
  3730. }
  3731. }
  3732. if( allSkipped )
  3733. break;
  3734. index++;
  3735. }
  3736. return common;
  3737. }
  3738. static const char* _flu_file_chooser( const char *message, const char *pattern, const char *filename, int type,
  3739. int *count = 0, FluStringVector *filelist = 0 )
  3740. {
  3741. static Flu_File_Chooser *fc = NULL;
  3742. static FluSimpleString retname;
  3743. if( !fc )
  3744. {
  3745. fc = new Flu_File_Chooser( filename, pattern, type, message );
  3746. }
  3747. else
  3748. {
  3749. fc->type( type );
  3750. fc->clear_history();
  3751. fc->label( message );
  3752. if( !filename || filename[0] == '\0' )
  3753. {
  3754. if( (!pattern || !fc->filter() || strcmp(pattern,fc->filter())) && fc->value() )
  3755. {
  3756. // if pattern is different, remove name but leave old directory:
  3757. retname = fc->value();
  3758. char *p = (char*)strrchr( retname.c_str(), '/' );
  3759. if( p )
  3760. {
  3761. // If the filename is "/foo", then the directory will be "/", not ""
  3762. if( p == retname.c_str() )
  3763. retname[1] = '\0';
  3764. else
  3765. p[1] = '\0';
  3766. }
  3767. }
  3768. fc->filter( pattern );
  3769. fc->value( retname.c_str() );
  3770. }
  3771. else
  3772. {
  3773. fc->filter( pattern );
  3774. fc->value( filename );
  3775. }
  3776. }
  3777. fc->set_modal();
  3778. fc->show();
  3779. while( fc->shown() )
  3780. Fl::wait( 0.01 );
  3781. Fl_Group::current(0);
  3782. if( fc->value() )
  3783. {
  3784. if( count && filelist )
  3785. {
  3786. *count = fc->count();
  3787. for( int i = 1; i <= *count; i++ )
  3788. filelist->add( FluSimpleString(fc->value(i)) );
  3789. }
  3790. retname = fc->value();
  3791. return retname.c_str();
  3792. }
  3793. else
  3794. return 0;
  3795. }
  3796. int flu_multi_file_chooser( const char *message, const char *pattern, const char *filename, FluStringVector *filelist )
  3797. {
  3798. int count = 0;
  3799. _flu_file_chooser( message, pattern, filename, Flu_File_Chooser::MULTI, &count, filelist );
  3800. return count;
  3801. }
  3802. const char* flu_file_chooser( const char *message, const char *pattern, const char *filename )
  3803. {
  3804. return _flu_file_chooser( message, pattern, filename, Flu_File_Chooser::SINGLE );
  3805. }
  3806. const char* flu_save_chooser( const char *message, const char *pattern, const char *filename )
  3807. {
  3808. return _flu_file_chooser( message, pattern, filename, Flu_File_Chooser::SINGLE | Flu_File_Chooser::SAVING );
  3809. }
  3810. const char* flu_dir_chooser( const char *message, const char *filename )
  3811. {
  3812. return _flu_file_chooser( message, "*", filename, Flu_File_Chooser::DIRECTORY );
  3813. }
  3814. const char* flu_dir_chooser( const char *message, const char *filename, bool showFiles )
  3815. {
  3816. if( showFiles )
  3817. return _flu_file_chooser( message, "*", filename,
  3818. Flu_File_Chooser::DIRECTORY | Flu_File_Chooser::DEACTIVATE_FILES );
  3819. else
  3820. return( flu_dir_chooser( message, filename ) );
  3821. }
  3822. const char* flu_file_and_dir_chooser( const char *message, const char *filename )
  3823. {
  3824. return _flu_file_chooser( message, "*", filename, Flu_File_Chooser::STDFILE );
  3825. }