GUITools.cpp 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <shlobj.h>
  5. #include <Common.h>
  6. #include "GUITools.h"
  7. #include "GUIConfiguration.h"
  8. #include <TextsCore.h>
  9. #include <CoreMain.h>
  10. #include <SessionData.h>
  11. #include <WinInterface.h>
  12. #include <TbxUtils.hpp>
  13. #include <Math.hpp>
  14. #include <WebBrowserEx.hpp>
  15. #include <Tools.h>
  16. #include "PngImageList.hpp"
  17. #include <StrUtils.hpp>
  18. #include <limits>
  19. #include <Glyphs.h>
  20. #include <PasTools.hpp>
  21. #include <VCLCommon.h>
  22. #include <WinApi.h>
  23. #include <Vcl.ScreenTips.hpp>
  24. #include <vssym32.h>
  25. #include "Animations96.h"
  26. #include "Animations120.h"
  27. #include "Animations144.h"
  28. #include "Animations192.h"
  29. //---------------------------------------------------------------------------
  30. #pragma package(smart_init)
  31. //---------------------------------------------------------------------------
  32. extern const UnicodeString PageantTool = L"pageant.exe";
  33. extern const UnicodeString PuttygenTool = L"puttygen.exe";
  34. //---------------------------------------------------------------------------
  35. bool __fastcall FindFile(UnicodeString & Path)
  36. {
  37. bool Result = FileExists(ApiPath(Path));
  38. if (!Result)
  39. {
  40. UnicodeString ProgramFiles32 = IncludeTrailingBackslash(GetEnvironmentVariable(L"ProgramFiles"));
  41. UnicodeString ProgramFiles64 = IncludeTrailingBackslash(GetEnvironmentVariable(L"ProgramW6432"));
  42. if (!ProgramFiles32.IsEmpty() &&
  43. SameText(Path.SubString(1, ProgramFiles32.Length()), ProgramFiles32) &&
  44. !ProgramFiles64.IsEmpty())
  45. {
  46. UnicodeString Path64 =
  47. ProgramFiles64 + Path.SubString(ProgramFiles32.Length() + 1, Path.Length() - ProgramFiles32.Length());
  48. if (FileExists(ApiPath(Path64)))
  49. {
  50. Path = Path64;
  51. Result = true;
  52. }
  53. }
  54. }
  55. if (!Result)
  56. {
  57. UnicodeString Paths = GetEnvironmentVariable(L"PATH");
  58. if (!Paths.IsEmpty())
  59. {
  60. UnicodeString NewPath = FileSearch(ExtractFileName(Path), Paths);
  61. Result = !NewPath.IsEmpty();
  62. if (Result)
  63. {
  64. Path = NewPath;
  65. }
  66. }
  67. }
  68. return Result;
  69. }
  70. //---------------------------------------------------------------------------
  71. void __fastcall OpenSessionInPutty(const UnicodeString PuttyPath,
  72. TSessionData * SessionData)
  73. {
  74. UnicodeString Program, AParams, Dir;
  75. SplitCommand(PuttyPath, Program, AParams, Dir);
  76. Program = ExpandEnvironmentVariables(Program);
  77. if (FindFile(Program))
  78. {
  79. AParams = ExpandEnvironmentVariables(AParams);
  80. UnicodeString Password = GUIConfiguration->PuttyPassword ? SessionData->Password : UnicodeString();
  81. TCustomCommandData Data(SessionData, SessionData->UserName, Password);
  82. TRemoteCustomCommand RemoteCustomCommand(Data, SessionData->RemoteDirectory);
  83. TWinInteractiveCustomCommand InteractiveCustomCommand(
  84. &RemoteCustomCommand, L"PuTTY", UnicodeString());
  85. UnicodeString Params =
  86. RemoteCustomCommand.Complete(InteractiveCustomCommand.Complete(AParams, false), true);
  87. UnicodeString PuttyParams;
  88. if (!RemoteCustomCommand.IsSiteCommand(AParams))
  89. {
  90. {
  91. bool SessionList = false;
  92. std::unique_ptr<THierarchicalStorage> SourceHostKeyStorage(Configuration->CreateScpStorage(SessionList));
  93. std::unique_ptr<THierarchicalStorage> TargetHostKeyStorage(new TRegistryStorage(Configuration->PuttyRegistryStorageKey));
  94. TargetHostKeyStorage->Explicit = true;
  95. TargetHostKeyStorage->AccessMode = smReadWrite;
  96. std::unique_ptr<TStoredSessionList> HostKeySessionList(new TStoredSessionList());
  97. HostKeySessionList->OwnsObjects = false;
  98. HostKeySessionList->Add(SessionData);
  99. TStoredSessionList::ImportHostKeys(SourceHostKeyStorage.get(), TargetHostKeyStorage.get(), HostKeySessionList.get(), false);
  100. }
  101. if (IsUWP())
  102. {
  103. bool Telnet = (SessionData->FSProtocol == fsFTP) && GUIConfiguration->TelnetForFtpInPutty;
  104. if (Telnet)
  105. {
  106. AddToList(PuttyParams, L"-telnet", L" ");
  107. // PuTTY does not allow -pw for telnet
  108. Password = L"";
  109. }
  110. AddToList(PuttyParams, EscapePuttyCommandParam(SessionData->HostName), L" ");
  111. if (!SessionData->UserName.IsEmpty())
  112. {
  113. AddToList(PuttyParams, FORMAT(L"-l %s", (EscapePuttyCommandParam(SessionData->UserName))), L" ");
  114. }
  115. if ((SessionData->FSProtocol != fsFTP) && (SessionData->PortNumber != SshPortNumber))
  116. {
  117. AddToList(PuttyParams, FORMAT(L"-P %d", (SessionData->PortNumber)), L" ");
  118. }
  119. if (!Telnet)
  120. {
  121. if (!SessionData->PublicKeyFile.IsEmpty())
  122. {
  123. AddToList(PuttyParams, FORMAT(L"-i \"%s\"", (SessionData->PublicKeyFile)), L" ");
  124. }
  125. AddToList(PuttyParams, (SessionData->TryAgent ? L"-agent" : L"-noagent"), L" ");
  126. if (SessionData->TryAgent)
  127. {
  128. AddToList(PuttyParams, (SessionData->AgentFwd ? L"-A" : L"-a"), L" ");
  129. }
  130. if (SessionData->Compression)
  131. {
  132. AddToList(PuttyParams, L"-C", L" ");
  133. }
  134. AddToList(PuttyParams,
  135. ((SessionData->SshProt == ssh1only || SessionData->SshProt == ssh1deprecated) ? L"-1" : L"-2"), L" ");
  136. if (!SessionData->LogicalHostName.IsEmpty())
  137. {
  138. AddToList(PuttyParams, FORMAT(L"-loghost \"%s\"", (SessionData->LogicalHostName)), L" ");
  139. }
  140. }
  141. if (SessionData->AddressFamily == afIPv4)
  142. {
  143. AddToList(PuttyParams, L"-4", L" ");
  144. }
  145. else if (SessionData->AddressFamily == afIPv6)
  146. {
  147. AddToList(PuttyParams, L"-6", L" ");
  148. }
  149. }
  150. else
  151. {
  152. UnicodeString SessionName;
  153. TRegistryStorage * Storage = NULL;
  154. TSessionData * ExportData = NULL;
  155. TRegistryStorage * SourceStorage = NULL;
  156. try
  157. {
  158. Storage = new TRegistryStorage(Configuration->PuttySessionsKey);
  159. Storage->AccessMode = smReadWrite;
  160. // make it compatible with putty
  161. Storage->MungeStringValues = false;
  162. Storage->ForceAnsi = true;
  163. if (Storage->OpenRootKey(true))
  164. {
  165. if (Storage->KeyExists(SessionData->StorageKey))
  166. {
  167. SessionName = SessionData->SessionName;
  168. }
  169. else
  170. {
  171. SourceStorage = new TRegistryStorage(Configuration->PuttySessionsKey);
  172. SourceStorage->MungeStringValues = false;
  173. SourceStorage->ForceAnsi = true;
  174. if (SourceStorage->OpenSubKey(StoredSessions->DefaultSettings->Name, false) &&
  175. Storage->OpenSubKey(GUIConfiguration->PuttySession, true))
  176. {
  177. Storage->Copy(SourceStorage);
  178. Storage->CloseSubKey();
  179. }
  180. ExportData = new TSessionData(L"");
  181. ExportData->Assign(SessionData);
  182. ExportData->Modified = true;
  183. ExportData->Name = GUIConfiguration->PuttySession;
  184. ExportData->WinTitle = SessionData->SessionName;
  185. ExportData->Password = L"";
  186. if (SessionData->FSProtocol == fsFTP)
  187. {
  188. if (GUIConfiguration->TelnetForFtpInPutty)
  189. {
  190. ExportData->PuttyProtocol = PuttyTelnetProtocol;
  191. ExportData->PortNumber = TelnetPortNumber;
  192. // PuTTY does not allow -pw for telnet
  193. Password = L"";
  194. }
  195. else
  196. {
  197. ExportData->PuttyProtocol = PuttySshProtocol;
  198. ExportData->PortNumber = SshPortNumber;
  199. }
  200. }
  201. ExportData->Save(Storage, true);
  202. SessionName = GUIConfiguration->PuttySession;
  203. }
  204. }
  205. }
  206. __finally
  207. {
  208. delete Storage;
  209. delete ExportData;
  210. delete SourceStorage;
  211. }
  212. UnicodeString LoadSwitch = L"-load";
  213. int P = Params.LowerCase().Pos(LoadSwitch + L" ");
  214. if ((P == 0) || ((P > 1) && (Params[P - 1] != L' ')))
  215. {
  216. AddToList(PuttyParams, FORMAT(L"%s %s", (LoadSwitch, EscapePuttyCommandParam(SessionName))), L" ");
  217. }
  218. }
  219. }
  220. if (!Password.IsEmpty() && !RemoteCustomCommand.IsPasswordCommand(AParams))
  221. {
  222. Password = NormalizeString(Password); // if password is empty, we should quote it always
  223. AddToList(PuttyParams, FORMAT(L"-pw %s", (EscapePuttyCommandParam(Password))), L" ");
  224. }
  225. AddToList(PuttyParams, Params, L" ");
  226. // PuTTY is started in its binary directory to allow relative paths in private key,
  227. // when opening PuTTY's own stored session.
  228. ExecuteShellChecked(Program, PuttyParams, true);
  229. }
  230. else
  231. {
  232. throw Exception(FMTLOAD(FILE_NOT_FOUND, (Program)));
  233. }
  234. }
  235. //---------------------------------------------------------------------------
  236. bool __fastcall FindTool(const UnicodeString & Name, UnicodeString & Path)
  237. {
  238. UnicodeString AppPath = IncludeTrailingBackslash(ExtractFilePath(Application->ExeName));
  239. Path = AppPath + Name;
  240. bool Result = true;
  241. if (!FileExists(ApiPath(Path)))
  242. {
  243. Path = AppPath + L"PuTTY\\" + Name;
  244. if (!FileExists(ApiPath(Path)))
  245. {
  246. Path = Name;
  247. if (!FindFile(Path))
  248. {
  249. Result = false;
  250. }
  251. }
  252. }
  253. return Result;
  254. }
  255. //---------------------------------------------------------------------------
  256. void __fastcall ExecuteTool(const UnicodeString & Name)
  257. {
  258. UnicodeString Path;
  259. if (!FindTool(Name, Path))
  260. {
  261. throw Exception(FMTLOAD(EXECUTE_APP_ERROR, (Name)));
  262. }
  263. ExecuteShellChecked(Path, L"");
  264. }
  265. //---------------------------------------------------------------------------
  266. TObjectList * StartCreationDirectoryMonitorsOnEachDrive(unsigned int Filter, TFileChangedEvent OnChanged)
  267. {
  268. std::unique_ptr<TObjectList> Result(new TObjectList());
  269. for (char Drive = FirstDrive; Drive <= LastDrive; Drive++)
  270. {
  271. std::unique_ptr<TDirectoryMonitor> Monitor(new TDirectoryMonitor(Application));
  272. TDriveInfoRec * DriveInfoRec = DriveInfo->Get(Drive);
  273. if (DriveInfoRec->Valid &&
  274. (DriveInfoRec->DriveType != DRIVE_CDROM) &&
  275. ((DriveInfoRec->DriveType != DRIVE_REMOVABLE) || (Drive >= FirstFixedDrive)))
  276. {
  277. try
  278. {
  279. Monitor->Path = DriveInfo->GetDriveRoot(Drive);
  280. Monitor->WatchSubtree = true;
  281. Monitor->WatchFilters = Filter;
  282. Monitor->OnCreated = OnChanged;
  283. Monitor->OnModified = OnChanged;
  284. Monitor->Active = true;
  285. Result->Add(Monitor.release());
  286. }
  287. catch (Exception & E)
  288. {
  289. // Ignore errors watching not-ready drives
  290. }
  291. }
  292. }
  293. return Result.release();
  294. }
  295. //---------------------------------------------------------------------------
  296. bool DontCopyCommandToClipboard = false;
  297. //---------------------------------------------------------------------------
  298. bool __fastcall CopyCommandToClipboard(const UnicodeString & Command)
  299. {
  300. bool Result = !DontCopyCommandToClipboard && UseAlternativeFunction() && IsKeyPressed(VK_CONTROL);
  301. if (Result)
  302. {
  303. TInstantOperationVisualizer Visualizer;
  304. CopyToClipboard(Command);
  305. }
  306. return Result;
  307. }
  308. //---------------------------------------------------------------------------
  309. static bool __fastcall DoExecuteShell(const UnicodeString Path, const UnicodeString Params,
  310. bool ChangeWorkingDirectory, HANDLE * Handle)
  311. {
  312. bool Result = CopyCommandToClipboard(FormatCommand(Path, Params));
  313. if (Result)
  314. {
  315. if (Handle != NULL)
  316. {
  317. *Handle = NULL;
  318. }
  319. }
  320. else
  321. {
  322. UnicodeString Directory = ExtractFilePath(Path);
  323. TShellExecuteInfoW ExecuteInfo;
  324. memset(&ExecuteInfo, 0, sizeof(ExecuteInfo));
  325. ExecuteInfo.cbSize = sizeof(ExecuteInfo);
  326. ExecuteInfo.fMask =
  327. SEE_MASK_FLAG_NO_UI |
  328. FLAGMASK((Handle != NULL), SEE_MASK_NOCLOSEPROCESS);
  329. ExecuteInfo.hwnd = Application->Handle;
  330. ExecuteInfo.lpFile = (wchar_t*)Path.data();
  331. ExecuteInfo.lpParameters = (wchar_t*)Params.data();
  332. ExecuteInfo.lpDirectory = (ChangeWorkingDirectory ? Directory.c_str() : NULL);
  333. ExecuteInfo.nShow = SW_SHOW;
  334. Result = (ShellExecuteEx(&ExecuteInfo) != 0);
  335. if (Result)
  336. {
  337. if (Handle != NULL)
  338. {
  339. *Handle = ExecuteInfo.hProcess;
  340. }
  341. }
  342. }
  343. return Result;
  344. }
  345. //---------------------------------------------------------------------------
  346. void __fastcall ExecuteShellChecked(const UnicodeString Path, const UnicodeString Params, bool ChangeWorkingDirectory)
  347. {
  348. if (!DoExecuteShell(Path, Params, ChangeWorkingDirectory, NULL))
  349. {
  350. throw EOSExtException(FMTLOAD(EXECUTE_APP_ERROR, (Path)));
  351. }
  352. }
  353. //---------------------------------------------------------------------------
  354. void __fastcall ExecuteShellChecked(const UnicodeString Command)
  355. {
  356. UnicodeString Program, Params, Dir;
  357. SplitCommand(Command, Program, Params, Dir);
  358. ExecuteShellChecked(Program, Params);
  359. }
  360. //---------------------------------------------------------------------------
  361. bool __fastcall ExecuteShell(const UnicodeString Path, const UnicodeString Params,
  362. HANDLE & Handle)
  363. {
  364. return DoExecuteShell(Path, Params, false, &Handle);
  365. }
  366. //---------------------------------------------------------------------------
  367. void __fastcall ExecuteShellCheckedAndWait(const UnicodeString Command,
  368. TProcessMessagesEvent ProcessMessages)
  369. {
  370. UnicodeString Program, Params, Dir;
  371. SplitCommand(Command, Program, Params, Dir);
  372. HANDLE ProcessHandle;
  373. bool Result = DoExecuteShell(Program, Params, false, &ProcessHandle);
  374. if (!Result)
  375. {
  376. throw EOSExtException(FMTLOAD(EXECUTE_APP_ERROR, (Program)));
  377. }
  378. else
  379. {
  380. if (ProcessHandle != NULL) // only if command was copied to clipboard only
  381. {
  382. if (ProcessMessages != NULL)
  383. {
  384. unsigned long WaitResult;
  385. do
  386. {
  387. // Same as in ExecuteProcessAndReadOutput
  388. WaitResult = WaitForSingleObject(ProcessHandle, 50);
  389. if (WaitResult == WAIT_FAILED)
  390. {
  391. throw Exception(LoadStr(DOCUMENT_WAIT_ERROR));
  392. }
  393. ProcessMessages();
  394. }
  395. while (WaitResult == WAIT_TIMEOUT);
  396. }
  397. else
  398. {
  399. WaitForSingleObject(ProcessHandle, INFINITE);
  400. }
  401. }
  402. }
  403. }
  404. //---------------------------------------------------------------------------
  405. bool __fastcall SpecialFolderLocation(int PathID, UnicodeString & Path)
  406. {
  407. LPITEMIDLIST Pidl;
  408. wchar_t Buf[256];
  409. if (SHGetSpecialFolderLocation(NULL, PathID, &Pidl) == NO_ERROR &&
  410. SHGetPathFromIDList(Pidl, Buf))
  411. {
  412. Path = UnicodeString(Buf);
  413. return true;
  414. }
  415. return false;
  416. }
  417. //---------------------------------------------------------------------------
  418. UnicodeString __fastcall UniqTempDir(const UnicodeString BaseDir, const UnicodeString Identity,
  419. bool Mask)
  420. {
  421. DebugAssert(!BaseDir.IsEmpty());
  422. UnicodeString TempDir;
  423. do
  424. {
  425. TempDir = IncludeTrailingBackslash(BaseDir) + Identity;
  426. if (Mask)
  427. {
  428. TempDir += L"?????";
  429. }
  430. else
  431. {
  432. TempDir += IncludeTrailingBackslash(FormatDateTime(L"nnzzz", Now()));
  433. }
  434. }
  435. while (!Mask && DirectoryExists(ApiPath(TempDir)));
  436. return TempDir;
  437. }
  438. //---------------------------------------------------------------------------
  439. bool __fastcall DeleteDirectory(const UnicodeString DirName)
  440. {
  441. TSearchRecOwned sr;
  442. bool retval = true;
  443. if (FindFirstUnchecked(DirName + L"\\*", faAnyFile, sr) == 0) // VCL Function
  444. {
  445. if (sr.IsDirectory())
  446. {
  447. if (sr.IsRealFile())
  448. retval = DeleteDirectory(DirName + L"\\" + sr.Name);
  449. }
  450. else
  451. {
  452. retval = DeleteFile(ApiPath(DirName + L"\\" + sr.Name));
  453. }
  454. if (retval)
  455. {
  456. while (FindNextChecked(sr) == 0)
  457. { // VCL Function
  458. if (sr.IsDirectory())
  459. {
  460. if (sr.IsRealFile())
  461. retval = DeleteDirectory(DirName + L"\\" + sr.Name);
  462. }
  463. else
  464. {
  465. retval = DeleteFile(ApiPath(DirName + L"\\" + sr.Name));
  466. }
  467. if (!retval) break;
  468. }
  469. }
  470. }
  471. sr.Close();
  472. if (retval) retval = RemoveDir(ApiPath(DirName)); // VCL function
  473. return retval;
  474. }
  475. //---------------------------------------------------------------------------
  476. class TSessionColors : public TComponent
  477. {
  478. public:
  479. __fastcall TSessionColors(TComponent * Owner) : TComponent(Owner)
  480. {
  481. Name = QualifiedClassName();
  482. }
  483. static TSessionColors * __fastcall Retrieve(TComponent * Component)
  484. {
  485. TSessionColors * SessionColors = dynamic_cast<TSessionColors *>(Component->FindComponent(QualifiedClassName()));
  486. if (SessionColors == NULL)
  487. {
  488. SessionColors = new TSessionColors(Component);
  489. }
  490. return SessionColors;
  491. }
  492. typedef std::map<TColor, int> TColorMap;
  493. TColorMap ColorMap;
  494. };
  495. //---------------------------------------------------------------------------
  496. int __fastcall GetSessionColorImage(
  497. TCustomImageList * ImageList, TColor Color, int MaskIndex)
  498. {
  499. TSessionColors * SessionColors = TSessionColors::Retrieve(ImageList);
  500. int Result;
  501. TSessionColors::TColorMap::const_iterator I = SessionColors->ColorMap.find(Color);
  502. if (I != SessionColors->ColorMap.end())
  503. {
  504. Result = I->second;
  505. }
  506. else
  507. {
  508. // This overly complex drawing is here to support color button on SiteAdvanced
  509. // dialog. There we use plain TImageList, instead of TPngImageList,
  510. // TButton does not work with transparent images
  511. // (not even TBitmap with Transparent = true)
  512. std::unique_ptr<TBitmap> MaskBitmap(new TBitmap());
  513. ImageList->GetBitmap(MaskIndex, MaskBitmap.get());
  514. std::unique_ptr<TPngImage> MaskImage(new TPngImage());
  515. MaskImage->Assign(MaskBitmap.get());
  516. std::unique_ptr<TPngImage> ColorImage(new TPngImage(COLOR_RGB, 16, ImageList->Width, ImageList->Height));
  517. TColor MaskTransparentColor = MaskImage->Pixels[0][0];
  518. TColor TransparentColor = MaskTransparentColor;
  519. // Expecting that the color to be replaced is in the centre of the image (HACK)
  520. TColor MaskColor = MaskImage->Pixels[ImageList->Width / 2][ImageList->Height / 2];
  521. for (int Y = 0; Y < ImageList->Height; Y++)
  522. {
  523. for (int X = 0; X < ImageList->Width; X++)
  524. {
  525. TColor SourceColor = MaskImage->Pixels[X][Y];
  526. TColor DestColor;
  527. // this branch is pointless as long as MaskTransparentColor and
  528. // TransparentColor are the same
  529. if (SourceColor == MaskTransparentColor)
  530. {
  531. DestColor = TransparentColor;
  532. }
  533. else if (SourceColor == MaskColor)
  534. {
  535. DestColor = Color;
  536. }
  537. else
  538. {
  539. DestColor = SourceColor;
  540. }
  541. ColorImage->Pixels[X][Y] = DestColor;
  542. }
  543. }
  544. std::unique_ptr<TBitmap> Bitmap(new TBitmap());
  545. Bitmap->SetSize(ImageList->Width, ImageList->Height);
  546. ColorImage->AssignTo(Bitmap.get());
  547. Result = ImageList->AddMasked(Bitmap.get(), TransparentColor);
  548. SessionColors->ColorMap.insert(std::make_pair(Color, Result));
  549. }
  550. return Result;
  551. }
  552. //---------------------------------------------------------------------------
  553. void __fastcall RegenerateSessionColorsImageList(TCustomImageList * ImageList, int MaskIndex)
  554. {
  555. TSessionColors * SessionColors = TSessionColors::Retrieve(ImageList);
  556. std::vector<TColor> Colors;
  557. size_t FixedImages = static_cast<size_t>(ImageList->Count);
  558. Colors.resize(FixedImages + SessionColors->ColorMap.size());
  559. TSessionColors::TColorMap::const_iterator I = SessionColors->ColorMap.begin();
  560. while (I != SessionColors->ColorMap.end())
  561. {
  562. DebugAssert(Colors[I->second] == TColor());
  563. Colors[I->second] = I->first;
  564. I++;
  565. }
  566. TSessionColors::TColorMap ColorMap = SessionColors->ColorMap;
  567. SessionColors->ColorMap.clear();
  568. for (size_t Index = 0; Index < Colors.size(); Index++)
  569. {
  570. bool IsFixedImageIndex = (Index < FixedImages);
  571. DebugAssert((Colors[Index] == TColor()) == IsFixedImageIndex);
  572. if (!IsFixedImageIndex)
  573. {
  574. GetSessionColorImage(ImageList, Colors[Index], MaskIndex);
  575. }
  576. }
  577. DebugAssert(SessionColors->ColorMap == ColorMap);
  578. }
  579. //---------------------------------------------------------------------------
  580. void __fastcall SetSubmenu(TTBXCustomItem * Item)
  581. {
  582. class TTBXPublicItem : public TTBXCustomItem
  583. {
  584. public:
  585. __property ItemStyle;
  586. };
  587. TTBXPublicItem * PublicItem = reinterpret_cast<TTBXPublicItem *>(Item);
  588. DebugAssert(PublicItem != NULL);
  589. // See TTBItemViewer.IsPtInButtonPart (called from TTBItemViewer.MouseDown)
  590. PublicItem->ItemStyle = PublicItem->ItemStyle << tbisSubmenu;
  591. }
  592. //---------------------------------------------------------------------------
  593. bool __fastcall IsEligibleForApplyingTabs(
  594. UnicodeString Line, int & TabPos, UnicodeString & Start, UnicodeString & Remaining)
  595. {
  596. bool Result = false;
  597. TabPos = Line.Pos(L"\t");
  598. if (TabPos > 0)
  599. {
  600. Remaining = Line.SubString(TabPos + 1, Line.Length() - TabPos);
  601. // WORKAROUND
  602. // Some translations still use obsolete hack of consecutive tabs to aling the contents.
  603. // Delete these, so that the following check does not fail on this
  604. while (Remaining.SubString(1, 1) == L"\t")
  605. {
  606. Remaining.Delete(1, 1);
  607. }
  608. // We do not have, not support, mutiple tabs on a single line
  609. if (DebugAlwaysTrue(Remaining.Pos(L"\t") == 0))
  610. {
  611. Start = Line.SubString(1, TabPos - 1);
  612. // WORKAROUND
  613. // Previously we padded the string before tab with spaces,
  614. // to aling the contents across multiple lines
  615. Start = Start.TrimRight();
  616. // at least two normal spaces for separation
  617. Start += L" ";
  618. Result = true;
  619. }
  620. }
  621. return Result;
  622. }
  623. //---------------------------------------------------------------------------
  624. static int __fastcall CalculateWidthByLength(UnicodeString Text, void * /*Arg*/)
  625. {
  626. return Text.Length();
  627. }
  628. //---------------------------------------------------------------------------
  629. void __fastcall ApplyTabs(
  630. UnicodeString & Text, wchar_t Padding,
  631. TCalculateWidth CalculateWidth, void * CalculateWidthArg)
  632. {
  633. if (CalculateWidth == NULL)
  634. {
  635. DebugAssert(CalculateWidthArg == NULL);
  636. CalculateWidth = CalculateWidthByLength;
  637. }
  638. std::unique_ptr<TStringList> Lines(TextToStringList(Text));
  639. int MaxWidth = -1;
  640. for (int Index = 0; Index < Lines->Count; Index++)
  641. {
  642. UnicodeString Line = Lines->Strings[Index];
  643. int TabPos;
  644. UnicodeString Start;
  645. UnicodeString Remaining;
  646. if (IsEligibleForApplyingTabs(Line, TabPos, Start, Remaining))
  647. {
  648. int Width = CalculateWidth(Start, CalculateWidthArg);
  649. MaxWidth = Max(MaxWidth, Width);
  650. }
  651. }
  652. // Optimization and also to prevent potential regression for texts without tabs
  653. if (MaxWidth >= 0)
  654. {
  655. for (int Index = 0; Index < Lines->Count; Index++)
  656. {
  657. UnicodeString Line = Lines->Strings[Index];
  658. int TabPos;
  659. UnicodeString Start;
  660. UnicodeString Remaining;
  661. if (IsEligibleForApplyingTabs(Line, TabPos, Start, Remaining))
  662. {
  663. int Width;
  664. int Iterations = 0;
  665. while ((Width = CalculateWidth(Start, CalculateWidthArg)) < MaxWidth)
  666. {
  667. int Wider = CalculateWidth(Start + Padding, CalculateWidthArg);
  668. // If padded string is wider than max width by more pixels
  669. // than non-padded string is shorter than max width
  670. if ((Wider > MaxWidth) && ((Wider - MaxWidth) > (MaxWidth - Width)))
  671. {
  672. break;
  673. }
  674. Start += Padding;
  675. Iterations++;
  676. // In rare case a tab is zero-width with some strange font (like HYLE)
  677. if (Iterations > 100)
  678. {
  679. break;
  680. }
  681. }
  682. Lines->Strings[Index] = Start + Remaining;
  683. }
  684. }
  685. Text = Lines->Text;
  686. // remove trailing newline
  687. Text = Text.TrimRight();
  688. }
  689. }
  690. //---------------------------------------------------------------------------
  691. static void __fastcall DoSelectScaledImageList(TImageList * ImageList)
  692. {
  693. TImageList * MatchingList = NULL;
  694. int MachingPixelsPerInch = 0;
  695. int PixelsPerInch = GetComponentPixelsPerInch(ImageList);
  696. for (int Index = 0; Index < ImageList->Owner->ComponentCount; Index++)
  697. {
  698. TImageList * OtherList = dynamic_cast<TImageList *>(ImageList->Owner->Components[Index]);
  699. if ((OtherList != NULL) &&
  700. (OtherList != ImageList) &&
  701. StartsStr(ImageList->Name, OtherList->Name))
  702. {
  703. UnicodeString OtherListPixelsPerInchStr =
  704. OtherList->Name.SubString(
  705. ImageList->Name.Length() + 1, OtherList->Name.Length() - ImageList->Name.Length());
  706. int OtherListPixelsPerInch = StrToInt(OtherListPixelsPerInchStr);
  707. if ((OtherListPixelsPerInch <= PixelsPerInch) &&
  708. ((MatchingList == NULL) ||
  709. (MachingPixelsPerInch < OtherListPixelsPerInch)))
  710. {
  711. MatchingList = OtherList;
  712. MachingPixelsPerInch = OtherListPixelsPerInch;
  713. }
  714. }
  715. }
  716. if (MatchingList != NULL)
  717. {
  718. UnicodeString ImageListBackupName = ImageList->Name + IntToStr(USER_DEFAULT_SCREEN_DPI);
  719. if (ImageList->Owner->FindComponent(ImageListBackupName) == NULL)
  720. {
  721. TImageList * ImageListBackup;
  722. TPngImageList * PngImageList = dynamic_cast<TPngImageList *>(ImageList);
  723. if (PngImageList != NULL)
  724. {
  725. ImageListBackup = new TPngImageList(ImageList->Owner);
  726. }
  727. else
  728. {
  729. ImageListBackup = new TImageList(ImageList->Owner);
  730. }
  731. ImageListBackup->Name = ImageListBackupName;
  732. ImageList->Owner->InsertComponent(ImageListBackup);
  733. CopyImageList(ImageListBackup, ImageList);
  734. }
  735. CopyImageList(ImageList, MatchingList);
  736. }
  737. }
  738. //---------------------------------------------------------------------------
  739. static void __fastcall ImageListRescale(TComponent * Sender, TObject * /*Token*/)
  740. {
  741. TImageList * ImageList = DebugNotNull(dynamic_cast<TImageList *>(Sender));
  742. DoSelectScaledImageList(ImageList);
  743. }
  744. //---------------------------------------------------------------------------
  745. void __fastcall SelectScaledImageList(TImageList * ImageList)
  746. {
  747. DoSelectScaledImageList(ImageList);
  748. SetRescaleFunction(ImageList, ImageListRescale);
  749. }
  750. //---------------------------------------------------------------------------
  751. void __fastcall CopyImageList(TImageList * TargetList, TImageList * SourceList)
  752. {
  753. // Maybe this is not necessary, once the TPngImageList::Assign was fixed
  754. TPngImageList * PngTargetList = dynamic_cast<TPngImageList *>(TargetList);
  755. TPngImageList * PngSourceList = dynamic_cast<TPngImageList *>(SourceList);
  756. TargetList->Clear();
  757. TargetList->Height = SourceList->Height;
  758. TargetList->Width = SourceList->Width;
  759. if ((PngTargetList != NULL) && (PngSourceList != NULL))
  760. {
  761. // AddImages won't copy the names and we need them for
  762. // LoadDialogImage and TFrameAnimation
  763. PngTargetList->PngImages->Assign(PngSourceList->PngImages);
  764. }
  765. else
  766. {
  767. TargetList->AddImages(SourceList);
  768. }
  769. }
  770. //---------------------------------------------------------------------------
  771. static bool __fastcall DoLoadDialogImage(TImage * Image, const UnicodeString & ImageName)
  772. {
  773. bool Result = false;
  774. if (GlyphsModule != NULL)
  775. {
  776. TPngImageList * DialogImages = GetDialogImages(Image);
  777. int Index;
  778. for (Index = 0; Index < DialogImages->PngImages->Count; Index++)
  779. {
  780. TPngImageCollectionItem * PngItem = DialogImages->PngImages->Items[Index];
  781. if (SameText(PngItem->Name, ImageName))
  782. {
  783. Image->Picture->Assign(PngItem->PngImage);
  784. break;
  785. }
  786. }
  787. DebugAssert(Index < DialogImages->PngImages->Count);
  788. Result = true;
  789. }
  790. // When showing an exception from wWinMain, the images are released already.
  791. // Non-existence of the glyphs module is just a good indication of that.
  792. // We expect errors only.
  793. else if (ImageName == L"Error")
  794. {
  795. Image->Picture->Icon->Handle = LoadIcon(0, IDI_HAND);
  796. }
  797. // For showing an information about trace files
  798. else if (DebugAlwaysTrue(ImageName == L"Information"))
  799. {
  800. Image->Picture->Icon->Handle = LoadIcon(0, IDI_APPLICATION);
  801. }
  802. return Result;
  803. }
  804. //---------------------------------------------------------------------------
  805. class TDialogImageName : public TObject
  806. {
  807. public:
  808. UnicodeString ImageName;
  809. };
  810. //---------------------------------------------------------------------------
  811. static void __fastcall DialogImageRescale(TComponent * Sender, TObject * Token)
  812. {
  813. TImage * Image = DebugNotNull(dynamic_cast<TImage *>(Sender));
  814. TDialogImageName * DialogImageName = DebugNotNull(dynamic_cast<TDialogImageName *>(Token));
  815. DoLoadDialogImage(Image, DialogImageName->ImageName);
  816. }
  817. //---------------------------------------------------------------------------
  818. void __fastcall LoadDialogImage(TImage * Image, const UnicodeString & ImageName)
  819. {
  820. if (DoLoadDialogImage(Image, ImageName))
  821. {
  822. TDialogImageName * DialogImageName = new TDialogImageName();
  823. DialogImageName->ImageName = ImageName;
  824. SetRescaleFunction(Image, DialogImageRescale, DialogImageName, true);
  825. }
  826. }
  827. //---------------------------------------------------------------------------
  828. int __fastcall DialogImageSize(TForm * Form)
  829. {
  830. return ScaleByPixelsPerInch(32, Form);
  831. }
  832. //---------------------------------------------------------------------------
  833. void __fastcall HideComponentsPanel(TForm * Form)
  834. {
  835. TComponent * Component = DebugNotNull(Form->FindComponent(L"ComponentsPanel"));
  836. TPanel * Panel = DebugNotNull(dynamic_cast<TPanel *>(Component));
  837. DebugAssert(Panel->Align == alBottom);
  838. int Offset = Panel->Height;
  839. Panel->Visible = false;
  840. Panel->Height = 0;
  841. Form->Height -= Offset;
  842. for (int Index = 0; Index < Form->ControlCount; Index++)
  843. {
  844. TControl * Control = Form->Controls[Index];
  845. // Shift back bottom-anchored controls
  846. // (needed for toolbar panel on Progress window and buttons on Preferences dialog),
  847. if ((Control->Align == alNone) &&
  848. Control->Anchors.Contains(akBottom) &&
  849. !Control->Anchors.Contains(akTop))
  850. {
  851. Control->Top += Offset;
  852. }
  853. // Resize back all-anchored controls
  854. // (needed for main panel on Preferences dialog),
  855. if (Control->Anchors.Contains(akBottom) &&
  856. Control->Anchors.Contains(akTop))
  857. {
  858. Control->Height += Offset;
  859. }
  860. }
  861. }
  862. //---------------------------------------------------------------------------
  863. class TBrowserViewer : public TWebBrowserEx
  864. {
  865. public:
  866. __fastcall virtual TBrowserViewer(TComponent* AOwner);
  867. void __fastcall AddLinkHandler(
  868. const UnicodeString & Url, TNotifyEvent Handler);
  869. void __fastcall NavigateToUrl(const UnicodeString & Url);
  870. TControl * LoadingPanel;
  871. protected:
  872. DYNAMIC void __fastcall DoContextPopup(const TPoint & MousePos, bool & Handled);
  873. void __fastcall DocumentComplete(
  874. TObject * Sender, const _di_IDispatch Disp, const OleVariant & URL);
  875. void __fastcall BeforeNavigate2(
  876. TObject * Sender, const _di_IDispatch Disp, const OleVariant & URL,
  877. const OleVariant & Flags, const OleVariant & TargetFrameName,
  878. const OleVariant & PostData, const OleVariant & Headers, WordBool & Cancel);
  879. bool FComplete;
  880. std::map<UnicodeString, TNotifyEvent> FHandlers;
  881. };
  882. //---------------------------------------------------------------------------
  883. __fastcall TBrowserViewer::TBrowserViewer(TComponent* AOwner) :
  884. TWebBrowserEx(AOwner)
  885. {
  886. FComplete = false;
  887. OnDocumentComplete = DocumentComplete;
  888. OnBeforeNavigate2 = BeforeNavigate2;
  889. LoadingPanel = NULL;
  890. }
  891. //---------------------------------------------------------------------------
  892. void __fastcall TBrowserViewer::AddLinkHandler(
  893. const UnicodeString & Url, TNotifyEvent Handler)
  894. {
  895. FHandlers.insert(std::make_pair(Url, Handler));
  896. }
  897. //---------------------------------------------------------------------------
  898. void __fastcall TBrowserViewer::DoContextPopup(const TPoint & MousePos, bool & Handled)
  899. {
  900. // suppress built-in context menu
  901. Handled = true;
  902. TWebBrowserEx::DoContextPopup(MousePos, Handled);
  903. }
  904. //---------------------------------------------------------------------------
  905. void __fastcall TBrowserViewer::DocumentComplete(
  906. TObject * /*Sender*/, const _di_IDispatch /*Disp*/, const OleVariant & /*URL*/)
  907. {
  908. SetBrowserDesignModeOff(this);
  909. FComplete = true;
  910. if (LoadingPanel != NULL)
  911. {
  912. LoadingPanel->Visible = false;
  913. }
  914. }
  915. //---------------------------------------------------------------------------
  916. void __fastcall TBrowserViewer::BeforeNavigate2(
  917. TObject * /*Sender*/, const _di_IDispatch /*Disp*/, const OleVariant & AURL,
  918. const OleVariant & /*Flags*/, const OleVariant & /*TargetFrameName*/,
  919. const OleVariant & /*PostData*/, const OleVariant & /*Headers*/, WordBool & Cancel)
  920. {
  921. // If OnDocumentComplete was not called yet, is has to be our initial message URL,
  922. // opened using TWebBrowserEx::Navigate(), allow it.
  923. // Otherwise it's user navigating, block that and open link
  924. // in an external browser, possibly adding campaign parameters on the way.
  925. if (FComplete)
  926. {
  927. Cancel = 1;
  928. UnicodeString URL = AURL;
  929. if (FHandlers.count(URL) > 0)
  930. {
  931. FHandlers[URL](this);
  932. }
  933. else
  934. {
  935. OpenBrowser(URL);
  936. }
  937. }
  938. }
  939. //---------------------------------------------------------------------------
  940. void __fastcall TBrowserViewer::NavigateToUrl(const UnicodeString & Url)
  941. {
  942. FComplete = false;
  943. Navigate(Url.c_str());
  944. }
  945. //---------------------------------------------------------------------------
  946. TPanel * __fastcall CreateLabelPanel(TPanel * Parent, const UnicodeString & Label)
  947. {
  948. TPanel * Result = CreateBlankPanel(Parent);
  949. Result->Parent = Parent;
  950. Result->Align = alClient;
  951. Result->Caption = Label;
  952. return Result;
  953. }
  954. //---------------------------------------------------------------------------
  955. TWebBrowserEx * __fastcall CreateBrowserViewer(TPanel * Parent, const UnicodeString & LoadingLabel)
  956. {
  957. TBrowserViewer * Result = new TBrowserViewer(Parent);
  958. // TWebBrowserEx has its own unrelated Name and Parent properties.
  959. // The name is used in DownloadUpdate().
  960. static_cast<TWinControl *>(Result)->Name = L"BrowserViewer";
  961. static_cast<TWinControl *>(Result)->Parent = Parent;
  962. Result->Align = alClient;
  963. Result->ControlBorder = cbNone;
  964. Result->LoadingPanel = CreateLabelPanel(Parent, LoadingLabel);
  965. return Result;
  966. }
  967. //---------------------------------------------------------------------------
  968. void __fastcall SetBrowserDesignModeOff(TWebBrowserEx * WebBrowser)
  969. {
  970. if (DebugAlwaysTrue(WebBrowser->Document2 != NULL))
  971. {
  972. WebBrowser->Document2->designMode = L"Off";
  973. }
  974. }
  975. //---------------------------------------------------------------------------
  976. void __fastcall AddBrowserLinkHandler(TWebBrowserEx * WebBrowser,
  977. const UnicodeString & Url, TNotifyEvent Handler)
  978. {
  979. TBrowserViewer * BrowserViewer = dynamic_cast<TBrowserViewer *>(WebBrowser);
  980. if (DebugAlwaysTrue(BrowserViewer != NULL))
  981. {
  982. BrowserViewer->AddLinkHandler(Url, Handler);
  983. }
  984. }
  985. //---------------------------------------------------------------------------
  986. void __fastcall NavigateBrowserToUrl(TWebBrowserEx * WebBrowser, const UnicodeString & Url)
  987. {
  988. TBrowserViewer * BrowserViewer = dynamic_cast<TBrowserViewer *>(WebBrowser);
  989. if (DebugAlwaysTrue(BrowserViewer != NULL))
  990. {
  991. BrowserViewer->NavigateToUrl(Url);
  992. }
  993. }
  994. //---------------------------------------------------------------------------
  995. TComponent * __fastcall FindComponentRecursively(TComponent * Root, const UnicodeString & Name)
  996. {
  997. for (int Index = 0; Index < Root->ComponentCount; Index++)
  998. {
  999. TComponent * Component = Root->Components[Index];
  1000. if (CompareText(Component->Name, Name) == 0)
  1001. {
  1002. return Component;
  1003. }
  1004. Component = FindComponentRecursively(Component, Name);
  1005. if (Component != NULL)
  1006. {
  1007. return Component;
  1008. }
  1009. }
  1010. return NULL;
  1011. }
  1012. //---------------------------------------------------------------------------
  1013. void __fastcall GetInstrutionsTheme(
  1014. TColor & MainInstructionColor, HFONT & MainInstructionFont, HFONT & InstructionFont)
  1015. {
  1016. MainInstructionColor = Graphics::clNone;
  1017. MainInstructionFont = 0;
  1018. InstructionFont = 0;
  1019. HTHEME Theme = OpenThemeData(0, L"TEXTSTYLE");
  1020. if (Theme != NULL)
  1021. {
  1022. LOGFONT AFont;
  1023. COLORREF AColor;
  1024. memset(&AFont, 0, sizeof(AFont));
  1025. // Using Canvas->Handle in the 2nd argument we can get scaled font,
  1026. // but at this point the form is sometime not scaled yet (difference is particularly for standalone messages like
  1027. // /UninstallCleanup), so the results are inconsistent.
  1028. if (GetThemeFont(Theme, NULL, TEXT_MAININSTRUCTION, 0, TMT_FONT, &AFont) == S_OK)
  1029. {
  1030. MainInstructionFont = CreateFontIndirect(&AFont);
  1031. }
  1032. if (GetThemeColor(Theme, TEXT_MAININSTRUCTION, 0, TMT_TEXTCOLOR, &AColor) == S_OK)
  1033. {
  1034. MainInstructionColor = (TColor)AColor;
  1035. }
  1036. memset(&AFont, 0, sizeof(AFont));
  1037. if (GetThemeFont(Theme, NULL, TEXT_INSTRUCTION, 0, TMT_FONT, &AFont) == S_OK)
  1038. {
  1039. InstructionFont = CreateFontIndirect(&AFont);
  1040. }
  1041. CloseThemeData(Theme);
  1042. }
  1043. }
  1044. //---------------------------------------------------------------------------
  1045. TLocalCustomCommand::TLocalCustomCommand()
  1046. {
  1047. }
  1048. //---------------------------------------------------------------------------
  1049. TLocalCustomCommand::TLocalCustomCommand(
  1050. const TCustomCommandData & Data, const UnicodeString & RemotePath, const UnicodeString & LocalPath) :
  1051. TFileCustomCommand(Data, RemotePath)
  1052. {
  1053. FLocalPath = LocalPath;
  1054. }
  1055. //---------------------------------------------------------------------------
  1056. TLocalCustomCommand::TLocalCustomCommand(const TCustomCommandData & Data,
  1057. const UnicodeString & RemotePath, const UnicodeString & LocalPath, const UnicodeString & FileName,
  1058. const UnicodeString & LocalFileName, const UnicodeString & FileList) :
  1059. TFileCustomCommand(Data, RemotePath, FileName, FileList)
  1060. {
  1061. FLocalPath = LocalPath;
  1062. FLocalFileName = LocalFileName;
  1063. }
  1064. //---------------------------------------------------------------------------
  1065. int __fastcall TLocalCustomCommand::PatternLen(const UnicodeString & Command, int Index)
  1066. {
  1067. int Len;
  1068. if ((Index < Command.Length()) && (Command[Index + 1] == L'\\'))
  1069. {
  1070. Len = 2;
  1071. }
  1072. else if ((Index < Command.Length()) && (Command[Index + 1] == L'^'))
  1073. {
  1074. Len = 3;
  1075. }
  1076. else
  1077. {
  1078. Len = TFileCustomCommand::PatternLen(Command, Index);
  1079. }
  1080. return Len;
  1081. }
  1082. //---------------------------------------------------------------------------
  1083. bool __fastcall TLocalCustomCommand::PatternReplacement(
  1084. int Index, const UnicodeString & Pattern, UnicodeString & Replacement, bool & Delimit)
  1085. {
  1086. bool Result;
  1087. if (Pattern == L"!\\")
  1088. {
  1089. // When used as "!\" in an argument to PowerShell, the trailing \ would escpae the ",
  1090. // so we exclude it
  1091. Replacement = ExcludeTrailingBackslash(FLocalPath);
  1092. Result = true;
  1093. }
  1094. else if (Pattern == L"!^!")
  1095. {
  1096. Replacement = FLocalFileName;
  1097. Result = true;
  1098. }
  1099. else
  1100. {
  1101. Result = TFileCustomCommand::PatternReplacement(Index, Pattern, Replacement, Delimit);
  1102. }
  1103. return Result;
  1104. }
  1105. //---------------------------------------------------------------------------
  1106. void __fastcall TLocalCustomCommand::DelimitReplacement(
  1107. UnicodeString & /*Replacement*/, wchar_t /*Quote*/)
  1108. {
  1109. // never delimit local commands
  1110. }
  1111. //---------------------------------------------------------------------------
  1112. bool __fastcall TLocalCustomCommand::HasLocalFileName(const UnicodeString & Command)
  1113. {
  1114. return FindPattern(Command, L'^');
  1115. }
  1116. //---------------------------------------------------------------------------
  1117. bool __fastcall TLocalCustomCommand::IsFileCommand(const UnicodeString & Command)
  1118. {
  1119. return TFileCustomCommand::IsFileCommand(Command) || HasLocalFileName(Command);
  1120. }
  1121. //---------------------------------------------------------------------------
  1122. //---------------------------------------------------------------------------
  1123. typedef std::set<TDataModule *> TImagesModules;
  1124. static TImagesModules ImagesModules;
  1125. static std::map<int, TPngImageList *> AnimationsImages;
  1126. static std::map<int, TImageList *> ButtonImages;
  1127. static std::map<int, TPngImageList *> DialogImages;
  1128. //---------------------------------------------------------------------------
  1129. int __fastcall NormalizePixelsPerInch(int PixelsPerInch)
  1130. {
  1131. if (PixelsPerInch >= 192)
  1132. {
  1133. PixelsPerInch = 192;
  1134. }
  1135. else if (PixelsPerInch >= 144)
  1136. {
  1137. PixelsPerInch = 144;
  1138. }
  1139. else if (PixelsPerInch >= 120)
  1140. {
  1141. PixelsPerInch = 120;
  1142. }
  1143. else
  1144. {
  1145. PixelsPerInch = 96;
  1146. }
  1147. return PixelsPerInch;
  1148. }
  1149. //---------------------------------------------------------------------------
  1150. static int __fastcall NeedImagesModule(TControl * Control)
  1151. {
  1152. int PixelsPerInch = NormalizePixelsPerInch(GetControlPixelsPerInch(Control));
  1153. if (AnimationsImages.find(PixelsPerInch) == AnimationsImages.end())
  1154. {
  1155. TDataModule * ImagesModule;
  1156. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  1157. try
  1158. {
  1159. if (PixelsPerInch == 192)
  1160. {
  1161. ImagesModule = new TAnimations192Module(Application);
  1162. }
  1163. else if (PixelsPerInch == 144)
  1164. {
  1165. ImagesModule = new TAnimations144Module(Application);
  1166. }
  1167. else if (PixelsPerInch == 120)
  1168. {
  1169. ImagesModule = new TAnimations120Module(Application);
  1170. }
  1171. else
  1172. {
  1173. DebugAssert(PixelsPerInch == 96);
  1174. ImagesModule = new TAnimations96Module(Application);
  1175. }
  1176. ImagesModules.insert(ImagesModule);
  1177. TPngImageList * AAnimationImages =
  1178. DebugNotNull(dynamic_cast<TPngImageList *>(ImagesModule->FindComponent(L"AnimationImages")));
  1179. AnimationsImages.insert(std::make_pair(PixelsPerInch, AAnimationImages));
  1180. TImageList * AButtonImages =
  1181. DebugNotNull(dynamic_cast<TImageList *>(ImagesModule->FindComponent(L"ButtonImages")));
  1182. ButtonImages.insert(std::make_pair(PixelsPerInch, AButtonImages));
  1183. TPngImageList * ADialogImages =
  1184. DebugNotNull(dynamic_cast<TPngImageList *>(ImagesModule->FindComponent(L"DialogImages")));
  1185. DialogImages.insert(std::make_pair(PixelsPerInch, ADialogImages));
  1186. }
  1187. __finally
  1188. {
  1189. GUIConfiguration->ChangeResourceModule(ResourceModule);
  1190. }
  1191. }
  1192. return PixelsPerInch;
  1193. }
  1194. //---------------------------------------------------------------------------
  1195. TPngImageList * __fastcall GetAnimationsImages(TControl * Control)
  1196. {
  1197. int PixelsPerInch = NeedImagesModule(Control);
  1198. return DebugNotNull(AnimationsImages[PixelsPerInch]);
  1199. }
  1200. //---------------------------------------------------------------------------
  1201. TImageList * __fastcall GetButtonImages(TControl * Control)
  1202. {
  1203. int PixelsPerInch = NeedImagesModule(Control);
  1204. return DebugNotNull(ButtonImages[PixelsPerInch]);
  1205. }
  1206. //---------------------------------------------------------------------------
  1207. TPngImageList * __fastcall GetDialogImages(TControl * Control)
  1208. {
  1209. int PixelsPerInch = NeedImagesModule(Control);
  1210. return DebugNotNull(DialogImages[PixelsPerInch]);
  1211. }
  1212. //---------------------------------------------------------------------------
  1213. void __fastcall ReleaseImagesModules()
  1214. {
  1215. TImagesModules::iterator i = ImagesModules.begin();
  1216. while (i != ImagesModules.end())
  1217. {
  1218. delete (*i);
  1219. i++;
  1220. }
  1221. ImagesModules.clear();
  1222. }
  1223. //---------------------------------------------------------------------------
  1224. __fastcall TFrameAnimation::TFrameAnimation()
  1225. {
  1226. FFirstFrame = -1;
  1227. }
  1228. //---------------------------------------------------------------------------
  1229. void __fastcall TFrameAnimation::Init(TPaintBox * PaintBox, const UnicodeString & Name)
  1230. {
  1231. FName = Name;
  1232. FPaintBox = PaintBox;
  1233. FPaintBox->ControlStyle = FPaintBox->ControlStyle << csOpaque;
  1234. DebugAssert((FPaintBox->OnPaint == NULL) || (FPaintBox->OnPaint == PaintBoxPaint));
  1235. FPaintBox->OnPaint = PaintBoxPaint;
  1236. SetRescaleFunction(FPaintBox, PaintBoxRescale, reinterpret_cast<TObject *>(this));
  1237. DoInit();
  1238. }
  1239. //---------------------------------------------------------------------------
  1240. void __fastcall TFrameAnimation::DoInit()
  1241. {
  1242. FImageList = GetAnimationsImages(FPaintBox);
  1243. FFirstFrame = -1;
  1244. FFirstLoopFrame = -1;
  1245. FPaintBox->Width = FImageList->Width;
  1246. FPaintBox->Height = FImageList->Height;
  1247. if (!FName.IsEmpty())
  1248. {
  1249. int Frame = 0;
  1250. while (Frame < FImageList->PngImages->Count)
  1251. {
  1252. UnicodeString FrameData = FImageList->PngImages->Items[Frame]->Name;
  1253. UnicodeString FrameName;
  1254. FrameName = CutToChar(FrameData, L'_', false);
  1255. if (SameText(FName, FrameName))
  1256. {
  1257. int FrameIndex = StrToInt(CutToChar(FrameData, L'_', false));
  1258. if (FFirstFrame < 0)
  1259. {
  1260. FFirstFrame = Frame;
  1261. }
  1262. if ((FFirstLoopFrame < 0) && (FrameIndex > 0))
  1263. {
  1264. FFirstLoopFrame = Frame;
  1265. }
  1266. FLastFrame = Frame;
  1267. }
  1268. else
  1269. {
  1270. if (FFirstFrame >= 0)
  1271. {
  1272. // optimization
  1273. break;
  1274. }
  1275. }
  1276. Frame++;
  1277. }
  1278. DebugAssert(FFirstFrame >= 0);
  1279. DebugAssert(FFirstLoopFrame >= 0);
  1280. }
  1281. Stop();
  1282. }
  1283. //---------------------------------------------------------------------------
  1284. void __fastcall TFrameAnimation::PaintBoxRescale(TComponent * /*Sender*/, TObject * Token)
  1285. {
  1286. TFrameAnimation * FrameAnimation = reinterpret_cast<TFrameAnimation *>(Token);
  1287. FrameAnimation->Rescale();
  1288. }
  1289. //---------------------------------------------------------------------------
  1290. void __fastcall TFrameAnimation::Rescale()
  1291. {
  1292. bool Started = (FTimer != NULL) && FTimer->Enabled;
  1293. DoInit();
  1294. if (Started)
  1295. {
  1296. Start();
  1297. }
  1298. }
  1299. //---------------------------------------------------------------------------
  1300. void __fastcall TFrameAnimation::Start()
  1301. {
  1302. if (FFirstFrame >= 0)
  1303. {
  1304. FNextFrameTick = GetTickCount();
  1305. CalculateNextFrameTick();
  1306. if (FTimer == NULL)
  1307. {
  1308. FTimer = new TTimer(GetParentForm(FPaintBox));
  1309. FTimer->Interval = static_cast<int>(GUIUpdateInterval);
  1310. FTimer->OnTimer = Timer;
  1311. }
  1312. else
  1313. {
  1314. // reset timer
  1315. FTimer->Enabled = false;
  1316. FTimer->Enabled = true;
  1317. }
  1318. }
  1319. }
  1320. //---------------------------------------------------------------------------
  1321. void __fastcall TFrameAnimation::Timer(TObject * /*Sender*/)
  1322. {
  1323. Animate();
  1324. }
  1325. //---------------------------------------------------------------------------
  1326. void __fastcall TFrameAnimation::PaintBoxPaint(TObject * Sender)
  1327. {
  1328. if (FFirstFrame >= 0)
  1329. {
  1330. // Double-buffered drawing to prevent flicker (as the images are transparent)
  1331. DebugUsedParam(Sender);
  1332. DebugAssert(FPaintBox == Sender);
  1333. DebugAssert(FPaintBox->ControlStyle.Contains(csOpaque));
  1334. std::unique_ptr<TBitmap> Bitmap(new TBitmap());
  1335. Bitmap->SetSize(FPaintBox->Width, FPaintBox->Height);
  1336. Bitmap->Canvas->Brush->Color = FPaintBox->Color;
  1337. TRect Rect(0, 0, Bitmap->Width, Bitmap->Height);
  1338. Bitmap->Canvas->FillRect(Rect);
  1339. TGraphic * Graphic = GetCurrentImage()->PngImage;
  1340. DebugAssert(Graphic->Width == FPaintBox->Width);
  1341. DebugAssert(Graphic->Height == FPaintBox->Height);
  1342. Bitmap->Canvas->Draw(0, 0, Graphic);
  1343. FPaintBox->Canvas->Draw(0, 0, Bitmap.get());
  1344. }
  1345. FPainted = true;
  1346. }
  1347. //---------------------------------------------------------------------------
  1348. void __fastcall TFrameAnimation::Repaint()
  1349. {
  1350. FPainted = false;
  1351. // If the form is not showing yet, the Paint() is not even called
  1352. FPaintBox->Repaint();
  1353. if (!FPainted)
  1354. {
  1355. // Paint later, alternativelly we may keep trying Repaint() in Animate().
  1356. // See also a hack in TAuthenticateForm::Log.
  1357. FPaintBox->Invalidate();
  1358. }
  1359. }
  1360. //---------------------------------------------------------------------------
  1361. void __fastcall TFrameAnimation::Stop()
  1362. {
  1363. FNextFrameTick = std::numeric_limits<DWORD>::max();
  1364. FCurrentFrame = FFirstFrame;
  1365. Repaint();
  1366. if (FTimer != NULL)
  1367. {
  1368. FTimer->Enabled = false;
  1369. }
  1370. }
  1371. //---------------------------------------------------------------------------
  1372. void __fastcall TFrameAnimation::Animate()
  1373. {
  1374. if (FFirstFrame >= 0)
  1375. {
  1376. // UPGRADE: Use GetTickCount64() when we stop supporting Windows XP.
  1377. DWORD TickCount = GetTickCount();
  1378. // Keep in sync with an opposite condition at the end of the loop.
  1379. // We may skip some frames if we got stalled for a while
  1380. while (TickCount >= FNextFrameTick)
  1381. {
  1382. if (FCurrentFrame >= FLastFrame)
  1383. {
  1384. FCurrentFrame = FFirstLoopFrame;
  1385. }
  1386. else
  1387. {
  1388. FCurrentFrame++;
  1389. }
  1390. CalculateNextFrameTick();
  1391. Repaint();
  1392. }
  1393. }
  1394. }
  1395. //---------------------------------------------------------------------------
  1396. TPngImageCollectionItem * __fastcall TFrameAnimation::GetCurrentImage()
  1397. {
  1398. return FImageList->PngImages->Items[FCurrentFrame];
  1399. }
  1400. //---------------------------------------------------------------------------
  1401. void __fastcall TFrameAnimation::CalculateNextFrameTick()
  1402. {
  1403. TPngImageCollectionItem * ImageItem = GetCurrentImage();
  1404. UnicodeString Duration = ImageItem->Name;
  1405. CutToChar(Duration, L'_', false);
  1406. // skip index (is not really used)
  1407. CutToChar(Duration, L'_', false);
  1408. // This should overflow, when tick count wraps.
  1409. FNextFrameTick += StrToInt(Duration) * 10;
  1410. }
  1411. //---------------------------------------------------------------------------
  1412. //---------------------------------------------------------------------------
  1413. // Hints use:
  1414. // - Cleanup list tooltip (multi line)
  1415. // - Combo edit button
  1416. // - Transfer settings label (multi line, follows label size and font)
  1417. // - HintLabel (hint and persistent hint, multipline)
  1418. // - status bar hints
  1419. //---------------------------------------------------------------------------
  1420. __fastcall TScreenTipHintWindow::TScreenTipHintWindow(TComponent * Owner) :
  1421. THintWindow(Owner)
  1422. {
  1423. FParentPainting = false;
  1424. }
  1425. //---------------------------------------------------------------------------
  1426. int __fastcall TScreenTipHintWindow::GetTextFlags(TControl * Control)
  1427. {
  1428. return DT_LEFT | DT_WORDBREAK | DT_NOPREFIX | Control->DrawTextBiDiModeFlagsReadingOnly();
  1429. }
  1430. //---------------------------------------------------------------------------
  1431. bool __fastcall TScreenTipHintWindow::UseBoldShortHint(TControl * HintControl)
  1432. {
  1433. return
  1434. (dynamic_cast<TTBCustomDockableWindow *>(HintControl) != NULL) ||
  1435. (dynamic_cast<TTBPopupWindow *>(HintControl) != NULL);
  1436. }
  1437. //---------------------------------------------------------------------------
  1438. bool __fastcall TScreenTipHintWindow::IsPathLabel(TControl * HintControl)
  1439. {
  1440. return (dynamic_cast<TPathLabel *>(HintControl) != NULL);
  1441. }
  1442. //---------------------------------------------------------------------------
  1443. int __fastcall TScreenTipHintWindow::GetMargin(TControl * HintControl, const UnicodeString & Hint)
  1444. {
  1445. int Result;
  1446. if (HasLabelHintPopup(HintControl, Hint) || IsPathLabel(HintControl))
  1447. {
  1448. Result = 3;
  1449. }
  1450. else
  1451. {
  1452. Result = 6;
  1453. }
  1454. Result = ScaleByTextHeight(HintControl, Result);
  1455. return Result;
  1456. }
  1457. //---------------------------------------------------------------------------
  1458. TFont * __fastcall TScreenTipHintWindow::GetFont(TControl * HintControl, const UnicodeString & Hint)
  1459. {
  1460. TFont * Result;
  1461. if (HasLabelHintPopup(HintControl, Hint) || IsPathLabel(HintControl))
  1462. {
  1463. Result = reinterpret_cast<TLabel *>(dynamic_cast<TCustomLabel *>(HintControl))->Font;
  1464. }
  1465. else
  1466. {
  1467. FScaledHintFont.reset(new TFont());
  1468. FScaledHintFont->Assign(Screen->HintFont);
  1469. FScaledHintFont->Size = ScaleByPixelsPerInchFromSystem(FScaledHintFont->Size, HintControl);
  1470. Result = FScaledHintFont.get();
  1471. }
  1472. return Result;
  1473. }
  1474. //---------------------------------------------------------------------------
  1475. void __fastcall TScreenTipHintWindow::CalcHintTextRect(TControl * Control, TCanvas * Canvas, TRect & Rect, const UnicodeString & Hint)
  1476. {
  1477. const int Flags = DT_CALCRECT | GetTextFlags(Control);
  1478. DrawText(Canvas->Handle, Hint.c_str(), -1, &Rect, Flags);
  1479. }
  1480. //---------------------------------------------------------------------------
  1481. TRect __fastcall TScreenTipHintWindow::CalcHintRect(int MaxWidth, const UnicodeString AHint, void * AData)
  1482. {
  1483. TControl * HintControl = GetHintControl(AData);
  1484. int Margin = GetMargin(HintControl, AHint);
  1485. UnicodeString ShortHint;
  1486. UnicodeString LongHint;
  1487. SplitHint(HintControl, AHint, ShortHint, LongHint);
  1488. Canvas->Font->Assign(GetFont(HintControl, AHint));
  1489. const int ScreenTipTextOnlyWidth = ScaleByTextHeight(HintControl, cScreenTipTextOnlyWidth);
  1490. if (!LongHint.IsEmpty())
  1491. {
  1492. // double-margin on the right
  1493. MaxWidth = ScreenTipTextOnlyWidth - (3 * Margin);
  1494. }
  1495. // Multi line short hints can be twice as wide, to not break the individual lines unless really necessary.
  1496. // (login site tree, clean up dialog list, preferences custom command list, persistent hint, etc).
  1497. // And they also can be twice as wide, to not break the individual lines unless really necessary.
  1498. if (ShortHint.Pos(L"\n") > 0)
  1499. {
  1500. MaxWidth *= 2;
  1501. }
  1502. bool HintPopup = HasLabelHintPopup(HintControl, AHint);
  1503. if (HintPopup)
  1504. {
  1505. MaxWidth = HintControl->Width;
  1506. }
  1507. if (UseBoldShortHint(HintControl))
  1508. {
  1509. Canvas->Font->Style = TFontStyles() << fsBold;
  1510. }
  1511. TRect ShortRect(0, 0, MaxWidth, 0);
  1512. CalcHintTextRect(this, Canvas, ShortRect, ShortHint);
  1513. Canvas->Font->Style = TFontStyles();
  1514. TRect Result;
  1515. if (LongHint.IsEmpty())
  1516. {
  1517. Result = ShortRect;
  1518. if (HintPopup)
  1519. {
  1520. Result.Right = MaxWidth + 2 * Margin;
  1521. }
  1522. else
  1523. {
  1524. Result.Right += 3 * Margin;
  1525. }
  1526. Result.Bottom += 2 * Margin;
  1527. }
  1528. else
  1529. {
  1530. const int LongIndentation = Margin * 3 / 2;
  1531. TRect LongRect(0, 0, MaxWidth - LongIndentation, 0);
  1532. CalcHintTextRect(this, Canvas, LongRect, LongHint);
  1533. Result.Right = ScreenTipTextOnlyWidth;
  1534. Result.Bottom = Margin + ShortRect.Height() + (Margin / 3 * 5) + LongRect.Height() + Margin;
  1535. }
  1536. // VCLCOPY: To counter the increase in THintWindow::ActivateHintData
  1537. Result.Bottom -= 4;
  1538. return Result;
  1539. }
  1540. //---------------------------------------------------------------------------
  1541. void __fastcall TScreenTipHintWindow::SplitHint(
  1542. TControl * HintControl, const UnicodeString & Hint, UnicodeString & ShortHint, UnicodeString & LongHint)
  1543. {
  1544. if (HasLabelHintPopup(HintControl, Hint))
  1545. {
  1546. ShortHint = HintControl->Hint;
  1547. }
  1548. else
  1549. {
  1550. ShortHint = GetShortHint(Hint);
  1551. LongHint = GetLongHintIfAny(Hint);
  1552. }
  1553. }
  1554. //---------------------------------------------------------------------------
  1555. void __fastcall TScreenTipHintWindow::ActivateHintData(const TRect & ARect, const UnicodeString AHint, void * AData)
  1556. {
  1557. FHintControl = GetHintControl(AData);
  1558. SplitHint(FHintControl, AHint, FShortHint, FLongHint);
  1559. FMargin = GetMargin(FHintControl, AHint);
  1560. FHintPopup = HasLabelHintPopup(FHintControl, AHint);
  1561. Canvas->Font->Assign(GetFont(FHintControl, AHint));
  1562. TRect Rect = ARect;
  1563. if (FHintPopup)
  1564. {
  1565. Rect.SetLocation(FHintControl->ClientToScreen(TPoint(-FMargin, -FMargin)));
  1566. }
  1567. if (IsPathLabel(FHintControl))
  1568. {
  1569. Rect.Offset(-FMargin, -FMargin);
  1570. }
  1571. THintWindow::ActivateHintData(Rect, FShortHint, AData);
  1572. }
  1573. //---------------------------------------------------------------------------
  1574. TControl * __fastcall TScreenTipHintWindow::GetHintControl(void * Data)
  1575. {
  1576. return reinterpret_cast<TControl *>(DebugNotNull(Data));
  1577. }
  1578. //---------------------------------------------------------------------------
  1579. UnicodeString __fastcall TScreenTipHintWindow::GetLongHintIfAny(const UnicodeString & AHint)
  1580. {
  1581. UnicodeString Result;
  1582. int P = Pos(L"|", AHint);
  1583. if (P > 0)
  1584. {
  1585. Result = GetLongHint(AHint);
  1586. }
  1587. return Result;
  1588. }
  1589. //---------------------------------------------------------------------------
  1590. void __fastcall TScreenTipHintWindow::Dispatch(void * AMessage)
  1591. {
  1592. TMessage * Message = static_cast<TMessage*>(AMessage);
  1593. switch (Message->Msg)
  1594. {
  1595. case WM_GETTEXTLENGTH:
  1596. if (FParentPainting)
  1597. {
  1598. // make THintWindow::Paint() not paint the Caption
  1599. Message->Result = 0;
  1600. }
  1601. else
  1602. {
  1603. THintWindow::Dispatch(AMessage);
  1604. }
  1605. break;
  1606. default:
  1607. THintWindow::Dispatch(AMessage);
  1608. break;
  1609. }
  1610. }
  1611. //---------------------------------------------------------------------------
  1612. void __fastcall TScreenTipHintWindow::Paint()
  1613. {
  1614. // paint frame/background
  1615. {
  1616. TAutoFlag ParentPaintingFlag(FParentPainting);
  1617. THintWindow::Paint();
  1618. }
  1619. const int Flags = GetTextFlags(this);
  1620. const int Margin = FMargin - 1; // 1 = border
  1621. TRect Rect = ClientRect;
  1622. Rect.Inflate(-Margin, -Margin);
  1623. if (!FHintPopup)
  1624. {
  1625. Rect.Right -= FMargin;
  1626. }
  1627. if (UseBoldShortHint(FHintControl))
  1628. {
  1629. Canvas->Font->Style = TFontStyles() << fsBold;
  1630. }
  1631. DrawText(Canvas->Handle, FShortHint.c_str(), -1, &Rect, Flags);
  1632. TRect ShortRect = Rect;
  1633. DrawText(Canvas->Handle, FShortHint.c_str(), -1, &ShortRect, DT_CALCRECT | Flags);
  1634. Canvas->Font->Style = TFontStyles();
  1635. if (!FLongHint.IsEmpty())
  1636. {
  1637. Rect.Left += FMargin * 3 / 2;
  1638. Rect.Top += ShortRect.Height() + (FMargin / 3 * 5);
  1639. DrawText(Canvas->Handle, FLongHint.c_str(), -1, &Rect, Flags);
  1640. }
  1641. }
  1642. //---------------------------------------------------------------------------
  1643. //---------------------------------------------------------------------------
  1644. __fastcall TNewRichEdit::TNewRichEdit(TComponent * AOwner) :
  1645. TRichEdit(AOwner),
  1646. FLibrary(0)
  1647. {
  1648. }
  1649. //---------------------------------------------------------------------------
  1650. void __fastcall TNewRichEdit::CreateParams(TCreateParams & Params)
  1651. {
  1652. UnicodeString RichEditModuleName(L"MSFTEDIT.DLL");
  1653. long int OldError;
  1654. OldError = SetErrorMode(SEM_NOOPENFILEERRORBOX);
  1655. FLibrary = LoadLibrary(RichEditModuleName.c_str());
  1656. SetErrorMode(OldError);
  1657. // No fallback, MSFTEDIT.DLL is available since Windows XP
  1658. // https://blogs.msdn.microsoft.com/murrays/2006/10/13/richedit-versions/
  1659. if (FLibrary == 0)
  1660. {
  1661. throw Exception(FORMAT(L"Cannot load %s", (RichEditModuleName)));
  1662. }
  1663. TCustomMemo::CreateParams(Params);
  1664. // MSDN says that we should use MSFTEDIT_CLASS to load Rich Edit 4.1:
  1665. // https://docs.microsoft.com/en-us/windows/desktop/controls/about-rich-edit-controls
  1666. // But MSFTEDIT_CLASS is defined as "RICHEDIT50W",
  1667. // so not sure what version we are loading.
  1668. // Seem to work on Windows XP SP3.
  1669. CreateSubClass(Params, MSFTEDIT_CLASS);
  1670. }
  1671. //---------------------------------------------------------------------------
  1672. void __fastcall TNewRichEdit::CreateWnd()
  1673. {
  1674. TRichEdit::CreateWnd();
  1675. SendMessage(Handle, EM_SETEDITSTYLEEX, 0, SES_EX_HANDLEFRIENDLYURL);
  1676. }
  1677. //---------------------------------------------------------------------------
  1678. void __fastcall TNewRichEdit::DestroyWnd()
  1679. {
  1680. TRichEdit::DestroyWnd();
  1681. if (FLibrary != 0)
  1682. {
  1683. FreeLibrary(FLibrary);
  1684. }
  1685. }