PathLabel.pas 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. unit PathLabel;
  2. interface
  3. {$WARN UNIT_PLATFORM OFF}
  4. uses
  5. Messages, StdCtrls, Controls, Classes, Forms, Windows, Graphics;
  6. type
  7. TCustomPathLabel = class;
  8. TPathLabelGetStatusEvent = procedure(Sender: TCustomPathLabel; var Active: Boolean) of object;
  9. TPathLabelPathClickEvent = procedure(Sender: TCustomPathLabel; Path: string) of object;
  10. TCustomPathLabel = class(TCustomLabel)
  11. private
  12. FColors: array[0..5] of TColor;
  13. FIndentHorizontal: Integer;
  14. FIndentVertical: Integer;
  15. FUnixPath: Boolean;
  16. FOnGetStatus: TPathLabelGetStatusEvent;
  17. FOnPathClick: TPathLabelPathClickEvent;
  18. FOnMaskClick: TNotifyEvent;
  19. FDisplayPath: string;
  20. FDisplayHotTrack: string;
  21. FDisplayMask: string;
  22. FHotTrack: Boolean;
  23. FMouseInView: Boolean;
  24. FIsActive: Boolean;
  25. FIsEnabled: Boolean;
  26. FMask: string;
  27. FAutoSizeVertical: Boolean;
  28. procedure CMHintShow(var Message: TMessage); message CM_HINTSHOW;
  29. procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
  30. procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
  31. function GetColors(Index: Integer): TColor;
  32. procedure SetColors(Index: Integer; Value: TColor);
  33. procedure SetIndentHorizontal(AIndent: Integer);
  34. procedure SetIndentVertical(AIndent: Integer);
  35. procedure SetUnixPath(AUnixPath: Boolean);
  36. procedure SetMask(Value: string);
  37. procedure SetAutoSizeVertical(Value: Boolean);
  38. procedure SetFocusControl(Value: TWinControl);
  39. function GetFocusControl: TWinControl;
  40. function CalculateAutoHotTrackColor(C: TColor): TColor;
  41. procedure CalculateAutoHotTrackColors;
  42. function CalculateAutoHotTrackColorComponent(C: Byte; Bright: Boolean): Byte;
  43. function UseHotTrack: Boolean;
  44. protected
  45. procedure AdjustBounds; override;
  46. procedure Click; override;
  47. procedure DoDrawTextIntern(var Rect: TRect; Flags: Longint; S: string);
  48. procedure DoDrawText(var Rect: TRect; Flags: Longint); override;
  49. procedure Notification(AComponent: TComponent;
  50. Operation: TOperation); override;
  51. procedure Paint; override;
  52. function IsActive: Boolean;
  53. function TrackingActive: Boolean;
  54. function HotTrackPath: string;
  55. function GetSeparator: Char;
  56. procedure MouseMove(Shift: TShiftState; X: Integer; Y: Integer); override;
  57. procedure DoPathClick(Path: string); virtual;
  58. procedure DoMaskClick;
  59. procedure DblClick; override;
  60. procedure DoContextPopup(MousePos: TPoint; var Handled: Boolean); override;
  61. public
  62. constructor Create(AnOwner: TComponent); override;
  63. procedure UpdateStatus;
  64. function UseRightToLeftAlignment: Boolean; override;
  65. property ActiveColor: TColor index 1 read GetColors write SetColors
  66. default clActiveCaption;
  67. property ActiveTextColor: TColor index 3 read GetColors write SetColors
  68. default clCaptionText;
  69. property UnixPath: Boolean read FUnixPath write SetUnixPath default False;
  70. property IndentHorizontal: Integer read FIndentHorizontal
  71. write SetIndentHorizontal default 5;
  72. property IndentVertical: Integer read FIndentVertical
  73. write SetIndentVertical default 1;
  74. property InactiveColor: TColor index 0 read GetColors write SetColors
  75. default clInactiveCaption;
  76. property InactiveTextColor: TColor index 2 read GetColors write SetColors
  77. default clInactiveCaptionText;
  78. property OnGetStatus: TPathLabelGetStatusEvent read FOnGetStatus write FOnGetStatus;
  79. property OnPathClick: TPathLabelPathClickEvent read FOnPathClick write FOnPathClick;
  80. property OnMaskClick: TNotifyEvent read FOnMaskClick write FOnMaskClick;
  81. property HotTrack: Boolean read FHotTrack write FHotTrack default False;
  82. property Mask: string read FMask write SetMask;
  83. property AutoSizeVertical: Boolean read FAutoSizeVertical write SetAutoSizeVertical default False;
  84. property FocusControl: TWinControl read GetFocusControl write SetFocusControl;
  85. property Caption;
  86. property Hint stored False;
  87. property Align default alTop;
  88. end;
  89. type
  90. TPathLabel = class(TCustomPathLabel)
  91. published
  92. property ActiveColor;
  93. property ActiveTextColor;
  94. property UnixPath;
  95. property IndentHorizontal;
  96. property IndentVertical;
  97. property InactiveColor;
  98. property InactiveTextColor;
  99. property AutoSizeVertical;
  100. property HotTrack;
  101. property OnGetStatus;
  102. property OnPathClick;
  103. property OnMaskClick;
  104. property Align;
  105. property Alignment;
  106. property Anchors;
  107. property AutoSize;
  108. property BiDiMode;
  109. property Constraints;
  110. property DragCursor;
  111. property DragKind;
  112. property DragMode;
  113. property Enabled;
  114. property Font;
  115. property ParentBiDiMode;
  116. property ParentFont;
  117. property PopupMenu;
  118. property Transparent;
  119. property Visible;
  120. property OnClick;
  121. property OnDblClick;
  122. property OnDragDrop;
  123. property OnDragOver;
  124. property OnEndDock;
  125. property OnEndDrag;
  126. property OnMouseDown;
  127. property OnMouseMove;
  128. property OnMouseUp;
  129. property OnStartDock;
  130. property OnStartDrag;
  131. end;
  132. procedure Register;
  133. implementation
  134. uses
  135. { SysUtils must overload deprecated FileCtrl (implements MinimizeName) }
  136. FileCtrl, SysUtils, Math;
  137. const
  138. // magic value
  139. HotTrackMask: string = #1;
  140. procedure Register;
  141. begin
  142. RegisterComponents('Martin', [TPathLabel]);
  143. end;
  144. { TCustomPathLabel }
  145. constructor TCustomPathLabel.Create(AnOwner: TComponent);
  146. begin
  147. inherited Create(AnOwner);
  148. WordWrap := False;
  149. Align := alTop;
  150. ShowAccelChar := False;
  151. FIndentHorizontal := 5;
  152. FIndentVertical := 1;
  153. FUnixPath := False;
  154. FHotTrack := False;
  155. FColors[0] := clInactiveCaption;
  156. FColors[1] := clActiveCaption;
  157. FColors[2] := clInactiveCaptionText;
  158. FColors[3] := clCaptionText;
  159. FIsEnabled := True;
  160. CalculateAutoHotTrackColors;
  161. end;
  162. procedure TCustomPathLabel.CMHintShow(var Message: TMessage);
  163. begin
  164. with TCMHintShow(Message).HintInfo^ do
  165. begin
  166. HintPos.X := ClientOrigin.X + IndentHorizontal;
  167. HintPos.Y := ClientOrigin.Y + IndentVertical;
  168. if UseHotTrack then Inc(HintPos.Y, Height);
  169. end;
  170. end; { CMHintShow }
  171. procedure TCustomPathLabel.Click;
  172. var
  173. HotPath: string;
  174. RemainingPath: string;
  175. begin
  176. if FIsEnabled then
  177. begin
  178. HotPath := HotTrackPath;
  179. if HotPath <> '' then
  180. begin
  181. if (Copy(HotPath, Length(HotPath), 1) = GetSeparator) and
  182. (HotPath <> GetSeparator) then
  183. SetLength(HotPath, Length(HotPath) - 1);
  184. if HotPath = HotTrackMask then DoMaskClick
  185. else
  186. if FDisplayPath = Caption then DoPathClick(HotPath)
  187. else
  188. begin
  189. // Displayed path is shortened.
  190. // The below is based on knowledge of MinimizeName algorithm
  191. RemainingPath := Copy(FDisplayPath, Length(HotPath) + 1,
  192. Length(FDisplayPath) - Length(HotPath));
  193. // Contrary to stripping separator from HotPath above, we strip it even from bare /,
  194. // as the Caption does not include it either
  195. if Copy(RemainingPath, Length(RemainingPath), 1) = GetSeparator then
  196. SetLength(RemainingPath, Length(RemainingPath) - 1);
  197. // Part from ellipsis (inclusive)
  198. if RemainingPath = Copy(Caption, Length(Caption) - Length(RemainingPath) + 1,
  199. Length(RemainingPath)) then
  200. begin
  201. DoPathClick(Copy(Caption, 1, Length(Caption) - Length(RemainingPath)));
  202. end
  203. else
  204. // Part before ellipsis
  205. if HotPath = Copy(Caption, 1, Length(HotPath)) then
  206. begin
  207. DoPathClick(HotPath);
  208. end
  209. else Assert(False);
  210. end;
  211. end;
  212. if Assigned(FocusControl) then FocusControl.SetFocus;
  213. inherited;
  214. end;
  215. end; { Click }
  216. procedure TCustomPathLabel.SetUnixPath(AUnixPath: Boolean);
  217. begin
  218. if FUnixPath <> AUnixPath then
  219. begin
  220. FUnixPath := AUnixPath;
  221. AdjustBounds;
  222. Invalidate;
  223. end;
  224. end;
  225. procedure TCustomPathLabel.SetMask(Value: string);
  226. begin
  227. if FMask <> Value then
  228. begin
  229. FMask := Value;
  230. AdjustBounds;
  231. Invalidate;
  232. end;
  233. end;
  234. procedure TCustomPathLabel.SetColors(Index: integer; Value: TColor);
  235. begin
  236. Assert(Index in [0..3]);
  237. if FColors[Index] <> Value then
  238. begin
  239. FColors[Index] := Value;
  240. CalculateAutoHotTrackColors;
  241. UpdateStatus;
  242. end;
  243. end; { SetColors }
  244. // taken from PngImageListEditor
  245. const
  246. WeightR: single = 0.764706;
  247. WeightG: single = 1.52941;
  248. WeightB: single = 0.254902;
  249. function ColorDistance(C1, C2: Integer): Single;
  250. var
  251. DR, DG, DB: Integer;
  252. begin
  253. DR := (C1 and $FF) - (C2 and $FF);
  254. Result := Sqr(DR * WeightR);
  255. DG := (C1 shr 8 and $FF) - (C2 shr 8 and $FF);
  256. Result := Result + Sqr(DG * WeightG);
  257. DB := (C1 shr 16) - (C2 shr 16);
  258. Result := Result + Sqr(DB * WeightB);
  259. Result := Sqrt(Result);
  260. end;
  261. function GetAdjustedThreshold(BkgndIntensity, Threshold: Single): Single;
  262. begin
  263. if BkgndIntensity < 220 then
  264. Result := (2 - BkgndIntensity / 220) * Threshold
  265. else
  266. Result := Threshold;
  267. end;
  268. function IsContrastEnough(AColor, ABkgndColor: Integer; DoAdjustThreshold: Boolean; Threshold: Single): Boolean;
  269. begin
  270. if DoAdjustThreshold then
  271. Threshold := GetAdjustedThreshold(ColorDistance(ABkgndColor, $000000),
  272. Threshold);
  273. Result := ColorDistance(ABkgndColor, AColor) > Threshold;
  274. end;
  275. procedure AdjustContrast(var AColor: Integer; ABkgndColor: Integer; Threshold: Single);
  276. var
  277. X, Y, Z: Single;
  278. R, G, B: Single;
  279. RR, GG, BB: Integer;
  280. I1, I2, S, Q, W: Single;
  281. DoInvert: Boolean;
  282. begin
  283. I1 := ColorDistance(AColor, $000000);
  284. I2 := ColorDistance(ABkgndColor, $000000);
  285. Threshold := GetAdjustedThreshold(I2, Threshold);
  286. if I1 > I2 then
  287. DoInvert := I2 < 442 - Threshold
  288. else
  289. DoInvert := I2 < Threshold;
  290. X := (ABkgndColor and $FF) * WeightR;
  291. Y := (ABkgndColor shr 8 and $FF) * WeightG;
  292. Z := (ABkgndColor shr 16) * WeightB;
  293. R := (AColor and $FF) * WeightR;
  294. G := (AColor shr 8 and $FF) * WeightG;
  295. B := (AColor shr 16) * WeightB;
  296. if DoInvert then begin
  297. R := 195 - R;
  298. G := 390 - G;
  299. B := 65 - B;
  300. X := 195 - X;
  301. Y := 390 - Y;
  302. Z := 65 - Z;
  303. end;
  304. S := Sqrt(Sqr(B) + Sqr(G) + Sqr(R));
  305. if S < 0.01 then
  306. S := 0.01;
  307. Q := (R * X + G * Y + B * Z) / S;
  308. X := Q / S * R - X;
  309. Y := Q / S * G - Y;
  310. Z := Q / S * B - Z;
  311. W := Sqrt(Sqr(Threshold) - Sqr(X) - Sqr(Y) - Sqr(Z));
  312. R := (Q - W) * R / S;
  313. G := (Q - W) * G / S;
  314. B := (Q - W) * B / S;
  315. if DoInvert then begin
  316. R := 195 - R;
  317. G := 390 - G;
  318. B := 65 - B;
  319. end;
  320. if R < 0 then
  321. R := 0
  322. else if R > 195 then
  323. R := 195;
  324. if G < 0 then
  325. G := 0
  326. else if G > 390 then
  327. G := 390;
  328. if B < 0 then
  329. B := 0
  330. else if B > 65 then
  331. B := 65;
  332. RR := Trunc(R * (1 / WeightR) + 0.5);
  333. GG := Trunc(G * (1 / WeightG) + 0.5);
  334. BB := Trunc(B * (1 / WeightB) + 0.5);
  335. if RR > $FF then
  336. RR := $FF
  337. else if RR < 0 then
  338. RR := 0;
  339. if GG > $FF then
  340. GG := $FF
  341. else if GG < 0 then
  342. GG := 0;
  343. if BB > $FF then
  344. BB := $FF
  345. else if BB < 0 then
  346. BB := 0;
  347. AColor := (BB and $FF) shl 16 or (GG and $FF) shl 8 or (RR and $FF);
  348. end;
  349. procedure SetContrast(var Color: TColor; BkgndColor: TColor; Threshold: Integer);
  350. var
  351. T: Single;
  352. begin
  353. if Color < 0 then
  354. Color := GetSysColor(Color and $FF);
  355. if BkgndColor < 0 then
  356. BkgndColor := GetSysColor(BkgndColor and $FF);
  357. T := Threshold;
  358. if not IsContrastEnough(Color, BkgndColor, True, T) then
  359. AdjustContrast(Integer(Color), BkgndColor, T);
  360. end;
  361. function TCustomPathLabel.CalculateAutoHotTrackColorComponent(C: Byte; Bright: Boolean): Byte;
  362. var
  363. Delta: Byte;
  364. begin
  365. Delta := Max(Round(C * 0.3), 80);
  366. if Bright then
  367. Result := Byte(Max(Integer(C) - Delta, 0))
  368. else
  369. Result := Byte(Min(C + Delta, 255));
  370. end;
  371. function TCustomPathLabel.CalculateAutoHotTrackColor(C: TColor): TColor;
  372. var
  373. R, G, B: Byte;
  374. Bright: Boolean;
  375. begin
  376. C := ColorToRGB(C);
  377. R := GetRValue(C);
  378. G := GetGValue(C);
  379. B := GetBValue(C);
  380. Bright := (R + G + B) > (256 / 2 * 3);
  381. R := CalculateAutoHotTrackColorComponent(R, Bright);
  382. G := CalculateAutoHotTrackColorComponent(G, Bright);
  383. B := CalculateAutoHotTrackColorComponent(B, Bright);
  384. Result := RGB(R, G, B);
  385. end;
  386. procedure TCustomPathLabel.CalculateAutoHotTrackColors;
  387. begin
  388. FColors[4] := CalculateAutoHotTrackColor(FColors[2]);
  389. SetContrast(FColors[4], FColors[0], 50);
  390. FColors[5] := CalculateAutoHotTrackColor(FColors[3]);
  391. SetContrast(FColors[5], FColors[1], 50);
  392. end;
  393. procedure TCustomPathLabel.SetIndentHorizontal(AIndent: Integer);
  394. begin
  395. if FIndentHorizontal <> AIndent then
  396. begin
  397. FIndentHorizontal := AIndent;
  398. AdjustBounds;
  399. Invalidate;
  400. end;
  401. end;
  402. procedure TCustomPathLabel.SetIndentVertical(AIndent: Integer);
  403. begin
  404. if FIndentVertical <> AIndent then
  405. begin
  406. FIndentVertical := AIndent;
  407. AdjustBounds;
  408. Invalidate;
  409. end;
  410. end;
  411. procedure TCustomPathLabel.SetAutoSizeVertical(Value: Boolean);
  412. begin
  413. if FAutoSizeVertical <> Value then
  414. begin
  415. FAutoSizeVertical := Value;
  416. AdjustBounds;
  417. Invalidate;
  418. end;
  419. end;
  420. function TCustomPathLabel.GetFocusControl: TWinControl;
  421. begin
  422. Result := inherited FocusControl;
  423. end;
  424. procedure TCustomPathLabel.SetFocusControl(Value: TWinControl);
  425. begin
  426. if FocusControl <> Value then
  427. begin
  428. inherited FocusControl := Value;
  429. UpdateStatus;
  430. end;
  431. end;
  432. procedure TCustomPathLabel.DoDrawTextIntern(var Rect: TRect; Flags: Longint; S: string);
  433. var
  434. i: Integer;
  435. Width: Integer;
  436. WidthMask: Integer;
  437. WidthPath: Integer;
  438. HotTrackOffset: Integer;
  439. HotTrackBottom: Integer;
  440. Separator: string;
  441. Str: string;
  442. HotTrackColor: TColor;
  443. VirtualMask: string;
  444. IsEmptyMask: Boolean;
  445. IsVirtualMask: Boolean;
  446. begin
  447. if (Flags and DT_CALCRECT <> 0) and ((S = '') or ShowAccelChar and
  448. (S[1] = '&') and (S[2] = #0)) then S := S + ' ';
  449. if not ShowAccelChar then Flags := Flags or DT_NOPREFIX;
  450. // have to apply DrawTextBiDiModeFlags if we ever deal with dibi
  451. Canvas.Font := Font;
  452. Width := (Rect.Right - Rect.Left);
  453. FDisplayPath := S;
  454. VirtualMask := Mask;
  455. IsVirtualMask := False;
  456. IsEmptyMask := (VirtualMask = '');
  457. if IsEmptyMask and HotTrack then
  458. begin
  459. VirtualMask := '*.*';
  460. IsVirtualMask := not FMouseInView;
  461. end;
  462. if VirtualMask <> '' then
  463. begin
  464. if (Length(FDisplayPath) > 0) and (FDisplayPath[Length(FDisplayPath)] <> GetSeparator) then
  465. Separator := GetSeparator;
  466. FDisplayPath := FDisplayPath + Separator;
  467. S := S + Separator;
  468. WidthMask := Canvas.TextWidth(VirtualMask);
  469. if WidthMask > Width div 3 then
  470. WidthPath := Width - (Width div 3)
  471. else
  472. WidthPath := Width - WidthMask;
  473. end
  474. else
  475. begin
  476. WidthMask := 0;
  477. WidthPath := Width;
  478. end;
  479. if FUnixPath then
  480. for i := 1 to Length(FDisplayPath) do
  481. begin
  482. case FDisplayPath[i] of
  483. '/': FDisplayPath[i] := '\';
  484. '\': FDisplayPath[i] := '/';
  485. end;
  486. end;
  487. FDisplayPath := MinimizeName(FDisplayPath, Canvas, WidthPath);
  488. if FUnixPath then
  489. for i := 1 to Length(FDisplayPath) do
  490. begin
  491. case FDisplayPath[i] of
  492. '\': FDisplayPath[i] := '/';
  493. '/': FDisplayPath[i] := '\';
  494. end;
  495. end;
  496. WidthPath := Canvas.TextWidth(FDisplayPath);
  497. if VirtualMask <> '' then
  498. begin
  499. if WidthMask > Width - WidthPath then
  500. begin
  501. VirtualMask := VirtualMask + '...';
  502. repeat
  503. Delete(VirtualMask, Length(VirtualMask) - 3, 1);
  504. WidthMask := Canvas.TextWidth(VirtualMask);
  505. until (WidthMask <= Width - WidthPath) or (Length(VirtualMask) = 3);
  506. end;
  507. end;
  508. Str := FDisplayPath;
  509. if not IsVirtualMask then
  510. begin
  511. Str := Str + VirtualMask;
  512. FDisplayMask := VirtualMask;
  513. end
  514. else
  515. begin
  516. FDisplayMask := '';
  517. end;
  518. ShowHint :=
  519. (FDisplayPath <> S) or
  520. ((not IsEmptyMask) and (FDisplayMask <> Mask)) or
  521. (WidthPath + WidthMask > Width);
  522. if not ShowHint then Hint := ''
  523. else Hint := S + Mask;
  524. if not FIsEnabled then
  525. begin
  526. Canvas.Font.Color := clBtnShadow;
  527. DrawText(Canvas.Handle, PChar(Str), Length(Str), Rect, Flags);
  528. end
  529. else
  530. begin
  531. FDisplayHotTrack := HotTrackPath;
  532. HotTrackColor := TColor(0); // shut up
  533. if FDisplayHotTrack <> '' then
  534. begin
  535. if TrackingActive then
  536. begin
  537. HotTrackColor := FColors[4 + Integer(FIsActive)]
  538. end
  539. else
  540. begin
  541. // We do not have a path label with hot-track and not tracking-active,
  542. // so this is untested branch
  543. Assert(False);
  544. // As if it were active
  545. HotTrackColor := FColors[2 + 1];
  546. end;
  547. end;
  548. if (FDisplayHotTrack <> '') and (FDisplayHotTrack <> HotTrackMask) then
  549. begin
  550. Canvas.Font.Color := HotTrackColor;
  551. DrawText(Canvas.Handle, PChar(FDisplayHotTrack), Length(FDisplayHotTrack), Rect, Flags);
  552. HotTrackOffset := Canvas.TextWidth(FDisplayHotTrack);
  553. Inc(Rect.Left, HotTrackOffset);
  554. Delete(Str, 1, Length(FDisplayHotTrack));
  555. HotTrackBottom := Rect.Bottom;
  556. end
  557. else
  558. begin
  559. HotTrackOffset := 0;
  560. HotTrackBottom := 0;
  561. end;
  562. if TrackingActive then
  563. Canvas.Font.Color := FColors[2 + Integer(FIsActive)]
  564. else
  565. Canvas.Font.Color := clWindowText;
  566. if FDisplayHotTrack = HotTrackMask then
  567. begin
  568. DrawText(Canvas.Handle, PChar(FDisplayPath), Length(FDisplayPath), Rect, Flags);
  569. HotTrackOffset := Canvas.TextWidth(FDisplayPath);
  570. HotTrackBottom := Rect.Bottom;
  571. Inc(Rect.Left, HotTrackOffset);
  572. Canvas.Font.Color := HotTrackColor;
  573. DrawText(Canvas.Handle, PChar(VirtualMask), Length(VirtualMask), Rect, Flags);
  574. end
  575. else
  576. begin
  577. DrawText(Canvas.Handle, PChar(Str), Length(Str), Rect, Flags);
  578. end;
  579. Dec(Rect.Left, HotTrackOffset);
  580. Rect.Bottom := Max(Rect.Bottom, HotTrackBottom);
  581. end;
  582. end;
  583. procedure TCustomPathLabel.DoDrawText(var Rect: TRect; Flags: Longint);
  584. begin
  585. DoDrawTextIntern(Rect, Flags, Caption);
  586. end;
  587. function TCustomPathLabel.GetSeparator: Char;
  588. begin
  589. if FUnixPath then Result := '/'
  590. else Result := '\';
  591. end;
  592. function TCustomPathLabel.HotTrackPath: string;
  593. var
  594. P: TPoint;
  595. DelimPos: Integer;
  596. Len: Integer;
  597. Path: string;
  598. begin
  599. Result := '';
  600. if UseHotTrack and FMouseInView and (FDisplayPath <> '') then
  601. begin
  602. P := ScreenToClient(Mouse.CursorPos);
  603. Len := P.X - FIndentHorizontal;
  604. if Len >= 0 then
  605. begin
  606. if Len < Canvas.TextWidth(FDisplayPath) then
  607. begin
  608. Result := '';
  609. Path := FDisplayPath;
  610. repeat
  611. Assert(Path <> '');
  612. DelimPos := Pos(GetSeparator, Path);
  613. if DelimPos > 0 then
  614. begin
  615. Result := Result + Copy(Path, 1, DelimPos);
  616. Delete(Path, 1, DelimPos);
  617. end
  618. else
  619. begin
  620. Result := Result + Path;
  621. Path := '';
  622. end;
  623. until (Canvas.TextWidth(Result) >= Len) or (Path = '');
  624. end
  625. else
  626. if Len < Canvas.TextWidth(FDisplayPath + FDisplayMask) then
  627. begin
  628. Result := HotTrackMask;
  629. end;
  630. end;
  631. end;
  632. end;
  633. function TCustomPathLabel.GetColors(Index: Integer): TColor;
  634. begin
  635. Assert(Index in [0..5]);
  636. Result := FColors[Index];
  637. end; { GetColors }
  638. function TCustomPathLabel.UseRightToLeftAlignment: Boolean;
  639. begin
  640. // Not sure how to properly deal with RTL atm.
  641. // See also a comment on DrawTextBiDiModeFlags in DoDrawTextIntern
  642. Result := False;
  643. end;
  644. procedure TCustomPathLabel.Paint;
  645. const
  646. Alignments: array[TAlignment] of Word = (DT_LEFT, DT_RIGHT, DT_CENTER);
  647. WordWraps: array[Boolean] of Word = (0, DT_WORDBREAK);
  648. var
  649. Rect, CalcRect: TRect;
  650. DrawStyle: Longint;
  651. begin
  652. with Canvas do
  653. begin
  654. if not Transparent then
  655. begin
  656. Brush.Color := Self.Color;
  657. Brush.Style := bsSolid;
  658. FillRect(ClientRect);
  659. end;
  660. Brush.Style := bsClear;
  661. Rect := ClientRect;
  662. // DoDrawText takes care of BiDi alignments
  663. DrawStyle := DT_EXPANDTABS or WordWraps[WordWrap] or Alignments[Alignment];
  664. // MP
  665. Rect.Left := Rect.Left + FIndentHorizontal;
  666. Rect.Right := Rect.Right - FIndentHorizontal;
  667. Rect.Top := Rect.Top + FIndentVertical;
  668. Rect.Bottom := Rect.Bottom - FIndentVertical;
  669. // Calculate vertical layout
  670. if Layout <> tlTop then
  671. begin
  672. CalcRect := Rect;
  673. DoDrawText(CalcRect, DrawStyle or DT_CALCRECT);
  674. if Layout = tlBottom then OffsetRect(Rect, 0, Height - CalcRect.Bottom)
  675. else OffsetRect(Rect, 0, (Height - CalcRect.Bottom) div 2);
  676. end;
  677. DoDrawText(Rect, DrawStyle);
  678. end;
  679. end;
  680. procedure TCustomPathLabel.AdjustBounds;
  681. const
  682. WordWraps: array[Boolean] of Word = (0, DT_WORDBREAK);
  683. var
  684. DC: HDC;
  685. X: Integer;
  686. Rect: TRect;
  687. AAlignment: TAlignment;
  688. AWidth: Integer;
  689. AHeight: Integer;
  690. S: string;
  691. begin
  692. if not (csReading in ComponentState) and
  693. (AutoSize or AutoSizeVertical) then
  694. begin
  695. Rect := ClientRect;
  696. DC := GetDC(0);
  697. Canvas.Handle := DC;
  698. S := Caption;
  699. if S = '' then S := 'SomeTextForSizing';
  700. DoDrawTextIntern(Rect, (DT_EXPANDTABS or DT_CALCRECT) or WordWraps[WordWrap], S);
  701. Canvas.Handle := 0;
  702. ReleaseDC(0, DC);
  703. X := Left;
  704. AAlignment := Alignment;
  705. if UseRightToLeftAlignment then ChangeBiDiModeAlignment(AAlignment);
  706. if AAlignment = taRightJustify then Inc(X, Width - 2*FIndentHorizontal + Rect.Right);
  707. if AutoSize then AWidth := 2*FIndentHorizontal + Rect.Right
  708. else AWidth := Width;
  709. if AutoSize or AutoSizeVertical then AHeight := 2*FIndentVertical + Rect.Bottom
  710. else AHeight := Height;
  711. SetBounds(X, Top, AWidth, AHeight);
  712. end;
  713. end;
  714. function TCustomPathLabel.IsActive: Boolean;
  715. begin
  716. if csDestroying in ComponentState then Result := False
  717. else
  718. begin
  719. Result := Assigned(FocusControl) and FocusControl.Focused;
  720. if Assigned(OnGetStatus) then
  721. OnGetStatus(Self, Result);
  722. end;
  723. end;
  724. function TCustomPathLabel.TrackingActive: Boolean;
  725. begin
  726. Result := Assigned(FocusControl) or Assigned(OnGetStatus);
  727. end;
  728. procedure TCustomPathLabel.UpdateStatus;
  729. var
  730. NewIsActive: Boolean;
  731. NewIsEnabled: Boolean;
  732. NewColor: TColor;
  733. begin
  734. if TrackingActive then
  735. begin
  736. NewIsActive := IsActive;
  737. NewIsEnabled :=
  738. Enabled and
  739. ((not Assigned(FocusControl)) or FocusControl.Enabled);
  740. NewColor := FColors[Integer(NewIsActive)];
  741. if (NewIsActive <> FIsActive) or
  742. (NewIsEnabled <> FIsEnabled) or
  743. (NewColor <> Color) then
  744. begin
  745. FIsActive := NewIsActive;
  746. FIsEnabled := NewIsEnabled;
  747. Color := NewColor;
  748. Invalidate;
  749. end;
  750. end
  751. else
  752. begin
  753. Color := clBtnFace;
  754. end;
  755. end; { UpdateStatus }
  756. procedure TCustomPathLabel.Notification(AComponent: TComponent;
  757. Operation: TOperation);
  758. var
  759. NeedUpdate: Boolean;
  760. begin
  761. NeedUpdate :=
  762. (Operation = opRemove) and (AComponent = FocusControl);
  763. inherited;
  764. if NeedUpdate then UpdateStatus;
  765. end; { Notification }
  766. function TCustomPathLabel.UseHotTrack: Boolean;
  767. begin
  768. Result := HotTrack and FIsEnabled;
  769. end;
  770. procedure TCustomPathLabel.MouseMove(Shift: TShiftState; X: Integer; Y: Integer);
  771. begin
  772. inherited;
  773. if FMouseInView and UseHotTrack and (FDisplayHotTrack <> HotTrackPath) then
  774. begin
  775. Invalidate;
  776. end;
  777. end;
  778. procedure TCustomPathLabel.DoPathClick(Path: string);
  779. begin
  780. if Assigned(OnPathClick) then
  781. OnPathClick(Self, Path);
  782. end;
  783. procedure TCustomPathLabel.DoMaskClick;
  784. begin
  785. if Assigned(OnMaskClick) then
  786. OnMaskClick(Self);
  787. end;
  788. procedure TCustomPathLabel.DblClick;
  789. begin
  790. if FIsEnabled then
  791. begin
  792. inherited;
  793. end;
  794. end;
  795. procedure TCustomPathLabel.DoContextPopup(MousePos: TPoint; var Handled: Boolean);
  796. begin
  797. if FIsEnabled then
  798. begin
  799. inherited;
  800. end
  801. else
  802. begin
  803. Handled := True;
  804. end;
  805. end;
  806. procedure TCustomPathLabel.CMMouseEnter(var Message: TMessage);
  807. begin
  808. inherited;
  809. FMouseInView := True;
  810. Invalidate;
  811. end;
  812. procedure TCustomPathLabel.CMMouseLeave(var Message: TMessage);
  813. begin
  814. FMouseInView := False;
  815. Invalidate;
  816. inherited;
  817. end;
  818. end.