GR32_Image.pas 143 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127
  1. unit GR32_Image;
  2. (* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1 or LGPL 2.1 with linking exception
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * Alternatively, the contents of this file may be used under the terms of the
  16. * Free Pascal modified version of the GNU Lesser General Public License
  17. * Version 2.1 (the "FPC modified LGPL License"), in which case the provisions
  18. * of this license are applicable instead of those above.
  19. * Please see the file LICENSE.txt for additional information concerning this
  20. * license.
  21. *
  22. * The Original Code is Graphics32
  23. *
  24. * The Initial Developer of the Original Code is
  25. * Alex A. Denisov
  26. *
  27. * Portions created by the Initial Developer are Copyright (C) 2000-2009
  28. * the Initial Developer. All Rights Reserved.
  29. *
  30. * ***** END LICENSE BLOCK ***** *)
  31. interface
  32. //------------------------------------------------------------------------------
  33. // Define CONSOLIDATE_UPDATERECTS to consolidate potentially overlapping
  34. // update areas into as few separate non-overlapping areas as possible.
  35. {$define CONSOLIDATE_UPDATERECTS}
  36. {-$define TRACE_BEGINENDUPDATE} // Batching trace
  37. {$define MOUSE_UPDATE_BATCHING}
  38. {-$define PAINT_UNCLIPPED} // Circumvent WM_PAINT/BeginDraw/EndDraw update region clipping
  39. {-$define UPDATERECT_DEBUGDRAW} // Display update rects. See issue # 202
  40. {$define UPDATERECT_DEBUGDRAW_RANDOM_COLORS} // More cow bell!
  41. {-$define UPDATERECT_SLOWMOTION} // Slow everything down so we can see what's going on
  42. {-$define UPDATERECT_SUPERSLOWMOTION} // Matrix bullet time mode
  43. {$ifdef UPDATERECT_DEBUGDRAW}
  44. {$define PAINT_UNCLIPPED}
  45. {$endif}
  46. //------------------------------------------------------------------------------
  47. {$include GR32.inc}
  48. uses
  49. {$if defined(MSWINDOWS)}
  50. Windows,
  51. {$ifend}
  52. {$if defined(FRAMEWORK_VCL)}
  53. Messages,
  54. {$elseif defined(FRAMEWORK_FMX)}
  55. {$elseif defined(FRAMEWORK_LCL)}
  56. LCLIntf, LCLType, LMessages,
  57. {$ifend}
  58. {$if defined(FRAMEWORK_VCL)}
  59. VCL.Graphics,
  60. VCL.Controls,
  61. VCL.StdCtrls, // TScrollBar
  62. {$elseif defined(FRAMEWORK_FMX)}
  63. FMX.Graphics,
  64. FMX.Controls,
  65. {$elseif defined(FRAMEWORK_LCL)}
  66. Graphics,
  67. Controls,
  68. StdCtrls, // TScrollBar
  69. {$ifend}
  70. Types,
  71. Classes,
  72. GR32,
  73. GR32_Layers,
  74. GR32_Containers,
  75. GR32_RepaintOpt;
  76. {$IFNDEF FPC}
  77. // Animated zoom relies on amEasing which relies on the System.Diagnostics unit (TStopwatch)
  78. {$define AnimatedZoom}
  79. {$ENDIF}
  80. //------------------------------------------------------------------------------
  81. //
  82. // TPaintStages & TPaintStage
  83. //
  84. //------------------------------------------------------------------------------
  85. const
  86. { Paint Stage Constants }
  87. PST_CUSTOM = 1; // Calls OnPaint with # of current stage in parameter
  88. PST_CLEAR_BUFFER = 2; // Clears the buffer
  89. PST_CLEAR_BACKGND = 3; // Clears a visible buffer area
  90. PST_DRAW_BITMAP = 4; // Draws a bitmap
  91. PST_DRAW_LAYERS = 5; // Draw layers (Parameter = Layer Mask)
  92. PST_CONTROL_FRAME = 6; // Draws a dotted frame around the control
  93. PST_BITMAP_FRAME = 7; // Draws a dotted frame around the scaled bitmap
  94. type
  95. TPaintStageEvent = procedure(Sender: TObject; Buffer: TBitmap32; StageNum: Cardinal) of object;
  96. { TPaintStage }
  97. TPaintStageMaskValue = (
  98. psmDesignTime, // Stage is painted at design-time
  99. psmRunTime, // Stage is painted at run-time
  100. psmExport // Stage is painted when exporting the image via PaintTo
  101. );
  102. TPaintStageMask = set of TPaintStageMaskValue;
  103. TPaintStages = class;
  104. TPaintStage = record
  105. private
  106. FPaintStages: TPaintStages;
  107. FMask: TPaintStageMask;
  108. FStage: Cardinal;
  109. FParameter: Cardinal;
  110. private
  111. function GetDesignTime: boolean;
  112. function GetRunTime: boolean;
  113. procedure SetDesignTime(const Value: boolean);
  114. procedure SetRunTime(const Value: boolean);
  115. procedure SetMask(const Value: TPaintStageMask);
  116. procedure SetStage(const Value: Cardinal);
  117. public
  118. property Mask: TPaintStageMask read FMask write SetMask;
  119. property Stage: Cardinal read FStage write SetStage; // a PST_* constant
  120. property Parameter: Cardinal read FParameter write FParameter; // an optional parameter
  121. // Backward compatibility
  122. property DsgnTime: boolean read GetDesignTime write SetDesignTime;
  123. property RunTime: boolean read GetRunTime write SetRunTime;
  124. end;
  125. PPaintStage = ^TPaintStage;
  126. { TPaintStages }
  127. TPaintStages = class
  128. private
  129. FItems: array of TPaintStage;
  130. FDirty: boolean;
  131. function GetItem(Index: Integer): PPaintStage;
  132. public
  133. constructor Create;
  134. destructor Destroy; override;
  135. function Add: PPaintStage;
  136. procedure Clear;
  137. function Count: Integer;
  138. procedure Delete(Index: Integer);
  139. function Insert(Index: Integer): PPaintStage;
  140. property Items[Index: Integer]: PPaintStage read GetItem; default;
  141. property Dirty: boolean read FDirty write FDirty;
  142. end;
  143. { Alignment of the bitmap in TCustomImage32 }
  144. TBitmapAlign = (baTopLeft, baCenter, baTile, baCustom);
  145. TScaleMode = (smNormal, smStretch, smScale, smResize, smOptimal, smOptimalScaled);
  146. TPaintBoxOptions = set of (pboWantArrowKeys, pboAutoFocus);
  147. TRepaintMode = (rmFull, rmDirect, rmOptimizer);
  148. //------------------------------------------------------------------------------
  149. //
  150. // Graphics32 custom control base class
  151. //
  152. //------------------------------------------------------------------------------
  153. // This can be eliminated if (when) we abandon FireMonkey support
  154. //------------------------------------------------------------------------------
  155. type
  156. {$if defined(FRAMEWORK_VCL)}
  157. TGraphics32ControlBaseClass = TCustomControl;
  158. {$elseif defined(FRAMEWORK_FMX)}
  159. TGraphics32ControlBaseClass = TControl;
  160. {$elseif defined(FRAMEWORK_LCL)}
  161. TGraphics32ControlBaseClass = TCustomControl;
  162. {$ifend}
  163. //------------------------------------------------------------------------------
  164. //
  165. // TCustomPaintBox32
  166. //
  167. //------------------------------------------------------------------------------
  168. type
  169. TCustomPaintBox32 = class(TGraphics32ControlBaseClass)
  170. strict private
  171. FBuffer: TBitmap32;
  172. FBufferOversize: Integer;
  173. FBufferValid: Boolean;
  174. FRepaintMode: TRepaintMode;
  175. FInvalidRects: TRectList;
  176. FUpdateRects: TRectList;
  177. FForceFullRepaint: Boolean;
  178. FPartialRepaintQueued: boolean;
  179. FRepaintOptimizer: TCustomRepaintOptimizer;
  180. FOptions: TPaintBoxOptions;
  181. FUpdateCount: Integer;
  182. FLockUpdateCount: Integer;
  183. FModified: boolean;
  184. FMouseInControl: Boolean;
  185. FOnGDIOverlay: TNotifyEvent;
  186. FOnMouseEnter: TNotifyEvent;
  187. FOnMouseLeave: TNotifyEvent;
  188. FOnChange: TNotifyEvent;
  189. procedure SetBufferOversize(Value: Integer);
  190. {$IFDEF FPC}
  191. procedure WMEraseBkgnd(var Message: TLMEraseBkgnd); message LM_ERASEBKGND;
  192. procedure WMGetDlgCode(var Msg: TLMessage); message LM_GETDLGCODE;
  193. procedure WMPaint(var Message: TLMPaint); message LM_PAINT;
  194. procedure CMMouseEnter(var Message: TLMessage); message LM_MOUSEENTER;
  195. procedure CMMouseLeave(var Message: TLMessage); message LM_MOUSELEAVE;
  196. {$ELSE}
  197. procedure WMEraseBkgnd(var Message: TWmEraseBkgnd); message WM_ERASEBKGND;
  198. procedure WMGetDlgCode(var Msg: TWmGetDlgCode); message WM_GETDLGCODE;
  199. procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
  200. procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
  201. procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
  202. {$ENDIF}
  203. protected
  204. procedure FullUpdateHandler(Sender: TObject);
  205. procedure AreaUpdateHandler(Sender: TObject; const Area: TRect; const Info: Cardinal);
  206. protected
  207. // IUpdateRectNotification
  208. procedure AreaUpdated(const AArea: TRect; const AInfo: Cardinal); virtual;
  209. protected
  210. procedure CreateBuffer; virtual;
  211. function CreateRepaintOptimizer(ABuffer: TBitmap32; AInvalidRects: TRectList): TCustomRepaintOptimizer; virtual;
  212. procedure RepaintModeChanged; virtual;
  213. procedure SetRepaintMode(const Value: TRepaintMode); virtual;
  214. function CustomRepaint: Boolean; virtual;
  215. function InvalidRectsAvailable: Boolean; virtual;
  216. procedure SetPartialRepaintQueued;
  217. procedure DoPrepareInvalidRects; virtual;
  218. procedure DoPaintBuffer; virtual;
  219. procedure DoPaintGDIOverlay; virtual;
  220. procedure DoBufferResized(const OldWidth, OldHeight: Integer); virtual;
  221. procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
  222. procedure MouseEnter; {$IFDEF FPC} override; {$ELSE} virtual; {$ENDIF}
  223. procedure MouseLeave; {$IFDEF FPC} override; {$ELSE} virtual; {$ENDIF}
  224. procedure AssignTo(Dest: TPersistent); override;
  225. procedure Loaded; override;
  226. procedure Paint; override;
  227. procedure ResetInvalidRects;
  228. procedure ResizeBuffer;
  229. procedure DoChanged; virtual;
  230. property RepaintOptimizer: TCustomRepaintOptimizer read FRepaintOptimizer;
  231. property BufferValid: Boolean read FBufferValid write FBufferValid;
  232. property InvalidRects: TRectList read FInvalidRects;
  233. property UpdateRects: TRectList read FUpdateRects;
  234. property UpdateCount: Integer read FUpdateCount;
  235. property LockUpdateCount: Integer read FLockUpdateCount;
  236. property Modified: boolean read FModified;
  237. public
  238. constructor Create(AOwner: TComponent); override;
  239. destructor Destroy; override;
  240. procedure BeginUpdate; {$IFDEF USEINLINING} inline; {$ENDIF}
  241. procedure EndUpdate;
  242. procedure Changed; {$IFDEF USEINLINING} inline; {$ENDIF}
  243. procedure BeginLockUpdate; {$IFDEF USEINLINING} inline; {$ENDIF}
  244. procedure EndLockUpdate; {$IFDEF USEINLINING} inline; {$ENDIF}
  245. function GetViewportRect: TRect; virtual;
  246. procedure Flush; overload;
  247. procedure Flush(const SrcRect: TRect); overload;
  248. procedure Invalidate; override;
  249. procedure ForceFullInvalidate; virtual;
  250. procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer); override;
  251. property Buffer: TBitmap32 read FBuffer;
  252. property BufferOversize: Integer read FBufferOversize write SetBufferOversize;
  253. property Options: TPaintBoxOptions read FOptions write FOptions default [];
  254. property MouseInControl: Boolean read FMouseInControl;
  255. property RepaintMode: TRepaintMode read FRepaintMode write SetRepaintMode default rmFull;
  256. property OnChange: TNotifyEvent read FOnChange write FOnChange;
  257. property OnMouseEnter: TNotifyEvent read FOnMouseEnter write FOnMouseEnter;
  258. property OnMouseLeave: TNotifyEvent read FOnMouseLeave write FOnMouseLeave;
  259. property OnGDIOverlay: TNotifyEvent read FOnGDIOverlay write FOnGDIOverlay;
  260. end;
  261. //------------------------------------------------------------------------------
  262. //
  263. // TPaintBox32
  264. //
  265. //------------------------------------------------------------------------------
  266. type
  267. TPaintBox32 = class(TCustomPaintBox32)
  268. strict private
  269. FOnPaintBuffer: TNotifyEvent;
  270. protected
  271. procedure DoPaintBuffer; override;
  272. public
  273. property Canvas;
  274. published
  275. property Align;
  276. property Anchors;
  277. property AutoSize;
  278. property Constraints;
  279. property Cursor;
  280. property DragCursor;
  281. property DragMode;
  282. property Options;
  283. property ParentShowHint;
  284. property PopupMenu;
  285. property RepaintMode;
  286. property ShowHint;
  287. property TabOrder;
  288. property TabStop;
  289. property Visible;
  290. {$IFNDEF PLATFORM_INDEPENDENT}
  291. property OnCanResize;
  292. {$ENDIF}
  293. property OnChange;
  294. property OnClick;
  295. property OnDblClick;
  296. property OnDragDrop;
  297. property OnDragOver;
  298. property OnEndDrag;
  299. property OnGDIOverlay;
  300. property OnMouseDown;
  301. property OnMouseMove;
  302. property OnMouseUp;
  303. property OnMouseWheel;
  304. property OnMouseWheelDown;
  305. property OnMouseWheelUp;
  306. property OnMouseEnter;
  307. property OnMouseLeave;
  308. property OnPaintBuffer: TNotifyEvent read FOnPaintBuffer write FOnPaintBuffer;
  309. property OnResize;
  310. property OnStartDrag;
  311. end;
  312. //------------------------------------------------------------------------------
  313. //
  314. // TCustomImage32
  315. //
  316. //------------------------------------------------------------------------------
  317. type
  318. TImgMouseEvent = procedure(Sender: TObject; Button: TMouseButton;
  319. Shift: TShiftState; X, Y: Integer; Layer: TCustomLayer) of object;
  320. TImgMouseMoveEvent = procedure(Sender: TObject; Shift: TShiftState;
  321. X, Y: Integer; Layer: TCustomLayer) of object;
  322. TPaintStageHandler = procedure(Dest: TBitmap32; StageNum: Integer) of object;
  323. TBackgroundCheckerStyle = (bcsCustom, bcsNone, bcsLight, bcsMedium, bcsDark);
  324. TBackgroundFillStyle = (bfsColor, bfsCheckers, bfsPattern);
  325. TBackgroundOptions = class(TNotifiablePersistent)
  326. private type
  327. TCheckersColors = array[0..1] of TColor32;
  328. strict private
  329. FPatternBitmap: TBitmap32;
  330. FOuterBorderColor: TColor;
  331. FInnerBorderWidth: integer;
  332. FInnerBorderColor: TColor;
  333. FDropShadowBitmap: TBitmap32;
  334. FDropShadowOffset: integer;
  335. FDropShadowSize: integer;
  336. FDropShadowColor: TColor32;
  337. FCheckersColors: TCheckersColors;
  338. FCheckersStyle: TBackgroundCheckerStyle;
  339. FCheckersExponent: integer;
  340. FFillStyle: TBackgroundFillStyle;
  341. protected
  342. procedure SetFillStyle(const Value: TBackgroundFillStyle);
  343. procedure SetPatternBitmap(const Value: TBitmap32);
  344. procedure SetDropShadowBitmap(const Value: TBitmap32);
  345. procedure SetDropShadowColor(const Value: TColor32);
  346. procedure SetDropShadowOffset(const Value: integer);
  347. procedure SetDropShadowSize(const Value: integer);
  348. procedure SetInnerBorderColor(const Value: TColor);
  349. procedure SetInnerBorderWidth(const Value: integer);
  350. procedure SetOuterBorderColor(const Value: TColor);
  351. procedure SetCheckersStyle(const Value: TBackgroundCheckerStyle);
  352. function GetCheckersColor(const Index: Integer): TColor;
  353. procedure SetCheckersColor(Index: integer; const Value: TColor);
  354. procedure SetCheckersExponent(const Value: integer);
  355. function IsFillStyleStored: Boolean;
  356. function IsCheckersColorsStored(Index: integer): boolean;
  357. function IsDropShadowBitmapStored: boolean;
  358. function IsPatternBitmapStored: boolean;
  359. procedure CheckFillStyle;
  360. procedure ChangeHandler(Sender: TObject);
  361. public
  362. constructor Create;
  363. destructor Destroy; override;
  364. property CheckersColors: TCheckersColors read FCheckersColors;
  365. published
  366. property PatternBitmap: TBitmap32 read FPatternBitmap write SetPatternBitmap stored IsPatternBitmapStored;
  367. property OuterBorderColor: TColor read FOuterBorderColor write SetOuterBorderColor default clNone;
  368. property InnerBorderWidth: integer read FInnerBorderWidth write SetInnerBorderWidth default 0;
  369. property InnerBorderColor: TColor read FInnerBorderColor write SetInnerBorderColor default clNone;
  370. property DropShadowColor: TColor32 read FDropShadowColor write SetDropShadowColor default 0;
  371. property DropShadowOffset: integer read FDropShadowOffset write SetDropShadowOffset default 0;
  372. property DropShadowSize: integer read FDropShadowSize write SetDropShadowSize default 0;
  373. property DropShadowBitmap: TBitmap32 read FDropShadowBitmap write SetDropShadowBitmap stored IsDropShadowBitmapStored;
  374. property CheckersStyle: TBackgroundCheckerStyle read FCheckersStyle write SetCheckersStyle default bcsNone;
  375. property CheckersColorOdd: TColor index 0 read GetCheckersColor write SetCheckersColor stored IsCheckersColorsStored;
  376. property CheckersColorEven: TColor index 1 read GetCheckersColor write SetCheckersColor stored IsCheckersColorsStored;
  377. property CheckersExponent: integer read FCheckersExponent write SetCheckersExponent default 3;
  378. // Last property! We need it to be set last when loading from the DFM so the
  379. // fill style rules isn't applied against incomplete property values.
  380. property FillStyle: TBackgroundFillStyle read FFillStyle write SetFillStyle stored IsFillStyleStored;
  381. end;
  382. TMouseShiftState = set of (mssShift, mssAlt, mssCtrl); // Order must be same as TShiftState
  383. TMousePanOptions = class(TNotifiablePersistent)
  384. strict private
  385. FPanCursor: TCursor;
  386. FEnabled: boolean;
  387. FMouseButton: TMouseButton;
  388. FShiftState: TMouseShiftState;
  389. protected
  390. public
  391. constructor Create;
  392. function MatchShiftState(AShiftState: TShiftState): boolean;
  393. published
  394. property Enabled: boolean read FEnabled write FEnabled default False;
  395. property MouseButton: TMouseButton read FMouseButton write FMouseButton default mbLeft;
  396. property ShiftState: TMouseShiftState read FShiftState write FShiftState default [];
  397. property PanCursor: TCursor read FPanCursor write FPanCursor default crSizeAll;
  398. end;
  399. TMouseZoomOptions = class(TNotifiablePersistent)
  400. strict private
  401. FEnabled: boolean;
  402. FInvert: boolean;
  403. FMaintainPivot: boolean;
  404. FMinScale: Single;
  405. FMaxScale: Single;
  406. FSteps: integer;
  407. FZoomFactor: Double;
  408. FShiftState: TMouseShiftState;
  409. FAnimate: boolean;
  410. protected
  411. procedure SetMaxScale(const Value: Single);
  412. procedure SetMinScale(const Value: Single);
  413. procedure SetSteps(const Value: integer);
  414. procedure SetZoomFactor(const Value: Double);
  415. function IsMaxScaleStored: Boolean;
  416. function IsMinScaleStored: Boolean;
  417. procedure UpdateZoomFactor;
  418. public
  419. constructor Create;
  420. function ScaleToLevel(AScale: Single): integer;
  421. function LevelToScale(ALevel: integer): Single;
  422. function MatchShiftState(AShiftState: TShiftState): boolean;
  423. published
  424. property Enabled: boolean read FEnabled write FEnabled default False;
  425. property Invert: boolean read FInvert write FInvert default False;
  426. property ShiftState: TMouseShiftState read FShiftState write FShiftState default [];
  427. property MaintainPivot: boolean read FMaintainPivot write FMaintainPivot default True;
  428. property MinScale: Single read FMinScale write SetMinScale stored IsMinScaleStored;
  429. property MaxScale: Single read FMaxScale write SetMaxScale stored IsMaxScaleStored;
  430. property Steps: integer read FSteps write SetSteps default 12;
  431. property ZoomFactor: Double read FZoomFactor write SetZoomFactor stored False;
  432. property Animate: boolean read FAnimate write FAnimate default False;
  433. end;
  434. TCustomImage32 = class(TCustomPaintBox32, IUpdateRectNotification, ILayerListNotification)
  435. strict private
  436. FBitmap: TBitmap32;
  437. FBitmapAlign: TBitmapAlign;
  438. FLayers: TLayerCollection;
  439. FOffsetHorz: TFloat;
  440. FOffsetVert: TFloat;
  441. FPaintStages: TPaintStages;
  442. FPaintStageHandlers: array of TPaintStageHandler;
  443. FPaintStageNum: array of Integer;
  444. FScaleX: TFloat;
  445. FScaleY: TFloat;
  446. FScaleMode: TScaleMode;
  447. FBackgroundOptions: TBackgroundOptions;
  448. FMousePanOptions: TMousePanOptions;
  449. FMouseZoomOptions: TMouseZoomOptions;
  450. FClicked: boolean;
  451. FIsMousePanning: boolean;
  452. FMousePanStartPos: TPoint;
  453. FHotLayer: TCustomLayer;
  454. FOnBitmapResize: TNotifyEvent;
  455. FOnInitStages: TNotifyEvent;
  456. FOnMouseDown: TImgMouseEvent;
  457. FOnMouseMove: TImgMouseMoveEvent;
  458. FOnMouseUp: TImgMouseEvent;
  459. FOnPaintStage: TPaintStageEvent;
  460. FOnScaleChange: TNotifyEvent;
  461. procedure BackgroundOptionsChangeHandler(Sender: TObject);
  462. procedure BitmapResizeHandler(Sender: TObject);
  463. procedure LayerCollectionChangeHandler(Sender: TObject);
  464. procedure LayerCollectionGDIUpdateHandler(Sender: TObject);
  465. procedure LayerCollectionGetViewportScaleHandler(Sender: TObject; out ScaleX, ScaleY: TFloat);
  466. procedure LayerCollectionGetViewportShiftHandler(Sender: TObject; out ShiftX, ShiftY: TFloat);
  467. function GetOnPixelCombine: TPixelCombineEvent;
  468. procedure SetBitmap(Value: TBitmap32);
  469. procedure SetBitmapAlign(Value: TBitmapAlign);
  470. procedure SetLayers(Value: TLayerCollection);
  471. procedure SetScale(Value: TFloat);
  472. procedure SetScaleX(Value: TFloat);
  473. procedure SetScaleY(Value: TFloat);
  474. procedure SetOnPixelCombine(Value: TPixelCombineEvent);
  475. procedure SetBackgroundOptions(const Value: TBackgroundOptions);
  476. procedure SetMousePanOptions(const Value: TMousePanOptions);
  477. procedure SetMouseZoomOptions(const Value: TMouseZoomOptions);
  478. procedure SetHotTrackLayer(ALayer: TCustomLayer);
  479. protected
  480. FCachedBitmapRect: TRect;
  481. FCacheValid: Boolean;
  482. CachedShiftX, CachedShiftY,
  483. CachedScaleX, CachedScaleY,
  484. CachedRecScaleX, CachedRecScaleY: TFloat;
  485. PaintToMode: Boolean;
  486. procedure UpdateCache(AForce: boolean = False); virtual;
  487. procedure InvalidateCache;
  488. property CacheValid: Boolean read FCacheValid;
  489. function GetCachedBitmapRect: TRect;
  490. property CachedBitmapRect: TRect read GetCachedBitmapRect;
  491. protected
  492. procedure CreateBuffer; override;
  493. procedure RepaintModeChanged; override;
  494. procedure DoBitmapResized; virtual;
  495. procedure BitmapResized; virtual;
  496. procedure BitmapChanged(const Area: TRect); virtual;
  497. function CanMousePan: boolean; virtual;
  498. function CanMouseZoom: boolean; virtual;
  499. function CanAutoSize(var NewWidth, NewHeight: Integer): Boolean; override;
  500. procedure DoInitStages; virtual;
  501. procedure DoPaintBuffer; override;
  502. procedure DoPaintGDIOverlay; override;
  503. procedure DoScaleChange; virtual;
  504. procedure InitDefaultStages; virtual;
  505. function InvalidRectsAvailable: Boolean; override;
  506. procedure KeyDown(var Key: Word; Shift: TShiftState); override;
  507. procedure KeyUp(var Key: Word; Shift: TShiftState); override;
  508. procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); overload; override;
  509. procedure MouseMove(Shift: TShiftState; X, Y: Integer); overload; override;
  510. procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); overload; override;
  511. procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer; Layer: TCustomLayer); reintroduce; overload; virtual;
  512. procedure MouseMove(Shift: TShiftState; X, Y: Integer; Layer: TCustomLayer); reintroduce; overload; virtual;
  513. procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer; Layer: TCustomLayer); reintroduce; overload; virtual;
  514. function DoMouseWheel(Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint): Boolean; override;
  515. procedure MouseEnter; override;
  516. procedure MouseLeave; override;
  517. procedure SetOffsetHorz(Value: TFloat); virtual;
  518. procedure SetOffsetVert(Value: TFloat); virtual;
  519. procedure SetScaleMode(Value: TScaleMode); virtual;
  520. procedure SetXForm(ShiftX, ShiftY, ScaleX, ScaleY: TFloat);
  521. function GetBitmapMargin: integer; virtual;
  522. procedure DoZoom({$ifdef CLOSURE_CAPTURES_CONST}const{$endif} APivot: TFloatPoint; AScale: TFloat; AMaintainPivot, AAnimate: boolean);
  523. procedure DoSetZoom(const APivot: TFloatPoint; AScale: TFloat; AMaintainPivot: boolean);
  524. procedure DoSetPivot(const APivot: TFloatPoint); virtual;
  525. function GetLayerCollectionClass: TLayerCollectionClass; virtual;
  526. function CreateLayerCollection: TLayerCollection; virtual;
  527. procedure Loaded; override;
  528. protected
  529. procedure BitmapChangeHandler(Sender: TObject);
  530. procedure BitmapAreaChangeHandler(Sender: TObject; const Area: TRect; const Info: Cardinal);
  531. protected
  532. procedure InvalidateArea(const AArea: TRect; const AInfo: Cardinal; AOptimize: boolean);
  533. // IUpdateRectNotification
  534. procedure AreaUpdated(const AArea: TRect; const AInfo: Cardinal); override;
  535. protected
  536. // ILayerListNotification
  537. procedure LayerListNotify(ALayer: TCustomLayer; AAction: TLayerListNotification; AIndex: Integer); virtual;
  538. public
  539. constructor Create(AOwner: TComponent); override;
  540. destructor Destroy; override;
  541. function BitmapToControl(const APoint: TPoint): TPoint; overload;
  542. function BitmapToControl(const APoint: TFloatPoint): TFloatPoint; overload;
  543. function BitmapToControl(const ARect: TRect): TRect; overload;
  544. function ControlToBitmap(const APoint: TPoint): TPoint; overload;
  545. function ControlToBitmap(const ARect: TRect; Rounding: TRectRounding = rrOutside): TRect; overload;
  546. function ControlToBitmap(const APoint: TFloatPoint): TFloatPoint; overload;
  547. procedure Update(const Rect: TRect); reintroduce; overload; virtual; deprecated 'Use Invalidate(Rect) instead';
  548. procedure Invalidate; overload; override;
  549. procedure Invalidate(const Rect: TRect); reintroduce; overload; virtual;
  550. procedure ExecBitmapFrame(Dest: TBitmap32; StageNum: Integer); virtual; // PST_BITMAP_FRAME
  551. procedure ExecClearBuffer(Dest: TBitmap32; StageNum: Integer); virtual; // PST_CLEAR_BUFFER
  552. procedure ExecClearBackgnd(Dest: TBitmap32; StageNum: Integer); virtual; // PST_CLEAR_BACKGND
  553. procedure ExecControlFrame(Dest: TBitmap32; StageNum: Integer); virtual; // PST_CONTROL_FRAME
  554. procedure ExecCustom(Dest: TBitmap32; StageNum: Integer); virtual; // PST_CUSTOM
  555. procedure ExecDrawBitmap(Dest: TBitmap32; StageNum: Integer); virtual; // PST_DRAW_BITMAP
  556. procedure ExecDrawLayers(Dest: TBitmap32; StageNum: Integer); virtual; // PST_DRAW_LAYERS
  557. function GetBitmapRect: TRect; virtual;
  558. function GetBitmapSize: TSize; virtual; // Note: Scaled bitmap size
  559. procedure PaintTo(Dest: TBitmap32; DestRect: TRect); virtual;
  560. procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer); override;
  561. procedure SetupBitmap(DoClear: Boolean = False; ClearColor: TColor32 = $FF000000); virtual;
  562. procedure Scroll(Dx, Dy: Integer); overload;
  563. procedure Scroll(Dx, Dy: Single); overload; virtual;
  564. procedure ScrollToCenter; overload;
  565. procedure ScrollToCenter(X, Y: Integer); overload; virtual;
  566. procedure Zoom(AScale: TFloat; const APivot: TFloatPoint; AAnimate: boolean = False); overload;
  567. procedure Zoom(AScale: TFloat; AAnimate: boolean = False); overload;
  568. property Bitmap: TBitmap32 read FBitmap write SetBitmap;
  569. property BitmapAlign: TBitmapAlign read FBitmapAlign write SetBitmapAlign;
  570. property Canvas;
  571. property Layers: TLayerCollection read FLayers write SetLayers;
  572. property OffsetHorz: TFloat read FOffsetHorz write SetOffsetHorz;
  573. property OffsetVert: TFloat read FOffsetVert write SetOffsetVert;
  574. property PaintStages: TPaintStages read FPaintStages;
  575. property Scale: TFloat read FScaleX write SetScale;
  576. property ScaleX: TFloat read FScaleX write SetScaleX;
  577. property ScaleY: TFloat read FScaleY write SetScaleY;
  578. property ScaleMode: TScaleMode read FScaleMode write SetScaleMode;
  579. property Background: TBackgroundOptions read FBackgroundOptions write SetBackgroundOptions;
  580. property MousePan: TMousePanOptions read FMousePanOptions write SetMousePanOptions;
  581. property MouseZoom: TMouseZoomOptions read FMouseZoomOptions write SetMouseZoomOptions;
  582. property IsMousePanning: boolean read FIsMousePanning;
  583. property OnBitmapResize: TNotifyEvent read FOnBitmapResize write FOnBitmapResize;
  584. property OnBitmapPixelCombine: TPixelCombineEvent read GetOnPixelCombine write SetOnPixelCombine;
  585. property OnInitStages: TNotifyEvent read FOnInitStages write FOnInitStages;
  586. property OnMouseDown: TImgMouseEvent read FOnMouseDown write FOnMouseDown;
  587. property OnMouseMove: TImgMouseMoveEvent read FOnMouseMove write FOnMouseMove;
  588. property OnMouseUp: TImgMouseEvent read FOnMouseUp write FOnMouseUp;
  589. property OnPaintStage: TPaintStageEvent read FOnPaintStage write FOnPaintStage;
  590. property OnScaleChange: TNotifyEvent read FOnScaleChange write FOnScaleChange;
  591. end;
  592. //------------------------------------------------------------------------------
  593. //
  594. // TImage32
  595. //
  596. //------------------------------------------------------------------------------
  597. type
  598. TImage32 = class(TCustomImage32)
  599. published
  600. property Align;
  601. property Anchors;
  602. property AutoSize;
  603. property Bitmap;
  604. property BitmapAlign;
  605. property Color;
  606. property Constraints;
  607. property Cursor;
  608. property DragCursor;
  609. property DragMode;
  610. property ParentColor;
  611. property ParentShowHint;
  612. property PopupMenu;
  613. property RepaintMode;
  614. property Scale;
  615. property ScaleMode;
  616. property Background;
  617. property MousePan;
  618. property MouseZoom;
  619. property ShowHint;
  620. property TabOrder;
  621. property TabStop;
  622. property Visible;
  623. property OnBitmapResize;
  624. {$IFNDEF PLATFORM_INDEPENDENT}
  625. property OnCanResize;
  626. {$ENDIF}
  627. property OnChange;
  628. property OnClick;
  629. property OnContextPopup;
  630. property OnDblClick;
  631. property OnGDIOverlay;
  632. property OnDragDrop;
  633. property OnDragOver;
  634. property OnEndDrag;
  635. property OnInitStages;
  636. property OnKeyDown;
  637. property OnKeyPress;
  638. property OnKeyUp;
  639. property OnMouseDown;
  640. property OnMouseMove;
  641. property OnMouseUp;
  642. property OnMouseWheel;
  643. property OnMouseWheelDown;
  644. property OnMouseWheelUp;
  645. property OnMouseEnter;
  646. property OnMouseLeave;
  647. property OnPaintStage;
  648. property OnResize;
  649. property OnStartDrag;
  650. property OnScaleChange;
  651. end;
  652. //------------------------------------------------------------------------------
  653. //
  654. // TCustomImgView32
  655. //
  656. //------------------------------------------------------------------------------
  657. TCustomImgView32 = class;
  658. { TIVScrollProperties }
  659. TScrollBarVisibility = (svAlways, svHidden, svAuto);
  660. TImageViewScrollProperties = class(TPersistent)
  661. private
  662. FOwner: TCustomImgView32;
  663. FVisibility: TScrollBarVisibility;
  664. FIncrement: Integer;
  665. FSize: Integer;
  666. procedure SetIncrement(Value: Integer);
  667. procedure SetSize(Value: Integer);
  668. procedure SetVisibility(const Value: TScrollbarVisibility);
  669. procedure SkipValue(Reader: TReader);
  670. protected
  671. procedure DefineProperties(Filer: TFiler); override;
  672. public
  673. constructor Create(AOwner: TCustomImgView32);
  674. published
  675. property Increment: Integer read FIncrement write SetIncrement default 8;
  676. property Size: Integer read FSize write SetSize default 0;
  677. property Visibility: TScrollBarVisibility read FVisibility write SetVisibility default svAlways;
  678. end;
  679. TSizeGripStyle = (sgAuto, sgNone, sgAlways);
  680. { TCustomImgView32 }
  681. TCustomImgView32 = class(TCustomImage32)
  682. strict private type
  683. TOffsetChange = (ocOffsetHorz, ocOffsetVert, ocScrollBars, ocScale, ocBitmapSize, ocControlSize);
  684. TOffsetChanges = set of TOffsetChange;
  685. strict private
  686. FCentered: Boolean;
  687. FScrollBars: TImageViewScrollProperties;
  688. FHorScroll: TScrollBar;
  689. FVerScroll: TScrollBar;
  690. FBitmapSize: TSize;
  691. FViewportSize: TSize;
  692. FSizeGrip: TSizeGripStyle;
  693. FOnScroll: TNotifyEvent;
  694. FOverSize: Integer;
  695. FOffsetChangeLock: integer;
  696. FOffsetChanges: TOffsetChanges;
  697. procedure SetCentered(Value: Boolean);
  698. procedure SetScrollBars(Value: TImageViewScrollProperties);
  699. procedure SetSizeGrip(Value: TSizeGripStyle);
  700. procedure SetOverSize(const Value: Integer);
  701. protected
  702. property HScroll: TScrollBar read FHorScroll;
  703. property VScroll: TScrollBar read FVerScroll;
  704. procedure DoUpdateOffsets;
  705. procedure BeginOffset;
  706. procedure UpdateOffsets(OffsetChanges: TOffsetChanges);
  707. procedure EndOffset;
  708. procedure UpdateScrollBar(ScrollBar: TScrollBar; ScrollMax, ScrollThumbSize: integer);
  709. procedure UpdateScrollbarVisibility;
  710. procedure SetOffsetHorz(Value: TFloat); override;
  711. procedure SetOffsetVert(Value: TFloat); override;
  712. procedure BitmapResized; override;
  713. procedure DoDrawSizeGrip(R: TRect);
  714. procedure DoScaleChange; override;
  715. function CanMousePan: boolean; override;
  716. procedure DoScroll; virtual;
  717. function GetBitmapMargin: integer; override;
  718. function GetOuterScaledBitmapSize: TSize; // Scaled bitmap + unscaled margin
  719. function CanShowScrollBars: Boolean;
  720. function GetScrollBarsVisible: Boolean;
  721. function GetScrollBarSize: Integer;
  722. function GetSizeGripRect: TRect;
  723. function IsSizeGripVisible: Boolean;
  724. procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
  725. procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
  726. procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer; Layer: TCustomLayer); override;
  727. procedure Paint; override;
  728. procedure PaintSizeGrip; virtual;
  729. procedure Recenter;
  730. procedure SetScaleMode(Value: TScaleMode); override;
  731. procedure DoSetPivot(const APivot: TFloatPoint); override;
  732. procedure ScrollHandler(Sender: TObject); virtual;
  733. procedure ScrollChangingHandler(Sender: TObject; ScrollCode: TScrollCode; var ScrollPos: Integer);
  734. public
  735. constructor Create(AOwner: TComponent); override;
  736. destructor Destroy; override;
  737. function GetViewportRect: TRect; override;
  738. procedure Loaded; override;
  739. procedure Resize; override;
  740. procedure ScrollToCenter(X, Y: Integer); override;
  741. procedure Scroll(Dx, Dy: Single); override;
  742. property Centered: Boolean read FCentered write SetCentered default True;
  743. property ScrollBars: TImageViewScrollProperties read FScrollBars write SetScrollBars;
  744. property SizeGrip: TSizeGripStyle read FSizeGrip write SetSizeGrip default sgAuto;
  745. property OverSize: Integer read FOverSize write SetOverSize;
  746. property OnScroll: TNotifyEvent read FOnScroll write FOnScroll;
  747. end;
  748. //------------------------------------------------------------------------------
  749. //
  750. // TImgView32
  751. //
  752. //------------------------------------------------------------------------------
  753. type
  754. TImgView32 = class(TCustomImgView32)
  755. property Align;
  756. property Anchors;
  757. property AutoSize;
  758. property Bitmap;
  759. property BitmapAlign;
  760. property Centered;
  761. property Color;
  762. property Constraints;
  763. property Cursor;
  764. property DragCursor;
  765. property DragMode;
  766. property ParentColor;
  767. property ParentShowHint;
  768. property PopupMenu;
  769. property RepaintMode;
  770. property Scale;
  771. property ScaleMode;
  772. property Background;
  773. property MousePan;
  774. property MouseZoom;
  775. property ScrollBars;
  776. property ShowHint;
  777. property SizeGrip;
  778. property OverSize;
  779. property TabOrder;
  780. property TabStop;
  781. property Visible;
  782. property OnBitmapResize;
  783. {$IFNDEF PLATFORM_INDEPENDENT}
  784. property OnCanResize;
  785. {$ENDIF}
  786. property OnClick;
  787. property OnChange;
  788. property OnDblClick;
  789. property OnDragDrop;
  790. property OnDragOver;
  791. property OnEndDrag;
  792. property OnGDIOverlay;
  793. property OnInitStages;
  794. property OnKeyDown;
  795. property OnKeyPress;
  796. property OnKeyUp;
  797. property OnMouseDown;
  798. property OnMouseEnter;
  799. property OnMouseLeave;
  800. property OnMouseMove;
  801. property OnMouseUp;
  802. property OnMouseWheel;
  803. property OnMouseWheelDown;
  804. property OnMouseWheelUp;
  805. property OnPaintStage;
  806. property OnResize;
  807. property OnScroll;
  808. property OnStartDrag;
  809. property OnScaleChange;
  810. end;
  811. //------------------------------------------------------------------------------
  812. //
  813. // TBitmap32List
  814. //
  815. //------------------------------------------------------------------------------
  816. type
  817. { TBitmap32Item }
  818. { A bitmap container designed to be inserted into TBitmap32Collection }
  819. TBitmap32Item = class(TCollectionItem)
  820. private
  821. FBitmap: TBitmap32;
  822. procedure SetBitmap(ABitmap: TBitmap32);
  823. protected
  824. procedure AssignTo(Dest: TPersistent); override;
  825. public
  826. constructor Create(Collection: TCollection); override;
  827. destructor Destroy; override;
  828. published
  829. property Bitmap: TBitmap32 read FBitmap write SetBitmap;
  830. end;
  831. TBitmap32ItemClass = class of TBitmap32Item;
  832. { TBitmap32Collection }
  833. { A collection of TBitmap32Item objects }
  834. TBitmap32Collection = class(TCollection)
  835. private
  836. FOwner: TPersistent;
  837. function GetItem(Index: Integer): TBitmap32Item;
  838. procedure SetItem(Index: Integer; Value: TBitmap32Item);
  839. protected
  840. function GetOwner: TPersistent; override;
  841. public
  842. constructor Create(AOwner: TPersistent; ItemClass: TBitmap32ItemClass);
  843. function Add: TBitmap32Item;
  844. property Items[Index: Integer]: TBitmap32Item read GetItem write SetItem; default;
  845. end;
  846. { TBitmap32List }
  847. { A component that stores TBitmap32Collection }
  848. TBitmap32List = class(TComponent)
  849. private
  850. FBitmap32Collection: TBitmap32Collection;
  851. procedure SetBitmap(Index: Integer; Value: TBitmap32);
  852. function GetBitmap(Index: Integer): TBitmap32;
  853. procedure SetBitmap32Collection(Value: TBitmap32Collection);
  854. public
  855. constructor Create(AOwner: TComponent); override;
  856. destructor Destroy; override;
  857. property Bitmap[Index: Integer]: TBitmap32 read GetBitmap write SetBitmap; default;
  858. published
  859. property Bitmaps: TBitmap32Collection read FBitmap32Collection write SetBitmap32Collection;
  860. end;
  861. //------------------------------------------------------------------------------
  862. //
  863. // Global options
  864. //
  865. //------------------------------------------------------------------------------
  866. var
  867. DefaultCheckersColors: array[TBackgroundCheckerStyle] of TBackgroundOptions.TCheckersColors =
  868. (($FFFFFFFF, $FF000000),
  869. ($FFFFFFFF, $FFFFFFFF),
  870. ($FFFFFFFF, $FFEBEBEB),
  871. ($FFFFFFFF, $FFD0D0D0),
  872. ($FFFFFFFF, $FFB0B0B0));
  873. // Maximum duration of animated zoom
  874. ZoomAnimateTime: integer = 300; // mS
  875. // Time between each zoom step. 1000 / ZoomAnimateDeltaTime = frame rate
  876. ZoomAnimateDeltaTime: integer = 5; // mS
  877. //------------------------------------------------------------------------------
  878. //------------------------------------------------------------------------------
  879. //------------------------------------------------------------------------------
  880. implementation
  881. uses
  882. Math,
  883. SysUtils,
  884. {$if not defined(FRAMEWORK_FMX)}
  885. Forms,
  886. {$ifend}
  887. {$if defined(MSWINDOWS)}
  888. MMSystem, // TimeGetTime
  889. Themes,
  890. {$ifend}
  891. {$if defined(AnimatedZoom)}
  892. amEasing,
  893. {$ifend}
  894. GR32_MicroTiles,
  895. GR32_Backends,
  896. GR32_LowLevel,
  897. GR32_Resamplers,
  898. GR32_Backends_Generic;
  899. type
  900. TLayerAccess = class(TCustomLayer);
  901. TLayerCollectionAccess = class(TLayerCollection);
  902. const
  903. DefaultRepaintOptimizerClass: TCustomRepaintOptimizerClass = TMicroTilesRepaintOptimizer;
  904. //------------------------------------------------------------------------------
  905. //
  906. // TPaintStage
  907. //
  908. //------------------------------------------------------------------------------
  909. function TPaintStage.GetDesignTime: boolean;
  910. begin
  911. Result := (psmDesignTime in FMask);
  912. end;
  913. function TPaintStage.GetRunTime: boolean;
  914. begin
  915. Result := (psmRunTime in FMask);
  916. end;
  917. procedure TPaintStage.SetDesignTime(const Value: boolean);
  918. begin
  919. if (Value) then
  920. Include(FMask, psmDesignTime)
  921. else
  922. Exclude(FMask, psmDesignTime);
  923. FPaintStages.Dirty := True;
  924. end;
  925. procedure TPaintStage.SetMask(const Value: TPaintStageMask);
  926. begin
  927. FMask := Value;
  928. FPaintStages.Dirty := True;
  929. end;
  930. procedure TPaintStage.SetRunTime(const Value: boolean);
  931. begin
  932. if (Value) then
  933. Include(FMask, psmRunTime)
  934. else
  935. Exclude(FMask, psmRunTime);
  936. end;
  937. procedure TPaintStage.SetStage(const Value: Cardinal);
  938. begin
  939. FStage := Value;
  940. FPaintStages.Dirty := True;
  941. end;
  942. //------------------------------------------------------------------------------
  943. //
  944. // TPaintStages
  945. //
  946. //------------------------------------------------------------------------------
  947. constructor TPaintStages.Create;
  948. begin
  949. inherited Create;
  950. FDirty := True;
  951. end;
  952. destructor TPaintStages.Destroy;
  953. begin
  954. Clear;
  955. inherited;
  956. end;
  957. function TPaintStages.Add: PPaintStage;
  958. var
  959. L: Integer;
  960. begin
  961. L := Length(FItems);
  962. SetLength(FItems, L + 1);
  963. Result := @FItems[L];
  964. with Result^ do
  965. begin
  966. FPaintStages := Self;
  967. FMask := [psmRunTime, psmExport];
  968. FStage := 0;
  969. FParameter := 0;
  970. end;
  971. FDirty := True;
  972. end;
  973. procedure TPaintStages.Clear;
  974. begin
  975. FItems := nil;
  976. FDirty := True;
  977. end;
  978. function TPaintStages.Count: Integer;
  979. begin
  980. Result := Length(FItems);
  981. end;
  982. procedure TPaintStages.Delete(Index: Integer);
  983. var
  984. LCount: Integer;
  985. begin
  986. if (Index < 0) or (Index > High(FItems)) then
  987. raise EListError.Create('Invalid stage index');
  988. LCount := Length(FItems) - Index - 1;
  989. if LCount > 0 then
  990. Move(FItems[Index + 1], FItems[Index], LCount * SizeOf(TPaintStage));
  991. SetLength(FItems, High(FItems));
  992. FDirty := True;
  993. end;
  994. function TPaintStages.GetItem(Index: Integer): PPaintStage;
  995. begin
  996. Result := @FItems[Index];
  997. end;
  998. function TPaintStages.Insert(Index: Integer): PPaintStage;
  999. var
  1000. LCount: Integer;
  1001. begin
  1002. if Index < 0 then
  1003. Index := 0
  1004. else
  1005. if Index > Length(FItems) then
  1006. Index := Length(FItems);
  1007. LCount := Length(FItems) - Index;
  1008. SetLength(FItems, Length(FItems) + 1);
  1009. if LCount > 0 then
  1010. Move(FItems[Index], FItems[Index + 1], LCount * SizeOf(TPaintStage));
  1011. Result := @FItems[Index];
  1012. with Result^ do
  1013. begin
  1014. FPaintStages := Self;
  1015. FMask := [psmRunTime, psmExport];
  1016. FStage := 0;
  1017. FParameter := 0;
  1018. end;
  1019. FDirty := True;
  1020. end;
  1021. //------------------------------------------------------------------------------
  1022. //
  1023. // TCustomPaintBox32
  1024. //
  1025. //------------------------------------------------------------------------------
  1026. constructor TCustomPaintBox32.Create(AOwner: TComponent);
  1027. begin
  1028. inherited;
  1029. ControlStyle := ControlStyle - [csAcceptsControls];
  1030. CreateBuffer;
  1031. FBufferOversize := 40;
  1032. FForceFullRepaint := True;
  1033. FInvalidRects := TRectList.Create;
  1034. FUpdateRects := TRectList.Create;
  1035. FRepaintOptimizer := CreateRepaintOptimizer(Buffer, FInvalidRects);
  1036. // RepaintModeChanged hooks up the bitmap event handlers according to the
  1037. // repaint mode.
  1038. RepaintModeChanged;
  1039. { Setting a initial size here will cause the control to crash under LCL }
  1040. {$IFNDEF FPC}
  1041. SetBounds(0, 0, 192, 192);
  1042. {$ENDIF}
  1043. end;
  1044. destructor TCustomPaintBox32.Destroy;
  1045. begin
  1046. FUpdateCount := -1;
  1047. FreeAndNil(FRepaintOptimizer);
  1048. FreeAndNil(FInvalidRects);
  1049. FreeAndNil(FUpdateRects);
  1050. FreeAndNil(FBuffer);
  1051. inherited;
  1052. end;
  1053. //------------------------------------------------------------------------------
  1054. procedure TCustomPaintBox32.Loaded;
  1055. begin
  1056. ResizeBuffer;
  1057. FBufferValid := False;
  1058. inherited;
  1059. end;
  1060. //------------------------------------------------------------------------------
  1061. procedure TCustomPaintBox32.CreateBuffer;
  1062. begin
  1063. FBuffer := TBitmap32.Create;
  1064. end;
  1065. //------------------------------------------------------------------------------
  1066. function TCustomPaintBox32.CreateRepaintOptimizer(ABuffer: TBitmap32;
  1067. AInvalidRects: TRectList): TCustomRepaintOptimizer;
  1068. begin
  1069. Result := DefaultRepaintOptimizerClass.Create(ABuffer, AInvalidRects);
  1070. end;
  1071. //------------------------------------------------------------------------------
  1072. procedure TCustomPaintBox32.BeginUpdate;
  1073. begin
  1074. // Defer OnChange notifications
  1075. Inc(FUpdateCount);
  1076. {$ifdef TRACE_BEGINENDUPDATE}
  1077. OutputDebugString(PChar(Format('%s:%s.BeginUpdate: %d', [Name, ClassName, FUpdateCount])));
  1078. {$endif TRACE_BEGINENDUPDATE}
  1079. end;
  1080. procedure TCustomPaintBox32.EndUpdate;
  1081. begin
  1082. {$ifdef TRACE_BEGINENDUPDATE}
  1083. OutputDebugString(PChar(Format('%s:%s.EndUpdate: %d', [Name, ClassName, FUpdateCount])));
  1084. {$endif TRACE_BEGINENDUPDATE}
  1085. Assert(FUpdateCount > 0, 'Unpaired EndUpdate call');
  1086. // Re-enable OnChange generation
  1087. if (FUpdateCount = 1) then
  1088. begin
  1089. if (FModified) then
  1090. begin
  1091. DoChanged;
  1092. FModified := False;
  1093. end;
  1094. end;
  1095. Dec(FUpdateCount);
  1096. end;
  1097. procedure TCustomPaintBox32.Changed;
  1098. begin
  1099. if (FLockUpdateCount > 0) then
  1100. exit;
  1101. BeginUpdate;
  1102. FModified := True;
  1103. EndUpdate;
  1104. end;
  1105. procedure TCustomPaintBox32.DoChanged;
  1106. begin
  1107. if Assigned(FOnChange) then
  1108. FOnChange(Self);
  1109. // If partial repaints hasn't been queued then we need to do a full repaint
  1110. if (not FPartialRepaintQueued) then
  1111. Invalidate;
  1112. // For RepaintMode=rmDirect any change leads to an immediate repaint
  1113. if (RepaintMode = rmDirect) and not(csCustomPaint in ControlState) then
  1114. Update;
  1115. end;
  1116. //------------------------------------------------------------------------------
  1117. procedure TCustomPaintBox32.BeginLockUpdate;
  1118. begin
  1119. Inc(FLockUpdateCount);
  1120. end;
  1121. procedure TCustomPaintBox32.EndLockUpdate;
  1122. begin
  1123. Assert(FLockUpdateCount > 0, 'Unpaired UnlockUpdate call');
  1124. Dec(FLockUpdateCount);
  1125. end;
  1126. //------------------------------------------------------------------------------
  1127. procedure TCustomPaintBox32.AreaUpdated(const AArea: TRect; const AInfo: Cardinal);
  1128. var
  1129. UpdateRectSupport: IUpdateRectSupport;
  1130. R: TRect;
  1131. Width: integer;
  1132. begin
  1133. if (Supports(FBuffer.Backend, IUpdateRectSupport, UpdateRectSupport)) then
  1134. begin
  1135. R := AArea;
  1136. if (AInfo and AREAINFO_LINE <> 0) then
  1137. begin
  1138. Width := Max((AInfo and (not AREAINFO_MASK)) - 1, 1);
  1139. InflateArea(R, Width, Width);
  1140. end;
  1141. UpdateRectSupport.InvalidateRect(Self, R);
  1142. SetPartialRepaintQueued;
  1143. end else
  1144. inherited Invalidate;
  1145. end;
  1146. //------------------------------------------------------------------------------
  1147. procedure TCustomPaintBox32.AssignTo(Dest: TPersistent);
  1148. begin
  1149. inherited AssignTo(Dest);
  1150. if Dest is TCustomPaintBox32 then
  1151. begin
  1152. FBuffer.Assign(TCustomPaintBox32(Dest).FBuffer);
  1153. TCustomPaintBox32(Dest).FBufferOversize := FBufferOversize;
  1154. TCustomPaintBox32(Dest).FBufferValid := FBufferValid;
  1155. TCustomPaintBox32(Dest).FRepaintMode := FRepaintMode;
  1156. TCustomPaintBox32(Dest).FInvalidRects.Assign(FInvalidRects);
  1157. TCustomPaintBox32(Dest).FForceFullRepaint := FForceFullRepaint;
  1158. TCustomPaintBox32(Dest).FOptions := FOptions;
  1159. TCustomPaintBox32(Dest).FOnGDIOverlay := FOnGDIOverlay;
  1160. TCustomPaintBox32(Dest).FOnMouseEnter := FOnMouseEnter;
  1161. TCustomPaintBox32(Dest).FOnMouseLeave := FOnMouseLeave;
  1162. end;
  1163. end;
  1164. //------------------------------------------------------------------------------
  1165. procedure TCustomPaintBox32.CMMouseEnter(var Message: {$IFDEF FPC}TLMessage{$ELSE}TMessage{$ENDIF});
  1166. begin
  1167. inherited;
  1168. MouseEnter;
  1169. end;
  1170. procedure TCustomPaintBox32.CMMouseLeave(var Message: {$IFDEF FPC}TLMessage{$ELSE}TMessage{$ENDIF});
  1171. begin
  1172. MouseLeave;
  1173. inherited;
  1174. end;
  1175. procedure TCustomPaintBox32.MouseDown(Button: TMouseButton; Shift: TShiftState;
  1176. X, Y: Integer);
  1177. begin
  1178. if (pboAutoFocus in Options) and CanFocus then
  1179. SetFocus;
  1180. inherited;
  1181. end;
  1182. procedure TCustomPaintBox32.MouseEnter;
  1183. begin
  1184. FMouseInControl := True;
  1185. if Assigned(FOnMouseEnter) then
  1186. FOnMouseEnter(Self);
  1187. end;
  1188. procedure TCustomPaintBox32.MouseLeave;
  1189. begin
  1190. FMouseInControl := False;
  1191. if Assigned(FOnMouseLeave) then
  1192. FOnMouseLeave(Self);
  1193. end;
  1194. //------------------------------------------------------------------------------
  1195. procedure TCustomPaintBox32.DoBufferResized(const OldWidth, OldHeight: Integer);
  1196. begin
  1197. if FRepaintOptimizer.Enabled then
  1198. FRepaintOptimizer.BufferResizedHandler(FBuffer.Width, FBuffer.Height);
  1199. end;
  1200. function TCustomPaintBox32.CustomRepaint: Boolean;
  1201. begin
  1202. Result := FRepaintOptimizer.Enabled and not FForceFullRepaint and
  1203. FRepaintOptimizer.UpdatesAvailable;
  1204. end;
  1205. procedure TCustomPaintBox32.DoPrepareInvalidRects;
  1206. begin
  1207. if FRepaintOptimizer.Enabled and not FForceFullRepaint then
  1208. FRepaintOptimizer.PerformOptimization;
  1209. end;
  1210. function TCustomPaintBox32.InvalidRectsAvailable: Boolean;
  1211. begin
  1212. Result := True;
  1213. end;
  1214. //------------------------------------------------------------------------------
  1215. procedure TCustomPaintBox32.DoPaintBuffer;
  1216. begin
  1217. // force full repaint, this is necessary when Buffer is invalid and was never painted
  1218. // This will omit calculating the invalid rects, thus we paint everything.
  1219. if FForceFullRepaint then
  1220. begin
  1221. FForceFullRepaint := False;
  1222. FInvalidRects.Clear;
  1223. end else
  1224. DoPrepareInvalidRects;
  1225. // descendants should override this method for painting operations,
  1226. // not the Paint method!!!
  1227. FBufferValid := True;
  1228. end;
  1229. procedure TCustomPaintBox32.DoPaintGDIOverlay;
  1230. begin
  1231. if Assigned(FOnGDIOverlay) then
  1232. FOnGDIOverlay(Self);
  1233. end;
  1234. //------------------------------------------------------------------------------
  1235. procedure TCustomPaintBox32.Flush;
  1236. var
  1237. ViewportRect: TRect;
  1238. begin
  1239. if (FBuffer.Handle <> 0) then
  1240. begin
  1241. Canvas.Lock;
  1242. try
  1243. FBuffer.Lock;
  1244. try
  1245. if (Canvas.Handle <> 0) then
  1246. begin
  1247. ViewportRect := GetViewportRect;
  1248. BitBlt(Canvas.Handle, ViewportRect.Left, ViewportRect.Top, ViewportRect.Width, ViewportRect.Height,
  1249. FBuffer.Handle, 0, 0, SRCCOPY);
  1250. end;
  1251. finally
  1252. FBuffer.Unlock;
  1253. end;
  1254. finally
  1255. Canvas.Unlock;
  1256. end;
  1257. end;
  1258. end;
  1259. procedure TCustomPaintBox32.Flush(const SrcRect: TRect);
  1260. var
  1261. ViewportRect: TRect;
  1262. begin
  1263. if (FBuffer.Handle <> 0) then
  1264. begin
  1265. Canvas.Lock;
  1266. try
  1267. FBuffer.Lock;
  1268. try
  1269. ViewportRect := GetViewPortRect;
  1270. if (Canvas.Handle <> 0) then
  1271. BitBlt(Canvas.Handle, SrcRect.Left + ViewportRect.Left, SrcRect.Top + ViewportRect.Top, SrcRect.Width, SrcRect.Height,
  1272. FBuffer.Handle, SrcRect.Left, SrcRect.Top, SRCCOPY);
  1273. finally
  1274. FBuffer.Unlock;
  1275. end;
  1276. finally
  1277. Canvas.Unlock;
  1278. end;
  1279. end;
  1280. end;
  1281. function TCustomPaintBox32.GetViewportRect: TRect;
  1282. begin
  1283. // returns position of the buffered area within the control bounds
  1284. // by default, the whole control is buffered
  1285. if (HandleAllocated) then
  1286. Result := ClientRect
  1287. else
  1288. Result := BoundsRect;
  1289. end;
  1290. procedure TCustomPaintBox32.Invalidate;
  1291. begin
  1292. FBufferValid := False;
  1293. {$if defined(FPC) and defined(MSWINDOWS)}
  1294. // LCL TWinControl.Invalidate doesn't take csOpaque in account when calling InvalidateRect.
  1295. if (HandleAllocated) then
  1296. InvalidateRect(Handle, nil, not(csOpaque in ControlStyle));
  1297. {$else}
  1298. inherited;
  1299. {$ifend}
  1300. end;
  1301. procedure TCustomPaintBox32.ForceFullInvalidate;
  1302. begin
  1303. if FRepaintOptimizer.Enabled then
  1304. FRepaintOptimizer.Reset;
  1305. FForceFullRepaint := True;
  1306. Invalidate;
  1307. end;
  1308. procedure TCustomPaintBox32.Paint;
  1309. var
  1310. PaintSupport: IPaintSupport;
  1311. i: integer;
  1312. r: TRect;
  1313. {$ifdef UPDATERECT_SLOWMOTION}
  1314. const
  1315. {$ifdef UPDATERECT_SUPERSLOWMOTION}
  1316. SlowMotionDelay = 100;
  1317. {$else}
  1318. SlowMotionDelay = 10;
  1319. {$endif}
  1320. {$endif}
  1321. {$ifdef UPDATERECT_DEBUGDRAW}
  1322. const
  1323. clDebugDrawFill = TColor32($00FF1010);
  1324. clDebugDrawFrame = TColor32($00AF0A0A);
  1325. var
  1326. C1, C2: TColor32;
  1327. {$endif}
  1328. {$ifdef PAINT_UNCLIPPED}
  1329. var
  1330. Canvas: TControlCanvas;
  1331. {$endif}
  1332. begin
  1333. if (Parent = nil) then
  1334. Exit;
  1335. if FRepaintOptimizer.Enabled then
  1336. FRepaintOptimizer.BeginPaint;
  1337. PaintSupport := FBuffer.Backend as IPaintSupport;
  1338. if not FBufferValid then
  1339. begin
  1340. PaintSupport.ImageNeeded;
  1341. DoPaintBuffer;
  1342. PaintSupport.CheckPixmap;
  1343. end;
  1344. // Create a new canvas so we can paint outside the WM_PAINT/BeginPaint/EndPaint update regions
  1345. // This will also reveal if the DoPaint below paints more than it should.
  1346. {$ifdef PAINT_UNCLIPPED}
  1347. Canvas := TControlCanvas.Create;
  1348. Canvas.Control := Self;
  1349. {$endif}
  1350. {$ifdef UPDATERECT_DEBUGDRAW}
  1351. {$ifdef UPDATERECT_DEBUGDRAW_RANDOM_COLORS}
  1352. C1 := Random($7F) or (Random($7F) shl 8) or (Random($7F) shl 16);
  1353. C2 := (C1 shl 1);
  1354. {$else}
  1355. C1 := clDebugDrawFill;
  1356. C2 := clDebugDrawFrame;
  1357. {$endif}
  1358. Canvas.Brush.Color := C1;
  1359. Canvas.Brush.Style := bsSolid;
  1360. if (FUpdateRects.Count > 0) then
  1361. begin
  1362. for i := 0 to FUpdateRects.Count-1 do
  1363. begin
  1364. r := FUpdateRects[i]^;
  1365. Canvas.FillRect(r);
  1366. end;
  1367. end else
  1368. Canvas.FillRect(Canvas.ClipRect);
  1369. {$ifdef UPDATERECT_SLOWMOTION}
  1370. Sleep(SlowMotionDelay);
  1371. {$endif}
  1372. {$endif}
  1373. FBuffer.Lock;
  1374. try
  1375. if (FUpdateRects.Count > 0) then
  1376. begin
  1377. // Clip update rects.
  1378. // Mainly so we don't paint over the ScrollBars/SizeGrip but also
  1379. // for possibly slightly better performance.
  1380. for i := 0 to FUpdateRects.Count-1 do
  1381. GR32.IntersectRect(FUpdateRects[i]^, FUpdateRects[i]^, FBuffer.ClipRect);
  1382. PaintSupport.DoPaint(FBuffer, FUpdateRects, Canvas)
  1383. end else
  1384. begin
  1385. GR32.IntersectRect(r, GetViewportRect, FBuffer.ClipRect);
  1386. PaintSupport.DoPaint(FBuffer, r, Canvas);
  1387. end;
  1388. finally
  1389. FBuffer.Unlock;
  1390. end;
  1391. {$ifdef UPDATERECT_SLOWMOTION}
  1392. Sleep(SlowMotionDelay);
  1393. {$endif}
  1394. {$ifdef UPDATERECT_DEBUGDRAW}
  1395. Canvas.Brush.Color := C2;
  1396. Canvas.Brush.Style := bsSolid;
  1397. if (FUpdateRects.Count > 0) then
  1398. begin
  1399. for i := 0 to FUpdateRects.Count-1 do
  1400. begin
  1401. r := FUpdateRects[i]^;
  1402. GR32.InflateRect(r, 1, 1);
  1403. Canvas.FrameRect(r);
  1404. end;
  1405. end else
  1406. Canvas.FrameRect(Canvas.ClipRect);
  1407. {$ifdef UPDATERECT_SLOWMOTION}
  1408. Sleep(SlowMotionDelay);
  1409. {$endif}
  1410. {$endif}
  1411. {$ifdef PAINT_UNCLIPPED}
  1412. Canvas.Free;
  1413. {$endif}
  1414. DoPaintGDIOverlay;
  1415. if FRepaintOptimizer.Enabled then
  1416. FRepaintOptimizer.EndPaint;
  1417. ResetInvalidRects;
  1418. FForceFullRepaint := False;
  1419. FPartialRepaintQueued := False;
  1420. end;
  1421. procedure TCustomPaintBox32.ResetInvalidRects;
  1422. begin
  1423. FInvalidRects.Clear;
  1424. end;
  1425. procedure TCustomPaintBox32.ResizeBuffer;
  1426. var
  1427. NewWidth, NewHeight, W, H: Integer;
  1428. OldWidth, OldHeight: Integer;
  1429. begin
  1430. // get the viewport parameters
  1431. with GetViewportRect do
  1432. begin
  1433. NewWidth := Right - Left;
  1434. NewHeight := Bottom - Top;
  1435. end;
  1436. if NewWidth < 0 then
  1437. NewWidth := 0;
  1438. if NewHeight < 0 then
  1439. NewHeight := 0;
  1440. W := FBuffer.Width;
  1441. if NewWidth > W then
  1442. W := NewWidth + FBufferOversize
  1443. else
  1444. if NewWidth < W - FBufferOversize then
  1445. W := NewWidth;
  1446. if W < 1 then
  1447. W := 1;
  1448. H := FBuffer.Height;
  1449. if NewHeight > H then
  1450. H := NewHeight + FBufferOversize
  1451. else
  1452. if NewHeight < H - FBufferOversize then
  1453. H := NewHeight;
  1454. if H < 1 then
  1455. H := 1;
  1456. if (W <> FBuffer.Width) or (H <> FBuffer.Height) then
  1457. begin
  1458. FBuffer.Lock;
  1459. OldWidth := Buffer.Width;
  1460. OldHeight := Buffer.Height;
  1461. FBuffer.SetSize(W, H);
  1462. FBuffer.Unlock;
  1463. DoBufferResized(OldWidth, OldHeight);
  1464. ForceFullInvalidate;
  1465. end;
  1466. end;
  1467. procedure TCustomPaintBox32.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
  1468. begin
  1469. inherited;
  1470. if (not (csLoading in ComponentState)) then
  1471. ResizeBuffer;
  1472. FBufferValid := False;
  1473. end;
  1474. procedure TCustomPaintBox32.SetBufferOversize(Value: Integer);
  1475. begin
  1476. if (Value < 0) then
  1477. Value := 0;
  1478. if (Value <> FBufferOversize) then
  1479. begin
  1480. FBufferOversize := Value;
  1481. ResizeBuffer;
  1482. FBufferValid := False
  1483. end;
  1484. end;
  1485. procedure TCustomPaintBox32.SetPartialRepaintQueued;
  1486. begin
  1487. FPartialRepaintQueued := True;
  1488. end;
  1489. procedure TCustomPaintBox32.WMEraseBkgnd(var Message: {$IFDEF FPC}TLmEraseBkgnd{$ELSE}TWmEraseBkgnd{$ENDIF});
  1490. begin
  1491. Message.Result := 1;
  1492. end;
  1493. procedure TCustomPaintBox32.WMGetDlgCode(var Msg: {$IFDEF FPC}TLMessage{$ELSE}TWmGetDlgCode{$ENDIF});
  1494. begin
  1495. if (pboWantArrowKeys in Options) then
  1496. Msg.Result:= Msg.Result or DLGC_WANTARROWS
  1497. else
  1498. Msg.Result:= Msg.Result and not DLGC_WANTARROWS;
  1499. end;
  1500. procedure TCustomPaintBox32.WMPaint(var Message: {$IFDEF FPC}TLMPaint{$ELSE}TWMPaint{$ENDIF});
  1501. var
  1502. FullRepaint: boolean;
  1503. UpdateRectSupport: IUpdateRectSupport;
  1504. i: integer;
  1505. Tiles: TMicroTiles;
  1506. begin
  1507. // Update the InvalidRects
  1508. if CustomRepaint then
  1509. DoPrepareInvalidRects;
  1510. // Get a list of update rects
  1511. if (Supports(FBuffer.Backend, IUpdateRectSupport, UpdateRectSupport)) then
  1512. begin
  1513. FullRepaint := False;
  1514. UpdateRectSupport.GetUpdateRects(Self, FUpdateRects, FInvalidRects.Count, FullRepaint);
  1515. end else
  1516. FullRepaint := True;
  1517. if (not FullRepaint) then
  1518. begin
  1519. // Merge FInvalidRects into FUpdateRects
  1520. for i := 0 to FInvalidRects.Count-1 do
  1521. FUpdateRects.Add(FInvalidRects[i]^);
  1522. // Consolidate potentially overlapping areas into as few separate
  1523. // non-overlapping areas as possible.
  1524. {$ifdef CONSOLIDATE_UPDATERECTS} // See issue # 202
  1525. MicroTilesCreate(Tiles);
  1526. MicroTilesSetSize(Tiles, ClientRect);
  1527. for i := 0 to FUpdateRects.Count-1 do
  1528. MicroTilesAddRect(Tiles, FUpdateRects[i]^, True);
  1529. FUpdateRects.Count := 0;
  1530. MicroTilesCalcRects(Tiles, FUpdateRects, False, True);
  1531. MicroTilesDestroy(Tiles);
  1532. {$endif CONSOLIDATE_UPDATERECTS}
  1533. end;
  1534. FullRepaint := FullRepaint or ((FUpdateRects.Count = 1) and (GR32.EqualRect(FUpdateRects[0]^, ClientRect)));
  1535. if (FullRepaint) then
  1536. FUpdateRects.Count := 0;
  1537. {$IFDEF FPC}
  1538. { On FPC we need to specify the name of the ancestor here }
  1539. inherited WMPaint(Message);
  1540. {$ELSE}
  1541. inherited;
  1542. {$ENDIF}
  1543. FUpdateRects.Count := 0;
  1544. end;
  1545. procedure TCustomPaintBox32.FullUpdateHandler(Sender: TObject);
  1546. begin
  1547. FRepaintOptimizer.Reset;
  1548. // Request that everything be repainted
  1549. inherited Invalidate;
  1550. end;
  1551. procedure TCustomPaintBox32.AreaUpdateHandler(Sender: TObject; const Area: TRect; const Info: Cardinal);
  1552. var
  1553. UpdateRectNotification: IUpdateRectNotification;
  1554. begin
  1555. Assert(Sender = FBuffer);
  1556. if (Area.Left = Area.Right) and (Area.Top = Area.Bottom) then // Don't use IsEmpty; Rect can be negative
  1557. Exit; // Empty area
  1558. // Add the area to the repaint optimizer
  1559. if (FRepaintOptimizer <> nil) and (FRepaintOptimizer.Enabled) and (Supports(FRepaintOptimizer, IUpdateRectNotification, UpdateRectNotification)) then
  1560. UpdateRectNotification.AreaUpdated(Area, Info);
  1561. // Request that the area be repainted...
  1562. AreaUpdated(Area, Info);
  1563. // ...and possibly process pending updates
  1564. Changed;
  1565. end;
  1566. procedure TCustomPaintBox32.RepaintModeChanged;
  1567. begin
  1568. // Setup event handler on change of area
  1569. if (FBuffer <> nil) then
  1570. begin
  1571. case FRepaintMode of
  1572. rmOptimizer:
  1573. begin
  1574. FBuffer.OnAreaChanged := AreaUpdateHandler;
  1575. FBuffer.OnChange := nil;
  1576. end;
  1577. rmDirect:
  1578. begin
  1579. FBuffer.OnAreaChanged := AreaUpdateHandler;
  1580. FBuffer.OnChange := nil;
  1581. end;
  1582. rmFull:
  1583. begin
  1584. FBuffer.OnAreaChanged := nil;
  1585. FBuffer.OnChange := FullUpdateHandler;
  1586. end
  1587. end;
  1588. end;
  1589. end;
  1590. procedure TCustomPaintBox32.SetRepaintMode(const Value: TRepaintMode);
  1591. begin
  1592. if (Value <> FRepaintMode) then
  1593. begin
  1594. FRepaintMode := Value;
  1595. if (FRepaintOptimizer <> nil) then
  1596. FRepaintOptimizer.Enabled := (FRepaintMode = rmOptimizer);
  1597. // Update buffer event handlers according to repaint mode
  1598. RepaintModeChanged;
  1599. Invalidate;
  1600. end;
  1601. end;
  1602. //------------------------------------------------------------------------------
  1603. //
  1604. // TPaintBox32
  1605. //
  1606. //------------------------------------------------------------------------------
  1607. procedure TPaintBox32.DoPaintBuffer;
  1608. var
  1609. BackgroundColor: TColor;
  1610. begin
  1611. if (csDesigning in ComponentState) then
  1612. begin
  1613. // Nothing to paint in design-mode
  1614. BackgroundColor := Color;
  1615. {$ifdef FPC}
  1616. if (BackgroundColor = clDefault) then
  1617. BackgroundColor := GetDefaultColor(dctBrush);
  1618. {$endif}
  1619. Buffer.Clear(Color32(BackgroundColor));
  1620. end;
  1621. if Assigned(FOnPaintBuffer) then
  1622. FOnPaintBuffer(Self);
  1623. inherited;
  1624. end;
  1625. //------------------------------------------------------------------------------
  1626. //
  1627. // TBackgroundOptions
  1628. //
  1629. //------------------------------------------------------------------------------
  1630. procedure TBackgroundOptions.ChangeHandler(Sender: TObject);
  1631. begin
  1632. CheckFillStyle;
  1633. Changed;
  1634. end;
  1635. constructor TBackgroundOptions.Create;
  1636. begin
  1637. inherited Create;
  1638. FPatternBitmap := TBitmap32.Create(TMemoryBackend);
  1639. FPatternBitmap.DrawMode := dmOpaque;
  1640. FPatternBitmap.OnChange := ChangeHandler;
  1641. FDropShadowBitmap := TBitmap32.Create(TMemoryBackend);
  1642. FDropShadowBitmap.DrawMode := dmBlend;
  1643. FDropShadowBitmap.OnChange := ChangeHandler;
  1644. FOuterBorderColor := clNone;
  1645. FInnerBorderColor := clNone;
  1646. SetCheckersStyle(bcsNone); // We need to go via the property setter
  1647. FCheckersExponent := 3;
  1648. end;
  1649. destructor TBackgroundOptions.Destroy;
  1650. begin
  1651. FPatternBitmap.Free;
  1652. FDropShadowBitmap.Free;
  1653. inherited;
  1654. end;
  1655. procedure TBackgroundOptions.CheckFillStyle;
  1656. begin
  1657. case FFillStyle of
  1658. bfsColor:
  1659. if (not FPatternBitmap.Empty) then
  1660. FFillStyle := bfsPattern;
  1661. bfsCheckers:
  1662. if (not FPatternBitmap.Empty) then
  1663. FFillStyle := bfsPattern
  1664. else
  1665. if (FCheckersStyle = bcsNone) then
  1666. FFillStyle := bfsColor
  1667. else
  1668. if (FInnerBorderColor <> clNone) and (FInnerBorderWidth <> 0) then
  1669. FFillStyle := bfsColor
  1670. else
  1671. if (FOuterBorderColor <> clNone) then
  1672. FFillStyle := bfsColor
  1673. else
  1674. if (not FDropShadowBitmap.Empty) or (FDropShadowSize <> 0) then
  1675. FFillStyle := bfsColor;
  1676. bfsPattern:
  1677. if (FPatternBitmap.Empty) then
  1678. FFillStyle := bfsColor;
  1679. end;
  1680. end;
  1681. function TBackgroundOptions.GetCheckersColor(const Index: Integer): TColor;
  1682. begin
  1683. Result := WinColor(FCheckersColors[Index]);
  1684. end;
  1685. function TBackgroundOptions.IsCheckersColorsStored(Index: integer): boolean;
  1686. begin
  1687. Result := (FCheckersStyle = bcsCustom);
  1688. end;
  1689. function TBackgroundOptions.IsDropShadowBitmapStored: boolean;
  1690. begin
  1691. Result := (not FDropShadowBitmap.Empty);
  1692. end;
  1693. function TBackgroundOptions.IsFillStyleStored: Boolean;
  1694. begin
  1695. case FFillStyle of
  1696. bfsColor:
  1697. Result := (FCheckersStyle <> bcsNone) and
  1698. ((FInnerBorderColor = clNone) or (FInnerBorderWidth = 0)) and
  1699. (FOuterBorderColor = clNone) and
  1700. (FDropShadowBitmap.Empty) and (FDropShadowSize = 0);
  1701. bfsCheckers:
  1702. Result := True;
  1703. bfsPattern:
  1704. Result := False;
  1705. else
  1706. Result := True;
  1707. end;
  1708. end;
  1709. function TBackgroundOptions.IsPatternBitmapStored: boolean;
  1710. begin
  1711. Result := (not FPatternBitmap.Empty);
  1712. end;
  1713. procedure TBackgroundOptions.SetPatternBitmap(const Value: TBitmap32);
  1714. begin
  1715. FPatternBitmap.Assign(Value);
  1716. end;
  1717. procedure TBackgroundOptions.SetCheckersColor(Index: integer; const Value: TColor);
  1718. begin
  1719. if (FCheckersStyle <> bcsCustom) or (Color32(Value) <> FCheckersColors[Index]) then
  1720. begin
  1721. FCheckersColors[Index] := Color32(Value);
  1722. FCheckersStyle := bcsCustom;
  1723. Changed;
  1724. end;
  1725. end;
  1726. procedure TBackgroundOptions.SetCheckersExponent(const Value: integer);
  1727. begin
  1728. if (Value <> FCheckersExponent) then
  1729. begin;
  1730. // There's no technical reason to limit the size, but there's also no
  1731. // practical reason to allow larger values.
  1732. FCheckersExponent := Min(10, Max(0, Value));
  1733. Changed;
  1734. end;
  1735. end;
  1736. procedure TBackgroundOptions.SetCheckersStyle(const Value: TBackgroundCheckerStyle);
  1737. begin
  1738. if (FCheckersStyle <> Value) then
  1739. begin
  1740. FCheckersStyle := Value;
  1741. if (FCheckersStyle <> bcsCustom) then
  1742. FCheckersColors := DefaultCheckersColors[FCheckersStyle];
  1743. CheckFillStyle;
  1744. Changed;
  1745. end;
  1746. end;
  1747. procedure TBackgroundOptions.SetDropShadowBitmap(const Value: TBitmap32);
  1748. begin
  1749. FDropShadowBitmap.Assign(Value);
  1750. end;
  1751. procedure TBackgroundOptions.SetDropShadowColor(const Value: TColor32);
  1752. begin
  1753. if (Value <> FDropShadowColor) then
  1754. begin
  1755. FDropShadowColor := Value;
  1756. CheckFillStyle;
  1757. Changed;
  1758. end;
  1759. end;
  1760. procedure TBackgroundOptions.SetDropShadowOffset(const Value: integer);
  1761. begin
  1762. if (Value <> FDropShadowOffset) then
  1763. begin
  1764. FDropShadowOffset := Max(0, Value);
  1765. Changed;
  1766. end;
  1767. end;
  1768. procedure TBackgroundOptions.SetDropShadowSize(const Value: integer);
  1769. begin
  1770. if (Value <> FDropShadowSize) then
  1771. begin
  1772. FDropShadowSize := Max(0, Value);
  1773. Changed;
  1774. end;
  1775. end;
  1776. procedure TBackgroundOptions.SetFillStyle(const Value: TBackgroundFillStyle);
  1777. begin
  1778. if (Value <> FFillStyle) then
  1779. begin
  1780. FFillStyle := Value;
  1781. CheckFillStyle;
  1782. Changed;
  1783. end;
  1784. end;
  1785. procedure TBackgroundOptions.SetInnerBorderColor(const Value: TColor);
  1786. begin
  1787. if (Value <> FInnerBorderColor) then
  1788. begin
  1789. FInnerBorderColor := Value;
  1790. CheckFillStyle;
  1791. Changed;
  1792. end;
  1793. end;
  1794. procedure TBackgroundOptions.SetInnerBorderWidth(const Value: integer);
  1795. begin
  1796. if (Value <> FInnerBorderWidth) then
  1797. begin
  1798. FInnerBorderWidth := Max(0, Value);
  1799. CheckFillStyle;
  1800. Changed;
  1801. end;
  1802. end;
  1803. procedure TBackgroundOptions.SetOuterBorderColor(const Value: TColor);
  1804. begin
  1805. if (Value <> FOuterBorderColor) then
  1806. begin
  1807. FOuterBorderColor := Value;
  1808. CheckFillStyle;
  1809. Changed;
  1810. end;
  1811. end;
  1812. //------------------------------------------------------------------------------
  1813. //
  1814. // TCustomImage32
  1815. //
  1816. //------------------------------------------------------------------------------
  1817. constructor TCustomImage32.Create(AOwner: TComponent);
  1818. begin
  1819. inherited;
  1820. ControlStyle := [csCaptureMouse, csClickEvents, csDoubleClicks, csReplicatable, csOpaque];
  1821. FLayers := CreateLayerCollection;
  1822. FLayers.Subscribe(Self);
  1823. RepaintOptimizer.RegisterLayerCollection(FLayers);
  1824. FPaintStages := TPaintStages.Create;
  1825. FScaleX := 1;
  1826. FScaleY := 1;
  1827. SetXForm(0, 0, 1, 1);
  1828. FBackgroundOptions := TBackgroundOptions.Create;
  1829. FBackgroundOptions.OnChange := BackgroundOptionsChangeHandler;
  1830. FMousePanOptions := TMousePanOptions.Create;
  1831. FMouseZoomOptions := TMouseZoomOptions.Create;
  1832. InitDefaultStages;
  1833. end;
  1834. destructor TCustomImage32.Destroy;
  1835. begin
  1836. BeginLockUpdate; // Block further notifications
  1837. Bitmap.OnResize := nil;
  1838. // Empty bitmap so we don't fail in UpdateCache when layers are
  1839. // hidden during destruction and calls back via LayerCollection to
  1840. // get the ClientRect.
  1841. Bitmap.Delete;
  1842. FreeAndNil(FPaintStages);
  1843. RepaintOptimizer.UnregisterLayerCollection(FLayers);
  1844. FLayers.Unsubscribe(Self);
  1845. FreeAndNil(FLayers);
  1846. FreeAndNil(FBitmap);
  1847. FreeAndNil(FBackgroundOptions);
  1848. FreeAndNil(FMousePanOptions);
  1849. FreeAndNil(FMouseZoomOptions);
  1850. inherited;
  1851. end;
  1852. procedure TCustomImage32.CreateBuffer;
  1853. begin
  1854. inherited;
  1855. FBitmap := TBitmap32.Create;
  1856. FBitmap.OnResize := BitmapResizeHandler;
  1857. end;
  1858. procedure TCustomImage32.RepaintModeChanged;
  1859. begin
  1860. // Beware! This is called from TCustomPaintBox32.Create
  1861. // Note: We don't really need to call inherited here since we don't want the
  1862. // paintbox buffer event handlers set. However, since we're supressing the
  1863. // buffer change events in derived classes with BeginUpdate/EndUpdate there's
  1864. // no harm in doing it.
  1865. inherited;
  1866. if (FBitmap <> nil) then
  1867. begin
  1868. case RepaintMode of
  1869. rmOptimizer:
  1870. begin
  1871. FBitmap.OnAreaChanged := BitmapAreaChangeHandler;
  1872. FBitmap.OnChange := nil;
  1873. end;
  1874. rmDirect:
  1875. begin
  1876. FBitmap.OnAreaChanged := BitmapAreaChangeHandler;
  1877. FBitmap.OnChange := nil;
  1878. end;
  1879. rmFull:
  1880. begin
  1881. FBitmap.OnAreaChanged := nil;
  1882. FBitmap.OnChange := BitmapChangeHandler;
  1883. end;
  1884. end;
  1885. end;
  1886. end;
  1887. function TCustomImage32.GetLayerCollectionClass: TLayerCollectionClass;
  1888. begin
  1889. Result := TLayerCollection;
  1890. end;
  1891. function TCustomImage32.CreateLayerCollection: TLayerCollection;
  1892. begin
  1893. Result := GetLayerCollectionClass.Create(Self);
  1894. TLayerCollectionAccess(Result).OnChange := LayerCollectionChangeHandler;
  1895. TLayerCollectionAccess(Result).OnGDIUpdate := LayerCollectionGDIUpdateHandler;
  1896. TLayerCollectionAccess(Result).OnGetViewportScale := LayerCollectionGetViewportScaleHandler;
  1897. TLayerCollectionAccess(Result).OnGetViewportShift := LayerCollectionGetViewportShiftHandler;
  1898. end;
  1899. procedure TCustomImage32.InvalidateArea(const AArea: TRect; const AInfo: Cardinal; AOptimize: boolean);
  1900. var
  1901. UpdateRectNotification: IUpdateRectNotification;
  1902. Tx, Ty, I, J: Integer;
  1903. BitmapRect: TRect;
  1904. R: TRect;
  1905. AreaUpdated: boolean;
  1906. begin
  1907. AreaUpdated := False;
  1908. if (AArea.Left <> AArea.Right) or (AArea.Top <> AArea.Bottom) then // Don't use IsEmpty; Rect can be negative
  1909. begin
  1910. if (not AOptimize) or (not RepaintOptimizer.Enabled) or (not Supports(RepaintOptimizer, IUpdateRectNotification, UpdateRectNotification)) then
  1911. UpdateRectNotification := nil;
  1912. if (FBitmapAlign <> baTile) then
  1913. begin
  1914. // ->Repaint optimizer
  1915. if (UpdateRectNotification <> nil) then
  1916. UpdateRectNotification.AreaUpdated(AArea, AInfo);
  1917. // ->Windows InvalidateRect
  1918. inherited AreaUpdated(AArea, AInfo);
  1919. // Note that even though we do a coarse InvalidateRect here, regardless of the shape of
  1920. // the area being invalidated, this does not spoil our repaint optimization. When
  1921. // processing WM_PAINT we will still only paint the repaint optimizer's fine-grained
  1922. // tiles.
  1923. // Note on the note: I'm not sure the above is correct anymore; Will have to verify.
  1924. AreaUpdated := True;
  1925. end else
  1926. begin
  1927. BitmapRect := CachedBitmapRect;
  1928. if (BitmapRect.Right <> 0) and (BitmapRect.Bottom <> 0) then
  1929. begin
  1930. Tx := Buffer.Width div BitmapRect.Right;
  1931. Ty := Buffer.Height div BitmapRect.Bottom;
  1932. for J := 0 to Ty do
  1933. for I := 0 to Tx do
  1934. begin
  1935. R := AArea;
  1936. GR32.OffsetRect(R, BitmapRect.Right * I, BitmapRect.Bottom * J);
  1937. if (UpdateRectNotification <> nil) then
  1938. UpdateRectNotification.AreaUpdated(R, AInfo);
  1939. inherited AreaUpdated(R, AInfo);
  1940. AreaUpdated := True;
  1941. end;
  1942. end;
  1943. end;
  1944. end;
  1945. if (not AreaUpdated) then
  1946. // Pretend that a partial repaint was just queued so the fact that
  1947. // we just skipped the partial invalidation above doesn't end up
  1948. // causing a full invalidate instead.
  1949. SetPartialRepaintQueued;
  1950. BufferValid := False;
  1951. end;
  1952. procedure TCustomImage32.AreaUpdated(const AArea: TRect; const AInfo: Cardinal);
  1953. begin
  1954. // We're called from TLayerCollection.DoUpdateArea which also calls AreaUpdated
  1955. // on the repaint optimizer so don't call that from here
  1956. InvalidateArea(AArea, AInfo, False);
  1957. end;
  1958. procedure TCustomImage32.DoBitmapResized;
  1959. begin
  1960. if Assigned(FOnBitmapResize) then
  1961. FOnBitmapResize(Self);
  1962. end;
  1963. procedure TCustomImage32.BitmapResized;
  1964. var
  1965. W, H: Integer;
  1966. begin
  1967. if AutoSize then
  1968. begin
  1969. W := Bitmap.Width;
  1970. H := Bitmap.Height;
  1971. if (ScaleMode = smScale) then
  1972. begin
  1973. W := Round(W * Scale);
  1974. H := Round(H * Scale);
  1975. end;
  1976. if AutoSize and (W > 0) and (H > 0) then
  1977. SetBounds(Left, Top, W, H);
  1978. end;
  1979. if (UpdateCount = 0) then
  1980. DoBitmapResized;
  1981. InvalidateCache;
  1982. ForceFullInvalidate;
  1983. end;
  1984. procedure TCustomImage32.BitmapChanged(const Area: TRect);
  1985. var
  1986. ViewportRect: TRect;
  1987. begin
  1988. // Translate the coordinates from bitmap to viewport
  1989. ViewportRect := BitmapToControl(Area);
  1990. InvalidateArea(ViewportRect, 0, True);
  1991. Changed;
  1992. end;
  1993. procedure TCustomImage32.BackgroundOptionsChangeHandler(Sender: TObject);
  1994. begin
  1995. Invalidate;
  1996. end;
  1997. procedure TCustomImage32.BitmapResizeHandler(Sender: TObject);
  1998. begin
  1999. BitmapResized;
  2000. end;
  2001. procedure TCustomImage32.BitmapChangeHandler(Sender: TObject);
  2002. begin
  2003. RepaintOptimizer.Reset;
  2004. BitmapChanged(Bitmap.BoundsRect);
  2005. end;
  2006. procedure TCustomImage32.BitmapAreaChangeHandler(Sender: TObject; const Area: TRect; const Info: Cardinal);
  2007. var
  2008. NewInfo: Cardinal;
  2009. T: TRect;
  2010. Width: Integer;
  2011. OffsetX, OffsetY: Integer;
  2012. WidthX, WidthY: Integer;
  2013. begin
  2014. Assert(Sender = FBitmap);
  2015. if (Area.Left = Area.Right) or (Area.Top = Area.Bottom) then
  2016. Exit; // Empty area
  2017. T := Area;
  2018. UpdateCache; // Ensure CachedScaleXY is up to date
  2019. NewInfo := Info;
  2020. if (NewInfo and AREAINFO_LINE <> 0) then
  2021. begin
  2022. // Unpack line width from Info param
  2023. Width := integer(NewInfo and (not AREAINFO_MASK));
  2024. // Add line and resampler width and scale value to viewport
  2025. Width := Max(1, Ceil((Width + FBitmap.Resampler.Width) * Max(CachedScaleX, CachedScaleY)));
  2026. // Pack width into Info param again
  2027. NewInfo := AREAINFO_LINE or Width;
  2028. end;
  2029. // Translate the coordinates from bitmap to viewport
  2030. T := BitmapToControl(T);
  2031. if (NewInfo and AREAINFO_LINE <> 0) then
  2032. begin
  2033. // Line coordinates specify the center of the pixel.
  2034. // For example the rect (0, 0, 0, 1) is a one pixel long line while (0, 0, 0, 0) is empty.
  2035. OffsetX := Round(CachedScaleX / 2);
  2036. OffsetY := Round(CachedScaleY / 2);
  2037. GR32.OffsetRect(T, OffsetX, OffsetY);
  2038. end else
  2039. begin
  2040. // Make sure rect is positive (i.e. dX >= 0)
  2041. T.NormalizeRect;
  2042. // Rect coordinates specify the pixel corners.
  2043. // It is assumed that (Top, Left) specify the top/left corner of the top/left pixel and
  2044. // that (Right, Bottom) specify the bottom/right corner of the bottom/right pixel.
  2045. // For example the rect (0, 0, 1, 1) covers just one pixel while (0, 0, 0, 1) is empty.
  2046. (* Disabled here as the majority of callers already take this into account and making
  2047. ** the adjustment here will make the update rectangle too small for those.
  2048. Dec(T.Right);
  2049. Dec(T.Bottom);
  2050. *)
  2051. WidthX := Max(1, Ceil(FBitmap.Resampler.Width * CachedScaleX));
  2052. WidthY := Max(1, Ceil(FBitmap.Resampler.Width * CachedScaleY));
  2053. InflateArea(T, WidthX, WidthY);
  2054. end;
  2055. InvalidateArea(T, NewInfo, True);
  2056. Changed;
  2057. end;
  2058. function TCustomImage32.CanAutoSize(var NewWidth, NewHeight: Integer): Boolean;
  2059. var
  2060. W, H: Integer;
  2061. begin
  2062. Result := True;
  2063. InvalidateCache;
  2064. W := Bitmap.Width;
  2065. H := Bitmap.Height;
  2066. if (ScaleMode = smScale) then
  2067. begin
  2068. W := Round(W * Scale);
  2069. H := Round(H * Scale);
  2070. end;
  2071. if not (csDesigning in ComponentState) or (W > 0) and (H > 0) then
  2072. begin
  2073. if Align in [alNone, alLeft, alRight] then
  2074. NewWidth := W;
  2075. if Align in [alNone, alTop, alBottom] then
  2076. NewHeight := H;
  2077. end;
  2078. end;
  2079. function TCustomImage32.CanMousePan: boolean;
  2080. begin
  2081. Result := (BitmapAlign = baCustom) and (FMousePanOptions.Enabled);
  2082. end;
  2083. function TCustomImage32.CanMouseZoom: boolean;
  2084. begin
  2085. Result := (ScaleMode in [smScale, smOptimalScaled]) and (FMouseZoomOptions.Enabled);
  2086. end;
  2087. function TCustomImage32.BitmapToControl(const ARect: TRect): TRect;
  2088. begin
  2089. // Convert coordinates from bitmap's ref. frame to control's ref. frame
  2090. UpdateCache;
  2091. Result.Left := Trunc(ARect.Left * CachedScaleX + CachedShiftX);
  2092. Result.Right := Trunc(ARect.Right * CachedScaleX + CachedShiftX);
  2093. Result.Top := Trunc(ARect.Top * CachedScaleY + CachedShiftY);
  2094. Result.Bottom := Trunc(ARect.Bottom * CachedScaleY + CachedShiftY);
  2095. end;
  2096. function TCustomImage32.BitmapToControl(const APoint: TPoint): TPoint;
  2097. begin
  2098. // Convert coordinates from bitmap's ref. frame to control's ref. frame
  2099. UpdateCache;
  2100. Result.X := Trunc(APoint.X * CachedScaleX + CachedShiftX);
  2101. Result.Y := Trunc(APoint.Y * CachedScaleY + CachedShiftY);
  2102. end;
  2103. function TCustomImage32.BitmapToControl(const APoint: TFloatPoint): TFloatPoint;
  2104. begin
  2105. // Sub-pixel precision version
  2106. UpdateCache;
  2107. Result.X := APoint.X * CachedScaleX + CachedShiftX;
  2108. Result.Y := APoint.Y * CachedScaleY + CachedShiftY;
  2109. end;
  2110. function TCustomImage32.ControlToBitmap(const ARect: TRect; Rounding: TRectRounding): TRect;
  2111. begin
  2112. // It is assumed that ARect.Top<=ARect.Bottom and ARect.Left<=ARect.Right
  2113. UpdateCache;
  2114. if (CachedRecScaleX = 0) then
  2115. begin
  2116. Result.Left := High(Result.Left);
  2117. Result.Right := High(Result.Right);
  2118. end else
  2119. begin
  2120. case Rounding of
  2121. rrClosest:
  2122. begin
  2123. Result.Left := Round((ARect.Left - CachedShiftX) * CachedRecScaleX);
  2124. Result.Right := Round((ARect.Right - CachedShiftX) * CachedRecScaleX);
  2125. end;
  2126. rrOutside:
  2127. begin
  2128. Result.Left := Floor((ARect.Left - CachedShiftX) * CachedRecScaleX);
  2129. Result.Right := Ceil((ARect.Right - CachedShiftX) * CachedRecScaleX);
  2130. end;
  2131. rrInside:
  2132. begin
  2133. Result.Left := Ceil((ARect.Left - CachedShiftX) * CachedRecScaleX);
  2134. Result.Right := Floor((ARect.Right - CachedShiftX) * CachedRecScaleX);
  2135. end;
  2136. end;
  2137. end;
  2138. if (CachedRecScaleY = 0) then
  2139. begin
  2140. Result.Top := High(Result.Top);
  2141. Result.Bottom := High(Result.Bottom);
  2142. end else
  2143. begin
  2144. case Rounding of
  2145. rrClosest:
  2146. begin
  2147. Result.Top := Round((ARect.Top - CachedShiftY) * CachedRecScaleY);
  2148. Result.Bottom := Round((ARect.Bottom - CachedShiftY) * CachedRecScaleY);
  2149. end;
  2150. rrOutside:
  2151. begin
  2152. Result.Top := Floor((ARect.Top - CachedShiftY) * CachedRecScaleY);
  2153. Result.Bottom := Ceil((ARect.Bottom - CachedShiftY) * CachedRecScaleY);
  2154. end;
  2155. rrInside:
  2156. begin
  2157. Result.Top := Ceil((ARect.Top - CachedShiftY) * CachedRecScaleY);
  2158. Result.Bottom := Floor((ARect.Bottom - CachedShiftY) * CachedRecScaleY);
  2159. end;
  2160. end;
  2161. end;
  2162. end;
  2163. function TCustomImage32.ControlToBitmap(const APoint: TPoint): TPoint;
  2164. begin
  2165. // Convert point coords from control's ref. frame to bitmap's ref. frame.
  2166. // The coordinates are not clipped to bitmap image boundary.
  2167. // Note that we are using Trunc instead of Round on purpose in order to
  2168. // be able to map directly from a scaled pixel to a bitmap pixel.
  2169. UpdateCache;
  2170. if (CachedRecScaleX = 0) then
  2171. Result.X := High(Result.X)
  2172. else
  2173. Result.X := Trunc((APoint.X - CachedShiftX) * CachedRecScaleX);
  2174. if (CachedRecScaleY = 0) then
  2175. Result.Y := High(Result.Y)
  2176. else
  2177. Result.Y := Trunc((APoint.Y - CachedShiftY) * CachedRecScaleY);
  2178. end;
  2179. function TCustomImage32.ControlToBitmap(const APoint: TFloatPoint): TFloatPoint;
  2180. begin
  2181. // Sub-pixel precision version
  2182. UpdateCache;
  2183. if (CachedRecScaleX = 0) then
  2184. Result.X := MaxInt
  2185. else
  2186. Result.X := (APoint.X - CachedShiftX) * CachedRecScaleX;
  2187. if (CachedRecScaleY = 0) then
  2188. Result.Y := MaxInt
  2189. else
  2190. Result.Y := (APoint.Y - CachedShiftY) * CachedRecScaleY;
  2191. end;
  2192. procedure TCustomImage32.DoInitStages;
  2193. begin
  2194. if Assigned(FOnInitStages) then
  2195. FOnInitStages(Self);
  2196. end;
  2197. function TCustomImage32.DoMouseWheel(Shift: TShiftState; WheelDelta: Integer;
  2198. MousePos: TPoint): Boolean;
  2199. var
  2200. r: TRect;
  2201. Pivot: TFloatPoint;
  2202. NewScale: TFloat;
  2203. ZoomIn: boolean;
  2204. ZoomFactor: TFloat;
  2205. begin
  2206. Result := inherited DoMouseWheel(Shift, WheelDelta, MousePos);
  2207. if (not Result) and (CanMouseZoom) and (FMouseZoomOptions.MatchShiftState(Shift)) then
  2208. begin
  2209. {$ifndef FPC} // On FPC the mouse position is in client coordinates. WTF?
  2210. MousePos := ScreenToClient(MousePos);
  2211. {$endif FPC}
  2212. r := GetBitmapRect;
  2213. // Constrain pivot to bitmap rect
  2214. Pivot.X := Constrain(MousePos.X, r.Left, r.Right);
  2215. Pivot.Y := Constrain(MousePos.Y, r.Top, r.Bottom);
  2216. // Transform coordinates from Control to Bitmap
  2217. Pivot.X := (Pivot.X - r.Left) / ScaleX;
  2218. Pivot.Y := (Pivot.Y - r.Top) / ScaleY;
  2219. ZoomIn := (WheelDelta > 0) xor (FMouseZoomOptions.Invert);
  2220. WheelDelta := Abs(WheelDelta);
  2221. // WheelDelta is expressed as 120 per step
  2222. ZoomFactor := Power(FMouseZoomOptions.ZoomFactor, WheelDelta div 120);
  2223. if (not ZoomIn) then
  2224. ZoomFactor := 1 / ZoomFactor;
  2225. NewScale := Scale * ZoomFactor;
  2226. DoZoom(Pivot, NewScale, FMouseZoomOptions.MaintainPivot, FMouseZoomOptions.Animate);
  2227. Result := True;
  2228. end;
  2229. end;
  2230. procedure TCustomImage32.DoPaintBuffer;
  2231. var
  2232. PaintStageHandlerCount: Integer;
  2233. i, j: Integer;
  2234. PaintStageMask: TPaintStageMaskValue;
  2235. begin
  2236. if RepaintOptimizer.Enabled then
  2237. RepaintOptimizer.BeginPaintBuffer;
  2238. UpdateCache;
  2239. if (FPaintStages.Dirty) then
  2240. begin
  2241. SetLength(FPaintStageHandlers, FPaintStages.Count);
  2242. SetLength(FPaintStageNum, FPaintStages.Count);
  2243. PaintStageHandlerCount := 0;
  2244. if (csDesigning in ComponentState) then
  2245. PaintStageMask := psmDesignTime
  2246. else
  2247. PaintStageMask := psmRunTime;
  2248. // compile list of paintstage handler methods
  2249. for i := 0 to FPaintStages.Count - 1 do
  2250. begin
  2251. if (PaintStageMask in FPaintStages[i].Mask) then
  2252. begin
  2253. FPaintStageNum[PaintStageHandlerCount] := i;
  2254. case FPaintStages[i].Stage of
  2255. PST_CUSTOM: FPaintStageHandlers[PaintStageHandlerCount] := ExecCustom;
  2256. PST_CLEAR_BUFFER: FPaintStageHandlers[PaintStageHandlerCount] := ExecClearBuffer;
  2257. PST_CLEAR_BACKGND: FPaintStageHandlers[PaintStageHandlerCount] := ExecClearBackgnd;
  2258. PST_DRAW_BITMAP: FPaintStageHandlers[PaintStageHandlerCount] := ExecDrawBitmap;
  2259. PST_DRAW_LAYERS: FPaintStageHandlers[PaintStageHandlerCount] := ExecDrawLayers;
  2260. PST_CONTROL_FRAME: FPaintStageHandlers[PaintStageHandlerCount] := ExecControlFrame;
  2261. PST_BITMAP_FRAME: FPaintStageHandlers[PaintStageHandlerCount] := ExecBitmapFrame;
  2262. else
  2263. Dec(PaintStageHandlerCount); // this should not happen .
  2264. end;
  2265. Inc(PaintStageHandlerCount);
  2266. end;
  2267. end;
  2268. SetLength(FPaintStageHandlers, PaintStageHandlerCount);
  2269. FPaintStages.Dirty := False;
  2270. end;
  2271. Buffer.BeginLockUpdate;
  2272. if (InvalidRects.Count = 0) then
  2273. begin
  2274. // No InvalidRects: Repaint everything
  2275. Buffer.ClipRect := GetViewportRect;
  2276. for i := 0 to High(FPaintStageHandlers) do
  2277. FPaintStageHandlers[i](Buffer, FPaintStageNum[i]);
  2278. end else
  2279. begin
  2280. // GR32_PAINT_ORDER_INVALIDRECTS is the classic method: For each update rect, repaint each layer.
  2281. // The other order (for each layer, repaint each update rect) gives slightly better performance
  2282. // but unfortunately not as much as one would think.
  2283. {$define GR32_PAINT_ORDER_INVALIDRECTS}
  2284. // We have InvalidRects: Repaint each rect
  2285. {$if defined(GR32_PAINT_ORDER_INVALIDRECTS)}
  2286. for j := 0 to InvalidRects.Count - 1 do
  2287. begin
  2288. Buffer.ClipRect := InvalidRects[j]^;
  2289. for i := 0 to High(FPaintStageHandlers) do
  2290. FPaintStageHandlers[i](Buffer, FPaintStageNum[i]);
  2291. end;
  2292. {$else}
  2293. for i := 0 to High(FPaintStageHandlers) do
  2294. for j := 0 to InvalidRects.Count - 1 do
  2295. begin
  2296. Buffer.ClipRect := InvalidRects[j]^;
  2297. FPaintStageHandlers[i](Buffer, FPaintStageNum[i]);
  2298. end;
  2299. {$ifend}
  2300. Buffer.ClipRect := GetViewportRect;
  2301. end;
  2302. Buffer.EndLockUpdate;
  2303. if RepaintOptimizer.Enabled then
  2304. RepaintOptimizer.EndPaintBuffer;
  2305. // avoid calling inherited, we have a totally different behaviour here...
  2306. BufferValid := True;
  2307. end;
  2308. procedure TCustomImage32.DoPaintGDIOverlay;
  2309. var
  2310. I: Integer;
  2311. begin
  2312. for I := 0 to Layers.Count - 1 do
  2313. if (Layers[I].LayerOptions and LOB_GDI_OVERLAY) <> 0 then
  2314. TLayerAccess(Layers[I]).PaintGDI(Canvas);
  2315. inherited;
  2316. end;
  2317. procedure TCustomImage32.DoScaleChange;
  2318. begin
  2319. if Assigned(FOnScaleChange) then
  2320. FOnScaleChange(Self);
  2321. end;
  2322. procedure TCustomImage32.DoSetPivot(const APivot: TFloatPoint);
  2323. begin
  2324. OffsetHorz := APivot.X;
  2325. OffsetVert := APivot.Y;
  2326. end;
  2327. procedure TCustomImage32.DoSetZoom(const APivot: TFloatPoint; AScale: TFloat; AMaintainPivot: boolean);
  2328. var
  2329. DeltaScale: TFloat;
  2330. NewOffset: TFloatPoint;
  2331. begin
  2332. if (AScale = Scale) then
  2333. exit;
  2334. DeltaScale := Scale;
  2335. NewOffset.X := OffsetHorz;
  2336. NewOffset.Y := OffsetVert;
  2337. BeginUpdate;
  2338. try
  2339. Scale := AScale;
  2340. if (AMaintainPivot) and (BitmapAlign = baCustom) and (ScaleMode = smScale) then
  2341. begin
  2342. DeltaScale := DeltaScale - Scale;
  2343. NewOffset.X := NewOffset.X + DeltaScale * APivot.X;
  2344. NewOffset.Y := NewOffset.Y + DeltaScale * APivot.Y;
  2345. DoSetPivot(NewOffset);
  2346. end;
  2347. Changed;
  2348. finally
  2349. EndUpdate;
  2350. end;
  2351. end;
  2352. procedure TCustomImage32.DoZoom({$ifdef CLOSURE_CAPTURES_CONST}const{$endif} APivot: TFloatPoint; AScale: TFloat; AMaintainPivot, AAnimate: boolean);
  2353. {$if defined(AnimatedZoom)}
  2354. var
  2355. StartValue, DeltaValue: TFloat;
  2356. const
  2357. MinZoomDelta = 0.01;
  2358. {$ifend}
  2359. begin
  2360. AScale := Constrain(AScale, FMouseZoomOptions.MinScale, FMouseZoomOptions.MaxScale);
  2361. if (AScale = Scale) then
  2362. exit;
  2363. {$if defined(AnimatedZoom)}
  2364. if (AAnimate) and (Showing) then
  2365. begin
  2366. StartValue := Scale;
  2367. DeltaValue := AScale-StartValue;
  2368. // Ease between old and new scale
  2369. AnimatedTween(TEaseCubic.EaseInOut, ZoomAnimateTime,
  2370. procedure(Value: Double; var Continue: boolean)
  2371. var
  2372. NewValue: Single;
  2373. begin
  2374. NewValue := StartValue + Value*DeltaValue;
  2375. if (Abs(StartValue-NewValue) >= MinZoomDelta) and
  2376. (Abs(AScale-NewValue) >= MinZoomDelta) and
  2377. (Scale <> NewValue) then
  2378. begin
  2379. DoSetZoom(APivot, NewValue, AMaintainPivot);
  2380. // Paint immediately or user will not see animation
  2381. Repaint;
  2382. end;
  2383. end, ZoomAnimateDeltaTime);
  2384. end;
  2385. {$ifend}
  2386. BeginUpdate;
  2387. try
  2388. DoSetZoom(APivot, AScale, AMaintainPivot);
  2389. ForceFullInvalidate;
  2390. finally
  2391. EndUpdate;
  2392. end;
  2393. end;
  2394. procedure TCustomImage32.Zoom(AScale: TFloat; const APivot: TFloatPoint; AAnimate: boolean);
  2395. begin
  2396. DoZoom(APivot, AScale, True, AAnimate);
  2397. end;
  2398. procedure TCustomImage32.Zoom(AScale: TFloat; AAnimate: boolean);
  2399. var
  2400. DummyPivot: TFloatPoint;
  2401. begin
  2402. DoZoom(DummyPivot, AScale, False, AAnimate);
  2403. end;
  2404. procedure TCustomImage32.ExecBitmapFrame(Dest: TBitmap32; StageNum: Integer);
  2405. begin
  2406. Dest.Canvas.DrawFocusRect(CachedBitmapRect);
  2407. end;
  2408. procedure TCustomImage32.ExecClearBackgnd(Dest: TBitmap32; StageNum: Integer);
  2409. var
  2410. OuterBorder: integer;
  2411. InnerBorder: integer;
  2412. Width: integer;
  2413. OddRow, EvenRow: TArrayOfColor32;
  2414. ColorEven, ColorOdd: PColor32;
  2415. X, Y: integer;
  2416. i: Integer;
  2417. Parity: integer;
  2418. ViewportRect: TRect;
  2419. BitmapRect: TRect;
  2420. r: TRect;
  2421. Tile: TRect;
  2422. BackgroundColor: TColor;
  2423. C: TColor32;
  2424. TileX, TileY: integer;
  2425. DrawFancyStuff: boolean;
  2426. DrawBitmapBackground: boolean;
  2427. begin
  2428. ViewportRect := GetViewportRect;
  2429. if (not Bitmap.Empty) and (Bitmap.DrawMode = dmOpaque) then
  2430. begin
  2431. // No need to draw background if bitmap covers everything
  2432. if (BitmapAlign = baTile) or (CachedBitmapRect.Contains(ViewportRect)) then
  2433. exit;
  2434. end;
  2435. // Background (from inside out/top down):
  2436. // - Checkers
  2437. // - white border
  2438. // - 1px black/dark border
  2439. // - bump map/solid color
  2440. // - alpha drop shadow
  2441. if (FBackgroundOptions.OuterBorderColor <> clNone) then
  2442. OuterBorder := 1
  2443. else
  2444. OuterBorder := 0;
  2445. if (FBackgroundOptions.InnerBorderWidth > 0) and (FBackgroundOptions.InnerBorderColor <> clNone) then
  2446. InnerBorder := FBackgroundOptions.InnerBorderWidth
  2447. else
  2448. InnerBorder := 0;
  2449. // If the bitmap is empty or if we're tiling it, or if the borders and dropshadow
  2450. // is disabled, then we only need to do a simple clear of the whole background.
  2451. DrawFancyStuff := (not Bitmap.Empty) and (BitmapAlign <> baTile) and
  2452. ((not FBackgroundOptions.DropShadowBitmap.Empty) or (FBackgroundOptions.DropShadowSize <> 0) or
  2453. (OuterBorder <> 0) or (InnerBorder <> 0));
  2454. // Do we need to clear the area below the bitmap?
  2455. DrawBitmapBackground := (not Bitmap.Empty) and (BitmapAlign <> baTile) and (Bitmap.DrawMode <> dmOpaque);
  2456. BitmapRect := CachedBitmapRect;
  2457. r := BitmapRect;
  2458. if (DrawFancyStuff) then
  2459. GR32.InflateRect(r, OuterBorder+InnerBorder, OuterBorder+InnerBorder);
  2460. (*
  2461. ** Background (pattern or solid color)
  2462. *)
  2463. if (FBackgroundOptions.FillStyle = bfsPattern) then
  2464. begin
  2465. Assert(not FBackgroundOptions.PatternBitmap.Empty);
  2466. TileX := (ViewportRect.Width + FBackgroundOptions.PatternBitmap.Width - 1) div FBackgroundOptions.PatternBitmap.Width;
  2467. TileY := (ViewportRect.Height + FBackgroundOptions.PatternBitmap.Height - 1) div FBackgroundOptions.PatternBitmap.Height;
  2468. for Y := 0 to TileY-1 do
  2469. for X := 0 to TileX-1 do
  2470. begin
  2471. Tile := Rect(0, 0, FBackgroundOptions.PatternBitmap.Width, FBackgroundOptions.PatternBitmap.Height);
  2472. GR32.OffsetRect(Tile, X * FBackgroundOptions.PatternBitmap.Width, Y * FBackgroundOptions.PatternBitmap.Height);
  2473. if (DrawBitmapBackground) and (BitmapRect.Contains(Tile)) then
  2474. // Tile would have been obscured by bitmap/checkers
  2475. continue;
  2476. BlockTransfer(Dest,
  2477. Tile.Left, Tile.Top, Dest.ClipRect,
  2478. FBackgroundOptions.PatternBitmap, FBackgroundOptions.PatternBitmap.BoundsRect, dmOpaque);
  2479. end;
  2480. // CheckersStyle=bcsNone doesn't clear the area under the bitmap so we need to do it here
  2481. if (DrawBitmapBackground) and (FBackgroundOptions.CheckersStyle = bcsNone) then
  2482. begin
  2483. BackgroundColor := Color;
  2484. {$ifdef FPC}
  2485. if (BackgroundColor = clDefault) then
  2486. BackgroundColor := GetDefaultColor(dctBrush);
  2487. {$endif}
  2488. C := Color32(BackgroundColor);
  2489. Dest.FillRectS(BitmapRect, C);
  2490. end;
  2491. end else
  2492. if (FBackgroundOptions.FillStyle = bfsColor) then
  2493. begin
  2494. BackgroundColor := Color;
  2495. {$ifdef FPC}
  2496. if (BackgroundColor = clDefault) then
  2497. BackgroundColor := GetDefaultColor(dctBrush);
  2498. {$endif}
  2499. C := Color32(BackgroundColor);
  2500. if InvalidRects.Count > 0 then
  2501. begin
  2502. for i := 0 to InvalidRects.Count-1 do
  2503. begin
  2504. if (DrawBitmapBackground) and (FBackgroundOptions.CheckersStyle <> bcsNone) and (BitmapRect.Contains(InvalidRects[i]^)) then
  2505. continue;
  2506. with InvalidRects[i]^ do
  2507. Dest.FillRectS(Left, Top, Right, Bottom, C);
  2508. end;
  2509. end else
  2510. if (DrawBitmapBackground) and (FBackgroundOptions.CheckersStyle <> bcsNone) then
  2511. begin
  2512. Dest.FillRectS(Rect(ViewportRect.Left, ViewportRect.Top, ViewportRect.Right, r.Top), C);
  2513. Dest.FillRectS(Rect(ViewportRect.Left, r.Top, r.Left, r.Bottom), C);
  2514. Dest.FillRectS(Rect(r.Right, r.Top, ViewportRect.Right, r.Bottom), C);
  2515. Dest.FillRectS(Rect(ViewportRect.Left, r.Bottom, ViewportRect.Right, ViewportRect.Bottom), C);
  2516. end else
  2517. Dest.Clear(C);
  2518. end;
  2519. if (DrawFancyStuff) then
  2520. begin
  2521. (*
  2522. ** Drop shadow
  2523. *)
  2524. if (not FBackgroundOptions.DropShadowBitmap.Empty) then
  2525. begin
  2526. (*
  2527. The drop shadow tile is partitioned into 5 segments
  2528. +
  2529. +-------+ 1|#
  2530. | |#
  2531. | |# -> 2|#
  2532. | |#
  2533. +-------+# 3 4 5|
  2534. ######### +- - -+
  2535. # # #
  2536. *)
  2537. X := FBackgroundOptions.DropShadowBitmap.Width div 2;
  2538. Y := FBackgroundOptions.DropShadowBitmap.Height div 2;
  2539. // Segment 1
  2540. FBackgroundOptions.DropShadowBitmap.DrawTo(Dest,
  2541. r.Right, r.Top+FBackgroundOptions.DropShadowOffset,
  2542. Rect(X, 0, FBackgroundOptions.DropShadowBitmap.Width-1, Y));
  2543. // Segment 2 (stretched)
  2544. FBackgroundOptions.DropShadowBitmap.DrawTo(Dest,
  2545. Rect(r.Right, r.Top+FBackgroundOptions.DropShadowOffset+Y, r.Right+X, r.Bottom),
  2546. Rect(X, Y, FBackgroundOptions.DropShadowBitmap.Width, Y+1));
  2547. // Segment 3
  2548. FBackgroundOptions.DropShadowBitmap.DrawTo(Dest,
  2549. r.Left+FBackgroundOptions.DropShadowOffset, r.Bottom,
  2550. Rect(0, Y, X, FBackgroundOptions.DropShadowBitmap.Height-1));
  2551. // Segment 4 (stretched)
  2552. FBackgroundOptions.DropShadowBitmap.DrawTo(Dest,
  2553. Rect(r.Left+FBackgroundOptions.DropShadowOffset+X, r.Bottom, r.Right, r.Bottom+Y),
  2554. Rect(X, Y, X+1, FBackgroundOptions.DropShadowBitmap.Height));
  2555. // Segment 5
  2556. FBackgroundOptions.DropShadowBitmap.DrawTo(Dest,
  2557. r.Right, r.Bottom,
  2558. Rect(X, Y, FBackgroundOptions.DropShadowBitmap.Width-1, FBackgroundOptions.DropShadowBitmap.Height-1));
  2559. end else
  2560. if (FBackgroundOptions.DropShadowSize <> 0) then
  2561. begin
  2562. // Note: Transparent
  2563. Dest.FillRectTS(Rect(r.Right, r.Top+FBackgroundOptions.DropShadowOffset, r.Right+FBackgroundOptions.DropShadowSize, r.Bottom), FBackgroundOptions.DropShadowColor);
  2564. Dest.FillRectTS(Rect(r.Left+FBackgroundOptions.DropShadowOffset, r.Bottom, r.Right+FBackgroundOptions.DropShadowSize, r.Bottom+FBackgroundOptions.DropShadowSize), FBackgroundOptions.DropShadowColor);
  2565. end;
  2566. (*
  2567. ** Outer dark border
  2568. *)
  2569. if (OuterBorder <> 0) then
  2570. begin
  2571. Dest.FrameRectS(r, Color32(FBackgroundOptions.OuterBorderColor));
  2572. GR32.InflateRect(r, -OuterBorder, -OuterBorder);
  2573. end;
  2574. (*
  2575. ** Inner light border
  2576. *)
  2577. if (InnerBorder <> 0) then
  2578. begin
  2579. C := Color32(FBackgroundOptions.InnerBorderColor);
  2580. if (InnerBorder > 1) then
  2581. begin
  2582. Dest.FillRectS(Rect(r.Left, r.Top, r.Right, BitmapRect.Top), C);
  2583. Dest.FillRectS(Rect(r.Left, BitmapRect.Top, BitmapRect.Left, BitmapRect.Bottom), C);
  2584. Dest.FillRectS(Rect(BitmapRect.Right, BitmapRect.Top, r.Right, BitmapRect.Bottom), C);
  2585. Dest.FillRectS(Rect(r.Left, BitmapRect.Bottom, r.Right, r.Bottom), C);
  2586. end else
  2587. Dest.FrameRectS(r, C);
  2588. end;
  2589. end;
  2590. (*
  2591. ** Checkers
  2592. *)
  2593. if (FBackgroundOptions.CheckersStyle <> bcsNone) and
  2594. ((DrawBitmapBackground) or
  2595. ((FBackgroundOptions.FillStyle = bfsCheckers) and
  2596. ((Bitmap.Empty) or (Bitmap.DrawMode = dmOpaque)))) then
  2597. begin
  2598. if (FBackgroundOptions.FillStyle = bfsCheckers) then
  2599. // Fill the whole viewport
  2600. r := Dest.ClipRect
  2601. else
  2602. // Fill the area under the bitmap
  2603. GR32.IntersectRect(r, BitmapRect, Dest.ClipRect);
  2604. Width := r.Width;
  2605. if (Width > 0) then
  2606. begin
  2607. if (FBackgroundOptions.CheckersStyle <> bcsCustom) or (FBackgroundOptions.CheckersColors[0] <> FBackgroundOptions.CheckersColors[1]) then
  2608. begin
  2609. SetLength(OddRow, Width);
  2610. SetLength(EvenRow, Width);
  2611. ColorEven := @EvenRow[0];
  2612. ColorOdd := @OddRow[0];
  2613. for X := 0 to Width-1 do
  2614. begin
  2615. Parity := ((r.Left+X) shr FBackgroundOptions.CheckersExponent) and $1;
  2616. ColorEven^ := FBackgroundOptions.CheckersColors[Parity];
  2617. ColorOdd^ := FBackgroundOptions.CheckersColors[1-Parity];
  2618. inc(ColorEven);
  2619. inc(ColorOdd);
  2620. end;
  2621. // Note: For ((DrawMode<>dmOpaque) and (FillStyle=bfsCheckers)) we should
  2622. // exclude filling the area covered by the bitmap. For simplicity we're
  2623. // not doing that.
  2624. for Y := r.Top to r.Bottom-1 do
  2625. begin
  2626. Parity := (Y shr FBackgroundOptions.CheckersExponent) and $1;
  2627. if (Parity = 0) then
  2628. MoveLongword(EvenRow[0], Dest.PixelPtr[r.Left, Y]^, Width)
  2629. else
  2630. MoveLongword(OddRow[0], Dest.PixelPtr[r.Left, Y]^, Width);
  2631. end;
  2632. end else
  2633. // Odd color = Even color -> Just clear with the color
  2634. Dest.FillRectS(r, FBackgroundOptions.CheckersColors[0]);
  2635. end;
  2636. end;
  2637. end;
  2638. procedure TCustomImage32.ExecClearBuffer(Dest: TBitmap32; StageNum: Integer);
  2639. var
  2640. BackgroundColor: TColor;
  2641. begin
  2642. // By default ExecClearBuffer is never called because the PST_CLEAR_BUFFER
  2643. // paint stage isn't used by default.
  2644. // We skip the clear if Image.Bitmap.DrawMode=dmOpaque since the bitmap will
  2645. // cover the area we cleared anyway.
  2646. if (Bitmap.Empty) or (Bitmap.DrawMode <> dmOpaque) then
  2647. begin
  2648. BackgroundColor := Color;
  2649. {$ifdef FPC}
  2650. if (BackgroundColor = clDefault) then
  2651. BackgroundColor := GetDefaultColor(dctBrush);
  2652. {$endif}
  2653. Dest.Clear(Color32(BackgroundColor));
  2654. end;
  2655. end;
  2656. procedure TCustomImage32.ExecControlFrame(Dest: TBitmap32; StageNum: Integer);
  2657. begin
  2658. DrawFocusRect(Dest.Handle, Rect(0, 0, Width, Height));
  2659. end;
  2660. procedure TCustomImage32.ExecCustom(Dest: TBitmap32; StageNum: Integer);
  2661. begin
  2662. if Assigned(FOnPaintStage) then
  2663. FOnPaintStage(Self, Dest, StageNum);
  2664. end;
  2665. procedure TCustomImage32.ExecDrawBitmap(Dest: TBitmap32; StageNum: Integer);
  2666. var
  2667. BitmapRect: TRect;
  2668. TileX, TileY: Integer;
  2669. TileCountX, TileCountY: Integer;
  2670. Tile: TRect;
  2671. begin
  2672. if Bitmap.Empty then
  2673. Exit;
  2674. BitmapRect := CachedBitmapRect;
  2675. if GR32.IsRectEmpty(BitmapRect) then
  2676. Exit;
  2677. Bitmap.Lock;
  2678. try
  2679. if (BitmapAlign <> baTile) then
  2680. Bitmap.DrawTo(Dest, BitmapRect)
  2681. else
  2682. begin
  2683. TileCountX := Dest.Width div BitmapRect.Right;
  2684. TileCountY := Dest.Height div BitmapRect.Bottom;
  2685. if ((TileCountX > 0) or (TileCountY > 0)) and
  2686. ((BitmapRect.Width <> Bitmap.Width) or (BitmapRect.Height <> Bitmap.Height)) then
  2687. begin
  2688. // Tile and Stretch
  2689. Tile := BitmapRect;
  2690. for TileY := 0 to TileCountY do
  2691. begin
  2692. for TileX := 0 to TileCountX do
  2693. begin
  2694. Bitmap.DrawTo(Dest, Tile);
  2695. GR32.OffsetRect(Tile, BitmapRect.Width, 0);
  2696. end;
  2697. Tile.Left := BitmapRect.Left;
  2698. Tile.Right := BitmapRect.Right;
  2699. GR32.OffsetRect(Tile, 0, BitmapRect.Height);
  2700. end;
  2701. end else
  2702. if (BitmapRect.Width = Bitmap.Width) and (BitmapRect.Height = Bitmap.Height) then
  2703. begin
  2704. // No stretch, possibly Tiling,
  2705. Tile := BitmapRect;
  2706. for TileY := 0 to TileCountY do
  2707. begin
  2708. for TileX := 0 to TileCountX do
  2709. begin
  2710. Bitmap.DrawTo(Dest, Tile.Left, Tile.Top);
  2711. GR32.OffsetRect(Tile, BitmapRect.Width, 0);
  2712. end;
  2713. Tile.Left := BitmapRect.Left;
  2714. Tile.Right := BitmapRect.Right;
  2715. GR32.OffsetRect(Tile, 0, BitmapRect.Height);
  2716. end;
  2717. end else
  2718. // Stretch, No tiling
  2719. Bitmap.DrawTo(Dest, BitmapRect);
  2720. end;
  2721. finally
  2722. Bitmap.Unlock;
  2723. end;
  2724. end;
  2725. procedure TCustomImage32.ExecDrawLayers(Dest: TBitmap32; StageNum: Integer);
  2726. var
  2727. I: Integer;
  2728. Mask: Cardinal;
  2729. begin
  2730. Mask := PaintStages[StageNum]^.Parameter;
  2731. for I := 0 to Layers.Count - 1 do
  2732. if (Layers.Items[I].LayerOptions and Mask) <> 0 then
  2733. TLayerAccess(Layers.Items[I]).DoPaint(Dest);
  2734. end;
  2735. function TCustomImage32.GetBitmapRect: TRect;
  2736. var
  2737. Size: TSize;
  2738. begin
  2739. if not Bitmap.Empty then
  2740. begin
  2741. Size := GetBitmapSize;
  2742. Result := Rect(0, 0, Size.cx, Size.cy);
  2743. if BitmapAlign = baCenter then
  2744. GR32.OffsetRect(Result, (ClientWidth - Size.cx) div 2, (ClientHeight - Size.cy) div 2)
  2745. else
  2746. if BitmapAlign = baCustom then
  2747. GR32.OffsetRect(Result, Round(OffsetHorz), Round(OffsetVert));
  2748. end else
  2749. Result := Default(TRect)
  2750. end;
  2751. function TCustomImage32.GetBitmapMargin: integer;
  2752. begin
  2753. Result := 0;
  2754. end;
  2755. function TCustomImage32.GetBitmapSize: TSize;
  2756. var
  2757. Mode: TScaleMode;
  2758. ViewportRect: TRect;
  2759. ViewportWidth, ViewportHeight: Integer;
  2760. BitmapMargin: integer;
  2761. ScaledBitmapWidth, ScaledBitmapHeight: integer;
  2762. ResizeScaleX, ResizeScaleY: TFloat;
  2763. begin
  2764. if Bitmap.Empty or (Width = 0) or (Height = 0) then
  2765. begin
  2766. Result.cx := 0;
  2767. Result.cy := 0;
  2768. Exit;
  2769. end;
  2770. ViewportRect := GetViewportRect;
  2771. ViewportWidth := ViewportRect.Width;
  2772. ViewportHeight := ViewportRect.Height;
  2773. BitmapMargin := GetBitmapMargin * 2;
  2774. case ScaleMode of
  2775. smOptimalScaled,
  2776. smScale:
  2777. begin
  2778. ScaledBitmapWidth := Round(Bitmap.Width * ScaleX);
  2779. ScaledBitmapHeight := Round(Bitmap.Height * ScaleY);
  2780. end;
  2781. else
  2782. ScaledBitmapWidth := Bitmap.Width;
  2783. ScaledBitmapHeight := Bitmap.Height;
  2784. end;
  2785. // Check for optimal modes as these are compounds of the other modes.
  2786. case ScaleMode of
  2787. smOptimal:
  2788. if (Bitmap.Width + BitmapMargin > ViewportWidth) or (Bitmap.Height + BitmapMargin > ViewportHeight) then
  2789. // Bitmap+margins is too big for viewport; Resize to fit
  2790. Mode := smResize
  2791. else
  2792. // Bitmap+margins fits within viewport; Don't resize
  2793. Mode := smNormal;
  2794. smOptimalScaled:
  2795. begin
  2796. if (ScaledBitmapWidth + BitmapMargin > ViewportWidth) or
  2797. (ScaledBitmapHeight + BitmapMargin > ViewportHeight) then
  2798. begin
  2799. // Scaled bitmap+margins is too big for viewport; Resize to fit
  2800. Mode := smResize;
  2801. ScaledBitmapWidth := Bitmap.Width;
  2802. ScaledBitmapHeight := Bitmap.Height;
  2803. end else
  2804. // Scaled bitmap+margins fits within viewport; Don't resize, just scale
  2805. Mode := smScale;
  2806. end
  2807. else
  2808. Mode := ScaleMode;
  2809. end;
  2810. case Mode of
  2811. smNormal:
  2812. begin
  2813. Result.cx := Bitmap.Width;
  2814. Result.cy := Bitmap.Height;
  2815. end;
  2816. smStretch:
  2817. begin
  2818. // Stretch bitmap to fit within margins
  2819. Result.cx := ViewportWidth - BitmapMargin;
  2820. Result.cy := ViewportHeight - BitmapMargin;
  2821. end;
  2822. smResize:
  2823. begin
  2824. // Scale bitmap to fit within margins
  2825. ResizeScaleX := (ViewportWidth - BitmapMargin) / ScaledBitmapWidth;
  2826. ResizeScaleY := (ViewportHeight - BitmapMargin) / ScaledBitmapHeight;
  2827. if (ResizeScaleX >= ResizeScaleY) then
  2828. begin
  2829. Result.cx := Round(Bitmap.Width * ResizeScaleY);
  2830. Result.cy := Round(Bitmap.Height * ResizeScaleY);
  2831. end else
  2832. begin
  2833. Result.cx := Round(Bitmap.Width * ResizeScaleX);
  2834. Result.cy := Round(Bitmap.Height * ResizeScaleX);
  2835. end;
  2836. end;
  2837. else // smScale
  2838. Result.cx := ScaledBitmapWidth;
  2839. Result.cy := ScaledBitmapHeight;
  2840. end;
  2841. if (Result.cx <= 0) then
  2842. Result.cx := 0;
  2843. if (Result.cy <= 0) then
  2844. Result.cy := 0;
  2845. end;
  2846. function TCustomImage32.GetOnPixelCombine: TPixelCombineEvent;
  2847. begin
  2848. Result := FBitmap.OnPixelCombine;
  2849. end;
  2850. procedure TCustomImage32.InitDefaultStages;
  2851. begin
  2852. // clear buffer
  2853. (* Not used. PST_CLEAR_BACKGND is used instead.
  2854. with PaintStages.Add^ do
  2855. begin
  2856. Mask := [];
  2857. Stage := PST_CLEAR_BUFFER;
  2858. end;
  2859. *)
  2860. // background
  2861. with PaintStages.Add^ do
  2862. begin
  2863. Mask := [psmRunTime, psmDesignTime]; // See issue #247
  2864. Stage := PST_CLEAR_BACKGND;
  2865. end;
  2866. // control frame
  2867. with PaintStages.Add^ do
  2868. begin
  2869. Mask := [psmDesignTime];
  2870. Stage := PST_CONTROL_FRAME;
  2871. end;
  2872. // bitmap
  2873. with PaintStages.Add^ do
  2874. begin
  2875. Mask := [psmRunTime, psmDesignTime, psmExport];
  2876. Stage := PST_DRAW_BITMAP;
  2877. end;
  2878. // bitmap frame
  2879. with PaintStages.Add^ do
  2880. begin
  2881. Mask := [psmDesignTime];
  2882. Stage := PST_BITMAP_FRAME;
  2883. end;
  2884. // layers
  2885. with PaintStages.Add^ do
  2886. begin
  2887. Mask := [psmRunTime, psmDesignTime, psmExport];
  2888. Stage := PST_DRAW_LAYERS;
  2889. Parameter := LOB_VISIBLE;
  2890. end;
  2891. end;
  2892. procedure TCustomImage32.Invalidate;
  2893. begin
  2894. FCacheValid := False;
  2895. inherited;
  2896. end;
  2897. procedure TCustomImage32.Update(const Rect: TRect);
  2898. begin
  2899. Invalidate(Rect);
  2900. end;
  2901. procedure TCustomImage32.Invalidate(const Rect: TRect);
  2902. begin
  2903. InvalidateArea(Rect, AREAINFO_RECT, True);
  2904. end;
  2905. function TCustomImage32.InvalidRectsAvailable: Boolean;
  2906. begin
  2907. // avoid calling inherited, we have a totally different behaviour here...
  2908. DoPrepareInvalidRects;
  2909. Result := (InvalidRects.Count > 0);
  2910. end;
  2911. procedure TCustomImage32.LayerCollectionChangeHandler(Sender: TObject);
  2912. begin
  2913. Changed;
  2914. end;
  2915. procedure TCustomImage32.LayerCollectionGDIUpdateHandler(Sender: TObject);
  2916. begin
  2917. Paint;
  2918. end;
  2919. procedure TCustomImage32.LayerCollectionGetViewportScaleHandler(Sender: TObject;
  2920. out ScaleX, ScaleY: TFloat);
  2921. begin
  2922. UpdateCache;
  2923. ScaleX := CachedScaleX;
  2924. ScaleY := CachedScaleY;
  2925. end;
  2926. procedure TCustomImage32.LayerCollectionGetViewportShiftHandler(Sender: TObject;
  2927. out ShiftX, ShiftY: TFloat);
  2928. begin
  2929. UpdateCache;
  2930. ShiftX := CachedShiftX;
  2931. ShiftY := CachedShiftY;
  2932. end;
  2933. procedure TCustomImage32.LayerListNotify(ALayer: TCustomLayer; AAction: TLayerListNotification; AIndex: Integer);
  2934. begin
  2935. case AAction of
  2936. lnLayerDeleted:
  2937. if (ALayer = FHotLayer) then
  2938. SetHotTrackLayer(nil);
  2939. end;
  2940. end;
  2941. procedure TCustomImage32.Loaded;
  2942. begin
  2943. inherited;
  2944. DoInitStages;
  2945. end;
  2946. // PanDetect is an adaption of DragDetectPlus from the Drag and Drop Component Suite.
  2947. // The following assumptions are made:
  2948. // - The Position parameter is in screen coordinates.
  2949. // - The mouse has already been captured.
  2950. // - Only the left mouse button is handled.
  2951. {$if defined(MSWINDOWS)}
  2952. function PanDetect(Handle: THandle; Position: TPoint): boolean;
  2953. var
  2954. DragRect: TRect;
  2955. Msg: TMsg;
  2956. StartTime: DWORD;
  2957. const
  2958. PM_QS_INPUT = QS_INPUT shl 16;
  2959. PM_QS_KEY = QS_KEY shl 16;
  2960. PM_QS_MOUSEMOVE = QS_MOUSEMOVE shl 16;
  2961. PM_QS_MOUSEBUTTON = QS_MOUSEBUTTON shl 16;
  2962. begin
  2963. Result := False;
  2964. // Check mouse state, and punt if none of the mouse buttons are down.
  2965. if ((GetKeyState(VK_LBUTTON) AND $8000) = 0) then
  2966. exit;
  2967. // Calculate the drag rect.
  2968. // If the mouse leaves this rect, while the mouse button is pressed, a drag is
  2969. // detected.
  2970. DragRect.TopLeft := Position;
  2971. DragRect.BottomRight := Position;
  2972. GR32.InflateRect(DragRect, GetSystemMetrics(SM_CXDRAG), GetSystemMetrics(SM_CYDRAG));
  2973. StartTime := TimeGetTime;
  2974. // Abort if we haven't captured the mouse.
  2975. if (GetCapture <> Handle) then
  2976. exit;
  2977. while (not Result) do
  2978. begin
  2979. // Wait for mouse or keyboard events.
  2980. // - but do not eat mouse button messages (so we don't break popup menus etc).
  2981. if (PeekMessage(Msg, 0, WM_LBUTTONDOWN, WM_LBUTTONUP, PM_NOREMOVE)) then
  2982. begin
  2983. // Mouse button was changed - bail out.
  2984. exit;
  2985. end;
  2986. while (not PeekMessage(Msg, 0, WM_LBUTTONDOWN, WM_LBUTTONUP, PM_NOREMOVE)) and
  2987. (not PeekMessage(Msg, 0, 0, 0, PM_REMOVE or PM_QS_KEY or PM_QS_MOUSEMOVE)) and
  2988. (GetCapture = Handle) do
  2989. begin
  2990. // If there are no events for 500mS start drag without further ado.
  2991. if (MsgWaitForMultipleObjects(0, nil^, False, 500, QS_INPUT) = WAIT_TIMEOUT) then
  2992. begin
  2993. Result := True;
  2994. exit;
  2995. end;
  2996. end;
  2997. // Bail out if someone else has captured the mouse.
  2998. if (GetCapture <> Handle) then
  2999. break;
  3000. case (Msg.message) of
  3001. // Mouse was moved.
  3002. WM_MOUSEMOVE:
  3003. // Start drag if mouse has moved outside the drag rect and the minimum
  3004. // time has elapsed.
  3005. // Note that we ignore time warp (wrap around) and that Msg.Time
  3006. // might be smaller than StartTime.
  3007. Result := (not GR32.PtInRect(DragRect, Msg.pt)) and (Msg.time >= StartTime + DWORD(100));
  3008. // [Esc] cancels drag detection.
  3009. WM_KEYDOWN:
  3010. if (Msg.wParam = VK_ESCAPE) then
  3011. break;
  3012. // Some operation cancelled our mouse capture.
  3013. WM_CANCELMODE:
  3014. break;
  3015. // Application is shutting down.
  3016. WM_QUIT:
  3017. begin
  3018. // Put quit message back in queue and abort.
  3019. PostQuitMessage(Msg.wParam);
  3020. exit;
  3021. end;
  3022. end;
  3023. end;
  3024. end;
  3025. {$ifend}
  3026. procedure TCustomImage32.KeyDown(var Key: Word; Shift: TShiftState);
  3027. var
  3028. FirstLayer: TCustomLayer;
  3029. i: integer;
  3030. begin
  3031. inherited;
  3032. // First forward key event to any layer that has captured the mouse...
  3033. FirstLayer := TLayerCollectionAccess(Layers).MouseListener;
  3034. if (FirstLayer <> nil) then
  3035. TLayerAccess(FirstLayer).KeyDown(Key, Shift);
  3036. // ... and the to the remaining layers in Z-order
  3037. i := Layers.Count-1;
  3038. while (Key <> 0) and (i >= 0) do
  3039. begin
  3040. if (Layers[i] <> FirstLayer) then
  3041. TLayerAccess(Layers[i]).KeyDown(Key, Shift);
  3042. Dec(i);
  3043. end;
  3044. end;
  3045. procedure TCustomImage32.KeyUp(var Key: Word; Shift: TShiftState);
  3046. var
  3047. FirstLayer: TCustomLayer;
  3048. i: integer;
  3049. begin
  3050. inherited;
  3051. // First forward key event to any layer that has captured the mouse...
  3052. FirstLayer := TLayerCollectionAccess(Layers).MouseListener;
  3053. if (FirstLayer <> nil) then
  3054. TLayerAccess(FirstLayer).KeyUp(Key, Shift);
  3055. // ... and the to the remaining layers in Z-order
  3056. i := Layers.Count-1;
  3057. while (Key <> 0) and (i >= 0) do
  3058. begin
  3059. if (Layers[i] <> FirstLayer) then
  3060. TLayerAccess(Layers[i]).KeyUp(Key, Shift);
  3061. Dec(i);
  3062. end;
  3063. end;
  3064. procedure TCustomImage32.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  3065. var
  3066. Layer: TCustomLayer;
  3067. begin
  3068. inherited;
  3069. if TabStop and CanFocus then
  3070. SetFocus;
  3071. if (not GetViewportRect.Contains(Point(X, Y))) then
  3072. begin
  3073. // Click outside viewport; Most likely the small rectangle in the
  3074. // lower right corner between the scrollbars.
  3075. MouseCapture := False;
  3076. exit;
  3077. end;
  3078. {$ifdef MOUSE_UPDATE_BATCHING}
  3079. BeginUpdate;
  3080. try
  3081. {$endif MOUSE_UPDATE_BATCHING}
  3082. if Layers.MouseEvents then
  3083. Layer := TLayerCollectionAccess(Layers).MouseDown(Button, Shift, X, Y)
  3084. else
  3085. Layer := nil;
  3086. // lock the capture only if mbLeft was pushed or any mouse listener was activated
  3087. if (Button = mbLeft) or (TLayerCollectionAccess(Layers).MouseListener <> nil) then
  3088. // Note that TControl will have already captured the mouse for us since we
  3089. // have ControlStyle=[...csCaptureMouse...]
  3090. MouseCapture := True;
  3091. MouseDown(Button, Shift, X, Y, Layer);
  3092. // Signal MouseUp that we handled the MouseDown
  3093. FClicked := True;
  3094. if (Layer = nil) and (CanMousePan) and (Button = FMousePanOptions.MouseButton) and (FMousePanOptions.MatchShiftState(Shift)) then
  3095. begin
  3096. // Wait a moment, looking for a mouse-up, before we decide that this
  3097. // is a drag and not a click. Note that we cannot use the Windows DragDetect
  3098. // function as it eats the mouse-up event and thus break the OnClick generation.
  3099. {$if defined(MSWINDOWS)}
  3100. if (not PanDetect(WindowHandle, ClientToScreen(Point(X, Y)))) then
  3101. exit;
  3102. {$ifend}
  3103. FIsMousePanning := True;
  3104. if (FMousePanOptions.PanCursor <> crDefault) then
  3105. Screen.Cursor := FMousePanOptions.PanCursor;
  3106. // Avoid OnClick event when pan finishes
  3107. ControlState := ControlState - [csClicked];
  3108. // Remember start point
  3109. FMousePanStartPos.X := X;
  3110. FMousePanStartPos.Y := Y;
  3111. end;
  3112. {$ifdef MOUSE_UPDATE_BATCHING}
  3113. finally
  3114. EndUpdate;
  3115. end;
  3116. {$endif MOUSE_UPDATE_BATCHING}
  3117. end;
  3118. procedure TCustomImage32.MouseMove(Shift: TShiftState; X, Y: Integer);
  3119. var
  3120. Layer: TCustomLayer;
  3121. Delta: TPoint;
  3122. begin
  3123. inherited;
  3124. // If we're panning then calculate how far mouse has moved since last and
  3125. // scroll image the same amount.
  3126. if (FIsMousePanning) then
  3127. begin
  3128. Delta.X := FMousePanStartPos.X - X;
  3129. Delta.Y := FMousePanStartPos.Y - Y;
  3130. FMousePanStartPos.X := X;
  3131. FMousePanStartPos.Y := Y;
  3132. if (Delta.X <> 0) or (Delta.Y <> 0) then
  3133. Scroll(Delta.X, Delta.Y);
  3134. if (FMousePanOptions.PanCursor <> crDefault) then
  3135. Screen.Cursor := FMousePanOptions.PanCursor;
  3136. end else
  3137. // Ignore movement outside viewport unless we have captured the mouse
  3138. if (MouseCapture) or (GetViewportRect.Contains(Point(X, Y))) then
  3139. begin
  3140. {$ifdef MOUSE_UPDATE_BATCHING}
  3141. BeginUpdate;
  3142. try
  3143. {$endif MOUSE_UPDATE_BATCHING}
  3144. if Layers.MouseEvents then
  3145. begin
  3146. Layer := TLayerCollectionAccess(Layers).MouseMove(Shift, X, Y);
  3147. if (Layer = nil) then
  3148. // Restore cursor in case we moved from a layer to outside any layer
  3149. Screen.Cursor := Cursor;
  3150. end else
  3151. Layer := nil;
  3152. MouseMove(Shift, X, Y, Layer);
  3153. SetHotTrackLayer(Layer);
  3154. {$ifdef MOUSE_UPDATE_BATCHING}
  3155. finally
  3156. EndUpdate;
  3157. end;
  3158. {$endif MOUSE_UPDATE_BATCHING}
  3159. end else
  3160. begin
  3161. // Restore cursor in case we moved from layer to outside viewport
  3162. // but inside control
  3163. Screen.Cursor := Cursor;
  3164. SetHotTrackLayer(nil);
  3165. end;
  3166. end;
  3167. procedure TCustomImage32.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  3168. var
  3169. Layer: TCustomLayer;
  3170. MouseListener: TCustomLayer;
  3171. begin
  3172. // Ignore MouseUp unless we handled the MouseDown. Do not use MouseCapture
  3173. // for this test (see below).
  3174. if (not FClicked) then
  3175. exit;
  3176. FClicked := False;
  3177. MouseListener := TLayerCollectionAccess(Layers).MouseListener;
  3178. {$ifdef MOUSE_UPDATE_BATCHING}
  3179. BeginUpdate;
  3180. try
  3181. {$endif MOUSE_UPDATE_BATCHING}
  3182. if Layers.MouseEvents then
  3183. Layer := TLayerCollectionAccess(Layers).MouseUp(Button, Shift, X, Y)
  3184. else
  3185. Layer := nil;
  3186. // Unlock the capture using same criteria as was used to acquire it
  3187. if (Button = mbLeft) or ((MouseListener <> nil) and (TLayerCollectionAccess(Layers).MouseListener = nil)) then
  3188. // Note that TControl will have already released the mouse capture since
  3189. // we have ControlStyle=[...csCaptureMouse...]
  3190. MouseCapture := False;
  3191. MouseUp(Button, Shift, X, Y, Layer);
  3192. if (FIsMousePanning) and (Button = FMousePanOptions.MouseButton) then
  3193. begin
  3194. FIsMousePanning := False;
  3195. if (FMousePanOptions.PanCursor <> crDefault) then
  3196. Screen.Cursor := crDefault;
  3197. end;
  3198. {$ifdef MOUSE_UPDATE_BATCHING}
  3199. finally
  3200. EndUpdate;
  3201. end;
  3202. {$endif MOUSE_UPDATE_BATCHING}
  3203. end;
  3204. procedure TCustomImage32.MouseDown(Button: TMouseButton;
  3205. Shift: TShiftState; X, Y: Integer; Layer: TCustomLayer);
  3206. begin
  3207. if Assigned(FOnMouseDown) then
  3208. FOnMouseDown(Self, Button, Shift, X, Y, Layer);
  3209. end;
  3210. procedure TCustomImage32.MouseMove(Shift: TShiftState; X, Y: Integer;
  3211. Layer: TCustomLayer);
  3212. begin
  3213. if Assigned(FOnMouseMove) then
  3214. FOnMouseMove(Self, Shift, X, Y, Layer);
  3215. end;
  3216. procedure TCustomImage32.MouseUp(Button: TMouseButton; Shift: TShiftState;
  3217. X, Y: Integer; Layer: TCustomLayer);
  3218. begin
  3219. if Assigned(FOnMouseUp) then
  3220. FOnMouseUp(Self, Button, Shift, X, Y, Layer);
  3221. end;
  3222. procedure TCustomImage32.MouseEnter;
  3223. begin
  3224. inherited MouseEnter;
  3225. end;
  3226. procedure TCustomImage32.MouseLeave;
  3227. begin
  3228. SetHotTrackLayer(nil);
  3229. if (Layers.MouseEvents) and (Layers.MouseListener = nil) then
  3230. Screen.Cursor := crDefault;
  3231. inherited;
  3232. end;
  3233. procedure TCustomImage32.PaintTo(Dest: TBitmap32; DestRect: TRect);
  3234. var
  3235. OldRepaintMode: TRepaintMode;
  3236. I: Integer;
  3237. begin
  3238. if (Dest = nil) then
  3239. exit;
  3240. OldRepaintMode := RepaintMode;
  3241. RepaintMode := rmFull;
  3242. FCachedBitmapRect := DestRect;
  3243. if (DestRect.Right <= DestRect.Left) or (DestRect.Bottom <= DestRect.Top) or Bitmap.Empty then
  3244. SetXForm(0, 0, 1, 1)
  3245. else
  3246. SetXForm(DestRect.Left, DestRect.Top, DestRect.Width / Bitmap.Width, DestRect.Height / Bitmap.Height);
  3247. FCacheValid := True;
  3248. //
  3249. // By default neither PST_CLEAR_BUFFER/ExecClearBuffer nor
  3250. // PST_CLEAR_BACKGND/ExecClearBackgnd are called to clear the
  3251. // destination bitmap.
  3252. //
  3253. // This means that we are painting the bitmap and layers onto whatever is
  3254. // already on the destination bitmap. This makes it possible to produce a
  3255. // flattened semitransparent bitmap.
  3256. // If an flattened opaque bitmap is desired then:
  3257. //
  3258. // - The destination bitmap can be made opaque before PaintTo is called
  3259. // (i.e. cleared with the desired background color).
  3260. //
  3261. // - An opaque Image.Bitmap can be used (i.e. all pixels have Alpha=255).
  3262. //
  3263. // - Image.Bitmap.DrawMode can be set to dmOpaque (the default).
  3264. //
  3265. // See issue #248
  3266. //
  3267. PaintToMode := True;
  3268. try
  3269. for I := 0 to FPaintStages.Count - 1 do
  3270. if (psmExport in FPaintStages[I].Mask) then
  3271. case FPaintStages[I].Stage of
  3272. PST_CUSTOM: ExecCustom(Dest, I);
  3273. PST_CLEAR_BUFFER: ExecClearBuffer(Dest, I);
  3274. PST_CLEAR_BACKGND: ExecClearBackgnd(Dest, I);
  3275. PST_DRAW_BITMAP: ExecDrawBitmap(Dest, I);
  3276. PST_DRAW_LAYERS: ExecDrawLayers(Dest, I);
  3277. PST_CONTROL_FRAME: ExecControlFrame(Dest, I);
  3278. PST_BITMAP_FRAME: ExecBitmapFrame(Dest, I);
  3279. end;
  3280. finally
  3281. PaintToMode := False;
  3282. end;
  3283. FCacheValid := False;
  3284. RepaintMode := OldRepaintMode;
  3285. end;
  3286. procedure TCustomImage32.Scroll(Dx, Dy: Single);
  3287. begin
  3288. if (IsZero(Dx)) and (IsZero(Dy)) then
  3289. Exit;
  3290. BeginUpdate;
  3291. try
  3292. OffsetHorz := OffsetHorz - Dx;
  3293. OffsetVert := OffsetVert - Dy;
  3294. finally
  3295. EndUpdate;
  3296. end;
  3297. end;
  3298. procedure TCustomImage32.Scroll(Dx, Dy: Integer);
  3299. begin
  3300. if (Dx <> 0) or (Dy <> 0) then
  3301. {$ifndef FPC} // FPC chokes on the float conversion with an exception
  3302. Scroll(Single(Dx), Single(Dy));
  3303. {$else FPC}
  3304. Scroll(Dx * 1.0, Dy * 1.0);
  3305. {$endif FPC}
  3306. end;
  3307. procedure TCustomImage32.ScrollToCenter(X, Y: Integer);
  3308. var
  3309. ViewportRect: TRect;
  3310. begin
  3311. BeginUpdate;
  3312. try
  3313. ViewportRect := GetViewportRect;
  3314. OffsetHorz := ViewportRect.Width * 0.5 - X * Scale;
  3315. OffsetVert := ViewportRect.Height * 0.5 - Y * Scale;
  3316. finally
  3317. EndUpdate;
  3318. end;
  3319. end;
  3320. procedure TCustomImage32.ScrollToCenter;
  3321. begin
  3322. ScrollToCenter(Bitmap.Width div 2, Bitmap.Height div 2);
  3323. end;
  3324. procedure TCustomImage32.SetBackgroundOptions(const Value: TBackgroundOptions);
  3325. begin
  3326. FBackgroundOptions.Assign(Value);
  3327. end;
  3328. procedure TCustomImage32.SetBitmap(Value: TBitmap32);
  3329. begin
  3330. InvalidateCache;
  3331. FBitmap.Assign(Value);
  3332. end;
  3333. procedure TCustomImage32.SetBitmapAlign(Value: TBitmapAlign);
  3334. begin
  3335. InvalidateCache;
  3336. FBitmapAlign := Value;
  3337. Changed;
  3338. end;
  3339. procedure TCustomImage32.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
  3340. begin
  3341. inherited;
  3342. InvalidateCache;
  3343. end;
  3344. procedure TCustomImage32.SetHotTrackLayer(ALayer: TCustomLayer);
  3345. begin
  3346. if (ALayer = FHotLayer) then
  3347. exit;
  3348. if (FHotLayer <> nil) then
  3349. TLayerAccess(FHotLayer).MouseLeave;
  3350. FHotLayer := ALayer;
  3351. if (FHotLayer <> nil) then
  3352. TLayerAccess(FHotLayer).MouseEnter;
  3353. end;
  3354. procedure TCustomImage32.SetLayers(Value: TLayerCollection);
  3355. begin
  3356. FLayers.Assign(Value);
  3357. end;
  3358. procedure TCustomImage32.SetOffsetHorz(Value: TFloat);
  3359. begin
  3360. if Value <> FOffsetHorz then
  3361. begin
  3362. InvalidateCache;
  3363. FOffsetHorz := Value;
  3364. Changed;
  3365. end;
  3366. end;
  3367. procedure TCustomImage32.SetOffsetVert(Value: TFloat);
  3368. begin
  3369. if Value <> FOffsetVert then
  3370. begin
  3371. FOffsetVert := Value;
  3372. InvalidateCache;
  3373. Changed;
  3374. end;
  3375. end;
  3376. procedure TCustomImage32.SetOnPixelCombine(Value: TPixelCombineEvent);
  3377. begin
  3378. FBitmap.OnPixelCombine := Value;
  3379. Changed;
  3380. end;
  3381. procedure TCustomImage32.SetMousePanOptions(const Value: TMousePanOptions);
  3382. begin
  3383. FMousePanOptions.Assign(Value);
  3384. end;
  3385. procedure TCustomImage32.SetMouseZoomOptions(const Value: TMouseZoomOptions);
  3386. begin
  3387. FMouseZoomOptions.Assign(Value);
  3388. end;
  3389. procedure TCustomImage32.SetScale(Value: TFloat);
  3390. begin
  3391. if Value < 0.001 then
  3392. Value := 0.001;
  3393. if Value <> FScaleX then
  3394. begin
  3395. InvalidateCache;
  3396. FScaleX := Value;
  3397. FScaleY := Value;
  3398. CachedScaleX := FScaleX;
  3399. CachedScaleY := FScaleY;
  3400. CachedRecScaleX := 1 / Value;
  3401. CachedRecScaleY := 1 / Value;
  3402. DoScaleChange;
  3403. Changed;
  3404. end;
  3405. end;
  3406. procedure TCustomImage32.SetScaleX(Value: TFloat);
  3407. begin
  3408. if Value < 0.001 then
  3409. Value := 0.001;
  3410. if Value <> FScaleX then
  3411. begin
  3412. InvalidateCache;
  3413. FScaleX := Value;
  3414. CachedScaleX := Value;
  3415. CachedRecScaleX := 1 / Value;
  3416. DoScaleChange;
  3417. Changed;
  3418. end;
  3419. end;
  3420. procedure TCustomImage32.SetScaleY(Value: TFloat);
  3421. begin
  3422. if Value < 0.001 then
  3423. Value := 0.001;
  3424. if Value <> FScaleY then
  3425. begin
  3426. InvalidateCache;
  3427. FScaleY := Value;
  3428. CachedScaleY := Value;
  3429. CachedRecScaleY := 1 / Value;
  3430. DoScaleChange;
  3431. Changed;
  3432. end;
  3433. end;
  3434. procedure TCustomImage32.SetScaleMode(Value: TScaleMode);
  3435. begin
  3436. if Value <> FScaleMode then
  3437. begin
  3438. InvalidateCache;
  3439. FScaleMode := Value;
  3440. Changed;
  3441. end;
  3442. end;
  3443. procedure TCustomImage32.SetupBitmap(DoClear: Boolean = False; ClearColor: TColor32 = $FF000000);
  3444. begin
  3445. BeginUpdate;
  3446. try
  3447. FBitmap.BeginUpdate;
  3448. with GetViewPortRect do
  3449. FBitmap.SetSize(Right - Left, Bottom - Top);
  3450. if DoClear then
  3451. FBitmap.Clear(ClearColor);
  3452. FBitmap.EndUpdate;
  3453. InvalidateCache;
  3454. Changed;
  3455. finally
  3456. EndUpdate;
  3457. end;
  3458. end;
  3459. procedure TCustomImage32.SetXForm(ShiftX, ShiftY, ScaleX, ScaleY: TFloat);
  3460. begin
  3461. CachedShiftX := ShiftX;
  3462. CachedShiftY := ShiftY;
  3463. CachedScaleX := ScaleX;
  3464. CachedScaleY := ScaleY;
  3465. if (ScaleX <> 0) then
  3466. CachedRecScaleX := 1 / ScaleX
  3467. else
  3468. CachedRecScaleX := 0;
  3469. if (ScaleY <> 0) then
  3470. CachedRecScaleY := 1 / ScaleY
  3471. else
  3472. CachedRecScaleY := 0;
  3473. end;
  3474. procedure TCustomImage32.UpdateCache(AForce: boolean);
  3475. begin
  3476. if (FCacheValid) and (not AForce) then
  3477. Exit;
  3478. FCacheValid := True;
  3479. FCachedBitmapRect := GetBitmapRect;
  3480. if Bitmap.Empty then
  3481. SetXForm(0, 0, 1, 1)
  3482. else
  3483. SetXForm(FCachedBitmapRect.Left, FCachedBitmapRect.Top, FCachedBitmapRect.Width / Bitmap.Width, FCachedBitmapRect.Height / Bitmap.Height);
  3484. end;
  3485. procedure TCustomImage32.InvalidateCache;
  3486. begin
  3487. if RepaintOptimizer.Enabled and CacheValid then
  3488. RepaintOptimizer.Reset;
  3489. FCacheValid := False;
  3490. end;
  3491. function TCustomImage32.GetCachedBitmapRect: TRect;
  3492. begin
  3493. UpdateCache;
  3494. Result := FCachedBitmapRect;
  3495. end;
  3496. //------------------------------------------------------------------------------
  3497. //
  3498. // TImageViewScrollProperties
  3499. //
  3500. //------------------------------------------------------------------------------
  3501. constructor TImageViewScrollProperties.Create(AOwner: TCustomImgView32);
  3502. begin
  3503. inherited Create;
  3504. FOwner := AOwner;
  3505. end;
  3506. //------------------------------------------------------------------------------
  3507. procedure TImageViewScrollProperties.SkipValue(Reader: TReader);
  3508. begin
  3509. {$ifndef FPC}
  3510. Reader.SkipValue;
  3511. {$else}
  3512. // Sheez!
  3513. Reader.Driver.SkipValue;
  3514. {$endif}
  3515. end;
  3516. procedure TImageViewScrollProperties.DefineProperties(Filer: TFiler);
  3517. begin
  3518. inherited;
  3519. // Obsolete properties. Skipped so old projects using them doesn't fail during load.
  3520. Filer.DefineProperty('Backgnd', SkipValue, nil, False);
  3521. Filer.DefineProperty('BorderColor', SkipValue, nil, False);
  3522. Filer.DefineProperty('ButtonColor', SkipValue, nil, False);
  3523. Filer.DefineProperty('ButtonSize', SkipValue, nil, False);
  3524. Filer.DefineProperty('Color', SkipValue, nil, False);
  3525. Filer.DefineProperty('HandleColor', SkipValue, nil, False);
  3526. Filer.DefineProperty('HighLightColor', SkipValue, nil, False);
  3527. Filer.DefineProperty('ShadowColor', SkipValue, nil, False);
  3528. Filer.DefineProperty('ShowArrows', SkipValue, nil, False);
  3529. Filer.DefineProperty('ShowHandleGrip', SkipValue, nil, False);
  3530. Filer.DefineProperty('Style', SkipValue, nil, False);
  3531. end;
  3532. //------------------------------------------------------------------------------
  3533. procedure TImageViewScrollProperties.SetIncrement(Value: Integer);
  3534. begin
  3535. if (FIncrement = Value) then
  3536. exit;
  3537. FIncrement := Value;
  3538. end;
  3539. procedure TImageViewScrollProperties.SetSize(Value: Integer);
  3540. begin
  3541. if (FSize = Value) then
  3542. exit;
  3543. FSize := Value;
  3544. FOwner.UpdateOffsets([ocControlSize, ocOffsetHorz, ocOffsetVert]);
  3545. end;
  3546. procedure TImageViewScrollProperties.SetVisibility(const Value: TScrollbarVisibility);
  3547. begin
  3548. if (FVisibility = Value) then
  3549. exit;
  3550. FVisibility := Value;
  3551. FOwner.UpdateOffsets([ocControlSize, ocOffsetHorz, ocOffsetVert]);
  3552. // TODO : Possibly Invalidate needed here so we can draw the resize grip
  3553. end;
  3554. //------------------------------------------------------------------------------
  3555. //
  3556. // TCustomImgView32
  3557. //
  3558. //------------------------------------------------------------------------------
  3559. constructor TCustomImgView32.Create(AOwner: TComponent);
  3560. begin
  3561. BeginOffset;
  3562. try
  3563. inherited;
  3564. FScrollBars := TImageViewScrollProperties.Create(Self);
  3565. FHorScroll := TScrollBar.Create(Self);
  3566. FHorScroll.ControlStyle := FHorScroll.ControlStyle - [csFramed];
  3567. FHorScroll.Visible := False;
  3568. FHorScroll.Parent := Self;
  3569. FHorScroll.Kind := sbHorizontal;
  3570. FHorScroll.OnChange := ScrollHandler; // Changed
  3571. FHorScroll.OnScroll := ScrollChangingHandler; // Changing
  3572. FVerScroll := TScrollBar.Create(Self);
  3573. FVerScroll.Visible := False;
  3574. FVerScroll.Parent := Self;
  3575. FVerScroll.ControlStyle := FVerScroll.ControlStyle - [csFramed];
  3576. FVerScroll.Kind := sbVertical;
  3577. FVerScroll.OnChange := ScrollHandler;
  3578. FVerScroll.OnScroll := ScrollChangingHandler;
  3579. FCentered := True;
  3580. ScaleMode := smScale;
  3581. BitmapAlign := baCustom;
  3582. UpdateOffsets([ocBitmapSize, ocControlSize, ocOffsetHorz, ocOffsetVert]);
  3583. finally
  3584. EndOffset;
  3585. end;
  3586. end;
  3587. destructor TCustomImgView32.Destroy;
  3588. begin
  3589. FreeAndNil(FScrollBars);
  3590. inherited;
  3591. end;
  3592. //------------------------------------------------------------------------------
  3593. procedure TCustomImgView32.BeginOffset;
  3594. begin
  3595. BeginUpdate;
  3596. Inc(FOffsetChangeLock);
  3597. end;
  3598. procedure TCustomImgView32.EndOffset;
  3599. begin
  3600. if (FOffsetChangeLock = 1) and (FOffsetChanges <> []) then
  3601. begin
  3602. DoUpdateOffsets;
  3603. FOffsetChanges := [];
  3604. end;
  3605. Dec(FOffsetChangeLock);
  3606. EndUpdate;
  3607. end;
  3608. procedure TCustomImgView32.UpdateScrollbarVisibility;
  3609. var
  3610. ScrollbarVisible: Boolean;
  3611. ViewPort: TRect;
  3612. ScrollbarSize: integer;
  3613. NeedResize: boolean;
  3614. begin
  3615. if (csReading in ComponentState) then
  3616. exit;
  3617. if (Width <= 0) or (Height <= 0) then
  3618. Exit;
  3619. NeedResize := False;
  3620. ViewPort := GetViewportRect;
  3621. ScrollbarSize := GetScrollbarSize;
  3622. ScrollbarVisible := GetScrollBarsVisible;
  3623. // Block scrollbar.OnChange in case we change their visibility.
  3624. BeginOffset;
  3625. try
  3626. if (FHorScroll <> nil) then
  3627. begin
  3628. FHorScroll.BoundsRect := Rect(ViewPort.Left, ViewPort.Bottom, ViewPort.Right, ViewPort.Bottom+ScrollbarSize);
  3629. if (FHorScroll.Visible <> ScrollbarVisible) then
  3630. begin
  3631. if (ScrollbarVisible) then
  3632. // Scrollbar is being shown; Update its initial position
  3633. FHorScroll.Position := 0;
  3634. FHorScroll.Visible := ScrollbarVisible;
  3635. UpdateOffsets([ocControlSize, ocOffsetHorz]);
  3636. NeedResize := True;
  3637. end;
  3638. end;
  3639. if (FVerScroll <> nil) then
  3640. begin
  3641. FVerScroll.BoundsRect := Rect(ViewPort.Right, ViewPort.Top, ViewPort.Right+ScrollbarSize, ViewPort.Bottom);
  3642. if (FVerScroll.Visible <> ScrollbarVisible) then
  3643. begin
  3644. if (ScrollbarVisible) then
  3645. // Scrollbar is being shown; Update its initial position
  3646. FVerScroll.Position := 0;
  3647. FVerScroll.Visible := ScrollbarVisible;
  3648. UpdateOffsets([ocControlSize, ocOffsetVert]);
  3649. NeedResize := True;
  3650. end;
  3651. end;
  3652. finally
  3653. EndOffset;
  3654. end;
  3655. if (NeedResize) then
  3656. // Scrollbars have been shown or hidden. Buffer must resize to align with new viewport.
  3657. // This will automatically lead to the viewport being redrawn.
  3658. ResizeBuffer;
  3659. end;
  3660. {$if defined(FPC) and(not defined(MSWINDOWS))}
  3661. type
  3662. TFormCracker = class(TCustomForm);
  3663. {$ifend}
  3664. procedure TCustomImgView32.UpdateScrollBar(ScrollBar: TScrollBar; ScrollMax, ScrollThumbSize: integer);
  3665. {$if defined(FPC) and(not defined(MSWINDOWS))}
  3666. var
  3667. Form: TCustomForm;
  3668. {$ifend}
  3669. begin
  3670. if (ScrollBar = nil) or (not ScrollBar.Visible) then
  3671. exit;
  3672. {$if defined(MSWINDOWS)}
  3673. if (ScrollBar.HandleAllocated) then
  3674. SendMessage(ScrollBar.Handle, WM_SETREDRAW, Ord(False), 0);
  3675. {$elseif defined(FPC)}
  3676. Form := GetParentForm(Self, False);
  3677. if (Form <> nil) then
  3678. // BeginFormUpdate/EndFormUpdate doesn't do the same as WM_SETREDRAW at all
  3679. // but I'm keeping it until we have a better alternative.
  3680. TFormCracker(Form).BeginFormUpdate;
  3681. {$ifend}
  3682. try
  3683. ScrollBar.PageSize := 0; // Guard against exception if Max<PageSize
  3684. ScrollBar.Max := ScrollMax;
  3685. ScrollBar.PageSize := ScrollThumbSize;
  3686. ScrollBar.SmallChange := 1;
  3687. ScrollBar.LargeChange := Max(2, Round((ScrollMax-ScrollThumbSize) / 16));
  3688. // Note: The VCL places incorrect constraints on the values of PageSize, Max and Position.
  3689. // The VCL requires PageSize <= Max, but Windows requires PageSize <= Max-Min+1.
  3690. // This means that if we set PageSize=Max then the user will still be able to scroll 1 unit
  3691. // up/down.
  3692. // We work around this here by disabling the scroll bar if PageSize=Max.
  3693. if (ScrollMax = ScrollThumbSize) then
  3694. begin
  3695. ScrollBar.Enabled := False;
  3696. ScrollBar.Position := 0;
  3697. end else
  3698. ScrollBar.Enabled := True;
  3699. finally
  3700. {$if defined(MSWINDOWS)}
  3701. if (ScrollBar.HandleAllocated) then
  3702. SendMessage(ScrollBar.Handle, WM_SETREDRAW, Ord(True), 0);
  3703. {$elseif defined(FPC)}
  3704. if (Form <> nil) then
  3705. TFormCracker(Form).EndFormUpdate;
  3706. {$ifend}
  3707. end;
  3708. {$if defined(MSWINDOWS)}
  3709. if (ScrollBar.HandleAllocated) then
  3710. RedrawWindow(ScrollBar.Handle, nil, 0, RDW_INVALIDATE);
  3711. {$ifend}
  3712. end;
  3713. procedure TCustomImgView32.BitmapResized;
  3714. begin
  3715. inherited;
  3716. UpdateOffsets([ocBitmapSize]);
  3717. end;
  3718. function TCustomImgView32.CanMousePan: boolean;
  3719. begin
  3720. // Unhandled case:
  3721. // - BitmapAlign=baCustom, Centered=False: Image can be panned out of viewport
  3722. Result := (inherited CanMousePan) and
  3723. (ScaleMode in [smScale, smNormal]) and
  3724. ((FViewportSize.cx < FBitmapSize.cx) or (FViewportSize.cy < FBitmapSize.cy));
  3725. end;
  3726. procedure TCustomImgView32.DoDrawSizeGrip(R: TRect);
  3727. {$if (not defined(FPC)) and defined(MSWINDOWS)}
  3728. var
  3729. ThemedElementDetails: TThemedElementDetails;
  3730. {$ifend}
  3731. begin
  3732. {$if defined(MSWINDOWS)}
  3733. Canvas.Brush.Color := clBtnFace;
  3734. Canvas.FillRect(R);
  3735. {$if (not defined(FPC))}
  3736. ThemedElementDetails := StyleServices.GetElementDetails(tsSizeBoxRightAlign);
  3737. if (StyleServices.DrawElement(Canvas.Handle, ThemedElementDetails, R)) then
  3738. exit;
  3739. DrawFrameControl(Canvas.Handle, R, DFC_SCROLL, DFCS_SCROLLSIZEGRIP)
  3740. {$else}
  3741. // Doesn't appear to work. I can't be bothered to figure out why.
  3742. // ThemedElementDetails := ThemeServices.GetElementDetails(tsSizeBoxRightAlign);
  3743. // ThemeServices.DrawElement(Canvas.Handle, ThemedElementDetails, R);
  3744. DrawFrameControl(Canvas.Handle, R, DFC_SCROLL, DFCS_SCROLLSIZEGRIP)
  3745. {$ifend}
  3746. {$ifend}
  3747. end;
  3748. procedure TCustomImgView32.DoScaleChange;
  3749. begin
  3750. inherited;
  3751. InvalidateCache;
  3752. BeginOffset;
  3753. try
  3754. // Constrain offsets
  3755. SetOffsetHorz(OffsetHorz);
  3756. SetOffsetVert(OffsetVert);
  3757. UpdateOffsets([ocScale]);
  3758. finally
  3759. EndOffset;
  3760. end;
  3761. Invalidate;
  3762. end;
  3763. procedure TCustomImgView32.DoScroll;
  3764. begin
  3765. if Assigned(FOnScroll) then
  3766. FOnScroll(Self);
  3767. end;
  3768. procedure TCustomImgView32.DoSetPivot(const APivot: TFloatPoint);
  3769. begin
  3770. inherited;
  3771. end;
  3772. function TCustomImgView32.GetBitmapMargin: integer;
  3773. begin
  3774. Result := FOverSize;
  3775. end;
  3776. function TCustomImgView32.GetOuterScaledBitmapSize: TSize;
  3777. var
  3778. BitmapMargin: Integer;
  3779. begin
  3780. Result := GetBitmapSize;
  3781. BitmapMargin := 2*GetBitmapMargin;
  3782. Inc(Result.cx, BitmapMargin);
  3783. Inc(Result.cy, BitmapMargin);
  3784. end;
  3785. function TCustomImgView32.GetScrollBarSize: Integer;
  3786. begin
  3787. if (CanShowScrollBars) then
  3788. begin
  3789. Result := FScrollBars.Size;
  3790. if (Result = 0) then
  3791. Result := GetSystemMetrics(SM_CYHSCROLL);
  3792. end else
  3793. Result := 0;
  3794. end;
  3795. function TCustomImgView32.CanShowScrollBars: Boolean;
  3796. begin
  3797. Result := (FScrollBars <> nil) and (FHorScroll <> nil) and (FVerScroll <> nil) and
  3798. (FScrollBars.Visibility <> svHidden) and
  3799. (BitmapAlign = baCustom) and
  3800. (ScaleMode in [smNormal, smScale]);
  3801. end;
  3802. function TCustomImgView32.GetScrollBarsVisible: Boolean;
  3803. begin
  3804. if (AutoSize) then // This doesn't seem right
  3805. Exit(False);
  3806. if (not CanShowScrollBars) then
  3807. Exit(False);
  3808. if (FScrollBars.Visibility = svAlways) then
  3809. Exit(True);
  3810. Assert(FScrollBars.Visibility = svAuto);
  3811. Result := (FViewportSize.cx < FBitmapSize.cx) or (FViewportSize.cy < FBitmapSize.cy);
  3812. end;
  3813. function TCustomImgView32.GetSizeGripRect: TRect;
  3814. var
  3815. ScrollBarSize: Integer;
  3816. begin
  3817. if (Parent = nil) then
  3818. Result := BoundsRect
  3819. else
  3820. Result := ClientRect;
  3821. ScrollBarSize := GetScrollBarSize;
  3822. Result.Left := Result.Right - ScrollBarSize;
  3823. Result.Top := Result.Bottom - ScrollBarSize;
  3824. end;
  3825. function TCustomImgView32.GetViewportRect: TRect;
  3826. var
  3827. ScrollBarSize: Integer;
  3828. begin
  3829. Result := inherited;
  3830. if (GetScrollBarsVisible) then
  3831. begin
  3832. ScrollBarSize := GetScrollBarSize;
  3833. Dec(Result.Right, ScrollBarSize);
  3834. Dec(Result.Bottom, ScrollBarSize);
  3835. end;
  3836. end;
  3837. function TCustomImgView32.IsSizeGripVisible: Boolean;
  3838. {$IFNDEF PLATFORM_INDEPENDENT}
  3839. var
  3840. P: TWinControl;
  3841. {$ENDIF}
  3842. begin
  3843. {$IFNDEF PLATFORM_INDEPENDENT}
  3844. case SizeGrip of
  3845. sgAuto:
  3846. begin
  3847. Result := False;
  3848. if (Align <> alClient) then
  3849. Exit;
  3850. P := Parent;
  3851. while True do
  3852. begin
  3853. if P is TCustomForm then
  3854. begin
  3855. Result := True;
  3856. Break;
  3857. end else
  3858. if (P = nil) or (P.Align <> alClient) then
  3859. Exit;
  3860. P := P.Parent;
  3861. end;
  3862. end;
  3863. sgNone:
  3864. Result := False
  3865. else { sgAlways }
  3866. Result := True;
  3867. end;
  3868. {$ELSE}
  3869. Result := False;
  3870. {$ENDIF}
  3871. end;
  3872. procedure TCustomImgView32.Loaded;
  3873. begin
  3874. BeginOffset;
  3875. try
  3876. Recenter;
  3877. UpdateOffsets([ocScale, ocBitmapSize, ocControlSize]);
  3878. finally
  3879. EndOffset;
  3880. end;
  3881. UpdateCache(True);
  3882. inherited;
  3883. end;
  3884. procedure TCustomImgView32.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  3885. {$if defined(MSWINDOWS)}
  3886. var
  3887. P: TPoint;
  3888. Action: Cardinal;
  3889. Msg: TMessage;
  3890. {$ifend}
  3891. begin
  3892. {$if defined(MSWINDOWS)}
  3893. P.X := X;
  3894. P.Y := Y;
  3895. if IsSizeGripVisible and (Owner is TCustomForm) and GR32.PtInRect(GetSizeGripRect, P) then
  3896. begin
  3897. Action := HTBOTTOMRIGHT;
  3898. Application.ProcessMessages;
  3899. Msg.Msg := WM_NCLBUTTONDOWN;
  3900. Msg.WParam := Action;
  3901. SetCaptureControl(nil);
  3902. SendMessage(TCustomForm(Owner).Handle, Msg.Msg, Msg.wParam, Msg.lParam);
  3903. Exit;
  3904. end;
  3905. {$ifend}
  3906. inherited;
  3907. end;
  3908. procedure TCustomImgView32.MouseMove(Shift: TShiftState; X, Y: Integer);
  3909. {$IFNDEF PLATFORM_INDEPENDENT}
  3910. var
  3911. P: TPoint;
  3912. {$ENDIF}
  3913. begin
  3914. inherited;
  3915. {$IFNDEF PLATFORM_INDEPENDENT}
  3916. if IsSizeGripVisible then
  3917. begin
  3918. P.X := X;
  3919. P.Y := Y;
  3920. if GR32.PtInRect(GetSizeGripRect, P) then
  3921. Screen.Cursor := crSizeNWSE;
  3922. end;
  3923. {$ENDIF}
  3924. end;
  3925. procedure TCustomImgView32.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer; Layer: TCustomLayer);
  3926. begin
  3927. inherited;
  3928. end;
  3929. procedure TCustomImgView32.Paint;
  3930. begin
  3931. PaintSizeGrip;
  3932. inherited;
  3933. end;
  3934. procedure TCustomImgView32.PaintSizeGrip;
  3935. var
  3936. SizeGripRect: TRect;
  3937. begin
  3938. if (Parent = nil) then
  3939. Exit;
  3940. if (GetScrollBarsVisible) then
  3941. begin
  3942. SizeGripRect := GetSizeGripRect;
  3943. {$IFNDEF PLATFORM_INDEPENDENT}
  3944. if IsSizeGripVisible then
  3945. DoDrawSizeGrip(SizeGripRect)
  3946. else
  3947. {$ENDIF}
  3948. if (not SizeGripRect.IsEmpty) then
  3949. begin
  3950. Canvas.Brush.Color := clBtnFace;
  3951. Canvas.FillRect(SizeGripRect);
  3952. end;
  3953. end;
  3954. end;
  3955. procedure TCustomImgView32.Scroll(Dx, Dy: Single);
  3956. begin
  3957. BeginOffset;
  3958. try
  3959. inherited;
  3960. finally
  3961. EndOffset;
  3962. end;
  3963. end;
  3964. procedure TCustomImgView32.ScrollHandler(Sender: TObject);
  3965. begin
  3966. if (FOffsetChangeLock > 0) then
  3967. // Scrollbars are being synced with offsets
  3968. Exit;
  3969. TControl(Sender).Repaint;
  3970. // User is using scrollbars to scroll; Update offsets based on scrollbars
  3971. UpdateOffsets([ocScrollBars]);
  3972. DoScroll;
  3973. Repaint;
  3974. end;
  3975. procedure TCustomImgView32.ScrollChangingHandler(Sender: TObject; ScrollCode: TScrollCode; var ScrollPos: Integer);
  3976. begin
  3977. // The Constrain below is a work around for a bug the VCL TScrollBar; It misinterprets
  3978. // the valid Position range as [Min..Max] while in reality it is [Min..Max-PageSize+1].
  3979. // So when TScrollBar Position = Max-PageSize Windows still allows the scrollbar
  3980. // position to be increased.
  3981. if (Sender = FHorScroll) then
  3982. ScrollPos := Constrain(ScrollPos, 0, FHorScroll.Max-FHorScroll.PageSize)
  3983. else
  3984. if (Sender = FVerScroll) then
  3985. ScrollPos := Constrain(ScrollPos, 0, FVerScroll.Max - FVerScroll.PageSize);
  3986. end;
  3987. procedure TCustomImgView32.ScrollToCenter(X, Y: Integer);
  3988. begin
  3989. BeginOffset;
  3990. try
  3991. inherited;
  3992. finally
  3993. EndOffset;
  3994. end;
  3995. end;
  3996. procedure TCustomImgView32.Recenter;
  3997. var
  3998. Margin: integer;
  3999. begin
  4000. InvalidateCache;
  4001. BeginOffset;
  4002. try
  4003. UpdateOffsets([ocBitmapSize]);
  4004. if FCentered then
  4005. ScrollToCenter
  4006. else
  4007. begin
  4008. Margin := GetBitmapMargin;
  4009. OffsetHorz := Margin;
  4010. OffsetVert := Margin;
  4011. end;
  4012. finally
  4013. EndOffset;
  4014. end;
  4015. end;
  4016. procedure TCustomImgView32.Resize;
  4017. begin
  4018. inherited; // Calls OnResize event handler
  4019. if (csReading in ComponentState) or (FHorScroll = nil) or (FVerScroll = nil) then
  4020. exit;
  4021. // Repaint size grip immediately so it doesn't lag behind if we are dragging it
  4022. PaintSizeGrip;
  4023. BeginOffset;
  4024. try
  4025. InvalidateCache;
  4026. UpdateOffsets([ocControlSize]);
  4027. if FCentered then
  4028. UpdateOffsets([ocOffsetHorz, ocOffsetVert]); // Center or maintain offset to center
  4029. // if FCentered then
  4030. // ScrollToCenter;
  4031. finally
  4032. EndOffset;
  4033. end;
  4034. Invalidate;
  4035. end;
  4036. procedure TCustomImgView32.SetCentered(Value: Boolean);
  4037. begin
  4038. FCentered := Value;
  4039. Recenter;
  4040. end;
  4041. procedure TCustomImgView32.SetOffsetHorz(Value: TFloat);
  4042. var
  4043. Margin: integer;
  4044. begin
  4045. if (FBitmapSize.cx > FViewportSize.cx) then
  4046. begin
  4047. Margin := GetBitmapMargin;
  4048. Value := Margin - Constrain(Margin - Value, 0, FBitmapSize.cx - FViewportSize.cx);
  4049. end;
  4050. if (Value <> OffsetHorz) then
  4051. begin
  4052. inherited;
  4053. UpdateOffsets([ocOffsetHorz]);
  4054. end;
  4055. end;
  4056. procedure TCustomImgView32.SetOffsetVert(Value: TFloat);
  4057. var
  4058. Margin: integer;
  4059. begin
  4060. if (FBitmapSize.cy > FViewportSize.cy) then
  4061. begin
  4062. Margin := GetBitmapMargin;
  4063. Value := Margin - Constrain(Margin - Value, 0, FBitmapSize.cy - FViewportSize.cy);
  4064. end;
  4065. if (Value <> OffsetVert) then
  4066. begin
  4067. inherited;
  4068. UpdateOffsets([ocOffsetVert]);
  4069. end;
  4070. end;
  4071. procedure TCustomImgView32.SetOverSize(const Value: Integer);
  4072. begin
  4073. if Value <> FOverSize then
  4074. begin
  4075. FOverSize := Value;
  4076. Invalidate;
  4077. UpdateOffsets([ocBitmapSize]);
  4078. // Recenter;
  4079. end;
  4080. end;
  4081. procedure TCustomImgView32.SetScrollBars(Value: TImageViewScrollProperties);
  4082. begin
  4083. FScrollBars.Assign(Value);
  4084. end;
  4085. procedure TCustomImgView32.SetSizeGrip(Value: TSizeGripStyle);
  4086. begin
  4087. if Value <> FSizeGrip then
  4088. begin
  4089. FSizeGrip := Value;
  4090. Invalidate;
  4091. end;
  4092. end;
  4093. procedure TCustomImgView32.UpdateOffsets(OffsetChanges: TOffsetChanges);
  4094. begin
  4095. BeginOffset;
  4096. FOffsetChanges := FOffsetChanges + OffsetChanges;
  4097. EndOffset;
  4098. end;
  4099. procedure TCustomImgView32.DoUpdateOffsets;
  4100. var
  4101. ViewportRect: TRect;
  4102. OldBitmapSize: TSize;
  4103. OldViewportSize: TSize;
  4104. InnerBitmapSize: TSize;
  4105. BitmapMargin: Integer;
  4106. Center: Single;
  4107. begin
  4108. if (csReading in ComponentState) then
  4109. exit;
  4110. if (FOffsetChanges = []) then
  4111. exit;
  4112. OldBitmapSize := FBitmapSize;
  4113. OldViewportSize := FViewportSize;
  4114. FBitmapSize := GetOuterScaledBitmapSize;
  4115. InnerBitmapSize := GetBitmapSize;
  4116. BitmapMargin := GetBitmapMargin;
  4117. ViewportRect := GetViewportRect;
  4118. FViewportSize.cx := ViewportRect.Width;
  4119. FViewportSize.cy := ViewportRect.Height;
  4120. BeginOffset;
  4121. try
  4122. if ([ocScale, ocBitmapSize, ocControlSize] * FOffsetChanges <> []) then
  4123. begin
  4124. // - If Visibility=svAuto then the ranges of the scrollbars may just have
  4125. // changed, thus we need to update the visibility of the scrollbars.
  4126. // - If the control is resize we need to reposition the scrollbars.
  4127. // - If the scollbars has been hidden/shown we need to update them.
  4128. UpdateScrollbarVisibility;
  4129. UpdateScrollBar(FHorScroll, FBitmapSize.cx, Min(FBitmapSize.cx, FViewportSize.cx));
  4130. UpdateScrollBar(FVerScroll, FBitmapSize.cy, Min(FBitmapSize.cy, FViewportSize.cy));
  4131. end;
  4132. if Centered then
  4133. begin
  4134. if (FViewportSize.cx >= FBitmapSize.cx) then // Viewport is bigger than scaled Bitmap
  4135. // No scrollbar; Center
  4136. OffsetHorz := (FViewportSize.cx - InnerBitmapSize.cx) * 0.5
  4137. else
  4138. begin
  4139. if ([ocBitmapSize, ocScale, ocControlSize] * FOffsetChanges <> []) then
  4140. begin
  4141. if (OldBitmapSize.cx <> 2*BitmapMargin) then
  4142. // Maintain relative offset from center
  4143. Center := (-OffsetHorz + OldViewportSize.cx * 0.5) / (OldBitmapSize.cx-2*BitmapMargin)
  4144. else
  4145. Center := 0.5;
  4146. OffsetHorz := FViewportSize.cx * 0.5 - InnerBitmapSize.cx * Center;
  4147. end;
  4148. if (ocOffsetHorz in FOffsetChanges) then
  4149. begin
  4150. // Offset has changed; Update scollbar
  4151. if (FHorScroll.Visible) then
  4152. FHorScroll.Position := Round(BitmapMargin - OffsetHorz);
  4153. end else
  4154. if (ocScrollBars in FOffsetChanges) then
  4155. // User has scrolled; Update offset
  4156. OffsetHorz := -FHorScroll.Position + BitmapMargin;
  4157. end;
  4158. if (FViewportSize.cy >= FBitmapSize.cy) then // Viewport is bigger than scaled Bitmap
  4159. // No scrollbar; Center
  4160. OffsetVert := (FViewportSize.cy - InnerBitmapSize.cy) * 0.5
  4161. else
  4162. begin
  4163. if ([ocBitmapSize, ocScale, ocControlSize] * FOffsetChanges <> []) then
  4164. begin
  4165. if (OldBitmapSize.cx <> 2*BitmapMargin) then
  4166. // Maintain relative offset from center
  4167. Center := (-OffsetVert + OldViewportSize.cy * 0.5) / (OldBitmapSize.cy-2*BitmapMargin)
  4168. else
  4169. Center := 0.5;
  4170. OffsetVert := FViewportSize.cy * 0.5 - InnerBitmapSize.cy * Center;
  4171. end;
  4172. if (ocOffsetVert in FOffsetChanges) then
  4173. begin
  4174. // Offset has changed; Update scollbar
  4175. if (FVerScroll.Visible) then
  4176. FVerScroll.Position := Round(BitmapMargin - OffsetVert);
  4177. end else
  4178. if (ocScrollBars in FOffsetChanges) then
  4179. // User has scrolled; Update offset
  4180. OffsetVert := -FVerScroll.Position + BitmapMargin;
  4181. end;
  4182. end else
  4183. begin
  4184. // Offset has changed; Update scollbars
  4185. if (ocOffsetHorz in FOffsetChanges) then
  4186. FHorScroll.Position := Round(BitmapMargin - OffsetHorz);
  4187. if (ocOffsetVert in FOffsetChanges) then
  4188. FVerScroll.Position := Round(BitmapMargin - OffsetVert);
  4189. if (ocScrollBars in FOffsetChanges) then
  4190. begin
  4191. // User has scrolled; Update offsets
  4192. OffsetHorz := -FHorScroll.Position + BitmapMargin;
  4193. OffsetVert := -FVerScroll.Position + BitmapMargin;
  4194. end;
  4195. end;
  4196. if (FOffsetChanges * [ocOffsetHorz, ocOffsetVert] <> []) then
  4197. Invalidate;
  4198. finally
  4199. EndOffset;
  4200. end;
  4201. end;
  4202. procedure TCustomImgView32.SetScaleMode(Value: TScaleMode);
  4203. begin
  4204. inherited;
  4205. Recenter;
  4206. end;
  4207. //------------------------------------------------------------------------------
  4208. //
  4209. // TBitmap32Item
  4210. //
  4211. //------------------------------------------------------------------------------
  4212. procedure TBitmap32Item.AssignTo(Dest: TPersistent);
  4213. begin
  4214. if Dest is TBitmap32Item then
  4215. TBitmap32Item(Dest).Bitmap.Assign(Bitmap)
  4216. else
  4217. inherited;
  4218. end;
  4219. constructor TBitmap32Item.Create(Collection: TCollection);
  4220. begin
  4221. inherited;
  4222. FBitmap := TBitmap32.Create;
  4223. end;
  4224. destructor TBitmap32Item.Destroy;
  4225. begin
  4226. FBitmap.Free;
  4227. inherited;
  4228. end;
  4229. procedure TBitmap32Item.SetBitmap(ABitmap: TBitmap32);
  4230. begin
  4231. FBitmap.Assign(ABitmap)
  4232. end;
  4233. //------------------------------------------------------------------------------
  4234. //
  4235. // TBitmap32Collection
  4236. //
  4237. //------------------------------------------------------------------------------
  4238. function TBitmap32Collection.Add: TBitmap32Item;
  4239. begin
  4240. Result := TBitmap32Item(inherited Add);
  4241. end;
  4242. constructor TBitmap32Collection.Create(AOwner: TPersistent; ItemClass: TBitmap32ItemClass);
  4243. begin
  4244. inherited Create(ItemClass);
  4245. FOwner := AOwner;
  4246. end;
  4247. function TBitmap32Collection.GetItem(Index: Integer): TBitmap32Item;
  4248. begin
  4249. Result := TBitmap32Item(inherited GetItem(Index));
  4250. end;
  4251. function TBitmap32Collection.GetOwner: TPersistent;
  4252. begin
  4253. Result := FOwner;
  4254. end;
  4255. procedure TBitmap32Collection.SetItem(Index: Integer; Value: TBitmap32Item);
  4256. begin
  4257. inherited SetItem(Index, Value);
  4258. end;
  4259. //------------------------------------------------------------------------------
  4260. //
  4261. // TBitmap32List
  4262. //
  4263. //------------------------------------------------------------------------------
  4264. constructor TBitmap32List.Create(AOwner: TComponent);
  4265. begin
  4266. inherited;
  4267. FBitmap32Collection := TBitmap32Collection.Create(Self, TBitmap32Item);
  4268. end;
  4269. destructor TBitmap32List.Destroy;
  4270. begin
  4271. FBitmap32Collection.Free;
  4272. inherited;
  4273. end;
  4274. function TBitmap32List.GetBitmap(Index: Integer): TBitmap32;
  4275. begin
  4276. Result := FBitmap32Collection.Items[Index].Bitmap;
  4277. end;
  4278. procedure TBitmap32List.SetBitmap(Index: Integer; Value: TBitmap32);
  4279. begin
  4280. FBitmap32Collection.Items[Index].Bitmap := Value;
  4281. end;
  4282. procedure TBitmap32List.SetBitmap32Collection(Value: TBitmap32Collection);
  4283. begin
  4284. FBitmap32Collection := Value;
  4285. end;
  4286. //------------------------------------------------------------------------------
  4287. //
  4288. // TMousePanOptions
  4289. //
  4290. //------------------------------------------------------------------------------
  4291. constructor TMousePanOptions.Create;
  4292. begin
  4293. inherited Create;
  4294. FMouseButton := mbLeft;
  4295. FPanCursor := crSizeAll;
  4296. end;
  4297. function TMousePanOptions.MatchShiftState(AShiftState: TShiftState): boolean;
  4298. begin
  4299. {$ifdef FPC}
  4300. {-$push}
  4301. {-$packset 4} // The FPC TShiftState does not fit in a word
  4302. Result := (DWord(AShiftState * [ssShift, ssAlt, ssCtrl]) = DWord(Byte(FShiftState)));
  4303. {-$pop}
  4304. {$else FPC}
  4305. Result := (Word(AShiftState * [ssShift, ssAlt, ssCtrl]) = Word(Byte(FShiftState)));
  4306. {$endif FPC}
  4307. end;
  4308. //------------------------------------------------------------------------------
  4309. //
  4310. // TMouseZoomOptions
  4311. //
  4312. //------------------------------------------------------------------------------
  4313. constructor TMouseZoomOptions.Create;
  4314. begin
  4315. inherited Create;
  4316. FSteps := 12;
  4317. FMinScale := 0.0625;
  4318. FMaxScale := 128.0;
  4319. FMaintainPivot := True;
  4320. UpdateZoomFactor;
  4321. end;
  4322. procedure TMouseZoomOptions.SetSteps(const Value: integer);
  4323. begin
  4324. FSteps := Min(100, Max(2, Value));
  4325. UpdateZoomFactor;
  4326. end;
  4327. procedure TMouseZoomOptions.SetZoomFactor(const Value: Double);
  4328. begin
  4329. FZoomFactor := Min(100, Max(1.001, Value));
  4330. // Calculate steps and recalculate FZoomFactor
  4331. Steps := Ceil(Ln(FMaxScale / FMinScale) / Ln(FZoomFactor)) + 1;
  4332. end;
  4333. procedure TMouseZoomOptions.UpdateZoomFactor;
  4334. begin
  4335. FZoomFactor := Power(FMaxScale / FMinScale, 1/(FSteps-1));
  4336. end;
  4337. function TMouseZoomOptions.IsMaxScaleStored: Boolean;
  4338. begin
  4339. Result := (FMinScale <> 0.0625);
  4340. end;
  4341. function TMouseZoomOptions.IsMinScaleStored: Boolean;
  4342. begin
  4343. Result := (FMaxScale <> 128.0);
  4344. end;
  4345. function TMouseZoomOptions.LevelToScale(ALevel: integer): Single;
  4346. var
  4347. LogMinZoom: Double;
  4348. LogMaxZoom: Double;
  4349. LogZoom: Double;
  4350. begin
  4351. ALevel := Max(0, Min(ALevel, FSteps-1));
  4352. // Work in log space...
  4353. LogMinZoom := Ln(FMinScale);
  4354. LogMaxZoom := Ln(FMaxScale);
  4355. // Linear interpolation in log space:
  4356. LogZoom := LogMinZoom + (LogMaxZoom-LogMinZoom) / (FSteps-1) * ALevel;
  4357. // Back to linear space
  4358. Result := Exp(LogZoom);
  4359. end;
  4360. function TMouseZoomOptions.MatchShiftState(AShiftState: TShiftState): boolean;
  4361. begin
  4362. {$ifdef FPC}
  4363. {-$push}
  4364. {-$packset 4} // The FPC TShiftState does not fit in a word
  4365. Result := (DWord(AShiftState * [ssShift, ssAlt, ssCtrl]) = DWord(Byte(FShiftState)));
  4366. {-$pop}
  4367. {$else FPC}
  4368. Result := (Word(AShiftState * [ssShift, ssAlt, ssCtrl]) = Word(Byte(FShiftState)));
  4369. {$endif FPC}
  4370. end;
  4371. function TMouseZoomOptions.ScaleToLevel(AScale: Single): integer;
  4372. var
  4373. LogMinZoom: Double;
  4374. LogMaxZoom: Double;
  4375. Step: Double;
  4376. begin
  4377. AScale := Max(FMinScale, Min(AScale, FMaxScale));
  4378. // Work in log space...
  4379. LogMinZoom := Ln(FMinScale);
  4380. LogMaxZoom := Ln(FMaxScale);
  4381. // Linear interpolation in log space:
  4382. Step := (FSteps-1) / (LogMaxZoom-LogMinZoom) * (Ln(AScale)-LogMinZoom);
  4383. // Back to linear space
  4384. Result := Ceil(Step);
  4385. Result := Max(0, Min(Result, FSteps-1));
  4386. end;
  4387. procedure TMouseZoomOptions.SetMaxScale(const Value: Single);
  4388. begin
  4389. FMaxScale := Max(FMinScale+0.001, Value);
  4390. UpdateZoomFactor;
  4391. end;
  4392. procedure TMouseZoomOptions.SetMinScale(const Value: Single);
  4393. begin
  4394. FMinScale := Min(FMaxScale-0.002, Max(0.001, Value));
  4395. UpdateZoomFactor;
  4396. end;
  4397. //------------------------------------------------------------------------------
  4398. //------------------------------------------------------------------------------
  4399. //------------------------------------------------------------------------------
  4400. end.