GUITools.cpp 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <shlobj.h>
  5. #include <mshtmhst.h>
  6. #include <Common.h>
  7. #include "GUITools.h"
  8. #include "WinConfiguration.h"
  9. #include <TextsCore.h>
  10. #include <TextsWin.h>
  11. #include <CoreMain.h>
  12. #include <SessionData.h>
  13. #include <WinInterface.h>
  14. #include <TbxUtils.hpp>
  15. #include <Math.hpp>
  16. #include <WebBrowserEx.hpp>
  17. #include <Tools.h>
  18. #include "PngImageList.hpp"
  19. #include <StrUtils.hpp>
  20. #include <limits>
  21. #include <Glyphs.h>
  22. #include <PasTools.hpp>
  23. #include <VCLCommon.h>
  24. #include <WinApi.h>
  25. #include <Vcl.ScreenTips.hpp>
  26. #include <HistoryComboBox.hpp>
  27. #include <vssym32.h>
  28. #include <DateUtils.hpp>
  29. #include <IOUtils.hpp>
  30. #include <Queue.h>
  31. #include "Animations96.h"
  32. #include "Animations120.h"
  33. #include "Animations144.h"
  34. #include "Animations192.h"
  35. //---------------------------------------------------------------------------
  36. #pragma package(smart_init)
  37. //---------------------------------------------------------------------------
  38. extern const UnicodeString PageantTool = L"pageant.exe";
  39. extern const UnicodeString PuttygenTool = L"puttygen.exe";
  40. //---------------------------------------------------------------------------
  41. bool __fastcall FindFile(UnicodeString & Path)
  42. {
  43. bool Result = FileExistsFix(Path);
  44. if (!Result)
  45. {
  46. UnicodeString ProgramFiles32 = IncludeTrailingBackslash(GetEnvironmentVariable(L"ProgramFiles"));
  47. UnicodeString ProgramFiles64 = IncludeTrailingBackslash(GetEnvironmentVariable(L"ProgramW6432"));
  48. if (!ProgramFiles32.IsEmpty() &&
  49. SameText(Path.SubString(1, ProgramFiles32.Length()), ProgramFiles32) &&
  50. !ProgramFiles64.IsEmpty())
  51. {
  52. UnicodeString Path64 =
  53. ProgramFiles64 + Path.SubString(ProgramFiles32.Length() + 1, Path.Length() - ProgramFiles32.Length());
  54. if (FileExists(ApiPath(Path64)))
  55. {
  56. Path = Path64;
  57. Result = true;
  58. }
  59. }
  60. }
  61. if (!Result && SameText(ExtractFileName(Path), Path))
  62. {
  63. UnicodeString Paths = GetEnvironmentVariable(L"PATH");
  64. if (!Paths.IsEmpty())
  65. {
  66. UnicodeString NewPath = FileSearch(Path, Paths);
  67. Result = !NewPath.IsEmpty();
  68. if (Result)
  69. {
  70. Path = NewPath;
  71. }
  72. else
  73. {
  74. // Basically the same what FileSearch does, except for FileExistsFix.
  75. // Once this proves working, we can ged rid of the FileSearch call.
  76. while (!Result && !Paths.IsEmpty())
  77. {
  78. UnicodeString P = CutToChar(Paths, L';', false);
  79. // Not using TPath::Combine as it throws on an invalid path and PATH is not under our control
  80. NewPath = IncludeTrailingBackslash(P) + Path;
  81. Result = FileExistsFix(NewPath);
  82. if (Result)
  83. {
  84. Path = NewPath;
  85. }
  86. }
  87. }
  88. }
  89. }
  90. return Result;
  91. }
  92. //---------------------------------------------------------------------------
  93. bool DoesSessionExistInPutty(const UnicodeString & StorageKey)
  94. {
  95. std::unique_ptr<TRegistryStorage> Storage(new TRegistryStorage(Configuration->PuttySessionsKey));
  96. Storage->ConfigureForPutty();
  97. return Storage->OpenRootKey(true) && Storage->KeyExists(StorageKey);
  98. }
  99. //---------------------------------------------------------------------------
  100. bool __fastcall ExportSessionToPutty(TSessionData * SessionData, bool ReuseExisting, const UnicodeString & SessionName)
  101. {
  102. bool Result = true;
  103. std::unique_ptr<TRegistryStorage> Storage(new TRegistryStorage(Configuration->PuttySessionsKey));
  104. Storage->AccessMode = smReadWrite;
  105. Storage->ConfigureForPutty();
  106. if (Storage->OpenRootKey(true))
  107. {
  108. Result = ReuseExisting && Storage->KeyExists(SessionData->StorageKey);
  109. if (!Result)
  110. {
  111. std::unique_ptr<TRegistryStorage> SourceStorage(new TRegistryStorage(Configuration->PuttySessionsKey));
  112. SourceStorage->ConfigureForPutty();
  113. if (SourceStorage->OpenSubKey(StoredSessions->DefaultSettings->Name, false) &&
  114. Storage->OpenSubKey(SessionName, true))
  115. {
  116. Storage->Copy(SourceStorage.get());
  117. Storage->CloseSubKey();
  118. }
  119. std::unique_ptr<TSessionData> ExportData(new TSessionData(L""));
  120. ExportData->Assign(SessionData);
  121. ExportData->Modified = true;
  122. ExportData->Name = SessionName;
  123. ExportData->WinTitle = SessionData->SessionName;
  124. ExportData->Password = L"";
  125. if (SessionData->FSProtocol == fsFTP)
  126. {
  127. if (GUIConfiguration->TelnetForFtpInPutty)
  128. {
  129. ExportData->PuttyProtocol = PuttyTelnetProtocol;
  130. ExportData->PortNumber = TelnetPortNumber;
  131. }
  132. else
  133. {
  134. ExportData->PuttyProtocol = PuttySshProtocol;
  135. ExportData->PortNumber = SshPortNumber;
  136. }
  137. }
  138. ExportData->Save(Storage.get(), true);
  139. }
  140. }
  141. return Result;
  142. }
  143. //---------------------------------------------------------------------------
  144. class TPuttyCleanupThread : public TSimpleThread
  145. {
  146. public:
  147. static void Schedule();
  148. static void Finalize();
  149. protected:
  150. virtual void __fastcall Execute();
  151. virtual void __fastcall Terminate();
  152. bool __fastcall Finished();
  153. void DoSchedule();
  154. private:
  155. TDateTime FTimer;
  156. static TPuttyCleanupThread * FInstance;
  157. static std::unique_ptr<TCriticalSection> FSection;
  158. };
  159. //---------------------------------------------------------------------------
  160. std::unique_ptr<TCriticalSection> TPuttyCleanupThread::FSection(TraceInitPtr(new TCriticalSection()));
  161. TPuttyCleanupThread * TPuttyCleanupThread::FInstance;
  162. //---------------------------------------------------------------------------
  163. void TPuttyCleanupThread::Schedule()
  164. {
  165. TGuard Guard(FSection.get());
  166. if (FInstance == NULL)
  167. {
  168. FInstance = new TPuttyCleanupThread();
  169. FInstance->DoSchedule();
  170. FInstance->Start();
  171. }
  172. else
  173. {
  174. FInstance->DoSchedule();
  175. }
  176. }
  177. //---------------------------------------------------------------------------
  178. void TPuttyCleanupThread::Finalize()
  179. {
  180. while (true)
  181. {
  182. {
  183. TGuard Guard(FSection.get());
  184. if (FInstance == NULL)
  185. {
  186. return;
  187. }
  188. }
  189. Sleep(100);
  190. }
  191. }
  192. //---------------------------------------------------------------------------
  193. void __fastcall TPuttyCleanupThread::Execute()
  194. {
  195. try
  196. {
  197. std::unique_ptr<TStrings> Sessions(new TStringList());
  198. bool Continue = true;
  199. do
  200. {
  201. {
  202. TGuard Guard(FSection.get());
  203. std::unique_ptr<TRegistryStorage> Storage(new TRegistryStorage(Configuration->PuttySessionsKey));
  204. Storage->AccessMode = smReadWrite;
  205. Storage->ConfigureForPutty();
  206. std::unique_ptr<TStringList> Sessions2(new TStringList());
  207. if (Storage->OpenRootKey(true))
  208. {
  209. std::unique_ptr<TStrings> SubKeys(new TStringList());
  210. Storage->GetSubKeyNames(SubKeys.get());
  211. for (int Index = 0; Index < SubKeys->Count; Index++)
  212. {
  213. UnicodeString SessionName = SubKeys->Strings[Index];
  214. if (StartsStr(GUIConfiguration->PuttySession, SessionName))
  215. {
  216. Sessions2->Add(SessionName);
  217. }
  218. }
  219. Sessions2->Sort();
  220. if (!Sessions->Equals(Sessions2.get()))
  221. {
  222. // Just in case new sessions from another WinSCP instance are added, delay the cleanup
  223. // (to avoid having to implement some inter-process communication).
  224. // Both instances will attempt to do the cleanup, but that not a problem
  225. Sessions->Assign(Sessions2.get());
  226. DoSchedule();
  227. }
  228. }
  229. if (FTimer < Now())
  230. {
  231. for (int Index = 0; Index < Sessions->Count; Index++)
  232. {
  233. UnicodeString SessionName = Sessions->Strings[Index];
  234. Storage->RecursiveDeleteSubKey(SessionName);
  235. }
  236. Continue = false;
  237. }
  238. }
  239. if (Continue)
  240. {
  241. Sleep(1000);
  242. }
  243. }
  244. while (Continue);
  245. }
  246. __finally
  247. {
  248. TGuard Guard(FSection.get());
  249. FInstance = NULL;
  250. }
  251. }
  252. //---------------------------------------------------------------------------
  253. void __fastcall TPuttyCleanupThread::Terminate()
  254. {
  255. DebugFail();
  256. }
  257. //---------------------------------------------------------------------------
  258. bool __fastcall TPuttyCleanupThread::Finished()
  259. {
  260. // self-destroy
  261. return TSimpleThread::Finished() || true;
  262. }
  263. //---------------------------------------------------------------------------
  264. void TPuttyCleanupThread::DoSchedule()
  265. {
  266. FTimer = IncSecond(Now(), 10);
  267. }
  268. //---------------------------------------------------------------------------
  269. void OpenSessionInPutty(TSessionData * SessionData)
  270. {
  271. // putty does not support resolving environment variables in session settings
  272. SessionData->ExpandEnvironmentVariables();
  273. // See also TSiteAdvancedDialog::PuttySettingsButtonClick
  274. UnicodeString Program, AParams, Dir;
  275. SplitCommand(GUIConfiguration->PuttyPath, Program, AParams, Dir);
  276. Program = ExpandEnvironmentVariables(Program);
  277. if (FindFile(Program))
  278. {
  279. AParams = ExpandEnvironmentVariables(AParams);
  280. UnicodeString Password;
  281. if (GUIConfiguration->PuttyPassword)
  282. {
  283. // Passphrase has precendence, as it's more likely entered by user during authentication, hence more likely really needed.
  284. if (!SessionData->Passphrase.IsEmpty())
  285. {
  286. Password = SessionData->Passphrase;
  287. }
  288. else if (!SessionData->Password.IsEmpty())
  289. {
  290. Password = SessionData->Password;
  291. }
  292. }
  293. TCustomCommandData Data(SessionData, SessionData->UserName, Password);
  294. TLocalCustomCommand LocalCustomCommand(Data, SessionData->RemoteDirectory, SessionData->LocalDirectory);
  295. TWinInteractiveCustomCommand InteractiveCustomCommand(
  296. &LocalCustomCommand, L"PuTTY", UnicodeString());
  297. UnicodeString Params =
  298. LocalCustomCommand.Complete(InteractiveCustomCommand.Complete(AParams, false), true);
  299. UnicodeString PuttyParams;
  300. if (!LocalCustomCommand.IsSiteCommand(AParams))
  301. {
  302. {
  303. bool SessionList = false;
  304. std::unique_ptr<THierarchicalStorage> SourceHostKeyStorage(Configuration->CreateScpStorage(SessionList));
  305. std::unique_ptr<THierarchicalStorage> TargetHostKeyStorage(new TRegistryStorage(Configuration->PuttyRegistryStorageKey));
  306. TargetHostKeyStorage->Explicit = true;
  307. TargetHostKeyStorage->AccessMode = smReadWrite;
  308. std::unique_ptr<TStoredSessionList> HostKeySessionList(new TStoredSessionList());
  309. HostKeySessionList->OwnsObjects = false;
  310. HostKeySessionList->Add(SessionData);
  311. TStoredSessionList::ImportHostKeys(SourceHostKeyStorage.get(), TargetHostKeyStorage.get(), HostKeySessionList.get(), false);
  312. }
  313. if (IsUWP())
  314. {
  315. bool Telnet = (SessionData->FSProtocol == fsFTP) && GUIConfiguration->TelnetForFtpInPutty;
  316. if (Telnet)
  317. {
  318. AddToList(PuttyParams, L"-telnet", L" ");
  319. // PuTTY does not allow -pw for telnet
  320. Password = L"";
  321. }
  322. AddToList(PuttyParams, EscapePuttyCommandParam(SessionData->HostName), L" ");
  323. if (!SessionData->UserName.IsEmpty())
  324. {
  325. AddToList(PuttyParams, FORMAT(L"-l %s", (EscapePuttyCommandParam(SessionData->UserName))), L" ");
  326. }
  327. if ((SessionData->FSProtocol != fsFTP) && (SessionData->PortNumber != SshPortNumber))
  328. {
  329. AddToList(PuttyParams, FORMAT(L"-P %d", (SessionData->PortNumber)), L" ");
  330. }
  331. if (!Telnet)
  332. {
  333. if (!SessionData->PublicKeyFile.IsEmpty())
  334. {
  335. AddToList(PuttyParams, FORMAT(L"-i \"%s\"", (SessionData->PublicKeyFile)), L" ");
  336. }
  337. AddToList(PuttyParams, (SessionData->TryAgent ? L"-agent" : L"-noagent"), L" ");
  338. if (SessionData->TryAgent)
  339. {
  340. AddToList(PuttyParams, (SessionData->AgentFwd ? L"-A" : L"-a"), L" ");
  341. }
  342. if (SessionData->Compression)
  343. {
  344. AddToList(PuttyParams, L"-C", L" ");
  345. }
  346. AddToList(PuttyParams, L"-2", L" ");
  347. if (!SessionData->LogicalHostName.IsEmpty())
  348. {
  349. AddToList(PuttyParams, FORMAT(L"-loghost \"%s\"", (SessionData->LogicalHostName)), L" ");
  350. }
  351. }
  352. if (SessionData->AddressFamily == afIPv4)
  353. {
  354. AddToList(PuttyParams, L"-4", L" ");
  355. }
  356. else if (SessionData->AddressFamily == afIPv6)
  357. {
  358. AddToList(PuttyParams, L"-6", L" ");
  359. }
  360. }
  361. else
  362. {
  363. UnicodeString SessionName;
  364. UnicodeString PuttySession = GUIConfiguration->PuttySession;
  365. int Uniq = 1;
  366. while (DoesSessionExistInPutty(PuttySession))
  367. {
  368. Uniq++;
  369. PuttySession = FORMAT(L"%s (%d)", (GUIConfiguration->PuttySession, Uniq));
  370. }
  371. if (ExportSessionToPutty(SessionData, true, PuttySession))
  372. {
  373. SessionName = SessionData->SessionName;
  374. }
  375. else
  376. {
  377. SessionName = PuttySession;
  378. TPuttyCleanupThread::Schedule();
  379. if ((SessionData->FSProtocol == fsFTP) &&
  380. GUIConfiguration->TelnetForFtpInPutty)
  381. {
  382. // PuTTY does not allow -pw for telnet
  383. Password = L"";
  384. }
  385. }
  386. UnicodeString LoadSwitch = L"-load";
  387. int P = Params.LowerCase().Pos(LoadSwitch + L" ");
  388. if ((P == 0) || ((P > 1) && (Params[P - 1] != L' ')))
  389. {
  390. AddToList(PuttyParams, FORMAT(L"%s %s", (LoadSwitch, EscapePuttyCommandParam(SessionName))), L" ");
  391. }
  392. }
  393. }
  394. if (!Password.IsEmpty() && !LocalCustomCommand.IsPasswordCommand(AParams))
  395. {
  396. Password = NormalizeString(Password); // if password is empty, we should quote it always
  397. AddToList(PuttyParams, FORMAT(L"-pw %s", (EscapePuttyCommandParam(Password))), L" ");
  398. }
  399. AddToList(PuttyParams, Params, L" ");
  400. // PuTTY is started in its binary directory to allow relative paths in private key,
  401. // when opening PuTTY's own stored session.
  402. ExecuteShellChecked(Program, PuttyParams, true);
  403. }
  404. else
  405. {
  406. throw Exception(FMTLOAD(FILE_NOT_FOUND, (Program)));
  407. }
  408. }
  409. //---------------------------------------------------------------------------
  410. bool __fastcall FindTool(const UnicodeString & Name, UnicodeString & Path)
  411. {
  412. UnicodeString AppPath = IncludeTrailingBackslash(ExtractFilePath(Application->ExeName));
  413. Path = AppPath + Name;
  414. bool Result = true;
  415. if (!FileExists(ApiPath(Path)))
  416. {
  417. Path = AppPath + L"PuTTY\\" + Name;
  418. if (!FileExists(ApiPath(Path)))
  419. {
  420. Path = Name;
  421. if (!FindFile(Path))
  422. {
  423. Result = false;
  424. }
  425. }
  426. }
  427. return Result;
  428. }
  429. //---------------------------------------------------------------------------
  430. void __fastcall ExecuteTool(const UnicodeString & Name)
  431. {
  432. UnicodeString Path;
  433. if (!FindTool(Name, Path))
  434. {
  435. throw Exception(FMTLOAD(EXECUTE_APP_ERROR, (Name)));
  436. }
  437. ExecuteShellChecked(Path, L"");
  438. }
  439. //---------------------------------------------------------------------------
  440. TObjectList * StartCreationDirectoryMonitorsOnEachDrive(unsigned int Filter, TFileChangedEvent OnChanged)
  441. {
  442. std::unique_ptr<TStrings> Drives(new TStringList());
  443. std::unique_ptr<TStrings> DDDrives(new TStringList());
  444. DDDrives->CommaText = WinConfiguration->DDDrives;
  445. UnicodeString ExcludedDrives;
  446. for (int Index = 0; Index < DDDrives->Count; Index++)
  447. {
  448. UnicodeString S = Trim(DDDrives->Strings[Index]);
  449. if (!S.IsEmpty() && (S[1] == L'-'))
  450. {
  451. S = Trim(S.SubString(2, S.Length() - 1));
  452. if (!S.IsEmpty())
  453. {
  454. ExcludedDrives += S[1];
  455. }
  456. }
  457. else
  458. {
  459. Drives->Add(S);
  460. }
  461. }
  462. for (char Drive = FirstDrive; Drive <= LastDrive; Drive++)
  463. {
  464. if (ExcludedDrives.Pos(Drive) == 0)
  465. {
  466. // Not calling ReadDriveStatus(... dsSize), relying on drive ready status cached by the background thread
  467. TDriveInfoRec * DriveInfoRec = DriveInfo->Get(Drive);
  468. if (DriveInfoRec->Valid && DriveInfoRec->DriveReady &&
  469. (DriveInfoRec->DriveType != DRIVE_CDROM) &&
  470. ((DriveInfoRec->DriveType != DRIVE_REMOVABLE) || (Drive >= DriveInfo->FirstFixedDrive)))
  471. {
  472. Drives->Add(Drive);
  473. }
  474. }
  475. }
  476. std::unique_ptr<TObjectList> Result(new TObjectList());
  477. for (int Index = 0; Index < Drives->Count; Index++)
  478. {
  479. UnicodeString Drive = Drives->Strings[Index];
  480. std::unique_ptr<TDirectoryMonitor> Monitor(new TDirectoryMonitor(Application));
  481. try
  482. {
  483. Monitor->Path = DriveInfo->GetDriveRoot(Drive);
  484. Monitor->WatchSubtree = true;
  485. Monitor->WatchFilters = Filter;
  486. Monitor->OnCreated = OnChanged;
  487. Monitor->OnModified = OnChanged;
  488. Monitor->Active = true;
  489. Result->Add(Monitor.release());
  490. }
  491. catch (Exception & E)
  492. {
  493. // Ignore errors watching not-ready drives
  494. }
  495. }
  496. return Result.release();
  497. }
  498. //---------------------------------------------------------------------------
  499. bool DontCopyCommandToClipboard = false;
  500. //---------------------------------------------------------------------------
  501. bool __fastcall CopyCommandToClipboard(const UnicodeString & Command)
  502. {
  503. bool Result = !DontCopyCommandToClipboard && UseAlternativeFunction() && IsKeyPressed(VK_CONTROL);
  504. if (Result)
  505. {
  506. TInstantOperationVisualizer Visualizer;
  507. CopyToClipboard(Command);
  508. }
  509. return Result;
  510. }
  511. //---------------------------------------------------------------------------
  512. static bool __fastcall DoExecuteShell(const UnicodeString Path, const UnicodeString Params,
  513. bool ChangeWorkingDirectory, HANDLE * Handle)
  514. {
  515. bool Result = CopyCommandToClipboard(FormatCommand(Path, Params));
  516. if (Result)
  517. {
  518. if (Handle != NULL)
  519. {
  520. *Handle = NULL;
  521. }
  522. }
  523. else
  524. {
  525. UnicodeString Directory = ExtractFilePath(Path);
  526. TShellExecuteInfoW ExecuteInfo;
  527. memset(&ExecuteInfo, 0, sizeof(ExecuteInfo));
  528. ExecuteInfo.cbSize = sizeof(ExecuteInfo);
  529. ExecuteInfo.fMask =
  530. SEE_MASK_FLAG_NO_UI |
  531. FLAGMASK((Handle != NULL), SEE_MASK_NOCLOSEPROCESS);
  532. ExecuteInfo.hwnd = Application->Handle;
  533. ExecuteInfo.lpFile = (wchar_t*)Path.data();
  534. ExecuteInfo.lpParameters = (wchar_t*)Params.data();
  535. ExecuteInfo.lpDirectory = (ChangeWorkingDirectory ? Directory.c_str() : NULL);
  536. ExecuteInfo.nShow = SW_SHOW;
  537. Result = (ShellExecuteEx(&ExecuteInfo) != 0);
  538. if (Result)
  539. {
  540. if (Handle != NULL)
  541. {
  542. *Handle = ExecuteInfo.hProcess;
  543. }
  544. }
  545. }
  546. return Result;
  547. }
  548. //---------------------------------------------------------------------------
  549. void __fastcall ExecuteShellChecked(const UnicodeString Path, const UnicodeString Params, bool ChangeWorkingDirectory)
  550. {
  551. if (!DoExecuteShell(Path, Params, ChangeWorkingDirectory, NULL))
  552. {
  553. throw EOSExtException(FMTLOAD(EXECUTE_APP_ERROR, (Path)));
  554. }
  555. }
  556. //---------------------------------------------------------------------------
  557. void __fastcall ExecuteShellChecked(const UnicodeString Command)
  558. {
  559. UnicodeString Program, Params, Dir;
  560. SplitCommand(Command, Program, Params, Dir);
  561. ExecuteShellChecked(Program, Params);
  562. }
  563. //---------------------------------------------------------------------------
  564. bool __fastcall ExecuteShell(const UnicodeString Path, const UnicodeString Params,
  565. HANDLE & Handle)
  566. {
  567. return DoExecuteShell(Path, Params, false, &Handle);
  568. }
  569. //---------------------------------------------------------------------------
  570. void __fastcall ExecuteShellCheckedAndWait(const UnicodeString Command,
  571. TProcessMessagesEvent ProcessMessages)
  572. {
  573. UnicodeString Program, Params, Dir;
  574. SplitCommand(Command, Program, Params, Dir);
  575. HANDLE ProcessHandle;
  576. bool Result = DoExecuteShell(Program, Params, false, &ProcessHandle);
  577. if (!Result)
  578. {
  579. throw EOSExtException(FMTLOAD(EXECUTE_APP_ERROR, (Program)));
  580. }
  581. else
  582. {
  583. if (ProcessHandle != NULL) // only if command was copied to clipboard only
  584. {
  585. if (ProcessMessages != NULL)
  586. {
  587. unsigned long WaitResult;
  588. do
  589. {
  590. // Same as in ExecuteProcessAndReadOutput
  591. WaitResult = WaitForSingleObject(ProcessHandle, 50);
  592. if (WaitResult == WAIT_FAILED)
  593. {
  594. throw Exception(LoadStr(DOCUMENT_WAIT_ERROR));
  595. }
  596. ProcessMessages();
  597. }
  598. while (WaitResult == WAIT_TIMEOUT);
  599. }
  600. else
  601. {
  602. WaitForSingleObject(ProcessHandle, INFINITE);
  603. }
  604. }
  605. }
  606. }
  607. //---------------------------------------------------------------------------
  608. bool __fastcall SpecialFolderLocation(int PathID, UnicodeString & Path)
  609. {
  610. LPITEMIDLIST Pidl;
  611. wchar_t Buf[256];
  612. if (SHGetSpecialFolderLocation(NULL, PathID, &Pidl) == NO_ERROR &&
  613. SHGetPathFromIDList(Pidl, Buf))
  614. {
  615. Path = UnicodeString(Buf);
  616. return true;
  617. }
  618. return false;
  619. }
  620. //---------------------------------------------------------------------------
  621. UnicodeString __fastcall UniqTempDir(const UnicodeString BaseDir, const UnicodeString Identity,
  622. bool Mask)
  623. {
  624. DebugAssert(!BaseDir.IsEmpty());
  625. UnicodeString TempDir;
  626. do
  627. {
  628. TempDir = IncludeTrailingBackslash(BaseDir) + Identity;
  629. if (Mask)
  630. {
  631. TempDir += L"?????";
  632. }
  633. else
  634. {
  635. TempDir += IncludeTrailingBackslash(FormatDateTime(L"nnzzz", Now()));
  636. }
  637. }
  638. while (!Mask && DirectoryExists(ApiPath(TempDir)));
  639. return TempDir;
  640. }
  641. //---------------------------------------------------------------------------
  642. bool __fastcall DeleteDirectory(const UnicodeString DirName)
  643. {
  644. TSearchRecOwned sr;
  645. bool retval = true;
  646. if (FindFirstUnchecked(DirName + L"\\*", faAnyFile, sr) == 0) // VCL Function
  647. {
  648. if (sr.IsDirectory())
  649. {
  650. if (sr.IsRealFile())
  651. retval = DeleteDirectory(DirName + L"\\" + sr.Name);
  652. }
  653. else
  654. {
  655. retval = DeleteFile(ApiPath(DirName + L"\\" + sr.Name));
  656. }
  657. if (retval)
  658. {
  659. while (FindNextChecked(sr) == 0)
  660. { // VCL Function
  661. if (sr.IsDirectory())
  662. {
  663. if (sr.IsRealFile())
  664. retval = DeleteDirectory(DirName + L"\\" + sr.Name);
  665. }
  666. else
  667. {
  668. retval = DeleteFile(ApiPath(DirName + L"\\" + sr.Name));
  669. }
  670. if (!retval) break;
  671. }
  672. }
  673. }
  674. sr.Close();
  675. if (retval) retval = RemoveDir(ApiPath(DirName)); // VCL function
  676. return retval;
  677. }
  678. //---------------------------------------------------------------------------
  679. class TSessionColors : public TComponent
  680. {
  681. public:
  682. __fastcall TSessionColors(TComponent * Owner) : TComponent(Owner)
  683. {
  684. Name = QualifiedClassName();
  685. }
  686. static TSessionColors * __fastcall Retrieve(TComponent * Component)
  687. {
  688. TSessionColors * SessionColors = dynamic_cast<TSessionColors *>(Component->FindComponent(QualifiedClassName()));
  689. if (SessionColors == NULL)
  690. {
  691. SessionColors = new TSessionColors(Component);
  692. }
  693. return SessionColors;
  694. }
  695. typedef std::map<TColor, int> TColorMap;
  696. TColorMap ColorMap;
  697. };
  698. //---------------------------------------------------------------------------
  699. int __fastcall GetSessionColorImage(
  700. TCustomImageList * ImageList, TColor Color, int MaskIndex)
  701. {
  702. TSessionColors * SessionColors = TSessionColors::Retrieve(ImageList);
  703. int Result;
  704. TSessionColors::TColorMap::const_iterator I = SessionColors->ColorMap.find(Color);
  705. if (I != SessionColors->ColorMap.end())
  706. {
  707. Result = I->second;
  708. }
  709. else
  710. {
  711. // This overly complex drawing is here to support color button on SiteAdvanced
  712. // dialog. There we use plain TImageList, instead of TPngImageList,
  713. // TButton does not work with transparent images
  714. // (not even TBitmap with Transparent = true)
  715. std::unique_ptr<TBitmap> MaskBitmap(new TBitmap());
  716. ImageList->GetBitmap(MaskIndex, MaskBitmap.get());
  717. std::unique_ptr<TPngImage> MaskImage(new TPngImage());
  718. MaskImage->Assign(MaskBitmap.get());
  719. std::unique_ptr<TPngImage> ColorImage(new TPngImage(COLOR_RGB, 16, ImageList->Width, ImageList->Height));
  720. TColor MaskTransparentColor = MaskImage->Pixels[0][0];
  721. TColor TransparentColor = MaskTransparentColor;
  722. // Expecting that the color to be replaced is in the centre of the image (HACK)
  723. TColor MaskColor = MaskImage->Pixels[ImageList->Width / 2][ImageList->Height / 2];
  724. for (int Y = 0; Y < ImageList->Height; Y++)
  725. {
  726. for (int X = 0; X < ImageList->Width; X++)
  727. {
  728. TColor SourceColor = MaskImage->Pixels[X][Y];
  729. TColor DestColor;
  730. // this branch is pointless as long as MaskTransparentColor and
  731. // TransparentColor are the same
  732. if (SourceColor == MaskTransparentColor)
  733. {
  734. DestColor = TransparentColor;
  735. }
  736. else if (SourceColor == MaskColor)
  737. {
  738. DestColor = Color;
  739. }
  740. else
  741. {
  742. DestColor = SourceColor;
  743. }
  744. ColorImage->Pixels[X][Y] = DestColor;
  745. }
  746. }
  747. std::unique_ptr<TBitmap> Bitmap(new TBitmap());
  748. Bitmap->SetSize(ImageList->Width, ImageList->Height);
  749. ColorImage->AssignTo(Bitmap.get());
  750. Result = ImageList->AddMasked(Bitmap.get(), TransparentColor);
  751. SessionColors->ColorMap.insert(std::make_pair(Color, Result));
  752. }
  753. return Result;
  754. }
  755. //---------------------------------------------------------------------------
  756. void __fastcall RegenerateSessionColorsImageList(TCustomImageList * ImageList, int MaskIndex)
  757. {
  758. TSessionColors * SessionColors = TSessionColors::Retrieve(ImageList);
  759. std::vector<TColor> Colors;
  760. size_t FixedImages = static_cast<size_t>(ImageList->Count);
  761. Colors.resize(FixedImages + SessionColors->ColorMap.size());
  762. TSessionColors::TColorMap::const_iterator I = SessionColors->ColorMap.begin();
  763. while (I != SessionColors->ColorMap.end())
  764. {
  765. DebugAssert(Colors[I->second] == TColor());
  766. Colors[I->second] = I->first;
  767. I++;
  768. }
  769. TSessionColors::TColorMap ColorMap = SessionColors->ColorMap;
  770. SessionColors->ColorMap.clear();
  771. for (size_t Index = 0; Index < Colors.size(); Index++)
  772. {
  773. bool IsFixedImageIndex = (Index < FixedImages);
  774. DebugAssert((Colors[Index] == TColor()) == IsFixedImageIndex);
  775. if (!IsFixedImageIndex)
  776. {
  777. GetSessionColorImage(ImageList, Colors[Index], MaskIndex);
  778. }
  779. }
  780. DebugAssert(SessionColors->ColorMap == ColorMap);
  781. }
  782. //---------------------------------------------------------------------------
  783. void __fastcall SetSubmenu(TTBXCustomItem * Item, bool Enable)
  784. {
  785. class TTBXPublicItem : public TTBXCustomItem
  786. {
  787. public:
  788. __property ItemStyle;
  789. };
  790. TTBXPublicItem * PublicItem = reinterpret_cast<TTBXPublicItem *>(Item);
  791. DebugAssert(PublicItem != NULL);
  792. // See TTBItemViewer.IsPtInButtonPart (called from TTBItemViewer.MouseDown)
  793. if (Enable)
  794. {
  795. PublicItem->ItemStyle = PublicItem->ItemStyle << tbisSubmenu;
  796. }
  797. else
  798. {
  799. PublicItem->ItemStyle = PublicItem->ItemStyle >> tbisSubmenu;
  800. }
  801. }
  802. //---------------------------------------------------------------------------
  803. bool __fastcall IsEligibleForApplyingTabs(
  804. UnicodeString Line, int & TabPos, UnicodeString & Start, UnicodeString & Remaining)
  805. {
  806. bool Result = false;
  807. TabPos = Line.Pos(L"\t");
  808. if (TabPos > 0)
  809. {
  810. Remaining = Line.SubString(TabPos + 1, Line.Length() - TabPos);
  811. // WORKAROUND
  812. // Some translations still use obsolete hack of consecutive tabs to aling the contents.
  813. // Delete these, so that the following check does not fail on this
  814. while (Remaining.SubString(1, 1) == L"\t")
  815. {
  816. Remaining.Delete(1, 1);
  817. }
  818. // We do not have, not support, mutiple tabs on a single line
  819. if (DebugAlwaysTrue(Remaining.Pos(L"\t") == 0))
  820. {
  821. Start = Line.SubString(1, TabPos - 1);
  822. // WORKAROUND
  823. // Previously we padded the string before tab with spaces,
  824. // to aling the contents across multiple lines
  825. Start = Start.TrimRight();
  826. // at least two normal spaces for separation
  827. Start += L" ";
  828. Result = true;
  829. }
  830. }
  831. return Result;
  832. }
  833. //---------------------------------------------------------------------------
  834. static int __fastcall CalculateWidthByLength(UnicodeString Text, void * /*Arg*/)
  835. {
  836. return Text.Length();
  837. }
  838. //---------------------------------------------------------------------------
  839. void __fastcall ApplyTabs(
  840. UnicodeString & Text, wchar_t Padding,
  841. TCalculateWidth CalculateWidth, void * CalculateWidthArg)
  842. {
  843. if (CalculateWidth == NULL)
  844. {
  845. DebugAssert(CalculateWidthArg == NULL);
  846. CalculateWidth = CalculateWidthByLength;
  847. }
  848. std::unique_ptr<TStringList> Lines(TextToStringList(Text));
  849. int MaxWidth = -1;
  850. for (int Index = 0; Index < Lines->Count; Index++)
  851. {
  852. UnicodeString Line = Lines->Strings[Index];
  853. int TabPos;
  854. UnicodeString Start;
  855. UnicodeString Remaining;
  856. if (IsEligibleForApplyingTabs(Line, TabPos, Start, Remaining))
  857. {
  858. int Width = CalculateWidth(Start, CalculateWidthArg);
  859. MaxWidth = Max(MaxWidth, Width);
  860. }
  861. }
  862. // Optimization and also to prevent potential regression for texts without tabs
  863. if (MaxWidth >= 0)
  864. {
  865. for (int Index = 0; Index < Lines->Count; Index++)
  866. {
  867. UnicodeString Line = Lines->Strings[Index];
  868. int TabPos;
  869. UnicodeString Start;
  870. UnicodeString Remaining;
  871. if (IsEligibleForApplyingTabs(Line, TabPos, Start, Remaining))
  872. {
  873. int Width;
  874. int Iterations = 0;
  875. while ((Width = CalculateWidth(Start, CalculateWidthArg)) < MaxWidth)
  876. {
  877. int Wider = CalculateWidth(Start + Padding, CalculateWidthArg);
  878. // If padded string is wider than max width by more pixels
  879. // than non-padded string is shorter than max width
  880. if ((Wider > MaxWidth) && ((Wider - MaxWidth) > (MaxWidth - Width)))
  881. {
  882. break;
  883. }
  884. Start += Padding;
  885. Iterations++;
  886. // In rare case a tab is zero-width with some strange font (like HYLE)
  887. if (Iterations > 100)
  888. {
  889. break;
  890. }
  891. }
  892. Lines->Strings[Index] = Start + Remaining;
  893. }
  894. }
  895. Text = Lines->Text;
  896. // remove trailing newline
  897. Text = Text.TrimRight();
  898. }
  899. }
  900. //---------------------------------------------------------------------------
  901. static void __fastcall DoSelectScaledImageList(TImageList * ImageList)
  902. {
  903. TImageList * MatchingList = NULL;
  904. int MachingPixelsPerInch = 0;
  905. int PixelsPerInch = GetComponentPixelsPerInch(ImageList);
  906. for (int Index = 0; Index < ImageList->Owner->ComponentCount; Index++)
  907. {
  908. TImageList * OtherList = dynamic_cast<TImageList *>(ImageList->Owner->Components[Index]);
  909. if ((OtherList != NULL) &&
  910. (OtherList != ImageList) &&
  911. StartsStr(ImageList->Name, OtherList->Name))
  912. {
  913. UnicodeString OtherListPixelsPerInchStr =
  914. OtherList->Name.SubString(
  915. ImageList->Name.Length() + 1, OtherList->Name.Length() - ImageList->Name.Length());
  916. int OtherListPixelsPerInch = StrToInt(OtherListPixelsPerInchStr);
  917. if ((OtherListPixelsPerInch <= PixelsPerInch) &&
  918. ((MatchingList == NULL) ||
  919. (MachingPixelsPerInch < OtherListPixelsPerInch)))
  920. {
  921. MatchingList = OtherList;
  922. MachingPixelsPerInch = OtherListPixelsPerInch;
  923. }
  924. }
  925. }
  926. if (MatchingList != NULL)
  927. {
  928. UnicodeString ImageListBackupName = ImageList->Name + IntToStr(USER_DEFAULT_SCREEN_DPI);
  929. if (ImageList->Owner->FindComponent(ImageListBackupName) == NULL)
  930. {
  931. TImageList * ImageListBackup;
  932. TPngImageList * PngImageList = dynamic_cast<TPngImageList *>(ImageList);
  933. if (PngImageList != NULL)
  934. {
  935. ImageListBackup = new TPngImageList(ImageList->Owner);
  936. }
  937. else
  938. {
  939. ImageListBackup = new TImageList(ImageList->Owner);
  940. }
  941. ImageListBackup->Name = ImageListBackupName;
  942. ImageList->Owner->InsertComponent(ImageListBackup);
  943. CopyImageList(ImageListBackup, ImageList);
  944. }
  945. CopyImageList(ImageList, MatchingList);
  946. }
  947. }
  948. //---------------------------------------------------------------------------
  949. static void __fastcall ImageListRescale(TComponent * Sender, TObject * /*Token*/)
  950. {
  951. TImageList * ImageList = DebugNotNull(dynamic_cast<TImageList *>(Sender));
  952. DoSelectScaledImageList(ImageList);
  953. }
  954. //---------------------------------------------------------------------------
  955. void __fastcall SelectScaledImageList(TImageList * ImageList)
  956. {
  957. DoSelectScaledImageList(ImageList);
  958. SetRescaleFunction(ImageList, ImageListRescale);
  959. }
  960. //---------------------------------------------------------------------------
  961. void __fastcall CopyImageList(TImageList * TargetList, TImageList * SourceList)
  962. {
  963. // Maybe this is not necessary, once the TPngImageList::Assign was fixed
  964. TPngImageList * PngTargetList = dynamic_cast<TPngImageList *>(TargetList);
  965. TPngImageList * PngSourceList = dynamic_cast<TPngImageList *>(SourceList);
  966. TargetList->Clear();
  967. TargetList->Height = SourceList->Height;
  968. TargetList->Width = SourceList->Width;
  969. if ((PngTargetList != NULL) && (PngSourceList != NULL))
  970. {
  971. // AddImages won't copy the names and we need them for
  972. // LoadDialogImage and TFrameAnimation
  973. PngTargetList->PngImages->Assign(PngSourceList->PngImages);
  974. }
  975. else
  976. {
  977. TargetList->AddImages(SourceList);
  978. }
  979. }
  980. //---------------------------------------------------------------------------
  981. static bool __fastcall DoLoadDialogImage(TImage * Image, const UnicodeString & ImageName)
  982. {
  983. bool Result = false;
  984. if (GlyphsModule != NULL)
  985. {
  986. TPngImageList * DialogImages = GetDialogImages(Image);
  987. int Index;
  988. for (Index = 0; Index < DialogImages->PngImages->Count; Index++)
  989. {
  990. TPngImageCollectionItem * PngItem = DialogImages->PngImages->Items[Index];
  991. if (SameText(PngItem->Name, ImageName))
  992. {
  993. Image->Picture->Assign(PngItem->PngImage);
  994. break;
  995. }
  996. }
  997. DebugAssert(Index < DialogImages->PngImages->Count);
  998. Result = true;
  999. }
  1000. // When showing an exception from wWinMain, the images are released already.
  1001. // Non-existence of the glyphs module is just a good indication of that.
  1002. // We expect errors only.
  1003. else if (ImageName == L"Error")
  1004. {
  1005. Image->Picture->Icon->Handle = LoadIcon(0, IDI_HAND);
  1006. }
  1007. // For showing an information about trace files
  1008. else if (DebugAlwaysTrue(ImageName == L"Information"))
  1009. {
  1010. Image->Picture->Icon->Handle = LoadIcon(0, IDI_APPLICATION);
  1011. }
  1012. return Result;
  1013. }
  1014. //---------------------------------------------------------------------------
  1015. class TDialogImageName : public TObject
  1016. {
  1017. public:
  1018. UnicodeString ImageName;
  1019. };
  1020. //---------------------------------------------------------------------------
  1021. static void __fastcall DialogImageRescale(TComponent * Sender, TObject * Token)
  1022. {
  1023. TImage * Image = DebugNotNull(dynamic_cast<TImage *>(Sender));
  1024. TDialogImageName * DialogImageName = DebugNotNull(dynamic_cast<TDialogImageName *>(Token));
  1025. DoLoadDialogImage(Image, DialogImageName->ImageName);
  1026. }
  1027. //---------------------------------------------------------------------------
  1028. void __fastcall LoadDialogImage(TImage * Image, const UnicodeString & ImageName)
  1029. {
  1030. if (DoLoadDialogImage(Image, ImageName))
  1031. {
  1032. TDialogImageName * DialogImageName = new TDialogImageName();
  1033. DialogImageName->ImageName = ImageName;
  1034. SetRescaleFunction(Image, DialogImageRescale, DialogImageName, true);
  1035. }
  1036. }
  1037. //---------------------------------------------------------------------------
  1038. int __fastcall DialogImageSize(TForm * Form)
  1039. {
  1040. return ScaleByPixelsPerInch(32, Form);
  1041. }
  1042. //---------------------------------------------------------------------------
  1043. void __fastcall HideComponentsPanel(TForm * Form)
  1044. {
  1045. TComponent * Component = DebugNotNull(Form->FindComponent(L"ComponentsPanel"));
  1046. TPanel * Panel = DebugNotNull(dynamic_cast<TPanel *>(Component));
  1047. DebugAssert(Panel->Align == alBottom);
  1048. int Offset = Panel->Height;
  1049. Panel->Visible = false;
  1050. Panel->Height = 0;
  1051. Form->Height -= Offset;
  1052. for (int Index = 0; Index < Form->ControlCount; Index++)
  1053. {
  1054. TControl * Control = Form->Controls[Index];
  1055. // Shift back bottom-anchored controls
  1056. // (needed for toolbar panel on Progress window and buttons on Preferences dialog),
  1057. if ((Control->Align == alNone) &&
  1058. Control->Anchors.Contains(akBottom) &&
  1059. !Control->Anchors.Contains(akTop))
  1060. {
  1061. Control->Top += Offset;
  1062. }
  1063. // Resize back all-anchored controls
  1064. // (needed for main panel on Preferences dialog),
  1065. if (Control->Anchors.Contains(akBottom) &&
  1066. Control->Anchors.Contains(akTop))
  1067. {
  1068. Control->Height += Offset;
  1069. }
  1070. }
  1071. }
  1072. //---------------------------------------------------------------------------
  1073. UnicodeString FormatIncrementalSearchStatus(const UnicodeString & Text, bool HaveNext)
  1074. {
  1075. UnicodeString Result =
  1076. L" " + FMTLOAD(INC_SEARCH, (Text)) +
  1077. (HaveNext ? L" " + LoadStr(INC_NEXT_SEARCH) : UnicodeString());
  1078. return Result;
  1079. }
  1080. //---------------------------------------------------------------------------
  1081. class TCustomDocHandler : public TComponent, public ::IDocHostUIHandler
  1082. {
  1083. public:
  1084. __fastcall TCustomDocHandler(TComponent * Owner) : TComponent(Owner)
  1085. {
  1086. }
  1087. protected:
  1088. #pragma warn -hid
  1089. virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID ClassId, void ** Intf)
  1090. {
  1091. HRESULT Result = S_OK;
  1092. if (ClassId == IID_IUnknown)
  1093. {
  1094. *Intf = (IUnknown *)this;
  1095. }
  1096. else if (ClassId == ::IID_IDocHostUIHandler)
  1097. {
  1098. *Intf = (::IDocHostUIHandler *)this;
  1099. }
  1100. else
  1101. {
  1102. Result = E_NOINTERFACE;
  1103. }
  1104. return Result;
  1105. }
  1106. #pragma warn .hid
  1107. virtual ULONG STDMETHODCALLTYPE AddRef()
  1108. {
  1109. return -1;
  1110. }
  1111. virtual ULONG STDMETHODCALLTYPE Release()
  1112. {
  1113. return -1;
  1114. }
  1115. virtual HRESULT STDMETHODCALLTYPE ShowContextMenu(
  1116. DWORD dwID, POINT * ppt, IUnknown * pcmdtReserved, IDispatch * pdispReserved)
  1117. {
  1118. // No context menu
  1119. // (implementing IDocHostUIHandler reenabled context menu disabled by TBrowserViewer::DoContextPopup)
  1120. return S_OK;
  1121. }
  1122. virtual HRESULT STDMETHODCALLTYPE GetHostInfo(::_DOCHOSTUIINFO * Info)
  1123. {
  1124. // Setting ControlBorder is ignored with IDocHostUIHandler.
  1125. // DOCHOSTUIFLAG_DPI_AWARE does not seem to have any effect
  1126. Info->dwFlags |= DOCHOSTUIFLAG_SCROLL_NO | DOCHOSTUIFLAG_NO3DBORDER | DOCHOSTUIFLAG_DPI_AWARE;
  1127. return S_OK;
  1128. }
  1129. virtual HRESULT STDMETHODCALLTYPE ShowUI(
  1130. DWORD dwID, IOleInPlaceActiveObject * pActiveObject, IOleCommandTarget * pCommandTarget, IOleInPlaceFrame * pFrame,
  1131. IOleInPlaceUIWindow * pDoc)
  1132. {
  1133. return E_NOTIMPL;
  1134. }
  1135. virtual HRESULT STDMETHODCALLTYPE HideUI()
  1136. {
  1137. return E_NOTIMPL;
  1138. }
  1139. virtual HRESULT STDMETHODCALLTYPE UpdateUI()
  1140. {
  1141. return E_NOTIMPL;
  1142. }
  1143. virtual HRESULT STDMETHODCALLTYPE EnableModeless(BOOL fEnable)
  1144. {
  1145. return E_NOTIMPL;
  1146. }
  1147. virtual HRESULT STDMETHODCALLTYPE OnDocWindowActivate(BOOL fActivate)
  1148. {
  1149. return E_NOTIMPL;
  1150. }
  1151. virtual HRESULT STDMETHODCALLTYPE OnFrameWindowActivate(BOOL fActivate)
  1152. {
  1153. return E_NOTIMPL;
  1154. }
  1155. virtual HRESULT STDMETHODCALLTYPE ResizeBorder(LPCRECT prcBorder, IOleInPlaceUIWindow * pUIWindow, BOOL fRameWindow)
  1156. {
  1157. return E_NOTIMPL;
  1158. }
  1159. virtual HRESULT STDMETHODCALLTYPE TranslateAccelerator(LPMSG lpMsg, const GUID * pguidCmdGroup, DWORD nCmdID)
  1160. {
  1161. return E_NOTIMPL;
  1162. }
  1163. virtual HRESULT STDMETHODCALLTYPE GetOptionKeyPath(LPOLESTR * pchKey, DWORD dw)
  1164. {
  1165. return E_NOTIMPL;
  1166. }
  1167. virtual HRESULT STDMETHODCALLTYPE GetDropTarget(IDropTarget * pDropTarget, IDropTarget ** ppDropTarget)
  1168. {
  1169. return E_NOTIMPL;
  1170. }
  1171. virtual HRESULT STDMETHODCALLTYPE GetExternal(IDispatch ** ppDispatch)
  1172. {
  1173. return E_NOTIMPL;
  1174. }
  1175. virtual HRESULT STDMETHODCALLTYPE TranslateUrl(DWORD dwTranslate, OLECHAR * pchURLIn, OLECHAR ** ppchURLOut)
  1176. {
  1177. return E_NOTIMPL;
  1178. }
  1179. virtual HRESULT STDMETHODCALLTYPE FilterDataObject(IDataObject * pDO, IDataObject ** ppDORet)
  1180. {
  1181. return E_NOTIMPL;
  1182. }
  1183. };
  1184. //---------------------------------------------------------------------------
  1185. class TBrowserViewer : public TWebBrowserEx
  1186. {
  1187. public:
  1188. __fastcall virtual TBrowserViewer(TComponent* AOwner);
  1189. void __fastcall AddLinkHandler(
  1190. const UnicodeString & Url, TNotifyEvent Handler);
  1191. void __fastcall NavigateToUrl(const UnicodeString & Url);
  1192. TControl * LoadingPanel;
  1193. protected:
  1194. DYNAMIC void __fastcall DoContextPopup(const TPoint & MousePos, bool & Handled);
  1195. void __fastcall DocumentComplete(
  1196. TObject * Sender, const _di_IDispatch Disp, const OleVariant & URL);
  1197. void __fastcall BeforeNavigate2(
  1198. TObject * Sender, const _di_IDispatch Disp, const OleVariant & URL,
  1199. const OleVariant & Flags, const OleVariant & TargetFrameName,
  1200. const OleVariant & PostData, const OleVariant & Headers, WordBool & Cancel);
  1201. bool FComplete;
  1202. std::map<UnicodeString, TNotifyEvent> FHandlers;
  1203. };
  1204. //---------------------------------------------------------------------------
  1205. __fastcall TBrowserViewer::TBrowserViewer(TComponent* AOwner) :
  1206. TWebBrowserEx(AOwner)
  1207. {
  1208. FComplete = false;
  1209. OnDocumentComplete = DocumentComplete;
  1210. OnBeforeNavigate2 = BeforeNavigate2;
  1211. LoadingPanel = NULL;
  1212. }
  1213. //---------------------------------------------------------------------------
  1214. void __fastcall TBrowserViewer::AddLinkHandler(
  1215. const UnicodeString & Url, TNotifyEvent Handler)
  1216. {
  1217. FHandlers.insert(std::make_pair(Url, Handler));
  1218. }
  1219. //---------------------------------------------------------------------------
  1220. void __fastcall TBrowserViewer::DoContextPopup(const TPoint & MousePos, bool & Handled)
  1221. {
  1222. // Suppress built-in context menu.
  1223. // Is ignored with IDocHostUIHandler. Needs to be overriden by ShowContextMenu.
  1224. Handled = true;
  1225. TWebBrowserEx::DoContextPopup(MousePos, Handled);
  1226. }
  1227. //---------------------------------------------------------------------------
  1228. void __fastcall TBrowserViewer::DocumentComplete(
  1229. TObject * /*Sender*/, const _di_IDispatch /*Disp*/, const OleVariant & /*URL*/)
  1230. {
  1231. SetBrowserDesignModeOff(this);
  1232. FComplete = true;
  1233. if (LoadingPanel != NULL)
  1234. {
  1235. LoadingPanel->Visible = false;
  1236. }
  1237. }
  1238. //---------------------------------------------------------------------------
  1239. void __fastcall TBrowserViewer::BeforeNavigate2(
  1240. TObject * /*Sender*/, const _di_IDispatch /*Disp*/, const OleVariant & AURL,
  1241. const OleVariant & /*Flags*/, const OleVariant & /*TargetFrameName*/,
  1242. const OleVariant & /*PostData*/, const OleVariant & /*Headers*/, WordBool & Cancel)
  1243. {
  1244. // If OnDocumentComplete was not called yet, is has to be our initial message URL,
  1245. // opened using TWebBrowserEx::Navigate(), allow it.
  1246. // Otherwise it's user navigating, block that and open link
  1247. // in an external browser, possibly adding campaign parameters on the way.
  1248. if (FComplete)
  1249. {
  1250. Cancel = 1;
  1251. UnicodeString URL = AURL;
  1252. if (FHandlers.count(URL) > 0)
  1253. {
  1254. FHandlers[URL](this);
  1255. }
  1256. else
  1257. {
  1258. OpenBrowser(URL);
  1259. }
  1260. }
  1261. }
  1262. //---------------------------------------------------------------------------
  1263. void __fastcall TBrowserViewer::NavigateToUrl(const UnicodeString & Url)
  1264. {
  1265. FComplete = false;
  1266. Navigate(Url.c_str());
  1267. }
  1268. //---------------------------------------------------------------------------
  1269. TPanel * __fastcall CreateLabelPanel(TPanel * Parent, const UnicodeString & Label)
  1270. {
  1271. TPanel * Result = CreateBlankPanel(Parent);
  1272. Result->Parent = Parent;
  1273. Result->Align = alClient;
  1274. Result->Caption = Label;
  1275. return Result;
  1276. }
  1277. //---------------------------------------------------------------------------
  1278. TWebBrowserEx * __fastcall CreateBrowserViewer(TPanel * Parent, const UnicodeString & LoadingLabel)
  1279. {
  1280. TBrowserViewer * Result = new TBrowserViewer(Parent);
  1281. // TWebBrowserEx has its own unrelated Name and Parent properties.
  1282. // The name is used in DownloadUpdate().
  1283. static_cast<TWinControl *>(Result)->Name = L"BrowserViewer";
  1284. static_cast<TWinControl *>(Result)->Parent = Parent;
  1285. Result->Align = alClient;
  1286. // Is ignored with IDocHostUIHandler. Needs to be overriden by DOCHOSTUIFLAG_NO3DBORDER in GetHostInfo.
  1287. Result->ControlBorder = cbNone;
  1288. Result->LoadingPanel = CreateLabelPanel(Parent, LoadingLabel);
  1289. return Result;
  1290. }
  1291. //---------------------------------------------------------------------------
  1292. void __fastcall SetBrowserDesignModeOff(TWebBrowserEx * WebBrowser)
  1293. {
  1294. if (DebugAlwaysTrue(WebBrowser->Document2 != NULL))
  1295. {
  1296. WebBrowser->Document2->designMode = L"Off";
  1297. }
  1298. }
  1299. //---------------------------------------------------------------------------
  1300. void __fastcall AddBrowserLinkHandler(TWebBrowserEx * WebBrowser,
  1301. const UnicodeString & Url, TNotifyEvent Handler)
  1302. {
  1303. TBrowserViewer * BrowserViewer = dynamic_cast<TBrowserViewer *>(WebBrowser);
  1304. if (DebugAlwaysTrue(BrowserViewer != NULL))
  1305. {
  1306. BrowserViewer->AddLinkHandler(Url, Handler);
  1307. }
  1308. }
  1309. //---------------------------------------------------------------------------
  1310. void __fastcall NavigateBrowserToUrl(TWebBrowserEx * WebBrowser, const UnicodeString & Url)
  1311. {
  1312. TBrowserViewer * BrowserViewer = dynamic_cast<TBrowserViewer *>(WebBrowser);
  1313. if (DebugAlwaysTrue(BrowserViewer != NULL))
  1314. {
  1315. BrowserViewer->NavigateToUrl(Url);
  1316. }
  1317. }
  1318. //---------------------------------------------------------------------------
  1319. void ReadyBrowserForStreaming(TWebBrowserEx * WebBrowser)
  1320. {
  1321. // This creates TWebBrowserEx::Document, which we need to stream in an in-memory document
  1322. NavigateBrowserToUrl(WebBrowser, L"about:blank");
  1323. // Needs to be followed by WaitBrowserToIdle
  1324. }
  1325. //---------------------------------------------------------------------------
  1326. void WaitBrowserToIdle(TWebBrowserEx * WebBrowser)
  1327. {
  1328. while (WebBrowser->ReadyState < ::READYSTATE_INTERACTIVE)
  1329. {
  1330. Application->ProcessMessages();
  1331. }
  1332. }
  1333. //---------------------------------------------------------------------------
  1334. void HideBrowserScrollbars(TWebBrowserEx * WebBrowser)
  1335. {
  1336. ICustomDoc * CustomDoc = NULL;
  1337. if (DebugAlwaysTrue(WebBrowser->Document != NULL) &&
  1338. SUCCEEDED(WebBrowser->Document->QueryInterface(&CustomDoc)) &&
  1339. DebugAlwaysTrue(CustomDoc != NULL))
  1340. {
  1341. TCustomDocHandler * Handler = new TCustomDocHandler(WebBrowser);
  1342. CustomDoc->SetUIHandler(Handler);
  1343. }
  1344. }
  1345. //---------------------------------------------------------------------------
  1346. UnicodeString GenerateAppHtmlPage(TFont * Font, TPanel * Parent, const UnicodeString & Body, bool Seamless)
  1347. {
  1348. UnicodeString Result =
  1349. L"<!DOCTYPE html>\n"
  1350. L"<meta charset=\"utf-8\">\n"
  1351. L"<html>\n"
  1352. L"<head>\n"
  1353. L"<style>\n"
  1354. L"\n"
  1355. L"body\n"
  1356. L"{\n"
  1357. L" font-family: '" + Font->Name + L"';\n"
  1358. L" margin: " + UnicodeString(Seamless ? L"0" : L"0.5em") + L";\n"
  1359. L" background-color: " + ColorToWebColorStr(Parent->Color) + L";\n" +
  1360. UnicodeString(Seamless ? L" overflow: hidden;\n" : L"") +
  1361. L"}\n"
  1362. L"\n"
  1363. L"body\n"
  1364. L"{\n"
  1365. L" font-size: " + IntToStr(Font->Size) + L"pt;\n"
  1366. L"}\n"
  1367. L"\n"
  1368. L"p\n"
  1369. L"{\n"
  1370. L" margin-top: 0;\n"
  1371. L" margin-bottom: 1em;\n"
  1372. L"}\n"
  1373. L"\n"
  1374. L"a, a:visited, a:hover, a:visited, a:current\n"
  1375. L"{\n"
  1376. L" color: " + ColorToWebColorStr(LinkColor) + L";\n"
  1377. L"}\n"
  1378. L"</style>\n"
  1379. L"</head>\n"
  1380. L"<body>\n" +
  1381. Body +
  1382. L"</body>\n"
  1383. L"</html>\n";
  1384. return Result;
  1385. }
  1386. //---------------------------------------------------------------------------
  1387. void LoadBrowserDocument(TWebBrowserEx * WebBrowser, const UnicodeString & Document)
  1388. {
  1389. std::unique_ptr<TMemoryStream> DocumentStream(new TMemoryStream());
  1390. UTF8String DocumentUTF8 = UTF8String(Document);
  1391. DocumentStream->Write(DocumentUTF8.c_str(), DocumentUTF8.Length());
  1392. DocumentStream->Seek(0, 0);
  1393. // For stream-loaded document, when set only after loading from OnDocumentComplete,
  1394. // browser stops working
  1395. SetBrowserDesignModeOff(WebBrowser);
  1396. TStreamAdapter * DocumentStreamAdapter = new TStreamAdapter(DocumentStream.get(), soReference);
  1397. IPersistStreamInit * PersistStreamInit = NULL;
  1398. if (DebugAlwaysTrue(WebBrowser->Document != NULL) &&
  1399. SUCCEEDED(WebBrowser->Document->QueryInterface(IID_IPersistStreamInit, (void **)&PersistStreamInit)) &&
  1400. DebugAlwaysTrue(PersistStreamInit != NULL))
  1401. {
  1402. PersistStreamInit->Load(static_cast<_di_IStream>(*DocumentStreamAdapter));
  1403. }
  1404. }
  1405. //---------------------------------------------------------------------------
  1406. TComponent * __fastcall FindComponentRecursively(TComponent * Root, const UnicodeString & Name)
  1407. {
  1408. for (int Index = 0; Index < Root->ComponentCount; Index++)
  1409. {
  1410. TComponent * Component = Root->Components[Index];
  1411. if (CompareText(Component->Name, Name) == 0)
  1412. {
  1413. return Component;
  1414. }
  1415. Component = FindComponentRecursively(Component, Name);
  1416. if (Component != NULL)
  1417. {
  1418. return Component;
  1419. }
  1420. }
  1421. return NULL;
  1422. }
  1423. //---------------------------------------------------------------------------
  1424. void __fastcall GetInstrutionsTheme(
  1425. TColor & MainInstructionColor, HFONT & MainInstructionFont, HFONT & InstructionFont)
  1426. {
  1427. MainInstructionColor = Graphics::clNone;
  1428. MainInstructionFont = 0;
  1429. InstructionFont = 0;
  1430. HTHEME Theme = OpenThemeData(0, L"TEXTSTYLE");
  1431. if (Theme != NULL)
  1432. {
  1433. LOGFONT AFont;
  1434. COLORREF AColor;
  1435. memset(&AFont, 0, sizeof(AFont));
  1436. // Using Canvas->Handle in the 2nd argument we can get scaled font,
  1437. // but at this point the form is sometime not scaled yet (difference is particularly for standalone messages like
  1438. // /UninstallCleanup), so the results are inconsistent.
  1439. if (GetThemeFont(Theme, NULL, TEXT_MAININSTRUCTION, 0, TMT_FONT, &AFont) == S_OK)
  1440. {
  1441. MainInstructionFont = CreateFontIndirect(&AFont);
  1442. }
  1443. if (GetThemeColor(Theme, TEXT_MAININSTRUCTION, 0, TMT_TEXTCOLOR, &AColor) == S_OK)
  1444. {
  1445. MainInstructionColor = (TColor)AColor;
  1446. }
  1447. memset(&AFont, 0, sizeof(AFont));
  1448. if (GetThemeFont(Theme, NULL, TEXT_INSTRUCTION, 0, TMT_FONT, &AFont) == S_OK)
  1449. {
  1450. InstructionFont = CreateFontIndirect(&AFont);
  1451. }
  1452. CloseThemeData(Theme);
  1453. }
  1454. }
  1455. //---------------------------------------------------------------------------
  1456. TLocalCustomCommand::TLocalCustomCommand()
  1457. {
  1458. }
  1459. //---------------------------------------------------------------------------
  1460. TLocalCustomCommand::TLocalCustomCommand(
  1461. const TCustomCommandData & Data, const UnicodeString & RemotePath, const UnicodeString & LocalPath) :
  1462. TFileCustomCommand(Data, RemotePath)
  1463. {
  1464. FLocalPath = LocalPath;
  1465. }
  1466. //---------------------------------------------------------------------------
  1467. TLocalCustomCommand::TLocalCustomCommand(const TCustomCommandData & Data,
  1468. const UnicodeString & RemotePath, const UnicodeString & LocalPath, const UnicodeString & FileName,
  1469. const UnicodeString & LocalFileName, const UnicodeString & FileList) :
  1470. TFileCustomCommand(Data, RemotePath, FileName, FileList)
  1471. {
  1472. FLocalPath = LocalPath;
  1473. FLocalFileName = LocalFileName;
  1474. }
  1475. //---------------------------------------------------------------------------
  1476. int __fastcall TLocalCustomCommand::PatternLen(const UnicodeString & Command, int Index)
  1477. {
  1478. int Len;
  1479. if ((Index < Command.Length()) && (Command[Index + 1] == L'\\'))
  1480. {
  1481. Len = 2;
  1482. }
  1483. else if ((Index < Command.Length()) && (Command[Index + 1] == L'^'))
  1484. {
  1485. Len = 3;
  1486. }
  1487. else
  1488. {
  1489. Len = TFileCustomCommand::PatternLen(Command, Index);
  1490. }
  1491. return Len;
  1492. }
  1493. //---------------------------------------------------------------------------
  1494. bool __fastcall TLocalCustomCommand::PatternReplacement(
  1495. int Index, const UnicodeString & Pattern, UnicodeString & Replacement, bool & Delimit)
  1496. {
  1497. bool Result;
  1498. if (Pattern == L"!\\")
  1499. {
  1500. // When used as "!\" in an argument to PowerShell, the trailing \ would escape the ",
  1501. // so we exclude it
  1502. Replacement = ExcludeTrailingBackslash(FLocalPath);
  1503. Result = true;
  1504. }
  1505. else if (Pattern == L"!^!")
  1506. {
  1507. Replacement = FLocalFileName;
  1508. Result = true;
  1509. }
  1510. else
  1511. {
  1512. Result = TFileCustomCommand::PatternReplacement(Index, Pattern, Replacement, Delimit);
  1513. }
  1514. return Result;
  1515. }
  1516. //---------------------------------------------------------------------------
  1517. void __fastcall TLocalCustomCommand::DelimitReplacement(
  1518. UnicodeString & /*Replacement*/, wchar_t /*Quote*/)
  1519. {
  1520. // never delimit local commands
  1521. }
  1522. //---------------------------------------------------------------------------
  1523. bool __fastcall TLocalCustomCommand::HasLocalFileName(const UnicodeString & Command)
  1524. {
  1525. return FindPattern(Command, L'^');
  1526. }
  1527. //---------------------------------------------------------------------------
  1528. bool __fastcall TLocalCustomCommand::IsFileCommand(const UnicodeString & Command)
  1529. {
  1530. return TFileCustomCommand::IsFileCommand(Command) || HasLocalFileName(Command);
  1531. }
  1532. //---------------------------------------------------------------------------
  1533. //---------------------------------------------------------------------------
  1534. typedef std::set<TDataModule *> TImagesModules;
  1535. static TImagesModules ImagesModules;
  1536. static std::map<int, TPngImageList *> AnimationsImages;
  1537. static std::map<int, TImageList *> ButtonImages;
  1538. static std::map<int, TPngImageList *> DialogImages;
  1539. //---------------------------------------------------------------------------
  1540. int __fastcall NormalizePixelsPerInch(int PixelsPerInch)
  1541. {
  1542. if (PixelsPerInch >= 192)
  1543. {
  1544. PixelsPerInch = 192;
  1545. }
  1546. else if (PixelsPerInch >= 144)
  1547. {
  1548. PixelsPerInch = 144;
  1549. }
  1550. else if (PixelsPerInch >= 120)
  1551. {
  1552. PixelsPerInch = 120;
  1553. }
  1554. else
  1555. {
  1556. PixelsPerInch = 96;
  1557. }
  1558. return PixelsPerInch;
  1559. }
  1560. //---------------------------------------------------------------------------
  1561. static int __fastcall NeedImagesModule(TControl * Control)
  1562. {
  1563. int PixelsPerInch = NormalizePixelsPerInch(GetControlPixelsPerInch(Control));
  1564. if (AnimationsImages.find(PixelsPerInch) == AnimationsImages.end())
  1565. {
  1566. TDataModule * ImagesModule;
  1567. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  1568. try
  1569. {
  1570. if (PixelsPerInch == 192)
  1571. {
  1572. ImagesModule = new TAnimations192Module(Application);
  1573. }
  1574. else if (PixelsPerInch == 144)
  1575. {
  1576. ImagesModule = new TAnimations144Module(Application);
  1577. }
  1578. else if (PixelsPerInch == 120)
  1579. {
  1580. ImagesModule = new TAnimations120Module(Application);
  1581. }
  1582. else
  1583. {
  1584. DebugAssert(PixelsPerInch == 96);
  1585. ImagesModule = new TAnimations96Module(Application);
  1586. }
  1587. ImagesModules.insert(ImagesModule);
  1588. TPngImageList * AAnimationImages =
  1589. DebugNotNull(dynamic_cast<TPngImageList *>(ImagesModule->FindComponent(L"AnimationImages")));
  1590. AnimationsImages.insert(std::make_pair(PixelsPerInch, AAnimationImages));
  1591. TImageList * AButtonImages =
  1592. DebugNotNull(dynamic_cast<TImageList *>(ImagesModule->FindComponent(L"ButtonImages")));
  1593. ButtonImages.insert(std::make_pair(PixelsPerInch, AButtonImages));
  1594. TPngImageList * ADialogImages =
  1595. DebugNotNull(dynamic_cast<TPngImageList *>(ImagesModule->FindComponent(L"DialogImages")));
  1596. DialogImages.insert(std::make_pair(PixelsPerInch, ADialogImages));
  1597. }
  1598. __finally
  1599. {
  1600. GUIConfiguration->ChangeResourceModule(ResourceModule);
  1601. }
  1602. }
  1603. return PixelsPerInch;
  1604. }
  1605. //---------------------------------------------------------------------------
  1606. TPngImageList * __fastcall GetAnimationsImages(TControl * Control)
  1607. {
  1608. int PixelsPerInch = NeedImagesModule(Control);
  1609. return DebugNotNull(AnimationsImages[PixelsPerInch]);
  1610. }
  1611. //---------------------------------------------------------------------------
  1612. TImageList * __fastcall GetButtonImages(TControl * Control)
  1613. {
  1614. int PixelsPerInch = NeedImagesModule(Control);
  1615. return DebugNotNull(ButtonImages[PixelsPerInch]);
  1616. }
  1617. //---------------------------------------------------------------------------
  1618. TPngImageList * __fastcall GetDialogImages(TControl * Control)
  1619. {
  1620. int PixelsPerInch = NeedImagesModule(Control);
  1621. return DebugNotNull(DialogImages[PixelsPerInch]);
  1622. }
  1623. //---------------------------------------------------------------------------
  1624. void __fastcall ReleaseImagesModules()
  1625. {
  1626. TImagesModules::iterator i = ImagesModules.begin();
  1627. while (i != ImagesModules.end())
  1628. {
  1629. delete (*i);
  1630. i++;
  1631. }
  1632. ImagesModules.clear();
  1633. }
  1634. //---------------------------------------------------------------------------
  1635. __fastcall TFrameAnimation::TFrameAnimation()
  1636. {
  1637. FFirstFrame = -1;
  1638. }
  1639. //---------------------------------------------------------------------------
  1640. void __fastcall TFrameAnimation::Init(TPaintBox * PaintBox, const UnicodeString & Name)
  1641. {
  1642. FName = Name;
  1643. FPaintBox = PaintBox;
  1644. FPaintBox->ControlStyle = FPaintBox->ControlStyle << csOpaque;
  1645. DebugAssert((FPaintBox->OnPaint == NULL) || (FPaintBox->OnPaint == PaintBoxPaint));
  1646. FPaintBox->OnPaint = PaintBoxPaint;
  1647. SetRescaleFunction(FPaintBox, PaintBoxRescale, reinterpret_cast<TObject *>(this));
  1648. DoInit();
  1649. }
  1650. //---------------------------------------------------------------------------
  1651. void __fastcall TFrameAnimation::DoInit()
  1652. {
  1653. FImageList = GetAnimationsImages(FPaintBox);
  1654. FFirstFrame = -1;
  1655. FFirstLoopFrame = -1;
  1656. FPaintBox->Width = FImageList->Width;
  1657. FPaintBox->Height = FImageList->Height;
  1658. if (!FName.IsEmpty())
  1659. {
  1660. int Frame = 0;
  1661. while (Frame < FImageList->PngImages->Count)
  1662. {
  1663. UnicodeString FrameData = FImageList->PngImages->Items[Frame]->Name;
  1664. UnicodeString FrameName;
  1665. FrameName = CutToChar(FrameData, L'_', false);
  1666. if (SameText(FName, FrameName))
  1667. {
  1668. int FrameIndex = StrToInt(CutToChar(FrameData, L'_', false));
  1669. if (FFirstFrame < 0)
  1670. {
  1671. FFirstFrame = Frame;
  1672. }
  1673. if ((FFirstLoopFrame < 0) && (FrameIndex > 0))
  1674. {
  1675. FFirstLoopFrame = Frame;
  1676. }
  1677. FLastFrame = Frame;
  1678. }
  1679. else
  1680. {
  1681. if (FFirstFrame >= 0)
  1682. {
  1683. // optimization
  1684. break;
  1685. }
  1686. }
  1687. Frame++;
  1688. }
  1689. DebugAssert(FFirstFrame >= 0);
  1690. DebugAssert(FFirstLoopFrame >= 0);
  1691. }
  1692. Stop();
  1693. }
  1694. //---------------------------------------------------------------------------
  1695. void __fastcall TFrameAnimation::PaintBoxRescale(TComponent * /*Sender*/, TObject * Token)
  1696. {
  1697. TFrameAnimation * FrameAnimation = reinterpret_cast<TFrameAnimation *>(Token);
  1698. FrameAnimation->Rescale();
  1699. }
  1700. //---------------------------------------------------------------------------
  1701. void __fastcall TFrameAnimation::Rescale()
  1702. {
  1703. bool Started = (FTimer != NULL) && FTimer->Enabled;
  1704. DoInit();
  1705. if (Started)
  1706. {
  1707. Start();
  1708. }
  1709. }
  1710. //---------------------------------------------------------------------------
  1711. void __fastcall TFrameAnimation::Start()
  1712. {
  1713. if (FFirstFrame >= 0)
  1714. {
  1715. FNextFrameTick = GetTickCount();
  1716. CalculateNextFrameTick();
  1717. if (FTimer == NULL)
  1718. {
  1719. FTimer = new TTimer(GetParentForm(FPaintBox));
  1720. FTimer->Interval = static_cast<int>(GUIUpdateInterval);
  1721. FTimer->OnTimer = Timer;
  1722. }
  1723. else
  1724. {
  1725. // reset timer
  1726. FTimer->Enabled = false;
  1727. FTimer->Enabled = true;
  1728. }
  1729. }
  1730. }
  1731. //---------------------------------------------------------------------------
  1732. void __fastcall TFrameAnimation::Timer(TObject * /*Sender*/)
  1733. {
  1734. Animate();
  1735. }
  1736. //---------------------------------------------------------------------------
  1737. void __fastcall TFrameAnimation::PaintBoxPaint(TObject * Sender)
  1738. {
  1739. if (FFirstFrame >= 0)
  1740. {
  1741. // Double-buffered drawing to prevent flicker (as the images are transparent)
  1742. DebugUsedParam(Sender);
  1743. DebugAssert(FPaintBox == Sender);
  1744. DebugAssert(FPaintBox->ControlStyle.Contains(csOpaque));
  1745. std::unique_ptr<TBitmap> Bitmap(new TBitmap());
  1746. Bitmap->SetSize(FPaintBox->Width, FPaintBox->Height);
  1747. Bitmap->Canvas->Brush->Color = FPaintBox->Color;
  1748. TRect Rect(0, 0, Bitmap->Width, Bitmap->Height);
  1749. Bitmap->Canvas->FillRect(Rect);
  1750. TGraphic * Graphic = GetCurrentImage()->PngImage;
  1751. DebugAssert(Graphic->Width == FPaintBox->Width);
  1752. DebugAssert(Graphic->Height == FPaintBox->Height);
  1753. Bitmap->Canvas->Draw(0, 0, Graphic);
  1754. FPaintBox->Canvas->Draw(0, 0, Bitmap.get());
  1755. }
  1756. FPainted = true;
  1757. }
  1758. //---------------------------------------------------------------------------
  1759. void __fastcall TFrameAnimation::Repaint()
  1760. {
  1761. FPainted = false;
  1762. // If the form is not showing yet, the Paint() is not even called
  1763. FPaintBox->Repaint();
  1764. if (!FPainted)
  1765. {
  1766. // Paint later, alternativelly we may keep trying Repaint() in Animate().
  1767. // See also a hack in TAuthenticateForm::Log.
  1768. FPaintBox->Invalidate();
  1769. }
  1770. }
  1771. //---------------------------------------------------------------------------
  1772. void __fastcall TFrameAnimation::Stop()
  1773. {
  1774. FNextFrameTick = std::numeric_limits<DWORD>::max();
  1775. FCurrentFrame = FFirstFrame;
  1776. Repaint();
  1777. if (FTimer != NULL)
  1778. {
  1779. FTimer->Enabled = false;
  1780. }
  1781. }
  1782. //---------------------------------------------------------------------------
  1783. void __fastcall TFrameAnimation::Animate()
  1784. {
  1785. if (FFirstFrame >= 0)
  1786. {
  1787. // UPGRADE: Use GetTickCount64() when we stop supporting Windows XP.
  1788. DWORD TickCount = GetTickCount();
  1789. // Keep in sync with an opposite condition at the end of the loop.
  1790. // We may skip some frames if we got stalled for a while
  1791. while (TickCount >= FNextFrameTick)
  1792. {
  1793. if (FCurrentFrame >= FLastFrame)
  1794. {
  1795. FCurrentFrame = FFirstLoopFrame;
  1796. }
  1797. else
  1798. {
  1799. FCurrentFrame++;
  1800. }
  1801. CalculateNextFrameTick();
  1802. Repaint();
  1803. }
  1804. }
  1805. }
  1806. //---------------------------------------------------------------------------
  1807. TPngImageCollectionItem * __fastcall TFrameAnimation::GetCurrentImage()
  1808. {
  1809. return FImageList->PngImages->Items[FCurrentFrame];
  1810. }
  1811. //---------------------------------------------------------------------------
  1812. void __fastcall TFrameAnimation::CalculateNextFrameTick()
  1813. {
  1814. TPngImageCollectionItem * ImageItem = GetCurrentImage();
  1815. UnicodeString Duration = ImageItem->Name;
  1816. CutToChar(Duration, L'_', false);
  1817. // skip index (is not really used)
  1818. CutToChar(Duration, L'_', false);
  1819. // This should overflow, when tick count wraps.
  1820. FNextFrameTick += StrToInt(Duration) * 10;
  1821. }
  1822. //---------------------------------------------------------------------------
  1823. //---------------------------------------------------------------------------
  1824. // Hints use:
  1825. // - Cleanup list tooltip (multi line)
  1826. // - Combo edit button
  1827. // - Transfer settings label (multi line, follows label size and font)
  1828. // - HintLabel (hint and persistent hint, multi line)
  1829. // - status bar hints
  1830. //---------------------------------------------------------------------------
  1831. __fastcall TScreenTipHintWindow::TScreenTipHintWindow(TComponent * Owner) :
  1832. THintWindow(Owner)
  1833. {
  1834. FParentPainting = false;
  1835. }
  1836. //---------------------------------------------------------------------------
  1837. int __fastcall TScreenTipHintWindow::GetTextFlags(TControl * Control)
  1838. {
  1839. return DT_LEFT | DT_WORDBREAK | DT_NOPREFIX | Control->DrawTextBiDiModeFlagsReadingOnly();
  1840. }
  1841. //---------------------------------------------------------------------------
  1842. bool __fastcall TScreenTipHintWindow::UseBoldShortHint(TControl * HintControl)
  1843. {
  1844. return
  1845. (dynamic_cast<TTBCustomDockableWindow *>(HintControl) != NULL) ||
  1846. (dynamic_cast<TTBPopupWindow *>(HintControl) != NULL) ||
  1847. (HintControl->Perform(WM_WANTS_SCREEN_TIPS, 0, 0) == 1);
  1848. }
  1849. //---------------------------------------------------------------------------
  1850. bool __fastcall TScreenTipHintWindow::IsPathLabel(TControl * HintControl)
  1851. {
  1852. return (dynamic_cast<TPathLabel *>(HintControl) != NULL);
  1853. }
  1854. //---------------------------------------------------------------------------
  1855. int __fastcall TScreenTipHintWindow::GetMargin(TControl * HintControl, const UnicodeString & Hint)
  1856. {
  1857. int Result;
  1858. if (HasLabelHintPopup(HintControl, Hint) || IsPathLabel(HintControl))
  1859. {
  1860. Result = 3;
  1861. }
  1862. else
  1863. {
  1864. Result = 6;
  1865. }
  1866. Result = ScaleByTextHeight(HintControl, Result);
  1867. return Result;
  1868. }
  1869. //---------------------------------------------------------------------------
  1870. TFont * __fastcall TScreenTipHintWindow::GetFont(TControl * HintControl, const UnicodeString & Hint)
  1871. {
  1872. TFont * Result;
  1873. if (HasLabelHintPopup(HintControl, Hint) || IsPathLabel(HintControl))
  1874. {
  1875. Result = reinterpret_cast<TLabel *>(dynamic_cast<TCustomLabel *>(HintControl))->Font;
  1876. }
  1877. else
  1878. {
  1879. FScaledHintFont.reset(new TFont());
  1880. FScaledHintFont->Assign(Screen->HintFont);
  1881. FScaledHintFont->Size = ScaleByPixelsPerInchFromSystem(FScaledHintFont->Size, HintControl);
  1882. Result = FScaledHintFont.get();
  1883. }
  1884. return Result;
  1885. }
  1886. //---------------------------------------------------------------------------
  1887. void __fastcall TScreenTipHintWindow::CalcHintTextRect(TControl * Control, TCanvas * Canvas, TRect & Rect, const UnicodeString & Hint)
  1888. {
  1889. const int Flags = DT_CALCRECT | GetTextFlags(Control);
  1890. DrawText(Canvas->Handle, Hint.c_str(), -1, &Rect, Flags);
  1891. }
  1892. //---------------------------------------------------------------------------
  1893. TRect __fastcall TScreenTipHintWindow::CalcHintRect(int MaxWidth, const UnicodeString AHint, void * AData)
  1894. {
  1895. TControl * HintControl = GetHintControl(AData);
  1896. int Margin = GetMargin(HintControl, AHint);
  1897. UnicodeString ShortHint;
  1898. UnicodeString LongHint;
  1899. SplitHint(HintControl, AHint, ShortHint, LongHint);
  1900. Canvas->Font->Assign(GetFont(HintControl, AHint));
  1901. const int ScreenTipTextOnlyWidth = ScaleByTextHeight(HintControl, cScreenTipTextOnlyWidth);
  1902. int LongHintMargin = 0; // shut up
  1903. bool HasLongHint = !LongHint.IsEmpty();
  1904. if (HasLongHint)
  1905. {
  1906. // double-margin on the right
  1907. LongHintMargin = (3 * Margin);
  1908. MaxWidth = ScreenTipTextOnlyWidth - LongHintMargin;
  1909. }
  1910. // Multi line short hints can be twice as wide, to not break the individual lines unless really necessary.
  1911. // (login site tree, clean up dialog list, preferences custom command list, persistent hint, etc).
  1912. // And they also can be twice as wide, to not break the individual lines unless really necessary.
  1913. if (ShortHint.Pos(L"\n") > 0)
  1914. {
  1915. MaxWidth *= 2;
  1916. }
  1917. bool HintPopup = HasLabelHintPopup(HintControl, AHint);
  1918. if (HintPopup)
  1919. {
  1920. MaxWidth = HintControl->Width;
  1921. }
  1922. if (UseBoldShortHint(HintControl))
  1923. {
  1924. Canvas->Font->Style = TFontStyles() << fsBold;
  1925. }
  1926. TRect ShortRect(0, 0, MaxWidth, 0);
  1927. CalcHintTextRect(this, Canvas, ShortRect, ShortHint);
  1928. Canvas->Font->Style = TFontStyles();
  1929. TRect Result;
  1930. if (!HasLongHint)
  1931. {
  1932. Result = ShortRect;
  1933. if (HintPopup)
  1934. {
  1935. Result.Right = MaxWidth + 2 * Margin;
  1936. }
  1937. else
  1938. {
  1939. Result.Right += 3 * Margin;
  1940. }
  1941. Result.Bottom += 2 * Margin;
  1942. }
  1943. else
  1944. {
  1945. const int LongIndentation = Margin * 3 / 2;
  1946. TRect LongRect(0, 0, MaxWidth - LongIndentation, 0);
  1947. CalcHintTextRect(this, Canvas, LongRect, LongHint);
  1948. Result.Right = Max(ScreenTipTextOnlyWidth, LongRect.Right + LongIndentation + LongHintMargin);
  1949. Result.Bottom = Margin + ShortRect.Height() + (Margin / 3 * 5) + LongRect.Height() + Margin;
  1950. }
  1951. // VCLCOPY: To counter the increase in THintWindow::ActivateHintData
  1952. Result.Bottom -= 4;
  1953. return Result;
  1954. }
  1955. //---------------------------------------------------------------------------
  1956. void __fastcall TScreenTipHintWindow::SplitHint(
  1957. TControl * HintControl, const UnicodeString & Hint, UnicodeString & ShortHint, UnicodeString & LongHint)
  1958. {
  1959. if (HasLabelHintPopup(HintControl, Hint))
  1960. {
  1961. ShortHint = HintControl->Hint;
  1962. }
  1963. else
  1964. {
  1965. ShortHint = GetShortHint(Hint);
  1966. LongHint = GetLongHintIfAny(Hint);
  1967. }
  1968. }
  1969. //---------------------------------------------------------------------------
  1970. void __fastcall TScreenTipHintWindow::ActivateHintData(const TRect & ARect, const UnicodeString AHint, void * AData)
  1971. {
  1972. FHintControl = GetHintControl(AData);
  1973. SplitHint(FHintControl, AHint, FShortHint, FLongHint);
  1974. FMargin = GetMargin(FHintControl, AHint);
  1975. FHintPopup = HasLabelHintPopup(FHintControl, AHint);
  1976. Canvas->Font->Assign(GetFont(FHintControl, AHint));
  1977. TRect Rect = ARect;
  1978. if (FHintPopup)
  1979. {
  1980. Rect.SetLocation(FHintControl->ClientToScreen(TPoint(-FMargin, -FMargin)));
  1981. }
  1982. if (IsPathLabel(FHintControl))
  1983. {
  1984. Rect.Offset(-FMargin, -FMargin);
  1985. }
  1986. THintWindow::ActivateHintData(Rect, FShortHint, AData);
  1987. }
  1988. //---------------------------------------------------------------------------
  1989. TControl * __fastcall TScreenTipHintWindow::GetHintControl(void * Data)
  1990. {
  1991. return reinterpret_cast<TControl *>(DebugNotNull(Data));
  1992. }
  1993. //---------------------------------------------------------------------------
  1994. UnicodeString __fastcall TScreenTipHintWindow::GetLongHintIfAny(const UnicodeString & AHint)
  1995. {
  1996. UnicodeString Result;
  1997. int P = Pos(L"|", AHint);
  1998. if (P > 0)
  1999. {
  2000. Result = GetLongHint(AHint);
  2001. }
  2002. return Result;
  2003. }
  2004. //---------------------------------------------------------------------------
  2005. void __fastcall TScreenTipHintWindow::Dispatch(void * AMessage)
  2006. {
  2007. TMessage * Message = static_cast<TMessage*>(AMessage);
  2008. switch (Message->Msg)
  2009. {
  2010. case WM_GETTEXTLENGTH:
  2011. if (FParentPainting)
  2012. {
  2013. // make THintWindow::Paint() not paint the Caption
  2014. Message->Result = 0;
  2015. }
  2016. else
  2017. {
  2018. THintWindow::Dispatch(AMessage);
  2019. }
  2020. break;
  2021. default:
  2022. THintWindow::Dispatch(AMessage);
  2023. break;
  2024. }
  2025. }
  2026. //---------------------------------------------------------------------------
  2027. void __fastcall TScreenTipHintWindow::Paint()
  2028. {
  2029. // paint frame/background
  2030. {
  2031. TAutoFlag ParentPaintingFlag(FParentPainting);
  2032. THintWindow::Paint();
  2033. }
  2034. const int Flags = GetTextFlags(this);
  2035. const int Margin = FMargin - 1; // 1 = border
  2036. TRect Rect = ClientRect;
  2037. Rect.Inflate(-Margin, -Margin);
  2038. if (!FHintPopup)
  2039. {
  2040. Rect.Right -= FMargin;
  2041. }
  2042. if (UseBoldShortHint(FHintControl))
  2043. {
  2044. Canvas->Font->Style = TFontStyles() << fsBold;
  2045. }
  2046. DrawText(Canvas->Handle, FShortHint.c_str(), -1, &Rect, Flags);
  2047. TRect ShortRect = Rect;
  2048. DrawText(Canvas->Handle, FShortHint.c_str(), -1, &ShortRect, DT_CALCRECT | Flags);
  2049. Canvas->Font->Style = TFontStyles();
  2050. if (!FLongHint.IsEmpty())
  2051. {
  2052. Rect.Left += FMargin * 3 / 2;
  2053. Rect.Top += ShortRect.Height() + (FMargin / 3 * 5);
  2054. DrawText(Canvas->Handle, FLongHint.c_str(), -1, &Rect, Flags);
  2055. }
  2056. }
  2057. //---------------------------------------------------------------------------
  2058. //---------------------------------------------------------------------------
  2059. __fastcall TNewRichEdit::TNewRichEdit(TComponent * AOwner) :
  2060. TRichEdit(AOwner),
  2061. FLibrary(0)
  2062. {
  2063. }
  2064. //---------------------------------------------------------------------------
  2065. void __fastcall TNewRichEdit::CreateParams(TCreateParams & Params)
  2066. {
  2067. UnicodeString RichEditModuleName(L"MSFTEDIT.DLL");
  2068. long int OldError;
  2069. OldError = SetErrorMode(SEM_NOOPENFILEERRORBOX);
  2070. FLibrary = LoadLibrary(RichEditModuleName.c_str());
  2071. SetErrorMode(OldError);
  2072. // No fallback, MSFTEDIT.DLL is available since Windows XP
  2073. // https://docs.microsoft.com/en-us/archive/blogs/murrays/richedit-versions
  2074. if (FLibrary == 0)
  2075. {
  2076. throw Exception(FORMAT(L"Cannot load %s", (RichEditModuleName)));
  2077. }
  2078. TCustomMemo::CreateParams(Params);
  2079. // MSDN says that we should use MSFTEDIT_CLASS to load Rich Edit 4.1:
  2080. // https://docs.microsoft.com/en-us/windows/win32/controls/about-rich-edit-controls
  2081. // But MSFTEDIT_CLASS is defined as "RICHEDIT50W",
  2082. // so not sure what version we are loading.
  2083. // Seem to work on Windows XP SP3.
  2084. CreateSubClass(Params, MSFTEDIT_CLASS);
  2085. }
  2086. //---------------------------------------------------------------------------
  2087. void __fastcall TNewRichEdit::CreateWnd()
  2088. {
  2089. TRichEdit::CreateWnd();
  2090. SendMessage(Handle, EM_SETEDITSTYLEEX, 0, SES_EX_HANDLEFRIENDLYURL);
  2091. }
  2092. //---------------------------------------------------------------------------
  2093. void __fastcall TNewRichEdit::DestroyWnd()
  2094. {
  2095. TRichEdit::DestroyWnd();
  2096. if (FLibrary != 0)
  2097. {
  2098. FreeLibrary(FLibrary);
  2099. }
  2100. }
  2101. //---------------------------------------------------------------------------
  2102. static int HideAccelFlag(TControl * Control)
  2103. {
  2104. //ask the top level window about its UI state
  2105. while (Control->Parent != NULL)
  2106. {
  2107. Control = Control->Parent;
  2108. }
  2109. int Result;
  2110. if (FLAGSET(Control->Perform(WM_QUERYUISTATE, 0, 0), UISF_HIDEACCEL))
  2111. {
  2112. Result = DT_HIDEPREFIX;
  2113. }
  2114. else
  2115. {
  2116. Result = 0;
  2117. }
  2118. return Result;
  2119. }
  2120. //---------------------------------------------------------------------------
  2121. void __fastcall TUIStateAwareLabel::DoDrawText(TRect & Rect, int Flags)
  2122. {
  2123. if (ShowAccelChar)
  2124. {
  2125. Flags = Flags | HideAccelFlag(this);
  2126. }
  2127. TLabel::DoDrawText(Rect, Flags);
  2128. }
  2129. //---------------------------------------------------------------------------
  2130. void __fastcall FindComponentClass(
  2131. void *, TReader *, const UnicodeString DebugUsedArg(ClassName), TComponentClass & ComponentClass)
  2132. {
  2133. if (ComponentClass == __classid(TLabel))
  2134. {
  2135. ComponentClass = __classid(TUIStateAwareLabel);
  2136. }
  2137. else if (ComponentClass == __classid(TComboBox))
  2138. {
  2139. ComponentClass = __classid(TUIStateAwareComboBox);
  2140. }
  2141. }
  2142. //---------------------------------------------------------------------------
  2143. bool CanShowTimeEstimate(TDateTime StartTime)
  2144. {
  2145. return (SecondsBetween(StartTime, Now()) >= 3);
  2146. }
  2147. //---------------------------------------------------------------------------
  2148. void GUIFinalize()
  2149. {
  2150. TPuttyCleanupThread::Finalize();
  2151. }