| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060 | 
							- //---------------------------------------------------------------------------
 
- #include <vcl.h>
 
- #pragma hdrstop
 
- #include <shlobj.h>
 
- #include <mshtmhst.h>
 
- #include <Common.h>
 
- #include "GUITools.h"
 
- #include "WinConfiguration.h"
 
- #include <TextsCore.h>
 
- #include <TextsWin.h>
 
- #include <CoreMain.h>
 
- #include <SessionData.h>
 
- #include <WinInterface.h>
 
- #include <TBXUtils.hpp>
 
- #include <Math.hpp>
 
- #include <Tools.h>
 
- #include "PngImageList.hpp"
 
- #include <StrUtils.hpp>
 
- #include <limits>
 
- #include <Glyphs.h>
 
- #include <PasTools.hpp>
 
- #include <VCLCommon.h>
 
- #include <WinApi.h>
 
- #include <HistoryComboBox.hpp>
 
- #include <vssym32.h>
 
- #include <DateUtils.hpp>
 
- #include <IOUtils.hpp>
 
- #include <Queue.h>
 
- #include <Vcl.Themes.hpp>
 
- #include "Animations96.h"
 
- #include "Animations120.h"
 
- #include "Animations144.h"
 
- #include "Animations192.h"
 
- //---------------------------------------------------------------------------
 
- #pragma package(smart_init)
 
- //---------------------------------------------------------------------------
 
- extern const UnicodeString PageantTool = L"pageant.exe";
 
- extern const UnicodeString PuttygenTool = L"puttygen.exe";
 
- //---------------------------------------------------------------------------
 
- bool __fastcall FindFile(UnicodeString & Path)
 
