GUITools.cpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  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 <Animations.h>
  21. #include <PasTools.hpp>
  22. #include <VCLCommon.h>
  23. #include <Vcl.ScreenTips.hpp>
  24. //---------------------------------------------------------------------------
  25. #pragma package(smart_init)
  26. //---------------------------------------------------------------------------
  27. extern const UnicodeString PageantTool = L"pageant.exe";
  28. extern const UnicodeString PuttygenTool = L"puttygen.exe";
  29. //---------------------------------------------------------------------------
  30. bool __fastcall FindFile(UnicodeString & Path)
  31. {
  32. bool Result = FileExists(ApiPath(Path));
  33. if (!Result)
  34. {
  35. UnicodeString Paths = GetEnvironmentVariable(L"PATH");
  36. if (!Paths.IsEmpty())
  37. {
  38. UnicodeString NewPath = FileSearch(ExtractFileName(Path), Paths);
  39. Result = !NewPath.IsEmpty();
  40. if (Result)
  41. {
  42. Path = NewPath;
  43. }
  44. }
  45. }
  46. return Result;
  47. }
  48. //---------------------------------------------------------------------------
  49. void __fastcall OpenSessionInPutty(const UnicodeString PuttyPath,
  50. TSessionData * SessionData)
  51. {
  52. UnicodeString Program, AParams, Dir;
  53. SplitCommand(PuttyPath, Program, AParams, Dir);
  54. Program = ExpandEnvironmentVariables(Program);
  55. if (FindFile(Program))
  56. {
  57. AParams = ExpandEnvironmentVariables(AParams);
  58. UnicodeString Password = GUIConfiguration->PuttyPassword ? SessionData->Password : UnicodeString();
  59. TCustomCommandData Data(SessionData, SessionData->UserName, Password);
  60. TRemoteCustomCommand RemoteCustomCommand(Data, SessionData->RemoteDirectory);
  61. TWinInteractiveCustomCommand InteractiveCustomCommand(
  62. &RemoteCustomCommand, L"PuTTY", UnicodeString());
  63. UnicodeString Params =
  64. RemoteCustomCommand.Complete(InteractiveCustomCommand.Complete(AParams, false), true);
  65. UnicodeString PuttyParams;
  66. if (!RemoteCustomCommand.IsSiteCommand(AParams))
  67. {
  68. UnicodeString SessionName;
  69. TRegistryStorage * Storage = NULL;
  70. TSessionData * ExportData = NULL;
  71. TRegistryStorage * SourceStorage = NULL;
  72. try
  73. {
  74. Storage = new TRegistryStorage(Configuration->PuttySessionsKey);
  75. Storage->AccessMode = smReadWrite;
  76. // make it compatible with putty
  77. Storage->MungeStringValues = false;
  78. Storage->ForceAnsi = true;
  79. if (Storage->OpenRootKey(true))
  80. {
  81. if (Storage->KeyExists(SessionData->StorageKey))
  82. {
  83. SessionName = SessionData->SessionName;
  84. }
  85. else
  86. {
  87. SourceStorage = new TRegistryStorage(Configuration->PuttySessionsKey);
  88. SourceStorage->MungeStringValues = false;
  89. SourceStorage->ForceAnsi = true;
  90. if (SourceStorage->OpenSubKey(StoredSessions->DefaultSettings->Name, false) &&
  91. Storage->OpenSubKey(GUIConfiguration->PuttySession, true))
  92. {
  93. Storage->Copy(SourceStorage);
  94. Storage->CloseSubKey();
  95. }
  96. ExportData = new TSessionData(L"");
  97. ExportData->Assign(SessionData);
  98. ExportData->Modified = true;
  99. ExportData->Name = GUIConfiguration->PuttySession;
  100. ExportData->WinTitle = SessionData->SessionName;
  101. ExportData->Password = L"";
  102. if (SessionData->FSProtocol == fsFTP)
  103. {
  104. if (GUIConfiguration->TelnetForFtpInPutty)
  105. {
  106. ExportData->PuttyProtocol = PuttyTelnetProtocol;
  107. ExportData->PortNumber = TelnetPortNumber;
  108. // PuTTY does not allow -pw for telnet
  109. Password = L"";
  110. }
  111. else
  112. {
  113. ExportData->PuttyProtocol = PuttySshProtocol;
  114. ExportData->PortNumber = SshPortNumber;
  115. }
  116. }
  117. ExportData->Save(Storage, true);
  118. SessionName = GUIConfiguration->PuttySession;
  119. }
  120. }
  121. }
  122. __finally
  123. {
  124. delete Storage;
  125. delete ExportData;
  126. delete SourceStorage;
  127. }
  128. UnicodeString LoadSwitch = L"-load";
  129. int P = Params.LowerCase().Pos(LoadSwitch + L" ");
  130. if ((P == 0) || ((P > 1) && (Params[P - 1] != L' ')))
  131. {
  132. AddToList(PuttyParams, FORMAT(L"%s %s", (LoadSwitch, EscapePuttyCommandParam(SessionName))), L" ");
  133. }
  134. }
  135. if (!Password.IsEmpty() && !RemoteCustomCommand.IsPasswordCommand(AParams))
  136. {
  137. AddToList(PuttyParams, FORMAT(L"-pw %s", (EscapePuttyCommandParam(Password))), L" ");
  138. }
  139. AddToList(PuttyParams, Params, L" ");
  140. // PuTTY is started in its binary directory to allow relative paths in private key,
  141. // when opening PuTTY's own stored session.
  142. ExecuteShellChecked(Program, PuttyParams, true);
  143. }
  144. else
  145. {
  146. throw Exception(FMTLOAD(FILE_NOT_FOUND, (Program)));
  147. }
  148. }
  149. //---------------------------------------------------------------------------
  150. bool __fastcall FindTool(const UnicodeString & Name, UnicodeString & Path)
  151. {
  152. UnicodeString AppPath = IncludeTrailingBackslash(ExtractFilePath(Application->ExeName));
  153. Path = AppPath + Name;
  154. bool Result = true;
  155. if (!FileExists(ApiPath(Path)))
  156. {
  157. Path = AppPath + L"PuTTY\\" + Name;
  158. if (!FileExists(ApiPath(Path)))
  159. {
  160. Path = Name;
  161. if (!FindFile(Path))
  162. {
  163. Result = false;
  164. }
  165. }
  166. }
  167. return Result;
  168. }
  169. //---------------------------------------------------------------------------
  170. bool __fastcall CopyCommandToClipboard(const UnicodeString & Command)
  171. {
  172. bool Result = UseAlternativeFunction() && IsKeyPressed(VK_CONTROL);
  173. if (Result)
  174. {
  175. TInstantOperationVisualizer Visualizer;
  176. CopyToClipboard(Command);
  177. }
  178. return Result;
  179. }
  180. //---------------------------------------------------------------------------
  181. static bool __fastcall DoExecuteShell(const UnicodeString Path, const UnicodeString Params,
  182. bool ChangeWorkingDirectory, HANDLE * Handle)
  183. {
  184. bool Result = CopyCommandToClipboard(FormatCommand(Path, Params));
  185. if (!Result)
  186. {
  187. UnicodeString Directory = ExtractFilePath(Path);
  188. TShellExecuteInfoW ExecuteInfo;
  189. memset(&ExecuteInfo, 0, sizeof(ExecuteInfo));
  190. ExecuteInfo.cbSize = sizeof(ExecuteInfo);
  191. ExecuteInfo.fMask =
  192. SEE_MASK_FLAG_NO_UI |
  193. FLAGMASK((Handle != NULL), SEE_MASK_NOCLOSEPROCESS);
  194. ExecuteInfo.hwnd = Application->Handle;
  195. ExecuteInfo.lpFile = (wchar_t*)Path.data();
  196. ExecuteInfo.lpParameters = (wchar_t*)Params.data();
  197. ExecuteInfo.lpDirectory = (ChangeWorkingDirectory ? Directory.c_str() : NULL);
  198. ExecuteInfo.nShow = SW_SHOW;
  199. Result = (ShellExecuteEx(&ExecuteInfo) != 0);
  200. if (Result)
  201. {
  202. if (Handle != NULL)
  203. {
  204. *Handle = ExecuteInfo.hProcess;
  205. }
  206. }
  207. }
  208. return Result;
  209. }
  210. //---------------------------------------------------------------------------
  211. void __fastcall ExecuteShellChecked(const UnicodeString Path, const UnicodeString Params, bool ChangeWorkingDirectory)
  212. {
  213. if (!DoExecuteShell(Path, Params, ChangeWorkingDirectory, NULL))
  214. {
  215. throw EOSExtException(FMTLOAD(EXECUTE_APP_ERROR, (Path)));
  216. }
  217. }
  218. //---------------------------------------------------------------------------
  219. void __fastcall ExecuteShellChecked(const UnicodeString Command)
  220. {
  221. UnicodeString Program, Params, Dir;
  222. SplitCommand(Command, Program, Params, Dir);
  223. ExecuteShellChecked(Program, Params);
  224. }
  225. //---------------------------------------------------------------------------
  226. bool __fastcall ExecuteShell(const UnicodeString Path, const UnicodeString Params,
  227. HANDLE & Handle)
  228. {
  229. return DoExecuteShell(Path, Params, false, &Handle);
  230. }
  231. //---------------------------------------------------------------------------
  232. void __fastcall ExecuteShellCheckedAndWait(const UnicodeString Command,
  233. TProcessMessagesEvent ProcessMessages)
  234. {
  235. UnicodeString Program, Params, Dir;
  236. SplitCommand(Command, Program, Params, Dir);
  237. HANDLE ProcessHandle;
  238. bool Result = DoExecuteShell(Program, Params, false, &ProcessHandle);
  239. if (!Result)
  240. {
  241. throw EOSExtException(FMTLOAD(EXECUTE_APP_ERROR, (Program)));
  242. }
  243. else
  244. {
  245. if (ProcessMessages != NULL)
  246. {
  247. unsigned long WaitResult;
  248. do
  249. {
  250. // Same as in ExecuteProcessAndReadOutput
  251. WaitResult = WaitForSingleObject(ProcessHandle, 200);
  252. if (WaitResult == WAIT_FAILED)
  253. {
  254. throw Exception(LoadStr(DOCUMENT_WAIT_ERROR));
  255. }
  256. ProcessMessages();
  257. }
  258. while (WaitResult == WAIT_TIMEOUT);
  259. }
  260. else
  261. {
  262. WaitForSingleObject(ProcessHandle, INFINITE);
  263. }
  264. }
  265. }
  266. //---------------------------------------------------------------------------
  267. bool __fastcall SpecialFolderLocation(int PathID, UnicodeString & Path)
  268. {
  269. LPITEMIDLIST Pidl;
  270. wchar_t Buf[256];
  271. if (SHGetSpecialFolderLocation(NULL, PathID, &Pidl) == NO_ERROR &&
  272. SHGetPathFromIDList(Pidl, Buf))
  273. {
  274. Path = UnicodeString(Buf);
  275. return true;
  276. }
  277. return false;
  278. }
  279. //---------------------------------------------------------------------------
  280. UnicodeString __fastcall UniqTempDir(const UnicodeString BaseDir, const UnicodeString Identity,
  281. bool Mask)
  282. {
  283. DebugAssert(!BaseDir.IsEmpty());
  284. UnicodeString TempDir;
  285. do
  286. {
  287. TempDir = IncludeTrailingBackslash(BaseDir) + Identity;
  288. if (Mask)
  289. {
  290. TempDir += L"?????";
  291. }
  292. else
  293. {
  294. TempDir += IncludeTrailingBackslash(FormatDateTime(L"nnzzz", Now()));
  295. }
  296. }
  297. while (!Mask && DirectoryExists(ApiPath(TempDir)));
  298. return TempDir;
  299. }
  300. //---------------------------------------------------------------------------
  301. bool __fastcall DeleteDirectory(const UnicodeString DirName)
  302. {
  303. TSearchRecChecked sr;
  304. bool retval = true;
  305. if (FindFirstUnchecked(DirName + L"\\*", faAnyFile, sr) == 0) // VCL Function
  306. {
  307. if (FLAGSET(sr.Attr, faDirectory))
  308. {
  309. if (sr.Name != L"." && sr.Name != L"..")
  310. retval = DeleteDirectory(DirName + L"\\" + sr.Name);
  311. }
  312. else
  313. {
  314. retval = DeleteFile(ApiPath(DirName + L"\\" + sr.Name));
  315. }
  316. if (retval)
  317. {
  318. while (FindNextChecked(sr) == 0)
  319. { // VCL Function
  320. if (FLAGSET(sr.Attr, faDirectory))
  321. {
  322. if (sr.Name != L"." && sr.Name != L"..")
  323. retval = DeleteDirectory(DirName + L"\\" + sr.Name);
  324. }
  325. else
  326. {
  327. retval = DeleteFile(ApiPath(DirName + L"\\" + sr.Name));
  328. }
  329. if (!retval) break;
  330. }
  331. }
  332. }
  333. FindClose(sr);
  334. if (retval) retval = RemoveDir(ApiPath(DirName)); // VCL function
  335. return retval;
  336. }
  337. //---------------------------------------------------------------------------
  338. void __fastcall AddSessionColorImage(
  339. TCustomImageList * ImageList, TColor Color, int MaskIndex)
  340. {
  341. // This overly complex drawing is here to support color button on SiteAdvanced
  342. // dialog. There we use plain TImageList, instead of TPngImageList,
  343. // TButton does not work with transparent images
  344. // (not even TBitmap with Transparent = true)
  345. std::unique_ptr<TBitmap> MaskBitmap(new TBitmap());
  346. ImageList->GetBitmap(MaskIndex, MaskBitmap.get());
  347. std::unique_ptr<TPngImage> MaskImage(new TPngImage());
  348. MaskImage->Assign(MaskBitmap.get());
  349. std::unique_ptr<TPngImage> ColorImage(new TPngImage(COLOR_RGB, 16, ImageList->Width, ImageList->Height));
  350. TColor MaskTransparentColor = MaskImage->Pixels[0][0];
  351. TColor TransparentColor = MaskTransparentColor;
  352. // Expecting that the color to be replaced is in the centre of the image (HACK)
  353. TColor MaskColor = MaskImage->Pixels[ImageList->Width / 2][ImageList->Height / 2];
  354. for (int Y = 0; Y < ImageList->Height; Y++)
  355. {
  356. for (int X = 0; X < ImageList->Width; X++)
  357. {
  358. TColor SourceColor = MaskImage->Pixels[X][Y];
  359. TColor DestColor;
  360. // this branch is pointless as long as MaskTransparentColor and
  361. // TransparentColor are the same
  362. if (SourceColor == MaskTransparentColor)
  363. {
  364. DestColor = TransparentColor;
  365. }
  366. else if (SourceColor == MaskColor)
  367. {
  368. DestColor = Color;
  369. }
  370. else
  371. {
  372. DestColor = SourceColor;
  373. }
  374. ColorImage->Pixels[X][Y] = DestColor;
  375. }
  376. }
  377. std::unique_ptr<TBitmap> Bitmap(new TBitmap());
  378. Bitmap->SetSize(ImageList->Width, ImageList->Height);
  379. ColorImage->AssignTo(Bitmap.get());
  380. ImageList->AddMasked(Bitmap.get(), TransparentColor);
  381. }
  382. //---------------------------------------------------------------------------
  383. void __fastcall SetSubmenu(TTBXCustomItem * Item)
  384. {
  385. class TTBXPublicItem : public TTBXCustomItem
  386. {
  387. public:
  388. __property ItemStyle;
  389. };
  390. TTBXPublicItem * PublicItem = reinterpret_cast<TTBXPublicItem *>(Item);
  391. DebugAssert(PublicItem != NULL);
  392. // See TTBItemViewer.IsPtInButtonPart (called from TTBItemViewer.MouseDown)
  393. PublicItem->ItemStyle = PublicItem->ItemStyle << tbisSubmenu;
  394. }
  395. //---------------------------------------------------------------------------
  396. bool __fastcall IsEligibleForApplyingTabs(
  397. UnicodeString Line, int & TabPos, UnicodeString & Start, UnicodeString & Remaining)
  398. {
  399. bool Result = false;
  400. TabPos = Line.Pos(L"\t");
  401. if (TabPos > 0)
  402. {
  403. Remaining = Line.SubString(TabPos + 1, Line.Length() - TabPos);
  404. // WORKAROUND
  405. // Some translations still use obsolete hack of consecutive tabs to aling the contents.
  406. // Delete these, so that the following check does not fail on this
  407. while (Remaining.SubString(1, 1) == L"\t")
  408. {
  409. Remaining.Delete(1, 1);
  410. }
  411. // We do not have, not support, mutiple tabs on a single line
  412. if (DebugAlwaysTrue(Remaining.Pos(L"\t") == 0))
  413. {
  414. Start = Line.SubString(1, TabPos - 1);
  415. // WORKAROUND
  416. // Previously we padded the string before tab with spaces,
  417. // to aling the contents across multiple lines
  418. Start = Start.TrimRight();
  419. // at least two normal spaces for separation
  420. Start += L" ";
  421. Result = true;
  422. }
  423. }
  424. return Result;
  425. }
  426. //---------------------------------------------------------------------------
  427. static int __fastcall CalculateWidthByLength(UnicodeString Text, void * /*Arg*/)
  428. {
  429. return Text.Length();
  430. }
  431. //---------------------------------------------------------------------------
  432. void __fastcall ApplyTabs(
  433. UnicodeString & Text, wchar_t Padding,
  434. TCalculateWidth CalculateWidth, void * CalculateWidthArg)
  435. {
  436. if (CalculateWidth == NULL)
  437. {
  438. DebugAssert(CalculateWidthArg == NULL);
  439. CalculateWidth = CalculateWidthByLength;
  440. }
  441. std::unique_ptr<TStringList> Lines(TextToStringList(Text));
  442. int MaxWidth = -1;
  443. for (int Index = 0; Index < Lines->Count; Index++)
  444. {
  445. UnicodeString Line = Lines->Strings[Index];
  446. int TabPos;
  447. UnicodeString Start;
  448. UnicodeString Remaining;
  449. if (IsEligibleForApplyingTabs(Line, TabPos, Start, Remaining))
  450. {
  451. int Width = CalculateWidth(Start, CalculateWidthArg);
  452. MaxWidth = Max(MaxWidth, Width);
  453. }
  454. }
  455. // Optimization and also to prevent potential regression for texts without tabs
  456. if (MaxWidth >= 0)
  457. {
  458. for (int Index = 0; Index < Lines->Count; Index++)
  459. {
  460. UnicodeString Line = Lines->Strings[Index];
  461. int TabPos;
  462. UnicodeString Start;
  463. UnicodeString Remaining;
  464. if (IsEligibleForApplyingTabs(Line, TabPos, Start, Remaining))
  465. {
  466. int Width;
  467. while ((Width = CalculateWidth(Start, CalculateWidthArg)) < MaxWidth)
  468. {
  469. int Wider = CalculateWidth(Start + Padding, CalculateWidthArg);
  470. // If padded string is wider than max width by more pixels
  471. // than non-padded string is shorter than max width
  472. if ((Wider > MaxWidth) && ((Wider - MaxWidth) > (MaxWidth - Width)))
  473. {
  474. break;
  475. }
  476. Start += Padding;
  477. }
  478. Lines->Strings[Index] = Start + Remaining;
  479. }
  480. }
  481. Text = Lines->Text;
  482. // remove trailing newline
  483. Text = Text.TrimRight();
  484. }
  485. }
  486. //---------------------------------------------------------------------------
  487. void __fastcall SelectScaledImageList(TImageList * ImageList)
  488. {
  489. TImageList * MatchingList = NULL;
  490. int MachingPixelsPerInch = 0;
  491. int PixelsPerInch = Screen->PixelsPerInch;
  492. for (int Index = 0; Index < ImageList->Owner->ComponentCount; Index++)
  493. {
  494. TImageList * OtherList = dynamic_cast<TImageList *>(ImageList->Owner->Components[Index]);
  495. if ((OtherList != NULL) &&
  496. (OtherList != ImageList) &&
  497. StartsStr(ImageList->Name, OtherList->Name))
  498. {
  499. UnicodeString OtherListPixelsPerInchStr =
  500. OtherList->Name.SubString(
  501. ImageList->Name.Length() + 1, OtherList->Name.Length() - ImageList->Name.Length());
  502. int OtherListPixelsPerInch = StrToInt(OtherListPixelsPerInchStr);
  503. if ((OtherListPixelsPerInch <= PixelsPerInch) &&
  504. ((MatchingList == NULL) ||
  505. (MachingPixelsPerInch < OtherListPixelsPerInch)))
  506. {
  507. MatchingList = OtherList;
  508. MachingPixelsPerInch = OtherListPixelsPerInch;
  509. }
  510. }
  511. }
  512. if (MatchingList != NULL)
  513. {
  514. CopyImageList(ImageList, MatchingList);
  515. }
  516. }
  517. //---------------------------------------------------------------------------
  518. void __fastcall CopyImageList(TImageList * TargetList, TImageList * SourceList)
  519. {
  520. TPngImageList * PngTargetList = dynamic_cast<TPngImageList *>(TargetList);
  521. TPngImageList * PngSourceList = dynamic_cast<TPngImageList *>(SourceList);
  522. TargetList->Clear();
  523. TargetList->Height = SourceList->Height;
  524. TargetList->Width = SourceList->Width;
  525. if ((PngTargetList != NULL) && (PngSourceList != NULL))
  526. {
  527. // AddImages won't copy the names and we need them for
  528. // LoadDialogImage and TFrameAnimation
  529. PngTargetList->PngImages->Assign(PngSourceList->PngImages);
  530. }
  531. else
  532. {
  533. TargetList->AddImages(SourceList);
  534. }
  535. }
  536. //---------------------------------------------------------------------------
  537. void __fastcall CopyDataModule(TDataModule * TargetModule, TDataModule * SourceModule)
  538. {
  539. DebugAssert(TargetModule->ComponentCount == SourceModule->ComponentCount);
  540. typedef std::vector<std::pair<TComponent *, TComponent *> > TComponentPairs;
  541. TComponentPairs ComponentPairs;
  542. for (int Index = 0; Index < TargetModule->ComponentCount; Index++)
  543. {
  544. TComponent * TargetComponent = TargetModule->Components[Index];
  545. TComponent * SourceComponent = SourceModule->FindComponent(TargetComponent->Name);
  546. if (DebugAlwaysTrue(SourceComponent != NULL))
  547. {
  548. ComponentPairs.push_back(std::make_pair(TargetComponent, SourceComponent));
  549. }
  550. }
  551. TComponentPairs::const_iterator Iterator = ComponentPairs.begin();
  552. while (Iterator != ComponentPairs.end())
  553. {
  554. TComponent * TargetComponent = Iterator->first;
  555. TComponent * SourceComponent = Iterator->second;
  556. TargetModule->RemoveComponent(TargetComponent);
  557. SourceModule->RemoveComponent(SourceComponent);
  558. TargetModule->InsertComponent(SourceComponent);
  559. SourceModule->InsertComponent(TargetComponent);
  560. Iterator++;
  561. }
  562. }
  563. //---------------------------------------------------------------------------
  564. void __fastcall LoadDialogImage(TImage * Image, const UnicodeString & ImageName)
  565. {
  566. if (GlyphsModule != NULL)
  567. {
  568. TPngImageList * DialogImages = GlyphsModule->DialogImages;
  569. int Index;
  570. for (Index = 0; Index < DialogImages->PngImages->Count; Index++)
  571. {
  572. TPngImageCollectionItem * PngItem = DialogImages->PngImages->Items[Index];
  573. if (SameText(PngItem->Name, ImageName))
  574. {
  575. Image->Picture->Assign(PngItem->PngImage);
  576. break;
  577. }
  578. }
  579. DebugAssert(Index < DialogImages->PngImages->Count);
  580. }
  581. // When showing an exception from wWinMain, the glyphs module does not exist anymore.
  582. // We expect errors only.
  583. else if (ImageName == L"Error")
  584. {
  585. Image->Picture->Icon->Handle = LoadIcon(0, IDI_HAND);
  586. }
  587. // For showing an information about trace files
  588. else if (DebugAlwaysTrue(ImageName == L"Information"))
  589. {
  590. Image->Picture->Icon->Handle = LoadIcon(0, IDI_APPLICATION);
  591. }
  592. }
  593. //---------------------------------------------------------------------------
  594. int __fastcall DialogImageSize()
  595. {
  596. return ScaleByPixelsPerInch(32);
  597. }
  598. //---------------------------------------------------------------------------
  599. void __fastcall HideComponentsPanel(TForm * Form)
  600. {
  601. TComponent * Component = DebugNotNull(Form->FindComponent(L"ComponentsPanel"));
  602. TPanel * Panel = DebugNotNull(dynamic_cast<TPanel *>(Component));
  603. DebugAssert(Panel->Align == alBottom);
  604. int Offset = Panel->Height;
  605. Panel->Visible = false;
  606. Form->Height -= Offset;
  607. for (int Index = 0; Index < Form->ControlCount; Index++)
  608. {
  609. TControl * Control = Form->Controls[Index];
  610. // Shift back bottom-anchored controls
  611. // (needed for toolbar panel on Progress window and buttons on Preferences dialog),
  612. if ((Control->Align == alNone) &&
  613. Control->Anchors.Contains(akBottom) &&
  614. !Control->Anchors.Contains(akTop))
  615. {
  616. Control->Top += Offset;
  617. }
  618. // Resize back all-anchored controls
  619. // (needed for main panel on Preferences dialog),
  620. if (Control->Anchors.Contains(akBottom) &&
  621. Control->Anchors.Contains(akTop))
  622. {
  623. Control->Height += Offset;
  624. }
  625. }
  626. }
  627. //---------------------------------------------------------------------------
  628. class TBrowserViewer : public TWebBrowserEx
  629. {
  630. public:
  631. __fastcall virtual TBrowserViewer(TComponent* AOwner);
  632. void __fastcall AddLinkHandler(
  633. const UnicodeString & Url, TNotifyEvent Handler);
  634. void __fastcall NavigateToUrl(const UnicodeString & Url);
  635. TControl * LoadingPanel;
  636. protected:
  637. DYNAMIC void __fastcall DoContextPopup(const TPoint & MousePos, bool & Handled);
  638. void __fastcall DocumentComplete(
  639. TObject * Sender, const _di_IDispatch Disp, const OleVariant & URL);
  640. void __fastcall BeforeNavigate2(
  641. TObject * Sender, const _di_IDispatch Disp, const OleVariant & URL,
  642. const OleVariant & Flags, const OleVariant & TargetFrameName,
  643. const OleVariant & PostData, const OleVariant & Headers, WordBool & Cancel);
  644. bool FComplete;
  645. std::map<UnicodeString, TNotifyEvent> FHandlers;
  646. };
  647. //---------------------------------------------------------------------------
  648. __fastcall TBrowserViewer::TBrowserViewer(TComponent* AOwner) :
  649. TWebBrowserEx(AOwner)
  650. {
  651. FComplete = false;
  652. OnDocumentComplete = DocumentComplete;
  653. OnBeforeNavigate2 = BeforeNavigate2;
  654. LoadingPanel = NULL;
  655. }
  656. //---------------------------------------------------------------------------
  657. void __fastcall TBrowserViewer::AddLinkHandler(
  658. const UnicodeString & Url, TNotifyEvent Handler)
  659. {
  660. FHandlers.insert(std::make_pair(Url, Handler));
  661. }
  662. //---------------------------------------------------------------------------
  663. void __fastcall TBrowserViewer::DoContextPopup(const TPoint & MousePos, bool & Handled)
  664. {
  665. // suppress built-in context menu
  666. Handled = true;
  667. TWebBrowserEx::DoContextPopup(MousePos, Handled);
  668. }
  669. //---------------------------------------------------------------------------
  670. void __fastcall TBrowserViewer::DocumentComplete(
  671. TObject * /*Sender*/, const _di_IDispatch /*Disp*/, const OleVariant & /*URL*/)
  672. {
  673. SetBrowserDesignModeOff(this);
  674. FComplete = true;
  675. if (LoadingPanel != NULL)
  676. {
  677. LoadingPanel->Visible = false;
  678. }
  679. }
  680. //---------------------------------------------------------------------------
  681. void __fastcall TBrowserViewer::BeforeNavigate2(
  682. TObject * /*Sender*/, const _di_IDispatch /*Disp*/, const OleVariant & AURL,
  683. const OleVariant & /*Flags*/, const OleVariant & /*TargetFrameName*/,
  684. const OleVariant & /*PostData*/, const OleVariant & /*Headers*/, WordBool & Cancel)
  685. {
  686. // If OnDocumentComplete was not called yet, is has to be our initial message URL,
  687. // opened using TWebBrowserEx::Navigate(), allow it.
  688. // Otherwise it's user navigating, block that and open link
  689. // in an external browser, possibly adding campaign parameters on the way.
  690. if (FComplete)
  691. {
  692. Cancel = 1;
  693. UnicodeString URL = AURL;
  694. if (FHandlers.count(URL) > 0)
  695. {
  696. FHandlers[URL](this);
  697. }
  698. else
  699. {
  700. OpenBrowser(URL);
  701. }
  702. }
  703. }
  704. //---------------------------------------------------------------------------
  705. void __fastcall TBrowserViewer::NavigateToUrl(const UnicodeString & Url)
  706. {
  707. FComplete = false;
  708. Navigate(Url.c_str());
  709. }
  710. //---------------------------------------------------------------------------
  711. TPanel * __fastcall CreateLabelPanel(TPanel * Parent, const UnicodeString & Label)
  712. {
  713. TPanel * Result = CreateBlankPanel(Parent);
  714. Result->Parent = Parent;
  715. Result->Align = alClient;
  716. Result->Caption = Label;
  717. return Result;
  718. }
  719. //---------------------------------------------------------------------------
  720. TWebBrowserEx * __fastcall CreateBrowserViewer(TPanel * Parent, const UnicodeString & LoadingLabel)
  721. {
  722. TBrowserViewer * Result = new TBrowserViewer(Parent);
  723. // TWebBrowserEx has its own unrelated Name and Parent properties.
  724. // The name is used in DownloadUpdate().
  725. static_cast<TWinControl *>(Result)->Name = L"BrowserViewer";
  726. static_cast<TWinControl *>(Result)->Parent = Parent;
  727. Result->Align = alClient;
  728. Result->ControlBorder = cbNone;
  729. Result->LoadingPanel = CreateLabelPanel(Parent, LoadingLabel);
  730. return Result;
  731. }
  732. //---------------------------------------------------------------------------
  733. void __fastcall SetBrowserDesignModeOff(TWebBrowserEx * WebBrowser)
  734. {
  735. if (DebugAlwaysTrue(WebBrowser->Document2 != NULL))
  736. {
  737. WebBrowser->Document2->designMode = L"Off";
  738. }
  739. }
  740. //---------------------------------------------------------------------------
  741. void __fastcall AddBrowserLinkHandler(TWebBrowserEx * WebBrowser,
  742. const UnicodeString & Url, TNotifyEvent Handler)
  743. {
  744. TBrowserViewer * BrowserViewer = dynamic_cast<TBrowserViewer *>(WebBrowser);
  745. if (DebugAlwaysTrue(BrowserViewer != NULL))
  746. {
  747. BrowserViewer->AddLinkHandler(Url, Handler);
  748. }
  749. }
  750. //---------------------------------------------------------------------------
  751. void __fastcall NavigateBrowserToUrl(TWebBrowserEx * WebBrowser, const UnicodeString & Url)
  752. {
  753. TBrowserViewer * BrowserViewer = dynamic_cast<TBrowserViewer *>(WebBrowser);
  754. if (DebugAlwaysTrue(BrowserViewer != NULL))
  755. {
  756. BrowserViewer->NavigateToUrl(Url);
  757. }
  758. }
  759. //---------------------------------------------------------------------------
  760. TComponent * __fastcall FindComponentRecursively(TComponent * Root, const UnicodeString & Name)
  761. {
  762. for (int Index = 0; Index < Root->ComponentCount; Index++)
  763. {
  764. TComponent * Component = Root->Components[Index];
  765. if (CompareText(Component->Name, Name) == 0)
  766. {
  767. return Component;
  768. }
  769. Component = FindComponentRecursively(Component, Name);
  770. if (Component != NULL)
  771. {
  772. return Component;
  773. }
  774. }
  775. return NULL;
  776. }
  777. //---------------------------------------------------------------------------
  778. TLocalCustomCommand::TLocalCustomCommand()
  779. {
  780. }
  781. //---------------------------------------------------------------------------
  782. TLocalCustomCommand::TLocalCustomCommand(
  783. const TCustomCommandData & Data, const UnicodeString & RemotePath, const UnicodeString & LocalPath) :
  784. TFileCustomCommand(Data, RemotePath)
  785. {
  786. FLocalPath = LocalPath;
  787. }
  788. //---------------------------------------------------------------------------
  789. TLocalCustomCommand::TLocalCustomCommand(const TCustomCommandData & Data,
  790. const UnicodeString & RemotePath, const UnicodeString & LocalPath, const UnicodeString & FileName,
  791. const UnicodeString & LocalFileName, const UnicodeString & FileList) :
  792. TFileCustomCommand(Data, RemotePath, FileName, FileList)
  793. {
  794. FLocalPath = LocalPath;
  795. FLocalFileName = LocalFileName;
  796. }
  797. //---------------------------------------------------------------------------
  798. int __fastcall TLocalCustomCommand::PatternLen(const UnicodeString & Command, int Index)
  799. {
  800. int Len;
  801. if ((Index < Command.Length()) && (Command[Index + 1] == L'\\'))
  802. {
  803. Len = 2;
  804. }
  805. else if ((Index < Command.Length()) && (Command[Index + 1] == L'^'))
  806. {
  807. Len = 3;
  808. }
  809. else
  810. {
  811. Len = TFileCustomCommand::PatternLen(Command, Index);
  812. }
  813. return Len;
  814. }
  815. //---------------------------------------------------------------------------
  816. bool __fastcall TLocalCustomCommand::PatternReplacement(
  817. int Index, const UnicodeString & Pattern, UnicodeString & Replacement, bool & Delimit)
  818. {
  819. bool Result;
  820. if (Pattern == L"!\\")
  821. {
  822. // When used as "!\" in an argument to PowerShell, the trailing \ would escpae the ",
  823. // so we exclude it
  824. Replacement = ExcludeTrailingBackslash(FLocalPath);
  825. Result = true;
  826. }
  827. else if (Pattern == L"!^!")
  828. {
  829. Replacement = FLocalFileName;
  830. Result = true;
  831. }
  832. else
  833. {
  834. Result = TFileCustomCommand::PatternReplacement(Index, Pattern, Replacement, Delimit);
  835. }
  836. return Result;
  837. }
  838. //---------------------------------------------------------------------------
  839. void __fastcall TLocalCustomCommand::DelimitReplacement(
  840. UnicodeString & /*Replacement*/, wchar_t /*Quote*/)
  841. {
  842. // never delimit local commands
  843. }
  844. //---------------------------------------------------------------------------
  845. bool __fastcall TLocalCustomCommand::HasLocalFileName(const UnicodeString & Command)
  846. {
  847. return FindPattern(Command, L'^');
  848. }
  849. //---------------------------------------------------------------------------
  850. bool __fastcall TLocalCustomCommand::IsFileCommand(const UnicodeString & Command)
  851. {
  852. return TFileCustomCommand::IsFileCommand(Command) || HasLocalFileName(Command);
  853. }
  854. //---------------------------------------------------------------------------
  855. //---------------------------------------------------------------------------
  856. __fastcall TFrameAnimation::TFrameAnimation()
  857. {
  858. FFirstFrame = -1;
  859. }
  860. //---------------------------------------------------------------------------
  861. void __fastcall TFrameAnimation::Init(TPaintBox * PaintBox, const UnicodeString & Name)
  862. {
  863. DoInit(PaintBox, NULL, Name, Name.IsEmpty());
  864. }
  865. //---------------------------------------------------------------------------
  866. void __fastcall TFrameAnimation::DoInit(TPaintBox * PaintBox, TPngImageList * ImageList, const UnicodeString & Name, bool Null)
  867. {
  868. FImageList = (ImageList != NULL) ? ImageList : GetAnimationsModule()->AnimationImages;
  869. FFirstFrame = -1;
  870. FFirstLoopFrame = -1;
  871. DebugAssert((PaintBox->OnPaint == NULL) || (PaintBox->OnPaint == PaintBoxPaint));
  872. PaintBox->ControlStyle = PaintBox->ControlStyle << csOpaque;
  873. PaintBox->OnPaint = PaintBoxPaint;
  874. PaintBox->Width = FImageList->Width;
  875. PaintBox->Height = FImageList->Height;
  876. FPaintBox = PaintBox;
  877. if (!Null)
  878. {
  879. int Frame = 0;
  880. while (Frame < FImageList->PngImages->Count)
  881. {
  882. UnicodeString FrameData = FImageList->PngImages->Items[Frame]->Name;
  883. UnicodeString FrameName;
  884. FrameName = CutToChar(FrameData, L'_', false);
  885. if (SameText(Name, FrameName))
  886. {
  887. int FrameIndex = StrToInt(CutToChar(FrameData, L'_', false));
  888. if (FFirstFrame < 0)
  889. {
  890. FFirstFrame = Frame;
  891. }
  892. if ((FFirstLoopFrame < 0) && (FrameIndex > 0))
  893. {
  894. FFirstLoopFrame = Frame;
  895. }
  896. FLastFrame = Frame;
  897. }
  898. else
  899. {
  900. if (FFirstFrame >= 0)
  901. {
  902. // optimization
  903. break;
  904. }
  905. }
  906. Frame++;
  907. }
  908. DebugAssert(FFirstFrame >= 0);
  909. DebugAssert(FFirstLoopFrame >= 0);
  910. }
  911. Stop();
  912. }
  913. //---------------------------------------------------------------------------
  914. void __fastcall TFrameAnimation::Start()
  915. {
  916. if (FFirstFrame >= 0)
  917. {
  918. FNextFrameTick = GetTickCount();
  919. CalculateNextFrameTick();
  920. if (FTimer == NULL)
  921. {
  922. FTimer = new TTimer(GetParentForm(FPaintBox));
  923. FTimer->Interval = static_cast<int>(GUIUpdateInterval);
  924. FTimer->OnTimer = Timer;
  925. }
  926. else
  927. {
  928. // reset timer
  929. FTimer->Enabled = false;
  930. FTimer->Enabled = true;
  931. }
  932. }
  933. }
  934. //---------------------------------------------------------------------------
  935. void __fastcall TFrameAnimation::Timer(TObject * /*Sender*/)
  936. {
  937. Animate();
  938. }
  939. //---------------------------------------------------------------------------
  940. void __fastcall TFrameAnimation::PaintBoxPaint(TObject * Sender)
  941. {
  942. if (FFirstFrame >= 0)
  943. {
  944. // Double-buffered drawing to prevent flicker (as the images are transparent)
  945. DebugUsedParam(Sender);
  946. DebugAssert(FPaintBox == Sender);
  947. DebugAssert(FPaintBox->ControlStyle.Contains(csOpaque));
  948. std::unique_ptr<TBitmap> Bitmap(new TBitmap());
  949. Bitmap->SetSize(FPaintBox->Width, FPaintBox->Height);
  950. Bitmap->Canvas->Brush->Color = FPaintBox->Color;
  951. TRect Rect(0, 0, Bitmap->Width, Bitmap->Height);
  952. Bitmap->Canvas->FillRect(Rect);
  953. TGraphic * Graphic = GetCurrentImage()->PngImage;
  954. // Do not trigger assertion when animation size does not match scaled
  955. // paint box as we do not have scaled animations available yet
  956. DebugAssert((Graphic->Width == FPaintBox->Width) || (Screen->PixelsPerInch != USER_DEFAULT_SCREEN_DPI));
  957. DebugAssert((Graphic->Height == FPaintBox->Height) || (Screen->PixelsPerInch != USER_DEFAULT_SCREEN_DPI));
  958. Bitmap->Canvas->Draw(0, 0, Graphic);
  959. FPaintBox->Canvas->Draw(0, 0, Bitmap.get());
  960. }
  961. FPainted = true;
  962. }
  963. //---------------------------------------------------------------------------
  964. void __fastcall TFrameAnimation::Repaint()
  965. {
  966. FPainted = false;
  967. // Ff the form is not showing yet, the Paint() is not even called
  968. FPaintBox->Repaint();
  969. if (!FPainted)
  970. {
  971. // Paint later, alternativelly we may keep trying Repaint() in Animate().
  972. // See also a hack in TAuthenticateForm::Log.
  973. FPaintBox->Invalidate();
  974. }
  975. }
  976. //---------------------------------------------------------------------------
  977. void __fastcall TFrameAnimation::Stop()
  978. {
  979. FNextFrameTick = std::numeric_limits<DWORD>::max();
  980. FCurrentFrame = FFirstFrame;
  981. Repaint();
  982. if (FTimer != NULL)
  983. {
  984. FTimer->Enabled = false;
  985. }
  986. }
  987. //---------------------------------------------------------------------------
  988. void __fastcall TFrameAnimation::Animate()
  989. {
  990. if (FFirstFrame >= 0)
  991. {
  992. // UPGRADE: Use GetTickCount64() when we stop supporting Windows XP.
  993. DWORD TickCount = GetTickCount();
  994. // Keep in sync with an opposite condition at the end of the loop.
  995. // We may skip some frames if we got stalled for a while
  996. while (TickCount >= FNextFrameTick)
  997. {
  998. if (FCurrentFrame >= FLastFrame)
  999. {
  1000. FCurrentFrame = FFirstLoopFrame;
  1001. }
  1002. else
  1003. {
  1004. FCurrentFrame++;
  1005. }
  1006. CalculateNextFrameTick();
  1007. Repaint();
  1008. }
  1009. }
  1010. }
  1011. //---------------------------------------------------------------------------
  1012. TPngImageCollectionItem * __fastcall TFrameAnimation::GetCurrentImage()
  1013. {
  1014. return FImageList->PngImages->Items[FCurrentFrame];
  1015. }
  1016. //---------------------------------------------------------------------------
  1017. void __fastcall TFrameAnimation::CalculateNextFrameTick()
  1018. {
  1019. TPngImageCollectionItem * ImageItem = GetCurrentImage();
  1020. UnicodeString Duration = ImageItem->Name;
  1021. CutToChar(Duration, L'_', false);
  1022. // skip index (is not really used)
  1023. CutToChar(Duration, L'_', false);
  1024. // This should overflow, when tick count wraps.
  1025. FNextFrameTick += StrToInt(Duration) * 10;
  1026. }
  1027. //---------------------------------------------------------------------------
  1028. //---------------------------------------------------------------------------
  1029. // Hints use:
  1030. // - Cleanup list tooltip (multi line)
  1031. // - Combo edit button
  1032. // - Transfer settings label (multi line, follows label size and font)
  1033. // - HintLabel (hint and persistent hint, multipline)
  1034. // - status bar hints
  1035. //---------------------------------------------------------------------------
  1036. __fastcall TScreenTipHintWindow::TScreenTipHintWindow(TComponent * Owner) :
  1037. THintWindow(Owner)
  1038. {
  1039. FParentPainting = false;
  1040. }
  1041. //---------------------------------------------------------------------------
  1042. int __fastcall TScreenTipHintWindow::GetTextFlags(TControl * Control)
  1043. {
  1044. return DT_LEFT | DT_WORDBREAK | DT_NOPREFIX | Control->DrawTextBiDiModeFlagsReadingOnly();
  1045. }
  1046. //---------------------------------------------------------------------------
  1047. bool __fastcall TScreenTipHintWindow::UseBoldShortHint(TControl * HintControl)
  1048. {
  1049. return
  1050. (dynamic_cast<TTBCustomDockableWindow *>(HintControl) != NULL) ||
  1051. (dynamic_cast<TTBPopupWindow *>(HintControl) != NULL);
  1052. }
  1053. //---------------------------------------------------------------------------
  1054. bool __fastcall TScreenTipHintWindow::IsPathLabel(TControl * HintControl)
  1055. {
  1056. return (dynamic_cast<TPathLabel *>(HintControl) != NULL);
  1057. }
  1058. //---------------------------------------------------------------------------
  1059. bool __fastcall TScreenTipHintWindow::IsHintPopup(TControl * HintControl, const UnicodeString & Hint)
  1060. {
  1061. TLabel * HintLabel = dynamic_cast<TLabel *>(HintControl);
  1062. return (HintLabel != NULL) && HasLabelHintPopup(HintLabel, Hint);
  1063. }
  1064. //---------------------------------------------------------------------------
  1065. int __fastcall TScreenTipHintWindow::GetMargin(TControl * HintControl, const UnicodeString & Hint)
  1066. {
  1067. int Result;
  1068. if (IsHintPopup(HintControl, Hint) || IsPathLabel(HintControl))
  1069. {
  1070. Result = 3;
  1071. }
  1072. else
  1073. {
  1074. Result = 6;
  1075. }
  1076. Result = ScaleByPixelsPerInch(Result);
  1077. return Result;
  1078. }
  1079. //---------------------------------------------------------------------------
  1080. TFont * __fastcall TScreenTipHintWindow::GetFont(TControl * HintControl, const UnicodeString & Hint)
  1081. {
  1082. TFont * Result;
  1083. if (IsHintPopup(HintControl, Hint) || IsPathLabel(HintControl))
  1084. {
  1085. Result = reinterpret_cast<TLabel *>(dynamic_cast<TCustomLabel *>(HintControl))->Font;
  1086. }
  1087. else
  1088. {
  1089. Result = Screen->HintFont;
  1090. }
  1091. return Result;
  1092. }
  1093. //---------------------------------------------------------------------------
  1094. void __fastcall TScreenTipHintWindow::CalcHintTextRect(TControl * Control, TCanvas * Canvas, TRect & Rect, const UnicodeString & Hint)
  1095. {
  1096. const int Flags = DT_CALCRECT | GetTextFlags(Control);
  1097. DrawText(Canvas->Handle, Hint.c_str(), -1, &Rect, Flags);
  1098. }
  1099. //---------------------------------------------------------------------------
  1100. TRect __fastcall TScreenTipHintWindow::CalcHintRect(int MaxWidth, const UnicodeString AHint, void * AData)
  1101. {
  1102. TControl * HintControl = GetHintControl(AData);
  1103. int Margin = GetMargin(HintControl, AHint);
  1104. const UnicodeString ShortHint = GetShortHint(AHint);
  1105. const UnicodeString LongHint = GetLongHintIfAny(AHint);
  1106. Canvas->Font->Assign(GetFont(HintControl, AHint));
  1107. // we do not have a parent form here, so we cannot scale by text height
  1108. const int ScreenTipTextOnlyWidth = ScaleByPixelsPerInch(cScreenTipTextOnlyWidth);
  1109. if (!LongHint.IsEmpty())
  1110. {
  1111. // double-margin on the right
  1112. MaxWidth = ScreenTipTextOnlyWidth - (3 * Margin);
  1113. }
  1114. // Multi line short hints can be twice as wide, to not break the individual lines unless really necessary.
  1115. // (login site tree, clean up dialog list, preferences custom command list, persistent hint, etc).
  1116. // And they also can be twice as wide, to not break the individual lines unless really necessary.
  1117. if (ShortHint.Pos(L"\n") > 0)
  1118. {
  1119. MaxWidth *= 2;
  1120. }
  1121. bool HintPopup = IsHintPopup(HintControl, AHint);
  1122. if (HintPopup)
  1123. {
  1124. MaxWidth = HintControl->Width;
  1125. }
  1126. if (UseBoldShortHint(HintControl))
  1127. {
  1128. Canvas->Font->Style = TFontStyles() << fsBold;
  1129. }
  1130. TRect ShortRect(0, 0, MaxWidth, 0);
  1131. CalcHintTextRect(this, Canvas, ShortRect, ShortHint);
  1132. Canvas->Font->Style = TFontStyles();
  1133. TRect Result;
  1134. if (LongHint.IsEmpty())
  1135. {
  1136. Result = ShortRect;
  1137. if (HintPopup)
  1138. {
  1139. Result.Right = MaxWidth + 2 * Margin;
  1140. }
  1141. else
  1142. {
  1143. Result.Right += 3 * Margin;
  1144. }
  1145. Result.Bottom += 2 * Margin;
  1146. }
  1147. else
  1148. {
  1149. const int LongIndentation = Margin * 3 / 2;
  1150. TRect LongRect(0, 0, MaxWidth - LongIndentation, 0);
  1151. CalcHintTextRect(this, Canvas, LongRect, LongHint);
  1152. Result.Right = ScreenTipTextOnlyWidth;
  1153. Result.Bottom = Margin + ShortRect.Height() + (Margin / 3 * 5) + LongRect.Height() + Margin;
  1154. }
  1155. // VCLCOPY: To counter the increase in THintWindow::ActivateHintData
  1156. Result.Bottom -= 4;
  1157. return Result;
  1158. }
  1159. //---------------------------------------------------------------------------
  1160. void __fastcall TScreenTipHintWindow::ActivateHintData(const TRect & ARect, const UnicodeString AHint, void * AData)
  1161. {
  1162. FShortHint = GetShortHint(AHint);
  1163. FLongHint = GetLongHintIfAny(AHint);
  1164. FHintControl = GetHintControl(AData);
  1165. FMargin = GetMargin(FHintControl, AHint);
  1166. FHintPopup = IsHintPopup(FHintControl, AHint);
  1167. Canvas->Font->Assign(GetFont(FHintControl, AHint));
  1168. TRect Rect = ARect;
  1169. if (FHintPopup)
  1170. {
  1171. Rect.SetLocation(FHintControl->ClientToScreen(TPoint(-FMargin, -FMargin)));
  1172. }
  1173. if (IsPathLabel(FHintControl))
  1174. {
  1175. Rect.Offset(-FMargin, -FMargin);
  1176. }
  1177. THintWindow::ActivateHintData(Rect, FShortHint, AData);
  1178. }
  1179. //---------------------------------------------------------------------------
  1180. TControl * __fastcall TScreenTipHintWindow::GetHintControl(void * Data)
  1181. {
  1182. return reinterpret_cast<TControl *>(DebugNotNull(Data));
  1183. }
  1184. //---------------------------------------------------------------------------
  1185. UnicodeString __fastcall TScreenTipHintWindow::GetLongHintIfAny(const UnicodeString & AHint)
  1186. {
  1187. UnicodeString Result;
  1188. int P = Pos(L"|", AHint);
  1189. if (P > 0)
  1190. {
  1191. Result = GetLongHint(AHint);
  1192. }
  1193. return Result;
  1194. }
  1195. //---------------------------------------------------------------------------
  1196. void __fastcall TScreenTipHintWindow::Dispatch(void * AMessage)
  1197. {
  1198. TMessage * Message = static_cast<TMessage*>(AMessage);
  1199. switch (Message->Msg)
  1200. {
  1201. case WM_GETTEXTLENGTH:
  1202. if (FParentPainting)
  1203. {
  1204. // make THintWindow::Paint() not paint the Caption
  1205. Message->Result = 0;
  1206. }
  1207. else
  1208. {
  1209. THintWindow::Dispatch(AMessage);
  1210. }
  1211. break;
  1212. default:
  1213. THintWindow::Dispatch(AMessage);
  1214. break;
  1215. }
  1216. }
  1217. //---------------------------------------------------------------------------
  1218. void __fastcall TScreenTipHintWindow::Paint()
  1219. {
  1220. // paint frame/background
  1221. {
  1222. TAutoFlag ParentPaintingFlag(FParentPainting);
  1223. THintWindow::Paint();
  1224. }
  1225. const int Flags = GetTextFlags(this);
  1226. const int Margin = FMargin - 1; // 1 = border
  1227. TRect Rect = ClientRect;
  1228. Rect.Inflate(-Margin, -Margin);
  1229. if (!FHintPopup)
  1230. {
  1231. Rect.Right -= FMargin;
  1232. }
  1233. if (UseBoldShortHint(FHintControl))
  1234. {
  1235. Canvas->Font->Style = TFontStyles() << fsBold;
  1236. }
  1237. DrawText(Canvas->Handle, FShortHint.c_str(), -1, &Rect, Flags);
  1238. TRect ShortRect = Rect;
  1239. DrawText(Canvas->Handle, FShortHint.c_str(), -1, &ShortRect, DT_CALCRECT | Flags);
  1240. Canvas->Font->Style = TFontStyles();
  1241. if (!FLongHint.IsEmpty())
  1242. {
  1243. Rect.Left += FMargin * 3 / 2;
  1244. Rect.Top += ShortRect.Height() + (FMargin / 3 * 5);
  1245. DrawText(Canvas->Handle, FLongHint.c_str(), -1, &Rect, Flags);
  1246. }
  1247. }