tinyfiledialogs.c 159 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096
  1. /*_________
  2. / \ tinyfiledialogs.c v2.9.2 [July 05, 2017] zlib licence
  3. |tiny file| Unique code file created [November 9, 2014]
  4. | dialogs | Copyright (c) 2014 - 2017 Guillaume Vareille http://ysengrin.com
  5. \____ ___/ http://tinyfiledialogs.sourceforge.net
  6. \|
  7. git://git.code.sf.net/p/tinyfiledialogs/code
  8. ______________________________________________
  9. | |
  10. | email: [email protected] |
  11. |______________________________________________|
  12. A big thank you to Don Heyse http://ldglite.sf.net for
  13. his code contributions, bug corrections & thorough testing!
  14. Please
  15. 1) let me know
  16. - if you are including tiny file dialogs,
  17. I'll be happy to add your link to the list of projects using it.
  18. - If you are using it on different hardware / OS / compiler.
  19. 2) leave a review on Sourceforge. Thanks.
  20. tiny file dialogs (cross-platform C C++)
  21. InputBox PasswordBox MessageBox ColorPicker
  22. OpenFileDialog SaveFileDialog SelectFolderDialog
  23. Native dialog library for WINDOWS MAC OSX GTK+ QT CONSOLE & more
  24. SSH supported via automatic switch to console mode or X11 forwarding
  25. A single C file (add it to your C or C++ project) with 6 functions:
  26. - message & question
  27. - input & password
  28. - save file
  29. - open file(s)
  30. - select folder
  31. - color picker.
  32. Complements OpenGL GLFW GLUT GLUI VTK SFML SDL Ogre Unity ION
  33. CEGUI MathGL CPW GLOW IMGUI MyGUI GLT NGL STB & GUI less programs
  34. NO INIT
  35. NO MAIN LOOP
  36. NO LINKING (true on unix, almost true on windows)
  37. The dialogs can be forced into console mode
  38. Windows (XP to 10) ASCII + MBCS + UTF-8 + UTF-16
  39. - native code & vbs create the graphic dialogs
  40. - enhanced console mode can use dialog.exe from
  41. http://andrear.altervista.org/home/cdialog.php
  42. - basic console input
  43. Unix (command line call attempts) ASCII + UTF-8
  44. - applescript
  45. - zenity / matedialog
  46. - qarma (zenity for qt)
  47. - kdialog
  48. - Xdialog
  49. - python2 tkinter
  50. - dialog (opens a console if needed)
  51. - basic console input
  52. The same executable can run across desktops & distributions
  53. tested with C & C++ compilers
  54. on VisualStudio MinGW Mac Linux Bsd Solaris Minix Raspbian C# fortran (iso_c)
  55. using Gnome Kde Enlightenment Mate Cinnamon Unity
  56. Lxde Lxqt Xfce WindowMaker IceWm Cde Jds OpenBox
  57. bindings for LUA and C# dll
  58. - License -
  59. This software is provided 'as-is', without any express or implied
  60. warranty. In no event will the authors be held liable for any damages
  61. arising from the use of this software.
  62. Permission is granted to anyone to use this software for any purpose,
  63. including commercial applications, and to alter it and redistribute it
  64. freely, subject to the following restrictions:
  65. 1. The origin of this software must not be misrepresented; you must not
  66. claim that you wrote the original software. If you use this software
  67. in a product, an acknowledgment in the product documentation would be
  68. appreciated but is not required.
  69. 2. Altered source versions must be plainly marked as such, and must not be
  70. misrepresented as being the original software.
  71. 3. This notice may not be removed or altered from any source distribution.
  72. */
  73. #include <stdio.h>
  74. #include <stdlib.h>
  75. #include <string.h>
  76. #include <ctype.h>
  77. #include <sys/stat.h>
  78. #include "tinyfiledialogs.h"
  79. /* #define TINYFD_NOLIB */
  80. #ifdef _WIN32
  81. #ifndef _WIN32_WINNT
  82. #define _WIN32_WINNT 0x0500
  83. #endif
  84. #ifndef TINYFD_NOLIB
  85. #include <Windows.h>
  86. /*#define TINYFD_NOSELECTFOLDERWIN*/
  87. #ifndef TINYFD_NOSELECTFOLDERWIN
  88. #include <Shlobj.h>
  89. #endif /*TINYFD_NOSELECTFOLDERWIN*/
  90. #endif
  91. #include <conio.h>
  92. /*#include <io.h>*/
  93. #define SLASH "\\"
  94. int tinyfd_winUtf8 = 0 ; /* on windows string char can be 0:MBSC or 1:UTF-8 */
  95. #else
  96. #include <limits.h>
  97. #include <unistd.h>
  98. #include <dirent.h> /* on old systems try <sys/dir.h> instead */
  99. #include <termios.h>
  100. #include <sys/utsname.h>
  101. #define SLASH "/"
  102. #endif /* _WIN32 */
  103. #define MAX_PATH_OR_CMD 1024 /* _MAX_PATH or MAX_PATH */
  104. #define MAX_MULTIPLE_FILES 32
  105. char tinyfd_version [8] = "2.9.2";
  106. static int tinyfd_verbose = 0 ; /* print on unix the command line calls */
  107. #if defined(TINYFD_NOLIB) && defined(_WIN32)
  108. int tinyfd_forceConsole = 1 ;
  109. #else
  110. int tinyfd_forceConsole = 0 ; /* 0 (default) or 1 */
  111. #endif
  112. /* for unix & windows: 0 (graphic mode) or 1 (console mode).
  113. 0: try to use a graphic solution, if it fails then it uses console mode.
  114. 1: forces all dialogs into console mode even when the X server is present,
  115. if the package dialog (and a console is present) or dialog.exe is installed.
  116. on windows it only make sense for console applications */
  117. char tinyfd_response[1024];
  118. /* if you pass "tinyfd_query" as aTitle,
  119. the functions will not display the dialogs
  120. but and return 0 for console mode, 1 for graphic mode.
  121. tinyfd_response is then filled with the retain solution.
  122. possible values for tinyfd_response are (all lowercase)
  123. for the graphic mode:
  124. windows applescript zenity zenity3 matedialog qarma kdialog
  125. tkinter gxmessage gmessage xmessage xdialog gdialog
  126. for the console mode:
  127. dialog whiptail basicinput */
  128. #if defined(TINYFD_NOLIB) && defined(_WIN32)
  129. static int gWarningDisplayed = 1 ;
  130. #else
  131. static int gWarningDisplayed = 0 ;
  132. #endif
  133. static char gTitle[]="missing software! (we will try basic console input)";
  134. #ifdef _WIN32
  135. static char gMessageWin[] = "\
  136. ___________\n\
  137. / \\ \n\
  138. | tiny file |\n\
  139. | dialogs |\n\
  140. \\_____ ____/\n\
  141. \\|\
  142. tiny file dialogs on Windows needs:\
  143. \n\ta graphic display\
  144. \nor\tdialog.exe (enhanced console mode)\
  145. \nor\ta console for basic input";
  146. #else
  147. static char gMessageUnix[] = "\
  148. ___________\n\
  149. / \\ \n\
  150. | tiny file |\n\
  151. | dialogs |\n\
  152. \\_____ ____/\n\
  153. \\|\
  154. \ntiny file dialogs on UNIX needs:\n\tapplescript\
  155. \nor\tzenity / matedialog\
  156. \nor\tqarma (zenity for qt)\
  157. \nor\tkdialog\
  158. \nor\tXdialog\
  159. \nor\tpython 2 + tkinter\
  160. \nor\tdialog (opens a console xterm if needed)\
  161. \nor\txterm + bash (opens a console for basic input)\
  162. \nor\tit will use the existing console for basic input";
  163. #endif
  164. #ifdef _MSC_VER
  165. #pragma warning(disable:4996) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */
  166. #pragma warning(disable:4100) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */
  167. #pragma warning(disable:4706) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */
  168. #endif
  169. static char * getPathWithoutFinalSlash(
  170. char * const aoDestination, /* make sure it is allocated, use _MAX_PATH */
  171. char const * const aSource) /* aoDestination and aSource can be the same */
  172. {
  173. char const * lTmp ;
  174. if ( aSource )
  175. {
  176. lTmp = strrchr(aSource, '/');
  177. if (!lTmp)
  178. {
  179. lTmp = strrchr(aSource, '\\');
  180. }
  181. if (lTmp)
  182. {
  183. strncpy(aoDestination, aSource, lTmp - aSource );
  184. aoDestination[lTmp - aSource] = '\0';
  185. }
  186. else
  187. {
  188. * aoDestination = '\0';
  189. }
  190. }
  191. else
  192. {
  193. * aoDestination = '\0';
  194. }
  195. return aoDestination;
  196. }
  197. static char * getLastName(
  198. char * const aoDestination, /* make sure it is allocated */
  199. char const * const aSource)
  200. {
  201. /* copy the last name after '/' or '\' */
  202. char const * lTmp ;
  203. if ( aSource )
  204. {
  205. lTmp = strrchr(aSource, '/');
  206. if (!lTmp)
  207. {
  208. lTmp = strrchr(aSource, '\\');
  209. }
  210. if (lTmp)
  211. {
  212. strcpy(aoDestination, lTmp + 1);
  213. }
  214. else
  215. {
  216. strcpy(aoDestination, aSource);
  217. }
  218. }
  219. else
  220. {
  221. * aoDestination = '\0';
  222. }
  223. return aoDestination;
  224. }
  225. static void ensureFinalSlash ( char * const aioString )
  226. {
  227. if ( aioString && strlen ( aioString ) )
  228. {
  229. char * lastcar = aioString + strlen ( aioString ) - 1 ;
  230. if ( strncmp ( lastcar , SLASH , 1 ) )
  231. {
  232. strcat ( lastcar , SLASH ) ;
  233. }
  234. }
  235. }
  236. static void Hex2RGB( char const aHexRGB [8] ,
  237. unsigned char aoResultRGB [3] )
  238. {
  239. char lColorChannel [8] ;
  240. if ( aoResultRGB )
  241. {
  242. if ( aHexRGB )
  243. {
  244. strcpy(lColorChannel, aHexRGB ) ;
  245. aoResultRGB[2] = (unsigned char)strtoul(lColorChannel+5,NULL,16);
  246. lColorChannel[5] = '\0';
  247. aoResultRGB[1] = (unsigned char)strtoul(lColorChannel+3,NULL,16);
  248. lColorChannel[3] = '\0';
  249. aoResultRGB[0] = (unsigned char)strtoul(lColorChannel+1,NULL,16);
  250. /* printf("%d %d %d\n", aoResultRGB[0], aoResultRGB[1], aoResultRGB[2]); */
  251. }
  252. else
  253. {
  254. aoResultRGB[0]=0;
  255. aoResultRGB[1]=0;
  256. aoResultRGB[2]=0;
  257. }
  258. }
  259. }
  260. static void RGB2Hex( unsigned char const aRGB [3] ,
  261. char aoResultHexRGB [8] )
  262. {
  263. if ( aoResultHexRGB )
  264. {
  265. if ( aRGB )
  266. {
  267. #if defined(__GNUC__) && defined(_WIN32)
  268. sprintf(aoResultHexRGB, "#%02hx%02hx%02hx",
  269. #else
  270. sprintf(aoResultHexRGB, "#%02hhx%02hhx%02hhx",
  271. #endif
  272. aRGB[0], aRGB[1], aRGB[2]);
  273. /* printf("aoResultHexRGB %s\n", aoResultHexRGB); */
  274. }
  275. else
  276. {
  277. aoResultHexRGB[0]=0;
  278. aoResultHexRGB[1]=0;
  279. aoResultHexRGB[2]=0;
  280. }
  281. }
  282. }
  283. static void replaceSubStr ( char const * const aSource ,
  284. char const * const aOldSubStr ,
  285. char const * const aNewSubStr ,
  286. char * const aoDestination )
  287. {
  288. char const * pOccurence ;
  289. char const * p ;
  290. char const * lNewSubStr = "" ;
  291. int lOldSubLen = strlen ( aOldSubStr ) ;
  292. if ( ! aSource )
  293. {
  294. * aoDestination = '\0' ;
  295. return ;
  296. }
  297. if ( ! aOldSubStr )
  298. {
  299. strcpy ( aoDestination , aSource ) ;
  300. return ;
  301. }
  302. if ( aNewSubStr )
  303. {
  304. lNewSubStr = aNewSubStr ;
  305. }
  306. p = aSource ;
  307. * aoDestination = '\0' ;
  308. while ( ( pOccurence = strstr ( p , aOldSubStr ) ) != NULL )
  309. {
  310. strncat ( aoDestination , p , pOccurence - p ) ;
  311. strcat ( aoDestination , lNewSubStr ) ;
  312. p = pOccurence + lOldSubLen ;
  313. }
  314. strcat ( aoDestination , p ) ;
  315. }
  316. static int filenameValid( char const * const aFileNameWithoutPath )
  317. {
  318. if ( ! aFileNameWithoutPath
  319. || ! strlen(aFileNameWithoutPath)
  320. || strpbrk(aFileNameWithoutPath , "\\/:*?\"<>|") )
  321. {
  322. return 0 ;
  323. }
  324. return 1 ;
  325. }
  326. static int fileExists( char const * const aFilePathAndName )
  327. {
  328. FILE * lIn ;
  329. if ( ! aFilePathAndName || ! strlen(aFilePathAndName) )
  330. {
  331. return 0 ;
  332. }
  333. lIn = fopen( aFilePathAndName , "r" ) ;
  334. if ( ! lIn )
  335. {
  336. return 0 ;
  337. }
  338. fclose ( lIn ) ;
  339. return 1 ;
  340. }
  341. /* source and destination can be the same or ovelap*/
  342. static char const * ensureFilesExist( char * const aDestination ,
  343. char const * const aSourcePathsAndNames)
  344. {
  345. char * lDestination = aDestination ;
  346. char const * p ;
  347. char const * p2 ;
  348. int lLen ;
  349. if ( ! aSourcePathsAndNames )
  350. {
  351. return NULL ;
  352. }
  353. lLen = strlen( aSourcePathsAndNames ) ;
  354. if ( ! lLen )
  355. {
  356. return NULL ;
  357. }
  358. p = aSourcePathsAndNames ;
  359. while ( (p2 = strchr(p, '|')) != NULL )
  360. {
  361. lLen = p2-p ;
  362. memmove(lDestination,p,lLen);
  363. lDestination[lLen] = '\0';
  364. if ( fileExists ( lDestination ) )
  365. {
  366. lDestination += lLen ;
  367. * lDestination = '|';
  368. lDestination ++ ;
  369. }
  370. p = p2 + 1 ;
  371. }
  372. if ( fileExists ( p ) )
  373. {
  374. lLen = strlen(p) ;
  375. memmove(lDestination,p,lLen);
  376. lDestination[lLen] = '\0';
  377. }
  378. else
  379. {
  380. * (lDestination-1) = '\0';
  381. }
  382. return aDestination ;
  383. }
  384. static void wipefile(char const * const aFilename)
  385. {
  386. int i;
  387. struct stat st;
  388. FILE * lIn;
  389. if (stat(aFilename, &st) == 0)
  390. {
  391. if ((lIn = fopen(aFilename, "w")))
  392. {
  393. for (i = 0; i < st.st_size; i++)
  394. {
  395. fputc('A', lIn);
  396. }
  397. }
  398. fclose(lIn);
  399. }
  400. }
  401. #ifdef _WIN32
  402. static int replaceChr ( char * const aString ,
  403. char const aOldChr ,
  404. char const aNewChr )
  405. {
  406. char * p ;
  407. int lRes = 0 ;
  408. if ( ! aString )
  409. {
  410. return 0 ;
  411. }
  412. if ( aOldChr == aNewChr )
  413. {
  414. return 0 ;
  415. }
  416. p = aString ;
  417. while ( (p = strchr ( p , aOldChr )) )
  418. {
  419. * p = aNewChr ;
  420. p ++ ;
  421. lRes = 1 ;
  422. }
  423. return lRes ;
  424. }
  425. static int dirExists ( char const * const aDirPath )
  426. {
  427. struct stat lInfo;
  428. if ( ! aDirPath || ! strlen ( aDirPath ) )
  429. return 0 ;
  430. if ( stat ( aDirPath , & lInfo ) != 0 )
  431. return 0 ;
  432. else if ( lInfo.st_mode & S_IFDIR )
  433. return 1 ;
  434. else
  435. return 0 ;
  436. }
  437. #ifndef TINYFD_NOLIB
  438. static wchar_t * getPathWithoutFinalSlashW(
  439. wchar_t * const aoDestination, /* make sure it is allocated, use _MAX_PATH */
  440. wchar_t const * const aSource) /* aoDestination and aSource can be the same */
  441. {
  442. wchar_t const * lTmp;
  443. if (aSource)
  444. {
  445. lTmp = wcsrchr(aSource, L'/');
  446. if (!lTmp)
  447. {
  448. lTmp = wcsrchr(aSource, L'\\');
  449. }
  450. if (lTmp)
  451. {
  452. wcsncpy(aoDestination, aSource, lTmp - aSource);
  453. aoDestination[lTmp - aSource] = L'\0';
  454. }
  455. else
  456. {
  457. *aoDestination = L'\0';
  458. }
  459. }
  460. else
  461. {
  462. *aoDestination = L'\0';
  463. }
  464. return aoDestination;
  465. }
  466. static wchar_t * getLastNameW(
  467. wchar_t * const aoDestination, /* make sure it is allocated */
  468. wchar_t const * const aSource)
  469. {
  470. /* copy the last name after '/' or '\' */
  471. wchar_t const * lTmp;
  472. if (aSource)
  473. {
  474. lTmp = wcsrchr(aSource, L'/');
  475. if (!lTmp)
  476. {
  477. lTmp = wcsrchr(aSource, L'\\');
  478. }
  479. if (lTmp)
  480. {
  481. wcscpy(aoDestination, lTmp + 1);
  482. }
  483. else
  484. {
  485. wcscpy(aoDestination, aSource);
  486. }
  487. }
  488. else
  489. {
  490. *aoDestination = L'\0';
  491. }
  492. return aoDestination;
  493. }
  494. static void Hex2RGBW(wchar_t const aHexRGB[8],
  495. unsigned char aoResultRGB[3])
  496. {
  497. wchar_t lColorChannel[8];
  498. if (aoResultRGB)
  499. {
  500. if (aHexRGB)
  501. {
  502. wcscpy(lColorChannel, aHexRGB);
  503. aoResultRGB[2] = (unsigned char)wcstoul(lColorChannel + 5, NULL, 16);
  504. lColorChannel[5] = '\0';
  505. aoResultRGB[1] = (unsigned char)wcstoul(lColorChannel + 3, NULL, 16);
  506. lColorChannel[3] = '\0';
  507. aoResultRGB[0] = (unsigned char)wcstoul(lColorChannel + 1, NULL, 16);
  508. /* printf("%d %d %d\n", aoResultRGB[0], aoResultRGB[1], aoResultRGB[2]); */
  509. }
  510. else
  511. {
  512. aoResultRGB[0] = 0;
  513. aoResultRGB[1] = 0;
  514. aoResultRGB[2] = 0;
  515. }
  516. }
  517. }
  518. static void RGB2HexW(
  519. unsigned char const aRGB[3],
  520. wchar_t aoResultHexRGB[8])
  521. {
  522. if (aoResultHexRGB)
  523. {
  524. if (aRGB)
  525. {
  526. #if defined(__GNUC__) && __GNUC__ < 5
  527. swprintf(aoResultHexRGB, L"#%02hhx%02hhx%02hhx", aRGB[0], aRGB[1], aRGB[2]);
  528. #else
  529. swprintf(aoResultHexRGB, 8, L"#%02hhx%02hhx%02hhx", aRGB[0], aRGB[1], aRGB[2]);
  530. #endif
  531. /* wprintf(L"aoResultHexRGB %s\n", aoResultHexRGB); */
  532. }
  533. else
  534. {
  535. aoResultHexRGB[0] = 0;
  536. aoResultHexRGB[1] = 0;
  537. aoResultHexRGB[2] = 0;
  538. }
  539. }
  540. }
  541. #if !defined(WC_ERR_INVALID_CHARS)
  542. /* undefined prior to Vista, so not yet in MINGW header file */
  543. #define WC_ERR_INVALID_CHARS 0x00000080
  544. #endif
  545. static int sizeUtf16(char const * const aUtf8string)
  546. {
  547. return MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
  548. aUtf8string, -1, NULL, 0);
  549. }
  550. static int sizeUtf8(wchar_t const * const aUtf16string)
  551. {
  552. return WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS,
  553. aUtf16string, -1, NULL, 0, NULL, NULL);
  554. }
  555. static wchar_t * utf8to16(char const * const aUtf8string)
  556. {
  557. wchar_t * lUtf16string ;
  558. int lSize = sizeUtf16(aUtf8string);
  559. lUtf16string = (wchar_t *) malloc( lSize * sizeof(wchar_t) );
  560. lSize = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
  561. aUtf8string, -1, lUtf16string, lSize);
  562. if (lSize == 0)
  563. {
  564. free(lUtf16string);
  565. return NULL;
  566. }
  567. return lUtf16string;
  568. }
  569. static char * utf16to8(wchar_t const * const aUtf16string)
  570. {
  571. char * lUtf8string ;
  572. int lSize = sizeUtf8(aUtf16string);
  573. lUtf8string = (char *) malloc( lSize );
  574. lSize = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS,
  575. aUtf16string, -1, lUtf8string, lSize, NULL, NULL);
  576. if (lSize == 0)
  577. {
  578. free(lUtf8string);
  579. return NULL;
  580. }
  581. return lUtf8string;
  582. }
  583. static void runSilentA(char const * const aString)
  584. {
  585. STARTUPINFOA StartupInfo;
  586. PROCESS_INFORMATION ProcessInfo;
  587. char * lArgs;
  588. char * pEnvCMD = NULL;
  589. char * pDefaultCMD = "CMD.EXE";
  590. ULONG rc;
  591. int lStringLen = 0;
  592. memset(&StartupInfo, 0, sizeof(StartupInfo));
  593. StartupInfo.cb = sizeof(STARTUPINFOA);
  594. StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
  595. StartupInfo.wShowWindow = SW_HIDE;
  596. if ( aString )
  597. {
  598. lStringLen = strlen(aString);
  599. }
  600. lArgs = (char *) malloc( MAX_PATH_OR_CMD + lStringLen );
  601. pEnvCMD = getenv("COMSPEC");
  602. if (pEnvCMD){
  603. strcpy(lArgs, pEnvCMD);
  604. }
  605. else{
  606. strcpy(lArgs, pDefaultCMD);
  607. }
  608. /* c to execute then terminate the command window */
  609. strcat(lArgs, " /c ");
  610. /* application and parameters to run from the command window */
  611. strcat(lArgs, aString);
  612. if (!CreateProcessA(NULL, lArgs, NULL, NULL, FALSE,
  613. CREATE_NEW_CONSOLE, NULL, NULL,
  614. &StartupInfo, &ProcessInfo))
  615. {
  616. free(lArgs);
  617. return; /* GetLastError(); */
  618. }
  619. WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
  620. if (!GetExitCodeProcess(ProcessInfo.hProcess, &rc))
  621. rc = 0;
  622. CloseHandle(ProcessInfo.hThread);
  623. CloseHandle(ProcessInfo.hProcess);
  624. free(lArgs);
  625. return; /* rc */
  626. }
  627. static void runSilentW(wchar_t const * const aString)
  628. {
  629. STARTUPINFOW StartupInfo;
  630. PROCESS_INFORMATION ProcessInfo;
  631. ULONG rc;
  632. wchar_t * lArgs;
  633. wchar_t * pEnvCMD;
  634. wchar_t * pDefaultCMD = L"CMD.EXE";
  635. int lStringLen = 0;
  636. memset(&StartupInfo, 0, sizeof(StartupInfo));
  637. StartupInfo.cb = sizeof(STARTUPINFOW);
  638. StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
  639. StartupInfo.wShowWindow = SW_HIDE;
  640. if ( aString )
  641. {
  642. lStringLen = wcslen(aString);
  643. }
  644. lArgs = (wchar_t *) malloc( (MAX_PATH_OR_CMD + lStringLen) * sizeof(wchar_t) );
  645. pEnvCMD = utf8to16( getenv("COMSPEC") );
  646. if (pEnvCMD)
  647. {
  648. wcscpy(lArgs, pEnvCMD);
  649. free(pEnvCMD);
  650. }
  651. else
  652. {
  653. wcscpy(lArgs, pDefaultCMD);
  654. }
  655. /* c to execute then terminate the command window */
  656. wcscat(lArgs, L" /c ");
  657. /* application and parameters to run from the command window */
  658. wcscat(lArgs, aString);
  659. if (!CreateProcessW(NULL, lArgs, NULL, NULL, FALSE,
  660. CREATE_NEW_CONSOLE, NULL, NULL,
  661. &StartupInfo, &ProcessInfo))
  662. {
  663. free(lArgs);
  664. return; /* GetLastError(); */
  665. }
  666. WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
  667. if (!GetExitCodeProcess(ProcessInfo.hProcess, &rc))
  668. {
  669. rc = 0;
  670. }
  671. CloseHandle(ProcessInfo.hThread);
  672. CloseHandle(ProcessInfo.hProcess);
  673. free(lArgs);
  674. return; /* rc */
  675. }
  676. int tinyfd_messageBoxW(
  677. wchar_t const * const aTitle, /* NULL or "" */
  678. wchar_t const * const aMessage, /* NULL or "" may contain \n and \t */
  679. wchar_t const * const aDialogType, /* "ok" "okcancel" "yesno" "yesnocancel" */
  680. wchar_t const * const aIconType, /* "info" "warning" "error" "question" */
  681. int const aDefaultButton) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */
  682. {
  683. int lBoxReturnValue;
  684. UINT aCode;
  685. if (aIconType && !wcscmp(L"warning", aIconType))
  686. {
  687. aCode = MB_ICONWARNING;
  688. }
  689. else if (aIconType && !wcscmp(L"error", aIconType))
  690. {
  691. aCode = MB_ICONERROR;
  692. }
  693. else if (aIconType && !wcscmp(L"question", aIconType))
  694. {
  695. aCode = MB_ICONQUESTION;
  696. }
  697. else
  698. {
  699. aCode = MB_ICONINFORMATION;
  700. }
  701. if (aDialogType && !wcscmp(L"okcancel", aDialogType))
  702. {
  703. aCode += MB_OKCANCEL;
  704. if (!aDefaultButton)
  705. {
  706. aCode += MB_DEFBUTTON2;
  707. }
  708. }
  709. else if (aDialogType && !wcscmp(L"yesno", aDialogType))
  710. {
  711. aCode += MB_YESNO;
  712. if (!aDefaultButton)
  713. {
  714. aCode += MB_DEFBUTTON2;
  715. }
  716. }
  717. else
  718. {
  719. aCode += MB_OK;
  720. }
  721. lBoxReturnValue = MessageBoxW(NULL, aMessage, aTitle, aCode);
  722. if (((aDialogType
  723. && wcscmp(L"okcancel", aDialogType)
  724. && wcscmp(L"yesno", aDialogType)))
  725. || (lBoxReturnValue == IDOK)
  726. || (lBoxReturnValue == IDYES))
  727. {
  728. return 1;
  729. }
  730. else
  731. {
  732. return 0;
  733. }
  734. }
  735. static int messageBoxWinGui8(
  736. char const * const aTitle, /* NULL or "" */
  737. char const * const aMessage, /* NULL or "" may contain \n and \t */
  738. char const * const aDialogType, /* "ok" "okcancel" "yesno" "yesnocancel" */
  739. char const * const aIconType, /* "info" "warning" "error" "question" */
  740. int const aDefaultButton) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */
  741. {
  742. int lIntRetVal;
  743. wchar_t * lTitle;
  744. wchar_t * lMessage;
  745. wchar_t * lDialogType;
  746. wchar_t * lIconType;
  747. lTitle = utf8to16(aTitle);
  748. lMessage = utf8to16(aMessage);
  749. lDialogType = utf8to16(aDialogType);
  750. lIconType = utf8to16(aIconType);
  751. lIntRetVal = tinyfd_messageBoxW(lTitle, lMessage,
  752. lDialogType, lIconType, aDefaultButton );
  753. free(lTitle);
  754. free(lMessage);
  755. free(lDialogType);
  756. free(lIconType);
  757. return lIntRetVal ;
  758. }
  759. static char const * inputBoxWinGui(
  760. char * const aoBuff ,
  761. char const * const aTitle , /* NULL or "" */
  762. char const * const aMessage , /* NULL or "" may NOT contain \n nor \t */
  763. char const * const aDefaultInput ) /* "" , if NULL it's a passwordBox */
  764. {
  765. char * lDialogString;
  766. FILE * lIn;
  767. int lResult;
  768. int lTitleLen;
  769. int lMessageLen;
  770. wchar_t * lDialogStringW;
  771. lTitleLen = aTitle ? strlen(aTitle) : 0 ;
  772. lMessageLen = aMessage ? strlen(aMessage) : 0 ;
  773. lDialogString = (char *)malloc(3 * MAX_PATH_OR_CMD + lTitleLen + lMessageLen);
  774. if (aDefaultInput)
  775. {
  776. sprintf(lDialogString, "%s\\AppData\\Local\\Temp\\tinyfd.vbs",
  777. getenv("USERPROFILE"));
  778. }
  779. else
  780. {
  781. sprintf(lDialogString, "%s\\AppData\\Local\\Temp\\tinyfd.hta",
  782. getenv("USERPROFILE"));
  783. }
  784. lIn = fopen(lDialogString, "w");
  785. if (!lIn)
  786. {
  787. free(lDialogString);
  788. return NULL;
  789. }
  790. if ( aDefaultInput )
  791. {
  792. strcpy(lDialogString, "Dim result:result=InputBox(\"");
  793. if (aMessage && strlen(aMessage))
  794. {
  795. strcat(lDialogString, aMessage);
  796. }
  797. strcat(lDialogString, "\",\"");
  798. if (aTitle && strlen(aTitle))
  799. {
  800. strcat(lDialogString, aTitle);
  801. }
  802. strcat(lDialogString, "\",\"");
  803. if (aDefaultInput && strlen(aDefaultInput))
  804. {
  805. strcat(lDialogString, aDefaultInput);
  806. }
  807. strcat(lDialogString, "\"):If IsEmpty(result) then:WScript.Echo 0");
  808. strcat(lDialogString, ":Else: WScript.Echo \"1\" & result : End If");
  809. }
  810. else
  811. {
  812. sprintf(lDialogString, "\n\
  813. <html>\n\
  814. <head>\n\
  815. <title>%s</title>\n\
  816. <HTA:APPLICATION\n\
  817. ID = 'tinyfdHTA'\n\
  818. APPLICATIONNAME = 'tinyfd_inputBox'\n\
  819. MINIMIZEBUTTON = 'no'\n\
  820. MAXIMIZEBUTTON = 'no'\n\
  821. BORDER = 'dialog'\n\
  822. SCROLL = 'no'\n\
  823. SINGLEINSTANCE = 'yes'\n\
  824. WINDOWSTATE = 'hidden'>\n\
  825. \n\
  826. <script language = 'VBScript'>\n\
  827. \n\
  828. intWidth = Screen.Width/4\n\
  829. intHeight = Screen.Height/6\n\
  830. ResizeTo intWidth, intHeight\n\
  831. MoveTo((Screen.Width/2)-(intWidth/2)),((Screen.Height/2)-(intHeight/2))\n\
  832. result = 0\n\
  833. \n\
  834. Sub Window_onLoad\n\
  835. txt_input.Focus\n\
  836. End Sub\n\
  837. \n\
  838. Sub Window_onUnload\n\
  839. Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n\
  840. Set oShell = CreateObject(\"WScript.Shell\")\n\
  841. strHomeFolder = oShell.ExpandEnvironmentStrings(\"%%USERPROFILE%%\")\n\
  842. Set objFile = objFSO.CreateTextFile(strHomeFolder & \"\\AppData\\Local\\Temp\\tinyfd.txt\",True)\n\
  843. If result = 1 Then\n\
  844. objFile.Write 1 & txt_input.Value\n\
  845. Else\n\
  846. objFile.Write 0\n\
  847. End If\n\
  848. objFile.Close\n\
  849. End Sub\n\
  850. \n\
  851. Sub Run_ProgramOK\n\
  852. result = 1\n\
  853. window.Close\n\
  854. End Sub\n\
  855. \n\
  856. Sub Run_ProgramCancel\n\
  857. window.Close\n\
  858. End Sub\n\
  859. \n\
  860. Sub Default_Buttons\n\
  861. If Window.Event.KeyCode = 13 Then\n\
  862. btn_OK.Click\n\
  863. ElseIf Window.Event.KeyCode = 27 Then\n\
  864. btn_Cancel.Click\n\
  865. End If\n\
  866. End Sub\n\
  867. \n\
  868. </script>\n\
  869. </head>\n\
  870. <body style = 'background-color:#EEEEEE' onkeypress = 'vbs:Default_Buttons' align = 'top'>\n\
  871. <table width = '100%%' height = '80%%' align = 'center' border = '0'>\n\
  872. <tr border = '0'>\n\
  873. <td align = 'left' valign = 'middle' style='Font-Family:Arial'>\n\
  874. %s\n\
  875. </td>\n\
  876. <td align = 'right' valign = 'middle' style = 'margin-top: 0em'>\n\
  877. <table align = 'right' style = 'margin-right: 0em;'>\n\
  878. <tr align = 'right' style = 'margin-top: 5em;'>\n\
  879. <input type = 'button' value = 'OK' name = 'btn_OK' onClick = 'vbs:Run_ProgramOK' style = 'width: 5em; margin-top: 2em;'><br>\n\
  880. <input type = 'button' value = 'Cancel' name = 'btn_Cancel' onClick = 'vbs:Run_ProgramCancel' style = 'width: 5em;'><br><br>\n\
  881. </tr>\n\
  882. </table>\n\
  883. </td>\n\
  884. </tr>\n\
  885. </table>\n\
  886. <table width = '100%%' height = '100%%' align = 'center' border = '0'>\n\
  887. <tr>\n\
  888. <td align = 'left' valign = 'top'>\n\
  889. <input type = 'password' id = 'txt_input'\n\
  890. name = 'txt_input' value = '' style = 'float:left;width:100%%' ><BR>\n\
  891. </td>\n\
  892. </tr>\n\
  893. </table>\n\
  894. </body>\n\
  895. </html>\n\
  896. " , aTitle ? aTitle : "", aMessage ? aMessage : "") ;
  897. }
  898. fputs(lDialogString, lIn);
  899. fclose(lIn);
  900. strcpy(lDialogString, "");
  901. if (aDefaultInput)
  902. {
  903. strcat(lDialogString, "cscript.exe ");
  904. strcat(lDialogString, "//Nologo ");
  905. strcat(lDialogString,"%USERPROFILE%\\AppData\\Local\\Temp\\tinyfd.vbs");
  906. strcat(lDialogString, " > %USERPROFILE%\\AppData\\Local\\Temp\\tinyfd.txt");
  907. }
  908. else
  909. {
  910. strcat(lDialogString,
  911. "mshta.exe %USERPROFILE%\\AppData\\Local\\Temp\\tinyfd.hta");
  912. }
  913. /* printf ( "lDialogString: %s\n" , lDialogString ) ; */
  914. if (tinyfd_winUtf8)
  915. {
  916. lDialogStringW = utf8to16(lDialogString);
  917. runSilentW(lDialogStringW);
  918. free(lDialogStringW);
  919. }
  920. else
  921. {
  922. runSilentA(lDialogString);
  923. }
  924. /*
  925. if (!(lIn = _popen(lDialogString, "r")))
  926. {
  927. free(lDialogString);
  928. return NULL;
  929. }
  930. while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL)
  931. {
  932. }
  933. _pclose(lIn);
  934. if (aoBuff[strlen(aoBuff) - 1] == '\n')
  935. {
  936. aoBuff[strlen(aoBuff) - 1] = '\0';
  937. }
  938. */
  939. if (aDefaultInput)
  940. {
  941. sprintf(lDialogString, "%s\\AppData\\Local\\Temp\\tinyfd.txt",
  942. getenv("USERPROFILE"));
  943. if (!(lIn = fopen(lDialogString, "r")))
  944. {
  945. remove(lDialogString);
  946. free(lDialogString);
  947. return NULL;
  948. }
  949. while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL)
  950. {}
  951. fclose(lIn);
  952. remove(lDialogString);
  953. sprintf(lDialogString, "%s\\AppData\\Local\\Temp\\tinyfd.vbs",
  954. getenv("USERPROFILE"));
  955. }
  956. else
  957. {
  958. sprintf(lDialogString, "%s\\AppData\\Local\\Temp\\tinyfd.txt",
  959. getenv("USERPROFILE"));
  960. if (!(lIn = fopen(lDialogString, "r")))
  961. {
  962. remove(lDialogString);
  963. free(lDialogString);
  964. return NULL;
  965. }
  966. while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL)
  967. {}
  968. fclose(lIn);
  969. wipefile(lDialogString);
  970. remove(lDialogString);
  971. sprintf(lDialogString, "%s\\AppData\\Local\\Temp\\tinyfd.hta",
  972. getenv("USERPROFILE"));
  973. }
  974. remove(lDialogString);
  975. free(lDialogString);
  976. /* printf ( "aoBuff: %s\n" , aoBuff ) ; */
  977. lResult = strncmp(aoBuff, "1", 1) ? 0 : 1;
  978. /* printf ( "lResult: %d \n" , lResult ) ; */
  979. if (!lResult)
  980. {
  981. return NULL ;
  982. }
  983. if (aoBuff[strlen(aoBuff) - 1] == '\n')
  984. {
  985. aoBuff[strlen(aoBuff) - 1] = '\0';
  986. }
  987. /* printf ( "aoBuff+1: %s\n" , aoBuff+1 ) ; */
  988. return aoBuff + 1;
  989. }
  990. wchar_t const * tinyfd_saveFileDialogW(
  991. wchar_t const * const aTitle, /* NULL or "" */
  992. wchar_t const * const aDefaultPathAndFile, /* NULL or "" */
  993. int const aNumOfFilterPatterns, /* 0 */
  994. wchar_t const * const * const aFilterPatterns, /* NULL or {"*.jpg","*.png"} */
  995. wchar_t const * const aSingleFilterDescription) /* NULL or "image files" */
  996. {
  997. static wchar_t lBuff[MAX_PATH_OR_CMD];
  998. wchar_t lDirname[MAX_PATH_OR_CMD];
  999. wchar_t lDialogString[MAX_PATH_OR_CMD];
  1000. wchar_t lFilterPatterns[MAX_PATH_OR_CMD] = L"";
  1001. wchar_t * p;
  1002. wchar_t * lRetval;
  1003. int i;
  1004. HRESULT lHResult;
  1005. OPENFILENAMEW ofn = {0};
  1006. lHResult = CoInitializeEx(NULL, 0);
  1007. getPathWithoutFinalSlashW(lDirname, aDefaultPathAndFile);
  1008. getLastNameW(lBuff, aDefaultPathAndFile);
  1009. if (aNumOfFilterPatterns > 0)
  1010. {
  1011. if (aSingleFilterDescription && wcslen(aSingleFilterDescription))
  1012. {
  1013. wcscpy(lFilterPatterns, aSingleFilterDescription);
  1014. wcscat(lFilterPatterns, L"\n");
  1015. }
  1016. wcscat(lFilterPatterns, aFilterPatterns[0]);
  1017. for (i = 1; i < aNumOfFilterPatterns; i++)
  1018. {
  1019. wcscat(lFilterPatterns, L";");
  1020. wcscat(lFilterPatterns, aFilterPatterns[i]);
  1021. }
  1022. wcscat(lFilterPatterns, L"\n");
  1023. if (!(aSingleFilterDescription && wcslen(aSingleFilterDescription)))
  1024. {
  1025. wcscpy(lDialogString, lFilterPatterns);
  1026. wcscat(lFilterPatterns, lDialogString);
  1027. }
  1028. wcscat(lFilterPatterns, L"All Files\n*.*\n");
  1029. p = lFilterPatterns;
  1030. while ((p = wcschr(p, L'\n')) != NULL)
  1031. {
  1032. *p = L'\0';
  1033. p++;
  1034. }
  1035. }
  1036. ofn.lStructSize = sizeof(OPENFILENAMEW);
  1037. ofn.hwndOwner = 0;
  1038. ofn.hInstance = 0;
  1039. ofn.lpstrFilter = lFilterPatterns && wcslen(lFilterPatterns) ? lFilterPatterns : NULL;
  1040. ofn.lpstrCustomFilter = NULL;
  1041. ofn.nMaxCustFilter = 0;
  1042. ofn.nFilterIndex = 1;
  1043. ofn.lpstrFile = lBuff;
  1044. ofn.nMaxFile = MAX_PATH_OR_CMD;
  1045. ofn.lpstrFileTitle = NULL;
  1046. ofn.nMaxFileTitle = _MAX_FNAME + _MAX_EXT;
  1047. ofn.lpstrInitialDir = lDirname && wcslen(lDirname) ? lDirname : NULL;
  1048. ofn.lpstrTitle = aTitle && wcslen(aTitle) ? aTitle : NULL;
  1049. ofn.Flags = OFN_OVERWRITEPROMPT | OFN_NOCHANGEDIR;
  1050. ofn.nFileOffset = 0;
  1051. ofn.nFileExtension = 0;
  1052. ofn.lpstrDefExt = NULL;
  1053. ofn.lCustData = 0L;
  1054. ofn.lpfnHook = NULL;
  1055. ofn.lpTemplateName = NULL;
  1056. if (GetSaveFileNameW(&ofn) == 0)
  1057. {
  1058. lRetval = NULL;
  1059. }
  1060. else
  1061. {
  1062. lRetval = lBuff;
  1063. }
  1064. if (lHResult == S_OK || lHResult == S_FALSE)
  1065. {
  1066. CoUninitialize();
  1067. }
  1068. return lRetval;
  1069. }
  1070. static char const * saveFileDialogWinGui8(
  1071. char * const aoBuff,
  1072. char const * const aTitle, /* NULL or "" */
  1073. char const * const aDefaultPathAndFile, /* NULL or "" */
  1074. int const aNumOfFilterPatterns, /* 0 */
  1075. char const * const * const aFilterPatterns, /* NULL or {"*.jpg","*.png"} */
  1076. char const * const aSingleFilterDescription) /* NULL or "image files" */
  1077. {
  1078. wchar_t * lTitle;
  1079. wchar_t * lDefaultPathAndFile;
  1080. wchar_t * lSingleFilterDescription;
  1081. wchar_t * * lFilterPatterns;
  1082. wchar_t const * lTmpWChar;
  1083. char * lTmpChar;
  1084. int i ;
  1085. lFilterPatterns = (wchar_t **) malloc(aNumOfFilterPatterns*sizeof(wchar_t *));
  1086. for (i = 0; i < aNumOfFilterPatterns; i++)
  1087. {
  1088. lFilterPatterns[i] = utf8to16(aFilterPatterns[i]);
  1089. }
  1090. lTitle = utf8to16(aTitle);
  1091. lDefaultPathAndFile = utf8to16(aDefaultPathAndFile);
  1092. lSingleFilterDescription = utf8to16(aSingleFilterDescription);
  1093. lTmpWChar = tinyfd_saveFileDialogW(
  1094. lTitle,
  1095. lDefaultPathAndFile,
  1096. aNumOfFilterPatterns,
  1097. (wchar_t const** ) /*stupid cast for gcc*/
  1098. lFilterPatterns,
  1099. lSingleFilterDescription);
  1100. free(lTitle);
  1101. free(lDefaultPathAndFile);
  1102. free(lSingleFilterDescription);
  1103. for (i = 0; i < aNumOfFilterPatterns; i++)
  1104. {
  1105. free(lFilterPatterns[i]);
  1106. }
  1107. free(lFilterPatterns);
  1108. if (!lTmpWChar)
  1109. {
  1110. return NULL;
  1111. }
  1112. lTmpChar = utf16to8(lTmpWChar);
  1113. strcpy(aoBuff, lTmpChar);
  1114. free(lTmpChar);
  1115. return aoBuff;
  1116. }
  1117. wchar_t const * tinyfd_openFileDialogW(
  1118. wchar_t const * const aTitle, /* NULL or "" */
  1119. wchar_t const * const aDefaultPathAndFile, /* NULL or "" */
  1120. int const aNumOfFilterPatterns, /* 0 */
  1121. wchar_t const * const * const aFilterPatterns, /* NULL or {"*.jpg","*.png"} */
  1122. wchar_t const * const aSingleFilterDescription, /* NULL or "image files" */
  1123. int const aAllowMultipleSelects) /* 0 or 1 */
  1124. {
  1125. static wchar_t lBuff[MAX_MULTIPLE_FILES*MAX_PATH_OR_CMD];
  1126. size_t lLengths[MAX_MULTIPLE_FILES];
  1127. wchar_t lDirname[MAX_PATH_OR_CMD];
  1128. wchar_t lFilterPatterns[MAX_PATH_OR_CMD] = L"";
  1129. wchar_t lDialogString[MAX_PATH_OR_CMD];
  1130. wchar_t * lPointers[MAX_MULTIPLE_FILES];
  1131. wchar_t * lRetval, * p;
  1132. int i, j;
  1133. size_t lBuffLen;
  1134. HRESULT lHResult;
  1135. OPENFILENAMEW ofn = { 0 };
  1136. lHResult = CoInitializeEx(NULL, 0);
  1137. getPathWithoutFinalSlashW(lDirname, aDefaultPathAndFile);
  1138. getLastNameW(lBuff, aDefaultPathAndFile);
  1139. if (aNumOfFilterPatterns > 0)
  1140. {
  1141. if (aSingleFilterDescription && wcslen(aSingleFilterDescription))
  1142. {
  1143. wcscpy(lFilterPatterns, aSingleFilterDescription);
  1144. wcscat(lFilterPatterns, L"\n");
  1145. }
  1146. wcscat(lFilterPatterns, aFilterPatterns[0]);
  1147. for (i = 1; i < aNumOfFilterPatterns; i++)
  1148. {
  1149. wcscat(lFilterPatterns, L";");
  1150. wcscat(lFilterPatterns, aFilterPatterns[i]);
  1151. }
  1152. wcscat(lFilterPatterns, L"\n");
  1153. if (!(aSingleFilterDescription && wcslen(aSingleFilterDescription)))
  1154. {
  1155. wcscpy(lDialogString, lFilterPatterns);
  1156. wcscat(lFilterPatterns, lDialogString);
  1157. }
  1158. wcscat(lFilterPatterns, L"All Files\n*.*\n");
  1159. p = lFilterPatterns;
  1160. while ((p = wcschr(p, L'\n')) != NULL)
  1161. {
  1162. *p = L'\0';
  1163. p++;
  1164. }
  1165. }
  1166. ofn.lStructSize = sizeof(OPENFILENAME);
  1167. ofn.hwndOwner = 0;
  1168. ofn.hInstance = 0;
  1169. ofn.lpstrFilter = lFilterPatterns && wcslen(lFilterPatterns) ? lFilterPatterns : NULL;
  1170. ofn.lpstrCustomFilter = NULL;
  1171. ofn.nMaxCustFilter = 0;
  1172. ofn.nFilterIndex = 1;
  1173. ofn.lpstrFile = lBuff;
  1174. ofn.nMaxFile = MAX_PATH_OR_CMD;
  1175. ofn.lpstrFileTitle = NULL;
  1176. ofn.nMaxFileTitle = _MAX_FNAME + _MAX_EXT;
  1177. ofn.lpstrInitialDir = lDirname && wcslen(lDirname) ? lDirname : NULL;
  1178. ofn.lpstrTitle = aTitle && wcslen(aTitle) ? aTitle : NULL;
  1179. ofn.Flags = OFN_EXPLORER | OFN_NOCHANGEDIR;
  1180. ofn.nFileOffset = 0;
  1181. ofn.nFileExtension = 0;
  1182. ofn.lpstrDefExt = NULL;
  1183. ofn.lCustData = 0L;
  1184. ofn.lpfnHook = NULL;
  1185. ofn.lpTemplateName = NULL;
  1186. if (aAllowMultipleSelects)
  1187. {
  1188. ofn.Flags |= OFN_ALLOWMULTISELECT;
  1189. }
  1190. if (GetOpenFileNameW(&ofn) == 0)
  1191. {
  1192. lRetval = NULL;
  1193. }
  1194. else
  1195. {
  1196. lBuffLen = wcslen(lBuff);
  1197. lPointers[0] = lBuff + lBuffLen + 1;
  1198. if (!aAllowMultipleSelects || (lPointers[0][0] == L'\0'))
  1199. {
  1200. lRetval = lBuff;
  1201. }
  1202. else
  1203. {
  1204. i = 0;
  1205. do
  1206. {
  1207. lLengths[i] = wcslen(lPointers[i]);
  1208. lPointers[i + 1] = lPointers[i] + lLengths[i] + 1;
  1209. i++;
  1210. } while (lPointers[i][0] != L'\0');
  1211. i--;
  1212. p = lBuff + MAX_MULTIPLE_FILES*MAX_PATH_OR_CMD - 1;
  1213. *p = L'\0';
  1214. for (j = i; j >= 0; j--)
  1215. {
  1216. p -= lLengths[j];
  1217. memmove(p, lPointers[j], lLengths[j]*sizeof(wchar_t));
  1218. p--;
  1219. *p = L'\\';
  1220. p -= lBuffLen;
  1221. memmove(p, lBuff, lBuffLen*sizeof(wchar_t));
  1222. p--;
  1223. *p = L'|';
  1224. }
  1225. p++;
  1226. lRetval = p;
  1227. }
  1228. }
  1229. if (lHResult == S_OK || lHResult == S_FALSE)
  1230. {
  1231. CoUninitialize();
  1232. }
  1233. return lRetval;
  1234. }
  1235. static char const * openFileDialogWinGui8(
  1236. char * const aoBuff,
  1237. char const * const aTitle, /* NULL or "" */
  1238. char const * const aDefaultPathAndFile, /* NULL or "" */
  1239. int const aNumOfFilterPatterns, /* 0 */
  1240. char const * const * const aFilterPatterns, /* NULL or {"*.jpg","*.png"} */
  1241. char const * const aSingleFilterDescription, /* NULL or "image files" */
  1242. int const aAllowMultipleSelects) /* 0 or 1 */
  1243. {
  1244. wchar_t * lTitle;
  1245. wchar_t * lDefaultPathAndFile;
  1246. wchar_t * lSingleFilterDescription;
  1247. wchar_t * * lFilterPatterns;
  1248. wchar_t const * lTmpWChar;
  1249. char * lTmpChar;
  1250. int i;
  1251. lFilterPatterns = (wchar_t * *) malloc(aNumOfFilterPatterns*sizeof(wchar_t *));
  1252. for (i = 0; i < aNumOfFilterPatterns; i++)
  1253. {
  1254. lFilterPatterns[i] = utf8to16(aFilterPatterns[i]);
  1255. }
  1256. lTitle = utf8to16(aTitle);
  1257. lDefaultPathAndFile = utf8to16(aDefaultPathAndFile);
  1258. lSingleFilterDescription = utf8to16(aSingleFilterDescription);
  1259. lTmpWChar = tinyfd_openFileDialogW(
  1260. lTitle,
  1261. lDefaultPathAndFile,
  1262. aNumOfFilterPatterns,
  1263. (wchar_t const**) /*stupid cast for gcc*/
  1264. lFilterPatterns,
  1265. lSingleFilterDescription,
  1266. aAllowMultipleSelects);
  1267. free(lTitle);
  1268. free(lDefaultPathAndFile);
  1269. free(lSingleFilterDescription);
  1270. for (i = 0; i < aNumOfFilterPatterns; i++)
  1271. {
  1272. free(lFilterPatterns[i]);
  1273. }
  1274. free(lFilterPatterns);
  1275. if (!lTmpWChar)
  1276. {
  1277. return NULL;
  1278. }
  1279. lTmpChar = utf16to8(lTmpWChar);
  1280. strcpy(aoBuff, lTmpChar);
  1281. free(lTmpChar);
  1282. return aoBuff;
  1283. }
  1284. #ifndef TINYFD_NOSELECTFOLDERWIN
  1285. static int __stdcall BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData)
  1286. {
  1287. if (uMsg == BFFM_INITIALIZED)
  1288. {
  1289. SendMessage(hwnd, BFFM_SETSELECTION, TRUE, pData);
  1290. }
  1291. return 0;
  1292. }
  1293. static int __stdcall BrowseCallbackProcW(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData)
  1294. {
  1295. if (uMsg == BFFM_INITIALIZED)
  1296. {
  1297. SendMessage(hwnd, BFFM_SETSELECTIONW, TRUE, (LPARAM)pData);
  1298. }
  1299. return 0;
  1300. }
  1301. wchar_t const * tinyfd_selectFolderDialogW(
  1302. wchar_t const * const aTitle, /* NULL or "" */
  1303. wchar_t const * const aDefaultPath) /* NULL or "" */
  1304. {
  1305. static wchar_t lBuff[MAX_PATH_OR_CMD];
  1306. BROWSEINFOW bInfo;
  1307. LPITEMIDLIST lpItem;
  1308. HRESULT lHResult;
  1309. lHResult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
  1310. bInfo.hwndOwner = 0;
  1311. bInfo.pidlRoot = NULL;
  1312. bInfo.pszDisplayName = lBuff;
  1313. bInfo.lpszTitle = aTitle && wcslen(aTitle) ? aTitle : NULL;
  1314. if (lHResult == S_OK || lHResult == S_FALSE)
  1315. {
  1316. bInfo.ulFlags = BIF_USENEWUI;
  1317. }
  1318. bInfo.lpfn = BrowseCallbackProcW;
  1319. bInfo.lParam = (LPARAM)aDefaultPath;
  1320. bInfo.iImage = -1;
  1321. lpItem = SHBrowseForFolderW(&bInfo);
  1322. if (lpItem)
  1323. {
  1324. SHGetPathFromIDListW(lpItem, lBuff);
  1325. }
  1326. if (lHResult == S_OK || lHResult == S_FALSE)
  1327. {
  1328. CoUninitialize();
  1329. }
  1330. return lBuff;
  1331. }
  1332. static char const * selectFolderDialogWinGui8 (
  1333. char * const aoBuff ,
  1334. char const * const aTitle , /* NULL or "" */
  1335. char const * const aDefaultPath ) /* NULL or "" */
  1336. {
  1337. wchar_t * lTitle;
  1338. wchar_t * lDefaultPath;
  1339. wchar_t const * lTmpWChar;
  1340. char * lTmpChar;
  1341. lTitle = utf8to16(aTitle);
  1342. lDefaultPath = utf8to16(aDefaultPath);
  1343. lTmpWChar = tinyfd_selectFolderDialogW(
  1344. lTitle,
  1345. lDefaultPath);
  1346. free(lTitle);
  1347. free(lDefaultPath);
  1348. if (!lTmpWChar)
  1349. {
  1350. return NULL;
  1351. }
  1352. lTmpChar = utf16to8(lTmpWChar);
  1353. strcpy(aoBuff, lTmpChar);
  1354. free(lTmpChar);
  1355. return aoBuff;
  1356. }
  1357. #endif /*TINYFD_NOSELECTFOLDERWIN*/
  1358. wchar_t const * tinyfd_colorChooserW(
  1359. wchar_t const * const aTitle, /* NULL or "" */
  1360. wchar_t const * const aDefaultHexRGB, /* NULL or "#FF0000"*/
  1361. unsigned char const aDefaultRGB[3], /* { 0 , 255 , 255 } */
  1362. unsigned char aoResultRGB[3]) /* { 0 , 0 , 0 } */
  1363. {
  1364. static wchar_t lResultHexRGB[8];
  1365. CHOOSECOLORW cc;
  1366. COLORREF crCustColors[16];
  1367. unsigned char lDefaultRGB[3];
  1368. int lRet;
  1369. HRESULT lHResult;
  1370. lHResult = CoInitializeEx(NULL, 0);
  1371. if (aDefaultHexRGB)
  1372. {
  1373. Hex2RGBW(aDefaultHexRGB, lDefaultRGB);
  1374. }
  1375. else
  1376. {
  1377. lDefaultRGB[0] = aDefaultRGB[0];
  1378. lDefaultRGB[1] = aDefaultRGB[1];
  1379. lDefaultRGB[2] = aDefaultRGB[2];
  1380. }
  1381. /* we can't use aTitle */
  1382. cc.lStructSize = sizeof(CHOOSECOLOR);
  1383. cc.hwndOwner = NULL;
  1384. cc.hInstance = NULL;
  1385. cc.rgbResult = RGB(lDefaultRGB[0], lDefaultRGB[1], lDefaultRGB[2]);
  1386. cc.lpCustColors = crCustColors;
  1387. cc.Flags = CC_RGBINIT | CC_FULLOPEN;
  1388. cc.lCustData = 0;
  1389. cc.lpfnHook = NULL;
  1390. cc.lpTemplateName = NULL;
  1391. lRet = ChooseColorW(&cc);
  1392. if (!lRet)
  1393. {
  1394. return NULL;
  1395. }
  1396. aoResultRGB[0] = GetRValue(cc.rgbResult);
  1397. aoResultRGB[1] = GetGValue(cc.rgbResult);
  1398. aoResultRGB[2] = GetBValue(cc.rgbResult);
  1399. RGB2HexW(aoResultRGB, lResultHexRGB);
  1400. if (lHResult == S_OK || lHResult == S_FALSE)
  1401. {
  1402. CoUninitialize();
  1403. }
  1404. return lResultHexRGB;
  1405. }
  1406. static char const * colorChooserWinGui8(
  1407. char const * const aTitle, /* NULL or "" */
  1408. char const * const aDefaultHexRGB, /* NULL or "#FF0000"*/
  1409. unsigned char const aDefaultRGB[3], /* { 0 , 255 , 255 } */
  1410. unsigned char aoResultRGB[3]) /* { 0 , 0 , 0 } */
  1411. {
  1412. static char lResultHexRGB[8];
  1413. wchar_t * lTitle;
  1414. wchar_t * lDefaultHexRGB;
  1415. wchar_t const * lTmpWChar;
  1416. char * lTmpChar;
  1417. lTitle = utf8to16(aTitle);
  1418. lDefaultHexRGB = utf8to16(aDefaultHexRGB);
  1419. lTmpWChar = tinyfd_colorChooserW(
  1420. lTitle,
  1421. lDefaultHexRGB,
  1422. aDefaultRGB,
  1423. aoResultRGB );
  1424. free(lTitle);
  1425. free(lDefaultHexRGB);
  1426. if (!lTmpWChar)
  1427. {
  1428. return NULL;
  1429. }
  1430. lTmpChar = utf16to8(lTmpWChar);
  1431. strcpy(lResultHexRGB, lTmpChar);
  1432. free(lTmpChar);
  1433. return lResultHexRGB;
  1434. }
  1435. static int messageBoxWinGuiA (
  1436. char const * const aTitle , /* NULL or "" */
  1437. char const * const aMessage , /* NULL or "" may contain \n and \t */
  1438. char const * const aDialogType , /* "ok" "okcancel" "yesno" "yesnocancel" */
  1439. char const * const aIconType , /* "info" "warning" "error" "question" */
  1440. int const aDefaultButton ) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */
  1441. {
  1442. int lBoxReturnValue;
  1443. UINT aCode ;
  1444. if ( aIconType && ! strcmp( "warning" , aIconType ) )
  1445. {
  1446. aCode = MB_ICONWARNING ;
  1447. }
  1448. else if ( aIconType && ! strcmp("error", aIconType))
  1449. {
  1450. aCode = MB_ICONERROR ;
  1451. }
  1452. else if ( aIconType && ! strcmp("question", aIconType))
  1453. {
  1454. aCode = MB_ICONQUESTION ;
  1455. }
  1456. else
  1457. {
  1458. aCode = MB_ICONINFORMATION ;
  1459. }
  1460. if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) )
  1461. {
  1462. aCode += MB_OKCANCEL ;
  1463. if ( ! aDefaultButton )
  1464. {
  1465. aCode += MB_DEFBUTTON2 ;
  1466. }
  1467. }
  1468. else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) )
  1469. {
  1470. aCode += MB_YESNO ;
  1471. if ( ! aDefaultButton )
  1472. {
  1473. aCode += MB_DEFBUTTON2 ;
  1474. }
  1475. }
  1476. else if (aDialogType && !strcmp("yesnocancel", aDialogType))
  1477. {
  1478. aCode += MB_YESNOCANCEL;
  1479. if (!aDefaultButton)
  1480. {
  1481. aCode += MB_DEFBUTTON3;
  1482. }
  1483. else if (aDefaultButton == 2)
  1484. {
  1485. aCode += MB_DEFBUTTON2;
  1486. }
  1487. }
  1488. else
  1489. {
  1490. aCode += MB_OK ;
  1491. }
  1492. lBoxReturnValue = MessageBoxA(NULL, aMessage, aTitle, aCode);
  1493. if (((aDialogType && !strcmp("yesnocancel", aDialogType))
  1494. && (lBoxReturnValue == IDNO)))
  1495. {
  1496. return 2;
  1497. }
  1498. if ( ( ( aDialogType
  1499. && strcmp("yesnocancel", aDialogType)
  1500. && strcmp("okcancel", aDialogType)
  1501. && strcmp("yesno", aDialogType)))
  1502. || (lBoxReturnValue == IDOK)
  1503. || (lBoxReturnValue == IDYES) )
  1504. {
  1505. return 1 ;
  1506. }
  1507. else
  1508. {
  1509. return 0 ;
  1510. }
  1511. }
  1512. static char const * saveFileDialogWinGuiA (
  1513. char * const aoBuff ,
  1514. char const * const aTitle , /* NULL or "" */
  1515. char const * const aDefaultPathAndFile , /* NULL or "" */
  1516. int const aNumOfFilterPatterns , /* 0 */
  1517. char const * const * const aFilterPatterns , /* NULL or {"*.jpg","*.png"} */
  1518. char const * const aSingleFilterDescription ) /* NULL or "image files" */
  1519. {
  1520. char lDirname [MAX_PATH_OR_CMD] ;
  1521. char lDialogString[MAX_PATH_OR_CMD];
  1522. char lFilterPatterns[MAX_PATH_OR_CMD] = "";
  1523. int i ;
  1524. char * p;
  1525. char * lRetval;
  1526. HRESULT lHResult;
  1527. OPENFILENAMEA ofn = { 0 };
  1528. lHResult = CoInitializeEx(NULL,0);
  1529. getPathWithoutFinalSlash(lDirname, aDefaultPathAndFile);
  1530. getLastName(aoBuff, aDefaultPathAndFile);
  1531. if (aNumOfFilterPatterns > 0)
  1532. {
  1533. if ( aSingleFilterDescription && strlen(aSingleFilterDescription) )
  1534. {
  1535. strcpy(lFilterPatterns, aSingleFilterDescription);
  1536. strcat(lFilterPatterns, "\n");
  1537. }
  1538. strcat(lFilterPatterns, aFilterPatterns[0]);
  1539. for (i = 1; i < aNumOfFilterPatterns; i++)
  1540. {
  1541. strcat(lFilterPatterns, ";");
  1542. strcat(lFilterPatterns, aFilterPatterns[i]);
  1543. }
  1544. strcat(lFilterPatterns, "\n");
  1545. if ( ! (aSingleFilterDescription && strlen(aSingleFilterDescription) ) )
  1546. {
  1547. strcpy(lDialogString, lFilterPatterns);
  1548. strcat(lFilterPatterns, lDialogString);
  1549. }
  1550. strcat(lFilterPatterns, "All Files\n*.*\n");
  1551. p = lFilterPatterns;
  1552. while ((p = strchr(p, '\n')) != NULL)
  1553. {
  1554. *p = '\0';
  1555. p ++ ;
  1556. }
  1557. }
  1558. ofn.lStructSize = sizeof(OPENFILENAME) ;
  1559. ofn.hwndOwner = 0 ;
  1560. ofn.hInstance = 0 ;
  1561. ofn.lpstrFilter = lFilterPatterns && strlen(lFilterPatterns) ? lFilterPatterns : NULL;
  1562. ofn.lpstrCustomFilter = NULL ;
  1563. ofn.nMaxCustFilter = 0 ;
  1564. ofn.nFilterIndex = 1 ;
  1565. ofn.lpstrFile = aoBuff;
  1566. ofn.nMaxFile = MAX_PATH_OR_CMD ;
  1567. ofn.lpstrFileTitle = NULL ;
  1568. ofn.nMaxFileTitle = _MAX_FNAME + _MAX_EXT ;
  1569. ofn.lpstrInitialDir = lDirname && strlen(lDirname) ? lDirname : NULL;
  1570. ofn.lpstrTitle = aTitle && strlen(aTitle) ? aTitle : NULL;
  1571. ofn.Flags = OFN_OVERWRITEPROMPT | OFN_NOCHANGEDIR ;
  1572. ofn.nFileOffset = 0 ;
  1573. ofn.nFileExtension = 0 ;
  1574. ofn.lpstrDefExt = NULL ;
  1575. ofn.lCustData = 0L ;
  1576. ofn.lpfnHook = NULL ;
  1577. ofn.lpTemplateName = NULL ;
  1578. if ( GetSaveFileNameA ( & ofn ) == 0 )
  1579. {
  1580. lRetval = NULL ;
  1581. }
  1582. else
  1583. {
  1584. lRetval = aoBuff ;
  1585. }
  1586. if (lHResult==S_OK || lHResult==S_FALSE)
  1587. {
  1588. CoUninitialize();
  1589. }
  1590. return lRetval ;
  1591. }
  1592. static char const * openFileDialogWinGuiA (
  1593. char * const aoBuff ,
  1594. char const * const aTitle , /* NULL or "" */
  1595. char const * const aDefaultPathAndFile , /* NULL or "" */
  1596. int const aNumOfFilterPatterns , /* 0 */
  1597. char const * const * const aFilterPatterns , /* NULL or {"*.jpg","*.png"} */
  1598. char const * const aSingleFilterDescription , /* NULL or "image files" */
  1599. int const aAllowMultipleSelects ) /* 0 or 1 */
  1600. {
  1601. char lDirname [MAX_PATH_OR_CMD] ;
  1602. char lFilterPatterns[MAX_PATH_OR_CMD] = "";
  1603. char lDialogString[MAX_PATH_OR_CMD] ;
  1604. char * lPointers[MAX_MULTIPLE_FILES];
  1605. size_t lLengths[MAX_MULTIPLE_FILES];
  1606. int i , j ;
  1607. char * p;
  1608. size_t lBuffLen ;
  1609. char * lRetval;
  1610. HRESULT lHResult;
  1611. OPENFILENAMEA ofn = {0};
  1612. lHResult = CoInitializeEx(NULL,0);
  1613. getPathWithoutFinalSlash(lDirname, aDefaultPathAndFile);
  1614. getLastName(aoBuff, aDefaultPathAndFile);
  1615. if (aNumOfFilterPatterns > 0)
  1616. {
  1617. if ( aSingleFilterDescription && strlen(aSingleFilterDescription) )
  1618. {
  1619. strcpy(lFilterPatterns, aSingleFilterDescription);
  1620. strcat(lFilterPatterns, "\n");
  1621. }
  1622. strcat(lFilterPatterns, aFilterPatterns[0]);
  1623. for (i = 1; i < aNumOfFilterPatterns; i++)
  1624. {
  1625. strcat(lFilterPatterns, ";");
  1626. strcat(lFilterPatterns, aFilterPatterns[i]);
  1627. }
  1628. strcat(lFilterPatterns, "\n");
  1629. if ( ! (aSingleFilterDescription && strlen(aSingleFilterDescription) ) )
  1630. {
  1631. strcpy(lDialogString, lFilterPatterns);
  1632. strcat(lFilterPatterns, lDialogString);
  1633. }
  1634. strcat(lFilterPatterns, "All Files\n*.*\n");
  1635. p = lFilterPatterns;
  1636. while ((p = strchr(p, '\n')) != NULL)
  1637. {
  1638. *p = '\0';
  1639. p ++ ;
  1640. }
  1641. }
  1642. ofn.lStructSize = sizeof ( OPENFILENAME ) ;
  1643. ofn.hwndOwner = 0 ;
  1644. ofn.hInstance = 0 ;
  1645. ofn.lpstrFilter = lFilterPatterns && strlen(lFilterPatterns) ? lFilterPatterns : NULL;
  1646. ofn.lpstrCustomFilter = NULL ;
  1647. ofn.nMaxCustFilter = 0 ;
  1648. ofn.nFilterIndex = 1 ;
  1649. ofn.lpstrFile = aoBuff ;
  1650. ofn.nMaxFile = MAX_PATH_OR_CMD ;
  1651. ofn.lpstrFileTitle = NULL ;
  1652. ofn.nMaxFileTitle = _MAX_FNAME + _MAX_EXT ;
  1653. ofn.lpstrInitialDir = lDirname && strlen(lDirname) ? lDirname : NULL;
  1654. ofn.lpstrTitle = aTitle && strlen(aTitle) ? aTitle : NULL;
  1655. ofn.Flags = OFN_EXPLORER | OFN_NOCHANGEDIR ;
  1656. ofn.nFileOffset = 0 ;
  1657. ofn.nFileExtension = 0 ;
  1658. ofn.lpstrDefExt = NULL ;
  1659. ofn.lCustData = 0L ;
  1660. ofn.lpfnHook = NULL ;
  1661. ofn.lpTemplateName = NULL ;
  1662. if ( aAllowMultipleSelects )
  1663. {
  1664. ofn.Flags |= OFN_ALLOWMULTISELECT;
  1665. }
  1666. if ( GetOpenFileNameA ( & ofn ) == 0 )
  1667. {
  1668. lRetval = NULL ;
  1669. }
  1670. else
  1671. {
  1672. lBuffLen = strlen(aoBuff) ;
  1673. lPointers[0] = aoBuff + lBuffLen + 1 ;
  1674. if ( !aAllowMultipleSelects || (lPointers[0][0] == '\0') )
  1675. {
  1676. lRetval = aoBuff ;
  1677. }
  1678. else
  1679. {
  1680. i = 0 ;
  1681. do
  1682. {
  1683. lLengths[i] = strlen(lPointers[i]);
  1684. lPointers[i+1] = lPointers[i] + lLengths[i] + 1 ;
  1685. i ++ ;
  1686. }
  1687. while ( lPointers[i][0] != '\0' );
  1688. i--;
  1689. p = aoBuff + MAX_MULTIPLE_FILES*MAX_PATH_OR_CMD - 1 ;
  1690. * p = '\0';
  1691. for ( j = i ; j >=0 ; j-- )
  1692. {
  1693. p -= lLengths[j];
  1694. memmove(p, lPointers[j], lLengths[j]);
  1695. p--;
  1696. *p = '\\';
  1697. p -= lBuffLen ;
  1698. memmove(p, aoBuff, lBuffLen);
  1699. p--;
  1700. *p = '|';
  1701. }
  1702. p++;
  1703. lRetval = p ;
  1704. }
  1705. }
  1706. if (lHResult==S_OK || lHResult==S_FALSE)
  1707. {
  1708. CoUninitialize();
  1709. }
  1710. return lRetval;
  1711. }
  1712. #ifndef TINYFD_NOSELECTFOLDERWIN
  1713. static char const * selectFolderDialogWinGuiA (
  1714. char * const aoBuff ,
  1715. char const * const aTitle , /* NULL or "" */
  1716. char const * const aDefaultPath ) /* NULL or "" */
  1717. {
  1718. BROWSEINFOA bInfo ;
  1719. LPITEMIDLIST lpItem ;
  1720. HRESULT lHResult;
  1721. lHResult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
  1722. /* we can't use aDefaultPath */
  1723. bInfo.hwndOwner = 0 ;
  1724. bInfo.pidlRoot = NULL ;
  1725. bInfo.pszDisplayName = aoBuff ;
  1726. bInfo.lpszTitle = aTitle && strlen(aTitle) ? aTitle : NULL;
  1727. if (lHResult == S_OK || lHResult == S_FALSE)
  1728. {
  1729. bInfo.ulFlags = BIF_USENEWUI;
  1730. }
  1731. bInfo.lpfn = BrowseCallbackProc;
  1732. bInfo.lParam = (LPARAM)aDefaultPath;
  1733. bInfo.iImage = -1 ;
  1734. lpItem = SHBrowseForFolderA ( & bInfo ) ;
  1735. if ( lpItem )
  1736. {
  1737. SHGetPathFromIDListA ( lpItem , aoBuff ) ;
  1738. }
  1739. if (lHResult==S_OK || lHResult==S_FALSE)
  1740. {
  1741. CoUninitialize();
  1742. }
  1743. return aoBuff ;
  1744. }
  1745. #endif /*TINYFD_NOSELECTFOLDERWIN*/
  1746. static char const * colorChooserWinGuiA(
  1747. char const * const aTitle, /* NULL or "" */
  1748. char const * const aDefaultHexRGB, /* NULL or "#FF0000"*/
  1749. unsigned char const aDefaultRGB[3], /* { 0 , 255 , 255 } */
  1750. unsigned char aoResultRGB[3]) /* { 0 , 0 , 0 } */
  1751. {
  1752. static char lResultHexRGB[8];
  1753. CHOOSECOLORA cc;
  1754. COLORREF crCustColors[16];
  1755. unsigned char lDefaultRGB[3];
  1756. int lRet;
  1757. if ( aDefaultHexRGB )
  1758. {
  1759. Hex2RGB(aDefaultHexRGB, lDefaultRGB);
  1760. }
  1761. else
  1762. {
  1763. lDefaultRGB[0]=aDefaultRGB[0];
  1764. lDefaultRGB[1]=aDefaultRGB[1];
  1765. lDefaultRGB[2]=aDefaultRGB[2];
  1766. }
  1767. /* we can't use aTitle */
  1768. cc.lStructSize = sizeof ( CHOOSECOLOR ) ;
  1769. cc.hwndOwner = NULL ;
  1770. cc.hInstance = NULL ;
  1771. cc.rgbResult = RGB(lDefaultRGB[0], lDefaultRGB[1], lDefaultRGB[2]);
  1772. cc.lpCustColors = crCustColors;
  1773. cc.Flags = CC_RGBINIT | CC_FULLOPEN;
  1774. cc.lCustData = 0;
  1775. cc.lpfnHook = NULL;
  1776. cc.lpTemplateName = NULL;
  1777. lRet = ChooseColorA(&cc);
  1778. if ( ! lRet )
  1779. {
  1780. return NULL;
  1781. }
  1782. aoResultRGB[0] = GetRValue(cc.rgbResult);
  1783. aoResultRGB[1] = GetGValue(cc.rgbResult);
  1784. aoResultRGB[2] = GetBValue(cc.rgbResult);
  1785. RGB2Hex(aoResultRGB, lResultHexRGB);
  1786. return lResultHexRGB;
  1787. }
  1788. #endif /* TINYFD_NOLIB */
  1789. static int dialogPresent ( )
  1790. {
  1791. static int lDialogPresent = -1 ;
  1792. char lBuff [MAX_PATH_OR_CMD] ;
  1793. FILE * lIn ;
  1794. char const * lString = "dialog.exe";
  1795. if ( lDialogPresent < 0 )
  1796. {
  1797. if (!(lIn = _popen("where dialog.exe","r")))
  1798. {
  1799. lDialogPresent = 0 ;
  1800. return 0 ;
  1801. }
  1802. while ( fgets ( lBuff , sizeof ( lBuff ) , lIn ) != NULL )
  1803. {}
  1804. _pclose ( lIn ) ;
  1805. if ( lBuff[strlen ( lBuff ) -1] == '\n' )
  1806. {
  1807. lBuff[strlen ( lBuff ) -1] = '\0' ;
  1808. }
  1809. if ( strcmp(lBuff+strlen(lBuff)-strlen(lString),lString) )
  1810. {
  1811. lDialogPresent = 0 ;
  1812. }
  1813. else
  1814. {
  1815. lDialogPresent = 1 ;
  1816. }
  1817. }
  1818. return lDialogPresent;
  1819. }
  1820. static int messageBoxWinConsole (
  1821. char const * const aTitle , /* NULL or "" */
  1822. char const * const aMessage , /* NULL or "" may contain \n and \t */
  1823. char const * const aDialogType , /* "ok" "okcancel" "yesno" "yesnocancel" */
  1824. char const * const aIconType , /* "info" "warning" "error" "question" */
  1825. int const aDefaultButton ) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */
  1826. {
  1827. char lDialogString[MAX_PATH_OR_CMD];
  1828. char lDialogFile[MAX_PATH_OR_CMD];
  1829. FILE * lIn;
  1830. char lBuff [MAX_PATH_OR_CMD] = "";
  1831. strcpy ( lDialogString , "dialog " ) ;
  1832. if ( aTitle && strlen(aTitle) )
  1833. {
  1834. strcat(lDialogString, "--title \"") ;
  1835. strcat(lDialogString, aTitle) ;
  1836. strcat(lDialogString, "\" ") ;
  1837. }
  1838. if ( aDialogType && ( !strcmp( "okcancel" , aDialogType )
  1839. || !strcmp("yesno", aDialogType) || !strcmp("yesnocancel", aDialogType) ) )
  1840. {
  1841. strcat(lDialogString, "--backtitle \"") ;
  1842. strcat(lDialogString, "tab: move focus") ;
  1843. strcat(lDialogString, "\" ") ;
  1844. }
  1845. if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) )
  1846. {
  1847. if ( ! aDefaultButton )
  1848. {
  1849. strcat ( lDialogString , "--defaultno " ) ;
  1850. }
  1851. strcat ( lDialogString ,
  1852. "--yes-label \"Ok\" --no-label \"Cancel\" --yesno " ) ;
  1853. }
  1854. else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) )
  1855. {
  1856. if ( ! aDefaultButton )
  1857. {
  1858. strcat ( lDialogString , "--defaultno " ) ;
  1859. }
  1860. strcat ( lDialogString , "--yesno " ) ;
  1861. }
  1862. else if (aDialogType && !strcmp("yesnocancel", aDialogType))
  1863. {
  1864. if (!aDefaultButton)
  1865. {
  1866. strcat(lDialogString, "--defaultno ");
  1867. }
  1868. strcat(lDialogString, "--menu ");
  1869. }
  1870. else
  1871. {
  1872. strcat ( lDialogString , "--msgbox " ) ;
  1873. }
  1874. strcat ( lDialogString , "\"" ) ;
  1875. if ( aMessage && strlen(aMessage) )
  1876. {
  1877. replaceSubStr ( aMessage , "\n" , "\\n" , lBuff ) ;
  1878. strcat(lDialogString, lBuff) ;
  1879. lBuff[0]='\0';
  1880. }
  1881. strcat(lDialogString, "\" ");
  1882. if (aDialogType && !strcmp("yesnocancel", aDialogType))
  1883. {
  1884. strcat(lDialogString, "0 60 0 Yes \"\" No \"\"");
  1885. strcat(lDialogString, "2>>");
  1886. }
  1887. else
  1888. {
  1889. strcat(lDialogString, "10 60");
  1890. strcat(lDialogString, " && echo 1 > ");
  1891. }
  1892. strcpy(lDialogFile, getenv("USERPROFILE"));
  1893. strcat(lDialogFile, "\\AppData\\Local\\Temp\\tinyfd.txt");
  1894. strcat(lDialogString, lDialogFile);
  1895. /*if (tinyfd_verbose) printf ( "lDialogString: %s\n" , lDialogString ) ;*/
  1896. system ( lDialogString ) ;
  1897. if (!(lIn = fopen(lDialogFile, "r")))
  1898. {
  1899. remove(lDialogFile);
  1900. return 0 ;
  1901. }
  1902. while (fgets(lBuff, sizeof(lBuff), lIn) != NULL)
  1903. {}
  1904. fclose(lIn);
  1905. remove(lDialogFile);
  1906. if ( lBuff[strlen ( lBuff ) -1] == '\n' )
  1907. {
  1908. lBuff[strlen ( lBuff ) -1] = '\0' ;
  1909. }
  1910. /* if (tinyfd_verbose) printf("lBuff: %s\n", lBuff); */
  1911. if ( ! strlen(lBuff) )
  1912. {
  1913. return 0;
  1914. }
  1915. if (aDialogType && !strcmp("yesnocancel", aDialogType))
  1916. {
  1917. if (lBuff[0] == 'Y') return 1;
  1918. else return 2;
  1919. }
  1920. return 1;
  1921. }
  1922. static char const * inputBoxWinConsole(
  1923. char * const aoBuff ,
  1924. char const * const aTitle , /* NULL or "" */
  1925. char const * const aMessage , /* NULL or "" may NOT contain \n nor \t */
  1926. char const * const aDefaultInput ) /* "" , if NULL it's a passwordBox */
  1927. {
  1928. char lDialogString[MAX_PATH_OR_CMD];
  1929. char lDialogFile[MAX_PATH_OR_CMD];
  1930. FILE * lIn;
  1931. int lResult;
  1932. strcpy(lDialogFile, getenv("USERPROFILE"));
  1933. strcat(lDialogFile, "\\AppData\\Local\\Temp\\tinyfd.txt");
  1934. strcpy(lDialogString , "echo|set /p=1 >" ) ;
  1935. strcat(lDialogString, lDialogFile);
  1936. strcat( lDialogString , " & " ) ;
  1937. strcat ( lDialogString , "dialog " ) ;
  1938. if ( aTitle && strlen(aTitle) )
  1939. {
  1940. strcat(lDialogString, "--title \"") ;
  1941. strcat(lDialogString, aTitle) ;
  1942. strcat(lDialogString, "\" ") ;
  1943. }
  1944. strcat(lDialogString, "--backtitle \"") ;
  1945. strcat(lDialogString, "tab: move focus") ;
  1946. if ( ! aDefaultInput )
  1947. {
  1948. strcat(lDialogString, " (sometimes nothing, no blink nor star, is shown in text field)") ;
  1949. }
  1950. strcat(lDialogString, "\" ") ;
  1951. if ( ! aDefaultInput )
  1952. {
  1953. strcat ( lDialogString , "--insecure --passwordbox" ) ;
  1954. }
  1955. else
  1956. {
  1957. strcat ( lDialogString , "--inputbox" ) ;
  1958. }
  1959. strcat ( lDialogString , " \"" ) ;
  1960. if ( aMessage && strlen(aMessage) )
  1961. {
  1962. strcat(lDialogString, aMessage) ;
  1963. }
  1964. strcat(lDialogString,"\" 10 60 ") ;
  1965. if ( aDefaultInput && strlen(aDefaultInput) )
  1966. {
  1967. strcat(lDialogString, "\"") ;
  1968. strcat(lDialogString, aDefaultInput) ;
  1969. strcat(lDialogString, "\" ") ;
  1970. }
  1971. strcat(lDialogString, "2>>");
  1972. strcpy(lDialogFile, getenv("USERPROFILE"));
  1973. strcat(lDialogFile, "\\AppData\\Local\\Temp\\tinyfd.txt");
  1974. strcat(lDialogString, lDialogFile);
  1975. strcat(lDialogString, " || echo 0 > ");
  1976. strcat(lDialogString, lDialogFile);
  1977. /* printf ( "lDialogString: %s\n" , lDialogString ) ; */
  1978. system ( lDialogString ) ;
  1979. if (!(lIn = fopen(lDialogFile, "r")))
  1980. {
  1981. remove(lDialogFile);
  1982. return 0 ;
  1983. }
  1984. while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL)
  1985. {}
  1986. fclose(lIn);
  1987. wipefile(lDialogFile);
  1988. remove(lDialogFile);
  1989. if ( aoBuff[strlen ( aoBuff ) -1] == '\n' )
  1990. {
  1991. aoBuff[strlen ( aoBuff ) -1] = '\0' ;
  1992. }
  1993. /* printf ( "aoBuff: %s\n" , aoBuff ) ; */
  1994. /* printf ( "aoBuff: %s len: %lu \n" , aoBuff , strlen(aoBuff) ) ; */
  1995. lResult = strncmp ( aoBuff , "1" , 1) ? 0 : 1 ;
  1996. /* printf ( "lResult: %d \n" , lResult ) ; */
  1997. if ( ! lResult )
  1998. {
  1999. return NULL ;
  2000. }
  2001. /* printf ( "aoBuff+1: %s\n" , aoBuff+1 ) ; */
  2002. return aoBuff+3 ;
  2003. }
  2004. static char const * saveFileDialogWinConsole (
  2005. char * const aoBuff ,
  2006. char const * const aTitle , /* NULL or "" */
  2007. char const * const aDefaultPathAndFile ) /* NULL or "" */
  2008. {
  2009. char lDialogString[MAX_PATH_OR_CMD];
  2010. char lPathAndFile[MAX_PATH_OR_CMD] = "";
  2011. FILE * lIn;
  2012. strcpy ( lDialogString , "dialog " ) ;
  2013. if ( aTitle && strlen(aTitle) )
  2014. {
  2015. strcat(lDialogString, "--title \"") ;
  2016. strcat(lDialogString, aTitle) ;
  2017. strcat(lDialogString, "\" ") ;
  2018. }
  2019. strcat(lDialogString, "--backtitle \"") ;
  2020. strcat(lDialogString,
  2021. "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ;
  2022. strcat(lDialogString, "\" ") ;
  2023. strcat ( lDialogString , "--fselect \"" ) ;
  2024. if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) )
  2025. {
  2026. /* dialog.exe uses unix separators even on windows */
  2027. strcpy(lPathAndFile, aDefaultPathAndFile);
  2028. replaceChr ( lPathAndFile , '\\' , '/' ) ;
  2029. }
  2030. /* dialog.exe needs at least one separator */
  2031. if ( ! strchr(lPathAndFile, '/') )
  2032. {
  2033. strcat(lDialogString, "./") ;
  2034. }
  2035. strcat(lDialogString, lPathAndFile) ;
  2036. strcat(lDialogString, "\" 0 60 2>");
  2037. strcpy(lPathAndFile, getenv("USERPROFILE"));
  2038. strcat(lPathAndFile, "\\AppData\\Local\\Temp\\tinyfd.txt");
  2039. strcat(lDialogString, lPathAndFile);
  2040. /* printf ( "lDialogString: %s\n" , lDialogString ) ; */
  2041. system ( lDialogString ) ;
  2042. if (!(lIn = fopen(lPathAndFile, "r")))
  2043. {
  2044. remove(lPathAndFile);
  2045. return NULL;
  2046. }
  2047. while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL)
  2048. {}
  2049. fclose(lIn);
  2050. remove(lPathAndFile);
  2051. replaceChr ( aoBuff , '/' , '\\' ) ;
  2052. /* printf ( "aoBuff: %s\n" , aoBuff ) ; */
  2053. getLastName(lDialogString,aoBuff);
  2054. if ( ! strlen(lDialogString) )
  2055. {
  2056. return NULL;
  2057. }
  2058. return aoBuff;
  2059. }
  2060. static char const * openFileDialogWinConsole (
  2061. char * const aoBuff ,
  2062. char const * const aTitle , /* NULL or "" */
  2063. char const * const aDefaultPathAndFile , /* NULL or "" */
  2064. int const aAllowMultipleSelects ) /* 0 or 1 */
  2065. {
  2066. char lFilterPatterns[MAX_PATH_OR_CMD] = "";
  2067. char lDialogString[MAX_PATH_OR_CMD] ;
  2068. FILE * lIn;
  2069. strcpy ( lDialogString , "dialog " ) ;
  2070. if ( aTitle && strlen(aTitle) )
  2071. {
  2072. strcat(lDialogString, "--title \"") ;
  2073. strcat(lDialogString, aTitle) ;
  2074. strcat(lDialogString, "\" ") ;
  2075. }
  2076. strcat(lDialogString, "--backtitle \"") ;
  2077. strcat(lDialogString,
  2078. "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ;
  2079. strcat(lDialogString, "\" ") ;
  2080. strcat ( lDialogString , "--fselect \"" ) ;
  2081. if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) )
  2082. {
  2083. /* dialog.exe uses unix separators even on windows */
  2084. strcpy(lFilterPatterns, aDefaultPathAndFile);
  2085. replaceChr ( lFilterPatterns , '\\' , '/' ) ;
  2086. }
  2087. /* dialog.exe needs at least one separator */
  2088. if ( ! strchr(lFilterPatterns, '/') )
  2089. {
  2090. strcat(lDialogString, "./") ;
  2091. }
  2092. strcat(lDialogString, lFilterPatterns) ;
  2093. strcat(lDialogString, "\" 0 60 2>");
  2094. strcpy(lFilterPatterns, getenv("USERPROFILE"));
  2095. strcat(lFilterPatterns, "\\AppData\\Local\\Temp\\tinyfd.txt");
  2096. strcat(lDialogString, lFilterPatterns);
  2097. /* printf ( "lDialogString: %s\n" , lDialogString ) ; */
  2098. system ( lDialogString ) ;
  2099. if (!(lIn = fopen(lFilterPatterns, "r")))
  2100. {
  2101. remove(lFilterPatterns);
  2102. return NULL;
  2103. }
  2104. while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL)
  2105. {}
  2106. fclose(lIn);
  2107. remove(lFilterPatterns);
  2108. replaceChr ( aoBuff , '/' , '\\' ) ;
  2109. /* printf ( "aoBuff: %s\n" , aoBuff ) ; */
  2110. return aoBuff;
  2111. }
  2112. static char const * selectFolderDialogWinConsole (
  2113. char * const aoBuff ,
  2114. char const * const aTitle , /* NULL or "" */
  2115. char const * const aDefaultPath ) /* NULL or "" */
  2116. {
  2117. char lDialogString [MAX_PATH_OR_CMD] ;
  2118. char lString [MAX_PATH_OR_CMD] ;
  2119. FILE * lIn ;
  2120. strcpy ( lDialogString , "dialog " ) ;
  2121. if ( aTitle && strlen(aTitle) )
  2122. {
  2123. strcat(lDialogString, "--title \"") ;
  2124. strcat(lDialogString, aTitle) ;
  2125. strcat(lDialogString, "\" ") ;
  2126. }
  2127. strcat(lDialogString, "--backtitle \"") ;
  2128. strcat(lDialogString,
  2129. "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ;
  2130. strcat(lDialogString, "\" ") ;
  2131. strcat ( lDialogString , "--dselect \"" ) ;
  2132. if ( aDefaultPath && strlen(aDefaultPath) )
  2133. {
  2134. /* dialog.exe uses unix separators even on windows */
  2135. strcpy(lString, aDefaultPath) ;
  2136. ensureFinalSlash(lString);
  2137. replaceChr ( lString , '\\' , '/' ) ;
  2138. strcat(lDialogString, lString) ;
  2139. }
  2140. else
  2141. {
  2142. /* dialog.exe needs at least one separator */
  2143. strcat(lDialogString, "./") ;
  2144. }
  2145. strcat(lDialogString, "\" 0 60 2>");
  2146. strcpy(lString, getenv("USERPROFILE"));
  2147. strcat(lString, "\\AppData\\Local\\Temp\\tinyfd.txt");
  2148. strcat(lDialogString, lString);
  2149. /* printf ( "lDialogString: %s\n" , lDialogString ) ; */
  2150. system ( lDialogString ) ;
  2151. if (!(lIn = fopen(lString, "r")))
  2152. {
  2153. remove(lString);
  2154. return NULL;
  2155. }
  2156. while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL)
  2157. {}
  2158. fclose(lIn);
  2159. remove(lString);
  2160. replaceChr ( aoBuff , '/' , '\\' ) ;
  2161. /* printf ( "aoBuff: %s\n" , aoBuff ) ; */
  2162. return aoBuff;
  2163. }
  2164. int tinyfd_messageBox (
  2165. char const * const aTitle , /* NULL or "" */
  2166. char const * const aMessage , /* NULL or "" may contain \n and \t */
  2167. char const * const aDialogType , /* "ok" "okcancel" "yesno" "yesnocancel" */
  2168. char const * const aIconType , /* "info" "warning" "error" "question" */
  2169. int const aDefaultButton ) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */
  2170. {
  2171. char lChar ;
  2172. #ifndef TINYFD_NOLIB
  2173. if ((!tinyfd_forceConsole || !(GetConsoleWindow() || dialogPresent()))
  2174. && (!getenv("SSH_CLIENT") || getenv("DISPLAY")))
  2175. {
  2176. if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "windows"); return 1; }
  2177. if (tinyfd_winUtf8)
  2178. {
  2179. return messageBoxWinGui8(
  2180. aTitle, aMessage, aDialogType, aIconType, aDefaultButton);
  2181. }
  2182. else
  2183. {
  2184. return messageBoxWinGuiA(
  2185. aTitle, aMessage, aDialogType, aIconType, aDefaultButton);
  2186. }
  2187. }
  2188. else
  2189. #endif /* TINYFD_NOLIB */
  2190. if ( dialogPresent() )
  2191. {
  2192. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return 0;}
  2193. return messageBoxWinConsole(
  2194. aTitle,aMessage,aDialogType,aIconType,aDefaultButton);
  2195. }
  2196. else
  2197. {
  2198. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return 0;}
  2199. if (!gWarningDisplayed && !tinyfd_forceConsole )
  2200. {
  2201. gWarningDisplayed = 1;
  2202. printf("\n\n%s\n", gTitle);
  2203. printf("%s\n\n", gMessageWin);
  2204. }
  2205. if ( aTitle && strlen(aTitle) )
  2206. {
  2207. printf ("\n%s\n\n", aTitle);
  2208. }
  2209. if ( aDialogType && !strcmp("yesno",aDialogType) )
  2210. {
  2211. do
  2212. {
  2213. if ( aMessage && strlen(aMessage) )
  2214. {
  2215. printf("%s\n",aMessage);
  2216. }
  2217. printf("y/n: ");
  2218. lChar = (char) tolower ( _getch() ) ;
  2219. printf("\n\n");
  2220. }
  2221. while ( lChar != 'y' && lChar != 'n' ) ;
  2222. return lChar == 'y' ? 1 : 0 ;
  2223. }
  2224. else if ( aDialogType && !strcmp("okcancel",aDialogType) )
  2225. {
  2226. do
  2227. {
  2228. if ( aMessage && strlen(aMessage) )
  2229. {
  2230. printf("%s\n",aMessage);
  2231. }
  2232. printf("[O]kay/[C]ancel: ");
  2233. lChar = (char) tolower ( _getch() ) ;
  2234. printf("\n\n");
  2235. }
  2236. while ( lChar != 'o' && lChar != 'c' ) ;
  2237. return lChar == 'o' ? 1 : 0 ;
  2238. }
  2239. else if (aDialogType && !strcmp("yesnocancel", aDialogType))
  2240. {
  2241. do
  2242. {
  2243. if (aMessage && strlen(aMessage))
  2244. {
  2245. printf("%s\n", aMessage);
  2246. }
  2247. printf("[Y]es/[N]o/[C]ancel: ");
  2248. lChar = (char)tolower(_getch());
  2249. printf("\n\n");
  2250. } while (lChar != 'y' && lChar != 'n' && lChar != 'c');
  2251. return (lChar == 'y') ? 1 : (lChar == 'n') ? 2 : 0 ;
  2252. }
  2253. else
  2254. {
  2255. if ( aMessage && strlen(aMessage) )
  2256. {
  2257. printf("%s\n\n",aMessage);
  2258. }
  2259. printf("press enter to continue ");
  2260. lChar = (char) _getch() ;
  2261. printf("\n\n");
  2262. return 1 ;
  2263. }
  2264. }
  2265. }
  2266. /* returns NULL on cancel */
  2267. char const * tinyfd_inputBox(
  2268. char const * const aTitle , /* NULL or "" */
  2269. char const * const aMessage , /* NULL or "" may NOT contain \n nor \t */
  2270. char const * const aDefaultInput ) /* "" , if NULL it's a passwordBox */
  2271. {
  2272. static char lBuff [MAX_PATH_OR_CMD] ;
  2273. char * lEOF;
  2274. #ifndef TINYFD_NOLIB
  2275. DWORD mode = 0;
  2276. HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
  2277. if ((!tinyfd_forceConsole || !(
  2278. GetConsoleWindow() ||
  2279. dialogPresent()))
  2280. && ( !getenv("SSH_CLIENT") || getenv("DISPLAY") ) )
  2281. {
  2282. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char const *)1;}
  2283. lBuff[0]='\0';
  2284. return inputBoxWinGui(lBuff,aTitle,aMessage,aDefaultInput);
  2285. }
  2286. else
  2287. #endif /* TINYFD_NOLIB */
  2288. if ( dialogPresent() )
  2289. {
  2290. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;}
  2291. lBuff[0]='\0';
  2292. return inputBoxWinConsole(lBuff,aTitle,aMessage,aDefaultInput);
  2293. }
  2294. else
  2295. {
  2296. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return (char const *)0;}
  2297. lBuff[0]='\0';
  2298. if (!gWarningDisplayed && !tinyfd_forceConsole)
  2299. {
  2300. gWarningDisplayed = 1 ;
  2301. printf("\n\n%s\n", gTitle);
  2302. printf("%s\n\n", gMessageWin);
  2303. }
  2304. if ( aTitle && strlen(aTitle) )
  2305. {
  2306. printf ("\n%s\n\n", aTitle);
  2307. }
  2308. if ( aMessage && strlen(aMessage) )
  2309. {
  2310. printf("%s\n",aMessage);
  2311. }
  2312. printf("(ctrl-Z + enter to cancel): ");
  2313. #ifndef TINYFD_NOLIB
  2314. if ( ! aDefaultInput )
  2315. {
  2316. GetConsoleMode(hStdin,&mode);
  2317. SetConsoleMode(hStdin,mode & (~ENABLE_ECHO_INPUT) );
  2318. }
  2319. #endif /* TINYFD_NOLIB */
  2320. lEOF = fgets(lBuff, MAX_PATH_OR_CMD, stdin);
  2321. if ( ! lEOF )
  2322. {
  2323. return NULL;
  2324. }
  2325. #ifndef TINYFD_NOLIB
  2326. if ( ! aDefaultInput )
  2327. {
  2328. SetConsoleMode(hStdin,mode);
  2329. printf ("\n");
  2330. }
  2331. #endif /* TINYFD_NOLIB */
  2332. printf ("\n");
  2333. if ( strchr(lBuff,27) )
  2334. {
  2335. return NULL ;
  2336. }
  2337. if ( lBuff[strlen ( lBuff ) -1] == '\n' )
  2338. {
  2339. lBuff[strlen ( lBuff ) -1] = '\0' ;
  2340. }
  2341. return lBuff ;
  2342. }
  2343. }
  2344. char const * tinyfd_saveFileDialog (
  2345. char const * const aTitle , /* NULL or "" */
  2346. char const * const aDefaultPathAndFile , /* NULL or "" */
  2347. int const aNumOfFilterPatterns , /* 0 */
  2348. char const * const * const aFilterPatterns , /* NULL or {"*.jpg","*.png"} */
  2349. char const * const aSingleFilterDescription ) /* NULL or "image files" */
  2350. {
  2351. static char lBuff [MAX_PATH_OR_CMD] ;
  2352. char lString[MAX_PATH_OR_CMD] ;
  2353. char const * p ;
  2354. lBuff[0]='\0';
  2355. #ifndef TINYFD_NOLIB
  2356. if ( ( !tinyfd_forceConsole || !( GetConsoleWindow() || dialogPresent() ) )
  2357. && ( !getenv("SSH_CLIENT") || getenv("DISPLAY") ) )
  2358. {
  2359. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char const *)1;}
  2360. if (tinyfd_winUtf8)
  2361. {
  2362. p = saveFileDialogWinGui8(lBuff,
  2363. aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription);
  2364. }
  2365. else
  2366. {
  2367. p = saveFileDialogWinGuiA(lBuff,
  2368. aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription);
  2369. }
  2370. }
  2371. else
  2372. #endif /* TINYFD_NOLIB */
  2373. if ( dialogPresent() )
  2374. {
  2375. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;}
  2376. p = saveFileDialogWinConsole(lBuff,aTitle,aDefaultPathAndFile);
  2377. }
  2378. else
  2379. {
  2380. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return (char const *)0;}
  2381. p = tinyfd_inputBox(aTitle, "Save file","");
  2382. }
  2383. if ( ! p || ! strlen ( p ) )
  2384. {
  2385. return NULL;
  2386. }
  2387. getPathWithoutFinalSlash ( lString , p ) ;
  2388. if ( strlen ( lString ) && ! dirExists ( lString ) )
  2389. {
  2390. return NULL ;
  2391. }
  2392. getLastName(lString,p);
  2393. if ( ! filenameValid(lString) )
  2394. {
  2395. return NULL;
  2396. }
  2397. return p ;
  2398. }
  2399. /* in case of multiple files, the separator is | */
  2400. char const * tinyfd_openFileDialog (
  2401. char const * const aTitle , /* NULL or "" */
  2402. char const * const aDefaultPathAndFile , /* NULL or "" */
  2403. int const aNumOfFilterPatterns , /* 0 */
  2404. char const * const * const aFilterPatterns , /* NULL or {"*.jpg","*.png"} */
  2405. char const * const aSingleFilterDescription , /* NULL or "image files" */
  2406. int const aAllowMultipleSelects ) /* 0 or 1 */
  2407. {
  2408. static char lBuff[MAX_MULTIPLE_FILES*MAX_PATH_OR_CMD];
  2409. char const * p ;
  2410. #ifndef TINYFD_NOLIB
  2411. if ( ( !tinyfd_forceConsole || !( GetConsoleWindow() || dialogPresent() ) )
  2412. && ( !getenv("SSH_CLIENT") || getenv("DISPLAY") ) )
  2413. {
  2414. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char const *)1;}
  2415. if (tinyfd_winUtf8)
  2416. {
  2417. p = openFileDialogWinGui8(lBuff,
  2418. aTitle, aDefaultPathAndFile, aNumOfFilterPatterns,
  2419. aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects);
  2420. }
  2421. else
  2422. {
  2423. p = openFileDialogWinGuiA(lBuff,
  2424. aTitle, aDefaultPathAndFile, aNumOfFilterPatterns,
  2425. aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects);
  2426. }
  2427. }
  2428. else
  2429. #endif /* TINYFD_NOLIB */
  2430. if ( dialogPresent() )
  2431. {
  2432. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;}
  2433. p = openFileDialogWinConsole(lBuff,
  2434. aTitle,aDefaultPathAndFile,aAllowMultipleSelects);
  2435. }
  2436. else
  2437. {
  2438. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return (char const *)0;}
  2439. p = tinyfd_inputBox(aTitle, "Open file","");
  2440. }
  2441. if ( ! p || ! strlen ( p ) )
  2442. {
  2443. return NULL;
  2444. }
  2445. if ( aAllowMultipleSelects && strchr(p, '|') )
  2446. {
  2447. p = ensureFilesExist( lBuff , p ) ;
  2448. }
  2449. else if ( ! fileExists (p) )
  2450. {
  2451. return NULL ;
  2452. }
  2453. /* printf ( "lBuff3: %s\n" , p ) ; */
  2454. return p ;
  2455. }
  2456. char const * tinyfd_selectFolderDialog (
  2457. char const * const aTitle , /* NULL or "" */
  2458. char const * const aDefaultPath ) /* NULL or "" */
  2459. {
  2460. static char lBuff [MAX_PATH_OR_CMD] ;
  2461. char const * p ;
  2462. #ifndef TINYFD_NOLIB
  2463. if ( ( !tinyfd_forceConsole || !( GetConsoleWindow() || dialogPresent() ) )
  2464. && ( !getenv("SSH_CLIENT") || getenv("DISPLAY") ) )
  2465. {
  2466. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char const *)1;}
  2467. if (tinyfd_winUtf8)
  2468. {
  2469. #ifndef TINYFD_NOSELECTFOLDERWIN
  2470. p = selectFolderDialogWinGui8(lBuff, aTitle, aDefaultPath);
  2471. }
  2472. else
  2473. {
  2474. p = selectFolderDialogWinGuiA(lBuff, aTitle, aDefaultPath);
  2475. #endif /*TINYFD_NOSELECTFOLDERWIN*/
  2476. }
  2477. }
  2478. else
  2479. #endif /* TINYFD_NOLIB */
  2480. if ( dialogPresent() )
  2481. {
  2482. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;}
  2483. p = selectFolderDialogWinConsole(lBuff,aTitle,aDefaultPath);
  2484. }
  2485. else
  2486. {
  2487. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return (char const *)0;}
  2488. p = tinyfd_inputBox(aTitle, "Select folder","");
  2489. }
  2490. if ( ! p || ! strlen ( p ) || ! dirExists ( p ) )
  2491. {
  2492. return NULL ;
  2493. }
  2494. return p ;
  2495. }
  2496. /* returns the hexcolor as a string "#FF0000" */
  2497. /* aoResultRGB also contains the result */
  2498. /* aDefaultRGB is used only if aDefaultHexRGB is NULL */
  2499. /* aDefaultRGB and aoResultRGB can be the same array */
  2500. char const * tinyfd_colorChooser(
  2501. char const * const aTitle, /* NULL or "" */
  2502. char const * const aDefaultHexRGB, /* NULL or "#FF0000"*/
  2503. unsigned char const aDefaultRGB[3], /* { 0 , 255 , 255 } */
  2504. unsigned char aoResultRGB[3]) /* { 0 , 0 , 0 } */
  2505. {
  2506. char lDefaultHexRGB[8];
  2507. char * lpDefaultHexRGB;
  2508. int i;
  2509. char const * p ;
  2510. #ifndef TINYFD_NOLIB
  2511. if ( (!tinyfd_forceConsole || !( GetConsoleWindow() || dialogPresent()) )
  2512. && (!getenv("SSH_CLIENT") || getenv("DISPLAY")) )
  2513. {
  2514. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char const *)1;}
  2515. if (tinyfd_winUtf8)
  2516. {
  2517. return colorChooserWinGui8(
  2518. aTitle, aDefaultHexRGB, aDefaultRGB, aoResultRGB);
  2519. }
  2520. else
  2521. {
  2522. return colorChooserWinGuiA(
  2523. aTitle, aDefaultHexRGB, aDefaultRGB, aoResultRGB);
  2524. }
  2525. }
  2526. else
  2527. #endif /* TINYFD_NOLIB */
  2528. if ( aDefaultHexRGB )
  2529. {
  2530. lpDefaultHexRGB = (char *) aDefaultHexRGB ;
  2531. }
  2532. else
  2533. {
  2534. RGB2Hex( aDefaultRGB , lDefaultHexRGB ) ;
  2535. lpDefaultHexRGB = (char *) lDefaultHexRGB ;
  2536. }
  2537. p = tinyfd_inputBox(aTitle,
  2538. "Enter hex rgb color (i.e. #f5ca20)",lpDefaultHexRGB);
  2539. if (aTitle&&!strcmp(aTitle,"tinyfd_query")) return p;
  2540. if ( !p || (strlen(p) != 7) || (p[0] != '#') )
  2541. {
  2542. return NULL ;
  2543. }
  2544. for ( i = 1 ; i < 7 ; i ++ )
  2545. {
  2546. if ( ! isxdigit( p[i] ) )
  2547. {
  2548. return NULL ;
  2549. }
  2550. }
  2551. Hex2RGB(p,aoResultRGB);
  2552. return p ;
  2553. }
  2554. #else /* unix */
  2555. static char gPython2Name[16];
  2556. static int isDarwin ( )
  2557. {
  2558. static int lsIsDarwin = -1 ;
  2559. struct utsname lUtsname ;
  2560. if ( lsIsDarwin < 0 )
  2561. {
  2562. lsIsDarwin = !uname(&lUtsname) && !strcmp(lUtsname.sysname,"Darwin") ;
  2563. }
  2564. return lsIsDarwin ;
  2565. }
  2566. static int dirExists ( char const * const aDirPath )
  2567. {
  2568. DIR * lDir ;
  2569. if ( ! aDirPath || ! strlen ( aDirPath ) )
  2570. return 0 ;
  2571. lDir = opendir ( aDirPath ) ;
  2572. if ( ! lDir )
  2573. {
  2574. return 0 ;
  2575. }
  2576. closedir ( lDir ) ;
  2577. return 1 ;
  2578. }
  2579. static int detectPresence ( char const * const aExecutable )
  2580. {
  2581. char lBuff [MAX_PATH_OR_CMD] ;
  2582. char lTestedString [MAX_PATH_OR_CMD] = "which " ;
  2583. FILE * lIn ;
  2584. strcat ( lTestedString , aExecutable ) ;
  2585. lIn = popen ( lTestedString , "r" ) ;
  2586. if ( ( fgets ( lBuff , sizeof ( lBuff ) , lIn ) != NULL )
  2587. && ( ! strchr ( lBuff , ':' ) )
  2588. && ( strncmp(lBuff, "no ", 3) ) )
  2589. { /* present */
  2590. pclose ( lIn ) ;
  2591. return 1 ;
  2592. }
  2593. else
  2594. {
  2595. pclose ( lIn ) ;
  2596. return 0 ;
  2597. }
  2598. }
  2599. static char const * getVersion ( char const * const aExecutable ) /*version # must follow :*/
  2600. {
  2601. static char lBuff [MAX_PATH_OR_CMD] ;
  2602. char lTestedString [MAX_PATH_OR_CMD] ;
  2603. FILE * lIn ;
  2604. char * lTmp ;
  2605. strcpy ( lTestedString , aExecutable ) ;
  2606. strcat ( lTestedString , " --version" ) ;
  2607. lIn = popen ( lTestedString , "r" ) ;
  2608. lTmp = fgets ( lBuff , sizeof ( lBuff ) , lIn ) ;
  2609. pclose ( lIn ) ;
  2610. if ( ! lTmp || !(lTmp = strchr ( lBuff , ':' )) ) return 0 ;
  2611. lTmp ++ ;
  2612. /* printf("lTmp %s\n", lTmp); */
  2613. return lTmp ;
  2614. }
  2615. static int tryCommand ( char const * const aCommand )
  2616. {
  2617. char lBuff [MAX_PATH_OR_CMD] ;
  2618. FILE * lIn ;
  2619. lIn = popen ( aCommand , "r" ) ;
  2620. if ( fgets ( lBuff , sizeof ( lBuff ) , lIn ) == NULL )
  2621. { /* present */
  2622. pclose ( lIn ) ;
  2623. return 1 ;
  2624. }
  2625. else
  2626. {
  2627. pclose ( lIn ) ;
  2628. return 0 ;
  2629. }
  2630. }
  2631. static int isTerminalRunning()
  2632. {
  2633. return isatty(1);
  2634. }
  2635. static char const * dialogNameOnly ( )
  2636. {
  2637. static char lDialogName[128] = "*" ;
  2638. if ( lDialogName[0] == '*' )
  2639. {
  2640. if ( isDarwin() && strcpy(lDialogName , "/opt/local/bin/dialog" )
  2641. && detectPresence ( lDialogName ) )
  2642. {}
  2643. else if ( strcpy(lDialogName , "dialog" )
  2644. && detectPresence ( lDialogName ) )
  2645. {}
  2646. else
  2647. {
  2648. strcpy(lDialogName , "" ) ;
  2649. }
  2650. }
  2651. return lDialogName ;
  2652. }
  2653. int isDialogVersionBetter09b ( )
  2654. {
  2655. char const * lDialogName ;
  2656. char * lVersion ;
  2657. int lMajor ;
  2658. int lMinor ;
  2659. int lDate ;
  2660. int lResult ;
  2661. char * lMinorP ;
  2662. char * lLetter ;
  2663. char lBuff[128] ;
  2664. /*char lTest[128] = " 0.9b-20031126" ;*/
  2665. lDialogName = dialogNameOnly ( ) ;
  2666. if ( ! lDialogName || !(lVersion = (char *) getVersion(lDialogName)) ) return 0 ;
  2667. /*lVersion = lTest ;*/
  2668. /*printf("lVersion %s\n", lVersion);*/
  2669. strcpy(lBuff,lVersion);
  2670. lMajor = atoi ( strtok(lVersion," ,.-") ) ;
  2671. /*printf("lMajor %d\n", lMajor);*/
  2672. lMinorP = strtok(0," ,.-abcdefghijklmnopqrstuvxyz");
  2673. lMinor = atoi ( lMinorP ) ;
  2674. /*printf("lMinor %d\n", lMinor );*/
  2675. lDate = atoi ( strtok(0," ,.-") ) ;
  2676. if (lDate<0) lDate = - lDate;
  2677. /*printf("lDate %d\n", lDate);*/
  2678. lLetter = lMinorP + strlen(lMinorP) ;
  2679. strcpy(lVersion,lBuff);
  2680. strtok(lLetter," ,.-");
  2681. /*printf("lLetter %s\n", lLetter);*/
  2682. lResult = (lMajor > 0) || ( ( lMinor == 9 ) && (*lLetter == 'b') && (lDate >= 20031126) );
  2683. /*printf("lResult %d\n", lResult);*/
  2684. return lResult;
  2685. }
  2686. static int whiptailPresentOnly ( )
  2687. {
  2688. static int lWhiptailPresent = -1 ;
  2689. if ( lWhiptailPresent < 0 )
  2690. {
  2691. lWhiptailPresent = detectPresence ( "whiptail" ) ;
  2692. }
  2693. return lWhiptailPresent ;
  2694. }
  2695. static char const * terminalName ( )
  2696. {
  2697. static char lTerminalName[128] = "*" ;
  2698. char lShellName[64] = "*" ;
  2699. if ( lTerminalName[0] == '*' )
  2700. {
  2701. if ( detectPresence ( "bash" ) )
  2702. {
  2703. strcpy(lShellName , "bash -c " ) ; /*good for basic input*/
  2704. }
  2705. else if ( dialogNameOnly() || whiptailPresentOnly() )
  2706. {
  2707. strcpy(lShellName , "sh -c " ) ; /*good enough for dialog & whiptail*/
  2708. }
  2709. else
  2710. {
  2711. return NULL ;
  2712. }
  2713. if ( isDarwin() )
  2714. {
  2715. if ( strcpy(lTerminalName , "/opt/X11/bin/xterm" )
  2716. && detectPresence ( lTerminalName ) )
  2717. {
  2718. strcat(lTerminalName , " -fa 'DejaVu Sans Mono' -fs 10 -title tinyfiledialogs -e " ) ;
  2719. strcat(lTerminalName , lShellName ) ;
  2720. }
  2721. else
  2722. {
  2723. strcpy(lTerminalName , "" ) ;
  2724. }
  2725. }
  2726. else if ( strcpy(lTerminalName,"xterm") /*good small without parameters*/
  2727. && detectPresence(lTerminalName) )
  2728. {
  2729. strcat(lTerminalName , " -fa 'DejaVu Sans Mono' -fs 10 -title tinyfiledialogs -e " ) ;
  2730. strcat(lTerminalName , lShellName ) ;
  2731. }
  2732. else if ( strcpy(lTerminalName,"terminator") /*good*/
  2733. && detectPresence(lTerminalName) )
  2734. {
  2735. strcat(lTerminalName , " -x " ) ;
  2736. strcat(lTerminalName , lShellName ) ;
  2737. }
  2738. else if ( strcpy(lTerminalName,"lxterminal") /*good*/
  2739. && detectPresence(lTerminalName) )
  2740. {
  2741. strcat(lTerminalName , " -e " ) ;
  2742. strcat(lTerminalName , lShellName ) ;
  2743. }
  2744. else if ( strcpy(lTerminalName,"mate-terminal") /*good*/
  2745. && detectPresence(lTerminalName) )
  2746. {
  2747. strcat(lTerminalName , " -x " ) ;
  2748. strcat(lTerminalName , lShellName ) ;
  2749. }
  2750. else if ( strcpy(lTerminalName,"konsole") /*good*/
  2751. && detectPresence(lTerminalName) )
  2752. {
  2753. strcat(lTerminalName , " -e " ) ;
  2754. strcat(lTerminalName , lShellName ) ;
  2755. }
  2756. else if ( strcpy(lTerminalName,"rxvt") /*good*/
  2757. && detectPresence(lTerminalName) )
  2758. {
  2759. strcat(lTerminalName , " -e " ) ;
  2760. strcat(lTerminalName , lShellName ) ;
  2761. }
  2762. else if ( strcpy(lTerminalName,"urxvt") /*good*/
  2763. && detectPresence(lTerminalName) )
  2764. {
  2765. strcat(lTerminalName , " -e " ) ;
  2766. strcat(lTerminalName , lShellName ) ;
  2767. }
  2768. else if ( strcpy(lTerminalName,"mrxvt") /*good*/
  2769. && detectPresence(lTerminalName) )
  2770. {
  2771. strcat(lTerminalName , " -e " ) ;
  2772. strcat(lTerminalName , lShellName ) ;
  2773. }
  2774. else if ( strcpy(lTerminalName,"evilvte") /*good*/
  2775. && detectPresence(lTerminalName) )
  2776. {
  2777. strcat(lTerminalName , " -e " ) ;
  2778. strcat(lTerminalName , lShellName ) ;
  2779. }
  2780. else if ( strcpy(lTerminalName,"termit") /*good*/
  2781. && detectPresence(lTerminalName) )
  2782. {
  2783. strcat(lTerminalName , " -e " ) ;
  2784. strcat(lTerminalName , lShellName ) ;
  2785. }
  2786. else if ( strcpy(lTerminalName,"kterm") /*good*/
  2787. && detectPresence(lTerminalName) )
  2788. {
  2789. strcat(lTerminalName , " -e " ) ;
  2790. strcat(lTerminalName , lShellName ) ;
  2791. }
  2792. else if ( strcpy(lTerminalName,"roxterm") /*good*/
  2793. && detectPresence(lTerminalName) )
  2794. {
  2795. strcat(lTerminalName , " -e " ) ;
  2796. strcat(lTerminalName , lShellName ) ;
  2797. }
  2798. else if ( strcpy(lTerminalName,"lxterm") /*good small*/
  2799. && detectPresence(lTerminalName) )
  2800. {
  2801. strcat(lTerminalName , " -e " ) ;
  2802. strcat(lTerminalName , lShellName ) ;
  2803. }
  2804. else if ( strcpy(lTerminalName,"gnome-terminal") /*good*/
  2805. && detectPresence(lTerminalName) )
  2806. {
  2807. strcat(lTerminalName , " --disable-factory -x " ) ;
  2808. strcat(lTerminalName , lShellName ) ;
  2809. }
  2810. else if ( strcpy(lTerminalName,"xfce4-terminal") /*was bad but maybe corrected now*/
  2811. && detectPresence(lTerminalName) )
  2812. {
  2813. strcat(lTerminalName , " -x " ) ;
  2814. strcat(lTerminalName , lShellName ) ;
  2815. }
  2816. else if ( strcpy(lTerminalName,"xvt") /*good B&W*/
  2817. && detectPresence(lTerminalName) )
  2818. {
  2819. strcat(lTerminalName , " -e " ) ;
  2820. strcat(lTerminalName , lShellName ) ;
  2821. }
  2822. else if ( strcpy(lTerminalName,"pterm") /*good only letters*/
  2823. && detectPresence(lTerminalName) )
  2824. {
  2825. strcat(lTerminalName , " -e " ) ;
  2826. strcat(lTerminalName , lShellName ) ;
  2827. }
  2828. else
  2829. {
  2830. strcpy(lTerminalName , "" ) ;
  2831. }
  2832. /* bad: koi8rxterm xfce4-terminal guake tilda vala-terminal qterminal
  2833. Eterm aterm Terminal terminology sakura lilyterm
  2834. aliases: x-terminal-emulator $TERM */
  2835. }
  2836. if ( strlen(lTerminalName) )
  2837. {
  2838. return lTerminalName ;
  2839. }
  2840. else
  2841. {
  2842. return NULL ;
  2843. }
  2844. }
  2845. static char const * dialogName ( )
  2846. {
  2847. char const * lDialogName ;
  2848. lDialogName = dialogNameOnly ( ) ;
  2849. if ( strlen(lDialogName) && ( isTerminalRunning() || terminalName() ) )
  2850. {
  2851. return lDialogName ;
  2852. }
  2853. else
  2854. {
  2855. return NULL ;
  2856. }
  2857. }
  2858. static int whiptailPresent ( )
  2859. {
  2860. int lWhiptailPresent ;
  2861. lWhiptailPresent = whiptailPresentOnly ( ) ;
  2862. if ( lWhiptailPresent && ( isTerminalRunning() || terminalName() ) )
  2863. {
  2864. return lWhiptailPresent ;
  2865. }
  2866. else
  2867. {
  2868. return 0 ;
  2869. }
  2870. }
  2871. static int graphicMode()
  2872. {
  2873. return !( tinyfd_forceConsole && (isTerminalRunning() || terminalName()) )
  2874. && ( getenv("DISPLAY")
  2875. || (isDarwin() && (!getenv("SSH_TTY") || getenv("DISPLAY") ) ) ) ;
  2876. }
  2877. static int xmessagePresent ( )
  2878. {
  2879. static int lXmessagePresent = -1 ;
  2880. if ( lXmessagePresent < 0 )
  2881. {
  2882. lXmessagePresent = detectPresence("xmessage");/*if not tty,not on osxpath*/
  2883. }
  2884. return lXmessagePresent && graphicMode ( ) ;
  2885. }
  2886. static int gxmessagePresent ( )
  2887. {
  2888. static int lGxmessagePresent = -1 ;
  2889. if ( lGxmessagePresent < 0 )
  2890. {
  2891. lGxmessagePresent = detectPresence("gxmessage") ;
  2892. }
  2893. return lGxmessagePresent && graphicMode ( ) ;
  2894. }
  2895. static int gmessagePresent ( )
  2896. {
  2897. static int lGmessagePresent = -1 ;
  2898. if ( lGmessagePresent < 0 )
  2899. {
  2900. lGmessagePresent = detectPresence("gmessage") ;
  2901. }
  2902. return lGmessagePresent && graphicMode ( ) ;
  2903. }
  2904. static int notifysendPresent ( )
  2905. {
  2906. static int lNotifysendPresent = -1 ;
  2907. if ( lNotifysendPresent < 0 )
  2908. {
  2909. lNotifysendPresent = detectPresence("notify-send") ;
  2910. }
  2911. return lNotifysendPresent && graphicMode ( ) ;
  2912. }
  2913. static int xdialogPresent ( )
  2914. {
  2915. static int lXdialogPresent = -1 ;
  2916. if ( lXdialogPresent < 0 )
  2917. {
  2918. lXdialogPresent = detectPresence("Xdialog") ;
  2919. }
  2920. return lXdialogPresent && graphicMode ( ) ;
  2921. }
  2922. static int gdialogPresent ( )
  2923. {
  2924. static int lGdialoglPresent = -1 ;
  2925. if ( lGdialoglPresent < 0 )
  2926. {
  2927. lGdialoglPresent = detectPresence ( "gdialog" ) ;
  2928. }
  2929. return lGdialoglPresent && graphicMode ( ) ;
  2930. }
  2931. static int osascriptPresent ( )
  2932. {
  2933. static int lOsascriptPresent = -1 ;
  2934. if ( lOsascriptPresent < 0 )
  2935. {
  2936. gWarningDisplayed |= !!getenv("SSH_TTY");
  2937. lOsascriptPresent = detectPresence ( "osascript" ) ;
  2938. }
  2939. return lOsascriptPresent && graphicMode() && !getenv("SSH_TTY") ;
  2940. }
  2941. static int kdialogPresent ( )
  2942. {
  2943. static int lKdialogPresent = -1 ;
  2944. if ( lKdialogPresent < 0 )
  2945. {
  2946. lKdialogPresent = detectPresence("kdialog") ;
  2947. }
  2948. return lKdialogPresent && graphicMode ( ) ;
  2949. }
  2950. static int qarmaPresent ( )
  2951. {
  2952. static int lQarmaPresent = -1 ;
  2953. if ( lQarmaPresent < 0 )
  2954. {
  2955. lQarmaPresent = detectPresence("qarma") ;
  2956. }
  2957. return lQarmaPresent && graphicMode ( ) ;
  2958. }
  2959. static int matedialogPresent ( )
  2960. {
  2961. static int lMatedialogPresent = -1 ;
  2962. if ( lMatedialogPresent < 0 )
  2963. {
  2964. lMatedialogPresent = detectPresence("matedialog") ;
  2965. }
  2966. return lMatedialogPresent && graphicMode ( ) ;
  2967. }
  2968. static int zenityPresent ( )
  2969. {
  2970. static int lZenityPresent = -1 ;
  2971. if ( lZenityPresent < 0 )
  2972. {
  2973. lZenityPresent = detectPresence("zenity") ;
  2974. }
  2975. return lZenityPresent && graphicMode ( ) ;
  2976. }
  2977. static int osx9orBetter ( )
  2978. {
  2979. static int lOsx9orBetter = -1 ;
  2980. char lBuff [MAX_PATH_OR_CMD] ;
  2981. FILE * lIn ;
  2982. int V,v;
  2983. if ( lOsx9orBetter < 0 )
  2984. {
  2985. lOsx9orBetter = 0 ;
  2986. lIn = popen ( "osascript -e 'set osver to system version of (system info)'" , "r" ) ;
  2987. if ( ( fgets ( lBuff , sizeof ( lBuff ) , lIn ) != NULL )
  2988. && ( 2 == sscanf(lBuff, "%d.%d", &V, &v) ) )
  2989. {
  2990. V = V * 100 + v;
  2991. if ( V >= 1009 )
  2992. {
  2993. lOsx9orBetter = 1 ;
  2994. }
  2995. }
  2996. pclose ( lIn ) ;
  2997. /* printf ("Osx10 = %d, %d = <%s>\n", lOsx9orBetter, V, lBuff) ; */
  2998. }
  2999. return lOsx9orBetter ;
  3000. }
  3001. static int zenity3Present ( )
  3002. {
  3003. static int lZenity3Present = -1 ;
  3004. char lBuff [MAX_PATH_OR_CMD] ;
  3005. FILE * lIn ;
  3006. if ( lZenity3Present < 0 )
  3007. {
  3008. lZenity3Present = 0 ;
  3009. if ( zenityPresent() )
  3010. {
  3011. lIn = popen ( "zenity --version" , "r" ) ;
  3012. if ( fgets ( lBuff , sizeof ( lBuff ) , lIn ) != NULL )
  3013. {
  3014. if ( atoi(lBuff) >= 3 )
  3015. {
  3016. lZenity3Present = 3 ;
  3017. }
  3018. else if ( ( atoi(lBuff) == 2 ) && ( atoi(strtok(lBuff,".")+2 ) >= 32 ) )
  3019. {
  3020. lZenity3Present = 2 ;
  3021. }
  3022. }
  3023. pclose ( lIn ) ;
  3024. }
  3025. }
  3026. return lZenity3Present && graphicMode ( ) ;
  3027. }
  3028. static int tkinter2Present ( )
  3029. {
  3030. static int lTkinter2Present = -1 ;
  3031. char lPythonCommand[256];
  3032. char lPythonParams[256] =
  3033. "-c \"try:\n\timport Tkinter;\nexcept:\n\tprint(0);\"";
  3034. int i;
  3035. if ( lTkinter2Present < 0 )
  3036. {
  3037. lTkinter2Present = 0 ;
  3038. strcpy(gPython2Name , "python" ) ;
  3039. sprintf ( lPythonCommand , "%s %s" , gPython2Name , lPythonParams ) ;
  3040. if ( ! detectPresence(gPython2Name)
  3041. || ! (lTkinter2Present = tryCommand(lPythonCommand)) )
  3042. {
  3043. strcpy(gPython2Name , "python2" ) ;
  3044. if ( detectPresence(gPython2Name) )
  3045. {
  3046. sprintf ( lPythonCommand , "%s %s" , gPython2Name , lPythonParams ) ;
  3047. lTkinter2Present = tryCommand(lPythonCommand);
  3048. }
  3049. else
  3050. {
  3051. for ( i = 9 ; i >= 0 ; i -- )
  3052. {
  3053. sprintf ( gPython2Name , "python2.%d" , i ) ;
  3054. if ( detectPresence(gPython2Name) )
  3055. {
  3056. sprintf ( lPythonCommand , "%s %s" , gPython2Name , lPythonParams ) ;
  3057. lTkinter2Present = tryCommand(lPythonCommand);
  3058. break ;
  3059. }
  3060. }
  3061. }
  3062. }
  3063. }
  3064. /* printf ("lTkinter2Present %d\n", lTkinter2Present) ; */
  3065. /* printf ("gPython2Name %s\n", gPython2Name) ; */
  3066. return lTkinter2Present && graphicMode() && !(isDarwin() && getenv("SSH_TTY") );
  3067. }
  3068. int tinyfd_messageBox (
  3069. char const * const aTitle , /* NULL or "" */
  3070. char const * const aMessage , /* NULL or "" may contain \n and \t */
  3071. char const * const aDialogType , /* "ok" "okcancel" "yesno" "yesnocancel" */
  3072. char const * const aIconType , /* "info" "warning" "error" "question" */
  3073. int const aDefaultButton ) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */
  3074. {
  3075. char lBuff [MAX_PATH_OR_CMD] ;
  3076. char * lDialogString = NULL ;
  3077. char * lpDialogString;
  3078. FILE * lIn ;
  3079. int lWasGraphicDialog = 0 ;
  3080. int lWasXterm = 0 ;
  3081. int lResult ;
  3082. char lChar ;
  3083. struct termios infoOri;
  3084. struct termios info;
  3085. int lTitleLen ;
  3086. int lMessageLen ;
  3087. lBuff[0]='\0';
  3088. lTitleLen = aTitle ? strlen(aTitle) : 0 ;
  3089. lMessageLen = aMessage ? strlen(aMessage) : 0 ;
  3090. if ( !aTitle || strcmp(aTitle,"tinyfd_query") )
  3091. {
  3092. lDialogString = (char *) malloc( MAX_PATH_OR_CMD + lTitleLen + lMessageLen );
  3093. }
  3094. if ( osascriptPresent ( ) )
  3095. {
  3096. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return 1;}
  3097. strcpy ( lDialogString , "osascript ");
  3098. if ( ! osx9orBetter() ) strcat ( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'");
  3099. strcat ( lDialogString , " -e 'try' -e 'set {vButton} to {button returned} of ( display dialog \"") ;
  3100. if ( aMessage && strlen(aMessage) )
  3101. {
  3102. strcat(lDialogString, aMessage) ;
  3103. }
  3104. strcat(lDialogString, "\" ") ;
  3105. if ( aTitle && strlen(aTitle) )
  3106. {
  3107. strcat(lDialogString, "with title \"") ;
  3108. strcat(lDialogString, aTitle) ;
  3109. strcat(lDialogString, "\" ") ;
  3110. }
  3111. strcat(lDialogString, "with icon ") ;
  3112. if ( aIconType && ! strcmp( "error" , aIconType ) )
  3113. {
  3114. strcat(lDialogString, "stop " ) ;
  3115. }
  3116. else if ( aIconType && ! strcmp( "warning" , aIconType ) )
  3117. {
  3118. strcat(lDialogString, "caution " ) ;
  3119. }
  3120. else /* question or info */
  3121. {
  3122. strcat(lDialogString, "note " ) ;
  3123. }
  3124. if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) )
  3125. {
  3126. if ( ! aDefaultButton )
  3127. {
  3128. strcat ( lDialogString ,"default button \"Cancel\" " ) ;
  3129. }
  3130. }
  3131. else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) )
  3132. {
  3133. strcat ( lDialogString ,"buttons {\"No\", \"Yes\"} " ) ;
  3134. if (aDefaultButton)
  3135. {
  3136. strcat ( lDialogString ,"default button \"Yes\" " ) ;
  3137. }
  3138. else
  3139. {
  3140. strcat ( lDialogString ,"default button \"No\" " ) ;
  3141. }
  3142. strcat ( lDialogString ,"cancel button \"No\"" ) ;
  3143. }
  3144. else if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) )
  3145. {
  3146. strcat ( lDialogString ,"buttons {\"No\", \"Yes\", \"Cancel\"} " ) ;
  3147. switch (aDefaultButton)
  3148. {
  3149. case 1: strcat ( lDialogString ,"default button \"Yes\" " ) ; break;
  3150. case 2: strcat ( lDialogString ,"default button \"No\" " ) ; break;
  3151. case 0: strcat ( lDialogString ,"default button \"Cancel\" " ) ; break;
  3152. }
  3153. strcat ( lDialogString ,"cancel button \"Cancel\"" ) ;
  3154. }
  3155. else
  3156. {
  3157. strcat ( lDialogString ,"buttons {\"OK\"} " ) ;
  3158. strcat ( lDialogString ,"default button \"OK\" " ) ;
  3159. }
  3160. strcat ( lDialogString, ")' ") ;
  3161. strcat ( lDialogString,
  3162. "-e 'if vButton is \"Yes\" then' -e 'return 1' -e 'else if vButton is \"No\" then' -e 'return 2' -e 'else' -e 'return 0' -e 'end if' " );
  3163. strcat ( lDialogString, "-e 'on error number -128' " ) ;
  3164. strcat ( lDialogString, "-e '0' " );
  3165. strcat ( lDialogString, "-e 'end try'") ;
  3166. if ( ! osx9orBetter() ) strcat ( lDialogString, " -e 'end tell'") ;
  3167. }
  3168. else if ( zenityPresent() || matedialogPresent() || qarmaPresent() )
  3169. {
  3170. if ( zenityPresent() )
  3171. {
  3172. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return 1;}
  3173. strcpy ( lDialogString , "szAnswer=$(zenity --" ) ;
  3174. }
  3175. else if ( matedialogPresent() )
  3176. {
  3177. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return 1;}
  3178. strcpy ( lDialogString , "szAnswer=$(matedialog --" ) ;
  3179. }
  3180. else
  3181. {
  3182. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return 1;}
  3183. strcpy ( lDialogString , "szAnswer=$(qarma --" ) ;
  3184. }
  3185. if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) )
  3186. {
  3187. strcat ( lDialogString ,
  3188. "question --ok-label=Ok --cancel-label=Cancel" ) ;
  3189. }
  3190. else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) )
  3191. {
  3192. strcat ( lDialogString , "question" ) ;
  3193. }
  3194. else if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) )
  3195. {
  3196. strcat ( lDialogString , "list --column \"\" --hide-header \"Yes\" \"No\"" ) ;
  3197. }
  3198. else if ( aIconType && ! strcmp( "error" , aIconType ) )
  3199. {
  3200. strcat ( lDialogString , "error" ) ;
  3201. }
  3202. else if ( aIconType && ! strcmp( "warning" , aIconType ) )
  3203. {
  3204. strcat ( lDialogString , "warning" ) ;
  3205. }
  3206. else
  3207. {
  3208. strcat ( lDialogString , "info" ) ;
  3209. }
  3210. if ( aTitle && strlen(aTitle) )
  3211. {
  3212. strcat(lDialogString, " --title=\"") ;
  3213. strcat(lDialogString, aTitle) ;
  3214. strcat(lDialogString, "\"") ;
  3215. }
  3216. if ( aMessage && strlen(aMessage) )
  3217. {
  3218. strcat(lDialogString, " --text=\"") ;
  3219. strcat(lDialogString, aMessage) ;
  3220. strcat(lDialogString, "\"") ;
  3221. }
  3222. if ( zenity3Present ( ) >= 3 )
  3223. {
  3224. strcat ( lDialogString , " --icon-name=dialog-" ) ;
  3225. if ( aIconType && (! strcmp( "question" , aIconType )
  3226. || ! strcmp( "error" , aIconType )
  3227. || ! strcmp( "warning" , aIconType ) ) )
  3228. {
  3229. strcat ( lDialogString , aIconType ) ;
  3230. }
  3231. else
  3232. {
  3233. strcat ( lDialogString , "information" ) ;
  3234. }
  3235. }
  3236. if ( ! strcmp( "yesnocancel" , aDialogType ) )
  3237. {
  3238. strcat ( lDialogString ,
  3239. ");if [ $? = 1 ];then echo 0;elif [ $szAnswer = \"No\" ];then echo 2;else echo 1;fi");
  3240. }
  3241. else
  3242. {
  3243. strcat ( lDialogString , ");if [ $? = 0 ];then echo 1;else echo 0;fi");
  3244. }
  3245. }
  3246. else if ( kdialogPresent() )
  3247. {
  3248. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return 1;}
  3249. strcpy ( lDialogString , "kdialog --" ) ;
  3250. if ( aDialogType && ( ! strcmp( "okcancel" , aDialogType )
  3251. || ! strcmp( "yesno" , aDialogType ) || ! strcmp( "yesnocancel" , aDialogType ) ) )
  3252. {
  3253. if ( aIconType && ( ! strcmp( "warning" , aIconType )
  3254. || ! strcmp( "error" , aIconType ) ) )
  3255. {
  3256. strcat ( lDialogString , "warning" ) ;
  3257. }
  3258. if ( ! strcmp( "yesnocancel" , aDialogType ) )
  3259. {
  3260. strcat ( lDialogString , "yesnocancel" ) ;
  3261. }
  3262. else
  3263. {
  3264. strcat ( lDialogString , "yesno" ) ;
  3265. }
  3266. }
  3267. else if ( aIconType && ! strcmp( "error" , aIconType ) )
  3268. {
  3269. strcat ( lDialogString , "error" ) ;
  3270. }
  3271. else if ( aIconType && ! strcmp( "warning" , aIconType ) )
  3272. {
  3273. strcat ( lDialogString , "sorry" ) ;
  3274. }
  3275. else
  3276. {
  3277. strcat ( lDialogString , "msgbox" ) ;
  3278. }
  3279. strcat ( lDialogString , " \"" ) ;
  3280. if ( aMessage )
  3281. {
  3282. strcat ( lDialogString , aMessage ) ;
  3283. }
  3284. strcat ( lDialogString , "\"" ) ;
  3285. if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) )
  3286. {
  3287. strcat ( lDialogString ,
  3288. " --yes-label Ok --no-label Cancel" ) ;
  3289. }
  3290. if ( aTitle && strlen(aTitle) )
  3291. {
  3292. strcat(lDialogString, " --title \"") ;
  3293. strcat(lDialogString, aTitle) ;
  3294. strcat(lDialogString, "\"") ;
  3295. }
  3296. if ( ! strcmp( "yesnocancel" , aDialogType ) )
  3297. {
  3298. strcat ( lDialogString , "; x=$? ;if [ $x = 0 ] ;then echo 1;elif [ $x = 1 ] ;then echo 2;else echo 0;fi");
  3299. }
  3300. else
  3301. {
  3302. strcat ( lDialogString , ";if [ $? = 0 ];then echo 1;else echo 0;fi");
  3303. }
  3304. }
  3305. else if ( !gxmessagePresent() && !gmessagePresent() && !gdialogPresent() && !xdialogPresent() && tkinter2Present ( ) )
  3306. {
  3307. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"tkinter");return 1;}
  3308. strcpy ( lDialogString , gPython2Name ) ;
  3309. if ( ! isTerminalRunning ( ) && isDarwin ( ) )
  3310. {
  3311. strcat ( lDialogString , " -i" ) ; /* for osx without console */
  3312. }
  3313. strcat ( lDialogString ,
  3314. " -c \"import Tkinter,tkMessageBox;root=Tkinter.Tk();root.withdraw();");
  3315. if ( isDarwin ( ) )
  3316. {
  3317. strcat ( lDialogString ,
  3318. "import os;os.system('''/usr/bin/osascript -e 'tell app \\\"Finder\\\" to set \
  3319. frontmost of process \\\"Python\\\" to true' ''');");
  3320. }
  3321. strcat ( lDialogString ,"res=tkMessageBox." ) ;
  3322. if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) )
  3323. {
  3324. strcat ( lDialogString , "askokcancel(" ) ;
  3325. if ( aDefaultButton )
  3326. {
  3327. strcat ( lDialogString , "default=tkMessageBox.OK," ) ;
  3328. }
  3329. else
  3330. {
  3331. strcat ( lDialogString , "default=tkMessageBox.CANCEL," ) ;
  3332. }
  3333. }
  3334. else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) )
  3335. {
  3336. strcat ( lDialogString , "askyesno(" ) ;
  3337. if ( aDefaultButton )
  3338. {
  3339. strcat ( lDialogString , "default=tkMessageBox.YES," ) ;
  3340. }
  3341. else
  3342. {
  3343. strcat ( lDialogString , "default=tkMessageBox.NO," ) ;
  3344. }
  3345. }
  3346. else if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) )
  3347. {
  3348. strcat ( lDialogString , "askyesnocancel(" ) ;
  3349. switch ( aDefaultButton )
  3350. {
  3351. case 1: strcat ( lDialogString , "default=tkMessageBox.YES," ); break;
  3352. case 2: strcat ( lDialogString , "default=tkMessageBox.NO," ); break;
  3353. case 0: strcat ( lDialogString , "default=tkMessageBox.CANCEL," ); break;
  3354. }
  3355. }
  3356. else
  3357. {
  3358. strcat ( lDialogString , "showinfo(" ) ;
  3359. }
  3360. strcat ( lDialogString , "icon='" ) ;
  3361. if ( aIconType && (! strcmp( "question" , aIconType )
  3362. || ! strcmp( "error" , aIconType )
  3363. || ! strcmp( "warning" , aIconType ) ) )
  3364. {
  3365. strcat ( lDialogString , aIconType ) ;
  3366. }
  3367. else
  3368. {
  3369. strcat ( lDialogString , "info" ) ;
  3370. }
  3371. strcat(lDialogString, "',") ;
  3372. if ( aTitle && strlen(aTitle) )
  3373. {
  3374. strcat(lDialogString, "title='") ;
  3375. strcat(lDialogString, aTitle) ;
  3376. strcat(lDialogString, "',") ;
  3377. }
  3378. if ( aMessage && strlen(aMessage) )
  3379. {
  3380. strcat(lDialogString, "message='") ;
  3381. lpDialogString = lDialogString + strlen(lDialogString);
  3382. replaceSubStr ( aMessage , "\n" , "\\n" , lpDialogString ) ;
  3383. strcat(lDialogString, "'") ;
  3384. }
  3385. if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) )
  3386. {
  3387. strcat(lDialogString, ");\n\
  3388. if res is None :\n\tprint 0\n\
  3389. elif res is False :\n\tprint 2\n\
  3390. else :\n\tprint 1\n\"" ) ;
  3391. }
  3392. else
  3393. {
  3394. strcat(lDialogString, ");\n\
  3395. if res is False :\n\tprint 0\n\
  3396. else :\n\tprint 1\n\"" ) ;
  3397. }
  3398. }
  3399. else if ( gxmessagePresent() || gmessagePresent() || (!gdialogPresent() && !xdialogPresent() && xmessagePresent()) )
  3400. {
  3401. if ( gxmessagePresent() )
  3402. {
  3403. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gxmessage");return 1;}
  3404. strcpy ( lDialogString , "gxmessage");
  3405. }
  3406. else if ( gmessagePresent() )
  3407. {
  3408. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gmessage");return 1;}
  3409. strcpy ( lDialogString , "gmessage");
  3410. }
  3411. else
  3412. {
  3413. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xmessage");return 1;}
  3414. strcpy ( lDialogString , "xmessage");
  3415. }
  3416. if ( aDialogType && ! strcmp("okcancel" , aDialogType) )
  3417. {
  3418. strcat ( lDialogString , " -buttons Ok:1,Cancel:0");
  3419. switch ( aDefaultButton )
  3420. {
  3421. case 1: strcat ( lDialogString , " -default Ok"); break;
  3422. case 0: strcat ( lDialogString , " -default Cancel"); break;
  3423. }
  3424. }
  3425. else if ( aDialogType && ! strcmp("yesno" , aDialogType) )
  3426. {
  3427. strcat ( lDialogString , " -buttons Yes:1,No:0");
  3428. switch ( aDefaultButton )
  3429. {
  3430. case 1: strcat ( lDialogString , " -default Yes"); break;
  3431. case 0: strcat ( lDialogString , " -default No"); break;
  3432. }
  3433. }
  3434. else if ( aDialogType && ! strcmp("yesnocancel" , aDialogType) )
  3435. {
  3436. strcat ( lDialogString , " -buttons Yes:1,No:2,Cancel:0");
  3437. switch ( aDefaultButton )
  3438. {
  3439. case 1: strcat ( lDialogString , " -default Yes"); break;
  3440. case 2: strcat ( lDialogString , " -default No"); break;
  3441. case 0: strcat ( lDialogString , " -default Cancel"); break;
  3442. }
  3443. }
  3444. else
  3445. {
  3446. strcat ( lDialogString , " -buttons Ok:1");
  3447. strcat ( lDialogString , " -default Ok");
  3448. }
  3449. strcat ( lDialogString , " -center \"");
  3450. if ( aMessage && strlen(aMessage) )
  3451. {
  3452. strcat ( lDialogString , aMessage ) ;
  3453. }
  3454. strcat(lDialogString, "\"" ) ;
  3455. if ( aTitle && strlen(aTitle) )
  3456. {
  3457. strcat ( lDialogString , " -title \"");
  3458. strcat ( lDialogString , aTitle ) ;
  3459. strcat ( lDialogString, "\"" ) ;
  3460. }
  3461. strcat ( lDialogString , " ; echo $? ");
  3462. }
  3463. else if ( xdialogPresent() || gdialogPresent() || dialogName() || whiptailPresent() )
  3464. {
  3465. if ( gdialogPresent ( ) )
  3466. {
  3467. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gdialog");return 1;}
  3468. lWasGraphicDialog = 1 ;
  3469. strcpy ( lDialogString , "(gdialog " ) ;
  3470. }
  3471. else if ( xdialogPresent ( ) )
  3472. {
  3473. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return 1;}
  3474. lWasGraphicDialog = 1 ;
  3475. strcpy ( lDialogString , "(Xdialog " ) ;
  3476. }
  3477. else if ( dialogName ( ) )
  3478. {
  3479. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return 0;}
  3480. if ( isTerminalRunning ( ) )
  3481. {
  3482. strcpy ( lDialogString , "(dialog " ) ;
  3483. }
  3484. else
  3485. {
  3486. lWasXterm = 1 ;
  3487. strcpy ( lDialogString , terminalName() ) ;
  3488. strcat ( lDialogString , "'(" ) ;
  3489. strcat ( lDialogString , dialogName() ) ;
  3490. strcat ( lDialogString , " " ) ;
  3491. }
  3492. }
  3493. else if ( isTerminalRunning ( ) )
  3494. {
  3495. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"whiptail");return 0;}
  3496. strcpy ( lDialogString , "(whiptail " ) ;
  3497. }
  3498. else
  3499. {
  3500. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"whiptail");return 0;}
  3501. lWasXterm = 1 ;
  3502. strcpy ( lDialogString , terminalName() ) ;
  3503. strcat ( lDialogString , "'(whiptail " ) ;
  3504. }
  3505. if ( aTitle && strlen(aTitle) )
  3506. {
  3507. strcat(lDialogString, "--title \"") ;
  3508. strcat(lDialogString, aTitle) ;
  3509. strcat(lDialogString, "\" ") ;
  3510. }
  3511. if ( !xdialogPresent() && !gdialogPresent() )
  3512. {
  3513. if ( aDialogType && ( !strcmp( "okcancel" , aDialogType ) || !strcmp( "yesno" , aDialogType )
  3514. || !strcmp( "yesnocancel" , aDialogType ) ) )
  3515. {
  3516. strcat(lDialogString, "--backtitle \"") ;
  3517. strcat(lDialogString, "tab: move focus") ;
  3518. strcat(lDialogString, "\" ") ;
  3519. }
  3520. }
  3521. if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) )
  3522. {
  3523. if ( ! aDefaultButton )
  3524. {
  3525. strcat ( lDialogString , "--defaultno " ) ;
  3526. }
  3527. strcat ( lDialogString ,
  3528. "--yes-label \"Ok\" --no-label \"Cancel\" --yesno " ) ;
  3529. }
  3530. else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) )
  3531. {
  3532. if ( ! aDefaultButton )
  3533. {
  3534. strcat ( lDialogString , "--defaultno " ) ;
  3535. }
  3536. strcat ( lDialogString , "--yesno " ) ;
  3537. }
  3538. else if (aDialogType && !strcmp("yesnocancel", aDialogType))
  3539. {
  3540. if (!aDefaultButton)
  3541. {
  3542. strcat(lDialogString, "--defaultno ");
  3543. }
  3544. strcat(lDialogString, "--menu ");
  3545. }
  3546. else
  3547. {
  3548. strcat ( lDialogString , "--msgbox " ) ;
  3549. }
  3550. strcat ( lDialogString , "\"" ) ;
  3551. if ( aMessage && strlen(aMessage) )
  3552. {
  3553. strcat(lDialogString, aMessage) ;
  3554. }
  3555. strcat(lDialogString, "\" ");
  3556. if ( lWasGraphicDialog )
  3557. {
  3558. if (aDialogType && !strcmp("yesnocancel", aDialogType))
  3559. {
  3560. strcat(lDialogString,"0 60 0 Yes \"\" No \"\") 2>/tmp/tinyfd.txt;\
  3561. if [ $? = 0 ];then tinyfdBool=1;else tinyfdBool=0;fi;\
  3562. tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ;
  3563. }
  3564. else
  3565. {
  3566. strcat(lDialogString,
  3567. "10 60 ) 2>&1;if [ $? = 0 ];then echo 1;else echo 0;fi");
  3568. }
  3569. }
  3570. else
  3571. {
  3572. if (aDialogType && !strcmp("yesnocancel", aDialogType))
  3573. {
  3574. strcat(lDialogString,"0 60 0 Yes \"\" No \"\" >/dev/tty ) 2>/tmp/tinyfd.txt;\
  3575. if [ $? = 0 ];then tinyfdBool=1;else tinyfdBool=0;fi;\
  3576. tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ;
  3577. if ( lWasXterm )
  3578. {
  3579. strcat(lDialogString," >/tmp/tinyfd0.txt';cat /tmp/tinyfd0.txt");
  3580. }
  3581. else
  3582. {
  3583. strcat(lDialogString, "; clear >/dev/tty") ;
  3584. }
  3585. }
  3586. else
  3587. {
  3588. strcat(lDialogString, "10 60 >/dev/tty) 2>&1;if [ $? = 0 ];");
  3589. if ( lWasXterm )
  3590. {
  3591. strcat ( lDialogString ,
  3592. "then\n\techo 1\nelse\n\techo 0\nfi >/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt");
  3593. }
  3594. else
  3595. {
  3596. strcat(lDialogString,
  3597. "then echo 1;else echo 0;fi;clear >/dev/tty");
  3598. }
  3599. }
  3600. }
  3601. }
  3602. else if ( ! isTerminalRunning ( ) && terminalName() )
  3603. {
  3604. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return 0;}
  3605. strcpy ( lDialogString , terminalName() ) ;
  3606. strcat ( lDialogString , "'" ) ;
  3607. if ( !gWarningDisplayed && !tinyfd_forceConsole)
  3608. {
  3609. gWarningDisplayed = 1 ;
  3610. strcat ( lDialogString , "echo \"" ) ;
  3611. strcat ( lDialogString, gTitle) ;
  3612. strcat ( lDialogString , "\";" ) ;
  3613. strcat ( lDialogString , "echo \"" ) ;
  3614. strcat ( lDialogString, gMessageUnix) ;
  3615. strcat ( lDialogString , "\";echo;echo;" ) ;
  3616. }
  3617. if ( aTitle && strlen(aTitle) )
  3618. {
  3619. strcat ( lDialogString , "echo \"" ) ;
  3620. strcat ( lDialogString, aTitle) ;
  3621. strcat ( lDialogString , "\";echo;" ) ;
  3622. }
  3623. if ( aMessage && strlen(aMessage) )
  3624. {
  3625. strcat ( lDialogString , "echo \"" ) ;
  3626. strcat ( lDialogString, aMessage) ;
  3627. strcat ( lDialogString , "\"; " ) ;
  3628. }
  3629. if ( aDialogType && !strcmp("yesno",aDialogType) )
  3630. {
  3631. strcat ( lDialogString , "echo -n \"y/n: \"; " ) ;
  3632. strcat ( lDialogString , "stty sane -echo;" ) ;
  3633. strcat ( lDialogString ,
  3634. "answer=$( while ! head -c 1 | grep -i [ny];do true ;done);");
  3635. strcat ( lDialogString ,
  3636. "if echo \"$answer\" | grep -iq \"^y\";then\n");
  3637. strcat ( lDialogString , "\techo 1\nelse\n\techo 0\nfi" ) ;
  3638. }
  3639. else if ( aDialogType && !strcmp("okcancel",aDialogType) )
  3640. {
  3641. strcat ( lDialogString , "echo -n \"[O]kay/[C]ancel: \"; " ) ;
  3642. strcat ( lDialogString , "stty sane -echo;" ) ;
  3643. strcat ( lDialogString ,
  3644. "answer=$( while ! head -c 1 | grep -i [oc];do true ;done);");
  3645. strcat ( lDialogString ,
  3646. "if echo \"$answer\" | grep -iq \"^o\";then\n");
  3647. strcat ( lDialogString , "\techo 1\nelse\n\techo 0\nfi" ) ;
  3648. }
  3649. else if ( aDialogType && !strcmp("yesnocancel",aDialogType) )
  3650. {
  3651. strcat ( lDialogString , "echo -n \"[Y]es/[N]o/[C]ancel: \"; " ) ;
  3652. strcat ( lDialogString , "stty sane -echo;" ) ;
  3653. strcat ( lDialogString ,
  3654. "answer=$( while ! head -c 1 | grep -i [nyc];do true ;done);");
  3655. strcat ( lDialogString ,
  3656. "if echo \"$answer\" | grep -iq \"^y\";then\n\techo 1\n");
  3657. strcat ( lDialogString , "elif echo \"$answer\" | grep -iq \"^n\";then\n\techo 2\n" ) ;
  3658. strcat ( lDialogString , "else\n\techo 0\nfi" ) ;
  3659. }
  3660. else
  3661. {
  3662. strcat(lDialogString , "echo -n \"press enter to continue \"; ");
  3663. strcat ( lDialogString , "stty sane -echo;" ) ;
  3664. strcat ( lDialogString ,
  3665. "answer=$( while ! head -c 1;do true ;done);echo 1");
  3666. }
  3667. strcat ( lDialogString ,
  3668. " >/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt");
  3669. }
  3670. else if ( !isTerminalRunning() && notifysendPresent() && !strcmp("ok" , aDialogType) )
  3671. {
  3672. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"notify");return 1;}
  3673. strcpy ( lDialogString , "notify-send \"" ) ;
  3674. if ( aTitle && strlen(aTitle) )
  3675. {
  3676. strcat(lDialogString, aTitle) ;
  3677. strcat ( lDialogString , " | " ) ;
  3678. }
  3679. if ( aMessage && strlen(aMessage) )
  3680. {
  3681. strcat(lDialogString, aMessage) ;
  3682. }
  3683. strcat ( lDialogString , "\"" ) ;
  3684. }
  3685. else
  3686. {
  3687. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return 0;}
  3688. if ( !gWarningDisplayed && !tinyfd_forceConsole)
  3689. {
  3690. gWarningDisplayed = 1 ;
  3691. printf ("\n\n%s\n", gTitle);
  3692. printf ("%s\n\n", gMessageUnix);
  3693. }
  3694. if ( aTitle && strlen(aTitle) )
  3695. {
  3696. printf ("\n%s\n", aTitle);
  3697. }
  3698. tcgetattr(0, &infoOri);
  3699. tcgetattr(0, &info);
  3700. info.c_lflag &= ~ICANON;
  3701. info.c_cc[VMIN] = 1;
  3702. info.c_cc[VTIME] = 0;
  3703. tcsetattr(0, TCSANOW, &info);
  3704. if ( aDialogType && !strcmp("yesno",aDialogType) )
  3705. {
  3706. do
  3707. {
  3708. if ( aMessage && strlen(aMessage) )
  3709. {
  3710. printf("\n%s\n",aMessage);
  3711. }
  3712. printf("y/n: "); fflush(stdout);
  3713. lChar = tolower ( getchar() ) ;
  3714. printf("\n\n");
  3715. }
  3716. while ( lChar != 'y' && lChar != 'n' );
  3717. lResult = lChar == 'y' ? 1 : 0 ;
  3718. }
  3719. else if ( aDialogType && !strcmp("okcancel",aDialogType) )
  3720. {
  3721. do
  3722. {
  3723. if ( aMessage && strlen(aMessage) )
  3724. {
  3725. printf("\n%s\n",aMessage);
  3726. }
  3727. printf("[O]kay/[C]ancel: "); fflush(stdout);
  3728. lChar = tolower ( getchar() ) ;
  3729. printf("\n\n");
  3730. }
  3731. while ( lChar != 'o' && lChar != 'c' );
  3732. lResult = lChar == 'o' ? 1 : 0 ;
  3733. }
  3734. else if ( aDialogType && !strcmp("yesnocancel",aDialogType) )
  3735. {
  3736. do
  3737. {
  3738. if ( aMessage && strlen(aMessage) )
  3739. {
  3740. printf("\n%s\n",aMessage);
  3741. }
  3742. printf("[Y]es/[N]o/[C]ancel: "); fflush(stdout);
  3743. lChar = tolower ( getchar() ) ;
  3744. printf("\n\n");
  3745. }
  3746. while ( lChar != 'y' && lChar != 'n' && lChar != 'c' );
  3747. lResult = (lChar == 'y') ? 1 : (lChar == 'n') ? 2 : 0 ;
  3748. }
  3749. else
  3750. {
  3751. if ( aMessage && strlen(aMessage) )
  3752. {
  3753. printf("\n%s\n\n",aMessage);
  3754. }
  3755. printf("press enter to continue "); fflush(stdout);
  3756. getchar() ;
  3757. printf("\n\n");
  3758. lResult = 1 ;
  3759. }
  3760. tcsetattr(0, TCSANOW, &infoOri);
  3761. free(lDialogString);
  3762. return lResult ;
  3763. }
  3764. if (tinyfd_verbose) printf ( "lDialogString: %s\n" , lDialogString ) ;
  3765. if ( ! ( lIn = popen ( lDialogString , "r" ) ) )
  3766. {
  3767. free(lDialogString);
  3768. return 0 ;
  3769. }
  3770. while ( fgets ( lBuff , sizeof ( lBuff ) , lIn ) != NULL )
  3771. {}
  3772. pclose ( lIn ) ;
  3773. /* printf ( "lBuff: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; */
  3774. if ( lBuff[strlen ( lBuff ) -1] == '\n' )
  3775. {
  3776. lBuff[strlen ( lBuff ) -1] = '\0' ;
  3777. }
  3778. /* printf ( "lBuff1: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; */
  3779. if (aDialogType && !strcmp("yesnocancel", aDialogType))
  3780. {
  3781. if ( lBuff[0]=='1' )
  3782. {
  3783. if ( !strcmp ( lBuff+1 , "Yes" )) strcpy(lBuff,"1");
  3784. else if ( !strcmp ( lBuff+1 , "No" )) strcpy(lBuff,"2");
  3785. }
  3786. }
  3787. /* printf ( "lBuff2: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; */
  3788. lResult = !strcmp ( lBuff , "2" ) ? 2 : !strcmp ( lBuff , "1" ) ? 1 : 0;
  3789. /* printf ( "lResult: %d\n" , lResult ) ; */
  3790. free(lDialogString);
  3791. return lResult ;
  3792. }
  3793. /* returns NULL on cancel */
  3794. char const * tinyfd_inputBox(
  3795. char const * const aTitle , /* NULL or "" */
  3796. char const * const aMessage , /* NULL or "" may NOT contain \n nor \t */
  3797. char const * const aDefaultInput ) /* "" , if NULL it's a passwordBox */
  3798. {
  3799. static char lBuff[MAX_PATH_OR_CMD];
  3800. char * lDialogString = NULL;
  3801. char * lpDialogString;
  3802. FILE * lIn ;
  3803. int lResult ;
  3804. int lWasGdialog = 0 ;
  3805. int lWasGraphicDialog = 0 ;
  3806. int lWasXterm = 0 ;
  3807. int lWasBasicXterm = 0 ;
  3808. struct termios oldt ;
  3809. struct termios newt ;
  3810. char * lEOF;
  3811. int lTitleLen ;
  3812. int lMessageLen ;
  3813. lBuff[0]='\0';
  3814. lTitleLen = aTitle ? strlen(aTitle) : 0 ;
  3815. lMessageLen = aMessage ? strlen(aMessage) : 0 ;
  3816. if ( !aTitle || strcmp(aTitle,"tinyfd_query") )
  3817. {
  3818. lDialogString = (char *) malloc( MAX_PATH_OR_CMD + lTitleLen + lMessageLen );
  3819. }
  3820. if ( osascriptPresent ( ) )
  3821. {
  3822. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char const *)1;}
  3823. strcpy ( lDialogString , "osascript ");
  3824. if ( ! osx9orBetter() ) strcat ( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'");
  3825. strcat ( lDialogString , " -e 'try' -e 'display dialog \"") ;
  3826. if ( aMessage && strlen(aMessage) )
  3827. {
  3828. strcat(lDialogString, aMessage) ;
  3829. }
  3830. strcat(lDialogString, "\" ") ;
  3831. strcat(lDialogString, "default answer \"") ;
  3832. if ( aDefaultInput && strlen(aDefaultInput) )
  3833. {
  3834. strcat(lDialogString, aDefaultInput) ;
  3835. }
  3836. strcat(lDialogString, "\" ") ;
  3837. if ( ! aDefaultInput )
  3838. {
  3839. strcat(lDialogString, "hidden answer true ") ;
  3840. }
  3841. if ( aTitle && strlen(aTitle) )
  3842. {
  3843. strcat(lDialogString, "with title \"") ;
  3844. strcat(lDialogString, aTitle) ;
  3845. strcat(lDialogString, "\" ") ;
  3846. }
  3847. strcat(lDialogString, "with icon note' ") ;
  3848. strcat(lDialogString, "-e '\"1\" & text returned of result' " );
  3849. strcat(lDialogString, "-e 'on error number -128' " ) ;
  3850. strcat(lDialogString, "-e '0' " );
  3851. strcat(lDialogString, "-e 'end try'") ;
  3852. if ( ! osx9orBetter() ) strcat(lDialogString, " -e 'end tell'") ;
  3853. }
  3854. else if ( zenityPresent() || matedialogPresent() || qarmaPresent() )
  3855. {
  3856. if ( zenityPresent() )
  3857. {
  3858. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char const *)1;}
  3859. strcpy ( lDialogString , "szAnswer=$(zenity --entry" ) ;
  3860. }
  3861. else if ( matedialogPresent() )
  3862. {
  3863. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char const *)1;}
  3864. strcpy ( lDialogString , "szAnswer=$(matedialog --entry" ) ;
  3865. }
  3866. else
  3867. {
  3868. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char const *)1;}
  3869. strcpy ( lDialogString , "szAnswer=$(qarma --entry" ) ;
  3870. }
  3871. if ( aTitle && strlen(aTitle) )
  3872. {
  3873. strcat(lDialogString, " --title=\"") ;
  3874. strcat(lDialogString, aTitle) ;
  3875. strcat(lDialogString, "\"") ;
  3876. }
  3877. if ( aMessage && strlen(aMessage) )
  3878. {
  3879. strcat(lDialogString, " --text=\"") ;
  3880. strcat(lDialogString, aMessage) ;
  3881. strcat(lDialogString, "\"") ;
  3882. }
  3883. if ( aDefaultInput && strlen(aDefaultInput) )
  3884. {
  3885. strcat(lDialogString, " --entry-text=\"") ;
  3886. strcat(lDialogString, aDefaultInput) ;
  3887. strcat(lDialogString, "\"") ;
  3888. }
  3889. else
  3890. {
  3891. strcat(lDialogString, " --hide-text") ;
  3892. }
  3893. strcat ( lDialogString ,
  3894. ");if [ $? = 0 ];then echo 1$szAnswer;else echo 0$szAnswer;fi");
  3895. }
  3896. else if ( kdialogPresent() )
  3897. {
  3898. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char const *)1;}
  3899. strcpy ( lDialogString , "szAnswer=$(kdialog" ) ;
  3900. if ( ! aDefaultInput )
  3901. {
  3902. strcat(lDialogString, " --password ") ;
  3903. }
  3904. else
  3905. {
  3906. strcat(lDialogString, " --inputbox ") ;
  3907. }
  3908. strcat(lDialogString, "\"") ;
  3909. if ( aMessage && strlen(aMessage) )
  3910. {
  3911. strcat(lDialogString, aMessage ) ;
  3912. }
  3913. strcat(lDialogString , "\" \"" ) ;
  3914. if ( aDefaultInput && strlen(aDefaultInput) )
  3915. {
  3916. strcat(lDialogString, aDefaultInput ) ;
  3917. }
  3918. strcat(lDialogString , "\"" ) ;
  3919. if ( aTitle && strlen(aTitle) )
  3920. {
  3921. strcat(lDialogString, " --title \"") ;
  3922. strcat(lDialogString, aTitle) ;
  3923. strcat(lDialogString, "\"") ;
  3924. }
  3925. strcat ( lDialogString ,
  3926. ");if [ $? = 0 ];then echo 1$szAnswer;else echo 0$szAnswer;fi");
  3927. }
  3928. else if ( gxmessagePresent() || gmessagePresent() )
  3929. {
  3930. if ( gxmessagePresent() ) {
  3931. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gxmessage");return (char const *)1;}
  3932. strcpy ( lDialogString , "szAnswer=$(gxmessage -buttons Ok:1,Cancel:0 -center \"");
  3933. }
  3934. else
  3935. {
  3936. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gmessage");return (char const *)1;}
  3937. strcpy ( lDialogString , "szAnswer=$(gmessage -buttons Ok:1,Cancel:0 -center \"");
  3938. }
  3939. if ( aMessage && strlen(aMessage) )
  3940. {
  3941. strcat ( lDialogString , aMessage ) ;
  3942. }
  3943. strcat(lDialogString, "\"" ) ;
  3944. if ( aTitle && strlen(aTitle) )
  3945. {
  3946. strcat ( lDialogString , " -title \"");
  3947. strcat ( lDialogString , aTitle ) ;
  3948. strcat(lDialogString, "\" " ) ;
  3949. }
  3950. strcat(lDialogString, " -entrytext \"" ) ;
  3951. if ( aDefaultInput && strlen(aDefaultInput) )
  3952. {
  3953. strcat ( lDialogString , aDefaultInput ) ;
  3954. }
  3955. strcat(lDialogString, "\"" ) ;
  3956. strcat ( lDialogString , ");echo $?$szAnswer");
  3957. }
  3958. else if ( !gdialogPresent() && tkinter2Present ( ) )
  3959. {
  3960. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"tkinter");return (char const *)1;}
  3961. strcpy ( lDialogString , gPython2Name ) ;
  3962. if ( ! isTerminalRunning ( ) && isDarwin ( ) )
  3963. {
  3964. strcat ( lDialogString , " -i" ) ; /* for osx without console */
  3965. }
  3966. strcat ( lDialogString ,
  3967. " -c \"import Tkinter,tkSimpleDialog;root=Tkinter.Tk();root.withdraw();");
  3968. if ( isDarwin ( ) )
  3969. {
  3970. strcat ( lDialogString ,
  3971. "import os;os.system('''/usr/bin/osascript -e 'tell app \\\"Finder\\\" to set \
  3972. frontmost of process \\\"Python\\\" to true' ''');");
  3973. }
  3974. strcat ( lDialogString ,"res=tkSimpleDialog.askstring(" ) ;
  3975. if ( aTitle && strlen(aTitle) )
  3976. {
  3977. strcat(lDialogString, "title='") ;
  3978. strcat(lDialogString, aTitle) ;
  3979. strcat(lDialogString, "',") ;
  3980. }
  3981. if ( aMessage && strlen(aMessage) )
  3982. {
  3983. strcat(lDialogString, "prompt='") ;
  3984. lpDialogString = lDialogString + strlen(lDialogString);
  3985. replaceSubStr ( aMessage , "\n" , "\\n" , lpDialogString ) ;
  3986. strcat(lDialogString, "',") ;
  3987. }
  3988. if ( aDefaultInput )
  3989. {
  3990. if ( strlen(aDefaultInput) )
  3991. {
  3992. strcat(lDialogString, "initialvalue='") ;
  3993. strcat(lDialogString, aDefaultInput) ;
  3994. strcat(lDialogString, "',") ;
  3995. }
  3996. }
  3997. else
  3998. {
  3999. strcat(lDialogString, "show='*'") ;
  4000. }
  4001. strcat(lDialogString, ");\nif res is None :\n\tprint 0");
  4002. strcat(lDialogString, "\nelse :\n\tprint '1'+res\n\"" ) ;
  4003. }
  4004. else if ( gdialogPresent() || xdialogPresent()
  4005. || dialogName() || whiptailPresent() )
  4006. {
  4007. if ( gdialogPresent ( ) )
  4008. {
  4009. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gdialog");return (char const *)1;}
  4010. lWasGraphicDialog = 1 ;
  4011. lWasGdialog = 1 ;
  4012. strcpy ( lDialogString , "(gdialog " ) ;
  4013. }
  4014. else if ( xdialogPresent ( ) )
  4015. {
  4016. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char const *)1;}
  4017. lWasGraphicDialog = 1 ;
  4018. strcpy ( lDialogString , "(Xdialog " ) ;
  4019. }
  4020. else if ( dialogName ( ) )
  4021. {
  4022. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;}
  4023. if ( isTerminalRunning ( ) )
  4024. {
  4025. strcpy ( lDialogString , "(dialog " ) ;
  4026. }
  4027. else
  4028. {
  4029. lWasXterm = 1 ;
  4030. strcpy ( lDialogString , terminalName() ) ;
  4031. strcat ( lDialogString , "'(" ) ;
  4032. strcat ( lDialogString , dialogName() ) ;
  4033. strcat ( lDialogString , " " ) ;
  4034. }
  4035. }
  4036. else if ( isTerminalRunning ( ) )
  4037. {
  4038. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"whiptail");return (char const *)0;}
  4039. strcpy ( lDialogString , "(whiptail " ) ;
  4040. }
  4041. else
  4042. {
  4043. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"whiptail");return (char const *)0;}
  4044. lWasXterm = 1 ;
  4045. strcpy ( lDialogString , terminalName() ) ;
  4046. strcat ( lDialogString , "'(whiptail " ) ;
  4047. }
  4048. if ( aTitle && strlen(aTitle) )
  4049. {
  4050. strcat(lDialogString, "--title \"") ;
  4051. strcat(lDialogString, aTitle) ;
  4052. strcat(lDialogString, "\" ") ;
  4053. }
  4054. if ( !xdialogPresent() && !gdialogPresent() )
  4055. {
  4056. strcat(lDialogString, "--backtitle \"") ;
  4057. strcat(lDialogString, "tab: move focus") ;
  4058. if ( ! aDefaultInput && !lWasGdialog )
  4059. {
  4060. strcat(lDialogString, " (sometimes nothing, no blink nor star, is shown in text field)") ;
  4061. }
  4062. strcat(lDialogString, "\" ") ;
  4063. }
  4064. if ( aDefaultInput || lWasGdialog )
  4065. {
  4066. strcat ( lDialogString , "--inputbox" ) ;
  4067. }
  4068. else
  4069. {
  4070. if ( !lWasGraphicDialog && dialogName() && isDialogVersionBetter09b() )
  4071. {
  4072. strcat ( lDialogString , "--insecure " ) ;
  4073. }
  4074. strcat ( lDialogString , "--passwordbox" ) ;
  4075. }
  4076. strcat ( lDialogString , " \"" ) ;
  4077. if ( aMessage && strlen(aMessage) )
  4078. {
  4079. strcat(lDialogString, aMessage) ;
  4080. }
  4081. strcat(lDialogString,"\" 10 60 ") ;
  4082. if ( aDefaultInput && strlen(aDefaultInput) )
  4083. {
  4084. strcat(lDialogString, "\"") ;
  4085. strcat(lDialogString, aDefaultInput) ;
  4086. strcat(lDialogString, "\" ") ;
  4087. }
  4088. if ( lWasGraphicDialog )
  4089. {
  4090. strcat(lDialogString,") 2>/tmp/tinyfd.txt;\
  4091. if [ $? = 0 ];then tinyfdBool=1;else tinyfdBool=0;fi;\
  4092. tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ;
  4093. }
  4094. else
  4095. {
  4096. strcat(lDialogString,">/dev/tty ) 2>/tmp/tinyfd.txt;\
  4097. if [ $? = 0 ];then tinyfdBool=1;else tinyfdBool=0;fi;\
  4098. tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ;
  4099. if ( lWasXterm )
  4100. {
  4101. strcat(lDialogString," >/tmp/tinyfd0.txt';cat /tmp/tinyfd0.txt");
  4102. }
  4103. else
  4104. {
  4105. strcat(lDialogString, "; clear >/dev/tty") ;
  4106. }
  4107. }
  4108. }
  4109. else if ( ! isTerminalRunning ( ) && terminalName() )
  4110. {
  4111. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return (char const *)0;}
  4112. lWasBasicXterm = 1 ;
  4113. strcpy ( lDialogString , terminalName() ) ;
  4114. strcat ( lDialogString , "'" ) ;
  4115. if ( !gWarningDisplayed && !tinyfd_forceConsole)
  4116. {
  4117. tinyfd_messageBox(gTitle,gMessageUnix,"ok","warning",0);
  4118. gWarningDisplayed = 1 ;
  4119. }
  4120. if ( aTitle && strlen(aTitle) && !tinyfd_forceConsole)
  4121. {
  4122. strcat ( lDialogString , "echo \"" ) ;
  4123. strcat ( lDialogString, aTitle) ;
  4124. strcat ( lDialogString , "\";echo;" ) ;
  4125. }
  4126. strcat ( lDialogString , "echo \"" ) ;
  4127. if ( aMessage && strlen(aMessage) )
  4128. {
  4129. strcat ( lDialogString, aMessage) ;
  4130. }
  4131. strcat ( lDialogString , "\";read " ) ;
  4132. if ( ! aDefaultInput )
  4133. {
  4134. strcat ( lDialogString , "-s " ) ;
  4135. }
  4136. strcat ( lDialogString , "-p \"" ) ;
  4137. strcat ( lDialogString , "(esc+enter to cancel): \" ANSWER " ) ;
  4138. strcat ( lDialogString , ";echo 1$ANSWER >/tmp/tinyfd.txt';" ) ;
  4139. strcat ( lDialogString , "cat -v /tmp/tinyfd.txt");
  4140. }
  4141. else
  4142. {
  4143. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return (char const *)0;}
  4144. if ( !gWarningDisplayed && !tinyfd_forceConsole)
  4145. {
  4146. tinyfd_messageBox(gTitle,gMessageUnix,"ok","warning",0);
  4147. gWarningDisplayed = 1 ;
  4148. }
  4149. if ( aTitle && strlen(aTitle) )
  4150. {
  4151. printf ("\n%s\n", aTitle);
  4152. }
  4153. if ( aMessage && strlen(aMessage) )
  4154. {
  4155. printf("\n%s\n",aMessage);
  4156. }
  4157. printf("(esc+enter to cancel): "); fflush(stdout);
  4158. if ( ! aDefaultInput )
  4159. {
  4160. tcgetattr(STDIN_FILENO, & oldt) ;
  4161. newt = oldt ;
  4162. newt.c_lflag &= ~ECHO ;
  4163. tcsetattr(STDIN_FILENO, TCSANOW, & newt);
  4164. }
  4165. lEOF = fgets(lBuff, MAX_PATH_OR_CMD, stdin);
  4166. /* printf("lbuff<%c><%d>\n",lBuff[0],lBuff[0]); */
  4167. if ( ! lEOF || (lBuff[0] == '\0') )
  4168. {
  4169. free(lDialogString);
  4170. return NULL;
  4171. }
  4172. if ( lBuff[0] == '\n' )
  4173. {
  4174. lEOF = fgets(lBuff, MAX_PATH_OR_CMD, stdin);
  4175. /* printf("lbuff<%c><%d>\n",lBuff[0],lBuff[0]); */
  4176. if ( ! lEOF || (lBuff[0] == '\0') )
  4177. {
  4178. free(lDialogString);
  4179. return NULL;
  4180. }
  4181. }
  4182. if ( ! aDefaultInput )
  4183. {
  4184. tcsetattr(STDIN_FILENO, TCSANOW, & oldt);
  4185. printf ("\n");
  4186. }
  4187. printf ("\n");
  4188. if ( strchr(lBuff,27) )
  4189. {
  4190. free(lDialogString);
  4191. return NULL ;
  4192. }
  4193. if ( lBuff[strlen ( lBuff ) -1] == '\n' )
  4194. {
  4195. lBuff[strlen ( lBuff ) -1] = '\0' ;
  4196. }
  4197. free(lDialogString);
  4198. return lBuff ;
  4199. }
  4200. if (tinyfd_verbose) printf ( "lDialogString: %s\n" , lDialogString ) ;
  4201. lIn = popen ( lDialogString , "r" );
  4202. if ( ! lIn )
  4203. {
  4204. if ( fileExists("/tmp/tinyfd.txt") )
  4205. {
  4206. wipefile("/tmp/tinyfd.txt");
  4207. remove("/tmp/tinyfd.txt");
  4208. }
  4209. if ( fileExists("/tmp/tinyfd0.txt") )
  4210. {
  4211. wipefile("/tmp/tinyfd0.txt");
  4212. remove("/tmp/tinyfd0.txt");
  4213. }
  4214. free(lDialogString);
  4215. return NULL ;
  4216. }
  4217. while ( fgets ( lBuff , sizeof ( lBuff ) , lIn ) != NULL )
  4218. {}
  4219. pclose ( lIn ) ;
  4220. if ( fileExists("/tmp/tinyfd.txt") )
  4221. {
  4222. wipefile("/tmp/tinyfd.txt");
  4223. remove("/tmp/tinyfd.txt");
  4224. }
  4225. if ( fileExists("/tmp/tinyfd0.txt") )
  4226. {
  4227. wipefile("/tmp/tinyfd0.txt");
  4228. remove("/tmp/tinyfd0.txt");
  4229. }
  4230. /* printf ( "len Buff: %lu\n" , strlen(lBuff) ) ; */
  4231. /* printf ( "lBuff0: %s\n" , lBuff ) ; */
  4232. if ( lBuff[strlen ( lBuff ) -1] == '\n' )
  4233. {
  4234. lBuff[strlen ( lBuff ) -1] = '\0' ;
  4235. }
  4236. /* printf ( "lBuff1: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; */
  4237. if ( lWasBasicXterm )
  4238. {
  4239. if ( strstr(lBuff,"^[") ) /* esc was pressed */
  4240. {
  4241. free(lDialogString);
  4242. return NULL ;
  4243. }
  4244. }
  4245. lResult = strncmp ( lBuff , "1" , 1) ? 0 : 1 ;
  4246. /* printf ( "lResult: %d \n" , lResult ) ; */
  4247. if ( ! lResult )
  4248. {
  4249. free(lDialogString);
  4250. return NULL ;
  4251. }
  4252. /* printf ( "lBuff+1: %s\n" , lBuff+1 ) ; */
  4253. free(lDialogString);
  4254. return lBuff+1 ;
  4255. }
  4256. char const * tinyfd_saveFileDialog (
  4257. char const * const aTitle , /* NULL or "" */
  4258. char const * const aDefaultPathAndFile , /* NULL or "" */
  4259. int const aNumOfFilterPatterns , /* 0 */
  4260. char const * const * const aFilterPatterns , /* NULL or {"*.jpg","*.png"} */
  4261. char const * const aSingleFilterDescription ) /* NULL or "image files" */
  4262. {
  4263. static char lBuff [MAX_PATH_OR_CMD] ;
  4264. char lDialogString [MAX_PATH_OR_CMD] ;
  4265. char lString [MAX_PATH_OR_CMD] ;
  4266. int i ;
  4267. int lWasGraphicDialog = 0 ;
  4268. int lWasXterm = 0 ;
  4269. char const * p ;
  4270. FILE * lIn ;
  4271. lBuff[0]='\0';
  4272. if ( osascriptPresent ( ) )
  4273. {
  4274. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char const *)1;}
  4275. strcpy ( lDialogString , "osascript ");
  4276. if ( ! osx9orBetter() ) strcat ( lDialogString , " -e 'tell application \"Finder\"' -e 'Activate'");
  4277. strcat ( lDialogString , " -e 'try' -e 'POSIX path of ( choose file name " );
  4278. if ( aTitle && strlen(aTitle) )
  4279. {
  4280. strcat(lDialogString, "with prompt \"") ;
  4281. strcat(lDialogString, aTitle) ;
  4282. strcat(lDialogString, "\" ") ;
  4283. }
  4284. getPathWithoutFinalSlash ( lString , aDefaultPathAndFile ) ;
  4285. if ( strlen(lString) )
  4286. {
  4287. strcat(lDialogString, "default location \"") ;
  4288. strcat(lDialogString, lString ) ;
  4289. strcat(lDialogString , "\" " ) ;
  4290. }
  4291. getLastName ( lString , aDefaultPathAndFile ) ;
  4292. if ( strlen(lString) )
  4293. {
  4294. strcat(lDialogString, "default name \"") ;
  4295. strcat(lDialogString, lString ) ;
  4296. strcat(lDialogString , "\" " ) ;
  4297. }
  4298. strcat ( lDialogString , ")' " ) ;
  4299. strcat(lDialogString, "-e 'on error number -128' " ) ;
  4300. strcat(lDialogString, "-e 'end try'") ;
  4301. if ( ! osx9orBetter() ) strcat ( lDialogString, " -e 'end tell'") ;
  4302. }
  4303. else if ( zenityPresent() || matedialogPresent() || qarmaPresent() )
  4304. {
  4305. if ( zenityPresent() )
  4306. {
  4307. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char const *)1;}
  4308. strcpy ( lDialogString , "zenity" ) ;
  4309. }
  4310. else if ( matedialogPresent() )
  4311. {
  4312. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char const *)1;}
  4313. strcpy ( lDialogString , "matedialog" ) ;
  4314. }
  4315. else
  4316. {
  4317. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char const *)1;}
  4318. strcpy ( lDialogString , "qarma" ) ;
  4319. }
  4320. strcat(lDialogString, " --file-selection --save --confirm-overwrite" ) ;
  4321. if ( aTitle && strlen(aTitle) )
  4322. {
  4323. strcat(lDialogString, " --title=\"") ;
  4324. strcat(lDialogString, aTitle) ;
  4325. strcat(lDialogString, "\"") ;
  4326. }
  4327. if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) )
  4328. {
  4329. strcat(lDialogString, " --filename=\"") ;
  4330. strcat(lDialogString, aDefaultPathAndFile) ;
  4331. strcat(lDialogString, "\"") ;
  4332. }
  4333. if ( aNumOfFilterPatterns > 0 )
  4334. {
  4335. strcat ( lDialogString , " --file-filter='" ) ;
  4336. if ( aSingleFilterDescription && strlen(aSingleFilterDescription) )
  4337. {
  4338. strcat ( lDialogString , aSingleFilterDescription ) ;
  4339. strcat ( lDialogString , " | " ) ;
  4340. }
  4341. for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ )
  4342. {
  4343. strcat ( lDialogString , aFilterPatterns [i] ) ;
  4344. strcat ( lDialogString , " " ) ;
  4345. }
  4346. strcat ( lDialogString , "' --file-filter='All files | *'" ) ;
  4347. }
  4348. }
  4349. else if ( kdialogPresent() )
  4350. {
  4351. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char const *)1;}
  4352. strcpy ( lDialogString , "kdialog --getsavefilename" ) ;
  4353. if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) )
  4354. {
  4355. strcat(lDialogString, " \"") ;
  4356. strcat(lDialogString, aDefaultPathAndFile ) ;
  4357. strcat(lDialogString , "\"" ) ;
  4358. }
  4359. else
  4360. {
  4361. strcat(lDialogString, " :" ) ;
  4362. }
  4363. if ( aNumOfFilterPatterns > 0 )
  4364. {
  4365. strcat(lDialogString , " \"" ) ;
  4366. for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ )
  4367. {
  4368. strcat ( lDialogString , aFilterPatterns [i] ) ;
  4369. strcat ( lDialogString , " " ) ;
  4370. }
  4371. if ( aSingleFilterDescription && strlen(aSingleFilterDescription) )
  4372. {
  4373. strcat ( lDialogString , " | " ) ;
  4374. strcat ( lDialogString , aSingleFilterDescription ) ;
  4375. }
  4376. strcat ( lDialogString , "\"" ) ;
  4377. }
  4378. if ( aTitle && strlen(aTitle) )
  4379. {
  4380. strcat(lDialogString, " --title \"") ;
  4381. strcat(lDialogString, aTitle) ;
  4382. strcat(lDialogString, "\"") ;
  4383. }
  4384. }
  4385. else if ( ! xdialogPresent() && tkinter2Present ( ) )
  4386. {
  4387. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"tkinter");return (char const *)1;}
  4388. strcpy ( lDialogString , gPython2Name ) ;
  4389. if ( ! isTerminalRunning ( ) && isDarwin ( ))
  4390. {
  4391. strcat ( lDialogString , " -i" ) ; /* for osx without console */
  4392. }
  4393. strcat ( lDialogString ,
  4394. " -c \"import Tkinter,tkFileDialog;root=Tkinter.Tk();root.withdraw();");
  4395. if ( isDarwin ( ) )
  4396. {
  4397. strcat ( lDialogString ,
  4398. "import os;os.system('''/usr/bin/osascript -e 'tell app \\\"Finder\\\" to set\
  4399. frontmost of process \\\"Python\\\" to true' ''');");
  4400. }
  4401. strcat ( lDialogString , "print tkFileDialog.asksaveasfilename(");
  4402. if ( aTitle && strlen(aTitle) )
  4403. {
  4404. strcat(lDialogString, "title='") ;
  4405. strcat(lDialogString, aTitle) ;
  4406. strcat(lDialogString, "',") ;
  4407. }
  4408. if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) )
  4409. {
  4410. getPathWithoutFinalSlash ( lString , aDefaultPathAndFile ) ;
  4411. if ( strlen(lString) )
  4412. {
  4413. strcat(lDialogString, "initialdir='") ;
  4414. strcat(lDialogString, lString ) ;
  4415. strcat(lDialogString , "'," ) ;
  4416. }
  4417. getLastName ( lString , aDefaultPathAndFile ) ;
  4418. if ( strlen(lString) )
  4419. {
  4420. strcat(lDialogString, "initialfile='") ;
  4421. strcat(lDialogString, lString ) ;
  4422. strcat(lDialogString , "'," ) ;
  4423. }
  4424. }
  4425. if ( ( aNumOfFilterPatterns > 1 )
  4426. || ( (aNumOfFilterPatterns == 1) /* test because poor osx behaviour */
  4427. && ( aFilterPatterns[0][strlen(aFilterPatterns[0])-1] != '*' ) ) )
  4428. {
  4429. strcat(lDialogString , "filetypes=(" ) ;
  4430. strcat ( lDialogString , "('" ) ;
  4431. if ( aSingleFilterDescription && strlen(aSingleFilterDescription) )
  4432. {
  4433. strcat ( lDialogString , aSingleFilterDescription ) ;
  4434. }
  4435. strcat ( lDialogString , "',(" ) ;
  4436. for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ )
  4437. {
  4438. strcat ( lDialogString , "'" ) ;
  4439. strcat ( lDialogString , aFilterPatterns [i] ) ;
  4440. strcat ( lDialogString , "'," ) ;
  4441. }
  4442. strcat ( lDialogString , "))," ) ;
  4443. strcat ( lDialogString , "('All files','*'))" ) ;
  4444. }
  4445. strcat ( lDialogString , ")\"" ) ;
  4446. }
  4447. else if ( xdialogPresent() || dialogName() )
  4448. {
  4449. if ( xdialogPresent ( ) )
  4450. {
  4451. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char const *)1;}
  4452. lWasGraphicDialog = 1 ;
  4453. strcpy ( lDialogString , "(Xdialog " ) ;
  4454. }
  4455. else if ( isTerminalRunning ( ) )
  4456. {
  4457. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;}
  4458. strcpy ( lDialogString , "(dialog " ) ;
  4459. }
  4460. else
  4461. {
  4462. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;}
  4463. lWasXterm = 1 ;
  4464. strcpy ( lDialogString , terminalName() ) ;
  4465. strcat ( lDialogString , "'(" ) ;
  4466. strcat ( lDialogString , dialogName() ) ;
  4467. strcat ( lDialogString , " " ) ;
  4468. }
  4469. if ( aTitle && strlen(aTitle) )
  4470. {
  4471. strcat(lDialogString, "--title \"") ;
  4472. strcat(lDialogString, aTitle) ;
  4473. strcat(lDialogString, "\" ") ;
  4474. }
  4475. if ( !xdialogPresent() && !gdialogPresent() )
  4476. {
  4477. strcat(lDialogString, "--backtitle \"") ;
  4478. strcat(lDialogString,
  4479. "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ;
  4480. strcat(lDialogString, "\" ") ;
  4481. }
  4482. strcat ( lDialogString , "--fselect \"" ) ;
  4483. if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) )
  4484. {
  4485. if ( ! strchr(aDefaultPathAndFile, '/') )
  4486. {
  4487. strcat(lDialogString, "./") ;
  4488. }
  4489. strcat(lDialogString, aDefaultPathAndFile) ;
  4490. }
  4491. else if ( ! isTerminalRunning ( ) && !lWasGraphicDialog )
  4492. {
  4493. strcat(lDialogString, getenv("HOME")) ;
  4494. strcat(lDialogString, "/") ;
  4495. }
  4496. else
  4497. {
  4498. strcat(lDialogString, "./") ;
  4499. }
  4500. if ( lWasGraphicDialog )
  4501. {
  4502. strcat(lDialogString, "\" 0 60 ) 2>&1 ") ;
  4503. }
  4504. else
  4505. {
  4506. strcat(lDialogString, "\" 0 60 >/dev/tty) ") ;
  4507. if ( lWasXterm )
  4508. {
  4509. strcat ( lDialogString ,
  4510. "2>/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt");
  4511. }
  4512. else
  4513. {
  4514. strcat(lDialogString, "2>&1 ; clear >/dev/tty") ;
  4515. }
  4516. }
  4517. }
  4518. else
  4519. {
  4520. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){return tinyfd_inputBox (aTitle,NULL,NULL);}
  4521. p = tinyfd_inputBox ( aTitle , "Save file" , "" ) ;
  4522. getPathWithoutFinalSlash ( lString , p ) ;
  4523. if ( strlen ( lString ) && ! dirExists ( lString ) )
  4524. {
  4525. return NULL ;
  4526. }
  4527. getLastName(lString,p);
  4528. if ( ! strlen(lString) )
  4529. {
  4530. return NULL;
  4531. }
  4532. return p ;
  4533. }
  4534. if (tinyfd_verbose) printf ( "lDialogString: %s\n" , lDialogString ) ;
  4535. if ( ! ( lIn = popen ( lDialogString , "r" ) ) )
  4536. {
  4537. return NULL ;
  4538. }
  4539. while ( fgets ( lBuff , sizeof ( lBuff ) , lIn ) != NULL )
  4540. {}
  4541. pclose ( lIn ) ;
  4542. if ( lBuff[strlen ( lBuff ) -1] == '\n' )
  4543. {
  4544. lBuff[strlen ( lBuff ) -1] = '\0' ;
  4545. }
  4546. /* printf ( "lBuff: %s\n" , lBuff ) ; */
  4547. if ( ! strlen(lBuff) )
  4548. {
  4549. return NULL;
  4550. }
  4551. getPathWithoutFinalSlash ( lString , lBuff ) ;
  4552. if ( strlen ( lString ) && ! dirExists ( lString ) )
  4553. {
  4554. return NULL ;
  4555. }
  4556. getLastName(lString,lBuff);
  4557. if ( ! filenameValid(lString) )
  4558. {
  4559. return NULL;
  4560. }
  4561. return lBuff ;
  4562. }
  4563. /* in case of multiple files, the separator is | */
  4564. char const * tinyfd_openFileDialog (
  4565. char const * const aTitle , /* NULL or "" */
  4566. char const * const aDefaultPathAndFile , /* NULL or "" */
  4567. int const aNumOfFilterPatterns , /* 0 */
  4568. char const * const * const aFilterPatterns , /* NULL or {"*.jpg","*.png"} */
  4569. char const * const aSingleFilterDescription , /* NULL or "image files" */
  4570. int const aAllowMultipleSelects ) /* 0 or 1 */
  4571. {
  4572. static char lBuff [MAX_MULTIPLE_FILES*MAX_PATH_OR_CMD] ;
  4573. char lDialogString [MAX_PATH_OR_CMD] ;
  4574. char lString [MAX_PATH_OR_CMD] ;
  4575. int i ;
  4576. FILE * lIn ;
  4577. char * p ;
  4578. char const * p2 ;
  4579. int lWasKdialog = 0 ;
  4580. int lWasGraphicDialog = 0 ;
  4581. int lWasXterm = 0 ;
  4582. lBuff[0]='\0';
  4583. if ( osascriptPresent ( ) )
  4584. {
  4585. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char const *)1;}
  4586. strcpy ( lDialogString , "osascript ");
  4587. if ( ! osx9orBetter() ) strcat ( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'");
  4588. strcat ( lDialogString , " -e 'try' -e '" );
  4589. if ( ! aAllowMultipleSelects )
  4590. {
  4591. strcat ( lDialogString , "POSIX path of ( " );
  4592. }
  4593. else
  4594. {
  4595. strcat ( lDialogString , "set mylist to " );
  4596. }
  4597. strcat ( lDialogString , "choose file " );
  4598. if ( aTitle && strlen(aTitle) )
  4599. {
  4600. strcat(lDialogString, "with prompt \"") ;
  4601. strcat(lDialogString, aTitle) ;
  4602. strcat(lDialogString, "\" ") ;
  4603. }
  4604. getPathWithoutFinalSlash ( lString , aDefaultPathAndFile ) ;
  4605. if ( strlen(lString) )
  4606. {
  4607. strcat(lDialogString, "default location \"") ;
  4608. strcat(lDialogString, lString ) ;
  4609. strcat(lDialogString , "\" " ) ;
  4610. }
  4611. if ( aNumOfFilterPatterns > 0 )
  4612. {
  4613. strcat(lDialogString , "of type {\"" );
  4614. strcat ( lDialogString , aFilterPatterns [0] + 2 ) ;
  4615. strcat ( lDialogString , "\"" ) ;
  4616. for ( i = 1 ; i < aNumOfFilterPatterns ; i ++ )
  4617. {
  4618. strcat ( lDialogString , ",\"" ) ;
  4619. strcat ( lDialogString , aFilterPatterns [i] + 2) ;
  4620. strcat ( lDialogString , "\"" ) ;
  4621. }
  4622. strcat ( lDialogString , "} " ) ;
  4623. }
  4624. if ( aAllowMultipleSelects )
  4625. {
  4626. strcat ( lDialogString , "multiple selections allowed true ' " ) ;
  4627. strcat ( lDialogString ,
  4628. "-e 'set mystring to POSIX path of item 1 of mylist' " );
  4629. strcat ( lDialogString ,
  4630. "-e 'repeat with i from 2 to the count of mylist' " );
  4631. strcat ( lDialogString , "-e 'set mystring to mystring & \"|\"' " );
  4632. strcat ( lDialogString ,
  4633. "-e 'set mystring to mystring & POSIX path of item i of mylist' " );
  4634. strcat ( lDialogString , "-e 'end repeat' " );
  4635. strcat ( lDialogString , "-e 'mystring' " );
  4636. }
  4637. else
  4638. {
  4639. strcat ( lDialogString , ")' " ) ;
  4640. }
  4641. strcat(lDialogString, "-e 'on error number -128' " ) ;
  4642. strcat(lDialogString, "-e 'end try'") ;
  4643. if ( ! osx9orBetter() ) strcat ( lDialogString, " -e 'end tell'") ;
  4644. }
  4645. else if ( zenityPresent() || matedialogPresent() || qarmaPresent() )
  4646. {
  4647. if ( zenityPresent() )
  4648. {
  4649. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char const *)1;}
  4650. strcpy ( lDialogString , "zenity --file-selection" ) ;
  4651. }
  4652. else if ( matedialogPresent() )
  4653. {
  4654. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char const *)1;}
  4655. strcpy ( lDialogString , "matedialog --file-selection" ) ;
  4656. }
  4657. else
  4658. {
  4659. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char const *)1;}
  4660. strcpy ( lDialogString , "qarma --file-selection" ) ;
  4661. }
  4662. if ( aAllowMultipleSelects )
  4663. {
  4664. strcat ( lDialogString , " --multiple" ) ;
  4665. }
  4666. if ( aTitle && strlen(aTitle) )
  4667. {
  4668. strcat(lDialogString, " --title=\"") ;
  4669. strcat(lDialogString, aTitle) ;
  4670. strcat(lDialogString, "\"") ;
  4671. }
  4672. if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) )
  4673. {
  4674. strcat(lDialogString, " --filename=\"") ;
  4675. strcat(lDialogString, aDefaultPathAndFile) ;
  4676. strcat(lDialogString, "\"") ;
  4677. }
  4678. if ( aNumOfFilterPatterns > 0 )
  4679. {
  4680. strcat ( lDialogString , " --file-filter='" ) ;
  4681. if ( aSingleFilterDescription && strlen(aSingleFilterDescription) )
  4682. {
  4683. strcat ( lDialogString , aSingleFilterDescription ) ;
  4684. strcat ( lDialogString , " | " ) ;
  4685. }
  4686. for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ )
  4687. {
  4688. strcat ( lDialogString , aFilterPatterns [i] ) ;
  4689. strcat ( lDialogString , " " ) ;
  4690. }
  4691. strcat ( lDialogString , "' --file-filter='All files | *'" ) ;
  4692. }
  4693. }
  4694. else if ( kdialogPresent() )
  4695. {
  4696. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char const *)1;}
  4697. lWasKdialog = 1 ;
  4698. strcpy ( lDialogString , "kdialog --getopenfilename" ) ;
  4699. if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) )
  4700. {
  4701. strcat(lDialogString, " \"") ;
  4702. strcat(lDialogString, aDefaultPathAndFile ) ;
  4703. strcat(lDialogString , "\"" ) ;
  4704. }
  4705. else
  4706. {
  4707. strcat(lDialogString, " :" ) ;
  4708. }
  4709. if ( aNumOfFilterPatterns > 0 )
  4710. {
  4711. strcat(lDialogString , " \"" ) ;
  4712. for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ )
  4713. {
  4714. strcat ( lDialogString , aFilterPatterns [i] ) ;
  4715. strcat ( lDialogString , " " ) ;
  4716. }
  4717. if ( aSingleFilterDescription && strlen(aSingleFilterDescription) )
  4718. {
  4719. strcat ( lDialogString , " | " ) ;
  4720. strcat ( lDialogString , aSingleFilterDescription ) ;
  4721. }
  4722. strcat ( lDialogString , "\"" ) ;
  4723. }
  4724. if ( aAllowMultipleSelects )
  4725. {
  4726. strcat ( lDialogString , " --multiple --separate-output" ) ;
  4727. }
  4728. if ( aTitle && strlen(aTitle) )
  4729. {
  4730. strcat(lDialogString, " --title \"") ;
  4731. strcat(lDialogString, aTitle) ;
  4732. strcat(lDialogString, "\"") ;
  4733. }
  4734. }
  4735. else if ( ! xdialogPresent() && tkinter2Present ( ) )
  4736. {
  4737. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"tkinter");return (char const *)1;}
  4738. strcpy ( lDialogString , gPython2Name ) ;
  4739. if ( ! isTerminalRunning ( ) && isDarwin ( ) )
  4740. {
  4741. strcat ( lDialogString , " -i" ) ; /* for osx without console */
  4742. }
  4743. strcat ( lDialogString ,
  4744. " -c \"import Tkinter,tkFileDialog;root=Tkinter.Tk();root.withdraw();");
  4745. if ( isDarwin ( ) )
  4746. {
  4747. strcat ( lDialogString ,
  4748. "import os;os.system('''/usr/bin/osascript -e 'tell app \\\"Finder\\\" to set \
  4749. frontmost of process \\\"Python\\\" to true' ''');");
  4750. }
  4751. strcat ( lDialogString , "lFiles=tkFileDialog.askopenfilename(");
  4752. if ( aAllowMultipleSelects )
  4753. {
  4754. strcat ( lDialogString , "multiple=1," ) ;
  4755. }
  4756. if ( aTitle && strlen(aTitle) )
  4757. {
  4758. strcat(lDialogString, "title='") ;
  4759. strcat(lDialogString, aTitle) ;
  4760. strcat(lDialogString, "',") ;
  4761. }
  4762. if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) )
  4763. {
  4764. getPathWithoutFinalSlash ( lString , aDefaultPathAndFile ) ;
  4765. if ( strlen(lString) )
  4766. {
  4767. strcat(lDialogString, "initialdir='") ;
  4768. strcat(lDialogString, lString ) ;
  4769. strcat(lDialogString , "'," ) ;
  4770. }
  4771. getLastName ( lString , aDefaultPathAndFile ) ;
  4772. if ( strlen(lString) )
  4773. {
  4774. strcat(lDialogString, "initialfile='") ;
  4775. strcat(lDialogString, lString ) ;
  4776. strcat(lDialogString , "'," ) ;
  4777. }
  4778. }
  4779. if ( ( aNumOfFilterPatterns > 1 )
  4780. || ( ( aNumOfFilterPatterns == 1 ) /*test because poor osx behaviour*/
  4781. && ( aFilterPatterns[0][strlen(aFilterPatterns[0])-1] != '*' ) ) )
  4782. {
  4783. strcat(lDialogString , "filetypes=(" ) ;
  4784. strcat ( lDialogString , "('" ) ;
  4785. if ( aSingleFilterDescription && strlen(aSingleFilterDescription) )
  4786. {
  4787. strcat ( lDialogString , aSingleFilterDescription ) ;
  4788. }
  4789. strcat ( lDialogString , "',(" ) ;
  4790. for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ )
  4791. {
  4792. strcat ( lDialogString , "'" ) ;
  4793. strcat ( lDialogString , aFilterPatterns [i] ) ;
  4794. strcat ( lDialogString , "'," ) ;
  4795. }
  4796. strcat ( lDialogString , "))," ) ;
  4797. strcat ( lDialogString , "('All files','*'))" ) ;
  4798. }
  4799. strcat ( lDialogString , ");\
  4800. \nif not isinstance(lFiles, tuple):\n\tprint lFiles\nelse:\
  4801. \n\tlFilesString=''\n\tfor lFile in lFiles:\n\t\tlFilesString+=str(lFile)+'|'\
  4802. \n\tprint lFilesString[:-1]\n\"" ) ;
  4803. }
  4804. else if ( xdialogPresent() || dialogName() )
  4805. {
  4806. if ( xdialogPresent ( ) )
  4807. {
  4808. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char const *)1;}
  4809. lWasGraphicDialog = 1 ;
  4810. strcpy ( lDialogString , "(Xdialog " ) ;
  4811. }
  4812. else if ( isTerminalRunning ( ) )
  4813. {
  4814. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;}
  4815. strcpy ( lDialogString , "(dialog " ) ;
  4816. }
  4817. else
  4818. {
  4819. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;}
  4820. lWasXterm = 1 ;
  4821. strcpy ( lDialogString , terminalName() ) ;
  4822. strcat ( lDialogString , "'(" ) ;
  4823. strcat ( lDialogString , dialogName() ) ;
  4824. strcat ( lDialogString , " " ) ;
  4825. }
  4826. if ( aTitle && strlen(aTitle) )
  4827. {
  4828. strcat(lDialogString, "--title \"") ;
  4829. strcat(lDialogString, aTitle) ;
  4830. strcat(lDialogString, "\" ") ;
  4831. }
  4832. if ( !xdialogPresent() && !gdialogPresent() )
  4833. {
  4834. strcat(lDialogString, "--backtitle \"") ;
  4835. strcat(lDialogString,
  4836. "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ;
  4837. strcat(lDialogString, "\" ") ;
  4838. }
  4839. strcat ( lDialogString , "--fselect \"" ) ;
  4840. if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) )
  4841. {
  4842. if ( ! strchr(aDefaultPathAndFile, '/') )
  4843. {
  4844. strcat(lDialogString, "./") ;
  4845. }
  4846. strcat(lDialogString, aDefaultPathAndFile) ;
  4847. }
  4848. else if ( ! isTerminalRunning ( ) && !lWasGraphicDialog )
  4849. {
  4850. strcat(lDialogString, getenv("HOME")) ;
  4851. strcat(lDialogString, "/");
  4852. }
  4853. else
  4854. {
  4855. strcat(lDialogString, "./") ;
  4856. }
  4857. if ( lWasGraphicDialog )
  4858. {
  4859. strcat(lDialogString, "\" 0 60 ) 2>&1 ") ;
  4860. }
  4861. else
  4862. {
  4863. strcat(lDialogString, "\" 0 60 >/dev/tty) ") ;
  4864. if ( lWasXterm )
  4865. {
  4866. strcat ( lDialogString ,
  4867. "2>/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt");
  4868. }
  4869. else
  4870. {
  4871. strcat(lDialogString, "2>&1 ; clear >/dev/tty") ;
  4872. }
  4873. }
  4874. }
  4875. else
  4876. {
  4877. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){return tinyfd_inputBox (aTitle,NULL,NULL);}
  4878. p2 = tinyfd_inputBox(aTitle, "Open file","");
  4879. if ( ! fileExists (p2) )
  4880. {
  4881. return NULL ;
  4882. }
  4883. return p2 ;
  4884. }
  4885. if (tinyfd_verbose) printf ( "lDialogString: %s\n" , lDialogString ) ;
  4886. if ( ! ( lIn = popen ( lDialogString , "r" ) ) )
  4887. {
  4888. return NULL ;
  4889. }
  4890. lBuff[0]='\0';
  4891. p=lBuff;
  4892. while ( fgets ( p , sizeof ( lBuff ) , lIn ) != NULL )
  4893. {
  4894. p += strlen ( p );
  4895. }
  4896. pclose ( lIn ) ;
  4897. if ( lBuff[strlen ( lBuff ) -1] == '\n' )
  4898. {
  4899. lBuff[strlen ( lBuff ) -1] = '\0' ;
  4900. }
  4901. /* printf ( "lBuff: %s\n" , lBuff ) ; */
  4902. if ( lWasKdialog && aAllowMultipleSelects )
  4903. {
  4904. p = lBuff ;
  4905. while ( ( p = strchr ( p , '\n' ) ) )
  4906. * p = '|' ;
  4907. }
  4908. /* printf ( "lBuff2: %s\n" , lBuff ) ; */
  4909. if ( ! strlen ( lBuff ) )
  4910. {
  4911. return NULL;
  4912. }
  4913. if ( aAllowMultipleSelects && strchr(lBuff, '|') )
  4914. {
  4915. p2 = ensureFilesExist( lBuff , lBuff ) ;
  4916. }
  4917. else if ( fileExists (lBuff) )
  4918. {
  4919. p2 = lBuff ;
  4920. }
  4921. else
  4922. {
  4923. return NULL ;
  4924. }
  4925. /* printf ( "lBuff3: %s\n" , p2 ) ; */
  4926. return p2 ;
  4927. }
  4928. char const * tinyfd_selectFolderDialog (
  4929. char const * const aTitle , /* "" */
  4930. char const * const aDefaultPath ) /* "" */
  4931. {
  4932. static char lBuff [MAX_PATH_OR_CMD] ;
  4933. char lDialogString [MAX_PATH_OR_CMD] ;
  4934. FILE * lIn ;
  4935. char const * p ;
  4936. int lWasGraphicDialog = 0 ;
  4937. int lWasXterm = 0 ;
  4938. lBuff[0]='\0';
  4939. if ( osascriptPresent ( ))
  4940. {
  4941. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char const *)1;}
  4942. strcpy ( lDialogString , "osascript ");
  4943. if ( ! osx9orBetter() ) strcat ( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'");
  4944. strcat ( lDialogString , " -e 'try' -e 'POSIX path of ( choose folder ");
  4945. if ( aTitle && strlen(aTitle) )
  4946. {
  4947. strcat(lDialogString, "with prompt \"") ;
  4948. strcat(lDialogString, aTitle) ;
  4949. strcat(lDialogString, "\" ") ;
  4950. }
  4951. if ( aDefaultPath && strlen(aDefaultPath) )
  4952. {
  4953. strcat(lDialogString, "default location \"") ;
  4954. strcat(lDialogString, aDefaultPath ) ;
  4955. strcat(lDialogString , "\" " ) ;
  4956. }
  4957. strcat ( lDialogString , ")' " ) ;
  4958. strcat(lDialogString, "-e 'on error number -128' " ) ;
  4959. strcat(lDialogString, "-e 'end try'") ;
  4960. if ( ! osx9orBetter() ) strcat ( lDialogString, " -e 'end tell'") ;
  4961. }
  4962. else if ( zenityPresent() || matedialogPresent() || qarmaPresent() )
  4963. {
  4964. if ( zenityPresent() )
  4965. {
  4966. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char const *)1;}
  4967. strcpy ( lDialogString , "zenity --file-selection --directory" ) ;
  4968. }
  4969. else if ( matedialogPresent() )
  4970. {
  4971. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char const *)1;}
  4972. strcpy ( lDialogString , "matedialog --file-selection --directory" ) ;
  4973. }
  4974. else
  4975. {
  4976. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char const *)1;}
  4977. strcpy ( lDialogString , "qarma --file-selection --directory" ) ;
  4978. }
  4979. if ( aTitle && strlen(aTitle) )
  4980. {
  4981. strcat(lDialogString, " --title=\"") ;
  4982. strcat(lDialogString, aTitle) ;
  4983. strcat(lDialogString, "\"") ;
  4984. }
  4985. if ( aDefaultPath && strlen(aDefaultPath) )
  4986. {
  4987. strcat(lDialogString, " --filename=\"") ;
  4988. strcat(lDialogString, aDefaultPath) ;
  4989. strcat(lDialogString, "\"") ;
  4990. }
  4991. }
  4992. else if ( kdialogPresent() )
  4993. {
  4994. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char const *)1;}
  4995. strcpy ( lDialogString , "kdialog --getexistingdirectory" ) ;
  4996. if ( aDefaultPath && strlen(aDefaultPath) )
  4997. {
  4998. strcat(lDialogString, " \"") ;
  4999. strcat(lDialogString, aDefaultPath ) ;
  5000. strcat(lDialogString , "\"" ) ;
  5001. }
  5002. else
  5003. {
  5004. strcat(lDialogString, " :" ) ;
  5005. }
  5006. if ( aTitle && strlen(aTitle) )
  5007. {
  5008. strcat(lDialogString, " --title \"") ;
  5009. strcat(lDialogString, aTitle) ;
  5010. strcat(lDialogString, "\"") ;
  5011. }
  5012. }
  5013. else if ( ! xdialogPresent() && tkinter2Present ( ) )
  5014. {
  5015. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"tkinter");return (char const *)1;}
  5016. strcpy ( lDialogString , gPython2Name ) ;
  5017. if ( ! isTerminalRunning ( ) && isDarwin ( ) )
  5018. {
  5019. strcat ( lDialogString , " -i" ) ; /* for osx without console */
  5020. }
  5021. strcat ( lDialogString ,
  5022. " -c \"import Tkinter,tkFileDialog;root=Tkinter.Tk();root.withdraw();");
  5023. if ( isDarwin ( ) )
  5024. {
  5025. strcat ( lDialogString ,
  5026. "import os;os.system('''/usr/bin/osascript -e 'tell app \\\"Finder\\\" to set \
  5027. frontmost of process \\\"Python\\\" to true' ''');");
  5028. }
  5029. strcat ( lDialogString , "print tkFileDialog.askdirectory(");
  5030. if ( aTitle && strlen(aTitle) )
  5031. {
  5032. strcat(lDialogString, "title='") ;
  5033. strcat(lDialogString, aTitle) ;
  5034. strcat(lDialogString, "',") ;
  5035. }
  5036. if ( aDefaultPath && strlen(aDefaultPath) )
  5037. {
  5038. strcat(lDialogString, "initialdir='") ;
  5039. strcat(lDialogString, aDefaultPath ) ;
  5040. strcat(lDialogString , "'" ) ;
  5041. }
  5042. strcat ( lDialogString , ")\"" ) ;
  5043. }
  5044. else if ( xdialogPresent() || dialogName() )
  5045. {
  5046. if ( xdialogPresent ( ) )
  5047. {
  5048. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char const *)1;}
  5049. lWasGraphicDialog = 1 ;
  5050. strcpy ( lDialogString , "(Xdialog " ) ;
  5051. }
  5052. else if ( isTerminalRunning ( ) )
  5053. {
  5054. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;}
  5055. strcpy ( lDialogString , "(dialog " ) ;
  5056. }
  5057. else
  5058. {
  5059. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;}
  5060. lWasXterm = 1 ;
  5061. strcpy ( lDialogString , terminalName() ) ;
  5062. strcat ( lDialogString , "'(" ) ;
  5063. strcat ( lDialogString , dialogName() ) ;
  5064. strcat ( lDialogString , " " ) ;
  5065. }
  5066. if ( aTitle && strlen(aTitle) )
  5067. {
  5068. strcat(lDialogString, "--title \"") ;
  5069. strcat(lDialogString, aTitle) ;
  5070. strcat(lDialogString, "\" ") ;
  5071. }
  5072. if ( !xdialogPresent() && !gdialogPresent() )
  5073. {
  5074. strcat(lDialogString, "--backtitle \"") ;
  5075. strcat(lDialogString,
  5076. "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ;
  5077. strcat(lDialogString, "\" ") ;
  5078. }
  5079. strcat ( lDialogString , "--dselect \"" ) ;
  5080. if ( aDefaultPath && strlen(aDefaultPath) )
  5081. {
  5082. strcat(lDialogString, aDefaultPath) ;
  5083. ensureFinalSlash(lDialogString);
  5084. }
  5085. else if ( ! isTerminalRunning ( ) && !lWasGraphicDialog )
  5086. {
  5087. strcat(lDialogString, getenv("HOME")) ;
  5088. strcat(lDialogString, "/");
  5089. }
  5090. else
  5091. {
  5092. strcat(lDialogString, "./") ;
  5093. }
  5094. if ( lWasGraphicDialog )
  5095. {
  5096. strcat(lDialogString, "\" 0 60 ) 2>&1 ") ;
  5097. }
  5098. else
  5099. {
  5100. strcat(lDialogString, "\" 0 60 >/dev/tty) ") ;
  5101. if ( lWasXterm )
  5102. {
  5103. strcat ( lDialogString ,
  5104. "2>/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt");
  5105. }
  5106. else
  5107. {
  5108. strcat(lDialogString, "2>&1 ; clear >/dev/tty") ;
  5109. }
  5110. }
  5111. }
  5112. else
  5113. {
  5114. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){return tinyfd_inputBox (aTitle,NULL,NULL);}
  5115. p = tinyfd_inputBox(aTitle, "Select folder","");
  5116. if ( !p || ! strlen ( p ) || ! dirExists ( p ) )
  5117. {
  5118. return NULL ;
  5119. }
  5120. return p ;
  5121. }
  5122. if (tinyfd_verbose) printf ( "lDialogString: %s\n" , lDialogString ) ;
  5123. if ( ! ( lIn = popen ( lDialogString , "r" ) ) )
  5124. {
  5125. return NULL ;
  5126. }
  5127. while ( fgets ( lBuff , sizeof ( lBuff ) , lIn ) != NULL )
  5128. {}
  5129. pclose ( lIn ) ;
  5130. if ( lBuff[strlen ( lBuff ) -1] == '\n' )
  5131. {
  5132. lBuff[strlen ( lBuff ) -1] = '\0' ;
  5133. }
  5134. /* printf ( "lBuff: %s\n" , lBuff ) ; */
  5135. if ( ! strlen ( lBuff ) || ! dirExists ( lBuff ) )
  5136. {
  5137. return NULL ;
  5138. }
  5139. return lBuff ;
  5140. }
  5141. /* returns the hexcolor as a string "#FF0000" */
  5142. /* aoResultRGB also contains the result */
  5143. /* aDefaultRGB is used only if aDefaultHexRGB is NULL */
  5144. /* aDefaultRGB and aoResultRGB can be the same array */
  5145. char const * tinyfd_colorChooser(
  5146. char const * const aTitle , /* NULL or "" */
  5147. char const * const aDefaultHexRGB , /* NULL or "#FF0000"*/
  5148. unsigned char const aDefaultRGB[3] , /* { 0 , 255 , 255 } */
  5149. unsigned char aoResultRGB[3] ) /* { 0 , 0 , 0 } */
  5150. {
  5151. static char lBuff [128] ;
  5152. char lTmp [128] ;
  5153. char lDialogString [MAX_PATH_OR_CMD] ;
  5154. char lDefaultHexRGB[8];
  5155. char * lpDefaultHexRGB;
  5156. unsigned char lDefaultRGB[3];
  5157. char const * p;
  5158. FILE * lIn ;
  5159. int i ;
  5160. int lWasZenity3 = 0 ;
  5161. int lWasOsascript = 0 ;
  5162. int lWasXdialog = 0 ;
  5163. lBuff[0]='\0';
  5164. if ( aDefaultHexRGB )
  5165. {
  5166. Hex2RGB ( aDefaultHexRGB , lDefaultRGB ) ;
  5167. lpDefaultHexRGB = (char *) aDefaultHexRGB ;
  5168. }
  5169. else
  5170. {
  5171. lDefaultRGB[0]=aDefaultRGB[0];
  5172. lDefaultRGB[1]=aDefaultRGB[1];
  5173. lDefaultRGB[2]=aDefaultRGB[2];
  5174. RGB2Hex( aDefaultRGB , lDefaultHexRGB ) ;
  5175. lpDefaultHexRGB = (char *) lDefaultHexRGB ;
  5176. }
  5177. if ( osascriptPresent ( ) )
  5178. {
  5179. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char const *)1;}
  5180. lWasOsascript = 1 ;
  5181. strcpy ( lDialogString , "osascript");
  5182. if ( ! osx9orBetter() )
  5183. {
  5184. strcat ( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'");
  5185. strcat ( lDialogString , " -e 'try' -e 'set mycolor to choose color default color {");
  5186. }
  5187. else
  5188. {
  5189. strcat ( lDialogString ,
  5190. " -e 'try' -e 'tell app (path to frontmost application as Unicode text) \
  5191. to set mycolor to choose color default color {");
  5192. }
  5193. sprintf(lTmp, "%d", 256 * lDefaultRGB[0] ) ;
  5194. strcat(lDialogString, lTmp ) ;
  5195. strcat(lDialogString, "," ) ;
  5196. sprintf(lTmp, "%d", 256 * lDefaultRGB[1] ) ;
  5197. strcat(lDialogString, lTmp ) ;
  5198. strcat(lDialogString, "," ) ;
  5199. sprintf(lTmp, "%d", 256 * lDefaultRGB[2] ) ;
  5200. strcat(lDialogString, lTmp ) ;
  5201. strcat(lDialogString, "}' " ) ;
  5202. strcat ( lDialogString ,
  5203. "-e 'set mystring to ((item 1 of mycolor) div 256 as integer) as string' " );
  5204. strcat ( lDialogString ,
  5205. "-e 'repeat with i from 2 to the count of mycolor' " );
  5206. strcat ( lDialogString ,
  5207. "-e 'set mystring to mystring & \" \" & ((item i of mycolor) div 256 as integer) as string' " );
  5208. strcat ( lDialogString , "-e 'end repeat' " );
  5209. strcat ( lDialogString , "-e 'mystring' ");
  5210. strcat(lDialogString, "-e 'on error number -128' " ) ;
  5211. strcat(lDialogString, "-e 'end try'") ;
  5212. if ( ! osx9orBetter() ) strcat ( lDialogString, " -e 'end tell'") ;
  5213. }
  5214. else if ( zenity3Present() || matedialogPresent() || qarmaPresent() )
  5215. {
  5216. lWasZenity3 = 1 ;
  5217. if ( zenity3Present() )
  5218. {
  5219. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity3");return (char const *)1;}
  5220. sprintf ( lDialogString ,
  5221. "zenity --color-selection --show-palette --color=%s" , lpDefaultHexRGB ) ;
  5222. }
  5223. else if ( matedialogPresent() )
  5224. {
  5225. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char const *)1;}
  5226. sprintf ( lDialogString ,
  5227. "matedialog --color-selection --show-palette --color=%s" , lpDefaultHexRGB ) ;
  5228. }
  5229. else
  5230. {
  5231. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char const *)1;}
  5232. sprintf ( lDialogString ,
  5233. "qarma --color-selection --show-palette --color=%s" , lpDefaultHexRGB ) ;
  5234. }
  5235. if ( aTitle && strlen(aTitle) )
  5236. {
  5237. strcat(lDialogString, " --title=\"") ;
  5238. strcat(lDialogString, aTitle) ;
  5239. strcat(lDialogString, "\"") ;
  5240. }
  5241. }
  5242. else if ( kdialogPresent() )
  5243. {
  5244. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char const *)1;}
  5245. sprintf ( lDialogString ,
  5246. "kdialog --getcolor --default '%s'" , lpDefaultHexRGB ) ;
  5247. if ( aTitle && strlen(aTitle) )
  5248. {
  5249. strcat(lDialogString, " --title \"") ;
  5250. strcat(lDialogString, aTitle) ;
  5251. strcat(lDialogString, "\"") ;
  5252. }
  5253. }
  5254. else if ( xdialogPresent() )
  5255. {
  5256. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char const *)1;}
  5257. lWasXdialog = 1 ;
  5258. strcpy ( lDialogString , "Xdialog --colorsel \"" ) ;
  5259. if ( aTitle && strlen(aTitle) )
  5260. {
  5261. strcat(lDialogString, aTitle) ;
  5262. }
  5263. strcat(lDialogString, "\" 0 60 ") ;
  5264. sprintf(lTmp,"%hhu %hhu %hhu",lDefaultRGB[0],
  5265. lDefaultRGB[1],lDefaultRGB[2]);
  5266. strcat(lDialogString, lTmp) ;
  5267. strcat(lDialogString, " 2>&1");
  5268. }
  5269. else if ( tkinter2Present ( ) )
  5270. {
  5271. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"tkinter");return (char const *)1;}
  5272. strcpy ( lDialogString , gPython2Name ) ;
  5273. if ( ! isTerminalRunning ( ) && isDarwin ( ) )
  5274. {
  5275. strcat ( lDialogString , " -i" ) ; /* for osx without console */
  5276. }
  5277. strcat ( lDialogString ,
  5278. " -c \"import Tkinter,tkColorChooser;root=Tkinter.Tk();root.withdraw();");
  5279. if ( isDarwin ( ) )
  5280. {
  5281. strcat ( lDialogString ,
  5282. "import os;os.system('''osascript -e 'tell app \\\"Finder\\\" to set \
  5283. frontmost of process \\\"Python\\\" to true' ''');");
  5284. }
  5285. strcat ( lDialogString , "res=tkColorChooser.askcolor(color='" ) ;
  5286. strcat(lDialogString, lpDefaultHexRGB ) ;
  5287. strcat(lDialogString, "'") ;
  5288. if ( aTitle && strlen(aTitle) )
  5289. {
  5290. strcat(lDialogString, ",title='") ;
  5291. strcat(lDialogString, aTitle) ;
  5292. strcat(lDialogString, "'") ;
  5293. }
  5294. strcat ( lDialogString , ");\
  5295. \nif res[1] is not None:\n\tprint res[1]\"" ) ;
  5296. }
  5297. else
  5298. {
  5299. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){return tinyfd_inputBox (aTitle,NULL,NULL);}
  5300. p = tinyfd_inputBox(aTitle,
  5301. "Enter hex rgb color (i.e. #f5ca20)",lpDefaultHexRGB);
  5302. if ( !p || (strlen(p) != 7) || (p[0] != '#') )
  5303. {
  5304. return NULL ;
  5305. }
  5306. for ( i = 1 ; i < 7 ; i ++ )
  5307. {
  5308. if ( ! isxdigit( p[i] ) )
  5309. {
  5310. return NULL ;
  5311. }
  5312. }
  5313. Hex2RGB(p,aoResultRGB);
  5314. return p ;
  5315. }
  5316. if (tinyfd_verbose) printf ( "lDialogString: %s\n" , lDialogString ) ;
  5317. if ( ! ( lIn = popen ( lDialogString , "r" ) ) )
  5318. {
  5319. return NULL ;
  5320. }
  5321. while ( fgets ( lBuff , sizeof ( lBuff ) , lIn ) != NULL )
  5322. {
  5323. }
  5324. pclose ( lIn ) ;
  5325. if ( ! strlen ( lBuff ) )
  5326. {
  5327. return NULL ;
  5328. }
  5329. /* printf ( "len Buff: %lu\n" , strlen(lBuff) ) ; */
  5330. /* printf ( "lBuff0: %s\n" , lBuff ) ; */
  5331. if ( lBuff[strlen ( lBuff ) -1] == '\n' )
  5332. {
  5333. lBuff[strlen ( lBuff ) -1] = '\0' ;
  5334. }
  5335. if ( lWasZenity3 )
  5336. {
  5337. if ( lBuff[0] == '#' ) {
  5338. lBuff[3]=lBuff[5];
  5339. lBuff[4]=lBuff[6];
  5340. lBuff[5]=lBuff[9];
  5341. lBuff[6]=lBuff[10];
  5342. lBuff[7]='\0';
  5343. Hex2RGB(lBuff,aoResultRGB);
  5344. }
  5345. else if ( lBuff[3] == '(' ) {
  5346. sscanf(lBuff,"rgb(%hhu,%hhu,%hhu",
  5347. & aoResultRGB[0], & aoResultRGB[1],& aoResultRGB[2]);
  5348. RGB2Hex(aoResultRGB,lBuff);
  5349. }
  5350. else if ( lBuff[4] == '(' ) {
  5351. sscanf(lBuff,"rgba(%hhu,%hhu,%hhu",
  5352. & aoResultRGB[0], & aoResultRGB[1],& aoResultRGB[2]);
  5353. RGB2Hex(aoResultRGB,lBuff);
  5354. }
  5355. }
  5356. else if ( lWasOsascript || lWasXdialog )
  5357. {
  5358. /* printf ( "lBuff: %s\n" , lBuff ) ; */
  5359. sscanf(lBuff,"%hhu %hhu %hhu",
  5360. & aoResultRGB[0], & aoResultRGB[1],& aoResultRGB[2]);
  5361. RGB2Hex(aoResultRGB,lBuff);
  5362. }
  5363. else
  5364. {
  5365. Hex2RGB(lBuff,aoResultRGB);
  5366. }
  5367. /* printf("%d %d %d\n", aoResultRGB[0],aoResultRGB[1],aoResultRGB[2]); */
  5368. /* printf ( "lBuff: %s\n" , lBuff ) ; */
  5369. return lBuff ;
  5370. }
  5371. /* not cross platform - zenity only */
  5372. /* contributed by Attila Dusnoki */
  5373. char const * tinyfd_arrayDialog (
  5374. char const * const aTitle , /* "" */
  5375. int const aNumOfColumns , /* 2 */
  5376. char const * const * const aColumns , /* {"Column 1","Column 2"} */
  5377. int const aNumOfRows , /* 2 */
  5378. char const * const * const aCells )
  5379. /* {"Row1 Col1","Row1 Col2","Row2 Col1","Row2 Col2"} */
  5380. {
  5381. static char lBuff [MAX_PATH_OR_CMD] ;
  5382. char lDialogString [MAX_PATH_OR_CMD] ;
  5383. FILE * lIn ;
  5384. lBuff[0]='\0';
  5385. int i ;
  5386. if ( zenityPresent() )
  5387. {
  5388. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char const *)1;}
  5389. strcpy ( lDialogString , "zenity --list --print-column=ALL" ) ;
  5390. if ( aTitle && strlen(aTitle) )
  5391. {
  5392. strcat(lDialogString, " --title=\"") ;
  5393. strcat(lDialogString, aTitle) ;
  5394. strcat(lDialogString, "\"") ;
  5395. }
  5396. if ( aColumns && (aNumOfColumns > 0) )
  5397. {
  5398. for ( i = 0 ; i < aNumOfColumns ; i ++ )
  5399. {
  5400. strcat ( lDialogString , " --column=\"" ) ;
  5401. strcat ( lDialogString , aColumns [i] ) ;
  5402. strcat ( lDialogString , "\"" ) ;
  5403. }
  5404. }
  5405. if ( aCells && (aNumOfRows > 0) )
  5406. {
  5407. strcat ( lDialogString , " " ) ;
  5408. for ( i = 0 ; i < aNumOfRows*aNumOfColumns ; i ++ )
  5409. {
  5410. strcat ( lDialogString , "\"" ) ;
  5411. strcat ( lDialogString , aCells [i] ) ;
  5412. strcat ( lDialogString , "\" " ) ;
  5413. }
  5414. }
  5415. }
  5416. else
  5417. {
  5418. if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"");return (char const *)0;}
  5419. return NULL ;
  5420. }
  5421. if (tinyfd_verbose) printf ( "lDialogString: %s\n" , lDialogString ) ;
  5422. if ( ! ( lIn = popen ( lDialogString , "r" ) ) )
  5423. {
  5424. return NULL ;
  5425. }
  5426. while ( fgets ( lBuff , sizeof ( lBuff ) , lIn ) != NULL )
  5427. {}
  5428. pclose ( lIn ) ;
  5429. if ( lBuff[strlen ( lBuff ) -1] == '\n' )
  5430. {
  5431. lBuff[strlen ( lBuff ) -1] = '\0' ;
  5432. }
  5433. /* printf ( "lBuff: %s\n" , lBuff ) ; */
  5434. if ( ! strlen ( lBuff ) )
  5435. {
  5436. return NULL ;
  5437. }
  5438. return lBuff ;
  5439. }
  5440. #endif /* _WIN32 */
  5441. /*
  5442. int main(void)
  5443. {
  5444. char const * lTmp;
  5445. char const * lTheSaveFileName;
  5446. char const * lTheOpenFileName;
  5447. char const * lTheSelectFolderName;
  5448. char const * lTheHexColor;
  5449. char const * lWillBeGraphicMode;
  5450. unsigned char lRgbColor[3];
  5451. FILE * lIn;
  5452. char lBuffer[1024];
  5453. char lThePassword[1024];
  5454. char const * lFilterPatterns[2] = { "*.txt", "*.text" };
  5455. lWillBeGraphicMode = tinyfd_inputBox("tinyfd_query", NULL, NULL);
  5456. if (lWillBeGraphicMode)
  5457. {
  5458. strcpy(lBuffer, "graphic mode: ");
  5459. }
  5460. else
  5461. {
  5462. strcpy(lBuffer, "console mode: ");
  5463. }
  5464. strcat(lBuffer, tinyfd_response);
  5465. strcpy(lThePassword, "tinyfiledialogs v");
  5466. strcat(lThePassword, tinyfd_version);
  5467. tinyfd_messageBox(lThePassword, lBuffer, "ok", "info", 0);
  5468. if (lWillBeGraphicMode && !tinyfd_forceConsole)
  5469. {
  5470. tinyfd_forceConsole = ! tinyfd_messageBox("Hello World",
  5471. "graphic dialogs [yes] / console mode [no]?",
  5472. "yesno", "question", 1);
  5473. }
  5474. lTmp = tinyfd_inputBox(
  5475. "a password box", "your password will be revealed", NULL);
  5476. if (!lTmp) return 1;
  5477. strcpy(lThePassword, lTmp);
  5478. lTheSaveFileName = tinyfd_saveFileDialog(
  5479. "let us save this password",
  5480. "passwordFile.txt",
  5481. 2,
  5482. lFilterPatterns,
  5483. NULL);
  5484. if (!lTheSaveFileName)
  5485. {
  5486. tinyfd_messageBox(
  5487. "Error",
  5488. "Save file name is NULL",
  5489. "ok",
  5490. "error",
  5491. 1);
  5492. return 1;
  5493. }
  5494. lIn = fopen(lTheSaveFileName, "w");
  5495. if (!lIn)
  5496. {
  5497. tinyfd_messageBox(
  5498. "Error",
  5499. "Can not open this file in write mode",
  5500. "ok",
  5501. "error",
  5502. 1);
  5503. return 1;
  5504. }
  5505. fputs(lThePassword, lIn);
  5506. fclose(lIn);
  5507. lTheOpenFileName = tinyfd_openFileDialog(
  5508. "let us read the password back",
  5509. "",
  5510. 2,
  5511. lFilterPatterns,
  5512. NULL,
  5513. 0);
  5514. if (!lTheOpenFileName)
  5515. {
  5516. tinyfd_messageBox(
  5517. "Error",
  5518. "Open file name is NULL",
  5519. "ok",
  5520. "error",
  5521. 1);
  5522. return 1;
  5523. }
  5524. lIn = fopen(lTheOpenFileName, "r");
  5525. if (!lIn)
  5526. {
  5527. tinyfd_messageBox(
  5528. "Error",
  5529. "Can not open this file in read mode",
  5530. "ok",
  5531. "error",
  5532. 1);
  5533. return(1);
  5534. }
  5535. lBuffer[0] = '\0';
  5536. fgets(lBuffer, sizeof(lBuffer), lIn);
  5537. fclose(lIn);
  5538. tinyfd_messageBox("your password is",
  5539. lBuffer, "ok", "info", 1);
  5540. lTheSelectFolderName = tinyfd_selectFolderDialog(
  5541. "let us just select a directory", NULL);
  5542. if (!lTheSelectFolderName)
  5543. {
  5544. tinyfd_messageBox(
  5545. "Error",
  5546. "Select folder name is NULL",
  5547. "ok",
  5548. "error",
  5549. 1);
  5550. return 1;
  5551. }
  5552. tinyfd_messageBox("The selected folder is",
  5553. lTheSelectFolderName, "ok", "info", 1);
  5554. lTheHexColor = tinyfd_colorChooser(
  5555. "choose a nice color",
  5556. "#FF0077",
  5557. lRgbColor,
  5558. lRgbColor);
  5559. if (!lTheHexColor)
  5560. {
  5561. tinyfd_messageBox(
  5562. "Error",
  5563. "hexcolor is NULL",
  5564. "ok",
  5565. "error",
  5566. 1);
  5567. return 1;
  5568. }
  5569. tinyfd_messageBox("The selected hexcolor is",
  5570. lTheHexColor, "ok", "info", 1);
  5571. return 0;
  5572. }
  5573. */
  5574. #ifdef _MSC_VER
  5575. #pragma warning(default:4996)
  5576. #pragma warning(default:4100)
  5577. #pragma warning(default:4706)
  5578. #endif