tinyfiledialogs.c 238 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194
  1. /* SPDX-License-Identifier: Zlib
  2. Copyright (c) 2014 - 2024 Guillaume Vareille http://ysengrin.com
  3. ________________________________________________________________
  4. | |
  5. | 100% compatible C C++ -> You can rename this .c file as .cpp |
  6. |________________________________________________________________|
  7. ********* TINY FILE DIALOGS OFFICIAL WEBSITE IS ON SOURCEFORGE *********
  8. _________
  9. / \ tinyfiledialogs.c v3.19.1 [Jan 27, 2025] zlib licence
  10. |tiny file| Unique code file created [November 9, 2014]
  11. | dialogs |
  12. \____ ___/ http://tinyfiledialogs.sourceforge.net
  13. \| git clone http://git.code.sf.net/p/tinyfiledialogs/code tinyfd
  14. ____________________________________________
  15. | |
  16. | email: tinyfiledialogs at ysengrin.com |
  17. |____________________________________________|
  18. _________________________________________________________________________________
  19. | |
  20. | the windows only wchar_t UTF-16 prototypes are at the bottom of the header file |
  21. |_________________________________________________________________________________|
  22. _________________________________________________________
  23. | |
  24. | on windows: - since v3.6 char is UTF-8 by default |
  25. | - if you want MBCS set tinyfd_winUtf8 to 0 |
  26. | - functions like fopen expect MBCS |
  27. |_________________________________________________________|
  28. If you like tinyfiledialogs, please upvote my stackoverflow answer
  29. https://stackoverflow.com/a/47651444
  30. - License -
  31. This software is provided 'as-is', without any express or implied
  32. warranty. In no event will the authors be held liable for any damages
  33. arising from the use of this software.
  34. Permission is granted to anyone to use this software for any purpose,
  35. including commercial applications, and to alter it and redistribute it
  36. freely, subject to the following restrictions:
  37. 1. The origin of this software must not be misrepresented; you must not
  38. claim that you wrote the original software. If you use this software
  39. in a product, an acknowledgment in the product documentation would be
  40. appreciated but is not required.
  41. 2. Altered source versions must be plainly marked as such, and must not be
  42. misrepresented as being the original software.
  43. 3. This notice may not be removed or altered from any source distribution.
  44. __________________________________________
  45. | ______________________________________ |
  46. | | | |
  47. | | DO NOT USE USER INPUT IN THE DIALOGS | |
  48. | |______________________________________| |
  49. |__________________________________________|
  50. */
  51. #if defined(__GNUC__) || defined(__clang__)
  52. #ifndef _GNU_SOURCE
  53. #define _GNU_SOURCE /* used only to resolve symbolic links. Can be commented out */
  54. #ifndef _POSIX_C_SOURCE
  55. #ifdef __FreeBSD__
  56. #define _POSIX_C_SOURCE 199506L /* 199506L is enough for freebsd for realpath() */
  57. #elif defined(__illumos__) || defined(__solaris__)
  58. #define _POSIX_C_SOURCE 200112L /* illumos/solaris needs 200112L for realpath() */
  59. #else
  60. #define _POSIX_C_SOURCE 2 /* to accept POSIX 2 in old ANSI C standards */
  61. #endif
  62. #endif
  63. #endif
  64. #endif
  65. #include <stdio.h>
  66. #include <stdlib.h>
  67. #include <string.h>
  68. #include <ctype.h>
  69. #include <sys/stat.h>
  70. #ifdef _WIN32
  71. #ifdef __BORLANDC__
  72. #define _getch getch
  73. #endif
  74. #ifndef _WIN32_WINNT
  75. #define _WIN32_WINNT 0x0500
  76. #endif
  77. #include <windows.h>
  78. #include <commdlg.h>
  79. #include <shlobj.h>
  80. #include <conio.h>
  81. #include <direct.h>
  82. #define TINYFD_NOCCSUNICODE
  83. #define TINYFD_SLASH "\\"
  84. #else
  85. #include <limits.h>
  86. #include <unistd.h>
  87. #include <dirent.h> /* on old systems try <sys/dir.h> instead */
  88. #include <termios.h>
  89. #include <sys/utsname.h>
  90. #include <signal.h> /* on old systems try <sys/signal.h> instead */
  91. #define TINYFD_SLASH "/"
  92. #endif /* _WIN32 */
  93. #include "tinyfiledialogs.h"
  94. #define MAX_PATH_OR_CMD 1024 /* _MAX_PATH or MAX_PATH */
  95. #ifndef MAX_MULTIPLE_FILES
  96. #define MAX_MULTIPLE_FILES 1024
  97. #endif
  98. #define LOW_MULTIPLE_FILES 32
  99. char tinyfd_version[8] = "3.19.1";
  100. /******************************************************************************************************/
  101. /**************************************** UTF-8 on Windows ********************************************/
  102. /******************************************************************************************************/
  103. #ifdef _WIN32
  104. /* if you want to use UTF-8 ( instead of the UTF-16/wchar_t functions at the end of tinyfiledialogs.h )
  105. Make sure your code is really prepared for UTF-8 (on windows, functions like fopen() expect MBCS and not UTF-8) */
  106. int tinyfd_winUtf8 = 1; /* on windows char strings can be 1:UTF-8(default) or 0:MBCS */
  107. /* for MBCS change this to 0, here or in your code */
  108. #endif
  109. /******************************************************************************************************/
  110. /******************************************************************************************************/
  111. /******************************************************************************************************/
  112. int tinyfd_verbose = 0 ; /* on unix: prints the command line calls */
  113. int tinyfd_silent = 1 ; /* 1 (default) or 0 : on unix, hide errors and warnings from called dialogs */
  114. /* Curses dialogs are difficult to use, on windows they are only ascii and uses the unix backslah */
  115. int tinyfd_allowCursesDialogs = 0 ; /* 0 (default) or 1 */
  116. int tinyfd_forceConsole = 0 ; /* 0 (default) or 1 */
  117. /* for unix & windows: 0 (graphic mode) or 1 (console mode).
  118. 0: try to use a graphic solution, if it fails then it uses console mode.
  119. 1: forces all dialogs into console mode even when the X server is present.
  120. it can use the package dialog or dialog.exe.
  121. on windows it only make sense for console applications */
  122. /* int tinyfd_assumeGraphicDisplay = 0; */ /* 0 (default) or 1 */
  123. /* some systems don't set the environment variable DISPLAY even when a graphic display is present.
  124. set this to 1 to tell tinyfiledialogs to assume the existence of a graphic display */
  125. char tinyfd_response[1024];
  126. /* if you pass "tinyfd_query" as aTitle,
  127. the functions will not display the dialogs
  128. but return 0 for console mode, 1 for graphic mode.
  129. tinyfd_response is then filled with the retain solution.
  130. possible values for tinyfd_response are (all lowercase)
  131. for graphic mode:
  132. windows_wchar windows applescript kdialog zenity zenity3 yad matedialog
  133. shellementary qarma python2-tkinter python3-tkinter python-dbus
  134. perl-dbus gxmessage gmessage xmessage xdialog gdialog dunst
  135. for console mode:
  136. dialog whiptail basicinput no_solution */
  137. static int gWarningDisplayed = 0 ;
  138. static char gTitle[]="missing software! (we will try basic console input)";
  139. #ifdef _WIN32
  140. char tinyfd_needs[] = "\
  141. ___________\n\
  142. / \\ \n\
  143. | tiny file |\n\
  144. | dialogs |\n\
  145. \\_____ ____/\n\
  146. \\|\
  147. \ntiny file dialogs on Windows needs:\
  148. \n a graphic display\
  149. \nor dialog.exe (curses console mode ** Disabled by default **)\
  150. \nor a console for basic input";
  151. #else
  152. char tinyfd_needs[] = "\
  153. ___________\n\
  154. / \\ \n\
  155. | tiny file |\n\
  156. | dialogs |\n\
  157. \\_____ ____/\n\
  158. \\|\
  159. \ntiny file dialogs on UNIX needs:\
  160. \n applescript or kdialog or yad or Xdialog\
  161. \nor zenity (or matedialog or shellementary or qarma)\
  162. \nor python (2 or 3) + tkinter + python-dbus (optional)\
  163. \nor dialog (opens console if needed) ** Disabled by default **\
  164. \nor xterm + bash (opens console for basic input)\
  165. \nor existing console for basic input.";
  166. #endif
  167. #ifdef _MSC_VER
  168. #pragma warning(disable:4996) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */
  169. #pragma warning(disable:4100) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */
  170. #pragma warning(disable:4706) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */
  171. #endif
  172. static int getenvDISPLAY(void)
  173. {
  174. /* return tinyfd_assumeGraphicDisplay || getenv("DISPLAY") || getenv("WAYLAND_DISPLAY") ; */
  175. static int lReturnValue = -1 ;
  176. if ( lReturnValue < 0 )
  177. {
  178. lReturnValue = 0 ;
  179. if ( getenv("DISPLAY") ) lReturnValue += 1 ;
  180. if ( getenv("WAYLAND_DISPLAY") ) lReturnValue += 2 ;
  181. }
  182. return lReturnValue ;
  183. }
  184. static char * getCurDir(void)
  185. {
  186. static char lCurDir[MAX_PATH_OR_CMD];
  187. return getcwd(lCurDir, sizeof(lCurDir));
  188. }
  189. static char * getPathWithoutFinalSlash(
  190. char * aoDestination, /* make sure it is allocated, use _MAX_PATH */
  191. char const * aSource) /* aoDestination and aSource can be the same */
  192. {
  193. char const * lTmp ;
  194. if ( aSource )
  195. {
  196. lTmp = strrchr(aSource, '/');
  197. if (!lTmp)
  198. {
  199. lTmp = strrchr(aSource, '\\');
  200. }
  201. if (lTmp)
  202. {
  203. strncpy(aoDestination, aSource, lTmp - aSource );
  204. aoDestination[lTmp - aSource] = '\0';
  205. }
  206. else
  207. {
  208. * aoDestination = '\0';
  209. }
  210. }
  211. else
  212. {
  213. * aoDestination = '\0';
  214. }
  215. return aoDestination;
  216. }
  217. static char * getLastName(
  218. char * aoDestination, /* make sure it is allocated */
  219. char const * aSource)
  220. {
  221. /* copy the last name after '/' or '\' */
  222. char const * lTmp ;
  223. if ( aSource )
  224. {
  225. lTmp = strrchr(aSource, '/');
  226. if (!lTmp)
  227. {
  228. lTmp = strrchr(aSource, '\\');
  229. }
  230. if (lTmp)
  231. {
  232. strcpy(aoDestination, lTmp + 1);
  233. }
  234. else
  235. {
  236. strcpy(aoDestination, aSource);
  237. }
  238. }
  239. else
  240. {
  241. * aoDestination = '\0';
  242. }
  243. return aoDestination;
  244. }
  245. static void ensureFinalSlash( char * aioString )
  246. {
  247. if ( aioString && strlen( aioString ) )
  248. {
  249. char * lastcar = aioString + strlen( aioString ) - 1 ;
  250. if ( strncmp( lastcar , TINYFD_SLASH , 1 ) )
  251. {
  252. strcat( lastcar , TINYFD_SLASH ) ;
  253. }
  254. }
  255. }
  256. static void Hex2RGB( char const aHexRGB[8] , unsigned char aoResultRGB[3] )
  257. {
  258. char lColorChannel[8] ;
  259. if ( aoResultRGB )
  260. {
  261. if ( aHexRGB )
  262. {
  263. strcpy(lColorChannel, aHexRGB ) ;
  264. aoResultRGB[2] = (unsigned char)strtoul(lColorChannel+5,NULL,16);
  265. lColorChannel[5] = '\0';
  266. aoResultRGB[1] = (unsigned char)strtoul(lColorChannel+3,NULL,16);
  267. lColorChannel[3] = '\0';
  268. aoResultRGB[0] = (unsigned char)strtoul(lColorChannel+1,NULL,16);
  269. /* printf("%d %d %d\n", aoResultRGB[0], aoResultRGB[1], aoResultRGB[2]); */
  270. }
  271. else
  272. {
  273. aoResultRGB[0]=0;
  274. aoResultRGB[1]=0;
  275. aoResultRGB[2]=0;
  276. }
  277. }
  278. }
  279. static void RGB2Hex( unsigned char const aRGB[3], char aoResultHexRGB[8] )
  280. {
  281. if ( aoResultHexRGB )
  282. {
  283. if ( aRGB )
  284. {
  285. #if (defined(__cplusplus ) && __cplusplus >= 201103L) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__clang__)
  286. sprintf(aoResultHexRGB, "#%02hhx%02hhx%02hhx", aRGB[0], aRGB[1], aRGB[2]);
  287. #else
  288. sprintf(aoResultHexRGB, "#%02hx%02hx%02hx", aRGB[0], aRGB[1], aRGB[2]);
  289. #endif
  290. /*printf("aoResultHexRGB %s\n", aoResultHexRGB);*/
  291. }
  292. else
  293. {
  294. aoResultHexRGB[0]=0;
  295. aoResultHexRGB[1]=0;
  296. aoResultHexRGB[2]=0;
  297. }
  298. }
  299. }
  300. void tfd_replaceSubStr( char const * aSource, char const * aOldSubStr,
  301. char const * aNewSubStr, char * aoDestination )
  302. {
  303. char const * pOccurence ;
  304. char const * p ;
  305. char const * lNewSubStr = "" ;
  306. size_t lOldSubLen = strlen( aOldSubStr ) ;
  307. if ( ! aSource )
  308. {
  309. * aoDestination = '\0' ;
  310. return ;
  311. }
  312. if ( ! aOldSubStr )
  313. {
  314. strcpy( aoDestination , aSource ) ;
  315. return ;
  316. }
  317. if ( aNewSubStr )
  318. {
  319. lNewSubStr = aNewSubStr ;
  320. }
  321. p = aSource ;
  322. * aoDestination = '\0' ;
  323. while ( ( pOccurence = strstr( p , aOldSubStr ) ) != NULL )
  324. {
  325. strncat( aoDestination , p , pOccurence - p ) ;
  326. strcat( aoDestination , lNewSubStr ) ;
  327. p = pOccurence + lOldSubLen ;
  328. }
  329. strcat( aoDestination , p ) ;
  330. }
  331. static int filenameValid( char const * aFileNameWithoutPath )
  332. {
  333. if ( ! aFileNameWithoutPath
  334. || ! strlen(aFileNameWithoutPath)
  335. || strpbrk(aFileNameWithoutPath , "\\/:*?\"<>|") )
  336. {
  337. return 0 ;
  338. }
  339. return 1 ;
  340. }
  341. #ifndef _WIN32
  342. static int fileExists( char const * aFilePathAndName )
  343. {
  344. FILE * lIn ;
  345. if ( ! aFilePathAndName || ! strlen(aFilePathAndName) )
  346. {
  347. return 0 ;
  348. }
  349. lIn = fopen( aFilePathAndName , "r" ) ;
  350. if ( ! lIn )
  351. {
  352. return 0 ;
  353. }
  354. fclose( lIn ) ;
  355. return 1 ;
  356. }
  357. #endif
  358. static void wipefile(char const * aFilename)
  359. {
  360. int i;
  361. struct stat st;
  362. FILE * lIn;
  363. if (stat(aFilename, &st) == 0)
  364. {
  365. if ((lIn = fopen(aFilename, "w")))
  366. {
  367. for (i = 0; i < st.st_size; i++)
  368. {
  369. fputc('A', lIn);
  370. }
  371. fclose(lIn);
  372. }
  373. }
  374. }
  375. int tfd_quoteDetected(char const * aString)
  376. {
  377. char const * p;
  378. if (!aString) return 0;
  379. p = aString;
  380. if ( strchr(p, '\''))
  381. {
  382. return 1;
  383. }
  384. if ( strchr(p, '\"'))
  385. {
  386. return 1;
  387. }
  388. if ( strchr(p, '`'))
  389. {
  390. return 1;
  391. }
  392. p = aString;
  393. while ((p = strchr(p, '$')))
  394. {
  395. p ++ ;
  396. if ( ( * p == '(' ) || ( * p == '_' ) || isalpha( * p) ) return 1 ;
  397. }
  398. return 0;
  399. }
  400. char const * tinyfd_getGlobalChar(char const * aCharVariableName) /* to be called from C# (you don't need this in C or C++) */
  401. {
  402. if (!aCharVariableName || !strlen(aCharVariableName)) return NULL;
  403. else if (!strcmp(aCharVariableName, "tinyfd_version")) return tinyfd_version;
  404. else if (!strcmp(aCharVariableName, "tinyfd_needs")) return tinyfd_needs;
  405. else if (!strcmp(aCharVariableName, "tinyfd_response")) return tinyfd_response;
  406. else return NULL ;
  407. }
  408. int tinyfd_getGlobalInt(char const * aIntVariableName) /* to be called from C# (you don't need this in C or C++) */
  409. {
  410. if ( !aIntVariableName || !strlen(aIntVariableName) ) return -1 ;
  411. else if ( !strcmp(aIntVariableName, "tinyfd_verbose") ) return tinyfd_verbose ;
  412. else if ( !strcmp(aIntVariableName, "tinyfd_silent") ) return tinyfd_silent ;
  413. else if ( !strcmp(aIntVariableName, "tinyfd_allowCursesDialogs") ) return tinyfd_allowCursesDialogs ;
  414. else if ( !strcmp(aIntVariableName, "tinyfd_forceConsole") ) return tinyfd_forceConsole ;
  415. /* else if ( !strcmp(aIntVariableName, "tinyfd_assumeGraphicDisplay") ) return tinyfd_assumeGraphicDisplay ; */
  416. #ifdef _WIN32
  417. else if ( !strcmp(aIntVariableName, "tinyfd_winUtf8") ) return tinyfd_winUtf8 ;
  418. #endif
  419. else return -1;
  420. }
  421. int tinyfd_setGlobalInt(char const * aIntVariableName, int aValue) /* to be called from C# (you don't need this in C or C++) */
  422. {
  423. if (!aIntVariableName || !strlen(aIntVariableName)) return -1 ;
  424. else if (!strcmp(aIntVariableName, "tinyfd_verbose")) { tinyfd_verbose = aValue; return tinyfd_verbose; }
  425. else if (!strcmp(aIntVariableName, "tinyfd_silent")) { tinyfd_silent = aValue; return tinyfd_silent; }
  426. else if (!strcmp(aIntVariableName, "tinyfd_allowCursesDialogs")) { tinyfd_allowCursesDialogs = aValue; return tinyfd_allowCursesDialogs; }
  427. else if (!strcmp(aIntVariableName, "tinyfd_forceConsole")) { tinyfd_forceConsole = aValue; return tinyfd_forceConsole; }
  428. /* else if (!strcmp(aIntVariableName, "tinyfd_assumeGraphicDisplay")) { tinyfd_assumeGraphicDisplay = aValue; return tinyfd_assumeGraphicDisplay; } */
  429. #ifdef _WIN32
  430. else if (!strcmp(aIntVariableName, "tinyfd_winUtf8")) { tinyfd_winUtf8 = aValue; return tinyfd_winUtf8; }
  431. #endif
  432. else return -1;
  433. }
  434. #ifdef _WIN32
  435. static int powershellPresent(void)
  436. { /*only on vista and above (or installed on xp)*/
  437. static int lPowershellPresent = -1;
  438. char lBuff[MAX_PATH_OR_CMD];
  439. FILE* lIn;
  440. char const* lString = "powershell.exe";
  441. if (lPowershellPresent < 0)
  442. {
  443. if (!(lIn = _popen("where powershell.exe", "r")))
  444. {
  445. lPowershellPresent = 0;
  446. return 0;
  447. }
  448. while (fgets(lBuff, sizeof(lBuff), lIn) != NULL)
  449. {
  450. }
  451. _pclose(lIn);
  452. if (lBuff[strlen(lBuff) - 1] == '\n')
  453. {
  454. lBuff[strlen(lBuff) - 1] = '\0';
  455. }
  456. if (strcmp(lBuff + strlen(lBuff) - strlen(lString), lString))
  457. {
  458. lPowershellPresent = 0;
  459. }
  460. else
  461. {
  462. lPowershellPresent = 1;
  463. }
  464. }
  465. return lPowershellPresent;
  466. }
  467. static int windowsVersion(void)
  468. {
  469. #if !defined(__MINGW32__) || defined(__MINGW64_VERSION_MAJOR)
  470. typedef LONG NTSTATUS ;
  471. typedef NTSTATUS(WINAPI* RtlGetVersionPtr)(PRTL_OSVERSIONINFOW);
  472. HMODULE hMod;
  473. RtlGetVersionPtr lFxPtr;
  474. RTL_OSVERSIONINFOW lRovi = { 0 };
  475. hMod = GetModuleHandleW(L"ntdll.dll");
  476. if (hMod) {
  477. lFxPtr = (RtlGetVersionPtr)GetProcAddress(hMod, "RtlGetVersion");
  478. if (lFxPtr)
  479. {
  480. lRovi.dwOSVersionInfoSize = sizeof(lRovi);
  481. if (!lFxPtr(&lRovi))
  482. {
  483. return lRovi.dwMajorVersion;
  484. }
  485. }
  486. }
  487. #endif
  488. if (powershellPresent()) return 6; /*minimum is vista or installed on xp*/
  489. return 0;
  490. }
  491. static void replaceChr(char * aString, char aOldChr, char aNewChr)
  492. {
  493. char * p;
  494. if (!aString) return;
  495. if (aOldChr == aNewChr) return;
  496. p = aString;
  497. while ((p = strchr(p, aOldChr)))
  498. {
  499. *p = aNewChr;
  500. p++;
  501. }
  502. return;
  503. }
  504. #if !defined(WC_ERR_INVALID_CHARS)
  505. /* undefined prior to Vista, so not yet in MINGW header file */
  506. #define WC_ERR_INVALID_CHARS 0x00000000 /* 0x00000080 for MINGW maybe ? */
  507. #endif
  508. static int sizeUtf16From8(char const * aUtf8string)
  509. {
  510. return MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
  511. aUtf8string, -1, NULL, 0);
  512. }
  513. static int sizeUtf16FromMbcs(char const * aMbcsString)
  514. {
  515. return MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS,
  516. aMbcsString, -1, NULL, 0);
  517. }
  518. static int sizeUtf8(wchar_t const * aUtf16string)
  519. {
  520. return WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS,
  521. aUtf16string, -1, NULL, 0, NULL, NULL);
  522. }
  523. static int sizeMbcs(wchar_t const * aMbcsString)
  524. {
  525. int lRes = WideCharToMultiByte(CP_ACP, 0,
  526. aMbcsString, -1, NULL, 0, NULL, NULL);
  527. /* DWORD licic = GetLastError(); */
  528. return lRes;
  529. }
  530. wchar_t* tinyfd_mbcsTo16(char const* aMbcsString)
  531. {
  532. static wchar_t* lMbcsString = NULL;
  533. int lSize;
  534. free(lMbcsString);
  535. if (!aMbcsString) { lMbcsString = NULL; return NULL; }
  536. lSize = sizeUtf16FromMbcs(aMbcsString);
  537. if (lSize)
  538. {
  539. lMbcsString = (wchar_t*) malloc(lSize * sizeof(wchar_t));
  540. lSize = MultiByteToWideChar(CP_ACP, 0, aMbcsString, -1, lMbcsString, lSize);
  541. }
  542. else wcscpy(lMbcsString, L"");
  543. return lMbcsString;
  544. }
  545. wchar_t * tinyfd_utf8to16(char const * aUtf8string)
  546. {
  547. static wchar_t * lUtf16string = NULL;
  548. int lSize;
  549. free(lUtf16string);
  550. if (!aUtf8string) {lUtf16string = NULL; return NULL;}
  551. lSize = sizeUtf16From8(aUtf8string);
  552. if (lSize)
  553. {
  554. lUtf16string = (wchar_t*) malloc(lSize * sizeof(wchar_t));
  555. lSize = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
  556. aUtf8string, -1, lUtf16string, lSize);
  557. return lUtf16string;
  558. }
  559. else
  560. {
  561. /* let's try mbcs anyway */
  562. lUtf16string = NULL;
  563. return tinyfd_mbcsTo16(aUtf8string);
  564. }
  565. }
  566. char * tinyfd_utf16toMbcs(wchar_t const * aUtf16string)
  567. {
  568. static char * lMbcsString = NULL;
  569. int lSize;
  570. free(lMbcsString);
  571. if (!aUtf16string) { lMbcsString = NULL; return NULL; }
  572. lSize = sizeMbcs(aUtf16string);
  573. if (lSize)
  574. {
  575. lMbcsString = (char*) malloc(lSize);
  576. lSize = WideCharToMultiByte(CP_ACP, 0, aUtf16string, -1, lMbcsString, lSize, NULL, NULL);
  577. }
  578. else strcpy(lMbcsString, "");
  579. return lMbcsString;
  580. }
  581. char * tinyfd_utf8toMbcs(char const * aUtf8string)
  582. {
  583. wchar_t const * lUtf16string;
  584. lUtf16string = tinyfd_utf8to16(aUtf8string);
  585. return tinyfd_utf16toMbcs(lUtf16string);
  586. }
  587. char * tinyfd_utf16to8(wchar_t const * aUtf16string)
  588. {
  589. static char * lUtf8string = NULL;
  590. int lSize;
  591. free(lUtf8string);
  592. if (!aUtf16string) { lUtf8string = NULL; return NULL; }
  593. lSize = sizeUtf8(aUtf16string);
  594. if (lSize)
  595. {
  596. lUtf8string = (char*) malloc(lSize);
  597. lSize = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, aUtf16string, -1, lUtf8string, lSize, NULL, NULL);
  598. }
  599. else strcpy(lUtf8string, "");
  600. return lUtf8string;
  601. }
  602. char * tinyfd_mbcsTo8(char const * aMbcsString)
  603. {
  604. wchar_t const * lUtf16string;
  605. lUtf16string = tinyfd_mbcsTo16(aMbcsString);
  606. return tinyfd_utf16to8(lUtf16string);
  607. }
  608. void tinyfd_beep(void)
  609. {
  610. if (windowsVersion() > 5) Beep(440, 300);
  611. else MessageBeep(MB_OK);
  612. }
  613. static void wipefileW(wchar_t const * aFilename)
  614. {
  615. int i;
  616. FILE * lIn;
  617. #if (defined(__MINGW32_MAJOR_VERSION) && !defined(__MINGW64__) && (__MINGW32_MAJOR_VERSION <= 3)) || defined(__BORLANDC__) || defined(__WATCOMC__)
  618. struct _stat st;
  619. if (_wstat(aFilename, &st) == 0)
  620. #else
  621. struct __stat64 st;
  622. if (_wstat64(aFilename, &st) == 0)
  623. #endif
  624. {
  625. if ((lIn = _wfopen(aFilename, L"w")))
  626. {
  627. for (i = 0; i < st.st_size; i++)
  628. {
  629. fputc('A', lIn);
  630. }
  631. fclose(lIn);
  632. }
  633. }
  634. }
  635. static wchar_t * getPathWithoutFinalSlashW(
  636. wchar_t * aoDestination, /* make sure it is allocated, use _MAX_PATH */
  637. wchar_t const * aSource) /* aoDestination and aSource can be the same */
  638. {
  639. wchar_t const * lTmp;
  640. if (aSource)
  641. {
  642. lTmp = wcsrchr(aSource, L'/');
  643. if (!lTmp)
  644. {
  645. lTmp = wcsrchr(aSource, L'\\');
  646. }
  647. if (lTmp)
  648. {
  649. wcsncpy(aoDestination, aSource, lTmp - aSource);
  650. aoDestination[lTmp - aSource] = L'\0';
  651. }
  652. else
  653. {
  654. *aoDestination = L'\0';
  655. }
  656. }
  657. else
  658. {
  659. *aoDestination = L'\0';
  660. }
  661. return aoDestination;
  662. }
  663. static wchar_t * getLastNameW(
  664. wchar_t * aoDestination, /* make sure it is allocated */
  665. wchar_t const * aSource)
  666. {
  667. /* copy the last name after '/' or '\' */
  668. wchar_t const * lTmp;
  669. if (aSource)
  670. {
  671. lTmp = wcsrchr(aSource, L'/');
  672. if (!lTmp)
  673. {
  674. lTmp = wcsrchr(aSource, L'\\');
  675. }
  676. if (lTmp)
  677. {
  678. wcscpy(aoDestination, lTmp + 1);
  679. }
  680. else
  681. {
  682. wcscpy(aoDestination, aSource);
  683. }
  684. }
  685. else
  686. {
  687. *aoDestination = L'\0';
  688. }
  689. return aoDestination;
  690. }
  691. static void Hex2RGBW(wchar_t const aHexRGB[8], unsigned char aoResultRGB[3])
  692. {
  693. wchar_t lColorChannel[8];
  694. if (aoResultRGB)
  695. {
  696. if (aHexRGB)
  697. {
  698. wcscpy(lColorChannel, aHexRGB);
  699. aoResultRGB[2] = (unsigned char)wcstoul(lColorChannel + 5, NULL, 16);
  700. lColorChannel[5] = '\0';
  701. aoResultRGB[1] = (unsigned char)wcstoul(lColorChannel + 3, NULL, 16);
  702. lColorChannel[3] = '\0';
  703. aoResultRGB[0] = (unsigned char)wcstoul(lColorChannel + 1, NULL, 16);
  704. /* printf("%d %d %d\n", aoResultRGB[0], aoResultRGB[1], aoResultRGB[2]); */
  705. }
  706. else
  707. {
  708. aoResultRGB[0] = 0;
  709. aoResultRGB[1] = 0;
  710. aoResultRGB[2] = 0;
  711. }
  712. }
  713. }
  714. static void RGB2HexW( unsigned char const aRGB[3], wchar_t aoResultHexRGB[8])
  715. {
  716. #if (defined(__cplusplus ) && __cplusplus >= 201103L) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__clang__)
  717. wchar_t const * const lPrintFormat = L"#%02hhx%02hhx%02hhx";
  718. #else
  719. wchar_t const * const lPrintFormat = L"#%02hx%02hx%02hx";
  720. #endif
  721. if (aoResultHexRGB)
  722. {
  723. if (aRGB)
  724. {
  725. /* wprintf(L"aoResultHexRGB %s\n", aoResultHexRGB); */
  726. #if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR))
  727. swprintf(aoResultHexRGB, 8, lPrintFormat, aRGB[0], aRGB[1], aRGB[2]);
  728. #else
  729. swprintf(aoResultHexRGB, lPrintFormat, aRGB[0], aRGB[1], aRGB[2]);
  730. #endif
  731. }
  732. else
  733. {
  734. aoResultHexRGB[0] = 0;
  735. aoResultHexRGB[1] = 0;
  736. aoResultHexRGB[2] = 0;
  737. }
  738. }
  739. }
  740. static int dirExists(char const * aDirPath)
  741. {
  742. #if (defined(__MINGW32_MAJOR_VERSION) && !defined(__MINGW64__) && (__MINGW32_MAJOR_VERSION <= 3)) || defined(__BORLANDC__) || defined(__WATCOMC__)
  743. struct _stat lInfo;
  744. #else
  745. struct __stat64 lInfo;
  746. #endif
  747. wchar_t * lTmpWChar;
  748. int lStatRet;
  749. size_t lDirLen;
  750. if (!aDirPath)
  751. return 0;
  752. lDirLen = strlen(aDirPath);
  753. if (!lDirLen)
  754. return 1;
  755. if ( (lDirLen == 2) && (aDirPath[1] == ':') )
  756. return 1;
  757. if (tinyfd_winUtf8)
  758. {
  759. lTmpWChar = tinyfd_utf8to16(aDirPath);
  760. #if (defined(__MINGW32_MAJOR_VERSION) && !defined(__MINGW64__) && (__MINGW32_MAJOR_VERSION <= 3)) || defined(__BORLANDC__) || defined(__WATCOMC__)
  761. lStatRet = _wstat(lTmpWChar, &lInfo);
  762. #else
  763. lStatRet = _wstat64(lTmpWChar, &lInfo);
  764. #endif
  765. if (lStatRet != 0)
  766. return 0;
  767. else if (lInfo.st_mode & S_IFDIR)
  768. return 1;
  769. else
  770. return 0;
  771. }
  772. #if (defined(__MINGW32_MAJOR_VERSION) && !defined(__MINGW64__) && (__MINGW32_MAJOR_VERSION <= 3)) || defined(__BORLANDC__) || defined(__WATCOMC__)
  773. else if (_stat(aDirPath, &lInfo) != 0)
  774. #else
  775. else if (_stat64(aDirPath, &lInfo) != 0)
  776. #endif
  777. return 0;
  778. else if (lInfo.st_mode & S_IFDIR)
  779. return 1;
  780. else
  781. return 0;
  782. }
  783. static int fileExists(char const * aFilePathAndName)
  784. {
  785. #if (defined(__MINGW32_MAJOR_VERSION) && !defined(__MINGW64__) && (__MINGW32_MAJOR_VERSION <= 3)) || defined(__BORLANDC__) || defined(__WATCOMC__)
  786. struct _stat lInfo;
  787. #else
  788. struct __stat64 lInfo;
  789. #endif
  790. wchar_t * lTmpWChar;
  791. int lStatRet;
  792. FILE * lIn;
  793. if (!aFilePathAndName || !strlen(aFilePathAndName))
  794. {
  795. return 0;
  796. }
  797. if (tinyfd_winUtf8)
  798. {
  799. lTmpWChar = tinyfd_utf8to16(aFilePathAndName);
  800. #if (defined(__MINGW32_MAJOR_VERSION) && !defined(__MINGW64__) && (__MINGW32_MAJOR_VERSION <= 3)) || defined(__BORLANDC__) || defined(__WATCOMC__)
  801. lStatRet = _wstat(lTmpWChar, &lInfo);
  802. #else
  803. lStatRet = _wstat64(lTmpWChar, &lInfo);
  804. #endif
  805. if (lStatRet != 0)
  806. return 0;
  807. else if (lInfo.st_mode & _S_IFREG)
  808. return 1;
  809. else
  810. return 0;
  811. }
  812. else
  813. {
  814. lIn = fopen(aFilePathAndName, "r");
  815. if (!lIn)
  816. {
  817. return 0;
  818. }
  819. fclose(lIn);
  820. return 1;
  821. }
  822. }
  823. static void replaceWchar(wchar_t * aString,
  824. wchar_t aOldChr,
  825. wchar_t aNewChr)
  826. {
  827. wchar_t * p;
  828. if (!aString)
  829. {
  830. return ;
  831. }
  832. if (aOldChr == aNewChr)
  833. {
  834. return ;
  835. }
  836. p = aString;
  837. while ((p = wcsrchr(p, aOldChr)))
  838. {
  839. *p = aNewChr;
  840. #ifdef TINYFD_NOCCSUNICODE
  841. p++;
  842. #endif
  843. p++;
  844. }
  845. return ;
  846. }
  847. static int quoteDetectedW(wchar_t const * aString)
  848. {
  849. wchar_t const * p;
  850. if (!aString) return 0;
  851. p = aString;
  852. while ((p = wcsrchr(p, L'\'')))
  853. {
  854. return 1;
  855. }
  856. p = aString;
  857. while ((p = wcsrchr(p, L'\"')))
  858. {
  859. return 1;
  860. }
  861. return 0;
  862. }
  863. #endif /* _WIN32 */
  864. /* source and destination can be the same or ovelap*/
  865. static char * ensureFilesExist(char * aDestination,
  866. char const * aSourcePathsAndNames)
  867. {
  868. char * lDestination = aDestination;
  869. char const * p;
  870. char const * p2;
  871. size_t lLen;
  872. if (!aSourcePathsAndNames)
  873. {
  874. return NULL;
  875. }
  876. lLen = strlen(aSourcePathsAndNames);
  877. if (!lLen)
  878. {
  879. return NULL;
  880. }
  881. p = aSourcePathsAndNames;
  882. while ((p2 = strchr(p, '|')) != NULL)
  883. {
  884. lLen = p2 - p;
  885. memmove(lDestination, p, lLen);
  886. lDestination[lLen] = '\0';
  887. if (fileExists(lDestination))
  888. {
  889. lDestination += lLen;
  890. *lDestination = '|';
  891. lDestination++;
  892. }
  893. p = p2 + 1;
  894. }
  895. if (fileExists(p))
  896. {
  897. lLen = strlen(p);
  898. memmove(lDestination, p, lLen);
  899. lDestination[lLen] = '\0';
  900. }
  901. else
  902. {
  903. *(lDestination - 1) = '\0';
  904. }
  905. return aDestination;
  906. }
  907. #ifdef _WIN32
  908. static int __stdcall EnumThreadWndProc(HWND hwnd, LPARAM lParam)
  909. {
  910. wchar_t lTitleName[MAX_PATH];
  911. wchar_t const* lDialogTitle = (wchar_t const *) lParam;
  912. GetWindowTextW(hwnd, lTitleName, MAX_PATH);
  913. /* wprintf(L"lTitleName %ls \n", lTitleName); */
  914. if (wcscmp(lDialogTitle, lTitleName) == 0)
  915. {
  916. SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
  917. return 0;
  918. }
  919. return 1;
  920. }
  921. static void hiddenConsoleW(wchar_t const * aString, wchar_t const * aDialogTitle, int aInFront)
  922. {
  923. STARTUPINFOW StartupInfo;
  924. PROCESS_INFORMATION ProcessInfo;
  925. if (!aString || !wcslen(aString) ) return;
  926. memset(&StartupInfo, 0, sizeof(StartupInfo));
  927. StartupInfo.cb = sizeof(STARTUPINFOW);
  928. StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
  929. StartupInfo.wShowWindow = SW_HIDE;
  930. if (!CreateProcessW(NULL, (LPWSTR)aString, NULL, NULL, FALSE,
  931. CREATE_NEW_CONSOLE, NULL, NULL,
  932. &StartupInfo, &ProcessInfo))
  933. {
  934. return; /* GetLastError(); */
  935. }
  936. WaitForInputIdle(ProcessInfo.hProcess, INFINITE);
  937. if (aInFront)
  938. {
  939. while (EnumWindows(EnumThreadWndProc, (LPARAM)aDialogTitle)) {}
  940. }
  941. WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
  942. CloseHandle(ProcessInfo.hThread);
  943. CloseHandle(ProcessInfo.hProcess);
  944. }
  945. int tinyfd_messageBoxW(
  946. wchar_t const * aTitle, /* NULL or "" */
  947. wchar_t const * aMessage, /* NULL or "" may contain \n and \t */
  948. wchar_t const * aDialogType, /* "ok" "okcancel" "yesno" "yesnocancel" */
  949. wchar_t const * aIconType, /* "info" "warning" "error" "question" */
  950. int aDefaultButton) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */
  951. {
  952. int lBoxReturnValue;
  953. UINT aCode;
  954. if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return 1; }
  955. /*if (quoteDetectedW(aTitle)) return tinyfd_messageBoxW(L"INVALID TITLE WITH QUOTES", aMessage, aDialogType, aIconType, aDefaultButton);
  956. if (quoteDetectedW(aMessage)) return tinyfd_messageBoxW(aTitle, L"INVALID MESSAGE WITH QUOTES", aDialogType, aIconType, aDefaultButton);*/
  957. if (aIconType && !wcscmp(L"warning", aIconType))
  958. {
  959. aCode = MB_ICONWARNING;
  960. }
  961. else if (aIconType && !wcscmp(L"error", aIconType))
  962. {
  963. aCode = MB_ICONERROR;
  964. }
  965. else if (aIconType && !wcscmp(L"question", aIconType))
  966. {
  967. aCode = MB_ICONQUESTION;
  968. }
  969. else
  970. {
  971. aCode = MB_ICONINFORMATION;
  972. }
  973. if (aDialogType && !wcscmp(L"okcancel", aDialogType))
  974. {
  975. aCode += MB_OKCANCEL;
  976. if (!aDefaultButton)
  977. {
  978. aCode += MB_DEFBUTTON2;
  979. }
  980. }
  981. else if (aDialogType && !wcscmp(L"yesno", aDialogType))
  982. {
  983. aCode += MB_YESNO;
  984. if (!aDefaultButton)
  985. {
  986. aCode += MB_DEFBUTTON2;
  987. }
  988. }
  989. else if (aDialogType && !wcscmp(L"yesnocancel", aDialogType))
  990. {
  991. aCode += MB_YESNOCANCEL;
  992. if (aDefaultButton == 1)
  993. {
  994. aCode += MB_DEFBUTTON1;
  995. }
  996. else if (aDefaultButton == 2)
  997. {
  998. aCode += MB_DEFBUTTON2;
  999. }
  1000. else
  1001. {
  1002. aCode += MB_DEFBUTTON3;
  1003. }
  1004. }
  1005. else
  1006. {
  1007. aCode += MB_OK;
  1008. }
  1009. aCode += MB_TOPMOST;
  1010. lBoxReturnValue = MessageBoxW(GetForegroundWindow(), aMessage, aTitle, aCode);
  1011. if ( (lBoxReturnValue == IDNO) && (aDialogType && !wcscmp(L"yesnocancel", aDialogType)) )
  1012. {
  1013. return 2;
  1014. }
  1015. else if ( (lBoxReturnValue == IDOK) || (lBoxReturnValue == IDYES) )
  1016. {
  1017. return 1;
  1018. }
  1019. else
  1020. {
  1021. return 0;
  1022. }
  1023. }
  1024. /* int tinyfd_notifyPopupW_ORIGINAL(
  1025. wchar_t const * aTitle,
  1026. wchar_t const * aMessage,
  1027. wchar_t const * aIconType)
  1028. {
  1029. wchar_t * lDialogString;
  1030. size_t lTitleLen;
  1031. size_t lMessageLen;
  1032. size_t lDialogStringLen;
  1033. if (aTitle && !wcscmp(aTitle, L"tinyfd_query")) { strcpy(tinyfd_response, "windows_wchar"); return 1; }
  1034. if (quoteDetectedW(aTitle)) return tinyfd_notifyPopupW(L"INVALID TITLE WITH QUOTES", aMessage, aIconType);
  1035. if (quoteDetectedW(aMessage)) return tinyfd_notifyPopupW(aTitle, L"INVALID MESSAGE WITH QUOTES", aIconType);
  1036. lTitleLen = aTitle ? wcslen(aTitle) : 0;
  1037. lMessageLen = aMessage ? wcslen(aMessage) : 0;
  1038. lDialogStringLen = 3 * MAX_PATH_OR_CMD + lTitleLen + lMessageLen;
  1039. lDialogString = (wchar_t *) malloc(2 * lDialogStringLen);
  1040. if (!lDialogString) return 0;
  1041. wcscpy(lDialogString, L"powershell.exe -executionpolicy bypass -command \"\
  1042. function Show-BalloonTip {\
  1043. [cmdletbinding()] \
  1044. param( \
  1045. [string]$Title = ' ', \
  1046. [string]$Message = ' ', \
  1047. [ValidateSet('info', 'warning', 'error')] \
  1048. [string]$IconType = 'info');\
  1049. [system.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms') | Out-Null ; \
  1050. $balloon = New-Object System.Windows.Forms.NotifyIcon ; \
  1051. $path = Get-Process -id $pid | Select-Object -ExpandProperty Path ; \
  1052. $icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path) ;");
  1053. wcscat(lDialogString, L"\
  1054. $balloon.Icon = $icon ; \
  1055. $balloon.BalloonTipIcon = $IconType ; \
  1056. $balloon.BalloonTipText = $Message ; \
  1057. $balloon.BalloonTipTitle = $Title ; \
  1058. $balloon.Text = 'tinyfiledialogs' ; \
  1059. $balloon.Visible = $true ; \
  1060. $balloon.ShowBalloonTip(5000)};\
  1061. Show-BalloonTip");
  1062. if (aTitle && wcslen(aTitle))
  1063. {
  1064. wcscat(lDialogString, L" -Title '");
  1065. wcscat(lDialogString, aTitle);
  1066. wcscat(lDialogString, L"'");
  1067. }
  1068. if (aMessage && wcslen(aMessage))
  1069. {
  1070. wcscat(lDialogString, L" -Message '");
  1071. wcscat(lDialogString, aMessage);
  1072. wcscat(lDialogString, L"'");
  1073. }
  1074. if (aMessage && wcslen(aIconType))
  1075. {
  1076. wcscat(lDialogString, L" -IconType '");
  1077. wcscat(lDialogString, aIconType);
  1078. wcscat(lDialogString, L"'");
  1079. }
  1080. wcscat(lDialogString, L"\"");
  1081. hiddenConsoleW(lDialogString, aTitle, 0);
  1082. free(lDialogString);
  1083. return 1;
  1084. }*/
  1085. /* return has only meaning for tinyfd_query */
  1086. int tinyfd_notifyPopupW(
  1087. wchar_t const* aTitle, /* NULL or L"" */
  1088. wchar_t const* aMessage, /* NULL or L"" may contain \n \t */
  1089. wchar_t const* aIconType) /* L"info" L"warning" L"error" */
  1090. {
  1091. wchar_t* lDialogString;
  1092. size_t lTitleLen;
  1093. size_t lMessageLen;
  1094. size_t lDialogStringLen;
  1095. FILE* lIn;
  1096. if (aTitle && !wcscmp(aTitle, L"tinyfd_query")) { strcpy(tinyfd_response, "windows_wchar"); return 1; }
  1097. if (quoteDetectedW(aTitle)) return tinyfd_notifyPopupW(L"INVALID TITLE WITH QUOTES", aMessage, aIconType);
  1098. if (quoteDetectedW(aMessage)) return tinyfd_notifyPopupW(aTitle, L"INVALID MESSAGE WITH QUOTES", aIconType);
  1099. lTitleLen = aTitle ? wcslen(aTitle) : 0;
  1100. lMessageLen = aMessage ? wcslen(aMessage) : 0;
  1101. lDialogStringLen = 3 * MAX_PATH_OR_CMD + lTitleLen + lMessageLen;
  1102. lDialogString = (wchar_t*)malloc(2 * lDialogStringLen);
  1103. if (!lDialogString) return 0;
  1104. swprintf(lDialogString,
  1105. #if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR))
  1106. lDialogStringLen,
  1107. #endif
  1108. L"%ls\\tinyfd.hta", _wgetenv(L"TEMP"));
  1109. lIn = _wfopen(lDialogString, L"w");
  1110. if (!lIn)
  1111. {
  1112. free(lDialogString);
  1113. return 0;
  1114. }
  1115. wcscpy(lDialogString, L"\n\
  1116. <html>\n\
  1117. <head>\n\
  1118. <title>");
  1119. if ( aTitle && wcslen(aTitle) ) wcscat(lDialogString, aTitle);
  1120. wcscat(lDialogString, L"</title>\n\
  1121. </head>\n\
  1122. <HTA:APPLICATION\n\
  1123. SysMenu = 'no'\n\
  1124. ID = 'tinyfdHTA'\n\
  1125. APPLICATIONNAME = 'tinyfd_notifyPopup'\n\
  1126. MINIMIZEBUTTON = 'no'\n\
  1127. MAXIMIZEBUTTON = 'no'\n\
  1128. BORDER = 'dialog'\n\
  1129. SCROLL = 'no'\n\
  1130. SINGLEINSTANCE = 'yes'\n\
  1131. WINDOWSTATE = 'hidden'>\n\
  1132. <script language = 'VBScript'>\n\
  1133. intWidth = Screen.Width/4\n\
  1134. intHeight = Screen.Height/10\n\
  1135. ResizeTo intWidth, intHeight\n\
  1136. MoveTo Screen.Width * .7, Screen.Height * .8\n\
  1137. result = 0\n\
  1138. Sub Window_onLoad\n\
  1139. idTimer = window.setTimeout(\"PausedSection\", 3000, \"VBScript\")\n\
  1140. End Sub\n");
  1141. wcscat(lDialogString, L"\n\
  1142. Sub PausedSection\n\
  1143. window.Close\n\
  1144. End Sub\n\
  1145. </script>\n\
  1146. <body style = 'background-color:#EEEEEE' onkeypress = 'vbs:Default_Buttons' align = 'top'>\n\
  1147. <table width = '100%' height = '80%' align = 'center' border = '0'>\n\
  1148. <tr border = '0'>\n\
  1149. <td align = 'left' valign = 'middle' style='Font-Family:Arial'>\n");
  1150. wcscat(lDialogString, aMessage ? aMessage : L"");
  1151. wcscat(lDialogString, L"\n\
  1152. </body>\n\
  1153. </html>\n\
  1154. ");
  1155. fputws(lDialogString, lIn);
  1156. fclose(lIn);
  1157. if (aTitle && wcslen(aTitle))
  1158. {
  1159. wcscat(lDialogString, L" -Title '");
  1160. wcscat(lDialogString, aTitle);
  1161. wcscat(lDialogString, L"'");
  1162. }
  1163. if (aMessage && wcslen(aMessage))
  1164. {
  1165. wcscat(lDialogString, L" -Message '");
  1166. wcscat(lDialogString, aMessage);
  1167. wcscat(lDialogString, L"'");
  1168. }
  1169. if (aMessage && wcslen(aIconType))
  1170. {
  1171. wcscat(lDialogString, L" -IconType '");
  1172. wcscat(lDialogString, aIconType);
  1173. wcscat(lDialogString, L"'");
  1174. }
  1175. wcscat(lDialogString, L"\"");
  1176. /* wprintf ( L"lDialogString: %ls\n" , lDialogString ) ; */
  1177. wcscpy(lDialogString,
  1178. L"cmd.exe /c mshta.exe \"%TEMP%\\tinyfd.hta\"");
  1179. hiddenConsoleW(lDialogString, aTitle, 0);
  1180. free(lDialogString);
  1181. return 1;
  1182. }
  1183. wchar_t * tinyfd_inputBoxW(
  1184. wchar_t const * aTitle, /* NULL or L"" */
  1185. wchar_t const * aMessage, /* NULL or L"" (\n and \t have no effect) */
  1186. wchar_t const * aDefaultInput) /* L"" , if NULL it's a passwordBox */
  1187. {
  1188. static wchar_t lBuff[MAX_PATH_OR_CMD];
  1189. wchar_t * lDialogString;
  1190. FILE * lIn;
  1191. FILE * lFile;
  1192. int lResult;
  1193. size_t lTitleLen;
  1194. size_t lMessageLen;
  1195. size_t lDialogStringLen;
  1196. if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return (wchar_t *)1; }
  1197. if (quoteDetectedW(aTitle)) return tinyfd_inputBoxW(L"INVALID TITLE WITH QUOTES", aMessage, aDefaultInput);
  1198. if (quoteDetectedW(aMessage)) return tinyfd_inputBoxW(aTitle, L"INVALID MESSAGE WITH QUOTES", aDefaultInput);
  1199. if (quoteDetectedW(aDefaultInput)) return tinyfd_inputBoxW(aTitle, aMessage, L"INVALID DEFAULT_INPUT WITH QUOTES: use the GRAVE ACCENT \\x60 instead.");
  1200. lTitleLen = aTitle ? wcslen(aTitle) : 0 ;
  1201. lMessageLen = aMessage ? wcslen(aMessage) : 0 ;
  1202. lDialogStringLen = 3 * MAX_PATH_OR_CMD + lTitleLen + lMessageLen;
  1203. lDialogString = (wchar_t *) malloc(2 * lDialogStringLen);
  1204. if (aDefaultInput)
  1205. {
  1206. swprintf(lDialogString,
  1207. #if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR))
  1208. lDialogStringLen,
  1209. #endif
  1210. L"%ls\\tinyfd.vbs", _wgetenv(L"TEMP"));
  1211. }
  1212. else
  1213. {
  1214. swprintf(lDialogString,
  1215. #if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR))
  1216. lDialogStringLen,
  1217. #endif
  1218. L"%ls\\tinyfd.hta", _wgetenv(L"TEMP"));
  1219. }
  1220. lIn = _wfopen(lDialogString, L"w");
  1221. if (!lIn)
  1222. {
  1223. free(lDialogString);
  1224. return NULL;
  1225. }
  1226. if ( aDefaultInput )
  1227. {
  1228. wcscpy(lDialogString, L"Dim result:result=InputBox(\"");
  1229. if (aMessage && wcslen(aMessage))
  1230. {
  1231. wcscpy(lBuff, aMessage);
  1232. replaceWchar(lBuff, L'\n', L' ');
  1233. wcscat(lDialogString, lBuff);
  1234. }
  1235. wcscat(lDialogString, L"\",\"");
  1236. if (aTitle) wcscat(lDialogString, aTitle);
  1237. wcscat(lDialogString, L"\",\"");
  1238. if (aDefaultInput && wcslen(aDefaultInput))
  1239. {
  1240. wcscpy(lBuff, aDefaultInput);
  1241. replaceWchar(lBuff, L'\n', L' ');
  1242. wcscat(lDialogString, lBuff);
  1243. }
  1244. wcscat(lDialogString, L"\"):If IsEmpty(result) then:WScript.Echo 0");
  1245. wcscat(lDialogString, L":Else: WScript.Echo \"1\" & result : End If");
  1246. }
  1247. else
  1248. {
  1249. wcscpy(lDialogString, L"\n\
  1250. <html>\n\
  1251. <head>\n\
  1252. <title>");
  1253. if (aTitle) wcscat(lDialogString, aTitle);
  1254. wcscat(lDialogString, L"</title>\n\
  1255. </head>\n\
  1256. <HTA:APPLICATION\n\
  1257. ID = 'tinyfdHTA'\n\
  1258. APPLICATIONNAME = 'tinyfd_inputBox'\n\
  1259. MINIMIZEBUTTON = 'no'\n\
  1260. MAXIMIZEBUTTON = 'no'\n\
  1261. BORDER = 'dialog'\n\
  1262. SCROLL = 'no'\n\
  1263. SINGLEINSTANCE = 'yes'\n\
  1264. WINDOWSTATE = 'hidden'>\n\
  1265. \n\
  1266. <script language = 'VBScript'>\n\
  1267. \n\
  1268. intWidth = Screen.Width/4\n\
  1269. intHeight = Screen.Height/6\n\
  1270. ResizeTo intWidth, intHeight\n\
  1271. MoveTo((Screen.Width/2)-(intWidth/2)),((Screen.Height/2)-(intHeight/2))\n\
  1272. result = 0\n\
  1273. \n\
  1274. Sub Window_onLoad\n\
  1275. txt_input.Focus\n\
  1276. End Sub\n\
  1277. \n");
  1278. wcscat(lDialogString, L"\
  1279. Sub Window_onUnload\n\
  1280. Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n\
  1281. Set oShell = CreateObject(\"WScript.Shell\")\n\
  1282. strTempFolder = oShell.ExpandEnvironmentStrings(\"%TEMP%\")\n\
  1283. Set objFile = objFSO.CreateTextFile(strTempFolder & \"\\tinyfd.txt\",True,True)\n\
  1284. If result = 1 Then\n\
  1285. objFile.Write 1 & txt_input.Value\n\
  1286. Else\n\
  1287. objFile.Write 0\n\
  1288. End If\n\
  1289. objFile.Close\n\
  1290. End Sub\n\
  1291. \n\
  1292. Sub Run_ProgramOK\n\
  1293. result = 1\n\
  1294. window.Close\n\
  1295. End Sub\n\
  1296. \n\
  1297. Sub Run_ProgramCancel\n\
  1298. window.Close\n\
  1299. End Sub\n\
  1300. \n");
  1301. wcscat(lDialogString, L"Sub Default_Buttons\n\
  1302. If Window.Event.KeyCode = 13 Then\n\
  1303. btn_OK.Click\n\
  1304. ElseIf Window.Event.KeyCode = 27 Then\n\
  1305. btn_Cancel.Click\n\
  1306. End If\n\
  1307. End Sub\n\
  1308. \n\
  1309. </script>\n\
  1310. <body style = 'background-color:#EEEEEE' onkeypress = 'vbs:Default_Buttons' align = 'top'>\n\
  1311. <table width = '100%' height = '80%' align = 'center' border = '0'>\n\
  1312. <tr border = '0'>\n\
  1313. <td align = 'left' valign = 'middle' style='Font-Family:Arial'>\n");
  1314. wcscat(lDialogString, aMessage ? aMessage : L"");
  1315. wcscat(lDialogString, L"\n\
  1316. </td>\n\
  1317. <td align = 'right' valign = 'middle' style = 'margin-top: 0em'>\n\
  1318. <table align = 'right' style = 'margin-right: 0em;'>\n\
  1319. <tr align = 'right' style = 'margin-top: 5em;'>\n\
  1320. <input type = 'button' value = 'OK' name = 'btn_OK' onClick = 'vbs:Run_ProgramOK' style = 'width: 5em; margin-top: 2em;'><br>\n\
  1321. <input type = 'button' value = 'Cancel' name = 'btn_Cancel' onClick = 'vbs:Run_ProgramCancel' style = 'width: 5em;'><br><br>\n\
  1322. </tr>\n\
  1323. </table>\n\
  1324. </td>\n\
  1325. </tr>\n\
  1326. </table>\n");
  1327. wcscat(lDialogString, L"<table width = '100%' height = '100%' align = 'center' border = '0'>\n\
  1328. <tr>\n\
  1329. <td align = 'left' valign = 'top'>\n\
  1330. <input type = 'password' id = 'txt_input'\n\
  1331. name = 'txt_input' value = '' style = 'float:left;width:100%' ><BR>\n\
  1332. </td>\n\
  1333. </tr>\n\
  1334. </table>\n\
  1335. </body>\n\
  1336. </html>\n\
  1337. " ) ;
  1338. }
  1339. fputws(lDialogString, lIn);
  1340. fclose(lIn);
  1341. if (aDefaultInput)
  1342. {
  1343. swprintf(lDialogString,
  1344. #if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR))
  1345. lDialogStringLen,
  1346. #endif
  1347. L"%ls\\tinyfd.txt",_wgetenv(L"TEMP"));
  1348. #ifdef TINYFD_NOCCSUNICODE
  1349. lFile = _wfopen(lDialogString, L"w");
  1350. fputc(0xFF, lFile);
  1351. fputc(0xFE, lFile);
  1352. #else
  1353. lFile = _wfopen(lDialogString, L"wt, ccs=UNICODE"); /*or ccs=UTF-16LE*/
  1354. #endif
  1355. fclose(lFile);
  1356. wcscpy(lDialogString, L"cmd.exe /c cscript.exe //U //Nologo ");
  1357. wcscat(lDialogString, L"\"%TEMP%\\tinyfd.vbs\" ");
  1358. wcscat(lDialogString, L">> \"%TEMP%\\tinyfd.txt\"");
  1359. }
  1360. else
  1361. {
  1362. wcscpy(lDialogString,
  1363. L"cmd.exe /c mshta.exe \"%TEMP%\\tinyfd.hta\"");
  1364. }
  1365. /* wprintf ( "lDialogString: %ls\n" , lDialogString ) ; */
  1366. hiddenConsoleW(lDialogString, aTitle, 1);
  1367. swprintf(lDialogString,
  1368. #if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR))
  1369. lDialogStringLen,
  1370. #endif
  1371. L"%ls\\tinyfd.txt", _wgetenv(L"TEMP"));
  1372. /* wprintf(L"lDialogString: %ls\n", lDialogString); */
  1373. #ifdef TINYFD_NOCCSUNICODE
  1374. if (!(lIn = _wfopen(lDialogString, L"r")))
  1375. #else
  1376. if (!(lIn = _wfopen(lDialogString, L"rt, ccs=UNICODE"))) /*or ccs=UTF-16LE*/
  1377. #endif
  1378. {
  1379. _wremove(lDialogString);
  1380. free(lDialogString);
  1381. return NULL;
  1382. }
  1383. memset(lBuff, 0, MAX_PATH_OR_CMD * sizeof(wchar_t) );
  1384. #ifdef TINYFD_NOCCSUNICODE
  1385. fgets((char *)lBuff, 2*MAX_PATH_OR_CMD, lIn);
  1386. #else
  1387. fgetws(lBuff, MAX_PATH_OR_CMD, lIn);
  1388. #endif
  1389. fclose(lIn);
  1390. wipefileW(lDialogString);
  1391. _wremove(lDialogString);
  1392. if (aDefaultInput)
  1393. {
  1394. swprintf(lDialogString,
  1395. #if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR))
  1396. lDialogStringLen,
  1397. #endif
  1398. L"%ls\\tinyfd.vbs", _wgetenv(L"TEMP"));
  1399. }
  1400. else
  1401. {
  1402. swprintf(lDialogString,
  1403. #if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR))
  1404. lDialogStringLen,
  1405. #endif
  1406. L"%ls\\tinyfd.hta", _wgetenv(L"TEMP"));
  1407. }
  1408. _wremove(lDialogString);
  1409. free(lDialogString);
  1410. /* wprintf( L"lBuff: %ls\n" , lBuff ) ; */
  1411. #ifdef TINYFD_NOCCSUNICODE
  1412. lResult = !wcsncmp(lBuff+1, L"1", 1);
  1413. #else
  1414. lResult = !wcsncmp(lBuff, L"1", 1);
  1415. #endif
  1416. /* printf( "lResult: %d \n" , lResult ) ; */
  1417. if (!lResult)
  1418. {
  1419. return NULL ;
  1420. }
  1421. /* wprintf( "lBuff+1: %ls\n" , lBuff+1 ) ; */
  1422. #ifdef TINYFD_NOCCSUNICODE
  1423. if (aDefaultInput)
  1424. {
  1425. lDialogStringLen = wcslen(lBuff) ;
  1426. lBuff[lDialogStringLen - 1] = L'\0';
  1427. lBuff[lDialogStringLen - 2] = L'\0';
  1428. }
  1429. return lBuff + 2;
  1430. #else
  1431. if (aDefaultInput) lBuff[wcslen(lBuff) - 1] = L'\0';
  1432. return lBuff + 1;
  1433. #endif
  1434. }
  1435. wchar_t * tinyfd_saveFileDialogW(
  1436. wchar_t const * aTitle, /* NULL or "" */
  1437. wchar_t const * aDefaultPathAndOrFile, /* NULL or "" */
  1438. int aNumOfFilterPatterns, /* 0 */
  1439. wchar_t const * const * aFilterPatterns, /* NULL or {"*.jpg","*.png"} */
  1440. wchar_t const * aSingleFilterDescription) /* NULL or "image files" */
  1441. {
  1442. static wchar_t lBuff[MAX_PATH_OR_CMD];
  1443. wchar_t lDirname[MAX_PATH_OR_CMD];
  1444. wchar_t lDialogString[MAX_PATH_OR_CMD];
  1445. wchar_t lFilterPatterns[MAX_PATH_OR_CMD] = L"";
  1446. wchar_t * p;
  1447. wchar_t * lRetval;
  1448. wchar_t const * ldefExt = NULL;
  1449. int i;
  1450. HRESULT lHResult;
  1451. OPENFILENAMEW ofn = {0};
  1452. if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return (wchar_t *)1; }
  1453. /*if (quoteDetectedW(aTitle)) return tinyfd_saveFileDialogW(L"INVALID TITLE WITH QUOTES", aDefaultPathAndOrFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription);
  1454. if (quoteDetectedW(aDefaultPathAndOrFile)) return tinyfd_saveFileDialogW(aTitle, L"INVALID DEFAULT_PATH WITH QUOTES", aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription);
  1455. if (quoteDetectedW(aSingleFilterDescription)) return tinyfd_saveFileDialogW(aTitle, aDefaultPathAndOrFile, aNumOfFilterPatterns, aFilterPatterns, L"INVALID FILTER_DESCRIPTION WITH QUOTES");
  1456. for (i = 0; i < aNumOfFilterPatterns; i++)
  1457. {
  1458. if (quoteDetectedW(aFilterPatterns[i])) return tinyfd_saveFileDialogW(L"INVALID FILTER_PATTERN WITH QUOTES: use the GRAVE ACCENT \\x60 instead.", aDefaultPathAndOrFile, 0, NULL, NULL);
  1459. }*/
  1460. lHResult = CoInitializeEx(NULL, 0);
  1461. getPathWithoutFinalSlashW(lDirname, aDefaultPathAndOrFile);
  1462. getLastNameW(lBuff, aDefaultPathAndOrFile);
  1463. if (aNumOfFilterPatterns > 0)
  1464. {
  1465. ldefExt = aFilterPatterns[0];
  1466. if (aSingleFilterDescription && wcslen(aSingleFilterDescription))
  1467. {
  1468. wcscpy(lFilterPatterns, aSingleFilterDescription);
  1469. wcscat(lFilterPatterns, L"\n");
  1470. }
  1471. wcscat(lFilterPatterns, aFilterPatterns[0]);
  1472. for (i = 1; i < aNumOfFilterPatterns; i++)
  1473. {
  1474. wcscat(lFilterPatterns, L";");
  1475. wcscat(lFilterPatterns, aFilterPatterns[i]);
  1476. }
  1477. wcscat(lFilterPatterns, L"\n");
  1478. if (!(aSingleFilterDescription && wcslen(aSingleFilterDescription)))
  1479. {
  1480. wcscpy(lDialogString, lFilterPatterns);
  1481. wcscat(lFilterPatterns, lDialogString);
  1482. }
  1483. wcscat(lFilterPatterns, L"All Files\n*.*\n");
  1484. p = lFilterPatterns;
  1485. while ((p = wcschr(p, L'\n')) != NULL)
  1486. {
  1487. *p = L'\0';
  1488. p++;
  1489. }
  1490. }
  1491. ofn.lStructSize = sizeof(OPENFILENAMEW);
  1492. ofn.hwndOwner = GetForegroundWindow();
  1493. ofn.hInstance = 0;
  1494. ofn.lpstrFilter = wcslen(lFilterPatterns) ? lFilterPatterns : NULL;
  1495. ofn.lpstrCustomFilter = NULL;
  1496. ofn.nMaxCustFilter = 0;
  1497. ofn.nFilterIndex = 1;
  1498. ofn.lpstrFile = lBuff;
  1499. ofn.nMaxFile = MAX_PATH_OR_CMD;
  1500. ofn.lpstrFileTitle = NULL;
  1501. ofn.nMaxFileTitle = MAX_PATH_OR_CMD/2;
  1502. ofn.lpstrInitialDir = wcslen(lDirname) ? lDirname : NULL;
  1503. ofn.lpstrTitle = aTitle && wcslen(aTitle) ? aTitle : NULL;
  1504. ofn.Flags = OFN_OVERWRITEPROMPT | OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST ;
  1505. ofn.nFileOffset = 0;
  1506. ofn.nFileExtension = 0;
  1507. ofn.lpstrDefExt = ldefExt;
  1508. ofn.lCustData = 0L;
  1509. ofn.lpfnHook = NULL;
  1510. ofn.lpTemplateName = NULL;
  1511. if (GetSaveFileNameW(&ofn) == 0)
  1512. {
  1513. lRetval = NULL;
  1514. }
  1515. else
  1516. {
  1517. lRetval = lBuff;
  1518. }
  1519. if (lHResult == S_OK || lHResult == S_FALSE)
  1520. {
  1521. CoUninitialize();
  1522. }
  1523. return lRetval;
  1524. }
  1525. wchar_t * tinyfd_openFileDialogW(
  1526. wchar_t const * aTitle, /* NULL or "" */
  1527. wchar_t const * aDefaultPathAndOrFile, /* NULL or "" */
  1528. int aNumOfFilterPatterns, /* 0 */
  1529. wchar_t const * const * aFilterPatterns, /* NULL or {"*.jpg","*.png"} */
  1530. wchar_t const * aSingleFilterDescription, /* NULL or "image files" */
  1531. int aAllowMultipleSelects) /* 0 or 1 ; -1 to free allocated memory and return */
  1532. {
  1533. size_t lLengths[MAX_MULTIPLE_FILES];
  1534. wchar_t lDirname[MAX_PATH_OR_CMD];
  1535. wchar_t lFilterPatterns[MAX_PATH_OR_CMD] = L"";
  1536. wchar_t lDialogString[MAX_PATH_OR_CMD];
  1537. wchar_t * lPointers[MAX_MULTIPLE_FILES+1];
  1538. wchar_t * p;
  1539. int i, j;
  1540. size_t lBuffLen;
  1541. DWORD lFullBuffLen;
  1542. HRESULT lHResult;
  1543. OPENFILENAMEW ofn = { 0 };
  1544. static wchar_t * lBuff = NULL;
  1545. free(lBuff);
  1546. lBuff = NULL;
  1547. if (aAllowMultipleSelects < 0) return (wchar_t *)0;
  1548. if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return (wchar_t *)1; }
  1549. /*if (quoteDetectedW(aTitle)) return tinyfd_openFileDialogW(L"INVALID TITLE WITH QUOTES", aDefaultPathAndOrFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects);
  1550. if (quoteDetectedW(aDefaultPathAndOrFile)) return tinyfd_openFileDialogW(aTitle, L"INVALID DEFAULT_PATH WITH QUOTES", aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects);
  1551. if (quoteDetectedW(aSingleFilterDescription)) return tinyfd_openFileDialogW(aTitle, aDefaultPathAndOrFile, aNumOfFilterPatterns, aFilterPatterns, L"INVALID FILTER_DESCRIPTION WITH QUOTES", aAllowMultipleSelects);
  1552. for (i = 0; i < aNumOfFilterPatterns; i++)
  1553. {
  1554. if (quoteDetectedW(aFilterPatterns[i])) return tinyfd_openFileDialogW(L"INVALID FILTER_PATTERN WITH QUOTES: use the GRAVE ACCENT \\x60 instead.", aDefaultPathAndOrFile, 0, NULL, NULL, aAllowMultipleSelects);
  1555. }*/
  1556. if (aAllowMultipleSelects)
  1557. {
  1558. lFullBuffLen = MAX_MULTIPLE_FILES * MAX_PATH_OR_CMD + 1;
  1559. lBuff = (wchar_t*) malloc(lFullBuffLen * sizeof(wchar_t));
  1560. if (!lBuff)
  1561. {
  1562. lFullBuffLen = LOW_MULTIPLE_FILES * MAX_PATH_OR_CMD + 1;
  1563. lBuff = (wchar_t*) malloc( lFullBuffLen * sizeof(wchar_t));
  1564. }
  1565. }
  1566. else
  1567. {
  1568. lFullBuffLen = MAX_PATH_OR_CMD + 1;
  1569. lBuff = (wchar_t*) malloc(lFullBuffLen * sizeof(wchar_t));
  1570. }
  1571. if (!lBuff) return NULL;
  1572. lHResult = CoInitializeEx(NULL, 0);
  1573. getPathWithoutFinalSlashW(lDirname, aDefaultPathAndOrFile);
  1574. getLastNameW(lBuff, aDefaultPathAndOrFile);
  1575. if (aNumOfFilterPatterns > 0)
  1576. {
  1577. if (aSingleFilterDescription && wcslen(aSingleFilterDescription))
  1578. {
  1579. wcscpy(lFilterPatterns, aSingleFilterDescription);
  1580. wcscat(lFilterPatterns, L"\n");
  1581. }
  1582. wcscat(lFilterPatterns, aFilterPatterns[0]);
  1583. for (i = 1; i < aNumOfFilterPatterns; i++)
  1584. {
  1585. wcscat(lFilterPatterns, L";");
  1586. wcscat(lFilterPatterns, aFilterPatterns[i]);
  1587. }
  1588. wcscat(lFilterPatterns, L"\n");
  1589. if (!(aSingleFilterDescription && wcslen(aSingleFilterDescription)))
  1590. {
  1591. wcscpy(lDialogString, lFilterPatterns);
  1592. wcscat(lFilterPatterns, lDialogString);
  1593. }
  1594. wcscat(lFilterPatterns, L"All Files\n*.*\n");
  1595. p = lFilterPatterns;
  1596. while ((p = wcschr(p, L'\n')) != NULL)
  1597. {
  1598. *p = L'\0';
  1599. p++;
  1600. }
  1601. }
  1602. ofn.lStructSize = sizeof(OPENFILENAME);
  1603. ofn.hwndOwner = GetForegroundWindow();
  1604. ofn.hInstance = 0;
  1605. ofn.lpstrFilter = wcslen(lFilterPatterns) ? lFilterPatterns : NULL;
  1606. ofn.lpstrCustomFilter = NULL;
  1607. ofn.nMaxCustFilter = 0;
  1608. ofn.nFilterIndex = 1;
  1609. ofn.lpstrFile = lBuff;
  1610. ofn.nMaxFile = lFullBuffLen;
  1611. ofn.lpstrFileTitle = NULL;
  1612. ofn.nMaxFileTitle = MAX_PATH_OR_CMD / 2;
  1613. ofn.lpstrInitialDir = wcslen(lDirname) ? lDirname : NULL;
  1614. ofn.lpstrTitle = aTitle && wcslen(aTitle) ? aTitle : NULL;
  1615. ofn.Flags = OFN_EXPLORER | OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  1616. ofn.nFileOffset = 0;
  1617. ofn.nFileExtension = 0;
  1618. ofn.lpstrDefExt = NULL;
  1619. ofn.lCustData = 0L;
  1620. ofn.lpfnHook = NULL;
  1621. ofn.lpTemplateName = NULL;
  1622. if (aAllowMultipleSelects)
  1623. {
  1624. ofn.Flags |= OFN_ALLOWMULTISELECT;
  1625. }
  1626. if (GetOpenFileNameW(&ofn) == 0)
  1627. {
  1628. free(lBuff);
  1629. lBuff = NULL;
  1630. }
  1631. else
  1632. {
  1633. lBuffLen = wcslen(lBuff);
  1634. lPointers[0] = lBuff + lBuffLen + 1;
  1635. if (aAllowMultipleSelects && (lPointers[0][0] != L'\0'))
  1636. {
  1637. i = 0;
  1638. do
  1639. {
  1640. lLengths[i] = wcslen(lPointers[i]);
  1641. lPointers[i + 1] = lPointers[i] + lLengths[i] + 1;
  1642. i++;
  1643. } while (lPointers[i][0] != L'\0' && i < MAX_MULTIPLE_FILES );
  1644. if (i > MAX_MULTIPLE_FILES)
  1645. {
  1646. free(lBuff);
  1647. lBuff = NULL;
  1648. }
  1649. else
  1650. {
  1651. i--;
  1652. p = lBuff + lFullBuffLen - 1;
  1653. *p = L'\0';
  1654. for (j = i; j >= 0; j--)
  1655. {
  1656. p -= lLengths[j];
  1657. memmove(p, lPointers[j], lLengths[j] * sizeof(wchar_t));
  1658. p--;
  1659. *p = L'\\';
  1660. p -= lBuffLen;
  1661. memmove(p, lBuff, lBuffLen*sizeof(wchar_t));
  1662. p--;
  1663. *p = L'|';
  1664. }
  1665. p++;
  1666. wcscpy(lBuff, p);
  1667. lBuffLen = wcslen(lBuff);
  1668. }
  1669. }
  1670. if (lBuff) lBuff = (wchar_t*)(realloc(lBuff, (lBuffLen + 1) * sizeof(wchar_t)));
  1671. }
  1672. if (lHResult == S_OK || lHResult == S_FALSE)
  1673. {
  1674. CoUninitialize();
  1675. }
  1676. return lBuff;
  1677. }
  1678. BOOL CALLBACK BrowseCallbackProcW_enum(HWND hWndChild, LPARAM lParam)
  1679. {
  1680. wchar_t buf[255];
  1681. (void)lParam;
  1682. GetClassNameW(hWndChild, buf, sizeof(buf));
  1683. if (wcscmp(buf, L"SysTreeView32") == 0)
  1684. {
  1685. HTREEITEM hNode = TreeView_GetSelection(hWndChild);
  1686. TreeView_EnsureVisible(hWndChild, hNode);
  1687. return FALSE;
  1688. }
  1689. return TRUE;
  1690. }
  1691. static int __stdcall BrowseCallbackProcW(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData)
  1692. {
  1693. (void)lp;
  1694. switch (uMsg)
  1695. {
  1696. case BFFM_INITIALIZED:
  1697. SendMessage(hwnd, BFFM_SETSELECTIONW, TRUE, (LPARAM)pData);
  1698. break;
  1699. case BFFM_SELCHANGED:
  1700. EnumChildWindows(hwnd, BrowseCallbackProcW_enum, 0);
  1701. }
  1702. return 0;
  1703. }
  1704. wchar_t * tinyfd_selectFolderDialogW(
  1705. wchar_t const * aTitle, /* NULL or "" */
  1706. wchar_t const * aDefaultPath) /* NULL or "" */
  1707. {
  1708. static wchar_t lBuff[MAX_PATH_OR_CMD];
  1709. wchar_t * lRetval;
  1710. BROWSEINFOW bInfo;
  1711. LPITEMIDLIST lpItem;
  1712. HRESULT lHResult;
  1713. if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return (wchar_t *)1; }
  1714. /*if (quoteDetectedW(aTitle)) return tinyfd_selectFolderDialogW(L"INVALID TITLE WITH QUOTES", aDefaultPath);
  1715. if (quoteDetectedW(aDefaultPath)) return tinyfd_selectFolderDialogW(aTitle, L"INVALID DEFAULT_PATH WITH QUOTES");*/
  1716. lHResult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
  1717. bInfo.hwndOwner = GetForegroundWindow();
  1718. bInfo.pidlRoot = NULL;
  1719. bInfo.pszDisplayName = lBuff;
  1720. bInfo.lpszTitle = aTitle && wcslen(aTitle) ? aTitle : NULL;
  1721. if (lHResult == S_OK || lHResult == S_FALSE)
  1722. {
  1723. bInfo.ulFlags = BIF_USENEWUI;
  1724. }
  1725. bInfo.lpfn = BrowseCallbackProcW;
  1726. bInfo.lParam = (LPARAM)aDefaultPath;
  1727. bInfo.iImage = -1;
  1728. lpItem = SHBrowseForFolderW(&bInfo);
  1729. if (!lpItem)
  1730. {
  1731. lRetval = NULL;
  1732. }
  1733. else
  1734. {
  1735. SHGetPathFromIDListW(lpItem, lBuff);
  1736. lRetval = lBuff ;
  1737. }
  1738. if (lHResult == S_OK || lHResult == S_FALSE)
  1739. {
  1740. CoUninitialize();
  1741. }
  1742. return lRetval;
  1743. }
  1744. wchar_t * tinyfd_colorChooserW(
  1745. wchar_t const * aTitle, /* NULL or "" */
  1746. wchar_t const * aDefaultHexRGB, /* NULL or "#FF0000"*/
  1747. unsigned char const aDefaultRGB[3], /* { 0 , 255 , 255 } */
  1748. unsigned char aoResultRGB[3]) /* { 0 , 0 , 0 } */
  1749. {
  1750. static wchar_t lResultHexRGB[8];
  1751. CHOOSECOLORW cc;
  1752. COLORREF crCustColors[16];
  1753. unsigned char lDefaultRGB[3];
  1754. int lRet;
  1755. HRESULT lHResult;
  1756. if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return (wchar_t *)1; }
  1757. /*if (quoteDetectedW(aTitle)) return tinyfd_colorChooserW(L"INVALID TITLE WITH QUOTES", aDefaultHexRGB, aDefaultRGB, aoResultRGB);
  1758. if (quoteDetectedW(aDefaultHexRGB)) return tinyfd_colorChooserW(aTitle, L"INVALID DEFAULT_HEX_RGB WITH QUOTES: use the GRAVE ACCENT \\x60 instead.", aDefaultRGB, aoResultRGB);*/
  1759. lHResult = CoInitializeEx(NULL, 0);
  1760. if ( aDefaultHexRGB && wcslen(aDefaultHexRGB) )
  1761. {
  1762. Hex2RGBW(aDefaultHexRGB, lDefaultRGB);
  1763. }
  1764. else
  1765. {
  1766. lDefaultRGB[0] = aDefaultRGB[0];
  1767. lDefaultRGB[1] = aDefaultRGB[1];
  1768. lDefaultRGB[2] = aDefaultRGB[2];
  1769. }
  1770. /* we can't use aTitle */
  1771. cc.lStructSize = sizeof(CHOOSECOLOR);
  1772. cc.hwndOwner = GetForegroundWindow();
  1773. cc.hInstance = NULL;
  1774. cc.rgbResult = RGB(lDefaultRGB[0], lDefaultRGB[1], lDefaultRGB[2]);
  1775. cc.lpCustColors = crCustColors;
  1776. cc.Flags = CC_RGBINIT | CC_FULLOPEN | CC_ANYCOLOR ;
  1777. cc.lCustData = 0;
  1778. cc.lpfnHook = NULL;
  1779. cc.lpTemplateName = NULL;
  1780. lRet = ChooseColorW(&cc);
  1781. if (!lRet)
  1782. {
  1783. return NULL;
  1784. }
  1785. aoResultRGB[0] = GetRValue(cc.rgbResult);
  1786. aoResultRGB[1] = GetGValue(cc.rgbResult);
  1787. aoResultRGB[2] = GetBValue(cc.rgbResult);
  1788. RGB2HexW(aoResultRGB, lResultHexRGB);
  1789. if (lHResult == S_OK || lHResult == S_FALSE)
  1790. {
  1791. CoUninitialize();
  1792. }
  1793. return lResultHexRGB;
  1794. }
  1795. static int messageBoxWinGui(
  1796. char const * aTitle, /* NULL or "" */
  1797. char const * aMessage, /* NULL or "" may contain \n and \t */
  1798. char const * aDialogType, /* "ok" "okcancel" "yesno" "yesnocancel" */
  1799. char const * aIconType, /* "info" "warning" "error" "question" */
  1800. int aDefaultButton) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */
  1801. {
  1802. int lIntRetVal;
  1803. wchar_t lTitle[128] = L"";
  1804. wchar_t * lMessage = NULL;
  1805. wchar_t lDialogType[16] = L"";
  1806. wchar_t lIconType[16] = L"";
  1807. wchar_t * lTmpWChar;
  1808. if (aTitle)
  1809. {
  1810. if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle);
  1811. else lTmpWChar = tinyfd_mbcsTo16(aTitle);
  1812. wcscpy(lTitle, lTmpWChar);
  1813. }
  1814. if (aMessage)
  1815. {
  1816. if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aMessage);
  1817. else lTmpWChar = tinyfd_mbcsTo16(aMessage);
  1818. lMessage = (wchar_t *) malloc((wcslen(lTmpWChar) + 1)* sizeof(wchar_t));
  1819. if (lMessage) wcscpy(lMessage, lTmpWChar);
  1820. }
  1821. if (aDialogType)
  1822. {
  1823. if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aDialogType);
  1824. else lTmpWChar = tinyfd_mbcsTo16(aDialogType);
  1825. wcscpy(lDialogType, lTmpWChar);
  1826. }
  1827. if (aIconType)
  1828. {
  1829. if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aIconType);
  1830. else lTmpWChar = tinyfd_mbcsTo16(aIconType);
  1831. wcscpy(lIconType, lTmpWChar);
  1832. }
  1833. lIntRetVal = tinyfd_messageBoxW(lTitle, lMessage, lDialogType, lIconType, aDefaultButton);
  1834. free(lMessage);
  1835. return lIntRetVal;
  1836. }
  1837. static int notifyWinGui(
  1838. char const * aTitle, /* NULL or "" */
  1839. char const * aMessage, /* NULL or "" may NOT contain \n nor \t */
  1840. char const * aIconType)
  1841. {
  1842. wchar_t lTitle[128] = L"";
  1843. wchar_t * lMessage = NULL;
  1844. wchar_t lIconType[16] = L"";
  1845. wchar_t * lTmpWChar;
  1846. if (aTitle)
  1847. {
  1848. if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle);
  1849. else lTmpWChar = tinyfd_mbcsTo16(aTitle);
  1850. wcscpy(lTitle, lTmpWChar);
  1851. }
  1852. if (aMessage)
  1853. {
  1854. if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aMessage);
  1855. else lTmpWChar = tinyfd_mbcsTo16(aMessage);
  1856. lMessage = (wchar_t *) malloc((wcslen(lTmpWChar) + 1)* sizeof(wchar_t));
  1857. if (lMessage) wcscpy(lMessage, lTmpWChar);
  1858. }
  1859. if (aIconType)
  1860. {
  1861. if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aIconType);
  1862. else lTmpWChar = tinyfd_mbcsTo16(aIconType);
  1863. wcscpy(lIconType, lTmpWChar);
  1864. }
  1865. tinyfd_notifyPopupW(lTitle, lMessage, lIconType);
  1866. free(lMessage);
  1867. return 1;
  1868. }
  1869. static int inputBoxWinGui(
  1870. char * aoBuff,
  1871. char const * aTitle, /* NULL or "" */
  1872. char const * aMessage, /* NULL or "" may NOT contain \n nor \t */
  1873. char const * aDefaultInput) /* "" , if NULL it's a passwordBox */
  1874. {
  1875. wchar_t lTitle[128] = L"";
  1876. wchar_t * lMessage = NULL;
  1877. wchar_t lDefaultInput[MAX_PATH_OR_CMD] = L"";
  1878. wchar_t * lTmpWChar;
  1879. char * lTmpChar;
  1880. if (aTitle)
  1881. {
  1882. if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle);
  1883. else lTmpWChar = tinyfd_mbcsTo16(aTitle);
  1884. wcscpy(lTitle, lTmpWChar);
  1885. }
  1886. if (aMessage)
  1887. {
  1888. if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aMessage);
  1889. else lTmpWChar = tinyfd_mbcsTo16(aMessage);
  1890. lMessage = (wchar_t *) malloc((wcslen(lTmpWChar) + 1)* sizeof(wchar_t));
  1891. if (lMessage) wcscpy(lMessage, lTmpWChar);
  1892. }
  1893. if (aDefaultInput)
  1894. {
  1895. if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aDefaultInput);
  1896. else lTmpWChar = tinyfd_mbcsTo16(aDefaultInput);
  1897. wcscpy(lDefaultInput, lTmpWChar);
  1898. lTmpWChar = tinyfd_inputBoxW(lTitle, lMessage, lDefaultInput);
  1899. }
  1900. else lTmpWChar = tinyfd_inputBoxW(lTitle, lMessage, NULL);
  1901. free(lMessage);
  1902. if (!lTmpWChar)
  1903. {
  1904. aoBuff[0] = '\0';
  1905. return 0;
  1906. }
  1907. if (tinyfd_winUtf8) lTmpChar = tinyfd_utf16to8(lTmpWChar);
  1908. else lTmpChar = tinyfd_utf16toMbcs(lTmpWChar);
  1909. strcpy(aoBuff, lTmpChar);
  1910. return 1;
  1911. }
  1912. static char * saveFileDialogWinGui(
  1913. char * aoBuff,
  1914. char const * aTitle, /* NULL or "" */
  1915. char const * aDefaultPathAndOrFile, /* NULL or "" */
  1916. int aNumOfFilterPatterns, /* 0 */
  1917. char const * const * aFilterPatterns, /* NULL or {"*.jpg","*.png"} */
  1918. char const * aSingleFilterDescription) /* NULL or "image files" */
  1919. {
  1920. wchar_t lTitle[128] = L"";
  1921. wchar_t lDefaultPathAndFile[MAX_PATH_OR_CMD] = L"";
  1922. wchar_t lSingleFilterDescription[128] = L"";
  1923. wchar_t * * lFilterPatterns;
  1924. wchar_t * lTmpWChar;
  1925. char * lTmpChar;
  1926. int i;
  1927. lFilterPatterns = (wchar_t **) malloc(aNumOfFilterPatterns*sizeof(wchar_t *));
  1928. for (i = 0; i < aNumOfFilterPatterns; i++)
  1929. {
  1930. if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aFilterPatterns[i]);
  1931. else lTmpWChar = tinyfd_mbcsTo16(aFilterPatterns[i]);
  1932. lFilterPatterns[i] = (wchar_t *) malloc((wcslen(lTmpWChar) + 1) * sizeof(wchar_t *));
  1933. if (lFilterPatterns[i]) wcscpy(lFilterPatterns[i], lTmpWChar);
  1934. }
  1935. if (aTitle)
  1936. {
  1937. if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle);
  1938. else lTmpWChar = tinyfd_mbcsTo16(aTitle);
  1939. wcscpy(lTitle, lTmpWChar);
  1940. }
  1941. if (aDefaultPathAndOrFile)
  1942. {
  1943. if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aDefaultPathAndOrFile);
  1944. else lTmpWChar = tinyfd_mbcsTo16(aDefaultPathAndOrFile);
  1945. wcscpy(lDefaultPathAndFile, lTmpWChar);
  1946. }
  1947. if (aSingleFilterDescription)
  1948. {
  1949. if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aSingleFilterDescription);
  1950. else lTmpWChar = tinyfd_mbcsTo16(aSingleFilterDescription);
  1951. wcscpy(lSingleFilterDescription, lTmpWChar);
  1952. }
  1953. lTmpWChar = tinyfd_saveFileDialogW(
  1954. lTitle,
  1955. lDefaultPathAndFile,
  1956. aNumOfFilterPatterns,
  1957. (wchar_t const**) lFilterPatterns, /*stupid cast for gcc*/
  1958. lSingleFilterDescription);
  1959. for (i = 0; i < aNumOfFilterPatterns; i++)
  1960. {
  1961. free(lFilterPatterns[i]);
  1962. }
  1963. free(lFilterPatterns);
  1964. if (!lTmpWChar)
  1965. {
  1966. return NULL;
  1967. }
  1968. if (tinyfd_winUtf8) lTmpChar = tinyfd_utf16to8(lTmpWChar);
  1969. else lTmpChar = tinyfd_utf16toMbcs(lTmpWChar);
  1970. strcpy(aoBuff, lTmpChar);
  1971. if (tinyfd_winUtf8) (void)tinyfd_utf16to8(NULL);
  1972. else (void)tinyfd_utf16toMbcs(NULL);
  1973. return aoBuff;
  1974. }
  1975. static char * openFileDialogWinGui(
  1976. char const * aTitle, /* NULL or "" */
  1977. char const * aDefaultPathAndOrFile, /* NULL or "" */
  1978. int aNumOfFilterPatterns, /* 0 */
  1979. char const * const * aFilterPatterns, /* NULL or {"*.jpg","*.png"} */
  1980. char const * aSingleFilterDescription, /* NULL or "image files" */
  1981. int aAllowMultipleSelects) /* 0 or 1 */
  1982. {
  1983. wchar_t lTitle[128] = L"";
  1984. wchar_t lDefaultPathAndFile[MAX_PATH_OR_CMD] = L"";
  1985. wchar_t lSingleFilterDescription[128] = L"";
  1986. wchar_t * * lFilterPatterns;
  1987. wchar_t * lTmpWChar;
  1988. char * lTmpChar;
  1989. int i;
  1990. lFilterPatterns = (wchar_t * *) malloc(aNumOfFilterPatterns*sizeof(wchar_t *));
  1991. for (i = 0; i < aNumOfFilterPatterns; i++)
  1992. {
  1993. if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aFilterPatterns[i]);
  1994. else lTmpWChar = tinyfd_mbcsTo16(aFilterPatterns[i]);
  1995. lFilterPatterns[i] = (wchar_t *) malloc((wcslen(lTmpWChar) + 1)*sizeof(wchar_t *));
  1996. if (lFilterPatterns[i]) wcscpy(lFilterPatterns[i], lTmpWChar);
  1997. }
  1998. if (aTitle)
  1999. {
  2000. if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle);
  2001. else lTmpWChar = tinyfd_mbcsTo16(aTitle);
  2002. wcscpy(lTitle, lTmpWChar);
  2003. }
  2004. if (aDefaultPathAndOrFile)
  2005. {
  2006. if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aDefaultPathAndOrFile);
  2007. else lTmpWChar = tinyfd_mbcsTo16(aDefaultPathAndOrFile);
  2008. wcscpy(lDefaultPathAndFile, lTmpWChar);
  2009. }
  2010. if (aSingleFilterDescription)
  2011. {
  2012. if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aSingleFilterDescription);
  2013. else lTmpWChar = tinyfd_mbcsTo16(aSingleFilterDescription);
  2014. wcscpy(lSingleFilterDescription, lTmpWChar);
  2015. }
  2016. lTmpWChar = tinyfd_openFileDialogW(
  2017. lTitle,
  2018. lDefaultPathAndFile,
  2019. aNumOfFilterPatterns,
  2020. (wchar_t const**) lFilterPatterns, /*stupid cast for gcc*/
  2021. lSingleFilterDescription,
  2022. aAllowMultipleSelects);
  2023. for (i = 0; i < aNumOfFilterPatterns; i++)
  2024. {
  2025. free(lFilterPatterns[i]);
  2026. }
  2027. free(lFilterPatterns);
  2028. if (!lTmpWChar) return NULL;
  2029. if (tinyfd_winUtf8) lTmpChar = tinyfd_utf16to8(lTmpWChar);
  2030. else lTmpChar = tinyfd_utf16toMbcs(lTmpWChar);
  2031. (void)tinyfd_openFileDialogW(NULL, NULL, 0, NULL, NULL, -1);
  2032. return lTmpChar;
  2033. }
  2034. static char * selectFolderDialogWinGui(
  2035. char * aoBuff,
  2036. char const * aTitle, /* NULL or "" */
  2037. char const * aDefaultPath) /* NULL or "" */
  2038. {
  2039. wchar_t lTitle[128] = L"";
  2040. wchar_t lDefaultPath[MAX_PATH_OR_CMD] = L"";
  2041. wchar_t * lTmpWChar;
  2042. char * lTmpChar;
  2043. if (aTitle)
  2044. {
  2045. if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle);
  2046. else lTmpWChar = tinyfd_mbcsTo16(aTitle);
  2047. wcscpy(lTitle, lTmpWChar);
  2048. }
  2049. if (aDefaultPath)
  2050. {
  2051. if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aDefaultPath);
  2052. else lTmpWChar = tinyfd_mbcsTo16(aDefaultPath);
  2053. wcscpy(lDefaultPath, lTmpWChar);
  2054. }
  2055. lTmpWChar = tinyfd_selectFolderDialogW(
  2056. lTitle,
  2057. lDefaultPath);
  2058. if (!lTmpWChar)
  2059. {
  2060. return NULL;
  2061. }
  2062. if (tinyfd_winUtf8) lTmpChar = tinyfd_utf16to8(lTmpWChar);
  2063. else lTmpChar = tinyfd_utf16toMbcs(lTmpWChar);
  2064. strcpy(aoBuff, lTmpChar);
  2065. return aoBuff;
  2066. }
  2067. static char * colorChooserWinGui(
  2068. char const * aTitle, /* NULL or "" */
  2069. char const * aDefaultHexRGB, /* NULL or "#FF0000"*/
  2070. unsigned char const aDefaultRGB[3], /* { 0 , 255 , 255 } */
  2071. unsigned char aoResultRGB[3]) /* { 0 , 0 , 0 } */
  2072. {
  2073. static char lResultHexRGB[8];
  2074. wchar_t lTitle[128];
  2075. wchar_t * lTmpWChar;
  2076. char * lTmpChar;
  2077. wchar_t lDefaultHexRGB[16] = L"";
  2078. if (aTitle)
  2079. {
  2080. if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle);
  2081. else lTmpWChar = tinyfd_mbcsTo16(aTitle);
  2082. wcscpy(lTitle, lTmpWChar);
  2083. }
  2084. if (aDefaultHexRGB)
  2085. {
  2086. if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aDefaultHexRGB);
  2087. else lTmpWChar = tinyfd_mbcsTo16(aDefaultHexRGB);
  2088. wcscpy(lDefaultHexRGB, lTmpWChar);
  2089. }
  2090. lTmpWChar = tinyfd_colorChooserW(
  2091. lTitle,
  2092. lDefaultHexRGB,
  2093. aDefaultRGB,
  2094. aoResultRGB );
  2095. if (!lTmpWChar)
  2096. {
  2097. return NULL;
  2098. }
  2099. if (tinyfd_winUtf8) lTmpChar = tinyfd_utf16to8(lTmpWChar);
  2100. else lTmpChar = tinyfd_utf16toMbcs(lTmpWChar);
  2101. strcpy(lResultHexRGB, lTmpChar);
  2102. return lResultHexRGB;
  2103. }
  2104. static int dialogPresent(void)
  2105. {
  2106. static int lDialogPresent = -1 ;
  2107. char lBuff[MAX_PATH_OR_CMD] ;
  2108. FILE * lIn ;
  2109. char const * lString = "dialog.exe";
  2110. if (!tinyfd_allowCursesDialogs) return 0;
  2111. if (lDialogPresent < 0)
  2112. {
  2113. lIn = _popen("where dialog.exe", "r");
  2114. if ( ! lIn )
  2115. {
  2116. lDialogPresent = 0 ;
  2117. return 0 ;
  2118. }
  2119. while ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL )
  2120. {}
  2121. _pclose( lIn ) ;
  2122. if ( lBuff[strlen( lBuff ) -1] == '\n' )
  2123. {
  2124. lBuff[strlen( lBuff ) -1] = '\0' ;
  2125. }
  2126. if ( strcmp(lBuff+strlen(lBuff)-strlen(lString),lString) )
  2127. {
  2128. lDialogPresent = 0 ;
  2129. }
  2130. else
  2131. {
  2132. lDialogPresent = 1 ;
  2133. }
  2134. }
  2135. return lDialogPresent;
  2136. }
  2137. static int messageBoxWinConsole(
  2138. char const * aTitle , /* NULL or "" */
  2139. char const * aMessage , /* NULL or "" may contain \n and \t */
  2140. char const * aDialogType , /* "ok" "okcancel" "yesno" "yesnocancel" */
  2141. char const * aIconType , /* "info" "warning" "error" "question" */
  2142. int aDefaultButton ) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */
  2143. {
  2144. char lDialogString[MAX_PATH_OR_CMD];
  2145. char lDialogFile[MAX_PATH_OR_CMD];
  2146. FILE * lIn;
  2147. char lBuff[MAX_PATH_OR_CMD] = "";
  2148. (void)aIconType;
  2149. strcpy(lDialogString, "dialog ");
  2150. if (aTitle && strlen(aTitle))
  2151. {
  2152. strcat(lDialogString, "--title \"") ;
  2153. strcat(lDialogString, aTitle) ;
  2154. strcat(lDialogString, "\" ") ;
  2155. }
  2156. if ( aDialogType && ( !strcmp( "okcancel" , aDialogType )
  2157. || !strcmp("yesno", aDialogType) || !strcmp("yesnocancel", aDialogType) ) )
  2158. {
  2159. strcat(lDialogString, "--backtitle \"") ;
  2160. strcat(lDialogString, "tab: move focus") ;
  2161. strcat(lDialogString, "\" ") ;
  2162. }
  2163. if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) )
  2164. {
  2165. if ( ! aDefaultButton )
  2166. {
  2167. strcat( lDialogString , "--defaultno " ) ;
  2168. }
  2169. strcat( lDialogString ,
  2170. "--yes-label \"Ok\" --no-label \"Cancel\" --yesno " ) ;
  2171. }
  2172. else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) )
  2173. {
  2174. if ( ! aDefaultButton )
  2175. {
  2176. strcat( lDialogString , "--defaultno " ) ;
  2177. }
  2178. strcat( lDialogString , "--yesno " ) ;
  2179. }
  2180. else if (aDialogType && !strcmp("yesnocancel", aDialogType))
  2181. {
  2182. if (!aDefaultButton)
  2183. {
  2184. strcat(lDialogString, "--defaultno ");
  2185. }
  2186. strcat(lDialogString, "--menu ");
  2187. }
  2188. else
  2189. {
  2190. strcat( lDialogString , "--msgbox " ) ;
  2191. }
  2192. strcat( lDialogString , "\"" ) ;
  2193. if ( aMessage && strlen(aMessage) )
  2194. {
  2195. tfd_replaceSubStr( aMessage , "\n" , "\\n" , lBuff ) ;
  2196. strcat(lDialogString, lBuff) ;
  2197. lBuff[0]='\0';
  2198. }
  2199. strcat(lDialogString, "\" ");
  2200. if (aDialogType && !strcmp("yesnocancel", aDialogType))
  2201. {
  2202. strcat(lDialogString, "0 60 0 Yes \"\" No \"\"");
  2203. strcat(lDialogString, "2>>");
  2204. }
  2205. else
  2206. {
  2207. strcat(lDialogString, "10 60");
  2208. strcat(lDialogString, " && echo 1 > ");
  2209. }
  2210. strcpy(lDialogFile, getenv("TEMP"));
  2211. strcat(lDialogFile, "\\tinyfd.txt");
  2212. strcat(lDialogString, lDialogFile);
  2213. /*if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ;*/
  2214. system( lDialogString ) ;
  2215. if (!(lIn = fopen(lDialogFile, "r")))
  2216. {
  2217. remove(lDialogFile);
  2218. return 0 ;
  2219. }
  2220. while (fgets(lBuff, sizeof(lBuff), lIn) != NULL)
  2221. {}
  2222. fclose(lIn);
  2223. remove(lDialogFile);
  2224. if ( lBuff[strlen( lBuff ) -1] == '\n' )
  2225. {
  2226. lBuff[strlen( lBuff ) -1] = '\0' ;
  2227. }
  2228. /* if (tinyfd_verbose) printf("lBuff: %s\n", lBuff); */
  2229. if ( ! strlen(lBuff) )
  2230. {
  2231. return 0;
  2232. }
  2233. if (aDialogType && !strcmp("yesnocancel", aDialogType))
  2234. {
  2235. if (lBuff[0] == 'Y') return 1;
  2236. else return 2;
  2237. }
  2238. return 1;
  2239. }
  2240. static int inputBoxWinConsole(
  2241. char * aoBuff ,
  2242. char const * aTitle , /* NULL or "" */
  2243. char const * aMessage , /* NULL or "" may NOT contain \n nor \t */
  2244. char const * aDefaultInput ) /* "" , if NULL it's a passwordBox */
  2245. {
  2246. char lDialogString[MAX_PATH_OR_CMD];
  2247. char lDialogFile[MAX_PATH_OR_CMD];
  2248. FILE * lIn;
  2249. int lResult;
  2250. strcpy(lDialogFile, getenv("TEMP"));
  2251. strcat(lDialogFile, "\\tinyfd.txt");
  2252. strcpy(lDialogString , "echo|set /p=1 >" ) ;
  2253. strcat(lDialogString, lDialogFile);
  2254. strcat( lDialogString , " & " ) ;
  2255. strcat( lDialogString , "dialog " ) ;
  2256. if ( aTitle && strlen(aTitle) )
  2257. {
  2258. strcat(lDialogString, "--title \"") ;
  2259. strcat(lDialogString, aTitle) ;
  2260. strcat(lDialogString, "\" ") ;
  2261. }
  2262. strcat(lDialogString, "--backtitle \"") ;
  2263. strcat(lDialogString, "tab: move focus") ;
  2264. if ( ! aDefaultInput )
  2265. {
  2266. strcat(lDialogString, " (sometimes nothing, no blink nor star, is shown in text field)") ;
  2267. }
  2268. strcat(lDialogString, "\" ") ;
  2269. if ( ! aDefaultInput )
  2270. {
  2271. strcat( lDialogString , "--insecure --passwordbox" ) ;
  2272. }
  2273. else
  2274. {
  2275. strcat( lDialogString , "--inputbox" ) ;
  2276. }
  2277. strcat( lDialogString , " \"" ) ;
  2278. if ( aMessage && strlen(aMessage) )
  2279. {
  2280. strcat(lDialogString, aMessage) ;
  2281. }
  2282. strcat(lDialogString,"\" 10 60 ") ;
  2283. if ( aDefaultInput && strlen(aDefaultInput) )
  2284. {
  2285. strcat(lDialogString, "\"") ;
  2286. strcat(lDialogString, aDefaultInput) ;
  2287. strcat(lDialogString, "\" ") ;
  2288. }
  2289. strcat(lDialogString, "2>>");
  2290. strcpy(lDialogFile, getenv("TEMP"));
  2291. strcat(lDialogFile, "\\tinyfd.txt");
  2292. strcat(lDialogString, lDialogFile);
  2293. strcat(lDialogString, " || echo 0 > ");
  2294. strcat(lDialogString, lDialogFile);
  2295. /* printf( "lDialogString: %s\n" , lDialogString ) ; */
  2296. system( lDialogString ) ;
  2297. if (!(lIn = fopen(lDialogFile, "r")))
  2298. {
  2299. remove(lDialogFile);
  2300. aoBuff[0] = '\0';
  2301. return 0;
  2302. }
  2303. while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL)
  2304. {}
  2305. fclose(lIn);
  2306. wipefile(lDialogFile);
  2307. remove(lDialogFile);
  2308. if ( aoBuff[strlen( aoBuff ) -1] == '\n' )
  2309. {
  2310. aoBuff[strlen( aoBuff ) -1] = '\0' ;
  2311. }
  2312. /* printf( "aoBuff: %s\n" , aoBuff ) ; */
  2313. /* printf( "aoBuff: %s len: %lu \n" , aoBuff , strlen(aoBuff) ) ; */
  2314. lResult = strncmp( aoBuff , "1" , 1) ? 0 : 1 ;
  2315. /* printf( "lResult: %d \n" , lResult ) ; */
  2316. if ( ! lResult )
  2317. {
  2318. aoBuff[0] = '\0';
  2319. return 0 ;
  2320. }
  2321. /* printf( "aoBuff+1: %s\n" , aoBuff+1 ) ; */
  2322. strcpy(aoBuff, aoBuff+3);
  2323. return 1;
  2324. }
  2325. static char * saveFileDialogWinConsole(
  2326. char * aoBuff ,
  2327. char const * aTitle , /* NULL or "" */
  2328. char const * aDefaultPathAndOrFile ) /* NULL or "" */
  2329. {
  2330. char lDialogString[MAX_PATH_OR_CMD];
  2331. char lPathAndFile[MAX_PATH_OR_CMD] = "";
  2332. FILE * lIn;
  2333. strcpy( lDialogString , "dialog " ) ;
  2334. if ( aTitle && strlen(aTitle) )
  2335. {
  2336. strcat(lDialogString, "--title \"") ;
  2337. strcat(lDialogString, aTitle) ;
  2338. strcat(lDialogString, "\" ") ;
  2339. }
  2340. strcat(lDialogString, "--backtitle \"") ;
  2341. strcat(lDialogString,
  2342. "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ;
  2343. strcat(lDialogString, "\" ") ;
  2344. strcat( lDialogString , "--fselect \"" ) ;
  2345. if ( aDefaultPathAndOrFile && strlen(aDefaultPathAndOrFile) )
  2346. {
  2347. /* dialog.exe uses unix separators even on windows */
  2348. strcpy(lPathAndFile, aDefaultPathAndOrFile);
  2349. replaceChr( lPathAndFile , '\\' , '/' ) ;
  2350. }
  2351. /* dialog.exe needs at least one separator */
  2352. if ( ! strchr(lPathAndFile, '/') )
  2353. {
  2354. strcat(lDialogString, "./") ;
  2355. }
  2356. strcat(lDialogString, lPathAndFile) ;
  2357. strcat(lDialogString, "\" 0 60 2>");
  2358. strcpy(lPathAndFile, getenv("TEMP"));
  2359. strcat(lPathAndFile, "\\tinyfd.txt");
  2360. strcat(lDialogString, lPathAndFile);
  2361. /* printf( "lDialogString: %s\n" , lDialogString ) ; */
  2362. system( lDialogString ) ;
  2363. if (!(lIn = fopen(lPathAndFile, "r")))
  2364. {
  2365. remove(lPathAndFile);
  2366. return NULL;
  2367. }
  2368. while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL)
  2369. {}
  2370. fclose(lIn);
  2371. remove(lPathAndFile);
  2372. replaceChr( aoBuff , '/' , '\\' ) ;
  2373. /* printf( "aoBuff: %s\n" , aoBuff ) ; */
  2374. getLastName(lDialogString,aoBuff);
  2375. if ( ! strlen(lDialogString) )
  2376. {
  2377. return NULL;
  2378. }
  2379. return aoBuff;
  2380. }
  2381. static char * openFileDialogWinConsole(
  2382. char const * aTitle , /* NULL or "" */
  2383. char const * aDefaultPathAndOrFile ) /* NULL or "" */
  2384. {
  2385. char lFilterPatterns[MAX_PATH_OR_CMD] = "";
  2386. char lDialogString[MAX_PATH_OR_CMD] ;
  2387. FILE * lIn;
  2388. static char aoBuff[MAX_PATH_OR_CMD];
  2389. strcpy( lDialogString , "dialog " ) ;
  2390. if ( aTitle && strlen(aTitle) )
  2391. {
  2392. strcat(lDialogString, "--title \"") ;
  2393. strcat(lDialogString, aTitle) ;
  2394. strcat(lDialogString, "\" ") ;
  2395. }
  2396. strcat(lDialogString, "--backtitle \"") ;
  2397. strcat(lDialogString,
  2398. "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ;
  2399. strcat(lDialogString, "\" ") ;
  2400. strcat( lDialogString , "--fselect \"" ) ;
  2401. if ( aDefaultPathAndOrFile && strlen(aDefaultPathAndOrFile) )
  2402. {
  2403. /* dialog.exe uses unix separators even on windows */
  2404. strcpy(lFilterPatterns, aDefaultPathAndOrFile);
  2405. replaceChr( lFilterPatterns , '\\' , '/' ) ;
  2406. }
  2407. /* dialog.exe needs at least one separator */
  2408. if ( ! strchr(lFilterPatterns, '/') )
  2409. {
  2410. strcat(lDialogString, "./") ;
  2411. }
  2412. strcat(lDialogString, lFilterPatterns) ;
  2413. strcat(lDialogString, "\" 0 60 2>");
  2414. strcpy(lFilterPatterns, getenv("TEMP"));
  2415. strcat(lFilterPatterns, "\\tinyfd.txt");
  2416. strcat(lDialogString, lFilterPatterns);
  2417. /* printf( "lDialogString: %s\n" , lDialogString ) ; */
  2418. system( lDialogString ) ;
  2419. if (!(lIn = fopen(lFilterPatterns, "r")))
  2420. {
  2421. remove(lFilterPatterns);
  2422. return NULL;
  2423. }
  2424. while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL)
  2425. {}
  2426. fclose(lIn);
  2427. remove(lFilterPatterns);
  2428. replaceChr( aoBuff , '/' , '\\' ) ;
  2429. /* printf( "aoBuff: %s\n" , aoBuff ) ; */
  2430. return aoBuff;
  2431. }
  2432. static char * selectFolderDialogWinConsole(
  2433. char * aoBuff ,
  2434. char const * aTitle , /* NULL or "" */
  2435. char const * aDefaultPath ) /* NULL or "" */
  2436. {
  2437. char lDialogString[MAX_PATH_OR_CMD] ;
  2438. char lString[MAX_PATH_OR_CMD] ;
  2439. FILE * lIn ;
  2440. strcpy( lDialogString , "dialog " ) ;
  2441. if ( aTitle && strlen(aTitle) )
  2442. {
  2443. strcat(lDialogString, "--title \"") ;
  2444. strcat(lDialogString, aTitle) ;
  2445. strcat(lDialogString, "\" ") ;
  2446. }
  2447. strcat(lDialogString, "--backtitle \"") ;
  2448. strcat(lDialogString,
  2449. "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ;
  2450. strcat(lDialogString, "\" ") ;
  2451. strcat( lDialogString , "--dselect \"" ) ;
  2452. if ( aDefaultPath && strlen(aDefaultPath) )
  2453. {
  2454. /* dialog.exe uses unix separators even on windows */
  2455. strcpy(lString, aDefaultPath) ;
  2456. ensureFinalSlash(lString);
  2457. replaceChr( lString , '\\' , '/' ) ;
  2458. strcat(lDialogString, lString) ;
  2459. }
  2460. else
  2461. {
  2462. /* dialog.exe needs at least one separator */
  2463. strcat(lDialogString, "./") ;
  2464. }
  2465. strcat(lDialogString, "\" 0 60 2>");
  2466. strcpy(lString, getenv("TEMP"));
  2467. strcat(lString, "\\tinyfd.txt");
  2468. strcat(lDialogString, lString);
  2469. /* printf( "lDialogString: %s\n" , lDialogString ) ; */
  2470. system( lDialogString ) ;
  2471. if (!(lIn = fopen(lString, "r")))
  2472. {
  2473. remove(lString);
  2474. return NULL;
  2475. }
  2476. while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL)
  2477. {}
  2478. fclose(lIn);
  2479. remove(lString);
  2480. replaceChr( aoBuff , '/' , '\\' ) ;
  2481. /* printf( "aoBuff: %s\n" , aoBuff ) ; */
  2482. return aoBuff;
  2483. }
  2484. static void writeUtf8( char const * aUtf8String )
  2485. {
  2486. unsigned long lNum;
  2487. void * lConsoleHandle;
  2488. wchar_t * lTmpWChar;
  2489. lConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
  2490. lTmpWChar = tinyfd_utf8to16(aUtf8String);
  2491. (void)WriteConsoleW(lConsoleHandle, lTmpWChar, (DWORD) wcslen(lTmpWChar), &lNum, NULL);
  2492. }
  2493. int tinyfd_messageBox(
  2494. char const * aTitle, /* NULL or "" */
  2495. char const * aMessage, /* NULL or "" may contain \n and \t */
  2496. char const * aDialogType, /* "ok" "okcancel" "yesno" "yesnocancel" */
  2497. char const * aIconType, /* "info" "warning" "error" "question" */
  2498. int aDefaultButton) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */
  2499. {
  2500. char lChar;
  2501. UINT lOriginalCP = 0;
  2502. UINT lOriginalOutputCP = 0;
  2503. if (tfd_quoteDetected(aTitle)) return tinyfd_messageBox("INVALID TITLE WITH QUOTES", aMessage, aDialogType, aIconType, aDefaultButton);
  2504. if (tfd_quoteDetected(aMessage)) return tinyfd_messageBox(aTitle, "INVALID MESSAGE WITH QUOTES", aDialogType, aIconType, aDefaultButton);
  2505. if ((!tinyfd_forceConsole || !(GetConsoleWindow() || dialogPresent()))
  2506. && (!getenv("SSH_CLIENT") || getenvDISPLAY()))
  2507. {
  2508. if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "windows"); return 1; }
  2509. return messageBoxWinGui(aTitle, aMessage, aDialogType, aIconType, aDefaultButton);
  2510. }
  2511. else if (dialogPresent())
  2512. {
  2513. if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "dialog"); return 0; }
  2514. return messageBoxWinConsole(
  2515. aTitle, aMessage, aDialogType, aIconType, aDefaultButton);
  2516. }
  2517. else
  2518. {
  2519. if (!tinyfd_winUtf8)
  2520. {
  2521. lOriginalCP = GetConsoleCP();
  2522. lOriginalOutputCP = GetConsoleOutputCP();
  2523. (void)SetConsoleCP(GetACP());
  2524. (void)SetConsoleOutputCP(GetACP());
  2525. }
  2526. if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "basicinput"); return 0; }
  2527. if (!gWarningDisplayed && !tinyfd_forceConsole)
  2528. {
  2529. gWarningDisplayed = 1;
  2530. printf("\n\n%s\n", gTitle);
  2531. printf("%s\n\n", tinyfd_needs);
  2532. }
  2533. if (aTitle && strlen(aTitle))
  2534. {
  2535. printf("\n");
  2536. if (tinyfd_winUtf8) writeUtf8(aTitle);
  2537. else printf("%s", aTitle);
  2538. printf("\n\n");
  2539. }
  2540. if (aDialogType && !strcmp("yesno", aDialogType))
  2541. {
  2542. do
  2543. {
  2544. if (aMessage && strlen(aMessage))
  2545. {
  2546. if (tinyfd_winUtf8) writeUtf8(aMessage);
  2547. else printf("%s", aMessage);
  2548. printf("\n");
  2549. }
  2550. printf("y/n: ");
  2551. lChar = (char)tolower(_getch());
  2552. printf("\n\n");
  2553. } while (lChar != 'y' && lChar != 'n');
  2554. if (!tinyfd_winUtf8) { (void)SetConsoleCP(lOriginalCP); (void)SetConsoleOutputCP(lOriginalOutputCP); }
  2555. return lChar == 'y' ? 1 : 0;
  2556. }
  2557. else if (aDialogType && !strcmp("okcancel", aDialogType))
  2558. {
  2559. do
  2560. {
  2561. if (aMessage && strlen(aMessage))
  2562. {
  2563. if (tinyfd_winUtf8) writeUtf8(aMessage);
  2564. else printf("%s", aMessage);
  2565. printf("\n");
  2566. }
  2567. printf("[O]kay/[C]ancel: ");
  2568. lChar = (char)tolower(_getch());
  2569. printf("\n\n");
  2570. } while (lChar != 'o' && lChar != 'c');
  2571. if (!tinyfd_winUtf8) { (void)SetConsoleCP(lOriginalCP); (void)SetConsoleOutputCP(lOriginalOutputCP); }
  2572. return lChar == 'o' ? 1 : 0;
  2573. }
  2574. else if (aDialogType && !strcmp("yesnocancel", aDialogType))
  2575. {
  2576. do
  2577. {
  2578. if (aMessage && strlen(aMessage))
  2579. {
  2580. if (tinyfd_winUtf8) writeUtf8(aMessage);
  2581. else printf("%s", aMessage);
  2582. printf("\n");
  2583. }
  2584. printf("[Y]es/[N]o/[C]ancel: ");
  2585. lChar = (char)tolower(_getch());
  2586. printf("\n\n");
  2587. } while (lChar != 'y' && lChar != 'n' && lChar != 'c');
  2588. if (!tinyfd_winUtf8) { (void)SetConsoleCP(lOriginalCP); (void)SetConsoleOutputCP(lOriginalOutputCP); }
  2589. return (lChar == 'y') ? 1 : (lChar == 'n') ? 2 : 0;
  2590. }
  2591. else
  2592. {
  2593. if (aMessage && strlen(aMessage))
  2594. {
  2595. if (tinyfd_winUtf8) writeUtf8(aMessage);
  2596. else printf("%s", aMessage);
  2597. printf("\n\n");
  2598. }
  2599. printf("press enter to continue "); fflush(stdout);
  2600. lChar = (char)_getch();
  2601. printf("\n\n");
  2602. if (!tinyfd_winUtf8) { (void)SetConsoleCP(lOriginalCP); (void)SetConsoleOutputCP(lOriginalOutputCP); }
  2603. return 1;
  2604. }
  2605. }
  2606. }
  2607. /* return has only meaning for tinyfd_query */
  2608. int tinyfd_notifyPopup(
  2609. char const * aTitle, /* NULL or "" */
  2610. char const * aMessage , /* NULL or "" may contain \n \t */
  2611. char const * aIconType ) /* "info" "warning" "error" */
  2612. {
  2613. if (tfd_quoteDetected(aTitle)) return tinyfd_notifyPopup("INVALID TITLE WITH QUOTES", aMessage, aIconType);
  2614. if (tfd_quoteDetected(aMessage)) return tinyfd_notifyPopup(aTitle, "INVALID MESSAGE WITH QUOTES", aIconType);
  2615. if ( powershellPresent() && (!tinyfd_forceConsole || !(
  2616. GetConsoleWindow() ||
  2617. dialogPresent()))
  2618. && (!getenv("SSH_CLIENT") || getenvDISPLAY()))
  2619. {
  2620. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return 1;}
  2621. return notifyWinGui(aTitle, aMessage, aIconType);
  2622. }
  2623. else
  2624. return tinyfd_messageBox(aTitle, aMessage, "ok" , aIconType, 0);
  2625. }
  2626. /* returns NULL on cancel */
  2627. char * tinyfd_inputBox(
  2628. char const * aTitle , /* NULL or "" */
  2629. char const * aMessage , /* NULL or "" (\n and \t have no effect) */
  2630. char const * aDefaultInput ) /* "" , if NULL it's a passwordBox */
  2631. {
  2632. static char lBuff[MAX_PATH_OR_CMD] = "";
  2633. char * lEOF;
  2634. DWORD mode = 0;
  2635. HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
  2636. unsigned long lNum;
  2637. void * lConsoleHandle;
  2638. char * lTmpChar;
  2639. wchar_t lBuffW[1024];
  2640. UINT lOriginalCP = 0;
  2641. UINT lOriginalOutputCP = 0;
  2642. if (!aTitle && !aMessage && !aDefaultInput) return lBuff; /* now I can fill lBuff from outside */
  2643. if (tfd_quoteDetected(aTitle)) return tinyfd_inputBox("INVALID TITLE WITH QUOTES", aMessage, aDefaultInput);
  2644. if (tfd_quoteDetected(aMessage)) return tinyfd_inputBox(aTitle, "INVALID MESSAGE WITH QUOTES", aDefaultInput);
  2645. if (tfd_quoteDetected(aDefaultInput)) return tinyfd_inputBox(aTitle, aMessage, "INVALID DEFAULT_INPUT WITH QUOTES: use the GRAVE ACCENT \\x60 instead.");
  2646. mode = 0;
  2647. hStdin = GetStdHandle(STD_INPUT_HANDLE);
  2648. if ((!tinyfd_forceConsole || !(
  2649. GetConsoleWindow() ||
  2650. dialogPresent()))
  2651. && (!getenv("SSH_CLIENT") || getenvDISPLAY()))
  2652. {
  2653. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char *)1;}
  2654. lBuff[0]='\0';
  2655. if (inputBoxWinGui(lBuff, aTitle, aMessage, aDefaultInput)) return lBuff;
  2656. else return NULL;
  2657. }
  2658. else if ( dialogPresent() )
  2659. {
  2660. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char *)0;}
  2661. lBuff[0]='\0';
  2662. if (inputBoxWinConsole(lBuff, aTitle, aMessage, aDefaultInput) ) return lBuff;
  2663. else return NULL;
  2664. }
  2665. else
  2666. {
  2667. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return (char *)0;}
  2668. lBuff[0]='\0';
  2669. if (!gWarningDisplayed && !tinyfd_forceConsole)
  2670. {
  2671. gWarningDisplayed = 1 ;
  2672. printf("\n\n%s\n", gTitle);
  2673. printf("%s\n\n", tinyfd_needs);
  2674. }
  2675. if (!tinyfd_winUtf8)
  2676. {
  2677. lOriginalCP = GetConsoleCP();
  2678. lOriginalOutputCP = GetConsoleOutputCP();
  2679. (void)SetConsoleCP(GetACP());
  2680. (void)SetConsoleOutputCP(GetACP());
  2681. }
  2682. if (aTitle && strlen(aTitle))
  2683. {
  2684. printf("\n");
  2685. if (tinyfd_winUtf8) writeUtf8(aTitle);
  2686. else printf("%s", aTitle);
  2687. printf("\n\n");
  2688. }
  2689. if ( aMessage && strlen(aMessage) )
  2690. {
  2691. if (tinyfd_winUtf8) writeUtf8(aMessage);
  2692. else printf("%s", aMessage);
  2693. printf("\n");
  2694. }
  2695. printf("(ctrl-Z + enter to cancel): "); fflush(stdout);
  2696. if ( ! aDefaultInput )
  2697. {
  2698. (void) GetConsoleMode(hStdin, &mode);
  2699. (void) SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT));
  2700. }
  2701. if (tinyfd_winUtf8)
  2702. {
  2703. lConsoleHandle = GetStdHandle(STD_INPUT_HANDLE);
  2704. (void) ReadConsoleW(lConsoleHandle, lBuffW, MAX_PATH_OR_CMD, &lNum, NULL);
  2705. if (!aDefaultInput)
  2706. {
  2707. (void)SetConsoleMode(hStdin, mode);
  2708. printf("\n");
  2709. }
  2710. lBuffW[lNum] = '\0';
  2711. if (lBuffW[wcslen(lBuffW) - 1] == '\n') lBuffW[wcslen(lBuffW) - 1] = '\0';
  2712. if (lBuffW[wcslen(lBuffW) - 1] == '\r') lBuffW[wcslen(lBuffW) - 1] = '\0';
  2713. lTmpChar = tinyfd_utf16to8(lBuffW);
  2714. if (lTmpChar)
  2715. {
  2716. strcpy(lBuff, lTmpChar);
  2717. return lBuff;
  2718. }
  2719. else
  2720. return NULL;
  2721. }
  2722. else
  2723. {
  2724. lEOF = fgets(lBuff, MAX_PATH_OR_CMD, stdin);
  2725. if (!aDefaultInput)
  2726. {
  2727. (void)SetConsoleMode(hStdin, mode);
  2728. printf("\n");
  2729. }
  2730. if (!tinyfd_winUtf8)
  2731. {
  2732. (void)SetConsoleCP(lOriginalCP);
  2733. (void)SetConsoleOutputCP(lOriginalOutputCP);
  2734. }
  2735. if (!lEOF)
  2736. {
  2737. return NULL;
  2738. }
  2739. printf("\n");
  2740. if (strchr(lBuff, 27))
  2741. {
  2742. return NULL;
  2743. }
  2744. if (lBuff[strlen(lBuff) - 1] == '\n')
  2745. {
  2746. lBuff[strlen(lBuff) - 1] = '\0';
  2747. }
  2748. return lBuff;
  2749. }
  2750. }
  2751. }
  2752. char * tinyfd_saveFileDialog(
  2753. char const * aTitle , /* NULL or "" */
  2754. char const * aDefaultPathAndOrFile , /* NULL or "" */
  2755. int aNumOfFilterPatterns , /* 0 */
  2756. char const * const * aFilterPatterns , /* NULL or {"*.jpg","*.png"} */
  2757. char const * aSingleFilterDescription ) /* NULL or "image files" */
  2758. {
  2759. static char lBuff[MAX_PATH_OR_CMD] ;
  2760. char lString[MAX_PATH_OR_CMD] ;
  2761. char * p ;
  2762. char * lPointerInputBox;
  2763. int i;
  2764. lBuff[0]='\0';
  2765. if ( ! aFilterPatterns ) aNumOfFilterPatterns = 0 ;
  2766. if (tfd_quoteDetected(aTitle)) return tinyfd_saveFileDialog("INVALID TITLE WITH QUOTES", aDefaultPathAndOrFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription);
  2767. if (tfd_quoteDetected(aDefaultPathAndOrFile)) return tinyfd_saveFileDialog(aTitle, "INVALID DEFAULT_PATH WITH QUOTES", aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription);
  2768. if (tfd_quoteDetected(aSingleFilterDescription)) return tinyfd_saveFileDialog(aTitle, aDefaultPathAndOrFile, aNumOfFilterPatterns, aFilterPatterns, "INVALID FILTER_DESCRIPTION WITH QUOTES");
  2769. for (i = 0; i < aNumOfFilterPatterns; i++)
  2770. {
  2771. if (tfd_quoteDetected(aFilterPatterns[i])) return tinyfd_saveFileDialog("INVALID FILTER_PATTERN WITH QUOTES: use the GRAVE ACCENT \\x60 instead.", aDefaultPathAndOrFile, 0, NULL, NULL);
  2772. }
  2773. if ( ( !tinyfd_forceConsole || !( GetConsoleWindow() || dialogPresent() ) )
  2774. && (!getenv("SSH_CLIENT") || getenvDISPLAY()))
  2775. {
  2776. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char *)1;}
  2777. p = saveFileDialogWinGui(lBuff,
  2778. aTitle, aDefaultPathAndOrFile, aNumOfFilterPatterns, (char const * const *)aFilterPatterns, aSingleFilterDescription);
  2779. }
  2780. else if (dialogPresent())
  2781. {
  2782. if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "dialog"); return (char *)0; }
  2783. p = saveFileDialogWinConsole(lBuff, aTitle, aDefaultPathAndOrFile);
  2784. }
  2785. else
  2786. {
  2787. if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "basicinput"); return (char *)0; }
  2788. strcpy(lBuff, "Save file in ");
  2789. strcat(lBuff, getCurDir());
  2790. lPointerInputBox = tinyfd_inputBox(NULL,NULL,NULL); /* obtain a pointer on the current content of tinyfd_inputBox */
  2791. if (lPointerInputBox) strcpy(lString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */
  2792. p = tinyfd_inputBox(aTitle, lBuff, "");
  2793. if (p) strcpy(lBuff, p); else lBuff[0] = '\0';
  2794. if (lPointerInputBox) strcpy(lPointerInputBox, lString); /* restore its previous content to tinyfd_inputBox */
  2795. p = lBuff;
  2796. }
  2797. if ( ! p || ! strlen( p ) )
  2798. {
  2799. return NULL;
  2800. }
  2801. getPathWithoutFinalSlash( lString , p ) ;
  2802. if ( strlen( lString ) && ! dirExists( lString ) )
  2803. {
  2804. return NULL ;
  2805. }
  2806. getLastName(lString,p);
  2807. if ( ! filenameValid(lString) )
  2808. {
  2809. return NULL;
  2810. }
  2811. return p ;
  2812. }
  2813. /* in case of multiple files, the separator is | */
  2814. char * tinyfd_openFileDialog(
  2815. char const * aTitle , /* NULL or "" */
  2816. char const * aDefaultPathAndOrFile, /* NULL or "" */
  2817. int aNumOfFilterPatterns , /* 0 */
  2818. char const * const * aFilterPatterns, /* NULL or {"*.jpg","*.png"} */
  2819. char const * aSingleFilterDescription, /* NULL or "image files" */
  2820. int aAllowMultipleSelects ) /* 0 or 1 */
  2821. {
  2822. static char lBuff[MAX_PATH_OR_CMD];
  2823. char lString[MAX_PATH_OR_CMD];
  2824. char * p;
  2825. char * lPointerInputBox;
  2826. int i;
  2827. if ( ! aFilterPatterns ) aNumOfFilterPatterns = 0 ;
  2828. if (tfd_quoteDetected(aTitle)) return tinyfd_openFileDialog("INVALID TITLE WITH QUOTES", aDefaultPathAndOrFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects);
  2829. if (tfd_quoteDetected(aDefaultPathAndOrFile)) return tinyfd_openFileDialog(aTitle, "INVALID DEFAULT_PATH WITH QUOTES", aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects);
  2830. if (tfd_quoteDetected(aSingleFilterDescription)) return tinyfd_openFileDialog(aTitle, aDefaultPathAndOrFile, aNumOfFilterPatterns, aFilterPatterns, "INVALID FILTER_DESCRIPTION WITH QUOTES", aAllowMultipleSelects);
  2831. for (i = 0; i < aNumOfFilterPatterns; i++)
  2832. {
  2833. if (tfd_quoteDetected(aFilterPatterns[i])) return tinyfd_openFileDialog("INVALID FILTER_PATTERN WITH QUOTES: use the GRAVE ACCENT \\x60 instead.", aDefaultPathAndOrFile, 0, NULL, NULL, aAllowMultipleSelects);
  2834. }
  2835. if ( ( !tinyfd_forceConsole || !( GetConsoleWindow() || dialogPresent() ) )
  2836. && (!getenv("SSH_CLIENT") || getenvDISPLAY()))
  2837. {
  2838. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char *)1;}
  2839. p = openFileDialogWinGui( aTitle, aDefaultPathAndOrFile, aNumOfFilterPatterns,
  2840. (char const * const *)aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects);
  2841. }
  2842. else if (dialogPresent())
  2843. {
  2844. if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "dialog"); return (char *)0; }
  2845. p = openFileDialogWinConsole(aTitle, aDefaultPathAndOrFile);
  2846. }
  2847. else
  2848. {
  2849. if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "basicinput"); return (char *)0; }
  2850. strcpy(lBuff, "Open file from ");
  2851. strcat(lBuff, getCurDir());
  2852. lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */
  2853. if (lPointerInputBox) strcpy(lString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */
  2854. p = tinyfd_inputBox(aTitle, lBuff, "");
  2855. if (p) strcpy(lBuff, p); else lBuff[0] = '\0';
  2856. if (lPointerInputBox) strcpy(lPointerInputBox, lString); /* restore its previous content to tinyfd_inputBox */
  2857. p = lBuff;
  2858. }
  2859. if ( ! p || ! strlen( p ) )
  2860. {
  2861. return NULL;
  2862. }
  2863. if ( aAllowMultipleSelects && strchr(p, '|') )
  2864. {
  2865. p = ensureFilesExist( (char *) p , p ) ;
  2866. }
  2867. else if ( ! fileExists(p) )
  2868. {
  2869. return NULL ;
  2870. }
  2871. /* printf( "lBuff3: %s\n" , p ) ; */
  2872. return p ;
  2873. }
  2874. char * tinyfd_selectFolderDialog(
  2875. char const * aTitle , /* NULL or "" */
  2876. char const * aDefaultPath ) /* NULL or "" */
  2877. {
  2878. static char lBuff[MAX_PATH_OR_CMD];
  2879. char * p;
  2880. char * lPointerInputBox;
  2881. char lString[MAX_PATH_OR_CMD];
  2882. if (tfd_quoteDetected(aTitle)) return tinyfd_selectFolderDialog("INVALID TITLE WITH QUOTES", aDefaultPath);
  2883. if (tfd_quoteDetected(aDefaultPath)) return tinyfd_selectFolderDialog(aTitle, "INVALID DEFAULT_PATH WITH QUOTES");
  2884. if ( ( !tinyfd_forceConsole || !( GetConsoleWindow() || dialogPresent() ) )
  2885. && (!getenv("SSH_CLIENT") || getenvDISPLAY()))
  2886. {
  2887. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char *)1;}
  2888. p = selectFolderDialogWinGui(lBuff, aTitle, aDefaultPath);
  2889. }
  2890. else
  2891. if (dialogPresent())
  2892. {
  2893. if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "dialog"); return (char *)0; }
  2894. p = selectFolderDialogWinConsole(lBuff, aTitle, aDefaultPath);
  2895. }
  2896. else
  2897. {
  2898. if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "basicinput"); return (char *)0; }
  2899. strcpy(lBuff, "Select folder from ");
  2900. strcat(lBuff, getCurDir());
  2901. lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */
  2902. if (lPointerInputBox) strcpy(lString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */
  2903. p = tinyfd_inputBox(aTitle, lBuff, "");
  2904. if (p) strcpy(lBuff, p); else lBuff[0] = '\0';
  2905. if (lPointerInputBox) strcpy(lPointerInputBox, lString); /* restore its previous content to tinyfd_inputBox */
  2906. p = lBuff;
  2907. }
  2908. if ( ! p || ! strlen( p ) || ! dirExists( p ) )
  2909. {
  2910. return NULL ;
  2911. }
  2912. return p ;
  2913. }
  2914. /* aDefaultRGB is used only if aDefaultHexRGB is absent */
  2915. /* aDefaultRGB and aoResultRGB can be the same array */
  2916. /* returns NULL on cancel */
  2917. /* returns the hexcolor as a string "#FF0000" */
  2918. /* aoResultRGB also contains the result */
  2919. char * tinyfd_colorChooser(
  2920. char const * aTitle, /* NULL or "" */
  2921. char const * aDefaultHexRGB, /* NULL or "" or "#FF0000"*/
  2922. unsigned char const aDefaultRGB[3], /* { 0 , 255 , 255 } */
  2923. unsigned char aoResultRGB[3]) /* { 0 , 0 , 0 } */
  2924. {
  2925. static char lDefaultHexRGB[16];
  2926. int i;
  2927. char * p ;
  2928. char * lPointerInputBox;
  2929. char lString[MAX_PATH_OR_CMD];
  2930. lDefaultHexRGB[0] = '\0';
  2931. if (tfd_quoteDetected(aTitle)) return tinyfd_colorChooser("INVALID TITLE WITH QUOTES", aDefaultHexRGB, aDefaultRGB, aoResultRGB);
  2932. if (tfd_quoteDetected(aDefaultHexRGB)) return tinyfd_colorChooser(aTitle, "INVALID DEFAULT_HEX_RGB WITH QUOTES: use the GRAVE ACCENT \\x60 instead.", aDefaultRGB, aoResultRGB);
  2933. if ( (!tinyfd_forceConsole || !( GetConsoleWindow() || dialogPresent()) )
  2934. && (!getenv("SSH_CLIENT") || getenvDISPLAY()))
  2935. {
  2936. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char *)1;}
  2937. p = colorChooserWinGui(aTitle, aDefaultHexRGB, aDefaultRGB, aoResultRGB);
  2938. if (p)
  2939. {
  2940. strcpy(lDefaultHexRGB, p);
  2941. return lDefaultHexRGB;
  2942. }
  2943. return NULL;
  2944. }
  2945. else if (dialogPresent())
  2946. {
  2947. if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "dialog"); return (char *)0; }
  2948. }
  2949. else
  2950. {
  2951. if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "basicinput"); return (char *)0; }
  2952. }
  2953. if (aDefaultHexRGB && (strlen(aDefaultHexRGB)==7) )
  2954. {
  2955. strncpy(lDefaultHexRGB, aDefaultHexRGB,7);
  2956. lDefaultHexRGB[7]='\0';
  2957. }
  2958. else
  2959. {
  2960. RGB2Hex(aDefaultRGB, lDefaultHexRGB);
  2961. }
  2962. lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */
  2963. if (lPointerInputBox) strcpy(lString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */
  2964. p = tinyfd_inputBox(aTitle, "Enter hex rgb color (i.e. #f5ca20)", lDefaultHexRGB);
  2965. if ( !p || (strlen(p) != 7) || (p[0] != '#') )
  2966. {
  2967. return NULL ;
  2968. }
  2969. for ( i = 1 ; i < 7 ; i ++ )
  2970. {
  2971. if ( ! isxdigit( (int) p[i] ) )
  2972. {
  2973. return NULL ;
  2974. }
  2975. }
  2976. Hex2RGB(p,aoResultRGB);
  2977. strcpy(lDefaultHexRGB, p);
  2978. if (lPointerInputBox) strcpy(lPointerInputBox, lString); /* restore its previous content to tinyfd_inputBox */
  2979. return lDefaultHexRGB;
  2980. }
  2981. #else /* unix */
  2982. static char gPython2Name[16];
  2983. static char gPython3Name[16];
  2984. static char gPythonName[16];
  2985. int tfd_isDarwin(void)
  2986. {
  2987. static int lsIsDarwin = -1 ;
  2988. struct utsname lUtsname ;
  2989. if ( lsIsDarwin < 0 )
  2990. {
  2991. lsIsDarwin = !uname(&lUtsname) && !strcmp(lUtsname.sysname,"Darwin") ;
  2992. }
  2993. return lsIsDarwin ;
  2994. }
  2995. static int dirExists( char const * aDirPath )
  2996. {
  2997. DIR * lDir ;
  2998. if ( ! aDirPath || ! strlen( aDirPath ) )
  2999. return 0 ;
  3000. lDir = opendir( aDirPath ) ;
  3001. if ( ! lDir )
  3002. return 0 ;
  3003. closedir( lDir ) ;
  3004. return 1 ;
  3005. }
  3006. static int detectPresence( char const * aExecutable )
  3007. {
  3008. char lBuff[MAX_PATH_OR_CMD] ;
  3009. char lTestedString[MAX_PATH_OR_CMD] = "command -v " ;
  3010. FILE * lIn ;
  3011. #ifdef _GNU_SOURCE
  3012. char* lAllocatedCharString;
  3013. int lSubstringUndetected;
  3014. #endif
  3015. strcat( lTestedString , aExecutable ) ;
  3016. strcat( lTestedString, " 2>/dev/null ");
  3017. lIn = popen( lTestedString , "r" ) ;
  3018. if ( ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL )
  3019. && ( ! strchr( lBuff , ':' ) ) && ( strncmp(lBuff, "no ", 3) ) )
  3020. { /* present */
  3021. pclose( lIn ) ;
  3022. #ifdef _GNU_SOURCE /*to bypass this, just comment out "#define _GNU_SOURCE" at the top of the file*/
  3023. if ( lBuff[strlen( lBuff ) -1] == '\n' ) lBuff[strlen( lBuff ) -1] = '\0' ;
  3024. lAllocatedCharString = realpath(lBuff,NULL); /*same as canonicalize_file_name*/
  3025. lSubstringUndetected = ! strstr(lAllocatedCharString, aExecutable);
  3026. free(lAllocatedCharString);
  3027. if (lSubstringUndetected)
  3028. {
  3029. if (tinyfd_verbose) printf("detectPresence %s %d\n", aExecutable, 0);
  3030. return 0;
  3031. }
  3032. #endif /*_GNU_SOURCE*/
  3033. if (tinyfd_verbose) printf("detectPresence %s %d\n", aExecutable, 1);
  3034. return 1 ;
  3035. }
  3036. else
  3037. {
  3038. pclose( lIn ) ;
  3039. if (tinyfd_verbose) printf("detectPresence %s %d\n", aExecutable, 0);
  3040. return 0 ;
  3041. }
  3042. }
  3043. static char * getVersion( char const * aExecutable ) /*version must be first numeral*/
  3044. {
  3045. static char lBuff[MAX_PATH_OR_CMD] ;
  3046. char lTestedString[MAX_PATH_OR_CMD] ;
  3047. FILE * lIn ;
  3048. char * lTmp ;
  3049. strcpy( lTestedString , aExecutable ) ;
  3050. strcat( lTestedString , " --version" ) ;
  3051. lIn = popen( lTestedString , "r" ) ;
  3052. lTmp = fgets( lBuff , sizeof( lBuff ) , lIn ) ;
  3053. pclose( lIn ) ;
  3054. lTmp += strcspn(lTmp,"0123456789");
  3055. /* printf("lTmp:%s\n", lTmp); */
  3056. return lTmp ;
  3057. }
  3058. static int * getMajorMinorPatch( char const * aExecutable )
  3059. {
  3060. static int lArray[3] ;
  3061. char * lTmp ;
  3062. lTmp = (char *) getVersion(aExecutable);
  3063. lArray[0] = atoi( strtok(lTmp," ,.-") ) ;
  3064. /* printf("lArray0 %d\n", lArray[0]); */
  3065. lArray[1] = atoi( strtok(0," ,.-") ) ;
  3066. /* printf("lArray1 %d\n", lArray[1]); */
  3067. lArray[2] = atoi( strtok(0," ,.-") ) ;
  3068. /* printf("lArray2 %d\n", lArray[2]); */
  3069. if ( !lArray[0] && !lArray[1] && !lArray[2] ) return NULL;
  3070. return lArray ;
  3071. }
  3072. static int tryCommand( char const * aCommand )
  3073. {
  3074. char lBuff[MAX_PATH_OR_CMD] ;
  3075. FILE * lIn ;
  3076. lIn = popen( aCommand , "r" ) ;
  3077. if ( fgets( lBuff , sizeof( lBuff ) , lIn ) == NULL )
  3078. { /* present */
  3079. pclose( lIn ) ;
  3080. return 1 ;
  3081. }
  3082. else
  3083. {
  3084. pclose( lIn ) ;
  3085. return 0 ;
  3086. }
  3087. }
  3088. static int isTerminalRunning(void)
  3089. {
  3090. static int lIsTerminalRunning = -1 ;
  3091. if ( lIsTerminalRunning < 0 )
  3092. {
  3093. lIsTerminalRunning = isatty(1);
  3094. if (tinyfd_verbose) printf("isTerminalRunning %d\n", lIsTerminalRunning );
  3095. }
  3096. return lIsTerminalRunning;
  3097. }
  3098. static char * dialogNameOnly(void)
  3099. {
  3100. static char lDialogName[128] = "*" ;
  3101. if ( lDialogName[0] == '*' )
  3102. {
  3103. if (!tinyfd_allowCursesDialogs)
  3104. {
  3105. strcpy(lDialogName , "" );
  3106. }
  3107. else if ( tfd_isDarwin() && * strcpy(lDialogName , "/opt/local/bin/dialog" )
  3108. && detectPresence( lDialogName ) )
  3109. {}
  3110. else if ( * strcpy(lDialogName , "dialog" )
  3111. && detectPresence( lDialogName ) )
  3112. {}
  3113. else
  3114. {
  3115. strcpy(lDialogName , "" );
  3116. }
  3117. }
  3118. return lDialogName ;
  3119. }
  3120. int isDialogVersionBetter09b(void)
  3121. {
  3122. char const * lDialogName ;
  3123. char * lVersion ;
  3124. int lMajor ;
  3125. int lMinor ;
  3126. int lDate ;
  3127. int lResult ;
  3128. char * lMinorP ;
  3129. char * lLetter ;
  3130. char lBuff[128] ;
  3131. /*char lTest[128] = " 0.9b-20031126" ;*/
  3132. lDialogName = dialogNameOnly() ;
  3133. if ( ! strlen(lDialogName) || !(lVersion = (char *) getVersion(lDialogName)) ) return 0 ;
  3134. /*lVersion = lTest ;*/
  3135. /*printf("lVersion %s\n", lVersion);*/
  3136. strcpy(lBuff,lVersion);
  3137. lMajor = atoi( strtok(lVersion," ,.-") ) ;
  3138. /*printf("lMajor %d\n", lMajor);*/
  3139. lMinorP = strtok(0," ,.-abcdefghijklmnopqrstuvxyz");
  3140. lMinor = atoi( lMinorP ) ;
  3141. /*printf("lMinor %d\n", lMinor );*/
  3142. lDate = atoi( strtok(0," ,.-") ) ;
  3143. if (lDate<0) lDate = - lDate;
  3144. /*printf("lDate %d\n", lDate);*/
  3145. lLetter = lMinorP + strlen(lMinorP) ;
  3146. strcpy(lVersion,lBuff);
  3147. strtok(lLetter," ,.-");
  3148. /*printf("lLetter %s\n", lLetter);*/
  3149. lResult = (lMajor > 0) || ( ( lMinor == 9 ) && (*lLetter == 'b') && (lDate >= 20031126) );
  3150. /*printf("lResult %d\n", lResult);*/
  3151. return lResult;
  3152. }
  3153. static int whiptailPresentOnly(void)
  3154. {
  3155. static int lWhiptailPresent = -1 ;
  3156. if (!tinyfd_allowCursesDialogs) return 0;
  3157. if ( lWhiptailPresent < 0 )
  3158. {
  3159. lWhiptailPresent = detectPresence( "whiptail" ) ;
  3160. }
  3161. return lWhiptailPresent ;
  3162. }
  3163. static char * terminalName(void)
  3164. {
  3165. static char lTerminalName[128] = "*" ;
  3166. char lShellName[64] = "*" ;
  3167. int * lArray;
  3168. if ( lTerminalName[0] == '*' )
  3169. {
  3170. if ( detectPresence( "bash" ) )
  3171. {
  3172. strcpy(lShellName , "bash -c " ) ; /*good for basic input*/
  3173. }
  3174. else if ( strlen(dialogNameOnly()) || whiptailPresentOnly() )
  3175. {
  3176. strcpy(lShellName , "sh -c " ) ; /*good enough for dialog & whiptail*/
  3177. }
  3178. else
  3179. {
  3180. strcpy(lTerminalName , "" ) ;
  3181. return NULL ;
  3182. }
  3183. if ( tfd_isDarwin() )
  3184. {
  3185. if ( * strcpy(lTerminalName , "/opt/X11/bin/xterm" )
  3186. && detectPresence( lTerminalName ) )
  3187. {
  3188. strcat(lTerminalName , " -fa 'DejaVu Sans Mono' -fs 10 -title tinyfiledialogs -e " ) ;
  3189. strcat(lTerminalName , lShellName ) ;
  3190. }
  3191. else
  3192. {
  3193. strcpy(lTerminalName , "" ) ;
  3194. }
  3195. }
  3196. else if ( * strcpy(lTerminalName,"xterm") /*good (small without parameters)*/
  3197. && detectPresence(lTerminalName) )
  3198. {
  3199. strcat(lTerminalName , " -fa 'DejaVu Sans Mono' -fs 10 -title tinyfiledialogs -e " ) ;
  3200. strcat(lTerminalName , lShellName ) ;
  3201. }
  3202. else if ( * strcpy(lTerminalName,"terminator") /*good*/
  3203. && detectPresence(lTerminalName) )
  3204. {
  3205. strcat(lTerminalName , " -x " ) ;
  3206. strcat(lTerminalName , lShellName ) ;
  3207. }
  3208. else if ( * strcpy(lTerminalName,"lxterminal") /*good*/
  3209. && detectPresence(lTerminalName) )
  3210. {
  3211. strcat(lTerminalName , " -e " ) ;
  3212. strcat(lTerminalName , lShellName ) ;
  3213. }
  3214. else if ( * strcpy(lTerminalName,"konsole") /*good*/
  3215. && detectPresence(lTerminalName) )
  3216. {
  3217. strcat(lTerminalName , " -e " ) ;
  3218. strcat(lTerminalName , lShellName ) ;
  3219. }
  3220. else if ( * strcpy(lTerminalName,"kterm") /*good*/
  3221. && detectPresence(lTerminalName) )
  3222. {
  3223. strcat(lTerminalName , " -e " ) ;
  3224. strcat(lTerminalName , lShellName ) ;
  3225. }
  3226. else if ( * strcpy(lTerminalName,"tilix") /*good*/
  3227. && detectPresence(lTerminalName) )
  3228. {
  3229. strcat(lTerminalName , " -e " ) ;
  3230. strcat(lTerminalName , lShellName ) ;
  3231. }
  3232. else if ( * strcpy(lTerminalName,"xfce4-terminal") /*good*/
  3233. && detectPresence(lTerminalName) )
  3234. {
  3235. strcat(lTerminalName , " -x " ) ;
  3236. strcat(lTerminalName , lShellName ) ;
  3237. }
  3238. else if ( * strcpy(lTerminalName,"mate-terminal") /*good*/
  3239. && detectPresence(lTerminalName) )
  3240. {
  3241. strcat(lTerminalName , " -x " ) ;
  3242. strcat(lTerminalName , lShellName ) ;
  3243. }
  3244. else if ( * strcpy(lTerminalName,"Eterm") /*good*/
  3245. && detectPresence(lTerminalName) )
  3246. {
  3247. strcat(lTerminalName , " -e " ) ;
  3248. strcat(lTerminalName , lShellName ) ;
  3249. }
  3250. else if ( * strcpy(lTerminalName,"evilvte") /*good*/
  3251. && detectPresence(lTerminalName) )
  3252. {
  3253. strcat(lTerminalName , " -e " ) ;
  3254. strcat(lTerminalName , lShellName ) ;
  3255. }
  3256. else if ( * strcpy(lTerminalName,"pterm") /*good (only letters)*/
  3257. && detectPresence(lTerminalName) )
  3258. {
  3259. strcat(lTerminalName , " -e " ) ;
  3260. strcat(lTerminalName , lShellName ) ;
  3261. }
  3262. else if ( * strcpy(lTerminalName,"gnome-terminal")
  3263. && detectPresence(lTerminalName) && (lArray = getMajorMinorPatch(lTerminalName))
  3264. && ((lArray[0]<3) || (lArray[0]==3 && lArray[1]<=6)) )
  3265. {
  3266. strcat(lTerminalName , " --disable-factory -x " ) ;
  3267. strcat(lTerminalName , lShellName ) ;
  3268. }
  3269. else
  3270. {
  3271. strcpy(lTerminalName , "" ) ;
  3272. }
  3273. /* bad: koi rxterm guake tilda vala-terminal qterminal kgx
  3274. aterm Terminal terminology sakura lilyterm weston-terminal
  3275. roxterm termit xvt rxvt mrxvt urxvt */
  3276. }
  3277. if ( strlen(lTerminalName) )
  3278. {
  3279. return lTerminalName ;
  3280. }
  3281. else
  3282. {
  3283. return NULL ;
  3284. }
  3285. }
  3286. static char * dialogName(void)
  3287. {
  3288. char * lDialogName ;
  3289. lDialogName = dialogNameOnly( ) ;
  3290. if ( strlen(lDialogName) && ( isTerminalRunning() || terminalName() ) )
  3291. {
  3292. return lDialogName ;
  3293. }
  3294. else
  3295. {
  3296. return NULL ;
  3297. }
  3298. }
  3299. static int whiptailPresent(void)
  3300. {
  3301. int lWhiptailPresent ;
  3302. lWhiptailPresent = whiptailPresentOnly( ) ;
  3303. if ( lWhiptailPresent && ( isTerminalRunning() || terminalName() ) )
  3304. {
  3305. return lWhiptailPresent ;
  3306. }
  3307. else
  3308. {
  3309. return 0 ;
  3310. }
  3311. }
  3312. static int graphicMode(void)
  3313. {
  3314. return !( tinyfd_forceConsole && (isTerminalRunning() || terminalName()) )
  3315. && ( getenvDISPLAY()
  3316. || (tfd_isDarwin() && (!getenv("SSH_TTY") || getenvDISPLAY() ) ) ) ;
  3317. }
  3318. static int ffplayPresent(void)
  3319. {
  3320. static int lFFplayPresent = -1;
  3321. if (lFFplayPresent < 0)
  3322. {
  3323. lFFplayPresent = detectPresence("ffplay");
  3324. }
  3325. return lFFplayPresent;
  3326. }
  3327. static int pactlPresent( void )
  3328. {
  3329. static int lPactlPresent = -1 ;
  3330. char lBuff [256] ;
  3331. FILE * lIn ;
  3332. if ( lPactlPresent < 0 )
  3333. {
  3334. lPactlPresent = detectPresence("pactl") ;
  3335. if ( lPactlPresent )
  3336. {
  3337. lIn = popen( "pactl info | grep -iF pulseaudio" , "r" ) ;
  3338. if ( ! (fgets( lBuff , sizeof( lBuff ) , lIn ) && ! strstr(lBuff, "PipeWire") ) )
  3339. {
  3340. lPactlPresent = 0 ;
  3341. }
  3342. pclose( lIn ) ;
  3343. if (tinyfd_verbose) printf("is pactl valid ? %d\n", lPactlPresent);
  3344. }
  3345. }
  3346. return lPactlPresent ;
  3347. }
  3348. static int speakertestPresent(void)
  3349. {
  3350. static int lSpeakertestPresent = -1 ;
  3351. if ( lSpeakertestPresent < 0 )
  3352. {
  3353. lSpeakertestPresent = detectPresence("speaker-test") ;
  3354. }
  3355. return lSpeakertestPresent ;
  3356. }
  3357. static int playPresent(void) /* play is part of sox */
  3358. {
  3359. static int lPlayPresent = -1;
  3360. if (lPlayPresent < 0)
  3361. {
  3362. lPlayPresent = detectPresence("sox"); /*if sox is present, play is ready*/
  3363. }
  3364. return lPlayPresent;
  3365. }
  3366. static int beepexePresent(void)
  3367. {
  3368. static int lBeepexePresent = -1;
  3369. if (lBeepexePresent < 0)
  3370. {
  3371. lBeepexePresent = detectPresence("beep.exe");
  3372. }
  3373. return lBeepexePresent;
  3374. }
  3375. /*static int beepPresent(void)
  3376. {
  3377. static int lBeepPresent = -1 ;
  3378. if ( lBeepPresent < 0 )
  3379. {
  3380. lBeepPresent = detectPresence("beep") ;
  3381. }
  3382. return lBeepPresent ;
  3383. }*/
  3384. static int playsoundPresent(void) /* playsound is part of pipewire */
  3385. {
  3386. static int lPlaysoundPresent = -1 ;
  3387. if (lPlaysoundPresent < 0)
  3388. {
  3389. lPlaysoundPresent = detectPresence("playsound_simple");
  3390. if ( lPlaysoundPresent && ! fileExists("/usr/share/sounds/freedesktop/stereo/bell.oga") )
  3391. {
  3392. lPlaysoundPresent = 0 ;
  3393. }
  3394. }
  3395. return lPlaysoundPresent;
  3396. }
  3397. static int paplayPresent(void) /* playsound is part of pipewire */
  3398. {
  3399. static int lPaplayPresent = -1 ;
  3400. if (lPaplayPresent < 0)
  3401. {
  3402. lPaplayPresent = detectPresence("paplay");
  3403. if ( lPaplayPresent && ! fileExists("/usr/share/sounds/freedesktop/stereo/bell.oga") )
  3404. {
  3405. lPaplayPresent = 0 ;
  3406. }
  3407. }
  3408. return lPaplayPresent;
  3409. }
  3410. static int xmessagePresent(void)
  3411. {
  3412. static int lXmessagePresent = -1 ;
  3413. if ( lXmessagePresent < 0 )
  3414. {
  3415. lXmessagePresent = detectPresence("xmessage");/*if not tty,not on osxpath*/
  3416. }
  3417. return lXmessagePresent && graphicMode( ) ;
  3418. }
  3419. static int gxmessagePresent(void)
  3420. {
  3421. static int lGxmessagePresent = -1 ;
  3422. if ( lGxmessagePresent < 0 )
  3423. {
  3424. lGxmessagePresent = detectPresence("gxmessage") ;
  3425. }
  3426. return lGxmessagePresent && graphicMode( ) ;
  3427. }
  3428. static int gmessagePresent(void)
  3429. {
  3430. static int lGmessagePresent = -1 ;
  3431. if ( lGmessagePresent < 0 )
  3432. {
  3433. lGmessagePresent = detectPresence("gmessage") ;
  3434. }
  3435. return lGmessagePresent && graphicMode( ) ;
  3436. }
  3437. static int notifysendPresent(void)
  3438. {
  3439. static int lNotifysendPresent = -1 ;
  3440. if ( lNotifysendPresent < 0 )
  3441. {
  3442. lNotifysendPresent = detectPresence("notify-send") ;
  3443. }
  3444. return lNotifysendPresent && graphicMode( ) ;
  3445. }
  3446. static int perlPresent(void)
  3447. {
  3448. static int lPerlPresent = -1 ;
  3449. char lBuff[MAX_PATH_OR_CMD] ;
  3450. FILE * lIn ;
  3451. if ( lPerlPresent < 0 )
  3452. {
  3453. lPerlPresent = detectPresence("perl") ;
  3454. if (lPerlPresent)
  3455. {
  3456. lIn = popen("perl -MNet::DBus -e \"Net::DBus->session->get_service('org.freedesktop.Notifications')\" 2>&1", "r");
  3457. if (fgets(lBuff, sizeof(lBuff), lIn) == NULL)
  3458. {
  3459. lPerlPresent = 2;
  3460. }
  3461. pclose(lIn);
  3462. if (tinyfd_verbose) printf("perl-dbus %d\n", lPerlPresent);
  3463. }
  3464. }
  3465. return graphicMode() ? lPerlPresent : 0 ;
  3466. }
  3467. static int afplayPresent(void)
  3468. {
  3469. static int lAfplayPresent = -1 ;
  3470. char lBuff[MAX_PATH_OR_CMD] ;
  3471. FILE * lIn ;
  3472. if ( lAfplayPresent < 0 )
  3473. {
  3474. lAfplayPresent = detectPresence("afplay") ;
  3475. if ( lAfplayPresent )
  3476. {
  3477. lIn = popen( "test -e /System/Library/Sounds/Ping.aiff || echo Ping" , "r" ) ;
  3478. if ( fgets( lBuff , sizeof( lBuff ) , lIn ) == NULL )
  3479. {
  3480. lAfplayPresent = 2 ;
  3481. }
  3482. pclose( lIn ) ;
  3483. if (tinyfd_verbose) printf("afplay %d\n", lAfplayPresent);
  3484. }
  3485. }
  3486. return graphicMode() ? lAfplayPresent : 0 ;
  3487. }
  3488. static int xdialogPresent(void)
  3489. {
  3490. static int lXdialogPresent = -1 ;
  3491. if ( lXdialogPresent < 0 )
  3492. {
  3493. lXdialogPresent = detectPresence("Xdialog") ;
  3494. }
  3495. return lXdialogPresent && graphicMode( ) ;
  3496. }
  3497. static int gdialogPresent(void)
  3498. {
  3499. static int lGdialoglPresent = -1 ;
  3500. if ( lGdialoglPresent < 0 )
  3501. {
  3502. lGdialoglPresent = detectPresence( "gdialog" ) ;
  3503. }
  3504. return lGdialoglPresent && graphicMode( ) ;
  3505. }
  3506. static int osascriptPresent(void)
  3507. {
  3508. static int lOsascriptPresent = -1 ;
  3509. if ( lOsascriptPresent < 0 )
  3510. {
  3511. gWarningDisplayed |= !!getenv("SSH_TTY");
  3512. lOsascriptPresent = detectPresence( "osascript" ) ;
  3513. }
  3514. return lOsascriptPresent && graphicMode() && !getenv("SSH_TTY") ;
  3515. }
  3516. static int dunstifyPresent(void)
  3517. {
  3518. static int lDunstifyPresent = -1 ;
  3519. static char lBuff[MAX_PATH_OR_CMD] ;
  3520. FILE * lIn ;
  3521. char * lTmp ;
  3522. if ( lDunstifyPresent < 0 )
  3523. {
  3524. lDunstifyPresent = detectPresence( "dunstify" ) ;
  3525. if ( lDunstifyPresent )
  3526. {
  3527. lIn = popen( "dunstify -s" , "r" ) ;
  3528. lTmp = fgets( lBuff , sizeof( lBuff ) , lIn ) ;
  3529. pclose( lIn ) ;
  3530. /* printf("lTmp:%s\n", lTmp); */
  3531. lDunstifyPresent = strstr(lTmp,"name:dunst\n") ? 1 : 0 ;
  3532. if (tinyfd_verbose) printf("lDunstifyPresent %d\n", lDunstifyPresent);
  3533. }
  3534. }
  3535. return lDunstifyPresent && graphicMode( ) ;
  3536. }
  3537. static int dunstPresent(void)
  3538. {
  3539. static int lDunstPresent = -1 ;
  3540. static char lBuff[MAX_PATH_OR_CMD] ;
  3541. FILE * lIn ;
  3542. char * lTmp ;
  3543. if ( lDunstPresent < 0 )
  3544. {
  3545. lDunstPresent = detectPresence( "dunst" ) ;
  3546. if ( lDunstPresent )
  3547. {
  3548. lIn = popen( "ps -e | grep dunst | grep -v grep" , "r" ) ; /* add "| wc -l" to receive the number of lines */
  3549. lTmp = fgets( lBuff , sizeof( lBuff ) , lIn ) ;
  3550. pclose( lIn ) ;
  3551. /* if ( lTmp ) printf("lTmp:%s\n", lTmp); */
  3552. if ( lTmp ) lDunstPresent = 1 ;
  3553. else lDunstPresent = 0 ;
  3554. if (tinyfd_verbose) printf("lDunstPresent %d\n", lDunstPresent);
  3555. }
  3556. }
  3557. return lDunstPresent && graphicMode( ) ;
  3558. }
  3559. int tfd_qarmaPresent(void)
  3560. {
  3561. static int lQarmaPresent = -1 ;
  3562. if ( lQarmaPresent < 0 )
  3563. {
  3564. lQarmaPresent = detectPresence("qarma") ;
  3565. }
  3566. return lQarmaPresent && graphicMode( ) ;
  3567. }
  3568. int tfd_matedialogPresent(void)
  3569. {
  3570. static int lMatedialogPresent = -1 ;
  3571. if ( lMatedialogPresent < 0 )
  3572. {
  3573. lMatedialogPresent = detectPresence("matedialog") ;
  3574. }
  3575. return lMatedialogPresent && graphicMode( ) ;
  3576. }
  3577. int tfd_shellementaryPresent(void)
  3578. {
  3579. static int lShellementaryPresent = -1 ;
  3580. if ( lShellementaryPresent < 0 )
  3581. {
  3582. lShellementaryPresent = 0 ; /*detectPresence("shellementary"); shellementary is not ready yet */
  3583. }
  3584. return lShellementaryPresent && graphicMode( ) ;
  3585. }
  3586. int tfd_xpropPresent(void)
  3587. {
  3588. static int lXpropReady = 0 ;
  3589. static int lXpropDetected = -1 ;
  3590. char lBuff[MAX_PATH_OR_CMD] ;
  3591. FILE * lIn ;
  3592. if ( lXpropDetected < 0 )
  3593. {
  3594. if ( getenvDISPLAY() & 1 ) lXpropDetected = detectPresence("xprop") ; /* bitwise & */
  3595. else lXpropDetected = 0 ;
  3596. }
  3597. if ( ! lXpropDetected ) return 0 ;
  3598. if ( ! lXpropReady )
  3599. { /* xwayland Debian issue reported by Kay F. Jahnke and solved with his help */
  3600. lIn = popen( "xprop -root 32x ' $0' _NET_ACTIVE_WINDOW" , "r" ) ;
  3601. if ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL )
  3602. {
  3603. if ( ! strstr( lBuff , "not found" ) )
  3604. {
  3605. if (tinyfd_verbose) printf("xprop is ready\n");
  3606. lXpropReady = 1 ;
  3607. }
  3608. }
  3609. pclose( lIn ) ;
  3610. }
  3611. return graphicMode() ? lXpropReady : 0 ;
  3612. }
  3613. int tfd_zenityPresent(void)
  3614. {
  3615. static int lZenityPresent = -1 ;
  3616. if ( lZenityPresent < 0 )
  3617. {
  3618. lZenityPresent = detectPresence("zenity") ;
  3619. }
  3620. return lZenityPresent && graphicMode( ) ;
  3621. }
  3622. int tfd_yadPresent(void)
  3623. {
  3624. static int lYadPresent = -1;
  3625. if (lYadPresent < 0)
  3626. {
  3627. lYadPresent = detectPresence("yad");
  3628. }
  3629. return lYadPresent && graphicMode();
  3630. }
  3631. int tfd_zenity3Present(void)
  3632. {
  3633. static int lZenity3Present = -1 ;
  3634. char lBuff[MAX_PATH_OR_CMD] ;
  3635. FILE * lIn ;
  3636. int lIntTmp ;
  3637. if ( lZenity3Present < 0 )
  3638. {
  3639. lZenity3Present = 0 ;
  3640. if ( tfd_zenityPresent() )
  3641. {
  3642. lIn = popen( "zenity --version" , "r" ) ;
  3643. if ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL )
  3644. {
  3645. if ( atoi(lBuff) >= 3 )
  3646. {
  3647. lZenity3Present = 3 ;
  3648. lIntTmp = atoi(strtok(lBuff,".")+2 ) ;
  3649. if ( lIntTmp >= 18 )
  3650. {
  3651. lZenity3Present = 5 ;
  3652. }
  3653. else if ( lIntTmp >= 10 )
  3654. {
  3655. lZenity3Present = 4 ;
  3656. }
  3657. }
  3658. else if ( ( atoi(lBuff) == 2 ) && ( atoi(strtok(lBuff,".")+2 ) >= 32 ) )
  3659. {
  3660. lZenity3Present = 2 ;
  3661. }
  3662. if (tinyfd_verbose) printf("zenity type %d\n", lZenity3Present);
  3663. }
  3664. pclose( lIn ) ;
  3665. }
  3666. }
  3667. return graphicMode() ? lZenity3Present : 0 ;
  3668. }
  3669. int tfd_kdialogPresent(void)
  3670. {
  3671. static int lKdialogPresent = -1 ;
  3672. char lBuff[MAX_PATH_OR_CMD] ;
  3673. FILE * lIn ;
  3674. char * lDesktop;
  3675. if ( lKdialogPresent < 0 )
  3676. {
  3677. lDesktop = getenv("XDG_SESSION_DESKTOP");
  3678. if ( !lDesktop || ( strcmp(lDesktop, "KDE") && strcmp(lDesktop, "lxqt") ) )
  3679. {
  3680. if ( tfd_zenityPresent() )
  3681. {
  3682. lKdialogPresent = 0 ;
  3683. return lKdialogPresent ;
  3684. }
  3685. }
  3686. lKdialogPresent = detectPresence("kdialog") ;
  3687. if ( lKdialogPresent && !getenv("SSH_TTY") )
  3688. {
  3689. lIn = popen( "kdialog --attach 2>&1" , "r" ) ;
  3690. if ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL )
  3691. {
  3692. if ( ! strstr( "Unknown" , lBuff ) )
  3693. {
  3694. lKdialogPresent = 2 ;
  3695. if (tinyfd_verbose) printf("kdialog-attach %d\n", lKdialogPresent);
  3696. }
  3697. }
  3698. pclose( lIn ) ;
  3699. if (lKdialogPresent == 2)
  3700. {
  3701. lKdialogPresent = 1 ;
  3702. lIn = popen( "kdialog --passivepopup 2>&1" , "r" ) ;
  3703. if ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL )
  3704. {
  3705. if ( ! strstr( "Unknown" , lBuff ) )
  3706. {
  3707. lKdialogPresent = 2 ;
  3708. if (tinyfd_verbose) printf("kdialog-popup %d\n", lKdialogPresent);
  3709. }
  3710. }
  3711. pclose( lIn ) ;
  3712. }
  3713. }
  3714. }
  3715. return graphicMode() ? lKdialogPresent : 0 ;
  3716. }
  3717. static int osx9orBetter(void)
  3718. {
  3719. static int lOsx9orBetter = -1 ;
  3720. char lBuff[MAX_PATH_OR_CMD] ;
  3721. FILE * lIn ;
  3722. int V,v;
  3723. if ( lOsx9orBetter < 0 )
  3724. {
  3725. lOsx9orBetter = 0 ;
  3726. lIn = popen( "osascript -e 'set osver to system version of (system info)'" , "r" ) ;
  3727. V = 0 ;
  3728. if ( ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL )
  3729. && ( 2 == sscanf(lBuff, "%d.%d", &V, &v) ) )
  3730. {
  3731. V = V * 100 + v;
  3732. if ( V >= 1009 )
  3733. {
  3734. lOsx9orBetter = 1 ;
  3735. }
  3736. }
  3737. pclose( lIn ) ;
  3738. if (tinyfd_verbose) printf("Osx10 = %d, %d = %s\n", lOsx9orBetter, V, lBuff) ;
  3739. }
  3740. return lOsx9orBetter ;
  3741. }
  3742. static int python3Present(void)
  3743. {
  3744. static int lPython3Present = -1 ;
  3745. if ( lPython3Present < 0 )
  3746. {
  3747. lPython3Present = 0 ;
  3748. strcpy(gPython3Name , "python3" ) ;
  3749. if ( detectPresence(gPython3Name) ) lPython3Present = 1;
  3750. if (tinyfd_verbose) printf("lPython3Present %d\n", lPython3Present) ;
  3751. if (tinyfd_verbose) printf("gPython3Name %s\n", gPython3Name) ;
  3752. }
  3753. return lPython3Present ;
  3754. }
  3755. static int python2Present(void)
  3756. {
  3757. static int lPython2Present = -1 ;
  3758. if ( lPython2Present < 0 )
  3759. {
  3760. lPython2Present = 0 ;
  3761. strcpy(gPython2Name , "python2" ) ;
  3762. if ( detectPresence(gPython2Name) ) lPython2Present = 1;
  3763. if (tinyfd_verbose) printf("lPython2Present %d\n", lPython2Present) ;
  3764. if (tinyfd_verbose) printf("gPython2Name %s\n", gPython2Name) ;
  3765. }
  3766. return lPython2Present ;
  3767. }
  3768. static int tkinter3Present(void)
  3769. {
  3770. static int lTkinter3Present = -1 ;
  3771. char lPythonCommand[256];
  3772. char lPythonParams[128] =
  3773. "-S -c \"try:\n\timport tkinter;\nexcept:\n\tprint(0);\"";
  3774. if ( lTkinter3Present < 0 )
  3775. {
  3776. lTkinter3Present = 0 ;
  3777. if ( python3Present() )
  3778. {
  3779. sprintf( lPythonCommand , "%s %s" , gPython3Name , lPythonParams ) ;
  3780. lTkinter3Present = tryCommand(lPythonCommand) ;
  3781. }
  3782. if (tinyfd_verbose) printf("lTkinter3Present %d\n", lTkinter3Present) ;
  3783. }
  3784. return lTkinter3Present && graphicMode() && !(tfd_isDarwin() && getenv("SSH_TTY") );
  3785. }
  3786. static int tkinter2Present(void)
  3787. {
  3788. static int lTkinter2Present = -1 ;
  3789. char lPythonCommand[256];
  3790. char lPythonParams[128] =
  3791. "-S -c \"try:\n\timport Tkinter;\nexcept:\n\tprint 0;\"";
  3792. if ( lTkinter2Present < 0 )
  3793. {
  3794. lTkinter2Present = 0 ;
  3795. if ( python2Present() )
  3796. {
  3797. sprintf( lPythonCommand , "%s %s" , gPython2Name , lPythonParams ) ;
  3798. lTkinter2Present = tryCommand(lPythonCommand) ;
  3799. }
  3800. if (tinyfd_verbose) printf("lTkinter2Present %d graphicMode %d \n", lTkinter2Present, graphicMode() ) ;
  3801. }
  3802. return lTkinter2Present && graphicMode() && !(tfd_isDarwin() && getenv("SSH_TTY") );
  3803. }
  3804. static int pythonDbusPresent(void)
  3805. {
  3806. static int lPythonDbusPresent = -1 ;
  3807. char lPythonCommand[384];
  3808. char lPythonParams[256] =
  3809. "-c \"try:\n\timport dbus;bus=dbus.SessionBus();\
  3810. notif=bus.get_object('org.freedesktop.Notifications','/org/freedesktop/Notifications');\
  3811. notify=dbus.Interface(notif,'org.freedesktop.Notifications');\nexcept:\n\tprint(0);\"";
  3812. if (lPythonDbusPresent < 0 )
  3813. {
  3814. lPythonDbusPresent = 0 ;
  3815. if ( python2Present() )
  3816. {
  3817. strcpy(gPythonName , gPython2Name ) ;
  3818. sprintf( lPythonCommand , "%s %s" , gPythonName , lPythonParams ) ;
  3819. lPythonDbusPresent = tryCommand(lPythonCommand) ;
  3820. }
  3821. if ( !lPythonDbusPresent && python3Present() )
  3822. {
  3823. strcpy(gPythonName , gPython3Name ) ;
  3824. sprintf( lPythonCommand , "%s %s" , gPythonName , lPythonParams ) ;
  3825. lPythonDbusPresent = tryCommand(lPythonCommand) ;
  3826. }
  3827. if (tinyfd_verbose) printf("lPythonDbusPresent %d\n", lPythonDbusPresent) ;
  3828. if (tinyfd_verbose) printf("gPythonName %s\n", gPythonName) ;
  3829. }
  3830. return lPythonDbusPresent && graphicMode() && !(tfd_isDarwin() && getenv("SSH_TTY") );
  3831. }
  3832. static void sigHandler(int signum)
  3833. {
  3834. FILE * lIn ;
  3835. if ( ( lIn = popen( "pactl unload-module module-sine" , "r" ) ) )
  3836. {
  3837. pclose( lIn ) ;
  3838. }
  3839. if (tinyfd_verbose) printf("tinyfiledialogs caught signal %d\n", signum);
  3840. }
  3841. void tinyfd_beep(void)
  3842. {
  3843. char lDialogString[256] ;
  3844. FILE * lIn ;
  3845. if ( pactlPresent() )
  3846. {
  3847. signal(SIGINT, sigHandler);
  3848. strcpy( lDialogString ,
  3849. "thnum=$(pactl load-module module-sine frequency=440);sleep .3;pactl unload-module $thnum" ) ;
  3850. }
  3851. else if ( osascriptPresent() )
  3852. {
  3853. if ( afplayPresent() >= 2 )
  3854. {
  3855. strcpy( lDialogString , "afplay /System/Library/Sounds/Ping.aiff") ;
  3856. }
  3857. else
  3858. {
  3859. strcpy( lDialogString , "osascript -e 'tell application \"System Events\" to beep'") ;
  3860. }
  3861. }
  3862. else if ( speakertestPresent() )
  3863. {
  3864. /*strcpy( lDialogString , "timeout -k .3 .3 speaker-test --frequency 440 --test sine > /dev/tty" ) ;*/
  3865. strcpy( lDialogString , "( speaker-test -t sine -f 440 > /dev/tty )& pid=$!;sleep .5; kill -9 $pid" ) ; /*.3 was too short for mac g3*/
  3866. }
  3867. else if ( ffplayPresent() )
  3868. {
  3869. strcpy(lDialogString, "ffplay -f lavfi -i sine=f=440:d=0.15 -autoexit -nodisp" );
  3870. }
  3871. else if (playPresent()) /* play is part of sox */
  3872. {
  3873. strcpy(lDialogString, "play -q -n synth .3 sine 440");
  3874. }
  3875. else if ( playsoundPresent() )
  3876. {
  3877. strcpy( lDialogString , "playsound_simple /usr/share/sounds/freedesktop/stereo/bell.oga") ;
  3878. }
  3879. else if ( paplayPresent() )
  3880. {
  3881. strcpy( lDialogString , "paplay /usr/share/sounds/freedesktop/stereo/bell.oga") ;
  3882. }
  3883. else if (beepexePresent())
  3884. {
  3885. strcpy(lDialogString, "beep.exe 440 300");
  3886. }
  3887. /*else if ( beepPresent() )
  3888. {
  3889. strcpy( lDialogString , "beep -f 440 -l 300" ) ;
  3890. }*/
  3891. else
  3892. {
  3893. strcpy( lDialogString , "printf '\\a' > /dev/tty" ) ;
  3894. }
  3895. if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ;
  3896. if ( ( lIn = popen( lDialogString , "r" ) ) )
  3897. {
  3898. pclose( lIn ) ;
  3899. }
  3900. if ( pactlPresent() )
  3901. {
  3902. signal(SIGINT, SIG_DFL);
  3903. }
  3904. }
  3905. int tinyfd_messageBox(
  3906. char const * aTitle , /* NULL or "" */
  3907. char const * aMessage , /* NULL or "" may contain \n and \t */
  3908. char const * aDialogType , /* "ok" "okcancel" "yesno" "yesnocancel" */
  3909. char const * aIconType , /* "info" "warning" "error" "question" */
  3910. int aDefaultButton ) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */
  3911. {
  3912. char lBuff[MAX_PATH_OR_CMD] ;
  3913. char * lDialogString = NULL ;
  3914. char * lpDialogString;
  3915. FILE * lIn ;
  3916. int lWasGraphicDialog = 0 ;
  3917. int lWasXterm = 0 ;
  3918. int lResult ;
  3919. char lChar ;
  3920. struct termios infoOri;
  3921. struct termios info;
  3922. size_t lTitleLen ;
  3923. size_t lMessageLen ;
  3924. lBuff[0]='\0';
  3925. if (tfd_quoteDetected(aTitle)) return tinyfd_messageBox("INVALID TITLE WITH QUOTES", aMessage, aDialogType, aIconType, aDefaultButton);
  3926. if (tfd_quoteDetected(aMessage)) return tinyfd_messageBox(aTitle, "INVALID MESSAGE WITH QUOTES", aDialogType, aIconType, aDefaultButton);
  3927. lTitleLen = aTitle ? strlen(aTitle) : 0 ;
  3928. lMessageLen = aMessage ? strlen(aMessage) : 0 ;
  3929. if ( !aTitle || strcmp(aTitle,"tinyfd_query") )
  3930. {
  3931. lDialogString = (char *) malloc( MAX_PATH_OR_CMD + lTitleLen + lMessageLen );
  3932. }
  3933. if ( osascriptPresent( ) )
  3934. {
  3935. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return 1;}
  3936. strcpy( lDialogString , "osascript ");
  3937. if ( ! osx9orBetter() ) strcat( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'");
  3938. strcat( lDialogString , " -e 'try' -e 'set {vButton} to {button returned} of ( display dialog \"") ;
  3939. if ( aMessage && strlen(aMessage) )
  3940. {
  3941. strcat(lDialogString, aMessage) ;
  3942. }
  3943. strcat(lDialogString, "\" ") ;
  3944. if ( aTitle && strlen(aTitle) )
  3945. {
  3946. strcat(lDialogString, "with title \"") ;
  3947. strcat(lDialogString, aTitle) ;
  3948. strcat(lDialogString, "\" ") ;
  3949. }
  3950. strcat(lDialogString, "with icon ") ;
  3951. if ( aIconType && ! strcmp( "error" , aIconType ) )
  3952. {
  3953. strcat(lDialogString, "stop " ) ;
  3954. }
  3955. else if ( aIconType && ! strcmp( "warning" , aIconType ) )
  3956. {
  3957. strcat(lDialogString, "caution " ) ;
  3958. }
  3959. else /* question or info */
  3960. {
  3961. strcat(lDialogString, "note " ) ;
  3962. }
  3963. if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) )
  3964. {
  3965. if ( ! aDefaultButton )
  3966. {
  3967. strcat( lDialogString ,"default button \"Cancel\" " ) ;
  3968. }
  3969. }
  3970. else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) )
  3971. {
  3972. strcat( lDialogString ,"buttons {\"No\", \"Yes\"} " ) ;
  3973. if (aDefaultButton)
  3974. {
  3975. strcat( lDialogString ,"default button \"Yes\" " ) ;
  3976. }
  3977. else
  3978. {
  3979. strcat( lDialogString ,"default button \"No\" " ) ;
  3980. }
  3981. strcat( lDialogString ,"cancel button \"No\"" ) ;
  3982. }
  3983. else if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) )
  3984. {
  3985. strcat( lDialogString ,"buttons {\"No\", \"Yes\", \"Cancel\"} " ) ;
  3986. switch (aDefaultButton)
  3987. {
  3988. case 1: strcat( lDialogString ,"default button \"Yes\" " ) ; break;
  3989. case 2: strcat( lDialogString ,"default button \"No\" " ) ; break;
  3990. case 0: strcat( lDialogString ,"default button \"Cancel\" " ) ; break;
  3991. }
  3992. strcat( lDialogString ,"cancel button \"Cancel\"" ) ;
  3993. }
  3994. else
  3995. {
  3996. strcat( lDialogString ,"buttons {\"OK\"} " ) ;
  3997. strcat( lDialogString ,"default button \"OK\" " ) ;
  3998. }
  3999. strcat( lDialogString, ")' ") ;
  4000. strcat( lDialogString,
  4001. "-e 'if vButton is \"Yes\" then' -e 'return 1'\
  4002. -e 'else if vButton is \"OK\" then' -e 'return 1'\
  4003. -e 'else if vButton is \"No\" then' -e 'return 2'\
  4004. -e 'else' -e 'return 0' -e 'end if' " );
  4005. strcat( lDialogString, "-e 'on error number -128' " ) ;
  4006. strcat( lDialogString, "-e '0' " );
  4007. strcat( lDialogString, "-e 'end try'") ;
  4008. if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ;
  4009. }
  4010. else if ( tfd_kdialogPresent() )
  4011. {
  4012. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return 1;}
  4013. strcpy( lDialogString , "kdialog" ) ;
  4014. if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() )
  4015. {
  4016. strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
  4017. }
  4018. strcat( lDialogString , " --" ) ;
  4019. if ( aDialogType && ( ! strcmp( "okcancel" , aDialogType )
  4020. || ! strcmp( "yesno" , aDialogType ) || ! strcmp( "yesnocancel" , aDialogType ) ) )
  4021. {
  4022. if ( aIconType && ( ! strcmp( "warning" , aIconType )
  4023. || ! strcmp( "error" , aIconType ) ) )
  4024. {
  4025. strcat( lDialogString , "warning" ) ;
  4026. }
  4027. if ( ! strcmp( "yesnocancel" , aDialogType ) )
  4028. {
  4029. strcat( lDialogString , "yesnocancel" ) ;
  4030. }
  4031. else
  4032. {
  4033. strcat( lDialogString , "yesno" ) ;
  4034. }
  4035. }
  4036. else if ( aIconType && ! strcmp( "error" , aIconType ) )
  4037. {
  4038. strcat( lDialogString , "error" ) ;
  4039. }
  4040. else if ( aIconType && ! strcmp( "warning" , aIconType ) )
  4041. {
  4042. strcat( lDialogString , "sorry" ) ;
  4043. }
  4044. else
  4045. {
  4046. strcat( lDialogString , "msgbox" ) ;
  4047. }
  4048. strcat( lDialogString , " \"" ) ;
  4049. if ( aMessage )
  4050. {
  4051. strcat( lDialogString , aMessage ) ;
  4052. }
  4053. strcat( lDialogString , "\"" ) ;
  4054. if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) )
  4055. {
  4056. strcat( lDialogString ,
  4057. " --yes-label Ok --no-label Cancel" ) ;
  4058. }
  4059. if ( aTitle && strlen(aTitle) )
  4060. {
  4061. strcat(lDialogString, " --title \"") ;
  4062. strcat(lDialogString, aTitle) ;
  4063. strcat(lDialogString, "\"") ;
  4064. }
  4065. if ( ! strcmp( "yesnocancel" , aDialogType ) )
  4066. {
  4067. strcat( lDialogString , "; x=$? ;if [ $x = 0 ] ;then echo 1;elif [ $x = 1 ] ;then echo 2;else echo 0;fi");
  4068. }
  4069. else
  4070. {
  4071. strcat( lDialogString , ";if [ $? = 0 ];then echo 1;else echo 0;fi");
  4072. }
  4073. }
  4074. else if ( tfd_zenityPresent() || tfd_matedialogPresent() || tfd_shellementaryPresent() || tfd_qarmaPresent() )
  4075. {
  4076. if ( tfd_zenityPresent() )
  4077. {
  4078. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return 1;}
  4079. strcpy( lDialogString , "szAnswer=$(zenity" ) ;
  4080. if ( (tfd_zenity3Present() >= 4) && !getenv("SSH_TTY") && tfd_xpropPresent() )
  4081. {
  4082. strcat(lDialogString, " --attach=$(sleep .01;xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
  4083. }
  4084. }
  4085. else if ( tfd_matedialogPresent() )
  4086. {
  4087. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return 1;}
  4088. strcpy( lDialogString , "szAnswer=$(matedialog" ) ;
  4089. }
  4090. else if ( tfd_shellementaryPresent() )
  4091. {
  4092. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"shellementary");return 1;}
  4093. strcpy( lDialogString , "szAnswer=$(shellementary" ) ;
  4094. }
  4095. else
  4096. {
  4097. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return 1;}
  4098. strcpy( lDialogString , "szAnswer=$(qarma" ) ;
  4099. if ( !getenv("SSH_TTY") && tfd_xpropPresent() )
  4100. {
  4101. strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
  4102. }
  4103. }
  4104. strcat(lDialogString, " --");
  4105. if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) )
  4106. {
  4107. strcat( lDialogString ,
  4108. "question --ok-label=Ok --cancel-label=Cancel" ) ;
  4109. }
  4110. else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) )
  4111. {
  4112. strcat( lDialogString , "question" ) ;
  4113. }
  4114. else if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) )
  4115. {
  4116. strcat( lDialogString , "list --column \"\" --hide-header \"Yes\" \"No\"" ) ;
  4117. }
  4118. else if ( aIconType && ! strcmp( "error" , aIconType ) )
  4119. {
  4120. strcat( lDialogString , "error" ) ;
  4121. }
  4122. else if ( aIconType && ! strcmp( "warning" , aIconType ) )
  4123. {
  4124. strcat( lDialogString , "warning" ) ;
  4125. }
  4126. else
  4127. {
  4128. strcat( lDialogString , "info" ) ;
  4129. }
  4130. strcat(lDialogString, " --title=\"");
  4131. if ( aTitle && strlen(aTitle) ) strcat(lDialogString, aTitle) ;
  4132. strcat(lDialogString, "\"");
  4133. if (strcmp("yesnocancel", aDialogType)) strcat(lDialogString, " --no-wrap");
  4134. strcat(lDialogString, " --text=\"") ;
  4135. if (aMessage && strlen(aMessage)) strcat(lDialogString, aMessage) ;
  4136. strcat(lDialogString, "\"") ;
  4137. if ( (tfd_zenity3Present() >= 3) || (!tfd_zenityPresent() && (tfd_shellementaryPresent() || tfd_qarmaPresent()) ) )
  4138. {
  4139. strcat( lDialogString , " --icon-name=dialog-" ) ;
  4140. if ( aIconType && (! strcmp( "question" , aIconType )
  4141. || ! strcmp( "error" , aIconType )
  4142. || ! strcmp( "warning" , aIconType ) ) )
  4143. {
  4144. strcat( lDialogString , aIconType ) ;
  4145. }
  4146. else
  4147. {
  4148. strcat( lDialogString , "information" ) ;
  4149. }
  4150. }
  4151. if (tinyfd_silent) strcat( lDialogString , " 2>/dev/null ");
  4152. if ( ! strcmp( "yesnocancel" , aDialogType ) )
  4153. {
  4154. strcat( lDialogString ,
  4155. ");if [ $? = 1 ];then echo 0;elif [ $szAnswer = \"No\" ];then echo 2;else echo 1;fi");
  4156. }
  4157. else
  4158. {
  4159. strcat( lDialogString , ");if [ $? = 0 ];then echo 1;else echo 0;fi");
  4160. }
  4161. }
  4162. else if (tfd_yadPresent())
  4163. {
  4164. if (aTitle && !strcmp(aTitle, "tinyfd_query")) { strcpy(tinyfd_response, "yad"); return 1; }
  4165. strcpy(lDialogString, "szAnswer=$(yad --");
  4166. if (aDialogType && !strcmp("ok", aDialogType))
  4167. {
  4168. strcat(lDialogString,"button=Ok:1");
  4169. }
  4170. else if (aDialogType && !strcmp("okcancel", aDialogType))
  4171. {
  4172. strcat(lDialogString,"button=Ok:1 --button=Cancel:0");
  4173. }
  4174. else if (aDialogType && !strcmp("yesno", aDialogType))
  4175. {
  4176. strcat(lDialogString, "button=Yes:1 --button=No:0");
  4177. }
  4178. else if (aDialogType && !strcmp("yesnocancel", aDialogType))
  4179. {
  4180. strcat(lDialogString, "button=Yes:1 --button=No:2 --button=Cancel:0");
  4181. }
  4182. else if (aIconType && !strcmp("error", aIconType))
  4183. {
  4184. strcat(lDialogString, "error");
  4185. }
  4186. else if (aIconType && !strcmp("warning", aIconType))
  4187. {
  4188. strcat(lDialogString, "warning");
  4189. }
  4190. else
  4191. {
  4192. strcat(lDialogString, "info");
  4193. }
  4194. if (aTitle && strlen(aTitle))
  4195. {
  4196. strcat(lDialogString, " --title=\"");
  4197. strcat(lDialogString, aTitle);
  4198. strcat(lDialogString, "\"");
  4199. }
  4200. if (aMessage && strlen(aMessage))
  4201. {
  4202. strcat(lDialogString, " --text=\"");
  4203. strcat(lDialogString, aMessage);
  4204. strcat(lDialogString, "\"");
  4205. }
  4206. strcat(lDialogString, " --image=dialog-");
  4207. if (aIconType && (!strcmp("question", aIconType)
  4208. || !strcmp("error", aIconType)
  4209. || !strcmp("warning", aIconType)))
  4210. {
  4211. strcat(lDialogString, aIconType);
  4212. }
  4213. else
  4214. {
  4215. strcat(lDialogString, "information");
  4216. }
  4217. if (tinyfd_silent) strcat(lDialogString, " 2>/dev/null ");
  4218. strcat(lDialogString,");echo $?");
  4219. }
  4220. else if ( !gxmessagePresent() && !gmessagePresent() && !gdialogPresent() && !xdialogPresent() && tkinter3Present() )
  4221. {
  4222. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return 1;}
  4223. strcpy( lDialogString , gPython3Name ) ;
  4224. strcat( lDialogString ,
  4225. " -S -c \"import tkinter;from tkinter import messagebox;root=tkinter.Tk();root.withdraw();");
  4226. strcat( lDialogString ,"res=messagebox." ) ;
  4227. if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) )
  4228. {
  4229. strcat( lDialogString , "askokcancel(" ) ;
  4230. if ( aDefaultButton )
  4231. {
  4232. strcat( lDialogString , "default=messagebox.OK," ) ;
  4233. }
  4234. else
  4235. {
  4236. strcat( lDialogString , "default=messagebox.CANCEL," ) ;
  4237. }
  4238. }
  4239. else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) )
  4240. {
  4241. strcat( lDialogString , "askyesno(" ) ;
  4242. if ( aDefaultButton )
  4243. {
  4244. strcat( lDialogString , "default=messagebox.YES," ) ;
  4245. }
  4246. else
  4247. {
  4248. strcat( lDialogString , "default=messagebox.NO," ) ;
  4249. }
  4250. }
  4251. else if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) )
  4252. {
  4253. strcat( lDialogString , "askyesnocancel(" ) ;
  4254. switch ( aDefaultButton )
  4255. {
  4256. case 1: strcat( lDialogString , "default=messagebox.YES," ); break;
  4257. case 2: strcat( lDialogString , "default=messagebox.NO," ); break;
  4258. case 0: strcat( lDialogString , "default=messagebox.CANCEL," ); break;
  4259. }
  4260. }
  4261. else
  4262. {
  4263. strcat( lDialogString , "showinfo(" ) ;
  4264. }
  4265. strcat( lDialogString , "icon='" ) ;
  4266. if ( aIconType && (! strcmp( "question" , aIconType )
  4267. || ! strcmp( "error" , aIconType )
  4268. || ! strcmp( "warning" , aIconType ) ) )
  4269. {
  4270. strcat( lDialogString , aIconType ) ;
  4271. }
  4272. else
  4273. {
  4274. strcat( lDialogString , "info" ) ;
  4275. }
  4276. strcat(lDialogString, "',") ;
  4277. if ( aTitle && strlen(aTitle) )
  4278. {
  4279. strcat(lDialogString, "title='") ;
  4280. strcat(lDialogString, aTitle) ;
  4281. strcat(lDialogString, "',") ;
  4282. }
  4283. if ( aMessage && strlen(aMessage) )
  4284. {
  4285. strcat(lDialogString, "message='") ;
  4286. lpDialogString = lDialogString + strlen(lDialogString);
  4287. tfd_replaceSubStr( aMessage , "\n" , "\\n" , lpDialogString ) ;
  4288. strcat(lDialogString, "'") ;
  4289. }
  4290. if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) )
  4291. {
  4292. strcat(lDialogString, ");\n\
  4293. if res is None :\n\tprint(0)\n\
  4294. elif res is False :\n\tprint(2)\n\
  4295. else :\n\tprint (1)\n\"" ) ;
  4296. }
  4297. else
  4298. {
  4299. strcat(lDialogString, ");\n\
  4300. if res is False :\n\tprint(0)\n\
  4301. else :\n\tprint(1)\n\"" ) ;
  4302. }
  4303. }
  4304. else if ( !gxmessagePresent() && !gmessagePresent() && !gdialogPresent() && !xdialogPresent() && tkinter2Present() )
  4305. {
  4306. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python2-tkinter");return 1;}
  4307. strcpy( lDialogString , "export PYTHONIOENCODING=utf-8;" ) ;
  4308. strcat( lDialogString , gPython2Name ) ;
  4309. if ( ! isTerminalRunning( ) && tfd_isDarwin( ) )
  4310. {
  4311. strcat( lDialogString , " -i" ) ; /* for osx without console */
  4312. }
  4313. strcat( lDialogString ,
  4314. " -S -c \"import Tkinter,tkMessageBox;root=Tkinter.Tk();root.withdraw();");
  4315. if ( tfd_isDarwin( ) )
  4316. {
  4317. strcat( lDialogString ,
  4318. "import os;os.system('''/usr/bin/osascript -e 'tell app \\\"Finder\\\" to set \
  4319. frontmost of process \\\"Python\\\" to true' ''');");
  4320. }
  4321. strcat( lDialogString ,"res=tkMessageBox." ) ;
  4322. if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) )
  4323. {
  4324. strcat( lDialogString , "askokcancel(" ) ;
  4325. if ( aDefaultButton )
  4326. {
  4327. strcat( lDialogString , "default=tkMessageBox.OK," ) ;
  4328. }
  4329. else
  4330. {
  4331. strcat( lDialogString , "default=tkMessageBox.CANCEL," ) ;
  4332. }
  4333. }
  4334. else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) )
  4335. {
  4336. strcat( lDialogString , "askyesno(" ) ;
  4337. if ( aDefaultButton )
  4338. {
  4339. strcat( lDialogString , "default=tkMessageBox.YES," ) ;
  4340. }
  4341. else
  4342. {
  4343. strcat( lDialogString , "default=tkMessageBox.NO," ) ;
  4344. }
  4345. }
  4346. else if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) )
  4347. {
  4348. strcat( lDialogString , "askyesnocancel(" ) ;
  4349. switch ( aDefaultButton )
  4350. {
  4351. case 1: strcat( lDialogString , "default=tkMessageBox.YES," ); break;
  4352. case 2: strcat( lDialogString , "default=tkMessageBox.NO," ); break;
  4353. case 0: strcat( lDialogString , "default=tkMessageBox.CANCEL," ); break;
  4354. }
  4355. }
  4356. else
  4357. {
  4358. strcat( lDialogString , "showinfo(" ) ;
  4359. }
  4360. strcat( lDialogString , "icon='" ) ;
  4361. if ( aIconType && (! strcmp( "question" , aIconType )
  4362. || ! strcmp( "error" , aIconType )
  4363. || ! strcmp( "warning" , aIconType ) ) )
  4364. {
  4365. strcat( lDialogString , aIconType ) ;
  4366. }
  4367. else
  4368. {
  4369. strcat( lDialogString , "info" ) ;
  4370. }
  4371. strcat(lDialogString, "',") ;
  4372. if ( aTitle && strlen(aTitle) )
  4373. {
  4374. strcat(lDialogString, "title='") ;
  4375. strcat(lDialogString, aTitle) ;
  4376. strcat(lDialogString, "',") ;
  4377. }
  4378. if ( aMessage && strlen(aMessage) )
  4379. {
  4380. strcat(lDialogString, "message='") ;
  4381. lpDialogString = lDialogString + strlen(lDialogString);
  4382. tfd_replaceSubStr( aMessage , "\n" , "\\n" , lpDialogString ) ;
  4383. strcat(lDialogString, "'") ;
  4384. }
  4385. if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) )
  4386. {
  4387. strcat(lDialogString, ");\n\
  4388. if res is None :\n\tprint 0\n\
  4389. elif res is False :\n\tprint 2\n\
  4390. else :\n\tprint 1\n\"" ) ;
  4391. }
  4392. else
  4393. {
  4394. strcat(lDialogString, ");\n\
  4395. if res is False :\n\tprint 0\n\
  4396. else :\n\tprint 1\n\"" ) ;
  4397. }
  4398. }
  4399. else if ( gxmessagePresent() || gmessagePresent() || (!gdialogPresent() && !xdialogPresent() && xmessagePresent()) )
  4400. {
  4401. if ( gxmessagePresent() )
  4402. {
  4403. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gxmessage");return 1;}
  4404. strcpy( lDialogString , "gxmessage");
  4405. }
  4406. else if ( gmessagePresent() )
  4407. {
  4408. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gmessage");return 1;}
  4409. strcpy( lDialogString , "gmessage");
  4410. }
  4411. else
  4412. {
  4413. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xmessage");return 1;}
  4414. strcpy( lDialogString , "xmessage");
  4415. }
  4416. if ( aDialogType && ! strcmp("okcancel" , aDialogType) )
  4417. {
  4418. strcat( lDialogString , " -buttons Ok:1,Cancel:0");
  4419. switch ( aDefaultButton )
  4420. {
  4421. case 1: strcat( lDialogString , " -default Ok"); break;
  4422. case 0: strcat( lDialogString , " -default Cancel"); break;
  4423. }
  4424. }
  4425. else if ( aDialogType && ! strcmp("yesno" , aDialogType) )
  4426. {
  4427. strcat( lDialogString , " -buttons Yes:1,No:0");
  4428. switch ( aDefaultButton )
  4429. {
  4430. case 1: strcat( lDialogString , " -default Yes"); break;
  4431. case 0: strcat( lDialogString , " -default No"); break;
  4432. }
  4433. }
  4434. else if ( aDialogType && ! strcmp("yesnocancel" , aDialogType) )
  4435. {
  4436. strcat( lDialogString , " -buttons Yes:1,No:2,Cancel:0");
  4437. switch ( aDefaultButton )
  4438. {
  4439. case 1: strcat( lDialogString , " -default Yes"); break;
  4440. case 2: strcat( lDialogString , " -default No"); break;
  4441. case 0: strcat( lDialogString , " -default Cancel"); break;
  4442. }
  4443. }
  4444. else
  4445. {
  4446. strcat( lDialogString , " -buttons Ok:1");
  4447. strcat( lDialogString , " -default Ok");
  4448. }
  4449. strcat( lDialogString , " -center \"");
  4450. if ( aMessage && strlen(aMessage) )
  4451. {
  4452. strcat( lDialogString , aMessage ) ;
  4453. }
  4454. strcat(lDialogString, "\"" ) ;
  4455. if ( aTitle && strlen(aTitle) )
  4456. {
  4457. strcat( lDialogString , " -title \"");
  4458. strcat( lDialogString , aTitle ) ;
  4459. strcat( lDialogString, "\"" ) ;
  4460. }
  4461. strcat( lDialogString , " ; echo $? ");
  4462. }
  4463. else if ( xdialogPresent() || gdialogPresent() || dialogName() || whiptailPresent() )
  4464. {
  4465. if ( gdialogPresent( ) )
  4466. {
  4467. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gdialog");return 1;}
  4468. lWasGraphicDialog = 1 ;
  4469. strcpy( lDialogString , "(gdialog " ) ;
  4470. }
  4471. else if ( xdialogPresent( ) )
  4472. {
  4473. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return 1;}
  4474. lWasGraphicDialog = 1 ;
  4475. strcpy( lDialogString , "(Xdialog " ) ;
  4476. }
  4477. else if ( dialogName( ) )
  4478. {
  4479. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return 0;}
  4480. if ( isTerminalRunning( ) )
  4481. {
  4482. strcpy( lDialogString , "(dialog " ) ;
  4483. }
  4484. else
  4485. {
  4486. lWasXterm = 1 ;
  4487. strcpy( lDialogString , terminalName() ) ;
  4488. strcat( lDialogString , "'(" ) ;
  4489. strcat( lDialogString , dialogName() ) ;
  4490. strcat( lDialogString , " " ) ;
  4491. }
  4492. }
  4493. else if ( isTerminalRunning( ) )
  4494. {
  4495. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"whiptail");return 0;}
  4496. strcpy( lDialogString , "(whiptail " ) ;
  4497. }
  4498. else
  4499. {
  4500. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"whiptail");return 0;}
  4501. lWasXterm = 1 ;
  4502. strcpy( lDialogString , terminalName() ) ;
  4503. strcat( lDialogString , "'(whiptail " ) ;
  4504. }
  4505. if ( aTitle && strlen(aTitle) )
  4506. {
  4507. strcat(lDialogString, "--title \"") ;
  4508. strcat(lDialogString, aTitle) ;
  4509. strcat(lDialogString, "\" ") ;
  4510. }
  4511. if ( !xdialogPresent() && !gdialogPresent() )
  4512. {
  4513. if ( aDialogType && ( !strcmp( "okcancel" , aDialogType ) || !strcmp( "yesno" , aDialogType )
  4514. || !strcmp( "yesnocancel" , aDialogType ) ) )
  4515. {
  4516. strcat(lDialogString, "--backtitle \"") ;
  4517. strcat(lDialogString, "tab: move focus") ;
  4518. strcat(lDialogString, "\" ") ;
  4519. }
  4520. }
  4521. if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) )
  4522. {
  4523. if ( ! aDefaultButton )
  4524. {
  4525. strcat( lDialogString , "--defaultno " ) ;
  4526. }
  4527. strcat( lDialogString ,
  4528. "--yes-label \"Ok\" --no-label \"Cancel\" --yesno " ) ;
  4529. }
  4530. else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) )
  4531. {
  4532. if ( ! aDefaultButton )
  4533. {
  4534. strcat( lDialogString , "--defaultno " ) ;
  4535. }
  4536. strcat( lDialogString , "--yesno " ) ;
  4537. }
  4538. else if (aDialogType && !strcmp("yesnocancel", aDialogType))
  4539. {
  4540. if (!aDefaultButton)
  4541. {
  4542. strcat(lDialogString, "--defaultno ");
  4543. }
  4544. strcat(lDialogString, "--menu ");
  4545. }
  4546. else
  4547. {
  4548. strcat( lDialogString , "--msgbox " ) ;
  4549. }
  4550. strcat( lDialogString , "\"" ) ;
  4551. if ( aMessage && strlen(aMessage) )
  4552. {
  4553. strcat(lDialogString, aMessage) ;
  4554. }
  4555. strcat(lDialogString, "\" ");
  4556. if ( lWasGraphicDialog )
  4557. {
  4558. if (aDialogType && !strcmp("yesnocancel", aDialogType))
  4559. {
  4560. strcat(lDialogString,"0 60 0 Yes \"\" No \"\") 2>/tmp/tinyfd.txt;\
  4561. if [ $? = 0 ];then tinyfdBool=1;else tinyfdBool=0;fi;\
  4562. tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ;
  4563. }
  4564. else
  4565. {
  4566. strcat(lDialogString,
  4567. "10 60 ) 2>&1;if [ $? = 0 ];then echo 1;else echo 0;fi");
  4568. }
  4569. }
  4570. else
  4571. {
  4572. if (aDialogType && !strcmp("yesnocancel", aDialogType))
  4573. {
  4574. strcat(lDialogString,"0 60 0 Yes \"\" No \"\" >/dev/tty ) 2>/tmp/tinyfd.txt;\
  4575. if [ $? = 0 ];then tinyfdBool=1;else tinyfdBool=0;fi;\
  4576. tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ;
  4577. if ( lWasXterm )
  4578. {
  4579. strcat(lDialogString," >/tmp/tinyfd0.txt';cat /tmp/tinyfd0.txt");
  4580. }
  4581. else
  4582. {
  4583. strcat(lDialogString, "; clear >/dev/tty") ;
  4584. }
  4585. }
  4586. else
  4587. {
  4588. strcat(lDialogString, "10 60 >/dev/tty) 2>&1;if [ $? = 0 ];");
  4589. if ( lWasXterm )
  4590. {
  4591. strcat( lDialogString ,
  4592. "then\n\techo 1\nelse\n\techo 0\nfi >/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt");
  4593. }
  4594. else
  4595. {
  4596. strcat(lDialogString,
  4597. "then echo 1;else echo 0;fi;clear >/dev/tty");
  4598. }
  4599. }
  4600. }
  4601. }
  4602. else if ( !isTerminalRunning() && terminalName() )
  4603. {
  4604. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return 0;}
  4605. strcpy( lDialogString , terminalName() ) ;
  4606. strcat( lDialogString , "'" ) ;
  4607. if ( !gWarningDisplayed && !tinyfd_forceConsole)
  4608. {
  4609. gWarningDisplayed = 1 ;
  4610. strcat( lDialogString , "echo \"" ) ;
  4611. strcat( lDialogString, gTitle) ;
  4612. strcat( lDialogString , "\";" ) ;
  4613. strcat( lDialogString , "echo \"" ) ;
  4614. strcat( lDialogString, tinyfd_needs) ;
  4615. strcat( lDialogString , "\";echo;echo;" ) ;
  4616. }
  4617. if ( aTitle && strlen(aTitle) )
  4618. {
  4619. strcat( lDialogString , "echo \"" ) ;
  4620. strcat( lDialogString, aTitle) ;
  4621. strcat( lDialogString , "\";echo;" ) ;
  4622. }
  4623. if ( aMessage && strlen(aMessage) )
  4624. {
  4625. strcat( lDialogString , "echo \"" ) ;
  4626. strcat( lDialogString, aMessage) ;
  4627. strcat( lDialogString , "\"; " ) ;
  4628. }
  4629. if ( aDialogType && !strcmp("yesno",aDialogType) )
  4630. {
  4631. strcat( lDialogString , "echo -n \"y/n: \"; " ) ;
  4632. strcat( lDialogString , "stty sane -echo;" ) ;
  4633. strcat( lDialogString ,
  4634. "answer=$( while ! head -c 1 | grep -i [ny];do true ;done);");
  4635. strcat( lDialogString ,
  4636. "if echo \"$answer\" | grep -iq \"^y\";then\n");
  4637. strcat( lDialogString , "\techo 1\nelse\n\techo 0\nfi" ) ;
  4638. }
  4639. else if ( aDialogType && !strcmp("okcancel",aDialogType) )
  4640. {
  4641. strcat( lDialogString , "echo -n \"[O]kay/[C]ancel: \"; " ) ;
  4642. strcat( lDialogString , "stty sane -echo;" ) ;
  4643. strcat( lDialogString ,
  4644. "answer=$( while ! head -c 1 | grep -i [oc];do true ;done);");
  4645. strcat( lDialogString ,
  4646. "if echo \"$answer\" | grep -iq \"^o\";then\n");
  4647. strcat( lDialogString , "\techo 1\nelse\n\techo 0\nfi" ) ;
  4648. }
  4649. else if ( aDialogType && !strcmp("yesnocancel",aDialogType) )
  4650. {
  4651. strcat( lDialogString , "echo -n \"[Y]es/[N]o/[C]ancel: \"; " ) ;
  4652. strcat( lDialogString , "stty sane -echo;" ) ;
  4653. strcat( lDialogString ,
  4654. "answer=$( while ! head -c 1 | grep -i [nyc];do true ;done);");
  4655. strcat( lDialogString ,
  4656. "if echo \"$answer\" | grep -iq \"^y\";then\n\techo 1\n");
  4657. strcat( lDialogString , "elif echo \"$answer\" | grep -iq \"^n\";then\n\techo 2\n" ) ;
  4658. strcat( lDialogString , "else\n\techo 0\nfi" ) ;
  4659. }
  4660. else
  4661. {
  4662. strcat(lDialogString , "echo -n \"press enter to continue \"; ");
  4663. strcat( lDialogString , "stty sane -echo;" ) ;
  4664. strcat( lDialogString ,
  4665. "answer=$( while ! head -c 1;do true ;done);echo 1");
  4666. }
  4667. strcat( lDialogString ,
  4668. " >/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt");
  4669. }
  4670. else if ( !isTerminalRunning() && pythonDbusPresent() && !strcmp("ok" , aDialogType) )
  4671. {
  4672. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python-dbus");return 1;}
  4673. strcpy( lDialogString , gPythonName ) ;
  4674. strcat( lDialogString ," -c \"import dbus;bus=dbus.SessionBus();");
  4675. strcat( lDialogString ,"notif=bus.get_object('org.freedesktop.Notifications','/org/freedesktop/Notifications');" ) ;
  4676. strcat( lDialogString ,"notify=dbus.Interface(notif,'org.freedesktop.Notifications');" ) ;
  4677. strcat( lDialogString ,"notify.Notify('',0,'" ) ;
  4678. if ( aIconType && strlen(aIconType) )
  4679. {
  4680. strcat( lDialogString , aIconType ) ;
  4681. }
  4682. strcat(lDialogString, "','") ;
  4683. if ( aTitle && strlen(aTitle) )
  4684. {
  4685. strcat(lDialogString, aTitle) ;
  4686. }
  4687. strcat(lDialogString, "','") ;
  4688. if ( aMessage && strlen(aMessage) )
  4689. {
  4690. lpDialogString = lDialogString + strlen(lDialogString);
  4691. tfd_replaceSubStr( aMessage , "\n" , "\\n" , lpDialogString ) ;
  4692. }
  4693. strcat(lDialogString, "','','',5000)\"") ;
  4694. }
  4695. else if ( !isTerminalRunning() && (perlPresent() >= 2) && !strcmp("ok" , aDialogType) )
  4696. {
  4697. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"perl-dbus");return 1;}
  4698. strcpy( lDialogString , "perl -e \"use Net::DBus;\
  4699. my \\$sessionBus = Net::DBus->session;\
  4700. my \\$notificationsService = \\$sessionBus->get_service('org.freedesktop.Notifications');\
  4701. my \\$notificationsObject = \\$notificationsService->get_object('/org/freedesktop/Notifications',\
  4702. 'org.freedesktop.Notifications');");
  4703. sprintf( lDialogString + strlen(lDialogString),
  4704. "my \\$notificationId;\\$notificationId = \\$notificationsObject->Notify(shift, 0, '%s', '%s', '%s', [], {}, -1);\" ",
  4705. aIconType?aIconType:"", aTitle?aTitle:"", aMessage?aMessage:"" ) ;
  4706. }
  4707. else if ( !isTerminalRunning() && notifysendPresent() && !strcmp("ok" , aDialogType) )
  4708. {
  4709. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"notifysend");return 1;}
  4710. strcpy( lDialogString , "notify-send" ) ;
  4711. if ( aIconType && strlen(aIconType) )
  4712. {
  4713. strcat( lDialogString , " -i '" ) ;
  4714. strcat( lDialogString , aIconType ) ;
  4715. strcat( lDialogString , "'" ) ;
  4716. }
  4717. strcat( lDialogString , " \"" ) ;
  4718. if ( aTitle && strlen(aTitle) )
  4719. {
  4720. strcat(lDialogString, aTitle) ;
  4721. strcat( lDialogString , " | " ) ;
  4722. }
  4723. if ( aMessage && strlen(aMessage) )
  4724. {
  4725. tfd_replaceSubStr( aMessage , "\n\t" , " | " , lBuff ) ;
  4726. tfd_replaceSubStr( aMessage , "\n" , " | " , lBuff ) ;
  4727. tfd_replaceSubStr( aMessage , "\t" , " " , lBuff ) ;
  4728. strcat(lDialogString, lBuff) ;
  4729. }
  4730. strcat( lDialogString , "\"" ) ;
  4731. }
  4732. else
  4733. {
  4734. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return 0;}
  4735. if ( !gWarningDisplayed && !tinyfd_forceConsole)
  4736. {
  4737. gWarningDisplayed = 1 ;
  4738. printf("\n\n%s\n", gTitle);
  4739. printf("%s\n\n", tinyfd_needs);
  4740. }
  4741. if ( aTitle && strlen(aTitle) )
  4742. {
  4743. printf("\n%s\n", aTitle);
  4744. }
  4745. tcgetattr(0, &infoOri);
  4746. tcgetattr(0, &info);
  4747. info.c_lflag &= ~ICANON;
  4748. info.c_cc[VMIN] = 1;
  4749. info.c_cc[VTIME] = 0;
  4750. tcsetattr(0, TCSANOW, &info);
  4751. if ( aDialogType && !strcmp("yesno",aDialogType) )
  4752. {
  4753. do
  4754. {
  4755. if ( aMessage && strlen(aMessage) )
  4756. {
  4757. printf("\n%s\n",aMessage);
  4758. }
  4759. printf("y/n: "); fflush(stdout);
  4760. lChar = (char) tolower( getchar() ) ;
  4761. printf("\n\n");
  4762. }
  4763. while ( lChar != 'y' && lChar != 'n' );
  4764. lResult = lChar == 'y' ? 1 : 0 ;
  4765. }
  4766. else if ( aDialogType && !strcmp("okcancel",aDialogType) )
  4767. {
  4768. do
  4769. {
  4770. if ( aMessage && strlen(aMessage) )
  4771. {
  4772. printf("\n%s\n",aMessage);
  4773. }
  4774. printf("[O]kay/[C]ancel: "); fflush(stdout);
  4775. lChar = (char) tolower( getchar() ) ;
  4776. printf("\n\n");
  4777. }
  4778. while ( lChar != 'o' && lChar != 'c' );
  4779. lResult = lChar == 'o' ? 1 : 0 ;
  4780. }
  4781. else if ( aDialogType && !strcmp("yesnocancel",aDialogType) )
  4782. {
  4783. do
  4784. {
  4785. if ( aMessage && strlen(aMessage) )
  4786. {
  4787. printf("\n%s\n",aMessage);
  4788. }
  4789. printf("[Y]es/[N]o/[C]ancel: "); fflush(stdout);
  4790. lChar = (char) tolower( getchar() ) ;
  4791. printf("\n\n");
  4792. }
  4793. while ( lChar != 'y' && lChar != 'n' && lChar != 'c' );
  4794. lResult = (lChar == 'y') ? 1 : (lChar == 'n') ? 2 : 0 ;
  4795. }
  4796. else
  4797. {
  4798. if ( aMessage && strlen(aMessage) )
  4799. {
  4800. printf("\n%s\n\n",aMessage);
  4801. }
  4802. printf("press enter to continue "); fflush(stdout);
  4803. getchar() ;
  4804. printf("\n\n");
  4805. lResult = 1 ;
  4806. }
  4807. tcsetattr(0, TCSANOW, &infoOri);
  4808. free(lDialogString);
  4809. return lResult ;
  4810. }
  4811. if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ;
  4812. if ( ! ( lIn = popen( lDialogString , "r" ) ) )
  4813. {
  4814. free(lDialogString);
  4815. return 0 ;
  4816. }
  4817. while ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL )
  4818. {}
  4819. pclose( lIn ) ;
  4820. /* printf( "lBuff: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; */
  4821. if ( strlen( lBuff ) && lBuff[strlen( lBuff ) -1] == '\n' )
  4822. {
  4823. lBuff[strlen( lBuff ) -1] = '\0' ;
  4824. }
  4825. /* printf( "lBuff1: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; */
  4826. if (aDialogType && !strcmp("yesnocancel", aDialogType))
  4827. {
  4828. if ( lBuff[0]=='1' )
  4829. {
  4830. if ( !strcmp( lBuff+1 , "Yes" )) strcpy(lBuff,"1");
  4831. else if ( !strcmp( lBuff+1 , "No" )) strcpy(lBuff,"2");
  4832. }
  4833. }
  4834. /* printf( "lBuff2: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; */
  4835. lResult = !strcmp( lBuff , "2" ) ? 2 : !strcmp( lBuff , "1" ) ? 1 : 0;
  4836. /* printf( "lResult: %d\n" , lResult ) ; */
  4837. free(lDialogString);
  4838. return lResult ;
  4839. }
  4840. /* return has only meaning for tinyfd_query */
  4841. int tinyfd_notifyPopup(
  4842. char const * aTitle , /* NULL or "" */
  4843. char const * aMessage , /* NULL or "" may contain \n and \t */
  4844. char const * aIconType ) /* "info" "warning" "error" */
  4845. {
  4846. char lBuff[MAX_PATH_OR_CMD];
  4847. char * lDialogString = NULL ;
  4848. char * lpDialogString ;
  4849. FILE * lIn ;
  4850. size_t lTitleLen ;
  4851. size_t lMessageLen ;
  4852. if (tfd_quoteDetected(aTitle)) return tinyfd_notifyPopup("INVALID TITLE WITH QUOTES", aMessage, aIconType);
  4853. if (tfd_quoteDetected(aMessage)) return tinyfd_notifyPopup(aTitle, "INVALID MESSAGE WITH QUOTES", aIconType);
  4854. if ( getenv("SSH_TTY") && !dunstifyPresent() && !dunstPresent() )
  4855. {
  4856. return tinyfd_messageBox(aTitle, aMessage, "ok", aIconType, 0);
  4857. }
  4858. lTitleLen = aTitle ? strlen(aTitle) : 0 ;
  4859. lMessageLen = aMessage ? strlen(aMessage) : 0 ;
  4860. if ( !aTitle || strcmp(aTitle,"tinyfd_query") )
  4861. {
  4862. lDialogString = (char *) malloc( MAX_PATH_OR_CMD + lTitleLen + lMessageLen );
  4863. }
  4864. if ( getenv("SSH_TTY") )
  4865. {
  4866. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dunst");return 1;}
  4867. strcpy( lDialogString , "notify-send \"" ) ;
  4868. if ( aTitle && strlen(aTitle) )
  4869. {
  4870. strcat( lDialogString , aTitle ) ;
  4871. strcat( lDialogString , "\" \"" ) ;
  4872. }
  4873. if ( aMessage && strlen(aMessage) )
  4874. {
  4875. strcat(lDialogString, aMessage) ;
  4876. }
  4877. strcat( lDialogString , "\"" ) ;
  4878. }
  4879. else if ( osascriptPresent( ) )
  4880. {
  4881. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return 1;}
  4882. strcpy( lDialogString , "osascript ");
  4883. if ( ! osx9orBetter() ) strcat( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'");
  4884. strcat( lDialogString , " -e 'try' -e 'display notification \"") ;
  4885. if ( aMessage && strlen(aMessage) )
  4886. {
  4887. strcat(lDialogString, aMessage) ;
  4888. }
  4889. strcat(lDialogString, " \" ") ;
  4890. if ( aTitle && strlen(aTitle) )
  4891. {
  4892. strcat(lDialogString, "with title \"") ;
  4893. strcat(lDialogString, aTitle) ;
  4894. strcat(lDialogString, "\" ") ;
  4895. }
  4896. strcat( lDialogString, "' -e 'end try'") ;
  4897. if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ;
  4898. }
  4899. else if ( tfd_kdialogPresent() )
  4900. {
  4901. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return 1;}
  4902. strcpy( lDialogString , "kdialog" ) ;
  4903. if ( aIconType && strlen(aIconType) )
  4904. {
  4905. strcat( lDialogString , " --icon '" ) ;
  4906. strcat( lDialogString , aIconType ) ;
  4907. strcat( lDialogString , "'" ) ;
  4908. }
  4909. if ( aTitle && strlen(aTitle) )
  4910. {
  4911. strcat( lDialogString , " --title \"" ) ;
  4912. strcat( lDialogString , aTitle ) ;
  4913. strcat( lDialogString , "\"" ) ;
  4914. }
  4915. strcat( lDialogString , " --passivepopup" ) ;
  4916. strcat( lDialogString , " \"" ) ;
  4917. if ( aMessage )
  4918. {
  4919. strcat( lDialogString , aMessage ) ;
  4920. }
  4921. strcat( lDialogString , " \" 5" ) ;
  4922. }
  4923. else if ( tfd_yadPresent() )
  4924. {
  4925. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"yad");return 1;}
  4926. strcpy( lDialogString , "yad --notification");
  4927. if ( aIconType && strlen( aIconType ) )
  4928. {
  4929. strcat( lDialogString , " --image=\"");
  4930. strcat( lDialogString , aIconType ) ;
  4931. strcat( lDialogString , "\"" ) ;
  4932. }
  4933. strcat( lDialogString , " --text=\"" ) ;
  4934. if ( aTitle && strlen(aTitle) )
  4935. {
  4936. strcat(lDialogString, aTitle) ;
  4937. strcat(lDialogString, "\n") ;
  4938. }
  4939. if ( aMessage && strlen( aMessage ) )
  4940. {
  4941. strcat( lDialogString , aMessage ) ;
  4942. }
  4943. strcat( lDialogString , " \"" ) ;
  4944. }
  4945. else if ( perlPresent() >= 2 )
  4946. {
  4947. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"perl-dbus");return 1;}
  4948. strcpy( lDialogString , "perl -e \"use Net::DBus;\
  4949. my \\$sessionBus = Net::DBus->session;\
  4950. my \\$notificationsService = \\$sessionBus->get_service('org.freedesktop.Notifications');\
  4951. my \\$notificationsObject = \\$notificationsService->get_object('/org/freedesktop/Notifications',\
  4952. 'org.freedesktop.Notifications');");
  4953. sprintf( lDialogString + strlen(lDialogString) ,
  4954. "my \\$notificationId;\\$notificationId = \\$notificationsObject->Notify(shift, 0, '%s', '%s', '%s', [], {}, -1);\" ",
  4955. aIconType?aIconType:"", aTitle?aTitle:"", aMessage?aMessage:"" ) ;
  4956. }
  4957. else if ( pythonDbusPresent( ) )
  4958. {
  4959. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python-dbus");return 1;}
  4960. strcpy( lDialogString , gPythonName ) ;
  4961. strcat( lDialogString ," -c \"import dbus;bus=dbus.SessionBus();");
  4962. strcat( lDialogString ,"notif=bus.get_object('org.freedesktop.Notifications','/org/freedesktop/Notifications');" ) ;
  4963. strcat( lDialogString ,"notify=dbus.Interface(notif,'org.freedesktop.Notifications');" ) ;
  4964. strcat( lDialogString ,"notify.Notify('',0,'" ) ;
  4965. if ( aIconType && strlen(aIconType) )
  4966. {
  4967. strcat( lDialogString , aIconType ) ;
  4968. }
  4969. strcat(lDialogString, "','") ;
  4970. if ( aTitle && strlen(aTitle) )
  4971. {
  4972. strcat(lDialogString, aTitle) ;
  4973. }
  4974. strcat(lDialogString, "','") ;
  4975. if ( aMessage && strlen(aMessage) )
  4976. {
  4977. lpDialogString = lDialogString + strlen(lDialogString);
  4978. tfd_replaceSubStr( aMessage , "\n" , "\\n" , lpDialogString ) ;
  4979. }
  4980. strcat(lDialogString, "','','',5000)\"") ;
  4981. }
  4982. else if ( notifysendPresent() )
  4983. {
  4984. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"notifysend");return 1;}
  4985. strcpy( lDialogString , "notify-send" ) ;
  4986. if ( aIconType && strlen(aIconType) )
  4987. {
  4988. strcat( lDialogString , " -i '" ) ;
  4989. strcat( lDialogString , aIconType ) ;
  4990. strcat( lDialogString , "'" ) ;
  4991. }
  4992. strcat( lDialogString , " \"" ) ;
  4993. if ( aTitle && strlen(aTitle) )
  4994. {
  4995. strcat(lDialogString, aTitle) ;
  4996. strcat( lDialogString , " | " ) ;
  4997. }
  4998. if ( aMessage && strlen(aMessage) )
  4999. {
  5000. tfd_replaceSubStr( aMessage , "\n\t" , " | " , lBuff ) ;
  5001. tfd_replaceSubStr( aMessage , "\n" , " | " , lBuff ) ;
  5002. tfd_replaceSubStr( aMessage , "\t" , " " , lBuff ) ;
  5003. strcat(lDialogString, lBuff) ;
  5004. }
  5005. strcat( lDialogString , "\"" ) ;
  5006. }
  5007. else if ( (tfd_zenity3Present()>=5) )
  5008. {
  5009. /* zenity 2.32 & 3.14 has the notification but with a bug: it doesnt return from it */
  5010. /* zenity 3.8 show the notification as an alert ok cancel box */
  5011. /* zenity 3.44 doesn't have the notification (3.42 has it) */
  5012. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return 1;}
  5013. strcpy( lDialogString , "zenity --notification");
  5014. if ( aIconType && strlen( aIconType ) )
  5015. {
  5016. strcat( lDialogString , " --window-icon '");
  5017. strcat( lDialogString , aIconType ) ;
  5018. strcat( lDialogString , "'" ) ;
  5019. }
  5020. strcat( lDialogString , " --text \"" ) ;
  5021. if ( aTitle && strlen(aTitle) )
  5022. {
  5023. strcat(lDialogString, aTitle) ;
  5024. strcat(lDialogString, "\n") ;
  5025. }
  5026. if ( aMessage && strlen( aMessage ) )
  5027. {
  5028. strcat( lDialogString , aMessage ) ;
  5029. }
  5030. strcat( lDialogString , " \"" ) ;
  5031. }
  5032. else
  5033. {
  5034. if (lDialogString) free(lDialogString);
  5035. return tinyfd_messageBox(aTitle, aMessage, "ok", aIconType, 0);
  5036. }
  5037. if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ;
  5038. if ( ! ( lIn = popen( lDialogString , "r" ) ) )
  5039. {
  5040. free(lDialogString);
  5041. return 0 ;
  5042. }
  5043. pclose( lIn ) ;
  5044. free(lDialogString);
  5045. return 1;
  5046. }
  5047. /* returns NULL on cancel */
  5048. char * tinyfd_inputBox(
  5049. char const * aTitle , /* NULL or "" */
  5050. char const * aMessage , /* NULL or "" (\n and \t have no effect) */
  5051. char const * aDefaultInput ) /* "" , if NULL it's a passwordBox */
  5052. {
  5053. static char lBuff[MAX_PATH_OR_CMD];
  5054. char * lDialogString = NULL;
  5055. char * lpDialogString;
  5056. FILE * lIn ;
  5057. int lResult ;
  5058. int lWasGdialog = 0 ;
  5059. int lWasGraphicDialog = 0 ;
  5060. int lWasXterm = 0 ;
  5061. int lWasBasicXterm = 0 ;
  5062. struct termios oldt ;
  5063. struct termios newt ;
  5064. char * lEOF;
  5065. size_t lTitleLen ;
  5066. size_t lMessageLen ;
  5067. if (!aTitle && !aMessage && !aDefaultInput) return lBuff; /* now I can fill lBuff from outside */
  5068. lBuff[0]='\0';
  5069. if (tfd_quoteDetected(aTitle)) return tinyfd_inputBox("INVALID TITLE WITH QUOTES", aMessage, aDefaultInput);
  5070. if (tfd_quoteDetected(aMessage)) return tinyfd_inputBox(aTitle, "INVALID MESSAGE WITH QUOTES", aDefaultInput);
  5071. if (tfd_quoteDetected(aDefaultInput)) return tinyfd_inputBox(aTitle, aMessage, "INVALID DEFAULT_INPUT WITH QUOTES: use the GRAVE ACCENT \\x60 instead.");
  5072. lTitleLen = aTitle ? strlen(aTitle) : 0 ;
  5073. lMessageLen = aMessage ? strlen(aMessage) : 0 ;
  5074. if ( !aTitle || strcmp(aTitle,"tinyfd_query") )
  5075. {
  5076. lDialogString = (char *) malloc( MAX_PATH_OR_CMD + lTitleLen + lMessageLen );
  5077. }
  5078. if ( osascriptPresent( ) )
  5079. {
  5080. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char *)1;}
  5081. strcpy( lDialogString , "osascript ");
  5082. if ( ! osx9orBetter() ) strcat( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'");
  5083. strcat( lDialogString , " -e 'try' -e 'display dialog \"") ;
  5084. if ( aMessage && strlen(aMessage) )
  5085. {
  5086. strcat(lDialogString, aMessage) ;
  5087. }
  5088. strcat(lDialogString, "\" ") ;
  5089. strcat(lDialogString, "default answer \"") ;
  5090. if ( aDefaultInput && strlen(aDefaultInput) )
  5091. {
  5092. strcat(lDialogString, aDefaultInput) ;
  5093. }
  5094. strcat(lDialogString, "\" ") ;
  5095. if ( ! aDefaultInput )
  5096. {
  5097. strcat(lDialogString, "hidden answer true ") ;
  5098. }
  5099. if ( aTitle && strlen(aTitle) )
  5100. {
  5101. strcat(lDialogString, "with title \"") ;
  5102. strcat(lDialogString, aTitle) ;
  5103. strcat(lDialogString, "\" ") ;
  5104. }
  5105. strcat(lDialogString, "with icon note' ") ;
  5106. strcat(lDialogString, "-e '\"1\" & text returned of result' " );
  5107. strcat(lDialogString, "-e 'on error number -128' " ) ;
  5108. strcat(lDialogString, "-e '0' " );
  5109. strcat(lDialogString, "-e 'end try'") ;
  5110. if ( ! osx9orBetter() ) strcat(lDialogString, " -e 'end tell'") ;
  5111. }
  5112. else if ( tfd_kdialogPresent() )
  5113. {
  5114. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char *)1;}
  5115. strcpy( lDialogString , "szAnswer=$(kdialog" ) ;
  5116. if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() )
  5117. {
  5118. strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
  5119. }
  5120. if ( ! aDefaultInput )
  5121. {
  5122. strcat(lDialogString, " --password ") ;
  5123. }
  5124. else
  5125. {
  5126. strcat(lDialogString, " --inputbox ") ;
  5127. }
  5128. strcat(lDialogString, "\"") ;
  5129. if ( aMessage && strlen(aMessage) )
  5130. {
  5131. strcat(lDialogString, aMessage ) ;
  5132. }
  5133. strcat(lDialogString , "\" \"" ) ;
  5134. if ( aDefaultInput && strlen(aDefaultInput) )
  5135. {
  5136. strcat(lDialogString, aDefaultInput ) ;
  5137. }
  5138. strcat(lDialogString , "\"" ) ;
  5139. if ( aTitle && strlen(aTitle) )
  5140. {
  5141. strcat(lDialogString, " --title \"") ;
  5142. strcat(lDialogString, aTitle) ;
  5143. strcat(lDialogString, "\"") ;
  5144. }
  5145. strcat( lDialogString ,
  5146. ");if [ $? = 0 ];then echo 1$szAnswer;else echo 0$szAnswer;fi");
  5147. }
  5148. else if ( tfd_zenityPresent() || tfd_matedialogPresent() || tfd_shellementaryPresent() || tfd_qarmaPresent() )
  5149. {
  5150. if ( tfd_zenityPresent() )
  5151. {
  5152. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char *)1;}
  5153. strcpy( lDialogString , "szAnswer=$(zenity" ) ;
  5154. if ( (tfd_zenity3Present() >= 4) && !getenv("SSH_TTY") && tfd_xpropPresent() )
  5155. {
  5156. strcat( lDialogString, " --attach=$(sleep .01;xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
  5157. }
  5158. }
  5159. else if ( tfd_matedialogPresent() )
  5160. {
  5161. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char *)1;}
  5162. strcpy( lDialogString , "szAnswer=$(matedialog" ) ;
  5163. }
  5164. else if ( tfd_shellementaryPresent() )
  5165. {
  5166. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"shellementary");return (char *)1;}
  5167. strcpy( lDialogString , "szAnswer=$(shellementary" ) ;
  5168. }
  5169. else
  5170. {
  5171. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char *)1;}
  5172. strcpy( lDialogString , "szAnswer=$(qarma" ) ;
  5173. if ( !getenv("SSH_TTY") && tfd_xpropPresent() )
  5174. {
  5175. strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
  5176. }
  5177. }
  5178. strcat( lDialogString ," --entry" ) ;
  5179. strcat(lDialogString, " --title=\"") ;
  5180. if (aTitle && strlen(aTitle)) strcat(lDialogString, aTitle) ;
  5181. strcat(lDialogString, "\"") ;
  5182. strcat(lDialogString, " --text=\"") ;
  5183. if (aMessage && strlen(aMessage)) strcat(lDialogString, aMessage) ;
  5184. strcat(lDialogString, "\"") ;
  5185. if ( aDefaultInput )
  5186. {
  5187. strcat(lDialogString, " --entry-text=\"") ;
  5188. strcat(lDialogString, aDefaultInput) ;
  5189. strcat(lDialogString, "\"") ;
  5190. }
  5191. else
  5192. {
  5193. strcat(lDialogString, " --hide-text") ;
  5194. }
  5195. if (tinyfd_silent) strcat( lDialogString , " 2>/dev/null ");
  5196. strcat( lDialogString ,
  5197. ");if [ $? = 0 ];then echo 1$szAnswer;else echo 0$szAnswer;fi");
  5198. }
  5199. else if (tfd_yadPresent())
  5200. {
  5201. if (aTitle && !strcmp(aTitle, "tinyfd_query")) { strcpy(tinyfd_response, "yad"); return (char*)1; }
  5202. strcpy(lDialogString, "szAnswer=$(yad --entry");
  5203. if (aTitle && strlen(aTitle))
  5204. {
  5205. strcat(lDialogString, " --title=\"");
  5206. strcat(lDialogString, aTitle);
  5207. strcat(lDialogString, "\"");
  5208. }
  5209. if (aMessage && strlen(aMessage))
  5210. {
  5211. strcat(lDialogString, " --text=\"");
  5212. strcat(lDialogString, aMessage);
  5213. strcat(lDialogString, "\"");
  5214. }
  5215. if (aDefaultInput && strlen(aDefaultInput))
  5216. {
  5217. strcat(lDialogString, " --entry-text=\"");
  5218. strcat(lDialogString, aDefaultInput);
  5219. strcat(lDialogString, "\"");
  5220. }
  5221. else
  5222. {
  5223. strcat(lDialogString, " --hide-text");
  5224. }
  5225. if (tinyfd_silent) strcat(lDialogString, " 2>/dev/null ");
  5226. strcat(lDialogString,
  5227. ");if [ $? = 0 ];then echo 1$szAnswer;else echo 0$szAnswer;fi");
  5228. }
  5229. else if ( gxmessagePresent() || gmessagePresent() )
  5230. {
  5231. if ( gxmessagePresent() ) {
  5232. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gxmessage");return (char *)1;}
  5233. strcpy( lDialogString , "szAnswer=$(gxmessage -buttons Ok:1,Cancel:0 -center \"");
  5234. }
  5235. else
  5236. {
  5237. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gmessage");return (char *)1;}
  5238. strcpy( lDialogString , "szAnswer=$(gmessage -buttons Ok:1,Cancel:0 -center \"");
  5239. }
  5240. if ( aMessage && strlen(aMessage) )
  5241. {
  5242. strcat( lDialogString , aMessage ) ;
  5243. }
  5244. strcat(lDialogString, "\"" ) ;
  5245. if ( aTitle && strlen(aTitle) )
  5246. {
  5247. strcat( lDialogString , " -title \"");
  5248. strcat( lDialogString , aTitle ) ;
  5249. strcat(lDialogString, "\" " ) ;
  5250. }
  5251. strcat(lDialogString, " -entrytext \"" ) ;
  5252. if ( aDefaultInput && strlen(aDefaultInput) )
  5253. {
  5254. strcat( lDialogString , aDefaultInput ) ;
  5255. }
  5256. strcat(lDialogString, "\"" ) ;
  5257. strcat( lDialogString , ");echo $?$szAnswer");
  5258. }
  5259. else if ( !gdialogPresent() && !xdialogPresent() && tkinter3Present( ) )
  5260. {
  5261. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return (char *)1;}
  5262. strcpy( lDialogString , gPython3Name ) ;
  5263. strcat( lDialogString ,
  5264. " -S -c \"import tkinter; from tkinter import simpledialog;root=tkinter.Tk();root.withdraw();");
  5265. strcat( lDialogString ,"res=simpledialog.askstring(" ) ;
  5266. if ( aTitle && strlen(aTitle) )
  5267. {
  5268. strcat(lDialogString, "title='") ;
  5269. strcat(lDialogString, aTitle) ;
  5270. strcat(lDialogString, "',") ;
  5271. }
  5272. if ( aMessage && strlen(aMessage) )
  5273. {
  5274. strcat(lDialogString, "prompt='") ;
  5275. lpDialogString = lDialogString + strlen(lDialogString);
  5276. tfd_replaceSubStr( aMessage , "\n" , "\\n" , lpDialogString ) ;
  5277. strcat(lDialogString, "',") ;
  5278. }
  5279. if ( aDefaultInput )
  5280. {
  5281. if ( strlen(aDefaultInput) )
  5282. {
  5283. strcat(lDialogString, "initialvalue='") ;
  5284. strcat(lDialogString, aDefaultInput) ;
  5285. strcat(lDialogString, "',") ;
  5286. }
  5287. }
  5288. else
  5289. {
  5290. strcat(lDialogString, "show='*'") ;
  5291. }
  5292. strcat(lDialogString, ");\nif res is None :\n\tprint(0)");
  5293. strcat(lDialogString, "\nelse :\n\tprint('1'+res)\n\"" ) ;
  5294. }
  5295. else if ( !gdialogPresent() && !xdialogPresent() && tkinter2Present( ) )
  5296. {
  5297. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python2-tkinter");return (char *)1;}
  5298. strcpy( lDialogString , "export PYTHONIOENCODING=utf-8;" ) ;
  5299. strcat( lDialogString , gPython2Name ) ;
  5300. if ( ! isTerminalRunning( ) && tfd_isDarwin( ) )
  5301. {
  5302. strcat( lDialogString , " -i" ) ; /* for osx without console */
  5303. }
  5304. strcat( lDialogString ,
  5305. " -S -c \"import Tkinter,tkSimpleDialog;root=Tkinter.Tk();root.withdraw();");
  5306. if ( tfd_isDarwin( ) )
  5307. {
  5308. strcat( lDialogString ,
  5309. "import os;os.system('''/usr/bin/osascript -e 'tell app \\\"Finder\\\" to set \
  5310. frontmost of process \\\"Python\\\" to true' ''');");
  5311. }
  5312. strcat( lDialogString ,"res=tkSimpleDialog.askstring(" ) ;
  5313. if ( aTitle && strlen(aTitle) )
  5314. {
  5315. strcat(lDialogString, "title='") ;
  5316. strcat(lDialogString, aTitle) ;
  5317. strcat(lDialogString, "',") ;
  5318. }
  5319. if ( aMessage && strlen(aMessage) )
  5320. {
  5321. strcat(lDialogString, "prompt='") ;
  5322. lpDialogString = lDialogString + strlen(lDialogString);
  5323. tfd_replaceSubStr( aMessage , "\n" , "\\n" , lpDialogString ) ;
  5324. strcat(lDialogString, "',") ;
  5325. }
  5326. if ( aDefaultInput )
  5327. {
  5328. if ( strlen(aDefaultInput) )
  5329. {
  5330. strcat(lDialogString, "initialvalue='") ;
  5331. strcat(lDialogString, aDefaultInput) ;
  5332. strcat(lDialogString, "',") ;
  5333. }
  5334. }
  5335. else
  5336. {
  5337. strcat(lDialogString, "show='*'") ;
  5338. }
  5339. strcat(lDialogString, ");\nif res is None :\n\tprint 0");
  5340. strcat(lDialogString, "\nelse :\n\tprint '1'+res\n\"" ) ;
  5341. }
  5342. else if ( gdialogPresent() || xdialogPresent() || dialogName() || whiptailPresent() )
  5343. {
  5344. if ( gdialogPresent( ) )
  5345. {
  5346. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gdialog");return (char *)1;}
  5347. lWasGraphicDialog = 1 ;
  5348. lWasGdialog = 1 ;
  5349. strcpy( lDialogString , "(gdialog " ) ;
  5350. }
  5351. else if ( xdialogPresent( ) )
  5352. {
  5353. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char *)1;}
  5354. lWasGraphicDialog = 1 ;
  5355. strcpy( lDialogString , "(Xdialog " ) ;
  5356. }
  5357. else if ( dialogName( ) )
  5358. {
  5359. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char *)0;}
  5360. if ( isTerminalRunning( ) )
  5361. {
  5362. strcpy( lDialogString , "(dialog " ) ;
  5363. }
  5364. else
  5365. {
  5366. lWasXterm = 1 ;
  5367. strcpy( lDialogString , terminalName() ) ;
  5368. strcat( lDialogString , "'(" ) ;
  5369. strcat( lDialogString , dialogName() ) ;
  5370. strcat( lDialogString , " " ) ;
  5371. }
  5372. }
  5373. else if ( isTerminalRunning( ) )
  5374. {
  5375. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"whiptail");return (char *)0;}
  5376. strcpy( lDialogString , "(whiptail " ) ;
  5377. }
  5378. else
  5379. {
  5380. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"whiptail");return (char *)0;}
  5381. lWasXterm = 1 ;
  5382. strcpy( lDialogString , terminalName() ) ;
  5383. strcat( lDialogString , "'(whiptail " ) ;
  5384. }
  5385. if ( aTitle && strlen(aTitle) )
  5386. {
  5387. strcat(lDialogString, "--title \"") ;
  5388. strcat(lDialogString, aTitle) ;
  5389. strcat(lDialogString, "\" ") ;
  5390. }
  5391. if ( !xdialogPresent() && !gdialogPresent() )
  5392. {
  5393. strcat(lDialogString, "--backtitle \"") ;
  5394. strcat(lDialogString, "tab: move focus") ;
  5395. if ( ! aDefaultInput && !lWasGdialog )
  5396. {
  5397. strcat(lDialogString, " (sometimes nothing, no blink nor star, is shown in text field)") ;
  5398. }
  5399. strcat(lDialogString, "\" ") ;
  5400. }
  5401. if ( aDefaultInput || lWasGdialog )
  5402. {
  5403. strcat( lDialogString , "--inputbox" ) ;
  5404. }
  5405. else
  5406. {
  5407. if ( !lWasGraphicDialog && dialogName() && isDialogVersionBetter09b() )
  5408. {
  5409. strcat( lDialogString , "--insecure " ) ;
  5410. }
  5411. strcat( lDialogString , "--passwordbox" ) ;
  5412. }
  5413. strcat( lDialogString , " \"" ) ;
  5414. if ( aMessage && strlen(aMessage) )
  5415. {
  5416. strcat(lDialogString, aMessage) ;
  5417. }
  5418. strcat(lDialogString,"\" 10 60 ") ;
  5419. if ( aDefaultInput && strlen(aDefaultInput) )
  5420. {
  5421. strcat(lDialogString, "\"") ;
  5422. strcat(lDialogString, aDefaultInput) ;
  5423. strcat(lDialogString, "\" ") ;
  5424. }
  5425. if ( lWasGraphicDialog )
  5426. {
  5427. strcat(lDialogString,") 2>/tmp/tinyfd.txt;\
  5428. if [ $? = 0 ];then tinyfdBool=1;else tinyfdBool=0;fi;\
  5429. tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ;
  5430. }
  5431. else
  5432. {
  5433. strcat(lDialogString,">/dev/tty ) 2>/tmp/tinyfd.txt;\
  5434. if [ $? = 0 ];then tinyfdBool=1;else tinyfdBool=0;fi;\
  5435. tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ;
  5436. if ( lWasXterm )
  5437. {
  5438. strcat(lDialogString," >/tmp/tinyfd0.txt';cat /tmp/tinyfd0.txt");
  5439. }
  5440. else
  5441. {
  5442. strcat(lDialogString, "; clear >/dev/tty") ;
  5443. }
  5444. }
  5445. }
  5446. else if ( ! isTerminalRunning( ) && terminalName() )
  5447. {
  5448. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return (char *)0;}
  5449. lWasBasicXterm = 1 ;
  5450. strcpy( lDialogString , terminalName() ) ;
  5451. strcat( lDialogString , "'" ) ;
  5452. if ( !gWarningDisplayed && !tinyfd_forceConsole)
  5453. {
  5454. gWarningDisplayed = 1 ;
  5455. tinyfd_messageBox(gTitle,tinyfd_needs,"ok","warning",0);
  5456. }
  5457. if ( aTitle && strlen(aTitle) && !tinyfd_forceConsole)
  5458. {
  5459. strcat( lDialogString , "echo \"" ) ;
  5460. strcat( lDialogString, aTitle) ;
  5461. strcat( lDialogString , "\";echo;" ) ;
  5462. }
  5463. strcat( lDialogString , "echo \"" ) ;
  5464. if ( aMessage && strlen(aMessage) )
  5465. {
  5466. strcat( lDialogString, aMessage) ;
  5467. }
  5468. strcat( lDialogString , "\";read " ) ;
  5469. if ( ! aDefaultInput )
  5470. {
  5471. strcat( lDialogString , "-s " ) ;
  5472. }
  5473. strcat( lDialogString , "-p \"" ) ;
  5474. strcat( lDialogString , "(esc+enter to cancel): \" ANSWER " ) ;
  5475. strcat( lDialogString , ";echo 1$ANSWER >/tmp/tinyfd.txt';" ) ;
  5476. strcat( lDialogString , "cat -v /tmp/tinyfd.txt");
  5477. }
  5478. else if ( !gWarningDisplayed && ! isTerminalRunning( ) && ! terminalName() ) {
  5479. gWarningDisplayed = 1 ;
  5480. tinyfd_messageBox(gTitle,tinyfd_needs,"ok","warning",0);
  5481. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"no_solution");return (char *)0;}
  5482. free(lDialogString);
  5483. return NULL;
  5484. }
  5485. else
  5486. {
  5487. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return (char *)0;}
  5488. if ( !gWarningDisplayed && !tinyfd_forceConsole)
  5489. {
  5490. gWarningDisplayed = 1 ;
  5491. tinyfd_messageBox(gTitle,tinyfd_needs,"ok","warning",0);
  5492. }
  5493. if ( aTitle && strlen(aTitle) )
  5494. {
  5495. printf("\n%s\n", aTitle);
  5496. }
  5497. if ( aMessage && strlen(aMessage) )
  5498. {
  5499. printf("\n%s\n",aMessage);
  5500. }
  5501. printf("(esc+enter to cancel): "); fflush(stdout);
  5502. if ( ! aDefaultInput )
  5503. {
  5504. tcgetattr(STDIN_FILENO, & oldt) ;
  5505. newt = oldt ;
  5506. newt.c_lflag &= ~ECHO ;
  5507. tcsetattr(STDIN_FILENO, TCSANOW, & newt);
  5508. }
  5509. lEOF = fgets(lBuff, MAX_PATH_OR_CMD, stdin);
  5510. /* printf("lbuff<%c><%d>\n",lBuff[0],lBuff[0]); */
  5511. if ( ! lEOF || (lBuff[0] == '\0') )
  5512. {
  5513. free(lDialogString);
  5514. return NULL;
  5515. }
  5516. if ( lBuff[0] == '\n' )
  5517. {
  5518. lEOF = fgets(lBuff, MAX_PATH_OR_CMD, stdin);
  5519. /* printf("lbuff<%c><%d>\n",lBuff[0],lBuff[0]); */
  5520. if ( ! lEOF || (lBuff[0] == '\0') )
  5521. {
  5522. free(lDialogString);
  5523. return NULL;
  5524. }
  5525. }
  5526. if ( ! aDefaultInput )
  5527. {
  5528. tcsetattr(STDIN_FILENO, TCSANOW, & oldt);
  5529. printf("\n");
  5530. }
  5531. printf("\n");
  5532. if ( strchr(lBuff,27) )
  5533. {
  5534. free(lDialogString);
  5535. return NULL ;
  5536. }
  5537. if ( lBuff[strlen( lBuff ) -1] == '\n' )
  5538. {
  5539. lBuff[strlen( lBuff ) -1] = '\0' ;
  5540. }
  5541. free(lDialogString);
  5542. return lBuff ;
  5543. }
  5544. if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ;
  5545. lIn = popen( lDialogString , "r" );
  5546. if ( ! lIn )
  5547. {
  5548. if ( fileExists("/tmp/tinyfd.txt") )
  5549. {
  5550. wipefile("/tmp/tinyfd.txt");
  5551. remove("/tmp/tinyfd.txt");
  5552. }
  5553. if ( fileExists("/tmp/tinyfd0.txt") )
  5554. {
  5555. wipefile("/tmp/tinyfd0.txt");
  5556. remove("/tmp/tinyfd0.txt");
  5557. }
  5558. free(lDialogString);
  5559. return NULL ;
  5560. }
  5561. while ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL )
  5562. {}
  5563. pclose( lIn ) ;
  5564. if ( fileExists("/tmp/tinyfd.txt") )
  5565. {
  5566. wipefile("/tmp/tinyfd.txt");
  5567. remove("/tmp/tinyfd.txt");
  5568. }
  5569. if ( fileExists("/tmp/tinyfd0.txt") )
  5570. {
  5571. wipefile("/tmp/tinyfd0.txt");
  5572. remove("/tmp/tinyfd0.txt");
  5573. }
  5574. /* printf( "len Buff: %lu\n" , strlen(lBuff) ) ; */
  5575. /* printf( "lBuff0: %s\n" , lBuff ) ; */
  5576. if ( strlen( lBuff ) && lBuff[strlen( lBuff ) -1] == '\n' )
  5577. {
  5578. lBuff[strlen( lBuff ) -1] = '\0' ;
  5579. }
  5580. /* printf( "lBuff1: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; */
  5581. if ( lWasBasicXterm )
  5582. {
  5583. if ( strstr(lBuff,"^[") ) /* esc was pressed */
  5584. {
  5585. free(lDialogString);
  5586. return NULL ;
  5587. }
  5588. }
  5589. lResult = strncmp( lBuff , "1" , 1) ? 0 : 1 ;
  5590. /* printf( "lResult: %d \n" , lResult ) ; */
  5591. if ( ! lResult )
  5592. {
  5593. free(lDialogString);
  5594. return NULL ;
  5595. }
  5596. /* printf( "lBuff+1: %s\n" , lBuff+1 ) ; */
  5597. free(lDialogString);
  5598. return lBuff+1 ;
  5599. }
  5600. char * tinyfd_saveFileDialog(
  5601. char const * aTitle , /* NULL or "" */
  5602. char const * aDefaultPathAndOrFile , /* NULL or "" , ends with / to set only a directory */
  5603. int aNumOfFilterPatterns , /* 0 */
  5604. char const * const * aFilterPatterns , /* NULL or {"*.txt","*.doc"} */
  5605. char const * aSingleFilterDescription ) /* NULL or "text files" */
  5606. {
  5607. static char lBuff[MAX_PATH_OR_CMD] ;
  5608. static char lLastDirectory[MAX_PATH_OR_CMD] = "$PWD" ;
  5609. char lDialogString[MAX_PATH_OR_CMD] ;
  5610. char lString[MAX_PATH_OR_CMD] ;
  5611. int i ;
  5612. int lWasGraphicDialog = 0 ;
  5613. int lWasXterm = 0 ;
  5614. char * p ;
  5615. char * lPointerInputBox ;
  5616. FILE * lIn ;
  5617. lBuff[0]='\0';
  5618. if ( ! aFilterPatterns ) aNumOfFilterPatterns = 0 ;
  5619. if (tfd_quoteDetected(aTitle)) return tinyfd_saveFileDialog("INVALID TITLE WITH QUOTES", aDefaultPathAndOrFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription);
  5620. if (tfd_quoteDetected(aDefaultPathAndOrFile)) return tinyfd_saveFileDialog(aTitle, "INVALID DEFAULT_PATH WITH QUOTES", aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription);
  5621. if (tfd_quoteDetected(aSingleFilterDescription)) return tinyfd_saveFileDialog(aTitle, aDefaultPathAndOrFile, aNumOfFilterPatterns, aFilterPatterns, "INVALID FILTER_DESCRIPTION WITH QUOTES");
  5622. for (i = 0; i < aNumOfFilterPatterns; i++)
  5623. {
  5624. if (tfd_quoteDetected(aFilterPatterns[i])) return tinyfd_saveFileDialog("INVALID FILTER_PATTERN WITH QUOTES: use the GRAVE ACCENT \\x60 instead.", aDefaultPathAndOrFile, 0, NULL, NULL);
  5625. }
  5626. if ( osascriptPresent( ) )
  5627. {
  5628. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char *)1;}
  5629. strcpy( lDialogString , "osascript ");
  5630. if ( ! osx9orBetter() ) strcat( lDialogString , " -e 'tell application \"Finder\"' -e 'Activate'");
  5631. strcat( lDialogString , " -e 'try' -e 'POSIX path of ( choose file name " );
  5632. if ( aTitle && strlen(aTitle) )
  5633. {
  5634. strcat(lDialogString, "with prompt \"") ;
  5635. strcat(lDialogString, aTitle) ;
  5636. strcat(lDialogString, "\" ") ;
  5637. }
  5638. getPathWithoutFinalSlash( lString , aDefaultPathAndOrFile ) ;
  5639. if ( strlen(lString) )
  5640. {
  5641. strcat(lDialogString, "default location \"") ;
  5642. strcat(lDialogString, lString ) ;
  5643. strcat(lDialogString , "\" " ) ;
  5644. }
  5645. getLastName( lString , aDefaultPathAndOrFile ) ;
  5646. if ( strlen(lString) )
  5647. {
  5648. strcat(lDialogString, "default name \"") ;
  5649. strcat(lDialogString, lString ) ;
  5650. strcat(lDialogString , "\" " ) ;
  5651. }
  5652. strcat( lDialogString , ")' " ) ;
  5653. strcat(lDialogString, "-e 'on error number -128' " ) ;
  5654. strcat(lDialogString, "-e 'end try'") ;
  5655. if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ;
  5656. }
  5657. else if ( tfd_kdialogPresent() )
  5658. {
  5659. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char *)1;}
  5660. strcpy( lDialogString , "kdialog" ) ;
  5661. if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() )
  5662. {
  5663. strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
  5664. }
  5665. strcat( lDialogString , " --getsavefilename " ) ;
  5666. if ( aDefaultPathAndOrFile && strlen(aDefaultPathAndOrFile) )
  5667. {
  5668. strcat(lDialogString, "\"") ;
  5669. if ( aDefaultPathAndOrFile[0] != '/' )
  5670. {
  5671. strcat(lDialogString, lLastDirectory) ;
  5672. strcat(lDialogString , "/" ) ;
  5673. }
  5674. strcat(lDialogString, aDefaultPathAndOrFile ) ;
  5675. strcat(lDialogString , "\"" ) ;
  5676. }
  5677. else
  5678. {
  5679. strcat(lDialogString, "\"") ;
  5680. strcat(lDialogString, lLastDirectory) ;
  5681. strcat(lDialogString , "/" ) ;
  5682. strcat(lDialogString, "\"") ;
  5683. }
  5684. if ( aNumOfFilterPatterns > 0 )
  5685. {
  5686. strcat(lDialogString , " \"" ) ;
  5687. strcat( lDialogString , aFilterPatterns[0] ) ;
  5688. for ( i = 1 ; i < aNumOfFilterPatterns ; i ++ )
  5689. {
  5690. strcat( lDialogString , " " ) ;
  5691. strcat( lDialogString , aFilterPatterns[i] ) ;
  5692. }
  5693. if ( aSingleFilterDescription && strlen(aSingleFilterDescription) )
  5694. {
  5695. strcat( lDialogString , " | " ) ;
  5696. strcat( lDialogString , aSingleFilterDescription ) ;
  5697. }
  5698. strcat( lDialogString , "\"" ) ;
  5699. }
  5700. if ( aTitle && strlen(aTitle) )
  5701. {
  5702. strcat(lDialogString, " --title \"") ;
  5703. strcat(lDialogString, aTitle) ;
  5704. strcat(lDialogString, "\"") ;
  5705. }
  5706. }
  5707. else if ( tfd_zenityPresent() || tfd_matedialogPresent() || tfd_shellementaryPresent() || tfd_qarmaPresent() )
  5708. {
  5709. if ( tfd_zenityPresent() )
  5710. {
  5711. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char *)1;}
  5712. strcpy( lDialogString , "zenity" ) ;
  5713. if ( (tfd_zenity3Present() >= 4) && !getenv("SSH_TTY") && tfd_xpropPresent() )
  5714. {
  5715. strcat( lDialogString, " --attach=$(sleep .01;xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
  5716. }
  5717. }
  5718. else if ( tfd_matedialogPresent() )
  5719. {
  5720. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char *)1;}
  5721. strcpy( lDialogString , "matedialog" ) ;
  5722. }
  5723. else if ( tfd_shellementaryPresent() )
  5724. {
  5725. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"shellementary");return (char *)1;}
  5726. strcpy( lDialogString , "shellementary" ) ;
  5727. }
  5728. else
  5729. {
  5730. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char *)1;}
  5731. strcpy( lDialogString , "qarma" ) ;
  5732. if ( !getenv("SSH_TTY") && tfd_xpropPresent() )
  5733. {
  5734. strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
  5735. }
  5736. }
  5737. strcat(lDialogString, " --file-selection --save --confirm-overwrite" ) ;
  5738. strcat(lDialogString, " --title=\"") ;
  5739. if (aTitle && strlen(aTitle)) strcat(lDialogString, aTitle) ;
  5740. strcat(lDialogString, "\"") ;
  5741. if ( aDefaultPathAndOrFile && strlen(aDefaultPathAndOrFile) )
  5742. {
  5743. strcat(lDialogString, " --filename=\"") ;
  5744. strcat(lDialogString, aDefaultPathAndOrFile) ;
  5745. strcat(lDialogString, "\"") ;
  5746. }
  5747. if ( aNumOfFilterPatterns > 0 )
  5748. {
  5749. strcat( lDialogString , " --file-filter='" ) ;
  5750. if ( aSingleFilterDescription && strlen(aSingleFilterDescription) )
  5751. {
  5752. strcat( lDialogString , aSingleFilterDescription ) ;
  5753. strcat( lDialogString , " |" ) ;
  5754. }
  5755. for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ )
  5756. {
  5757. strcat( lDialogString , " " ) ;
  5758. strcat( lDialogString , aFilterPatterns[i] ) ;
  5759. }
  5760. strcat( lDialogString , "' --file-filter='All files | *'" ) ;
  5761. }
  5762. if (tinyfd_silent) strcat( lDialogString , " 2>/dev/null ");
  5763. }
  5764. else if (tfd_yadPresent())
  5765. {
  5766. if (aTitle && !strcmp(aTitle, "tinyfd_query")) { strcpy(tinyfd_response, "yad"); return (char*)1; }
  5767. strcpy(lDialogString, "yad --file --save --confirm-overwrite");
  5768. if (aTitle && strlen(aTitle))
  5769. {
  5770. strcat(lDialogString, " --title=\"");
  5771. strcat(lDialogString, aTitle);
  5772. strcat(lDialogString, "\"");
  5773. }
  5774. if (aDefaultPathAndOrFile && strlen(aDefaultPathAndOrFile))
  5775. {
  5776. strcat(lDialogString, " --filename=\"");
  5777. strcat(lDialogString, aDefaultPathAndOrFile);
  5778. strcat(lDialogString, "\"");
  5779. }
  5780. if (aNumOfFilterPatterns > 0)
  5781. {
  5782. strcat(lDialogString, " --file-filter='");
  5783. if (aSingleFilterDescription && strlen(aSingleFilterDescription))
  5784. {
  5785. strcat(lDialogString, aSingleFilterDescription);
  5786. strcat(lDialogString, " |");
  5787. }
  5788. for (i = 0; i < aNumOfFilterPatterns; i++)
  5789. {
  5790. strcat(lDialogString, " ");
  5791. strcat(lDialogString, aFilterPatterns[i]);
  5792. }
  5793. strcat(lDialogString, "' --file-filter='All files | *'");
  5794. }
  5795. if (tinyfd_silent) strcat(lDialogString, " 2>/dev/null ");
  5796. }
  5797. else if ( !xdialogPresent() && tkinter3Present( ) )
  5798. {
  5799. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return (char *)1;}
  5800. strcpy( lDialogString , gPython3Name ) ;
  5801. strcat( lDialogString ,
  5802. " -S -c \"import tkinter;from tkinter import filedialog;root=tkinter.Tk();root.withdraw();");
  5803. strcat( lDialogString , "res=filedialog.asksaveasfilename(");
  5804. if ( aTitle && strlen(aTitle) )
  5805. {
  5806. strcat(lDialogString, "title='") ;
  5807. strcat(lDialogString, aTitle) ;
  5808. strcat(lDialogString, "',") ;
  5809. }
  5810. if ( aDefaultPathAndOrFile && strlen(aDefaultPathAndOrFile) )
  5811. {
  5812. getPathWithoutFinalSlash( lString , aDefaultPathAndOrFile ) ;
  5813. if ( strlen(lString) )
  5814. {
  5815. strcat(lDialogString, "initialdir='") ;
  5816. strcat(lDialogString, lString ) ;
  5817. strcat(lDialogString , "'," ) ;
  5818. }
  5819. getLastName( lString , aDefaultPathAndOrFile ) ;
  5820. if ( strlen(lString) )
  5821. {
  5822. strcat(lDialogString, "initialfile='") ;
  5823. strcat(lDialogString, lString ) ;
  5824. strcat(lDialogString , "'," ) ;
  5825. }
  5826. }
  5827. if ( ( aNumOfFilterPatterns > 1 )
  5828. || ( (aNumOfFilterPatterns == 1) /* test because poor osx behaviour */
  5829. && ( aFilterPatterns[0][strlen(aFilterPatterns[0])-1] != '*' ) ) )
  5830. {
  5831. strcat(lDialogString , "filetypes=(" ) ;
  5832. strcat( lDialogString , "('" ) ;
  5833. if ( aSingleFilterDescription && strlen(aSingleFilterDescription) )
  5834. {
  5835. strcat( lDialogString , aSingleFilterDescription ) ;
  5836. }
  5837. strcat( lDialogString , "',(" ) ;
  5838. for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ )
  5839. {
  5840. strcat( lDialogString , "'" ) ;
  5841. strcat( lDialogString , aFilterPatterns[i] ) ;
  5842. strcat( lDialogString , "'," ) ;
  5843. }
  5844. strcat( lDialogString , "))," ) ;
  5845. strcat( lDialogString , "('All files','*'))" ) ;
  5846. }
  5847. strcat( lDialogString, ");\nif not isinstance(res, tuple):\n\tprint(res)\n\"" ) ;
  5848. }
  5849. else if ( !xdialogPresent() && tkinter2Present( ) )
  5850. {
  5851. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python2-tkinter");return (char *)1;}
  5852. strcpy( lDialogString , "export PYTHONIOENCODING=utf-8;" ) ;
  5853. strcat( lDialogString , gPython2Name ) ;
  5854. if ( ! isTerminalRunning( ) && tfd_isDarwin( ))
  5855. {
  5856. strcat( lDialogString , " -i" ) ; /* for osx without console */
  5857. }
  5858. strcat( lDialogString ,
  5859. " -S -c \"import Tkinter,tkFileDialog;root=Tkinter.Tk();root.withdraw();");
  5860. if ( tfd_isDarwin( ) )
  5861. {
  5862. strcat( lDialogString ,
  5863. "import os;os.system('''/usr/bin/osascript -e 'tell app \\\"Finder\\\" to set\
  5864. frontmost of process \\\"Python\\\" to true' ''');");
  5865. }
  5866. strcat( lDialogString , "res=tkFileDialog.asksaveasfilename(");
  5867. if ( aTitle && strlen(aTitle) )
  5868. {
  5869. strcat(lDialogString, "title='") ;
  5870. strcat(lDialogString, aTitle) ;
  5871. strcat(lDialogString, "',") ;
  5872. }
  5873. if ( aDefaultPathAndOrFile && strlen(aDefaultPathAndOrFile) )
  5874. {
  5875. getPathWithoutFinalSlash( lString , aDefaultPathAndOrFile ) ;
  5876. if ( strlen(lString) )
  5877. {
  5878. strcat(lDialogString, "initialdir='") ;
  5879. strcat(lDialogString, lString ) ;
  5880. strcat(lDialogString , "'," ) ;
  5881. }
  5882. getLastName( lString , aDefaultPathAndOrFile ) ;
  5883. if ( strlen(lString) )
  5884. {
  5885. strcat(lDialogString, "initialfile='") ;
  5886. strcat(lDialogString, lString ) ;
  5887. strcat(lDialogString , "'," ) ;
  5888. }
  5889. }
  5890. if ( ( aNumOfFilterPatterns > 1 )
  5891. || ( (aNumOfFilterPatterns == 1) /* test because poor osx behaviour */
  5892. && ( aFilterPatterns[0][strlen(aFilterPatterns[0])-1] != '*' ) ) )
  5893. {
  5894. strcat(lDialogString , "filetypes=(" ) ;
  5895. strcat( lDialogString , "('" ) ;
  5896. if ( aSingleFilterDescription && strlen(aSingleFilterDescription) )
  5897. {
  5898. strcat( lDialogString , aSingleFilterDescription ) ;
  5899. }
  5900. strcat( lDialogString , "',(" ) ;
  5901. for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ )
  5902. {
  5903. strcat( lDialogString , "'" ) ;
  5904. strcat( lDialogString , aFilterPatterns[i] ) ;
  5905. strcat( lDialogString , "'," ) ;
  5906. }
  5907. strcat( lDialogString , "))," ) ;
  5908. strcat( lDialogString , "('All files','*'))" ) ;
  5909. }
  5910. strcat( lDialogString, ");\nif not isinstance(res, tuple):\n\tprint res \n\"" ) ;
  5911. }
  5912. else if ( xdialogPresent() || dialogName() )
  5913. {
  5914. if ( xdialogPresent( ) )
  5915. {
  5916. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char *)1;}
  5917. lWasGraphicDialog = 1 ;
  5918. strcpy( lDialogString , "(Xdialog " ) ;
  5919. }
  5920. else if ( isTerminalRunning( ) )
  5921. {
  5922. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char *)0;}
  5923. strcpy( lDialogString , "(dialog " ) ;
  5924. }
  5925. else
  5926. {
  5927. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char *)0;}
  5928. lWasXterm = 1 ;
  5929. strcpy( lDialogString , terminalName() ) ;
  5930. strcat( lDialogString , "'(" ) ;
  5931. strcat( lDialogString , dialogName() ) ;
  5932. strcat( lDialogString , " " ) ;
  5933. }
  5934. if ( aTitle && strlen(aTitle) )
  5935. {
  5936. strcat(lDialogString, "--title \"") ;
  5937. strcat(lDialogString, aTitle) ;
  5938. strcat(lDialogString, "\" ") ;
  5939. }
  5940. if ( !xdialogPresent() && !gdialogPresent() )
  5941. {
  5942. strcat(lDialogString, "--backtitle \"") ;
  5943. strcat(lDialogString,
  5944. "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ;
  5945. strcat(lDialogString, "\" ") ;
  5946. }
  5947. strcat( lDialogString , "--fselect \"" ) ;
  5948. if ( aDefaultPathAndOrFile && strlen(aDefaultPathAndOrFile) )
  5949. {
  5950. if ( ! strchr(aDefaultPathAndOrFile, '/') )
  5951. {
  5952. strcat(lDialogString, "./") ;
  5953. }
  5954. strcat(lDialogString, aDefaultPathAndOrFile) ;
  5955. }
  5956. else if ( ! isTerminalRunning( ) && !lWasGraphicDialog )
  5957. {
  5958. strcat(lDialogString, getenv("HOME")) ;
  5959. strcat(lDialogString, "/") ;
  5960. }
  5961. else
  5962. {
  5963. strcat(lDialogString, "./") ;
  5964. }
  5965. if ( lWasGraphicDialog )
  5966. {
  5967. strcat(lDialogString, "\" 0 60 ) 2>&1 ") ;
  5968. }
  5969. else
  5970. {
  5971. strcat(lDialogString, "\" 0 60 >/dev/tty) ") ;
  5972. if ( lWasXterm )
  5973. {
  5974. strcat( lDialogString ,
  5975. "2>/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt");
  5976. }
  5977. else
  5978. {
  5979. strcat(lDialogString, "2>&1 ; clear >/dev/tty") ;
  5980. }
  5981. }
  5982. }
  5983. else
  5984. {
  5985. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){return tinyfd_inputBox(aTitle,NULL,NULL);}
  5986. strcpy(lBuff, "Save file in ");
  5987. strcat(lBuff, getCurDir());
  5988. lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */
  5989. if (lPointerInputBox) strcpy(lString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */
  5990. p = tinyfd_inputBox(aTitle, lBuff, "");
  5991. if (p) strcpy(lBuff, p); else lBuff[0] = '\0';
  5992. if (lPointerInputBox) strcpy(lPointerInputBox, lString); /* restore its previous content to tinyfd_inputBox */
  5993. p = lBuff;
  5994. getPathWithoutFinalSlash( lString , p ) ;
  5995. if ( strlen( lString ) && ! dirExists( lString ) )
  5996. {
  5997. return NULL ;
  5998. }
  5999. getLastName(lString,p);
  6000. if ( ! strlen(lString) )
  6001. {
  6002. return NULL;
  6003. }
  6004. return p ;
  6005. }
  6006. if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ;
  6007. if ( ! ( lIn = popen( lDialogString , "r" ) ) )
  6008. {
  6009. return NULL ;
  6010. }
  6011. while ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL )
  6012. {}
  6013. pclose( lIn ) ;
  6014. if ( strlen(lBuff) && lBuff[strlen( lBuff ) -1] == '\n' )
  6015. {
  6016. lBuff[strlen( lBuff ) -1] = '\0' ;
  6017. }
  6018. /* printf( "lBuff: %s\n" , lBuff ) ; */
  6019. if ( ! strlen(lBuff) )
  6020. {
  6021. return NULL;
  6022. }
  6023. getPathWithoutFinalSlash( lString , lBuff ) ;
  6024. if ( strlen( lString ) && ! dirExists( lString ) )
  6025. {
  6026. return NULL ;
  6027. }
  6028. strcpy(lLastDirectory, lString) ;
  6029. getLastName(lString,lBuff);
  6030. if ( ! filenameValid(lString) )
  6031. {
  6032. return NULL;
  6033. }
  6034. return lBuff ;
  6035. }
  6036. /* in case of multiple files, the separator is | */
  6037. char * tinyfd_openFileDialog(
  6038. char const * aTitle , /* NULL or "" */
  6039. char const * aDefaultPathAndOrFile , /* NULL or "" , ends with / to set only a directory */
  6040. int aNumOfFilterPatterns , /* 0 */
  6041. char const * const * aFilterPatterns , /* NULL or {"*.jpg","*.png"} */
  6042. char const * aSingleFilterDescription , /* NULL or "image files" */
  6043. int aAllowMultipleSelects ) /* 0 or 1 */
  6044. {
  6045. static char * lBuff = NULL;
  6046. static char lLastDirectory[MAX_PATH_OR_CMD] = "$PWD" ;
  6047. char lDialogString[MAX_PATH_OR_CMD] ;
  6048. char lString[MAX_PATH_OR_CMD] ;
  6049. int i ;
  6050. FILE * lIn ;
  6051. char * p ;
  6052. char * lPointerInputBox ;
  6053. size_t lFullBuffLen ;
  6054. int lWasKdialog = 0 ;
  6055. int lWasGraphicDialog = 0 ;
  6056. int lWasXterm = 0 ;
  6057. if ( ! aFilterPatterns ) aNumOfFilterPatterns = 0 ;
  6058. if (tfd_quoteDetected(aTitle)) return tinyfd_openFileDialog("INVALID TITLE WITH QUOTES", aDefaultPathAndOrFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects);
  6059. if (tfd_quoteDetected(aDefaultPathAndOrFile)) return tinyfd_openFileDialog(aTitle, "INVALID DEFAULT_PATH WITH QUOTES", aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects);
  6060. if (tfd_quoteDetected(aSingleFilterDescription)) return tinyfd_openFileDialog(aTitle, aDefaultPathAndOrFile, aNumOfFilterPatterns, aFilterPatterns, "INVALID FILTER_DESCRIPTION WITH QUOTES", aAllowMultipleSelects);
  6061. for (i = 0; i < aNumOfFilterPatterns; i++)
  6062. {
  6063. if (tfd_quoteDetected(aFilterPatterns[i])) return tinyfd_openFileDialog("INVALID FILTER_PATTERN WITH QUOTES: use the GRAVE ACCENT \\x60 instead.", aDefaultPathAndOrFile, 0, NULL, NULL, aAllowMultipleSelects);
  6064. }
  6065. free(lBuff);
  6066. if (aTitle&&!strcmp(aTitle,"tinyfd_query"))
  6067. {
  6068. lBuff = NULL;
  6069. }
  6070. else
  6071. {
  6072. if (aAllowMultipleSelects)
  6073. {
  6074. lFullBuffLen = MAX_MULTIPLE_FILES * MAX_PATH_OR_CMD + 1;
  6075. lBuff = (char *) malloc(lFullBuffLen * sizeof(char));
  6076. if (!lBuff)
  6077. {
  6078. lFullBuffLen = LOW_MULTIPLE_FILES * MAX_PATH_OR_CMD + 1;
  6079. lBuff = (char *) malloc( lFullBuffLen * sizeof(char));
  6080. }
  6081. }
  6082. else
  6083. {
  6084. lFullBuffLen = MAX_PATH_OR_CMD + 1;
  6085. lBuff = (char *) malloc(lFullBuffLen * sizeof(char));
  6086. }
  6087. if (!lBuff) return NULL;
  6088. lBuff[0]='\0';
  6089. }
  6090. if ( osascriptPresent( ) )
  6091. {
  6092. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char *)1;}
  6093. strcpy( lDialogString , "osascript ");
  6094. if ( ! osx9orBetter() ) strcat( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'");
  6095. strcat( lDialogString , " -e 'try' -e '" );
  6096. if ( ! aAllowMultipleSelects )
  6097. {
  6098. strcat( lDialogString , "POSIX path of ( " );
  6099. }
  6100. else
  6101. {
  6102. strcat( lDialogString , "set mylist to " );
  6103. }
  6104. strcat( lDialogString , "choose file " );
  6105. if ( aTitle && strlen(aTitle) )
  6106. {
  6107. strcat(lDialogString, "with prompt \"") ;
  6108. strcat(lDialogString, aTitle) ;
  6109. strcat(lDialogString, "\" ") ;
  6110. }
  6111. getPathWithoutFinalSlash( lString , aDefaultPathAndOrFile ) ;
  6112. if ( strlen(lString) )
  6113. {
  6114. strcat(lDialogString, "default location \"") ;
  6115. strcat(lDialogString, lString ) ;
  6116. strcat(lDialogString , "\" " ) ;
  6117. }
  6118. if ( aNumOfFilterPatterns > 0 )
  6119. {
  6120. strcat(lDialogString , "of type {\"" );
  6121. strcat( lDialogString , aFilterPatterns[0] + 2 ) ;
  6122. strcat( lDialogString , "\"" ) ;
  6123. for ( i = 1 ; i < aNumOfFilterPatterns ; i ++ )
  6124. {
  6125. strcat( lDialogString , ",\"" ) ;
  6126. strcat( lDialogString , aFilterPatterns[i] + 2) ;
  6127. strcat( lDialogString , "\"" ) ;
  6128. }
  6129. strcat( lDialogString , "} " ) ;
  6130. }
  6131. if ( aAllowMultipleSelects )
  6132. {
  6133. strcat( lDialogString , "multiple selections allowed true ' " ) ;
  6134. strcat( lDialogString ,
  6135. "-e 'set mystring to POSIX path of item 1 of mylist' " );
  6136. strcat( lDialogString ,
  6137. "-e 'repeat with i from 2 to the count of mylist' " );
  6138. strcat( lDialogString , "-e 'set mystring to mystring & \"|\"' " );
  6139. strcat( lDialogString ,
  6140. "-e 'set mystring to mystring & POSIX path of item i of mylist' " );
  6141. strcat( lDialogString , "-e 'end repeat' " );
  6142. strcat( lDialogString , "-e 'mystring' " );
  6143. }
  6144. else
  6145. {
  6146. strcat( lDialogString , ")' " ) ;
  6147. }
  6148. strcat(lDialogString, "-e 'on error number -128' " ) ;
  6149. strcat(lDialogString, "-e 'end try'") ;
  6150. if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ;
  6151. }
  6152. else if ( tfd_kdialogPresent() )
  6153. {
  6154. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char *)1;}
  6155. lWasKdialog = 1 ;
  6156. strcpy( lDialogString , "kdialog" ) ;
  6157. if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() )
  6158. {
  6159. strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
  6160. }
  6161. strcat( lDialogString , " --getopenfilename " ) ;
  6162. if ( aDefaultPathAndOrFile && strlen(aDefaultPathAndOrFile) )
  6163. {
  6164. strcat(lDialogString, "\"") ;
  6165. if ( aDefaultPathAndOrFile[0] != '/' )
  6166. {
  6167. strcat(lDialogString, lLastDirectory) ;
  6168. strcat(lDialogString , "/" ) ;
  6169. }
  6170. strcat(lDialogString, aDefaultPathAndOrFile ) ;
  6171. strcat(lDialogString , "\"" ) ;
  6172. }
  6173. else
  6174. {
  6175. strcat(lDialogString, "\"") ;
  6176. strcat(lDialogString, lLastDirectory) ;
  6177. strcat(lDialogString , "/" ) ;
  6178. strcat(lDialogString, "\"") ;
  6179. }
  6180. if ( aNumOfFilterPatterns > 0 )
  6181. {
  6182. strcat(lDialogString , " \"" ) ;
  6183. strcat( lDialogString , aFilterPatterns[0] ) ;
  6184. for ( i = 1 ; i < aNumOfFilterPatterns ; i ++ )
  6185. {
  6186. strcat( lDialogString , " " ) ;
  6187. strcat( lDialogString , aFilterPatterns[i] ) ;
  6188. }
  6189. if ( aSingleFilterDescription && strlen(aSingleFilterDescription) )
  6190. {
  6191. strcat( lDialogString , " | " ) ;
  6192. strcat( lDialogString , aSingleFilterDescription ) ;
  6193. }
  6194. strcat( lDialogString , "\"" ) ;
  6195. }
  6196. if ( aAllowMultipleSelects )
  6197. {
  6198. strcat( lDialogString , " --multiple --separate-output" ) ;
  6199. }
  6200. if ( aTitle && strlen(aTitle) )
  6201. {
  6202. strcat(lDialogString, " --title \"") ;
  6203. strcat(lDialogString, aTitle) ;
  6204. strcat(lDialogString, "\"") ;
  6205. }
  6206. }
  6207. else if ( tfd_zenityPresent() || tfd_matedialogPresent() || tfd_shellementaryPresent() || tfd_qarmaPresent() )
  6208. {
  6209. if ( tfd_zenityPresent() )
  6210. {
  6211. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char *)1;}
  6212. strcpy( lDialogString , "zenity" ) ;
  6213. if ( (tfd_zenity3Present() >= 4) && !getenv("SSH_TTY") && tfd_xpropPresent() )
  6214. {
  6215. strcat( lDialogString, " --attach=$(sleep .01;xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
  6216. }
  6217. }
  6218. else if ( tfd_matedialogPresent() )
  6219. {
  6220. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char *)1;}
  6221. strcpy( lDialogString , "matedialog" ) ;
  6222. }
  6223. else if ( tfd_shellementaryPresent() )
  6224. {
  6225. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"shellementary");return (char *)1;}
  6226. strcpy( lDialogString , "shellementary" ) ;
  6227. }
  6228. else
  6229. {
  6230. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char *)1;}
  6231. strcpy( lDialogString , "qarma" ) ;
  6232. if ( !getenv("SSH_TTY") && tfd_xpropPresent() )
  6233. {
  6234. strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
  6235. }
  6236. }
  6237. strcat( lDialogString , " --file-selection" ) ;
  6238. if ( aAllowMultipleSelects )
  6239. {
  6240. strcat( lDialogString , " --multiple" ) ;
  6241. }
  6242. strcat(lDialogString, " --title=\"") ;
  6243. if (aTitle && strlen(aTitle)) strcat(lDialogString, aTitle) ;
  6244. strcat(lDialogString, "\"") ;
  6245. if ( aDefaultPathAndOrFile && strlen(aDefaultPathAndOrFile) )
  6246. {
  6247. strcat(lDialogString, " --filename=\"") ;
  6248. strcat(lDialogString, aDefaultPathAndOrFile) ;
  6249. strcat(lDialogString, "\"") ;
  6250. }
  6251. if ( aNumOfFilterPatterns > 0 )
  6252. {
  6253. strcat( lDialogString , " --file-filter='" ) ;
  6254. if ( aSingleFilterDescription && strlen(aSingleFilterDescription) )
  6255. {
  6256. strcat( lDialogString , aSingleFilterDescription ) ;
  6257. strcat( lDialogString , " |" ) ;
  6258. }
  6259. for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ )
  6260. {
  6261. strcat( lDialogString , " " ) ;
  6262. strcat( lDialogString , aFilterPatterns[i] ) ;
  6263. }
  6264. strcat( lDialogString , "' --file-filter='All files | *'" ) ;
  6265. }
  6266. if (tinyfd_silent) strcat( lDialogString , " 2>/dev/null ");
  6267. }
  6268. else if (tfd_yadPresent())
  6269. {
  6270. if (aTitle && !strcmp(aTitle, "tinyfd_query")) { strcpy(tinyfd_response, "yad"); return (char*)1; }
  6271. strcpy(lDialogString, "yad --file");
  6272. if (aAllowMultipleSelects)
  6273. {
  6274. strcat(lDialogString, " --multiple");
  6275. }
  6276. if (aTitle && strlen(aTitle))
  6277. {
  6278. strcat(lDialogString, " --title=\"");
  6279. strcat(lDialogString, aTitle);
  6280. strcat(lDialogString, "\"");
  6281. }
  6282. if (aDefaultPathAndOrFile && strlen(aDefaultPathAndOrFile))
  6283. {
  6284. strcat(lDialogString, " --filename=\"");
  6285. strcat(lDialogString, aDefaultPathAndOrFile);
  6286. strcat(lDialogString, "\"");
  6287. }
  6288. if (aNumOfFilterPatterns > 0)
  6289. {
  6290. strcat(lDialogString, " --file-filter='");
  6291. if (aSingleFilterDescription && strlen(aSingleFilterDescription))
  6292. {
  6293. strcat(lDialogString, aSingleFilterDescription);
  6294. strcat(lDialogString, " |");
  6295. }
  6296. for (i = 0; i < aNumOfFilterPatterns; i++)
  6297. {
  6298. strcat(lDialogString, " ");
  6299. strcat(lDialogString, aFilterPatterns[i]);
  6300. }
  6301. strcat(lDialogString, "' --file-filter='All files | *'");
  6302. }
  6303. if (tinyfd_silent) strcat(lDialogString, " 2>/dev/null ");
  6304. }
  6305. else if ( tkinter3Present( ) )
  6306. {
  6307. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return (char *)1;}
  6308. strcpy( lDialogString , gPython3Name ) ;
  6309. strcat( lDialogString ,
  6310. " -S -c \"import tkinter;from tkinter import filedialog;root=tkinter.Tk();root.withdraw();");
  6311. strcat( lDialogString , "lFiles=filedialog.askopenfilename(");
  6312. if ( aAllowMultipleSelects )
  6313. {
  6314. strcat( lDialogString , "multiple=1," ) ;
  6315. }
  6316. if ( aTitle && strlen(aTitle) )
  6317. {
  6318. strcat(lDialogString, "title='") ;
  6319. strcat(lDialogString, aTitle) ;
  6320. strcat(lDialogString, "',") ;
  6321. }
  6322. if ( aDefaultPathAndOrFile && strlen(aDefaultPathAndOrFile) )
  6323. {
  6324. getPathWithoutFinalSlash( lString , aDefaultPathAndOrFile ) ;
  6325. if ( strlen(lString) )
  6326. {
  6327. strcat(lDialogString, "initialdir='") ;
  6328. strcat(lDialogString, lString ) ;
  6329. strcat(lDialogString , "'," ) ;
  6330. }
  6331. getLastName( lString , aDefaultPathAndOrFile ) ;
  6332. if ( strlen(lString) )
  6333. {
  6334. strcat(lDialogString, "initialfile='") ;
  6335. strcat(lDialogString, lString ) ;
  6336. strcat(lDialogString , "'," ) ;
  6337. }
  6338. }
  6339. if ( ( aNumOfFilterPatterns > 1 )
  6340. || ( ( aNumOfFilterPatterns == 1 ) /*test because poor osx behaviour*/
  6341. && ( aFilterPatterns[0][strlen(aFilterPatterns[0])-1] != '*' ) ) )
  6342. {
  6343. strcat(lDialogString , "filetypes=(" ) ;
  6344. strcat( lDialogString , "('" ) ;
  6345. if ( aSingleFilterDescription && strlen(aSingleFilterDescription) )
  6346. {
  6347. strcat( lDialogString , aSingleFilterDescription ) ;
  6348. }
  6349. strcat( lDialogString , "',(" ) ;
  6350. for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ )
  6351. {
  6352. strcat( lDialogString , "'" ) ;
  6353. strcat( lDialogString , aFilterPatterns[i] ) ;
  6354. strcat( lDialogString , "'," ) ;
  6355. }
  6356. strcat( lDialogString , "))," ) ;
  6357. strcat( lDialogString , "('All files','*'))" ) ;
  6358. }
  6359. strcat( lDialogString , ");\
  6360. \nif not isinstance(lFiles, tuple):\n\tprint(lFiles)\nelse:\
  6361. \n\tlFilesString=''\n\tfor lFile in lFiles:\n\t\tlFilesString+=str(lFile)+'|'\
  6362. \n\tprint(lFilesString[:-1])\n\"" ) ;
  6363. }
  6364. else if ( tkinter2Present( ) )
  6365. {
  6366. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python2-tkinter");return (char *)1;}
  6367. strcpy( lDialogString , "export PYTHONIOENCODING=utf-8;" ) ;
  6368. strcat( lDialogString , gPython2Name ) ;
  6369. if ( ! isTerminalRunning( ) && tfd_isDarwin( ) )
  6370. {
  6371. strcat( lDialogString , " -i" ) ; /* for osx without console */
  6372. }
  6373. strcat( lDialogString ,
  6374. " -S -c \"import Tkinter,tkFileDialog;root=Tkinter.Tk();root.withdraw();");
  6375. if ( tfd_isDarwin( ) )
  6376. {
  6377. strcat( lDialogString ,
  6378. "import os;os.system('''/usr/bin/osascript -e 'tell app \\\"Finder\\\" to set \
  6379. frontmost of process \\\"Python\\\" to true' ''');");
  6380. }
  6381. strcat( lDialogString , "lFiles=tkFileDialog.askopenfilename(");
  6382. if ( aAllowMultipleSelects )
  6383. {
  6384. strcat( lDialogString , "multiple=1," ) ;
  6385. }
  6386. if ( aTitle && strlen(aTitle) )
  6387. {
  6388. strcat(lDialogString, "title='") ;
  6389. strcat(lDialogString, aTitle) ;
  6390. strcat(lDialogString, "',") ;
  6391. }
  6392. if ( aDefaultPathAndOrFile && strlen(aDefaultPathAndOrFile) )
  6393. {
  6394. getPathWithoutFinalSlash( lString , aDefaultPathAndOrFile ) ;
  6395. if ( strlen(lString) )
  6396. {
  6397. strcat(lDialogString, "initialdir='") ;
  6398. strcat(lDialogString, lString ) ;
  6399. strcat(lDialogString , "'," ) ;
  6400. }
  6401. getLastName( lString , aDefaultPathAndOrFile ) ;
  6402. if ( strlen(lString) )
  6403. {
  6404. strcat(lDialogString, "initialfile='") ;
  6405. strcat(lDialogString, lString ) ;
  6406. strcat(lDialogString , "'," ) ;
  6407. }
  6408. }
  6409. if ( ( aNumOfFilterPatterns > 1 )
  6410. || ( ( aNumOfFilterPatterns == 1 ) /*test because poor osx behaviour*/
  6411. && ( aFilterPatterns[0][strlen(aFilterPatterns[0])-1] != '*' ) ) )
  6412. {
  6413. strcat(lDialogString , "filetypes=(" ) ;
  6414. strcat( lDialogString , "('" ) ;
  6415. if ( aSingleFilterDescription && strlen(aSingleFilterDescription) )
  6416. {
  6417. strcat( lDialogString , aSingleFilterDescription ) ;
  6418. }
  6419. strcat( lDialogString , "',(" ) ;
  6420. for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ )
  6421. {
  6422. strcat( lDialogString , "'" ) ;
  6423. strcat( lDialogString , aFilterPatterns[i] ) ;
  6424. strcat( lDialogString , "'," ) ;
  6425. }
  6426. strcat( lDialogString , "))," ) ;
  6427. strcat( lDialogString , "('All files','*'))" ) ;
  6428. }
  6429. strcat( lDialogString , ");\
  6430. \nif not isinstance(lFiles, tuple):\n\tprint lFiles\nelse:\
  6431. \n\tlFilesString=''\n\tfor lFile in lFiles:\n\t\tlFilesString+=str(lFile)+'|'\
  6432. \n\tprint lFilesString[:-1]\n\"" ) ;
  6433. }
  6434. else if ( xdialogPresent() || dialogName() )
  6435. {
  6436. if ( xdialogPresent( ) )
  6437. {
  6438. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char *)1;}
  6439. lWasGraphicDialog = 1 ;
  6440. strcpy( lDialogString , "(Xdialog " ) ;
  6441. }
  6442. else if ( isTerminalRunning( ) )
  6443. {
  6444. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char *)0;}
  6445. strcpy( lDialogString , "(dialog " ) ;
  6446. }
  6447. else
  6448. {
  6449. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char *)0;}
  6450. lWasXterm = 1 ;
  6451. strcpy( lDialogString , terminalName() ) ;
  6452. strcat( lDialogString , "'(" ) ;
  6453. strcat( lDialogString , dialogName() ) ;
  6454. strcat( lDialogString , " " ) ;
  6455. }
  6456. if ( aTitle && strlen(aTitle) )
  6457. {
  6458. strcat(lDialogString, "--title \"") ;
  6459. strcat(lDialogString, aTitle) ;
  6460. strcat(lDialogString, "\" ") ;
  6461. }
  6462. if ( !xdialogPresent() && !gdialogPresent() )
  6463. {
  6464. strcat(lDialogString, "--backtitle \"") ;
  6465. strcat(lDialogString,
  6466. "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ;
  6467. strcat(lDialogString, "\" ") ;
  6468. }
  6469. strcat( lDialogString , "--fselect \"" ) ;
  6470. if ( aDefaultPathAndOrFile && strlen(aDefaultPathAndOrFile) )
  6471. {
  6472. if ( ! strchr(aDefaultPathAndOrFile, '/') )
  6473. {
  6474. strcat(lDialogString, "./") ;
  6475. }
  6476. strcat(lDialogString, aDefaultPathAndOrFile) ;
  6477. }
  6478. else if ( ! isTerminalRunning( ) && !lWasGraphicDialog )
  6479. {
  6480. strcat(lDialogString, getenv("HOME")) ;
  6481. strcat(lDialogString, "/");
  6482. }
  6483. else
  6484. {
  6485. strcat(lDialogString, "./") ;
  6486. }
  6487. if ( lWasGraphicDialog )
  6488. {
  6489. strcat(lDialogString, "\" 0 60 ) 2>&1 ") ;
  6490. }
  6491. else
  6492. {
  6493. strcat(lDialogString, "\" 0 60 >/dev/tty) ") ;
  6494. if ( lWasXterm )
  6495. {
  6496. strcat( lDialogString ,
  6497. "2>/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt");
  6498. }
  6499. else
  6500. {
  6501. strcat(lDialogString, "2>&1 ; clear >/dev/tty") ;
  6502. }
  6503. }
  6504. }
  6505. else
  6506. {
  6507. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){return tinyfd_inputBox(aTitle,NULL,NULL);}
  6508. strcpy(lBuff, "Open file from ");
  6509. strcat(lBuff, getCurDir());
  6510. lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */
  6511. if (lPointerInputBox) strcpy(lDialogString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */
  6512. p = tinyfd_inputBox(aTitle, lBuff, "");
  6513. if ( p ) strcpy(lBuff, p); else lBuff[0] = '\0';
  6514. if (lPointerInputBox) strcpy(lPointerInputBox, lDialogString); /* restore its previous content to tinyfd_inputBox */
  6515. if ( ! fileExists(lBuff) )
  6516. {
  6517. free(lBuff);
  6518. lBuff = NULL;
  6519. }
  6520. else
  6521. {
  6522. lBuff = (char *)( realloc( lBuff, (strlen(lBuff)+1) * sizeof(char)));
  6523. }
  6524. return lBuff ;
  6525. }
  6526. if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ;
  6527. if ( ! ( lIn = popen( lDialogString , "r" ) ) )
  6528. {
  6529. free(lBuff);
  6530. lBuff = NULL;
  6531. return NULL ;
  6532. }
  6533. lBuff[0]='\0';
  6534. p = lBuff;
  6535. while ( fgets( p , sizeof( lBuff ) , lIn ) != NULL )
  6536. {
  6537. p += strlen( p );
  6538. }
  6539. pclose( lIn ) ;
  6540. if ( strlen( lBuff ) && lBuff[strlen( lBuff ) -1] == '\n' )
  6541. {
  6542. lBuff[strlen( lBuff ) -1] = '\0' ;
  6543. }
  6544. /* printf( "strlen lBuff: %d\n" , strlen( lBuff ) ) ; */
  6545. if ( lWasKdialog && aAllowMultipleSelects )
  6546. {
  6547. p = lBuff ;
  6548. while ( ( p = strchr( p , '\n' ) ) )
  6549. * p = '|' ;
  6550. }
  6551. /* printf( "lBuff2: %s\n" , lBuff ) ; */
  6552. if ( ! strlen( lBuff ) )
  6553. {
  6554. free(lBuff);
  6555. lBuff = NULL;
  6556. return NULL;
  6557. }
  6558. if ( aAllowMultipleSelects && strchr(lBuff, '|') )
  6559. {
  6560. if( ! ensureFilesExist( lBuff , lBuff ) )
  6561. {
  6562. free(lBuff);
  6563. lBuff = NULL;
  6564. return NULL;
  6565. }
  6566. }
  6567. else if ( !fileExists(lBuff) )
  6568. {
  6569. free(lBuff);
  6570. lBuff = NULL;
  6571. return NULL;
  6572. }
  6573. p = strrchr(lBuff, '|');
  6574. if ( !p ) p = lBuff ;
  6575. else p ++ ;
  6576. getPathWithoutFinalSlash( lString , p ) ;
  6577. /* printf( "lString [%lu]: %s\n" , strlen(lString) , lString ) ; */
  6578. if ( strlen( lString ) && ! dirExists( lString ) )
  6579. {
  6580. return NULL ;
  6581. }
  6582. strcpy(lLastDirectory, lString) ;
  6583. lBuff = (char *)( realloc( lBuff, (strlen(lBuff)+1) * sizeof(char)));
  6584. /*printf( "lBuff3 [%lu]: %s\n" , strlen(lBuff) , lBuff ) ; */
  6585. return lBuff ;
  6586. }
  6587. char * tinyfd_selectFolderDialog(
  6588. char const * aTitle , /* "" */
  6589. char const * aDefaultPath ) /* "" */
  6590. {
  6591. static char lBuff[MAX_PATH_OR_CMD] ;
  6592. static char lLastDirectory[MAX_PATH_OR_CMD] = "$PWD" ;
  6593. char lDialogString[MAX_PATH_OR_CMD] ;
  6594. FILE * lIn ;
  6595. char * p ;
  6596. char * lPointerInputBox ;
  6597. int lWasGraphicDialog = 0 ;
  6598. int lWasXterm = 0 ;
  6599. lBuff[0]='\0';
  6600. if (tfd_quoteDetected(aTitle)) return tinyfd_selectFolderDialog("INVALID TITLE WITH QUOTES", aDefaultPath);
  6601. if (tfd_quoteDetected(aDefaultPath)) return tinyfd_selectFolderDialog(aTitle, "INVALID DEFAULT_PATH WITH QUOTES");
  6602. if ( osascriptPresent( ))
  6603. {
  6604. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char *)1;}
  6605. strcpy( lDialogString , "osascript ");
  6606. if ( ! osx9orBetter() ) strcat( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'");
  6607. strcat( lDialogString , " -e 'try' -e 'POSIX path of ( choose folder ");
  6608. if ( aTitle && strlen(aTitle) )
  6609. {
  6610. strcat(lDialogString, "with prompt \"") ;
  6611. strcat(lDialogString, aTitle) ;
  6612. strcat(lDialogString, "\" ") ;
  6613. }
  6614. if ( aDefaultPath && strlen(aDefaultPath) )
  6615. {
  6616. strcat(lDialogString, "default location \"") ;
  6617. strcat(lDialogString, aDefaultPath ) ;
  6618. strcat(lDialogString , "\" " ) ;
  6619. }
  6620. strcat( lDialogString , ")' " ) ;
  6621. strcat(lDialogString, "-e 'on error number -128' " ) ;
  6622. strcat(lDialogString, "-e 'end try'") ;
  6623. if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ;
  6624. }
  6625. else if ( tfd_kdialogPresent() )
  6626. {
  6627. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char *)1;}
  6628. strcpy( lDialogString , "kdialog" ) ;
  6629. if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() )
  6630. {
  6631. strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
  6632. }
  6633. strcat( lDialogString , " --getexistingdirectory " ) ;
  6634. if ( aDefaultPath && strlen(aDefaultPath) )
  6635. {
  6636. strcat(lDialogString, "\"") ;
  6637. if ( aDefaultPath[0] != '/' )
  6638. {
  6639. strcat(lDialogString, lLastDirectory) ;
  6640. strcat(lDialogString , "/" ) ;
  6641. }
  6642. strcat(lDialogString, aDefaultPath ) ;
  6643. strcat(lDialogString , "\"" ) ;
  6644. }
  6645. else
  6646. {
  6647. strcat(lDialogString, "\"") ;
  6648. strcat(lDialogString, lLastDirectory) ;
  6649. strcat(lDialogString , "/" ) ;
  6650. strcat(lDialogString, "\"") ;
  6651. }
  6652. if ( aTitle && strlen(aTitle) )
  6653. {
  6654. strcat(lDialogString, " --title \"") ;
  6655. strcat(lDialogString, aTitle) ;
  6656. strcat(lDialogString, "\"") ;
  6657. }
  6658. }
  6659. else if ( tfd_zenityPresent() || tfd_matedialogPresent() || tfd_shellementaryPresent() || tfd_qarmaPresent() )
  6660. {
  6661. if ( tfd_zenityPresent() )
  6662. {
  6663. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char *)1;}
  6664. strcpy( lDialogString , "zenity" ) ;
  6665. if ( (tfd_zenity3Present() >= 4) && !getenv("SSH_TTY") && tfd_xpropPresent() )
  6666. {
  6667. strcat( lDialogString, " --attach=$(sleep .01;xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
  6668. }
  6669. }
  6670. else if ( tfd_matedialogPresent() )
  6671. {
  6672. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char *)1;}
  6673. strcpy( lDialogString , "matedialog" ) ;
  6674. }
  6675. else if ( tfd_shellementaryPresent() )
  6676. {
  6677. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"shellementary");return (char *)1;}
  6678. strcpy( lDialogString , "shellementary" ) ;
  6679. }
  6680. else
  6681. {
  6682. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char *)1;}
  6683. strcpy( lDialogString , "qarma" ) ;
  6684. if ( !getenv("SSH_TTY") && tfd_xpropPresent() )
  6685. {
  6686. strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
  6687. }
  6688. }
  6689. strcat( lDialogString , " --file-selection --directory" ) ;
  6690. strcat(lDialogString, " --title=\"") ;
  6691. if (aTitle && strlen(aTitle)) strcat(lDialogString, aTitle) ;
  6692. strcat(lDialogString, "\"") ;
  6693. if ( aDefaultPath && strlen(aDefaultPath) )
  6694. {
  6695. strcat(lDialogString, " --filename=\"") ;
  6696. strcat(lDialogString, aDefaultPath) ;
  6697. strcat(lDialogString, "\"") ;
  6698. }
  6699. if (tinyfd_silent) strcat( lDialogString , " 2>/dev/null ");
  6700. }
  6701. else if (tfd_yadPresent())
  6702. {
  6703. if (aTitle && !strcmp(aTitle, "tinyfd_query")) { strcpy(tinyfd_response, "yad"); return (char*)1; }
  6704. strcpy(lDialogString, "yad --file --directory");
  6705. if (aTitle && strlen(aTitle))
  6706. {
  6707. strcat(lDialogString, " --title=\"");
  6708. strcat(lDialogString, aTitle);
  6709. strcat(lDialogString, "\"");
  6710. }
  6711. if (aDefaultPath && strlen(aDefaultPath))
  6712. {
  6713. strcat(lDialogString, " --filename=\"");
  6714. strcat(lDialogString, aDefaultPath);
  6715. strcat(lDialogString, "\"");
  6716. }
  6717. if (tinyfd_silent) strcat(lDialogString, " 2>/dev/null ");
  6718. }
  6719. else if ( !xdialogPresent() && tkinter3Present( ) )
  6720. {
  6721. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return (char *)1;}
  6722. strcpy( lDialogString , gPython3Name ) ;
  6723. strcat( lDialogString ,
  6724. " -S -c \"import tkinter;from tkinter import filedialog;root=tkinter.Tk();root.withdraw();");
  6725. strcat( lDialogString , "res=filedialog.askdirectory(");
  6726. if ( aTitle && strlen(aTitle) )
  6727. {
  6728. strcat(lDialogString, "title='") ;
  6729. strcat(lDialogString, aTitle) ;
  6730. strcat(lDialogString, "',") ;
  6731. }
  6732. if ( aDefaultPath && strlen(aDefaultPath) )
  6733. {
  6734. strcat(lDialogString, "initialdir='") ;
  6735. strcat(lDialogString, aDefaultPath ) ;
  6736. strcat(lDialogString , "'" ) ;
  6737. }
  6738. strcat( lDialogString, ");\nif not isinstance(res, tuple):\n\tprint(res)\n\"" ) ;
  6739. }
  6740. else if ( !xdialogPresent() && tkinter2Present( ) )
  6741. {
  6742. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python2-tkinter");return (char *)1;}
  6743. strcpy( lDialogString , "export PYTHONIOENCODING=utf-8;" ) ;
  6744. strcat( lDialogString , gPython2Name ) ;
  6745. if ( ! isTerminalRunning( ) && tfd_isDarwin( ) )
  6746. {
  6747. strcat( lDialogString , " -i" ) ; /* for osx without console */
  6748. }
  6749. strcat( lDialogString ,
  6750. " -S -c \"import Tkinter,tkFileDialog;root=Tkinter.Tk();root.withdraw();");
  6751. if ( tfd_isDarwin( ) )
  6752. {
  6753. strcat( lDialogString ,
  6754. "import os;os.system('''/usr/bin/osascript -e 'tell app \\\"Finder\\\" to set \
  6755. frontmost of process \\\"Python\\\" to true' ''');");
  6756. }
  6757. strcat( lDialogString , "print tkFileDialog.askdirectory(");
  6758. if ( aTitle && strlen(aTitle) )
  6759. {
  6760. strcat(lDialogString, "title='") ;
  6761. strcat(lDialogString, aTitle) ;
  6762. strcat(lDialogString, "',") ;
  6763. }
  6764. if ( aDefaultPath && strlen(aDefaultPath) )
  6765. {
  6766. strcat(lDialogString, "initialdir='") ;
  6767. strcat(lDialogString, aDefaultPath ) ;
  6768. strcat(lDialogString , "'" ) ;
  6769. }
  6770. strcat( lDialogString , ")\"" ) ;
  6771. }
  6772. else if ( xdialogPresent() || dialogName() )
  6773. {
  6774. if ( xdialogPresent( ) )
  6775. {
  6776. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char *)1;}
  6777. lWasGraphicDialog = 1 ;
  6778. strcpy( lDialogString , "(Xdialog " ) ;
  6779. }
  6780. else if ( isTerminalRunning( ) )
  6781. {
  6782. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char *)0;}
  6783. strcpy( lDialogString , "(dialog " ) ;
  6784. }
  6785. else
  6786. {
  6787. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char *)0;}
  6788. lWasXterm = 1 ;
  6789. strcpy( lDialogString , terminalName() ) ;
  6790. strcat( lDialogString , "'(" ) ;
  6791. strcat( lDialogString , dialogName() ) ;
  6792. strcat( lDialogString , " " ) ;
  6793. }
  6794. if ( aTitle && strlen(aTitle) )
  6795. {
  6796. strcat(lDialogString, "--title \"") ;
  6797. strcat(lDialogString, aTitle) ;
  6798. strcat(lDialogString, "\" ") ;
  6799. }
  6800. if ( !xdialogPresent() && !gdialogPresent() )
  6801. {
  6802. strcat(lDialogString, "--backtitle \"") ;
  6803. strcat(lDialogString,
  6804. "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ;
  6805. strcat(lDialogString, "\" ") ;
  6806. }
  6807. strcat( lDialogString , "--dselect \"" ) ;
  6808. if ( aDefaultPath && strlen(aDefaultPath) )
  6809. {
  6810. strcat(lDialogString, aDefaultPath) ;
  6811. ensureFinalSlash(lDialogString);
  6812. }
  6813. else if ( ! isTerminalRunning( ) && !lWasGraphicDialog )
  6814. {
  6815. strcat(lDialogString, getenv("HOME")) ;
  6816. strcat(lDialogString, "/");
  6817. }
  6818. else
  6819. {
  6820. strcat(lDialogString, "./") ;
  6821. }
  6822. if ( lWasGraphicDialog )
  6823. {
  6824. strcat(lDialogString, "\" 0 60 ) 2>&1 ") ;
  6825. }
  6826. else
  6827. {
  6828. strcat(lDialogString, "\" 0 60 >/dev/tty) ") ;
  6829. if ( lWasXterm )
  6830. {
  6831. strcat( lDialogString ,
  6832. "2>/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt");
  6833. }
  6834. else
  6835. {
  6836. strcat(lDialogString, "2>&1 ; clear >/dev/tty") ;
  6837. }
  6838. }
  6839. }
  6840. else
  6841. {
  6842. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){return tinyfd_inputBox(aTitle,NULL,NULL);}
  6843. strcpy(lBuff, "Select folder from ");
  6844. strcat(lBuff, getCurDir());
  6845. lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */
  6846. if (lPointerInputBox) strcpy(lDialogString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */
  6847. p = tinyfd_inputBox(aTitle, lBuff, "");
  6848. if (p) strcpy(lBuff, p); else lBuff[0] = '\0';
  6849. if (lPointerInputBox) strcpy(lPointerInputBox, lDialogString); /* restore its previous content to tinyfd_inputBox */
  6850. p = lBuff;
  6851. if ( !p || ! strlen( p ) || ! dirExists( p ) )
  6852. {
  6853. return NULL ;
  6854. }
  6855. return p ;
  6856. }
  6857. if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ;
  6858. if ( ! ( lIn = popen( lDialogString , "r" ) ) )
  6859. {
  6860. return NULL ;
  6861. }
  6862. while ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL )
  6863. {}
  6864. pclose( lIn ) ;
  6865. if ( strlen( lBuff ) && lBuff[strlen( lBuff ) -1] == '\n' )
  6866. {
  6867. lBuff[strlen( lBuff ) -1] = '\0' ;
  6868. }
  6869. /* printf( "lBuff: %s\n" , lBuff ) ; */
  6870. if ( ! strlen( lBuff ) || ! dirExists( lBuff ) )
  6871. {
  6872. return NULL ;
  6873. }
  6874. getPathWithoutFinalSlash( lLastDirectory , lBuff ) ;
  6875. return lBuff ;
  6876. }
  6877. /* aDefaultRGB is used only if aDefaultHexRGB is absent */
  6878. /* aDefaultRGB and aoResultRGB can be the same array */
  6879. /* returns NULL on cancel */
  6880. /* returns the hexcolor as a string "#FF0000" */
  6881. /* aoResultRGB also contains the result */
  6882. char * tinyfd_colorChooser(
  6883. char const * aTitle , /* NULL or "" */
  6884. char const * aDefaultHexRGB , /* NULL or "#FF0000"*/
  6885. unsigned char const aDefaultRGB[3] , /* { 0 , 255 , 255 } */
  6886. unsigned char aoResultRGB[3] ) /* { 0 , 0 , 0 } */
  6887. {
  6888. static char lDefaultHexRGB[16];
  6889. char lBuff[128] ;
  6890. char lTmp[128] ;
  6891. #if !((defined(__cplusplus ) && __cplusplus >= 201103L) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__clang__))
  6892. char * lTmp2 ;
  6893. #endif
  6894. char lDialogString[MAX_PATH_OR_CMD] ;
  6895. unsigned char lDefaultRGB[3];
  6896. char * p;
  6897. char * lPointerInputBox;
  6898. FILE * lIn ;
  6899. int i ;
  6900. int lWasZenity3 = 0 ;
  6901. int lWasOsascript = 0 ;
  6902. int lWasXdialog = 0 ;
  6903. lBuff[0]='\0';
  6904. if (tfd_quoteDetected(aTitle)) return tinyfd_colorChooser("INVALID TITLE WITH QUOTES", aDefaultHexRGB, aDefaultRGB, aoResultRGB);
  6905. if (tfd_quoteDetected(aDefaultHexRGB)) return tinyfd_colorChooser(aTitle, "INVALID DEFAULT_HEX_RGB WITH QUOTES: use the GRAVE ACCENT \\x60 instead.", aDefaultRGB, aoResultRGB);
  6906. if (aDefaultHexRGB && (strlen(aDefaultHexRGB)==7) )
  6907. {
  6908. Hex2RGB(aDefaultHexRGB, lDefaultRGB);
  6909. strcpy(lDefaultHexRGB, aDefaultHexRGB);
  6910. }
  6911. else
  6912. {
  6913. lDefaultRGB[0] = aDefaultRGB[0];
  6914. lDefaultRGB[1] = aDefaultRGB[1];
  6915. lDefaultRGB[2] = aDefaultRGB[2];
  6916. RGB2Hex(aDefaultRGB, lDefaultHexRGB);
  6917. }
  6918. if ( osascriptPresent( ) )
  6919. {
  6920. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char *)1;}
  6921. lWasOsascript = 1 ;
  6922. strcpy( lDialogString , "osascript");
  6923. if ( ! osx9orBetter() )
  6924. {
  6925. strcat( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'");
  6926. strcat( lDialogString , " -e 'try' -e 'set mycolor to choose color default color {");
  6927. }
  6928. else
  6929. {
  6930. strcat( lDialogString ,
  6931. " -e 'try' -e 'tell app (path to frontmost application as Unicode text) \
  6932. to set mycolor to choose color default color {");
  6933. }
  6934. sprintf(lTmp, "%d", 256 * lDefaultRGB[0] ) ;
  6935. strcat(lDialogString, lTmp ) ;
  6936. strcat(lDialogString, "," ) ;
  6937. sprintf(lTmp, "%d", 256 * lDefaultRGB[1] ) ;
  6938. strcat(lDialogString, lTmp ) ;
  6939. strcat(lDialogString, "," ) ;
  6940. sprintf(lTmp, "%d", 256 * lDefaultRGB[2] ) ;
  6941. strcat(lDialogString, lTmp ) ;
  6942. strcat(lDialogString, "}' " ) ;
  6943. strcat( lDialogString ,
  6944. "-e 'set mystring to ((item 1 of mycolor) div 256 as integer) as string' " );
  6945. strcat( lDialogString ,
  6946. "-e 'repeat with i from 2 to the count of mycolor' " );
  6947. strcat( lDialogString ,
  6948. "-e 'set mystring to mystring & \" \" & ((item i of mycolor) div 256 as integer) as string' " );
  6949. strcat( lDialogString , "-e 'end repeat' " );
  6950. strcat( lDialogString , "-e 'mystring' ");
  6951. strcat(lDialogString, "-e 'on error number -128' " ) ;
  6952. strcat(lDialogString, "-e 'end try'") ;
  6953. if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ;
  6954. }
  6955. else if ( tfd_kdialogPresent() )
  6956. {
  6957. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char *)1;}
  6958. strcpy( lDialogString , "kdialog" ) ;
  6959. if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() )
  6960. {
  6961. strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
  6962. }
  6963. sprintf( lDialogString + strlen(lDialogString) , " --getcolor --default '%s'" , lDefaultHexRGB ) ;
  6964. if ( aTitle && strlen(aTitle) )
  6965. {
  6966. strcat(lDialogString, " --title \"") ;
  6967. strcat(lDialogString, aTitle) ;
  6968. strcat(lDialogString, "\"") ;
  6969. }
  6970. }
  6971. else if ( tfd_zenity3Present() || tfd_matedialogPresent() || tfd_shellementaryPresent() || tfd_qarmaPresent() )
  6972. {
  6973. lWasZenity3 = 1 ;
  6974. if ( tfd_zenity3Present() )
  6975. {
  6976. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity3");return (char *)1;}
  6977. strcpy( lDialogString , "zenity" );
  6978. if ( (tfd_zenity3Present() >= 4) && !getenv("SSH_TTY") && tfd_xpropPresent() )
  6979. {
  6980. strcat( lDialogString, " --attach=$(sleep .01;xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
  6981. }
  6982. }
  6983. else if ( tfd_matedialogPresent() )
  6984. {
  6985. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char *)1;}
  6986. strcpy( lDialogString , "matedialog" ) ;
  6987. }
  6988. else if ( tfd_shellementaryPresent() )
  6989. {
  6990. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"shellementary");return (char *)1;}
  6991. strcpy( lDialogString , "shellementary" ) ;
  6992. }
  6993. else
  6994. {
  6995. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char *)1;}
  6996. strcpy( lDialogString , "qarma" ) ;
  6997. if ( !getenv("SSH_TTY") && tfd_xpropPresent() )
  6998. {
  6999. strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
  7000. }
  7001. }
  7002. strcat( lDialogString , " --color-selection --show-palette" ) ;
  7003. sprintf( lDialogString + strlen(lDialogString), " --color=%s" , lDefaultHexRGB ) ;
  7004. strcat(lDialogString, " --title=\"") ;
  7005. if (aTitle && strlen(aTitle)) strcat(lDialogString, aTitle) ;
  7006. strcat(lDialogString, "\"") ;
  7007. if (tinyfd_silent) strcat( lDialogString , " 2>/dev/null ");
  7008. }
  7009. else if (tfd_yadPresent())
  7010. {
  7011. if (aTitle && !strcmp(aTitle, "tinyfd_query")) { strcpy(tinyfd_response, "yad"); return (char*)1; }
  7012. strcpy(lDialogString, "yad --color");
  7013. sprintf(lDialogString + strlen(lDialogString), " --init-color=%s", lDefaultHexRGB);
  7014. if (aTitle && strlen(aTitle))
  7015. {
  7016. strcat(lDialogString, " --title=\"");
  7017. strcat(lDialogString, aTitle);
  7018. strcat(lDialogString, "\"");
  7019. }
  7020. if (tinyfd_silent) strcat(lDialogString, " 2>/dev/null ");
  7021. }
  7022. else if ( xdialogPresent() )
  7023. {
  7024. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char *)1;}
  7025. lWasXdialog = 1 ;
  7026. strcpy( lDialogString , "Xdialog --colorsel \"" ) ;
  7027. if ( aTitle && strlen(aTitle) )
  7028. {
  7029. strcat(lDialogString, aTitle) ;
  7030. }
  7031. strcat(lDialogString, "\" 0 60 ") ;
  7032. #if (defined(__cplusplus ) && __cplusplus >= 201103L) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__clang__)
  7033. sprintf(lTmp,"%hhu %hhu %hhu",lDefaultRGB[0],lDefaultRGB[1],lDefaultRGB[2]);
  7034. #else
  7035. sprintf(lTmp,"%hu %hu %hu",lDefaultRGB[0],lDefaultRGB[1],lDefaultRGB[2]);
  7036. #endif
  7037. strcat(lDialogString, lTmp) ;
  7038. strcat(lDialogString, " 2>&1");
  7039. }
  7040. else if ( tkinter3Present( ) )
  7041. {
  7042. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return (char *)1;}
  7043. strcpy( lDialogString , gPython3Name ) ;
  7044. strcat( lDialogString ,
  7045. " -S -c \"import tkinter;from tkinter import colorchooser;root=tkinter.Tk();root.withdraw();");
  7046. strcat( lDialogString , "res=colorchooser.askcolor(color='" ) ;
  7047. strcat(lDialogString, lDefaultHexRGB ) ;
  7048. strcat(lDialogString, "'") ;
  7049. if ( aTitle && strlen(aTitle) )
  7050. {
  7051. strcat(lDialogString, ",title='") ;
  7052. strcat(lDialogString, aTitle) ;
  7053. strcat(lDialogString, "'") ;
  7054. }
  7055. strcat( lDialogString , ");\
  7056. \nif res[1] is not None:\n\tprint(res[1])\"" ) ;
  7057. }
  7058. else if ( tkinter2Present( ) )
  7059. {
  7060. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python2-tkinter");return (char *)1;}
  7061. strcpy( lDialogString , "export PYTHONIOENCODING=utf-8;" ) ;
  7062. strcat( lDialogString , gPython2Name ) ;
  7063. if ( ! isTerminalRunning( ) && tfd_isDarwin( ) )
  7064. {
  7065. strcat( lDialogString , " -i" ) ; /* for osx without console */
  7066. }
  7067. strcat( lDialogString ,
  7068. " -S -c \"import Tkinter,tkColorChooser;root=Tkinter.Tk();root.withdraw();");
  7069. if ( tfd_isDarwin( ) )
  7070. {
  7071. strcat( lDialogString ,
  7072. "import os;os.system('''osascript -e 'tell app \\\"Finder\\\" to set \
  7073. frontmost of process \\\"Python\\\" to true' ''');");
  7074. }
  7075. strcat( lDialogString , "res=tkColorChooser.askcolor(color='" ) ;
  7076. strcat(lDialogString, lDefaultHexRGB ) ;
  7077. strcat(lDialogString, "'") ;
  7078. if ( aTitle && strlen(aTitle) )
  7079. {
  7080. strcat(lDialogString, ",title='") ;
  7081. strcat(lDialogString, aTitle) ;
  7082. strcat(lDialogString, "'") ;
  7083. }
  7084. strcat( lDialogString , ");\
  7085. \nif res[1] is not None:\n\tprint res[1]\"" ) ;
  7086. }
  7087. else
  7088. {
  7089. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){return tinyfd_inputBox(aTitle,NULL,NULL);}
  7090. lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */
  7091. if (lPointerInputBox) strcpy(lDialogString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */
  7092. p = tinyfd_inputBox(aTitle, "Enter hex rgb color (i.e. #f5ca20)", lDefaultHexRGB);
  7093. if ( !p || (strlen(p) != 7) || (p[0] != '#') )
  7094. {
  7095. return NULL ;
  7096. }
  7097. for ( i = 1 ; i < 7 ; i ++ )
  7098. {
  7099. if ( ! isxdigit( (int) p[i] ) )
  7100. {
  7101. return NULL ;
  7102. }
  7103. }
  7104. Hex2RGB(p,aoResultRGB);
  7105. strcpy(lDefaultHexRGB, p);
  7106. if (lPointerInputBox) strcpy(lPointerInputBox, lDialogString); /* restore its previous content to tinyfd_inputBox */
  7107. return lDefaultHexRGB;
  7108. }
  7109. if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ;
  7110. if ( ! ( lIn = popen( lDialogString , "r" ) ) )
  7111. {
  7112. return NULL ;
  7113. }
  7114. while ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL )
  7115. {
  7116. }
  7117. pclose( lIn ) ;
  7118. if ( ! strlen( lBuff ) )
  7119. {
  7120. return NULL ;
  7121. }
  7122. /* printf( "len Buff: %lu\n" , strlen(lBuff) ) ; */
  7123. /* printf( "lBuff0: %s\n" , lBuff ) ; */
  7124. if ( lBuff[strlen( lBuff ) -1] == '\n' )
  7125. {
  7126. lBuff[strlen( lBuff ) -1] = '\0' ;
  7127. }
  7128. if ( lWasZenity3 )
  7129. {
  7130. if ( lBuff[0] == '#' )
  7131. {
  7132. if ( strlen(lBuff)>7 )
  7133. {
  7134. lBuff[3]=lBuff[5];
  7135. lBuff[4]=lBuff[6];
  7136. lBuff[5]=lBuff[9];
  7137. lBuff[6]=lBuff[10];
  7138. lBuff[7]='\0';
  7139. }
  7140. Hex2RGB(lBuff,aoResultRGB);
  7141. }
  7142. else if ( lBuff[3] == '(' ) {
  7143. #if (defined(__cplusplus ) && __cplusplus >= 201103L) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__clang__)
  7144. sscanf(lBuff,"rgb(%hhu,%hhu,%hhu", & aoResultRGB[0], & aoResultRGB[1],& aoResultRGB[2]);
  7145. #else
  7146. aoResultRGB[0] = (unsigned char) strtol(lBuff+4, & lTmp2, 10 );
  7147. aoResultRGB[1] = (unsigned char) strtol(lTmp2+1, & lTmp2, 10 );
  7148. aoResultRGB[2] = (unsigned char) strtol(lTmp2+1, NULL, 10 );
  7149. #endif
  7150. RGB2Hex(aoResultRGB,lBuff);
  7151. }
  7152. else if ( lBuff[4] == '(' ) {
  7153. #if (defined(__cplusplus ) && __cplusplus >= 201103L) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__clang__)
  7154. sscanf(lBuff,"rgba(%hhu,%hhu,%hhu", & aoResultRGB[0], & aoResultRGB[1],& aoResultRGB[2]);
  7155. #else
  7156. aoResultRGB[0] = (unsigned char) strtol(lBuff+5, & lTmp2, 10 );
  7157. aoResultRGB[1] = (unsigned char) strtol(lTmp2+1, & lTmp2, 10 );
  7158. aoResultRGB[2] = (unsigned char) strtol(lTmp2+1, NULL, 10 );
  7159. #endif
  7160. RGB2Hex(aoResultRGB,lBuff);
  7161. }
  7162. }
  7163. else if ( lWasOsascript || lWasXdialog )
  7164. {
  7165. /* printf( "lBuff: %s\n" , lBuff ) ; */
  7166. #if (defined(__cplusplus ) && __cplusplus >= 201103L) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__clang__)
  7167. sscanf(lBuff,"%hhu %hhu %hhu", & aoResultRGB[0], & aoResultRGB[1],& aoResultRGB[2]);
  7168. #else
  7169. aoResultRGB[0] = (unsigned char) strtol(lBuff, & lTmp2, 10 );
  7170. aoResultRGB[1] = (unsigned char) strtol(lTmp2+1, & lTmp2, 10 );
  7171. aoResultRGB[2] = (unsigned char) strtol(lTmp2+1, NULL, 10 );
  7172. #endif
  7173. RGB2Hex(aoResultRGB,lBuff);
  7174. }
  7175. else
  7176. {
  7177. Hex2RGB(lBuff,aoResultRGB);
  7178. }
  7179. /* printf("%d %d %d\n", aoResultRGB[0],aoResultRGB[1],aoResultRGB[2]); */
  7180. /* printf( "lBuff: %s\n" , lBuff ) ; */
  7181. strcpy(lDefaultHexRGB,lBuff);
  7182. return lDefaultHexRGB ;
  7183. }
  7184. #endif /* _WIN32 */
  7185. /* Modified prototypes for R */
  7186. void tfd_messageBox(
  7187. char const * aTitle ,
  7188. char const * aMessage ,
  7189. char const * aDialogType ,
  7190. char const * aIconType ,
  7191. int * aiDefaultButton )
  7192. {
  7193. * aiDefaultButton = tinyfd_messageBox( aTitle , aMessage , aDialogType , aIconType , * aiDefaultButton ) ;
  7194. }
  7195. void tfd_inputBox(
  7196. char const * aTitle ,
  7197. char const * aMessage ,
  7198. char * * aiDefaultInput )
  7199. {
  7200. char * lReturnedInput ;
  7201. if ( ! strcmp( * aiDefaultInput , "NULL") ) lReturnedInput = tinyfd_inputBox( aTitle , aMessage , NULL ) ;
  7202. else lReturnedInput = tinyfd_inputBox( aTitle , aMessage , * aiDefaultInput ) ;
  7203. if ( lReturnedInput ) strcpy ( * aiDefaultInput , lReturnedInput ) ;
  7204. else strcpy ( * aiDefaultInput , "NULL" ) ;
  7205. }
  7206. void tfd_saveFileDialog(
  7207. char const * aTitle ,
  7208. char * * aiDefaultPathAndFile ,
  7209. int const * aNumOfFilterPatterns ,
  7210. char const * const * aFilterPatterns ,
  7211. char const * aSingleFilterDescription )
  7212. {
  7213. char * lSavefile ;
  7214. /* printf( "aFilterPatterns %s\n" , aFilterPatterns [0]); */
  7215. lSavefile = tinyfd_saveFileDialog( aTitle , * aiDefaultPathAndFile , * aNumOfFilterPatterns ,
  7216. aFilterPatterns, aSingleFilterDescription ) ;
  7217. if ( lSavefile ) strcpy ( * aiDefaultPathAndFile , lSavefile ) ;
  7218. else strcpy ( * aiDefaultPathAndFile , "NULL" ) ;
  7219. }
  7220. void tfd_openFileDialog(
  7221. char const * aTitle ,
  7222. char * * aiDefaultPathAndFile ,
  7223. int const * aNumOfFilterPatterns ,
  7224. char const * const * aFilterPatterns ,
  7225. char const * aSingleFilterDescription ,
  7226. int const * aAllowMultipleSelects )
  7227. {
  7228. char * lOpenfile ;
  7229. /* printf( "aFilterPatterns %s\n" , aFilterPatterns [0]); */
  7230. lOpenfile = tinyfd_openFileDialog( aTitle , * aiDefaultPathAndFile , * aNumOfFilterPatterns ,
  7231. aFilterPatterns , aSingleFilterDescription , * aAllowMultipleSelects ) ;
  7232. if ( lOpenfile ) strcpy ( * aiDefaultPathAndFile , lOpenfile ) ;
  7233. else strcpy ( * aiDefaultPathAndFile , "NULL" ) ;
  7234. }
  7235. void tfd_selectFolderDialog(
  7236. char const * aTitle ,
  7237. char * * aiDefaultPath )
  7238. {
  7239. char * lSelectedfolder ;
  7240. lSelectedfolder = tinyfd_selectFolderDialog( aTitle, * aiDefaultPath ) ;
  7241. if ( lSelectedfolder ) strcpy ( * aiDefaultPath , lSelectedfolder ) ;
  7242. else strcpy ( * aiDefaultPath , "NULL" ) ;
  7243. }
  7244. void tfd_colorChooser(
  7245. char const * aTitle ,
  7246. char * * aiDefaultHexRGB )
  7247. {
  7248. unsigned char const aDefaultRGB [ 3 ] = {128,128,128} ;
  7249. unsigned char aoResultRGB [ 3 ] = {128,128,128} ;
  7250. char * lChosenColor ;
  7251. lChosenColor = tinyfd_colorChooser( aTitle, * aiDefaultHexRGB, aDefaultRGB, aoResultRGB ) ;
  7252. if ( lChosenColor ) strcpy ( * aiDefaultHexRGB , lChosenColor ) ;
  7253. else strcpy ( * aiDefaultHexRGB , "NULL" ) ;
  7254. }
  7255. /* end of Modified prototypes for R */
  7256. /*
  7257. int main( int argc , char * argv[] )
  7258. {
  7259. char const * lTmp;
  7260. char const * lTheSaveFileName;
  7261. char const * lTheOpenFileName;
  7262. char const * lTheSelectFolderName;
  7263. char const * lTheHexColor;
  7264. char const * lWillBeGraphicMode;
  7265. unsigned char lRgbColor[3];
  7266. FILE * lIn;
  7267. char lBuffer[1024];
  7268. char lString[1024];
  7269. char const * lFilterPatterns[2] = { "*.txt", "*.text" };
  7270. tinyfd_verbose = argc - 1;
  7271. tinyfd_silent = 1;
  7272. lWillBeGraphicMode = tinyfd_inputBox("tinyfd_query", NULL, NULL);
  7273. strcpy(lBuffer, "v");
  7274. strcat(lBuffer, tinyfd_version);
  7275. if (lWillBeGraphicMode)
  7276. {
  7277. strcat(lBuffer, "\ngraphic mode: ");
  7278. }
  7279. else
  7280. {
  7281. strcat(lBuffer, "\nconsole mode: ");
  7282. }
  7283. strcat(lBuffer, tinyfd_response);
  7284. strcat(lBuffer, "\n");
  7285. strcat(lBuffer, tinyfd_needs+78);
  7286. strcpy(lString, "tinyfiledialogs");
  7287. tinyfd_messageBox(lString, lBuffer, "ok", "info", 0);
  7288. tinyfd_notifyPopup("the title", "the message\n\tfrom outer-space", "info");
  7289. if (lWillBeGraphicMode && !tinyfd_forceConsole)
  7290. {
  7291. tinyfd_forceConsole = ! tinyfd_messageBox("Hello World",
  7292. "graphic dialogs [yes] / console mode [no]?",
  7293. "yesno", "question", 1);
  7294. }
  7295. lTmp = tinyfd_inputBox(
  7296. "a password box", "your password will be revealed", NULL);
  7297. if (!lTmp) return 1;
  7298. strcpy(lString, lTmp);
  7299. lTheSaveFileName = tinyfd_saveFileDialog(
  7300. "let us save this password",
  7301. "passwordFile.txt",
  7302. 2,
  7303. lFilterPatterns,
  7304. NULL);
  7305. if (!lTheSaveFileName)
  7306. {
  7307. tinyfd_messageBox(
  7308. "Error",
  7309. "Save file name is NULL",
  7310. "ok",
  7311. "error",
  7312. 1);
  7313. return 1;
  7314. }
  7315. lIn = fopen(lTheSaveFileName, "w");
  7316. if (!lIn)
  7317. {
  7318. tinyfd_messageBox(
  7319. "Error",
  7320. "Can not open this file in write mode",
  7321. "ok",
  7322. "error",
  7323. 1);
  7324. return 1;
  7325. }
  7326. fputs(lString, lIn);
  7327. fclose(lIn);
  7328. lTheOpenFileName = tinyfd_openFileDialog(
  7329. "let us read the password back",
  7330. "",
  7331. 2,
  7332. lFilterPatterns,
  7333. NULL,
  7334. 0);
  7335. if (!lTheOpenFileName)
  7336. {
  7337. tinyfd_messageBox(
  7338. "Error",
  7339. "Open file name is NULL",
  7340. "ok",
  7341. "error",
  7342. 1);
  7343. return 1;
  7344. }
  7345. lIn = fopen(lTheOpenFileName, "r");
  7346. if (!lIn)
  7347. {
  7348. tinyfd_messageBox(
  7349. "Error",
  7350. "Can not open this file in read mode",
  7351. "ok",
  7352. "error",
  7353. 1);
  7354. return(1);
  7355. }
  7356. lBuffer[0] = '\0';
  7357. fgets(lBuffer, sizeof(lBuffer), lIn);
  7358. fclose(lIn);
  7359. tinyfd_messageBox("your password is",
  7360. lBuffer, "ok", "info", 1);
  7361. lTheSelectFolderName = tinyfd_selectFolderDialog(
  7362. "let us just select a directory", NULL);
  7363. if (!lTheSelectFolderName)
  7364. {
  7365. tinyfd_messageBox(
  7366. "Error",
  7367. "Select folder name is NULL",
  7368. "ok",
  7369. "error",
  7370. 1);
  7371. return 1;
  7372. }
  7373. tinyfd_messageBox("The selected folder is",
  7374. lTheSelectFolderName, "ok", "info", 1);
  7375. lTheHexColor = tinyfd_colorChooser(
  7376. "choose a nice color",
  7377. "#FF0077",
  7378. lRgbColor,
  7379. lRgbColor);
  7380. if (!lTheHexColor)
  7381. {
  7382. tinyfd_messageBox(
  7383. "Error",
  7384. "hexcolor is NULL",
  7385. "ok",
  7386. "error",
  7387. 1);
  7388. return 1;
  7389. }
  7390. tinyfd_messageBox("The selected hexcolor is",
  7391. lTheHexColor, "ok", "info", 1);
  7392. tinyfd_beep();
  7393. return 0;
  7394. }
  7395. */
  7396. #ifdef _MSC_VER
  7397. #pragma warning(default:4996)
  7398. #pragma warning(default:4100)
  7399. #pragma warning(default:4706)
  7400. #endif