- {
 
-   bool Result = FileExistsFix(Path);
 
-   if (!Result)
 
-   {
 
-     UnicodeString ProgramFiles32 = IncludeTrailingBackslash(GetEnvironmentVariable(L"ProgramFiles"));
 
-     UnicodeString ProgramFiles64 = IncludeTrailingBackslash(GetEnvironmentVariable(L"ProgramW6432"));
 
-     if (!ProgramFiles32.IsEmpty() &&
 
-         SameText(Path.SubString(1, ProgramFiles32.Length()), ProgramFiles32) &&
 
-         !ProgramFiles64.IsEmpty())
 
-     {
 
-       UnicodeString Path64 =
 
-         ProgramFiles64 + Path.SubString(ProgramFiles32.Length() + 1, Path.Length() - ProgramFiles32.Length());
 
-       if (FileExists(ApiPath(Path64)))
 
-       {
 
-         Path = Path64;
 
-         Result = true;
 
-       }
 
-     }
 
-   }
 
-   if (!Result && SameText(ExtractFileName(Path), Path))
 
-   {
 
-     UnicodeString Paths = GetEnvironmentVariable(L"PATH");
 
-     if (!Paths.IsEmpty())
 
-     {
 
-       UnicodeString NewPath = FileSearch(Path, Paths);
 
-       Result = !NewPath.IsEmpty();
 
-       if (Result)
 
-       {
 
-         Path = NewPath;
 
-       }
 
-       else
 
-       {
 
-         // Basically the same what FileSearch does, except for FileExistsFix.
 
-         // Once this proves working, we can ged rid of the FileSearch call.
 
-         while (!Result && !Paths.IsEmpty())
 
-         {
 
-           UnicodeString P = CutToChar(Paths, L';', false);
 
-           // Not using CombinePaths as it throws on an invalid path and PATH is not under our control
 
-           NewPath = IncludeTrailingBackslash(P) + Path;
 
-           Result = FileExistsFix(NewPath);
 
-           if (Result)
 
-           {
 
-             Path = NewPath;
 
-           }
 
-         }
 
-       }
 
-     }
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- bool DoesSessionExistInPutty(const UnicodeString & StorageKey)
 
- {
 
-   std::unique_ptr<TRegistryStorage> Storage(new TRegistryStorage(Configuration->PuttySessionsKey));
 
-   Storage->ConfigureForPutty();
 
-   return Storage->OpenRootKey(true) && Storage->KeyExists(StorageKey);
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall ExportSessionToPutty(TSessionData * SessionData, bool ReuseExisting, const UnicodeString & SessionName)
 
- {
 
-   bool Result = true;
 
-   std::unique_ptr<TRegistryStorage> Storage(new TRegistryStorage(Configuration->PuttySessionsKey));
 
-   Storage->AccessMode = smReadWrite;
 
-   Storage->ConfigureForPutty();
 
-   if (Storage->OpenRootKey(true))
 
-   {
 
-     Result = ReuseExisting && Storage->KeyExists(SessionData->StorageKey);
 
-     if (Result)
 
-     {
 
-       AppLogFmt(L"Reusing existing PuTTY session: %s", (SessionData->StorageKey));
 
-     }
 
-     else
 
-     {
 
-       std::unique_ptr<TRegistryStorage> SourceStorage(new TRegistryStorage(Configuration->PuttySessionsKey));
 
-       SourceStorage->ConfigureForPutty();
 
-       if (SourceStorage->OpenSubKey(StoredSessions->DefaultSettings->Name, false) &&
 
-           Storage->OpenSubKey(SessionName, true))
 
-       {
 
-         Storage->Copy(SourceStorage.get());
 
-         Storage->CloseSubKey();
 
-       }
 
-       std::unique_ptr<TSessionData> ExportData(new TSessionData(L""));
 
-       ExportData->Assign(SessionData);
 
-       ExportData->Modified = true;
 
-       ExportData->Name = SessionName;
 
-       ExportData->WinTitle = SessionData->SessionName;
 
-       ExportData->Password = L"";
 
-       if (SessionData->FSProtocol == fsFTP)
 
-       {
 
-         if (GUIConfiguration->TelnetForFtpInPutty)
 
-         {
 
-           ExportData->PuttyProtocol = PuttyTelnetProtocol;
 
-           ExportData->PortNumber = TelnetPortNumber;
 
-         }
 
-         else
 
-         {
 
-           ExportData->PuttyProtocol = PuttySshProtocol;
 
-           ExportData->PortNumber = SshPortNumber;
 
-         }
 
-       }
 
-       ExportData->Save(Storage.get(), true);
 
-       AppLogFmt(L"Exported site settings to PuTTY session: %s", (SessionName));
 
-     }
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- class TPuttyCleanupThread : public TSimpleThread
 
- {
 
- public:
 
-   static void Schedule();
 
-   static void Finalize();
 
- protected:
 
-   virtual void __fastcall Execute();
 
-   virtual void __fastcall Terminate();
 
-   bool __fastcall Finished();
 
-   void DoSchedule();
 
- private:
 
-   TDateTime FTimer;
 
-   static TPuttyCleanupThread * FInstance;
 
-   static std::unique_ptr<TCriticalSection> FSection;
 
- };
 
- //---------------------------------------------------------------------------
 
- std::unique_ptr<TCriticalSection> TPuttyCleanupThread::FSection(TraceInitPtr(new TCriticalSection()));
 
- TPuttyCleanupThread * TPuttyCleanupThread::FInstance;
 
- //---------------------------------------------------------------------------
 
- void TPuttyCleanupThread::Schedule()
 
- {
 
-   TGuard Guard(FSection.get());
 
-   if (FInstance == NULL)
 
-   {
 
-     FInstance = new TPuttyCleanupThread();
 
-     FInstance->DoSchedule();
 
-     FInstance->Start();
 
-   }
 
-   else
 
-   {
 
-     FInstance->DoSchedule();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void TPuttyCleanupThread::Finalize()
 
- {
 
-   while (true)
 
-   {
 
-     {
 
-       TGuard Guard(FSection.get());
 
-       if (FInstance == NULL)
 
-       {
 
-         return;
 
-       }
 
-     }
 
-     Sleep(100);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TPuttyCleanupThread::Execute()
 
- {
 
-   try
 
-   {
 
-     std::unique_ptr<TStrings> Sessions(new TStringList());
 
-     bool Continue = true;
 
-     do
 
-     {
 
-       {
 
-         TGuard Guard(FSection.get());
 
-         std::unique_ptr<TRegistryStorage> Storage(new TRegistryStorage(Configuration->PuttySessionsKey));
 
-         Storage->AccessMode = smReadWrite;
 
-         Storage->ConfigureForPutty();
 
-         std::unique_ptr<TStringList> Sessions2(new TStringList());
 
-         if (Storage->OpenRootKey(true))
 
-         {
 
-           std::unique_ptr<TStrings> SubKeys(new TStringList());
 
-           Storage->GetSubKeyNames(SubKeys.get());
 
-           for (int Index = 0; Index < SubKeys->Count; Index++)
 
-           {
 
-             UnicodeString SessionName = SubKeys->Strings[Index];
 
-             if (StartsStr(GUIConfiguration->PuttySession, SessionName))
 
-             {
 
-               Sessions2->Add(SessionName);
 
-             }
 
-           }
 
-           Sessions2->Sort();
 
-           if (!Sessions->Equals(Sessions2.get()))
 
-           {
 
-             // Just in case new sessions from another WinSCP instance are added, delay the cleanup
 
-             // (to avoid having to implement some inter-process communication).
 
-             // Both instances will attempt to do the cleanup, but that not a problem
 
-             Sessions->Assign(Sessions2.get());
 
-             DoSchedule();
 
-           }
 
-         }
 
-         if (FTimer < Now())
 
-         {
 
-           for (int Index = 0; Index < Sessions->Count; Index++)
 
-           {
 
-             UnicodeString SessionName = Sessions->Strings[Index];
 
-             Storage->RecursiveDeleteSubKey(SessionName);
 
-           }
 
-           Continue = false;
 
-         }
 
-       }
 
-       if (Continue)
 
-       {
 
-         Sleep(1000);
 
-       }
 
-     }
 
-     while (Continue);
 
-   }
 
-   __finally
 
-   {
 
-     TGuard Guard(FSection.get());
 
-     FInstance = NULL;
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TPuttyCleanupThread::Terminate()
 
- {
 
-   DebugFail();
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TPuttyCleanupThread::Finished()
 
- {
 
-   // self-destroy
 
-   return TSimpleThread::Finished() || true;
 
- }
 
- //---------------------------------------------------------------------------
 
- void TPuttyCleanupThread::DoSchedule()
 
- {
 
-   FTimer = IncSecond(Now(), 10);
 
- }
 
- //---------------------------------------------------------------------------
 
- class TPuttyPasswordThread : public TSimpleThread
 
- {
 
- public:
 
-   TPuttyPasswordThread(const UnicodeString & Password, const UnicodeString & PipeName);
 
-   virtual __fastcall ~TPuttyPasswordThread();
 
- protected:
 
-   virtual void __fastcall Execute();
 
-   virtual void __fastcall Terminate();
 
-   virtual bool __fastcall Finished();
 
- private:
 
-   HANDLE FPipe;
 
-   AnsiString FPassword;
 
-   void DoSleep(int & Timeout);
 
- };
 
- //---------------------------------------------------------------------------
 
- TPuttyPasswordThread::TPuttyPasswordThread(const UnicodeString & Password, const UnicodeString & PipeName)
 
- {
 
-   DWORD OpenMode = PIPE_ACCESS_OUTBOUND | FILE_FLAG_FIRST_PIPE_INSTANCE;
 
-   DWORD PipeMode = PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_NOWAIT | PIPE_REJECT_REMOTE_CLIENTS;
 
-   DWORD BufferSize = 16 * 1024;
 
-   FPipe = CreateNamedPipe(PipeName.c_str(), OpenMode, PipeMode, 1, BufferSize, BufferSize, NMPWAIT_USE_DEFAULT_WAIT, NULL);
 
-   if (FPipe == INVALID_HANDLE_VALUE)
 
-   {
 
-     throw EOSExtException(L"Cannot create password pipe");
 
-   }
 
-   FPassword = AnsiString(Password);
 
-   Start();
 
- }
 
- //---------------------------------------------------------------------------
 
- __fastcall TPuttyPasswordThread::~TPuttyPasswordThread()
 
- {
 
-   DebugAssert(FFinished);
 
-   AppLog(L"Disconnecting and closing password pipe");
 
-   DisconnectNamedPipe(FPipe);
 
-   CloseHandle(FPipe);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TPuttyPasswordThread::Terminate()
 
- {
 
-   // noop - the thread always self-terminates
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TPuttyPasswordThread::Finished()
 
- {
 
-   return true;
 
- }
 
- //---------------------------------------------------------------------------
 
- void TPuttyPasswordThread::DoSleep(int & Timeout)
 
- {
 
-   unsigned int Step = 50;
 
-   Sleep(Step);
 
-   Timeout -= Step;
 
-   if (Timeout <= 0)
 
-   {
 
-     AppLog(L"Timeout waiting for PuTTY");
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TPuttyPasswordThread::Execute()
 
- {
 
-   AppLog(L"Waiting for PuTTY to connect to password pipe");
 
-   int Timeout = MSecsPerSec * SecsPerMin;
 
-   while (Timeout > 0)
 
-   {
 
-     if (ConnectNamedPipe(FPipe, NULL))
 
-     {
 
-       AppLog(L"Password pipe is ready");
 
-     }
 
-     else
 
-     {
 
-       int Error = GetLastError();
 
-       if (Error == ERROR_PIPE_CONNECTED)
 
-       {
 
-         AppLog(L"PuTTY has connected to password pipe");
 
-         int Pos = 0;
 
-         while ((Timeout > 0) && (Pos < FPassword.Length()))
 
-         {
 
-           DWORD Written = 0;
 
-           if (!WriteFile(FPipe, FPassword.c_str() + Pos, FPassword.Length() - Pos, &Written, NULL))
 
-           {
 
-             AppLog(L"Error writting password pipe");
 
-             Timeout = 0;
 
-           }
 
-           else
 
-           {
 
-             Pos += Written;
 
-             if (Pos >= FPassword.Length())
 
-             {
 
-               FlushFileBuffers(FPipe);
 
-               AppLog(L"Complete password was written to pipe");
 
-               Timeout = 0;
 
-             }
 
-             else
 
-             {
 
-               AppLog(L"Part of password was written to pipe");
 
-               DoSleep(Timeout);
 
-             }
 
-           }
 
-         }
 
-       }
 
-       else if (Error == ERROR_PIPE_LISTENING)
 
-       {
 
-         DoSleep(Timeout);
 
-       }
 
-       else
 
-       {
 
-         AppLogFmt(L"Password pipe error %d", (Error));
 
-         Timeout = 0;
 
-       }
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void SplitPuttyCommand(UnicodeString & Program, UnicodeString & Params)
 
- {
 
-   // See also TSiteAdvancedDialog::PuttySettingsButtonClick
 
-   UnicodeString Dir;
 
-   SplitCommand(GUIConfiguration->PuttyPath, Program, Params, Dir);
 
-   Program = ExpandEnvironmentVariables(Program);
 
-   Params = ExpandEnvironmentVariables(Params);
 
- }
 
- //---------------------------------------------------------------------------
 
- UnicodeString FindPuttyPath()
 
- {
 
-   UnicodeString Program, Params;
 
-   SplitPuttyCommand(Program, Params);
 
-   if (!FindFile(Program))
 
-   {
 
-     throw Exception(FMTLOAD(EXECUTE_APP_ERROR, (Program)));
 
-   }
 
-   return Program;
 
- }
 
- //---------------------------------------------------------------------------
 
- static unsigned int PipeCounter = 0;
 
- //---------------------------------------------------------------------------
 
- void OpenSessionInPutty(TSessionData * SessionData)
 
- {
 
-   // putty does not support resolving environment variables in session settings
 
-   SessionData->ExpandEnvironmentVariables();
 
-   UnicodeString Program, AParams;
 
-   SplitPuttyCommand(Program, AParams);
 
-   AppLogFmt(L"PuTTY program: %s", (Program));
 
-   AppLogFmt(L"Params: %s", (AParams));
 
-   if (FindFile(Program))
 
-   {
 
-     UnicodeString Password;
 
-     if (GUIConfiguration->PuttyPassword)
 
-     {
 
-       // Passphrase has precendence, as it's more likely entered by user during authentication, hence more likely really needed.
 
-       if (!SessionData->Passphrase.IsEmpty())
 
-       {
 
-         Password = SessionData->Passphrase;
 
-       }
 
-       else if (!SessionData->Password.IsEmpty())
 
-       {
 
-         Password = SessionData->Password;
 
-       }
 
-     }
 
-     TCustomCommandData Data(SessionData, SessionData->UserName, Password);
 
-     TLocalCustomCommand LocalCustomCommand(Data, SessionData->RemoteDirectory, SessionData->LocalDirectory);
 
-     TWinInteractiveCustomCommand InteractiveCustomCommand(
 
-       &LocalCustomCommand, L"PuTTY", UnicodeString());
 
-     UnicodeString Params =
 
-       LocalCustomCommand.Complete(InteractiveCustomCommand.Complete(AParams, false), true);
 
-     UnicodeString PuttyParams;
 
-     AppLogFmt(L"Expanded params: %s", (Params));
 
-     if (!LocalCustomCommand.IsSiteCommand(AParams))
 
-     {
 
-       {
 
-         bool SessionList = false;
 
-         std::unique_ptr<THierarchicalStorage> SourceHostKeyStorage(Configuration->CreateScpStorage(SessionList));
 
-         std::unique_ptr<THierarchicalStorage> TargetHostKeyStorage(new TRegistryStorage(Configuration->PuttyRegistryStorageKey));
 
-         TargetHostKeyStorage->Explicit = true;
 
-         TargetHostKeyStorage->AccessMode = smReadWrite;
 
-         std::unique_ptr<TStoredSessionList> HostKeySessionList(new TStoredSessionList());
 
-         HostKeySessionList->OwnsObjects = false;
 
-         HostKeySessionList->Add(SessionData);
 
-         int Imported = TStoredSessionList::ImportHostKeys(SourceHostKeyStorage.get(), TargetHostKeyStorage.get(), HostKeySessionList.get(), false);
 
-         AppLogFmt(L"Imported host keys: %d", (Imported));
 
-       }
 
-       if (IsUWP())
 
-       {
 
-         bool Telnet = (SessionData->FSProtocol == fsFTP) && GUIConfiguration->TelnetForFtpInPutty;
 
-         if (Telnet)
 
-         {
 
-           AddToList(PuttyParams, L"-telnet", L" ");
 
-           // PuTTY  does not allow -pw for telnet
 
-           Password = L"";
 
-         }
 
-         AddToList(PuttyParams, EscapePuttyCommandParam(SessionData->HostName), L" ");
 
-         if (!SessionData->UserName.IsEmpty())
 
-         {
 
-           AddToList(PuttyParams, FORMAT(L"-l %s", (EscapePuttyCommandParam(SessionData->UserName))), L" ");
 
-         }
 
-         if ((SessionData->FSProtocol != fsFTP) && (SessionData->PortNumber != SshPortNumber))
 
-         {
 
-           AddToList(PuttyParams, FORMAT(L"-P %d", (SessionData->PortNumber)), L" ");
 
-         }
 
-         if (!Telnet)
 
-         {
 
-           UnicodeString PublicKeyFile = SessionData->ResolvePublicKeyFile();
 
-           if (!PublicKeyFile.IsEmpty())
 
-           {
 
-             AddToList(PuttyParams, FORMAT(L"-i \"%s\"", (PublicKeyFile)), L" ");
 
-           }
 
-           AddToList(PuttyParams, (SessionData->TryAgent ? L"-agent" : L"-noagent"), L" ");
 
-           if (SessionData->TryAgent)
 
-           {
 
-             AddToList(PuttyParams, (SessionData->AgentFwd ? L"-A" : L"-a"), L" ");
 
-           }
 
-           if (SessionData->Compression)
 
-           {
 
-             AddToList(PuttyParams, L"-C", L" ");
 
-           }
 
-           AddToList(PuttyParams, L"-2", L" ");
 
-           if (!SessionData->LogicalHostName.IsEmpty())
 
-           {
 
-             AddToList(PuttyParams, FORMAT(L"-loghost \"%s\"", (SessionData->LogicalHostName)), L" ");
 
-           }
 
-         }
 
-         if (SessionData->AddressFamily == afIPv4)
 
-         {
 
-           AddToList(PuttyParams, L"-4", L" ");
 
-         }
 
-         else if (SessionData->AddressFamily == afIPv6)
 
-         {
 
-           AddToList(PuttyParams, L"-6", L" ");
 
-         }
 
-         AppLogFmt(L"Using command-line instead of stored session in UWP: %s", (PuttyParams));
 
-       }
 
-       else
 
-       {
 
-         UnicodeString SessionName;
 
-         UnicodeString PuttySession = GUIConfiguration->PuttySession;
 
-         int Uniq = 1;
 
-         while (DoesSessionExistInPutty(PuttySession))
 
-         {
 
-           Uniq++;
 
-           PuttySession = FORMAT(L"%s (%d)", (GUIConfiguration->PuttySession, Uniq));
 
-         }
 
-         if (ExportSessionToPutty(SessionData, true, PuttySession))
 
-         {
 
-           SessionName = SessionData->SessionName;
 
-         }
 
-         else
 
-         {
 
-           SessionName = PuttySession;
 
-           TPuttyCleanupThread::Schedule();
 
-           if ((SessionData->FSProtocol == fsFTP) &&
 
-               GUIConfiguration->TelnetForFtpInPutty)
 
-           {
 
-             // PuTTY  does not allow -pw for telnet
 
-             Password = L"";
 
-           }
 
-         }
 
-         UnicodeString LoadSwitch = L"-load";
 
-         int P = Params.LowerCase().Pos(LoadSwitch + L" ");
 
-         if ((P == 0) || ((P > 1) && (Params[P - 1] != L' ')))
 
-         {
 
-           AddToList(PuttyParams, FORMAT(L"%s %s", (LoadSwitch, EscapePuttyCommandParam(SessionName))), L" ");
 
-         }
 
-       }
 
-     }
 
-     if (!Password.IsEmpty() && !LocalCustomCommand.IsPasswordCommand(AParams))
 
-     {
 
-       Password = NormalizeString(Password); // if password is empty, we should quote it always
 
-       bool UsePuttyPwFile;
 
-       if (GUIConfiguration->UsePuttyPwFile == asAuto)
 
-       {
 
-         UsePuttyPwFile = false;
 
-         if (SameText(ExtractFileName(Program), OriginalPuttyExecutable))
 
-         {
 
-           unsigned int Version = GetFileVersion(Program);
 
-           if (Version != static_cast<unsigned int>(-1))
 
-           {
 
-             int MajorVersion = HIWORD(Version);
 
-             int MinorVersion = LOWORD(Version);
 
-             if (CalculateCompoundVersion(MajorVersion, MinorVersion) >= CalculateCompoundVersion(0, 77))
 
-             {
 
-               UsePuttyPwFile = true;
 
-             }
 
-           }
 
-         }
 
-       }
 
-       else
 
-       {
 
-         UsePuttyPwFile = (GUIConfiguration->UsePuttyPwFile == asOn);
 
-       }
 
-       UnicodeString PasswordParam;
 
-       if (UsePuttyPwFile)
 
-       {
 
-         PipeCounter++;
 
-         UnicodeString PipeName = FORMAT(L"\\\\.\\PIPE\\WinSCPPuTTYPassword.%.8x.%.8x.%.4x", (GetCurrentProcessId(), PipeCounter, rand()));
 
-         new TPuttyPasswordThread(Password, PipeName);
 
-         PasswordParam = FORMAT(L"-pwfile \"%s\"", (PipeName));
 
-       }
 
-       else
 
-       {
 
-         PasswordParam = FORMAT(L"-pw %s", (EscapePuttyCommandParam(Password)));
 
-       }
 
-       AddToList(PuttyParams, PasswordParam, L" ");
 
-     }
 
-     AddToList(PuttyParams, Params, L" ");
 
-     // PuTTY is started in its binary directory to allow relative paths in private key,
 
-     // when opening PuTTY's own stored session.
 
-     ExecuteShellChecked(Program, PuttyParams, true);
 
-   }
 
-   else
 
-   {
 
-     throw Exception(FMTLOAD(FILE_NOT_FOUND, (Program)));
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall FindTool(const UnicodeString & Name, UnicodeString & Path)
 
- {
 
-   UnicodeString AppPath = IncludeTrailingBackslash(ExtractFilePath(Application->ExeName));
 
-   Path = AppPath + Name;
 
-   bool Result = true;
 
-   if (!FileExists(ApiPath(Path)))
 
-   {
 
-     Path = AppPath + L"PuTTY\\" + Name;
 
-     if (!FileExists(ApiPath(Path)))
 
-     {
 
-       Path = Name;
 
-       if (!FindFile(Path))
 
-       {
 
-         Result = false;
 
-       }
 
-     }
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall ExecuteTool(const UnicodeString & Name)
 
- {
 
-   UnicodeString Path;
 
-   if (!FindTool(Name, Path))
 
-   {
 
-     throw Exception(FMTLOAD(EXECUTE_APP_ERROR, (Name)));
 
-   }
 
-   ExecuteShellChecked(Path, L"");
 
- }
 
- //---------------------------------------------------------------------------
 
- TObjectList * StartCreationDirectoryMonitorsOnEachDrive(unsigned int Filter, TFileChangedEvent OnChanged)
 
- {
 
-   std::unique_ptr<TStrings> Drives(new TStringList());
 
-   std::unique_ptr<TStrings> DDDrives(CommaTextToStringList(WinConfiguration->DDDrives));
 
-   UnicodeString ExcludedDrives;
 
-   for (int Index = 0; Index < DDDrives->Count; Index++)
 
-   {
 
-     UnicodeString S = Trim(DDDrives->Strings[Index]);
 
-     if (!S.IsEmpty() && (S[1] == L'-'))
 
-     {
 
-       S = Trim(S.SubString(2, S.Length() - 1));
 
-       if (!S.IsEmpty())
 
-       {
 
-         ExcludedDrives += S[1];
 
-       }
 
-     }
 
-     else
 
-     {
 
-       Drives->Add(S);
 
-     }
 
-   }
 
-   for (char Drive = FirstDrive; Drive <= LastDrive; Drive++)
 
-   {
 
-     if (ExcludedDrives.Pos(Drive) == 0)
 
-     {
 
-       // Not calling ReadDriveStatus(... dsSize), relying on drive ready status cached by the background thread
 
-       TDriveInfoRec * DriveInfoRec = DriveInfo->Get(Drive);
 
-       if (DriveInfoRec->Valid && DriveInfoRec->DriveReady &&
 
-           (DriveInfoRec->DriveType != DRIVE_CDROM) &&
 
-           ((DriveInfoRec->DriveType != DRIVE_REMOVABLE) || (Drive >= DriveInfo->FirstFixedDrive)))
 
-       {
 
-         Drives->Add(Drive);
 
-       }
 
-     }
 
-   }
 
-   std::unique_ptr<TObjectList> Result(new TObjectList());
 
-   for (int Index = 0; Index < Drives->Count; Index++)
 
-   {
 
-     UnicodeString Drive = Drives->Strings[Index];
 
-     std::unique_ptr<TDirectoryMonitor> Monitor(new TDirectoryMonitor(Application));
 
-     try
 
-     {
 
-       Monitor->Path = DriveInfo->GetDriveRoot(Drive);
 
-       Monitor->WatchSubtree = true;
 
-       Monitor->WatchFilters = Filter;
 
-       Monitor->OnCreated = OnChanged;
 
-       Monitor->OnModified = OnChanged;
 
-       Monitor->Active = true;
 
-       Result->Add(Monitor.release());
 
-     }
 
-     catch (Exception & E)
 
-     {
 
-       // Ignore errors watching not-ready drives
 
-       DebugUsedParam(E);
 
-     }
 
-   }
 
-   return Result.release();
 
- }
 
- //---------------------------------------------------------------------------
 
- bool DontCopyCommandToClipboard = false;
 
- //---------------------------------------------------------------------------
 
- bool __fastcall CopyCommandToClipboard(const UnicodeString & Command)
 
- {
 
-   bool Result = !DontCopyCommandToClipboard && UseAlternativeFunction() && IsKeyPressed(VK_CONTROL);
 
-   if (Result)
 
-   {
 
-     TInstantOperationVisualizer Visualizer;
 
-     AppLogFmt(L"Copied command to the clipboard: %s", (Command));
 
-     CopyToClipboard(Command);
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- static bool __fastcall DoExecuteShell(const UnicodeString Path, const UnicodeString Params,
 
-   bool ChangeWorkingDirectory, HANDLE * Handle)
 
- {
 
-   bool Result = CopyCommandToClipboard(FormatCommand(Path, Params));
 
-   if (Result)
 
-   {
 
-     if (Handle != NULL)
 
-     {
 
-       *Handle = NULL;
 
-     }
 
-   }
 
-   else
 
-   {
 
-     UnicodeString Directory = ExtractFilePath(Path);
 
-     TShellExecuteInfoW ExecuteInfo;
 
-     memset(&ExecuteInfo, 0, sizeof(ExecuteInfo));
 
-     ExecuteInfo.cbSize = sizeof(ExecuteInfo);
 
-     ExecuteInfo.fMask =
 
-       SEE_MASK_FLAG_NO_UI |
 
-       FLAGMASK((Handle != NULL), SEE_MASK_NOCLOSEPROCESS);
 
-     ExecuteInfo.hwnd = Application->Handle;
 
-     ExecuteInfo.lpFile = static_cast<const wchar_t *>(Path.data());
 
-     ExecuteInfo.lpParameters = static_cast<const wchar_t *>(Params.data());
 
-     ExecuteInfo.lpDirectory = (ChangeWorkingDirectory ? Directory.c_str() : NULL);
 
-     ExecuteInfo.nShow = SW_SHOW;
 
-     AppLogFmt(L"Executing program \"%s\" with params: %s", (Path, Params));
 
-     Result = (ShellExecuteEx(&ExecuteInfo) != 0);
 
-     if (Result)
 
-     {
 
-       if (Handle != NULL)
 
-       {
 
-         *Handle = ExecuteInfo.hProcess;
 
-       }
 
-     }
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall ExecuteShellChecked(const UnicodeString Path, const UnicodeString Params, bool ChangeWorkingDirectory)
 
- {
 
-   if (!DoExecuteShell(Path, Params, ChangeWorkingDirectory, NULL))
 
-   {
 
-     throw EOSExtException(FMTLOAD(EXECUTE_APP_ERROR, (Path)));
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall ExecuteShellChecked(const UnicodeString Command)
 
- {
 
-   UnicodeString Program, Params, Dir;
 
-   SplitCommand(Command, Program, Params, Dir);
 
-   ExecuteShellChecked(Program, Params);
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall ExecuteShell(const UnicodeString Path, const UnicodeString Params,
 
-   HANDLE & Handle)
 
- {
 
-   return DoExecuteShell(Path, Params, false, &Handle);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall ExecuteShellCheckedAndWait(const UnicodeString Command,
 
-   TProcessMessagesEvent ProcessMessages)
 
- {
 
-   UnicodeString Program, Params, Dir;
 
-   SplitCommand(Command, Program, Params, Dir);
 
-   HANDLE ProcessHandle;
 
-   bool Result = DoExecuteShell(Program, Params, false, &ProcessHandle);
 
-   if (!Result)
 
-   {
 
-     throw EOSExtException(FMTLOAD(EXECUTE_APP_ERROR, (Program)));
 
-   }
 
-   else
 
-   {
 
-     if (ProcessHandle != NULL) // only if command was copied to clipboard only
 
-     {
 
-       if (ProcessMessages != NULL)
 
-       {
 
-         unsigned long WaitResult;
 
-         do
 
-         {
 
-           // Same as in ExecuteProcessAndReadOutput
 
-           WaitResult = WaitForSingleObject(ProcessHandle, 50);
 
-           if (WaitResult == WAIT_FAILED)
 
-           {
 
-             throw Exception(LoadStr(DOCUMENT_WAIT_ERROR));
 
-           }
 
-           ProcessMessages();
 
-         }
 
-         while (WaitResult == WAIT_TIMEOUT);
 
-       }
 
-       else
 
-       {
 
-         WaitForSingleObject(ProcessHandle, INFINITE);
 
-       }
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall SpecialFolderLocation(int PathID, UnicodeString & Path)
 
- {
 
-   LPITEMIDLIST Pidl;
 
-   wchar_t Buf[256];
 
-   if (SHGetSpecialFolderLocation(NULL, PathID, &Pidl) == NO_ERROR &&
 
-       SHGetPathFromIDList(Pidl, Buf))
 
-   {
 
-     Path = UnicodeString(Buf);
 
-     return true;
 
-   }
 
-   return false;
 
- }
 
- //---------------------------------------------------------------------------
 
- UnicodeString __fastcall UniqTempDir(const UnicodeString BaseDir, const UnicodeString Identity,
 
-   bool Mask)
 
- {
 
-   DebugAssert(!BaseDir.IsEmpty());
 
-   UnicodeString TempDir;
 
-   do
 
-   {
 
-     TempDir = IncludeTrailingBackslash(BaseDir) + Identity;
 
-     if (Mask)
 
-     {
 
-       TempDir += L"?????";
 
-     }
 
-     else
 
-     {
 
-       TempDir += IncludeTrailingBackslash(FormatDateTime(L"nnzzz", Now()));
 
-     }
 
-   }
 
-   while (!Mask && DirectoryExists(ApiPath(TempDir)));
 
-   return TempDir;
 
- }
 
- //---------------------------------------------------------------------------
 
- class TSessionColors : public TComponent
 
- {
 
- public:
 
-   __fastcall TSessionColors(TComponent * Owner) : TComponent(Owner)
 
-   {
 
-     Name = QualifiedClassName();
 
-   }
 
-   static TSessionColors * __fastcall Retrieve(TComponent * Component)
 
-   {
 
-     TSessionColors * SessionColors = dynamic_cast<TSessionColors *>(Component->FindComponent(QualifiedClassName()));
 
-     if (SessionColors == NULL)
 
-     {
 
-       SessionColors = new TSessionColors(Component);
 
-     }
 
-     return SessionColors;
 
-   }
 
-   typedef std::map<TColor, int> TColorMap;
 
-   TColorMap ColorMap;
 
- };
 
- //---------------------------------------------------------------------------
 
- int __fastcall GetSessionColorImage(
 
-   TCustomImageList * ImageList, TColor Color, int MaskIndex)
 
- {
 
-   TSessionColors * SessionColors = TSessionColors::Retrieve(ImageList);
 
-   int Result;
 
-   TSessionColors::TColorMap::const_iterator I = SessionColors->ColorMap.find(Color);
 
-   if (I != SessionColors->ColorMap.end())
 
-   {
 
-     Result = I->second;
 
-   }
 
-   else
 
-   {
 
-     // This overly complex drawing is here to support color button on SiteAdvanced
 
-     // dialog. There we use plain TImageList, instead of TPngImageList,
 
-     // TButton does not work with transparent images
 
-     // (not even TBitmap with Transparent = true)
 
-     std::unique_ptr<TBitmap> MaskBitmap(new TBitmap());
 
-     ImageList->GetBitmap(MaskIndex, MaskBitmap.get());
 
-     std::unique_ptr<TPngImage> MaskImage(new TPngImage());
 
-     MaskImage->Assign(MaskBitmap.get());
 
-     std::unique_ptr<TPngImage> ColorImage(new TPngImage(COLOR_RGB, 16, ImageList->Width, ImageList->Height));
 
-     TColor MaskTransparentColor = MaskImage->Pixels[0][0];
 
-     TColor TransparentColor = MaskTransparentColor;
 
-     // Expecting that the color to be replaced is in the centre of the image (HACK)
 
-     TColor MaskColor = MaskImage->Pixels[ImageList->Width / 2][ImageList->Height / 2];
 
-     for (int Y = 0; Y < ImageList->Height; Y++)
 
-     {
 
-       for (int X = 0; X < ImageList->Width; X++)
 
-       {
 
-         TColor SourceColor = MaskImage->Pixels[X][Y];
 
-         TColor DestColor;
 
-         // this branch is pointless as long as MaskTransparentColor and
 
-         // TransparentColor are the same
 
-         if (SourceColor == MaskTransparentColor)
 
-         {
 
-           DestColor = TransparentColor;
 
-         }
 
-         else if (SourceColor == MaskColor)
 
-         {
 
-           DestColor = Color;
 
-         }
 
-         else
 
-         {
 
-           DestColor = SourceColor;
 
-         }
 
-         ColorImage->Pixels[X][Y] = DestColor;
 
-       }
 
-     }
 
-     std::unique_ptr<TBitmap> Bitmap(new TBitmap());
 
-     Bitmap->SetSize(ImageList->Width, ImageList->Height);
 
-     ColorImage->AssignTo(Bitmap.get());
 
-     Result = ImageList->AddMasked(Bitmap.get(), TransparentColor);
 
-     SessionColors->ColorMap.insert(std::make_pair(Color, Result));
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall RegenerateSessionColorsImageList(TCustomImageList * ImageList, int MaskIndex)
 
- {
 
-   TSessionColors * SessionColors = TSessionColors::Retrieve(ImageList);
 
-   std::vector<TColor> Colors;
 
-   size_t FixedImages = static_cast<size_t>(ImageList->Count);
 
-   Colors.resize(FixedImages + SessionColors->ColorMap.size());
 
-   TSessionColors::TColorMap::const_iterator I = SessionColors->ColorMap.begin();
 
-   while (I != SessionColors->ColorMap.end())
 
-   {
 
-     DebugAssert(Colors[I->second] == TColor());
 
-     Colors[I->second] = I->first;
 
-     I++;
 
-   }
 
-   TSessionColors::TColorMap ColorMap = SessionColors->ColorMap;
 
-   SessionColors->ColorMap.clear();
 
-   for (size_t Index = 0; Index < Colors.size(); Index++)
 
-   {
 
-     bool IsFixedImageIndex = (Index < FixedImages);
 
-     DebugAssert((Colors[Index] == TColor()) == IsFixedImageIndex);
 
-     if (!IsFixedImageIndex)
 
-     {
 
-       GetSessionColorImage(ImageList, Colors[Index], MaskIndex);
 
-     }
 
-   }
 
-   DebugAssert(SessionColors->ColorMap == ColorMap);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall SetSubmenu(TTBXCustomItem * Item, bool Enable)
 
- {
 
-   class TTBXPublicItem : public TTBXCustomItem
 
-   {
 
-   public:
 
-     __property ItemStyle;
 
-   };
 
-   TTBXPublicItem * PublicItem = reinterpret_cast<TTBXPublicItem *>(Item);
 
-   DebugAssert(PublicItem != NULL);
 
-   // See TTBItemViewer.IsPtInButtonPart (called from TTBItemViewer.MouseDown)
 
-   if (Enable)
 
-   {
 
-     PublicItem->ItemStyle = PublicItem->ItemStyle << tbisSubmenu;
 
-   }
 
-   else
 
-   {
 
-     PublicItem->ItemStyle = PublicItem->ItemStyle >> tbisSubmenu;
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall IsEligibleForApplyingTabs(
 
-   UnicodeString Line, int & TabPos, UnicodeString & Start, UnicodeString & Remaining)
 
- {
 
-   bool Result = false;
 
-   TabPos = Line.Pos(L"\t");
 
-   if (TabPos > 0)
 
-   {
 
-     Remaining = Line.SubString(TabPos + 1, Line.Length() - TabPos);
 
-     // WORKAROUND
 
-     // Some translations still use obsolete hack of consecutive tabs to aling the contents.
 
-     // Delete these, so that the following check does not fail on this
 
-     while (Remaining.SubString(1, 1) == L"\t")
 
-     {
 
-       Remaining.Delete(1, 1);
 
-     }
 
-     // We do not have, not support, mutiple tabs on a single line
 
-     if (DebugAlwaysTrue(Remaining.Pos(L"\t") == 0))
 
-     {
 
-       Start = Line.SubString(1, TabPos - 1);
 
-       // WORKAROUND
 
-       // Previously we padded the string before tab with spaces,
 
-       // to aling the contents across multiple lines
 
-       Start = Start.TrimRight();
 
-       // at least two normal spaces for separation
 
-       Start += L"  ";
 
-       Result = true;
 
-     }
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- static int __fastcall CalculateWidthByLength(UnicodeString Text, void * /*Arg*/)
 
- {
 
-   return Text.Length();
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall ApplyTabs(
 
-   UnicodeString & Text, wchar_t Padding,
 
-   TCalculateWidth CalculateWidth, void * CalculateWidthArg)
 
- {
 
-   if (CalculateWidth == NULL)
 
-   {
 
-     DebugAssert(CalculateWidthArg == NULL);
 
-     CalculateWidth = CalculateWidthByLength;
 
-   }
 
-   std::unique_ptr<TStringList> Lines(TextToStringList(Text));
 
-   int MaxWidth = -1;
 
-   for (int Index = 0; Index < Lines->Count; Index++)
 
-   {
 
-     UnicodeString Line = Lines->Strings[Index];
 
-     int TabPos;
 
-     UnicodeString Start;
 
-     UnicodeString Remaining;
 
-     if (IsEligibleForApplyingTabs(Line, TabPos, Start, Remaining))
 
-     {
 
-       int Width = CalculateWidth(Start, CalculateWidthArg);
 
-       MaxWidth = Max(MaxWidth, Width);
 
-     }
 
-   }
 
-   // Optimization and also to prevent potential regression for texts without tabs
 
-   if (MaxWidth >= 0)
 
-   {
 
-     for (int Index = 0; Index < Lines->Count; Index++)
 
-     {
 
-       UnicodeString Line = Lines->Strings[Index];
 
-       int TabPos;
 
-       UnicodeString Start;
 
-       UnicodeString Remaining;
 
-       if (IsEligibleForApplyingTabs(Line, TabPos, Start, Remaining))
 
-       {
 
-         int Width;
 
-         int Iterations = 0;
 
-         while ((Width = CalculateWidth(Start, CalculateWidthArg)) < MaxWidth)
 
-         {
 
-           int Wider = CalculateWidth(Start + Padding, CalculateWidthArg);
 
-           // If padded string is wider than max width by more pixels
 
-           // than non-padded string is shorter than max width
 
-           if ((Wider > MaxWidth) && ((Wider - MaxWidth) > (MaxWidth - Width)))
 
-           {
 
-             break;
 
-           }
 
-           Start += Padding;
 
-           Iterations++;
 
-           // In rare case a tab is zero-width with some strange font (like HYLE)
 
-           if (Iterations > 100)
 
-           {
 
-             break;
 
-           }
 
-         }
 
-         Lines->Strings[Index] = Start + Remaining;
 
-       }
 
-     }
 
-     Text = Lines->Text;
 
-     // remove trailing newline
 
-     Text = Text.TrimRight();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- static void __fastcall DoSelectScaledImageList(TImageList * ImageList)
 
- {
 
-   TImageList * MatchingList = NULL;
 
-   int MachingPixelsPerInch = 0;
 
-   int PixelsPerInch = LargerPixelsPerInch(GetComponentPixelsPerInch(ImageList), WinConfiguration->LargerToolbar);
 
-   for (int Index = 0; Index < ImageList->Owner->ComponentCount; Index++)
 
-   {
 
-     TImageList * OtherList = dynamic_cast<TImageList *>(ImageList->Owner->Components[Index]);
 
-     if ((OtherList != NULL) &&
 
-         (OtherList != ImageList) &&
 
-         StartsStr(ImageList->Name, OtherList->Name))
 
-     {
 
-       UnicodeString OtherListPixelsPerInchStr =
 
-         OtherList->Name.SubString(
 
-           ImageList->Name.Length() + 1, OtherList->Name.Length() - ImageList->Name.Length());
 
-       int OtherListPixelsPerInch = StrToInt(OtherListPixelsPerInchStr);
 
-       if ((OtherListPixelsPerInch <= PixelsPerInch) &&
 
-           ((MatchingList == NULL) ||
 
-            (MachingPixelsPerInch < OtherListPixelsPerInch)))
 
-       {
 
-         MatchingList = OtherList;
 
-         MachingPixelsPerInch = OtherListPixelsPerInch;
 
-       }
 
-     }
 
-   }
 
-   if (MatchingList != NULL)
 
-   {
 
-     UnicodeString ImageListBackupName = ImageList->Name + IntToStr(USER_DEFAULT_SCREEN_DPI);
 
-     if (ImageList->Owner->FindComponent(ImageListBackupName) == NULL)
 
-     {
 
-       TImageList * ImageListBackup;
 
-       TPngImageList * PngImageList = dynamic_cast<TPngImageList *>(ImageList);
 
-       if (PngImageList != NULL)
 
-       {
 
-         ImageListBackup = new TPngImageList(ImageList->Owner);
 
-       }
 
-       else
 
-       {
 
-         ImageListBackup = new TImageList(ImageList->Owner);
 
-       }
 
-       ImageListBackup->Name = ImageListBackupName;
 
-       ImageList->Owner->InsertComponent(ImageListBackup);
 
-       CopyImageList(ImageListBackup, ImageList);
 
-     }
 
-     CopyImageList(ImageList, MatchingList);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- static void __fastcall ImageListRescale(TComponent * Sender, TObject * /*Token*/)
 
- {
 
-   TImageList * ImageList = DebugNotNull(dynamic_cast<TImageList *>(Sender));
 
-   DoSelectScaledImageList(ImageList);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall SelectScaledImageList(TImageList * ImageList)
 
- {
 
-   DoSelectScaledImageList(ImageList);
 
-   SetRescaleFunction(ImageList, ImageListRescale);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall CopyImageList(TImageList * TargetList, TImageList * SourceList)
 
- {
 
-   // Maybe this is not necessary, once the TPngImageList::Assign was fixed.
 
-   // But if we ever use Assign, make sure the target keeps its Scaled property.
 
-   TPngImageList * PngTargetList = dynamic_cast<TPngImageList *>(TargetList);
 
-   TPngImageList * PngSourceList = dynamic_cast<TPngImageList *>(SourceList);
 
-   TargetList->Clear();
 
-   TargetList->Height = SourceList->Height;
 
-   TargetList->Width = SourceList->Width;
 
-   if ((PngTargetList != NULL) && (PngSourceList != NULL))
 
-   {
 
-     // AddImages won't copy the names and we need them for
 
-     // LoadDialogImage and TFrameAnimation
 
-     PngTargetList->PngImages->Assign(PngSourceList->PngImages);
 
-   }
 
-   else
 
-   {
 
-     TargetList->AddImages(SourceList);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- static bool __fastcall DoLoadDialogImage(TImage * Image, const UnicodeString & ImageName)
 
- {
 
-   bool Result = false;
 
-   if (GlyphsModule != NULL)
 
-   {
 
-     TPngImageList * DialogImages = GetDialogImages(Image);
 
-     int Index;
 
-     for (Index = 0; Index < DialogImages->PngImages->Count; Index++)
 
-     {
 
-       TPngImageCollectionItem * PngItem = DialogImages->PngImages->Items[Index];
 
-       if (SameText(PngItem->Name, ImageName))
 
-       {
 
-         Image->Picture->Assign(PngItem->PngImage);
 
-         break;
 
-       }
 
-     }
 
-     DebugAssert(Index < DialogImages->PngImages->Count);
 
-     Result = true;
 
-   }
 
-   // When showing an exception from wWinMain, the images are released already.
 
-   // Non-existence of the glyphs module is just a good indication of that.
 
-   // We expect errors only.
 
-   else if (ImageName == L"Error")
 
-   {
 
-     Image->Picture->Icon->Handle = LoadIcon(0, IDI_HAND);
 
-   }
 
-   // For showing an information about trace files
 
-   else if (DebugAlwaysTrue(ImageName == L"Information"))
 
-   {
 
-     Image->Picture->Icon->Handle = LoadIcon(0, IDI_APPLICATION);
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- class TDialogImageName : public TObject
 
- {
 
- public:
 
-   UnicodeString ImageName;
 
- };
 
- //---------------------------------------------------------------------------
 
- static void __fastcall DialogImageRescale(TComponent * Sender, TObject * Token)
 
- {
 
-   TImage * Image = DebugNotNull(dynamic_cast<TImage *>(Sender));
 
-   TDialogImageName * DialogImageName = DebugNotNull(dynamic_cast<TDialogImageName *>(Token));
 
-   DoLoadDialogImage(Image, DialogImageName->ImageName);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall LoadDialogImage(TImage * Image, const UnicodeString & ImageName)
 
- {
 
-   if (DoLoadDialogImage(Image, ImageName))
 
-   {
 
-     TDialogImageName * DialogImageName = new TDialogImageName();
 
-     DialogImageName->ImageName = ImageName;
 
-     SetRescaleFunction(Image, DialogImageRescale, DialogImageName, true);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- int __fastcall DialogImageSize(TForm * Form)
 
- {
 
-   return ScaleByPixelsPerInch(32, Form);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall HideComponentsPanel(TForm * Form)
 
- {
 
-   TComponent * Component = DebugNotNull(Form->FindComponent(L"ComponentsPanel"));
 
-   TPanel * Panel = DebugNotNull(dynamic_cast<TPanel *>(Component));
 
-   DebugAssert(Panel->Align == alBottom);
 
-   int Offset = Panel->Height;
 
-   Panel->Visible = false;
 
-   Panel->Height = 0;
 
-   Form->Height -= Offset;
 
-   for (int Index = 0; Index < Form->ControlCount; Index++)
 
-   {
 
-     TControl * Control = Form->Controls[Index];
 
-     // Shift back bottom-anchored controls
 
-     // (needed for toolbar panel on Progress window and buttons on Preferences dialog),
 
-     if ((Control->Align == alNone) &&
 
-         Control->Anchors.Contains(akBottom) &&
 
-         !Control->Anchors.Contains(akTop))
 
-     {
 
-       Control->Top += Offset;
 
-     }
 
-     // Resize back all-anchored controls
 
-     // (needed for main panel on Preferences dialog),
 
-     if (Control->Anchors.Contains(akBottom) &&
 
-         Control->Anchors.Contains(akTop))
 
-     {
 
-       Control->Height += Offset;
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- TIncrementalSearchState::TIncrementalSearchState()
 
- {
 
-   Reset();
 
- }
 
- //---------------------------------------------------------------------------
 
- void TIncrementalSearchState::Reset()
 
- {
 
-   Searching = false;
 
-   Text = EmptyStr;
 
-   HaveNext = false;
 
- }
 
- //---------------------------------------------------------------------------
 
- UnicodeString FormatIncrementalSearchStatus(const TIncrementalSearchState & SearchState)
 
- {
 
-   UnicodeString Result =
 
-     FMTLOAD(INC_SEARCH, (DefaultStr(SearchState.Text, LoadStr(INC_SEARCH_TYPE)))) +
 
-     ((SearchState.HaveNext && DebugAlwaysTrue(!SearchState.Text.IsEmpty())) ? L" " + LoadStr(INC_NEXT_SEARCH) : EmptyStr);
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- class TCustomDocHandler : public TComponent, public ::IDocHostUIHandler
 
- {
 
- public:
 
-   __fastcall TCustomDocHandler(TComponent * Owner, ICustomDoc * CustomDoc) :
 
-     TComponent(Owner), FCustomDoc(CustomDoc)
 
-   {
 
-   }
 
-   virtual __fastcall ~TCustomDocHandler()
 
-   {
 
-     // Something keeps the reference behind otherwise and calls it on WM_SETTINGCHANGE.
 
-     // Would make sense with TWebBrowserEx, which leaks. But it happens even with TWebBrowser.
 
-     FCustomDoc->SetUIHandler(NULL);
 
-     FCustomDoc->Release();
 
-   }
 
- protected:
 
-   ICustomDoc * FCustomDoc;
 
-   #pragma warn -hid
 
-   virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID ClassId, void ** Intf)
 
-   {
 
-     HRESULT Result = S_OK;
 
-     if (ClassId == IID_IUnknown)
 
-     {
 
-       *Intf = (IUnknown *)this;
 
-     }
 
-     else if (ClassId == ::IID_IDocHostUIHandler)
 
-     {
 
-       *Intf = (::IDocHostUIHandler *)this;
 
-     }
 
-     else
 
-     {
 
-       Result = E_NOINTERFACE;
 
-     }
 
-     return Result;
 
-   }
 
-   #pragma warn .hid
 
-   virtual ULONG STDMETHODCALLTYPE AddRef()
 
-   {
 
-     return -1;
 
-   }
 
-   virtual ULONG STDMETHODCALLTYPE Release()
 
-   {
 
-     return -1;
 
-   }
 
-   virtual HRESULT STDMETHODCALLTYPE ShowContextMenu(
 
-     DWORD DebugUsedArg(dwID), POINT *, IUnknown *, IDispatch *)
 
-   {
 
-     // No context menu
 
-     // (implementing IDocHostUIHandler reenabled context menu disabled by TBrowserViewer::DoContextPopup)
 
-     return S_OK;
 
-   }
 
-   virtual HRESULT STDMETHODCALLTYPE GetHostInfo(::_DOCHOSTUIINFO * Info)
 
-   {
 
-     // Setting ControlBorder is ignored with IDocHostUIHandler.
 
-     // DOCHOSTUIFLAG_DPI_AWARE does not seem to have any effect
 
-     Info->dwFlags |= DOCHOSTUIFLAG_SCROLL_NO | DOCHOSTUIFLAG_NO3DBORDER | DOCHOSTUIFLAG_DPI_AWARE;
 
-     return S_OK;
 
-   }
 
-   virtual HRESULT STDMETHODCALLTYPE ShowUI(
 
-     DWORD DebugUsedArg(dwID), IOleInPlaceActiveObject *, IOleCommandTarget *, IOleInPlaceFrame *, IOleInPlaceUIWindow *)
 
-   {
 
-     return E_NOTIMPL;
 
-   }
 
-   virtual HRESULT STDMETHODCALLTYPE HideUI()
 
-   {
 
-     return E_NOTIMPL;
 
-   }
 
-   virtual HRESULT STDMETHODCALLTYPE UpdateUI()
 
-   {
 
-     return E_NOTIMPL;
 
-   }
 
-   virtual HRESULT STDMETHODCALLTYPE EnableModeless(BOOL DebugUsedArg(fEnable))
 
-   {
 
-     return E_NOTIMPL;
 
-   }
 
-   virtual HRESULT STDMETHODCALLTYPE OnDocWindowActivate(BOOL DebugUsedArg(fActivate))
 
-   {
 
-     return E_NOTIMPL;
 
-   }
 
-   virtual HRESULT STDMETHODCALLTYPE OnFrameWindowActivate(BOOL DebugUsedArg(fActivate))
 
-   {
 
-     return E_NOTIMPL;
 
-   }
 
-   virtual HRESULT STDMETHODCALLTYPE ResizeBorder(LPCRECT, IOleInPlaceUIWindow *, BOOL DebugUsedArg(fRameWindow))
 
-   {
 
-     return E_NOTIMPL;
 
-   }
 
-   virtual HRESULT STDMETHODCALLTYPE TranslateAccelerator(LPMSG, const GUID * DebugUsedArg(pguidCmdGroup), DWORD DebugUsedArg(nCmdID))
 
-   {
 
-     return E_NOTIMPL;
 
-   }
 
-   virtual HRESULT STDMETHODCALLTYPE GetOptionKeyPath(LPOLESTR * DebugUsedArg(pchKey), DWORD DebugUsedArg(dw))
 
-   {
 
-     return E_NOTIMPL;
 
-   }
 
-   virtual HRESULT STDMETHODCALLTYPE GetDropTarget(IDropTarget *, IDropTarget **)
 
-   {
 
-     return E_NOTIMPL;
 
-   }
 
-   virtual HRESULT STDMETHODCALLTYPE GetExternal(IDispatch **)
 
-   {
 
-     return E_NOTIMPL;
 
-   }
 
-   virtual HRESULT STDMETHODCALLTYPE TranslateUrl(DWORD DebugUsedArg(dwTranslate), OLECHAR * DebugUsedArg(pchURLIn), OLECHAR ** DebugUsedArg(ppchURLOut))
 
-   {
 
-     return E_NOTIMPL;
 
-   }
 
-   virtual HRESULT STDMETHODCALLTYPE FilterDataObject(IDataObject *, IDataObject **)
 
-   {
 
-     return E_NOTIMPL;
 
-   }
 
- };
 
- //---------------------------------------------------------------------------
 
- // Included only here as it defines ambiguous LONG_PTR, causing INVALID_HANDLE_VALUE to be unusable
 
- #include <WebBrowserEx.hpp>
 
- //---------------------------------------------------------------------------
 
- class TBrowserViewer : public TWebBrowserEx
 
- {
 
- public:
 
-   __fastcall virtual TBrowserViewer(TComponent* AOwner);
 
-   void __fastcall AddLinkHandler(
 
-     const UnicodeString & Url, TNotifyEvent Handler);
 
-   void __fastcall NavigateToUrl(const UnicodeString & Url);
 
-   TControl * LoadingPanel;
 
- protected:
 
-   DYNAMIC void __fastcall DoContextPopup(const TPoint & MousePos, bool & Handled);
 
-   void __fastcall DocumentComplete(
 
-     TObject * Sender, const _di_IDispatch Disp, const OleVariant & URL);
 
-   void __fastcall BeforeNavigate2(
 
-     TObject * Sender, const _di_IDispatch Disp, const OleVariant & URL,
 
-     const OleVariant & Flags, const OleVariant & TargetFrameName,
 
-     const OleVariant & PostData, const OleVariant & Headers, WordBool & Cancel);
 
-   bool FComplete;
 
-   std::map<UnicodeString, TNotifyEvent> FHandlers;
 
- };
 
- //---------------------------------------------------------------------------
 
- __fastcall TBrowserViewer::TBrowserViewer(TComponent* AOwner) :
 
-   TWebBrowserEx(AOwner)
 
- {
 
-   FComplete = false;
 
-   OnDocumentComplete = DocumentComplete;
 
-   OnBeforeNavigate2 = BeforeNavigate2;
 
-   LoadingPanel = NULL;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TBrowserViewer::AddLinkHandler(
 
-   const UnicodeString & Url, TNotifyEvent Handler)
 
- {
 
-   FHandlers.insert(std::make_pair(Url, Handler));
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TBrowserViewer::DoContextPopup(const TPoint & MousePos, bool & Handled)
 
- {
 
-   // Suppress built-in context menu.
 
-   // Is ignored with IDocHostUIHandler. Needs to be overriden by ShowContextMenu.
 
-   Handled = true;
 
-   TWebBrowserEx::DoContextPopup(MousePos, Handled);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TBrowserViewer::DocumentComplete(
 
-     TObject * /*Sender*/, const _di_IDispatch /*Disp*/, const OleVariant & /*URL*/)
 
- {
 
-   SetBrowserDesignModeOff(this);
 
-   FComplete = true;
 
-   if (LoadingPanel != NULL)
 
-   {
 
-     LoadingPanel->Visible = false;
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TBrowserViewer::BeforeNavigate2(
 
-   TObject * /*Sender*/, const _di_IDispatch /*Disp*/, const OleVariant & AURL,
 
-   const OleVariant & /*Flags*/, const OleVariant & /*TargetFrameName*/,
 
-   const OleVariant & /*PostData*/, const OleVariant & /*Headers*/, WordBool & Cancel)
 
- {
 
-   // If OnDocumentComplete was not called yet, is has to be our initial message URL,
 
-   // opened using TWebBrowserEx::Navigate(), allow it.
 
-   // Otherwise it's user navigating, block that and open link
 
-   // in an external browser, possibly adding campaign parameters on the way.
 
-   if (FComplete)
 
-   {
 
-     Cancel = 1;
 
-     UnicodeString URL = AURL;
 
-     if (FHandlers.count(URL) > 0)
 
-     {
 
-       FHandlers[URL](this);
 
-     }
 
-     else
 
-     {
 
-       OpenBrowser(URL);
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TBrowserViewer::NavigateToUrl(const UnicodeString & Url)
 
- {
 
-   FComplete = false;
 
-   Navigate(Url.c_str());
 
- }
 
- //---------------------------------------------------------------------------
 
- TWebBrowserEx * __fastcall CreateBrowserViewer(TPanel * Parent, const UnicodeString & LoadingLabel)
 
- {
 
-   TBrowserViewer * Result = new TBrowserViewer(Parent);
 
-   // TWebBrowserEx has its own unrelated Name and Parent properties.
 
-   // The name is used in DownloadUpdate().
 
-   static_cast<TWinControl *>(Result)->Name = L"BrowserViewer";
 
-   static_cast<TWinControl *>(Result)->Parent = Parent;
 
-   Result->Align = alClient;
 
-   // Is ignored with IDocHostUIHandler. Needs to be overriden by DOCHOSTUIFLAG_NO3DBORDER in GetHostInfo.
 
-   Result->ControlBorder = cbNone;
 
-   Result->LoadingPanel = CreateLabelPanel(Parent, LoadingLabel);
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall SetBrowserDesignModeOff(TWebBrowserEx * WebBrowser)
 
- {
 
-   if (DebugAlwaysTrue(WebBrowser->Document2 != NULL))
 
-   {
 
-     WebBrowser->Document2->designMode = L"Off";
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall AddBrowserLinkHandler(TWebBrowserEx * WebBrowser,
 
-   const UnicodeString & Url, TNotifyEvent Handler)
 
- {
 
-   TBrowserViewer * BrowserViewer = dynamic_cast<TBrowserViewer *>(WebBrowser);
 
-   if (DebugAlwaysTrue(BrowserViewer != NULL))
 
-   {
 
-     BrowserViewer->AddLinkHandler(Url, Handler);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall NavigateBrowserToUrl(TWebBrowserEx * WebBrowser, const UnicodeString & Url)
 
- {
 
-   TBrowserViewer * BrowserViewer = dynamic_cast<TBrowserViewer *>(WebBrowser);
 
-   if (DebugAlwaysTrue(BrowserViewer != NULL))
 
-   {
 
-     BrowserViewer->NavigateToUrl(Url);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void ReadyBrowserForStreaming(TWebBrowserEx * WebBrowser)
 
- {
 
-   // This creates TWebBrowserEx::Document, which we need to stream in an in-memory document
 
-   NavigateBrowserToUrl(WebBrowser, L"about:blank");
 
-   // Needs to be followed by WaitBrowserToIdle
 
- }
 
- //---------------------------------------------------------------------------
 
- void WaitBrowserToIdle(TWebBrowserEx * WebBrowser)
 
- {
 
-   while (WebBrowser->ReadyState < ::READYSTATE_INTERACTIVE)
 
-   {
 
-     Application->ProcessMessages();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void HideBrowserScrollbars(TWebBrowserEx * WebBrowser)
 
- {
 
-   ICustomDoc * CustomDoc = NULL;
 
-   if (DebugAlwaysTrue(WebBrowser->Document != NULL) &&
 
-       SUCCEEDED(WebBrowser->Document->QueryInterface(&CustomDoc)) &&
 
-       DebugAlwaysTrue(CustomDoc != NULL))
 
-   {
 
-     TCustomDocHandler * Handler = new TCustomDocHandler(WebBrowser, CustomDoc);
 
-     CustomDoc->SetUIHandler(Handler);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- bool CopyTextFromBrowser(TWebBrowserEx * WebBrowser, UnicodeString & Text)
 
- {
 
-   WebBrowser->SelectAll();
 
-   WebBrowser->CopyToClipBoard();
 
-   bool Result = NonEmptyTextFromClipboard(Text);
 
-   WebBrowser->DoCommand(L"UNSELECT");
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- TPanel * CreateBlankPanel(TComponent * Owner)
 
- {
 
-   TPanel * Panel = new TPanel(Owner);
 
-   Panel->BevelOuter = bvNone;
 
-   Panel->BevelInner = bvNone; // default
 
-   Panel->BevelKind = bkNone;
 
-   Panel->Color = clWindow;
 
-   return Panel;
 
- }
 
- //---------------------------------------------------------------------------
 
- TPanel * CreateLabelPanel(TPanel * Parent, const UnicodeString & Label)
 
- {
 
-   TPanel * Result = CreateBlankPanel(Parent);
 
-   Result->Parent = Parent;
 
-   Result->Align = alClient;
 
-   Result->Caption = Label;
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- UnicodeString GenerateAppHtmlPage(TFont * Font, TPanel * Parent, const UnicodeString & Body, bool Seamless)
 
- {
 
-   UnicodeString Result =
 
-     L"<!DOCTYPE html>\n"
 
-     L"<meta charset=\"utf-8\">\n"
 
-     L"<html>\n"
 
-     L"<head>\n"
 
-     L"<style>\n"
 
-     L"\n"
 
-     L"body\n"
 
-     L"{\n"
 
-     L"    font-family: '" + Font->Name + L"';\n"
 
-     L"    margin: " + UnicodeString(Seamless ? L"0" : L"0.5em") + L";\n"
 
-     L"    color: " + ColorToWebColorStr(Parent->Font->Color) + L";\n"
 
-     L"    background-color: " + ColorToWebColorStr(Parent->Color) + L";\n" +
 
-     UnicodeString(Seamless ? L"    overflow: hidden;\n" : L"") +
 
-     L"    font-size: " + IntToStr(Font->Size) + L"pt;\n"
 
-     L"}\n"
 
-     L"\n"
 
-     L"p\n"
 
-     L"{\n"
 
-     L"    margin-top: 0;\n"
 
-     L"    margin-bottom: 1em;\n"
 
-     L"}\n"
 
-     L"\n"
 
-     L"a, a:visited, a:hover, a:visited, a:current\n"
 
-     L"{\n"
 
-     L"    color: " + ColorToWebColorStr(GetLinkColor(Parent)) + L";\n"
 
-     L"}\n"
 
-     L"</style>\n"
 
-     L"</head>\n"
 
-     L"<body>\n" +
 
-     Body +
 
-     L"</body>\n"
 
-     L"</html>\n";
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- void LoadBrowserDocument(TWebBrowserEx * WebBrowser, const UnicodeString & Document)
 
- {
 
-   std::unique_ptr<TMemoryStream> DocumentStream(new TMemoryStream());
 
-   UTF8String DocumentUTF8 = UTF8String(Document);
 
-   DocumentStream->Write(DocumentUTF8.c_str(), DocumentUTF8.Length());
 
-   DocumentStream->Seek(0, 0);
 
-   // For stream-loaded document, when set only after loading from OnDocumentComplete,
 
-   // browser stops working
 
-   SetBrowserDesignModeOff(WebBrowser);
 
-   TStreamAdapter * DocumentStreamAdapter = new TStreamAdapter(DocumentStream.get(), soReference);
 
-   IPersistStreamInit * PersistStreamInit = NULL;
 
-   if (DebugAlwaysTrue(WebBrowser->Document != NULL) &&
 
-       SUCCEEDED(WebBrowser->Document->QueryInterface(IID_IPersistStreamInit, (void **)&PersistStreamInit)) &&
 
-       DebugAlwaysTrue(PersistStreamInit != NULL))
 
-   {
 
-     PersistStreamInit->Load(static_cast<_di_IStream>(*DocumentStreamAdapter));
 
-     PersistStreamInit->Release();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- TComponent * __fastcall FindComponentRecursively(TComponent * Root, const UnicodeString & Name)
 
- {
 
-   for (int Index = 0; Index < Root->ComponentCount; Index++)
 
-   {
 
-     TComponent * Component = Root->Components[Index];
 
-     if (CompareText(Component->Name, Name) == 0)
 
-     {
 
-       return Component;
 
-     }
 
-     Component = FindComponentRecursively(Component, Name);
 
-     if (Component != NULL)
 
-     {
 
-       return Component;
 
-     }
 
-   }
 
-   return NULL;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall GetInstrutionsTheme(
 
-   TColor & MainInstructionColor, HFONT & MainInstructionFont, HFONT & InstructionFont)
 
- {
 
-   MainInstructionColor = Graphics::clNone;
 
-   MainInstructionFont = 0;
 
-   InstructionFont = 0;
 
-   HTHEME Theme = OpenThemeData(0, L"TEXTSTYLE");
 
-   if (Theme != NULL)
 
-   {
 
-     LOGFONT AFont;
 
-     COLORREF AColor;
 
-     memset(&AFont, 0, sizeof(AFont));
 
-     // Using Canvas->Handle in the 2nd argument we can get scaled font,
 
-     // but at this point the form is sometime not scaled yet (difference is particularly for standalone messages like
 
-     // /UninstallCleanup), so the results are inconsistent.
 
-     if (GetThemeFont(Theme, NULL, TEXT_MAININSTRUCTION, 0, TMT_FONT, &AFont) == S_OK)
 
-     {
 
-       MainInstructionFont = CreateFontIndirect(&AFont);
 
-     }
 
-     if (GetThemeColor(Theme, TEXT_MAININSTRUCTION, 0, TMT_TEXTCOLOR, &AColor) == S_OK)
 
-     {
 
-       MainInstructionColor = (TColor)AColor;
 
-     }
 
-     memset(&AFont, 0, sizeof(AFont));
 
-     if (GetThemeFont(Theme, NULL, TEXT_INSTRUCTION, 0, TMT_FONT, &AFont) == S_OK)
 
-     {
 
-       InstructionFont = CreateFontIndirect(&AFont);
 
-     }
 
-     CloseThemeData(Theme);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- TLocalCustomCommand::TLocalCustomCommand()
 
- {
 
- }
 
- //---------------------------------------------------------------------------
 
- TLocalCustomCommand::TLocalCustomCommand(
 
-     const TCustomCommandData & Data, const UnicodeString & RemotePath, const UnicodeString & LocalPath) :
 
-   TFileCustomCommand(Data, RemotePath)
 
- {
 
-   FLocalPath = LocalPath;
 
- }
 
- //---------------------------------------------------------------------------
 
- TLocalCustomCommand::TLocalCustomCommand(const TCustomCommandData & Data,
 
-   const UnicodeString & RemotePath, const UnicodeString & LocalPath, const UnicodeString & FileName,
 
-   const UnicodeString & LocalFileName, const UnicodeString & FileList) :
 
-   TFileCustomCommand(Data, RemotePath, FileName, FileList)
 
- {
 
-   FLocalPath = LocalPath;
 
-   FLocalFileName = LocalFileName;
 
- }
 
- //---------------------------------------------------------------------------
 
- int __fastcall TLocalCustomCommand::PatternLen(const UnicodeString & Command, int Index)
 
- {
 
-   int Len;
 
-   if ((Index < Command.Length()) && (Command[Index + 1] == L'\\'))
 
-   {
 
-     Len = 2;
 
-   }
 
-   else if ((Index < Command.Length()) && (Command[Index + 1] == L'^'))
 
-   {
 
-     Len = 3;
 
-   }
 
-   else
 
-   {
 
-     Len = TFileCustomCommand::PatternLen(Command, Index);
 
-   }
 
-   return Len;
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TLocalCustomCommand::PatternReplacement(
 
-   int Index, const UnicodeString & Pattern, UnicodeString & Replacement, bool & Delimit)
 
- {
 
-   bool Result;
 
-   if (Pattern == L"!\\")
 
-   {
 
-     // When used as "!\" in an argument to PowerShell, the trailing \ would escape the ",
 
-     // so we exclude it
 
-     Replacement = ExcludeTrailingBackslash(FLocalPath);
 
-     Result = true;
 
-   }
 
-   else if (Pattern == L"!^!")
 
-   {
 
-     Replacement = FLocalFileName;
 
-     Result = true;
 
-   }
 
-   else
 
-   {
 
-     Result = TFileCustomCommand::PatternReplacement(Index, Pattern, Replacement, Delimit);
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLocalCustomCommand::DelimitReplacement(
 
-   UnicodeString & /*Replacement*/, wchar_t /*Quote*/)
 
- {
 
-   // never delimit local commands
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TLocalCustomCommand::HasLocalFileName(const UnicodeString & Command)
 
- {
 
-   return FindPattern(Command, L'^');
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TLocalCustomCommand::IsFileCommand(const UnicodeString & Command)
 
- {
 
-   return TFileCustomCommand::IsFileCommand(Command) || HasLocalFileName(Command);
 
- }
 
- //---------------------------------------------------------------------------
 
- //---------------------------------------------------------------------------
 
- typedef std::set<TDataModule *> TImagesModules;
 
- static TImagesModules ImagesModules;
 
- static std::map<int, TPngImageList *> AnimationsImages;
 
- static std::map<int, TImageList *> ButtonImages;
 
- static std::map<int, TPngImageList *> DialogImages;
 
- //---------------------------------------------------------------------------
 
- int __fastcall DoNormalizePixelsPerInch(int PixelsPerInch, bool Larger)
 
- {
 
-   if (PixelsPerInch >= 192)
 
-   {
 
-     PixelsPerInch = 192;
 
-   }
 
-   else if (PixelsPerInch >= 144)
 
-   {
 
-     PixelsPerInch = Larger ? 192 : 144;
 
-   }
 
-   else if (PixelsPerInch >= 120)
 
-   {
 
-     PixelsPerInch = Larger ? 144 : 120;
 
-   }
 
-   else
 
-   {
 
-     PixelsPerInch = Larger ? 120 : 96;
 
-   }
 
-   return PixelsPerInch;
 
- }
 
- //---------------------------------------------------------------------------
 
- int NormalizePixelsPerInch(int PixelsPerInch)
 
- {
 
-   return DoNormalizePixelsPerInch(PixelsPerInch, false);
 
- }
 
- //---------------------------------------------------------------------------
 
- int LargerPixelsPerInch(int PixelsPerInch, int Larger)
 
- {
 
-   int Result = PixelsPerInch;
 
-   while (Larger > 0)
 
-   {
 
-     Result = DoNormalizePixelsPerInch(Result, true);
 
-     Larger--;
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- static int __fastcall NeedImagesModule(TControl * Control)
 
- {
 
-   int PixelsPerInch = NormalizePixelsPerInch(GetControlPixelsPerInch(Control));
 
-   if (AnimationsImages.find(PixelsPerInch) == AnimationsImages.end())
 
-   {
 
-     TDataModule * ImagesModule;
 
-     HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
 
-     try
 
-     {
 
-       if (PixelsPerInch == 192)
 
-       {
 
-         ImagesModule = new TAnimations192Module(Application);
 
-       }
 
-       else if (PixelsPerInch == 144)
 
-       {
 
-         ImagesModule = new TAnimations144Module(Application);
 
-       }
 
-       else if (PixelsPerInch == 120)
 
-       {
 
-         ImagesModule = new TAnimations120Module(Application);
 
-       }
 
-       else
 
-       {
 
-         DebugAssert(PixelsPerInch == 96);
 
-         ImagesModule = new TAnimations96Module(Application);
 
-       }
 
-       ImagesModules.insert(ImagesModule);
 
-       TPngImageList * AAnimationImages =
 
-         DebugNotNull(dynamic_cast<TPngImageList *>(ImagesModule->FindComponent(L"AnimationImages")));
 
-       AnimationsImages.insert(std::make_pair(PixelsPerInch, AAnimationImages));
 
-       TImageList * AButtonImages =
 
-         DebugNotNull(dynamic_cast<TImageList *>(ImagesModule->FindComponent(L"ButtonImages")));
 
-       ButtonImages.insert(std::make_pair(PixelsPerInch, AButtonImages));
 
-       TPngImageList * ADialogImages =
 
-         DebugNotNull(dynamic_cast<TPngImageList *>(ImagesModule->FindComponent(L"DialogImages")));
 
-       DialogImages.insert(std::make_pair(PixelsPerInch, ADialogImages));
 
-     }
 
-     __finally
 
-     {
 
-       GUIConfiguration->ChangeResourceModule(ResourceModule);
 
-     }
 
-   }
 
-   return PixelsPerInch;
 
- }
 
- //---------------------------------------------------------------------------
 
- TPngImageList * __fastcall GetAnimationsImages(TControl * Control)
 
- {
 
-   int PixelsPerInch = NeedImagesModule(Control);
 
-   return DebugNotNull(AnimationsImages[PixelsPerInch]);
 
- }
 
- //---------------------------------------------------------------------------
 
- TImageList * __fastcall GetButtonImages(TControl * Control)
 
- {
 
-   int PixelsPerInch = NeedImagesModule(Control);
 
-   return DebugNotNull(ButtonImages[PixelsPerInch]);
 
- }
 
- //---------------------------------------------------------------------------
 
- TPngImageList * __fastcall GetDialogImages(TControl * Control)
 
- {
 
-   int PixelsPerInch = NeedImagesModule(Control);
 
-   return DebugNotNull(DialogImages[PixelsPerInch]);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall ReleaseImagesModules()
 
- {
 
-   TImagesModules::iterator i = ImagesModules.begin();
 
-   while (i != ImagesModules.end())
 
-   {
 
-     delete (*i);
 
-     i++;
 
-   }
 
-   ImagesModules.clear();
 
- }
 
- //---------------------------------------------------------------------------
 
- __fastcall TFrameAnimation::TFrameAnimation()
 
- {
 
-   FFirstFrame = -1;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TFrameAnimation::Init(TPaintBox * PaintBox, const UnicodeString & Name)
 
- {
 
-   FName = Name;
 
-   FPaintBox = PaintBox;
 
-   FPaintBox->ControlStyle = FPaintBox->ControlStyle << csOpaque;
 
-   DebugAssert((FPaintBox->OnPaint == NULL) || (FPaintBox->OnPaint == PaintBoxPaint));
 
-   FPaintBox->OnPaint = PaintBoxPaint;
 
-   SetRescaleFunction(FPaintBox, PaintBoxRescale, reinterpret_cast<TObject *>(this));
 
-   DoInit();
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TFrameAnimation::DoInit()
 
- {
 
-   FImageList = GetAnimationsImages(FPaintBox);
 
-   FFirstFrame = -1;
 
-   FFirstLoopFrame = -1;
 
-   FPaintBox->Width = FImageList->Width;
 
-   FPaintBox->Height = FImageList->Height;
 
-   if (!FName.IsEmpty())
 
-   {
 
-     int Frame = 0;
 
-     while (Frame < FImageList->PngImages->Count)
 
-     {
 
-       UnicodeString FrameData = FImageList->PngImages->Items[Frame]->Name;
 
-       UnicodeString FrameName;
 
-       FrameName = CutToChar(FrameData, L'_', false);
 
-       if (SameText(FName, FrameName))
 
-       {
 
-         int FrameIndex = StrToInt(CutToChar(FrameData, L'_', false));
 
-         if (FFirstFrame < 0)
 
-         {
 
-           FFirstFrame = Frame;
 
-         }
 
-         if ((FFirstLoopFrame < 0) && (FrameIndex > 0))
 
-         {
 
-           FFirstLoopFrame = Frame;
 
-         }
 
-         FLastFrame = Frame;
 
-       }
 
-       else
 
-       {
 
-         if (FFirstFrame >= 0)
 
-         {
 
-           // optimization
 
-           break;
 
-         }
 
-       }
 
-       Frame++;
 
-     }
 
-     DebugAssert(FFirstFrame >= 0);
 
-     DebugAssert(FFirstLoopFrame >= 0);
 
-   }
 
-   Stop();
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TFrameAnimation::PaintBoxRescale(TComponent * /*Sender*/, TObject * Token)
 
- {
 
-   TFrameAnimation * FrameAnimation = reinterpret_cast<TFrameAnimation *>(Token);
 
-   FrameAnimation->Rescale();
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TFrameAnimation::Rescale()
 
- {
 
-   bool Started = (FTimer != NULL) && FTimer->Enabled;
 
-   DoInit();
 
-   if (Started)
 
-   {
 
-     Start();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TFrameAnimation::Start()
 
- {
 
-   if (FFirstFrame >= 0)
 
-   {
 
-     FNextFrameTick = GetTickCount();
 
-     CalculateNextFrameTick();
 
-     if (FTimer == NULL)
 
-     {
 
-       FTimer = new TTimer(GetParentForm(FPaintBox));
 
-       FTimer->Interval = static_cast<int>(GUIUpdateInterval);
 
-       FTimer->OnTimer = Timer;
 
-     }
 
-     else
 
-     {
 
-       // reset timer
 
-       FTimer->Enabled = false;
 
-       FTimer->Enabled = true;
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TFrameAnimation::Timer(TObject * /*Sender*/)
 
- {
 
-   Animate();
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TFrameAnimation::PaintBoxPaint(TObject * Sender)
 
- {
 
-   if (FFirstFrame >= 0)
 
-   {
 
-     // Double-buffered drawing to prevent flicker (as the images are transparent)
 
-     DebugUsedParam(Sender);
 
-     DebugAssert(FPaintBox == Sender);
 
-     DebugAssert(FPaintBox->ControlStyle.Contains(csOpaque));
 
-     std::unique_ptr<TBitmap> Bitmap(new TBitmap());
 
-     Bitmap->SetSize(FPaintBox->Width, FPaintBox->Height);
 
-     Bitmap->Canvas->Brush->Color = FPaintBox->Color;
 
-     TRect Rect(0, 0, Bitmap->Width, Bitmap->Height);
 
-     Bitmap->Canvas->FillRect(Rect);
 
-     TGraphic * Graphic = GetCurrentImage()->PngImage;
 
-     DebugAssert(Graphic->Width == FPaintBox->Width);
 
-     DebugAssert(Graphic->Height == FPaintBox->Height);
 
-     Bitmap->Canvas->Draw(0, 0, Graphic);
 
-     FPaintBox->Canvas->Draw(0, 0, Bitmap.get());
 
-   }
 
-   FPainted = true;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TFrameAnimation::Repaint()
 
- {
 
-   FPainted = false;
 
-   // If the form is not showing yet, the Paint() is not even called
 
-   FPaintBox->Repaint();
 
-   if (!FPainted)
 
-   {
 
-     // Paint later, alternatively we may keep trying Repaint() in Animate().
 
-     // See also a hack in TAuthenticateForm::Log.
 
-     FPaintBox->Invalidate();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TFrameAnimation::Stop()
 
- {
 
-   FNextFrameTick = std::numeric_limits<DWORD>::max();
 
-   FCurrentFrame = FFirstFrame;
 
-   Repaint();
 
-   if (FTimer != NULL)
 
-   {
 
-     FTimer->Enabled = false;
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TFrameAnimation::Animate()
 
- {
 
-   if (FFirstFrame >= 0)
 
-   {
 
-     // UPGRADE: Use GetTickCount64() when we stop supporting Windows XP.
 
-     DWORD TickCount = GetTickCount();
 
-     // Keep in sync with an opposite condition at the end of the loop.
 
-     // We may skip some frames if we got stalled for a while
 
-     while (TickCount >= FNextFrameTick)
 
-     {
 
-       if (FCurrentFrame >= FLastFrame)
 
-       {
 
-         FCurrentFrame = FFirstLoopFrame;
 
-       }
 
-       else
 
-       {
 
-         FCurrentFrame++;
 
-       }
 
-       CalculateNextFrameTick();
 
-       Repaint();
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- TPngImageCollectionItem * __fastcall TFrameAnimation::GetCurrentImage()
 
- {
 
-   return FImageList->PngImages->Items[FCurrentFrame];
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TFrameAnimation::CalculateNextFrameTick()
 
- {
 
-   TPngImageCollectionItem * ImageItem = GetCurrentImage();
 
-   UnicodeString Duration = ImageItem->Name;
 
-   CutToChar(Duration, L'_', false);
 
-   // skip index (is not really used)
 
-   CutToChar(Duration, L'_', false);
 
-   // This should overflow, when tick count wraps.
 
-   FNextFrameTick += StrToInt(Duration) * 10;
 
- }
 
- //---------------------------------------------------------------------------
 
- //---------------------------------------------------------------------------
 
- // Hints use:
 
- // - Cleanup list tooltip (multi line)
 
- // - Combo edit button
 
- // - Transfer settings label (multi line, follows label size and font)
 
- // - HintLabel (hint and persistent hint, multi line)
 
- // - status bar hints
 
- //---------------------------------------------------------------------------
 
- __fastcall TScreenTipHintWindow::TScreenTipHintWindow(TComponent * Owner) :
 
-   THintWindow(Owner)
 
- {
 
-   FParentPainting = false;
 
- }
 
- //---------------------------------------------------------------------------
 
- int __fastcall TScreenTipHintWindow::GetTextFlags(TControl * Control)
 
- {
 
-   return DT_LEFT | DT_WORDBREAK | DT_NOPREFIX | Control->DrawTextBiDiModeFlagsReadingOnly();
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TScreenTipHintWindow::UseBoldShortHint(TControl * HintControl)
 
- {
 
-   return
 
-     (dynamic_cast<TTBCustomDockableWindow *>(HintControl) != NULL) ||
 
-     (dynamic_cast<TTBPopupWindow *>(HintControl) != NULL) ||
 
-     (HintControl->Perform(WM_WANTS_SCREEN_TIPS, 0, 0) == 1);
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TScreenTipHintWindow::IsPathLabel(TControl * HintControl)
 
- {
 
-   return (dynamic_cast<TPathLabel *>(HintControl) != NULL);
 
- }
 
- //---------------------------------------------------------------------------
 
- int __fastcall TScreenTipHintWindow::GetMargin(TControl * HintControl, const UnicodeString & Hint)
 
- {
 
-   int Result;
 
-   if (HasLabelHintPopup(HintControl, Hint) || IsPathLabel(HintControl))
 
-   {
 
-     Result = 3;
 
-   }
 
-   else
 
-   {
 
-     Result = 6;
 
-   }
 
-   Result = ScaleByTextHeight(HintControl, Result);
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- TFont * __fastcall TScreenTipHintWindow::GetFont(TControl * HintControl, const UnicodeString & Hint)
 
- {
 
-   TFont * Result;
 
-   if (HasLabelHintPopup(HintControl, Hint) || IsPathLabel(HintControl))
 
-   {
 
-     Result = reinterpret_cast<TLabel *>(dynamic_cast<TCustomLabel *>(HintControl))->Font;
 
-   }
 
-   else
 
-   {
 
-     FScaledHintFont.reset(new TFont());
 
-     FScaledHintFont->Assign(Screen->HintFont);
 
-     FScaledHintFont->Size = ScaleByPixelsPerInchFromSystem(FScaledHintFont->Size, HintControl);
 
-     Result = FScaledHintFont.get();
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TScreenTipHintWindow::CalcHintTextRect(TControl * Control, TCanvas * Canvas, TRect & Rect, const UnicodeString & Hint)
 
- {
 
-   const int Flags = DT_CALCRECT | GetTextFlags(Control);
 
-   DrawText(Canvas->Handle, Hint.c_str(), -1, &Rect, Flags);
 
- }
 
- //---------------------------------------------------------------------------
 
- TRect __fastcall TScreenTipHintWindow::CalcHintRect(int MaxWidth, const UnicodeString AHint, void * AData)
 
- {
 
-   TControl * HintControl = GetHintControl(AData);
 
-   int Margin = GetMargin(HintControl, AHint);
 
-   UnicodeString ShortHint;
 
-   UnicodeString LongHint;
 
-   SplitHint(HintControl, AHint, ShortHint, LongHint);
 
-   Canvas->Font->Assign(GetFont(HintControl, AHint));
 
-   // from XE6 Vcl.ScreenTips.pas, but absent in 11
 
-   const int cScreenTipTextOnlyWidth = 210;
 
-   const int ScreenTipTextOnlyWidth = ScaleByTextHeight(HintControl, cScreenTipTextOnlyWidth);
 
-   int LongHintMargin = 0; // shut up
 
-   bool HasLongHint = !LongHint.IsEmpty();
 
-   if (HasLongHint)
 
-   {
 
-     // double-margin on the right
 
-     LongHintMargin = (3 * Margin);
 
-     MaxWidth = ScreenTipTextOnlyWidth - LongHintMargin;
 
-   }
 
-   // Multi line short hints can be twice as wide, to not break the individual lines unless really necessary.
 
-   // (login site tree, clean up dialog list, preferences custom command list, persistent hint, etc).
 
-   // And they also can be twice as wide, to not break the individual lines unless really necessary.
 
-   if (ShortHint.Pos(L"\n") > 0)
 
-   {
 
-     MaxWidth *= 2;
 
-   }
 
-   bool HintPopup = HasLabelHintPopup(HintControl, AHint);
 
-   if (HintPopup)
 
-   {
 
-     MaxWidth = HintControl->Width;
 
-   }
 
-   if (UseBoldShortHint(HintControl))
 
-   {
 
-     Canvas->Font->Style = TFontStyles() << fsBold;
 
-   }
 
-   TRect ShortRect(0, 0, MaxWidth, 0);
 
-   CalcHintTextRect(this, Canvas, ShortRect, ShortHint);
 
-   Canvas->Font->Style = TFontStyles();
 
-   TRect Result;
 
-   if (!HasLongHint)
 
-   {
 
-     Result = ShortRect;
 
-     if (HintPopup)
 
-     {
 
-       Result.Right = MaxWidth + 2 * Margin;
 
-     }
 
-     else
 
-     {
 
-       Result.Right += 3 * Margin;
 
-     }
 
-     Result.Bottom += 2 * Margin;
 
-   }
 
-   else
 
-   {
 
-     const int LongIndentation = Margin * 3 / 2;
 
-     TRect LongRect(0, 0, MaxWidth - LongIndentation, 0);
 
-     CalcHintTextRect(this, Canvas, LongRect, LongHint);
 
-     Result.Right = Max(ScreenTipTextOnlyWidth, LongRect.Right + LongIndentation + LongHintMargin);
 
-     Result.Bottom = Margin + ShortRect.Height() + (Margin / 3 * 5) + LongRect.Height() + Margin;
 
-   }
 
-   // VCLCOPY: To counter the increase in THintWindow::ActivateHintData
 
-   Result.Bottom -= 4;
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TScreenTipHintWindow::SplitHint(
 
-   TControl * HintControl, const UnicodeString & Hint, UnicodeString & ShortHint, UnicodeString & LongHint)
 
- {
 
-   if (HasLabelHintPopup(HintControl, Hint))
 
-   {
 
-     ShortHint = HintControl->Hint;
 
-   }
 
-   else
 
-   {
 
-     ShortHint = GetShortHint(Hint);
 
-     LongHint = GetLongHintIfAny(Hint);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TScreenTipHintWindow::ActivateHintData(const TRect & ARect, const UnicodeString AHint, void * AData)
 
- {
 
-   FHintControl = GetHintControl(AData);
 
-   SplitHint(FHintControl, AHint, FShortHint, FLongHint);
 
-   FMargin = GetMargin(FHintControl, AHint);
 
-   FHintPopup = HasLabelHintPopup(FHintControl, AHint);
 
-   Canvas->Font->Assign(GetFont(FHintControl, AHint));
 
-   TRect Rect = ARect;
 
-   if (FHintPopup)
 
-   {
 
-     Rect.SetLocation(FHintControl->ClientToScreen(TPoint(-FMargin, -FMargin)));
 
-   }
 
-   if (IsPathLabel(FHintControl))
 
-   {
 
-     Rect.Offset(-FMargin, -FMargin);
 
-   }
 
-   THintWindow::ActivateHintData(Rect, FShortHint, AData);
 
- }
 
- //---------------------------------------------------------------------------
 
- TControl * __fastcall TScreenTipHintWindow::GetHintControl(void * Data)
 
- {
 
-   return reinterpret_cast<TControl *>(DebugNotNull(Data));
 
- }
 
- //---------------------------------------------------------------------------
 
- UnicodeString __fastcall TScreenTipHintWindow::GetLongHintIfAny(const UnicodeString & AHint)
 
- {
 
-   UnicodeString Result;
 
-   int P = Pos(L"|", AHint);
 
-   if (P > 0)
 
-   {
 
-     Result = GetLongHint(AHint);
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TScreenTipHintWindow::Dispatch(void * AMessage)
 
- {
 
-   TMessage * Message = static_cast<TMessage*>(AMessage);
 
-   switch (Message->Msg)
 
-   {
 
-     case WM_GETTEXTLENGTH:
 
-       if (FParentPainting)
 
-       {
 
-         // make THintWindow::Paint() not paint the Caption
 
-         Message->Result = 0;
 
-       }
 
-       else
 
-       {
 
-         THintWindow::Dispatch(AMessage);
 
-       }
 
-       break;
 
-     default:
 
-       THintWindow::Dispatch(AMessage);
 
-       break;
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TScreenTipHintWindow::Paint()
 
- {
 
-   // paint frame/background
 
-   {
 
-     TAutoFlag ParentPaintingFlag(FParentPainting);
 
-     THintWindow::Paint();
 
-   }
 
-   const int Flags = GetTextFlags(this);
 
-   const int Margin = FMargin - 1; // 1 = border
 
-   TRect Rect = ClientRect;
 
-   Rect.Inflate(-Margin, -Margin);
 
-   if (!FHintPopup)
 
-   {
 
-     Rect.Right -= FMargin;
 
-   }
 
-   if (UseBoldShortHint(FHintControl))
 
-   {
 
-     Canvas->Font->Style = TFontStyles() << fsBold;
 
-   }
 
-   DrawText(Canvas->Handle, FShortHint.c_str(), -1, &Rect, Flags);
 
-   TRect ShortRect = Rect;
 
-   DrawText(Canvas->Handle, FShortHint.c_str(), -1, &ShortRect, DT_CALCRECT | Flags);
 
-   Canvas->Font->Style = TFontStyles();
 
-   if (!FLongHint.IsEmpty())
 
-   {
 
-     Rect.Left += FMargin * 3 / 2;
 
-     Rect.Top += ShortRect.Height() + (FMargin / 3 * 5);
 
-     DrawText(Canvas->Handle, FLongHint.c_str(), -1, &Rect, Flags);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- static int HideAccelFlag(TControl * Control)
 
- {
 
-   //ask the top level window about its UI state
 
-   while (Control->Parent != NULL)
 
-   {
 
-     Control = Control->Parent;
 
-   }
 
-   int Result;
 
-   if (FLAGSET(Control->Perform(WM_QUERYUISTATE, 0, 0), UISF_HIDEACCEL))
 
-   {
 
-     Result = DT_HIDEPREFIX;
 
-   }
 
-   else
 
-   {
 
-     Result = 0;
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- //---------------------------------------------------------------------------
 
- class TDarkUxThemeStyle : public TUxThemeStyle
 
- {
 
- public:
 
-   static TDarkUxThemeStyle * Instance();
 
- protected:
 
-   virtual bool __fastcall DoDrawText(
 
-     HDC DC, const TThemedElementDetails & Details, const UnicodeString S, TRect & R, TTextFormat Flags,
 
-     const TStyleTextOptions & Options, int DPI);
 
- private:
 
-   static std::unique_ptr<TDarkUxThemeStyle> FInstance;
 
- };
 
- //---------------------------------------------------------------------------
 
- std::unique_ptr<TDarkUxThemeStyle> TDarkUxThemeStyle::FInstance;
 
- //---------------------------------------------------------------------------
 
- TDarkUxThemeStyle * TDarkUxThemeStyle::Instance()
 
- {
 
-   if (FInstance.get() == NULL)
 
-   {
 
-     FInstance.reset(new TDarkUxThemeStyle());
 
-   }
 
-   return FInstance.get();
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TDarkUxThemeStyle::DoDrawText(
 
-   HDC DC, const TThemedElementDetails & Details, const UnicodeString S, TRect & R, TTextFormat Flags,
 
-   const TStyleTextOptions & AOptions, int DPI)
 
- {
 
-   TStyleTextOptions Options = AOptions;
 
-   Options.TextColor = GetWindowTextColor(GetBtnFaceColor());
 
-   Options.Flags << stfTextColor;
 
-   return TUxThemeStyle::DoDrawText(DC, Details, S, R, Flags, Options, DPI);
 
- }
 
- //---------------------------------------------------------------------------
 
- //---------------------------------------------------------------------------
 
- // For controls that need both custom VCL theme style and custom Windows theme.
 
- // Currently the checkbox (custom VCL theme for light caption and custom Windows theme for dark more checkbox bitmaps).
 
- // Current this handles only "button" theme.
 
- // It's a singleton and can be used only for controls that share the same Windows theme (curently used with "explorer").
 
- class TDarkExplorerUxThemeStyle : public TDarkUxThemeStyle
 
- {
 
- public:
 
-   static const UnicodeString ThemeName;
 
-   virtual __fastcall TDarkExplorerUxThemeStyle();
 
-   virtual __fastcall ~TDarkExplorerUxThemeStyle();
 
-   void SetControl(TWinControl * Control);
 
- protected:
 
-   virtual NativeUInt __fastcall GetTheme(TThemedElement Element);
 
-   virtual NativeUInt __fastcall GetThemeForDPI(TThemedElement Element, int DPI);
 
-   virtual void __fastcall UpdateThemes();
 
-   virtual bool __fastcall DoDrawText(
 
-     HDC DC, const TThemedElementDetails & Details, const UnicodeString S, TRect & R, TTextFormat Flags,
 
-     const TStyleTextOptions & Options, int DPI);
 
- protected:
 
-   typedef std::map<int, NativeUInt> TThemes;
 
-   TThemes FThemes;
 
-   bool FInitialized;
 
-   HWND FControlHandle;
 
-   int FTextFlags;
 
-   bool DoGetTheme(TThemedElement Element, int DPI, NativeUInt & Result);
 
-   void Clear();
 
- };
 
- //---------------------------------------------------------------------------
 
- const UnicodeString TDarkExplorerUxThemeStyle::ThemeName = L"explorer";
 
- //---------------------------------------------------------------------------
 
- __fastcall TDarkExplorerUxThemeStyle::TDarkExplorerUxThemeStyle()
 
- {
 
-   FControlHandle = NULL;
 
-   FTextFlags = 0;
 
-   FInitialized = true;
 
- }
 
- //---------------------------------------------------------------------------
 
- __fastcall TDarkExplorerUxThemeStyle::~TDarkExplorerUxThemeStyle()
 
- {
 
-   Clear();
 
- }
 
- //---------------------------------------------------------------------------
 
- void TDarkExplorerUxThemeStyle::SetControl(TWinControl * Control)
 
- {
 
-   // Handle is safer than a pointer
 
-   if (Control->HandleAllocated())
 
-   {
 
-     FControlHandle = Control->Handle;
 
-     // WORKAROUND - VCL does not hide accelerator of custom-styled (at least) checkboxes
 
-     FTextFlags = HideAccelFlag(Control);
 
-   }
 
-   else
 
-   {
 
-     FControlHandle = 0;
 
-     FTextFlags = 0;
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- NativeUInt __fastcall TDarkExplorerUxThemeStyle::GetTheme(TThemedElement Element)
 
- {
 
-   NativeUInt Result;
 
-   if (!DoGetTheme(Element, 0, Result))
 
-   {
 
-     Result = TDarkUxThemeStyle::GetTheme(Element);
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- NativeUInt __fastcall TDarkExplorerUxThemeStyle::GetThemeForDPI(TThemedElement Element, int DPI)
 
- {
 
-   NativeUInt Result;
 
-   if (!DoGetTheme(Element, DPI, Result))
 
-   {
 
-     Result = TDarkUxThemeStyle::GetThemeForDPI(Element, DPI);
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- bool TDarkExplorerUxThemeStyle::DoGetTheme(TThemedElement Element, int DPI, NativeUInt & Result)
 
- {
 
-   bool Handle = DebugAlwaysTrue(FControlHandle != 0) && DebugAlwaysTrue(Element == teButton);
 
-   if (Handle)
 
-   {
 
-     if (DPI == 0)
 
-     {
 
-       DPI = Screen->PixelsPerInch;
 
-     }
 
-     TThemes::const_iterator ITheme = FThemes.find(DPI);
 
-     if (ITheme != FThemes.end())
 
-     {
 
-       Result = ITheme->second;
 
-     }
 
-     else
 
-     {
 
-       if (!UseThemes())
 
-       {
 
-         Result = 0;
 
-       }
 
-       else
 
-       {
 
-         HTHEME Theme;
 
-         const wchar_t * ClassName = L"button";
 
-         if (!IsWin10() || !IsWin10Build(15063) || (DPI == Screen->PixelsPerInch))
 
-         {
 
-           Theme = OpenThemeData(FControlHandle, ClassName);
 
-         }
 
-         else
 
-         {
 
-           Theme = OpenThemeDataForDpi(FControlHandle, ClassName, DPI);
 
-         }
 
-         Result = reinterpret_cast<NativeUInt>(Theme);
 
-       }
 
-       FThemes.insert(std::make_pair(DPI, Result));
 
-     }
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- void TDarkExplorerUxThemeStyle::Clear()
 
- {
 
-   TThemes Themes;
 
-   // TUxThemeStyle.UnloadThemeData implementation (FThemeDataUnLoading) imply
 
-   // that GetTheme might be called while unloading, so protecting against that
 
-   FThemes.swap(Themes);
 
-   TThemes::iterator I = Themes.begin();
 
-   while (I != Themes.end())
 
-   {
 
-     CloseThemeData(reinterpret_cast<HTHEME>(I->second));
 
-     ++I;
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TDarkExplorerUxThemeStyle::UpdateThemes()
 
- {
 
-   TDarkUxThemeStyle::UpdateThemes();
 
-   // This is only ever called from the constructor, where FThemes is not initialized yet.
 
-   // It's not called for theme updates, as it is called only for the detault global app theme instance.
 
-   if (DebugAlwaysFalse(FInitialized))
 
-   {
 
-     Clear();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TDarkExplorerUxThemeStyle::DoDrawText(
 
-   HDC DC, const TThemedElementDetails & Details, const UnicodeString S, TRect & R, TTextFormat Flags,
 
-   const TStyleTextOptions & Options, int DPI)
 
- {
 
-   int TextFlags = FTextFlags;
 
-   if (FLAGSET(TextFlags, DT_HIDEPREFIX))
 
-   {
 
-     TextFlags -= DT_HIDEPREFIX;
 
-     Flags << tfHidePrefix;
 
-   }
 
-   DebugAssert(TextFlags == 0);
 
-   DebugUsedParam(TextFlags);
 
-   return TDarkUxThemeStyle::DoDrawText(DC, Details, S, R, Flags, Options, DPI);
 
- }
 
- //---------------------------------------------------------------------------
 
- static std::unique_ptr<TDarkExplorerUxThemeStyle> DarkExplorerUxThemeStyle;
 
- //---------------------------------------------------------------------------
 
- //---------------------------------------------------------------------------
 
- // Based on:
 
- // https://stackoverflow.com/q/6912424/850848
 
- // https://stackoverflow.com/q/4685863/850848
 
- class TUIStateAwareLabel : public TLabel
 
- {
 
- public:
 
-   __fastcall virtual TUIStateAwareLabel(TComponent * AOwner);
 
- protected:
 
-   DYNAMIC void __fastcall DoDrawText(TRect & Rect, int Flags);
 
-   virtual void __fastcall Dispatch(void * AMessage);
 
- };
 
- //---------------------------------------------------------------------------
 
- TLabel * CreateLabel(TComponent * AOwner)
 
- {
 
-   return new TUIStateAwareLabel(AOwner);
 
- }
 
- //---------------------------------------------------------------------------
 
- __fastcall TUIStateAwareLabel::TUIStateAwareLabel(TComponent * AOwner) :
 
-   TLabel(AOwner)
 
- {
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TUIStateAwareLabel::DoDrawText(TRect & Rect, int Flags)
 
- {
 
-   if (ShowAccelChar)
 
-   {
 
-     Flags = Flags | HideAccelFlag(this);
 
-   }
 
-   TLabel::DoDrawText(Rect, Flags);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TUIStateAwareLabel::Dispatch(void * AMessage)
 
- {
 
-   TMessage * Message = static_cast<TMessage*>(AMessage);
 
-   // WORKAROUND: Particularly when focusing csDropDownList-style combobox via label, there's no visual feedback
 
-   // that the combobox was selected (strangely, there is, when the previous focus was on TTreeView).
 
-   // For consistency, we enable focus display for all controls types (like checkboxes).
 
-   if (Message->Msg == CM_DIALOGCHAR)
 
-   {
 
-     bool WasFocused = (FocusControl != NULL) ? FocusControl->Focused() : false;
 
-     TLabel::Dispatch(AMessage);
 
-     if (!WasFocused && (FocusControl != NULL) && FocusControl->Focused())
 
-     {
 
-       TCustomForm * ParentForm = GetParentForm(this);
 
-       if (ParentForm != NULL)
 
-       {
 
-         ParentForm->Perform(WM_CHANGEUISTATE, MAKELONG(UIS_CLEAR, UISF_HIDEFOCUS), 0);
 
-       }
 
-     }
 
-   }
 
-   else
 
-   {
 
-     TLabel::Dispatch(AMessage);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- //---------------------------------------------------------------------------
 
- template<typename T>
 
- bool HandleMessageByDarkStyleHook(TMessage & Msg, TWinControl * Control, std::unique_ptr<T> & StyleHook)
 
- {
 
-   bool Result = false;
 
-   if (Control->HandleAllocated() &&
 
-       !Control->ComponentState.Contains(csDestroying) &&
 
-       !Control->ControlState.Contains(csDestroyingHandle) &&
 
-       !Control->ControlStyle.Contains(csOverrideStylePaint) &&
 
-       UseDarkModeForControl(Control))
 
-   {
 
-     if (StyleHook.get() == NULL)
 
-     {
 
-       StyleHook.reset(new T(Control));
 
-     }
 
-     Result = StyleHook->HandleMessage(Msg);
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- void SetColorModeTheme(TWinControl * Control, const UnicodeString & DarkSubAppName)
 
- {
 
-   if (UseDarkModeForControl(Control))
 
-   {
 
-     SetDarkModeTheme(Control, DarkSubAppName);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void SetExplorerTheme(TWinControl * Button)
 
- {
 
-   SetColorModeTheme(Button, TDarkExplorerUxThemeStyle::ThemeName);
 
- }
 
- //---------------------------------------------------------------------------
 
- //---------------------------------------------------------------------------
 
- class TDarkGroupBoxStyleHook : public TGroupBoxStyleHook
 
- {
 
- public:
 
-   __fastcall virtual TDarkGroupBoxStyleHook(TWinControl * AControl) :
 
-     TGroupBoxStyleHook(AControl)
 
-   {
 
-   }
 
- protected:
 
-   virtual TCustomStyleServices * __fastcall StyleServices()
 
-   {
 
-     return TDarkUxThemeStyle::Instance();
 
-   }
 
- };
 
- //---------------------------------------------------------------------------
 
- class TGroupBoxEx : public TGroupBox
 
- {
 
- protected:
 
-   virtual void __fastcall PaintWindow(HDC DC);
 
-   virtual void __fastcall WndProc(TMessage & Msg);
 
- private:
 
-   std::unique_ptr<TDarkGroupBoxStyleHook> FStyleHook;
 
- };
 
- //---------------------------------------------------------------------------
 
- void __fastcall TGroupBoxEx::WndProc(TMessage & Msg)
 
- {
 
-   if (!HandleMessageByDarkStyleHook(Msg, this, FStyleHook))
 
-   {
 
-     TGroupBox::WndProc(Msg);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TGroupBoxEx::PaintWindow(HDC DC)
 
- {
 
-   if (UseDarkModeForControl(this))
 
-   {
 
-     std::unique_ptr<TCanvas> Canvas(new TCanvas());
 
-     Canvas->Handle = DC;
 
-     Canvas->Font = Font;
 
-     TRect Rect = ClientRect;
 
-     Canvas->Brush->Style = bsSolid;
 
-     Canvas->Brush->Color = GetBtnFaceColor();
 
-     Canvas->FillRect(Rect);
 
-   }
 
-   TGroupBox::PaintWindow(DC);
 
- }
 
- //---------------------------------------------------------------------------
 
- //---------------------------------------------------------------------------
 
- class TDarkCheckBoxStyleHook : public TCheckBoxStyleHook
 
- {
 
- public:
 
-   __fastcall virtual TDarkCheckBoxStyleHook(TWinControl * AControl);
 
- protected:
 
-   virtual void __fastcall PaintBackground(TCanvas * Canvas);
 
-   virtual TCustomStyleServices * __fastcall StyleServices();
 
- };
 
- //---------------------------------------------------------------------------
 
- class TCheckBoxEx : public TCheckBox
 
- {
 
- public:
 
-   __fastcall virtual TCheckBoxEx(TComponent * AOwner);
 
- protected:
 
-   virtual void __fastcall WndProc(TMessage & Msg);
 
-   virtual void __fastcall CreateWnd();
 
- private:
 
-   std::unique_ptr<TDarkCheckBoxStyleHook> FStyleHook;
 
- };
 
- //---------------------------------------------------------------------------
 
- TCheckBox * CreateCheckBox(TComponent * AOwner)
 
- {
 
-   return new TCheckBoxEx(AOwner);
 
- }
 
- //---------------------------------------------------------------------------
 
- __fastcall TDarkCheckBoxStyleHook::TDarkCheckBoxStyleHook(TWinControl * AControl) :
 
-   TCheckBoxStyleHook(AControl)
 
- {
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TDarkCheckBoxStyleHook::PaintBackground(TCanvas * Canvas)
 
- {
 
-   Canvas->Brush->Style = bsSolid;
 
-   Canvas->Brush->Color = GetBtnFaceColor();
 
-   Canvas->FillRect(Rect(0, 0, Control->Width, Control->Height));
 
- }
 
- //---------------------------------------------------------------------------
 
- TCustomStyleServices * __fastcall TDarkCheckBoxStyleHook::StyleServices()
 
- {
 
-   if (DarkExplorerUxThemeStyle.get() == NULL)
 
-   {
 
-     DarkExplorerUxThemeStyle.reset(new TDarkExplorerUxThemeStyle());
 
-   }
 
-   DarkExplorerUxThemeStyle->SetControl(Control);
 
-   return DarkExplorerUxThemeStyle.get();
 
- }
 
- //---------------------------------------------------------------------------
 
- //---------------------------------------------------------------------------
 
- __fastcall TCheckBoxEx::TCheckBoxEx(TComponent * AOwner) :
 
-   TCheckBox(AOwner)
 
- {
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TCheckBoxEx::CreateWnd()
 
- {
 
-   TCheckBox::CreateWnd();
 
-   SetColorModeTheme(this, TDarkExplorerUxThemeStyle::ThemeName);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TCheckBoxEx::WndProc(TMessage & Msg)
 
- {
 
-   if (!HandleMessageByDarkStyleHook(Msg, this, FStyleHook))
 
-   {
 
-     TCheckBox::WndProc(Msg);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- //---------------------------------------------------------------------------
 
- class TButtonEx : public TButton
 
- {
 
- public:
 
-   __fastcall virtual TButtonEx(TComponent * AOwner);
 
- protected:
 
-   virtual void __fastcall CreateWnd();
 
- };
 
- //---------------------------------------------------------------------------
 
- __fastcall TButtonEx::TButtonEx(TComponent * AOwner) :
 
-   TButton(AOwner)
 
- {
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TButtonEx::CreateWnd()
 
- {
 
-   TButton::CreateWnd();
 
-   SetExplorerTheme(this);
 
- }
 
- //---------------------------------------------------------------------------
 
- TButton * CreateButton(TComponent * AOwner)
 
- {
 
-   return new TButtonEx(AOwner);
 
- }
 
- //---------------------------------------------------------------------------
 
- //---------------------------------------------------------------------------
 
- class TEditEx : public TEdit
 
- {
 
- public:
 
-   __fastcall virtual TEditEx(TComponent * AOwner);
 
- protected:
 
-   virtual void __fastcall CreateWnd();
 
- };
 
- //---------------------------------------------------------------------------
 
- __fastcall TEditEx::TEditEx(TComponent * AOwner) :
 
-   TEdit(AOwner)
 
- {
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TEditEx::CreateWnd()
 
- {
 
-   TEdit::CreateWnd();
 
-   SetColorModeTheme(this, L"CFD");
 
- }
 
- //---------------------------------------------------------------------------
 
- TEdit * CreateEdit(TComponent * AOwner)
 
- {
 
-   return new TEditEx(AOwner);
 
- }
 
- //---------------------------------------------------------------------------
 
- //---------------------------------------------------------------------------
 
- class TMemoEx : public TMemo
 
- {
 
- public:
 
-   __fastcall virtual TMemoEx(TComponent * AOwner);
 
- protected:
 
-   virtual void __fastcall CreateWnd();
 
- };
 
- //---------------------------------------------------------------------------
 
- __fastcall TMemoEx::TMemoEx(TComponent * AOwner) :
 
-   TMemo(AOwner)
 
- {
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TMemoEx::CreateWnd()
 
- {
 
-   TMemo::CreateWnd();
 
-   SetColorModeTheme(this, L"CFD");
 
- }
 
- //---------------------------------------------------------------------------
 
- TMemo * CreateMemo(TComponent * AOwner)
 
- {
 
-   return new TMemoEx(AOwner);
 
- }
 
- //---------------------------------------------------------------------------
 
- //---------------------------------------------------------------------------
 
- void __fastcall FindComponentClass(
 
-   void *, TReader *, const UnicodeString DebugUsedArg(ClassName), TComponentClass & ComponentClass)
 
- {
 
-   if (ComponentClass == __classid(TLabel))
 
-   {
 
-     ComponentClass = __classid(TUIStateAwareLabel);
 
-   }
 
-   else if (ComponentClass == __classid(TComboBox))
 
-   {
 
-     ComponentClass = __classid(TUIStateAwareComboBox);
 
-   }
 
-   else if (ComponentClass == __classid(TGroupBox))
 
-   {
 
-     ComponentClass = __classid(TGroupBoxEx);
 
-   }
 
-   else if (ComponentClass == __classid(TCheckBox))
 
-   {
 
-     ComponentClass = __classid(TCheckBoxEx);
 
-   }
 
-   else if (ComponentClass == __classid(TButton))
 
-   {
 
-     ComponentClass = __classid(TButtonEx);
 
-   }
 
-   else if (ComponentClass == __classid(TEdit))
 
-   {
 
-     ComponentClass = __classid(TEditEx);
 
-   }
 
-   else if (ComponentClass == __classid(TMemo))
 
-   {
 
-     ComponentClass = __classid(TMemoEx);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- bool CanShowTimeEstimate(TDateTime StartTime)
 
- {
 
-   return (SecondsBetween(StartTime, Now()) >= 3);
 
- }
 
- //---------------------------------------------------------------------------
 
- class TSystemRequiredThread : public TSignalThread
 
- {
 
- public:
 
-   TSystemRequiredThread();
 
-   void Required();
 
- protected:
 
-   virtual bool __fastcall WaitForEvent();
 
-   virtual void __fastcall ProcessEvent();
 
- private:
 
-   bool FRequired;
 
-   TDateTime FLastRequired;
 
- };
 
- //---------------------------------------------------------------------------
 
- static std::unique_ptr<TCriticalSection> SystemRequiredThreadSection(TraceInitPtr(new TCriticalSection()));
 
- static TSystemRequiredThread * SystemRequiredThread = NULL;
 
- //---------------------------------------------------------------------------
 
- TSystemRequiredThread::TSystemRequiredThread() :
 
-   TSignalThread(true), FRequired(false)
 
- {
 
- }
 
- //---------------------------------------------------------------------------
 
- void TSystemRequiredThread::Required()
 
- {
 
-   // guarded in SystemRequired()
 
-   FLastRequired = Now();
 
-   TriggerEvent();
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TSystemRequiredThread::WaitForEvent()
 
- {
 
-   const int ExpireInterval = 5000;
 
-   bool Result = (TSignalThread::WaitForEvent(ExpireInterval) > 0);
 
-   if (!Result && !FTerminated)
 
-   {
 
-     TGuard Guard(SystemRequiredThreadSection.get());
 
-     if (!FTerminated && FRequired &&
 
-         (MilliSecondsBetween(Now(), FLastRequired) > ExpireInterval))
 
-     {
 
-       AppLog(L"System is not required");
 
-       SetThreadExecutionState(ES_CONTINUOUS);
 
-       FLastRequired = TDateTime();
 
-       FRequired = false;
 
-     }
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSystemRequiredThread::ProcessEvent()
 
- {
 
-   TGuard Guard(SystemRequiredThreadSection.get());
 
-   if (!FRequired &&
 
-       (FLastRequired != TDateTime()))
 
-   {
 
-     AppLog(L"System is required");
 
-     SetThreadExecutionState(ES_SYSTEM_REQUIRED | ES_CONTINUOUS);
 
-     FRequired = true;
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void SystemRequired()
 
- {
 
-   if (IsWin11())
 
-   {
 
-     TGuard Guard(SystemRequiredThreadSection.get());
 
-     if (SystemRequiredThread == NULL)
 
-     {
 
-       AppLog(L"Starting system required thread");
 
-       SystemRequiredThread = new TSystemRequiredThread();
 
-       SystemRequiredThread->Start();
 
-     }
 
-     SystemRequiredThread->Required();
 
-   }
 
-   else
 
-   {
 
-     SetThreadExecutionState(ES_SYSTEM_REQUIRED);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void GUIFinalize()
 
- {
 
-   TPuttyCleanupThread::Finalize();
 
-   TSystemRequiredThread * Thread;
 
-   {
 
-     TGuard Guard(SystemRequiredThreadSection.get());
 
-     Thread = SystemRequiredThread;
 
-     SystemRequiredThread = NULL;
 
-   }
 
-   if (Thread != NULL)
 
-   {
 
-     AppLog(L"Stopping system required thread");
 
-     Thread->Terminate();
 
-     Thread->WaitFor();
 
-     delete Thread;
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- TCustomImageList * TreeViewImageList(TPngImageList * ImageList)
 
- {
 
-   // WORKAROUND Prevent DPI scaling, see TCustomTreeView.SetImages
 
-   ImageList->Scaled = true;
 
-   return ImageList;
 
- }
 
 
  |