GUITools.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  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::IsHintPopup(TControl * HintControl, const UnicodeString & Hint)
  1096. {
  1097. TLabel * HintLabel = dynamic_cast<TLabel *>(HintControl);
  1098. return (HintLabel != NULL) && HasLabelHintPopup(HintLabel, Hint);
  1099. }
  1100. //---------------------------------------------------------------------------
  1101. int __fastcall TScreenTipHintWindow::GetMargin(TControl * HintControl, const UnicodeString & Hint)
  1102. {
  1103. int Result;
  1104. if (IsHintPopup(HintControl, Hint))
  1105. {
  1106. Result = 3;
  1107. }
  1108. else
  1109. {
  1110. Result = 6;
  1111. }
  1112. Result = ScaleByPixelsPerInch(Result);
  1113. return Result;
  1114. }
  1115. //---------------------------------------------------------------------------
  1116. TFont * __fastcall TScreenTipHintWindow::GetFont(TControl * HintControl, const UnicodeString & Hint)
  1117. {
  1118. bool HintPopup = IsHintPopup(HintControl, Hint);
  1119. TFont * Result;
  1120. if (HintPopup)
  1121. {
  1122. Result = dynamic_cast<TLabel *>(HintControl)->Font;
  1123. }
  1124. else
  1125. {
  1126. Result = Screen->HintFont;
  1127. }
  1128. return Result;
  1129. }
  1130. //---------------------------------------------------------------------------
  1131. void __fastcall TScreenTipHintWindow::CalcHintTextRect(TControl * Control, TCanvas * Canvas, TRect & Rect, const UnicodeString & Hint)
  1132. {
  1133. const int Flags = DT_CALCRECT | GetTextFlags(Control);
  1134. DrawText(Canvas->Handle, Hint.c_str(), -1, &Rect, Flags);
  1135. }
  1136. //---------------------------------------------------------------------------
  1137. TRect __fastcall TScreenTipHintWindow::CalcHintRect(int MaxWidth, const UnicodeString AHint, void * AData)
  1138. {
  1139. TControl * HintControl = GetHintControl(AData);
  1140. int Margin = GetMargin(HintControl, AHint);
  1141. const UnicodeString ShortHint = GetShortHint(AHint);
  1142. const UnicodeString LongHint = GetLongHintIfAny(AHint);
  1143. Canvas->Font->Assign(GetFont(HintControl, AHint));
  1144. // we do not have a parent form here, so we cannot scale by text height
  1145. const int ScreenTipTextOnlyWidth = ScaleByPixelsPerInch(cScreenTipTextOnlyWidth);
  1146. if (!LongHint.IsEmpty())
  1147. {
  1148. // double-margin on the right
  1149. MaxWidth = ScreenTipTextOnlyWidth - (3 * Margin);
  1150. }
  1151. // Multi line short hints can be twice as wide, to not break the individual lines unless really necessary.
  1152. // (login site tree, clean up dialog list, preferences custom command list, persistent hint, etc).
  1153. // And they also can be twice as wide, to not break the individual lines unless really necessary.
  1154. if (ShortHint.Pos(L"\n") > 0)
  1155. {
  1156. MaxWidth *= 2;
  1157. }
  1158. bool HintPopup = IsHintPopup(HintControl, AHint);
  1159. if (HintPopup)
  1160. {
  1161. MaxWidth = HintControl->Width;
  1162. }
  1163. if (UseBoldShortHint(HintControl))
  1164. {
  1165. Canvas->Font->Style = TFontStyles() << fsBold;
  1166. }
  1167. TRect ShortRect(0, 0, MaxWidth, 0);
  1168. CalcHintTextRect(this, Canvas, ShortRect, ShortHint);
  1169. Canvas->Font->Style = TFontStyles();
  1170. TRect Result;
  1171. if (LongHint.IsEmpty())
  1172. {
  1173. Result = ShortRect;
  1174. if (HintPopup)
  1175. {
  1176. Result.Right = MaxWidth + 2 * Margin;
  1177. }
  1178. else
  1179. {
  1180. Result.Right += 3 * Margin;
  1181. }
  1182. Result.Bottom += 2 * Margin;
  1183. }
  1184. else
  1185. {
  1186. const int LongIndentation = Margin * 3 / 2;
  1187. TRect LongRect(0, 0, MaxWidth - LongIndentation, 0);
  1188. CalcHintTextRect(this, Canvas, LongRect, LongHint);
  1189. Result.Right = ScreenTipTextOnlyWidth;
  1190. Result.Bottom = Margin + ShortRect.Height() + (Margin / 3 * 5) + LongRect.Height() + Margin;
  1191. }
  1192. // To counter the increase in THintWindow::ActivateHintData
  1193. Result.Bottom -= 4;
  1194. return Result;
  1195. }
  1196. //---------------------------------------------------------------------------
  1197. void __fastcall TScreenTipHintWindow::ActivateHintData(const TRect & ARect, const UnicodeString AHint, void * AData)
  1198. {
  1199. FShortHint = GetShortHint(AHint);
  1200. FLongHint = GetLongHintIfAny(AHint);
  1201. FHintControl = GetHintControl(AData);
  1202. FMargin = GetMargin(FHintControl, AHint);
  1203. FHintPopup = IsHintPopup(FHintControl, AHint);
  1204. Canvas->Font->Assign(GetFont(FHintControl, AHint));
  1205. TRect Rect = ARect;
  1206. if (FHintPopup)
  1207. {
  1208. Rect.SetLocation(FHintControl->ClientToScreen(TPoint(-FMargin, -FMargin)));
  1209. }
  1210. THintWindow::ActivateHintData(Rect, FShortHint, AData);
  1211. }
  1212. //---------------------------------------------------------------------------
  1213. TControl * __fastcall TScreenTipHintWindow::GetHintControl(void * Data)
  1214. {
  1215. return reinterpret_cast<TControl *>(DebugNotNull(Data));
  1216. }
  1217. //---------------------------------------------------------------------------
  1218. UnicodeString __fastcall TScreenTipHintWindow::GetLongHintIfAny(const UnicodeString & AHint)
  1219. {
  1220. UnicodeString Result;
  1221. int P = Pos(L"|", AHint);
  1222. if (P > 0)
  1223. {
  1224. Result = GetLongHint(AHint);
  1225. }
  1226. return Result;
  1227. }
  1228. //---------------------------------------------------------------------------
  1229. void __fastcall TScreenTipHintWindow::Dispatch(void * AMessage)
  1230. {
  1231. TMessage * Message = static_cast<TMessage*>(AMessage);
  1232. switch (Message->Msg)
  1233. {
  1234. case WM_GETTEXTLENGTH:
  1235. if (FParentPainting)
  1236. {
  1237. // make THintWindow::Paint() not paint the Caption
  1238. Message->Result = 0;
  1239. }
  1240. else
  1241. {
  1242. THintWindow::Dispatch(AMessage);
  1243. }
  1244. break;
  1245. default:
  1246. THintWindow::Dispatch(AMessage);
  1247. break;
  1248. }
  1249. }
  1250. //---------------------------------------------------------------------------
  1251. void __fastcall TScreenTipHintWindow::Paint()
  1252. {
  1253. // paint frame/background
  1254. {
  1255. TAutoFlag ParentPaintingFlag(FParentPainting);
  1256. THintWindow::Paint();
  1257. }
  1258. const int Flags = GetTextFlags(this);
  1259. const int Margin = FMargin - 1; // 1 = border
  1260. TRect Rect = ClientRect;
  1261. Rect.Inflate(-Margin, -Margin);
  1262. if (!FHintPopup)
  1263. {
  1264. Rect.Right -= FMargin;
  1265. }
  1266. if (UseBoldShortHint(FHintControl))
  1267. {
  1268. Canvas->Font->Style = TFontStyles() << fsBold;
  1269. }
  1270. DrawText(Canvas->Handle, FShortHint.c_str(), -1, &Rect, Flags);
  1271. TRect ShortRect = Rect;
  1272. DrawText(Canvas->Handle, FShortHint.c_str(), -1, &ShortRect, DT_CALCRECT | Flags);
  1273. Canvas->Font->Style = TFontStyles();
  1274. if (!FLongHint.IsEmpty())
  1275. {
  1276. Rect.Left += FMargin * 3 / 2;
  1277. Rect.Top += ShortRect.Height() + (FMargin / 3 * 5);
  1278. DrawText(Canvas->Handle, FLongHint.c_str(), -1, &Rect, Flags);
  1279. }
  1280. }