atldlgs.h 164 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233
  1. // Windows Template Library - WTL version 10.0
  2. // Copyright (C) Microsoft Corporation, WTL Team. All rights reserved.
  3. //
  4. // This file is a part of the Windows Template Library.
  5. // The use and distribution terms for this software are covered by the
  6. // Microsoft Public License (http://opensource.org/licenses/MS-PL)
  7. // which can be found in the file MS-PL.txt at the root folder.
  8. #ifndef __ATLDLGS_H__
  9. #define __ATLDLGS_H__
  10. #pragma once
  11. #ifndef __ATLAPP_H__
  12. #error atldlgs.h requires atlapp.h to be included first
  13. #endif
  14. #ifndef __ATLWIN_H__
  15. #error atldlgs.h requires atlwin.h to be included first
  16. #endif
  17. #include <shlobj.h>
  18. #if (_WIN32_WINNT >= 0x0600)
  19. #include <shobjidl.h>
  20. #endif // (_WIN32_WINNT >= 0x0600)
  21. ///////////////////////////////////////////////////////////////////////////////
  22. // Classes in this file:
  23. //
  24. // CFileDialogImpl<T>
  25. // CFileDialog
  26. // CSimpleFileDialog
  27. // CMultiFileDialogImpl<T>
  28. // CMultiFileDialog
  29. // CShellFileDialogImpl<T>
  30. // CShellFileOpenDialogImpl<T>
  31. // CShellFileOpenDialog
  32. // CShellFileSaveDialogImpl<T>
  33. // CShellFileSaveDialog
  34. // CFolderDialogImpl<T>
  35. // CFolderDialog
  36. // CFontDialogImpl<T>
  37. // CFontDialog
  38. // CRichEditFontDialogImpl<T>
  39. // CRichEditFontDialog
  40. // CColorDialogImpl<T>
  41. // CColorDialog
  42. // CPrintDialogImpl<T>
  43. // CPrintDialog
  44. // CPrintDialogExImpl<T>
  45. // CPrintDialogEx
  46. // CPageSetupDialogImpl<T>
  47. // CPageSetupDialog
  48. // CFindReplaceDialogImpl<T>
  49. // CFindReplaceDialog
  50. //
  51. // CDialogBaseUnits
  52. // CMemDlgTemplate
  53. // CIndirectDialogImpl<T, TDlgTemplate, TBase>
  54. //
  55. // CPropertySheetWindow
  56. // CPropertySheetImpl<T, TBase>
  57. // CPropertySheet
  58. // CPropertyPageWindow
  59. // CPropertyPageImpl<T, TBase>
  60. // CPropertyPage<t_wDlgTemplateID>
  61. // CAxPropertyPageImpl<T, TBase>
  62. // CAxPropertyPage<t_wDlgTemplateID>
  63. //
  64. // CWizard97SheetWindow
  65. // CWizard97SheetImpl<T, TBase>
  66. // CWizard97Sheet
  67. // CWizard97PageWindow
  68. // CWizard97PageImpl<T, TBase>
  69. // CWizard97ExteriorPageImpl<T, TBase>
  70. // CWizard97InteriorPageImpl<T, TBase>
  71. //
  72. // CAeroWizardFrameWindow
  73. // CAeroWizardFrameImpl<T, TBase>
  74. // CAeroWizardFrame
  75. // CAeroWizardPageWindow
  76. // CAeroWizardPageImpl<T, TBase>
  77. // CAeroWizardPage<t_wDlgTemplateID>
  78. // CAeroWizardAxPageImpl<T, TBase>
  79. // CAeroWizardAxPage<t_wDlgTemplateID>
  80. //
  81. // CTaskDialogConfig
  82. // CTaskDialogImpl<T>
  83. // CTaskDialog
  84. //
  85. // Global functions:
  86. // AtlTaskDialog()
  87. namespace WTL
  88. {
  89. ///////////////////////////////////////////////////////////////////////////////
  90. // CFileDialogImpl - used for File Open or File Save As
  91. template <class T>
  92. class ATL_NO_VTABLE CFileDialogImpl : public ATL::CDialogImplBase
  93. {
  94. public:
  95. OPENFILENAME m_ofn;
  96. BOOL m_bOpenFileDialog; // TRUE for file open, FALSE for file save
  97. TCHAR m_szFileTitle[_MAX_FNAME]; // contains file title after return
  98. TCHAR m_szFileName[_MAX_PATH]; // contains full path name after return
  99. CFileDialogImpl(BOOL bOpenFileDialog, // TRUE for FileOpen, FALSE for FileSaveAs
  100. LPCTSTR lpszDefExt = NULL,
  101. LPCTSTR lpszFileName = NULL,
  102. DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
  103. LPCTSTR lpszFilter = NULL,
  104. HWND hWndParent = NULL) : m_bOpenFileDialog(bOpenFileDialog)
  105. {
  106. memset(&m_ofn, 0, sizeof(m_ofn)); // initialize structure to 0/NULL
  107. m_ofn.lStructSize = sizeof(m_ofn);
  108. m_ofn.lpstrFile = m_szFileName;
  109. m_ofn.nMaxFile = _MAX_PATH;
  110. m_ofn.lpstrDefExt = lpszDefExt;
  111. m_ofn.lpstrFileTitle = (LPTSTR)m_szFileTitle;
  112. m_ofn.nMaxFileTitle = _MAX_FNAME;
  113. m_ofn.Flags = dwFlags | OFN_EXPLORER | OFN_ENABLEHOOK | OFN_ENABLESIZING;
  114. m_ofn.lpstrFilter = lpszFilter;
  115. m_ofn.hInstance = ModuleHelper::GetResourceInstance();
  116. m_ofn.lpfnHook = (LPOFNHOOKPROC)T::StartDialogProc;
  117. m_ofn.hwndOwner = hWndParent;
  118. m_szFileName[0] = _T('\0');
  119. m_szFileTitle[0] = _T('\0');
  120. // setup initial file name
  121. if(lpszFileName != NULL)
  122. ATL::Checked::tcsncpy_s(m_szFileName, _countof(m_szFileName), lpszFileName, _TRUNCATE);
  123. }
  124. INT_PTR DoModal(HWND hWndParent = ::GetActiveWindow())
  125. {
  126. ATLASSERT((m_ofn.Flags & OFN_ENABLEHOOK) != 0);
  127. ATLASSERT(m_ofn.lpfnHook != NULL); // can still be a user hook
  128. ATLASSERT((m_ofn.Flags & OFN_EXPLORER) != 0);
  129. if(m_ofn.hwndOwner == NULL) // set only if not specified before
  130. m_ofn.hwndOwner = hWndParent;
  131. ATLASSERT(m_hWnd == NULL);
  132. // Allocate the thunk structure here, where we can fail gracefully.
  133. BOOL bRetTh = m_thunk.Init(NULL, NULL);
  134. if(bRetTh == FALSE)
  135. {
  136. ::SetLastError(ERROR_OUTOFMEMORY);
  137. return -1;
  138. }
  139. ModuleHelper::AddCreateWndData(&m_thunk.cd, (ATL::CDialogImplBase*)this);
  140. BOOL bRet = (m_bOpenFileDialog != FALSE) ? ::GetOpenFileName(&m_ofn) : ::GetSaveFileName(&m_ofn);
  141. m_hWnd = NULL;
  142. return (bRet != FALSE) ? IDOK : IDCANCEL;
  143. }
  144. // Attributes
  145. ATL::CWindow GetFileDialogWindow() const
  146. {
  147. ATLASSERT(::IsWindow(m_hWnd));
  148. return ATL::CWindow(GetParent());
  149. }
  150. int GetFilePath(LPTSTR lpstrFilePath, int nLength) const
  151. {
  152. ATLASSERT(::IsWindow(m_hWnd));
  153. ATLASSERT((m_ofn.Flags & OFN_EXPLORER) != 0);
  154. return (int)GetFileDialogWindow().SendMessage(CDM_GETFILEPATH, nLength, (LPARAM)lpstrFilePath);
  155. }
  156. int GetFolderIDList(LPVOID lpBuff, int nLength) const
  157. {
  158. ATLASSERT(::IsWindow(m_hWnd));
  159. ATLASSERT((m_ofn.Flags & OFN_EXPLORER) != 0);
  160. return (int)GetFileDialogWindow().SendMessage(CDM_GETFOLDERIDLIST, nLength, (LPARAM)lpBuff);
  161. }
  162. int GetFolderPath(LPTSTR lpstrFolderPath, int nLength) const
  163. {
  164. ATLASSERT(::IsWindow(m_hWnd));
  165. ATLASSERT((m_ofn.Flags & OFN_EXPLORER) != 0);
  166. return (int)GetFileDialogWindow().SendMessage(CDM_GETFOLDERPATH, nLength, (LPARAM)lpstrFolderPath);
  167. }
  168. int GetSpec(LPTSTR lpstrSpec, int nLength) const
  169. {
  170. ATLASSERT(::IsWindow(m_hWnd));
  171. ATLASSERT((m_ofn.Flags & OFN_EXPLORER) != 0);
  172. return (int)GetFileDialogWindow().SendMessage(CDM_GETSPEC, nLength, (LPARAM)lpstrSpec);
  173. }
  174. void SetControlText(int nCtrlID, LPCTSTR lpstrText)
  175. {
  176. ATLASSERT(::IsWindow(m_hWnd));
  177. ATLASSERT((m_ofn.Flags & OFN_EXPLORER) != 0);
  178. GetFileDialogWindow().SendMessage(CDM_SETCONTROLTEXT, nCtrlID, (LPARAM)lpstrText);
  179. }
  180. void SetDefExt(LPCTSTR lpstrExt)
  181. {
  182. ATLASSERT(::IsWindow(m_hWnd));
  183. ATLASSERT((m_ofn.Flags & OFN_EXPLORER) != 0);
  184. GetFileDialogWindow().SendMessage(CDM_SETDEFEXT, 0, (LPARAM)lpstrExt);
  185. }
  186. BOOL GetReadOnlyPref() const // return TRUE if readonly checked
  187. {
  188. return ((m_ofn.Flags & OFN_READONLY) != 0) ? TRUE : FALSE;
  189. }
  190. // Operations
  191. void HideControl(int nCtrlID)
  192. {
  193. ATLASSERT(::IsWindow(m_hWnd));
  194. ATLASSERT((m_ofn.Flags & OFN_EXPLORER) != 0);
  195. GetFileDialogWindow().SendMessage(CDM_HIDECONTROL, nCtrlID);
  196. }
  197. // Special override for common dialogs
  198. BOOL EndDialog(INT_PTR /*nRetCode*/ = 0)
  199. {
  200. ATLASSERT(::IsWindow(m_hWnd));
  201. GetFileDialogWindow().SendMessage(WM_COMMAND, MAKEWPARAM(IDCANCEL, 0));
  202. return TRUE;
  203. }
  204. // Message map and handlers
  205. BEGIN_MSG_MAP(CFileDialogImpl)
  206. NOTIFY_CODE_HANDLER(CDN_FILEOK, _OnFileOK)
  207. NOTIFY_CODE_HANDLER(CDN_FOLDERCHANGE, _OnFolderChange)
  208. NOTIFY_CODE_HANDLER(CDN_HELP, _OnHelp)
  209. NOTIFY_CODE_HANDLER(CDN_INITDONE, _OnInitDone)
  210. NOTIFY_CODE_HANDLER(CDN_SELCHANGE, _OnSelChange)
  211. NOTIFY_CODE_HANDLER(CDN_SHAREVIOLATION, _OnShareViolation)
  212. NOTIFY_CODE_HANDLER(CDN_TYPECHANGE, _OnTypeChange)
  213. NOTIFY_CODE_HANDLER(CDN_INCLUDEITEM, _OnIncludeItem)
  214. END_MSG_MAP()
  215. LRESULT _OnFileOK(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/)
  216. {
  217. ATLASSERT(::IsWindow(m_hWnd));
  218. T* pT = static_cast<T*>(this);
  219. return !pT->OnFileOK((LPOFNOTIFY)pnmh);
  220. }
  221. LRESULT _OnFolderChange(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/)
  222. {
  223. ATLASSERT(::IsWindow(m_hWnd));
  224. T* pT = static_cast<T*>(this);
  225. pT->OnFolderChange((LPOFNOTIFY)pnmh);
  226. return 0;
  227. }
  228. LRESULT _OnHelp(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/)
  229. {
  230. ATLASSERT(::IsWindow(m_hWnd));
  231. T* pT = static_cast<T*>(this);
  232. pT->OnHelp((LPOFNOTIFY)pnmh);
  233. return 0;
  234. }
  235. LRESULT _OnInitDone(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/)
  236. {
  237. ATLASSERT(::IsWindow(m_hWnd));
  238. T* pT = static_cast<T*>(this);
  239. pT->OnInitDone((LPOFNOTIFY)pnmh);
  240. return 0;
  241. }
  242. LRESULT _OnSelChange(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/)
  243. {
  244. ATLASSERT(::IsWindow(m_hWnd));
  245. T* pT = static_cast<T*>(this);
  246. pT->OnSelChange((LPOFNOTIFY)pnmh);
  247. return 0;
  248. }
  249. LRESULT _OnShareViolation(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/)
  250. {
  251. ATLASSERT(::IsWindow(m_hWnd));
  252. T* pT = static_cast<T*>(this);
  253. return pT->OnShareViolation((LPOFNOTIFY)pnmh);
  254. }
  255. LRESULT _OnTypeChange(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/)
  256. {
  257. ATLASSERT(::IsWindow(m_hWnd));
  258. T* pT = static_cast<T*>(this);
  259. pT->OnTypeChange((LPOFNOTIFY)pnmh);
  260. return 0;
  261. }
  262. LRESULT _OnIncludeItem(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/)
  263. {
  264. ATLASSERT(::IsWindow(m_hWnd));
  265. T* pT = static_cast<T*>(this);
  266. return pT->OnIncludeItem((LPOFNOTIFYEX)pnmh);
  267. }
  268. // Overrideables
  269. BOOL OnFileOK(LPOFNOTIFY /*lpon*/)
  270. {
  271. return TRUE;
  272. }
  273. void OnFolderChange(LPOFNOTIFY /*lpon*/)
  274. {
  275. }
  276. void OnHelp(LPOFNOTIFY /*lpon*/)
  277. {
  278. }
  279. void OnInitDone(LPOFNOTIFY /*lpon*/)
  280. {
  281. }
  282. void OnSelChange(LPOFNOTIFY /*lpon*/)
  283. {
  284. }
  285. int OnShareViolation(LPOFNOTIFY /*lpon*/)
  286. {
  287. return 0;
  288. }
  289. void OnTypeChange(LPOFNOTIFY /*lpon*/)
  290. {
  291. }
  292. BOOL OnIncludeItem(LPOFNOTIFYEX /*lponex*/)
  293. {
  294. return TRUE; // include item
  295. }
  296. };
  297. class CFileDialog : public CFileDialogImpl<CFileDialog>
  298. {
  299. public:
  300. CFileDialog(BOOL bOpenFileDialog, // TRUE for FileOpen, FALSE for FileSaveAs
  301. LPCTSTR lpszDefExt = NULL,
  302. LPCTSTR lpszFileName = NULL,
  303. DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
  304. LPCTSTR lpszFilter = NULL,
  305. HWND hWndParent = NULL)
  306. : CFileDialogImpl<CFileDialog>(bOpenFileDialog, lpszDefExt, lpszFileName, dwFlags, lpszFilter, hWndParent)
  307. { }
  308. // override base class map and references to handlers
  309. DECLARE_EMPTY_MSG_MAP()
  310. };
  311. ///////////////////////////////////////////////////////////////////////////////
  312. // CSimpleFileDialog - simple class for non-customized Open/SaveAs dialogs
  313. class CSimpleFileDialog
  314. {
  315. public:
  316. OPENFILENAME m_ofn;
  317. BOOL m_bOpenFileDialog; // TRUE for file open, FALSE for file save
  318. TCHAR m_szFileTitle[_MAX_FNAME]; // contains file title after return
  319. TCHAR m_szFileName[_MAX_PATH]; // contains full path name after return
  320. CSimpleFileDialog(BOOL bOpenFileDialog, // TRUE for FileOpen, FALSE for FileSaveAs
  321. LPCTSTR lpszDefExt = NULL,
  322. LPCTSTR lpszFileName = NULL,
  323. DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
  324. LPCTSTR lpszFilter = NULL,
  325. HWND hWndParent = NULL) : m_bOpenFileDialog(bOpenFileDialog)
  326. {
  327. memset(&m_ofn, 0, sizeof(m_ofn)); // initialize structure to 0/NULL
  328. m_ofn.lStructSize = sizeof(m_ofn);
  329. m_ofn.lpstrFile = m_szFileName;
  330. m_ofn.nMaxFile = _MAX_PATH;
  331. m_ofn.lpstrDefExt = lpszDefExt;
  332. m_ofn.lpstrFileTitle = (LPTSTR)m_szFileTitle;
  333. m_ofn.nMaxFileTitle = _MAX_FNAME;
  334. m_ofn.Flags = dwFlags | OFN_EXPLORER | OFN_ENABLESIZING;
  335. m_ofn.lpstrFilter = lpszFilter;
  336. m_ofn.hInstance = ModuleHelper::GetResourceInstance();
  337. m_ofn.hwndOwner = hWndParent;
  338. m_szFileName[0] = _T('\0');
  339. m_szFileTitle[0] = _T('\0');
  340. // setup initial file name
  341. if(lpszFileName != NULL)
  342. ATL::Checked::tcsncpy_s(m_szFileName, _countof(m_szFileName), lpszFileName, _TRUNCATE);
  343. }
  344. INT_PTR DoModal(HWND hWndParent = ::GetActiveWindow())
  345. {
  346. ATLASSERT((m_ofn.Flags & OFN_EXPLORER) != 0);
  347. if(m_ofn.hwndOwner == NULL) // set only if not specified before
  348. m_ofn.hwndOwner = hWndParent;
  349. BOOL bRet = (m_bOpenFileDialog != FALSE) ? ::GetOpenFileName(&m_ofn) : ::GetSaveFileName(&m_ofn);
  350. return (bRet != FALSE) ? IDOK : IDCANCEL;
  351. }
  352. };
  353. ///////////////////////////////////////////////////////////////////////////////
  354. // Multi File Dialog - Multi-select File Open dialog
  355. // The class dynamically resizes the buffer as the file selection changes
  356. // (as described in Knowledge Base article 131462). It also expands selected
  357. // shortcut files to take into account the full path of the target file.
  358. // Note that this doesn't work on Win9x for the old style dialogs, as well as
  359. // on NT for non-Unicode builds.
  360. #ifndef _WTL_FIXED_OFN_BUFFER_LENGTH
  361. #define _WTL_FIXED_OFN_BUFFER_LENGTH 0x10000
  362. #endif
  363. template <class T>
  364. class ATL_NO_VTABLE CMultiFileDialogImpl : public CFileDialogImpl< T >
  365. {
  366. public:
  367. mutable LPCTSTR m_pNextFile;
  368. #ifndef _UNICODE
  369. bool m_bIsNT;
  370. #endif
  371. CMultiFileDialogImpl(
  372. LPCTSTR lpszDefExt = NULL,
  373. LPCTSTR lpszFileName = NULL,
  374. DWORD dwFlags = OFN_HIDEREADONLY,
  375. LPCTSTR lpszFilter = NULL,
  376. HWND hWndParent = NULL)
  377. : CFileDialogImpl<T>(TRUE, lpszDefExt, lpszFileName, dwFlags, lpszFilter, hWndParent),
  378. m_pNextFile(NULL)
  379. {
  380. this->m_ofn.Flags |= OFN_ALLOWMULTISELECT; // Force multiple selection mode
  381. #ifndef _UNICODE
  382. #ifdef _versionhelpers_H_INCLUDED_
  383. OSVERSIONINFOEX ovi = { sizeof(OSVERSIONINFOEX) };
  384. ovi.dwPlatformId = VER_PLATFORM_WIN32_NT;
  385. DWORDLONG const dwlConditionMask = ::VerSetConditionMask(0, VER_PLATFORMID, VER_EQUAL);
  386. m_bIsNT = (::VerifyVersionInfo(&ovi, VER_PLATFORMID, dwlConditionMask) != FALSE);
  387. #else // !_versionhelpers_H_INCLUDED_
  388. OSVERSIONINFO ovi = { sizeof(OSVERSIONINFO) };
  389. ::GetVersionEx(&ovi);
  390. m_bIsNT = (ovi.dwPlatformId == VER_PLATFORM_WIN32_NT);
  391. #endif // _versionhelpers_H_INCLUDED_
  392. if (m_bIsNT)
  393. {
  394. // On NT platforms, GetOpenFileNameA thunks to GetOpenFileNameW and there
  395. // is absolutely nothing we can do except to start off with a large buffer.
  396. ATLVERIFY(ResizeFilenameBuffer(_WTL_FIXED_OFN_BUFFER_LENGTH));
  397. }
  398. #endif
  399. }
  400. ~CMultiFileDialogImpl()
  401. {
  402. if (this->m_ofn.lpstrFile != this->m_szFileName) // Free the buffer if we allocated it
  403. delete[] this->m_ofn.lpstrFile;
  404. }
  405. // Operations
  406. // Get the directory that the files were chosen from.
  407. // The function returns the number of characters copied, not including the terminating zero.
  408. // If the buffer is NULL, the function returns the required size, in characters, including the terminating zero.
  409. // If the function fails, the return value is zero.
  410. int GetDirectory(LPTSTR pBuffer, int nBufLen) const
  411. {
  412. if (this->m_ofn.lpstrFile == NULL)
  413. return 0;
  414. LPCTSTR pStr = this->m_ofn.lpstrFile;
  415. int nLength = lstrlen(pStr);
  416. if (pStr[nLength + 1] == 0)
  417. {
  418. // The OFN buffer contains a single item so extract its path.
  419. LPCTSTR pSep = _tcsrchr(pStr, _T('\\'));
  420. if (pSep != NULL)
  421. nLength = (int)(DWORD_PTR)(pSep - pStr);
  422. }
  423. int nRet = 0;
  424. if (pBuffer == NULL) // If the buffer is NULL, return the required length
  425. {
  426. nRet = nLength + 1;
  427. }
  428. else if (nBufLen > nLength)
  429. {
  430. ATL::Checked::tcsncpy_s(pBuffer, nBufLen, pStr, nLength);
  431. nRet = nLength;
  432. }
  433. return nRet;
  434. }
  435. #ifdef __ATLSTR_H__
  436. bool GetDirectory(ATL::CString& strDir) const
  437. {
  438. bool bRet = false;
  439. int nLength = GetDirectory(NULL, 0);
  440. if (nLength > 0)
  441. {
  442. bRet = (GetDirectory(strDir.GetBuffer(nLength), nLength) > 0);
  443. strDir.ReleaseBuffer(nLength - 1);
  444. }
  445. return bRet;
  446. }
  447. #endif // __ATLSTR_H__
  448. // Get the first filename as a pointer into the buffer.
  449. LPCTSTR GetFirstFileName() const
  450. {
  451. if (this->m_ofn.lpstrFile == NULL)
  452. return NULL;
  453. m_pNextFile = NULL; // Reset internal buffer pointer
  454. LPCTSTR pStr = this->m_ofn.lpstrFile;
  455. int nLength = lstrlen(pStr);
  456. if (pStr[nLength + 1] != 0)
  457. {
  458. // Multiple items were selected. The first string is the directory,
  459. // so skip forwards to the second string.
  460. pStr += nLength + 1;
  461. // Set up m_pNext so it points to the second item (or null).
  462. m_pNextFile = pStr;
  463. GetNextFileName();
  464. }
  465. else
  466. {
  467. // A single item was selected. Skip forward past the path.
  468. LPCTSTR pSep = _tcsrchr(pStr, _T('\\'));
  469. if (pSep != NULL)
  470. pStr = pSep + 1;
  471. }
  472. return pStr;
  473. }
  474. // Get the next filename as a pointer into the buffer.
  475. LPCTSTR GetNextFileName() const
  476. {
  477. if (m_pNextFile == NULL)
  478. return NULL;
  479. LPCTSTR pStr = m_pNextFile;
  480. // Set "m_pNextFile" to point to the next file name, or null if we
  481. // have reached the last file in the list.
  482. int nLength = lstrlen(pStr);
  483. m_pNextFile = (pStr[nLength + 1] != 0) ? &pStr[nLength + 1] : NULL;
  484. return pStr;
  485. }
  486. // Get the first filename as a full path.
  487. // The function returns the number of characters copied, not including the terminating zero.
  488. // If the buffer is NULL, the function returns the required size, in characters, including the terminating zero.
  489. // If the function fails, the return value is zero.
  490. int GetFirstPathName(LPTSTR pBuffer, int nBufLen) const
  491. {
  492. LPCTSTR pStr = GetFirstFileName();
  493. int nLengthDir = GetDirectory(NULL, 0);
  494. if((pStr == NULL) || (nLengthDir == 0))
  495. return 0;
  496. // Figure out the required length.
  497. int nLengthTotal = nLengthDir + lstrlen(pStr);
  498. int nRet = 0;
  499. if(pBuffer == NULL) // If the buffer is NULL, return the required length
  500. {
  501. nRet = nLengthTotal + 1;
  502. }
  503. else if (nBufLen > nLengthTotal) // If the buffer is big enough, go ahead and construct the path
  504. {
  505. GetDirectory(pBuffer, nBufLen);
  506. ATL::Checked::tcscat_s(pBuffer, nBufLen, _T("\\"));
  507. ATL::Checked::tcscat_s(pBuffer, nBufLen, pStr);
  508. nRet = nLengthTotal;
  509. }
  510. return nRet;
  511. }
  512. #ifdef __ATLSTR_H__
  513. bool GetFirstPathName(ATL::CString& strPath) const
  514. {
  515. bool bRet = false;
  516. int nLength = GetFirstPathName(NULL, 0);
  517. if (nLength > 0)
  518. {
  519. bRet = (GetFirstPathName(strPath.GetBuffer(nLength), nLength) > 0);
  520. strPath.ReleaseBuffer(nLength - 1);
  521. }
  522. return bRet;
  523. }
  524. #endif // __ATLSTR_H__
  525. // Get the next filename as a full path.
  526. // The function returns the number of characters copied, not including the terminating zero.
  527. // If the buffer is NULL, the function returns the required size, in characters, including the terminating zero.
  528. // If the function fails, the return value is zero.
  529. // The internal position marker is moved forward only if the function succeeds and the buffer was large enough.
  530. int GetNextPathName(LPTSTR pBuffer, int nBufLen) const
  531. {
  532. if (m_pNextFile == NULL)
  533. return 0;
  534. int nRet = 0;
  535. LPCTSTR pStr = m_pNextFile;
  536. // Does the filename contain a backslash?
  537. if (_tcsrchr(pStr, _T('\\')) != NULL)
  538. {
  539. // Yes, so we'll assume it's a full path.
  540. int nLength = lstrlen(pStr);
  541. if (pBuffer == NULL) // If the buffer is NULL, return the required length
  542. {
  543. nRet = nLength + 1;
  544. }
  545. else if (nBufLen > nLength) // The buffer is big enough, so go ahead and copy the filename
  546. {
  547. ATL::Checked::tcscpy_s(pBuffer, nBufLen, GetNextFileName());
  548. nRet = nBufLen;
  549. }
  550. }
  551. else
  552. {
  553. // The filename is relative, so construct the full path.
  554. int nLengthDir = GetDirectory(NULL, 0);
  555. if (nLengthDir > 0)
  556. {
  557. // Calculate the required space.
  558. int nLengthTotal = nLengthDir + lstrlen(pStr);
  559. if(pBuffer == NULL) // If the buffer is NULL, return the required length
  560. {
  561. nRet = nLengthTotal + 1;
  562. }
  563. else if (nBufLen > nLengthTotal) // If the buffer is big enough, go ahead and construct the path
  564. {
  565. GetDirectory(pBuffer, nBufLen);
  566. ATL::Checked::tcscat_s(pBuffer, nBufLen, _T("\\"));
  567. ATL::Checked::tcscat_s(pBuffer, nBufLen, GetNextFileName());
  568. nRet = nLengthTotal;
  569. }
  570. }
  571. }
  572. return nRet;
  573. }
  574. #ifdef __ATLSTR_H__
  575. bool GetNextPathName(ATL::CString& strPath) const
  576. {
  577. bool bRet = false;
  578. int nLength = GetNextPathName(NULL, 0);
  579. if (nLength > 0)
  580. {
  581. bRet = (GetNextPathName(strPath.GetBuffer(nLength), nLength) > 0);
  582. strPath.ReleaseBuffer(nLength - 1);
  583. }
  584. return bRet;
  585. }
  586. #endif // __ATLSTR_H__
  587. // Implementation
  588. bool ResizeFilenameBuffer(DWORD dwLength)
  589. {
  590. if (dwLength > this->m_ofn.nMaxFile)
  591. {
  592. // Free the old buffer.
  593. if (this->m_ofn.lpstrFile != this->m_szFileName)
  594. {
  595. delete[] this->m_ofn.lpstrFile;
  596. this->m_ofn.lpstrFile = NULL;
  597. this->m_ofn.nMaxFile = 0;
  598. }
  599. // Allocate the new buffer.
  600. LPTSTR lpstrBuff = NULL;
  601. ATLTRY(lpstrBuff = new TCHAR[dwLength]);
  602. if (lpstrBuff != NULL)
  603. {
  604. this->m_ofn.lpstrFile = lpstrBuff;
  605. this->m_ofn.lpstrFile[0] = 0;
  606. this->m_ofn.nMaxFile = dwLength;
  607. }
  608. }
  609. return (this->m_ofn.lpstrFile != NULL);
  610. }
  611. void OnSelChange(LPOFNOTIFY /*lpon*/)
  612. {
  613. #ifndef _UNICODE
  614. // There is no point resizing the buffer in ANSI builds running on NT.
  615. if (m_bIsNT)
  616. return;
  617. #endif
  618. // Get the buffer length required to hold the spec.
  619. int nLength = this->GetSpec(NULL, 0);
  620. if (nLength <= 1)
  621. return; // no files are selected, presumably
  622. // Add room for the directory, and an extra terminating zero.
  623. nLength += this->GetFolderPath(NULL, 0) + 1;
  624. if (!ResizeFilenameBuffer(nLength))
  625. {
  626. ATLASSERT(FALSE);
  627. return;
  628. }
  629. // If we are not following links then our work is done.
  630. if ((this->m_ofn.Flags & OFN_NODEREFERENCELINKS) != 0)
  631. return;
  632. // Get the file spec, which is the text in the edit control.
  633. if (this->GetSpec(this->m_ofn.lpstrFile, this->m_ofn.nMaxFile) <= 0)
  634. return;
  635. // Get the ID-list of the current folder.
  636. int nBytes = this->GetFolderIDList(NULL, 0);
  637. #ifdef STRICT_TYPED_ITEMIDS
  638. ATL::CTempBuffer<ITEMIDLIST_RELATIVE> idlist;
  639. #else
  640. ATL::CTempBuffer<ITEMIDLIST> idlist;
  641. #endif
  642. idlist.AllocateBytes(nBytes);
  643. if ((nBytes <= 0) || (this->GetFolderIDList(idlist, nBytes) <= 0))
  644. return;
  645. // First bind to the desktop folder, then to the current folder.
  646. ATL::CComPtr<IShellFolder> pDesktop, pFolder;
  647. if (FAILED(::SHGetDesktopFolder(&pDesktop)))
  648. return;
  649. if (FAILED(pDesktop->BindToObject(idlist, NULL, IID_IShellFolder, (void**)&pFolder)))
  650. return;
  651. // Work through the file spec, looking for quoted filenames. If we find a shortcut file, then
  652. // we need to add enough extra buffer space to hold its target path.
  653. DWORD nExtraChars = 0;
  654. bool bInsideQuotes = false;
  655. LPCTSTR pAnchor = this->m_ofn.lpstrFile;
  656. LPCTSTR pChar = this->m_ofn.lpstrFile;
  657. for ( ; *pChar; ++pChar)
  658. {
  659. // Look for quotation marks.
  660. if (*pChar == _T('\"'))
  661. {
  662. // We are either entering or leaving a passage of quoted text.
  663. bInsideQuotes = !bInsideQuotes;
  664. // Is it an opening or closing quote?
  665. if (bInsideQuotes)
  666. {
  667. // We found an opening quote, so set "pAnchor" to the following character.
  668. pAnchor = pChar + 1;
  669. }
  670. else // closing quote
  671. {
  672. // Each quoted entity should be shorter than MAX_PATH.
  673. if (pChar - pAnchor >= MAX_PATH)
  674. return;
  675. // Get the ID-list and attributes of the file.
  676. USES_CONVERSION;
  677. int nFileNameLength = (int)(DWORD_PTR)(pChar - pAnchor);
  678. TCHAR szFileName[MAX_PATH] = {};
  679. ATL::Checked::tcsncpy_s(szFileName, MAX_PATH, pAnchor, nFileNameLength);
  680. #ifdef STRICT_TYPED_ITEMIDS
  681. PIDLIST_RELATIVE pidl = NULL;
  682. #else
  683. LPITEMIDLIST pidl = NULL;
  684. #endif
  685. DWORD dwAttrib = SFGAO_LINK;
  686. if (SUCCEEDED(pFolder->ParseDisplayName(NULL, NULL, T2W(szFileName), NULL, &pidl, &dwAttrib)))
  687. {
  688. // Is it a shortcut file?
  689. if (dwAttrib & SFGAO_LINK)
  690. {
  691. // Bind to its IShellLink interface.
  692. ATL::CComPtr<IShellLink> pLink;
  693. if (SUCCEEDED(pFolder->BindToObject(pidl, NULL, IID_IShellLink, (void**)&pLink)))
  694. {
  695. // Get the shortcut's target path.
  696. TCHAR szPath[MAX_PATH] = {};
  697. if (SUCCEEDED(pLink->GetPath(szPath, MAX_PATH, NULL, 0)))
  698. {
  699. // If the target path is longer than the shortcut name, then add on the number
  700. // of extra characters that are required.
  701. int nNewLength = lstrlen(szPath);
  702. if (nNewLength > nFileNameLength)
  703. nExtraChars += nNewLength - nFileNameLength;
  704. }
  705. }
  706. }
  707. // Free the ID-list returned by ParseDisplayName.
  708. ::CoTaskMemFree(pidl);
  709. }
  710. }
  711. }
  712. }
  713. // If we need more space for shortcut targets, then reallocate.
  714. if (nExtraChars > 0)
  715. ATLVERIFY(ResizeFilenameBuffer(this->m_ofn.nMaxFile + nExtraChars));
  716. }
  717. };
  718. class CMultiFileDialog : public CMultiFileDialogImpl<CMultiFileDialog>
  719. {
  720. public:
  721. CMultiFileDialog(
  722. LPCTSTR lpszDefExt = NULL,
  723. LPCTSTR lpszFileName = NULL,
  724. DWORD dwFlags = OFN_HIDEREADONLY,
  725. LPCTSTR lpszFilter = NULL,
  726. HWND hWndParent = NULL)
  727. : CMultiFileDialogImpl<CMultiFileDialog>(lpszDefExt, lpszFileName, dwFlags, lpszFilter, hWndParent)
  728. { }
  729. BEGIN_MSG_MAP(CMultiFileDialog)
  730. CHAIN_MSG_MAP(CMultiFileDialogImpl<CMultiFileDialog>)
  731. END_MSG_MAP()
  732. };
  733. ///////////////////////////////////////////////////////////////////////////////
  734. // Shell File Dialog - new Shell File Open and Save dialogs in Vista
  735. // Note: Use GetPtr() to access dialog interface methods.
  736. // Example:
  737. // CShellFileOpenDialog dlg;
  738. // dlg.GetPtr()->SetTitle(L"MyFileOpenDialog");
  739. #if (_WIN32_WINNT >= 0x0600)
  740. ///////////////////////////////////////////////////////////////////////////////
  741. // CShellFileDialogImpl - base class for CShellFileOpenDialogImpl and CShellFileSaveDialogImpl
  742. template <class T>
  743. class ATL_NO_VTABLE CShellFileDialogImpl : public IFileDialogEvents
  744. {
  745. public:
  746. // Operations
  747. INT_PTR DoModal(HWND hWndParent = ::GetActiveWindow())
  748. {
  749. INT_PTR nRet = -1;
  750. T* pT = static_cast<T*>(this);
  751. if(pT->m_spFileDlg == NULL)
  752. {
  753. ATLASSERT(FALSE);
  754. return nRet;
  755. }
  756. DWORD dwCookie = 0;
  757. pT->_Advise(dwCookie);
  758. HRESULT hRet = pT->m_spFileDlg->Show(hWndParent);
  759. if(SUCCEEDED(hRet))
  760. nRet = IDOK;
  761. else if(hRet == HRESULT_FROM_WIN32(ERROR_CANCELLED))
  762. nRet = IDCANCEL;
  763. else
  764. ATLASSERT(FALSE); // error
  765. pT->_Unadvise(dwCookie);
  766. return nRet;
  767. }
  768. bool IsNull() const
  769. {
  770. const T* pT = static_cast<const T*>(this);
  771. return (pT->m_spFileDlg == NULL);
  772. }
  773. // Operations - get file path after dialog returns
  774. HRESULT GetFilePath(LPWSTR lpstrFilePath, int cchLength)
  775. {
  776. T* pT = static_cast<T*>(this);
  777. ATLASSERT(pT->m_spFileDlg != NULL);
  778. ATL::CComPtr<IShellItem> spItem;
  779. HRESULT hRet = pT->m_spFileDlg->GetResult(&spItem);
  780. if(SUCCEEDED(hRet))
  781. hRet = GetFileNameFromShellItem(spItem, SIGDN_FILESYSPATH, lpstrFilePath, cchLength);
  782. return hRet;
  783. }
  784. HRESULT GetFileTitle(LPWSTR lpstrFileTitle, int cchLength)
  785. {
  786. T* pT = static_cast<T*>(this);
  787. ATLASSERT(pT->m_spFileDlg != NULL);
  788. ATL::CComPtr<IShellItem> spItem;
  789. HRESULT hRet = pT->m_spFileDlg->GetResult(&spItem);
  790. if(SUCCEEDED(hRet))
  791. hRet = GetFileNameFromShellItem(spItem, SIGDN_NORMALDISPLAY, lpstrFileTitle, cchLength);
  792. return hRet;
  793. }
  794. #ifdef __ATLSTR_H__
  795. HRESULT GetFilePath(ATL::CString& strFilePath)
  796. {
  797. T* pT = static_cast<T*>(this);
  798. ATLASSERT(pT->m_spFileDlg != NULL);
  799. ATL::CComPtr<IShellItem> spItem;
  800. HRESULT hRet = pT->m_spFileDlg->GetResult(&spItem);
  801. if(SUCCEEDED(hRet))
  802. hRet = GetFileNameFromShellItem(spItem, SIGDN_FILESYSPATH, strFilePath);
  803. return hRet;
  804. }
  805. HRESULT GetFileTitle(ATL::CString& strFileTitle)
  806. {
  807. T* pT = static_cast<T*>(this);
  808. ATLASSERT(pT->m_spFileDlg != NULL);
  809. ATL::CComPtr<IShellItem> spItem;
  810. HRESULT hRet = pT->m_spFileDlg->GetResult(&spItem);
  811. if(SUCCEEDED(hRet))
  812. hRet = GetFileNameFromShellItem(spItem, SIGDN_NORMALDISPLAY, strFileTitle);
  813. return hRet;
  814. }
  815. #endif // __ATLSTR_H__
  816. // Helpers for IShellItem
  817. static HRESULT GetFileNameFromShellItem(IShellItem* pShellItem, SIGDN type, LPWSTR lpstr, int cchLength)
  818. {
  819. ATLASSERT(pShellItem != NULL);
  820. LPWSTR lpstrName = NULL;
  821. HRESULT hRet = pShellItem->GetDisplayName(type, &lpstrName);
  822. if(SUCCEEDED(hRet))
  823. {
  824. if(lstrlenW(lpstrName) < cchLength)
  825. {
  826. ATL::Checked::wcscpy_s(lpstr, cchLength, lpstrName);
  827. }
  828. else
  829. {
  830. ATLASSERT(FALSE);
  831. hRet = DISP_E_BUFFERTOOSMALL;
  832. }
  833. ::CoTaskMemFree(lpstrName);
  834. }
  835. return hRet;
  836. }
  837. #ifdef __ATLSTR_H__
  838. static HRESULT GetFileNameFromShellItem(IShellItem* pShellItem, SIGDN type, ATL::CString& str)
  839. {
  840. ATLASSERT(pShellItem != NULL);
  841. LPWSTR lpstrName = NULL;
  842. HRESULT hRet = pShellItem->GetDisplayName(type, &lpstrName);
  843. if(SUCCEEDED(hRet))
  844. {
  845. str = lpstrName;
  846. ::CoTaskMemFree(lpstrName);
  847. }
  848. return hRet;
  849. }
  850. #endif // __ATLSTR_H__
  851. // Implementation
  852. void _Advise(DWORD& dwCookie)
  853. {
  854. T* pT = static_cast<T*>(this);
  855. ATLASSERT(pT->m_spFileDlg != NULL);
  856. HRESULT hRet = pT->m_spFileDlg->Advise((IFileDialogEvents*)this, &dwCookie);
  857. ATLVERIFY(SUCCEEDED(hRet));
  858. }
  859. void _Unadvise(DWORD dwCookie)
  860. {
  861. T* pT = static_cast<T*>(this);
  862. ATLASSERT(pT->m_spFileDlg != NULL);
  863. HRESULT hRet = pT->m_spFileDlg->Unadvise(dwCookie);
  864. ATLVERIFY(SUCCEEDED(hRet));
  865. }
  866. void _Init(LPCWSTR lpszFileName, DWORD dwOptions, LPCWSTR lpszDefExt, const COMDLG_FILTERSPEC* arrFilterSpec, UINT uFilterSpecCount)
  867. {
  868. T* pT = static_cast<T*>(this);
  869. ATLASSERT(pT->m_spFileDlg != NULL);
  870. HRESULT hRet = E_FAIL;
  871. if(lpszFileName != NULL)
  872. {
  873. hRet = pT->m_spFileDlg->SetFileName(lpszFileName);
  874. ATLASSERT(SUCCEEDED(hRet));
  875. }
  876. hRet = pT->m_spFileDlg->SetOptions(dwOptions);
  877. ATLASSERT(SUCCEEDED(hRet));
  878. if(lpszDefExt != NULL)
  879. {
  880. hRet = pT->m_spFileDlg->SetDefaultExtension(lpszDefExt);
  881. ATLASSERT(SUCCEEDED(hRet));
  882. }
  883. if((arrFilterSpec != NULL) && (uFilterSpecCount != 0U))
  884. {
  885. hRet = pT->m_spFileDlg->SetFileTypes(uFilterSpecCount, arrFilterSpec);
  886. ATLASSERT(SUCCEEDED(hRet));
  887. }
  888. }
  889. // Implementation - IUnknown interface
  890. STDMETHOD(QueryInterface)(REFIID riid, void** ppvObject)
  891. {
  892. if(ppvObject == NULL)
  893. return E_POINTER;
  894. T* pT = static_cast<T*>(this);
  895. if(IsEqualGUID(riid, IID_IUnknown) || IsEqualGUID(riid, IID_IFileDialogEvents))
  896. {
  897. *ppvObject = (IFileDialogEvents*)pT;
  898. // AddRef() not needed
  899. return S_OK;
  900. }
  901. return E_NOINTERFACE;
  902. }
  903. virtual ULONG STDMETHODCALLTYPE AddRef()
  904. {
  905. return 1;
  906. }
  907. virtual ULONG STDMETHODCALLTYPE Release()
  908. {
  909. return 1;
  910. }
  911. // Implementation - IFileDialogEvents interface
  912. virtual HRESULT STDMETHODCALLTYPE OnFileOk(IFileDialog* pfd)
  913. {
  914. T* pT = static_cast<T*>(this);
  915. ATLASSERT(pT->m_spFileDlg.IsEqualObject(pfd));
  916. (void)pfd; // avoid level 4 warning
  917. return pT->OnFileOk();
  918. }
  919. virtual HRESULT STDMETHODCALLTYPE OnFolderChanging(IFileDialog* pfd, IShellItem* psiFolder)
  920. {
  921. T* pT = static_cast<T*>(this);
  922. ATLASSERT(pT->m_spFileDlg.IsEqualObject(pfd));
  923. (void)pfd; // avoid level 4 warning
  924. return pT->OnFolderChanging(psiFolder);
  925. }
  926. virtual HRESULT STDMETHODCALLTYPE OnFolderChange(IFileDialog* pfd)
  927. {
  928. T* pT = static_cast<T*>(this);
  929. ATLASSERT(pT->m_spFileDlg.IsEqualObject(pfd));
  930. (void)pfd; // avoid level 4 warning
  931. return pT->OnFolderChange();
  932. }
  933. virtual HRESULT STDMETHODCALLTYPE OnSelectionChange(IFileDialog* pfd)
  934. {
  935. T* pT = static_cast<T*>(this);
  936. ATLASSERT(pT->m_spFileDlg.IsEqualObject(pfd));
  937. (void)pfd; // avoid level 4 warning
  938. return pT->OnSelectionChange();
  939. }
  940. virtual HRESULT STDMETHODCALLTYPE OnShareViolation(IFileDialog* pfd, IShellItem* psi, FDE_SHAREVIOLATION_RESPONSE* pResponse)
  941. {
  942. T* pT = static_cast<T*>(this);
  943. ATLASSERT(pT->m_spFileDlg.IsEqualObject(pfd));
  944. (void)pfd; // avoid level 4 warning
  945. return pT->OnShareViolation(psi, pResponse);
  946. }
  947. virtual HRESULT STDMETHODCALLTYPE OnTypeChange(IFileDialog* pfd)
  948. {
  949. T* pT = static_cast<T*>(this);
  950. ATLASSERT(pT->m_spFileDlg.IsEqualObject(pfd));
  951. (void)pfd; // avoid level 4 warning
  952. return pT->OnTypeChange();
  953. }
  954. virtual HRESULT STDMETHODCALLTYPE OnOverwrite(IFileDialog* pfd, IShellItem* psi, FDE_OVERWRITE_RESPONSE* pResponse)
  955. {
  956. T* pT = static_cast<T*>(this);
  957. ATLASSERT(pT->m_spFileDlg.IsEqualObject(pfd));
  958. (void)pfd; // avoid level 4 warning
  959. return pT->OnOverwrite(psi, pResponse);
  960. }
  961. // Overrideables - Event handlers
  962. HRESULT OnFileOk()
  963. {
  964. return E_NOTIMPL;
  965. }
  966. HRESULT OnFolderChanging(IShellItem* /*psiFolder*/)
  967. {
  968. return E_NOTIMPL;
  969. }
  970. HRESULT OnFolderChange()
  971. {
  972. return E_NOTIMPL;
  973. }
  974. HRESULT OnSelectionChange()
  975. {
  976. return E_NOTIMPL;
  977. }
  978. HRESULT OnShareViolation(IShellItem* /*psi*/, FDE_SHAREVIOLATION_RESPONSE* /*pResponse*/)
  979. {
  980. return E_NOTIMPL;
  981. }
  982. HRESULT OnTypeChange()
  983. {
  984. return E_NOTIMPL;
  985. }
  986. HRESULT OnOverwrite(IShellItem* /*psi*/, FDE_OVERWRITE_RESPONSE* /*pResponse*/)
  987. {
  988. return E_NOTIMPL;
  989. }
  990. };
  991. ///////////////////////////////////////////////////////////////////////////////
  992. // CShellFileOpenDialogImpl - implements new Shell File Open dialog
  993. template <class T>
  994. class ATL_NO_VTABLE CShellFileOpenDialogImpl : public CShellFileDialogImpl< T >
  995. {
  996. public:
  997. ATL::CComPtr<IFileOpenDialog> m_spFileDlg;
  998. CShellFileOpenDialogImpl(LPCWSTR lpszFileName = NULL,
  999. DWORD dwOptions = FOS_FORCEFILESYSTEM | FOS_PATHMUSTEXIST | FOS_FILEMUSTEXIST,
  1000. LPCWSTR lpszDefExt = NULL,
  1001. const COMDLG_FILTERSPEC* arrFilterSpec = NULL,
  1002. UINT uFilterSpecCount = 0U)
  1003. {
  1004. HRESULT hRet = m_spFileDlg.CoCreateInstance(CLSID_FileOpenDialog);
  1005. if(SUCCEEDED(hRet))
  1006. this->_Init(lpszFileName, dwOptions, lpszDefExt, arrFilterSpec, uFilterSpecCount);
  1007. }
  1008. virtual ~CShellFileOpenDialogImpl()
  1009. { }
  1010. IFileOpenDialog* GetPtr()
  1011. {
  1012. return m_spFileDlg;
  1013. }
  1014. };
  1015. ///////////////////////////////////////////////////////////////////////////////
  1016. // CShellFileOpenDialog - new Shell File Open dialog without events
  1017. class CShellFileOpenDialog : public CShellFileOpenDialogImpl<CShellFileOpenDialog>
  1018. {
  1019. public:
  1020. CShellFileOpenDialog(LPCWSTR lpszFileName = NULL,
  1021. DWORD dwOptions = FOS_FORCEFILESYSTEM | FOS_PATHMUSTEXIST | FOS_FILEMUSTEXIST,
  1022. LPCWSTR lpszDefExt = NULL,
  1023. const COMDLG_FILTERSPEC* arrFilterSpec = NULL,
  1024. UINT uFilterSpecCount = 0U) : CShellFileOpenDialogImpl<CShellFileOpenDialog>(lpszFileName, dwOptions, lpszDefExt, arrFilterSpec, uFilterSpecCount)
  1025. { }
  1026. virtual ~CShellFileOpenDialog()
  1027. { }
  1028. // Implementation (remove _Advise/_Unadvise code using template magic)
  1029. void _Advise(DWORD& /*dwCookie*/)
  1030. { }
  1031. void _Unadvise(DWORD /*dwCookie*/)
  1032. { }
  1033. };
  1034. ///////////////////////////////////////////////////////////////////////////////
  1035. // CShellFileSaveDialogImpl - implements new Shell File Save dialog
  1036. template <class T>
  1037. class ATL_NO_VTABLE CShellFileSaveDialogImpl : public CShellFileDialogImpl< T >
  1038. {
  1039. public:
  1040. ATL::CComPtr<IFileSaveDialog> m_spFileDlg;
  1041. CShellFileSaveDialogImpl(LPCWSTR lpszFileName = NULL,
  1042. DWORD dwOptions = FOS_FORCEFILESYSTEM | FOS_PATHMUSTEXIST | FOS_OVERWRITEPROMPT,
  1043. LPCWSTR lpszDefExt = NULL,
  1044. const COMDLG_FILTERSPEC* arrFilterSpec = NULL,
  1045. UINT uFilterSpecCount = 0U)
  1046. {
  1047. HRESULT hRet = m_spFileDlg.CoCreateInstance(CLSID_FileSaveDialog);
  1048. if(SUCCEEDED(hRet))
  1049. this->_Init(lpszFileName, dwOptions, lpszDefExt, arrFilterSpec, uFilterSpecCount);
  1050. }
  1051. virtual ~CShellFileSaveDialogImpl()
  1052. { }
  1053. IFileSaveDialog* GetPtr()
  1054. {
  1055. return m_spFileDlg;
  1056. }
  1057. };
  1058. ///////////////////////////////////////////////////////////////////////////////
  1059. // CShellFileSaveDialog - new Shell File Save dialog without events
  1060. class CShellFileSaveDialog : public CShellFileSaveDialogImpl<CShellFileSaveDialog>
  1061. {
  1062. public:
  1063. CShellFileSaveDialog(LPCWSTR lpszFileName = NULL,
  1064. DWORD dwOptions = FOS_FORCEFILESYSTEM | FOS_PATHMUSTEXIST | FOS_OVERWRITEPROMPT,
  1065. LPCWSTR lpszDefExt = NULL,
  1066. const COMDLG_FILTERSPEC* arrFilterSpec = NULL,
  1067. UINT uFilterSpecCount = 0U) : CShellFileSaveDialogImpl<CShellFileSaveDialog>(lpszFileName, dwOptions, lpszDefExt, arrFilterSpec, uFilterSpecCount)
  1068. { }
  1069. virtual ~CShellFileSaveDialog()
  1070. { }
  1071. // Implementation (remove _Advise/_Unadvise code using template magic)
  1072. void _Advise(DWORD& /*dwCookie*/)
  1073. { }
  1074. void _Unadvise(DWORD /*dwCookie*/)
  1075. { }
  1076. };
  1077. #endif // (_WIN32_WINNT >= 0x0600)
  1078. ///////////////////////////////////////////////////////////////////////////////
  1079. // CFolderDialogImpl - used for browsing for a folder
  1080. template <class T>
  1081. class ATL_NO_VTABLE CFolderDialogImpl
  1082. {
  1083. public:
  1084. BROWSEINFO m_bi;
  1085. LPCTSTR m_lpstrInitialFolder;
  1086. LPCITEMIDLIST m_pidlInitialSelection;
  1087. bool m_bExpandInitialSelection;
  1088. TCHAR m_szFolderDisplayName[MAX_PATH];
  1089. TCHAR m_szFolderPath[MAX_PATH];
  1090. #ifdef STRICT_TYPED_ITEMIDS
  1091. PIDLIST_ABSOLUTE m_pidlSelected;
  1092. #else
  1093. LPITEMIDLIST m_pidlSelected;
  1094. #endif
  1095. HWND m_hWnd; // used only in the callback function
  1096. // Constructor
  1097. CFolderDialogImpl(HWND hWndParent = NULL, LPCTSTR lpstrTitle = NULL, UINT uFlags = BIF_RETURNONLYFSDIRS) :
  1098. m_lpstrInitialFolder(NULL), m_pidlInitialSelection(NULL), m_bExpandInitialSelection(false), m_pidlSelected(NULL), m_hWnd(NULL)
  1099. {
  1100. memset(&m_bi, 0, sizeof(m_bi)); // initialize structure to 0/NULL
  1101. m_bi.hwndOwner = hWndParent;
  1102. m_bi.pidlRoot = NULL;
  1103. m_bi.pszDisplayName = m_szFolderDisplayName;
  1104. m_bi.lpszTitle = lpstrTitle;
  1105. m_bi.ulFlags = uFlags;
  1106. m_bi.lpfn = BrowseCallbackProc;
  1107. m_bi.lParam = (LPARAM)static_cast<T*>(this);
  1108. m_szFolderPath[0] = 0;
  1109. m_szFolderDisplayName[0] = 0;
  1110. }
  1111. ~CFolderDialogImpl()
  1112. {
  1113. ::CoTaskMemFree(m_pidlSelected);
  1114. }
  1115. // Operations
  1116. INT_PTR DoModal(HWND hWndParent = ::GetActiveWindow())
  1117. {
  1118. if(m_bi.hwndOwner == NULL) // set only if not specified before
  1119. m_bi.hwndOwner = hWndParent;
  1120. // Clear out any previous results
  1121. m_szFolderPath[0] = 0;
  1122. m_szFolderDisplayName[0] = 0;
  1123. ::CoTaskMemFree(m_pidlSelected);
  1124. INT_PTR nRet = IDCANCEL;
  1125. m_pidlSelected = ::SHBrowseForFolder(&m_bi);
  1126. if(m_pidlSelected != NULL)
  1127. {
  1128. nRet = IDOK;
  1129. // If BIF_RETURNONLYFSDIRS is set, we try to get the filesystem path.
  1130. // Otherwise, the caller must handle the ID-list directly.
  1131. if((m_bi.ulFlags & BIF_RETURNONLYFSDIRS) != 0)
  1132. {
  1133. if(::SHGetPathFromIDList(m_pidlSelected, m_szFolderPath) == FALSE)
  1134. nRet = IDCANCEL;
  1135. }
  1136. }
  1137. return nRet;
  1138. }
  1139. // Methods to call before DoModal
  1140. void SetInitialFolder(LPCTSTR lpstrInitialFolder, bool bExpand = true)
  1141. {
  1142. // lpstrInitialFolder may be a file if BIF_BROWSEINCLUDEFILES is specified
  1143. m_lpstrInitialFolder = lpstrInitialFolder;
  1144. m_bExpandInitialSelection = bExpand;
  1145. }
  1146. void SetInitialSelection(LPCITEMIDLIST pidl, bool bExpand = true)
  1147. {
  1148. m_pidlInitialSelection = pidl;
  1149. m_bExpandInitialSelection = bExpand;
  1150. }
  1151. #ifdef STRICT_TYPED_ITEMIDS
  1152. void SetRootFolder(PCIDLIST_ABSOLUTE pidl)
  1153. #else
  1154. void SetRootFolder(LPCITEMIDLIST pidl)
  1155. #endif
  1156. {
  1157. m_bi.pidlRoot = pidl;
  1158. }
  1159. // Methods to call after DoModal
  1160. LPITEMIDLIST GetSelectedItem(bool bDetach = false)
  1161. {
  1162. LPITEMIDLIST pidl = m_pidlSelected;
  1163. if(bDetach)
  1164. m_pidlSelected = NULL;
  1165. return pidl;
  1166. }
  1167. LPCTSTR GetFolderPath() const
  1168. {
  1169. return m_szFolderPath;
  1170. }
  1171. LPCTSTR GetFolderDisplayName() const
  1172. {
  1173. return m_szFolderDisplayName;
  1174. }
  1175. int GetFolderImageIndex() const
  1176. {
  1177. return m_bi.iImage;
  1178. }
  1179. // Callback function and overrideables
  1180. static int CALLBACK BrowseCallbackProc(HWND hWnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
  1181. {
  1182. int nRet = 0;
  1183. T* pT = (T*)lpData;
  1184. bool bClear = false;
  1185. if(pT->m_hWnd == NULL)
  1186. {
  1187. pT->m_hWnd = hWnd;
  1188. bClear = true;
  1189. }
  1190. else
  1191. {
  1192. ATLASSERT(pT->m_hWnd == hWnd);
  1193. }
  1194. switch(uMsg)
  1195. {
  1196. case BFFM_INITIALIZED:
  1197. // Set initial selection
  1198. // Note that m_pidlInitialSelection, if set, takes precedence over m_lpstrInitialFolder
  1199. if(pT->m_pidlInitialSelection != NULL)
  1200. pT->SetSelection(pT->m_pidlInitialSelection);
  1201. else if(pT->m_lpstrInitialFolder != NULL)
  1202. pT->SetSelection(pT->m_lpstrInitialFolder);
  1203. // Expand initial selection if appropriate
  1204. if(pT->m_bExpandInitialSelection && ((pT->m_bi.ulFlags & BIF_NEWDIALOGSTYLE) != 0))
  1205. {
  1206. if(pT->m_pidlInitialSelection != NULL)
  1207. pT->SetExpanded(pT->m_pidlInitialSelection);
  1208. else if(pT->m_lpstrInitialFolder != NULL)
  1209. pT->SetExpanded(pT->m_lpstrInitialFolder);
  1210. }
  1211. pT->OnInitialized();
  1212. break;
  1213. case BFFM_SELCHANGED:
  1214. pT->OnSelChanged((LPITEMIDLIST)lParam);
  1215. break;
  1216. case BFFM_VALIDATEFAILED:
  1217. nRet = pT->OnValidateFailed((LPCTSTR)lParam);
  1218. break;
  1219. case BFFM_IUNKNOWN:
  1220. pT->OnIUnknown((IUnknown*)lParam);
  1221. break;
  1222. default:
  1223. ATLTRACE2(atlTraceUI, 0, _T("Unknown message received in CFolderDialogImpl::BrowseCallbackProc\n"));
  1224. break;
  1225. }
  1226. if(bClear)
  1227. pT->m_hWnd = NULL;
  1228. return nRet;
  1229. }
  1230. void OnInitialized()
  1231. {
  1232. }
  1233. void OnSelChanged(LPITEMIDLIST /*pItemIDList*/)
  1234. {
  1235. }
  1236. int OnValidateFailed(LPCTSTR /*lpstrFolderPath*/)
  1237. {
  1238. return 1; // 1=continue, 0=EndDialog
  1239. }
  1240. void OnIUnknown(IUnknown* /*pUnknown*/)
  1241. {
  1242. }
  1243. // Commands - valid to call only from handlers
  1244. void EnableOK(BOOL bEnable)
  1245. {
  1246. ATLASSERT(m_hWnd != NULL);
  1247. ::SendMessage(m_hWnd, BFFM_ENABLEOK, 0, bEnable);
  1248. }
  1249. void SetSelection(LPCITEMIDLIST pItemIDList)
  1250. {
  1251. ATLASSERT(m_hWnd != NULL);
  1252. ::SendMessage(m_hWnd, BFFM_SETSELECTION, FALSE, (LPARAM)pItemIDList);
  1253. }
  1254. void SetSelection(LPCTSTR lpstrFolderPath)
  1255. {
  1256. ATLASSERT(m_hWnd != NULL);
  1257. ::SendMessage(m_hWnd, BFFM_SETSELECTION, TRUE, (LPARAM)lpstrFolderPath);
  1258. }
  1259. void SetStatusText(LPCTSTR lpstrText)
  1260. {
  1261. ATLASSERT(m_hWnd != NULL);
  1262. ::SendMessage(m_hWnd, BFFM_SETSTATUSTEXT, 0, (LPARAM)lpstrText);
  1263. }
  1264. void SetOKText(LPCTSTR lpstrOKText)
  1265. {
  1266. ATLASSERT(m_hWnd != NULL);
  1267. USES_CONVERSION;
  1268. LPCWSTR lpstr = T2CW(lpstrOKText);
  1269. ::SendMessage(m_hWnd, BFFM_SETOKTEXT, 0, (LPARAM)lpstr);
  1270. }
  1271. void SetExpanded(LPCITEMIDLIST pItemIDList)
  1272. {
  1273. ATLASSERT(m_hWnd != NULL);
  1274. ::SendMessage(m_hWnd, BFFM_SETEXPANDED, FALSE, (LPARAM)pItemIDList);
  1275. }
  1276. void SetExpanded(LPCTSTR lpstrFolderPath)
  1277. {
  1278. ATLASSERT(m_hWnd != NULL);
  1279. USES_CONVERSION;
  1280. LPCWSTR lpstr = T2CW(lpstrFolderPath);
  1281. ::SendMessage(m_hWnd, BFFM_SETEXPANDED, TRUE, (LPARAM)lpstr);
  1282. }
  1283. };
  1284. class CFolderDialog : public CFolderDialogImpl<CFolderDialog>
  1285. {
  1286. public:
  1287. CFolderDialog(HWND hWndParent = NULL, LPCTSTR lpstrTitle = NULL, UINT uFlags = BIF_RETURNONLYFSDIRS)
  1288. : CFolderDialogImpl<CFolderDialog>(hWndParent, lpstrTitle, uFlags)
  1289. { }
  1290. };
  1291. ///////////////////////////////////////////////////////////////////////////////
  1292. // CCommonDialogImplBase - base class for common dialog classes
  1293. class ATL_NO_VTABLE CCommonDialogImplBase : public ATL::CWindowImplBase
  1294. {
  1295. public:
  1296. static UINT_PTR APIENTRY HookProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  1297. {
  1298. if(uMsg != WM_INITDIALOG)
  1299. return 0;
  1300. CCommonDialogImplBase* pT = (CCommonDialogImplBase*)ModuleHelper::ExtractCreateWndData();
  1301. ATLASSERT(pT != NULL);
  1302. ATLASSERT(pT->m_hWnd == NULL);
  1303. ATLASSERT(::IsWindow(hWnd));
  1304. // subclass dialog's window
  1305. if(!pT->SubclassWindow(hWnd))
  1306. {
  1307. ATLTRACE2(atlTraceUI, 0, _T("Subclassing a common dialog failed\n"));
  1308. return 0;
  1309. }
  1310. // check message map for WM_INITDIALOG handler
  1311. LRESULT lRes = 0;
  1312. if(pT->ProcessWindowMessage(pT->m_hWnd, uMsg, wParam, lParam, lRes, 0) == FALSE)
  1313. return 0;
  1314. return lRes;
  1315. }
  1316. // Special override for common dialogs
  1317. BOOL EndDialog(INT_PTR /*nRetCode*/ = 0)
  1318. {
  1319. ATLASSERT(::IsWindow(m_hWnd));
  1320. SendMessage(WM_COMMAND, MAKEWPARAM(IDABORT, 0));
  1321. return TRUE;
  1322. }
  1323. // Implementation - try to override these, to prevent errors
  1324. HWND Create(HWND, ATL::_U_RECT, LPCTSTR, DWORD, DWORD, ATL::_U_MENUorID, ATOM, LPVOID)
  1325. {
  1326. ATLASSERT(FALSE); // should not be called
  1327. return NULL;
  1328. }
  1329. static LRESULT CALLBACK StartWindowProc(HWND /*hWnd*/, UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/)
  1330. {
  1331. ATLASSERT(FALSE); // should not be called
  1332. return 0;
  1333. }
  1334. };
  1335. ///////////////////////////////////////////////////////////////////////////////
  1336. // CFontDialogImpl - font selection dialog
  1337. template <class T>
  1338. class ATL_NO_VTABLE CFontDialogImpl : public CCommonDialogImplBase
  1339. {
  1340. public:
  1341. enum { _cchStyleName = 64 };
  1342. CHOOSEFONT m_cf;
  1343. TCHAR m_szStyleName[_cchStyleName]; // contains style name after return
  1344. LOGFONT m_lf; // default LOGFONT to store the info
  1345. // Constructors
  1346. CFontDialogImpl(LPLOGFONT lplfInitial = NULL,
  1347. DWORD dwFlags = CF_EFFECTS | CF_SCREENFONTS,
  1348. HDC hDCPrinter = NULL,
  1349. HWND hWndParent = NULL)
  1350. {
  1351. memset(&m_cf, 0, sizeof(m_cf));
  1352. memset(&m_lf, 0, sizeof(m_lf));
  1353. memset(&m_szStyleName, 0, sizeof(m_szStyleName));
  1354. m_cf.lStructSize = sizeof(m_cf);
  1355. m_cf.hwndOwner = hWndParent;
  1356. m_cf.rgbColors = RGB(0, 0, 0);
  1357. m_cf.lpszStyle = (LPTSTR)&m_szStyleName;
  1358. m_cf.Flags = dwFlags | CF_ENABLEHOOK;
  1359. m_cf.lpfnHook = (LPCFHOOKPROC)T::HookProc;
  1360. if(lplfInitial != NULL)
  1361. {
  1362. m_cf.lpLogFont = lplfInitial;
  1363. m_cf.Flags |= CF_INITTOLOGFONTSTRUCT;
  1364. m_lf = *lplfInitial;
  1365. }
  1366. else
  1367. {
  1368. m_cf.lpLogFont = &m_lf;
  1369. }
  1370. if(hDCPrinter != NULL)
  1371. {
  1372. m_cf.hDC = hDCPrinter;
  1373. m_cf.Flags |= CF_PRINTERFONTS;
  1374. }
  1375. }
  1376. // Operations
  1377. INT_PTR DoModal(HWND hWndParent = ::GetActiveWindow())
  1378. {
  1379. ATLASSERT((m_cf.Flags & CF_ENABLEHOOK) != 0);
  1380. ATLASSERT(m_cf.lpfnHook != NULL); // can still be a user hook
  1381. if(m_cf.hwndOwner == NULL) // set only if not specified before
  1382. m_cf.hwndOwner = hWndParent;
  1383. ATLASSERT(m_hWnd == NULL);
  1384. // Allocate the thunk structure here, where we can fail gracefully.
  1385. BOOL bRetTh = m_thunk.Init(NULL, NULL);
  1386. if(bRetTh == FALSE)
  1387. {
  1388. ::SetLastError(ERROR_OUTOFMEMORY);
  1389. return -1;
  1390. }
  1391. ModuleHelper::AddCreateWndData(&m_thunk.cd, (CCommonDialogImplBase*)this);
  1392. BOOL bRet = ::ChooseFont(&m_cf);
  1393. m_hWnd = NULL;
  1394. if(bRet) // copy logical font from user's initialization buffer (if needed)
  1395. ATL::Checked::memcpy_s(&m_lf, sizeof(m_lf), m_cf.lpLogFont, sizeof(m_lf));
  1396. return bRet ? IDOK : IDCANCEL;
  1397. }
  1398. // works only when the dialog is dislayed or after
  1399. void GetCurrentFont(LPLOGFONT lplf) const
  1400. {
  1401. ATLASSERT(lplf != NULL);
  1402. if(m_hWnd != NULL)
  1403. ::SendMessage(m_hWnd, WM_CHOOSEFONT_GETLOGFONT, 0, (LPARAM)lplf);
  1404. else
  1405. *lplf = m_lf;
  1406. }
  1407. // works only when the dialog is dislayed or before
  1408. void SetLogFont(LPLOGFONT lplf)
  1409. {
  1410. ATLASSERT(lplf != NULL);
  1411. if(m_hWnd != NULL)
  1412. {
  1413. ::SendMessage(m_hWnd, WM_CHOOSEFONT_SETLOGFONT, 0, (LPARAM)lplf);
  1414. }
  1415. else
  1416. {
  1417. m_lf = *lplf;
  1418. m_cf.Flags |= CF_INITTOLOGFONTSTRUCT;
  1419. }
  1420. }
  1421. void SetFlags(DWORD dwFlags)
  1422. {
  1423. if(m_hWnd != NULL)
  1424. {
  1425. CHOOSEFONT cf = { sizeof(CHOOSEFONT) };
  1426. cf.Flags = dwFlags;
  1427. ::SendMessage(m_hWnd, WM_CHOOSEFONT_SETFLAGS, 0, (LPARAM)&cf);
  1428. }
  1429. else
  1430. {
  1431. m_cf.Flags = dwFlags;
  1432. }
  1433. }
  1434. // Helpers for parsing information after successful return
  1435. LPCTSTR GetFaceName() const // return the face name of the font
  1436. {
  1437. return (LPCTSTR)m_cf.lpLogFont->lfFaceName;
  1438. }
  1439. LPCTSTR GetStyleName() const // return the style name of the font
  1440. {
  1441. return m_cf.lpszStyle;
  1442. }
  1443. int GetSize() const // return the pt size of the font
  1444. {
  1445. return m_cf.iPointSize;
  1446. }
  1447. COLORREF GetColor() const // return the color of the font
  1448. {
  1449. return m_cf.rgbColors;
  1450. }
  1451. int GetWeight() const // return the chosen font weight
  1452. {
  1453. return (int)m_cf.lpLogFont->lfWeight;
  1454. }
  1455. BOOL IsStrikeOut() const // return TRUE if strikeout
  1456. {
  1457. return (m_cf.lpLogFont->lfStrikeOut) ? TRUE : FALSE;
  1458. }
  1459. BOOL IsUnderline() const // return TRUE if underline
  1460. {
  1461. return (m_cf.lpLogFont->lfUnderline) ? TRUE : FALSE;
  1462. }
  1463. BOOL IsBold() const // return TRUE if bold font
  1464. {
  1465. return (m_cf.lpLogFont->lfWeight == FW_BOLD) ? TRUE : FALSE;
  1466. }
  1467. BOOL IsItalic() const // return TRUE if italic font
  1468. {
  1469. return m_cf.lpLogFont->lfItalic ? TRUE : FALSE;
  1470. }
  1471. };
  1472. class CFontDialog : public CFontDialogImpl<CFontDialog>
  1473. {
  1474. public:
  1475. CFontDialog(LPLOGFONT lplfInitial = NULL,
  1476. DWORD dwFlags = CF_EFFECTS | CF_SCREENFONTS,
  1477. HDC hDCPrinter = NULL,
  1478. HWND hWndParent = NULL)
  1479. : CFontDialogImpl<CFontDialog>(lplfInitial, dwFlags, hDCPrinter, hWndParent)
  1480. { }
  1481. DECLARE_EMPTY_MSG_MAP()
  1482. };
  1483. ///////////////////////////////////////////////////////////////////////////////
  1484. // CRichEditFontDialogImpl - font selection for the Rich Edit ctrl
  1485. #ifdef _RICHEDIT_
  1486. template <class T>
  1487. class ATL_NO_VTABLE CRichEditFontDialogImpl : public CFontDialogImpl< T >
  1488. {
  1489. public:
  1490. CRichEditFontDialogImpl(const CHARFORMAT& charformat,
  1491. DWORD dwFlags = CF_SCREENFONTS,
  1492. HDC hDCPrinter = NULL,
  1493. HWND hWndParent = NULL)
  1494. : CFontDialogImpl< T >(NULL, dwFlags, hDCPrinter, hWndParent)
  1495. {
  1496. this->m_cf.Flags |= CF_INITTOLOGFONTSTRUCT;
  1497. this->m_cf.Flags |= FillInLogFont(charformat);
  1498. this->m_cf.lpLogFont = &this->m_lf;
  1499. if((charformat.dwMask & CFM_COLOR) != 0)
  1500. this->m_cf.rgbColors = charformat.crTextColor;
  1501. }
  1502. void GetCharFormat(CHARFORMAT& cf) const
  1503. {
  1504. USES_CONVERSION;
  1505. cf.dwEffects = 0;
  1506. cf.dwMask = 0;
  1507. if((this->m_cf.Flags & CF_NOSTYLESEL) == 0)
  1508. {
  1509. cf.dwMask |= CFM_BOLD | CFM_ITALIC;
  1510. cf.dwEffects |= this->IsBold() ? CFE_BOLD : 0;
  1511. cf.dwEffects |= this->IsItalic() ? CFE_ITALIC : 0;
  1512. }
  1513. if((this->m_cf.Flags & CF_NOSIZESEL) == 0)
  1514. {
  1515. cf.dwMask |= CFM_SIZE;
  1516. // GetSize() returns in tenths of points so mulitply by 2 to get twips
  1517. cf.yHeight = this->GetSize() * 2;
  1518. }
  1519. if((this->m_cf.Flags & CF_NOFACESEL) == 0)
  1520. {
  1521. cf.dwMask |= CFM_FACE;
  1522. cf.bPitchAndFamily = this->m_cf.lpLogFont->lfPitchAndFamily;
  1523. ATL::Checked::tcscpy_s(cf.szFaceName, _countof(cf.szFaceName), this->GetFaceName());
  1524. }
  1525. if((this->m_cf.Flags & CF_EFFECTS) != 0)
  1526. {
  1527. cf.dwMask |= CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR;
  1528. cf.dwEffects |= this->IsUnderline() ? CFE_UNDERLINE : 0;
  1529. cf.dwEffects |= this->IsStrikeOut() ? CFE_STRIKEOUT : 0;
  1530. cf.crTextColor = this->GetColor();
  1531. }
  1532. if((this->m_cf.Flags & CF_NOSCRIPTSEL) == 0)
  1533. {
  1534. cf.bCharSet = this->m_cf.lpLogFont->lfCharSet;
  1535. cf.dwMask |= CFM_CHARSET;
  1536. }
  1537. cf.yOffset = 0;
  1538. }
  1539. DWORD FillInLogFont(const CHARFORMAT& cf)
  1540. {
  1541. USES_CONVERSION;
  1542. DWORD dwFlags = 0;
  1543. if((cf.dwMask & CFM_SIZE) != 0)
  1544. {
  1545. HDC hDC = ::CreateDC(_T("DISPLAY"), NULL, NULL, NULL);
  1546. LONG yPerInch = ::GetDeviceCaps(hDC, LOGPIXELSY);
  1547. this->m_lf.lfHeight = -(int)((cf.yHeight * yPerInch) / 1440);
  1548. }
  1549. else
  1550. this->m_lf.lfHeight = 0;
  1551. this->m_lf.lfWidth = 0;
  1552. this->m_lf.lfEscapement = 0;
  1553. this->m_lf.lfOrientation = 0;
  1554. if((cf.dwMask & (CFM_ITALIC | CFM_BOLD)) == (CFM_ITALIC | CFM_BOLD))
  1555. {
  1556. this->m_lf.lfWeight = ((cf.dwEffects & CFE_BOLD) != 0) ? FW_BOLD : FW_NORMAL;
  1557. this->m_lf.lfItalic = (BYTE)(((cf.dwEffects & CFE_ITALIC) != 0) ? TRUE : FALSE);
  1558. }
  1559. else
  1560. {
  1561. dwFlags |= CF_NOSTYLESEL;
  1562. this->m_lf.lfWeight = FW_DONTCARE;
  1563. this->m_lf.lfItalic = FALSE;
  1564. }
  1565. if((cf.dwMask & (CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR)) == (CFM_UNDERLINE|CFM_STRIKEOUT|CFM_COLOR))
  1566. {
  1567. dwFlags |= CF_EFFECTS;
  1568. this->m_lf.lfUnderline = (BYTE)(((cf.dwEffects & CFE_UNDERLINE) != 0) ? TRUE : FALSE);
  1569. this->m_lf.lfStrikeOut = (BYTE)(((cf.dwEffects & CFE_STRIKEOUT) != 0) ? TRUE : FALSE);
  1570. }
  1571. else
  1572. {
  1573. this->m_lf.lfUnderline = (BYTE)FALSE;
  1574. this->m_lf.lfStrikeOut = (BYTE)FALSE;
  1575. }
  1576. if((cf.dwMask & CFM_CHARSET) != 0)
  1577. this->m_lf.lfCharSet = cf.bCharSet;
  1578. else
  1579. dwFlags |= CF_NOSCRIPTSEL;
  1580. this->m_lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
  1581. this->m_lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
  1582. this->m_lf.lfQuality = DEFAULT_QUALITY;
  1583. if((cf.dwMask & CFM_FACE) != 0)
  1584. {
  1585. this->m_lf.lfPitchAndFamily = cf.bPitchAndFamily;
  1586. ATL::Checked::tcscpy_s(this->m_lf.lfFaceName, _countof(this->m_lf.lfFaceName), cf.szFaceName);
  1587. }
  1588. else
  1589. {
  1590. this->m_lf.lfPitchAndFamily = DEFAULT_PITCH|FF_DONTCARE;
  1591. this->m_lf.lfFaceName[0] = (TCHAR)0;
  1592. }
  1593. return dwFlags;
  1594. }
  1595. };
  1596. class CRichEditFontDialog : public CRichEditFontDialogImpl<CRichEditFontDialog>
  1597. {
  1598. public:
  1599. CRichEditFontDialog(const CHARFORMAT& charformat,
  1600. DWORD dwFlags = CF_SCREENFONTS,
  1601. HDC hDCPrinter = NULL,
  1602. HWND hWndParent = NULL)
  1603. : CRichEditFontDialogImpl<CRichEditFontDialog>(charformat, dwFlags, hDCPrinter, hWndParent)
  1604. { }
  1605. DECLARE_EMPTY_MSG_MAP()
  1606. };
  1607. #endif // _RICHEDIT_
  1608. ///////////////////////////////////////////////////////////////////////////////
  1609. // CColorDialogImpl - color selection
  1610. template <class T>
  1611. class ATL_NO_VTABLE CColorDialogImpl : public CCommonDialogImplBase
  1612. {
  1613. public:
  1614. CHOOSECOLOR m_cc;
  1615. // Constructor
  1616. CColorDialogImpl(COLORREF clrInit = 0, DWORD dwFlags = 0, HWND hWndParent = NULL)
  1617. {
  1618. memset(&m_cc, 0, sizeof(m_cc));
  1619. m_cc.lStructSize = sizeof(m_cc);
  1620. m_cc.lpCustColors = GetCustomColors();
  1621. m_cc.hwndOwner = hWndParent;
  1622. m_cc.Flags = dwFlags | CC_ENABLEHOOK;
  1623. m_cc.lpfnHook = (LPCCHOOKPROC)T::HookProc;
  1624. if(clrInit != 0)
  1625. {
  1626. m_cc.rgbResult = clrInit;
  1627. m_cc.Flags |= CC_RGBINIT;
  1628. }
  1629. }
  1630. // Operations
  1631. INT_PTR DoModal(HWND hWndParent = ::GetActiveWindow())
  1632. {
  1633. ATLASSERT((m_cc.Flags & CC_ENABLEHOOK) != 0);
  1634. ATLASSERT(m_cc.lpfnHook != NULL); // can still be a user hook
  1635. if(m_cc.hwndOwner == NULL) // set only if not specified before
  1636. m_cc.hwndOwner = hWndParent;
  1637. ATLASSERT(m_hWnd == NULL);
  1638. // Allocate the thunk structure here, where we can fail gracefully.
  1639. BOOL bRetTh = m_thunk.Init(NULL, NULL);
  1640. if(bRetTh == FALSE)
  1641. {
  1642. ::SetLastError(ERROR_OUTOFMEMORY);
  1643. return -1;
  1644. }
  1645. ModuleHelper::AddCreateWndData(&m_thunk.cd, (CCommonDialogImplBase*)this);
  1646. BOOL bRet = ::ChooseColor(&m_cc);
  1647. m_hWnd = NULL;
  1648. return bRet ? IDOK : IDCANCEL;
  1649. }
  1650. // Set the current color while dialog is displayed
  1651. void SetCurrentColor(COLORREF clr)
  1652. {
  1653. ATLASSERT(::IsWindow(m_hWnd));
  1654. SendMessage(_GetSetRGBMessage(), 0, (LPARAM)clr);
  1655. }
  1656. // Get the selected color after DoModal returns, or in OnColorOK
  1657. COLORREF GetColor() const
  1658. {
  1659. return m_cc.rgbResult;
  1660. }
  1661. // Special override for the color dialog
  1662. static UINT_PTR APIENTRY HookProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  1663. {
  1664. if((uMsg != WM_INITDIALOG) && (uMsg != _GetColorOKMessage()))
  1665. return 0;
  1666. LPCHOOSECOLOR lpCC = (LPCHOOSECOLOR)lParam;
  1667. CCommonDialogImplBase* pT = NULL;
  1668. if(uMsg == WM_INITDIALOG)
  1669. {
  1670. pT = (CCommonDialogImplBase*)ModuleHelper::ExtractCreateWndData();
  1671. lpCC->lCustData = (LPARAM)pT;
  1672. ATLASSERT(pT != NULL);
  1673. ATLASSERT(pT->m_hWnd == NULL);
  1674. ATLASSERT(::IsWindow(hWnd));
  1675. // subclass dialog's window
  1676. if(!pT->SubclassWindow(hWnd))
  1677. {
  1678. ATLTRACE2(atlTraceUI, 0, _T("Subclassing a Color common dialog failed\n"));
  1679. return 0;
  1680. }
  1681. }
  1682. else if(uMsg == _GetColorOKMessage())
  1683. {
  1684. pT = (CCommonDialogImplBase*)lpCC->lCustData;
  1685. ATLASSERT(pT != NULL);
  1686. ATLASSERT(::IsWindow(pT->m_hWnd));
  1687. }
  1688. else
  1689. {
  1690. ATLASSERT(FALSE);
  1691. return 0;
  1692. }
  1693. // pass to the message map
  1694. LRESULT lRes = 0;
  1695. if(pT->ProcessWindowMessage(pT->m_hWnd, uMsg, wParam, lParam, lRes, 0) == FALSE)
  1696. return 0;
  1697. return lRes;
  1698. }
  1699. // Helpers
  1700. static COLORREF* GetCustomColors()
  1701. {
  1702. static COLORREF rgbCustomColors[16] =
  1703. {
  1704. RGB(255, 255, 255), RGB(255, 255, 255),
  1705. RGB(255, 255, 255), RGB(255, 255, 255),
  1706. RGB(255, 255, 255), RGB(255, 255, 255),
  1707. RGB(255, 255, 255), RGB(255, 255, 255),
  1708. RGB(255, 255, 255), RGB(255, 255, 255),
  1709. RGB(255, 255, 255), RGB(255, 255, 255),
  1710. RGB(255, 255, 255), RGB(255, 255, 255),
  1711. RGB(255, 255, 255), RGB(255, 255, 255),
  1712. };
  1713. return rgbCustomColors;
  1714. }
  1715. static UINT _GetSetRGBMessage()
  1716. {
  1717. static UINT uSetRGBMessage = 0;
  1718. if(uSetRGBMessage == 0)
  1719. {
  1720. CStaticDataInitCriticalSectionLock lock;
  1721. if(FAILED(lock.Lock()))
  1722. {
  1723. ATLTRACE2(atlTraceUI, 0, _T("ERROR : Unable to lock critical section in CColorDialogImpl::_GetSetRGBMessage.\n"));
  1724. ATLASSERT(FALSE);
  1725. return 0;
  1726. }
  1727. if(uSetRGBMessage == 0)
  1728. uSetRGBMessage = ::RegisterWindowMessage(SETRGBSTRING);
  1729. lock.Unlock();
  1730. }
  1731. ATLASSERT(uSetRGBMessage != 0);
  1732. return uSetRGBMessage;
  1733. }
  1734. static UINT _GetColorOKMessage()
  1735. {
  1736. static UINT uColorOKMessage = 0;
  1737. if(uColorOKMessage == 0)
  1738. {
  1739. CStaticDataInitCriticalSectionLock lock;
  1740. if(FAILED(lock.Lock()))
  1741. {
  1742. ATLTRACE2(atlTraceUI, 0, _T("ERROR : Unable to lock critical section in CColorDialogImpl::_GetColorOKMessage.\n"));
  1743. ATLASSERT(FALSE);
  1744. return 0;
  1745. }
  1746. if(uColorOKMessage == 0)
  1747. uColorOKMessage = ::RegisterWindowMessage(COLOROKSTRING);
  1748. lock.Unlock();
  1749. }
  1750. ATLASSERT(uColorOKMessage != 0);
  1751. return uColorOKMessage;
  1752. }
  1753. // Message map and handlers
  1754. BEGIN_MSG_MAP(CColorDialogImpl)
  1755. MESSAGE_HANDLER(_GetColorOKMessage(), _OnColorOK)
  1756. END_MSG_MAP()
  1757. LRESULT _OnColorOK(UINT, WPARAM, LPARAM, BOOL&)
  1758. {
  1759. T* pT = static_cast<T*>(this);
  1760. return pT->OnColorOK();
  1761. }
  1762. // Overrideable
  1763. BOOL OnColorOK() // validate color
  1764. {
  1765. return FALSE;
  1766. }
  1767. };
  1768. class CColorDialog : public CColorDialogImpl<CColorDialog>
  1769. {
  1770. public:
  1771. CColorDialog(COLORREF clrInit = 0, DWORD dwFlags = 0, HWND hWndParent = NULL)
  1772. : CColorDialogImpl<CColorDialog>(clrInit, dwFlags, hWndParent)
  1773. { }
  1774. // override base class map and references to handlers
  1775. DECLARE_EMPTY_MSG_MAP()
  1776. };
  1777. ///////////////////////////////////////////////////////////////////////////////
  1778. // CPrintDialogImpl - used for Print... and PrintSetup...
  1779. // global helper
  1780. static inline HDC _AtlCreateDC(HGLOBAL hDevNames, HGLOBAL hDevMode)
  1781. {
  1782. if(hDevNames == NULL)
  1783. return NULL;
  1784. LPDEVNAMES lpDevNames = (LPDEVNAMES)::GlobalLock(hDevNames);
  1785. LPDEVMODE lpDevMode = (hDevMode != NULL) ? (LPDEVMODE)::GlobalLock(hDevMode) : NULL;
  1786. if(lpDevNames == NULL)
  1787. return NULL;
  1788. HDC hDC = ::CreateDC((LPCTSTR)lpDevNames + lpDevNames->wDriverOffset,
  1789. (LPCTSTR)lpDevNames + lpDevNames->wDeviceOffset,
  1790. (LPCTSTR)lpDevNames + lpDevNames->wOutputOffset,
  1791. lpDevMode);
  1792. ::GlobalUnlock(hDevNames);
  1793. if(hDevMode != NULL)
  1794. ::GlobalUnlock(hDevMode);
  1795. return hDC;
  1796. }
  1797. #pragma warning(push)
  1798. #pragma warning(disable: 4512) // assignment operator could not be generated
  1799. template <class T>
  1800. class ATL_NO_VTABLE CPrintDialogImpl : public CCommonDialogImplBase
  1801. {
  1802. public:
  1803. // print dialog parameter block (note this is a reference)
  1804. PRINTDLG& m_pd;
  1805. // Constructors
  1806. CPrintDialogImpl(BOOL bPrintSetupOnly = FALSE, // TRUE for Print Setup, FALSE for Print Dialog
  1807. DWORD dwFlags = PD_ALLPAGES | PD_USEDEVMODECOPIES | PD_NOPAGENUMS | PD_NOSELECTION,
  1808. HWND hWndParent = NULL)
  1809. : m_pd(m_pdActual)
  1810. {
  1811. memset(&m_pdActual, 0, sizeof(m_pdActual));
  1812. m_pd.lStructSize = sizeof(m_pdActual);
  1813. m_pd.hwndOwner = hWndParent;
  1814. m_pd.Flags = (dwFlags | PD_ENABLEPRINTHOOK | PD_ENABLESETUPHOOK);
  1815. m_pd.lpfnPrintHook = (LPPRINTHOOKPROC)T::HookProc;
  1816. m_pd.lpfnSetupHook = (LPSETUPHOOKPROC)T::HookProc;
  1817. if(bPrintSetupOnly)
  1818. m_pd.Flags |= PD_PRINTSETUP;
  1819. else
  1820. m_pd.Flags |= PD_RETURNDC;
  1821. m_pd.Flags &= ~PD_RETURNIC; // do not support information context
  1822. }
  1823. // Operations
  1824. INT_PTR DoModal(HWND hWndParent = ::GetActiveWindow())
  1825. {
  1826. ATLASSERT((m_pd.Flags & PD_ENABLEPRINTHOOK) != 0);
  1827. ATLASSERT((m_pd.Flags & PD_ENABLESETUPHOOK) != 0);
  1828. ATLASSERT(m_pd.lpfnPrintHook != NULL); // can still be a user hook
  1829. ATLASSERT(m_pd.lpfnSetupHook != NULL); // can still be a user hook
  1830. ATLASSERT((m_pd.Flags & PD_RETURNDEFAULT) == 0); // use GetDefaults for this
  1831. if(m_pd.hwndOwner == NULL) // set only if not specified before
  1832. m_pd.hwndOwner = hWndParent;
  1833. ATLASSERT(m_hWnd == NULL);
  1834. // Allocate the thunk structure here, where we can fail gracefully.
  1835. BOOL bRetTh = m_thunk.Init(NULL, NULL);
  1836. if(bRetTh == FALSE)
  1837. {
  1838. ::SetLastError(ERROR_OUTOFMEMORY);
  1839. return -1;
  1840. }
  1841. ModuleHelper::AddCreateWndData(&m_thunk.cd, (CCommonDialogImplBase*)this);
  1842. BOOL bRet = ::PrintDlg(&m_pd);
  1843. m_hWnd = NULL;
  1844. return bRet ? IDOK : IDCANCEL;
  1845. }
  1846. // GetDefaults will not display a dialog but will get device defaults
  1847. BOOL GetDefaults()
  1848. {
  1849. m_pd.Flags |= PD_RETURNDEFAULT;
  1850. ATLASSERT(m_pd.hDevMode == NULL); // must be NULL
  1851. ATLASSERT(m_pd.hDevNames == NULL); // must be NULL
  1852. return ::PrintDlg(&m_pd);
  1853. }
  1854. // Helpers for parsing information after successful return num. copies requested
  1855. int GetCopies() const
  1856. {
  1857. if((m_pd.Flags & PD_USEDEVMODECOPIES) != 0)
  1858. {
  1859. LPDEVMODE lpDevMode = GetDevMode();
  1860. return (lpDevMode != NULL) ? lpDevMode->dmCopies : -1;
  1861. }
  1862. return m_pd.nCopies;
  1863. }
  1864. BOOL PrintCollate() const // TRUE if collate checked
  1865. {
  1866. return ((m_pd.Flags & PD_COLLATE) != 0) ? TRUE : FALSE;
  1867. }
  1868. BOOL PrintSelection() const // TRUE if printing selection
  1869. {
  1870. return ((m_pd.Flags & PD_SELECTION) != 0) ? TRUE : FALSE;
  1871. }
  1872. BOOL PrintAll() const // TRUE if printing all pages
  1873. {
  1874. return (!PrintRange() && !PrintSelection()) ? TRUE : FALSE;
  1875. }
  1876. BOOL PrintRange() const // TRUE if printing page range
  1877. {
  1878. return ((m_pd.Flags & PD_PAGENUMS) != 0) ? TRUE : FALSE;
  1879. }
  1880. BOOL PrintToFile() const // TRUE if printing to a file
  1881. {
  1882. return ((m_pd.Flags & PD_PRINTTOFILE) != 0) ? TRUE : FALSE;
  1883. }
  1884. int GetFromPage() const // starting page if valid
  1885. {
  1886. return PrintRange() ? m_pd.nFromPage : -1;
  1887. }
  1888. int GetToPage() const // ending page if valid
  1889. {
  1890. return PrintRange() ? m_pd.nToPage : -1;
  1891. }
  1892. LPDEVMODE GetDevMode() const // return DEVMODE
  1893. {
  1894. if(m_pd.hDevMode == NULL)
  1895. return NULL;
  1896. return (LPDEVMODE)::GlobalLock(m_pd.hDevMode);
  1897. }
  1898. LPCTSTR GetDriverName() const // return driver name
  1899. {
  1900. if(m_pd.hDevNames == NULL)
  1901. return NULL;
  1902. LPDEVNAMES lpDev = (LPDEVNAMES)::GlobalLock(m_pd.hDevNames);
  1903. if(lpDev == NULL)
  1904. return NULL;
  1905. return (LPCTSTR)lpDev + lpDev->wDriverOffset;
  1906. }
  1907. LPCTSTR GetDeviceName() const // return device name
  1908. {
  1909. if(m_pd.hDevNames == NULL)
  1910. return NULL;
  1911. LPDEVNAMES lpDev = (LPDEVNAMES)::GlobalLock(m_pd.hDevNames);
  1912. if(lpDev == NULL)
  1913. return NULL;
  1914. return (LPCTSTR)lpDev + lpDev->wDeviceOffset;
  1915. }
  1916. LPCTSTR GetPortName() const // return output port name
  1917. {
  1918. if(m_pd.hDevNames == NULL)
  1919. return NULL;
  1920. LPDEVNAMES lpDev = (LPDEVNAMES)::GlobalLock(m_pd.hDevNames);
  1921. if(lpDev == NULL)
  1922. return NULL;
  1923. return (LPCTSTR)lpDev + lpDev->wOutputOffset;
  1924. }
  1925. HDC GetPrinterDC() const // return HDC (caller must delete)
  1926. {
  1927. ATLASSERT((m_pd.Flags & PD_RETURNDC) != 0);
  1928. return m_pd.hDC;
  1929. }
  1930. // This helper creates a DC based on the DEVNAMES and DEVMODE structures.
  1931. // This DC is returned, but also stored in m_pd.hDC as though it had been
  1932. // returned by CommDlg. It is assumed that any previously obtained DC
  1933. // has been/will be deleted by the user. This may be
  1934. // used without ever invoking the print/print setup dialogs.
  1935. HDC CreatePrinterDC()
  1936. {
  1937. m_pd.hDC = _AtlCreateDC(m_pd.hDevNames, m_pd.hDevMode);
  1938. return m_pd.hDC;
  1939. }
  1940. // Implementation
  1941. PRINTDLG m_pdActual; // the Print/Print Setup need to share this
  1942. // The following handle the case of print setup... from the print dialog
  1943. CPrintDialogImpl(PRINTDLG& pdInit) : m_pd(pdInit)
  1944. {
  1945. memset(&m_pdActual, 0, sizeof(m_pdActual));
  1946. }
  1947. BEGIN_MSG_MAP(CPrintDialogImpl)
  1948. #ifdef psh1
  1949. COMMAND_ID_HANDLER(psh1, OnPrintSetup) // print setup button when print is displayed
  1950. #else // !psh1
  1951. COMMAND_ID_HANDLER(0x0400, OnPrintSetup) // value from dlgs.h
  1952. #endif // !psh1
  1953. END_MSG_MAP()
  1954. LRESULT OnPrintSetup(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& /*bHandled*/)
  1955. {
  1956. T dlgSetup(m_pd);
  1957. ModuleHelper::AddCreateWndData(&dlgSetup.m_thunk.cd, (CCommonDialogImplBase*)&dlgSetup);
  1958. return DefWindowProc(WM_COMMAND, MAKEWPARAM(wID, wNotifyCode), (LPARAM)hWndCtl);
  1959. }
  1960. };
  1961. class CPrintDialog : public CPrintDialogImpl<CPrintDialog>
  1962. {
  1963. public:
  1964. CPrintDialog(BOOL bPrintSetupOnly = FALSE,
  1965. DWORD dwFlags = PD_ALLPAGES | PD_USEDEVMODECOPIES | PD_NOPAGENUMS | PD_NOSELECTION,
  1966. HWND hWndParent = NULL)
  1967. : CPrintDialogImpl<CPrintDialog>(bPrintSetupOnly, dwFlags, hWndParent)
  1968. { }
  1969. CPrintDialog(PRINTDLG& pdInit) : CPrintDialogImpl<CPrintDialog>(pdInit)
  1970. { }
  1971. };
  1972. #pragma warning(pop)
  1973. ///////////////////////////////////////////////////////////////////////////////
  1974. // CPrintDialogExImpl - new print dialog for Windows 2000
  1975. } // namespace WTL
  1976. #include <atlcom.h>
  1977. extern "C" const __declspec(selectany) IID IID_IPrintDialogCallback = {0x5852a2c3, 0x6530, 0x11d1, {0xb6, 0xa3, 0x0, 0x0, 0xf8, 0x75, 0x7b, 0xf9}};
  1978. extern "C" const __declspec(selectany) IID IID_IPrintDialogServices = {0x509aaeda, 0x5639, 0x11d1, {0xb6, 0xa1, 0x0, 0x0, 0xf8, 0x75, 0x7b, 0xf9}};
  1979. namespace WTL
  1980. {
  1981. template <class T>
  1982. class ATL_NO_VTABLE CPrintDialogExImpl :
  1983. public ATL::CWindow,
  1984. public ATL::CMessageMap,
  1985. public IPrintDialogCallback,
  1986. public ATL::IObjectWithSiteImpl< T >
  1987. {
  1988. public:
  1989. PRINTDLGEX m_pdex;
  1990. // Constructor
  1991. CPrintDialogExImpl(DWORD dwFlags = PD_ALLPAGES | PD_USEDEVMODECOPIES | PD_NOPAGENUMS | PD_NOSELECTION | PD_NOCURRENTPAGE,
  1992. HWND hWndParent = NULL)
  1993. {
  1994. memset(&m_pdex, 0, sizeof(m_pdex));
  1995. m_pdex.lStructSize = sizeof(PRINTDLGEX);
  1996. m_pdex.hwndOwner = hWndParent;
  1997. m_pdex.Flags = dwFlags;
  1998. m_pdex.nStartPage = START_PAGE_GENERAL;
  1999. // callback object will be set in DoModal
  2000. m_pdex.Flags &= ~PD_RETURNIC; // do not support information context
  2001. }
  2002. // Operations
  2003. HRESULT DoModal(HWND hWndParent = ::GetActiveWindow())
  2004. {
  2005. ATLASSERT(m_hWnd == NULL);
  2006. ATLASSERT((m_pdex.Flags & PD_RETURNDEFAULT) == 0); // use GetDefaults for this
  2007. if(m_pdex.hwndOwner == NULL) // set only if not specified before
  2008. m_pdex.hwndOwner = hWndParent;
  2009. T* pT = static_cast<T*>(this);
  2010. m_pdex.lpCallback = (IUnknown*)(IPrintDialogCallback*)pT;
  2011. HRESULT hResult = ::PrintDlgEx(&m_pdex);
  2012. m_hWnd = NULL;
  2013. return hResult;
  2014. }
  2015. BOOL EndDialog(INT_PTR /*nRetCode*/ = 0)
  2016. {
  2017. ATLASSERT(::IsWindow(m_hWnd));
  2018. SendMessage(WM_COMMAND, MAKEWPARAM(IDABORT, 0));
  2019. return TRUE;
  2020. }
  2021. // GetDefaults will not display a dialog but will get device defaults
  2022. HRESULT GetDefaults()
  2023. {
  2024. ATLASSERT(m_pdex.hDevMode == NULL); // must be NULL
  2025. ATLASSERT(m_pdex.hDevNames == NULL); // must be NULL
  2026. if(m_pdex.hwndOwner == NULL) // set only if not specified before
  2027. m_pdex.hwndOwner = ::GetActiveWindow();
  2028. m_pdex.Flags |= PD_RETURNDEFAULT;
  2029. HRESULT hRet = ::PrintDlgEx(&m_pdex);
  2030. m_pdex.Flags &= ~PD_RETURNDEFAULT;
  2031. return hRet;
  2032. }
  2033. // Helpers for parsing information after successful return num. copies requested
  2034. int GetCopies() const
  2035. {
  2036. if((m_pdex.Flags & PD_USEDEVMODECOPIES) != 0)
  2037. {
  2038. LPDEVMODE lpDevMode = GetDevMode();
  2039. return (lpDevMode != NULL) ? lpDevMode->dmCopies : -1;
  2040. }
  2041. return m_pdex.nCopies;
  2042. }
  2043. BOOL PrintCollate() const // TRUE if collate checked
  2044. {
  2045. return ((m_pdex.Flags & PD_COLLATE) != 0) ? TRUE : FALSE;
  2046. }
  2047. BOOL PrintSelection() const // TRUE if printing selection
  2048. {
  2049. return ((m_pdex.Flags & PD_SELECTION) != 0) ? TRUE : FALSE;
  2050. }
  2051. BOOL PrintAll() const // TRUE if printing all pages
  2052. {
  2053. return (!PrintRange() && !PrintSelection()) ? TRUE : FALSE;
  2054. }
  2055. BOOL PrintRange() const // TRUE if printing page range
  2056. {
  2057. return ((m_pdex.Flags & PD_PAGENUMS) != 0) ? TRUE : FALSE;
  2058. }
  2059. BOOL PrintToFile() const // TRUE if printing to a file
  2060. {
  2061. return ((m_pdex.Flags & PD_PRINTTOFILE) != 0) ? TRUE : FALSE;
  2062. }
  2063. LPDEVMODE GetDevMode() const // return DEVMODE
  2064. {
  2065. if(m_pdex.hDevMode == NULL)
  2066. return NULL;
  2067. return (LPDEVMODE)::GlobalLock(m_pdex.hDevMode);
  2068. }
  2069. LPCTSTR GetDriverName() const // return driver name
  2070. {
  2071. if(m_pdex.hDevNames == NULL)
  2072. return NULL;
  2073. LPDEVNAMES lpDev = (LPDEVNAMES)::GlobalLock(m_pdex.hDevNames);
  2074. if(lpDev == NULL)
  2075. return NULL;
  2076. return (LPCTSTR)lpDev + lpDev->wDriverOffset;
  2077. }
  2078. LPCTSTR GetDeviceName() const // return device name
  2079. {
  2080. if(m_pdex.hDevNames == NULL)
  2081. return NULL;
  2082. LPDEVNAMES lpDev = (LPDEVNAMES)::GlobalLock(m_pdex.hDevNames);
  2083. if(lpDev == NULL)
  2084. return NULL;
  2085. return (LPCTSTR)lpDev + lpDev->wDeviceOffset;
  2086. }
  2087. LPCTSTR GetPortName() const // return output port name
  2088. {
  2089. if(m_pdex.hDevNames == NULL)
  2090. return NULL;
  2091. LPDEVNAMES lpDev = (LPDEVNAMES)::GlobalLock(m_pdex.hDevNames);
  2092. if(lpDev == NULL)
  2093. return NULL;
  2094. return (LPCTSTR)lpDev + lpDev->wOutputOffset;
  2095. }
  2096. HDC GetPrinterDC() const // return HDC (caller must delete)
  2097. {
  2098. ATLASSERT((m_pdex.Flags & PD_RETURNDC) != 0);
  2099. return m_pdex.hDC;
  2100. }
  2101. // This helper creates a DC based on the DEVNAMES and DEVMODE structures.
  2102. // This DC is returned, but also stored in m_pdex.hDC as though it had been
  2103. // returned by CommDlg. It is assumed that any previously obtained DC
  2104. // has been/will be deleted by the user. This may be
  2105. // used without ever invoking the print/print setup dialogs.
  2106. HDC CreatePrinterDC()
  2107. {
  2108. m_pdex.hDC = _AtlCreateDC(m_pdex.hDevNames, m_pdex.hDevMode);
  2109. return m_pdex.hDC;
  2110. }
  2111. // Implementation - interfaces
  2112. // IUnknown
  2113. STDMETHOD(QueryInterface)(REFIID riid, void** ppvObject)
  2114. {
  2115. if(ppvObject == NULL)
  2116. return E_POINTER;
  2117. T* pT = static_cast<T*>(this);
  2118. if(IsEqualGUID(riid, IID_IUnknown) || IsEqualGUID(riid, IID_IPrintDialogCallback))
  2119. {
  2120. *ppvObject = (IPrintDialogCallback*)pT;
  2121. // AddRef() not needed
  2122. return S_OK;
  2123. }
  2124. else if(IsEqualGUID(riid, IID_IObjectWithSite))
  2125. {
  2126. *ppvObject = (IObjectWithSite*)pT;
  2127. // AddRef() not needed
  2128. return S_OK;
  2129. }
  2130. return E_NOINTERFACE;
  2131. }
  2132. virtual ULONG STDMETHODCALLTYPE AddRef()
  2133. {
  2134. return 1;
  2135. }
  2136. virtual ULONG STDMETHODCALLTYPE Release()
  2137. {
  2138. return 1;
  2139. }
  2140. // IPrintDialogCallback
  2141. STDMETHOD(InitDone)()
  2142. {
  2143. return S_FALSE;
  2144. }
  2145. STDMETHOD(SelectionChange)()
  2146. {
  2147. return S_FALSE;
  2148. }
  2149. STDMETHOD(HandleMessage)(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT* plResult)
  2150. {
  2151. // set up m_hWnd the first time
  2152. if(m_hWnd == NULL)
  2153. Attach(hWnd);
  2154. // call message map
  2155. HRESULT hRet = ProcessWindowMessage(hWnd, uMsg, wParam, lParam, *plResult, 0) ? S_OK : S_FALSE;
  2156. if((hRet == S_OK) && (uMsg == WM_NOTIFY)) // return in DWLP_MSGRESULT
  2157. ::SetWindowLongPtr(GetParent(), DWLP_MSGRESULT, (LONG_PTR)*plResult);
  2158. if((uMsg == WM_INITDIALOG) && (hRet == S_OK) && ((BOOL)*plResult != FALSE))
  2159. hRet = S_FALSE;
  2160. return hRet;
  2161. }
  2162. };
  2163. class CPrintDialogEx : public CPrintDialogExImpl<CPrintDialogEx>
  2164. {
  2165. public:
  2166. CPrintDialogEx(
  2167. DWORD dwFlags = PD_ALLPAGES | PD_USEDEVMODECOPIES | PD_NOPAGENUMS | PD_NOSELECTION | PD_NOCURRENTPAGE,
  2168. HWND hWndParent = NULL)
  2169. : CPrintDialogExImpl<CPrintDialogEx>(dwFlags, hWndParent)
  2170. { }
  2171. DECLARE_EMPTY_MSG_MAP()
  2172. };
  2173. ///////////////////////////////////////////////////////////////////////////////
  2174. // CPageSetupDialogImpl - Page Setup dialog
  2175. template <class T>
  2176. class ATL_NO_VTABLE CPageSetupDialogImpl : public CCommonDialogImplBase
  2177. {
  2178. public:
  2179. PAGESETUPDLG m_psd;
  2180. ATL::CWndProcThunk m_thunkPaint;
  2181. // Constructors
  2182. CPageSetupDialogImpl(DWORD dwFlags = PSD_MARGINS | PSD_INWININIINTLMEASURE, HWND hWndParent = NULL)
  2183. {
  2184. memset(&m_psd, 0, sizeof(m_psd));
  2185. m_psd.lStructSize = sizeof(m_psd);
  2186. m_psd.hwndOwner = hWndParent;
  2187. m_psd.Flags = (dwFlags | PSD_ENABLEPAGESETUPHOOK | PSD_ENABLEPAGEPAINTHOOK);
  2188. m_psd.lpfnPageSetupHook = (LPPAGESETUPHOOK)T::HookProc;
  2189. m_thunkPaint.Init((WNDPROC)T::PaintHookProc, this);
  2190. m_psd.lpfnPagePaintHook = (LPPAGEPAINTHOOK)m_thunkPaint.GetWNDPROC();
  2191. }
  2192. DECLARE_EMPTY_MSG_MAP()
  2193. // Attributes
  2194. LPDEVMODE GetDevMode() const // return DEVMODE
  2195. {
  2196. if(m_psd.hDevMode == NULL)
  2197. return NULL;
  2198. return (LPDEVMODE)::GlobalLock(m_psd.hDevMode);
  2199. }
  2200. LPCTSTR GetDriverName() const // return driver name
  2201. {
  2202. if(m_psd.hDevNames == NULL)
  2203. return NULL;
  2204. LPDEVNAMES lpDev = (LPDEVNAMES)::GlobalLock(m_psd.hDevNames);
  2205. return (LPCTSTR)lpDev + lpDev->wDriverOffset;
  2206. }
  2207. LPCTSTR GetDeviceName() const // return device name
  2208. {
  2209. if(m_psd.hDevNames == NULL)
  2210. return NULL;
  2211. LPDEVNAMES lpDev = (LPDEVNAMES)::GlobalLock(m_psd.hDevNames);
  2212. return (LPCTSTR)lpDev + lpDev->wDeviceOffset;
  2213. }
  2214. LPCTSTR GetPortName() const // return output port name
  2215. {
  2216. if(m_psd.hDevNames == NULL)
  2217. return NULL;
  2218. LPDEVNAMES lpDev = (LPDEVNAMES)::GlobalLock(m_psd.hDevNames);
  2219. return (LPCTSTR)lpDev + lpDev->wOutputOffset;
  2220. }
  2221. HDC CreatePrinterDC()
  2222. {
  2223. return _AtlCreateDC(m_psd.hDevNames, m_psd.hDevMode);
  2224. }
  2225. SIZE GetPaperSize() const
  2226. {
  2227. SIZE size = { m_psd.ptPaperSize.x, m_psd.ptPaperSize.y };
  2228. return size;
  2229. }
  2230. void GetMargins(LPRECT lpRectMargins, LPRECT lpRectMinMargins) const
  2231. {
  2232. if(lpRectMargins != NULL)
  2233. *lpRectMargins = m_psd.rtMargin;
  2234. if(lpRectMinMargins != NULL)
  2235. *lpRectMinMargins = m_psd.rtMinMargin;
  2236. }
  2237. // Operations
  2238. INT_PTR DoModal(HWND hWndParent = ::GetActiveWindow())
  2239. {
  2240. ATLASSERT((m_psd.Flags & PSD_ENABLEPAGESETUPHOOK) != 0);
  2241. ATLASSERT((m_psd.Flags & PSD_ENABLEPAGEPAINTHOOK) != 0);
  2242. ATLASSERT(m_psd.lpfnPageSetupHook != NULL); // can still be a user hook
  2243. ATLASSERT(m_psd.lpfnPagePaintHook != NULL); // can still be a user hook
  2244. if(m_psd.hwndOwner == NULL) // set only if not specified before
  2245. m_psd.hwndOwner = hWndParent;
  2246. ATLASSERT(m_hWnd == NULL);
  2247. // Allocate the thunk structure here, where we can fail gracefully.
  2248. BOOL bRetTh = m_thunk.Init(NULL, NULL);
  2249. if(bRetTh == FALSE)
  2250. {
  2251. ::SetLastError(ERROR_OUTOFMEMORY);
  2252. return -1;
  2253. }
  2254. ModuleHelper::AddCreateWndData(&m_thunk.cd, (CCommonDialogImplBase*)this);
  2255. BOOL bRet = ::PageSetupDlg(&m_psd);
  2256. m_hWnd = NULL;
  2257. return bRet ? IDOK : IDCANCEL;
  2258. }
  2259. // Implementation
  2260. static UINT_PTR CALLBACK PaintHookProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  2261. {
  2262. T* pT = (T*)hWnd;
  2263. UINT_PTR uRet = 0;
  2264. switch(uMsg)
  2265. {
  2266. case WM_PSD_PAGESETUPDLG:
  2267. uRet = pT->PreDrawPage(LOWORD(wParam), HIWORD(wParam), (LPPAGESETUPDLG)lParam);
  2268. break;
  2269. case WM_PSD_FULLPAGERECT:
  2270. case WM_PSD_MINMARGINRECT:
  2271. case WM_PSD_MARGINRECT:
  2272. case WM_PSD_GREEKTEXTRECT:
  2273. case WM_PSD_ENVSTAMPRECT:
  2274. case WM_PSD_YAFULLPAGERECT:
  2275. uRet = pT->OnDrawPage(uMsg, (HDC)wParam, (LPRECT)lParam);
  2276. break;
  2277. default:
  2278. ATLTRACE2(atlTraceUI, 0, _T("CPageSetupDialogImpl::PaintHookProc - unknown message received\n"));
  2279. break;
  2280. }
  2281. return uRet;
  2282. }
  2283. // Overridables
  2284. UINT_PTR PreDrawPage(WORD /*wPaper*/, WORD /*wFlags*/, LPPAGESETUPDLG /*pPSD*/)
  2285. {
  2286. // return 1 to prevent any more drawing
  2287. return 0;
  2288. }
  2289. UINT_PTR OnDrawPage(UINT /*uMsg*/, HDC /*hDC*/, LPRECT /*lpRect*/)
  2290. {
  2291. return 0; // do the default
  2292. }
  2293. };
  2294. class CPageSetupDialog : public CPageSetupDialogImpl<CPageSetupDialog>
  2295. {
  2296. public:
  2297. CPageSetupDialog(DWORD dwFlags = PSD_MARGINS | PSD_INWININIINTLMEASURE, HWND hWndParent = NULL)
  2298. : CPageSetupDialogImpl<CPageSetupDialog>(dwFlags, hWndParent)
  2299. { }
  2300. // override PaintHookProc and references to handlers
  2301. static UINT_PTR CALLBACK PaintHookProc(HWND, UINT, WPARAM, LPARAM)
  2302. {
  2303. return 0;
  2304. }
  2305. };
  2306. ///////////////////////////////////////////////////////////////////////////////
  2307. // CFindReplaceDialogImpl - Find/FindReplace modeless dialogs
  2308. template <class T>
  2309. class ATL_NO_VTABLE CFindReplaceDialogImpl : public CCommonDialogImplBase
  2310. {
  2311. public:
  2312. enum { _cchFindReplaceBuffer = 128 };
  2313. FINDREPLACE m_fr;
  2314. TCHAR m_szFindWhat[_cchFindReplaceBuffer];
  2315. TCHAR m_szReplaceWith[_cchFindReplaceBuffer];
  2316. // Constructors
  2317. CFindReplaceDialogImpl()
  2318. {
  2319. memset(&m_fr, 0, sizeof(m_fr));
  2320. m_szFindWhat[0] = _T('\0');
  2321. m_szReplaceWith[0] = _T('\0');
  2322. m_fr.lStructSize = sizeof(m_fr);
  2323. m_fr.Flags = FR_ENABLEHOOK;
  2324. m_fr.lpfnHook = (LPFRHOOKPROC)T::HookProc;
  2325. m_fr.lpstrFindWhat = (LPTSTR)m_szFindWhat;
  2326. m_fr.wFindWhatLen = _cchFindReplaceBuffer;
  2327. m_fr.lpstrReplaceWith = (LPTSTR)m_szReplaceWith;
  2328. m_fr.wReplaceWithLen = _cchFindReplaceBuffer;
  2329. }
  2330. // Note: You must allocate the object on the heap.
  2331. // If you do not, you must override OnFinalMessage()
  2332. virtual void OnFinalMessage(HWND /*hWnd*/)
  2333. {
  2334. delete this;
  2335. }
  2336. HWND Create(BOOL bFindDialogOnly, // TRUE for Find, FALSE for FindReplace
  2337. LPCTSTR lpszFindWhat,
  2338. LPCTSTR lpszReplaceWith = NULL,
  2339. DWORD dwFlags = FR_DOWN,
  2340. HWND hWndParent = NULL)
  2341. {
  2342. ATLASSERT((m_fr.Flags & FR_ENABLEHOOK) != 0);
  2343. ATLASSERT(m_fr.lpfnHook != NULL);
  2344. m_fr.Flags |= dwFlags;
  2345. if(hWndParent == NULL)
  2346. m_fr.hwndOwner = ::GetActiveWindow();
  2347. else
  2348. m_fr.hwndOwner = hWndParent;
  2349. ATLASSERT(m_fr.hwndOwner != NULL); // must have an owner for modeless dialog
  2350. if(lpszFindWhat != NULL)
  2351. ATL::Checked::tcsncpy_s(m_szFindWhat, _countof(m_szFindWhat), lpszFindWhat, _TRUNCATE);
  2352. if(lpszReplaceWith != NULL)
  2353. ATL::Checked::tcsncpy_s(m_szReplaceWith, _countof(m_szReplaceWith), lpszReplaceWith, _TRUNCATE);
  2354. ATLASSERT(m_hWnd == NULL);
  2355. // Allocate the thunk structure here, where we can fail gracefully.
  2356. BOOL bRet = m_thunk.Init(NULL, NULL);
  2357. if(bRet == FALSE)
  2358. {
  2359. ::SetLastError(ERROR_OUTOFMEMORY);
  2360. return NULL;
  2361. }
  2362. ModuleHelper::AddCreateWndData(&m_thunk.cd, (CCommonDialogImplBase*)this);
  2363. HWND hWnd = NULL;
  2364. if(bFindDialogOnly)
  2365. hWnd = ::FindText(&m_fr);
  2366. else
  2367. hWnd = ::ReplaceText(&m_fr);
  2368. ATLASSERT(m_hWnd == hWnd);
  2369. return hWnd;
  2370. }
  2371. static UINT GetFindReplaceMsg()
  2372. {
  2373. static const UINT nMsgFindReplace = ::RegisterWindowMessage(FINDMSGSTRING);
  2374. return nMsgFindReplace;
  2375. }
  2376. // call while handling FINDMSGSTRING registered message
  2377. // to retreive the object
  2378. static T* PASCAL GetNotifier(LPARAM lParam)
  2379. {
  2380. ATLASSERT(lParam != NULL);
  2381. T* pDlg = (T*)(lParam - offsetof(T, m_fr));
  2382. return pDlg;
  2383. }
  2384. // Operations
  2385. // Helpers for parsing information after successful return
  2386. LPCTSTR GetFindString() const // get find string
  2387. {
  2388. return (LPCTSTR)m_fr.lpstrFindWhat;
  2389. }
  2390. LPCTSTR GetReplaceString() const // get replacement string
  2391. {
  2392. return (LPCTSTR)m_fr.lpstrReplaceWith;
  2393. }
  2394. BOOL SearchDown() const // TRUE if search down, FALSE is up
  2395. {
  2396. return ((m_fr.Flags & FR_DOWN) != 0) ? TRUE : FALSE;
  2397. }
  2398. BOOL FindNext() const // TRUE if command is find next
  2399. {
  2400. return ((m_fr.Flags & FR_FINDNEXT) != 0) ? TRUE : FALSE;
  2401. }
  2402. BOOL MatchCase() const // TRUE if matching case
  2403. {
  2404. return ((m_fr.Flags & FR_MATCHCASE) != 0) ? TRUE : FALSE;
  2405. }
  2406. BOOL MatchWholeWord() const // TRUE if matching whole words only
  2407. {
  2408. return ((m_fr.Flags & FR_WHOLEWORD) != 0) ? TRUE : FALSE;
  2409. }
  2410. BOOL ReplaceCurrent() const // TRUE if replacing current string
  2411. {
  2412. return ((m_fr. Flags & FR_REPLACE) != 0) ? TRUE : FALSE;
  2413. }
  2414. BOOL ReplaceAll() const // TRUE if replacing all occurrences
  2415. {
  2416. return ((m_fr.Flags & FR_REPLACEALL) != 0) ? TRUE : FALSE;
  2417. }
  2418. BOOL IsTerminating() const // TRUE if terminating dialog
  2419. {
  2420. return ((m_fr.Flags & FR_DIALOGTERM) != 0) ? TRUE : FALSE ;
  2421. }
  2422. };
  2423. class CFindReplaceDialog : public CFindReplaceDialogImpl<CFindReplaceDialog>
  2424. {
  2425. public:
  2426. DECLARE_EMPTY_MSG_MAP()
  2427. };
  2428. /////////////////////////////////////////////////////////////////////////
  2429. // CDialogBaseUnits - Dialog Units helper
  2430. //
  2431. class CDialogBaseUnits
  2432. {
  2433. public:
  2434. SIZE m_sizeUnits;
  2435. // Constructors
  2436. CDialogBaseUnits()
  2437. {
  2438. // The base units of the out-dated System Font
  2439. LONG nDlgBaseUnits = ::GetDialogBaseUnits();
  2440. m_sizeUnits.cx = LOWORD(nDlgBaseUnits);
  2441. m_sizeUnits.cy = HIWORD(nDlgBaseUnits);
  2442. }
  2443. CDialogBaseUnits(HWND hWnd)
  2444. {
  2445. if(!InitDialogBaseUnits(hWnd)) {
  2446. LONG nDlgBaseUnits = ::GetDialogBaseUnits();
  2447. m_sizeUnits.cx = LOWORD(nDlgBaseUnits);
  2448. m_sizeUnits.cy = HIWORD(nDlgBaseUnits);
  2449. }
  2450. }
  2451. CDialogBaseUnits(HFONT hFont, HWND hWnd = NULL)
  2452. {
  2453. if(!InitDialogBaseUnits(hFont, hWnd)) {
  2454. LONG nDlgBaseUnits = ::GetDialogBaseUnits();
  2455. m_sizeUnits.cx = LOWORD(nDlgBaseUnits);
  2456. m_sizeUnits.cy = HIWORD(nDlgBaseUnits);
  2457. }
  2458. }
  2459. CDialogBaseUnits(const LOGFONT& lf, HWND hWnd = NULL)
  2460. {
  2461. if(!InitDialogBaseUnits(lf, hWnd)) {
  2462. LONG nDlgBaseUnits = ::GetDialogBaseUnits();
  2463. m_sizeUnits.cx = LOWORD(nDlgBaseUnits);
  2464. m_sizeUnits.cy = HIWORD(nDlgBaseUnits);
  2465. }
  2466. }
  2467. // Operations
  2468. BOOL InitDialogBaseUnits(HWND hWnd)
  2469. {
  2470. ATLASSERT(::IsWindow(hWnd));
  2471. RECT rc = { 0, 0, 4, 8 };
  2472. if(!::MapDialogRect(hWnd, &rc)) return FALSE;
  2473. m_sizeUnits.cx = rc.right;
  2474. m_sizeUnits.cy = rc.bottom;
  2475. return TRUE;
  2476. }
  2477. BOOL InitDialogBaseUnits(const LOGFONT& lf, HWND hWnd = NULL)
  2478. {
  2479. CFont font;
  2480. font.CreateFontIndirect(&lf);
  2481. if(font.IsNull()) return FALSE;
  2482. return InitDialogBaseUnits(font, hWnd);
  2483. }
  2484. BOOL InitDialogBaseUnits(HFONT hFont, HWND hWnd = NULL)
  2485. {
  2486. ATLASSERT(hFont != NULL);
  2487. CWindowDC dc = hWnd;
  2488. TEXTMETRIC tmText = {};
  2489. SIZE sizeText = {};
  2490. HFONT hFontOld = dc.SelectFont(hFont);
  2491. dc.GetTextMetrics(&tmText);
  2492. m_sizeUnits.cy = tmText.tmHeight + tmText.tmExternalLeading;
  2493. dc.GetTextExtent(_T("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"), 52, &sizeText);
  2494. m_sizeUnits.cx = (sizeText.cx + 26) / 52;
  2495. dc.SelectFont(hFontOld);
  2496. return TRUE;
  2497. }
  2498. SIZE GetDialogBaseUnits() const
  2499. {
  2500. return m_sizeUnits;
  2501. }
  2502. INT MapDialogPixelsX(INT x) const
  2503. {
  2504. return ::MulDiv(x, 4, m_sizeUnits.cx); // Pixels X to DLU
  2505. }
  2506. INT MapDialogPixelsY(INT y) const
  2507. {
  2508. return ::MulDiv(y, 8, m_sizeUnits.cy); // Pixels Y to DLU
  2509. }
  2510. POINT MapDialogPixels(POINT pt) const
  2511. {
  2512. POINT out = { MapDialogPixelsX(pt.x), MapDialogPixelsY(pt.y) };
  2513. return out;
  2514. }
  2515. SIZE MapDialogPixels(SIZE input) const
  2516. {
  2517. SIZE out = { MapDialogPixelsX(input.cx), MapDialogPixelsY(input.cy) };
  2518. return out;
  2519. }
  2520. RECT MapDialogPixels(const RECT& input) const
  2521. {
  2522. RECT out = { MapDialogPixelsX(input.left), MapDialogPixelsY(input.top), MapDialogPixelsX(input.right), MapDialogPixelsY(input.bottom) };
  2523. return out;
  2524. }
  2525. INT MapDialogUnitsX(INT x) const
  2526. {
  2527. return ::MulDiv(x, m_sizeUnits.cx, 4); // DLU to Pixels X
  2528. }
  2529. INT MapDialogUnitsY(INT y) const
  2530. {
  2531. return ::MulDiv(y, m_sizeUnits.cy, 8); // DLU to Pixels Y
  2532. }
  2533. POINT MapDialogUnits(POINT pt) const
  2534. {
  2535. POINT out = { MapDialogUnitsX(pt.x), MapDialogUnitsY(pt.y) };
  2536. return out;
  2537. }
  2538. SIZE MapDialogUnits(SIZE input) const
  2539. {
  2540. SIZE out = { MapDialogUnitsX(input.cx), MapDialogUnitsY(input.cy) };
  2541. return out;
  2542. }
  2543. RECT MapDialogUnits(const RECT& input) const
  2544. {
  2545. RECT out = { MapDialogUnitsX(input.left), MapDialogUnitsY(input.top), MapDialogUnitsX(input.right), MapDialogUnitsY(input.bottom) };
  2546. return out;
  2547. }
  2548. };
  2549. ///////////////////////////////////////////////////////////////////////////////
  2550. // CMemDlgTemplate - in-memory dialog template - DLGTEMPLATE or DLGTEMPLATEEX
  2551. // traits suitable for dialog controls
  2552. typedef ATL::CWinTraits<WS_CHILD | WS_VISIBLE, 0> CDlgControlWinTraits;
  2553. template <class TWinTraits>
  2554. class CMemDlgTemplateT
  2555. {
  2556. public:
  2557. typedef ATL::_DialogSplitHelper::DLGTEMPLATEEX DLGTEMPLATEEX;
  2558. typedef ATL::_DialogSplitHelper::DLGITEMTEMPLATEEX DLGITEMTEMPLATEEX;
  2559. enum StdCtrlType
  2560. {
  2561. CTRL_BUTTON = 0x0080,
  2562. CTRL_EDIT = 0x0081,
  2563. CTRL_STATIC = 0x0082,
  2564. CTRL_LISTBOX = 0x0083,
  2565. CTRL_SCROLLBAR = 0x0084,
  2566. CTRL_COMBOBOX = 0x0085
  2567. };
  2568. HANDLE m_hData;
  2569. LPBYTE m_pData;
  2570. LPBYTE m_pPtr;
  2571. SIZE_T m_cAllocated;
  2572. CMemDlgTemplateT() : m_hData(NULL), m_pData(NULL), m_pPtr(NULL), m_cAllocated(0)
  2573. { }
  2574. ~CMemDlgTemplateT()
  2575. {
  2576. Reset();
  2577. }
  2578. bool IsValid() const
  2579. {
  2580. return (m_pData != NULL);
  2581. }
  2582. bool IsTemplateEx() const
  2583. {
  2584. return (IsValid() && ((DLGTEMPLATEEX*)m_pData)->signature == 0xFFFF);
  2585. }
  2586. LPDLGTEMPLATE GetTemplatePtr()
  2587. {
  2588. return reinterpret_cast<LPDLGTEMPLATE>(m_pData);
  2589. }
  2590. DLGTEMPLATEEX* GetTemplateExPtr()
  2591. {
  2592. return reinterpret_cast<DLGTEMPLATEEX*>(m_pData);
  2593. }
  2594. void Reset()
  2595. {
  2596. if (IsValid())
  2597. {
  2598. ::GlobalUnlock(m_pData);
  2599. ATLVERIFY(::GlobalFree(m_hData) == NULL);
  2600. }
  2601. m_hData = NULL;
  2602. m_pData = NULL;
  2603. m_pPtr = NULL;
  2604. m_cAllocated = 0;
  2605. }
  2606. void Create(bool bDlgEx, LPCTSTR lpszCaption, const RECT& rc, DWORD dwStyle = 0, DWORD dwExStyle = 0,
  2607. LPCTSTR lpstrFontName = NULL, WORD wFontSize = 0, WORD wWeight = 0, BYTE bItalic = 0, BYTE bCharset = 0, DWORD dwHelpID = 0,
  2608. ATL::_U_STRINGorID ClassName = 0U, ATL::_U_STRINGorID Menu = 0U)
  2609. {
  2610. Create(bDlgEx, lpszCaption, (short) rc.left, (short) rc.top, (short) (rc.right - rc.left), (short) (rc.bottom - rc.top), dwStyle, dwExStyle,
  2611. lpstrFontName, wFontSize, wWeight, bItalic, bCharset, dwHelpID, ClassName.m_lpstr, Menu.m_lpstr);
  2612. }
  2613. void Create(bool bDlgEx, LPCTSTR lpszCaption, short nX, short nY, short nWidth, short nHeight, DWORD dwStyle = 0, DWORD dwExStyle = 0,
  2614. LPCTSTR lpstrFontName = NULL, WORD wFontSize = 0, WORD wWeight = 0, BYTE bItalic = 0, BYTE bCharset = 0, DWORD dwHelpID = 0,
  2615. ATL::_U_STRINGorID ClassName = 0U, ATL::_U_STRINGorID Menu = 0U)
  2616. {
  2617. // Should have DS_SETFONT style to set the dialog font name and size
  2618. if (lpstrFontName != NULL)
  2619. {
  2620. dwStyle |= DS_SETFONT;
  2621. }
  2622. else
  2623. {
  2624. dwStyle &= ~DS_SETFONT;
  2625. }
  2626. if (bDlgEx)
  2627. {
  2628. DLGTEMPLATEEX dlg = {1, 0xFFFF, dwHelpID, dwExStyle, dwStyle, 0, nX, nY, nWidth, nHeight};
  2629. AddData(&dlg, sizeof(dlg));
  2630. }
  2631. else
  2632. {
  2633. DLGTEMPLATE dlg = {dwStyle, dwExStyle, 0, nX, nY, nWidth, nHeight};
  2634. AddData(&dlg, sizeof(dlg));
  2635. }
  2636. if (Menu.m_lpstr == NULL)
  2637. {
  2638. WORD menuData = 0;
  2639. AddData(&menuData, sizeof(WORD));
  2640. }
  2641. else if (IS_INTRESOURCE(Menu.m_lpstr))
  2642. {
  2643. WORD menuData[] = { 0xFFFF, LOWORD(Menu.m_lpstr) };
  2644. AddData(menuData, sizeof(menuData));
  2645. }
  2646. else
  2647. {
  2648. AddString(Menu.m_lpstr);
  2649. }
  2650. if (ClassName.m_lpstr == NULL)
  2651. {
  2652. WORD classData = 0;
  2653. AddData(&classData, sizeof(WORD));
  2654. }
  2655. else if (IS_INTRESOURCE(ClassName.m_lpstr))
  2656. {
  2657. WORD classData[] = { 0xFFFF, LOWORD(ClassName.m_lpstr) };
  2658. AddData(classData, sizeof(classData));
  2659. }
  2660. else
  2661. {
  2662. AddString(ClassName.m_lpstr);
  2663. }
  2664. // Set dialog caption
  2665. AddString(lpszCaption);
  2666. if (lpstrFontName != NULL)
  2667. {
  2668. AddData(&wFontSize, sizeof(wFontSize));
  2669. if (bDlgEx)
  2670. {
  2671. AddData(&wWeight, sizeof(wWeight));
  2672. AddData(&bItalic, sizeof(bItalic));
  2673. AddData(&bCharset, sizeof(bCharset));
  2674. }
  2675. AddString(lpstrFontName);
  2676. }
  2677. }
  2678. void AddControl(ATL::_U_STRINGorID ClassName, WORD wId, const RECT& rc, DWORD dwStyle, DWORD dwExStyle,
  2679. ATL::_U_STRINGorID Text, const WORD* pCreationData = NULL, WORD nCreationData = 0, DWORD dwHelpID = 0)
  2680. {
  2681. AddControl(ClassName.m_lpstr, wId, (short) rc.left, (short) rc.top, (short) (rc.right - rc.left), (short) (rc.bottom - rc.top), dwStyle, dwExStyle,
  2682. Text.m_lpstr, pCreationData, nCreationData, dwHelpID);
  2683. }
  2684. void AddControl(ATL::_U_STRINGorID ClassName, WORD wId, short nX, short nY, short nWidth, short nHeight, DWORD dwStyle, DWORD dwExStyle,
  2685. ATL::_U_STRINGorID Text, const WORD* pCreationData = NULL, WORD nCreationData = 0, DWORD dwHelpID = 0)
  2686. {
  2687. ATLASSERT(IsValid());
  2688. // DWORD align data
  2689. const DWORD_PTR dwDwordAlignBits = sizeof(DWORD) - 1;
  2690. m_pPtr = (LPBYTE)(((DWORD_PTR)m_pPtr + dwDwordAlignBits) & (~dwDwordAlignBits));
  2691. if (IsTemplateEx())
  2692. {
  2693. DLGTEMPLATEEX* dlg = (DLGTEMPLATEEX*)m_pData;
  2694. dlg->cDlgItems++;
  2695. DLGITEMTEMPLATEEX item = {dwHelpID, TWinTraits::GetWndExStyle(0) | dwExStyle, TWinTraits::GetWndStyle(0) | dwStyle, nX, nY, nWidth, nHeight, wId};
  2696. AddData(&item, sizeof(item));
  2697. }
  2698. else
  2699. {
  2700. LPDLGTEMPLATE dlg = (LPDLGTEMPLATE)m_pData;
  2701. dlg->cdit++;
  2702. DLGITEMTEMPLATE item = {TWinTraits::GetWndStyle(0) | dwStyle, TWinTraits::GetWndExStyle(0) | dwExStyle, nX, nY, nWidth, nHeight, wId};
  2703. AddData(&item, sizeof(item));
  2704. }
  2705. ATLASSERT(ClassName.m_lpstr != NULL);
  2706. if (IS_INTRESOURCE(ClassName.m_lpstr))
  2707. {
  2708. WORD wData[] = { 0xFFFF, LOWORD(ClassName.m_lpstr) };
  2709. AddData(wData, sizeof(wData));
  2710. }
  2711. else
  2712. {
  2713. AddString(ClassName.m_lpstr);
  2714. }
  2715. if (Text.m_lpstr == NULL)
  2716. {
  2717. WORD classData = 0;
  2718. AddData(&classData, sizeof(WORD));
  2719. }
  2720. else if (IS_INTRESOURCE(Text.m_lpstr))
  2721. {
  2722. WORD wData[] = { 0xFFFF, LOWORD(Text.m_lpstr) };
  2723. AddData(wData, sizeof(wData));
  2724. }
  2725. else
  2726. {
  2727. AddString(Text.m_lpstr);
  2728. }
  2729. AddData(&nCreationData, sizeof(nCreationData));
  2730. if ((nCreationData != 0))
  2731. {
  2732. ATLASSERT(pCreationData != NULL);
  2733. AddData(pCreationData, nCreationData * sizeof(WORD));
  2734. }
  2735. }
  2736. void AddStdControl(StdCtrlType CtrlType, WORD wId, short nX, short nY, short nWidth, short nHeight,
  2737. DWORD dwStyle, DWORD dwExStyle, ATL::_U_STRINGorID Text, const WORD* pCreationData = NULL, WORD nCreationData = 0, DWORD dwHelpID = 0)
  2738. {
  2739. AddControl(CtrlType, wId, nX, nY, nWidth, nHeight, dwStyle, dwExStyle, Text, pCreationData, nCreationData, dwHelpID);
  2740. }
  2741. void AddData(LPCVOID pData, size_t nData)
  2742. {
  2743. ATLASSERT(pData != NULL);
  2744. const SIZE_T ALLOCATION_INCREMENT = 1024;
  2745. if (m_pData == NULL)
  2746. {
  2747. m_cAllocated = ((nData / ALLOCATION_INCREMENT) + 1) * ALLOCATION_INCREMENT;
  2748. m_hData = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, m_cAllocated);
  2749. ATLASSERT(m_hData != NULL);
  2750. m_pPtr = m_pData = static_cast<LPBYTE>(::GlobalLock(m_hData));
  2751. ATLASSERT(m_pData != NULL);
  2752. }
  2753. else if (((m_pPtr - m_pData) + nData) > m_cAllocated)
  2754. {
  2755. SIZE_T ptrPos = (m_pPtr - m_pData);
  2756. m_cAllocated += ((nData / ALLOCATION_INCREMENT) + 1) * ALLOCATION_INCREMENT;
  2757. ::GlobalUnlock(m_pData);
  2758. m_hData = ::GlobalReAlloc(m_hData, m_cAllocated, GMEM_MOVEABLE | GMEM_ZEROINIT);
  2759. ATLASSERT(m_hData != NULL);
  2760. m_pData = static_cast<LPBYTE>(::GlobalLock(m_hData));
  2761. ATLASSERT(m_pData != NULL);
  2762. m_pPtr = m_pData + ptrPos;
  2763. }
  2764. ATL::Checked::memcpy_s(m_pPtr, m_cAllocated - (m_pPtr - m_pData), pData, nData);
  2765. m_pPtr += nData;
  2766. }
  2767. void AddString(LPCTSTR lpszStr)
  2768. {
  2769. if (lpszStr == NULL)
  2770. {
  2771. WCHAR szEmpty = 0;
  2772. AddData(&szEmpty, sizeof(szEmpty));
  2773. }
  2774. else
  2775. {
  2776. USES_CONVERSION;
  2777. LPCWSTR lpstr = T2CW(lpszStr);
  2778. int nSize = lstrlenW(lpstr) + 1;
  2779. AddData(lpstr, nSize * sizeof(WCHAR));
  2780. }
  2781. }
  2782. };
  2783. typedef CMemDlgTemplateT<CDlgControlWinTraits> CMemDlgTemplate;
  2784. ///////////////////////////////////////////////////////////////////////////////
  2785. // Dialog and control macros for indirect dialogs
  2786. // for DLGTEMPLATE
  2787. #define BEGIN_DIALOG(x, y, width, height) \
  2788. void DoInitTemplate() \
  2789. { \
  2790. bool bExTemplate = false; \
  2791. short nX = x, nY = y, nWidth = width, nHeight = height; \
  2792. LPCTSTR szCaption = NULL; \
  2793. DWORD dwStyle = WS_POPUP | WS_BORDER | WS_SYSMENU; \
  2794. DWORD dwExStyle = 0; \
  2795. LPCTSTR szFontName = NULL; \
  2796. WORD wFontSize = 0; \
  2797. WORD wWeight = 0; \
  2798. BYTE bItalic = 0; \
  2799. BYTE bCharset = 0; \
  2800. DWORD dwHelpID = 0; \
  2801. ATL::_U_STRINGorID Menu = 0U; \
  2802. ATL::_U_STRINGorID ClassName = 0U;
  2803. // for DLGTEMPLATEEX
  2804. #define BEGIN_DIALOG_EX(x, y, width, height, helpID) \
  2805. void DoInitTemplate() \
  2806. { \
  2807. bool bExTemplate = true; \
  2808. short nX = x, nY = y, nWidth = width, nHeight = height; \
  2809. LPCTSTR szCaption = NULL; \
  2810. DWORD dwStyle = WS_POPUP | WS_BORDER | WS_SYSMENU; \
  2811. DWORD dwExStyle = 0; \
  2812. LPCTSTR szFontName = NULL; \
  2813. WORD wFontSize = 0; \
  2814. WORD wWeight = 0; \
  2815. BYTE bItalic = 0; \
  2816. BYTE bCharset = 0; \
  2817. DWORD dwHelpID = helpID; \
  2818. ATL::_U_STRINGorID Menu = 0U; \
  2819. ATL::_U_STRINGorID ClassName = 0U;
  2820. #define END_DIALOG() \
  2821. m_Template.Create(bExTemplate, szCaption, nX, nY, nWidth, nHeight, dwStyle, dwExStyle, szFontName, wFontSize, wWeight, bItalic, bCharset, dwHelpID, ClassName, Menu); \
  2822. }
  2823. #define DIALOG_CAPTION(caption) \
  2824. szCaption = caption;
  2825. #define DIALOG_STYLE(style) \
  2826. dwStyle = style;
  2827. #define DIALOG_EXSTYLE(exStyle) \
  2828. dwExStyle = exStyle;
  2829. #define DIALOG_FONT(pointSize, typeFace) \
  2830. wFontSize = pointSize; \
  2831. szFontName = typeFace;
  2832. #define DIALOG_FONT_EX(pointsize, typeface, weight, italic, charset) \
  2833. ATLASSERT(bExTemplate); \
  2834. wFontSize = pointsize; \
  2835. szFontName = typeface; \
  2836. wWeight = weight; \
  2837. bItalic = italic; \
  2838. bCharset = charset;
  2839. #define DIALOG_MENU(menuName) \
  2840. Menu = menuName;
  2841. #define DIALOG_CLASS(className) \
  2842. ClassName = className;
  2843. #define BEGIN_CONTROLS_MAP() \
  2844. void DoInitControls() \
  2845. {
  2846. #define END_CONTROLS_MAP() \
  2847. }
  2848. #define CONTROL_LTEXT(text, id, x, y, width, height, style, exStyle) \
  2849. m_Template.AddStdControl(m_Template.CTRL_STATIC, (WORD)id, x, y, width, height, style | SS_LEFT | WS_GROUP, exStyle, text, NULL, 0);
  2850. #define CONTROL_CTEXT(text, id, x, y, width, height, style, exStyle) \
  2851. m_Template.AddStdControl(m_Template.CTRL_STATIC, (WORD)id, x, y, width, height, style | SS_CENTER | WS_GROUP, exStyle, text, NULL, 0);
  2852. #define CONTROL_RTEXT(text, id, x, y, width, height, style, exStyle) \
  2853. m_Template.AddStdControl(m_Template.CTRL_STATIC, (WORD)id, x, y, width, height, style | SS_RIGHT | WS_GROUP, exStyle, text, NULL, 0);
  2854. #define CONTROL_PUSHBUTTON(text, id, x, y, width, height, style, exStyle) \
  2855. m_Template.AddStdControl(m_Template.CTRL_BUTTON, (WORD)id, x, y, width, height, style | BS_PUSHBUTTON | WS_TABSTOP, exStyle, text, NULL, 0);
  2856. #define CONTROL_DEFPUSHBUTTON(text, id, x, y, width, height, style, exStyle) \
  2857. m_Template.AddStdControl(m_Template.CTRL_BUTTON, (WORD)id, x, y, width, height, style | BS_DEFPUSHBUTTON | WS_TABSTOP, exStyle, text, NULL, 0);
  2858. #define CONTROL_PUSHBOX(text, id, x, y, width, height, style, exStyle) \
  2859. m_Template.AddStdControl(m_Template.CTRL_BUTTON, (WORD)id, x, y, width, height, style | BS_PUSHBOX | WS_TABSTOP, exStyle, text, NULL, 0);
  2860. #define CONTROL_STATE3(text, id, x, y, width, height, style, exStyle) \
  2861. m_Template.AddStdControl(m_Template.CTRL_BUTTON, (WORD)id, x, y, width, height, style | BS_3STATE | WS_TABSTOP, exStyle, text, NULL, 0);
  2862. #define CONTROL_AUTO3STATE(text, id, x, y, width, height, style, exStyle) \
  2863. m_Template.AddStdControl(m_Template.CTRL_BUTTON, (WORD)id, x, y, width, height, style | BS_AUTO3STATE | WS_TABSTOP, exStyle, text, NULL, 0);
  2864. #define CONTROL_CHECKBOX(text, id, x, y, width, height, style, exStyle) \
  2865. m_Template.AddStdControl(m_Template.CTRL_BUTTON, (WORD)id, x, y, width, height, style | BS_CHECKBOX | WS_TABSTOP, exStyle, text, NULL, 0);
  2866. #define CONTROL_AUTOCHECKBOX(text, id, x, y, width, height, style, exStyle) \
  2867. m_Template.AddStdControl(m_Template.CTRL_BUTTON, (WORD)id, x, y, width, height, style | BS_AUTOCHECKBOX | WS_TABSTOP, exStyle, text, NULL, 0);
  2868. #define CONTROL_RADIOBUTTON(text, id, x, y, width, height, style, exStyle) \
  2869. m_Template.AddStdControl(m_Template.CTRL_BUTTON, (WORD)id, x, y, width, height, style | BS_RADIOBUTTON | WS_TABSTOP, exStyle, text, NULL, 0);
  2870. #define CONTROL_AUTORADIOBUTTON(text, id, x, y, width, height, style, exStyle) \
  2871. m_Template.AddStdControl(m_Template.CTRL_BUTTON, (WORD)id, x, y, width, height, style | BS_AUTORADIOBUTTON | WS_TABSTOP, exStyle, text, NULL, 0);
  2872. #define CONTROL_COMBOBOX(id, x, y, width, height, style, exStyle) \
  2873. m_Template.AddStdControl(m_Template.CTRL_COMBOBOX, (WORD)id, x, y, width, height, style | CBS_DROPDOWN | WS_TABSTOP, exStyle, (LPCTSTR)NULL, NULL, 0);
  2874. #define CONTROL_EDITTEXT(id, x, y, width, height, style, exStyle) \
  2875. m_Template.AddStdControl(m_Template.CTRL_EDIT, (WORD)id, x, y, width, height, style | ES_LEFT | WS_BORDER | WS_TABSTOP, exStyle, (LPCTSTR)NULL, NULL, 0);
  2876. #define CONTROL_GROUPBOX(text, id, x, y, width, height, style, exStyle) \
  2877. m_Template.AddStdControl(m_Template.CTRL_BUTTON, (WORD)id, x, y, width, height, style | BS_GROUPBOX, exStyle, text, NULL, 0);
  2878. #define CONTROL_LISTBOX(id, x, y, width, height, style, exStyle) \
  2879. m_Template.AddStdControl(m_Template.CTRL_LISTBOX, (WORD)id, x, y, width, height, style | LBS_NOTIFY | WS_BORDER, exStyle, (LPCTSTR)NULL, NULL, 0);
  2880. #define CONTROL_SCROLLBAR(id, x, y, width, height, style, exStyle) \
  2881. m_Template.AddStdControl(m_Template.CTRL_SCROLLBAR, (WORD)id, x, y, width, height, style | SBS_HORZ, exStyle, (LPCTSTR)NULL, NULL, 0);
  2882. #define CONTROL_ICON(text, id, x, y, width, height, style, exStyle) \
  2883. m_Template.AddStdControl(m_Template.CTRL_STATIC, (WORD)id, x, y, width, height, style | SS_ICON, exStyle, text, NULL, 0);
  2884. #define CONTROL_CONTROL(text, id, className, style, x, y, width, height, exStyle) \
  2885. m_Template.AddControl(className, (WORD)id, x, y, width, height, style, exStyle, text, NULL, 0);
  2886. ///////////////////////////////////////////////////////////////////////////////
  2887. // CIndirectDialogImpl - dialogs with template in memory
  2888. template <class T, class TDlgTemplate = CMemDlgTemplate, class TBase = ATL::CWindow>
  2889. class ATL_NO_VTABLE CIndirectDialogImpl : public ATL::CDialogImpl< T, TBase >
  2890. {
  2891. public:
  2892. enum { IDD = 0 }; // no dialog template resource
  2893. TDlgTemplate m_Template;
  2894. void CreateTemplate()
  2895. {
  2896. T* pT = static_cast<T*>(this);
  2897. pT->DoInitTemplate();
  2898. pT->DoInitControls();
  2899. }
  2900. INT_PTR DoModal(HWND hWndParent = ::GetActiveWindow(), LPARAM dwInitParam = NULL)
  2901. {
  2902. T* pT = static_cast<T*>(this);
  2903. ATLASSERT(pT->m_hWnd == NULL);
  2904. if(!m_Template.IsValid())
  2905. CreateTemplate();
  2906. // Allocate the thunk structure here, where we can fail gracefully.
  2907. BOOL bRet = this->m_thunk.Init(NULL, NULL);
  2908. if(bRet == FALSE)
  2909. {
  2910. ::SetLastError(ERROR_OUTOFMEMORY);
  2911. return -1;
  2912. }
  2913. ModuleHelper::AddCreateWndData(&this->m_thunk.cd, (ATL::CDialogImplBaseT< TBase >*)pT);
  2914. #ifdef _DEBUG
  2915. this->m_bModal = true;
  2916. #endif // _DEBUG
  2917. return ::DialogBoxIndirectParam(ModuleHelper::GetResourceInstance(), m_Template.GetTemplatePtr(), hWndParent, (DLGPROC)T::StartDialogProc, dwInitParam);
  2918. }
  2919. HWND Create(HWND hWndParent, LPARAM dwInitParam = NULL)
  2920. {
  2921. T* pT = static_cast<T*>(this);
  2922. ATLASSERT(pT->m_hWnd == NULL);
  2923. if(!m_Template.IsValid())
  2924. CreateTemplate();
  2925. // Allocate the thunk structure here, where we can fail gracefully.
  2926. BOOL bRet = this->m_thunk.Init(NULL, NULL);
  2927. if(bRet == FALSE)
  2928. {
  2929. ::SetLastError(ERROR_OUTOFMEMORY);
  2930. return NULL;
  2931. }
  2932. ModuleHelper::AddCreateWndData(&this->m_thunk.cd, (ATL::CDialogImplBaseT< TBase >*)pT);
  2933. #ifdef _DEBUG
  2934. this->m_bModal = false;
  2935. #endif // _DEBUG
  2936. HWND hWnd = ::CreateDialogIndirectParam(ModuleHelper::GetResourceInstance(), (LPCDLGTEMPLATE)m_Template.GetTemplatePtr(), hWndParent, (DLGPROC)T::StartDialogProc, dwInitParam);
  2937. ATLASSERT(this->m_hWnd == hWnd);
  2938. return hWnd;
  2939. }
  2940. // for CComControl
  2941. HWND Create(HWND hWndParent, RECT&, LPARAM dwInitParam = NULL)
  2942. {
  2943. return Create(hWndParent, dwInitParam);
  2944. }
  2945. void DoInitTemplate()
  2946. {
  2947. ATLASSERT(FALSE); // MUST be defined in derived class
  2948. }
  2949. void DoInitControls()
  2950. {
  2951. ATLASSERT(FALSE); // MUST be defined in derived class
  2952. }
  2953. };
  2954. ///////////////////////////////////////////////////////////////////////////////
  2955. // CPropertySheetWindow - client side for a property sheet
  2956. class CPropertySheetWindow : public ATL::CWindow
  2957. {
  2958. public:
  2959. // Constructors
  2960. CPropertySheetWindow(HWND hWnd = NULL) : ATL::CWindow(hWnd)
  2961. { }
  2962. CPropertySheetWindow& operator =(HWND hWnd)
  2963. {
  2964. m_hWnd = hWnd;
  2965. return *this;
  2966. }
  2967. // Attributes
  2968. int GetPageCount() const
  2969. {
  2970. ATLASSERT(::IsWindow(m_hWnd));
  2971. HWND hWndTabCtrl = GetTabControl();
  2972. ATLASSERT(hWndTabCtrl != NULL);
  2973. return (int)::SendMessage(hWndTabCtrl, TCM_GETITEMCOUNT, 0, 0L);
  2974. }
  2975. HWND GetActivePage() const
  2976. {
  2977. ATLASSERT(::IsWindow(m_hWnd));
  2978. return (HWND)::SendMessage(m_hWnd, PSM_GETCURRENTPAGEHWND, 0, 0L);
  2979. }
  2980. int GetActiveIndex() const
  2981. {
  2982. ATLASSERT(::IsWindow(m_hWnd));
  2983. HWND hWndTabCtrl = GetTabControl();
  2984. ATLASSERT(hWndTabCtrl != NULL);
  2985. return (int)::SendMessage(hWndTabCtrl, TCM_GETCURSEL, 0, 0L);
  2986. }
  2987. BOOL SetActivePage(int nPageIndex)
  2988. {
  2989. ATLASSERT(::IsWindow(m_hWnd));
  2990. return (BOOL)::SendMessage(m_hWnd, PSM_SETCURSEL, nPageIndex, 0L);
  2991. }
  2992. BOOL SetActivePage(HPROPSHEETPAGE hPage)
  2993. {
  2994. ATLASSERT(::IsWindow(m_hWnd));
  2995. ATLASSERT(hPage != NULL);
  2996. return (BOOL)::SendMessage(m_hWnd, PSM_SETCURSEL, 0, (LPARAM)hPage);
  2997. }
  2998. BOOL SetActivePageByID(int nPageID)
  2999. {
  3000. ATLASSERT(::IsWindow(m_hWnd));
  3001. return (BOOL)::SendMessage(m_hWnd, PSM_SETCURSELID, 0, nPageID);
  3002. }
  3003. void SetTitle(LPCTSTR lpszText, UINT nStyle = 0)
  3004. {
  3005. ATLASSERT(::IsWindow(m_hWnd));
  3006. ATLASSERT((nStyle & ~PSH_PROPTITLE) == 0); // only PSH_PROPTITLE is valid
  3007. ATLASSERT(lpszText != NULL);
  3008. ::SendMessage(m_hWnd, PSM_SETTITLE, nStyle, (LPARAM)lpszText);
  3009. }
  3010. HWND GetTabControl() const
  3011. {
  3012. ATLASSERT(::IsWindow(m_hWnd));
  3013. return (HWND)::SendMessage(m_hWnd, PSM_GETTABCONTROL, 0, 0L);
  3014. }
  3015. void SetFinishText(LPCTSTR lpszText)
  3016. {
  3017. ATLASSERT(::IsWindow(m_hWnd));
  3018. ::SendMessage(m_hWnd, PSM_SETFINISHTEXT, 0, (LPARAM)lpszText);
  3019. }
  3020. void SetWizardButtons(DWORD dwFlags)
  3021. {
  3022. ATLASSERT(::IsWindow(m_hWnd));
  3023. ::PostMessage(m_hWnd, PSM_SETWIZBUTTONS, 0, dwFlags);
  3024. }
  3025. // Operations
  3026. BOOL AddPage(HPROPSHEETPAGE hPage)
  3027. {
  3028. ATLASSERT(::IsWindow(m_hWnd));
  3029. ATLASSERT(hPage != NULL);
  3030. return (BOOL)::SendMessage(m_hWnd, PSM_ADDPAGE, 0, (LPARAM)hPage);
  3031. }
  3032. BOOL AddPage(LPCPROPSHEETPAGE pPage)
  3033. {
  3034. ATLASSERT(::IsWindow(m_hWnd));
  3035. ATLASSERT(pPage != NULL);
  3036. HPROPSHEETPAGE hPage = ::CreatePropertySheetPage(pPage);
  3037. if(hPage == NULL)
  3038. return FALSE;
  3039. return (BOOL)::SendMessage(m_hWnd, PSM_ADDPAGE, 0, (LPARAM)hPage);
  3040. }
  3041. BOOL InsertPage(int nNewPageIndex, HPROPSHEETPAGE hPage)
  3042. {
  3043. ATLASSERT(::IsWindow(m_hWnd));
  3044. ATLASSERT(hPage != NULL);
  3045. return (BOOL)::SendMessage(m_hWnd, PSM_INSERTPAGE, nNewPageIndex, (LPARAM)hPage);
  3046. }
  3047. BOOL InsertPage(int nNewPageIndex, LPCPROPSHEETPAGE pPage)
  3048. {
  3049. ATLASSERT(::IsWindow(m_hWnd));
  3050. ATLASSERT(pPage != NULL);
  3051. HPROPSHEETPAGE hPage = ::CreatePropertySheetPage(pPage);
  3052. if(hPage == NULL)
  3053. return FALSE;
  3054. return (BOOL)::SendMessage(m_hWnd, PSM_INSERTPAGE, nNewPageIndex, (LPARAM)hPage);
  3055. }
  3056. BOOL InsertPage(HPROPSHEETPAGE hPageInsertAfter, HPROPSHEETPAGE hPage)
  3057. {
  3058. ATLASSERT(::IsWindow(m_hWnd));
  3059. ATLASSERT(hPage != NULL);
  3060. return (BOOL)::SendMessage(m_hWnd, PSM_INSERTPAGE, (WPARAM)hPageInsertAfter, (LPARAM)hPage);
  3061. }
  3062. BOOL InsertPage(HPROPSHEETPAGE hPageInsertAfter, LPCPROPSHEETPAGE pPage)
  3063. {
  3064. ATLASSERT(::IsWindow(m_hWnd));
  3065. ATLASSERT(pPage != NULL);
  3066. HPROPSHEETPAGE hPage = ::CreatePropertySheetPage(pPage);
  3067. if(hPage == NULL)
  3068. return FALSE;
  3069. return (BOOL)::SendMessage(m_hWnd, PSM_INSERTPAGE, (WPARAM)hPageInsertAfter, (LPARAM)hPage);
  3070. }
  3071. void RemovePage(int nPageIndex)
  3072. {
  3073. ATLASSERT(::IsWindow(m_hWnd));
  3074. ::SendMessage(m_hWnd, PSM_REMOVEPAGE, nPageIndex, 0L);
  3075. }
  3076. void RemovePage(HPROPSHEETPAGE hPage)
  3077. {
  3078. ATLASSERT(::IsWindow(m_hWnd));
  3079. ATLASSERT(hPage != NULL);
  3080. ::SendMessage(m_hWnd, PSM_REMOVEPAGE, 0, (LPARAM)hPage);
  3081. }
  3082. BOOL PressButton(int nButton)
  3083. {
  3084. ATLASSERT(::IsWindow(m_hWnd));
  3085. return (BOOL)::SendMessage(m_hWnd, PSM_PRESSBUTTON, nButton, 0L);
  3086. }
  3087. BOOL Apply()
  3088. {
  3089. ATLASSERT(::IsWindow(m_hWnd));
  3090. return (BOOL)::SendMessage(m_hWnd, PSM_APPLY, 0, 0L);
  3091. }
  3092. void CancelToClose()
  3093. {
  3094. ATLASSERT(::IsWindow(m_hWnd));
  3095. ::SendMessage(m_hWnd, PSM_CANCELTOCLOSE, 0, 0L);
  3096. }
  3097. void SetModified(HWND hWndPage, BOOL bChanged = TRUE)
  3098. {
  3099. ATLASSERT(::IsWindow(m_hWnd));
  3100. ATLASSERT(::IsWindow(hWndPage));
  3101. UINT uMsg = bChanged ? PSM_CHANGED : PSM_UNCHANGED;
  3102. ::SendMessage(m_hWnd, uMsg, (WPARAM)hWndPage, 0L);
  3103. }
  3104. LRESULT QuerySiblings(WPARAM wParam, LPARAM lParam)
  3105. {
  3106. ATLASSERT(::IsWindow(m_hWnd));
  3107. return ::SendMessage(m_hWnd, PSM_QUERYSIBLINGS, wParam, lParam);
  3108. }
  3109. void RebootSystem()
  3110. {
  3111. ATLASSERT(::IsWindow(m_hWnd));
  3112. ::SendMessage(m_hWnd, PSM_REBOOTSYSTEM, 0, 0L);
  3113. }
  3114. void RestartWindows()
  3115. {
  3116. ATLASSERT(::IsWindow(m_hWnd));
  3117. ::SendMessage(m_hWnd, PSM_RESTARTWINDOWS, 0, 0L);
  3118. }
  3119. BOOL IsDialogMessage(LPMSG lpMsg)
  3120. {
  3121. ATLASSERT(::IsWindow(m_hWnd));
  3122. return (BOOL)::SendMessage(m_hWnd, PSM_ISDIALOGMESSAGE, 0, (LPARAM)lpMsg);
  3123. }
  3124. int HwndToIndex(HWND hWnd) const
  3125. {
  3126. ATLASSERT(::IsWindow(m_hWnd));
  3127. return (int)::SendMessage(m_hWnd, PSM_HWNDTOINDEX, (WPARAM)hWnd, 0L);
  3128. }
  3129. HWND IndexToHwnd(int nIndex) const
  3130. {
  3131. ATLASSERT(::IsWindow(m_hWnd));
  3132. return (HWND)::SendMessage(m_hWnd, PSM_INDEXTOHWND, nIndex, 0L);
  3133. }
  3134. int PageToIndex(HPROPSHEETPAGE hPage) const
  3135. {
  3136. ATLASSERT(::IsWindow(m_hWnd));
  3137. return (int)::SendMessage(m_hWnd, PSM_PAGETOINDEX, 0, (LPARAM)hPage);
  3138. }
  3139. HPROPSHEETPAGE IndexToPage(int nIndex) const
  3140. {
  3141. ATLASSERT(::IsWindow(m_hWnd));
  3142. return (HPROPSHEETPAGE)::SendMessage(m_hWnd, PSM_INDEXTOPAGE, nIndex, 0L);
  3143. }
  3144. int IdToIndex(int nID) const
  3145. {
  3146. ATLASSERT(::IsWindow(m_hWnd));
  3147. return (int)::SendMessage(m_hWnd, PSM_IDTOINDEX, 0, nID);
  3148. }
  3149. int IndexToId(int nIndex) const
  3150. {
  3151. ATLASSERT(::IsWindow(m_hWnd));
  3152. return (int)::SendMessage(m_hWnd, PSM_INDEXTOID, nIndex, 0L);
  3153. }
  3154. int GetResult() const
  3155. {
  3156. ATLASSERT(::IsWindow(m_hWnd));
  3157. return (int)::SendMessage(m_hWnd, PSM_GETRESULT, 0, 0L);
  3158. }
  3159. BOOL RecalcPageSizes()
  3160. {
  3161. ATLASSERT(::IsWindow(m_hWnd));
  3162. return (BOOL)::SendMessage(m_hWnd, PSM_RECALCPAGESIZES, 0, 0L);
  3163. }
  3164. void SetHeaderTitle(int nIndex, LPCTSTR lpstrHeaderTitle)
  3165. {
  3166. ATLASSERT(::IsWindow(m_hWnd));
  3167. ::SendMessage(m_hWnd, PSM_SETHEADERTITLE, nIndex, (LPARAM)lpstrHeaderTitle);
  3168. }
  3169. void SetHeaderSubTitle(int nIndex, LPCTSTR lpstrHeaderSubTitle)
  3170. {
  3171. ATLASSERT(::IsWindow(m_hWnd));
  3172. ::SendMessage(m_hWnd, PSM_SETHEADERSUBTITLE, nIndex, (LPARAM)lpstrHeaderSubTitle);
  3173. }
  3174. // Implementation - override to prevent usage
  3175. HWND Create(LPCTSTR, HWND, ATL::_U_RECT = NULL, LPCTSTR = NULL, DWORD = 0, DWORD = 0, ATL::_U_MENUorID = 0U, LPVOID = NULL)
  3176. {
  3177. ATLASSERT(FALSE);
  3178. return NULL;
  3179. }
  3180. };
  3181. ///////////////////////////////////////////////////////////////////////////////
  3182. // CPropertySheetImpl - implements a property sheet
  3183. template <class T, class TBase = CPropertySheetWindow>
  3184. class ATL_NO_VTABLE CPropertySheetImpl : public ATL::CWindowImplBaseT< TBase >
  3185. {
  3186. public:
  3187. PROPSHEETHEADER m_psh;
  3188. ATL::CSimpleArray<HPROPSHEETPAGE> m_arrPages;
  3189. // Construction/Destruction
  3190. CPropertySheetImpl(ATL::_U_STRINGorID title = (LPCTSTR)NULL, UINT uStartPage = 0, HWND hWndParent = NULL)
  3191. {
  3192. memset(&m_psh, 0, sizeof(PROPSHEETHEADER));
  3193. m_psh.dwSize = sizeof(PROPSHEETHEADER);
  3194. m_psh.dwFlags = PSH_USECALLBACK;
  3195. m_psh.hInstance = ModuleHelper::GetResourceInstance();
  3196. m_psh.phpage = NULL; // will be set later
  3197. m_psh.nPages = 0; // will be set later
  3198. m_psh.pszCaption = title.m_lpstr;
  3199. m_psh.nStartPage = uStartPage;
  3200. m_psh.hwndParent = hWndParent; // if NULL, will be set in DoModal/Create
  3201. m_psh.pfnCallback = T::PropSheetCallback;
  3202. }
  3203. ~CPropertySheetImpl()
  3204. {
  3205. if(m_arrPages.GetSize() > 0) // sheet never created, destroy all pages
  3206. {
  3207. for(int i = 0; i < m_arrPages.GetSize(); i++)
  3208. ::DestroyPropertySheetPage((HPROPSHEETPAGE)m_arrPages[i]);
  3209. }
  3210. }
  3211. // Callback function and overrideables
  3212. static int CALLBACK PropSheetCallback(HWND hWnd, UINT uMsg, LPARAM lParam)
  3213. {
  3214. (void)lParam; // avoid level 4 warning
  3215. int nRet = 0;
  3216. if(uMsg == PSCB_INITIALIZED)
  3217. {
  3218. ATLASSERT(hWnd != NULL);
  3219. T* pT = (T*)ModuleHelper::ExtractCreateWndData();
  3220. // subclass the sheet window
  3221. pT->SubclassWindow(hWnd);
  3222. // remove page handles array
  3223. pT->_CleanUpPages();
  3224. pT->OnSheetInitialized();
  3225. }
  3226. return nRet;
  3227. }
  3228. void OnSheetInitialized()
  3229. {
  3230. }
  3231. // Create method
  3232. HWND Create(HWND hWndParent = NULL)
  3233. {
  3234. ATLASSERT(this->m_hWnd == NULL);
  3235. m_psh.dwFlags |= PSH_MODELESS;
  3236. if(m_psh.hwndParent == NULL)
  3237. m_psh.hwndParent = hWndParent;
  3238. m_psh.phpage = (HPROPSHEETPAGE*)m_arrPages.GetData();
  3239. m_psh.nPages = m_arrPages.GetSize();
  3240. T* pT = static_cast<T*>(this);
  3241. // Allocate the thunk structure here, where we can fail gracefully.
  3242. BOOL bRet = pT->m_thunk.Init(NULL, NULL);
  3243. if(bRet == FALSE)
  3244. {
  3245. ::SetLastError(ERROR_OUTOFMEMORY);
  3246. return NULL;
  3247. }
  3248. ModuleHelper::AddCreateWndData(&pT->m_thunk.cd, pT);
  3249. HWND hWnd = (HWND)::PropertySheet(&m_psh);
  3250. _CleanUpPages(); // ensure clean-up, required if call failed
  3251. ATLASSERT(this->m_hWnd == hWnd);
  3252. return hWnd;
  3253. }
  3254. INT_PTR DoModal(HWND hWndParent = ::GetActiveWindow())
  3255. {
  3256. ATLASSERT(this->m_hWnd == NULL);
  3257. m_psh.dwFlags &= ~PSH_MODELESS;
  3258. if(m_psh.hwndParent == NULL)
  3259. m_psh.hwndParent = hWndParent;
  3260. m_psh.phpage = (HPROPSHEETPAGE*)m_arrPages.GetData();
  3261. m_psh.nPages = m_arrPages.GetSize();
  3262. T* pT = static_cast<T*>(this);
  3263. // Allocate the thunk structure here, where we can fail gracefully.
  3264. BOOL bRet = pT->m_thunk.Init(NULL, NULL);
  3265. if(bRet == FALSE)
  3266. {
  3267. ::SetLastError(ERROR_OUTOFMEMORY);
  3268. return -1;
  3269. }
  3270. ModuleHelper::AddCreateWndData(&pT->m_thunk.cd, pT);
  3271. INT_PTR nRet = ::PropertySheet(&m_psh);
  3272. _CleanUpPages(); // ensure clean-up, required if call failed
  3273. return nRet;
  3274. }
  3275. // implementation helper - clean up pages array
  3276. void _CleanUpPages()
  3277. {
  3278. m_psh.nPages = 0;
  3279. m_psh.phpage = NULL;
  3280. m_arrPages.RemoveAll();
  3281. }
  3282. // Attributes (extended overrides of client class methods)
  3283. // These now can be called before the sheet is created
  3284. // Note: Calling these after the sheet is created gives unpredictable results
  3285. int GetPageCount() const
  3286. {
  3287. if(this->m_hWnd == NULL) // not created yet
  3288. return m_arrPages.GetSize();
  3289. return TBase::GetPageCount();
  3290. }
  3291. int GetActiveIndex() const
  3292. {
  3293. if(this->m_hWnd == NULL) // not created yet
  3294. return m_psh.nStartPage;
  3295. return TBase::GetActiveIndex();
  3296. }
  3297. HPROPSHEETPAGE GetPage(int nPageIndex) const
  3298. {
  3299. ATLASSERT(this->m_hWnd == NULL); // can't do this after it's created
  3300. return (HPROPSHEETPAGE)m_arrPages[nPageIndex];
  3301. }
  3302. int GetPageIndex(HPROPSHEETPAGE hPage) const
  3303. {
  3304. ATLASSERT(this->m_hWnd == NULL); // can't do this after it's created
  3305. return m_arrPages.Find((HPROPSHEETPAGE&)hPage);
  3306. }
  3307. BOOL SetActivePage(int nPageIndex)
  3308. {
  3309. if(this->m_hWnd == NULL) // not created yet
  3310. {
  3311. ATLASSERT((nPageIndex >= 0) && (nPageIndex < m_arrPages.GetSize()));
  3312. m_psh.nStartPage = nPageIndex;
  3313. return TRUE;
  3314. }
  3315. return TBase::SetActivePage(nPageIndex);
  3316. }
  3317. BOOL SetActivePage(HPROPSHEETPAGE hPage)
  3318. {
  3319. ATLASSERT(hPage != NULL);
  3320. if(this->m_hWnd == NULL) // not created yet
  3321. {
  3322. int nPageIndex = GetPageIndex(hPage);
  3323. if(nPageIndex == -1)
  3324. return FALSE;
  3325. return SetActivePage(nPageIndex);
  3326. }
  3327. return TBase::SetActivePage(hPage);
  3328. }
  3329. void SetTitle(LPCTSTR lpszText, UINT nStyle = 0)
  3330. {
  3331. ATLASSERT((nStyle & ~PSH_PROPTITLE) == 0); // only PSH_PROPTITLE is valid
  3332. ATLASSERT(lpszText != NULL);
  3333. if(this->m_hWnd == NULL)
  3334. {
  3335. // set internal state
  3336. m_psh.pszCaption = lpszText; // must exist until sheet is created
  3337. m_psh.dwFlags &= ~PSH_PROPTITLE;
  3338. m_psh.dwFlags |= nStyle;
  3339. }
  3340. else
  3341. {
  3342. // set external state
  3343. TBase::SetTitle(lpszText, nStyle);
  3344. }
  3345. }
  3346. void SetWizardMode()
  3347. {
  3348. m_psh.dwFlags |= PSH_WIZARD;
  3349. }
  3350. void EnableHelp()
  3351. {
  3352. m_psh.dwFlags |= PSH_HASHELP;
  3353. }
  3354. // Operations
  3355. BOOL AddPage(HPROPSHEETPAGE hPage)
  3356. {
  3357. ATLASSERT(hPage != NULL);
  3358. BOOL bRet = FALSE;
  3359. if(this->m_hWnd != NULL)
  3360. bRet = TBase::AddPage(hPage);
  3361. else // sheet not created yet, use internal data
  3362. bRet = m_arrPages.Add((HPROPSHEETPAGE&)hPage);
  3363. return bRet;
  3364. }
  3365. BOOL AddPage(LPCPROPSHEETPAGE pPage)
  3366. {
  3367. ATLASSERT(pPage != NULL);
  3368. HPROPSHEETPAGE hPage = ::CreatePropertySheetPage(pPage);
  3369. if(hPage == NULL)
  3370. return FALSE;
  3371. BOOL bRet = AddPage(hPage);
  3372. if(!bRet)
  3373. ::DestroyPropertySheetPage(hPage);
  3374. return bRet;
  3375. }
  3376. BOOL RemovePage(HPROPSHEETPAGE hPage)
  3377. {
  3378. ATLASSERT(hPage != NULL);
  3379. if(this->m_hWnd == NULL) // not created yet
  3380. {
  3381. int nPage = GetPageIndex(hPage);
  3382. if(nPage == -1)
  3383. return FALSE;
  3384. return RemovePage(nPage);
  3385. }
  3386. TBase::RemovePage(hPage);
  3387. return TRUE;
  3388. }
  3389. BOOL RemovePage(int nPageIndex)
  3390. {
  3391. BOOL bRet = TRUE;
  3392. if(this->m_hWnd != NULL)
  3393. TBase::RemovePage(nPageIndex);
  3394. else // sheet not created yet, use internal data
  3395. bRet = m_arrPages.RemoveAt(nPageIndex);
  3396. return bRet;
  3397. }
  3398. void SetHeader(LPCTSTR szbmHeader)
  3399. {
  3400. ATLASSERT(this->m_hWnd == NULL); // can't do this after it's created
  3401. m_psh.dwFlags &= ~PSH_WIZARD;
  3402. m_psh.dwFlags |= (PSH_HEADER | PSH_WIZARD97);
  3403. m_psh.pszbmHeader = szbmHeader;
  3404. }
  3405. void SetHeader(HBITMAP hbmHeader)
  3406. {
  3407. ATLASSERT(this->m_hWnd == NULL); // can't do this after it's created
  3408. m_psh.dwFlags &= ~PSH_WIZARD;
  3409. m_psh.dwFlags |= (PSH_HEADER | PSH_USEHBMHEADER | PSH_WIZARD97);
  3410. m_psh.hbmHeader = hbmHeader;
  3411. }
  3412. void SetWatermark(LPCTSTR szbmWatermark, HPALETTE hplWatermark = NULL)
  3413. {
  3414. ATLASSERT(this->m_hWnd == NULL); // can't do this after it's created
  3415. m_psh.dwFlags &= ~PSH_WIZARD;
  3416. m_psh.dwFlags |= PSH_WATERMARK | PSH_WIZARD97;
  3417. m_psh.pszbmWatermark = szbmWatermark;
  3418. if(hplWatermark != NULL)
  3419. {
  3420. m_psh.dwFlags |= PSH_USEHPLWATERMARK;
  3421. m_psh.hplWatermark = hplWatermark;
  3422. }
  3423. }
  3424. void SetWatermark(HBITMAP hbmWatermark, HPALETTE hplWatermark = NULL)
  3425. {
  3426. ATLASSERT(this->m_hWnd == NULL); // can't do this after it's created
  3427. m_psh.dwFlags &= ~PSH_WIZARD;
  3428. m_psh.dwFlags |= (PSH_WATERMARK | PSH_USEHBMWATERMARK | PSH_WIZARD97);
  3429. m_psh.hbmWatermark = hbmWatermark;
  3430. if(hplWatermark != NULL)
  3431. {
  3432. m_psh.dwFlags |= PSH_USEHPLWATERMARK;
  3433. m_psh.hplWatermark = hplWatermark;
  3434. }
  3435. }
  3436. void StretchWatermark(bool bStretchWatermark)
  3437. {
  3438. ATLASSERT(this->m_hWnd == NULL); // can't do this after it's created
  3439. if(bStretchWatermark)
  3440. m_psh.dwFlags |= PSH_STRETCHWATERMARK;
  3441. else
  3442. m_psh.dwFlags &= ~PSH_STRETCHWATERMARK;
  3443. }
  3444. // Message map and handlers
  3445. BEGIN_MSG_MAP(CPropertySheetImpl)
  3446. MESSAGE_HANDLER(WM_COMMAND, OnCommand)
  3447. MESSAGE_HANDLER(WM_SYSCOMMAND, OnSysCommand)
  3448. END_MSG_MAP()
  3449. LRESULT OnCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
  3450. {
  3451. LRESULT lRet = this->DefWindowProc(uMsg, wParam, lParam);
  3452. if((HIWORD(wParam) == BN_CLICKED) && ((LOWORD(wParam) == IDOK) || (LOWORD(wParam) == IDCANCEL)) &&
  3453. ((m_psh.dwFlags & PSH_MODELESS) != 0) && (this->GetActivePage() == NULL))
  3454. this->DestroyWindow();
  3455. return lRet;
  3456. }
  3457. LRESULT OnSysCommand(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled)
  3458. {
  3459. if(((m_psh.dwFlags & PSH_MODELESS) == PSH_MODELESS) && ((wParam & 0xFFF0) == SC_CLOSE))
  3460. this->SendMessage(WM_CLOSE);
  3461. else
  3462. bHandled = FALSE;
  3463. return 0;
  3464. }
  3465. };
  3466. // for non-customized sheets
  3467. class CPropertySheet : public CPropertySheetImpl<CPropertySheet>
  3468. {
  3469. public:
  3470. CPropertySheet(ATL::_U_STRINGorID title = (LPCTSTR)NULL, UINT uStartPage = 0, HWND hWndParent = NULL)
  3471. : CPropertySheetImpl<CPropertySheet>(title, uStartPage, hWndParent)
  3472. { }
  3473. };
  3474. ///////////////////////////////////////////////////////////////////////////////
  3475. // CPropertyPageWindow - client side for a property page
  3476. class CPropertyPageWindow : public ATL::CWindow
  3477. {
  3478. public:
  3479. // Constructors
  3480. CPropertyPageWindow(HWND hWnd = NULL) : ATL::CWindow(hWnd)
  3481. { }
  3482. CPropertyPageWindow& operator =(HWND hWnd)
  3483. {
  3484. m_hWnd = hWnd;
  3485. return *this;
  3486. }
  3487. // Attributes
  3488. CPropertySheetWindow GetPropertySheet() const
  3489. {
  3490. ATLASSERT(::IsWindow(m_hWnd));
  3491. return CPropertySheetWindow(GetParent());
  3492. }
  3493. // Operations
  3494. BOOL Apply()
  3495. {
  3496. ATLASSERT(::IsWindow(m_hWnd));
  3497. ATLASSERT(GetParent() != NULL);
  3498. return GetPropertySheet().Apply();
  3499. }
  3500. void CancelToClose()
  3501. {
  3502. ATLASSERT(::IsWindow(m_hWnd));
  3503. ATLASSERT(GetParent() != NULL);
  3504. GetPropertySheet().CancelToClose();
  3505. }
  3506. void SetModified(BOOL bChanged = TRUE)
  3507. {
  3508. ATLASSERT(::IsWindow(m_hWnd));
  3509. ATLASSERT(GetParent() != NULL);
  3510. GetPropertySheet().SetModified(m_hWnd, bChanged);
  3511. }
  3512. LRESULT QuerySiblings(WPARAM wParam, LPARAM lParam)
  3513. {
  3514. ATLASSERT(::IsWindow(m_hWnd));
  3515. ATLASSERT(GetParent() != NULL);
  3516. return GetPropertySheet().QuerySiblings(wParam, lParam);
  3517. }
  3518. void RebootSystem()
  3519. {
  3520. ATLASSERT(::IsWindow(m_hWnd));
  3521. ATLASSERT(GetParent() != NULL);
  3522. GetPropertySheet().RebootSystem();
  3523. }
  3524. void RestartWindows()
  3525. {
  3526. ATLASSERT(::IsWindow(m_hWnd));
  3527. ATLASSERT(GetParent() != NULL);
  3528. GetPropertySheet().RestartWindows();
  3529. }
  3530. void SetWizardButtons(DWORD dwFlags)
  3531. {
  3532. ATLASSERT(::IsWindow(m_hWnd));
  3533. ATLASSERT(GetParent() != NULL);
  3534. GetPropertySheet().SetWizardButtons(dwFlags);
  3535. }
  3536. // Implementation - overrides to prevent usage
  3537. HWND Create(LPCTSTR, HWND, ATL::_U_RECT = NULL, LPCTSTR = NULL, DWORD = 0, DWORD = 0, ATL::_U_MENUorID = 0U, LPVOID = NULL)
  3538. {
  3539. ATLASSERT(FALSE);
  3540. return NULL;
  3541. }
  3542. };
  3543. ///////////////////////////////////////////////////////////////////////////////
  3544. // CPropertyPageImpl - implements a property page
  3545. #if defined(_WTL_FORCE_OLD_PAGE_NOTIFY_HANDLERS) && defined(_WTL_NEW_PAGE_NOTIFY_HANDLERS)
  3546. #error _WTL_FORCE_OLD_PAGE_NOTIFY_HANDLERS and _WTL_NEW_PAGE_NOTIFY_HANDLERS cannot be both defined
  3547. #endif
  3548. #if !defined(_WTL_FORCE_OLD_PAGE_NOTIFY_HANDLERS) && !defined(_WTL_NEW_PAGE_NOTIFY_HANDLERS)
  3549. #define _WTL_NEW_PAGE_NOTIFY_HANDLERS
  3550. #endif
  3551. // NOTE: _WTL_NEW_PAGE_NOTIFY_HANDLERS is now defined by default.
  3552. // It enables use of new notification handlers that
  3553. // return direct values without any restrictions.
  3554. // Define _WTL_FORCE_OLD_PAGE_NOTIFY_HANDLERS to use old handlers.
  3555. template <class T, class TBase = CPropertyPageWindow>
  3556. class ATL_NO_VTABLE CPropertyPageImpl : public ATL::CDialogImplBaseT< TBase >
  3557. {
  3558. public:
  3559. PROPSHEETPAGE m_psp;
  3560. operator PROPSHEETPAGE*() { return &m_psp; }
  3561. // Construction
  3562. CPropertyPageImpl(ATL::_U_STRINGorID title = (LPCTSTR)NULL)
  3563. {
  3564. // initialize PROPSHEETPAGE struct
  3565. memset(&m_psp, 0, sizeof(PROPSHEETPAGE));
  3566. m_psp.dwSize = sizeof(PROPSHEETPAGE);
  3567. m_psp.dwFlags = PSP_USECALLBACK;
  3568. m_psp.hInstance = ModuleHelper::GetResourceInstance();
  3569. T* pT = static_cast<T*>(this);
  3570. m_psp.pszTemplate = MAKEINTRESOURCE(pT->IDD);
  3571. m_psp.pfnDlgProc = (DLGPROC)T::StartDialogProc;
  3572. m_psp.pfnCallback = T::PropPageCallback;
  3573. m_psp.lParam = (LPARAM)pT;
  3574. if(title.m_lpstr != NULL)
  3575. SetTitle(title);
  3576. }
  3577. // Callback function and overrideables
  3578. static UINT CALLBACK PropPageCallback(HWND hWnd, UINT uMsg, LPPROPSHEETPAGE ppsp)
  3579. {
  3580. (void)hWnd; // avoid level 4 warning
  3581. ATLASSERT(hWnd == NULL);
  3582. T* pT = (T*)ppsp->lParam;
  3583. UINT uRet = 0;
  3584. switch(uMsg)
  3585. {
  3586. case PSPCB_CREATE:
  3587. {
  3588. ATL::CDialogImplBaseT< TBase >* pPage = (ATL::CDialogImplBaseT< TBase >*)pT;
  3589. ModuleHelper::AddCreateWndData(&pPage->m_thunk.cd, pPage);
  3590. uRet = pT->OnPageCreate() ? 1 : 0;
  3591. }
  3592. break;
  3593. case PSPCB_ADDREF:
  3594. pT->OnPageAddRef();
  3595. break;
  3596. case PSPCB_RELEASE:
  3597. pT->OnPageRelease();
  3598. break;
  3599. default:
  3600. break;
  3601. }
  3602. return uRet;
  3603. }
  3604. bool OnPageCreate()
  3605. {
  3606. return true; // true - allow page to be created, false - prevent creation
  3607. }
  3608. void OnPageAddRef()
  3609. {
  3610. }
  3611. void OnPageRelease()
  3612. {
  3613. }
  3614. // Create method
  3615. HPROPSHEETPAGE Create()
  3616. {
  3617. return ::CreatePropertySheetPage(&m_psp);
  3618. }
  3619. // Attributes
  3620. void SetTitle(ATL::_U_STRINGorID title)
  3621. {
  3622. m_psp.pszTitle = title.m_lpstr;
  3623. m_psp.dwFlags |= PSP_USETITLE;
  3624. }
  3625. void SetHeaderTitle(LPCTSTR lpstrHeaderTitle)
  3626. {
  3627. ATLASSERT(this->m_hWnd == NULL); // can't do this after it's created
  3628. m_psp.dwFlags |= PSP_USEHEADERTITLE;
  3629. m_psp.pszHeaderTitle = lpstrHeaderTitle;
  3630. }
  3631. void SetHeaderSubTitle(LPCTSTR lpstrHeaderSubTitle)
  3632. {
  3633. ATLASSERT(this->m_hWnd == NULL); // can't do this after it's created
  3634. m_psp.dwFlags |= PSP_USEHEADERSUBTITLE;
  3635. m_psp.pszHeaderSubTitle = lpstrHeaderSubTitle;
  3636. }
  3637. // Operations
  3638. void EnableHelp()
  3639. {
  3640. m_psp.dwFlags |= PSP_HASHELP;
  3641. }
  3642. // Message map and handlers
  3643. BEGIN_MSG_MAP(CPropertyPageImpl)
  3644. MESSAGE_HANDLER(WM_NOTIFY, OnNotify)
  3645. END_MSG_MAP()
  3646. LRESULT OnNotify(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& bHandled)
  3647. {
  3648. ATLASSERT(::IsWindow(this->m_hWnd));
  3649. NMHDR* pNMHDR = (NMHDR*)lParam;
  3650. // don't handle messages not from the page/sheet itself
  3651. if((pNMHDR->hwndFrom != this->m_hWnd) && (pNMHDR->hwndFrom != ::GetParent(this->m_hWnd)))
  3652. {
  3653. bHandled = FALSE;
  3654. return 1;
  3655. }
  3656. T* pT = static_cast<T*>(this);
  3657. LRESULT lResult = 0;
  3658. switch(pNMHDR->code)
  3659. {
  3660. #ifdef _WTL_NEW_PAGE_NOTIFY_HANDLERS
  3661. case PSN_SETACTIVE:
  3662. lResult = pT->OnSetActive();
  3663. break;
  3664. case PSN_KILLACTIVE:
  3665. lResult = pT->OnKillActive();
  3666. break;
  3667. case PSN_APPLY:
  3668. lResult = pT->OnApply();
  3669. break;
  3670. case PSN_RESET:
  3671. pT->OnReset();
  3672. break;
  3673. case PSN_QUERYCANCEL:
  3674. lResult = pT->OnQueryCancel();
  3675. break;
  3676. case PSN_WIZNEXT:
  3677. lResult = pT->OnWizardNext();
  3678. break;
  3679. case PSN_WIZBACK:
  3680. lResult = pT->OnWizardBack();
  3681. break;
  3682. case PSN_WIZFINISH:
  3683. lResult = pT->OnWizardFinish();
  3684. break;
  3685. case PSN_HELP:
  3686. pT->OnHelp();
  3687. break;
  3688. case PSN_GETOBJECT:
  3689. if(!pT->OnGetObject((LPNMOBJECTNOTIFY)lParam))
  3690. bHandled = FALSE;
  3691. break;
  3692. case PSN_TRANSLATEACCELERATOR:
  3693. {
  3694. LPPSHNOTIFY lpPSHNotify = (LPPSHNOTIFY)lParam;
  3695. lResult = pT->OnTranslateAccelerator((LPMSG)lpPSHNotify->lParam);
  3696. }
  3697. break;
  3698. case PSN_QUERYINITIALFOCUS:
  3699. {
  3700. LPPSHNOTIFY lpPSHNotify = (LPPSHNOTIFY)lParam;
  3701. lResult = (LRESULT)pT->OnQueryInitialFocus((HWND)lpPSHNotify->lParam);
  3702. }
  3703. break;
  3704. #else // !_WTL_NEW_PAGE_NOTIFY_HANDLERS
  3705. case PSN_SETACTIVE:
  3706. lResult = pT->OnSetActive() ? 0 : -1;
  3707. break;
  3708. case PSN_KILLACTIVE:
  3709. lResult = !pT->OnKillActive();
  3710. break;
  3711. case PSN_APPLY:
  3712. lResult = pT->OnApply() ? PSNRET_NOERROR : PSNRET_INVALID_NOCHANGEPAGE;
  3713. break;
  3714. case PSN_RESET:
  3715. pT->OnReset();
  3716. break;
  3717. case PSN_QUERYCANCEL:
  3718. lResult = !pT->OnQueryCancel();
  3719. break;
  3720. case PSN_WIZNEXT:
  3721. lResult = pT->OnWizardNext();
  3722. break;
  3723. case PSN_WIZBACK:
  3724. lResult = pT->OnWizardBack();
  3725. break;
  3726. case PSN_WIZFINISH:
  3727. lResult = !pT->OnWizardFinish();
  3728. break;
  3729. case PSN_HELP:
  3730. pT->OnHelp();
  3731. break;
  3732. case PSN_GETOBJECT:
  3733. if(!pT->OnGetObject((LPNMOBJECTNOTIFY)lParam))
  3734. bHandled = FALSE;
  3735. break;
  3736. case PSN_TRANSLATEACCELERATOR:
  3737. {
  3738. LPPSHNOTIFY lpPSHNotify = (LPPSHNOTIFY)lParam;
  3739. lResult = pT->OnTranslateAccelerator((LPMSG)lpPSHNotify->lParam) ? PSNRET_MESSAGEHANDLED : PSNRET_NOERROR;
  3740. }
  3741. break;
  3742. case PSN_QUERYINITIALFOCUS:
  3743. {
  3744. LPPSHNOTIFY lpPSHNotify = (LPPSHNOTIFY)lParam;
  3745. lResult = (LRESULT)pT->OnQueryInitialFocus((HWND)lpPSHNotify->lParam);
  3746. }
  3747. break;
  3748. #endif // !_WTL_NEW_PAGE_NOTIFY_HANDLERS
  3749. default:
  3750. bHandled = FALSE; // not handled
  3751. }
  3752. return lResult;
  3753. }
  3754. // Overridables
  3755. #ifdef _WTL_NEW_PAGE_NOTIFY_HANDLERS
  3756. int OnSetActive()
  3757. {
  3758. // 0 = allow activate
  3759. // -1 = go back that was active
  3760. // page ID = jump to page
  3761. return 0;
  3762. }
  3763. BOOL OnKillActive()
  3764. {
  3765. // FALSE = allow deactivate
  3766. // TRUE = prevent deactivation
  3767. return FALSE;
  3768. }
  3769. int OnApply()
  3770. {
  3771. // PSNRET_NOERROR = apply OK
  3772. // PSNRET_INVALID = apply not OK, return to this page
  3773. // PSNRET_INVALID_NOCHANGEPAGE = apply not OK, don't change focus
  3774. return PSNRET_NOERROR;
  3775. }
  3776. void OnReset()
  3777. {
  3778. }
  3779. BOOL OnQueryCancel()
  3780. {
  3781. // FALSE = allow cancel
  3782. // TRUE = prevent cancel
  3783. return FALSE;
  3784. }
  3785. int OnWizardBack()
  3786. {
  3787. // 0 = goto previous page
  3788. // -1 = prevent page change
  3789. // >0 = jump to page by dlg ID
  3790. return 0;
  3791. }
  3792. int OnWizardNext()
  3793. {
  3794. // 0 = goto next page
  3795. // -1 = prevent page change
  3796. // >0 = jump to page by dlg ID
  3797. return 0;
  3798. }
  3799. INT_PTR OnWizardFinish()
  3800. {
  3801. // FALSE = allow finish
  3802. // TRUE = prevent finish
  3803. // HWND = prevent finish and set focus to HWND (CommCtrl 5.80 only)
  3804. return FALSE;
  3805. }
  3806. void OnHelp()
  3807. {
  3808. }
  3809. BOOL OnGetObject(LPNMOBJECTNOTIFY /*lpObjectNotify*/)
  3810. {
  3811. return FALSE; // not processed
  3812. }
  3813. int OnTranslateAccelerator(LPMSG /*lpMsg*/)
  3814. {
  3815. // PSNRET_NOERROR - message not handled
  3816. // PSNRET_MESSAGEHANDLED - message handled
  3817. return PSNRET_NOERROR;
  3818. }
  3819. HWND OnQueryInitialFocus(HWND /*hWndFocus*/)
  3820. {
  3821. // NULL = set focus to default control
  3822. // HWND = set focus to HWND
  3823. return NULL;
  3824. }
  3825. #else // !_WTL_NEW_PAGE_NOTIFY_HANDLERS
  3826. BOOL OnSetActive()
  3827. {
  3828. return TRUE;
  3829. }
  3830. BOOL OnKillActive()
  3831. {
  3832. return TRUE;
  3833. }
  3834. BOOL OnApply()
  3835. {
  3836. return TRUE;
  3837. }
  3838. void OnReset()
  3839. {
  3840. }
  3841. BOOL OnQueryCancel()
  3842. {
  3843. return TRUE; // ok to cancel
  3844. }
  3845. int OnWizardBack()
  3846. {
  3847. // 0 = goto previous page
  3848. // -1 = prevent page change
  3849. // >0 = jump to page by dlg ID
  3850. return 0;
  3851. }
  3852. int OnWizardNext()
  3853. {
  3854. // 0 = goto next page
  3855. // -1 = prevent page change
  3856. // >0 = jump to page by dlg ID
  3857. return 0;
  3858. }
  3859. BOOL OnWizardFinish()
  3860. {
  3861. return TRUE;
  3862. }
  3863. void OnHelp()
  3864. {
  3865. }
  3866. BOOL OnGetObject(LPNMOBJECTNOTIFY /*lpObjectNotify*/)
  3867. {
  3868. return FALSE; // not processed
  3869. }
  3870. BOOL OnTranslateAccelerator(LPMSG /*lpMsg*/)
  3871. {
  3872. return FALSE; // not translated
  3873. }
  3874. HWND OnQueryInitialFocus(HWND /*hWndFocus*/)
  3875. {
  3876. return NULL; // default
  3877. }
  3878. #endif // !_WTL_NEW_PAGE_NOTIFY_HANDLERS
  3879. };
  3880. // for non-customized pages
  3881. template <WORD t_wDlgTemplateID>
  3882. class CPropertyPage : public CPropertyPageImpl<CPropertyPage<t_wDlgTemplateID> >
  3883. {
  3884. public:
  3885. enum { IDD = t_wDlgTemplateID };
  3886. CPropertyPage(ATL::_U_STRINGorID title = (LPCTSTR)NULL) : CPropertyPageImpl<CPropertyPage>(title)
  3887. { }
  3888. DECLARE_EMPTY_MSG_MAP()
  3889. };
  3890. ///////////////////////////////////////////////////////////////////////////////
  3891. // CAxPropertyPageImpl - property page that hosts ActiveX controls
  3892. #ifndef _ATL_NO_HOSTING
  3893. // Note: You must #include <atlhost.h> to use these classes
  3894. template <class T, class TBase = CPropertyPageWindow>
  3895. class ATL_NO_VTABLE CAxPropertyPageImpl : public CPropertyPageImpl< T, TBase >
  3896. {
  3897. public:
  3898. // Data members
  3899. HGLOBAL m_hInitData;
  3900. HGLOBAL m_hDlgRes;
  3901. HGLOBAL m_hDlgResSplit;
  3902. // Constructor/destructor
  3903. CAxPropertyPageImpl(ATL::_U_STRINGorID title = (LPCTSTR)NULL) :
  3904. CPropertyPageImpl< T, TBase >(title),
  3905. m_hInitData(NULL), m_hDlgRes(NULL), m_hDlgResSplit(NULL)
  3906. {
  3907. T* pT = static_cast<T*>(this);
  3908. (void)pT; // avoid level 4 warning
  3909. // initialize ActiveX hosting and modify dialog template
  3910. ATL::AtlAxWinInit();
  3911. HINSTANCE hInstance = ModuleHelper::GetResourceInstance();
  3912. LPCTSTR lpTemplateName = MAKEINTRESOURCE(pT->IDD);
  3913. HRSRC hDlg = ::FindResource(hInstance, lpTemplateName, (LPTSTR)RT_DIALOG);
  3914. if(hDlg != NULL)
  3915. {
  3916. HRSRC hDlgInit = ::FindResource(hInstance, lpTemplateName, (LPTSTR)_ATL_RT_DLGINIT);
  3917. BYTE* pInitData = NULL;
  3918. if(hDlgInit != NULL)
  3919. {
  3920. m_hInitData = ::LoadResource(hInstance, hDlgInit);
  3921. pInitData = (BYTE*)::LockResource(m_hInitData);
  3922. }
  3923. m_hDlgRes = ::LoadResource(hInstance, hDlg);
  3924. DLGTEMPLATE* pDlg = (DLGTEMPLATE*)::LockResource(m_hDlgRes);
  3925. LPCDLGTEMPLATE lpDialogTemplate = ATL::_DialogSplitHelper::SplitDialogTemplate(pDlg, pInitData);
  3926. if(lpDialogTemplate != pDlg)
  3927. m_hDlgResSplit = GlobalHandle(lpDialogTemplate);
  3928. // set up property page to use in-memory dialog template
  3929. if(lpDialogTemplate != NULL)
  3930. {
  3931. this->m_psp.dwFlags |= PSP_DLGINDIRECT;
  3932. this->m_psp.pResource = lpDialogTemplate;
  3933. }
  3934. else
  3935. {
  3936. ATLASSERT(FALSE && _T("CAxPropertyPageImpl - ActiveX initializtion failed!"));
  3937. }
  3938. }
  3939. else
  3940. {
  3941. ATLASSERT(FALSE && _T("CAxPropertyPageImpl - Cannot find dialog template!"));
  3942. }
  3943. }
  3944. ~CAxPropertyPageImpl()
  3945. {
  3946. if(m_hInitData != NULL)
  3947. {
  3948. UnlockResource(m_hInitData);
  3949. FreeResource(m_hInitData);
  3950. }
  3951. if(m_hDlgRes != NULL)
  3952. {
  3953. UnlockResource(m_hDlgRes);
  3954. FreeResource(m_hDlgRes);
  3955. }
  3956. if(m_hDlgResSplit != NULL)
  3957. {
  3958. ::GlobalFree(m_hDlgResSplit);
  3959. }
  3960. }
  3961. // Methods
  3962. // call this one to handle keyboard message for ActiveX controls
  3963. BOOL PreTranslateMessage(LPMSG pMsg)
  3964. {
  3965. if (((pMsg->message < WM_KEYFIRST) || (pMsg->message > WM_KEYLAST)) &&
  3966. ((pMsg->message < WM_MOUSEFIRST) || (pMsg->message > WM_MOUSELAST)))
  3967. return FALSE;
  3968. // find a direct child of the dialog from the window that has focus
  3969. HWND hWndCtl = ::GetFocus();
  3970. if (this->IsChild(hWndCtl) && (::GetParent(hWndCtl) != this->m_hWnd))
  3971. {
  3972. do
  3973. {
  3974. hWndCtl = ::GetParent(hWndCtl);
  3975. }
  3976. while (::GetParent(hWndCtl) != this->m_hWnd);
  3977. }
  3978. // give controls a chance to translate this message
  3979. return (BOOL)::SendMessage(hWndCtl, WM_FORWARDMSG, 0, (LPARAM)pMsg);
  3980. }
  3981. // Overridables
  3982. // new default implementation for ActiveX hosting pages
  3983. #ifdef _WTL_NEW_PAGE_NOTIFY_HANDLERS
  3984. int OnTranslateAccelerator(LPMSG lpMsg)
  3985. {
  3986. T* pT = static_cast<T*>(this);
  3987. return (pT->PreTranslateMessage(lpMsg) != FALSE) ? PSNRET_MESSAGEHANDLED : PSNRET_NOERROR;
  3988. }
  3989. #else // !_WTL_NEW_PAGE_NOTIFY_HANDLERS
  3990. BOOL OnTranslateAccelerator(LPMSG lpMsg)
  3991. {
  3992. T* pT = static_cast<T*>(this);
  3993. return pT->PreTranslateMessage(lpMsg);
  3994. }
  3995. #endif // !_WTL_NEW_PAGE_NOTIFY_HANDLERS
  3996. int GetIDD()
  3997. {
  3998. return( static_cast<T*>(this)->IDD );
  3999. }
  4000. virtual DLGPROC GetDialogProc()
  4001. {
  4002. return DialogProc;
  4003. }
  4004. static INT_PTR CALLBACK DialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  4005. {
  4006. CAxPropertyPageImpl< T, TBase >* pThis = (CAxPropertyPageImpl< T, TBase >*)hWnd;
  4007. if (uMsg == WM_INITDIALOG)
  4008. {
  4009. HRESULT hr;
  4010. if (FAILED(hr = pThis->CreateActiveXControls(pThis->GetIDD())))
  4011. {
  4012. ATLASSERT(FALSE);
  4013. return FALSE;
  4014. }
  4015. }
  4016. return CPropertyPageImpl< T, TBase >::DialogProc(hWnd, uMsg, wParam, lParam);
  4017. }
  4018. // ActiveX controls creation
  4019. virtual HRESULT CreateActiveXControls(UINT nID)
  4020. {
  4021. // Load dialog template and InitData
  4022. HRSRC hDlgInit = ::FindResource(ATL::_AtlBaseModule.GetResourceInstance(), MAKEINTRESOURCE(nID), (LPTSTR)_ATL_RT_DLGINIT);
  4023. BYTE* pInitData = NULL;
  4024. HGLOBAL hData = NULL;
  4025. HRESULT hr = S_OK;
  4026. if (hDlgInit != NULL)
  4027. {
  4028. hData = ::LoadResource(ATL::_AtlBaseModule.GetResourceInstance(), hDlgInit);
  4029. if (hData != NULL)
  4030. pInitData = (BYTE*) ::LockResource(hData);
  4031. }
  4032. HRSRC hDlg = ::FindResource(ATL::_AtlBaseModule.GetResourceInstance(), MAKEINTRESOURCE(nID), (LPTSTR)RT_DIALOG);
  4033. if (hDlg != NULL)
  4034. {
  4035. HGLOBAL hResource = ::LoadResource(ATL::_AtlBaseModule.GetResourceInstance(), hDlg);
  4036. DLGTEMPLATE* pDlg = NULL;
  4037. if (hResource != NULL)
  4038. {
  4039. pDlg = (DLGTEMPLATE*) ::LockResource(hResource);
  4040. if (pDlg != NULL)
  4041. {
  4042. // Get first control on the template
  4043. BOOL bDialogEx = ATL::_DialogSplitHelper::IsDialogEx(pDlg);
  4044. WORD nItems = ATL::_DialogSplitHelper::DlgTemplateItemCount(pDlg);
  4045. // Get first control on the dialog
  4046. DLGITEMTEMPLATE* pItem = ATL::_DialogSplitHelper::FindFirstDlgItem(pDlg);
  4047. HWND hWndPrev = this->GetWindow(GW_CHILD);
  4048. // Create all ActiveX cotnrols in the dialog template and place them in the correct tab order (z-order)
  4049. for (WORD nItem = 0; nItem < nItems; nItem++)
  4050. {
  4051. DWORD wID = bDialogEx ? ((ATL::_DialogSplitHelper::DLGITEMTEMPLATEEX*)pItem)->id : pItem->id;
  4052. if (ATL::_DialogSplitHelper::IsActiveXControl(pItem, bDialogEx))
  4053. {
  4054. BYTE* pData = NULL;
  4055. DWORD dwLen = ATL::_DialogSplitHelper::FindCreateData(wID, pInitData, &pData);
  4056. ATL::CComPtr<IStream> spStream;
  4057. if (dwLen != 0)
  4058. {
  4059. HGLOBAL h = GlobalAlloc(GHND, dwLen);
  4060. if (h != NULL)
  4061. {
  4062. BYTE* pBytes = (BYTE*) GlobalLock(h);
  4063. BYTE* pSource = pData;
  4064. ATL::Checked::memcpy_s(pBytes, dwLen, pSource, dwLen);
  4065. GlobalUnlock(h);
  4066. CreateStreamOnHGlobal(h, TRUE, &spStream);
  4067. }
  4068. else
  4069. {
  4070. hr = E_OUTOFMEMORY;
  4071. break;
  4072. }
  4073. }
  4074. ATL::CComBSTR bstrLicKey;
  4075. hr = ATL::_DialogSplitHelper::ParseInitData(spStream, &bstrLicKey.m_str);
  4076. if (SUCCEEDED(hr))
  4077. {
  4078. ATL::CAxWindow2 wnd;
  4079. // Get control caption.
  4080. LPWSTR pszClassName =
  4081. bDialogEx ?
  4082. (LPWSTR)(((ATL::_DialogSplitHelper::DLGITEMTEMPLATEEX*)pItem) + 1) :
  4083. (LPWSTR)(pItem + 1);
  4084. // Get control rect.
  4085. RECT rect = {};
  4086. rect.left = bDialogEx ? ((ATL::_DialogSplitHelper::DLGITEMTEMPLATEEX*)pItem)->x : pItem->x;
  4087. rect.top = bDialogEx ? ((ATL::_DialogSplitHelper::DLGITEMTEMPLATEEX*)pItem)->y : pItem->y;
  4088. rect.right = rect.left + (bDialogEx ? ((ATL::_DialogSplitHelper::DLGITEMTEMPLATEEX*)pItem)->cx : pItem->cx);
  4089. rect.bottom = rect.top + (bDialogEx ? ((ATL::_DialogSplitHelper::DLGITEMTEMPLATEEX*)pItem)->cy : pItem->cy);
  4090. // Convert from dialog units to screen units
  4091. this->MapDialogRect(&rect);
  4092. // Create AxWindow with a NULL caption.
  4093. wnd.Create(this->m_hWnd,
  4094. &rect,
  4095. NULL,
  4096. (bDialogEx ?
  4097. ((ATL::_DialogSplitHelper::DLGITEMTEMPLATEEX*)pItem)->style :
  4098. pItem->style) | WS_TABSTOP,
  4099. bDialogEx ?
  4100. ((ATL::_DialogSplitHelper::DLGITEMTEMPLATEEX*)pItem)->exStyle :
  4101. 0,
  4102. bDialogEx ?
  4103. ((ATL::_DialogSplitHelper::DLGITEMTEMPLATEEX*)pItem)->id :
  4104. pItem->id,
  4105. NULL);
  4106. if (wnd != NULL)
  4107. {
  4108. // Set the Help ID
  4109. if (bDialogEx && ((ATL::_DialogSplitHelper::DLGITEMTEMPLATEEX*)pItem)->helpID != 0)
  4110. wnd.SetWindowContextHelpId(((ATL::_DialogSplitHelper::DLGITEMTEMPLATEEX*)pItem)->helpID);
  4111. // Try to create the ActiveX control.
  4112. hr = wnd.CreateControlLic(pszClassName, spStream, NULL, bstrLicKey);
  4113. if (FAILED(hr))
  4114. break;
  4115. // Set the correct tab position.
  4116. if (nItem == 0)
  4117. hWndPrev = HWND_TOP;
  4118. wnd.SetWindowPos(hWndPrev, 0,0,0,0,SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
  4119. hWndPrev = wnd;
  4120. }
  4121. else
  4122. {
  4123. hr = ATL::AtlHresultFromLastError();
  4124. }
  4125. }
  4126. }
  4127. else
  4128. {
  4129. if (nItem != 0)
  4130. hWndPrev = ::GetWindow(hWndPrev, GW_HWNDNEXT);
  4131. }
  4132. pItem = ATL::_DialogSplitHelper::FindNextDlgItem(pItem, bDialogEx);
  4133. }
  4134. }
  4135. else
  4136. hr = ATL::AtlHresultFromLastError();
  4137. }
  4138. else
  4139. hr = ATL::AtlHresultFromLastError();
  4140. }
  4141. return hr;
  4142. }
  4143. // Event handling support
  4144. HRESULT AdviseSinkMap(bool bAdvise)
  4145. {
  4146. if(!bAdvise && (this->m_hWnd == NULL))
  4147. {
  4148. // window is gone, controls are already unadvised
  4149. ATLTRACE2(atlTraceUI, 0, _T("CAxPropertyPageImpl::AdviseSinkMap called after the window was destroyed\n"));
  4150. return S_OK;
  4151. }
  4152. HRESULT hRet = E_NOTIMPL;
  4153. __if_exists(T::_GetSinkMapFinder)
  4154. {
  4155. T* pT = static_cast<T*>(this);
  4156. hRet = AtlAdviseSinkMap(pT, bAdvise);
  4157. }
  4158. return hRet;
  4159. }
  4160. // Message map and handlers
  4161. typedef CPropertyPageImpl< T, TBase> _baseClass;
  4162. BEGIN_MSG_MAP(CAxPropertyPageImpl)
  4163. MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
  4164. MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
  4165. CHAIN_MSG_MAP(_baseClass)
  4166. END_MSG_MAP()
  4167. LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
  4168. {
  4169. // initialize controls in dialog with DLGINIT resource section
  4170. this->ExecuteDlgInit(static_cast<T*>(this)->IDD);
  4171. AdviseSinkMap(true);
  4172. bHandled = FALSE;
  4173. return 1;
  4174. }
  4175. LRESULT OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
  4176. {
  4177. AdviseSinkMap(false);
  4178. bHandled = FALSE;
  4179. return 1;
  4180. }
  4181. };
  4182. // for non-customized pages
  4183. template <WORD t_wDlgTemplateID>
  4184. class CAxPropertyPage : public CAxPropertyPageImpl<CAxPropertyPage<t_wDlgTemplateID> >
  4185. {
  4186. public:
  4187. enum { IDD = t_wDlgTemplateID };
  4188. CAxPropertyPage(ATL::_U_STRINGorID title = (LPCTSTR)NULL) : CAxPropertyPageImpl<CAxPropertyPage>(title)
  4189. { }
  4190. BEGIN_MSG_MAP(CAxPropertyPage)
  4191. CHAIN_MSG_MAP(CAxPropertyPageImpl<CAxPropertyPage<t_wDlgTemplateID> >)
  4192. END_MSG_MAP()
  4193. };
  4194. #endif // _ATL_NO_HOSTING
  4195. ///////////////////////////////////////////////////////////////////////////////
  4196. // Wizard97 Support
  4197. // Sample wizard dialog resources:
  4198. //
  4199. // IDD_WIZ97_INTERIOR_BLANK DIALOG 0, 0, 317, 143
  4200. // STYLE DS_SETFONT | WS_CHILD | WS_DISABLED | WS_CAPTION
  4201. // CAPTION "Wizard97 Property Page - Interior"
  4202. // FONT 8, "MS Shell Dlg"
  4203. // BEGIN
  4204. // END
  4205. //
  4206. // IDD_WIZ97_EXTERIOR_BLANK DIALOGEX 0, 0, 317, 193
  4207. // STYLE DS_SETFONT | DS_FIXEDSYS | WS_CHILD | WS_DISABLED | WS_CAPTION
  4208. // CAPTION "Wizard97 Property Page - Welcome/Complete"
  4209. // FONT 8, "MS Shell Dlg", 0, 0, 0x0
  4210. // BEGIN
  4211. // LTEXT "Welcome to the X Wizard",IDC_WIZ97_EXTERIOR_TITLE,115,8,
  4212. // 195,24
  4213. // LTEXT "Wizard Explanation\r\n(The height of the static text should be in multiples of 8 dlus)",
  4214. // IDC_STATIC,115,40,195,16
  4215. // LTEXT "h",IDC_WIZ97_BULLET1,118,64,8,8
  4216. // LTEXT "List Item 1 (the h is turned into a bullet)",IDC_STATIC,
  4217. // 127,63,122,8
  4218. // LTEXT "h",IDC_WIZ97_BULLET2,118,79,8,8
  4219. // LTEXT "List Item 2. Keep 7 dlus between paragraphs",IDC_STATIC,
  4220. // 127,78,33,8
  4221. // CONTROL "&Do not show this Welcome page again",
  4222. // IDC_WIZ97_WELCOME_NOTAGAIN,"Button",BS_AUTOCHECKBOX |
  4223. // WS_TABSTOP,115,169,138,10
  4224. // END
  4225. //
  4226. // GUIDELINES DESIGNINFO
  4227. // BEGIN
  4228. // IDD_WIZ97_INTERIOR_BLANK, DIALOG
  4229. // BEGIN
  4230. // LEFTMARGIN, 7
  4231. // RIGHTMARGIN, 310
  4232. // VERTGUIDE, 21
  4233. // VERTGUIDE, 31
  4234. // VERTGUIDE, 286
  4235. // VERTGUIDE, 296
  4236. // TOPMARGIN, 7
  4237. // BOTTOMMARGIN, 136
  4238. // HORZGUIDE, 8
  4239. // END
  4240. //
  4241. // IDD_WIZ97_EXTERIOR_BLANK, DIALOG
  4242. // BEGIN
  4243. // RIGHTMARGIN, 310
  4244. // VERTGUIDE, 115
  4245. // VERTGUIDE, 118
  4246. // VERTGUIDE, 127
  4247. // TOPMARGIN, 7
  4248. // BOTTOMMARGIN, 186
  4249. // HORZGUIDE, 8
  4250. // HORZGUIDE, 32
  4251. // HORZGUIDE, 40
  4252. // HORZGUIDE, 169
  4253. // END
  4254. // END
  4255. ///////////////////////////////////////////////////////////////////////////////
  4256. // CWizard97SheetWindow - client side for a Wizard 97 style wizard sheet
  4257. class CWizard97SheetWindow : public CPropertySheetWindow
  4258. {
  4259. public:
  4260. // Constructors
  4261. CWizard97SheetWindow(HWND hWnd = NULL) : CPropertySheetWindow(hWnd)
  4262. { }
  4263. CWizard97SheetWindow& operator =(HWND hWnd)
  4264. {
  4265. m_hWnd = hWnd;
  4266. return *this;
  4267. }
  4268. // Operations
  4269. HFONT GetExteriorPageTitleFont(void)
  4270. {
  4271. ATLASSERT(::IsWindow(m_hWnd));
  4272. return (HFONT)::SendMessage(m_hWnd, GetMessage_GetExteriorPageTitleFont(), 0, 0L);
  4273. }
  4274. HFONT GetBulletFont(void)
  4275. {
  4276. ATLASSERT(::IsWindow(m_hWnd));
  4277. return (HFONT)::SendMessage(m_hWnd, GetMessage_GetBulletFont(), 0, 0L);
  4278. }
  4279. // Helpers
  4280. static UINT GetMessage_GetExteriorPageTitleFont()
  4281. {
  4282. static UINT uGetExteriorPageTitleFont = 0;
  4283. if(uGetExteriorPageTitleFont == 0)
  4284. {
  4285. CStaticDataInitCriticalSectionLock lock;
  4286. if(FAILED(lock.Lock()))
  4287. {
  4288. ATLTRACE2(atlTraceUI, 0, _T("ERROR : Unable to lock critical section in CWizard97SheetWindow::GetMessage_GetExteriorPageTitleFont().\n"));
  4289. ATLASSERT(FALSE);
  4290. return 0;
  4291. }
  4292. if(uGetExteriorPageTitleFont == 0)
  4293. uGetExteriorPageTitleFont = ::RegisterWindowMessage(_T("GetExteriorPageTitleFont_531AF056-B8BE-4c4c-B786-AC608DF0DF12"));
  4294. lock.Unlock();
  4295. }
  4296. ATLASSERT(uGetExteriorPageTitleFont != 0);
  4297. return uGetExteriorPageTitleFont;
  4298. }
  4299. static UINT GetMessage_GetBulletFont()
  4300. {
  4301. static UINT uGetBulletFont = 0;
  4302. if(uGetBulletFont == 0)
  4303. {
  4304. CStaticDataInitCriticalSectionLock lock;
  4305. if(FAILED(lock.Lock()))
  4306. {
  4307. ATLTRACE2(atlTraceUI, 0, _T("ERROR : Unable to lock critical section in CWizard97SheetWindow::GetMessage_GetBulletFont().\n"));
  4308. ATLASSERT(FALSE);
  4309. return 0;
  4310. }
  4311. if(uGetBulletFont == 0)
  4312. uGetBulletFont = ::RegisterWindowMessage(_T("GetBulletFont_AD347D08-8F65-45ef-982E-6352E8218AD5"));
  4313. lock.Unlock();
  4314. }
  4315. ATLASSERT(uGetBulletFont != 0);
  4316. return uGetBulletFont;
  4317. }
  4318. // Implementation - override to prevent usage
  4319. HWND Create(LPCTSTR, HWND, ATL::_U_RECT = NULL, LPCTSTR = NULL, DWORD = 0, DWORD = 0, ATL::_U_MENUorID = 0U, LPVOID = NULL)
  4320. {
  4321. ATLASSERT(FALSE);
  4322. return NULL;
  4323. }
  4324. };
  4325. ///////////////////////////////////////////////////////////////////////////////
  4326. // CWizard97SheetImpl - implements a Wizard 97 style wizard sheet
  4327. template <class T, class TBase = CWizard97SheetWindow>
  4328. class ATL_NO_VTABLE CWizard97SheetImpl : public CPropertySheetImpl< T, TBase >
  4329. {
  4330. protected:
  4331. // Typedefs
  4332. typedef CWizard97SheetImpl< T, TBase > thisClass;
  4333. typedef CPropertySheetImpl< T, TBase > baseClass;
  4334. // Member variables
  4335. CFont m_fontExteriorPageTitle; // Welcome and Completion page title font
  4336. CFont m_fontBullet; // Bullet font (used on static text 'h' to produce a small bullet)
  4337. bool m_bReceivedFirstSizeMessage;
  4338. public:
  4339. CWizard97SheetImpl(ATL::_U_STRINGorID title, ATL::_U_STRINGorID headerBitmap, ATL::_U_STRINGorID watermarkBitmap, UINT uStartPage = 0, HWND hWndParent = NULL) :
  4340. baseClass(title, uStartPage, hWndParent),
  4341. m_bReceivedFirstSizeMessage(false)
  4342. {
  4343. this->m_psh.dwFlags &= ~(PSH_NOCONTEXTHELP);
  4344. this->m_psh.dwFlags &= ~(PSH_WIZARD | PSH_WIZARD_LITE);
  4345. this->m_psh.dwFlags |= (PSH_HASHELP | PSH_WIZARDCONTEXTHELP);
  4346. this->m_psh.dwFlags |= PSH_WIZARD97;
  4347. baseClass::SetHeader(headerBitmap.m_lpstr);
  4348. baseClass::SetWatermark(watermarkBitmap.m_lpstr);
  4349. }
  4350. // Overrides from base class
  4351. void OnSheetInitialized()
  4352. {
  4353. T* pT = static_cast<T*>(this);
  4354. pT->_InitializeFonts();
  4355. // We'd like to center the wizard here, but its too early.
  4356. // Instead, we'll do CenterWindow upon our first WM_SIZE message
  4357. }
  4358. // Initialization
  4359. void _InitializeFonts()
  4360. {
  4361. // Setup the Title and Bullet Font
  4362. // (Property pages can send the "get external page title font" and "get bullet font" messages)
  4363. // The derived class needs to do the actual SetFont for the dialog items)
  4364. CFontHandle fontThisDialog = this->GetFont();
  4365. CClientDC dcScreen(NULL);
  4366. LOGFONT titleLogFont = {};
  4367. LOGFONT bulletLogFont = {};
  4368. fontThisDialog.GetLogFont(&titleLogFont);
  4369. fontThisDialog.GetLogFont(&bulletLogFont);
  4370. // The Wizard 97 Spec recommends to do the Title Font
  4371. // as Verdana Bold, 12pt.
  4372. titleLogFont.lfCharSet = DEFAULT_CHARSET;
  4373. titleLogFont.lfWeight = FW_BOLD;
  4374. ATL::Checked::tcscpy_s(titleLogFont.lfFaceName, _countof(titleLogFont.lfFaceName), _T("Verdana Bold"));
  4375. INT titleFontPointSize = 12;
  4376. titleLogFont.lfHeight = -::MulDiv(titleFontPointSize, dcScreen.GetDeviceCaps(LOGPIXELSY), 72);
  4377. m_fontExteriorPageTitle.CreateFontIndirect(&titleLogFont);
  4378. // The Wizard 97 Spec recommends to do Bullets by having
  4379. // static text of "h" in the Marlett font.
  4380. bulletLogFont.lfCharSet = DEFAULT_CHARSET;
  4381. bulletLogFont.lfWeight = FW_NORMAL;
  4382. ATL::Checked::tcscpy_s(bulletLogFont.lfFaceName, _countof(bulletLogFont.lfFaceName), _T("Marlett"));
  4383. INT bulletFontSize = 8;
  4384. bulletLogFont.lfHeight = -::MulDiv(bulletFontSize, dcScreen.GetDeviceCaps(LOGPIXELSY), 72);
  4385. m_fontBullet.CreateFontIndirect(&bulletLogFont);
  4386. }
  4387. // Message Handling
  4388. BEGIN_MSG_MAP(thisClass)
  4389. MESSAGE_HANDLER(CWizard97SheetWindow::GetMessage_GetExteriorPageTitleFont(), OnGetExteriorPageTitleFont)
  4390. MESSAGE_HANDLER(CWizard97SheetWindow::GetMessage_GetBulletFont(), OnGetBulletFont)
  4391. MESSAGE_HANDLER(WM_SIZE, OnSize)
  4392. CHAIN_MSG_MAP(baseClass)
  4393. END_MSG_MAP()
  4394. LRESULT OnGetExteriorPageTitleFont(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
  4395. {
  4396. return (LRESULT)(HFONT)m_fontExteriorPageTitle;
  4397. }
  4398. LRESULT OnGetBulletFont(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
  4399. {
  4400. return (LRESULT)(HFONT)m_fontBullet;
  4401. }
  4402. LRESULT OnSize(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
  4403. {
  4404. if(!m_bReceivedFirstSizeMessage)
  4405. {
  4406. m_bReceivedFirstSizeMessage = true;
  4407. this->CenterWindow();
  4408. }
  4409. bHandled = FALSE;
  4410. return 0;
  4411. }
  4412. };
  4413. // for non-customized sheets
  4414. class CWizard97Sheet : public CWizard97SheetImpl<CWizard97Sheet>
  4415. {
  4416. protected:
  4417. // Typedefs
  4418. typedef CWizard97Sheet thisClass;
  4419. typedef CWizard97SheetImpl<CWizard97Sheet> baseClass;
  4420. public:
  4421. CWizard97Sheet(ATL::_U_STRINGorID title, ATL::_U_STRINGorID headerBitmap, ATL::_U_STRINGorID watermarkBitmap, UINT uStartPage = 0, HWND hWndParent = NULL) :
  4422. baseClass(title, headerBitmap, watermarkBitmap, uStartPage, hWndParent)
  4423. { }
  4424. BEGIN_MSG_MAP(thisClass)
  4425. CHAIN_MSG_MAP(baseClass)
  4426. END_MSG_MAP()
  4427. };
  4428. ///////////////////////////////////////////////////////////////////////////////
  4429. // CWizard97PageWindow - client side for a Wizard 97 style wizard page
  4430. #define WIZARD97_EXTERIOR_CXDLG 317
  4431. #define WIZARD97_EXTERIOR_CYDLG 193
  4432. #define WIZARD97_INTERIOR_CXDLG 317
  4433. #define WIZARD97_INTERIOR_CYDLG 143
  4434. class CWizard97PageWindow : public CPropertyPageWindow
  4435. {
  4436. public:
  4437. // Constructors
  4438. CWizard97PageWindow(HWND hWnd = NULL) : CPropertyPageWindow(hWnd)
  4439. { }
  4440. CWizard97PageWindow& operator =(HWND hWnd)
  4441. {
  4442. m_hWnd = hWnd;
  4443. return *this;
  4444. }
  4445. // Attributes
  4446. CWizard97SheetWindow GetPropertySheet() const
  4447. {
  4448. ATLASSERT(::IsWindow(m_hWnd));
  4449. return CWizard97SheetWindow(GetParent());
  4450. }
  4451. // Operations
  4452. HFONT GetExteriorPageTitleFont(void)
  4453. {
  4454. ATLASSERT(::IsWindow(m_hWnd));
  4455. return GetPropertySheet().GetExteriorPageTitleFont();
  4456. }
  4457. HFONT GetBulletFont(void)
  4458. {
  4459. ATLASSERT(::IsWindow(m_hWnd));
  4460. return GetPropertySheet().GetBulletFont();
  4461. }
  4462. // Implementation - overrides to prevent usage
  4463. HWND Create(LPCTSTR, HWND, ATL::_U_RECT = NULL, LPCTSTR = NULL, DWORD = 0, DWORD = 0, ATL::_U_MENUorID = 0U, LPVOID = NULL)
  4464. {
  4465. ATLASSERT(FALSE);
  4466. return NULL;
  4467. }
  4468. };
  4469. ///////////////////////////////////////////////////////////////////////////////
  4470. // CWizard97PageImpl - implements a Wizard 97 style wizard page
  4471. template <class T, class TBase = CWizard97PageWindow>
  4472. class ATL_NO_VTABLE CWizard97PageImpl : public CPropertyPageImpl< T, TBase >
  4473. {
  4474. protected:
  4475. // Typedefs
  4476. typedef CWizard97PageImpl< T, TBase > thisClass;
  4477. typedef CPropertyPageImpl< T, TBase > baseClass;
  4478. public:
  4479. CWizard97PageImpl(ATL::_U_STRINGorID title = (LPCTSTR)NULL) : baseClass(title)
  4480. { }
  4481. // Message Handling
  4482. BEGIN_MSG_MAP(thisClass)
  4483. CHAIN_MSG_MAP(baseClass)
  4484. END_MSG_MAP()
  4485. };
  4486. ///////////////////////////////////////////////////////////////////////////////
  4487. // CWizard97ExteriorPageImpl - implements a Wizard 97 style exterior wizard page
  4488. template <class T, class TBase = CWizard97PageWindow>
  4489. class ATL_NO_VTABLE CWizard97ExteriorPageImpl : public CPropertyPageImpl< T, TBase >
  4490. {
  4491. protected:
  4492. // Typedefs
  4493. typedef CWizard97ExteriorPageImpl< T, TBase > thisClass;
  4494. typedef CPropertyPageImpl< T, TBase > baseClass;
  4495. public:
  4496. // Constructors
  4497. CWizard97ExteriorPageImpl(ATL::_U_STRINGorID title = (LPCTSTR)NULL) : baseClass(title)
  4498. {
  4499. this->m_psp.dwFlags |= PSP_HASHELP;
  4500. this->m_psp.dwFlags |= PSP_HIDEHEADER;
  4501. }
  4502. // Message Handling
  4503. BEGIN_MSG_MAP(thisClass)
  4504. CHAIN_MSG_MAP(baseClass)
  4505. END_MSG_MAP()
  4506. };
  4507. ///////////////////////////////////////////////////////////////////////////////
  4508. // CWizard97InteriorPageImpl - implements a Wizard 97 style interior wizard page
  4509. template <class T, class TBase = CWizard97PageWindow>
  4510. class ATL_NO_VTABLE CWizard97InteriorPageImpl : public CPropertyPageImpl< T, TBase >
  4511. {
  4512. protected:
  4513. // Typedefs
  4514. typedef CWizard97InteriorPageImpl< T, TBase > thisClass;
  4515. typedef CPropertyPageImpl< T, TBase > baseClass;
  4516. public:
  4517. // Constructors
  4518. CWizard97InteriorPageImpl(ATL::_U_STRINGorID title = (LPCTSTR)NULL) : baseClass(title)
  4519. {
  4520. this->m_psp.dwFlags |= PSP_HASHELP;
  4521. this->m_psp.dwFlags &= ~PSP_HIDEHEADER;
  4522. this->m_psp.dwFlags |= PSP_USEHEADERTITLE | PSP_USEHEADERSUBTITLE;
  4523. // Be sure to have the derived class define this in the constructor.
  4524. // We'll default it to something obvious in case its forgotten.
  4525. baseClass::SetHeaderTitle(_T("Call SetHeaderTitle in Derived Class"));
  4526. baseClass::SetHeaderSubTitle(_T("Call SetHeaderSubTitle in the constructor of the Derived Class."));
  4527. }
  4528. // Message Handling
  4529. BEGIN_MSG_MAP(thisClass)
  4530. CHAIN_MSG_MAP(baseClass)
  4531. END_MSG_MAP()
  4532. };
  4533. ///////////////////////////////////////////////////////////////////////////////
  4534. // Aero Wizard support
  4535. #if (_WIN32_WINNT >= 0x0600)
  4536. ///////////////////////////////////////////////////////////////////////////////
  4537. // CAeroWizardFrameWindow - client side for an Aero Wizard frame window
  4538. class CAeroWizardFrameWindow : public CPropertySheetWindow
  4539. {
  4540. public:
  4541. // Constructors
  4542. CAeroWizardFrameWindow(HWND hWnd = NULL) : CPropertySheetWindow(hWnd)
  4543. { }
  4544. CAeroWizardFrameWindow& operator =(HWND hWnd)
  4545. {
  4546. m_hWnd = hWnd;
  4547. return *this;
  4548. }
  4549. // Operations - new, Aero Wizard only
  4550. void SetNextText(LPCWSTR lpszText)
  4551. {
  4552. ATLASSERT(::IsWindow(m_hWnd));
  4553. ::SendMessage(m_hWnd, PSM_SETNEXTTEXT, 0, (LPARAM)lpszText);
  4554. }
  4555. void ShowWizardButtons(DWORD dwButtons, DWORD dwStates)
  4556. {
  4557. ATLASSERT(::IsWindow(m_hWnd));
  4558. ::PostMessage(m_hWnd, PSM_SHOWWIZBUTTONS, (WPARAM)dwStates, (LPARAM)dwButtons);
  4559. }
  4560. void EnableWizardButtons(DWORD dwButtons, DWORD dwStates)
  4561. {
  4562. ATLASSERT(::IsWindow(m_hWnd));
  4563. ::PostMessage(m_hWnd, PSM_ENABLEWIZBUTTONS, (WPARAM)dwStates, (LPARAM)dwButtons);
  4564. }
  4565. void SetButtonText(DWORD dwButton, LPCWSTR lpszText)
  4566. {
  4567. ATLASSERT(::IsWindow(m_hWnd));
  4568. ::SendMessage(m_hWnd, PSM_SETBUTTONTEXT, (WPARAM)dwButton, (LPARAM)lpszText);
  4569. }
  4570. };
  4571. ///////////////////////////////////////////////////////////////////////////////
  4572. // CAeroWizardFrameImpl - implements an Aero Wizard frame
  4573. template <class T, class TBase = CAeroWizardFrameWindow>
  4574. class ATL_NO_VTABLE CAeroWizardFrameImpl : public CPropertySheetImpl<T, TBase >
  4575. {
  4576. public:
  4577. // Constructor
  4578. CAeroWizardFrameImpl(ATL::_U_STRINGorID title = (LPCTSTR)NULL, UINT uStartPage = 0, HWND hWndParent = NULL) :
  4579. CPropertySheetImpl<T, TBase >(title, uStartPage, hWndParent)
  4580. {
  4581. this->m_psh.dwFlags |= PSH_WIZARD | PSH_AEROWIZARD;
  4582. }
  4583. // Operations
  4584. void EnableResizing()
  4585. {
  4586. ATLASSERT(this->m_hWnd == NULL); // can't do this after it's created
  4587. this->m_psh.dwFlags |= PSH_RESIZABLE;
  4588. }
  4589. void UseHeaderBitmap()
  4590. {
  4591. ATLASSERT(this->m_hWnd == NULL); // can't do this after it's created
  4592. this->m_psh.dwFlags |= PSH_HEADERBITMAP;
  4593. }
  4594. void SetNoMargin()
  4595. {
  4596. ATLASSERT(this->m_hWnd == NULL); // can't do this after it's created
  4597. this->m_psh.dwFlags |= PSH_NOMARGIN;
  4598. }
  4599. // Override to prevent use
  4600. HWND Create(HWND /*hWndParent*/ = NULL)
  4601. {
  4602. ATLASSERT(FALSE); // not supported for Aero Wizard
  4603. return NULL;
  4604. }
  4605. };
  4606. ///////////////////////////////////////////////////////////////////////////////
  4607. // CAeroWizardFrame - for non-customized frames
  4608. class CAeroWizardFrame : public CAeroWizardFrameImpl<CAeroWizardFrame>
  4609. {
  4610. public:
  4611. CAeroWizardFrame(ATL::_U_STRINGorID title = (LPCTSTR)NULL, UINT uStartPage = 0, HWND hWndParent = NULL)
  4612. : CAeroWizardFrameImpl<CAeroWizardFrame>(title, uStartPage, hWndParent)
  4613. { }
  4614. BEGIN_MSG_MAP(CAeroWizardFrame)
  4615. MESSAGE_HANDLER(WM_COMMAND, CAeroWizardFrameImpl<CAeroWizardFrame>::OnCommand)
  4616. END_MSG_MAP()
  4617. };
  4618. ///////////////////////////////////////////////////////////////////////////////
  4619. // CAeroWizardPageWindow - client side for an Aero Wizard page
  4620. class CAeroWizardPageWindow : public CPropertyPageWindow
  4621. {
  4622. public:
  4623. // Constructors
  4624. CAeroWizardPageWindow(HWND hWnd = NULL) : CPropertyPageWindow(hWnd)
  4625. { }
  4626. CAeroWizardPageWindow& operator =(HWND hWnd)
  4627. {
  4628. m_hWnd = hWnd;
  4629. return *this;
  4630. }
  4631. // Attributes
  4632. CAeroWizardFrameWindow GetAeroWizardFrame() const
  4633. {
  4634. ATLASSERT(::IsWindow(m_hWnd));
  4635. // This is not really top-level frame window, but it processes all frame messages
  4636. return CAeroWizardFrameWindow(GetParent());
  4637. }
  4638. // Operations - new, Aero Wizard only
  4639. void SetNextText(LPCWSTR lpszText)
  4640. {
  4641. ATLASSERT(::IsWindow(m_hWnd));
  4642. ATLASSERT(GetParent() != NULL);
  4643. GetAeroWizardFrame().SetNextText(lpszText);
  4644. }
  4645. void ShowWizardButtons(DWORD dwButtons, DWORD dwStates)
  4646. {
  4647. ATLASSERT(::IsWindow(m_hWnd));
  4648. ATLASSERT(GetParent() != NULL);
  4649. GetAeroWizardFrame().ShowWizardButtons(dwButtons, dwStates);
  4650. }
  4651. void EnableWizardButtons(DWORD dwButtons, DWORD dwStates)
  4652. {
  4653. ATLASSERT(::IsWindow(m_hWnd));
  4654. ATLASSERT(GetParent() != NULL);
  4655. GetAeroWizardFrame().EnableWizardButtons(dwButtons, dwStates);
  4656. }
  4657. void SetButtonText(DWORD dwButton, LPCWSTR lpszText)
  4658. {
  4659. ATLASSERT(::IsWindow(m_hWnd));
  4660. ATLASSERT(GetParent() != NULL);
  4661. GetAeroWizardFrame().SetButtonText(dwButton, lpszText);
  4662. }
  4663. };
  4664. ///////////////////////////////////////////////////////////////////////////////
  4665. // CAeroWizardPageImpl - implements an Aero Wizard page
  4666. template <class T, class TBase = CAeroWizardPageWindow>
  4667. class ATL_NO_VTABLE CAeroWizardPageImpl : public CPropertyPageImpl<T, TBase >
  4668. {
  4669. public:
  4670. CAeroWizardPageImpl(ATL::_U_STRINGorID title = (LPCTSTR)NULL) : CPropertyPageImpl<T, TBase >(title)
  4671. { }
  4672. };
  4673. ///////////////////////////////////////////////////////////////////////////////
  4674. // CAeroWizardPage - for non-customized pages
  4675. template <WORD t_wDlgTemplateID>
  4676. class CAeroWizardPage : public CAeroWizardPageImpl<CAeroWizardPage<t_wDlgTemplateID> >
  4677. {
  4678. public:
  4679. enum { IDD = t_wDlgTemplateID };
  4680. CAeroWizardPage(ATL::_U_STRINGorID title = (LPCTSTR)NULL) : CAeroWizardPageImpl<CAeroWizardPage>(title)
  4681. { }
  4682. DECLARE_EMPTY_MSG_MAP()
  4683. };
  4684. #ifndef _ATL_NO_HOSTING
  4685. // Note: You must #include <atlhost.h> to use these classes
  4686. ///////////////////////////////////////////////////////////////////////////////
  4687. // CAeroWizardAxPageImpl - Aero Wizard page that hosts ActiveX controls
  4688. template <class T, class TBase = CAeroWizardPageWindow>
  4689. class ATL_NO_VTABLE CAeroWizardAxPageImpl : public CAxPropertyPageImpl< T, TBase >
  4690. {
  4691. public:
  4692. CAeroWizardAxPageImpl(ATL::_U_STRINGorID title = (LPCTSTR)NULL) : CAxPropertyPageImpl< T, TBase >(title)
  4693. { }
  4694. };
  4695. ///////////////////////////////////////////////////////////////////////////////
  4696. // CAeroWizardAxPage - for non-customized pages
  4697. template <WORD t_wDlgTemplateID>
  4698. class CAeroWizardAxPage : public CAeroWizardAxPageImpl<CAeroWizardAxPage<t_wDlgTemplateID> >
  4699. {
  4700. public:
  4701. enum { IDD = t_wDlgTemplateID };
  4702. CAeroWizardAxPage(ATL::_U_STRINGorID title = (LPCTSTR)NULL) : CAeroWizardAxPageImpl<CAeroWizardAxPage>(title)
  4703. { }
  4704. BEGIN_MSG_MAP(CAeroWizardAxPage)
  4705. CHAIN_MSG_MAP(CAeroWizardAxPageImpl<CAeroWizardAxPage<t_wDlgTemplateID> >)
  4706. END_MSG_MAP()
  4707. };
  4708. #endif // _ATL_NO_HOSTING
  4709. #endif // (_WIN32_WINNT >= 0x0600)
  4710. ///////////////////////////////////////////////////////////////////////////////
  4711. // TaskDialog support
  4712. #if (_WIN32_WINNT >= 0x0600) || defined(_WTL_TASKDIALOG)
  4713. ///////////////////////////////////////////////////////////////////////////////
  4714. // AtlTaskDialog - support for TaskDialog() function
  4715. inline int AtlTaskDialog(HWND hWndParent,
  4716. ATL::_U_STRINGorID WindowTitle, ATL::_U_STRINGorID MainInstructionText, ATL::_U_STRINGorID ContentText,
  4717. TASKDIALOG_COMMON_BUTTON_FLAGS dwCommonButtons = 0U, ATL::_U_STRINGorID Icon = (LPCTSTR)NULL)
  4718. {
  4719. int nRet = -1;
  4720. #ifdef _WTL_TASKDIALOG_DIRECT
  4721. USES_CONVERSION;
  4722. HRESULT hRet = ::TaskDialog(hWndParent, ModuleHelper::GetResourceInstance(),
  4723. IS_INTRESOURCE(WindowTitle.m_lpstr) ? (LPCWSTR) WindowTitle.m_lpstr : T2CW(WindowTitle.m_lpstr),
  4724. IS_INTRESOURCE(MainInstructionText.m_lpstr) ? (LPCWSTR) MainInstructionText.m_lpstr : T2CW(MainInstructionText.m_lpstr),
  4725. IS_INTRESOURCE(ContentText.m_lpstr) ? (LPCWSTR) ContentText.m_lpstr : T2CW(ContentText.m_lpstr),
  4726. dwCommonButtons,
  4727. IS_INTRESOURCE(Icon.m_lpstr) ? (LPCWSTR) Icon.m_lpstr : T2CW(Icon.m_lpstr),
  4728. &nRet);
  4729. ATLVERIFY(SUCCEEDED(hRet));
  4730. #else
  4731. // This allows apps to run on older versions of Windows
  4732. typedef HRESULT (STDAPICALLTYPE *PFN_TaskDialog)(HWND hwndParent, HINSTANCE hInstance, PCWSTR pszWindowTitle, PCWSTR pszMainInstruction, PCWSTR pszContent, TASKDIALOG_COMMON_BUTTON_FLAGS dwCommonButtons, PCWSTR pszIcon, int* pnButton);
  4733. HMODULE m_hCommCtrlDLL = ::LoadLibrary(_T("comctl32.dll"));
  4734. if(m_hCommCtrlDLL != NULL)
  4735. {
  4736. PFN_TaskDialog pfnTaskDialog = (PFN_TaskDialog)::GetProcAddress(m_hCommCtrlDLL, "TaskDialog");
  4737. if(pfnTaskDialog != NULL)
  4738. {
  4739. USES_CONVERSION;
  4740. HRESULT hRet = pfnTaskDialog(hWndParent, ModuleHelper::GetResourceInstance(),
  4741. IS_INTRESOURCE(WindowTitle.m_lpstr) ? (LPCWSTR) WindowTitle.m_lpstr : T2CW(WindowTitle.m_lpstr),
  4742. IS_INTRESOURCE(MainInstructionText.m_lpstr) ? (LPCWSTR) MainInstructionText.m_lpstr : T2CW(MainInstructionText.m_lpstr),
  4743. IS_INTRESOURCE(ContentText.m_lpstr) ? (LPCWSTR) ContentText.m_lpstr : T2CW(ContentText.m_lpstr),
  4744. dwCommonButtons,
  4745. IS_INTRESOURCE(Icon.m_lpstr) ? (LPCWSTR) Icon.m_lpstr : T2CW(Icon.m_lpstr),
  4746. &nRet);
  4747. ATLVERIFY(SUCCEEDED(hRet));
  4748. }
  4749. ::FreeLibrary(m_hCommCtrlDLL);
  4750. }
  4751. #endif
  4752. return nRet;
  4753. }
  4754. ///////////////////////////////////////////////////////////////////////////////
  4755. // CTaskDialogConfig - TASKDIALOGCONFIG wrapper
  4756. class CTaskDialogConfig : public TASKDIALOGCONFIG
  4757. {
  4758. public:
  4759. // Constructor
  4760. CTaskDialogConfig()
  4761. {
  4762. Init();
  4763. }
  4764. void Init()
  4765. {
  4766. memset(this, 0, sizeof(TASKDIALOGCONFIG)); // initialize structure to 0/NULL
  4767. this->cbSize = sizeof(TASKDIALOGCONFIG);
  4768. this->hInstance = ModuleHelper::GetResourceInstance();
  4769. }
  4770. // Operations - setting values
  4771. // common buttons
  4772. void SetCommonButtons(TASKDIALOG_COMMON_BUTTON_FLAGS dwCommonButtonsArg)
  4773. {
  4774. this->dwCommonButtons = dwCommonButtonsArg;
  4775. }
  4776. // window title text
  4777. void SetWindowTitle(UINT nID)
  4778. {
  4779. this->pszWindowTitle = MAKEINTRESOURCEW(nID);
  4780. }
  4781. void SetWindowTitle(LPCWSTR lpstrWindowTitle)
  4782. {
  4783. this->pszWindowTitle = lpstrWindowTitle;
  4784. }
  4785. // main icon
  4786. void SetMainIcon(HICON hIcon)
  4787. {
  4788. this->dwFlags |= TDF_USE_HICON_MAIN;
  4789. this->hMainIcon = hIcon;
  4790. }
  4791. void SetMainIcon(UINT nID)
  4792. {
  4793. this->dwFlags &= ~TDF_USE_HICON_MAIN;
  4794. this->pszMainIcon = MAKEINTRESOURCEW(nID);
  4795. }
  4796. void SetMainIcon(LPCWSTR lpstrMainIcon)
  4797. {
  4798. this->dwFlags &= ~TDF_USE_HICON_MAIN;
  4799. this->pszMainIcon = lpstrMainIcon;
  4800. }
  4801. // main instruction text
  4802. void SetMainInstructionText(UINT nID)
  4803. {
  4804. this->pszMainInstruction = MAKEINTRESOURCEW(nID);
  4805. }
  4806. void SetMainInstructionText(LPCWSTR lpstrMainInstruction)
  4807. {
  4808. this->pszMainInstruction = lpstrMainInstruction;
  4809. }
  4810. // content text
  4811. void SetContentText(UINT nID)
  4812. {
  4813. this->pszContent = MAKEINTRESOURCEW(nID);
  4814. }
  4815. void SetContentText(LPCWSTR lpstrContent)
  4816. {
  4817. this->pszContent = lpstrContent;
  4818. }
  4819. // buttons
  4820. void SetButtons(const TASKDIALOG_BUTTON* pButtonsArg, UINT cButtonsArg, int nDefaultButtonArg = 0)
  4821. {
  4822. this->pButtons = pButtonsArg;
  4823. this->cButtons = cButtonsArg;
  4824. if(nDefaultButtonArg != 0)
  4825. this->nDefaultButton = nDefaultButtonArg;
  4826. }
  4827. void SetDefaultButton(int nDefaultButtonArg)
  4828. {
  4829. this->nDefaultButton = nDefaultButtonArg;
  4830. }
  4831. // radio buttons
  4832. void SetRadioButtons(const TASKDIALOG_BUTTON* pRadioButtonsArg, UINT cRadioButtonsArg, int nDefaultRadioButtonArg = 0)
  4833. {
  4834. this->pRadioButtons = pRadioButtonsArg;
  4835. this->cRadioButtons = cRadioButtonsArg;
  4836. if(nDefaultRadioButtonArg != 0)
  4837. this->nDefaultRadioButton = nDefaultRadioButtonArg;
  4838. }
  4839. void SetDefaultRadioButton(int nDefaultRadioButtonArg)
  4840. {
  4841. this->nDefaultRadioButton = nDefaultRadioButtonArg;
  4842. }
  4843. // verification text
  4844. void SetVerificationText(UINT nID)
  4845. {
  4846. this->pszVerificationText = MAKEINTRESOURCEW(nID);
  4847. }
  4848. void SetVerificationText(LPCWSTR lpstrVerificationText)
  4849. {
  4850. this->pszVerificationText = lpstrVerificationText;
  4851. }
  4852. // expanded information text
  4853. void SetExpandedInformationText(UINT nID)
  4854. {
  4855. this->pszExpandedInformation = MAKEINTRESOURCEW(nID);
  4856. }
  4857. void SetExpandedInformationText(LPCWSTR lpstrExpandedInformation)
  4858. {
  4859. this->pszExpandedInformation = lpstrExpandedInformation;
  4860. }
  4861. // expanded control text
  4862. void SetExpandedControlText(UINT nID)
  4863. {
  4864. this->pszExpandedControlText = MAKEINTRESOURCEW(nID);
  4865. }
  4866. void SetExpandedControlText(LPCWSTR lpstrExpandedControlText)
  4867. {
  4868. this->pszExpandedControlText = lpstrExpandedControlText;
  4869. }
  4870. // collapsed control text
  4871. void SetCollapsedControlText(UINT nID)
  4872. {
  4873. this->pszCollapsedControlText = MAKEINTRESOURCEW(nID);
  4874. }
  4875. void SetCollapsedControlText(LPCWSTR lpstrCollapsedControlText)
  4876. {
  4877. this->pszCollapsedControlText = lpstrCollapsedControlText;
  4878. }
  4879. // footer icon
  4880. void SetFooterIcon(HICON hIcon)
  4881. {
  4882. this->dwFlags |= TDF_USE_HICON_FOOTER;
  4883. this->hFooterIcon = hIcon;
  4884. }
  4885. void SetFooterIcon(UINT nID)
  4886. {
  4887. this->dwFlags &= ~TDF_USE_HICON_FOOTER;
  4888. this->pszFooterIcon = MAKEINTRESOURCEW(nID);
  4889. }
  4890. void SetFooterIcon(LPCWSTR lpstrFooterIcon)
  4891. {
  4892. this->dwFlags &= ~TDF_USE_HICON_FOOTER;
  4893. this->pszFooterIcon = lpstrFooterIcon;
  4894. }
  4895. // footer text
  4896. void SetFooterText(UINT nID)
  4897. {
  4898. this->pszFooter = MAKEINTRESOURCEW(nID);
  4899. }
  4900. void SetFooterText(LPCWSTR lpstrFooterText)
  4901. {
  4902. this->pszFooter = lpstrFooterText;
  4903. }
  4904. // width (in DLUs)
  4905. void SetWidth(UINT cxWidthArg)
  4906. {
  4907. this->cxWidth = cxWidthArg;
  4908. }
  4909. // modify flags
  4910. void ModifyFlags(DWORD dwRemove, DWORD dwAdd)
  4911. {
  4912. this->dwFlags = (this->dwFlags & ~dwRemove) | dwAdd;
  4913. }
  4914. };
  4915. ///////////////////////////////////////////////////////////////////////////////
  4916. // CTaskDialogImpl - implements a Task Dialog
  4917. template <class T>
  4918. class ATL_NO_VTABLE CTaskDialogImpl
  4919. {
  4920. public:
  4921. CTaskDialogConfig m_tdc;
  4922. HWND m_hWnd; // used only in callback functions
  4923. // Constructor
  4924. CTaskDialogImpl(HWND hWndParent = NULL) : m_hWnd(NULL)
  4925. {
  4926. m_tdc.hwndParent = hWndParent;
  4927. m_tdc.pfCallback = T::TaskDialogCallback;
  4928. m_tdc.lpCallbackData = (LONG_PTR)static_cast<T*>(this);
  4929. }
  4930. // Operations
  4931. HRESULT DoModal(HWND hWndParent = ::GetActiveWindow(), int* pnButton = NULL, int* pnRadioButton = NULL, BOOL* pfVerificationFlagChecked = NULL)
  4932. {
  4933. if(m_tdc.hwndParent == NULL)
  4934. m_tdc.hwndParent = hWndParent;
  4935. #ifdef _WTL_TASKDIALOG_DIRECT
  4936. return ::TaskDialogIndirect(&m_tdc, pnButton, pnRadioButton, pfVerificationFlagChecked);
  4937. #else
  4938. // This allows apps to run on older versions of Windows
  4939. typedef HRESULT (STDAPICALLTYPE *PFN_TaskDialogIndirect)(const TASKDIALOGCONFIG* pTaskConfig, int* pnButton, int* pnRadioButton, BOOL* pfVerificationFlagChecked);
  4940. HRESULT hRet = E_UNEXPECTED;
  4941. HMODULE m_hCommCtrlDLL = ::LoadLibrary(_T("comctl32.dll"));
  4942. if(m_hCommCtrlDLL != NULL)
  4943. {
  4944. PFN_TaskDialogIndirect pfnTaskDialogIndirect = (PFN_TaskDialogIndirect)::GetProcAddress(m_hCommCtrlDLL, "TaskDialogIndirect");
  4945. if(pfnTaskDialogIndirect != NULL)
  4946. hRet = pfnTaskDialogIndirect(&m_tdc, pnButton, pnRadioButton, pfVerificationFlagChecked);
  4947. ::FreeLibrary(m_hCommCtrlDLL);
  4948. }
  4949. return hRet;
  4950. #endif
  4951. }
  4952. // Operations - setting values of TASKDIALOGCONFIG
  4953. // common buttons
  4954. void SetCommonButtons(TASKDIALOG_COMMON_BUTTON_FLAGS dwCommonButtons)
  4955. { m_tdc.SetCommonButtons(dwCommonButtons); }
  4956. // window title text
  4957. void SetWindowTitle(UINT nID)
  4958. { m_tdc.SetWindowTitle(nID); }
  4959. void SetWindowTitle(LPCWSTR lpstrWindowTitle)
  4960. { m_tdc.SetWindowTitle(lpstrWindowTitle); }
  4961. // main icon
  4962. void SetMainIcon(HICON hIcon)
  4963. { m_tdc.SetMainIcon(hIcon); }
  4964. void SetMainIcon(UINT nID)
  4965. { m_tdc.SetMainIcon(nID); }
  4966. void SetMainIcon(LPCWSTR lpstrMainIcon)
  4967. { m_tdc.SetMainIcon(lpstrMainIcon); }
  4968. // main instruction text
  4969. void SetMainInstructionText(UINT nID)
  4970. { m_tdc.SetMainInstructionText(nID); }
  4971. void SetMainInstructionText(LPCWSTR lpstrMainInstruction)
  4972. { m_tdc.SetMainInstructionText(lpstrMainInstruction); }
  4973. // content text
  4974. void SetContentText(UINT nID)
  4975. { m_tdc.SetContentText(nID); }
  4976. void SetContentText(LPCWSTR lpstrContent)
  4977. { m_tdc.SetContentText(lpstrContent); }
  4978. // buttons
  4979. void SetButtons(const TASKDIALOG_BUTTON* pButtons, UINT cButtons, int nDefaultButton = 0)
  4980. { m_tdc.SetButtons(pButtons, cButtons, nDefaultButton); }
  4981. void SetDefaultButton(int nDefaultButton)
  4982. { m_tdc.SetDefaultButton(nDefaultButton); }
  4983. // radio buttons
  4984. void SetRadioButtons(const TASKDIALOG_BUTTON* pRadioButtons, UINT cRadioButtons, int nDefaultRadioButton = 0)
  4985. { m_tdc.SetRadioButtons(pRadioButtons, cRadioButtons, nDefaultRadioButton); }
  4986. void SetDefaultRadioButton(int nDefaultRadioButton)
  4987. { m_tdc.SetDefaultRadioButton(nDefaultRadioButton); }
  4988. // verification text
  4989. void SetVerificationText(UINT nID)
  4990. { m_tdc.SetVerificationText(nID); }
  4991. void SetVerificationText(LPCWSTR lpstrVerificationText)
  4992. { m_tdc.SetVerificationText(lpstrVerificationText); }
  4993. // expanded information text
  4994. void SetExpandedInformationText(UINT nID)
  4995. { m_tdc.SetExpandedInformationText(nID); }
  4996. void SetExpandedInformationText(LPCWSTR lpstrExpandedInformation)
  4997. { m_tdc.SetExpandedInformationText(lpstrExpandedInformation); }
  4998. // expanded control text
  4999. void SetExpandedControlText(UINT nID)
  5000. { m_tdc.SetExpandedControlText(nID); }
  5001. void SetExpandedControlText(LPCWSTR lpstrExpandedControlText)
  5002. { m_tdc.SetExpandedControlText(lpstrExpandedControlText); }
  5003. // collapsed control text
  5004. void SetCollapsedControlText(UINT nID)
  5005. { m_tdc.SetCollapsedControlText(nID); }
  5006. void SetCollapsedControlText(LPCWSTR lpstrCollapsedControlText)
  5007. { m_tdc.SetCollapsedControlText(lpstrCollapsedControlText); }
  5008. // footer icon
  5009. void SetFooterIcon(HICON hIcon)
  5010. { m_tdc.SetFooterIcon(hIcon); }
  5011. void SetFooterIcon(UINT nID)
  5012. { m_tdc.SetFooterIcon(nID); }
  5013. void SetFooterIcon(LPCWSTR lpstrFooterIcon)
  5014. { m_tdc.SetFooterIcon(lpstrFooterIcon); }
  5015. // footer text
  5016. void SetFooterText(UINT nID)
  5017. { m_tdc.SetFooterText(nID); }
  5018. void SetFooterText(LPCWSTR lpstrFooterText)
  5019. { m_tdc.SetFooterText(lpstrFooterText); }
  5020. // width (in DLUs)
  5021. void SetWidth(UINT cxWidth)
  5022. { m_tdc.SetWidth(cxWidth); }
  5023. // modify flags
  5024. void ModifyFlags(DWORD dwRemove, DWORD dwAdd)
  5025. { m_tdc.ModifyFlags(dwRemove, dwAdd); }
  5026. // Implementation
  5027. static HRESULT CALLBACK TaskDialogCallback(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, LONG_PTR lpRefData)
  5028. {
  5029. T* pT = (T*)lpRefData;
  5030. ATLASSERT((pT->m_hWnd == NULL) || (pT->m_hWnd == hWnd));
  5031. BOOL bRet = FALSE;
  5032. switch(uMsg)
  5033. {
  5034. case TDN_DIALOG_CONSTRUCTED:
  5035. pT->m_hWnd = hWnd;
  5036. pT->OnDialogConstructed();
  5037. break;
  5038. case TDN_CREATED:
  5039. pT->OnCreated();
  5040. break;
  5041. case TDN_BUTTON_CLICKED:
  5042. bRet = pT->OnButtonClicked((int)wParam);
  5043. break;
  5044. case TDN_RADIO_BUTTON_CLICKED:
  5045. pT->OnRadioButtonClicked((int)wParam);
  5046. break;
  5047. case TDN_HYPERLINK_CLICKED:
  5048. pT->OnHyperlinkClicked((LPCWSTR)lParam);
  5049. break;
  5050. case TDN_EXPANDO_BUTTON_CLICKED:
  5051. pT->OnExpandoButtonClicked((wParam != 0));
  5052. break;
  5053. case TDN_VERIFICATION_CLICKED:
  5054. pT->OnVerificationClicked((wParam != 0));
  5055. break;
  5056. case TDN_HELP:
  5057. pT->OnHelp();
  5058. break;
  5059. case TDN_TIMER:
  5060. bRet = pT->OnTimer((DWORD)wParam);
  5061. break;
  5062. case TDN_NAVIGATED:
  5063. pT->OnNavigated();
  5064. break;
  5065. case TDN_DESTROYED:
  5066. pT->OnDestroyed();
  5067. pT->m_hWnd = NULL;
  5068. break;
  5069. default:
  5070. ATLTRACE2(atlTraceUI, 0, _T("Unknown notification received in CTaskDialogImpl::TaskDialogCallback\n"));
  5071. break;
  5072. }
  5073. return (bRet != FALSE) ? S_OK : S_FALSE;
  5074. }
  5075. // Overrideables - notification handlers
  5076. void OnDialogConstructed()
  5077. {
  5078. }
  5079. void OnCreated()
  5080. {
  5081. }
  5082. BOOL OnButtonClicked(int /*nButton*/)
  5083. {
  5084. return FALSE; // don't prevent dialog to close
  5085. }
  5086. void OnRadioButtonClicked(int /*nRadioButton*/)
  5087. {
  5088. }
  5089. void OnHyperlinkClicked(LPCWSTR /*pszHREF*/)
  5090. {
  5091. }
  5092. void OnExpandoButtonClicked(bool /*bExpanded*/)
  5093. {
  5094. }
  5095. void OnVerificationClicked(bool /*bChecked*/)
  5096. {
  5097. }
  5098. void OnHelp()
  5099. {
  5100. }
  5101. BOOL OnTimer(DWORD /*dwTickCount*/)
  5102. {
  5103. return FALSE; // don't reset counter
  5104. }
  5105. void OnNavigated()
  5106. {
  5107. }
  5108. void OnDestroyed()
  5109. {
  5110. }
  5111. // Commands - valid to call only from handlers
  5112. void NavigatePage(TASKDIALOGCONFIG& tdc)
  5113. {
  5114. ATLASSERT(m_hWnd != NULL);
  5115. tdc.cbSize = sizeof(TASKDIALOGCONFIG);
  5116. if(tdc.hwndParent == NULL)
  5117. tdc.hwndParent = m_tdc.hwndParent;
  5118. tdc.pfCallback = m_tdc.pfCallback;
  5119. tdc.lpCallbackData = m_tdc.lpCallbackData;
  5120. (TASKDIALOGCONFIG)m_tdc = tdc;
  5121. ::SendMessage(m_hWnd, TDM_NAVIGATE_PAGE, 0, (LPARAM)&tdc);
  5122. }
  5123. // modify TASKDIALOGCONFIG values, then call this to update task dialog
  5124. void NavigatePage()
  5125. {
  5126. ATLASSERT(m_hWnd != NULL);
  5127. ::SendMessage(m_hWnd, TDM_NAVIGATE_PAGE, 0, (LPARAM)&m_tdc);
  5128. }
  5129. void ClickButton(int nButton)
  5130. {
  5131. ATLASSERT(m_hWnd != NULL);
  5132. ::SendMessage(m_hWnd, TDM_CLICK_BUTTON, nButton, 0L);
  5133. }
  5134. void SetMarqueeProgressBar(BOOL bMarquee)
  5135. {
  5136. ATLASSERT(m_hWnd != NULL);
  5137. ::SendMessage(m_hWnd, TDM_SET_MARQUEE_PROGRESS_BAR, bMarquee, 0L);
  5138. }
  5139. BOOL SetProgressBarState(int nNewState)
  5140. {
  5141. ATLASSERT(m_hWnd != NULL);
  5142. return (BOOL)::SendMessage(m_hWnd, TDM_SET_PROGRESS_BAR_STATE, nNewState, 0L);
  5143. }
  5144. DWORD SetProgressBarRange(int nMinRange, int nMaxRange)
  5145. {
  5146. ATLASSERT(m_hWnd != NULL);
  5147. return (DWORD)::SendMessage(m_hWnd, TDM_SET_PROGRESS_BAR_RANGE, 0, MAKELPARAM(nMinRange, nMaxRange));
  5148. }
  5149. int SetProgressBarPos(int nNewPos)
  5150. {
  5151. ATLASSERT(m_hWnd != NULL);
  5152. return (int)::SendMessage(m_hWnd, TDM_SET_PROGRESS_BAR_POS, nNewPos, 0L);
  5153. }
  5154. BOOL SetProgressBarMarquee(BOOL bMarquee, UINT uSpeed)
  5155. {
  5156. ATLASSERT(m_hWnd != NULL);
  5157. return (BOOL)::SendMessage(m_hWnd, TDM_SET_PROGRESS_BAR_MARQUEE, bMarquee, uSpeed);
  5158. }
  5159. void SetElementText(TASKDIALOG_ELEMENTS element, LPCWSTR lpstrText)
  5160. {
  5161. ATLASSERT(m_hWnd != NULL);
  5162. ::SendMessage(m_hWnd, TDM_SET_ELEMENT_TEXT, element, (LPARAM)lpstrText);
  5163. }
  5164. void ClickRadioButton(int nRadioButton)
  5165. {
  5166. ATLASSERT(m_hWnd != NULL);
  5167. ::SendMessage(m_hWnd, TDM_CLICK_RADIO_BUTTON, nRadioButton, 0L);
  5168. }
  5169. void EnableButton(int nButton, BOOL bEnable)
  5170. {
  5171. ATLASSERT(m_hWnd != NULL);
  5172. ::SendMessage(m_hWnd, TDM_ENABLE_BUTTON, nButton, bEnable);
  5173. }
  5174. void EnableRadioButton(int nButton, BOOL bEnable)
  5175. {
  5176. ATLASSERT(m_hWnd != NULL);
  5177. ::SendMessage(m_hWnd, TDM_ENABLE_RADIO_BUTTON, nButton, bEnable);
  5178. }
  5179. void ClickVerification(BOOL bCheck, BOOL bFocus)
  5180. {
  5181. ATLASSERT(m_hWnd != NULL);
  5182. ::SendMessage(m_hWnd, TDM_CLICK_VERIFICATION, bCheck, bFocus);
  5183. }
  5184. void UpdateElementText(TASKDIALOG_ELEMENTS element, LPCWSTR lpstrText)
  5185. {
  5186. ATLASSERT(m_hWnd != NULL);
  5187. ::SendMessage(m_hWnd, TDM_UPDATE_ELEMENT_TEXT, element, (LPARAM)lpstrText);
  5188. }
  5189. void SetButtonElevationRequiredState(int nButton, BOOL bElevation)
  5190. {
  5191. ATLASSERT(m_hWnd != NULL);
  5192. ::SendMessage(m_hWnd, TDM_SET_BUTTON_ELEVATION_REQUIRED_STATE, nButton, bElevation);
  5193. }
  5194. void UpdateIcon(TASKDIALOG_ICON_ELEMENTS element, HICON hIcon)
  5195. {
  5196. ATLASSERT(m_hWnd != NULL);
  5197. #ifdef _DEBUG
  5198. if(element == TDIE_ICON_MAIN)
  5199. ATLASSERT((m_tdc.dwFlags & TDF_USE_HICON_MAIN) != 0);
  5200. else if(element == TDIE_ICON_FOOTER)
  5201. ATLASSERT((m_tdc.dwFlags & TDF_USE_HICON_FOOTER) != 0);
  5202. #endif // _DEBUG
  5203. ::SendMessage(m_hWnd, TDM_UPDATE_ICON, element, (LPARAM)hIcon);
  5204. }
  5205. void UpdateIcon(TASKDIALOG_ICON_ELEMENTS element, LPCWSTR lpstrIcon)
  5206. {
  5207. ATLASSERT(m_hWnd != NULL);
  5208. #ifdef _DEBUG
  5209. if(element == TDIE_ICON_MAIN)
  5210. ATLASSERT((m_tdc.dwFlags & TDF_USE_HICON_MAIN) == 0);
  5211. else if(element == TDIE_ICON_FOOTER)
  5212. ATLASSERT((m_tdc.dwFlags & TDF_USE_HICON_FOOTER) == 0);
  5213. #endif // _DEBUG
  5214. ::SendMessage(m_hWnd, TDM_UPDATE_ICON, element, (LPARAM)lpstrIcon);
  5215. }
  5216. };
  5217. ///////////////////////////////////////////////////////////////////////////////
  5218. // CTaskDialog - for non-customized task dialogs
  5219. class CTaskDialog : public CTaskDialogImpl<CTaskDialog>
  5220. {
  5221. public:
  5222. CTaskDialog(HWND hWndParent = NULL) : CTaskDialogImpl<CTaskDialog>(hWndParent)
  5223. {
  5224. m_tdc.pfCallback = NULL;
  5225. }
  5226. };
  5227. #endif // (_WIN32_WINNT >= 0x0600) || defined(_WTL_TASKDIALOG)
  5228. } // namespace WTL
  5229. #endif // __ATLDLGS_H__