GUITools.cpp 49 KB

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