Flu_File_Chooser.cpp 109 KB

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