GUITools.cpp 44 KB

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