tinyfiledialogs.c 150 KB

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