TB2Item.pas 229 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151
  1. { MP }
  2. unit TB2Item;
  3. {
  4. Toolbar2000
  5. Copyright (C) 1998-2005 by Jordan Russell
  6. All rights reserved.
  7. The contents of this file are subject to the "Toolbar2000 License"; you may
  8. not use or distribute this file except in compliance with the
  9. "Toolbar2000 License". A copy of the "Toolbar2000 License" may be found in
  10. TB2k-LICENSE.txt or at:
  11. https://jrsoftware.org/files/tb2k/TB2k-LICENSE.txt
  12. Alternatively, the contents of this file may be used under the terms of the
  13. GNU General Public License (the "GPL"), in which case the provisions of the
  14. GPL are applicable instead of those in the "Toolbar2000 License". A copy of
  15. the GPL may be found in GPL-LICENSE.txt or at:
  16. https://jrsoftware.org/files/tb2k/GPL-LICENSE.txt
  17. If you wish to allow use of your version of this file only under the terms of
  18. the GPL and not to allow others to use your version of this file under the
  19. "Toolbar2000 License", indicate your decision by deleting the provisions
  20. above and replace them with the notice and other provisions required by the
  21. GPL. If you do not delete the provisions above, a recipient may use your
  22. version of this file under either the "Toolbar2000 License" or the GPL.
  23. $jrsoftware: tb2k/Source/TB2Item.pas,v 1.277 2005/06/23 21:55:44 jr Exp $
  24. }
  25. interface
  26. {$I TB2Ver.inc}
  27. {x$DEFINE TB2K_NO_ANIMATION}
  28. { Enabling the above define disables all menu animation. For debugging
  29. purpose only. }
  30. {x$DEFINE TB2K_USE_STRICT_O2K_MENU_STYLE}
  31. { Enabling the above define forces it to use clBtnFace for the menu color
  32. instead of clMenu, and disables the use of flat menu borders on Windows
  33. XP with themes enabled. }
  34. uses
  35. Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  36. StdCtrls, CommCtrl, Menus, ActnList, ImgList, TB2Anim, UITypes;
  37. const
  38. WM_TB2K_POPUPSHOWING = WM_USER + 554;
  39. { Parameter in LParam of WM_TB2K_POPUPSHOWING }
  40. TPS_ANIMSTART = 1; // animation query: if Result <> 0, do not animate!
  41. TPS_ANIMFINISHED = 2; // only fired when animation thread is done
  42. TPS_NOANIM = 3; // fired when animation is done, or if showing with no animation
  43. type
  44. TTBCustomItem = class;
  45. TTBCustomItemClass = class of TTBCustomItem;
  46. TTBCustomItemActionLink = class;
  47. TTBCustomItemActionLinkClass = class of TTBCustomItemActionLink;
  48. TTBItemViewer = class;
  49. TTBItemViewerClass = class of TTBItemViewer;
  50. TTBPopupWindow = class;
  51. TTBPopupWindowClass = class of TTBPopupWindow;
  52. TTBView = class;
  53. TTBDoneAction = (tbdaNone, tbdaCancel, tbdaClickItem, tbdaOpenSystemMenu,
  54. tbdaHelpContext { MP }, tbdaHelpKeyword { /MP });
  55. PTBDoneActionData = ^TTBDoneActionData;
  56. TTBDoneActionData = record
  57. DoneAction: TTBDoneAction;
  58. case TTBDoneAction of
  59. tbdaClickItem: (ClickItem: TTBCustomItem; Sound: Boolean);
  60. tbdaOpenSystemMenu: (Wnd: HWND; Key: Cardinal);
  61. tbdaHelpContext: (ContextID: Integer);
  62. { MP }
  63. tbdaHelpKeyword: (HelpKeyword: String[100]);
  64. end;
  65. TTBInsertItemProc = procedure(AParent: TComponent; AItem: TTBCustomItem) of object;
  66. TTBItemChangedAction = (tbicInserted, tbicDeleting, tbicSubitemsChanged,
  67. tbicSubitemsBeginUpdate, tbicSubitemsEndUpdate, tbicInvalidate,
  68. tbicInvalidateAndResize, tbicRecreateItemViewers, tbicNameChanged,
  69. tbicSubMenuImagesChanged);
  70. TTBItemChangedProc = procedure(Sender: TTBCustomItem; Relayed: Boolean;
  71. Action: TTBItemChangedAction; Index: Integer; Item: TTBCustomItem) of object;
  72. TTBItemData = record
  73. Item: TTBCustomItem;
  74. end;
  75. PTBItemDataArray = ^TTBItemDataArray;
  76. TTBItemDataArray = array[0..$7FFFFFFF div SizeOf(TTBItemData)-1] of TTBItemData;
  77. TTBItemDisplayMode = (nbdmDefault, nbdmTextOnly, nbdmTextOnlyInMenus, nbdmImageAndText);
  78. TTBItemOption = (tboDefault, tboDropdownArrow, tboImageAboveCaption,
  79. tboLongHintInMenuOnly, tboNoAutoHint, tboNoRotation, tboSameWidth,
  80. tboShowHint, tboToolbarStyle, tboToolbarSize);
  81. TTBItemOptions = set of TTBItemOption;
  82. TTBItemStyle = set of (tbisSubmenu, tbisSelectable, tbisSeparator,
  83. tbisEmbeddedGroup, tbisClicksTransparent, tbisCombo, tbisNoAutoOpen,
  84. tbisSubitemsEditable, tbisNoLineBreak, tbisRightAlign, tbisDontSelectFirst,
  85. tbisRedrawOnSelChange, tbisRedrawOnMouseOverChange, tbisStretch);
  86. TTBPopupAlignment = (tbpaLeft, tbpaRight, tbpaCenter);
  87. TTBPopupEvent = procedure(Sender: TTBCustomItem; FromLink: Boolean) of object;
  88. TTBSelectEvent = procedure(Sender: TTBCustomItem; Viewer: TTBItemViewer;
  89. Selecting: Boolean) of object;
  90. ETBItemError = class(Exception);
  91. TTBImageChangeLink = class(TChangeLink)
  92. private
  93. FLastWidth, FLastHeight: Integer;
  94. end;
  95. TTBPopupPositionRec = record
  96. PositionAsSubmenu: Boolean;
  97. Alignment: TTBPopupAlignment;
  98. Opposite: Boolean;
  99. MonitorRect: TRect;
  100. ParentItemRect: TRect;
  101. NCSizeX: Integer;
  102. NCSizeY: Integer;
  103. X, Y, W, H: Integer;
  104. AnimDir: TTBAnimationDirection;
  105. PlaySound: Boolean;
  106. end;
  107. TTBCustomItem = class(TComponent)
  108. private
  109. FActionLink: TTBCustomItemActionLink;
  110. FAutoCheck: Boolean;
  111. FCaption: String;
  112. FChecked: Boolean;
  113. FDisplayMode: TTBItemDisplayMode;
  114. FEnabled: Boolean;
  115. FEffectiveOptions: TTBItemOptions;
  116. FGroupIndex: Integer;
  117. FHelpContext: THelpContext;
  118. { MP }
  119. FHelpKeyword: String;
  120. FHint: String;
  121. FImageIndex: TImageIndex;
  122. FImages: TCustomImageList;
  123. FImagesChangeLink: TTBImageChangeLink;
  124. FItems: PTBItemDataArray;
  125. FItemCount: Integer;
  126. FItemStyle: TTBItemStyle;
  127. FLinkParents: TList;
  128. FMaskOptions: TTBItemOptions;
  129. FOptions: TTBItemOptions;
  130. FInheritOptions: Boolean;
  131. FNotifyList: TList;
  132. FOnClick: TNotifyEvent;
  133. FOnPopup: TTBPopupEvent;
  134. FOnSelect: TTBSelectEvent;
  135. FParent: TTBCustomItem;
  136. FParentComponent: TComponent;
  137. FRadioItem: Boolean;
  138. FShortCut: TShortCut;
  139. FSubMenuImages: TCustomImageList;
  140. FSubMenuImagesChangeLink: TTBImageChangeLink;
  141. FLinkSubitems: TTBCustomItem;
  142. FVisible: Boolean;
  143. procedure DoActionChange(Sender: TObject);
  144. function ChangeImages(var AImages: TCustomImageList;
  145. const Value: TCustomImageList; var AChangeLink: TTBImageChangeLink): Boolean;
  146. class procedure ClickWndProc(var Message: TMessage);
  147. function FindItemWithShortCut(AShortCut: TShortCut;
  148. var ATopmostParent: TTBCustomItem): TTBCustomItem;
  149. function FixOptions(const AOptions: TTBItemOptions): TTBItemOptions;
  150. function GetAction: TBasicAction;
  151. function GetItem(Index: Integer): TTBCustomItem;
  152. procedure ImageListChangeHandler(Sender: TObject);
  153. procedure InternalNotify(Ancestor: TTBCustomItem; NestingLevel: Integer;
  154. Action: TTBItemChangedAction; Index: Integer; Item: TTBCustomItem);
  155. function IsAutoCheckStored: Boolean;
  156. function IsCaptionStored: Boolean;
  157. function IsCheckedStored: Boolean;
  158. function IsEnabledStored: Boolean;
  159. function IsHelpContextStored: Boolean;
  160. function IsHintStored: Boolean;
  161. function IsImageIndexStored: Boolean;
  162. function IsOnClickStored: Boolean;
  163. function IsShortCutStored: Boolean;
  164. function IsVisibleStored: Boolean;
  165. procedure Notify(Action: TTBItemChangedAction; Index: Integer; Item: TTBCustomItem);
  166. procedure RefreshOptions;
  167. procedure SetAction(Value: TBasicAction);
  168. procedure SetCaption(Value: String);
  169. procedure SetChecked(Value: Boolean);
  170. procedure SetDisplayMode(Value: TTBItemDisplayMode);
  171. procedure SetEnabled(Value: Boolean);
  172. procedure SetGroupIndex(Value: Integer);
  173. procedure SetImageIndex(Value: TImageIndex);
  174. procedure SetImages(Value: TCustomImageList);
  175. procedure SetInheritOptions(Value: Boolean);
  176. procedure SetLinkSubitems(Value: TTBCustomItem);
  177. procedure SetMaskOptions(Value: TTBItemOptions);
  178. procedure SetOptions(Value: TTBItemOptions);
  179. procedure SetRadioItem(Value: Boolean);
  180. procedure SetSubMenuImages(Value: TCustomImageList);
  181. procedure SetVisible(Value: Boolean);
  182. procedure SubMenuImagesChanged;
  183. procedure TurnSiblingsOff;
  184. protected
  185. procedure ActionChange(Sender: TObject; CheckDefaults: Boolean); dynamic;
  186. procedure Change(NeedResize: Boolean); virtual;
  187. function CreatePopup(const ParentView: TTBView; const ParentViewer: TTBItemViewer;
  188. const PositionAsSubmenu, SelectFirstItem, Customizing: Boolean;
  189. const APopupPoint: TPoint; const Alignment: TTBPopupAlignment): TTBPopupWindow; virtual;
  190. procedure DoPopup(Sender: TTBCustomItem; FromLink: Boolean); virtual;
  191. procedure EnabledChanged; virtual;
  192. function GetActionLinkClass: TTBCustomItemActionLinkClass; dynamic;
  193. function GetChevronParentView: TTBView; virtual;
  194. procedure GetChildren(Proc: TGetChildProc; Root: TComponent); override;
  195. function GetItemViewerClass(AView: TTBView): TTBItemViewerClass; virtual;
  196. procedure GetPopupPosition(ParentView: TTBView;
  197. PopupWindow: TTBPopupWindow; var PopupPositionRec: TTBPopupPositionRec); virtual;
  198. function GetPopupWindowClass: TTBPopupWindowClass; virtual;
  199. procedure IndexError;
  200. procedure Loaded; override;
  201. function NeedToRecreateViewer(AViewer: TTBItemViewer): Boolean; virtual;
  202. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  203. function OpenPopup(const SelectFirstItem, TrackRightButton: Boolean;
  204. const PopupPoint: TPoint; const Alignment: TTBPopupAlignment;
  205. const ReturnClickedItemOnly: Boolean; PositionAsSubmenu: Boolean): TTBCustomItem;
  206. procedure RecreateItemViewers;
  207. procedure SetChildOrder(Child: TComponent; Order: Integer); override;
  208. procedure SetName(const NewName: TComponentName); override;
  209. procedure SetParentComponent(Value: TComponent); override;
  210. property ActionLink: TTBCustomItemActionLink read FActionLink write FActionLink;
  211. property ItemStyle: TTBItemStyle read FItemStyle write FItemStyle;
  212. public
  213. constructor Create(AOwner: TComponent); override;
  214. destructor Destroy; override;
  215. function HasParent: Boolean; override;
  216. function GetParentComponent: TComponent; override;
  217. function GetTopComponent: TComponent;
  218. procedure Add(AItem: TTBCustomItem);
  219. procedure Clear;
  220. procedure Click; virtual;
  221. function ContainsItem(AItem: TTBCustomItem): Boolean;
  222. procedure Delete(Index: Integer);
  223. function GetShortCutText: String;
  224. function IndexOf(AItem: TTBCustomItem): Integer;
  225. procedure InitiateAction; virtual;
  226. procedure Insert(NewIndex: Integer; AItem: TTBCustomItem);
  227. function IsShortCut(var Message: TWMKey): Boolean;
  228. procedure Move(CurIndex, NewIndex: Integer);
  229. function Popup(X, Y: Integer; TrackRightButton: Boolean;
  230. Alignment: TTBPopupAlignment = tbpaLeft;
  231. ReturnClickedItemOnly: Boolean = False;
  232. PositionAsSubmenu: Boolean = False): TTBCustomItem;
  233. procedure PostClick;
  234. procedure RegisterNotification(ANotify: TTBItemChangedProc);
  235. procedure Remove(Item: TTBCustomItem);
  236. procedure UnregisterNotification(ANotify: TTBItemChangedProc);
  237. procedure ViewBeginUpdate;
  238. procedure ViewEndUpdate;
  239. procedure ChangeScale(M, D: Integer); virtual;
  240. property Action: TBasicAction read GetAction write SetAction;
  241. property AutoCheck: Boolean read FAutoCheck write FAutoCheck stored IsAutoCheckStored default False;
  242. property Caption: String read FCaption write SetCaption stored IsCaptionStored;
  243. property Count: Integer read FItemCount;
  244. property Checked: Boolean read FChecked write SetChecked stored IsCheckedStored default False;
  245. property DisplayMode: TTBItemDisplayMode read FDisplayMode write SetDisplayMode default nbdmDefault;
  246. property EffectiveOptions: TTBItemOptions read FEffectiveOptions;
  247. property Enabled: Boolean read FEnabled write SetEnabled stored IsEnabledStored default True;
  248. property GroupIndex: Integer read FGroupIndex write SetGroupIndex default 0;
  249. property HelpContext: THelpContext read FHelpContext write FHelpContext stored IsHelpContextStored default 0;
  250. { MP }
  251. property HelpKeyword: String read FHelpKeyword write FHelpKeyword stored IsHelpContextStored;
  252. property Hint: String read FHint write FHint stored IsHintStored;
  253. property ImageIndex: TImageIndex read FImageIndex write SetImageIndex stored IsImageIndexStored default -1;
  254. property Images: TCustomImageList read FImages write SetImages;
  255. property InheritOptions: Boolean read FInheritOptions write SetInheritOptions default True;
  256. property Items[Index: Integer]: TTBCustomItem read GetItem; default;
  257. property LinkSubitems: TTBCustomItem read FLinkSubitems write SetLinkSubitems;
  258. property MaskOptions: TTBItemOptions read FMaskOptions write SetMaskOptions default [];
  259. property Options: TTBItemOptions read FOptions write SetOptions default [];
  260. property Parent: TTBCustomItem read FParent;
  261. property ParentComponent: TComponent read FParentComponent write FParentComponent;
  262. property RadioItem: Boolean read FRadioItem write SetRadioItem default False;
  263. property ShortCut: TShortCut read FShortCut write FShortCut stored IsShortCutStored default 0;
  264. property SubMenuImages: TCustomImageList read FSubMenuImages write SetSubMenuImages;
  265. property Visible: Boolean read FVisible write SetVisible stored IsVisibleStored default True;
  266. property OnClick: TNotifyEvent read FOnClick write FOnClick stored IsOnClickStored;
  267. property OnPopup: TTBPopupEvent read FOnPopup write FOnPopup;
  268. property OnSelect: TTBSelectEvent read FOnSelect write FOnSelect;
  269. end;
  270. TTBCustomItemActionLink = class(TActionLink)
  271. protected
  272. FClient: TTBCustomItem;
  273. procedure AssignClient(AClient: TObject); override;
  274. function IsAutoCheckLinked: Boolean; virtual;
  275. function IsCaptionLinked: Boolean; override;
  276. function IsCheckedLinked: Boolean; override;
  277. function IsEnabledLinked: Boolean; override;
  278. function IsHelpContextLinked: Boolean; override;
  279. { MP }
  280. function IsHelpLinked: Boolean; override;
  281. function IsHintLinked: Boolean; override;
  282. function IsImageIndexLinked: Boolean; override;
  283. function IsShortCutLinked: Boolean; override;
  284. function IsVisibleLinked: Boolean; override;
  285. function IsOnExecuteLinked: Boolean; override;
  286. procedure SetAutoCheck(Value: Boolean); override;
  287. procedure SetCaption(const Value: String); override;
  288. procedure SetChecked(Value: Boolean); override;
  289. procedure SetEnabled(Value: Boolean); override;
  290. procedure SetHelpContext(Value: THelpContext); override;
  291. { MP }
  292. procedure SetHelpKeyword(const Value: string); override;
  293. procedure SetHint(const Value: String); override;
  294. procedure SetImageIndex(Value: Integer); override;
  295. procedure SetShortCut(Value: TShortCut); override;
  296. procedure SetVisible(Value: Boolean); override;
  297. procedure SetOnExecute(Value: TNotifyEvent); override;
  298. end;
  299. TTBBaseAccObject = class(TInterfacedObject, IDispatch)
  300. public
  301. procedure ClientIsDestroying; virtual; abstract;
  302. { IDispatch }
  303. function GetTypeInfoCount(out Count: Integer): HResult; stdcall;
  304. function GetTypeInfo(Index, LocaleID: Integer; out TypeInfo): HResult; stdcall;
  305. function GetIDsOfNames(const IID: TGUID; Names: Pointer;
  306. NameCount, LocaleID: Integer; DispIDs: Pointer): HResult; stdcall;
  307. function Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer;
  308. Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult; stdcall;
  309. end;
  310. TTBItemViewer = class
  311. private
  312. FBoundsRect: TRect;
  313. FClipped: Boolean;
  314. FGroupLevel: Integer;
  315. FItem: TTBCustomItem;
  316. FOffEdge: Boolean;
  317. FShow: Boolean;
  318. FView: TTBView;
  319. procedure AccSelect(const AExecute: Boolean);
  320. function GetIndex: Integer;
  321. protected
  322. FAccObjectInstance: TTBBaseAccObject;
  323. procedure CalcSize(const Canvas: TCanvas; var AWidth, AHeight: Integer);
  324. virtual;
  325. function CaptionShown: Boolean; dynamic;
  326. function DoExecute: Boolean; virtual;
  327. procedure DrawItemCaption(const Canvas: TCanvas; ARect: TRect;
  328. const ACaption: String; ADrawDisabledShadow: Boolean; AFormat: UINT); virtual;
  329. procedure Entering(OldSelected: TTBItemViewer); virtual;
  330. function GetAccRole: Integer; virtual;
  331. function GetAccValue(var Value: WideString): Boolean; virtual;
  332. function GetCaptionText: String; virtual;
  333. procedure GetCursor(const Pt: TPoint; var ACursor: HCURSOR); virtual;
  334. function GetImageList: TCustomImageList;
  335. function ImageShown: Boolean;
  336. function IsRotated: Boolean;
  337. function IsToolbarSize: Boolean; virtual;
  338. function IsPtInButtonPart(X, Y: Integer): Boolean; virtual;
  339. procedure KeyDown(var Key: Word; Shift: TShiftState); virtual;
  340. procedure Leaving; virtual;
  341. procedure LosingCapture; virtual;
  342. procedure MouseDown(Shift: TShiftState; X, Y: Integer;
  343. var MouseDownOnMenu: Boolean); virtual;
  344. procedure MouseMove(X, Y: Integer); virtual;
  345. procedure MouseUp(X, Y: Integer; MouseWasDownOnMenu: Boolean); virtual;
  346. procedure MouseWheel(WheelDelta: Integer; X, Y: Integer); virtual;
  347. procedure Paint(const Canvas: TCanvas; const ClientAreaRect: TRect;
  348. IsSelected, IsPushed, UseDisabledShadow: Boolean); virtual;
  349. procedure PostAccSelect(const AExecute: Boolean);
  350. function UsesSameWidth: Boolean; virtual;
  351. public
  352. State: set of (tbisInvalidated, tbisLineSep);
  353. property BoundsRect: TRect read FBoundsRect;
  354. property Clipped: Boolean read FClipped;
  355. property Index: Integer read GetIndex;
  356. property Item: TTBCustomItem read FItem;
  357. property OffEdge: Boolean read FOffEdge;
  358. property Show: Boolean read FShow;
  359. property View: TTBView read FView;
  360. constructor Create(AView: TTBView; AItem: TTBCustomItem; AGroupLevel: Integer); virtual;
  361. destructor Destroy; override;
  362. procedure Execute(AGivePriority: Boolean);
  363. function GetAccObject: IDispatch;
  364. function GetHintText: String;
  365. function IsAccessible: Boolean;
  366. function IsToolbarStyle: Boolean; virtual;
  367. function ScreenToClient(const P: TPoint): TPoint;
  368. end;
  369. PTBItemViewerArray = ^TTBItemViewerArray;
  370. TTBItemViewerArray = array[0..$7FFFFFFF div SizeOf(TTBItemViewer)-1] of TTBItemViewer;
  371. TTBViewOrientation = (tbvoHorizontal, tbvoVertical, tbvoFloating);
  372. TTBEnterToolbarLoopOptions = set of (tbetMouseDown, tbetExecuteSelected,
  373. tbetFromMSAA);
  374. TTBViewState = set of (vsModal, vsMouseInWindow, vsDrawInOrder, vsOppositePopup,
  375. vsIgnoreFirstMouseUp, vsShowAccels, vsDropDownMenus, vsNoAnimation);
  376. TTBViewStyle = set of (vsMenuBar, vsUseHiddenAccels, vsAlwaysShowHints);
  377. TTBViewTimerID = (tiOpen, tiClose, tiScrollUp, tiScrollDown);
  378. TTBViewClass = class of TTBView;
  379. TTBView = class(TComponent)
  380. private
  381. FActiveTimers: set of TTBViewTimerID;
  382. FBackgroundColor: TColor;
  383. FBaseSize: TPoint;
  384. FCapture: Boolean;
  385. FCaptureWnd: HWND;
  386. FChevronOffset: Integer;
  387. FChevronParentView: TTBView;
  388. FChevronSize: Integer;
  389. FCurParentItem: TTBCustomItem;
  390. FCustomizing: Boolean;
  391. FDoneActionData: TTBDoneActionData;
  392. FInternalViewersAtEnd: Integer;
  393. FInternalViewersAtFront: Integer;
  394. FIsPopup: Boolean;
  395. FIsToolbar: Boolean;
  396. FMaxHeight: Integer;
  397. FMonitorRect: TRect;
  398. FMouseOverSelected: Boolean;
  399. FNewViewersGetHighestPriority: Boolean;
  400. FOpenViewer: TTBItemViewer;
  401. FOpenViewerView: TTBView;
  402. FOpenViewerWindow: TTBPopupWindow;
  403. FParentView: TTBView;
  404. FParentItem: TTBCustomItem;
  405. FPriorityList: TList;
  406. FOrientation: TTBViewOrientation;
  407. FScrollOffset: Integer;
  408. FSelected: TTBItemViewer;
  409. FSelectedViaMouse: Boolean;
  410. FShowDownArrow: Boolean;
  411. FShowUpArrow: Boolean;
  412. FState: TTBViewState;
  413. FStyle: TTBViewStyle;
  414. FUpdating: Integer;
  415. FUsePriorityList: Boolean;
  416. FValidated: Boolean;
  417. FViewerCount: Integer;
  418. FViewers: PTBItemViewerArray;
  419. FWindow: TWinControl;
  420. FWrapOffset: Integer;
  421. procedure DeletingViewer(Viewer: TTBItemViewer);
  422. procedure DrawItem(Viewer: TTBItemViewer; DrawTo: TCanvas; Offscreen: Boolean);
  423. procedure FreeViewers;
  424. procedure ImagesChanged;
  425. function InsertItemViewers(const NewIndex: Integer;
  426. const AItem: TTBCustomItem; const AGroupLevel: Integer;
  427. const AddToPriorityList, TopOfPriorityList: Boolean): Integer;
  428. procedure ItemNotification(Ancestor: TTBCustomItem; Relayed: Boolean;
  429. Action: TTBItemChangedAction; Index: Integer; Item: TTBCustomItem);
  430. procedure LinkNotification(Ancestor: TTBCustomItem; Relayed: Boolean;
  431. Action: TTBItemChangedAction; Index: Integer; Item: TTBCustomItem);
  432. procedure RecreateItemViewer(const I: Integer);
  433. procedure Scroll(ADown: Boolean);
  434. procedure SetCustomizing(Value: Boolean);
  435. procedure SetSelected(Value: TTBItemViewer);
  436. procedure SetUsePriorityList(Value: Boolean);
  437. procedure StartTimer(const ATimer: TTBViewTimerID; const Interval: Integer);
  438. procedure StopAllTimers;
  439. procedure StopTimer(const ATimer: TTBViewTimerID);
  440. procedure UpdateCurParentItem;
  441. protected
  442. FAccObjectInstance: TTBBaseAccObject;
  443. procedure AutoSize(AWidth, AHeight: Integer); virtual;
  444. function CalculatePositions(const CanMoveControls: Boolean;
  445. const AOrientation: TTBViewOrientation;
  446. AWrapOffset, AChevronOffset, AChevronSize: Integer;
  447. var ABaseSize, TotalSize: TPoint;
  448. var AWrappedLines: Integer): Boolean;
  449. procedure DoUpdatePositions(var ASize: TPoint); virtual;
  450. function GetChevronItem: TTBCustomItem; virtual;
  451. procedure GetMargins(AOrientation: TTBViewOrientation; var Margins: TRect);
  452. virtual;
  453. function GetMDIButtonsItem: TTBCustomItem; virtual;
  454. function GetMDISystemMenuItem: TTBCustomItem; virtual;
  455. function GetParentToolbarView: TTBView;
  456. function GetRootView: TTBView;
  457. function HandleWMGetObject(var Message: TMessage): Boolean;
  458. procedure InitiateActions;
  459. procedure KeyDown(var Key: Word; Shift: TShiftState); virtual;
  460. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  461. procedure SetAccelsVisibility(AShowAccels: Boolean);
  462. procedure SetState(AState: TTBViewState);
  463. property DoneActionData: TTBDoneActionData read FDoneActionData write FDoneActionData;
  464. property ShowDownArrow: Boolean read FShowDownArrow; {vb+}
  465. property ShowUpArrow: Boolean read FShowUpArrow; {vb+}
  466. public
  467. constructor CreateView(AOwner: TComponent; AParentView: TTBView;
  468. AParentItem: TTBCustomItem; AWindow: TWinControl;
  469. AIsToolbar, ACustomizing, AUsePriorityList: Boolean); virtual;
  470. destructor Destroy; override;
  471. procedure BeginUpdate;
  472. procedure CancelCapture;
  473. procedure CancelChildPopups;
  474. procedure CancelMode;
  475. procedure CloseChildPopups;
  476. function ContainsView(AView: TTBView): Boolean;
  477. procedure DrawSubitems(ACanvas: TCanvas);
  478. procedure EndModal;
  479. procedure EndModalWithClick(AViewer: TTBItemViewer);
  480. { MP }
  481. procedure EndModalWithHelp(AContextID: Integer); overload;
  482. procedure EndModalWithHelp(HelpKeyword: string); overload;
  483. { /MP }
  484. procedure EndModalWithSystemMenu(AWnd: HWND; AKey: Cardinal);
  485. procedure EndUpdate;
  486. procedure EnterToolbarLoop(Options: TTBEnterToolbarLoopOptions); virtual;
  487. procedure ExecuteSelected(AGivePriority: Boolean);
  488. function Find(Item: TTBCustomItem): TTBItemViewer;
  489. function FirstSelectable: TTBItemViewer;
  490. function GetAccObject: IDispatch;
  491. function GetCaptureWnd: HWND;
  492. function GetFont: TFont; virtual;
  493. procedure GetOffEdgeControlList(const List: TList);
  494. procedure GivePriority(AViewer: TTBItemViewer);
  495. function HighestPriorityViewer: TTBItemViewer;
  496. procedure Invalidate(AViewer: TTBItemViewer);
  497. procedure InvalidatePositions; virtual;
  498. function IndexOf(AViewer: TTBItemViewer): Integer;
  499. function IsModalEnding: Boolean;
  500. function NextSelectable(CurViewer: TTBItemViewer; GoForward: Boolean): TTBItemViewer;
  501. function NextSelectableWithAccel(CurViewer: TTBItemViewer; Key: Char;
  502. RequirePrimaryAccel: Boolean; var IsOnlyItemWithAccel: Boolean): TTBItemViewer;
  503. procedure NotifyFocusEvent;
  504. function OpenChildPopup(const SelectFirstItem: Boolean): Boolean;
  505. procedure RecreateAllViewers;
  506. procedure ScrollSelectedIntoView;
  507. procedure Select(Value: TTBItemViewer; ViaMouse: Boolean);
  508. procedure SetCapture;
  509. procedure TryValidatePositions;
  510. procedure UpdateSelection(const P: PPoint; const AllowNewSelection: Boolean);
  511. function UpdatePositions: TPoint;
  512. procedure ValidatePositions;
  513. function ViewerFromPoint(const P: TPoint): TTBItemViewer;
  514. function GetMonitor: TMonitor; virtual;
  515. property BackgroundColor: TColor read FBackgroundColor write FBackgroundColor;
  516. property BaseSize: TPoint read FBaseSize;
  517. property Capture: Boolean read FCapture;
  518. property ChevronOffset: Integer read FChevronOffset write FChevronOffset;
  519. property ChevronSize: Integer read FChevronSize write FChevronSize;
  520. property Customizing: Boolean read FCustomizing write SetCustomizing;
  521. property IsPopup: Boolean read FIsPopup;
  522. property IsToolbar: Boolean read FIsToolbar;
  523. property MouseOverSelected: Boolean read FMouseOverSelected;
  524. property NewViewersGetHighestPriority: Boolean read FNewViewersGetHighestPriority
  525. write FNewViewersGetHighestPriority;
  526. property ParentView: TTBView read FParentView;
  527. property ParentItem: TTBCustomItem read FParentItem;
  528. property OpenViewer: TTBItemViewer read FOpenViewer;
  529. property OpenViewerView: TTBView read FOpenViewerView;
  530. property Orientation: TTBViewOrientation read FOrientation write FOrientation;
  531. property Selected: TTBItemViewer read FSelected write SetSelected;
  532. property SelectedViaMouse: Boolean read FSelectedViaMouse;
  533. property State: TTBViewState read FState;
  534. property Style: TTBViewStyle read FStyle write FStyle;
  535. property UsePriorityList: Boolean read FUsePriorityList write SetUsePriorityList;
  536. property Viewers: PTBItemViewerArray read FViewers;
  537. property ViewerCount: Integer read FViewerCount;
  538. property Window: TWinControl read FWindow;
  539. property WrapOffset: Integer read FWrapOffset write FWrapOffset;
  540. end;
  541. TTBRootItemClass = class of TTBRootItem;
  542. TTBRootItem = class(TTBCustomItem);
  543. { same as TTBCustomItem, except there's a property editor for it }
  544. TTBItem = class(TTBCustomItem)
  545. published
  546. property Action;
  547. property AutoCheck;
  548. property Caption;
  549. property Checked;
  550. property DisplayMode;
  551. property Enabled;
  552. property GroupIndex;
  553. property HelpContext;
  554. { MP }
  555. property HelpKeyword;
  556. property Hint;
  557. property ImageIndex;
  558. property Images;
  559. property InheritOptions;
  560. property MaskOptions;
  561. property Options;
  562. property RadioItem;
  563. property ShortCut;
  564. property Visible;
  565. property OnClick;
  566. property OnSelect;
  567. end;
  568. TTBGroupItem = class(TTBCustomItem)
  569. public
  570. constructor Create(AOwner: TComponent); override;
  571. published
  572. property InheritOptions;
  573. property LinkSubitems;
  574. property MaskOptions;
  575. property Options;
  576. end;
  577. TTBSubmenuItem = class(TTBCustomItem)
  578. private
  579. function GetDropdownCombo: Boolean;
  580. procedure SetDropdownCombo(Value: Boolean);
  581. public
  582. constructor Create(AOwner: TComponent); override;
  583. published
  584. property Action;
  585. property AutoCheck;
  586. property Caption;
  587. property Checked;
  588. //property DisplayAsToolbar;
  589. property DisplayMode;
  590. property DropdownCombo: Boolean read GetDropdownCombo write SetDropdownCombo default False;
  591. property Enabled;
  592. property GroupIndex;
  593. property HelpContext;
  594. { MP }
  595. property HelpKeyword;
  596. property Hint;
  597. property ImageIndex;
  598. property Images;
  599. property InheritOptions;
  600. property LinkSubitems;
  601. property MaskOptions;
  602. property Options;
  603. property RadioItem;
  604. property ShortCut;
  605. property SubMenuImages;
  606. property Visible;
  607. property OnClick;
  608. property OnPopup;
  609. property OnSelect;
  610. end;
  611. TTBSeparatorItem = class(TTBCustomItem)
  612. private
  613. FBlank: Boolean;
  614. procedure SetBlank(Value: Boolean);
  615. protected
  616. function GetItemViewerClass(AView: TTBView): TTBItemViewerClass; override;
  617. public
  618. constructor Create(AOwner: TComponent); override;
  619. published
  620. property Blank: Boolean read FBlank write SetBlank default False;
  621. property Hint;
  622. property Visible;
  623. end;
  624. TTBSeparatorItemViewer = class(TTBItemViewer)
  625. protected
  626. procedure CalcSize(const Canvas: TCanvas;
  627. var AWidth, AHeight: Integer); override;
  628. procedure Paint(const Canvas: TCanvas; const ClientAreaRect: TRect;
  629. IsSelected, IsPushed, UseDisabledShadow: Boolean); override;
  630. function UsesSameWidth: Boolean; override;
  631. end;
  632. TTBControlItem = class(TTBCustomItem)
  633. private
  634. FControl: TControl;
  635. FDontFreeControl: Boolean;
  636. procedure SetControl(Value: TControl);
  637. protected
  638. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  639. public
  640. constructor Create(AOwner: TComponent); override;
  641. constructor CreateControlItem(AOwner: TComponent; AControl: TControl);
  642. destructor Destroy; override;
  643. property DontFreeControl: Boolean read FDontFreeControl write FDontFreeControl;
  644. published
  645. property Control: TControl read FControl write SetControl;
  646. end;
  647. TTBPopupView = class(TTBView)
  648. protected
  649. procedure AutoSize(AWidth, AHeight: Integer); override;
  650. public
  651. function GetMonitor: TMonitor; override;
  652. function GetFont: TFont; override;
  653. end;
  654. ITBPopupWindow = interface
  655. ['{E45CBE74-1ECF-44CB-B064-6D45B1924708}']
  656. end;
  657. TTBPopupWindow = class(TCustomControl, ITBPopupWindow)
  658. private
  659. FAccelsVisibilitySet: Boolean;
  660. FAnimationDirection: TTBAnimationDirection;
  661. FView: TTBView;
  662. procedure CMHintShow(var Message: TCMHintShow); message CM_HINTSHOW;
  663. procedure CMHintShowPause(var Message: TMessage); message CM_HINTSHOWPAUSE;
  664. procedure CMShowingChanged(var Message: TMessage); message CM_SHOWINGCHANGED;
  665. procedure WMClose(var Message: TWMClose); message WM_CLOSE;
  666. procedure WMEraseBkgnd(var Message: TWMEraseBkgnd); message WM_ERASEBKGND;
  667. procedure WMGetObject(var Message: TMessage); message WM_GETOBJECT;
  668. procedure WMNCCalcSize(var Message: TWMNCCalcSize); message WM_NCCALCSIZE;
  669. procedure WMNCPaint(var Message: TMessage); message WM_NCPAINT;
  670. procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
  671. procedure WMPrint(var Message: TMessage); message WM_PRINT;
  672. procedure WMPrintClient(var Message: TMessage); message WM_PRINTCLIENT;
  673. procedure WMTB2kStepAnimation(var Message: TMessage); message WM_TB2K_STEPANIMATION;
  674. procedure WMTB2kAnimationEnded (var Message: TMessage); message WM_TB2K_ANIMATIONENDED;
  675. protected
  676. procedure CreateParams(var Params: TCreateParams); override;
  677. procedure CreateWnd; override;
  678. procedure DestroyWindowHandle; override;
  679. function GetNCSize: TPoint; dynamic;
  680. function GetViewClass: TTBViewClass; dynamic;
  681. procedure Paint; override;
  682. procedure PaintScrollArrows; virtual;
  683. property AnimationDirection: TTBAnimationDirection read FAnimationDirection;
  684. {MP}
  685. procedure Cancel; dynamic;
  686. public
  687. constructor CreatePopupWindow(AOwner: TComponent; const AParentView: TTBView;
  688. const AItem: TTBCustomItem; const ACustomizing: Boolean; const PopupPoint: TPoint); virtual;
  689. destructor Destroy; override;
  690. procedure BeforeDestruction; override;
  691. property View: TTBView read FView;
  692. end;
  693. ITBItems = interface
  694. ['{A5C0D7CC-3EC4-4090-A0F8-3D03271877EA}']
  695. function GetItems: TTBCustomItem;
  696. end;
  697. TTBItemContainer = class(TComponent, ITBItems)
  698. private
  699. FItem: TTBRootItem;
  700. function GetImages: TCustomImageList;
  701. function GetItems: TTBCustomItem;
  702. procedure SetImages(Value: TCustomImageList);
  703. protected
  704. procedure GetChildren(Proc: TGetChildProc; Root: TComponent); override;
  705. public
  706. constructor Create(AOwner: TComponent); override;
  707. destructor Destroy; override;
  708. property Items: TTBRootItem read FItem;
  709. published
  710. property Images: TCustomImageList read GetImages write SetImages;
  711. end;
  712. TTBPopupMenu = class(TPopupMenu, ITBItems)
  713. private
  714. FItem: TTBRootItem;
  715. //procedure SetItems(Value: TTBCustomItem);
  716. function GetImages: TCustomImageList;
  717. function GetItems: TTBCustomItem;
  718. function GetLinkSubitems: TTBCustomItem;
  719. function GetOptions: TTBItemOptions;
  720. procedure RootItemClick(Sender: TObject);
  721. procedure SetImages(Value: TCustomImageList);
  722. procedure SetLinkSubitems(Value: TTBCustomItem);
  723. procedure SetOptions(Value: TTBItemOptions);
  724. protected
  725. function GetRootItemClass: TTBRootItemClass; dynamic;
  726. procedure SetChildOrder(Child: TComponent; Order: Integer); override;
  727. public
  728. constructor Create(AOwner: TComponent); override;
  729. destructor Destroy; override;
  730. function IsShortCut(var Message: TWMKey): Boolean; override;
  731. procedure Popup(X, Y: Integer); override;
  732. function PopupEx(X, Y: Integer; ReturnClickedItemOnly: Boolean = False): TTBCustomItem;
  733. procedure GetChildren(Proc: TGetChildProc; Root: TComponent); override;
  734. published
  735. property Images: TCustomImageList read GetImages write SetImages;
  736. property Items: TTBRootItem read FItem;
  737. property LinkSubitems: TTBCustomItem read GetLinkSubitems write SetLinkSubitems;
  738. property Options: TTBItemOptions read GetOptions write SetOptions default [];
  739. end;
  740. TTBCustomImageList = class(TImageList)
  741. private
  742. FCheckedImages: TCustomImageList;
  743. FCheckedImagesChangeLink: TChangeLink;
  744. FDisabledImages: TCustomImageList;
  745. FDisabledImagesChangeLink: TChangeLink;
  746. FHotImages: TCustomImageList;
  747. FHotImagesChangeLink: TChangeLink;
  748. FImagesBitmap: TBitmap;
  749. FImagesBitmapMaskColor: TColor;
  750. procedure ChangeImages(var AImageList: TCustomImageList;
  751. Value: TCustomImageList; AChangeLink: TChangeLink);
  752. procedure ImageListChanged(Sender: TObject);
  753. procedure ImagesBitmapChanged(Sender: TObject);
  754. procedure SetCheckedImages(Value: TCustomImageList);
  755. procedure SetDisabledImages(Value: TCustomImageList);
  756. procedure SetHotImages(Value: TCustomImageList);
  757. procedure SetImagesBitmap(Value: TBitmap);
  758. procedure SetImagesBitmapMaskColor(Value: TColor);
  759. protected
  760. procedure DefineProperties(Filer: TFiler); override;
  761. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  762. property CheckedImages: TCustomImageList read FCheckedImages write SetCheckedImages;
  763. property DisabledImages: TCustomImageList read FDisabledImages write SetDisabledImages;
  764. property HotImages: TCustomImageList read FHotImages write SetHotImages;
  765. property ImagesBitmap: TBitmap read FImagesBitmap write SetImagesBitmap;
  766. property ImagesBitmapMaskColor: TColor read FImagesBitmapMaskColor
  767. write SetImagesBitmapMaskColor default clFuchsia;
  768. public
  769. constructor Create(AOwner: TComponent); override;
  770. destructor Destroy; override;
  771. procedure DrawState(Canvas: TCanvas; X, Y, Index: Integer;
  772. Enabled, Selected, Checked: Boolean); virtual;
  773. end;
  774. TTBImageList = class(TTBCustomImageList)
  775. published
  776. property CheckedImages;
  777. property DisabledImages;
  778. property HotImages;
  779. property ImagesBitmap;
  780. property ImagesBitmapMaskColor;
  781. end;
  782. const
  783. {$IFNDEF TB2K_USE_STRICT_O2K_MENU_STYLE}
  784. tbMenuBkColor = clMenu;
  785. tbMenuTextColor = clMenuText;
  786. {$ELSE}
  787. tbMenuBkColor = clBtnFace;
  788. tbMenuTextColor = clBtnText;
  789. {$ENDIF}
  790. tbMenuVerticalMargin = 4;
  791. tbMenuImageTextSpace = 1;
  792. tbMenuLeftTextMargin = 2;
  793. tbMenuRightTextMargin = 3;
  794. tbMenuSeparatorOffset = 12;
  795. tbMenuScrollArrowHeight = 19;
  796. tbDropdownArrowWidth = 8;
  797. tbDropdownArrowMargin = 3;
  798. tbDropdownComboArrowWidth = 11;
  799. tbDropdownComboMargin = 2;
  800. tbLineSpacing = 6;
  801. tbLineSepOffset = 1;
  802. tbDockedLineSepOffset = 4;
  803. WM_TB2K_CLICKITEM = WM_USER + $100;
  804. procedure TBInitToolbarSystemFont;
  805. function GetToolbarFont(PixelsPerInch: Integer): TFont; overload;
  806. function GetToolbarFont(Control: TControl): TFont; overload;
  807. type
  808. TTBModalHandler = class
  809. private
  810. FCreatedWnd: Boolean;
  811. FInited: Boolean;
  812. FWnd: HWND;
  813. FRootPopup: TTBPopupWindow;
  814. procedure WndProc(var Msg: TMessage);
  815. public
  816. constructor Create(AExistingWnd: HWND);
  817. destructor Destroy; override;
  818. procedure Loop(const RootView: TTBView; const AMouseDown, AExecuteSelected,
  819. AFromMSAA, TrackRightButton: Boolean);
  820. property RootPopup: TTBPopupWindow read FRootPopup write FRootPopup;
  821. property Wnd: HWND read FWnd;
  822. class procedure DoLockForegroundWindow(LockCode: Cardinal);
  823. class procedure LockForegroundWindow;
  824. class procedure UnlockForegroundWindow;
  825. end;
  826. function ProcessDoneAction(const DoneActionData: TTBDoneActionData;
  827. const ReturnClickedItemOnly: Boolean): TTBCustomItem;
  828. implementation
  829. uses
  830. MMSYSTEM, TB2Consts, TB2Common, IMM, TB2Acc, Winapi.oleacc, Types, PasTools, Generics.Collections;
  831. var
  832. LastPos: TPoint;
  833. threadvar
  834. ClickWndRefCount: Integer;
  835. ClickWnd: HWND;
  836. ClickList: TList;
  837. type
  838. PItemChangedNotificationData = ^TItemChangedNotificationData;
  839. TItemChangedNotificationData = record
  840. Proc: TTBItemChangedProc;
  841. RefCount: Integer;
  842. end;
  843. TComponentAccess = class(TComponent);
  844. TControlAccess = class(TControl);
  845. const
  846. ViewTimerBaseID = 9000;
  847. MaxGroupLevel = 10;
  848. { Misc. }
  849. var
  850. ShortCutToTextFixes: TStrings = nil;
  851. function ShortCutToTextFix(AShortCut: TShortCut): string;
  852. begin
  853. Result := ShortCutToText(AShortCut);
  854. // WORKAROUND: German keyboard driver is giving wrong name for VK_MULTIPLY
  855. if (AShortCut = ShortCut(VK_MULTIPLY, [])) and
  856. (Result = ' (ZEHNERTASTATUR)') then
  857. begin
  858. Result := ShortCutToText(ShortCut(VK_ADD, []));
  859. Result := StringReplace(Result, '+', '*', []);
  860. end;
  861. end;
  862. procedure DestroyClickWnd;
  863. begin
  864. if ClickWnd <> 0 then begin
  865. Classes.DeallocateHWnd(ClickWnd);
  866. ClickWnd := 0;
  867. end;
  868. FreeAndNil(ClickList);
  869. end;
  870. procedure ReferenceClickWnd;
  871. begin
  872. Inc(ClickWndRefCount);
  873. end;
  874. procedure ReleaseClickWnd;
  875. begin
  876. Dec(ClickWndRefCount);
  877. if ClickWndRefCount = 0 then
  878. DestroyClickWnd;
  879. end;
  880. procedure QueueClick(const AItem: TObject; const AArg: Integer);
  881. { Adds an item to ClickList and posts a message to handle it. AItem must be
  882. either a TTBCustomItem or TTBItemViewer. }
  883. var
  884. I: Integer;
  885. begin
  886. if ClickWnd = 0 then
  887. ClickWnd := Classes.AllocateHWnd(TTBCustomItem.ClickWndProc);
  888. if ClickList = nil then
  889. ClickList := TList.Create;
  890. { Add a new item to ClickList or replace an empty one }
  891. I := ClickList.IndexOf(nil);
  892. if I = -1 then
  893. I := ClickList.Add(AItem)
  894. else
  895. ClickList[I] := AItem;
  896. PostMessage(ClickWnd, WM_TB2K_CLICKITEM, AArg, I);
  897. end;
  898. procedure RemoveFromClickList(const AItem: TObject);
  899. { Any class that potentially calls QueueClick needs to call RemoveFromClickList
  900. before an instance is destroyed to ensure that any references to the
  901. instance still in ClickList are removed. }
  902. var
  903. I: Integer;
  904. begin
  905. if Assigned(ClickList) and Assigned(AItem) then
  906. for I := 0 to ClickList.Count-1 do
  907. if ClickList[I] = AItem then
  908. ClickList[I] := Pointer(1);
  909. { ^ The special value of Pointer(1) is assigned to the item instead of
  910. of nil because we want the index to stay reserved until the
  911. WM_TB2K_CLICKITEM message for the index is processed. We don't want
  912. the WM_TB2K_CLICKITEM message that's still in the queue to later
  913. refer to a different item; this would result in queued clicks being
  914. processed in the wrong order in a case like this:
  915. A.PostClick; B.PostClick; A.Free; C.PostClick;
  916. C's click would end up being processed before A's, because C would
  917. get A's index. }
  918. end;
  919. function ProcessDoneAction(const DoneActionData: TTBDoneActionData;
  920. const ReturnClickedItemOnly: Boolean): TTBCustomItem;
  921. begin
  922. Result := nil;
  923. case DoneActionData.DoneAction of
  924. tbdaNone: ;
  925. tbdaClickItem: begin
  926. if DoneActionData.Sound and NeedToPlaySound('MenuCommand') then
  927. PlaySoundA('MenuCommand', 0, SND_ALIAS or SND_ASYNC or SND_NODEFAULT or SND_NOSTOP);
  928. Result := DoneActionData.ClickItem;
  929. if not ReturnClickedItemOnly then
  930. Result.PostClick;
  931. end;
  932. tbdaOpenSystemMenu: begin
  933. SendMessage(DoneActionData.Wnd, WM_SYSCOMMAND, SC_KEYMENU, DoneActionData.Key);
  934. end;
  935. tbdaHelpContext: begin
  936. { Based on code in TPopupList.WndProc: }
  937. if Assigned(Screen.ActiveForm) and
  938. (biHelp in Screen.ActiveForm.BorderIcons) then
  939. Application.HelpCommand(HELP_CONTEXTPOPUP, DoneActionData.ContextID)
  940. else
  941. Application.HelpContext(DoneActionData.ContextID);
  942. end;
  943. { MP }
  944. tbdaHelpKeyword: begin
  945. Application.HelpKeyword(string(DoneActionData.HelpKeyword));
  946. end;
  947. { /MP }
  948. end;
  949. end;
  950. { TTBItemDataArray routines }
  951. procedure InsertIntoItemArray(var AItems: PTBItemDataArray;
  952. var AItemCount: Integer; NewIndex: Integer; AItem: TTBCustomItem);
  953. begin
  954. ReallocMem(AItems, (AItemCount+1) * SizeOf(AItems[0]));
  955. if NewIndex < AItemCount then
  956. System.Move(AItems[NewIndex], AItems[NewIndex+1],
  957. (AItemCount-NewIndex) * SizeOf(AItems[0]));
  958. AItems[NewIndex].Item := AItem;
  959. Inc(AItemCount);
  960. end;
  961. procedure DeleteFromItemArray(var AItems: PTBItemDataArray;
  962. var AItemCount: Integer; Index: Integer);
  963. begin
  964. Dec(AItemCount);
  965. if Index < AItemCount then
  966. System.Move(AItems[Index+1], AItems[Index],
  967. (AItemCount-Index) * SizeOf(AItems[0]));
  968. ReallocMem(AItems, AItemCount * SizeOf(AItems[0]));
  969. end;
  970. procedure InsertIntoViewerArray(var AItems: PTBItemViewerArray;
  971. var AItemCount: Integer; NewIndex: Integer; AItem: TTBItemViewer);
  972. begin
  973. ReallocMem(AItems, (AItemCount+1) * SizeOf(AItems[0]));
  974. if NewIndex < AItemCount then
  975. System.Move(AItems[NewIndex], AItems[NewIndex+1],
  976. (AItemCount-NewIndex) * SizeOf(AItems[0]));
  977. AItems[NewIndex] := AItem;
  978. Inc(AItemCount);
  979. end;
  980. procedure DeleteFromViewerArray(var AItems: PTBItemViewerArray;
  981. var AItemCount: Integer; Index: Integer);
  982. begin
  983. Dec(AItemCount);
  984. if Index < AItemCount then
  985. System.Move(AItems[Index+1], AItems[Index],
  986. (AItemCount-Index) * SizeOf(AItems[0]));
  987. ReallocMem(AItems, AItemCount * SizeOf(AItems[0]));
  988. end;
  989. { TTBCustomItemActionLink }
  990. procedure TTBCustomItemActionLink.AssignClient(AClient: TObject);
  991. begin
  992. FClient := AClient as TTBCustomItem;
  993. end;
  994. function TTBCustomItemActionLink.IsAutoCheckLinked: Boolean;
  995. begin
  996. Result := (FClient.AutoCheck = (Action as TCustomAction).AutoCheck);
  997. end;
  998. function TTBCustomItemActionLink.IsCaptionLinked: Boolean;
  999. begin
  1000. Result := inherited IsCaptionLinked and
  1001. (FClient.Caption = (Action as TCustomAction).Caption);
  1002. end;
  1003. function TTBCustomItemActionLink.IsCheckedLinked: Boolean;
  1004. begin
  1005. Result := inherited IsCheckedLinked and
  1006. (FClient.Checked = (Action as TCustomAction).Checked);
  1007. end;
  1008. function TTBCustomItemActionLink.IsEnabledLinked: Boolean;
  1009. begin
  1010. Result := inherited IsEnabledLinked and
  1011. (FClient.Enabled = (Action as TCustomAction).Enabled);
  1012. end;
  1013. function TTBCustomItemActionLink.IsHelpContextLinked: Boolean;
  1014. begin
  1015. Result := inherited IsHelpContextLinked and
  1016. (FClient.HelpContext = (Action as TCustomAction).HelpContext);
  1017. end;
  1018. { MP }
  1019. function TTBCustomItemActionLink.IsHelpLinked: Boolean;
  1020. begin
  1021. Result := inherited IsHelpLinked and
  1022. (FClient.HelpContext = (Action as TCustomAction).HelpContext) and
  1023. (FClient.HelpKeyword = (Action as TCustomAction).HelpKeyword){ and
  1024. (FClient.HelpType = (Action as TCustomAction).HelpType);} // TODO
  1025. end;
  1026. { /MP }
  1027. function TTBCustomItemActionLink.IsHintLinked: Boolean;
  1028. begin
  1029. Result := inherited IsHintLinked and
  1030. (FClient.Hint = (Action as TCustomAction).Hint);
  1031. end;
  1032. function TTBCustomItemActionLink.IsImageIndexLinked: Boolean;
  1033. begin
  1034. Result := inherited IsImageIndexLinked and
  1035. (FClient.ImageIndex = (Action as TCustomAction).ImageIndex);
  1036. end;
  1037. function TTBCustomItemActionLink.IsShortCutLinked: Boolean;
  1038. begin
  1039. Result := inherited IsShortCutLinked and
  1040. (FClient.ShortCut = (Action as TCustomAction).ShortCut);
  1041. end;
  1042. function TTBCustomItemActionLink.IsVisibleLinked: Boolean;
  1043. begin
  1044. Result := inherited IsVisibleLinked and
  1045. (FClient.Visible = (Action as TCustomAction).Visible);
  1046. end;
  1047. function TTBCustomItemActionLink.IsOnExecuteLinked: Boolean;
  1048. begin
  1049. Result := inherited IsOnExecuteLinked and
  1050. MethodsEqual(TMethod(FClient.OnClick), TMethod(Action.OnExecute));
  1051. end;
  1052. procedure TTBCustomItemActionLink.SetAutoCheck(Value: Boolean);
  1053. begin
  1054. if IsAutoCheckLinked then FClient.AutoCheck := Value;
  1055. end;
  1056. procedure TTBCustomItemActionLink.SetCaption(const Value: string);
  1057. begin
  1058. if IsCaptionLinked then FClient.Caption := Value;
  1059. end;
  1060. procedure TTBCustomItemActionLink.SetChecked(Value: Boolean);
  1061. begin
  1062. if IsCheckedLinked then FClient.Checked := Value;
  1063. end;
  1064. procedure TTBCustomItemActionLink.SetEnabled(Value: Boolean);
  1065. begin
  1066. if IsEnabledLinked then FClient.Enabled := Value;
  1067. end;
  1068. procedure TTBCustomItemActionLink.SetHelpContext(Value: THelpContext);
  1069. begin
  1070. if { MP } IsHelpLinked { /MP } then FClient.HelpContext := Value;
  1071. end;
  1072. { MP }
  1073. procedure TTBCustomItemActionLink.SetHelpKeyword(const Value: String);
  1074. begin
  1075. if IsHelpLinked then FClient.HelpKeyword := Value;
  1076. end;
  1077. { /MP }
  1078. procedure TTBCustomItemActionLink.SetHint(const Value: string);
  1079. begin
  1080. if IsHintLinked then FClient.Hint := Value;
  1081. end;
  1082. procedure TTBCustomItemActionLink.SetImageIndex(Value: Integer);
  1083. begin
  1084. if IsImageIndexLinked then FClient.ImageIndex := Value;
  1085. end;
  1086. procedure TTBCustomItemActionLink.SetShortCut(Value: TShortCut);
  1087. begin
  1088. if IsShortCutLinked then FClient.ShortCut := Value;
  1089. end;
  1090. procedure TTBCustomItemActionLink.SetVisible(Value: Boolean);
  1091. begin
  1092. if IsVisibleLinked then FClient.Visible := Value;
  1093. end;
  1094. procedure TTBCustomItemActionLink.SetOnExecute(Value: TNotifyEvent);
  1095. begin
  1096. if IsOnExecuteLinked then FClient.OnClick := Value;
  1097. end;
  1098. { TTBCustomItem }
  1099. {}function ItemContainingItems(const AItem: TTBCustomItem): TTBCustomItem;
  1100. begin
  1101. if Assigned(AItem) and Assigned(AItem.FLinkSubitems) then
  1102. Result := AItem.FLinkSubitems
  1103. else
  1104. Result := AItem;
  1105. end;
  1106. constructor TTBCustomItem.Create(AOwner: TComponent);
  1107. begin
  1108. inherited;
  1109. FEnabled := True;
  1110. FImageIndex := -1;
  1111. FInheritOptions := True;
  1112. FItemStyle := [tbisSelectable, tbisRedrawOnSelChange, tbisRedrawOnMouseOverChange];
  1113. FVisible := True;
  1114. ReferenceClickWnd;
  1115. end;
  1116. destructor TTBCustomItem.Destroy;
  1117. var
  1118. I: Integer;
  1119. begin
  1120. Destroying;
  1121. RemoveFromClickList(Self);
  1122. { Changed in 0.33. Moved FParent.Remove call *after* the child items are
  1123. deleted. }
  1124. for I := Count-1 downto 0 do
  1125. Items[I].Free;
  1126. if Assigned(FParent) then
  1127. FParent.Remove(Self);
  1128. FreeMem(FItems);
  1129. FActionLink.Free;
  1130. FActionLink := nil;
  1131. FreeAndNil(FSubMenuImagesChangeLink);
  1132. FreeAndNil(FImagesChangeLink);
  1133. inherited;
  1134. if Assigned(FNotifyList) then begin
  1135. for I := FNotifyList.Count-1 downto 0 do
  1136. Dispose(PItemChangedNotificationData(FNotifyList[I]));
  1137. FNotifyList.Free;
  1138. end;
  1139. FLinkParents.Free;
  1140. ReleaseClickWnd;
  1141. end;
  1142. function TTBCustomItem.IsAutoCheckStored: Boolean;
  1143. begin
  1144. Result := (ActionLink = nil) or not FActionLink.IsAutoCheckLinked;
  1145. end;
  1146. function TTBCustomItem.IsCaptionStored: Boolean;
  1147. begin
  1148. Result := (ActionLink = nil) or not FActionLink.IsCaptionLinked;
  1149. end;
  1150. function TTBCustomItem.IsCheckedStored: Boolean;
  1151. begin
  1152. Result := (ActionLink = nil) or not FActionLink.IsCheckedLinked;
  1153. end;
  1154. function TTBCustomItem.IsEnabledStored: Boolean;
  1155. begin
  1156. Result := (ActionLink = nil) or not FActionLink.IsEnabledLinked;
  1157. end;
  1158. function TTBCustomItem.IsHintStored: Boolean;
  1159. begin
  1160. Result := (ActionLink = nil) or not FActionLink.IsHintLinked;
  1161. end;
  1162. function TTBCustomItem.IsHelpContextStored: Boolean;
  1163. begin
  1164. { MP }
  1165. Result := (ActionLink = nil) or not FActionLink.IsHelpLinked;
  1166. end;
  1167. function TTBCustomItem.IsImageIndexStored: Boolean;
  1168. begin
  1169. Result := (ActionLink = nil) or not FActionLink.IsImageIndexLinked;
  1170. end;
  1171. function TTBCustomItem.IsShortCutStored: Boolean;
  1172. begin
  1173. Result := (ActionLink = nil) or not FActionLink.IsShortCutLinked;
  1174. end;
  1175. function TTBCustomItem.IsVisibleStored: Boolean;
  1176. begin
  1177. Result := (ActionLink = nil) or not FActionLink.IsVisibleLinked;
  1178. end;
  1179. function TTBCustomItem.IsOnClickStored: Boolean;
  1180. begin
  1181. Result := (ActionLink = nil) or not FActionLink.IsOnExecuteLinked;
  1182. end;
  1183. function TTBCustomItem.GetAction: TBasicAction;
  1184. begin
  1185. if FActionLink <> nil then
  1186. Result := FActionLink.Action
  1187. else
  1188. Result := nil;
  1189. end;
  1190. function TTBCustomItem.GetActionLinkClass: TTBCustomItemActionLinkClass;
  1191. begin
  1192. Result := TTBCustomItemActionLink;
  1193. end;
  1194. procedure TTBCustomItem.DoActionChange(Sender: TObject);
  1195. begin
  1196. if Sender = Action then ActionChange(Sender, False);
  1197. end;
  1198. procedure TTBCustomItem.ActionChange(Sender: TObject; CheckDefaults: Boolean);
  1199. begin
  1200. if Action is TCustomAction then
  1201. with TCustomAction(Sender) do
  1202. begin
  1203. if not CheckDefaults or (Self.AutoCheck = False) then
  1204. Self.AutoCheck := AutoCheck;
  1205. if not CheckDefaults or (Self.Caption = '') then
  1206. Self.Caption := Caption;
  1207. if not CheckDefaults or (Self.Checked = False) then
  1208. Self.Checked := Checked;
  1209. if not CheckDefaults or (Self.Enabled = True) then
  1210. Self.Enabled := Enabled;
  1211. if not CheckDefaults or (Self.HelpContext = 0) then
  1212. Self.HelpContext := HelpContext;
  1213. { MP }
  1214. if not CheckDefaults or (Self.HelpKeyword = '') then
  1215. Self.HelpKeyword := HelpKeyword;
  1216. { /MP }
  1217. if not CheckDefaults or (Self.Hint = '') then
  1218. Self.Hint := Hint;
  1219. if not CheckDefaults or (Self.ImageIndex = -1) then
  1220. Self.ImageIndex := ImageIndex;
  1221. if not CheckDefaults or (Self.ShortCut = scNone) then
  1222. Self.ShortCut := ShortCut;
  1223. if not CheckDefaults or (Self.Visible = True) then
  1224. Self.Visible := Visible;
  1225. if not CheckDefaults or not Assigned(Self.OnClick) then
  1226. Self.OnClick := OnExecute;
  1227. end;
  1228. end;
  1229. procedure TTBCustomItem.SetAction(Value: TBasicAction);
  1230. begin
  1231. if Value = nil then begin
  1232. FActionLink.Free;
  1233. FActionLink := nil;
  1234. end
  1235. else begin
  1236. if FActionLink = nil then
  1237. FActionLink := GetActionLinkClass.Create(Self);
  1238. FActionLink.Action := Value;
  1239. FActionLink.OnChange := DoActionChange;
  1240. { Note: Delphi's Controls.pas and Menus.pas merely check for
  1241. "csLoading in Value.ComponentState" here. But that doesn't help when
  1242. the Action property references an action on another form / data module
  1243. that has already finished loading. So we check two things:
  1244. 1. csLoading in Value.ComponentState
  1245. 2. csLoading in ComponentState
  1246. In the typical case where the item and action list reside on the same
  1247. form, #1 and #2 are both true.
  1248. Only #1 is true when Action references an action on another form / data
  1249. module that is created *after* the item (e.g. if Form1.TBItem1.Action =
  1250. Form2.Action1, and Form1 is created before Form2).
  1251. Only #2 is true when Action references an action on another form / data
  1252. module that is created *before* the item (e.g. if Form2.TBItem1.Action =
  1253. Form1.Action1, and Form1 is created before Form2). }
  1254. ActionChange(Value, (csLoading in Value.ComponentState) or
  1255. (csLoading in ComponentState));
  1256. Value.FreeNotification(Self);
  1257. end;
  1258. end;
  1259. procedure TTBCustomItem.InitiateAction;
  1260. begin
  1261. if FActionLink <> nil then FActionLink.Update;
  1262. end;
  1263. procedure TTBCustomItem.Loaded;
  1264. begin
  1265. inherited;
  1266. if Action <> nil then ActionChange(Action, True);
  1267. end;
  1268. procedure TTBCustomItem.GetChildren(Proc: TGetChildProc; Root: TComponent);
  1269. var
  1270. I: Integer;
  1271. begin
  1272. for I := 0 to FItemCount-1 do
  1273. Proc(FItems[I].Item);
  1274. end;
  1275. procedure TTBCustomItem.SetChildOrder(Child: TComponent; Order: Integer);
  1276. var
  1277. I: Integer;
  1278. begin
  1279. I := IndexOf(Child as TTBCustomItem);
  1280. if I <> -1 then
  1281. Move(I, Order);
  1282. end;
  1283. function TTBCustomItem.HasParent: Boolean;
  1284. begin
  1285. Result := True;
  1286. end;
  1287. function TTBCustomItem.GetParentComponent: TComponent;
  1288. begin
  1289. if (FParent <> nil) and (FParent.FParentComponent <> nil) then
  1290. Result := FParent.FParentComponent
  1291. else
  1292. Result := FParent;
  1293. end;
  1294. function TTBCustomItem.GetTopComponent: TComponent;
  1295. begin
  1296. if Parent <> nil then Result := Parent.GetTopComponent
  1297. else Result := FParentComponent;
  1298. end;
  1299. procedure TTBCustomItem.SetName(const NewName: TComponentName);
  1300. begin
  1301. if Name <> NewName then begin
  1302. inherited;
  1303. if Assigned(FParent) then
  1304. FParent.Notify(tbicNameChanged, -1, Self);
  1305. end;
  1306. end;
  1307. procedure TTBCustomItem.SetParentComponent(Value: TComponent);
  1308. var
  1309. Intf: ITBItems;
  1310. begin
  1311. if FParent <> nil then FParent.Remove(Self);
  1312. if Value <> nil then begin
  1313. if Value is TTBCustomItem then
  1314. TTBCustomItem(Value).Add(Self)
  1315. else if Value.GetInterface(ITBItems, Intf) then
  1316. Intf.GetItems.Add(Self);
  1317. end;
  1318. end;
  1319. procedure TTBCustomItem.Notification(AComponent: TComponent;
  1320. Operation: TOperation);
  1321. begin
  1322. inherited;
  1323. if Operation = opRemove then begin
  1324. RemoveFromList(FLinkParents, AComponent);
  1325. if AComponent = Action then Action := nil;
  1326. if AComponent = Images then Images := nil;
  1327. if AComponent = SubMenuImages then SubMenuImages := nil;
  1328. if AComponent = LinkSubitems then LinkSubitems := nil;
  1329. end;
  1330. end;
  1331. procedure TTBCustomItem.IndexError;
  1332. begin
  1333. raise ETBItemError.Create(STBToolbarIndexOutOfBounds);
  1334. end;
  1335. class procedure TTBCustomItem.ClickWndProc(var Message: TMessage);
  1336. var
  1337. List: TList;
  1338. I: Integer;
  1339. Item: TObject;
  1340. begin
  1341. if Message.Msg = WM_TB2K_CLICKITEM then begin
  1342. List := ClickList; { optimization... }
  1343. if Assigned(List) then begin
  1344. I := Message.LParam;
  1345. if (I >= 0) and (I < List.Count) then begin
  1346. Item := List[I];
  1347. List[I] := nil;
  1348. if Item = Pointer(1) then { is it a destroyed item? }
  1349. Item := nil;
  1350. end
  1351. else
  1352. Item := nil;
  1353. { Remove trailing nil items from ClickList. This is not *necessary*, but
  1354. it will make RemoveFromClickList faster if we clean out items that
  1355. aren't used, and may never be used again. }
  1356. for I := List.Count-1 downto 0 do begin
  1357. if List[I] = nil then
  1358. List.Delete(I)
  1359. else
  1360. Break;
  1361. end;
  1362. if Assigned(Item) then begin
  1363. try
  1364. if Item is TTBCustomItem then
  1365. TTBCustomItem(Item).Click
  1366. else if Item is TTBItemViewer then
  1367. TTBItemViewer(Item).AccSelect(Message.WParam <> 0);
  1368. except
  1369. Application.HandleException(Item);
  1370. end;
  1371. end;
  1372. end;
  1373. end
  1374. else
  1375. with Message do
  1376. Result := DefWindowProc(ClickWnd, Msg, wParam, lParam);
  1377. end;
  1378. procedure TTBCustomItem.PostClick;
  1379. { Posts a message to the message queue that causes the item's Click handler to
  1380. be executed when control is returned to the message loop.
  1381. This should be called instead of Click when a WM_SYSCOMMAND message is
  1382. (possibly) currently being handled, because TApplication.WndProc's
  1383. CM_APPSYSCOMMAND handler disables the VCL's processing of focus messages
  1384. until the Perform(WM_SYSCOMMAND, ...) call returns. (An OnClick handler which
  1385. calls TForm.ShowModal needs focus messages to be enabled or else the form
  1386. will be shown with no initial focus.) }
  1387. begin
  1388. QueueClick(Self, 0);
  1389. end;
  1390. procedure TTBCustomItem.Click;
  1391. begin
  1392. if Enabled then begin
  1393. { Following code based on D6's TMenuItem.Click }
  1394. if (not Assigned(ActionLink) and AutoCheck) or
  1395. (Assigned(ActionLink) and not ActionLink.IsAutoCheckLinked and AutoCheck) then
  1396. Checked := not Checked;
  1397. { Following code based on D4's TControl.Click }
  1398. { Call OnClick if assigned and not equal to associated action's OnExecute.
  1399. If associated action's OnExecute assigned then call it, otherwise, call
  1400. OnClick. }
  1401. if Assigned(FOnClick) and (Action <> nil) and
  1402. not MethodsEqual(TMethod(FOnClick), TMethod(Action.OnExecute)) then
  1403. FOnClick(Self)
  1404. else
  1405. if not(csDesigning in ComponentState) and (ActionLink <> nil) then
  1406. ActionLink.Execute(Self)
  1407. else
  1408. if Assigned(FOnClick) then
  1409. FOnClick(Self);
  1410. end;
  1411. end;
  1412. function TTBCustomItem.GetItem(Index: Integer): TTBCustomItem;
  1413. begin
  1414. if (Index < 0) or (Index >= FItemCount) then IndexError;
  1415. Result := FItems[Index].Item;
  1416. end;
  1417. procedure TTBCustomItem.Add(AItem: TTBCustomItem);
  1418. begin
  1419. Insert(Count, AItem);
  1420. end;
  1421. procedure TTBCustomItem.InternalNotify(Ancestor: TTBCustomItem;
  1422. NestingLevel: Integer; Action: TTBItemChangedAction; Index: Integer;
  1423. Item: TTBCustomItem);
  1424. { Note: Ancestor is Item's parent, or in the case of a group item relayed
  1425. notification, it can also be a group item which *links* to Item's parent
  1426. (i.e. ItemContainingItems(Ancestor) = Item.Parent). }
  1427. procedure RelayToParentOf(const AItem: TTBCustomItem);
  1428. begin
  1429. if NestingLevel > MaxGroupLevel then
  1430. Exit;
  1431. if (tbisEmbeddedGroup in AItem.ItemStyle) and Assigned(AItem.Parent) then begin
  1432. if Ancestor = Self then
  1433. AItem.Parent.InternalNotify(AItem, NestingLevel + 1, Action, Index, Item)
  1434. else
  1435. { Don't alter Ancestor on subsequent relays; only on the first. }
  1436. AItem.Parent.InternalNotify(Ancestor, NestingLevel + 1, Action, Index, Item);
  1437. end;
  1438. end;
  1439. var
  1440. I: Integer;
  1441. P: TTBCustomItem;
  1442. SaveProc: TTBItemChangedProc;
  1443. begin
  1444. { If Self is a group item, relay the notification to the parent }
  1445. RelayToParentOf(Self);
  1446. { If any group items are linked to Self, relay the notification to
  1447. those items' parents }
  1448. if Assigned(FLinkParents) then
  1449. for I := 0 to FLinkParents.Count-1 do begin
  1450. P := FLinkParents[I];
  1451. if P <> Parent then
  1452. RelayToParentOf(P);
  1453. end;
  1454. if Assigned(FNotifyList) then begin
  1455. I := 0;
  1456. while I < FNotifyList.Count do begin
  1457. with PItemChangedNotificationData(FNotifyList[I])^ do begin
  1458. SaveProc := Proc;
  1459. Proc(Ancestor, Ancestor <> Self, Action, Index, Item);
  1460. end;
  1461. { Is I now out of bounds? }
  1462. if I >= FNotifyList.Count then
  1463. Break;
  1464. { Only proceed to the next index if the list didn't change }
  1465. if MethodsEqual(TMethod(PItemChangedNotificationData(FNotifyList[I])^.Proc),
  1466. TMethod(SaveProc)) then
  1467. Inc(I);
  1468. end;
  1469. end;
  1470. end;
  1471. procedure TTBCustomItem.Notify(Action: TTBItemChangedAction; Index: Integer;
  1472. Item: TTBCustomItem);
  1473. begin
  1474. InternalNotify(Self, 0, Action, Index, Item);
  1475. end;
  1476. procedure TTBCustomItem.ViewBeginUpdate;
  1477. begin
  1478. Notify(tbicSubitemsBeginUpdate, -1, nil);
  1479. end;
  1480. procedure TTBCustomItem.ViewEndUpdate;
  1481. begin
  1482. Notify(tbicSubitemsEndUpdate, -1, nil);
  1483. end;
  1484. procedure TTBCustomItem.Insert(NewIndex: Integer; AItem: TTBCustomItem);
  1485. begin
  1486. if Assigned(AItem.FParent) then
  1487. raise ETBItemError.Create(STBToolbarItemReinserted);
  1488. if (NewIndex < 0) or (NewIndex > FItemCount) then IndexError;
  1489. InsertIntoItemArray(FItems, FItemCount, NewIndex, AItem);
  1490. AItem.FParent := Self;
  1491. ViewBeginUpdate;
  1492. try
  1493. Notify(tbicInserted, NewIndex, AItem);
  1494. AItem.RefreshOptions;
  1495. finally
  1496. ViewEndUpdate;
  1497. end;
  1498. end;
  1499. procedure TTBCustomItem.Delete(Index: Integer);
  1500. begin
  1501. if (Index < 0) or (Index >= FItemCount) then IndexError;
  1502. Notify(tbicDeleting, Index, FItems[Index].Item);
  1503. FItems[Index].Item.FParent := nil;
  1504. DeleteFromItemArray(FItems, FItemCount, Index);
  1505. end;
  1506. function TTBCustomItem.IndexOf(AItem: TTBCustomItem): Integer;
  1507. var
  1508. I: Integer;
  1509. begin
  1510. for I := 0 to FItemCount-1 do
  1511. if FItems[I].Item = AItem then begin
  1512. Result := I;
  1513. Exit;
  1514. end;
  1515. Result := -1;
  1516. end;
  1517. procedure TTBCustomItem.Remove(Item: TTBCustomItem);
  1518. var
  1519. I: Integer;
  1520. begin
  1521. I := IndexOf(Item);
  1522. //if I = -1 then raise ETBItemError.Create(STBToolbarItemNotFound);
  1523. if I <> -1 then
  1524. Delete(I);
  1525. end;
  1526. procedure TTBCustomItem.Clear;
  1527. var
  1528. I: Integer;
  1529. begin
  1530. for I := Count-1 downto 0 do
  1531. Items[I].Free;
  1532. end;
  1533. procedure TTBCustomItem.Move(CurIndex, NewIndex: Integer);
  1534. var
  1535. Item: TTBCustomItem;
  1536. begin
  1537. if CurIndex <> NewIndex then begin
  1538. if (NewIndex < 0) or (NewIndex >= FItemCount) then IndexError;
  1539. Item := Items[CurIndex];
  1540. ViewBeginUpdate;
  1541. try
  1542. Delete(CurIndex);
  1543. Insert(NewIndex, Item);
  1544. finally
  1545. ViewEndUpdate;
  1546. end;
  1547. end;
  1548. end;
  1549. function TTBCustomItem.ContainsItem(AItem: TTBCustomItem): Boolean;
  1550. begin
  1551. while Assigned(AItem) and (AItem <> Self) do
  1552. AItem := AItem.Parent;
  1553. Result := Assigned(AItem);
  1554. end;
  1555. procedure TTBCustomItem.RegisterNotification(ANotify: TTBItemChangedProc);
  1556. var
  1557. I: Integer;
  1558. Data: PItemChangedNotificationData;
  1559. begin
  1560. if FNotifyList = nil then FNotifyList := TList.Create;
  1561. for I := 0 to FNotifyList.Count-1 do
  1562. with PItemChangedNotificationData(FNotifyList[I])^ do
  1563. if MethodsEqual(TMethod(ANotify), TMethod(Proc)) then begin
  1564. Inc(RefCount);
  1565. Exit;
  1566. end;
  1567. FNotifyList.Expand;
  1568. New(Data);
  1569. Data.Proc := ANotify;
  1570. Data.RefCount := 1;
  1571. FNotifyList.Add(Data);
  1572. end;
  1573. procedure TTBCustomItem.UnregisterNotification(ANotify: TTBItemChangedProc);
  1574. var
  1575. I: Integer;
  1576. Data: PItemChangedNotificationData;
  1577. begin
  1578. if Assigned(FNotifyList) then
  1579. for I := 0 to FNotifyList.Count-1 do begin
  1580. Data := FNotifyList[I];
  1581. if MethodsEqual(TMethod(Data.Proc), TMethod(ANotify)) then begin
  1582. Dec(Data.RefCount);
  1583. if Data.RefCount = 0 then begin
  1584. FNotifyList.Delete(I);
  1585. Dispose(Data);
  1586. if FNotifyList.Count = 0 then begin
  1587. FNotifyList.Free;
  1588. FNotifyList := nil;
  1589. end;
  1590. end;
  1591. Break;
  1592. end;
  1593. end;
  1594. end;
  1595. function TTBCustomItem.GetPopupWindowClass: TTBPopupWindowClass;
  1596. begin
  1597. Result := TTBPopupWindow;
  1598. end;
  1599. procedure TTBCustomItem.DoPopup(Sender: TTBCustomItem; FromLink: Boolean);
  1600. begin
  1601. if Assigned(FOnPopup) then
  1602. FOnPopup(Sender, FromLink);
  1603. if not(tbisCombo in ItemStyle) then
  1604. Click;
  1605. end;
  1606. var
  1607. PlayedSound: Boolean = False;
  1608. procedure TTBCustomItem.GetPopupPosition(ParentView: TTBView;
  1609. PopupWindow: TTBPopupWindow; var PopupPositionRec: TTBPopupPositionRec);
  1610. var
  1611. X2, Y2: Integer;
  1612. RepeatCalcX: Boolean;
  1613. function CountObscured(X, Y, W, H: Integer): Integer;
  1614. var
  1615. I: Integer;
  1616. P: TPoint;
  1617. V: TTBItemViewer;
  1618. begin
  1619. Result := 0;
  1620. if ParentView = nil then
  1621. Exit;
  1622. P := ParentView.FWindow.ClientToScreen(Point(0, 0));
  1623. Dec(X, P.X);
  1624. Dec(Y, P.Y);
  1625. Inc(W, X);
  1626. Inc(H, Y);
  1627. for I := 0 to ParentView.FViewerCount-1 do begin
  1628. V := ParentView.FViewers[I];
  1629. if V.Show and (V.BoundsRect.Left >= X) and (V.BoundsRect.Right <= W) and
  1630. (V.BoundsRect.Top >= Y) and (V.BoundsRect.Bottom <= H) then
  1631. Inc(Result);
  1632. end;
  1633. end;
  1634. begin
  1635. with PopupPositionRec do
  1636. begin
  1637. { Adjust the Y position of the popup window }
  1638. { If the window is going off the bottom of the monitor, try placing it
  1639. above the parent item }
  1640. if (Y + H > MonitorRect.Bottom) and
  1641. ((ParentView = nil) or (ParentView.FOrientation <> tbvoVertical)) then begin
  1642. if not PositionAsSubmenu then
  1643. Y2 := ParentItemRect.Top
  1644. else
  1645. Y2 := ParentItemRect.Bottom + NCSizeY;
  1646. Dec(Y2, H);
  1647. { Only place it above the parent item if it isn't going to go off the
  1648. top of the monitor }
  1649. if Y2 >= MonitorRect.Top then
  1650. Y := Y2;
  1651. end;
  1652. { If it's still going off the bottom (which can be possible if a menu bar
  1653. was off the screen to begin with), clip it to the bottom of the monitor }
  1654. if Y + H > MonitorRect.Bottom then
  1655. Y := MonitorRect.Bottom - H;
  1656. if Y < MonitorRect.Top then
  1657. Y := MonitorRect.Top;
  1658. { Other adjustments to the position of the popup window }
  1659. if not PositionAsSubmenu then begin
  1660. if (ParentView = nil) and (Alignment = tbpaRight) and (X < MonitorRect.Left) then
  1661. Inc(X, W);
  1662. if X + W > MonitorRect.Right then begin
  1663. if Assigned(ParentView) or (Alignment <> tbpaLeft) then
  1664. X := MonitorRect.Right;
  1665. Dec(X, W);
  1666. end;
  1667. if X < MonitorRect.Left then
  1668. X := MonitorRect.Left;
  1669. if (ParentView = nil) or (ParentView.FOrientation <> tbvoVertical) then begin
  1670. Y2 := ParentItemRect.Top - H;
  1671. if Y2 >= MonitorRect.Top then begin
  1672. { Would the popup window obscure less items if it popped out to the
  1673. top instead? }
  1674. if (CountObscured(X, Y2, W, H) < CountObscured(X, Y, W, H)) or
  1675. ((Y < ParentItemRect.Bottom) and (Y + H > ParentItemRect.Top) and
  1676. (X < ParentItemRect.Right) and (X + W > ParentItemRect.Left)) then
  1677. Y := Y2;
  1678. end;
  1679. { Make sure a tall popup window doesn't overlap the parent item }
  1680. if (Y < ParentItemRect.Bottom) and (Y + H > ParentItemRect.Top) and
  1681. (X < ParentItemRect.Right) and (X + W > ParentItemRect.Left) then begin
  1682. if ParentItemRect.Right + W <= MonitorRect.Right then
  1683. X := ParentItemRect.Right
  1684. else
  1685. X := ParentItemRect.Left - W;
  1686. if X < MonitorRect.Top then
  1687. X := MonitorRect.Top;
  1688. end;
  1689. end
  1690. else begin
  1691. X2 := ParentItemRect.Right;
  1692. if X2 + W <= MonitorRect.Right then begin
  1693. { Would the popup window obscure less items if it popped out to the
  1694. right instead? }
  1695. if (CountObscured(X2, Y, W, H) < CountObscured(X, Y, W, H)) or
  1696. ((Y < ParentItemRect.Bottom) and (Y + H > ParentItemRect.Top) and
  1697. (X < ParentItemRect.Right) and (X + W > ParentItemRect.Left)) then
  1698. X := X2;
  1699. end;
  1700. { Make sure a wide popup window doesn't overlap the parent item }
  1701. if (Y < ParentItemRect.Bottom) and (Y + H > ParentItemRect.Top) and
  1702. (X < ParentItemRect.Right) and (X + W > ParentItemRect.Left) then begin
  1703. if ParentItemRect.Bottom + H <= MonitorRect.Bottom then
  1704. Y := ParentItemRect.Bottom
  1705. else
  1706. Y := ParentItemRect.Top - H;
  1707. if Y < MonitorRect.Top then
  1708. Y := MonitorRect.Top;
  1709. end;
  1710. end;
  1711. end
  1712. else begin
  1713. { Make nested submenus go from left to right on the screen. Each it
  1714. runs out of space on the screen, switch directions }
  1715. repeat
  1716. RepeatCalcX := False;
  1717. X2 := X;
  1718. if Opposite or (X2 + W > MonitorRect.Right) then begin
  1719. if Assigned(ParentView) then
  1720. X2 := ParentItemRect.Left + NCSizeX;
  1721. Dec(X2, W);
  1722. if not Opposite then
  1723. Include(PopupWindow.View.FState, vsOppositePopup)
  1724. else begin
  1725. if X2 < MonitorRect.Left then begin
  1726. Opposite := False;
  1727. RepeatCalcX := True;
  1728. end
  1729. else
  1730. Include(PopupWindow.View.FState, vsOppositePopup);
  1731. end;
  1732. end;
  1733. until not RepeatCalcX;
  1734. X := X2;
  1735. if X < MonitorRect.Left then
  1736. X := MonitorRect.Left;
  1737. end;
  1738. { Determine animation direction }
  1739. AnimDir := [];
  1740. if not PositionAsSubmenu then begin
  1741. if Y >= ParentItemRect.Bottom then
  1742. Include(AnimDir, tbadDown)
  1743. else if Y + H <= ParentItemRect.Top then
  1744. Include(AnimDir, tbadUp);
  1745. if X >= ParentItemRect.Right then
  1746. Include(AnimDir, tbadRight)
  1747. else if X + W <= ParentItemRect.Left then
  1748. Include(AnimDir, tbadLeft);
  1749. end
  1750. else begin
  1751. if X + W div 2 >= ParentItemRect.Left + (ParentItemRect.Right - ParentItemRect.Left) div 2 then
  1752. Include(AnimDir, tbadRight)
  1753. else
  1754. Include(AnimDir, tbadLeft);
  1755. end;
  1756. end;
  1757. end;
  1758. function TTBCustomItem.CreatePopup(const ParentView: TTBView;
  1759. const ParentViewer: TTBItemViewer; const PositionAsSubmenu, SelectFirstItem,
  1760. Customizing: Boolean; const APopupPoint: TPoint;
  1761. const Alignment: TTBPopupAlignment): TTBPopupWindow;
  1762. var
  1763. EventItem, ParentItem: TTBCustomItem;
  1764. Opposite: Boolean;
  1765. ChevronParentView: TTBView;
  1766. X, Y, W, H: Integer;
  1767. P: TPoint;
  1768. ParentItemRect: TRect;
  1769. MonitorRect: TRect;
  1770. PopupRec: TTBPopupPositionRec;
  1771. NCSize: TPoint;
  1772. begin
  1773. EventItem := ItemContainingItems(Self);
  1774. if EventItem <> Self then
  1775. EventItem.DoPopup(Self, True);
  1776. DoPopup(Self, False);
  1777. ChevronParentView := GetChevronParentView;
  1778. if ChevronParentView = nil then
  1779. ParentItem := Self
  1780. else
  1781. ParentItem := ChevronParentView.FParentItem;
  1782. Opposite := Assigned(ParentView) and (vsOppositePopup in ParentView.FState);
  1783. Result := GetPopupWindowClass.CreatePopupWindow(nil, ParentView, ParentItem,
  1784. Customizing, APopupPoint);
  1785. try
  1786. if Assigned(ChevronParentView) then begin
  1787. ChevronParentView.FreeNotification(Result.View);
  1788. Result.View.FChevronParentView := ChevronParentView;
  1789. Result.View.FIsToolbar := True;
  1790. Result.View.Style := Result.View.Style +
  1791. (ChevronParentView.Style * [vsAlwaysShowHints]);
  1792. Result.Color := clBtnFace;
  1793. end;
  1794. { Calculate ParentItemRect, and MonitorRect (the rectangle of the monitor
  1795. that the popup window will be confined to) }
  1796. if Assigned(ParentView) then begin
  1797. ParentView.ValidatePositions;
  1798. ParentItemRect := ParentViewer.BoundsRect;
  1799. P := ParentView.FWindow.ClientToScreen(Point(0, 0));
  1800. OffsetRect(ParentItemRect, P.X, P.Y);
  1801. if not IsRectEmpty(ParentView.FMonitorRect) then
  1802. MonitorRect := ParentView.FMonitorRect
  1803. else
  1804. { MP (display menu on correct monitor) }
  1805. MonitorRect := GetRectOfMonitorContainingRect(ParentItemRect, True);
  1806. {MonitorRect := GetRectOfMonitorContainingPoint(APopupPoint, False);} {vb-}
  1807. { MP }
  1808. {MonitorRect := GetRectOfMonitorContainingPoint(APopupPoint, True);} {vb+}
  1809. end
  1810. else begin
  1811. ParentItemRect.TopLeft := APopupPoint;
  1812. ParentItemRect.BottomRight := APopupPoint;
  1813. {MonitorRect := GetRectOfMonitorContainingPoint(APopupPoint, False);} {vb-}
  1814. MonitorRect := GetRectOfMonitorContainingPoint(APopupPoint, True); {vb+}
  1815. end;
  1816. Result.View.FMonitorRect := MonitorRect;
  1817. { Initialize item positions and size of the popup window }
  1818. NCSize := Result.GetNCSize;
  1819. if ChevronParentView = nil then
  1820. Result.View.FMaxHeight := (MonitorRect.Bottom - MonitorRect.Top) -
  1821. (NCSize.Y * 2)
  1822. else
  1823. Result.View.WrapOffset := (MonitorRect.Right - MonitorRect.Left) -
  1824. (NCSize.X * 2);
  1825. if SelectFirstItem then
  1826. Result.View.Selected := Result.View.FirstSelectable;
  1827. Result.View.UpdatePositions;
  1828. W := Result.Width;
  1829. H := Result.Height;
  1830. { Calculate initial X,Y position of the popup window }
  1831. if Assigned(ParentView) then begin
  1832. if not PositionAsSubmenu then begin
  1833. if ChevronParentView = nil then begin
  1834. if (ParentView = nil) or (ParentView.FOrientation <> tbvoVertical) then begin
  1835. if GetSystemMetrics(SM_MENUDROPALIGNMENT) = 0 then
  1836. X := ParentItemRect.Left
  1837. else
  1838. X := ParentItemRect.Right - W;
  1839. Y := ParentItemRect.Bottom;
  1840. end
  1841. else begin
  1842. X := ParentItemRect.Left - W;
  1843. Y := ParentItemRect.Top;
  1844. end;
  1845. end
  1846. else begin
  1847. if ChevronParentView.FOrientation <> tbvoVertical then begin
  1848. X := ParentItemRect.Right - W;
  1849. Y := ParentItemRect.Bottom;
  1850. end
  1851. else begin
  1852. X := ParentItemRect.Left - W;
  1853. Y := ParentItemRect.Top;
  1854. end;
  1855. end;
  1856. end
  1857. else begin
  1858. X := ParentItemRect.Right - NCSize.X;
  1859. Y := ParentItemRect.Top - NCSize.Y;
  1860. end;
  1861. end
  1862. else begin
  1863. X := APopupPoint.X;
  1864. Y := APopupPoint.Y;
  1865. case Alignment of
  1866. tbpaRight: Dec(X, W);
  1867. tbpaCenter: Dec(X, W div 2);
  1868. end;
  1869. end;
  1870. PopupRec.PositionAsSubmenu := PositionAsSubmenu;
  1871. PopupRec.Alignment := Alignment;
  1872. PopupRec.Opposite := Opposite;
  1873. PopupRec.MonitorRect := MonitorRect;
  1874. PopupRec.ParentItemRect := ParentItemRect;
  1875. PopupRec.NCSizeX := NCSize.X;
  1876. PopupRec.NCSizeY := NCSize.Y;
  1877. PopupRec.X := X;
  1878. PopupRec.Y := Y;
  1879. PopupRec.W := W;
  1880. PopupRec.H := H;
  1881. PopupRec.AnimDir := [];
  1882. PopupRec.PlaySound := True;
  1883. GetPopupPosition(ParentView, Result, PopupRec);
  1884. X := PopupRec.X;
  1885. Y := PopupRec.Y;
  1886. W := PopupRec.W;
  1887. H := PopupRec.H;
  1888. Result.FAnimationDirection := PopupRec.AnimDir;
  1889. Result.SetBounds(X, Y, W, H);
  1890. if Assigned(ParentView) then begin
  1891. Result.FreeNotification(ParentView);
  1892. ParentView.FOpenViewerWindow := Result;
  1893. ParentView.FOpenViewerView := Result.View;
  1894. ParentView.FOpenViewer := ParentViewer;
  1895. if ParentView.FIsToolbar then begin
  1896. Include(ParentView.FState, vsDropDownMenus);
  1897. ParentView.Invalidate(ParentViewer);
  1898. ParentView.FWindow.Update;
  1899. end;
  1900. end;
  1901. Include(Result.View.FState, vsDrawInOrder);
  1902. if not PopupRec.PlaySound or not NeedToPlaySound('MenuPopup') then begin
  1903. { Don't call PlaySound if we don't have to }
  1904. Result.Visible := True;
  1905. end
  1906. else begin
  1907. if not PlayedSound then begin
  1908. { Work around Windows 2000 "bug" where there's a 1/3 second delay upon the
  1909. first call to PlaySound (or sndPlaySound) by painting the window
  1910. completely first. This way the delay isn't very noticable. }
  1911. PlayedSound := True;
  1912. Result.Visible := True;
  1913. Result.Update;
  1914. PlaySoundA('MenuPopup', 0, SND_ALIAS or SND_ASYNC or SND_NODEFAULT or SND_NOSTOP);
  1915. end
  1916. else begin
  1917. PlaySoundA('MenuPopup', 0, SND_ALIAS or SND_ASYNC or SND_NODEFAULT or SND_NOSTOP);
  1918. Result.Visible := True;
  1919. end;
  1920. end;
  1921. NotifyWinEvent(EVENT_SYSTEM_MENUPOPUPSTART, Result.View.FWindow.Handle,
  1922. OBJID_CLIENT, CHILDID_SELF);
  1923. { Call NotifyFocusEvent now that the window is visible }
  1924. if Assigned(Result.View.Selected) then
  1925. Result.View.NotifyFocusEvent;
  1926. except
  1927. Result.Free;
  1928. raise;
  1929. end;
  1930. end;
  1931. function TTBCustomItem.OpenPopup(const SelectFirstItem, TrackRightButton: Boolean;
  1932. const PopupPoint: TPoint; const Alignment: TTBPopupAlignment;
  1933. const ReturnClickedItemOnly: Boolean; PositionAsSubmenu: Boolean): TTBCustomItem;
  1934. var
  1935. ModalHandler: TTBModalHandler;
  1936. Popup: TTBPopupWindow;
  1937. DoneActionData: TTBDoneActionData;
  1938. begin
  1939. ModalHandler := TTBModalHandler.Create(0);
  1940. try
  1941. Popup := CreatePopup(nil, nil, PositionAsSubmenu, SelectFirstItem, False, PopupPoint,
  1942. Alignment);
  1943. try
  1944. Include(Popup.View.FState, vsIgnoreFirstMouseUp);
  1945. ModalHandler.RootPopup := Popup;
  1946. ModalHandler.Loop(Popup.View, False, False, False, TrackRightButton);
  1947. DoneActionData := Popup.View.FDoneActionData;
  1948. finally
  1949. ModalHandler.RootPopup := nil;
  1950. { Remove vsModal state from the root view before any TTBView.Destroy
  1951. methods get called, so that NotifyFocusEvent becomes a no-op }
  1952. Exclude(Popup.View.FState, vsModal);
  1953. Popup.Free;
  1954. end;
  1955. finally
  1956. ModalHandler.Free;
  1957. end;
  1958. Result := ProcessDoneAction(DoneActionData, ReturnClickedItemOnly);
  1959. end;
  1960. function TTBCustomItem.Popup(X, Y: Integer; TrackRightButton: Boolean;
  1961. Alignment: TTBPopupAlignment = tbpaLeft;
  1962. ReturnClickedItemOnly: Boolean = False;
  1963. PositionAsSubmenu: Boolean = False): TTBCustomItem;
  1964. var
  1965. P: TPoint;
  1966. begin
  1967. P.X := X;
  1968. P.Y := Y;
  1969. Result := OpenPopup(False, TrackRightButton, P, Alignment,
  1970. ReturnClickedItemOnly, PositionAsSubmenu);
  1971. end;
  1972. function TTBCustomItem.FindItemWithShortCut(AShortCut: TShortCut;
  1973. var ATopmostParent: TTBCustomItem): TTBCustomItem;
  1974. function DoItem(AParentItem: TTBCustomItem; LinkDepth: Integer): TTBCustomItem;
  1975. var
  1976. I: Integer;
  1977. NewParentItem, Item: TTBCustomItem;
  1978. begin
  1979. Result := nil;
  1980. NewParentItem := AParentItem;
  1981. if Assigned(NewParentItem.LinkSubitems) then begin
  1982. NewParentItem := NewParentItem.LinkSubitems;
  1983. Inc(LinkDepth);
  1984. if LinkDepth > 25 then
  1985. Exit; { prevent infinite link recursion }
  1986. end;
  1987. for I := 0 to NewParentItem.Count-1 do begin
  1988. Item := NewParentItem.Items[I];
  1989. if Item.ShortCut = AShortCut then begin
  1990. Result := Item;
  1991. Exit;
  1992. end;
  1993. Result := DoItem(Item, LinkDepth);
  1994. if Assigned(Result) then begin
  1995. ATopmostParent := Item;
  1996. Exit;
  1997. end;
  1998. end;
  1999. end;
  2000. begin
  2001. ATopmostParent := nil;
  2002. Result := DoItem(Self, 0);
  2003. end;
  2004. function TTBCustomItem.IsShortCut(var Message: TWMKey): Boolean;
  2005. var
  2006. ShortCut: TShortCut;
  2007. ShiftState: TShiftState;
  2008. ShortCutItem, TopmostItem, Item, EventItem: TTBCustomItem;
  2009. I: Integer;
  2010. label 1;
  2011. begin
  2012. Result := False;
  2013. ShiftState := KeyDataToShiftState(Message.KeyData);
  2014. ShortCut := Menus.ShortCut(Message.CharCode, ShiftState);
  2015. 1:ShortCutItem := FindItemWithShortCut(ShortCut, TopmostItem);
  2016. if Assigned(ShortCutItem) then begin
  2017. { Send OnPopup/OnClick events to ShortCutItem's parents so that they can
  2018. update the Enabled state of ShortCutItem if needed }
  2019. Item := Self;
  2020. repeat
  2021. if not Item.Enabled then
  2022. Exit;
  2023. EventItem := ItemContainingItems(Item);
  2024. if not(csDesigning in ComponentState) then begin
  2025. for I := 0 to EventItem.Count-1 do
  2026. EventItem.Items[I].InitiateAction;
  2027. end;
  2028. if not(tbisEmbeddedGroup in Item.ItemStyle) then begin
  2029. if EventItem <> Item then begin
  2030. try
  2031. EventItem.DoPopup(Item, True);
  2032. except
  2033. Application.HandleException(Self);
  2034. end;
  2035. end;
  2036. try
  2037. Item.DoPopup(Item, False);
  2038. except
  2039. Application.HandleException(Self);
  2040. end;
  2041. end;
  2042. ShortCutItem := Item.FindItemWithShortCut(ShortCut, TopmostItem);
  2043. if ShortCutItem = nil then
  2044. { Can no longer find the shortcut inside TopmostItem. Start over
  2045. because the shortcut might have moved. }
  2046. goto 1;
  2047. Item := TopmostItem;
  2048. until Item = nil;
  2049. if ShortCutItem.Enabled then begin
  2050. try
  2051. ShortCutItem.Click;
  2052. except
  2053. Application.HandleException(Self);
  2054. end;
  2055. Result := True;
  2056. end;
  2057. end;
  2058. end;
  2059. function TTBCustomItem.GetChevronParentView: TTBView;
  2060. begin
  2061. Result := nil;
  2062. end;
  2063. function TTBCustomItem.GetItemViewerClass(AView: TTBView): TTBItemViewerClass;
  2064. begin
  2065. Result := TTBItemViewer;
  2066. end;
  2067. function TTBCustomItem.NeedToRecreateViewer(AViewer: TTBItemViewer): Boolean;
  2068. begin
  2069. Result := False;
  2070. end;
  2071. function TTBCustomItem.GetShortCutText: String;
  2072. var
  2073. P: Integer;
  2074. begin
  2075. P := Pos(#9, Caption);
  2076. if P = 0 then begin
  2077. if ShortCut <> 0 then
  2078. Result := ShortCutToTextFix(ShortCut)
  2079. else
  2080. Result := '';
  2081. end
  2082. else
  2083. Result := Copy(Caption, P+1, Maxint);
  2084. end;
  2085. procedure TTBCustomItem.Change(NeedResize: Boolean);
  2086. const
  2087. ItemChangedActions: array[Boolean] of TTBItemChangedAction =
  2088. (tbicInvalidate, tbicInvalidateAndResize);
  2089. begin
  2090. if Assigned(FParent) then
  2091. FParent.Notify(ItemChangedActions[NeedResize], -1, Self);
  2092. end;
  2093. procedure TTBCustomItem.RecreateItemViewers;
  2094. begin
  2095. if Assigned(FParent) then
  2096. FParent.Notify(tbicRecreateItemViewers, -1, Self);
  2097. end;
  2098. procedure TTBCustomItem.ImageListChangeHandler(Sender: TObject);
  2099. var
  2100. Resize: Boolean;
  2101. begin
  2102. if Sender = FSubMenuImages then begin
  2103. FSubMenuImagesChangeLink.FLastWidth := FSubMenuImages.Width;
  2104. FSubMenuImagesChangeLink.FLastHeight := FSubMenuImages.Height;
  2105. SubMenuImagesChanged;
  2106. end
  2107. else begin
  2108. { Sender is FImages }
  2109. Resize := False;
  2110. if (FImagesChangeLink.FLastWidth <> FImages.Width) or
  2111. (FImagesChangeLink.FLastHeight <> FImages.Height) then begin
  2112. FImagesChangeLink.FLastWidth := FImages.Width;
  2113. FImagesChangeLink.FLastHeight := FImages.Height;
  2114. Resize := True;
  2115. end;
  2116. Change(Resize);
  2117. end;
  2118. end;
  2119. procedure TTBCustomItem.SubMenuImagesChanged;
  2120. begin
  2121. Notify(tbicSubMenuImagesChanged, -1, nil);
  2122. end;
  2123. procedure TTBCustomItem.TurnSiblingsOff;
  2124. var
  2125. I: Integer;
  2126. Item: TTBCustomItem;
  2127. begin
  2128. if (GroupIndex <> 0) and Assigned(FParent) then begin
  2129. for I := 0 to FParent.Count-1 do begin
  2130. Item := FParent[I];
  2131. if (Item <> Self) and (Item.GroupIndex = GroupIndex) then
  2132. Item.Checked := False;
  2133. end;
  2134. end;
  2135. end;
  2136. procedure TTBCustomItem.SetCaption(Value: String);
  2137. begin
  2138. if FCaption <> Value then begin
  2139. FCaption := Value;
  2140. Change(True);
  2141. end;
  2142. end;
  2143. procedure TTBCustomItem.SetChecked(Value: Boolean);
  2144. begin
  2145. if FChecked <> Value then begin
  2146. FChecked := Value;
  2147. Change(False);
  2148. if Value then
  2149. TurnSiblingsOff;
  2150. end;
  2151. end;
  2152. procedure TTBCustomItem.SetDisplayMode(Value: TTBItemDisplayMode);
  2153. begin
  2154. if FDisplayMode <> Value then begin
  2155. FDisplayMode := Value;
  2156. Change(True);
  2157. end;
  2158. end;
  2159. procedure TTBCustomItem.EnabledChanged;
  2160. begin
  2161. Change(False);
  2162. end;
  2163. procedure TTBCustomItem.SetEnabled(Value: Boolean);
  2164. begin
  2165. if FEnabled <> Value then begin
  2166. FEnabled := Value;
  2167. EnabledChanged;
  2168. end;
  2169. end;
  2170. procedure TTBCustomItem.SetGroupIndex(Value: Integer);
  2171. begin
  2172. if FGroupIndex <> Value then begin
  2173. FGroupIndex := Value;
  2174. if Checked then
  2175. TurnSiblingsOff;
  2176. end;
  2177. end;
  2178. procedure TTBCustomItem.SetImageIndex(Value: TImageIndex);
  2179. var
  2180. HadNoImage: Boolean;
  2181. begin
  2182. if FImageIndex <> Value then begin
  2183. HadNoImage := FImageIndex = -1;
  2184. FImageIndex := Value;
  2185. Change(HadNoImage xor (Value = -1));
  2186. end;
  2187. end;
  2188. function TTBCustomItem.ChangeImages(var AImages: TCustomImageList;
  2189. const Value: TCustomImageList; var AChangeLink: TTBImageChangeLink): Boolean;
  2190. { Returns True if image list was resized }
  2191. var
  2192. LastWidth, LastHeight: Integer;
  2193. begin
  2194. Result := False;
  2195. LastWidth := -1;
  2196. LastHeight := -1;
  2197. if Assigned(AImages) then begin
  2198. LastWidth := AImages.Width;
  2199. LastHeight := AImages.Height;
  2200. AImages.UnregisterChanges(AChangeLink);
  2201. if Value = nil then begin
  2202. AChangeLink.Free;
  2203. AChangeLink := nil;
  2204. Result := True;
  2205. end;
  2206. end;
  2207. AImages := Value;
  2208. if Assigned(Value) then begin
  2209. Result := (Value.Width <> LastWidth) or (Value.Height <> LastHeight);
  2210. if AChangeLink = nil then begin
  2211. AChangeLink := TTBImageChangeLink.Create;
  2212. AChangeLink.FLastWidth := Value.Width;
  2213. AChangeLink.FLastHeight := Value.Height;
  2214. AChangeLink.OnChange := ImageListChangeHandler;
  2215. end;
  2216. Value.RegisterChanges(AChangeLink);
  2217. Value.FreeNotification(Self);
  2218. end;
  2219. end;
  2220. procedure TTBCustomItem.SetImages(Value: TCustomImageList);
  2221. begin
  2222. if FImages <> Value then
  2223. Change(ChangeImages(FImages, Value, FImagesChangeLink));
  2224. end;
  2225. procedure TTBCustomItem.SetSubMenuImages(Value: TCustomImageList);
  2226. begin
  2227. if FSubMenuImages <> Value then begin
  2228. ChangeImages(FSubMenuImages, Value, FSubMenuImagesChangeLink);
  2229. SubMenuImagesChanged;
  2230. end;
  2231. end;
  2232. procedure TTBCustomItem.SetInheritOptions(Value: Boolean);
  2233. begin
  2234. if FInheritOptions <> Value then begin
  2235. FInheritOptions := Value;
  2236. RefreshOptions;
  2237. end;
  2238. end;
  2239. procedure TTBCustomItem.SetLinkSubitems(Value: TTBCustomItem);
  2240. begin
  2241. if Value = Self then
  2242. Value := nil;
  2243. if FLinkSubitems <> Value then begin
  2244. if Assigned(FLinkSubitems) then
  2245. RemoveFromList(FLinkSubitems.FLinkParents, Self);
  2246. FLinkSubitems := Value;
  2247. if Assigned(Value) then begin
  2248. Value.FreeNotification(Self);
  2249. AddToList(Value.FLinkParents, Self);
  2250. end;
  2251. Notify(tbicSubitemsChanged, -1, nil);
  2252. end;
  2253. end;
  2254. function TTBCustomItem.FixOptions(const AOptions: TTBItemOptions): TTBItemOptions;
  2255. begin
  2256. Result := AOptions;
  2257. if not(tboToolbarStyle in Result) then
  2258. Exclude(Result, tboToolbarSize);
  2259. end;
  2260. procedure TTBCustomItem.RefreshOptions;
  2261. const
  2262. NonInheritedOptions = [tboDefault];
  2263. ChangeOptions = [tboDefault, tboDropdownArrow, tboImageAboveCaption,
  2264. tboNoRotation, tboSameWidth, tboToolbarStyle, tboToolbarSize];
  2265. var
  2266. OldOptions, NewOptions: TTBItemOptions;
  2267. I: Integer;
  2268. Item: TTBCustomItem;
  2269. begin
  2270. OldOptions := FEffectiveOptions;
  2271. if FInheritOptions and Assigned(FParent) then
  2272. NewOptions := FParent.FEffectiveOptions - NonInheritedOptions
  2273. else
  2274. NewOptions := [];
  2275. NewOptions := FixOptions(NewOptions - FMaskOptions + FOptions);
  2276. if FEffectiveOptions <> NewOptions then begin
  2277. FEffectiveOptions := NewOptions;
  2278. if (OldOptions * ChangeOptions) <> (NewOptions * ChangeOptions) then
  2279. Change(True);
  2280. for I := 0 to FItemCount-1 do begin
  2281. Item := FItems[I].Item;
  2282. if Item.FInheritOptions then
  2283. Item.RefreshOptions;
  2284. end;
  2285. end;
  2286. end;
  2287. procedure TTBCustomItem.SetMaskOptions(Value: TTBItemOptions);
  2288. begin
  2289. if FMaskOptions <> Value then begin
  2290. FMaskOptions := Value;
  2291. RefreshOptions;
  2292. end;
  2293. end;
  2294. procedure TTBCustomItem.SetOptions(Value: TTBItemOptions);
  2295. begin
  2296. Value := FixOptions(Value);
  2297. if FOptions <> Value then begin
  2298. FOptions := Value;
  2299. RefreshOptions;
  2300. end;
  2301. end;
  2302. procedure TTBCustomItem.SetRadioItem(Value: Boolean);
  2303. begin
  2304. if FRadioItem <> Value then begin
  2305. FRadioItem := Value;
  2306. Change(False);
  2307. end;
  2308. end;
  2309. procedure TTBCustomItem.SetVisible(Value: Boolean);
  2310. begin
  2311. if FVisible <> Value then begin
  2312. FVisible := Value;
  2313. Change(True);
  2314. end;
  2315. end;
  2316. procedure TTBCustomItem.ChangeScale(M, D: Integer);
  2317. var
  2318. I: Integer;
  2319. begin
  2320. for I := 0 to Count - 1 do
  2321. begin
  2322. Items[I].ChangeScale(M, D);
  2323. end;
  2324. end;
  2325. { TTBGroupItem }
  2326. constructor TTBGroupItem.Create(AOwner: TComponent);
  2327. begin
  2328. inherited;
  2329. ItemStyle := ItemStyle + [tbisEmbeddedGroup, tbisSubitemsEditable];
  2330. end;
  2331. { TTBSubmenuItem }
  2332. constructor TTBSubmenuItem.Create(AOwner: TComponent);
  2333. begin
  2334. inherited;
  2335. ItemStyle := ItemStyle + [tbisSubMenu, tbisSubitemsEditable];
  2336. end;
  2337. function TTBSubmenuItem.GetDropdownCombo: Boolean;
  2338. begin
  2339. Result := tbisCombo in ItemStyle;
  2340. end;
  2341. procedure TTBSubmenuItem.SetDropdownCombo(Value: Boolean);
  2342. begin
  2343. if (tbisCombo in ItemStyle) <> Value then begin
  2344. if Value then
  2345. ItemStyle := ItemStyle + [tbisCombo]
  2346. else
  2347. ItemStyle := ItemStyle - [tbisCombo];
  2348. Change(True);
  2349. end;
  2350. end;
  2351. { TTBSeparatorItem }
  2352. constructor TTBSeparatorItem.Create(AOwner: TComponent);
  2353. begin
  2354. inherited;
  2355. ItemStyle := ItemStyle - [tbisSelectable, tbisRedrawOnSelChange,
  2356. tbisRedrawOnMouseOverChange] + [tbisSeparator, tbisClicksTransparent];
  2357. end;
  2358. function TTBSeparatorItem.GetItemViewerClass(AView: TTBView): TTBItemViewerClass;
  2359. begin
  2360. Result := TTBSeparatorItemViewer;
  2361. end;
  2362. procedure TTBSeparatorItem.SetBlank(Value: Boolean);
  2363. begin
  2364. if FBlank <> Value then begin
  2365. FBlank := Value;
  2366. Change(False);
  2367. end;
  2368. end;
  2369. { TTBSeparatorItemViewer }
  2370. procedure TTBSeparatorItemViewer.CalcSize(const Canvas: TCanvas;
  2371. var AWidth, AHeight: Integer);
  2372. begin
  2373. if not IsToolbarStyle then
  2374. Inc(AHeight, DivRoundUp(GetTextHeight(Canvas.Handle) * 2, 3))
  2375. else begin
  2376. AWidth := 6;
  2377. AHeight := 6;
  2378. end;
  2379. end;
  2380. procedure TTBSeparatorItemViewer.Paint(const Canvas: TCanvas;
  2381. const ClientAreaRect: TRect; IsSelected, IsPushed, UseDisabledShadow: Boolean);
  2382. var
  2383. DC: HDC;
  2384. R: TRect;
  2385. ToolbarStyle, Horiz, LineSep: Boolean;
  2386. begin
  2387. DC := Canvas.Handle;
  2388. if TTBSeparatorItem(Item).FBlank then
  2389. Exit;
  2390. R := ClientAreaRect;
  2391. ToolbarStyle := IsToolbarStyle;
  2392. Horiz := not ToolbarStyle or (View.FOrientation = tbvoVertical);
  2393. LineSep := tbisLineSep in State;
  2394. if LineSep then
  2395. Horiz := not Horiz;
  2396. if Horiz then begin
  2397. R.Top := R.Bottom div 2 - 1;
  2398. if not ToolbarStyle then
  2399. InflateRect(R, -tbMenuSeparatorOffset, 0)
  2400. else if LineSep then begin
  2401. if View.FOrientation = tbvoFloating then
  2402. InflateRect(R, -tbLineSepOffset, 0)
  2403. else
  2404. InflateRect(R, -tbDockedLineSepOffset, 0);
  2405. end;
  2406. DrawEdge(DC, R, EDGE_ETCHED, BF_TOP);
  2407. end
  2408. else begin
  2409. R.Left := R.Right div 2 - 1;
  2410. if LineSep then
  2411. InflateRect(R, 0, -tbDockedLineSepOffset);
  2412. DrawEdge(DC, R, EDGE_ETCHED, BF_LEFT);
  2413. end;
  2414. end;
  2415. function TTBSeparatorItemViewer.UsesSameWidth: Boolean;
  2416. begin
  2417. Result := False;
  2418. end;
  2419. { TTBControlItem }
  2420. constructor TTBControlItem.Create(AOwner: TComponent);
  2421. begin
  2422. inherited;
  2423. ItemStyle := ItemStyle - [tbisSelectable] + [tbisClicksTransparent];
  2424. end;
  2425. constructor TTBControlItem.CreateControlItem(AOwner: TComponent;
  2426. AControl: TControl);
  2427. begin
  2428. FControl := AControl;
  2429. AControl.FreeNotification(Self);
  2430. Create(AOwner);
  2431. end;
  2432. destructor TTBControlItem.Destroy;
  2433. begin
  2434. inherited;
  2435. { Free the associated control *after* the item is completely destroyed }
  2436. if not FDontFreeControl and Assigned(FControl) and
  2437. not(csAncestor in FControl.ComponentState) then
  2438. FControl.Free;
  2439. end;
  2440. procedure TTBControlItem.Notification(AComponent: TComponent;
  2441. Operation: TOperation);
  2442. begin
  2443. inherited;
  2444. if (Operation = opRemove) and (AComponent = FControl) then
  2445. Control := nil;
  2446. end;
  2447. procedure TTBControlItem.SetControl(Value: TControl);
  2448. begin
  2449. if FControl <> Value then begin
  2450. FControl := Value;
  2451. if Assigned(Value) then
  2452. Value.FreeNotification(Self);
  2453. Change(True);
  2454. end;
  2455. end;
  2456. { TTBItemViewer }
  2457. constructor TTBItemViewer.Create(AView: TTBView; AItem: TTBCustomItem;
  2458. AGroupLevel: Integer);
  2459. begin
  2460. FItem := AItem;
  2461. FView := AView;
  2462. FGroupLevel := AGroupLevel;
  2463. ReferenceClickWnd;
  2464. end;
  2465. destructor TTBItemViewer.Destroy;
  2466. begin
  2467. RemoveFromClickList(Self);
  2468. if Assigned(FAccObjectInstance) then begin
  2469. FAccObjectInstance.ClientIsDestroying;
  2470. FAccObjectInstance := nil;
  2471. end;
  2472. inherited;
  2473. ReleaseClickWnd;
  2474. end;
  2475. function TTBItemViewer.GetAccObject: IDispatch;
  2476. begin
  2477. if FAccObjectInstance = nil then begin
  2478. FAccObjectInstance := TTBItemViewerAccObject.Create(Self);
  2479. end;
  2480. Result := FAccObjectInstance;
  2481. end;
  2482. procedure TTBItemViewer.AccSelect(const AExecute: Boolean);
  2483. { Called by ClickWndProc when an item of type TTBItemViewer is in ClickList }
  2484. var
  2485. Obj: IDispatch;
  2486. begin
  2487. { Ensure FAccObjectInstance is created by calling GetAccObject }
  2488. Obj := GetAccObject;
  2489. if Assigned(Obj) then
  2490. (FAccObjectInstance as TTBItemViewerAccObject).HandleAccSelect(AExecute);
  2491. end;
  2492. procedure TTBItemViewer.PostAccSelect(const AExecute: Boolean);
  2493. { Internally called by TTBItemViewerAccObject. Don't call directly. }
  2494. begin
  2495. QueueClick(Self, Ord(AExecute));
  2496. end;
  2497. function TTBItemViewer.IsAccessible: Boolean;
  2498. { Returns True if MSAA clients should know about the viewer, specifically
  2499. if it's either shown, off-edge, or clipped (in other words, not completely
  2500. invisible/inaccessible). }
  2501. begin
  2502. { Note: Can't simply check Item.Visible because the chevron item's Visible
  2503. property is always True }
  2504. Result := Show or OffEdge or Clipped;
  2505. end;
  2506. function TTBItemViewer.GetCaptionText: String;
  2507. var
  2508. P: Integer;
  2509. begin
  2510. Result := Item.Caption;
  2511. P := Pos(#9, Result);
  2512. if P <> 0 then
  2513. SetLength(Result, P-1);
  2514. { MP }
  2515. if IsToolbarStyle and not (vsMenuBar in View.Style) then
  2516. Result := StripAccelChars(StripTrailingPunctuation(Result), True);
  2517. end;
  2518. function TTBItemViewer.GetHintText: String;
  2519. var
  2520. P: Integer;
  2521. LongHint: string;
  2522. HintStyleCaption: string;
  2523. begin
  2524. if Pos('|', Item.Hint) > 0 then
  2525. begin
  2526. Result := GetShortHint(Item.Hint);
  2527. LongHint := GetLongHint(Item.Hint);
  2528. end
  2529. else
  2530. begin
  2531. LongHint := Item.Hint;
  2532. end;
  2533. HintStyleCaption := StripAccelChars(StripTrailingPunctuation(GetCaptionText));
  2534. { If there is no short hint, use the caption for the hint. Like Office,
  2535. strip any trailing colon or ellipsis. }
  2536. if (Result = '') and (not(tboNoAutoHint in Item.EffectiveOptions) or (LongHint <> '')) and
  2537. (not(tbisSubmenu in Item.ItemStyle) or (tbisCombo in Item.ItemStyle) or
  2538. not CaptionShown) then
  2539. Result := HintStyleCaption;
  2540. { Call associated action's OnHint event handler to post-process the hint }
  2541. if Assigned(Item.ActionLink) and
  2542. (Item.ActionLink.Action is TCustomAction) then begin
  2543. if not TCustomAction(Item.ActionLink.Action).DoHint(Result) then
  2544. Result := '';
  2545. { Note: TControlActionLink.DoShowHint actually misinterprets the result
  2546. of DoHint, but we get it right... }
  2547. end;
  2548. if Result = '' then
  2549. Result := LongHint;
  2550. // "Select all" and "Select All" are still the same
  2551. if SameText(LongHint, Result) then
  2552. LongHint := '';
  2553. if CaptionShown and (LongHint = '') and SameText(Result, HintStyleCaption) then
  2554. Result := '';
  2555. { Add shortcut text }
  2556. if (Result <> '') and Application.HintShortCuts then
  2557. begin
  2558. { Custom shortcut }
  2559. P := Pos(#9, Item.Caption);
  2560. if (P <> 0) and (P < Length(Item.Caption)) then
  2561. Result := Format('%s (%s)', [Result, Copy(Item.Caption, P+ 1, MaxInt)])
  2562. else
  2563. if (Item.ShortCut <> scNone) then
  2564. Result := Format('%s (%s)', [Result, ShortCutToTextFix(Item.ShortCut)]);
  2565. end;
  2566. if LongHint <> '' then
  2567. Result := Result + '|' + GetLongHint(LongHint);
  2568. end;
  2569. function TTBItemViewer.CaptionShown: Boolean;
  2570. begin
  2571. Result := (GetCaptionText <> '') and (not IsToolbarSize or
  2572. (Item.ImageIndex < 0) or (Item.DisplayMode in [nbdmTextOnly, nbdmImageAndText])) or
  2573. (tboImageAboveCaption in Item.EffectiveOptions);
  2574. end;
  2575. function TTBItemViewer.ImageShown: Boolean;
  2576. begin
  2577. {}{should also return false if Images=nil (use UsedImageList?)}
  2578. ImageShown := (Item.ImageIndex >= 0) and
  2579. ((Item.DisplayMode in [nbdmDefault, nbdmImageAndText]) or
  2580. (IsToolbarStyle and (Item.DisplayMode = nbdmTextOnlyInMenus)));
  2581. end;
  2582. function TTBItemViewer.GetImageList: TCustomImageList;
  2583. var
  2584. V: TTBView;
  2585. begin
  2586. Result := Item.Images;
  2587. if Assigned(Result) then
  2588. Exit;
  2589. V := View;
  2590. repeat
  2591. if Assigned(V.FCurParentItem) then begin
  2592. Result := V.FCurParentItem.SubMenuImages;
  2593. if Assigned(Result) then
  2594. Break;
  2595. end;
  2596. if Assigned(V.FParentItem) then begin
  2597. Result := V.FParentItem.SubMenuImages;
  2598. if Assigned(Result) then
  2599. Break;
  2600. end;
  2601. V := V.FParentView;
  2602. until V = nil;
  2603. end;
  2604. function TTBItemViewer.IsRotated: Boolean;
  2605. { Returns True if the caption should be drawn with rotated (vertical) text,
  2606. underneath the image }
  2607. begin
  2608. Result := (View.Orientation = tbvoVertical) and
  2609. not (tboNoRotation in Item.EffectiveOptions) and
  2610. not (tboImageAboveCaption in Item.EffectiveOptions);
  2611. end;
  2612. procedure TTBItemViewer.CalcSize(const Canvas: TCanvas;
  2613. var AWidth, AHeight: Integer);
  2614. var
  2615. ToolbarStyle: Boolean;
  2616. DC: HDC;
  2617. TextMetrics: TTextMetric;
  2618. H, LeftMargin: Integer;
  2619. ImgList: TCustomImageList;
  2620. S: String;
  2621. RotatedFont, SaveFont: HFONT;
  2622. begin
  2623. ToolbarStyle := IsToolbarStyle;
  2624. DC := Canvas.Handle;
  2625. ImgList := GetImageList;
  2626. if ToolbarStyle then begin
  2627. AWidth := 6;
  2628. AHeight := 6;
  2629. end
  2630. else begin
  2631. AWidth := 0;
  2632. AHeight := 0;
  2633. end;
  2634. if not ToolbarStyle or CaptionShown then begin
  2635. if not IsRotated then begin
  2636. GetTextMetrics(DC, TextMetrics);
  2637. Inc(AHeight, TextMetrics.tmHeight);
  2638. Inc(AWidth, GetTextWidth(DC, GetCaptionText, True));
  2639. if ToolbarStyle then
  2640. Inc(AWidth, 6);
  2641. end
  2642. else begin
  2643. { Vertical text isn't always the same size as horizontal text, so we have
  2644. to select the rotated font into the DC to get an accurate size }
  2645. RotatedFont := CreateRotatedFont(DC);
  2646. SaveFont := SelectObject(DC, RotatedFont);
  2647. GetTextMetrics(DC, TextMetrics);
  2648. Inc(AWidth, TextMetrics.tmHeight);
  2649. Inc(AHeight, GetTextWidth(DC, GetCaptionText, True));
  2650. if ToolbarStyle then
  2651. Inc(AHeight, 6);
  2652. SelectObject(DC, SaveFont);
  2653. DeleteObject(RotatedFont);
  2654. end;
  2655. end;
  2656. if ToolbarStyle and ImageShown and Assigned(ImgList) then begin
  2657. if not IsRotated and not(tboImageAboveCaption in Item.EffectiveOptions) then begin
  2658. Inc(AWidth, ImgList.Width + 1);
  2659. if AHeight < ImgList.Height + 6 then
  2660. AHeight := ImgList.Height + 6;
  2661. end
  2662. else begin
  2663. Inc(AHeight, ImgList.Height);
  2664. if AWidth < ImgList.Width + 7 then
  2665. AWidth := ImgList.Width + 7;
  2666. end;
  2667. end;
  2668. if ToolbarStyle and (tbisSubmenu in Item.ItemStyle) then begin
  2669. if tbisCombo in Item.ItemStyle then
  2670. Inc(AWidth, tbDropdownComboArrowWidth)
  2671. else
  2672. if tboDropdownArrow in Item.EffectiveOptions then begin
  2673. if View.Orientation <> tbvoVertical then
  2674. Inc(AWidth, tbDropdownArrowWidth)
  2675. else
  2676. Inc(AHeight, tbDropdownArrowWidth);
  2677. end;
  2678. end;
  2679. if not ToolbarStyle then begin
  2680. Inc(AHeight, TextMetrics.tmExternalLeading + tbMenuVerticalMargin);
  2681. if Assigned(ImgList) then begin
  2682. H := ImgList.Height + 3;
  2683. if H > AHeight then
  2684. AHeight := H;
  2685. LeftMargin := MulDiv(ImgList.Width + 3, AHeight, H);
  2686. end
  2687. else
  2688. LeftMargin := AHeight;
  2689. Inc(AWidth, LeftMargin + tbMenuImageTextSpace + tbMenuLeftTextMargin +
  2690. tbMenuRightTextMargin);
  2691. S := Item.GetShortCutText;
  2692. if S <> '' then
  2693. Inc(AWidth, (AHeight - 6) + GetTextWidth(DC, S, True));
  2694. Inc(AWidth, AHeight);
  2695. end;
  2696. end;
  2697. procedure TTBItemViewer.DrawItemCaption(const Canvas: TCanvas; ARect: TRect;
  2698. const ACaption: String; ADrawDisabledShadow: Boolean; AFormat: UINT);
  2699. var
  2700. DC: HDC;
  2701. procedure Draw;
  2702. begin
  2703. if not IsRotated then
  2704. DrawText(DC, PChar(ACaption), Length(ACaption), ARect, AFormat)
  2705. else
  2706. DrawRotatedText(DC, ACaption, ARect, AFormat);
  2707. end;
  2708. var
  2709. ShadowColor, HighlightColor, SaveTextColor: DWORD;
  2710. begin
  2711. DC := Canvas.Handle;
  2712. if not ADrawDisabledShadow then
  2713. Draw
  2714. else begin
  2715. ShadowColor := GetSysColor(COLOR_BTNSHADOW);
  2716. HighlightColor := GetSysColor(COLOR_BTNHIGHLIGHT);
  2717. OffsetRect(ARect, 1, 1);
  2718. SaveTextColor := SetTextColor(DC, HighlightColor);
  2719. Draw;
  2720. OffsetRect(ARect, -1, -1);
  2721. SetTextColor(DC, ShadowColor);
  2722. Draw;
  2723. SetTextColor(DC, SaveTextColor);
  2724. end;
  2725. end;
  2726. procedure TTBItemViewer.Paint(const Canvas: TCanvas;
  2727. const ClientAreaRect: TRect; IsSelected, IsPushed, UseDisabledShadow: Boolean);
  2728. var
  2729. ShowEnabled, HasArrow: Boolean;
  2730. MenuCheckWidth, MenuCheckHeight: Integer;
  2731. function GetDrawTextFlags: UINT;
  2732. begin
  2733. Result := 0;
  2734. if not AreKeyboardCuesEnabled and (vsUseHiddenAccels in View.FStyle) and
  2735. not(vsShowAccels in View.FState) then
  2736. Result := DT_HIDEPREFIX;
  2737. end;
  2738. procedure DrawSubmenuArrow;
  2739. var
  2740. BR: TRect;
  2741. Bmp: TBitmap;
  2742. procedure DrawWithColor(AColor: TColor);
  2743. const
  2744. ROP_DSPDxax = $00E20746;
  2745. var
  2746. DC: HDC;
  2747. SaveTextColor, SaveBkColor: TColorRef;
  2748. begin
  2749. Canvas.Brush.Color := AColor;
  2750. DC := Canvas.Handle;
  2751. SaveTextColor := SetTextColor(DC, clWhite);
  2752. SaveBkColor := SetBkColor(DC, clBlack);
  2753. BitBlt(DC, BR.Left, BR.Top, MenuCheckWidth, MenuCheckHeight,
  2754. Bmp.Canvas.Handle, 0, 0, ROP_DSPDxax);
  2755. SetBkColor(DC, SaveBkColor);
  2756. SetTextColor(DC, SaveTextColor);
  2757. Canvas.Brush.Style := bsClear;
  2758. end;
  2759. begin
  2760. Bmp := TBitmap.Create;
  2761. try
  2762. Bmp.Monochrome := True;
  2763. Bmp.Width := MenuCheckWidth;
  2764. Bmp.Height := MenuCheckHeight;
  2765. BR := Rect(0, 0, MenuCheckWidth, MenuCheckHeight);
  2766. DrawFrameControl(Bmp.Canvas.Handle, BR, DFC_MENU, DFCS_MENUARROW);
  2767. OffsetRect(BR, ClientAreaRect.Right - MenuCheckWidth,
  2768. ClientAreaRect.Top + ((ClientAreaRect.Bottom - ClientAreaRect.Top) - MenuCheckHeight) div 2);
  2769. if not UseDisabledShadow then begin
  2770. if ShowEnabled and (tbisCombo in Item.ItemStyle) and IsSelected then begin
  2771. OffsetRect(BR, 1, 1);
  2772. DrawWithColor(clBtnText);
  2773. end
  2774. else
  2775. DrawWithColor(Canvas.Font.Color);
  2776. end
  2777. else begin
  2778. OffsetRect(BR, 1, 1);
  2779. DrawWithColor(clBtnHighlight);
  2780. OffsetRect(BR, -1, -1);
  2781. DrawWithColor(clBtnShadow);
  2782. end;
  2783. finally
  2784. Bmp.Free;
  2785. end;
  2786. end;
  2787. procedure DrawDropdownArrow(R: TRect; Rotated: Boolean);
  2788. procedure DrawWithColor(AColor: TColor);
  2789. var
  2790. X, Y: Integer;
  2791. P: array[0..2] of TPoint;
  2792. begin
  2793. X := (R.Left + R.Right) div 2;
  2794. Y := (R.Top + R.Bottom) div 2;
  2795. if not Rotated then begin
  2796. Dec(Y);
  2797. P[0].X := X-2;
  2798. P[0].Y := Y;
  2799. P[1].X := X+2;
  2800. P[1].Y := Y;
  2801. P[2].X := X;
  2802. P[2].Y := Y+2;
  2803. end
  2804. else begin
  2805. Dec(X);
  2806. P[0].X := X;
  2807. P[0].Y := Y+2;
  2808. P[1].X := X;
  2809. P[1].Y := Y-2;
  2810. P[2].X := X-2;
  2811. P[2].Y := Y;
  2812. end;
  2813. Canvas.Pen.Color := AColor;
  2814. Canvas.Brush.Color := AColor;
  2815. Canvas.Polygon(P);
  2816. end;
  2817. begin
  2818. if not UseDisabledShadow then
  2819. DrawWithColor(Canvas.Font.Color)
  2820. else begin
  2821. OffsetRect(R, 1, 1);
  2822. DrawWithColor(clBtnHighlight);
  2823. OffsetRect(R, -1, -1);
  2824. DrawWithColor(clBtnShadow);
  2825. end;
  2826. end;
  2827. function GetDitherBitmap: TBitmap;
  2828. begin
  2829. Result := AllocPatternBitmap(clBtnFace, clBtnHighlight);
  2830. Result.HandleType := bmDDB; { needed for Win95, or else brush is solid white }
  2831. end;
  2832. const
  2833. EdgeStyles: array[Boolean] of UINT = (BDR_RAISEDINNER, BDR_SUNKENOUTER);
  2834. CheckMarkPoints: array[0..11] of TPoint = (
  2835. { Black }
  2836. (X: -2; Y: -2), (X: 0; Y: 0), (X: 4; Y: -4),
  2837. (X: 4; Y: -3), (X: 0; Y: 1), (X: -2; Y: -1),
  2838. (X: -2; Y: -2),
  2839. { White }
  2840. (X: -3; Y: -2), (X: -3; Y: -1), (X: 0; Y: 2),
  2841. (X: 5; Y: -3), (X: 5; Y: -5));
  2842. var
  2843. ToolbarStyle, ImageIsShown: Boolean;
  2844. R, RC, RD: TRect;
  2845. S: String;
  2846. ImgList: TCustomImageList;
  2847. I, X, Y: Integer;
  2848. Points: array[0..11] of TPoint;
  2849. DrawTextFlags: UINT;
  2850. LeftMargin: Integer;
  2851. TextMetrics: TTextMetric;
  2852. begin
  2853. ToolbarStyle := IsToolbarStyle;
  2854. ShowEnabled := Item.Enabled or View.Customizing;
  2855. HasArrow := (tbisSubmenu in Item.ItemStyle) and
  2856. ((tbisCombo in Item.ItemStyle) or (tboDropdownArrow in Item.EffectiveOptions));
  2857. MenuCheckWidth := GetSystemMetricsForControl(View.FWindow, SM_CXMENUCHECK);
  2858. MenuCheckHeight := GetSystemMetricsForControl(View.FWindow, SM_CYMENUCHECK);
  2859. ImgList := GetImageList;
  2860. ImageIsShown := ImageShown and Assigned(ImgList);
  2861. LeftMargin := 0;
  2862. if not ToolbarStyle then begin
  2863. if Assigned(ImgList) then
  2864. LeftMargin := MulDiv(ImgList.Width + 3, ClientAreaRect.Bottom, ImgList.Height + 3)
  2865. else
  2866. LeftMargin := ClientAreaRect.Bottom;
  2867. end;
  2868. { Border }
  2869. RC := ClientAreaRect;
  2870. if ToolbarStyle then begin
  2871. if HasArrow then begin
  2872. if tbisCombo in Item.ItemStyle then begin
  2873. Dec(RC.Right, tbDropdownComboMargin);
  2874. RD := RC;
  2875. Dec(RC.Right, tbDropdownComboArrowWidth - tbDropdownComboMargin);
  2876. RD.Left := RC.Right;
  2877. end
  2878. else begin
  2879. if View.Orientation <> tbvoVertical then
  2880. RD := Rect(RC.Right - tbDropdownArrowWidth - tbDropdownArrowMargin, 0,
  2881. RC.Right - tbDropdownArrowMargin, RC.Bottom)
  2882. else
  2883. RD := Rect(0, RC.Bottom - tbDropdownArrowWidth - tbDropdownArrowMargin,
  2884. RC.Right, RC.Bottom - tbDropdownArrowMargin);
  2885. end;
  2886. end
  2887. else
  2888. SetRectEmpty(RD);
  2889. if (IsSelected and ShowEnabled) or Item.Checked or
  2890. (csDesigning in Item.ComponentState) then begin
  2891. if not(tbisCombo in Item.ItemStyle) then
  2892. DrawEdge(Canvas.Handle, RC, EdgeStyles[IsPushed or Item.Checked], BF_RECT)
  2893. else begin
  2894. DrawEdge(Canvas.Handle, RC, EdgeStyles[(IsPushed and View.FCapture) or Item.Checked], BF_RECT);
  2895. if (IsSelected and ShowEnabled) or
  2896. (csDesigning in Item.ComponentState) then
  2897. DrawEdge(Canvas.Handle, RD, EdgeStyles[IsPushed and not View.FCapture], BF_RECT);
  2898. end;
  2899. end;
  2900. if HasArrow then begin
  2901. if not(tbisCombo in Item.ItemStyle) and IsPushed then
  2902. OffsetRect(RD, 1, 1);
  2903. DrawDropdownArrow(RD, not(tbisCombo in Item.ItemStyle) and
  2904. (View.Orientation = tbvoVertical));
  2905. end;
  2906. InflateRect(RC, -1, -1);
  2907. if Item.Checked and not (IsSelected and ShowEnabled) then begin
  2908. Canvas.Brush.Bitmap := GetDitherBitmap;
  2909. Canvas.FillRect(RC);
  2910. Canvas.Brush.Style := bsClear;
  2911. end;
  2912. InflateRect(RC, -1, -1);
  2913. if Item.Checked or
  2914. ((IsSelected and IsPushed) and
  2915. (not(tbisCombo in Item.ItemStyle) or View.FCapture)) then
  2916. OffsetRect(RC, 1, 1);
  2917. if HasArrow and not(tbisCombo in Item.ItemStyle) then begin
  2918. if View.Orientation <> tbvoVertical then
  2919. Dec(RC.Right, tbDropdownArrowWidth)
  2920. else
  2921. Dec(RC.Bottom, tbDropdownArrowWidth);
  2922. end;
  2923. end
  2924. else begin
  2925. { On selected menu items, fill the background with the selected color.
  2926. Note: This assumes the brush color was not changed from the initial
  2927. value. }
  2928. if IsSelected then begin
  2929. R := RC;
  2930. if ImageIsShown or Item.Checked then
  2931. Inc(R.Left, LeftMargin + tbMenuImageTextSpace);
  2932. if (tbisCombo in Item.ItemStyle) and IsSelected and ShowEnabled then
  2933. Dec(R.Right, MenuCheckWidth);
  2934. Canvas.FillRect(R);
  2935. end;
  2936. end;
  2937. { Adjust brush & font }
  2938. Canvas.Brush.Style := bsClear;
  2939. if tboDefault in Item.EffectiveOptions then
  2940. with Canvas.Font do Style := Style + [fsBold];
  2941. GetTextMetrics(Canvas.Handle, TextMetrics);
  2942. { Caption }
  2943. if CaptionShown then begin
  2944. S := GetCaptionText;
  2945. R := RC;
  2946. DrawTextFlags := GetDrawTextFlags;
  2947. if ToolbarStyle then begin
  2948. if ImageIsShown then begin
  2949. if not IsRotated and not(tboImageAboveCaption in Item.EffectiveOptions) then
  2950. Inc(R.Left, ImgList.Width + 1)
  2951. else
  2952. Inc(R.Top, ImgList.Height + 1);
  2953. end;
  2954. DrawItemCaption(Canvas, R, S, UseDisabledShadow,
  2955. DT_SINGLELINE or DT_CENTER or DT_VCENTER or DrawTextFlags)
  2956. end
  2957. else begin
  2958. Inc(R.Left, LeftMargin + tbMenuImageTextSpace + tbMenuLeftTextMargin);
  2959. { Like standard menus, shift the text up one pixel if the text height
  2960. is 4 pixels less than the total item height. This is done so underlined
  2961. characters aren't displayed too low. }
  2962. if (R.Bottom - R.Top) - (TextMetrics.tmHeight + TextMetrics.tmExternalLeading) = tbMenuVerticalMargin then
  2963. Dec(R.Bottom);
  2964. Inc(R.Top, TextMetrics.tmExternalLeading);
  2965. DrawItemCaption(Canvas, R, S, UseDisabledShadow,
  2966. DT_SINGLELINE or DT_LEFT or DT_VCENTER or DrawTextFlags);
  2967. end;
  2968. end;
  2969. { Shortcut and/or submenu arrow (menus only) }
  2970. if not ToolbarStyle then begin
  2971. S := Item.GetShortCutText;
  2972. if S <> '' then begin
  2973. R := RC;
  2974. R.Left := R.Right - (R.Bottom - R.Top) - GetTextWidth(Canvas.Handle, S, True);
  2975. { Like standard menus, shift the text up one pixel if the text height
  2976. is 4 pixels less than the total item height. This is done so underlined
  2977. characters aren't displayed too low. }
  2978. if (R.Bottom - R.Top) - (TextMetrics.tmHeight + TextMetrics.tmExternalLeading) = tbMenuVerticalMargin then
  2979. Dec(R.Bottom);
  2980. Inc(R.Top, TextMetrics.tmExternalLeading);
  2981. DrawItemCaption(Canvas, R, S, UseDisabledShadow,
  2982. DT_SINGLELINE or DT_LEFT or DT_VCENTER or DT_NOPREFIX);
  2983. end;
  2984. if tbisSubmenu in Item.ItemStyle then begin
  2985. if tbisCombo in Item.ItemStyle then begin
  2986. R := RC;
  2987. R.Left := R.Right - MenuCheckWidth;
  2988. if IsSelected and ShowEnabled then
  2989. DrawEdge(Canvas.Handle, R, BDR_SUNKENOUTER, BF_RECT or BF_MIDDLE)
  2990. else begin
  2991. Dec(R.Left);
  2992. if not IsSelected then
  2993. DrawEdge(Canvas.Handle, R, EDGE_ETCHED, BF_LEFT)
  2994. else
  2995. DrawEdge(Canvas.Handle, R, BDR_SUNKENOUTER, BF_LEFT);
  2996. end;
  2997. end;
  2998. DrawSubmenuArrow;
  2999. end;
  3000. end;
  3001. { Image, or check box }
  3002. if ImageIsShown or (not ToolbarStyle and Item.Checked) then begin
  3003. R := RC;
  3004. if ToolbarStyle then begin
  3005. if not IsRotated and not(tboImageAboveCaption in Item.EffectiveOptions) then
  3006. R.Right := R.Left + ImgList.Width + 2
  3007. else
  3008. R.Bottom := R.Top + ImgList.Height + 2;
  3009. end
  3010. else begin
  3011. R.Right := R.Left + LeftMargin;
  3012. if (IsSelected and ShowEnabled) or Item.Checked then
  3013. DrawEdge(Canvas.Handle, R, EdgeStyles[Item.Checked], BF_RECT or BF_MIDDLE);
  3014. if Item.Checked and not IsSelected then begin
  3015. InflateRect(R, -1, -1);
  3016. Canvas.Brush.Bitmap := GetDitherBitmap;
  3017. Canvas.FillRect(R);
  3018. Canvas.Brush.Style := bsClear;
  3019. InflateRect(R, 1, 1);
  3020. end;
  3021. if Item.Checked then
  3022. OffsetRect(R, 1, 1);
  3023. end;
  3024. if ImageIsShown then begin
  3025. X := R.Left + ((R.Right - R.Left) - ImgList.Width) div 2;
  3026. Y := R.Top + ((R.Bottom - R.Top) - ImgList.Height) div 2;
  3027. if ImgList is TTBCustomImageList then
  3028. TTBCustomImageList(ImgList).DrawState(Canvas, X, Y, Item.ImageIndex,
  3029. ShowEnabled, IsSelected, Item.Checked)
  3030. else
  3031. ImgList.Draw(Canvas, X, Y, Item.ImageIndex, ShowEnabled);
  3032. end
  3033. else
  3034. if not ToolbarStyle and Item.Checked then begin
  3035. { Draw default check mark or radio button image when user hasn't
  3036. specified their own }
  3037. X := (R.Left + R.Right) div 2;
  3038. Y := (R.Top + R.Bottom) div 2;
  3039. if Item.RadioItem then begin
  3040. Canvas.Pen.Color := clBtnText;
  3041. Canvas.Brush.Color := clBtnText;
  3042. Canvas.RoundRect(X-3, Y-3, X+2, Y+2, 2, 2);
  3043. Canvas.Pen.Color := clBtnHighlight;
  3044. Canvas.Brush.Style := bsClear;
  3045. Canvas.RoundRect(X-4, Y-4, X+3, Y+3, 6, 6);
  3046. end
  3047. else begin
  3048. Dec(X, 2);
  3049. Inc(Y);
  3050. System.Move(CheckMarkPoints, Points, 12 * SizeOf(TPoint));
  3051. for I := Low(Points) to High(Points) do begin
  3052. Inc(Points[I].X, X);
  3053. Inc(Points[I].Y, Y);
  3054. end;
  3055. Canvas.Pen.Color := clBtnText;
  3056. Polyline(Canvas.Handle, Points[0], 7);
  3057. Canvas.Pen.Color := clBtnHighlight;
  3058. Polyline(Canvas.Handle, Points[7], 5);
  3059. end;
  3060. end;
  3061. end;
  3062. end;
  3063. procedure TTBItemViewer.GetCursor(const Pt: TPoint; var ACursor: HCURSOR);
  3064. begin
  3065. end;
  3066. function TTBItemViewer.GetIndex: Integer;
  3067. begin
  3068. Result := View.IndexOf(Self);
  3069. end;
  3070. function TTBItemViewer.IsToolbarSize: Boolean;
  3071. begin
  3072. Result := View.FIsToolbar or (tboToolbarSize in Item.FEffectiveOptions);
  3073. end;
  3074. function TTBItemViewer.IsToolbarStyle: Boolean;
  3075. begin
  3076. Result := View.FIsToolbar or (tboToolbarStyle in Item.FEffectiveOptions);
  3077. end;
  3078. function TTBItemViewer.IsPtInButtonPart(X, Y: Integer): Boolean;
  3079. var
  3080. W: Integer;
  3081. begin
  3082. Result := not(tbisSubmenu in Item.ItemStyle);
  3083. if tbisCombo in Item.ItemStyle then begin
  3084. if IsToolbarStyle then
  3085. W := tbDropdownComboArrowWidth
  3086. else
  3087. W := GetSystemMetricsForControl(View.FWindow, SM_CXMENUCHECK);
  3088. Result := X < (BoundsRect.Right - BoundsRect.Left) - W;
  3089. end;
  3090. end;
  3091. procedure TTBItemViewer.MouseDown(Shift: TShiftState; X, Y: Integer;
  3092. var MouseDownOnMenu: Boolean);
  3093. procedure HandleDefaultDoubleClick(const View: TTBView);
  3094. { Looks for a tboDefault item in View and ends the modal loop if it finds
  3095. one. }
  3096. var
  3097. I: Integer;
  3098. Viewer: TTBItemViewer;
  3099. Item: TTBCustomItem;
  3100. begin
  3101. for I := 0 to View.FViewerCount-1 do begin
  3102. Viewer := View.FViewers[I];
  3103. Item := Viewer.Item;
  3104. if (Viewer.Show or Viewer.Clipped) and (tboDefault in Item.EffectiveOptions) and
  3105. (tbisSelectable in Item.ItemStyle) and Item.Enabled and Item.Visible then begin
  3106. Viewer.Execute(True);
  3107. Break;
  3108. end;
  3109. end;
  3110. end;
  3111. var
  3112. WasAlreadyOpen: Boolean;
  3113. begin
  3114. if not Item.Enabled then begin
  3115. if (View.FParentView = nil) and not View.FIsPopup then
  3116. View.EndModal;
  3117. Exit;
  3118. end;
  3119. if IsPtInButtonPart(X, Y) then begin
  3120. if IsToolbarStyle then begin
  3121. View.CancelChildPopups;
  3122. View.SetCapture;
  3123. View.Invalidate(Self);
  3124. end;
  3125. end
  3126. else begin
  3127. WasAlreadyOpen := (View.FOpenViewer = Self);
  3128. if View.OpenChildPopup(False) then begin
  3129. if WasAlreadyOpen and ((View.FParentView = nil) and not View.FIsPopup) then
  3130. MouseDownOnMenu := True;
  3131. if (ssDouble in Shift) and not(tbisCombo in Item.ItemStyle) then
  3132. HandleDefaultDoubleClick(View.FOpenViewerView);
  3133. end;
  3134. end;
  3135. end;
  3136. procedure TTBItemViewer.MouseMove(X, Y: Integer);
  3137. begin
  3138. end;
  3139. procedure TTBItemViewer.MouseUp(X, Y: Integer; MouseWasDownOnMenu: Boolean);
  3140. var
  3141. HadCapture, IsToolbarItem: Boolean;
  3142. begin
  3143. HadCapture := View.FCapture;
  3144. View.CancelCapture;
  3145. IsToolbarItem := (View.FParentView = nil) and not View.FIsPopup;
  3146. if not View.FMouseOverSelected or not Item.Enabled or
  3147. (tbisClicksTransparent in Item.ItemStyle) then begin
  3148. if IsToolbarItem then
  3149. View.EndModal;
  3150. Exit;
  3151. end;
  3152. if (tbisSubmenu in Item.ItemStyle) and not IsPtInButtonPart(X, Y) then begin
  3153. if IsToolbarItem and MouseWasDownOnMenu then
  3154. View.EndModal;
  3155. end
  3156. else begin
  3157. { it's a 'normal' item }
  3158. if not IsToolbarStyle or HadCapture then
  3159. Execute(True);
  3160. end;
  3161. end;
  3162. procedure TTBItemViewer.MouseWheel(WheelDelta, X, Y: Integer);
  3163. begin
  3164. end;
  3165. procedure TTBItemViewer.LosingCapture;
  3166. begin
  3167. View.Invalidate(Self);
  3168. end;
  3169. procedure TTBItemViewer.Entering(OldSelected: TTBItemViewer);
  3170. begin
  3171. if Assigned(Item.FOnSelect) then
  3172. Item.FOnSelect(Item, Self, True);
  3173. end;
  3174. procedure TTBItemViewer.Leaving;
  3175. begin
  3176. if Assigned(Item.FOnSelect) then
  3177. Item.FOnSelect(Item, Self, False);
  3178. end;
  3179. procedure TTBItemViewer.KeyDown(var Key: Word; Shift: TShiftState);
  3180. begin
  3181. end;
  3182. function TTBItemViewer.ScreenToClient(const P: TPoint): TPoint;
  3183. begin
  3184. Result := View.FWindow.ScreenToClient(P);
  3185. Dec(Result.X, BoundsRect.Left);
  3186. Dec(Result.Y, BoundsRect.Top);
  3187. end;
  3188. function TTBItemViewer.UsesSameWidth: Boolean;
  3189. { If UsesSameWidth returns True, the item viewer's width will be expanded to
  3190. match the widest item viewer on the same view whose UsesSameWidth method
  3191. also returns True. }
  3192. begin
  3193. Result := (tboImageAboveCaption in Item.FEffectiveOptions) and
  3194. (tboSameWidth in Item.FEffectiveOptions) and IsToolbarSize;
  3195. end;
  3196. function TTBItemViewer.DoExecute: Boolean;
  3197. { Low-level 'execute' handler. Returns True if the caller should call
  3198. GivePriority on the viewer (normally, if the 'execute' operation was a
  3199. success and the modal loop is ending). }
  3200. begin
  3201. View.EndModalWithClick(Self);
  3202. Result := True;
  3203. end;
  3204. procedure TTBItemViewer.Execute(AGivePriority: Boolean);
  3205. { Calls DoExecute and, if applicable, View.GivePriority. Note that it is up to
  3206. the caller to check the viewer's visibility and enabled state. }
  3207. begin
  3208. if DoExecute and AGivePriority then
  3209. View.GivePriority(Self);
  3210. end;
  3211. function TTBItemViewer.GetAccRole: Integer;
  3212. { Returns the MSAA "role" of the viewer. }
  3213. const
  3214. { Constants from OleAcc.h }
  3215. ROLE_SYSTEM_CLIENT = $a;
  3216. ROLE_SYSTEM_MENUITEM = $c;
  3217. ROLE_SYSTEM_SEPARATOR = $15;
  3218. ROLE_SYSTEM_PUSHBUTTON = $2b;
  3219. ROLE_SYSTEM_BUTTONMENU = $39;
  3220. begin
  3221. if Item is TTBControlItem then
  3222. Result := ROLE_SYSTEM_CLIENT
  3223. else if tbisSeparator in Item.ItemStyle then
  3224. Result := ROLE_SYSTEM_SEPARATOR
  3225. else if View.IsPopup or (vsMenuBar in View.Style) then
  3226. Result := ROLE_SYSTEM_MENUITEM
  3227. else if tbisSubmenu in Item.ItemStyle then
  3228. Result := ROLE_SYSTEM_BUTTONMENU
  3229. else
  3230. Result := ROLE_SYSTEM_PUSHBUTTON;
  3231. end;
  3232. function TTBItemViewer.GetAccValue(var Value: WideString): Boolean;
  3233. { Gets the MSAA "value" text of the viewer. Returns True if something was
  3234. assigned to Value, or False if the viewer does not possess a "value". }
  3235. begin
  3236. Result := False;
  3237. end;
  3238. { TTBView }
  3239. constructor TTBView.CreateView(AOwner: TComponent; AParentView: TTBView;
  3240. AParentItem: TTBCustomItem; AWindow: TWinControl;
  3241. AIsToolbar, ACustomizing, AUsePriorityList: Boolean);
  3242. begin
  3243. Create(AOwner);
  3244. FBackgroundColor := clDefault;
  3245. FCustomizing := ACustomizing;
  3246. FIsPopup := not AIsToolbar;
  3247. FIsToolbar := AIsToolbar;
  3248. FNewViewersGetHighestPriority := True;
  3249. FParentView := AParentView;
  3250. FParentItem := AParentItem;
  3251. if Assigned(FParentItem) then begin
  3252. //FIsToolbar := FIsToolbar or FParentItem.FDisplayAsToolbar;
  3253. FParentItem.RegisterNotification(LinkNotification);
  3254. FParentItem.FreeNotification(Self);
  3255. end;
  3256. FUsePriorityList := AUsePriorityList;
  3257. FWindow := AWindow;
  3258. UpdateCurParentItem;
  3259. end;
  3260. destructor TTBView.Destroy;
  3261. begin
  3262. CloseChildPopups;
  3263. if Assigned(FAccObjectInstance) then begin
  3264. FAccObjectInstance.ClientIsDestroying;
  3265. { Get rid of our own reference to FAccObjectInstance. Normally the
  3266. reference count will be now be zero and FAccObjectInstance will be
  3267. freed, unless MSAA still holds a reference. }
  3268. FAccObjectInstance._Release;
  3269. FAccObjectInstance := nil;
  3270. end;
  3271. { If parent view is a toolbar, invalidate the open item so that it's
  3272. redrawn back in the "up" position }
  3273. if Assigned(ParentView) and ParentView.FIsToolbar then begin
  3274. Include(ParentView.FState, vsNoAnimation);
  3275. if Assigned(ParentView.FOpenViewer) then
  3276. ParentView.Invalidate(ParentView.FOpenViewer);
  3277. end;
  3278. if Assigned(FCurParentItem) then
  3279. FCurParentItem.UnregisterNotification(ItemNotification);
  3280. if Assigned(FParentItem) then
  3281. FParentItem.UnregisterNotification(LinkNotification);
  3282. inherited;
  3283. FPriorityList.Free;
  3284. FreeViewers;
  3285. { Now that we're destroyed, "focus" the parent view }
  3286. if Assigned(FParentView) then
  3287. FParentView.NotifyFocusEvent;
  3288. end;
  3289. function TTBView.GetAccObject: IDispatch;
  3290. begin
  3291. if FAccObjectInstance = nil then begin
  3292. FAccObjectInstance := TTBViewAccObject.Create(Self);
  3293. { Strictly as an optimization, take a reference for ourself and keep it
  3294. for the lifetime of the view. (Destroy calls _Release.) }
  3295. FAccObjectInstance._AddRef;
  3296. end;
  3297. Result := FAccObjectInstance;
  3298. end;
  3299. function TTBView.HandleWMGetObject(var Message: TMessage): Boolean;
  3300. begin
  3301. if (Message.LParam = Integer(OBJID_CLIENT)) then begin
  3302. Message.Result := LresultFromObject(ITBAccessible, Message.WParam, GetAccObject);
  3303. Result := True;
  3304. end
  3305. else
  3306. Result := False;
  3307. end;
  3308. procedure TTBView.UpdateCurParentItem;
  3309. var
  3310. Value: TTBCustomItem;
  3311. begin
  3312. Value := ItemContainingItems(FParentItem);
  3313. if FCurParentItem <> Value then begin
  3314. CloseChildPopups;
  3315. if Assigned(FCurParentItem) then
  3316. FCurParentItem.UnregisterNotification(ItemNotification);
  3317. FCurParentItem := Value;
  3318. if Assigned(Value) then
  3319. Value.RegisterNotification(ItemNotification);
  3320. RecreateAllViewers;
  3321. if Assigned(Value) and not(csDesigning in Value.ComponentState) then
  3322. InitiateActions;
  3323. end;
  3324. end;
  3325. procedure TTBView.InitiateActions;
  3326. var
  3327. I: Integer;
  3328. begin
  3329. { Use a 'while' instead of a 'for' since an InitiateAction implementation
  3330. may add/delete items }
  3331. I := 0;
  3332. while I < FViewerCount do begin
  3333. FViewers[I].Item.InitiateAction;
  3334. Inc(I);
  3335. end;
  3336. end;
  3337. procedure TTBView.Notification(AComponent: TComponent; Operation: TOperation);
  3338. begin
  3339. inherited;
  3340. if Operation = opRemove then begin
  3341. if AComponent = FParentItem then begin
  3342. FParentItem := nil;
  3343. UpdateCurParentItem;
  3344. if Assigned(FParentView) then
  3345. FParentView.CloseChildPopups;
  3346. end
  3347. else if AComponent = FOpenViewerWindow then begin
  3348. FOpenViewerWindow := nil;
  3349. FOpenViewerView := nil;
  3350. FOpenViewer := nil;
  3351. end
  3352. else if AComponent = FChevronParentView then
  3353. FChevronParentView := nil;
  3354. end
  3355. end;
  3356. function TTBView.ContainsView(AView: TTBView): Boolean;
  3357. begin
  3358. while Assigned(AView) and (AView <> Self) do
  3359. AView := AView.FParentView;
  3360. Result := Assigned(AView);
  3361. end;
  3362. function TTBView.GetRootView: TTBView;
  3363. begin
  3364. Result := Self;
  3365. while Assigned(Result.FParentView) do
  3366. Result := Result.FParentView;
  3367. end;
  3368. function TTBView.GetParentToolbarView: TTBView;
  3369. begin
  3370. Result := Self;
  3371. while Assigned(Result) and not Result.FIsToolbar do
  3372. Result := Result.FParentView;
  3373. end;
  3374. procedure TTBView.FreeViewers;
  3375. var
  3376. VI: PTBItemViewerArray;
  3377. I, C: Integer;
  3378. begin
  3379. if Assigned(FViewers) then begin
  3380. VI := FViewers;
  3381. C := FViewerCount;
  3382. FViewers := nil;
  3383. FViewerCount := 0;
  3384. for I := C-1 downto 0 do
  3385. FreeAndNil(VI[I]);
  3386. FreeMem(VI);
  3387. end;
  3388. end;
  3389. procedure TTBView.InvalidatePositions;
  3390. begin
  3391. if FValidated then begin
  3392. FValidated := False;
  3393. if Assigned(FWindow) and FWindow.HandleAllocated then
  3394. InvalidateRect(FWindow.Handle, nil, True);
  3395. end;
  3396. end;
  3397. procedure TTBView.ValidatePositions;
  3398. begin
  3399. if not FValidated then
  3400. UpdatePositions;
  3401. end;
  3402. procedure TTBView.TryValidatePositions;
  3403. begin
  3404. if (FUpdating = 0) and
  3405. (not Assigned(FParentItem) or not(csLoading in FParentItem.ComponentState)) and
  3406. (not Assigned(FParentItem.Owner) or not(csLoading in FParentItem.Owner.ComponentState)) then
  3407. ValidatePositions;
  3408. end;
  3409. (*procedure TTBView.TryRevalidatePositions;
  3410. begin
  3411. if FValidated then begin
  3412. if FUpdating = 0 then begin
  3413. FreePositions;
  3414. UpdatePositions;
  3415. end
  3416. else
  3417. InvalidatePositions;
  3418. end;
  3419. end;*)
  3420. function TTBView.Find(Item: TTBCustomItem): TTBItemViewer;
  3421. var
  3422. I: Integer;
  3423. begin
  3424. for I := 0 to FViewerCount-1 do
  3425. if FViewers[I].Item = Item then begin
  3426. Result := FViewers[I];
  3427. Exit;
  3428. end;
  3429. raise ETBItemError.Create(STBViewerNotFound);
  3430. end;
  3431. function TTBView.IndexOf(AViewer: TTBItemViewer): Integer;
  3432. var
  3433. I: Integer;
  3434. begin
  3435. if Assigned(AViewer) then
  3436. for I := 0 to FViewerCount-1 do
  3437. if FViewers[I] = AViewer then begin
  3438. Result := I;
  3439. Exit;
  3440. end;
  3441. Result := -1;
  3442. end;
  3443. procedure TTBView.DeletingViewer(Viewer: TTBItemViewer);
  3444. begin
  3445. if FSelected = Viewer then
  3446. FSelected := nil;
  3447. if FOpenViewer = Viewer then
  3448. CloseChildPopups;
  3449. end;
  3450. procedure TTBView.RecreateItemViewer(const I: Integer);
  3451. var
  3452. OldViewer, NewViewer: TTBItemViewer;
  3453. J: Integer;
  3454. begin
  3455. OldViewer := FViewers[I];
  3456. DeletingViewer(OldViewer);
  3457. NewViewer := OldViewer.Item.GetItemViewerClass(Self).Create(Self,
  3458. OldViewer.Item, OldViewer.FGroupLevel);
  3459. FViewers[I] := NewViewer;
  3460. if Assigned(FPriorityList) then begin
  3461. J := FPriorityList.IndexOf(OldViewer);
  3462. if J <> -1 then
  3463. FPriorityList[J] := NewViewer;
  3464. end;
  3465. OldViewer.Free;
  3466. end;
  3467. function TTBView.InsertItemViewers(const NewIndex: Integer;
  3468. const AItem: TTBCustomItem; const AGroupLevel: Integer;
  3469. const AddToPriorityList, TopOfPriorityList: Boolean): Integer;
  3470. var
  3471. NewViewer: TTBItemViewer;
  3472. LinkItem: TTBCustomItem;
  3473. I: Integer;
  3474. begin
  3475. if AGroupLevel > MaxGroupLevel then begin
  3476. Result := 0;
  3477. Exit;
  3478. end;
  3479. NewViewer := AItem.GetItemViewerClass(Self).Create(Self, AItem,
  3480. AGroupLevel);
  3481. InsertIntoViewerArray(FViewers, FViewerCount, NewIndex,
  3482. NewViewer);
  3483. if AddToPriorityList and FUsePriorityList then begin
  3484. if not TopOfPriorityList then
  3485. AddToList(FPriorityList, NewViewer)
  3486. else
  3487. { When new items are inserted programmatically at run-time, place
  3488. them at the top of FPriorityList }
  3489. AddToFrontOfList(FPriorityList, NewViewer);
  3490. end;
  3491. Result := 1;
  3492. { If a new group item is being inserted, insert all its child items too }
  3493. if not FCustomizing and (tbisEmbeddedGroup in AItem.ItemStyle) then begin
  3494. LinkItem := ItemContainingItems(AItem);
  3495. for I := 0 to LinkItem.Count-1 do begin
  3496. Inc(Result, InsertItemViewers(NewIndex + Result, LinkItem.FItems[I].Item,
  3497. AGroupLevel + 1, AddToPriorityList, TopOfPriorityList));
  3498. end;
  3499. end;
  3500. end;
  3501. procedure TTBView.ItemNotification(Ancestor: TTBCustomItem; Relayed: Boolean;
  3502. Action: TTBItemChangedAction; Index: Integer; Item: TTBCustomItem);
  3503. procedure ItemInserted;
  3504. var
  3505. NewLevel, Start, InsertPoint, Last: Integer;
  3506. GroupItem, NextItem: TTBCustomItem;
  3507. Found, SearchAgain: Boolean;
  3508. begin
  3509. InvalidatePositions;
  3510. NewLevel := 0;
  3511. Start := 0;
  3512. if Ancestor = FCurParentItem then
  3513. InsertPoint := FViewerCount
  3514. else begin
  3515. { Ancestor <> FCurParentItem, so apparently an item has been inserted
  3516. inside a group item }
  3517. repeat
  3518. Found := False;
  3519. while Start < FViewerCount do begin
  3520. GroupItem := FViewers[Start].Item;
  3521. if (tbisEmbeddedGroup in GroupItem.ItemStyle) and (GroupItem = Ancestor) then begin
  3522. NewLevel := FViewers[Start].FGroupLevel + 1;
  3523. Inc(Start);
  3524. Found := True;
  3525. Break;
  3526. end;
  3527. Inc(Start);
  3528. end;
  3529. if not Found then
  3530. { Couldn't find Ancestor; it shouldn't get here }
  3531. Exit;
  3532. InsertPoint := Start;
  3533. SearchAgain := False;
  3534. while (InsertPoint < FViewerCount) and
  3535. (FViewers[InsertPoint].FGroupLevel >= NewLevel) do begin
  3536. if (FViewers[InsertPoint].Item = Item) and
  3537. (FViewers[InsertPoint].FGroupLevel = NewLevel) then begin
  3538. { If the item we were going to insert already exists, then there
  3539. must be multiple instances of the same group item. This can
  3540. happen when are two group items on the same toolbar each
  3541. linking to the same submenu item, with the submenu item
  3542. containing a group item of its own, and an item is inserted
  3543. inside that. }
  3544. SearchAgain := True;
  3545. Break;
  3546. end;
  3547. Inc(InsertPoint);
  3548. end;
  3549. until not SearchAgain;
  3550. end;
  3551. if InsertPoint = FViewerCount then begin
  3552. { Don't add items after the chevron or MDI buttons item }
  3553. Dec(InsertPoint, FInternalViewersAtEnd);
  3554. if InsertPoint < 0 then
  3555. InsertPoint := 0; { just in case? }
  3556. end;
  3557. { If the new item wasn't placed at the end, adjust InsertPoint accordingly }
  3558. if Index < Item.Parent.Count-1 then begin
  3559. Last := InsertPoint;
  3560. InsertPoint := Start;
  3561. NextItem := Item.Parent.FItems[Index+1].Item;
  3562. while (InsertPoint < Last) and
  3563. ((FViewers[InsertPoint].Item <> NextItem) or
  3564. (FViewers[InsertPoint].FGroupLevel <> NewLevel)) do
  3565. Inc(InsertPoint);
  3566. end;
  3567. InsertItemViewers(InsertPoint, Item, NewLevel, True,
  3568. not(csLoading in Item.ComponentState) and FNewViewersGetHighestPriority);
  3569. end;
  3570. procedure ItemDeleting;
  3571. procedure DeleteItem(DeleteIndex: Integer);
  3572. var
  3573. Viewer: TTBItemViewer;
  3574. begin
  3575. Viewer := FViewers[DeleteIndex];
  3576. DeletingViewer(Viewer);
  3577. RemoveFromList(FPriorityList, Viewer);
  3578. FreeAndNil(Viewer);
  3579. DeleteFromViewerArray(FViewers, FViewerCount, DeleteIndex);
  3580. end;
  3581. var
  3582. I: Integer;
  3583. DeleteLevel: Integer;
  3584. begin
  3585. InvalidatePositions;
  3586. I := 0;
  3587. DeleteLevel := 0;
  3588. while I < FViewerCount do begin
  3589. if DeleteLevel > 0 then begin
  3590. if FViewers[I].FGroupLevel >= DeleteLevel then begin
  3591. DeleteItem(I);
  3592. Continue;
  3593. end
  3594. else
  3595. DeleteLevel := 0;
  3596. end;
  3597. if FViewers[I].Item = Item then begin
  3598. { Delete the item, and any group item children afterward }
  3599. DeleteLevel := FViewers[I].FGroupLevel + 1;
  3600. DeleteItem(I);
  3601. Continue;
  3602. end;
  3603. Inc(I);
  3604. end;
  3605. end;
  3606. var
  3607. I: Integer;
  3608. begin
  3609. case Action of
  3610. tbicInserted: ItemInserted;
  3611. tbicDeleting: ItemDeleting;
  3612. tbicSubitemsChanged: begin
  3613. { If Relayed=True, LinkSubitems must have changed on a child group
  3614. item. Currently there isn't any optimized way of handling this
  3615. situation; just recreate all viewers. }
  3616. if Relayed then
  3617. RecreateAllViewers;
  3618. end;
  3619. tbicSubitemsBeginUpdate: BeginUpdate;
  3620. tbicSubitemsEndUpdate: EndUpdate;
  3621. tbicInvalidate: begin
  3622. for I := 0 to FViewerCount-1 do
  3623. if FViewers[I].Item = Item then
  3624. Invalidate(FViewers[I]);
  3625. end;
  3626. tbicInvalidateAndResize: InvalidatePositions;
  3627. tbicRecreateItemViewers: begin
  3628. InvalidatePositions;
  3629. for I := 0 to FViewerCount-1 do
  3630. if FViewers[I].Item = Item then
  3631. RecreateItemViewer(I);
  3632. end;
  3633. tbicSubMenuImagesChanged: ImagesChanged;
  3634. else
  3635. { Prevent TryValidatePositions from being called below on Actions other than
  3636. those listed above. Currently there are no other Actions, but for forward
  3637. compatibility, we should ignore unknown Actions completely. }
  3638. Exit;
  3639. end;
  3640. TryValidatePositions;
  3641. end;
  3642. procedure TTBView.LinkNotification(Ancestor: TTBCustomItem; Relayed: Boolean;
  3643. Action: TTBItemChangedAction; Index: Integer; Item: TTBCustomItem);
  3644. { This notification procedure watches for tbicSubitemsChanged notifications
  3645. from FParentItem }
  3646. begin
  3647. case Action of
  3648. tbicSubitemsChanged: begin
  3649. { LinkSubitems may have changed on FParentItem, e.g. on the root item
  3650. of a toolbar, so see if FCurParentItem needs updating }
  3651. UpdateCurParentItem;
  3652. end;
  3653. tbicSubMenuImagesChanged: begin
  3654. { In case the images were inherited from the actual parent instead of
  3655. the linked parent... }
  3656. if FParentItem <> FCurParentItem then
  3657. ImagesChanged;
  3658. end;
  3659. end;
  3660. end;
  3661. procedure TTBView.ImagesChanged;
  3662. begin
  3663. InvalidatePositions;
  3664. TryValidatePositions;
  3665. if Assigned(FOpenViewerView) then
  3666. FOpenViewerView.ImagesChanged;
  3667. end;
  3668. procedure TTBView.GivePriority(AViewer: TTBItemViewer);
  3669. { Move item to top of priority list. Rearranges items if necessary. }
  3670. var
  3671. I: Integer;
  3672. begin
  3673. if Assigned(FChevronParentView) then begin
  3674. I := AViewer.Index + FChevronParentView.FInternalViewersAtFront;
  3675. if I < FChevronParentView.FViewerCount then { range check just in case }
  3676. FChevronParentView.GivePriority(FChevronParentView.FViewers[I]);
  3677. Exit;
  3678. end;
  3679. if Assigned(FPriorityList) then begin
  3680. I := FPriorityList.IndexOf(AViewer);
  3681. if I <> -1 then begin
  3682. FPriorityList.Move(I, 0);
  3683. if not FValidated or AViewer.OffEdge then
  3684. UpdatePositions;
  3685. end;
  3686. end;
  3687. { Call GivePriority on parent view, so that if an item on a submenu is
  3688. clicked, the parent item of the submenu gets priority. }
  3689. if Assigned(FParentView) and Assigned(FParentView.FOpenViewer) then
  3690. FParentView.GivePriority(FParentView.FOpenViewer);
  3691. end;
  3692. function TTBView.HighestPriorityViewer: TTBItemViewer;
  3693. { Returns index of first visible, non-separator item at top of priority list,
  3694. or -1 if there are no items found }
  3695. var
  3696. I: Integer;
  3697. J: TTBItemViewer;
  3698. begin
  3699. ValidatePositions;
  3700. Result := nil;
  3701. if Assigned(FPriorityList) then begin
  3702. for I := 0 to FPriorityList.Count-1 do begin
  3703. J := FPriorityList[I];
  3704. if J.Show and not(tbisSeparator in J.Item.ItemStyle) then begin
  3705. Result := J;
  3706. Break;
  3707. end;
  3708. end;
  3709. end
  3710. else begin
  3711. for I := 0 to FViewerCount-1 do begin
  3712. J := FViewers[I];
  3713. if J.Show and not(tbisSeparator in J.Item.ItemStyle) then begin
  3714. Result := J;
  3715. Break;
  3716. end;
  3717. end;
  3718. end;
  3719. end;
  3720. procedure TTBView.StartTimer(const ATimer: TTBViewTimerID;
  3721. const Interval: Integer);
  3722. { Starts a timer. Stops any previously set timer of the same ID first.
  3723. Note: WM_TIMER messages generated by timers set by the method are handled
  3724. in PopupMessageLoop. }
  3725. begin
  3726. StopTimer(ATimer);
  3727. if (FWindow is TTBPopupWindow) and FWindow.HandleAllocated then begin
  3728. SetTimer(FWindow.Handle, ViewTimerBaseID + Ord(ATimer), Interval, nil);
  3729. Include(FActiveTimers, ATimer);
  3730. end;
  3731. end;
  3732. procedure TTBView.StopAllTimers;
  3733. var
  3734. I: TTBViewTimerID;
  3735. begin
  3736. for I := Low(I) to High(I) do
  3737. StopTimer(I);
  3738. end;
  3739. procedure TTBView.StopTimer(const ATimer: TTBViewTimerID);
  3740. begin
  3741. if ATimer in FActiveTimers then begin
  3742. if (FWindow is TTBPopupWindow) and FWindow.HandleAllocated then
  3743. KillTimer(FWindow.Handle, ViewTimerBaseID + Ord(ATimer));
  3744. Exclude(FActiveTimers, ATimer);
  3745. end;
  3746. end;
  3747. function TTBView.OpenChildPopup(const SelectFirstItem: Boolean): Boolean;
  3748. var
  3749. Item: TTBCustomItem;
  3750. begin
  3751. StopTimer(tiClose);
  3752. StopTimer(tiOpen);
  3753. if FSelected <> FOpenViewer then begin
  3754. CloseChildPopups;
  3755. if Assigned(FSelected) then begin
  3756. Item := FSelected.Item;
  3757. if Item.Enabled and (tbisSubmenu in Item.ItemStyle) then
  3758. Item.CreatePopup(Self, FSelected, not FIsToolbar, SelectFirstItem,
  3759. False, Point(0, 0), tbpaLeft);
  3760. end;
  3761. end;
  3762. Result := Assigned(FOpenViewer);
  3763. end;
  3764. procedure TTBView.CloseChildPopups;
  3765. begin
  3766. if Assigned(FOpenViewerView) then
  3767. FOpenViewerView.CloseChildPopups;
  3768. StopTimer(tiClose);
  3769. FOpenViewerWindow.Free;
  3770. FOpenViewerWindow := nil;
  3771. FOpenViewerView := nil;
  3772. FOpenViewer := nil;
  3773. end;
  3774. procedure TTBView.CancelChildPopups;
  3775. begin
  3776. if FIsToolbar then
  3777. Exclude(FState, vsDropDownMenus);
  3778. {MP}
  3779. if Assigned(FOpenViewerWindow) then
  3780. FOpenViewerWindow.Cancel;
  3781. CloseChildPopups;
  3782. end;
  3783. function TTBView.ViewerFromPoint(const P: TPoint): TTBItemViewer;
  3784. var
  3785. I: Integer;
  3786. begin
  3787. ValidatePositions;
  3788. for I := 0 to FViewerCount-1 do begin
  3789. if FViewers[I].Show and
  3790. PtInRect(FViewers[I].BoundsRect, P) then begin
  3791. Result := FViewers[I];
  3792. Exit;
  3793. end;
  3794. end;
  3795. Result := nil;
  3796. end;
  3797. procedure TTBView.NotifyFocusEvent;
  3798. { Notifies Active Accessibility of a change in "focus". Has no effect if the
  3799. view or the root view lacks the vsModal state, or if the modal loop is
  3800. ending (EndModal* was called). }
  3801. var
  3802. I, ChildID, J: Integer;
  3803. begin
  3804. { Note: We don't notify about windows not yet shown (e.g. a popup menu that
  3805. is still initializing) because that would probably confuse screen readers.
  3806. Also allocating a window handle at this point *might* not be a good idea. }
  3807. if (vsModal in FState) and (vsModal in GetRootView.FState) and
  3808. not IsModalEnding and
  3809. FWindow.HandleAllocated and IsWindowVisible(FWindow.Handle) then begin
  3810. if Assigned(FSelected) and FSelected.IsAccessible then
  3811. I := IndexOf(FSelected)
  3812. else
  3813. I := -1;
  3814. if (I < 0) and Assigned(FParentView) then begin
  3815. { If we have no selected item, report the the selected item on the parent
  3816. view as having the "focus".
  3817. Note: With standard menus, when you go from having a selection to no
  3818. selection on a submenu, it sends two focus events - first with the
  3819. client window as having the focus, then with the parent item. I
  3820. figure that's probably a bug, so I don't try to emulate that behavior
  3821. here. }
  3822. FParentView.NotifyFocusEvent;
  3823. end
  3824. else begin
  3825. if I >= 0 then begin
  3826. { Convert viewer index into a one-based child index.
  3827. (TTBViewAccObject.get_accChild does the inverse.) }
  3828. ChildID := 1;
  3829. for J := 0 to I-1 do
  3830. if FViewers[J].IsAccessible then
  3831. Inc(ChildID);
  3832. end
  3833. else begin
  3834. { If there is no (accessible) selection and no parent view, report
  3835. the client window itself as being "focused". This is what happens
  3836. when a standard context menu has no selection. }
  3837. ChildID := CHILDID_SELF;
  3838. end;
  3839. NotifyWinEvent(EVENT_OBJECT_FOCUS, FWindow.Handle, OBJID_CLIENT, ChildID);
  3840. end;
  3841. end;
  3842. end;
  3843. procedure TTBView.SetSelected(Value: TTBItemViewer);
  3844. begin
  3845. Select(Value, False);
  3846. end;
  3847. procedure TTBView.Select(Value: TTBItemViewer; ViaMouse: Boolean);
  3848. { Sets the current selection.
  3849. When the selection is changing it will also, if necessary, open/close child
  3850. popups. How exactly this works depends on the setting of ViaMouse. If
  3851. ViaMouse is True it will delay the opening/closing of popups using timers. }
  3852. var
  3853. OldSelected: TTBItemViewer;
  3854. NewMouseOverSelected: Boolean;
  3855. P: TPoint;
  3856. begin
  3857. OldSelected := FSelected;
  3858. if Value <> OldSelected then begin
  3859. { If there's a new selection and the parent item on the parent view
  3860. isn't currently selected, select it. Also stop any timer running on
  3861. the parent view. }
  3862. if Assigned(Value) and Assigned(FParentView) and
  3863. Assigned(FParentView.FOpenViewer) and
  3864. (FParentView.FSelected <> FParentView.FOpenViewer) then begin
  3865. FParentView.Selected := FParentView.FOpenViewer;
  3866. FParentView.StopTimer(tiClose);
  3867. FParentView.StopTimer(tiOpen);
  3868. end;
  3869. { Handle automatic closing of child popups }
  3870. if vsModal in FState then begin
  3871. { If the view is a toolbar, or if the new selection didn't come from
  3872. the mouse, close child popups immediately }
  3873. if FIsToolbar or not ViaMouse then begin
  3874. { Always stop any close timer because CloseChildPopups may not be
  3875. called below }
  3876. StopTimer(tiClose);
  3877. if Value <> FOpenViewer then
  3878. { ^ But don't close if selection is returning to the open item.
  3879. Needed for the "FParentView.Selected := FParentView.FOpenViewer"
  3880. line above to work. }
  3881. CloseChildPopups;
  3882. end
  3883. else begin
  3884. { Otherwise, delay-close any child popup }
  3885. if Assigned(FOpenViewerView) and not(tiClose in FActiveTimers) then
  3886. StartTimer(tiClose, GetMenuShowDelay);
  3887. end;
  3888. end;
  3889. CancelCapture;
  3890. if Assigned(OldSelected) then
  3891. OldSelected.Leaving;
  3892. FSelected := Value;
  3893. FSelectedViaMouse := ViaMouse;
  3894. end;
  3895. NewMouseOverSelected := False;
  3896. if Assigned(Value) and Assigned(FWindow) then begin
  3897. P := SmallPointToPoint(TSmallPoint(GetMessagePos()));
  3898. if FindDragTarget(P, True) = FWindow then begin
  3899. P := FWindow.ScreenToClient(P);
  3900. NewMouseOverSelected := (ViewerFromPoint(P) = Value);
  3901. if NewMouseOverSelected and FCapture and
  3902. not Value.IsPtInButtonPart(P.X - Value.BoundsRect.Left,
  3903. P.Y - Value.BoundsRect.Top) then
  3904. NewMouseOverSelected := False;
  3905. end;
  3906. end;
  3907. if Value <> OldSelected then begin
  3908. FMouseOverSelected := NewMouseOverSelected;
  3909. if Assigned(OldSelected) and (tbisRedrawOnSelChange in OldSelected.Item.ItemStyle) then
  3910. Invalidate(OldSelected);
  3911. if Assigned(Value) then begin
  3912. if tbisRedrawOnSelChange in Value.Item.ItemStyle then
  3913. Invalidate(Value);
  3914. Value.Entering(OldSelected);
  3915. end;
  3916. NotifyFocusEvent;
  3917. { Handle automatic opening of a child popup }
  3918. if vsModal in FState then begin
  3919. { If the view is a toolbar, immediately open any child popup }
  3920. if FIsToolbar then begin
  3921. if Assigned(Value) then begin
  3922. if ViaMouse and Assigned(FParentView) then begin
  3923. { On chevron popups, always drop down menus when mouse passes
  3924. over them, like Office 2000 }
  3925. Include(FState, vsDropDownMenus);
  3926. end;
  3927. if (vsDropDownMenus in FState) and
  3928. (ViaMouse or not(tbisNoAutoOpen in Value.Item.ItemStyle)) then
  3929. OpenChildPopup(not ViaMouse);
  3930. end;
  3931. end
  3932. else begin
  3933. { Otherwise, delay-open any child popup if the selection came from
  3934. the mouse }
  3935. StopTimer(tiOpen);
  3936. if ViaMouse and Assigned(Value) and (tbisSubmenu in Value.Item.ItemStyle) then
  3937. StartTimer(tiOpen, GetMenuShowDelay);
  3938. end;
  3939. end;
  3940. end
  3941. else if FMouseOverSelected <> NewMouseOverSelected then begin
  3942. FMouseOverSelected := NewMouseOverSelected;
  3943. if Assigned(Value) and FCapture and (tbisRedrawOnMouseOverChange in Value.Item.ItemStyle) then
  3944. Invalidate(Value);
  3945. end;
  3946. end;
  3947. procedure TTBView.UpdateSelection(const P: PPoint; const AllowNewSelection: Boolean);
  3948. { Called in response to a mouse movement, this method updates the current
  3949. selection, updates the vsMouseInWindow view state, and enables/disables
  3950. scroll timers. }
  3951. function IsPtInScrollArrow(ADownArrow: Boolean): Boolean;
  3952. var
  3953. P2: TPoint;
  3954. R: TRect;
  3955. begin
  3956. Result := False;
  3957. if (vsModal in FState) and (vsMouseInWindow in FState) and
  3958. Assigned(P) then begin
  3959. P2 := FWindow.ScreenToClient(P^);
  3960. R := FWindow.ClientRect;
  3961. if PtInRect(R, P2) then begin
  3962. if ADownArrow then
  3963. Result := FShowDownArrow and (P2.Y >= R.Bottom - tbMenuScrollArrowHeight)
  3964. else
  3965. Result := FShowUpArrow and (P2.Y < tbMenuScrollArrowHeight);
  3966. end;
  3967. end;
  3968. end;
  3969. var
  3970. NewSelected, ViewerAtPoint: TTBItemViewer;
  3971. P2: TPoint;
  3972. MouseWasInWindow: Boolean;
  3973. begin
  3974. ValidatePositions;
  3975. { If modal, default to keeping the existing selection }
  3976. if vsModal in FState then
  3977. NewSelected := FSelected
  3978. else
  3979. NewSelected := nil;
  3980. { Is the mouse inside the window? }
  3981. MouseWasInWindow := vsMouseInWindow in FState;
  3982. if Assigned(P) and Assigned(FWindow) and (FindDragTarget(P^, True) = FWindow) then begin
  3983. { If we're a popup window and the mouse is inside, default to no selection }
  3984. if FIsPopup then
  3985. NewSelected := nil;
  3986. Include(FState, vsMouseInWindow);
  3987. if AllowNewSelection or Assigned(FSelected) then begin
  3988. P2 := FWindow.ScreenToClient(P^);
  3989. ViewerAtPoint := ViewerFromPoint(P2);
  3990. if Assigned(ViewerAtPoint) then
  3991. NewSelected := ViewerAtPoint;
  3992. end;
  3993. end
  3994. else
  3995. Exclude(FState, vsMouseInWindow);
  3996. { If FCapture is True, don't allow the selection to change }
  3997. if FCapture and (NewSelected <> FSelected) then
  3998. NewSelected := FSelected;
  3999. { If we're a popup window and there is a selection... }
  4000. if FIsPopup and Assigned(NewSelected) then begin
  4001. { If the mouse just moved out of the window and no submenu was open,
  4002. remove the highlight }
  4003. if not FCapture and MouseWasInWindow and not(vsMouseInWindow in FState) and
  4004. (not Assigned(FOpenViewerView) or not(tbisSubmenu in NewSelected.Item.ItemStyle)) then
  4005. NewSelected := nil;
  4006. end;
  4007. { Now we set the new Selected value }
  4008. Select(NewSelected, True);
  4009. { Update scroll arrow timers }
  4010. if IsPtInScrollArrow(False) then begin
  4011. StopTimer(tiScrollDown);
  4012. if not(tiScrollUp in FActiveTimers) then
  4013. StartTimer(tiScrollUp, 100);
  4014. end
  4015. else if IsPtInScrollArrow(True) then begin
  4016. StopTimer(tiScrollUp);
  4017. if not(tiScrollDown in FActiveTimers) then
  4018. StartTimer(tiScrollDown, 100);
  4019. end
  4020. else begin
  4021. StopTimer(tiScrollUp);
  4022. StopTimer(tiScrollDown);
  4023. end;
  4024. end;
  4025. procedure TTBView.RecreateAllViewers;
  4026. var
  4027. Item: TTBCustomItem;
  4028. I: Integer;
  4029. begin
  4030. { Since the FViewers list is being rebuilt, FOpenViewer and FSelected
  4031. will no longer be valid, so ensure they're set to nil. }
  4032. CloseChildPopups;
  4033. Selected := nil;
  4034. InvalidatePositions;
  4035. FreeAndNil(FPriorityList);
  4036. FreeViewers;
  4037. FInternalViewersAtFront := 0;
  4038. FInternalViewersAtEnd := 0;
  4039. { MDI system menu item }
  4040. Item := GetMDISystemMenuItem;
  4041. if Assigned(Item) then
  4042. Inc(FInternalViewersAtFront, InsertItemViewers(FViewerCount, Item, 0,
  4043. False, False));
  4044. { Items }
  4045. if Assigned(FCurParentItem) then begin
  4046. for I := 0 to FCurParentItem.Count-1 do
  4047. InsertItemViewers(FViewerCount, FCurParentItem.FItems[I].Item, 0,
  4048. True, False);
  4049. end;
  4050. { MDI buttons item }
  4051. Item := GetMDIButtonsItem;
  4052. if Assigned(Item) then begin
  4053. for I := 0 to Item.Count-1 do
  4054. Inc(FInternalViewersAtEnd, InsertItemViewers(FViewerCount,
  4055. Item.FItems[I].Item, 0, False, False));
  4056. end;
  4057. { Chevron item }
  4058. Item := GetChevronItem;
  4059. if Assigned(Item) then
  4060. Inc(FInternalViewersAtEnd, InsertItemViewers(FViewerCount, Item, 0,
  4061. False, False));
  4062. end;
  4063. function TTBView.CalculatePositions(const CanMoveControls: Boolean;
  4064. const AOrientation: TTBViewOrientation;
  4065. AWrapOffset, AChevronOffset, AChevronSize: Integer;
  4066. var ABaseSize, TotalSize: TPoint;
  4067. var AWrappedLines: Integer): Boolean;
  4068. { Returns True if the positions have changed }
  4069. type
  4070. PTempPosition = ^TTempPosition;
  4071. TTempPosition = record
  4072. BoundsRect: TRect;
  4073. Show, OffEdge, LineSep, Clipped, SameWidth: Boolean;
  4074. end;
  4075. PTempPositionArray = ^TTempPositionArray;
  4076. TTempPositionArray = array[0..$7FFFFFFF div SizeOf(TTempPosition)-1] of TTempPosition;
  4077. var
  4078. DC: HDC;
  4079. LeftX, TopY, CurX, CurY, I: Integer;
  4080. NewPositions: PTempPositionArray;
  4081. GroupSplit, DidWrap: Boolean;
  4082. LineStart, HighestHeightOnLine, HighestWidthOnLine: Integer;
  4083. function GetSizeOfGroup(const StartingIndex: Integer): Integer;
  4084. var
  4085. I: Integer;
  4086. begin
  4087. Result := 0;
  4088. for I := StartingIndex to FViewerCount-1 do begin
  4089. if not NewPositions[I].Show then
  4090. Continue;
  4091. if tbisSeparator in FViewers[I].Item.ItemStyle then
  4092. Break;
  4093. with NewPositions[I] do begin
  4094. if AOrientation <> tbvoVertical then
  4095. Inc(Result, BoundsRect.Right)
  4096. else
  4097. Inc(Result, BoundsRect.Bottom);
  4098. end;
  4099. end;
  4100. end;
  4101. procedure Mirror;
  4102. { Reverses the horizontal ordering (i.e. first item becomes last) }
  4103. var
  4104. I, NewRight: Integer;
  4105. begin
  4106. for I := 0 to FViewerCount-1 do
  4107. with NewPositions[I] do
  4108. if Show then begin
  4109. NewRight := TotalSize.X - BoundsRect.Left;
  4110. BoundsRect.Left := TotalSize.X - BoundsRect.Right;
  4111. BoundsRect.Right := NewRight;
  4112. end;
  4113. end;
  4114. procedure HandleMaxHeight;
  4115. { Decreases, if necessary, the height of the view to FMaxHeight, and adjusts
  4116. the visibility of the scroll arrows }
  4117. var
  4118. MaxOffset, I, MaxTop, MaxBottom: Integer;
  4119. begin
  4120. FShowUpArrow := False;
  4121. FShowDownArrow := False;
  4122. if (FMaxHeight > 0) and (TotalSize.Y > FMaxHeight) then begin
  4123. MaxOffset := TotalSize.Y - FMaxHeight;
  4124. if FScrollOffset > MaxOffset then
  4125. FScrollOffset := MaxOffset;
  4126. if FScrollOffset < 0 then
  4127. FScrollOffset := 0;
  4128. FShowUpArrow := (FScrollOffset > 0);
  4129. FShowDownArrow := (FScrollOffset < MaxOffset);
  4130. MaxTop := 0;
  4131. if FShowUpArrow then
  4132. MaxTop := tbMenuScrollArrowHeight;
  4133. MaxBottom := FMaxHeight;
  4134. if FShowDownArrow then
  4135. Dec(MaxBottom, tbMenuScrollArrowHeight);
  4136. for I := 0 to FViewerCount-1 do begin
  4137. if not IsRectEmpty(NewPositions[I].BoundsRect) then begin
  4138. OffsetRect(NewPositions[I].BoundsRect, 0, -FScrollOffset);
  4139. if NewPositions[I].Show and
  4140. ((NewPositions[I].BoundsRect.Top < MaxTop) or
  4141. (NewPositions[I].BoundsRect.Bottom > MaxBottom)) then begin
  4142. NewPositions[I].Show := False;
  4143. NewPositions[I].Clipped := True;
  4144. end;
  4145. end;
  4146. end;
  4147. TotalSize.Y := FMaxHeight;
  4148. end
  4149. else
  4150. FScrollOffset := 0;
  4151. end;
  4152. procedure FinalizeLine(const LineEnd: Integer; const LastLine: Boolean);
  4153. var
  4154. I, RightAlignStart: Integer;
  4155. Item: TTBCustomItem;
  4156. IsButton: Boolean;
  4157. Pos: PTempPosition;
  4158. Z: Integer;
  4159. begin
  4160. if LineStart <> -1 then begin
  4161. if DidWrap and (FChevronParentView = nil) then begin
  4162. { When wrapping on a docked toolbar, extend TotalSize.X/Y to
  4163. AWrapOffset so that the toolbar always fills the whole row }
  4164. if (AOrientation = tbvoHorizontal) and (TotalSize.X < AWrapOffset) then
  4165. TotalSize.X := AWrapOffset
  4166. else if (AOrientation = tbvoVertical) and (TotalSize.Y < AWrapOffset) then
  4167. TotalSize.Y := AWrapOffset;
  4168. end;
  4169. RightAlignStart := -1;
  4170. for I := LineStart to LineEnd do begin
  4171. Pos := @NewPositions[I];
  4172. if not Pos.Show then
  4173. Continue;
  4174. Item := FViewers[I].Item;
  4175. if (RightAlignStart < 0) and (tbisRightAlign in Item.ItemStyle) then
  4176. RightAlignStart := I;
  4177. IsButton := FIsToolbar or (tboToolbarSize in Item.FEffectiveOptions);
  4178. if FIsToolbar then begin
  4179. if LastLine and not DidWrap and (AOrientation <> tbvoFloating) then begin
  4180. { In case the toolbar is docked next to a taller/wider toolbar... }
  4181. HighestWidthOnLine := TotalSize.X;
  4182. HighestHeightOnLine := TotalSize.Y;
  4183. end;
  4184. { Make separators on toolbars as tall/wide as the tallest/widest item }
  4185. if [tbisSeparator, tbisStretch] * Item.ItemStyle <> [] then begin
  4186. if AOrientation <> tbvoVertical then
  4187. Pos.BoundsRect.Bottom := Pos.BoundsRect.Top + HighestHeightOnLine
  4188. else
  4189. Pos.BoundsRect.Right := Pos.BoundsRect.Left + HighestWidthOnLine;
  4190. end
  4191. else begin
  4192. { Center the item }
  4193. if AOrientation <> tbvoVertical then begin
  4194. Z := (HighestHeightOnLine - (Pos.BoundsRect.Bottom - Pos.BoundsRect.Top)) div 2;
  4195. Inc(Pos.BoundsRect.Top, Z);
  4196. Inc(Pos.BoundsRect.Bottom, Z);
  4197. end
  4198. else begin
  4199. Z := (HighestWidthOnLine - (Pos.BoundsRect.Right - Pos.BoundsRect.Left)) div 2;
  4200. Inc(Pos.BoundsRect.Left, Z);
  4201. Inc(Pos.BoundsRect.Right, Z);
  4202. end;
  4203. end;
  4204. end
  4205. else begin
  4206. { Make items in a menu as wide as the widest item }
  4207. if not IsButton then begin
  4208. with Pos.BoundsRect do Right := Left + HighestWidthOnLine;
  4209. end;
  4210. end;
  4211. end;
  4212. if RightAlignStart >= 0 then begin
  4213. Z := 0;
  4214. for I := LineEnd downto RightAlignStart do begin
  4215. Pos := @NewPositions[I];
  4216. if not Pos.Show then
  4217. Continue;
  4218. if AOrientation <> tbvoVertical then
  4219. Z := Min(AWrapOffset, TotalSize.X) - Pos.BoundsRect.Right
  4220. else
  4221. Z := Min(AWrapOffset, TotalSize.Y) - Pos.BoundsRect.Bottom;
  4222. Break;
  4223. end;
  4224. if Z > 0 then begin
  4225. for I := RightAlignStart to LineEnd do begin
  4226. Pos := @NewPositions[I];
  4227. if not Pos.Show then
  4228. Continue;
  4229. if AOrientation <> tbvoVertical then begin
  4230. Inc(Pos.BoundsRect.Left, Z);
  4231. Inc(Pos.BoundsRect.Right, Z);
  4232. end
  4233. else begin
  4234. Inc(Pos.BoundsRect.Top, Z);
  4235. Inc(Pos.BoundsRect.Bottom, Z);
  4236. end;
  4237. end;
  4238. end;
  4239. end;
  4240. end;
  4241. LineStart := -1;
  4242. HighestHeightOnLine := 0;
  4243. HighestWidthOnLine := 0;
  4244. end;
  4245. procedure PositionItem(const CurIndex: Integer; var Pos: TTempPosition);
  4246. var
  4247. O, X, Y: Integer;
  4248. IsLineSep, Vert: Boolean;
  4249. begin
  4250. if LineStart = -1 then begin
  4251. LineStart := CurIndex;
  4252. HighestHeightOnLine := 0;
  4253. HighestWidthOnLine := 0;
  4254. end;
  4255. IsLineSep := False;
  4256. Vert := (AOrientation = tbvoVertical);
  4257. if not Vert then
  4258. O := CurX
  4259. else
  4260. O := CurY;
  4261. if (AWrapOffset > 0) and (O > 0) then begin
  4262. if not Vert then
  4263. Inc(O, Pos.BoundsRect.Right)
  4264. else
  4265. Inc(O, Pos.BoundsRect.Bottom);
  4266. if (tbisSeparator in FViewers[CurIndex].Item.ItemStyle) and
  4267. ((GroupSplit and not(tbisNoLineBreak in FViewers[CurIndex].Item.ItemStyle))
  4268. or (O + GetSizeOfGroup(CurIndex+1) > AWrapOffset)) then begin
  4269. DidWrap := True;
  4270. Inc(AWrappedLines);
  4271. if not Vert then begin
  4272. CurX := 0;
  4273. Inc(CurY, HighestHeightOnLine);
  4274. end
  4275. else begin
  4276. CurY := 0;
  4277. Inc(CurX, HighestWidthOnLine);
  4278. end;
  4279. FinalizeLine(CurIndex-1, False);
  4280. LineStart := CurIndex+1;
  4281. if not Vert then begin
  4282. Pos.BoundsRect.Right := 0;
  4283. Pos.BoundsRect.Bottom := tbLineSpacing;
  4284. end
  4285. else begin
  4286. Pos.BoundsRect.Right := tbLineSpacing;
  4287. Pos.BoundsRect.Bottom := 0;
  4288. end;
  4289. Pos.LineSep := True;
  4290. IsLineSep := True;
  4291. end
  4292. else if O > AWrapOffset then begin
  4293. { proceed to next row }
  4294. DidWrap := True;
  4295. Inc(AWrappedLines);
  4296. if not Vert then begin
  4297. CurX := LeftX;
  4298. Inc(CurY, HighestHeightOnLine);
  4299. end
  4300. else begin
  4301. CurY := TopY;
  4302. Inc(CurX, HighestWidthOnLine);
  4303. end;
  4304. GroupSplit := True;
  4305. FinalizeLine(CurIndex-1, False);
  4306. LineStart := CurIndex;
  4307. end;
  4308. end;
  4309. if Pos.BoundsRect.Bottom > HighestHeightOnLine then
  4310. HighestHeightOnLine := Pos.BoundsRect.Bottom;
  4311. if Pos.BoundsRect.Right > HighestWidthOnLine then
  4312. HighestWidthOnLine := Pos.BoundsRect.Right;
  4313. X := CurX;
  4314. Y := CurY;
  4315. if X < 0 then X := 0;
  4316. if Y < 0 then Y := 0;
  4317. OffsetRect(Pos.BoundsRect, X, Y);
  4318. if IsLineSep then begin
  4319. if not Vert then begin
  4320. CurX := LeftX;
  4321. Inc(CurY, tbLineSpacing);
  4322. end
  4323. else begin
  4324. CurY := TopY;
  4325. Inc(CurX, tbLineSpacing);
  4326. end;
  4327. GroupSplit := False;
  4328. end;
  4329. end;
  4330. var
  4331. SaveOrientation: TTBViewOrientation;
  4332. ChevronItem: TTBCustomItem;
  4333. CalcCanvas: TCanvas;
  4334. LastWasSep, LastWasButton, IsButton, IsControl: Boolean;
  4335. Item: TTBCustomItem;
  4336. Ctl: TControl;
  4337. ChangedBold: Boolean;
  4338. HighestSameWidthViewerWidth, Total, J, TotalVisibleItems: Integer;
  4339. IsFirst: Boolean;
  4340. Viewer: TTBItemViewer;
  4341. UseChevron, NonControlsOffEdge, TempViewerCreated: Boolean;
  4342. Margins: TRect;
  4343. label 1;
  4344. begin
  4345. SaveOrientation := FOrientation;
  4346. AWrappedLines := 1;
  4347. ChevronItem := GetChevronItem;
  4348. NewPositions := nil;
  4349. DC := 0;
  4350. CalcCanvas := nil;
  4351. try
  4352. FOrientation := AOrientation;
  4353. CalcCanvas := TCanvas.Create;
  4354. DC := GetDC(0);
  4355. CalcCanvas.Handle := DC;
  4356. CalcCanvas.Font.Assign(GetFont);
  4357. NewPositions := AllocMem(FViewerCount * SizeOf(TTempPosition));
  4358. { Figure out which items should be shown }
  4359. LastWasSep := True; { set to True initially so it won't show leading seps }
  4360. for I := 0 to FViewerCount-1 do begin
  4361. Item := FViewers[I].Item;
  4362. IsControl := Item is TTBControlItem;
  4363. with NewPositions[I] do begin
  4364. { Show is initially False since AllocMem initializes to zero }
  4365. if Item = ChevronItem then
  4366. Continue;
  4367. if Assigned(FChevronParentView) then begin
  4368. if IsControl then
  4369. Continue;
  4370. FChevronParentView.ValidatePositions;
  4371. J := I + FChevronParentView.FInternalViewersAtFront;
  4372. if J < FChevronParentView.FViewerCount then
  4373. { range check just in case }
  4374. Viewer := FChevronParentView.FViewers[J]
  4375. else
  4376. Viewer := nil;
  4377. if (Viewer = nil) or (not Viewer.OffEdge and not(tbisSeparator in Item.ItemStyle)) then
  4378. Continue;
  4379. end;
  4380. if not IsControl then begin
  4381. if not(tbisEmbeddedGroup in Item.ItemStyle) or FCustomizing then begin
  4382. Show := Item.Visible;
  4383. { Don't display two consecutive separators }
  4384. if Show then begin
  4385. if (tbisSeparator in Item.ItemStyle) and LastWasSep then
  4386. Show := False;
  4387. LastWasSep := tbisSeparator in Item.ItemStyle;
  4388. end;
  4389. end;
  4390. end
  4391. else begin
  4392. { Controls can only be rendered on a single Parent, so only
  4393. include the control if its parent is currently equal to
  4394. FWindow }
  4395. Ctl := TTBControlItem(Item).FControl;
  4396. if Assigned(Ctl) and Assigned(FWindow) and (Ctl.Parent = FWindow) and
  4397. (Ctl.Visible or (csDesigning in Ctl.ComponentState)) then begin
  4398. Show := True;
  4399. LastWasSep := False;
  4400. end;
  4401. end;
  4402. end;
  4403. end;
  4404. { Hide any trailing separators, so that they aren't included in the
  4405. base size }
  4406. for I := FViewerCount-1 downto 0 do begin
  4407. with NewPositions[I] do
  4408. if Show then begin
  4409. if not(tbisSeparator in FViewers[I].Item.ItemStyle) then
  4410. Break;
  4411. Show := False;
  4412. end;
  4413. end;
  4414. { Calculate sizes of all the items }
  4415. HighestSameWidthViewerWidth := 0;
  4416. for I := 0 to FViewerCount-1 do begin
  4417. Item := FViewers[I].Item;
  4418. IsControl := Item is TTBControlItem;
  4419. with NewPositions[I] do begin
  4420. { BoundsRect is currently empty since AllocMem initializes to zero }
  4421. if not Show then
  4422. Continue;
  4423. if not IsControl then begin
  4424. ChangedBold := False;
  4425. if tboDefault in Item.EffectiveOptions then
  4426. with CalcCanvas.Font do
  4427. if not(fsBold in Style) then begin
  4428. ChangedBold := True;
  4429. Style := Style + [fsBold];
  4430. end;
  4431. Viewer := FViewers[I];
  4432. TempViewerCreated := False;
  4433. if Item.NeedToRecreateViewer(Viewer) then begin
  4434. if CanMoveControls then begin
  4435. RecreateItemViewer(I);
  4436. Viewer := FViewers[I];
  4437. end
  4438. else begin
  4439. Viewer := Item.GetItemViewerClass(Self).Create(Self, Item, 0);
  4440. TempViewerCreated := True;
  4441. end;
  4442. end;
  4443. try
  4444. Viewer.CalcSize(CalcCanvas, BoundsRect.Right, BoundsRect.Bottom);
  4445. if Viewer.UsesSameWidth then begin
  4446. SameWidth := True;
  4447. if (BoundsRect.Right > HighestSameWidthViewerWidth) then
  4448. HighestSameWidthViewerWidth := BoundsRect.Right;
  4449. end;
  4450. finally
  4451. if TempViewerCreated then
  4452. Viewer.Free;
  4453. end;
  4454. if ChangedBold then
  4455. with CalcCanvas.Font do
  4456. Style := Style - [fsBold];
  4457. end
  4458. else begin
  4459. Ctl := TTBControlItem(Item).FControl;
  4460. BoundsRect.Right := Ctl.Width;
  4461. BoundsRect.Bottom := Ctl.Height;
  4462. end;
  4463. end;
  4464. end;
  4465. { Increase widths of SameWidth items if necessary. Also calculate
  4466. ABaseSize.X (or Y). }
  4467. ABaseSize.X := 0;
  4468. ABaseSize.Y := 0;
  4469. for I := 0 to FViewerCount-1 do begin
  4470. with NewPositions[I] do begin
  4471. if SameWidth and (BoundsRect.Right < HighestSameWidthViewerWidth) then
  4472. BoundsRect.Right := HighestSameWidthViewerWidth;
  4473. if AOrientation <> tbvoVertical then
  4474. Inc(ABaseSize.X, BoundsRect.Right)
  4475. else
  4476. Inc(ABaseSize.Y, BoundsRect.Bottom);
  4477. end;
  4478. end;
  4479. { Hide partially visible items, mark them as 'OffEdge' }
  4480. if AOrientation <> tbvoVertical then
  4481. Total := ABaseSize.X
  4482. else
  4483. Total := ABaseSize.Y;
  4484. NonControlsOffEdge := False;
  4485. UseChevron := Assigned(ChevronItem) and (AChevronOffset > 0) and
  4486. (Total > AChevronOffset);
  4487. if UseChevron then begin
  4488. Dec(AChevronOffset, AChevronSize);
  4489. while Total > AChevronOffset do begin
  4490. { Count number of items. Stop loop if <= 1 }
  4491. TotalVisibleItems := 0;
  4492. for I := FViewerCount-1 downto 0 do begin
  4493. if NewPositions[I].Show and not(tbisSeparator in FViewers[I].Item.ItemStyle) then
  4494. Inc(TotalVisibleItems);
  4495. end;
  4496. if TotalVisibleItems <= 1 then
  4497. Break;
  4498. { Hide any trailing separators }
  4499. for I := FViewerCount-1 downto 0 do begin
  4500. with NewPositions[I] do
  4501. if Show then begin
  4502. if not(tbisSeparator in FViewers[I].Item.ItemStyle) then
  4503. Break;
  4504. Show := False;
  4505. if AOrientation <> tbvoVertical then
  4506. Dec(Total, BoundsRect.Right)
  4507. else
  4508. Dec(Total, BoundsRect.Bottom);
  4509. goto 1;
  4510. end;
  4511. end;
  4512. { Find an item to hide }
  4513. if Assigned(FPriorityList) then
  4514. I := FPriorityList.Count-1
  4515. else
  4516. I := FViewerCount-1;
  4517. while I >= 0 do begin
  4518. if Assigned(FPriorityList) then begin
  4519. Viewer := FPriorityList[I];
  4520. J := Viewer.Index;
  4521. end
  4522. else begin
  4523. Viewer := FViewers[I];
  4524. J := I;
  4525. end;
  4526. if NewPositions[J].Show and not(tbisSeparator in Viewer.Item.ItemStyle) then begin
  4527. NewPositions[J].Show := False;
  4528. NewPositions[J].OffEdge := True;
  4529. if AOrientation <> tbvoVertical then
  4530. Dec(Total, NewPositions[J].BoundsRect.Right)
  4531. else
  4532. Dec(Total, NewPositions[J].BoundsRect.Bottom);
  4533. if not NonControlsOffEdge and not(Viewer.Item is TTBControlItem) then
  4534. NonControlsOffEdge := True;
  4535. goto 1;
  4536. end;
  4537. Dec(I);
  4538. end;
  4539. Break; { prevent endless loop }
  4540. 1:
  4541. { Don't show two consecutive separators }
  4542. LastWasSep := True; { set to True initially so it won't show leading seps }
  4543. for J := 0 to FViewerCount-1 do begin
  4544. Item := FViewers[J].Item;
  4545. with NewPositions[J] do begin
  4546. if Show then begin
  4547. if (tbisSeparator in Item.ItemStyle) and LastWasSep then begin
  4548. Show := False;
  4549. if AOrientation <> tbvoVertical then
  4550. Dec(Total, BoundsRect.Right)
  4551. else
  4552. Dec(Total, BoundsRect.Bottom);
  4553. end;
  4554. LastWasSep := tbisSeparator in Item.ItemStyle;
  4555. end;
  4556. end;
  4557. end;
  4558. end;
  4559. end;
  4560. { Hide any trailing separators after items were hidden }
  4561. for I := FViewerCount-1 downto 0 do begin
  4562. with NewPositions[I] do
  4563. if Show then begin
  4564. if not(tbisSeparator in FViewers[I].Item.ItemStyle) then
  4565. Break;
  4566. Show := False;
  4567. end;
  4568. end;
  4569. { Set the ABaseSize.Y (or X) *after* items were hidden }
  4570. for I := 0 to FViewerCount-1 do begin
  4571. with NewPositions[I] do
  4572. if Show then begin
  4573. if AOrientation <> tbvoVertical then begin
  4574. if BoundsRect.Bottom > ABaseSize.Y then
  4575. ABaseSize.Y := BoundsRect.Bottom;
  4576. end
  4577. else begin
  4578. if BoundsRect.Right > ABaseSize.X then
  4579. ABaseSize.X := BoundsRect.Right;
  4580. end;
  4581. end;
  4582. end;
  4583. { On menus, set all non-separator items to be as tall as the tallest item }
  4584. {if not FIsToolbar then begin
  4585. J := 0;
  4586. for I := 0 to FViewerCount-1 do begin
  4587. Item := FViewers[I].Item;
  4588. with NewPositions[I] do
  4589. if Show and not(tbisSeparator in Item.ItemStyle) and
  4590. not(tboToolbarSize in Item.FEffectiveOptions) and
  4591. (BoundsRect.Bottom - BoundsRect.Top > J) then
  4592. J := BoundsRect.Bottom - BoundsRect.Top;
  4593. end;
  4594. for I := 0 to FViewerCount-1 do begin
  4595. Item := FViewers[I].Item;
  4596. with NewPositions[I] do
  4597. if Show and not(tbisSeparator in Item.ItemStyle) and
  4598. not(tboToolbarSize in Item.FEffectiveOptions) then
  4599. BoundsRect.Bottom := BoundsRect.Top + J;
  4600. end;
  4601. end;}
  4602. { Calculate the position of the items }
  4603. GetMargins(AOrientation, Margins);
  4604. LeftX := Margins.Left;
  4605. TopY := Margins.Top;
  4606. if AWrapOffset > 0 then begin
  4607. Dec(AWrapOffset, Margins.Right);
  4608. if AWrapOffset < 1 then AWrapOffset := 1;
  4609. end;
  4610. CurX := LeftX;
  4611. CurY := TopY;
  4612. GroupSplit := False;
  4613. DidWrap := False;
  4614. LastWasButton := FIsToolbar;
  4615. LineStart := -1;
  4616. for I := 0 to FViewerCount-1 do begin
  4617. Item := FViewers[I].Item;
  4618. with NewPositions[I] do begin
  4619. if not Show then
  4620. Continue;
  4621. IsButton := FIsToolbar or (tboToolbarSize in Item.FEffectiveOptions);
  4622. if LastWasButton and not IsButton then begin
  4623. { On a menu, if last item was a button and the current item isn't,
  4624. proceed to next row }
  4625. CurX := LeftX;
  4626. CurY := TotalSize.Y;
  4627. end;
  4628. LastWasButton := IsButton;
  4629. PositionItem(I, NewPositions[I]);
  4630. if IsButton and (AOrientation <> tbvoVertical) then
  4631. Inc(CurX, BoundsRect.Right - BoundsRect.Left)
  4632. else
  4633. Inc(CurY, BoundsRect.Bottom - BoundsRect.Top);
  4634. if BoundsRect.Right > TotalSize.X then
  4635. TotalSize.X := BoundsRect.Right;
  4636. if BoundsRect.Bottom > TotalSize.Y then
  4637. TotalSize.Y := BoundsRect.Bottom;
  4638. end;
  4639. end;
  4640. if FViewerCount <> 0 then
  4641. FinalizeLine(FViewerCount-1, True);
  4642. Inc(TotalSize.X, Margins.Right);
  4643. Inc(TotalSize.Y, Margins.Bottom);
  4644. if AOrientation = tbvoVertical then
  4645. Mirror;
  4646. HandleMaxHeight;
  4647. if CanMoveControls then begin
  4648. for I := 0 to FViewerCount-1 do begin
  4649. Item := FViewers[I].Item;
  4650. if Item is TTBControlItem then begin
  4651. if NewPositions[I].Show then begin
  4652. Ctl := TTBControlItem(Item).FControl;
  4653. if not EqualRect(NewPositions[I].BoundsRect, Ctl.BoundsRect) then
  4654. Ctl.BoundsRect := NewPositions[I].BoundsRect;
  4655. end
  4656. else if NewPositions[I].OffEdge or NewPositions[I].Clipped then begin
  4657. { Simulate hiding of OddEdge controls by literally moving them
  4658. off the edge. Do the same for Clipped controls. }
  4659. Ctl := TTBControlItem(Item).FControl;
  4660. Ctl.SetBounds(FWindow.ClientWidth, FWindow.ClientHeight,
  4661. Ctl.Width, Ctl.Height);
  4662. end;
  4663. end;
  4664. end;
  4665. end;
  4666. { Set size of line separators }
  4667. if FIsToolbar then
  4668. for I := 0 to FViewerCount-1 do begin
  4669. Item := FViewers[I].Item;
  4670. with NewPositions[I] do
  4671. if Show and (tbisSeparator in Item.ItemStyle) and
  4672. LineSep then begin
  4673. if AOrientation <> tbvoVertical then
  4674. BoundsRect.Right := TotalSize.X
  4675. else
  4676. BoundsRect.Bottom := TotalSize.Y;
  4677. end;
  4678. end;
  4679. { Position the chevron item }
  4680. if UseChevron then begin
  4681. if CanMoveControls then
  4682. ChevronItem.Enabled := NonControlsOffEdge;
  4683. NewPositions[FViewerCount-1].Show := True;
  4684. I := AChevronOffset;
  4685. if AOrientation <> tbvoVertical then begin
  4686. if I < TotalSize.X then
  4687. I := TotalSize.X;
  4688. NewPositions[FViewerCount-1].BoundsRect := Bounds(I, 0,
  4689. AChevronSize, TotalSize.Y);
  4690. end
  4691. else begin
  4692. if I < TotalSize.Y then
  4693. I := TotalSize.Y;
  4694. NewPositions[FViewerCount-1].BoundsRect := Bounds(0, I,
  4695. TotalSize.X, AChevronSize);
  4696. end;
  4697. end;
  4698. { Commit changes }
  4699. Result := False;
  4700. if CanMoveControls then begin
  4701. for I := 0 to FViewerCount-1 do begin
  4702. if not Result and
  4703. (not EqualRect(FViewers[I].BoundsRect, NewPositions[I].BoundsRect) or
  4704. (FViewers[I].Show <> NewPositions[I].Show) or
  4705. (tbisLineSep in FViewers[I].State <> NewPositions[I].LineSep)) then
  4706. Result := True;
  4707. FViewers[I].FBoundsRect := NewPositions[I].BoundsRect;
  4708. FViewers[I].FShow := NewPositions[I].Show;
  4709. FViewers[I].FOffEdge := NewPositions[I].OffEdge;
  4710. FViewers[I].FClipped := NewPositions[I].Clipped;
  4711. if NewPositions[I].LineSep then
  4712. Include(FViewers[I].State, tbisLineSep)
  4713. else
  4714. Exclude(FViewers[I].State, tbisLineSep);
  4715. end;
  4716. end;
  4717. finally
  4718. FOrientation := SaveOrientation;
  4719. if Assigned(CalcCanvas) then
  4720. CalcCanvas.Handle := 0;
  4721. if DC <> 0 then ReleaseDC(0, DC);
  4722. CalcCanvas.Free;
  4723. FreeMem(NewPositions);
  4724. end;
  4725. if (ABaseSize.X = 0) or (ABaseSize.Y = 0) then begin
  4726. { If there are no visible items... }
  4727. {}{scale this?}
  4728. ABaseSize.X := 23;
  4729. ABaseSize.Y := 22;
  4730. if TotalSize.X < 23 then TotalSize.X := 23;
  4731. if TotalSize.Y < 22 then TotalSize.Y := 22;
  4732. end;
  4733. end;
  4734. procedure TTBView.DoUpdatePositions(var ASize: TPoint);
  4735. { This is called by UpdatePositions }
  4736. var
  4737. Bmp: TBitmap;
  4738. CtlCanvas: TControlCanvas;
  4739. WrappedLines: Integer;
  4740. begin
  4741. { Don't call InvalidatePositions before CalculatePositions so that
  4742. endless recursion doesn't happen if an item's CalcSize uses a method that
  4743. calls ValidatePositions }
  4744. if not CalculatePositions(True, FOrientation, FWrapOffset, FChevronOffset,
  4745. FChevronSize, FBaseSize, ASize, WrappedLines) then begin
  4746. { If the new positions are identical to the previous ones, continue using
  4747. the previous ones, and don't redraw }
  4748. FValidated := True;
  4749. { Just because the positions are the same doesn't mean the size hasn't
  4750. changed. (If a shrunken toolbar moves between docks, the positions of
  4751. the non-OffEdge items may be the same on the new dock as on the old
  4752. dock.) }
  4753. AutoSize(ASize.X, ASize.Y);
  4754. end
  4755. else begin
  4756. if not(csDesigning in ComponentState) then begin
  4757. FValidated := True;
  4758. { Need to call ValidateRect before AutoSize, otherwise Windows will
  4759. erase the client area during a resize }
  4760. if FWindow.HandleAllocated then
  4761. ValidateRect(FWindow.Handle, nil);
  4762. AutoSize(ASize.X, ASize.Y);
  4763. if Assigned(FWindow) and FWindow.HandleAllocated and
  4764. IsWindowVisible(FWindow.Handle) and
  4765. (FWindow.ClientWidth > 0) and (FWindow.ClientHeight > 0) then begin
  4766. CtlCanvas := nil;
  4767. Bmp := TBitmap.Create;
  4768. try
  4769. CtlCanvas := TControlCanvas.Create;
  4770. CtlCanvas.Control := FWindow;
  4771. Bmp.Width := FWindow.ClientWidth;
  4772. Bmp.Height := FWindow.ClientHeight;
  4773. SendMessage(FWindow.Handle, WM_ERASEBKGND, WPARAM(Bmp.Canvas.Handle), 0);
  4774. SendMessage(FWindow.Handle, WM_PAINT, WPARAM(Bmp.Canvas.Handle), 0);
  4775. BitBlt(CtlCanvas.Handle, 0, 0, Bmp.Width, Bmp.Height,
  4776. Bmp.Canvas.Handle, 0, 0, SRCCOPY);
  4777. ValidateRect(FWindow.Handle, nil);
  4778. finally
  4779. CtlCanvas.Free;
  4780. Bmp.Free;
  4781. end;
  4782. end;
  4783. end
  4784. else begin
  4785. { Delphi's handling of canvases is different at design time -- child
  4786. controls aren't clipped from a parent control's canvas, so the above
  4787. offscreen rendering code doesn't work right at design-time }
  4788. InvalidatePositions;
  4789. FValidated := True;
  4790. AutoSize(ASize.X, ASize.Y);
  4791. end;
  4792. end;
  4793. end;
  4794. function TTBView.UpdatePositions: TPoint;
  4795. { Called whenever the size or orientation of a view changes. When items are
  4796. added or removed from the view, InvalidatePositions must be called instead,
  4797. otherwise the view may not be redrawn properly. }
  4798. begin
  4799. Result.X := 0;
  4800. Result.Y := 0;
  4801. DoUpdatePositions(Result);
  4802. end;
  4803. procedure TTBView.AutoSize(AWidth, AHeight: Integer);
  4804. begin
  4805. end;
  4806. function TTBView.GetChevronItem: TTBCustomItem;
  4807. begin
  4808. Result := nil;
  4809. end;
  4810. procedure TTBView.GetMargins(AOrientation: TTBViewOrientation;
  4811. var Margins: TRect);
  4812. begin
  4813. if AOrientation = tbvoFloating then begin
  4814. Margins.Left := 4;
  4815. Margins.Top := 2;
  4816. Margins.Right := 4;
  4817. Margins.Bottom := 1;
  4818. end
  4819. else begin
  4820. Margins.Left := 0;
  4821. Margins.Top := 0;
  4822. Margins.Right := 0;
  4823. Margins.Bottom := 0;
  4824. end;
  4825. end;
  4826. function TTBView.GetMDIButtonsItem: TTBCustomItem;
  4827. begin
  4828. Result := nil;
  4829. end;
  4830. function TTBView.GetMDISystemMenuItem: TTBCustomItem;
  4831. begin
  4832. Result := nil;
  4833. end;
  4834. function TTBView.GetFont: TFont;
  4835. begin
  4836. Result := GetToolbarFont(GetMonitorPixelsPerInch(GetMonitor));
  4837. if not Assigned(Result) then
  4838. begin
  4839. { ToolbarFont is destroyed during unit finalization, but in rare cases
  4840. this method may end up being called from ValidatePositions *after*
  4841. unit finalization if Application.Run is never called; see the
  4842. "EConvertError" newsgroup thread. We can't return nil because that would
  4843. cause an exception in the calling function, so just return the window
  4844. font. It's not the *right* font, but it shouldn't matter since the app
  4845. is exiting anyway. }
  4846. Result := TControlAccess(FWindow).Font;
  4847. end;
  4848. end;
  4849. procedure TTBView.DrawItem(Viewer: TTBItemViewer; DrawTo: TCanvas;
  4850. Offscreen: Boolean);
  4851. const
  4852. COLOR_MENUHILIGHT = 29;
  4853. clMenuHighlight = TColor(COLOR_MENUHILIGHT or $80000000);
  4854. var
  4855. Bmp: TBitmap;
  4856. DrawToDC, BmpDC: HDC;
  4857. DrawCanvas: TCanvas;
  4858. R1, R2, R3: TRect;
  4859. IsOpen, IsSelected, IsPushed: Boolean;
  4860. ToolbarStyle: Boolean;
  4861. UseDisabledShadow: Boolean;
  4862. SaveIndex, SaveIndex2: Integer;
  4863. BkColor: TColor;
  4864. begin
  4865. ValidatePositions;
  4866. if tbisInvalidated in Viewer.State then begin
  4867. Offscreen := True;
  4868. Exclude(Viewer.State, tbisInvalidated);
  4869. end;
  4870. R1 := Viewer.BoundsRect;
  4871. if not Viewer.Show or IsRectEmpty(R1) or (Viewer.Item is TTBControlItem) then
  4872. Exit;
  4873. R2 := R1;
  4874. OffsetRect(R2, -R2.Left, -R2.Top);
  4875. IsOpen := FOpenViewer = Viewer;
  4876. IsSelected := (FSelected = Viewer);
  4877. IsPushed := IsSelected and (IsOpen or (FMouseOverSelected and FCapture));
  4878. ToolbarStyle := Viewer.IsToolbarStyle;
  4879. DrawToDC := DrawTo.Handle;
  4880. Bmp := nil;
  4881. { Must deselect any currently selected handles before calling SaveDC, because
  4882. if they are left selected and DeleteObject gets called on them after the
  4883. SaveDC call, it will fail on Win9x/Me, and thus leak GDI resources. }
  4884. DrawTo.Refresh;
  4885. SaveIndex := SaveDC(DrawToDC);
  4886. try
  4887. IntersectClipRect(DrawToDC, R1.Left, R1.Top, R1.Right, R1.Bottom);
  4888. GetClipBox(DrawToDC, R3);
  4889. if IsRectEmpty(R3) then
  4890. Exit;
  4891. if not Offscreen then begin
  4892. MoveWindowOrg(DrawToDC, R1.Left, R1.Top);
  4893. { Tweak the brush origin so that the checked background drawn behind
  4894. checked items always looks the same regardless of whether the item
  4895. is positioned on an even or odd Left or Top coordinate. }
  4896. SetBrushOrgEx(DrawToDC, R1.Left and 1, R1.Top and 1, nil);
  4897. DrawCanvas := DrawTo;
  4898. end
  4899. else begin
  4900. Bmp := TBitmap.Create;
  4901. Bmp.Width := R2.Right;
  4902. Bmp.Height := R2.Bottom;
  4903. DrawCanvas := Bmp.Canvas;
  4904. BmpDC := DrawCanvas.Handle;
  4905. SaveIndex2 := SaveDC(BmpDC);
  4906. SetWindowOrgEx(BmpDC, R1.Left, R1.Top, nil);
  4907. FWindow.Perform(WM_ERASEBKGND, WPARAM(BmpDC), 0);
  4908. RestoreDC(BmpDC, SaveIndex2);
  4909. end;
  4910. { Initialize brush }
  4911. if not ToolbarStyle and IsSelected then begin
  4912. {$IFNDEF TB2K_USE_STRICT_O2K_MENU_STYLE}
  4913. if AreFlatMenusEnabled then
  4914. { Windows XP uses a different fill color for selected menu items when
  4915. flat menus are enabled }
  4916. DrawCanvas.Brush.Color := clMenuHighlight
  4917. else
  4918. {$ENDIF}
  4919. DrawCanvas.Brush.Color := clHighlight;
  4920. end
  4921. else
  4922. DrawCanvas.Brush.Style := bsClear;
  4923. { Initialize font }
  4924. DrawCanvas.Font.Assign(GetFont);
  4925. if Viewer.Item.Enabled then begin
  4926. if not ToolbarStyle and IsSelected then
  4927. DrawCanvas.Font.Color := clHighlightText
  4928. else begin
  4929. if ToolbarStyle then
  4930. DrawCanvas.Font.Color := clBtnText
  4931. else
  4932. DrawCanvas.Font.Color := tbMenuTextColor;
  4933. end;
  4934. UseDisabledShadow := False;
  4935. end
  4936. else begin
  4937. DrawCanvas.Font.Color := clGrayText;
  4938. { Use the disabled shadow if either:
  4939. 1. The item is a toolbar-style item.
  4940. 2. The item is not selected, and the background color equals the
  4941. button-face color.
  4942. 3. The gray-text color is the same as the background color.
  4943. Note: Windows actually uses dithered text in this case. }
  4944. BkColor := ColorToRGB(TControlAccess(FWindow).Color);
  4945. UseDisabledShadow := ToolbarStyle or
  4946. (not IsSelected and (BkColor = ColorToRGB(clBtnFace))) or
  4947. (ColorToRGB(clGrayText) = BkColor);
  4948. end;
  4949. Viewer.Paint(DrawCanvas, R2, IsSelected, IsPushed, UseDisabledShadow);
  4950. if Offscreen then
  4951. BitBlt(DrawToDC, R1.Left, R1.Top, Bmp.Width, Bmp.Height, DrawCanvas.Handle,
  4952. 0, 0, SRCCOPY);
  4953. finally
  4954. DrawTo.Refresh; { must do this before a RestoreDC }
  4955. RestoreDC(DrawToDC, SaveIndex);
  4956. Bmp.Free;
  4957. end;
  4958. end;
  4959. procedure TTBView.DrawSubitems(ACanvas: TCanvas);
  4960. var
  4961. I: Integer;
  4962. begin
  4963. for I := 0 to FViewerCount-1 do begin
  4964. if (vsDrawInOrder in FState) or (FViewers[I] <> FSelected) then
  4965. DrawItem(FViewers[I], ACanvas, False);
  4966. end;
  4967. if not(vsDrawInOrder in FState) and Assigned(FSelected) then
  4968. DrawItem(FSelected, ACanvas, False);
  4969. Exclude(FState, vsDrawInOrder);
  4970. end;
  4971. procedure TTBView.Invalidate(AViewer: TTBItemViewer);
  4972. begin
  4973. if not FValidated or not Assigned(FWindow) or not FWindow.HandleAllocated then
  4974. Exit;
  4975. if AViewer.Show and not IsRectEmpty(AViewer.BoundsRect) and
  4976. not(AViewer.Item is TTBControlItem) then begin
  4977. Include(AViewer.State, tbisInvalidated);
  4978. InvalidateRect(FWindow.Handle, @AViewer.BoundsRect, False);
  4979. end;
  4980. end;
  4981. procedure TTBView.SetAccelsVisibility(AShowAccels: Boolean);
  4982. var
  4983. I: Integer;
  4984. Viewer: TTBItemViewer;
  4985. begin
  4986. { Always show accels when keyboard cues are enabled }
  4987. AShowAccels := AShowAccels or not(vsUseHiddenAccels in FStyle) or
  4988. AreKeyboardCuesEnabled;
  4989. if AShowAccels <> (vsShowAccels in FState) then begin
  4990. if AShowAccels then
  4991. Include(FState, vsShowAccels)
  4992. else
  4993. Exclude(FState, vsShowAccels);
  4994. if Assigned(FWindow) and FWindow.HandleAllocated and
  4995. IsWindowVisible(FWindow.Handle) then
  4996. { ^ the visibility check is just an optimization }
  4997. for I := 0 to FViewerCount-1 do begin
  4998. Viewer := FViewers[I];
  4999. if Viewer.CaptionShown and
  5000. (FindAccelChar(Viewer.GetCaptionText) <> #0) then
  5001. Invalidate(Viewer);
  5002. end;
  5003. end;
  5004. end;
  5005. function TTBView.FirstSelectable: TTBItemViewer;
  5006. var
  5007. FirstViewer: TTBItemViewer;
  5008. begin
  5009. Result := NextSelectable(nil, True);
  5010. if Assigned(Result) then begin
  5011. FirstViewer := Result;
  5012. while tbisDontSelectFirst in Result.Item.ItemStyle do begin
  5013. Result := NextSelectable(Result, True);
  5014. if Result = FirstViewer then
  5015. { don't loop endlessly if all items have the tbisDontSelectFirst style }
  5016. Break;
  5017. end;
  5018. end;
  5019. end;
  5020. function TTBView.NextSelectable(CurViewer: TTBItemViewer;
  5021. GoForward: Boolean): TTBItemViewer;
  5022. var
  5023. I, J: Integer;
  5024. begin
  5025. ValidatePositions;
  5026. Result := nil;
  5027. if FViewerCount = 0 then Exit;
  5028. J := -1;
  5029. I := IndexOf(CurViewer);
  5030. while True do begin
  5031. if GoForward then begin
  5032. Inc(I);
  5033. if I >= FViewerCount then I := 0;
  5034. end
  5035. else begin
  5036. Dec(I);
  5037. if I < 0 then I := FViewerCount-1;
  5038. end;
  5039. if J = -1 then
  5040. J := I
  5041. else
  5042. if I = J then
  5043. Exit;
  5044. if (FViewers[I].Show or FViewers[I].Clipped) and FViewers[I].Item.Visible and
  5045. (tbisSelectable in FViewers[I].Item.ItemStyle) then
  5046. Break;
  5047. end;
  5048. Result := FViewers[I];
  5049. end;
  5050. function TTBView.NextSelectableWithAccel(CurViewer: TTBItemViewer;
  5051. Key: Char; RequirePrimaryAccel: Boolean; var IsOnlyItemWithAccel: Boolean): TTBItemViewer;
  5052. function IsAccelItem(const Index: Integer;
  5053. const Primary, EnabledItems: Boolean): Boolean;
  5054. var
  5055. S: String;
  5056. LastAccel: Char;
  5057. Viewer: TTBItemViewer;
  5058. Item: TTBCustomItem;
  5059. begin
  5060. Result := False;
  5061. Viewer := FViewers[Index];
  5062. Item := Viewer.Item;
  5063. if (Viewer.Show or Viewer.Clipped) and (tbisSelectable in Item.ItemStyle) and
  5064. (Item.Enabled = EnabledItems) and
  5065. Item.Visible and Viewer.CaptionShown then begin
  5066. S := Viewer.GetCaptionText;
  5067. if S <> '' then begin
  5068. LastAccel := FindAccelChar(S);
  5069. if Primary then begin
  5070. if LastAccel <> #0 then
  5071. Result := AnsiCompareText(LastAccel, Key) = 0;
  5072. end
  5073. else
  5074. if (LastAccel = #0) and (Key <> ' ') then
  5075. Result := AnsiCompareText(S[1], Key) = 0;
  5076. end;
  5077. end;
  5078. end;
  5079. function FindAccel(I: Integer;
  5080. const Primary, EnabledItems: Boolean): Integer;
  5081. var
  5082. J: Integer;
  5083. begin
  5084. Result := -1;
  5085. J := -1;
  5086. while True do begin
  5087. Inc(I);
  5088. if I >= FViewerCount then I := 0;
  5089. if J = -1 then
  5090. J := I
  5091. else
  5092. if I = J then
  5093. Break;
  5094. if IsAccelItem(I, Primary, EnabledItems) then begin
  5095. Result := I;
  5096. Break;
  5097. end;
  5098. end;
  5099. end;
  5100. var
  5101. Start, I: Integer;
  5102. Primary, EnabledItems: Boolean;
  5103. begin
  5104. ValidatePositions;
  5105. Result := nil;
  5106. IsOnlyItemWithAccel := False;
  5107. if FViewerCount = 0 then Exit;
  5108. Start := IndexOf(CurViewer);
  5109. for Primary := True downto False do
  5110. if not RequirePrimaryAccel or Primary then
  5111. for EnabledItems := True downto False do begin
  5112. I := FindAccel(Start, Primary, EnabledItems);
  5113. if I <> -1 then begin
  5114. Result := FViewers[I];
  5115. IsOnlyItemWithAccel := not EnabledItems or
  5116. (FindAccel(I, Primary, EnabledItems) = I);
  5117. Exit;
  5118. end;
  5119. end;
  5120. end;
  5121. procedure TTBView.EnterToolbarLoop(Options: TTBEnterToolbarLoopOptions);
  5122. var
  5123. ModalHandler: TTBModalHandler;
  5124. P: TPoint;
  5125. begin
  5126. if vsModal in FState then Exit;
  5127. ModalHandler := TTBModalHandler.Create(FWindow.Handle);
  5128. try
  5129. { remove all states except... }
  5130. FState := FState * [vsShowAccels];
  5131. try
  5132. Include(FState, vsModal);
  5133. { Must ensure that DoneAction is reset to tbdaNone *before* calling
  5134. NotifyFocusEvent so that the IsModalEnding call it makes won't return
  5135. True }
  5136. FDoneActionData.DoneAction := tbdaNone;
  5137. { Now that the vsModal state has been added, send an MSAA focus event }
  5138. if Assigned(Selected) then
  5139. NotifyFocusEvent;
  5140. ModalHandler.Loop(Self, tbetMouseDown in Options,
  5141. tbetExecuteSelected in Options, tbetFromMSAA in Options, False);
  5142. finally
  5143. { Remove vsModal state from the root view before any TTBView.Destroy
  5144. methods get called (as a result of the CloseChildPopups call below),
  5145. so that NotifyFocusEvent becomes a no-op }
  5146. Exclude(FState, vsModal);
  5147. StopAllTimers;
  5148. CloseChildPopups;
  5149. GetCursorPos(P);
  5150. UpdateSelection(@P, True);
  5151. end;
  5152. finally
  5153. ModalHandler.Free;
  5154. end;
  5155. SetAccelsVisibility(False);
  5156. Selected := nil;
  5157. // caused flicker: FWindow.Update;
  5158. ProcessDoneAction(FDoneActionData, False);
  5159. end;
  5160. procedure TTBView.SetCustomizing(Value: Boolean);
  5161. begin
  5162. if FCustomizing <> Value then begin
  5163. FCustomizing := Value;
  5164. RecreateAllViewers;
  5165. end;
  5166. end;
  5167. procedure TTBView.BeginUpdate;
  5168. begin
  5169. Inc(FUpdating);
  5170. end;
  5171. procedure TTBView.EndUpdate;
  5172. begin
  5173. Dec(FUpdating);
  5174. if FUpdating = 0 then
  5175. TryValidatePositions;
  5176. end;
  5177. procedure TTBView.GetOffEdgeControlList(const List: TList);
  5178. var
  5179. I: Integer;
  5180. Item: TTBCustomItem;
  5181. begin
  5182. for I := 0 to FViewerCount-1 do begin
  5183. Item := FViewers[I].Item;
  5184. if (Item is TTBControlItem) and FViewers[I].OffEdge and
  5185. (TTBControlItem(Item).FControl is TWinControl) then
  5186. List.Add(TTBControlItem(Item).FControl);
  5187. end;
  5188. end;
  5189. procedure TTBView.SetCapture;
  5190. begin
  5191. FCapture := True;
  5192. end;
  5193. procedure TTBView.CancelCapture;
  5194. begin
  5195. if FCapture then begin
  5196. FCapture := False;
  5197. LastPos.X := Low(LastPos.X);
  5198. if Assigned(FSelected) then
  5199. FSelected.LosingCapture;
  5200. end;
  5201. end;
  5202. procedure TTBView.KeyDown(var Key: Word; Shift: TShiftState);
  5203. procedure SelNextItem(const ParentView: TTBView; const GoForward: Boolean);
  5204. begin
  5205. ParentView.Selected := ParentView.NextSelectable(ParentView.FSelected,
  5206. GoForward);
  5207. ParentView.ScrollSelectedIntoView;
  5208. end;
  5209. procedure HelpKey;
  5210. var
  5211. V: TTBView;
  5212. ContextID: Integer;
  5213. { MP }
  5214. HelpKeyword: string;
  5215. begin
  5216. ContextID := 0;
  5217. V := Self;
  5218. while Assigned(V) do begin
  5219. if Assigned(V.FSelected) then begin
  5220. ContextID := V.FSelected.Item.HelpContext;
  5221. if ContextID <> 0 then Break;
  5222. end;
  5223. V := V.FParentView;
  5224. end;
  5225. { MP }
  5226. if ContextID <> 0 then
  5227. begin
  5228. EndModalWithHelp(ContextID);
  5229. Exit;
  5230. end;
  5231. HelpKeyword := '';
  5232. V := Self;
  5233. while Assigned(V) do begin
  5234. if Assigned(V.FSelected) then begin
  5235. HelpKeyword := V.FSelected.Item.HelpKeyword;
  5236. if HelpKeyword <> '' then Break;
  5237. end;
  5238. V := V.FParentView;
  5239. end;
  5240. if HelpKeyword <> '' then
  5241. EndModalWithHelp(HelpKeyword);
  5242. { /MP }
  5243. end;
  5244. var
  5245. ParentTBView: TTBView;
  5246. begin
  5247. ParentTBView := GetParentToolbarView;
  5248. case Key of
  5249. VK_TAB: begin
  5250. SelNextItem(Self, GetKeyState(VK_SHIFT) >= 0);
  5251. end;
  5252. VK_RETURN: begin
  5253. ExecuteSelected(True);
  5254. end;
  5255. VK_MENU, VK_F10: begin
  5256. EndModal;
  5257. end;
  5258. VK_ESCAPE: begin
  5259. Key := 0;
  5260. if FParentView = nil then
  5261. EndModal
  5262. else
  5263. FParentView.CancelChildPopups;
  5264. end;
  5265. VK_LEFT, VK_RIGHT: begin
  5266. if (Self = ParentTBView) and (Orientation = tbvoVertical) then
  5267. OpenChildPopup(True)
  5268. else if Key = VK_LEFT then begin
  5269. if Assigned(ParentTBView) and (ParentTBView.Orientation <> tbvoVertical) then begin
  5270. if (Self = ParentTBView) or
  5271. (FParentView = ParentTBView) then
  5272. SelNextItem(ParentTBView, False)
  5273. else
  5274. FParentView.CloseChildPopups;
  5275. end
  5276. else begin
  5277. if Assigned(FParentView) then
  5278. FParentView.CancelChildPopups;
  5279. end;
  5280. end
  5281. else begin
  5282. if ((Self = ParentTBView) or not OpenChildPopup(True)) and
  5283. (Assigned(ParentTBView) and (ParentTBView.Orientation <> tbvoVertical)) then begin
  5284. { If we're on ParentTBView, or if the selected item can't display
  5285. a submenu, proceed to next item on ParentTBView }
  5286. SelNextItem(ParentTBView, True);
  5287. end;
  5288. end;
  5289. end;
  5290. VK_UP, VK_DOWN: begin
  5291. if (Self = ParentTBView) and (Orientation <> tbvoVertical) then
  5292. OpenChildPopup(True)
  5293. else
  5294. SelNextItem(Self, Key = VK_DOWN);
  5295. end;
  5296. VK_HOME, VK_END: begin
  5297. Selected := NextSelectable(nil, Key = VK_HOME);
  5298. ScrollSelectedIntoView;
  5299. end;
  5300. VK_F1: HelpKey;
  5301. else
  5302. Exit; { don't set Key to 0 for unprocessed keys }
  5303. end;
  5304. Key := 0;
  5305. end;
  5306. function TTBView.IsModalEnding: Boolean;
  5307. begin
  5308. Result := (GetRootView.FDoneActionData.DoneAction <> tbdaNone);
  5309. end;
  5310. procedure TTBView.EndModal;
  5311. var
  5312. RootView: TTBView;
  5313. begin
  5314. RootView := GetRootView;
  5315. RootView.FDoneActionData.DoneAction := tbdaCancel;
  5316. end;
  5317. procedure TTBView.EndModalWithClick(AViewer: TTBItemViewer);
  5318. var
  5319. RootView: TTBView;
  5320. begin
  5321. RootView := GetRootView;
  5322. RootView.FDoneActionData.ClickItem := AViewer.Item;
  5323. RootView.FDoneActionData.Sound := AViewer.FView.FIsPopup;
  5324. RootView.FDoneActionData.DoneAction := tbdaClickItem;
  5325. end;
  5326. procedure TTBView.EndModalWithHelp(AContextID: Integer);
  5327. var
  5328. RootView: TTBView;
  5329. begin
  5330. RootView := GetRootView;
  5331. RootView.FDoneActionData.ContextID := AContextID;
  5332. RootView.FDoneActionData.DoneAction := tbdaHelpContext;
  5333. end;
  5334. { MP }
  5335. procedure TTBView.EndModalWithHelp(HelpKeyword: string);
  5336. var
  5337. RootView: TTBView;
  5338. begin
  5339. RootView := GetRootView;
  5340. RootView.FDoneActionData.HelpKeyword := ShortString(HelpKeyword);
  5341. RootView.FDoneActionData.DoneAction := tbdaHelpKeyword;
  5342. end;
  5343. { /MP }
  5344. procedure TTBView.EndModalWithSystemMenu(AWnd: HWND; AKey: Cardinal);
  5345. var
  5346. RootView: TTBView;
  5347. begin
  5348. RootView := GetRootView;
  5349. RootView.FDoneActionData.Wnd := AWnd;
  5350. RootView.FDoneActionData.Key := AKey;
  5351. RootView.FDoneActionData.DoneAction := tbdaOpenSystemMenu;
  5352. end;
  5353. procedure TTBView.ExecuteSelected(AGivePriority: Boolean);
  5354. { Normally called after an Enter or accelerator key press on the view, this
  5355. method 'executes' or opens the selected item. It ends the modal loop, except
  5356. when a submenu is opened. }
  5357. var
  5358. Item: TTBCustomItem;
  5359. begin
  5360. if Assigned(FSelected) and FSelected.Item.Enabled then begin
  5361. Item := FSelected.Item;
  5362. if (tbisCombo in Item.ItemStyle) or not OpenChildPopup(True) then begin
  5363. if tbisSelectable in Item.ItemStyle then
  5364. FSelected.Execute(AGivePriority)
  5365. else
  5366. EndModal;
  5367. end
  5368. end
  5369. else
  5370. EndModal;
  5371. Exit; asm db 0,'Toolbar2000 (C) 1998-2005 Jordan Russell',0 end;
  5372. end;
  5373. procedure TTBView.Scroll(ADown: Boolean);
  5374. var
  5375. CurPos, NewPos, I: Integer;
  5376. begin
  5377. ValidatePositions;
  5378. if ADown then begin
  5379. NewPos := High(NewPos);
  5380. CurPos := FMaxHeight - tbMenuScrollArrowHeight;
  5381. for I := 0 to FViewerCount-1 do begin
  5382. with FViewers[I] do
  5383. if Clipped and not(tbisSeparator in Item.ItemStyle) and
  5384. (BoundsRect.Bottom < NewPos) and (BoundsRect.Bottom > CurPos) then
  5385. NewPos := BoundsRect.Bottom;
  5386. end;
  5387. if NewPos = High(NewPos) then
  5388. Exit;
  5389. Dec(NewPos, FMaxHeight - tbMenuScrollArrowHeight);
  5390. end
  5391. else begin
  5392. NewPos := Low(NewPos);
  5393. CurPos := tbMenuScrollArrowHeight;
  5394. for I := 0 to FViewerCount-1 do begin
  5395. with FViewers[I] do
  5396. if Clipped and not(tbisSeparator in Item.ItemStyle) and
  5397. (BoundsRect.Top > NewPos) and (BoundsRect.Top < CurPos) then
  5398. NewPos := BoundsRect.Top;
  5399. end;
  5400. if NewPos = Low(NewPos) then
  5401. Exit;
  5402. Dec(NewPos, tbMenuScrollArrowHeight);
  5403. end;
  5404. Inc(FScrollOffset, NewPos);
  5405. UpdatePositions;
  5406. end;
  5407. procedure TTBView.ScrollSelectedIntoView;
  5408. begin
  5409. ValidatePositions;
  5410. if (FSelected = nil) or not FSelected.Clipped then
  5411. Exit;
  5412. if FSelected.BoundsRect.Top < tbMenuScrollArrowHeight then begin
  5413. Dec(FScrollOffset, tbMenuScrollArrowHeight - FSelected.BoundsRect.Top);
  5414. UpdatePositions;
  5415. end
  5416. else if FSelected.BoundsRect.Bottom > FMaxHeight - tbMenuScrollArrowHeight then begin
  5417. Dec(FScrollOffset, (FMaxHeight - tbMenuScrollArrowHeight) -
  5418. FSelected.BoundsRect.Bottom);
  5419. UpdatePositions;
  5420. end;
  5421. end;
  5422. procedure TTBView.SetUsePriorityList(Value: Boolean);
  5423. begin
  5424. if FUsePriorityList <> Value then begin
  5425. FUsePriorityList := Value;
  5426. RecreateAllViewers;
  5427. end;
  5428. end;
  5429. function TTBView.GetCaptureWnd: HWND;
  5430. begin
  5431. Result := GetRootView.FCaptureWnd;
  5432. end;
  5433. procedure TTBView.CancelMode;
  5434. var
  5435. View: TTBView;
  5436. begin
  5437. EndModal;
  5438. { Hide all parent/child popup windows. Can't actually destroy them using
  5439. CloseChildPopups because this method may be called while inside
  5440. TTBEditItemViewer's message loop, and it could result in the active
  5441. TTBEditItemViewer instance being destroyed (leading to an AV). }
  5442. View := Self;
  5443. while Assigned(View.FOpenViewerView) do
  5444. View := View.FOpenViewerView;
  5445. repeat
  5446. View.StopAllTimers;
  5447. if View.FWindow is TTBPopupWindow then
  5448. View.FWindow.Visible := False;
  5449. View := View.FParentView;
  5450. until View = nil;
  5451. { Note: This doesn't remove the selection from a top-level toolbar item.
  5452. Unfortunately, we can't do 'Selected := nil' because it would destroy
  5453. child popups and that must'nt happen for the reason stated above. }
  5454. end;
  5455. procedure TTBView.SetState(AState: TTBViewState);
  5456. begin
  5457. FState := AState;
  5458. end;
  5459. function TTBView.GetMonitor: TMonitor;
  5460. begin
  5461. if ParentView <> nil then
  5462. begin
  5463. Result := ParentView.GetMonitor;
  5464. end
  5465. else
  5466. if not IsRectEmpty(FMonitorRect) then
  5467. begin
  5468. Result := Screen.MonitorFromRect(FMonitorRect);
  5469. end
  5470. else
  5471. begin
  5472. Result := GetMonitorFromControl(Window);
  5473. end;
  5474. end;
  5475. { TTBModalHandler }
  5476. const
  5477. LSFW_LOCK = 1;
  5478. LSFW_UNLOCK = 2;
  5479. var
  5480. LockSetForegroundWindowInited: BOOL;
  5481. LockSetForegroundWindow: function(uLockCode: UINT): BOOL; stdcall;
  5482. constructor TTBModalHandler.Create(AExistingWnd: HWND);
  5483. begin
  5484. inherited Create;
  5485. LastPos := SmallPointToPoint(TSmallPoint(GetMessagePos()));
  5486. if AExistingWnd <> 0 then
  5487. FWnd := AExistingWnd
  5488. else begin
  5489. FWnd := Classes.AllocateHWnd(WndProc);
  5490. FCreatedWnd := True;
  5491. end;
  5492. { Like standard menus, don't allow other apps to steal the focus during
  5493. our modal loop. This also prevents us from losing activation when
  5494. "active window tracking" is enabled and the user moves the mouse over
  5495. another application's window. }
  5496. LockForegroundWindow;
  5497. SetCapture(FWnd);
  5498. SetCursor(LoadCursor(0, IDC_ARROW));
  5499. NotifyWinEvent(EVENT_SYSTEM_MENUSTART, FWnd, OBJID_CLIENT, CHILDID_SELF);
  5500. FInited := True;
  5501. end;
  5502. class procedure TTBModalHandler.DoLockForegroundWindow(LockCode: Cardinal);
  5503. begin
  5504. if not LockSetForegroundWindowInited then begin
  5505. LockSetForegroundWindow := GetProcAddress(GetModuleHandle(user32),
  5506. 'LockSetForegroundWindow');
  5507. InterlockedExchange(Integer(LockSetForegroundWindowInited), Ord(True));
  5508. end;
  5509. { Should always, as supported since Windows 2000 }
  5510. if Assigned(LockSetForegroundWindow) then
  5511. LockSetForegroundWindow(LockCode);
  5512. end;
  5513. class procedure TTBModalHandler.LockForegroundWindow;
  5514. begin
  5515. DoLockForegroundWindow(LSFW_LOCK);
  5516. end;
  5517. class procedure TTBModalHandler.UnlockForegroundWindow;
  5518. begin
  5519. DoLockForegroundWindow(LSFW_UNLOCK);
  5520. end;
  5521. destructor TTBModalHandler.Destroy;
  5522. begin
  5523. UnlockForegroundWindow;
  5524. if FWnd <> 0 then begin
  5525. if GetCapture = FWnd then
  5526. ReleaseCapture;
  5527. if FInited then
  5528. NotifyWinEvent(EVENT_SYSTEM_MENUEND, FWnd, OBJID_CLIENT, CHILDID_SELF);
  5529. if FCreatedWnd then
  5530. Classes.DeallocateHWnd(FWnd);
  5531. end;
  5532. inherited;
  5533. end;
  5534. procedure TTBModalHandler.WndProc(var Msg: TMessage);
  5535. begin
  5536. Msg.Result := DefWindowProc(FWnd, Msg.Msg, Msg.WParam, Msg.LParam);
  5537. if (Msg.Msg = WM_CANCELMODE) and Assigned(FRootPopup) then begin
  5538. try
  5539. { We can receive a WM_CANCELMODE message during a modal loop if a dialog
  5540. pops up. Respond by hiding menus to make it look like the modal loop
  5541. has returned, even though it really hasn't yet.
  5542. Note: Similar code in TTBCustomToolbar.WMCancelMode. }
  5543. FRootPopup.View.CancelMode;
  5544. except
  5545. Application.HandleException(Self);
  5546. end;
  5547. end;
  5548. end;
  5549. procedure TTBModalHandler.Loop(const RootView: TTBView;
  5550. const AMouseDown, AExecuteSelected, AFromMSAA, TrackRightButton: Boolean);
  5551. var
  5552. OriginalActiveWindow: HWND;
  5553. function GetActiveView: TTBView;
  5554. begin
  5555. Result := RootView;
  5556. while Assigned(Result.FOpenViewerView) do
  5557. Result := Result.FOpenViewerView;
  5558. end;
  5559. procedure UpdateAllSelections(const P: TPoint; const AllowNewSelection: Boolean);
  5560. var
  5561. View, CapView: TTBView;
  5562. begin
  5563. View := GetActiveView;
  5564. CapView := View;
  5565. while Assigned(CapView) and not CapView.FCapture do
  5566. CapView := CapView.FParentView;
  5567. while Assigned(View) do begin
  5568. if (CapView = nil) or (View = CapView) then
  5569. View.UpdateSelection(@P, AllowNewSelection);
  5570. View := View.FParentView;
  5571. end;
  5572. end;
  5573. function GetSelectedViewer(var AView: TTBView; var AViewer: TTBItemViewer): Boolean;
  5574. { Returns True if AViewer <> nil. }
  5575. var
  5576. View: TTBView;
  5577. begin
  5578. AView := nil;
  5579. AViewer := nil;
  5580. { Look for a capture item first }
  5581. View := RootView;
  5582. repeat
  5583. if View.FCapture then begin
  5584. AView := View;
  5585. AViewer := View.FSelected;
  5586. Break;
  5587. end;
  5588. View := View.FOpenViewerView;
  5589. until View = nil;
  5590. if View = nil then begin
  5591. View := RootView;
  5592. repeat
  5593. if Assigned(View.FSelected) and View.FMouseOverSelected then begin
  5594. AView := View;
  5595. AViewer := View.FSelected;
  5596. Break;
  5597. end;
  5598. if vsMouseInWindow in View.FState then begin
  5599. { ...there is no current selection, but the mouse is still in the
  5600. window. This can happen if the mouse is over the non-client area
  5601. of the toolbar or popup window, or in an area not containing an
  5602. item. }
  5603. AView := View;
  5604. Break;
  5605. end;
  5606. View := View.FOpenViewerView;
  5607. until View = nil;
  5608. end;
  5609. Result := Assigned(AViewer);
  5610. end;
  5611. function ContinueLoop: Boolean;
  5612. begin
  5613. { Don't continue if the mouse capture is lost, if a (modeless) top-level
  5614. window is shown causing the active window to change, or if EndModal* was
  5615. called. }
  5616. Result := (GetCapture = FWnd) and (GetActiveWindow = OriginalActiveWindow)
  5617. and not RootView.IsModalEnding;
  5618. end;
  5619. function SendKeyEvent(const View: TTBView; var Key: Word;
  5620. const Shift: TShiftState): Boolean;
  5621. begin
  5622. Result := True;
  5623. if Assigned(View.FSelected) then begin
  5624. View.FSelected.KeyDown(Key, Shift);
  5625. if RootView.IsModalEnding then
  5626. Exit;
  5627. end;
  5628. if Key <> 0 then begin
  5629. View.KeyDown(Key, Shift);
  5630. if RootView.IsModalEnding then
  5631. Exit;
  5632. end;
  5633. Result := False;
  5634. end;
  5635. procedure DoHintMouseMessage(const Ctl: TControl; const P: TPoint);
  5636. var
  5637. M: TWMMouseMove;
  5638. begin
  5639. M.Msg := WM_MOUSEMOVE;
  5640. M.Keys := 0;
  5641. M.Pos := PointToSmallPoint(P);
  5642. Application.HintMouseMessage(Ctl, TMessage(M));
  5643. end;
  5644. procedure MouseMoved;
  5645. var
  5646. View: TTBView;
  5647. Cursor: HCURSOR;
  5648. Item: TTBCustomItem;
  5649. P: TPoint;
  5650. R: TRect;
  5651. begin
  5652. UpdateAllSelections(LastPos, True);
  5653. View := GetActiveView;
  5654. Cursor := 0;
  5655. if Assigned(View.FSelected) and Assigned(View.FWindow) then begin
  5656. Item := View.FSelected.Item;
  5657. P := View.FWindow.ScreenToClient(LastPos);
  5658. if ((vsAlwaysShowHints in View.FStyle) or
  5659. (tboShowHint in Item.FEffectiveOptions)) and not View.FCapture then begin
  5660. { Display popup hint for the item. Update is called
  5661. first to minimize flicker caused by the hiding &
  5662. showing of the hint window. }
  5663. View.FWindow.Update;
  5664. DoHintMouseMessage(View.FWindow, P);
  5665. end
  5666. else
  5667. Application.CancelHint;
  5668. R := View.FSelected.BoundsRect;
  5669. Dec(P.X, R.Left);
  5670. Dec(P.Y, R.Top);
  5671. View.FSelected.GetCursor(P, Cursor);
  5672. end
  5673. else
  5674. Application.CancelHint;
  5675. if Cursor = 0 then
  5676. Cursor := LoadCursor(0, IDC_ARROW);
  5677. SetCursor(Cursor);
  5678. end;
  5679. procedure UpdateAppHint;
  5680. var
  5681. View: TTBView;
  5682. begin
  5683. View := RootView;
  5684. while Assigned(View.FOpenViewerView) and Assigned(View.FOpenViewerView.FSelected) do
  5685. View := View.FOpenViewerView;
  5686. if Assigned(View.FSelected) then
  5687. Application.Hint := GetLongHint(View.FSelected.Item.Hint)
  5688. else
  5689. Application.Hint := '';
  5690. end;
  5691. procedure HandleTimer(const View: TTBView; const ID: TTBViewTimerID);
  5692. begin
  5693. case ID of
  5694. tiOpen: begin
  5695. { Similar to standard menus, always close child popups, even if
  5696. Selected = OpenViewer.
  5697. Note: CloseChildPopups and OpenChildPopup will stop the tiClose
  5698. and tiOpen timers respectively. }
  5699. View.CloseChildPopups;
  5700. View.OpenChildPopup(False);
  5701. end;
  5702. tiClose: begin
  5703. { Note: CloseChildPopups stops the tiClose timer. }
  5704. View.CloseChildPopups;
  5705. end;
  5706. tiScrollUp: begin
  5707. if View.FShowUpArrow then
  5708. View.Scroll(False)
  5709. else
  5710. View.StopTimer(tiScrollUp);
  5711. end;
  5712. tiScrollDown: begin
  5713. if View.FShowDownArrow then
  5714. View.Scroll(True)
  5715. else
  5716. View.StopTimer(tiScrollDown);
  5717. end;
  5718. end;
  5719. end;
  5720. var
  5721. MouseDownOnMenu: Boolean;
  5722. Msg: TMsg;
  5723. P: TPoint;
  5724. Ctl: TControl;
  5725. View: TTBView;
  5726. IsOnlyItemWithAccel: Boolean;
  5727. MouseIsDown: Boolean;
  5728. Key: Word;
  5729. Shift: TShiftState;
  5730. Viewer: TTBItemViewer;
  5731. begin
  5732. FillChar(RootView.FDoneActionData, SizeOf(RootView.FDoneActionData), 0);
  5733. RootView.ValidatePositions;
  5734. try
  5735. try
  5736. RootView.FCaptureWnd := FWnd;
  5737. MouseDownOnMenu := False;
  5738. if AMouseDown then begin
  5739. P := RootView.FSelected.ScreenToClient(SmallPointToPoint(TSmallPoint(GetMessagePos())));
  5740. RootView.FSelected.MouseDown([], P.X, P.Y, MouseDownOnMenu);
  5741. if RootView.IsModalEnding then
  5742. Exit;
  5743. MouseDownOnMenu := False; { never set MouseDownOnMenu to True on first click }
  5744. end
  5745. else if AExecuteSelected then begin
  5746. RootView.ExecuteSelected(not AFromMSAA);
  5747. if RootView.IsModalEnding then
  5748. Exit;
  5749. end;
  5750. OriginalActiveWindow := GetActiveWindow;
  5751. while ContinueLoop do begin
  5752. { Examine the next message before popping it out of the queue }
  5753. if not PeekMessage(Msg, 0, 0, 0, PM_NOREMOVE) then begin
  5754. WaitMessage;
  5755. Continue;
  5756. end;
  5757. case Msg.message of
  5758. WM_LBUTTONDOWN, WM_RBUTTONDOWN: begin
  5759. P := SmallPointToPoint(TSmallPoint(Msg.lParam));
  5760. Windows.ClientToScreen(Msg.hwnd, P);
  5761. Ctl := FindDragTarget(P, True);
  5762. { Was the mouse not clicked on a popup, or was it clicked on a
  5763. popup that is not a child of RootView?
  5764. (The latter can happen when in customization mode, for example,
  5765. if the user right-clicks a popup menu being customized and
  5766. the context menu is displayed.) }
  5767. if not(Ctl is TTBPopupWindow) or
  5768. not RootView.ContainsView(TTBPopupWindow(Ctl).View) then begin
  5769. { If the root view is a popup, or if the root view is a toolbar
  5770. and the user clicked outside the toolbar or in its non-client
  5771. area (e.g. on its drag handle), exit }
  5772. if RootView.FIsPopup or (Ctl <> RootView.FWindow) or
  5773. not PtInRect(RootView.FWindow.ClientRect, RootView.FWindow.ScreenToClient(P)) then
  5774. Exit
  5775. else
  5776. if Msg.message = WM_LBUTTONDOWN then begin
  5777. { If the user clicked inside a toolbar on anything but an
  5778. item, exit }
  5779. UpdateAllSelections(P, True);
  5780. if (RootView.FSelected = nil) or not RootView.FMouseOverSelected or
  5781. (tbisClicksTransparent in RootView.FSelected.Item.ItemStyle) then
  5782. Exit;
  5783. end;
  5784. end;
  5785. end;
  5786. end;
  5787. { Now pop the message out of the queue }
  5788. if not PeekMessage(Msg, 0, Msg.message, Msg.message, PM_REMOVE or PM_NOYIELD) then
  5789. Continue;
  5790. case Msg.message of
  5791. $4D:
  5792. { This undocumented message is sent to the focused window when
  5793. F1 is pressed. Windows handles it by sending a WM_HELP message
  5794. to the same window. We don't want this to happen while a menu
  5795. is up, so swallow the message. }
  5796. ;
  5797. WM_CONTEXTMENU:
  5798. { Windows still sends WM_CONTEXTMENU messages for "context menu"
  5799. keystrokes even if WM_KEYUP messages are never dispatched,
  5800. so it must specifically ignore this message }
  5801. ;
  5802. WM_KEYFIRST..WM_KEYLAST: begin
  5803. Application.CancelHint;
  5804. MouseIsDown := (GetKeyState(VK_LBUTTON) < 0) or
  5805. (TrackRightButton and (GetKeyState(VK_RBUTTON) < 0));
  5806. case Msg.message of
  5807. WM_KEYDOWN, WM_SYSKEYDOWN:
  5808. begin
  5809. if Msg.wParam = VK_PROCESSKEY then
  5810. { Don't let IME process the key }
  5811. Msg.wParam := ImmGetVirtualKey(Msg.hwnd);
  5812. if not MouseIsDown or (Msg.wParam = VK_F1) then begin
  5813. Key := Word(Msg.wParam);
  5814. if SendKeyEvent(GetActiveView, Key,
  5815. KeyDataToShiftState(Msg.lParam)) then
  5816. Exit;
  5817. { If it's not handled by a KeyDown method, translate
  5818. it into a WM_*CHAR message }
  5819. if Key <> 0 then
  5820. TranslateMessage(Msg);
  5821. end;
  5822. end;
  5823. WM_CHAR, WM_SYSCHAR:
  5824. if not MouseIsDown then begin
  5825. View := GetActiveView;
  5826. Viewer := View.NextSelectableWithAccel(View.FSelected,
  5827. Chr(Msg.WParam), False, IsOnlyItemWithAccel);
  5828. if Viewer = nil then begin
  5829. if (Msg.WParam in [VK_SPACE, Ord('-')]) and
  5830. not RootView.FIsPopup and (View = RootView) and
  5831. (GetActiveWindow <> 0) then begin
  5832. RootView.EndModalWithSystemMenu(GetActiveWindow,
  5833. Msg.WParam);
  5834. Exit;
  5835. end
  5836. else
  5837. MessageBeep(0);
  5838. end
  5839. else begin
  5840. View.Selected := Viewer;
  5841. View.ScrollSelectedIntoView;
  5842. if IsOnlyItemWithAccel then
  5843. View.ExecuteSelected(True);
  5844. end;
  5845. end;
  5846. end;
  5847. end;
  5848. WM_TIMER:
  5849. begin
  5850. Ctl := FindControl(Msg.hwnd);
  5851. if Assigned(Ctl) and (Ctl is TTBPopupWindow) and
  5852. (Msg.wParam >= ViewTimerBaseID + Ord(Low(TTBViewTimerID))) and
  5853. (Msg.wParam <= ViewTimerBaseID + Ord(High(TTBViewTimerID))) then begin
  5854. if Assigned(TTBPopupWindow(Ctl).FView) then
  5855. HandleTimer(TTBPopupWindow(Ctl).FView,
  5856. TTBViewTimerID(WPARAM(Msg.wParam - ViewTimerBaseID)));
  5857. end
  5858. else
  5859. DispatchMessage(Msg);
  5860. end;
  5861. $118: ;
  5862. { ^ Like standard menus, don't dispatch WM_SYSTIMER messages
  5863. (the internal Windows message used for things like caret
  5864. blink and list box scrolling). }
  5865. WM_MOUSEFIRST..WM_MOUSELAST:
  5866. case Msg.message of
  5867. WM_MOUSEMOVE: begin
  5868. if (Msg.pt.X <> LastPos.X) or (Msg.pt.Y <> LastPos.Y) then begin
  5869. LastPos := Msg.pt;
  5870. MouseMoved;
  5871. end;
  5872. if GetSelectedViewer(View, Viewer) then begin
  5873. P := Viewer.ScreenToClient(Msg.pt);
  5874. Viewer.MouseMove(P.X, P.Y);
  5875. end;
  5876. end;
  5877. WM_MOUSEWHEEL:
  5878. if GetSelectedViewer(View, Viewer) then begin
  5879. P := Viewer.ScreenToClient(Msg.pt);
  5880. Viewer.MouseWheel(Smallint(LongRec(Msg.wParam).Hi), P.X, P.Y);
  5881. end;
  5882. WM_LBUTTONDOWN, WM_LBUTTONDBLCLK, WM_RBUTTONDOWN:
  5883. if (Msg.message <> WM_RBUTTONDOWN) or TrackRightButton then begin
  5884. Application.CancelHint;
  5885. MouseDownOnMenu := False;
  5886. Exclude(RootView.FState, vsIgnoreFirstMouseUp);
  5887. UpdateAllSelections(Msg.pt, True);
  5888. if GetSelectedViewer(View, Viewer) then begin
  5889. if Msg.message <> WM_LBUTTONDBLCLK then
  5890. Shift := []
  5891. else
  5892. Shift := [ssDouble];
  5893. P := Viewer.ScreenToClient(Msg.pt);
  5894. Viewer.MouseDown(Shift, P.X, P.Y, MouseDownOnMenu);
  5895. LastPos := SmallPointToPoint(TSmallPoint(GetMessagePos()));
  5896. end;
  5897. end;
  5898. WM_LBUTTONUP, WM_RBUTTONUP:
  5899. if (Msg.message = WM_LBUTTONUP) or TrackRightButton then begin
  5900. UpdateAllSelections(Msg.pt, False);
  5901. { ^ False is used so that when a popup menu is
  5902. displayed with the cursor currently inside it, the item
  5903. under the cursor won't be accidentally selected when the
  5904. user releases the button. The user must move the mouse at
  5905. at least one pixel (generating a WM_MOUSEMOVE message),
  5906. and then release the button. }
  5907. if not GetSelectedViewer(View, Viewer) then begin
  5908. { Mouse was not released over any item. Cancel out of the
  5909. loop if it's outside all views, or is inside unused
  5910. space on a topmost toolbar }
  5911. if not Assigned(View) or
  5912. ((View = RootView) and RootView.FIsToolbar) then begin
  5913. if not(vsIgnoreFirstMouseUp in RootView.FState) then
  5914. Exit
  5915. else
  5916. Exclude(RootView.FState, vsIgnoreFirstMouseUp);
  5917. end;
  5918. end
  5919. else begin
  5920. P := Viewer.ScreenToClient(Msg.pt);
  5921. Viewer.MouseUp(P.X, P.Y, MouseDownOnMenu);
  5922. end;
  5923. end;
  5924. end;
  5925. else
  5926. DispatchMessage(Msg);
  5927. end;
  5928. if not ContinueLoop then
  5929. begin
  5930. Exit;
  5931. end;
  5932. if LastPos.X = Low(LastPos.X) then begin
  5933. LastPos := SmallPointToPoint(TSmallPoint(GetMessagePos()));
  5934. MouseMoved;
  5935. end;
  5936. UpdateAppHint;
  5937. end;
  5938. finally
  5939. RootView.CancelCapture;
  5940. end;
  5941. finally
  5942. RootView.FCaptureWnd := 0;
  5943. Application.Hint := '';
  5944. { Make sure there are no outstanding WM_*CHAR messages }
  5945. RemoveMessages(WM_CHAR, WM_DEADCHAR);
  5946. RemoveMessages(WM_SYSCHAR, WM_SYSDEADCHAR);
  5947. { Nor any outstanding 'send WM_HELP' messages caused by an earlier press
  5948. of the F1 key }
  5949. RemoveMessages($4D, $4D);
  5950. end;
  5951. end;
  5952. { TTBPopupView }
  5953. procedure TTBPopupView.AutoSize(AWidth, AHeight: Integer);
  5954. begin
  5955. with TTBPopupWindow(FWindow) do
  5956. with GetNCSize do
  5957. SetBounds(Left, Top, AWidth + (X * 2),
  5958. AHeight + (Y * 2));
  5959. end;
  5960. function TTBPopupView.GetMonitor: TMonitor;
  5961. begin
  5962. Result := Screen.MonitorFromRect(FWindow.BoundsRect);
  5963. end;
  5964. function TTBPopupView.GetFont: TFont;
  5965. begin
  5966. Result := (Owner as TTBPopupWindow).Font;
  5967. end;
  5968. { TTBPopupWindow }
  5969. constructor TTBPopupWindow.CreatePopupWindow(AOwner: TComponent;
  5970. const AParentView: TTBView; const AItem: TTBCustomItem;
  5971. const ACustomizing: Boolean; const PopupPoint: TPoint);
  5972. begin
  5973. inherited Create(AOwner);
  5974. Visible := False;
  5975. SetBounds(PopupPoint.X, PopupPoint.Y, 320, 240);
  5976. ControlStyle := ControlStyle - [csCaptureMouse];
  5977. ShowHint := True;
  5978. Color := tbMenuBkColor;
  5979. FView := GetViewClass.CreateView(Self, AParentView, AItem, Self, False,
  5980. ACustomizing, False);
  5981. Include(FView.FState, vsModal);
  5982. { Inherit the font from the parent view, or use the system menu font if
  5983. there is no parent view }
  5984. if Assigned(AParentView) then
  5985. Font.Assign(AParentView.GetFont)
  5986. else
  5987. Font.Assign(GetToolbarFont(GetMonitorPixelsPerInch(Screen.MonitorFromPoint(PopupPoint))));
  5988. { Inherit the accelerator visibility state from the parent view. If there
  5989. is no parent view (i.e. it's a standalone popup menu), then default to
  5990. hiding accelerator keys, but change this in CreateWnd if the last input
  5991. came from the keyboard. }
  5992. if Assigned(AParentView) then begin
  5993. if vsUseHiddenAccels in AParentView.FStyle then
  5994. Include(FView.FStyle, vsUseHiddenAccels);
  5995. if vsShowAccels in AParentView.FState then
  5996. Include(FView.FState, vsShowAccels);
  5997. end
  5998. else
  5999. Include(FView.FStyle, vsUseHiddenAccels);
  6000. if Application.Handle <> 0 then
  6001. { Use Application.Handle if possible so that the taskbar button for the app
  6002. doesn't pop up when a TTBEditItem on a popup menu is focused }
  6003. ParentWindow := Application.Handle
  6004. else
  6005. { When Application.Handle is zero, use GetDesktopWindow() as the parent
  6006. window, not zero, otherwise UpdateControlState won't show the window }
  6007. ParentWindow := GetDesktopWindow;
  6008. end;
  6009. destructor TTBPopupWindow.Destroy;
  6010. begin
  6011. Destroying;
  6012. { Ensure window handle is destroyed *before* FView is freed, since
  6013. DestroyWindowHandle calls NotifyWinEvent which may result in
  6014. FView.HandleWMObject being called }
  6015. if HandleAllocated then
  6016. DestroyWindowHandle;
  6017. FreeAndNil(FView);
  6018. inherited;
  6019. end;
  6020. {MP}
  6021. procedure TTBPopupWindow.Cancel;
  6022. begin
  6023. { noop }
  6024. end;
  6025. procedure TTBPopupWindow.BeforeDestruction;
  6026. begin
  6027. { The inherited BeforeDestruction method hides the form. We need to close
  6028. any child popups first, so that pixels behind the popups are properly
  6029. restored without generating a WM_PAINT message. }
  6030. if Assigned(FView) then
  6031. FView.CloseChildPopups;
  6032. inherited;
  6033. end;
  6034. function TTBPopupWindow.GetNCSize: TPoint;
  6035. begin
  6036. Result.X := PopupMenuWindowNCSize;
  6037. Result.Y := PopupMenuWindowNCSize;
  6038. end;
  6039. function TTBPopupWindow.GetViewClass: TTBViewClass;
  6040. begin
  6041. Result := TTBPopupView;
  6042. end;
  6043. procedure TTBPopupWindow.CreateParams(var Params: TCreateParams);
  6044. const
  6045. CS_DROPSHADOW = $00020000;
  6046. begin
  6047. inherited;
  6048. with Params do begin
  6049. Style := (Style and not (WS_CHILD or WS_GROUP or WS_TABSTOP)) or WS_POPUP;
  6050. ExStyle := ExStyle or WS_EX_TOPMOST or WS_EX_TOOLWINDOW;
  6051. WindowClass.Style := WindowClass.Style or CS_SAVEBITS;
  6052. WindowClass.Style := WindowClass.Style or CS_DROPSHADOW;
  6053. end;
  6054. end;
  6055. procedure TTBPopupWindow.CreateWnd;
  6056. const
  6057. WM_CHANGEUISTATE = $0127;
  6058. WM_QUERYUISTATE = $0129;
  6059. UIS_INITIALIZE = 3;
  6060. UISF_HIDEACCEL = $2;
  6061. var
  6062. B: Boolean;
  6063. begin
  6064. inherited;
  6065. { On a top-level popup window, send WM_CHANGEUISTATE & WM_QUERYUISTATE
  6066. messages to the window to see if the last input came from the keyboard
  6067. and if the accelerator keys should be shown }
  6068. if (FView.ParentView = nil) and not FAccelsVisibilitySet then begin
  6069. FAccelsVisibilitySet := True;
  6070. SendMessage(Handle, WM_CHANGEUISTATE, UIS_INITIALIZE, 0);
  6071. B := (SendMessage(Handle, WM_QUERYUISTATE, 0, 0) and UISF_HIDEACCEL = 0);
  6072. FView.SetAccelsVisibility(B);
  6073. end;
  6074. end;
  6075. procedure TTBPopupWindow.DestroyWindowHandle;
  6076. begin
  6077. { Before destroying the window handle, we must stop any animation, otherwise
  6078. the animation thread will use an invalid handle }
  6079. TBEndAnimation(WindowHandle);
  6080. { Cleanly destroy any timers before the window handle is destroyed }
  6081. if Assigned(FView) then
  6082. FView.StopAllTimers;
  6083. NotifyWinEvent(EVENT_SYSTEM_MENUPOPUPEND, WindowHandle, OBJID_CLIENT,
  6084. CHILDID_SELF);
  6085. inherited;
  6086. end;
  6087. procedure TTBPopupWindow.WMGetObject(var Message: TMessage);
  6088. begin
  6089. if not FView.HandleWMGetObject(Message) then
  6090. inherited;
  6091. end;
  6092. procedure TTBPopupWindow.CMShowingChanged(var Message: TMessage);
  6093. const
  6094. ShowFlags: array[Boolean] of UINT = (
  6095. SWP_NOSIZE or SWP_NOMOVE or SWP_NOZORDER or SWP_NOACTIVATE or SWP_HIDEWINDOW,
  6096. SWP_NOSIZE or SWP_NOMOVE or SWP_NOZORDER or SWP_NOACTIVATE or SWP_SHOWWINDOW);
  6097. SPI_GETMENUFADE = $1012;
  6098. var
  6099. Animate: BOOL;
  6100. Blend: Boolean;
  6101. begin
  6102. { Must override TCustomForm/TForm's CM_SHOWINGCHANGED handler so that the
  6103. form doesn't get activated when Visible is set to True. }
  6104. { Handle animation. NOTE: I do not recommend trying to enable animation on
  6105. Windows 95 and NT 4.0 because there's a difference in the way the
  6106. SetWindowPos works on those versions. See the comment in the
  6107. TBStartAnimation function of TB2Anim.pas. }
  6108. {$IFNDEF TB2K_NO_ANIMATION}
  6109. if ((FView.ParentView = nil) or not(vsNoAnimation in FView.FParentView.FState)) and
  6110. Showing and (FView.Selected = nil) and not IsWindowVisible(WindowHandle) and
  6111. SystemParametersInfo(SPI_GETMENUANIMATION, 0, @Animate, 0) and Animate then begin
  6112. Blend := SystemParametersInfo(SPI_GETMENUFADE, 0, @Animate, 0) and Animate;
  6113. if Blend or (FAnimationDirection <> []) then begin
  6114. if SendMessage(WindowHandle, WM_TB2K_POPUPSHOWING, TPS_ANIMSTART, 0) = 0 then
  6115. begin
  6116. { Start animation only if WM_TB2K_POPUPSHOWING returns zero (or not handled) }
  6117. TBStartAnimation(WindowHandle, Blend, FAnimationDirection);
  6118. Exit;
  6119. end;
  6120. end;
  6121. end;
  6122. {$ENDIF}
  6123. { No animation... }
  6124. if not Showing then begin
  6125. { Call TBEndAnimation to ensure WS_EX_LAYERED style is removed before
  6126. hiding, otherwise windows under the popup window aren't repainted
  6127. properly. }
  6128. TBEndAnimation(WindowHandle);
  6129. end;
  6130. SetWindowPos(WindowHandle, 0, 0, 0, 0, 0, ShowFlags[Showing]);
  6131. if Showing then SendNotifyMessage(WindowHandle, WM_TB2K_POPUPSHOWING, TPS_NOANIM, 0);
  6132. end;
  6133. procedure TTBPopupWindow.WMTB2kAnimationEnded(var Message: TMessage);
  6134. begin
  6135. SendNotifyMessage(WindowHandle, WM_TB2K_POPUPSHOWING, TPS_ANIMFINISHED, 0);
  6136. end;
  6137. procedure TTBPopupWindow.WMTB2kStepAnimation(var Message: TMessage);
  6138. begin
  6139. TBStepAnimation(Message);
  6140. end;
  6141. procedure TTBPopupWindow.WMEraseBkgnd(var Message: TWMEraseBkgnd);
  6142. begin
  6143. { May be necessary in some cases... }
  6144. TBEndAnimation(WindowHandle);
  6145. inherited;
  6146. end;
  6147. procedure TTBPopupWindow.WMPaint(var Message: TWMPaint);
  6148. begin
  6149. { Must abort animation when a WM_PAINT message is received }
  6150. TBEndAnimation(WindowHandle);
  6151. inherited;
  6152. end;
  6153. procedure TTBPopupWindow.Paint;
  6154. begin
  6155. FView.DrawSubitems(Canvas);
  6156. PaintScrollArrows;
  6157. end;
  6158. procedure TTBPopupWindow.PaintScrollArrows;
  6159. procedure DrawArrow(const R: TRect; ADown: Boolean);
  6160. var
  6161. X, Y: Integer;
  6162. P: array[0..2] of TPoint;
  6163. begin
  6164. X := (R.Left + R.Right) div 2;
  6165. Y := (R.Top + R.Bottom) div 2;
  6166. Dec(Y);
  6167. P[0].X := X-3;
  6168. P[0].Y := Y;
  6169. P[1].X := X+3;
  6170. P[1].Y := Y;
  6171. P[2].X := X;
  6172. P[2].Y := Y;
  6173. if ADown then
  6174. Inc(P[2].Y, 3)
  6175. else begin
  6176. Inc(P[0].Y, 3);
  6177. Inc(P[1].Y, 3);
  6178. end;
  6179. Canvas.Pen.Color := tbMenuTextColor;
  6180. Canvas.Brush.Color := tbMenuTextColor;
  6181. Canvas.Polygon(P);
  6182. end;
  6183. begin
  6184. if FView.FShowUpArrow then
  6185. DrawArrow(Rect(0, 0, ClientWidth, tbMenuScrollArrowHeight), False);
  6186. if FView.FShowDownArrow then
  6187. DrawArrow(Bounds(0, ClientHeight - tbMenuScrollArrowHeight,
  6188. ClientWidth, tbMenuScrollArrowHeight), True);
  6189. end;
  6190. procedure TTBPopupWindow.WMClose(var Message: TWMClose);
  6191. begin
  6192. { do nothing -- ignore Alt+F4 keypresses }
  6193. end;
  6194. procedure TTBPopupWindow.WMNCCalcSize(var Message: TWMNCCalcSize);
  6195. begin
  6196. with GetNCSize do
  6197. InflateRect(Message.CalcSize_Params^.rgrc[0], -X, -Y);
  6198. inherited;
  6199. end;
  6200. procedure PopupWindowNCPaintProc(Control: TControl; Wnd: HWND; DC: HDC; AppData: Longint);
  6201. var
  6202. R: TRect;
  6203. {$IFNDEF TB2K_USE_STRICT_O2K_MENU_STYLE}
  6204. Brush: HBRUSH;
  6205. {$ENDIF}
  6206. begin
  6207. GetWindowRect(Wnd, R); OffsetRect(R, -R.Left, -R.Top);
  6208. {$IFNDEF TB2K_USE_STRICT_O2K_MENU_STYLE}
  6209. if not AreFlatMenusEnabled then begin
  6210. {$ENDIF}
  6211. DrawEdge(DC, R, EDGE_RAISED, BF_RECT or BF_ADJUST);
  6212. FrameRect(DC, R, GetSysColorBrush(COLOR_BTNFACE));
  6213. {$IFNDEF TB2K_USE_STRICT_O2K_MENU_STYLE}
  6214. end
  6215. else begin
  6216. FrameRect(DC, R, GetSysColorBrush(COLOR_BTNSHADOW));
  6217. Brush := CreateSolidBrush(ColorToRGB(TTBPopupWindow(AppData).Color));
  6218. InflateRect(R, -1, -1);
  6219. FrameRect(DC, R, Brush);
  6220. InflateRect(R, -1, -1);
  6221. FrameRect(DC, R, Brush);
  6222. DeleteObject(Brush);
  6223. end;
  6224. {$ENDIF}
  6225. end;
  6226. procedure TTBPopupWindow.WMNCPaint(var Message: TMessage);
  6227. var
  6228. DC: HDC;
  6229. begin
  6230. DC := GetWindowDC(Handle);
  6231. try
  6232. SelectNCUpdateRgn(Handle, DC, HRGN(Message.WParam));
  6233. PopupWindowNCPaintProc(Self, Handle, DC, Longint(Self));
  6234. finally
  6235. ReleaseDC(Handle, DC);
  6236. end;
  6237. end;
  6238. procedure TTBPopupWindow.WMPrint(var Message: TMessage);
  6239. begin
  6240. HandleWMPrint(Self, Handle, Message, PopupWindowNCPaintProc, Longint(Self));
  6241. end;
  6242. procedure TTBPopupWindow.WMPrintClient(var Message: TMessage);
  6243. begin
  6244. HandleWMPrintClient(Self, Message);
  6245. end;
  6246. procedure TTBPopupWindow.CMHintShow(var Message: TCMHintShow);
  6247. begin
  6248. with Message.HintInfo^ do begin
  6249. HintStr := '';
  6250. if Assigned(FView.Selected) then begin
  6251. CursorRect := FView.Selected.BoundsRect;
  6252. HintStr := FView.FSelected.GetHintText;
  6253. end;
  6254. end;
  6255. end;
  6256. procedure TTBPopupWindow.CMHintShowPause(var Message: TMessage);
  6257. begin
  6258. // Hint was not active previously
  6259. if not Boolean(Message.WParam) then
  6260. begin
  6261. PInteger(Message.LParam)^ := PInteger(Message.LParam)^ * 2;
  6262. end;
  6263. end;
  6264. { TTBItemContainer }
  6265. constructor TTBItemContainer.Create(AOwner: TComponent);
  6266. begin
  6267. inherited;
  6268. FItem := TTBRootItem.Create(Self);
  6269. FItem.ParentComponent := Self;
  6270. end;
  6271. destructor TTBItemContainer.Destroy;
  6272. begin
  6273. FItem.Free;
  6274. inherited;
  6275. end;
  6276. function TTBItemContainer.GetItems: TTBCustomItem;
  6277. begin
  6278. Result := FItem;
  6279. end;
  6280. procedure TTBItemContainer.GetChildren(Proc: TGetChildProc; Root: TComponent);
  6281. begin
  6282. FItem.GetChildren(Proc, Root);
  6283. end;
  6284. function TTBItemContainer.GetImages: TCustomImageList;
  6285. begin
  6286. Result := FItem.SubMenuImages;
  6287. end;
  6288. procedure TTBItemContainer.SetImages(Value: TCustomImageList);
  6289. begin
  6290. FItem.SubMenuImages := Value;
  6291. end;
  6292. { TTBPopupMenu }
  6293. constructor TTBPopupMenu.Create(AOwner: TComponent);
  6294. begin
  6295. inherited;
  6296. FItem := GetRootItemClass.Create(Self);
  6297. FItem.ParentComponent := Self;
  6298. FItem.OnClick := RootItemClick;
  6299. end;
  6300. destructor TTBPopupMenu.Destroy;
  6301. begin
  6302. FItem.Free;
  6303. inherited;
  6304. end;
  6305. function TTBPopupMenu.GetItems: TTBCustomItem;
  6306. begin
  6307. Result := FItem;
  6308. end;
  6309. procedure TTBPopupMenu.GetChildren(Proc: TGetChildProc; Root: TComponent);
  6310. begin
  6311. FItem.GetChildren(Proc, Root);
  6312. end;
  6313. procedure TTBPopupMenu.SetChildOrder(Child: TComponent; Order: Integer);
  6314. begin
  6315. FItem.SetChildOrder(Child, Order);
  6316. end;
  6317. function TTBPopupMenu.GetRootItemClass: TTBRootItemClass;
  6318. begin
  6319. Result := TTBRootItem;
  6320. end;
  6321. function TTBPopupMenu.GetImages: TCustomImageList;
  6322. begin
  6323. Result := FItem.SubMenuImages;
  6324. end;
  6325. function TTBPopupMenu.GetLinkSubitems: TTBCustomItem;
  6326. begin
  6327. Result := FItem.LinkSubitems;
  6328. end;
  6329. function TTBPopupMenu.GetOptions: TTBItemOptions;
  6330. begin
  6331. Result := FItem.Options;
  6332. end;
  6333. procedure TTBPopupMenu.SetImages(Value: TCustomImageList);
  6334. begin
  6335. FItem.SubMenuImages := Value;
  6336. end;
  6337. procedure TTBPopupMenu.SetLinkSubitems(Value: TTBCustomItem);
  6338. begin
  6339. FItem.LinkSubitems := Value;
  6340. end;
  6341. procedure TTBPopupMenu.SetOptions(Value: TTBItemOptions);
  6342. begin
  6343. FItem.Options := Value;
  6344. end;
  6345. procedure TTBPopupMenu.RootItemClick(Sender: TObject);
  6346. begin
  6347. if Sender = FItem then
  6348. Sender := Self;
  6349. DoPopup(Sender);
  6350. end;
  6351. procedure TTBPopupMenu.Popup(X, Y: Integer);
  6352. begin
  6353. PopupEx(X, Y, False);
  6354. end;
  6355. function TTBPopupMenu.PopupEx(X, Y: Integer;
  6356. ReturnClickedItemOnly: Boolean = False): TTBCustomItem;
  6357. begin
  6358. SetPopupPoint(Point(X, Y));
  6359. Result := FItem.Popup(X, Y, TrackButton = tbRightButton,
  6360. TTBPopupAlignment(Alignment), ReturnClickedItemOnly);
  6361. end;
  6362. function TTBPopupMenu.IsShortCut(var Message: TWMKey): Boolean;
  6363. begin
  6364. Result := FItem.IsShortCut(Message);
  6365. end;
  6366. { TTBImageList }
  6367. constructor TTBCustomImageList.Create(AOwner: TComponent);
  6368. begin
  6369. inherited;
  6370. FCheckedImagesChangeLink := TChangeLink.Create;
  6371. FCheckedImagesChangeLink.OnChange := ImageListChanged;
  6372. FDisabledImagesChangeLink := TChangeLink.Create;
  6373. FDisabledImagesChangeLink.OnChange := ImageListChanged;
  6374. FHotImagesChangeLink := TChangeLink.Create;
  6375. FHotImagesChangeLink.OnChange := ImageListChanged;
  6376. FImagesBitmap := TBitmap.Create;
  6377. FImagesBitmap.OnChange := ImagesBitmapChanged;
  6378. FImagesBitmapMaskColor := clFuchsia;
  6379. end;
  6380. destructor TTBCustomImageList.Destroy;
  6381. begin
  6382. FreeAndNil(FImagesBitmap);
  6383. FreeAndNil(FHotImagesChangeLink);
  6384. FreeAndNil(FDisabledImagesChangeLink);
  6385. FreeAndNil(FCheckedImagesChangeLink);
  6386. inherited;
  6387. end;
  6388. procedure TTBCustomImageList.ImagesBitmapChanged(Sender: TObject);
  6389. begin
  6390. if not ImagesBitmap.Empty then begin
  6391. Clear;
  6392. AddMasked(ImagesBitmap, FImagesBitmapMaskColor);
  6393. end;
  6394. end;
  6395. procedure TTBCustomImageList.ImageListChanged(Sender: TObject);
  6396. begin
  6397. Change;
  6398. end;
  6399. procedure TTBCustomImageList.DefineProperties(Filer: TFiler);
  6400. type
  6401. TProc = procedure(ASelf: TObject; Filer: TFiler);
  6402. begin
  6403. if (Filer is TReader) or FImagesBitmap.Empty then
  6404. inherited
  6405. else
  6406. { Bypass TCustomImageList.DefineProperties when we've got an ImageBitmap }
  6407. TProc(@TComponentAccess.DefineProperties)(Self, Filer);
  6408. end;
  6409. procedure TTBCustomImageList.DrawState(Canvas: TCanvas; X, Y, Index: Integer;
  6410. Enabled, Selected, Checked: Boolean);
  6411. begin
  6412. if not Enabled and Assigned(DisabledImages) then
  6413. DisabledImages.Draw(Canvas, X, Y, Index)
  6414. else if Checked and Assigned(CheckedImages) then
  6415. CheckedImages.Draw(Canvas, X, Y, Index, Enabled)
  6416. else if Selected and Assigned(HotImages) then
  6417. HotImages.Draw(Canvas, X, Y, Index, Enabled)
  6418. else
  6419. Draw(Canvas, X, Y, Index, Enabled);
  6420. end;
  6421. procedure TTBCustomImageList.Notification(AComponent: TComponent; Operation: TOperation);
  6422. begin
  6423. inherited;
  6424. if Operation = opRemove then begin
  6425. if AComponent = CheckedImages then CheckedImages := nil;
  6426. if AComponent = DisabledImages then DisabledImages := nil;
  6427. if AComponent = HotImages then HotImages := nil;
  6428. end;
  6429. end;
  6430. procedure TTBCustomImageList.ChangeImages(var AImageList: TCustomImageList;
  6431. Value: TCustomImageList; AChangeLink: TChangeLink);
  6432. begin
  6433. if Value = Self then
  6434. Value := nil;
  6435. if AImageList <> Value then begin
  6436. if Assigned(AImageList) then
  6437. AImageList.UnregisterChanges(AChangeLink);
  6438. AImageList := Value;
  6439. if Assigned(Value) then begin
  6440. Value.RegisterChanges(AChangeLink);
  6441. Value.FreeNotification(Self);
  6442. end;
  6443. { Don't call Change while loading because it causes the Delphi IDE to
  6444. think the form has been modified (?). Also, don't call Change while
  6445. destroying since there's no reason to. }
  6446. if not(csLoading in ComponentState) and
  6447. not(csDestroying in ComponentState) then
  6448. Change;
  6449. end;
  6450. end;
  6451. procedure TTBCustomImageList.SetCheckedImages(Value: TCustomImageList);
  6452. begin
  6453. ChangeImages(FCheckedImages, Value, FCheckedImagesChangeLink);
  6454. end;
  6455. procedure TTBCustomImageList.SetDisabledImages(Value: TCustomImageList);
  6456. begin
  6457. ChangeImages(FDisabledImages, Value, FDisabledImagesChangeLink);
  6458. end;
  6459. procedure TTBCustomImageList.SetHotImages(Value: TCustomImageList);
  6460. begin
  6461. ChangeImages(FHotImages, Value, FHotImagesChangeLink);
  6462. end;
  6463. procedure TTBCustomImageList.SetImagesBitmap(Value: TBitmap);
  6464. begin
  6465. FImagesBitmap.Assign(Value);
  6466. end;
  6467. procedure TTBCustomImageList.SetImagesBitmapMaskColor(Value: TColor);
  6468. begin
  6469. if FImagesBitmapMaskColor <> Value then begin
  6470. FImagesBitmapMaskColor := Value;
  6471. ImagesBitmapChanged(nil);
  6472. end;
  6473. end;
  6474. { TTBBaseAccObject }
  6475. { According to the MSAA docs:
  6476. "With Active Accessibility 2.0, servers can return E_NOTIMPL from IDispatch
  6477. methods and Active Accessibility will implement the IAccessible interface
  6478. for them."
  6479. And there was much rejoicing. }
  6480. function TTBBaseAccObject.GetIDsOfNames(const IID: TGUID; Names: Pointer;
  6481. NameCount, LocaleID: Integer; DispIDs: Pointer): HResult;
  6482. begin
  6483. Result := E_NOTIMPL;
  6484. end;
  6485. function TTBBaseAccObject.GetTypeInfo(Index, LocaleID: Integer;
  6486. out TypeInfo): HResult;
  6487. begin
  6488. Result := E_NOTIMPL;
  6489. end;
  6490. function TTBBaseAccObject.GetTypeInfoCount(out Count: Integer): HResult;
  6491. begin
  6492. Result := E_NOTIMPL;
  6493. end;
  6494. function TTBBaseAccObject.Invoke(DispID: Integer; const IID: TGUID;
  6495. LocaleID: Integer; Flags: Word; var Params; VarResult, ExcepInfo,
  6496. ArgErr: Pointer): HResult;
  6497. begin
  6498. Result := E_NOTIMPL;
  6499. end;
  6500. { Initialization & finalization }
  6501. var
  6502. ToolbarFonts: TDictionary<Integer, TFont>;
  6503. ToolbarFont: TFont;
  6504. function CreateToolbarFont(PixelsPerInch: Integer): HFONT;
  6505. var
  6506. NonClientMetrics: TNonClientMetrics;
  6507. begin
  6508. FillChar(NonClientMetrics, SizeOf(NonClientMetrics), 0);
  6509. NonClientMetrics.cbSize := SizeOf(NonClientMetrics);
  6510. Assert(HasSystemParametersInfoForPixelsPerInch);
  6511. if SystemParametersInfoForPixelsPerInch(SPI_GETNONCLIENTMETRICS, SizeOf(NonClientMetrics), @NonClientMetrics, 0, PixelsPerInch) then
  6512. begin
  6513. Result := CreateFontIndirect(NonClientMetrics.lfMenuFont);
  6514. end
  6515. else
  6516. begin
  6517. Result := 0;
  6518. end;
  6519. end;
  6520. function GetToolbarFont(Control: TControl; PixelsPerInch: Integer): TFont; overload;
  6521. var
  6522. H: HFONT;
  6523. begin
  6524. // Temporary redundant legacy fallback to limit impact of per-monitor DPI change
  6525. if not HasSystemParametersInfoForPixelsPerInch then
  6526. begin
  6527. Result := ToolbarFont;
  6528. end
  6529. else
  6530. begin
  6531. // See the comment in TTBView.GetFont
  6532. if not Assigned(ToolbarFonts) then
  6533. begin
  6534. Result := nil;
  6535. end
  6536. else
  6537. begin
  6538. if PixelsPerInch < 0 then
  6539. begin
  6540. PixelsPerInch := GetControlPixelsPerInch(Control);
  6541. end;
  6542. if not ToolbarFonts.TryGetValue(PixelsPerInch, Result) then
  6543. begin
  6544. H := CreateToolbarFont(PixelsPerInch);
  6545. if H <> 0 then
  6546. begin
  6547. Result := TFont.Create;
  6548. Result.Handle := H;
  6549. ToolbarFonts.Add(PixelsPerInch, Result);
  6550. end
  6551. else
  6552. begin
  6553. Result := nil;
  6554. end;
  6555. end
  6556. end;
  6557. end;
  6558. end;
  6559. function GetToolbarFont(PixelsPerInch: Integer): TFont; overload;
  6560. begin
  6561. Result := GetToolbarFont(nil, PixelsPerInch);
  6562. end;
  6563. function GetToolbarFont(Control: TControl): TFont; overload;
  6564. begin
  6565. Result := GetToolbarFont(Control, -1);
  6566. end;
  6567. procedure TBInitToolbarSystemFont;
  6568. var
  6569. NonClientMetrics: TNonClientMetrics;
  6570. FontPair: TPair<Integer, TFont>;
  6571. begin
  6572. NonClientMetrics.cbSize := SizeOf(NonClientMetrics);
  6573. if SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 0, @NonClientMetrics, 0) then
  6574. begin
  6575. ToolbarFont.Handle := CreateFontIndirect(NonClientMetrics.lfMenuFont);
  6576. end;
  6577. for FontPair in ToolbarFonts do
  6578. begin
  6579. FontPair.Value.Handle := CreateToolbarFont(FontPair.Key);
  6580. end;
  6581. end;
  6582. procedure TBFinalizeToolbarSystemFont;
  6583. var
  6584. Font: TFont;
  6585. begin
  6586. for Font in ToolbarFonts.Values do
  6587. begin
  6588. Font.Free;
  6589. end;
  6590. end;
  6591. initialization
  6592. ToolbarFonts := TDictionary<Integer, TFont>.Create;
  6593. ToolbarFont := TFont.Create;
  6594. TBInitToolbarSystemFont;
  6595. finalization
  6596. DestroyClickWnd;
  6597. FreeAndNil(ToolbarFont);
  6598. TBFinalizeToolbarSystemFont;
  6599. FreeAndNil(ToolbarFonts);
  6600. end.