GUITools.cpp 59 KB

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