1
0

PathLabel.pas 27 KB

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