ListViewColProperties.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. unit ListViewColProperties;
  2. interface
  3. uses
  4. Classes, ComCtrls, Contnrs;
  5. type
  6. TCustomListViewColProperty = class(TObject)
  7. Alignment: TAlignment;
  8. Caption: string;
  9. Width: Integer;
  10. MaxWidth: Integer;
  11. MinWidth: Integer;
  12. Visible: Boolean;
  13. Order: Integer;
  14. constructor Create(AOrder: Integer);
  15. end;
  16. type
  17. TCustomListViewColProperties = class(TPersistent)
  18. private
  19. FChanged: Boolean;
  20. FOnChange: TNotifyEvent;
  21. FUpdating: Integer;
  22. FProperties: TObjectList;
  23. FCreated: Boolean;
  24. function GetColumns: TListColumns;
  25. function GetCount: Integer;
  26. function GetOrderStr: string;
  27. procedure CheckBounds(Index: Integer);
  28. procedure SetWidthsStr(Value: string; PixelsPerInch: Integer);
  29. function GetWidthsStr: string;
  30. procedure SetOrderStr(Value: string);
  31. protected
  32. FListView: TCustomListView;
  33. FListViewManaged: Boolean;
  34. FConstraintsInitialized: Boolean;
  35. function GetAlignments(Index: Integer): TAlignment;
  36. function GetParamsStr: string; virtual;
  37. function GetVisible(Index: Integer): Boolean;
  38. function GetWidths(Index: Integer): Integer;
  39. procedure SetAlignments(Index: Integer; Value: TAlignment);
  40. procedure SetVisibleInternal(Index: Integer; Value: Boolean; SaveWidth: Boolean);
  41. procedure SetVisible(Index: Integer; Value: Boolean);
  42. procedure SetWidths(Index: Integer; Value: Integer);
  43. function GetCaptions(Index: Integer): string;
  44. procedure Changed; virtual;
  45. procedure SetCaptions(Index: Integer; Value: string); virtual;
  46. procedure SetParamsStr(Value: string); virtual;
  47. procedure UpdateListView;
  48. procedure UpdateFromListView;
  49. procedure UpdateOrderFromListView;
  50. procedure UpdateListViewOrder;
  51. function GetProperties(Index: Integer): TCustomListViewColProperty;
  52. function GetIndexByOrder(Order: Integer): Integer;
  53. function ColumnsExists: Boolean;
  54. procedure SetRuntimeVisible(Index: Integer; Value: Boolean; SaveWidth: Boolean);
  55. function GetColumn(Index: Integer): TListColumn;
  56. procedure CreateProperties(ACount: Integer);
  57. function DefaultConstraint(Value: Integer; Visible: Boolean; Def: Integer): Integer;
  58. property Columns: TListColumns read GetColumns stored False;
  59. public
  60. constructor Create(ListView: TCustomListView; ColCount: Integer);
  61. destructor Destroy; override;
  62. procedure EndUpdate;
  63. procedure BeginUpdate;
  64. procedure ListViewWndCreated;
  65. procedure ListViewWndDestroying;
  66. procedure ListViewWndDestroyed;
  67. procedure ChangeScale(M, D: Integer);
  68. property Count: Integer read GetCount stored False;
  69. property Alignments[Index: Integer]: TAlignment read GetAlignments write SetAlignments;
  70. property Captions[Index: Integer]: string read GetCaptions write SetCaptions;
  71. property Widths[Index: Integer]: Integer read GetWidths write SetWidths;
  72. property Visible[Index: Integer]: Boolean read GetVisible write SetVisible;
  73. procedure RecreateColumns;
  74. property OnChange: TNotifyEvent read FOnChange write FOnChange;
  75. property ParamsStr: string read GetParamsStr write SetParamsStr stored False;
  76. end; { TCustomListViewColProperties }
  77. type
  78. TListViewColProperties = class(TCustomListViewColProperties)
  79. published
  80. end; { TListViewColProperties }
  81. implementation
  82. uses
  83. SysUtils, CommCtrl, Windows, PasTools, Controls, Forms;
  84. const
  85. DefaultListViewMaxWidth = 1000;
  86. DefaultListViewMinWidth = 20;
  87. { TODO : V ListView zamezit zmenu velikosti neviditelnych sloupecku }
  88. constructor TCustomListViewColProperty.Create(AOrder: Integer);
  89. begin
  90. Alignment := taLeftJustify;
  91. Caption := '';
  92. Width := 50;
  93. Visible := True;
  94. Order := AOrder;
  95. end;
  96. { TCustomListViewColProperties }
  97. constructor TCustomListViewColProperties.Create(
  98. ListView: TCustomListView; ColCount: Integer);
  99. var
  100. ACount: Integer;
  101. begin
  102. // This contructor (and constructors of descendants)
  103. // is only even called from implementations of
  104. // TCustomNortonLikeListView.NewColProperties
  105. inherited Create;
  106. FConstraintsInitialized := False;
  107. FCreated := False;
  108. FUpdating := 0;
  109. FChanged := False;
  110. // ColCount is not 0 for file panels (TDirView and TCustomUnixDirView).
  111. // It is 0 otherwise.
  112. FListViewManaged := (ColCount = 0);
  113. FListView := ListView;
  114. FProperties := TObjectList.Create;
  115. if FListViewManaged then ACount := GetColumns.Count
  116. else ACount := ColCount;
  117. CreateProperties(ACount);
  118. if not Assigned(FListView) then
  119. raise Exception.Create('NIL ListView pointer.');
  120. end;
  121. destructor TCustomListViewColProperties.Destroy;
  122. begin
  123. inherited;
  124. FProperties.Free;
  125. end;
  126. procedure TCustomListViewColProperties.SetWidthsStr(Value: string; PixelsPerInch: Integer);
  127. var
  128. ColStr: string;
  129. Index: Integer;
  130. NeedInvalidate, NewVisible: Boolean;
  131. NewWidth: Integer;
  132. begin
  133. Index := 0;
  134. NeedInvalidate := False;
  135. BeginUpdate;
  136. try
  137. while (Value <> '') and (Index < Count) do
  138. begin
  139. ColStr := CutToChar(Value, ';', True);
  140. NewWidth := LoadDimension(StrToInt(CutToChar(ColStr, ',', True)), PixelsPerInch, FListView);
  141. Widths[Index] := NewWidth;
  142. NewVisible := Boolean(StrToInt(CutToChar(ColStr, ',', True)));
  143. if Visible[Index] <> NewVisible then
  144. begin
  145. // As we are within BeginUpdate guard, the width set just above is not propadated to WinAPI.
  146. // As reading width retrieves it from WinAPI (if the control is already allocated), we actually save wrong width.
  147. SetVisibleInternal(Index, NewVisible, False);
  148. NeedInvalidate := True;
  149. end;
  150. Inc(Index);
  151. end;
  152. finally
  153. EndUpdate;
  154. end;
  155. // When visibility changes (particularly while reseting layout) redraw is needed
  156. // (Invalidate is called in SetVisible too, but maybe it has no effect there, because it is within BeginUpdate/EndUpdate)
  157. if NeedInvalidate and FListView.HandleAllocated then
  158. FListView.Invalidate;
  159. end;
  160. function TCustomListViewColProperties.GetWidthsStr: string;
  161. var
  162. Index: Integer;
  163. begin
  164. Result := '';
  165. for Index := 0 to Count-1 do
  166. begin
  167. Result := Format('%s;%d,%d', [Result, SaveDimension(Widths[Index]), Integer(Visible[Index])]);
  168. end;
  169. Delete(Result, 1, 1);
  170. end;
  171. procedure TCustomListViewColProperties.BeginUpdate;
  172. begin
  173. Columns.BeginUpdate;
  174. Inc(FUpdating);
  175. end;
  176. procedure TCustomListViewColProperties.EndUpdate;
  177. begin
  178. Columns.EndUpdate;
  179. Dec(FUpdating);
  180. if FUpdating = 0 then
  181. begin
  182. // call Changed() even when FChange is false
  183. Changed;
  184. FChanged := False;
  185. end;
  186. end;
  187. procedure TCustomListViewColProperties.Changed;
  188. begin
  189. if FUpdating > 0 then FChanged := True
  190. else
  191. if Assigned(FOnChange) then FOnChange(Self);
  192. end;
  193. procedure TCustomListViewColProperties.CheckBounds(Index: Integer);
  194. begin
  195. if (Index < 0) or (Index >= Count) then
  196. raise Exception.Create('Index out of bounds.');
  197. end;
  198. function TCustomListViewColProperties.GetProperties(Index: Integer): TCustomListViewColProperty;
  199. begin
  200. Result := TCustomListViewColProperty(FProperties.Items[Index]);
  201. end;
  202. function TCustomListViewColProperties.GetIndexByOrder(Order: Integer): Integer;
  203. var
  204. I: Integer;
  205. begin
  206. for I := 0 to Count - 1 do
  207. begin
  208. if GetProperties(I).Order = Order then
  209. begin
  210. Result := I;
  211. Exit;
  212. end;
  213. end;
  214. raise Exception.Create('Column order out of bounds');
  215. end;
  216. function TCustomListViewColProperties.ColumnsExists: Boolean;
  217. begin
  218. Result := FListView.HandleAllocated;
  219. if Result and (not FCreated) and (not FListViewManaged) then
  220. UpdateListView;
  221. end;
  222. procedure TCustomListViewColProperties.SetAlignments(Index: Integer; Value: TAlignment);
  223. begin
  224. CheckBounds(Index);
  225. if Alignments[Index] <> Value then
  226. begin
  227. GetProperties(Index).Alignment := Value;
  228. if ColumnsExists then GetColumn(Index).Alignment := Value;
  229. Changed;
  230. end;
  231. end;
  232. procedure TCustomListViewColProperties.SetCaptions(Index: Integer; Value: string);
  233. begin
  234. CheckBounds(Index);
  235. if Captions[Index] <> Value then
  236. begin
  237. if ColumnsExists then GetColumn(Index).Caption := Value
  238. else GetProperties(Index).Caption := Value;
  239. Changed;
  240. end;
  241. end;
  242. function TCustomListViewColProperties.GetAlignments(Index: Integer): TAlignment;
  243. begin
  244. CheckBounds(Index);
  245. if ColumnsExists then Result := GetColumn(Index).Alignment
  246. else Result := GetProperties(Index).Alignment;
  247. end;
  248. function TCustomListViewColProperties.GetCaptions(Index: Integer): string;
  249. begin
  250. CheckBounds(Index);
  251. if ColumnsExists then Result := GetColumn(Index).Caption
  252. else Result := GetProperties(Index).Caption;
  253. end;
  254. procedure TCustomListViewColProperties.SetOrderStr(Value: string);
  255. var
  256. Order, Index: Integer;
  257. Properties: TCustomListViewColProperty;
  258. STemp: string;
  259. Phase: Boolean;
  260. begin
  261. BeginUpdate;
  262. try
  263. for Index := 0 to Count - 1 do
  264. GetProperties(Index).Order := -1;
  265. // First order invisible columns (not True), then visible (not not True)
  266. Phase := True;
  267. Order := 0;
  268. repeat
  269. Phase := not Phase;
  270. STemp := Value;
  271. while (STemp <> '') and (Order < Count) do
  272. begin
  273. Index := StrToInt(CutToChar(STemp, ';', True));
  274. Properties := GetProperties(Index);
  275. if (Properties.Visible = Phase) and
  276. (Properties.Order < 0) { robustness } then
  277. begin
  278. Properties.Order := Order;
  279. Inc(Order);
  280. end;
  281. end;
  282. // add missing columns from the same visibility class
  283. for Index := 0 to Count - 1 do
  284. begin
  285. Properties := GetProperties(Index);
  286. if (Properties.Visible = Phase) and
  287. (Properties.Order < 0) then
  288. begin
  289. Properties.Order := Order;
  290. Inc(Order);
  291. end;
  292. end;
  293. until Phase;
  294. if ColumnsExists then
  295. UpdateListViewOrder;
  296. finally
  297. EndUpdate;
  298. end;
  299. end;
  300. procedure TCustomListViewColProperties.SetParamsStr(Value: string);
  301. var
  302. S: string;
  303. WidthsStr: string;
  304. OrderStr: string;
  305. PixelsPerInch: Integer;
  306. begin
  307. // TFileFindDialog uses / as separator of its settings
  308. S := CutToChar(Value, '|', True);
  309. WidthsStr := CutToChar(S, '@', True);
  310. PixelsPerInch := LoadPixelsPerInch(S, FListView);
  311. SetWidthsStr(WidthsStr, PixelsPerInch);
  312. // Have to set order after visibility, otherwise we lost ordering of columns that are invisible by default,
  313. // but visible by configuration (as they would get ordered to the front)
  314. OrderStr := CutToChar(Value, '|', True);
  315. SetOrderStr(OrderStr);
  316. end;
  317. procedure TCustomListViewColProperties.SetVisibleInternal(Index: Integer; Value: Boolean; SaveWidth: Boolean);
  318. var
  319. I: Integer;
  320. Properties: TCustomListViewColProperty;
  321. begin
  322. CheckBounds(Index);
  323. if Visible[Index] <> Value then
  324. begin
  325. Properties := GetProperties(Index);
  326. if ColumnsExists then
  327. UpdateOrderFromListView;
  328. if Value then
  329. begin
  330. // shown column is moved to the back
  331. for I := 0 to Count - 1 do
  332. begin
  333. if GetProperties(I).Order > Properties.Order then
  334. Dec(GetProperties(I).Order);
  335. end;
  336. Properties.Order := Count - 1;
  337. if ColumnsExists then
  338. UpdateListViewOrder;
  339. // show only after reordering column
  340. Properties.Visible := True;
  341. if ColumnsExists then
  342. SetRuntimeVisible(Index, True, SaveWidth);
  343. end
  344. else
  345. begin
  346. // hide before reordering column
  347. Properties.Visible := False;
  348. if ColumnsExists then
  349. SetRuntimeVisible(Index, False, SaveWidth);
  350. // hidden column is moved to the front,
  351. // unless column to the left is not hidden already
  352. // (or unless it is first already, in which case the
  353. // condition in the loop is never satisfied)
  354. if (Properties.Order > 0) and
  355. GetProperties(GetIndexByOrder(Properties.Order - 1)).Visible then
  356. begin
  357. for I := 0 to Count - 1 do
  358. begin
  359. if GetProperties(I).Order < Properties.Order then
  360. Inc(GetProperties(I).Order);
  361. end;
  362. Properties.Order := 0;
  363. end;
  364. if ColumnsExists then
  365. UpdateListViewOrder;
  366. end;
  367. Changed;
  368. // It does not refresh itself at last one on Win7 when DoubleBuffered
  369. if FListView.HandleAllocated then
  370. FListView.Invalidate;
  371. end;
  372. end;
  373. procedure TCustomListViewColProperties.SetVisible(Index: Integer; Value: Boolean);
  374. begin
  375. SetVisibleInternal(Index, Value, True);
  376. end;
  377. procedure TCustomListViewColProperties.SetRuntimeVisible(
  378. Index: Integer; Value: Boolean; SaveWidth: Boolean);
  379. var
  380. Properties: TCustomListViewColProperty;
  381. begin
  382. // This is probably only ever called from file panels (DirViews)
  383. // as other uses ("sychronization checklist" and "file find")
  384. // have FListViewManaged = False and never change Visible property
  385. // (though user can hide some columns manually in configuration storage)
  386. with GetColumn(Index) do
  387. begin
  388. Properties := GetProperties(Index);
  389. if Value then
  390. begin
  391. MaxWidth := Properties.MaxWidth;
  392. MinWidth := Properties.MinWidth;
  393. Width := Properties.Width;
  394. end
  395. else
  396. begin
  397. if SaveWidth then
  398. Properties.Width := Width;
  399. MaxWidth := 1;
  400. MinWidth := 0;
  401. Width := 0
  402. end;
  403. end;
  404. end;
  405. procedure TCustomListViewColProperties.SetWidths(Index: Integer; Value: Integer);
  406. var
  407. Properties: TCustomListViewColProperty;
  408. begin
  409. CheckBounds(Index);
  410. Properties := GetProperties(Index);
  411. if (Properties.MinWidth > 0) and (Value < Properties.MinWidth) then
  412. begin
  413. Value := Properties.MinWidth;
  414. end
  415. else
  416. if (Properties.MaxWidth > 0) and (Value > Properties.MaxWidth) then
  417. begin
  418. Value := Properties.MaxWidth;
  419. end;
  420. if Widths[Index] <> Value then
  421. begin
  422. Properties.Width := Value;
  423. if ColumnsExists and Visible[Index] then GetColumn(Index).Width := Value;
  424. Changed;
  425. end;
  426. end;
  427. function TCustomListViewColProperties.GetColumns: TListColumns;
  428. begin
  429. Result := TListView(FListView).Columns;
  430. end;
  431. function TCustomListViewColProperties.GetColumn(Index: Integer): TListColumn;
  432. begin
  433. Result := Columns[Index];
  434. end;
  435. function TCustomListViewColProperties.GetCount: Integer;
  436. begin
  437. Result := FProperties.Count;
  438. end;
  439. function TCustomListViewColProperties.GetOrderStr: string;
  440. var
  441. Index: Integer;
  442. begin
  443. Result := '';
  444. if ColumnsExists then
  445. UpdateOrderFromListView;
  446. for Index := 0 to Count - 1 do
  447. Result := Format('%s;%d', [Result, GetIndexByOrder(Index)]);
  448. Delete(Result, 1, 1);
  449. end;
  450. function TCustomListViewColProperties.GetParamsStr: string;
  451. begin
  452. // WORKAROUND
  453. // Adding an additional semicolon after the list,
  454. // to ensure that old versions that did not expect the pixels-per-inch part,
  455. // stop at the semicolon, otherwise they try to parse the
  456. // "last-column-width|pixels-per-inch" as integer and throw.
  457. // For the other instance of this hack, see GetListViewStr.
  458. // The new pixels-per-inch part is inserted after the widths part
  459. // as parsing of this was always robust to stop at "count" elements,
  460. // what order part was not (due to its logic of skipping hidden columns)
  461. Result := Format('%s;@%s|%s', [GetWidthsStr, SavePixelsPerInch(FListView), GetOrderStr]);
  462. end;
  463. function TCustomListViewColProperties.GetVisible(Index: Integer): Boolean;
  464. begin
  465. CheckBounds(Index);
  466. Result := GetProperties(Index).Visible;
  467. end;
  468. function TCustomListViewColProperties.GetWidths(Index: Integer): Integer;
  469. begin
  470. CheckBounds(Index);
  471. if ColumnsExists and Visible[Index] then
  472. begin
  473. Result := GetColumn(Index).Width;
  474. end
  475. else
  476. begin
  477. Result := GetProperties(Index).Width;
  478. end;
  479. end;
  480. procedure TCustomListViewColProperties.RecreateColumns;
  481. var
  482. Copy: TListColumns;
  483. begin
  484. Copy := TListColumns.Create(nil);
  485. try
  486. Copy.Assign(Columns);
  487. Columns.Assign(Copy);
  488. finally
  489. Copy.Free;
  490. end;
  491. end;
  492. procedure TCustomListViewColProperties.CreateProperties(ACount: Integer);
  493. var
  494. Index: Integer;
  495. Properties: TCustomListViewColProperty;
  496. begin
  497. for Index := 0 to ACount - 1 do
  498. begin
  499. Properties := TCustomListViewColProperty.Create(Index);
  500. FProperties.Add(Properties);
  501. end;
  502. end;
  503. function TCustomListViewColProperties.DefaultConstraint(Value: Integer; Visible: Boolean; Def: Integer): Integer;
  504. begin
  505. if (Value > 0) and Visible then Result := Value
  506. else Result := Def;
  507. end;
  508. procedure TCustomListViewColProperties.ListViewWndCreated;
  509. var
  510. Index: Integer;
  511. Properties: TCustomListViewColProperty;
  512. Column: TListColumn;
  513. begin
  514. if FListViewManaged then
  515. begin
  516. if (FProperties.Count = 0) and (Columns.Count > 0) then
  517. CreateProperties(Columns.Count);
  518. UpdateFromListView;
  519. end
  520. else
  521. begin
  522. UpdateListView;
  523. end;
  524. Assert(ColumnsExists);
  525. for Index := 0 to Count - 1 do
  526. begin
  527. Column := GetColumn(Index);
  528. Properties := GetProperties(Index);
  529. if not FConstraintsInitialized then
  530. begin
  531. Properties.MaxWidth := DefaultConstraint(Column.MaxWidth, Properties.Visible, DefaultListViewMaxWidth);
  532. Properties.MinWidth := DefaultConstraint(Column.MinWidth, Properties.Visible, DefaultListViewMinWidth);
  533. end;
  534. // To apply the default constraints to columns that do not have their own
  535. if Properties.Visible then
  536. begin
  537. Column.MaxWidth := Properties.MaxWidth;
  538. if Column.Width > Column.MaxWidth then Column.Width := Column.MaxWidth;
  539. Column.MinWidth := Properties.MinWidth;
  540. if Column.Width < Column.MinWidth then Column.Width := Column.MinWidth;
  541. end;
  542. end;
  543. FConstraintsInitialized := True;
  544. end;
  545. procedure TCustomListViewColProperties.ListViewWndDestroying;
  546. begin
  547. UpdateFromListView;
  548. end;
  549. procedure TCustomListViewColProperties.ListViewWndDestroyed;
  550. begin
  551. if not FListViewManaged then
  552. FCreated := False;
  553. end;
  554. procedure TCustomListViewColProperties.ChangeScale(M, D: Integer);
  555. var
  556. Index: Integer;
  557. Properties: TCustomListViewColProperty;
  558. Column: TListColumn;
  559. begin
  560. for Index := 0 to Count - 1 do
  561. begin
  562. Properties := GetProperties(Index);
  563. // This is not perfect, as the constraints apply on re-scaled width before they are re-scaled themselves
  564. if Properties.Visible and ColumnsExists then
  565. begin
  566. Column := GetColumn(Index);
  567. Column.MaxWidth := MulDiv(Column.MaxWidth, M, D);
  568. Column.MinWidth := MulDiv(Column.MinWidth, M, D);
  569. end
  570. else
  571. begin
  572. Properties.MaxWidth := MulDiv(Properties.MaxWidth, M, D);
  573. Properties.MinWidth := MulDiv(Properties.MinWidth, M, D);
  574. end;
  575. end;
  576. end;
  577. procedure TCustomListViewColProperties.UpdateListViewOrder;
  578. var
  579. Index: Integer;
  580. Properties: TCustomListViewColProperty;
  581. Temp: array of Integer;
  582. begin
  583. SetLength(Temp, Count);
  584. // Seemingly useless,
  585. // but probably only because we swallow HDN_ENDDRAG in TCustomIEListView.WMNotify,
  586. // what prevents VLC from actually reordering columns collection
  587. ListView_GetColumnOrderArray(FListView.Handle, Count, PInteger(Temp));
  588. for Index := 0 to Count - 1 do
  589. begin
  590. Properties := GetProperties(Index);
  591. Temp[Properties.Order] := Index;
  592. end;
  593. ListView_SetColumnOrderArray(FListView.Handle, Count, PInteger(Temp));
  594. end;
  595. procedure TCustomListViewColProperties.UpdateListView;
  596. var
  597. Index: Integer;
  598. Column: TListColumn;
  599. Properties: TCustomListViewColProperty;
  600. begin
  601. // Only called when FListViewManaged = False
  602. BeginUpdate;
  603. try
  604. for Index := 0 to Count-1 do
  605. begin
  606. if Index < Columns.Count then
  607. Column := GetColumn(Index)
  608. else
  609. Column := Columns.Add;
  610. Properties := GetProperties(Index);
  611. Column.Alignment := Properties.Alignment;
  612. Column.Caption := Properties.Caption;
  613. SetRuntimeVisible(Index, Properties.Visible, False);
  614. end;
  615. UpdateListViewOrder;
  616. finally
  617. FCreated := True;
  618. EndUpdate;
  619. end;
  620. end;
  621. procedure TCustomListViewColProperties.UpdateOrderFromListView;
  622. var
  623. Index: Integer;
  624. Temp: array of Integer;
  625. begin
  626. SetLength(Temp, Count);
  627. ListView_GetColumnOrderArray(FListView.Handle, Count, PInteger(Temp));
  628. for Index := 0 to Count - 1 do
  629. begin
  630. GetProperties(Temp[Index]).Order := Index;
  631. end;
  632. end;
  633. procedure TCustomListViewColProperties.UpdateFromListView;
  634. var
  635. Index: Integer;
  636. Column: TListColumn;
  637. Properties: TCustomListViewColProperty;
  638. begin
  639. Assert(FProperties.Count = Columns.Count);
  640. for Index := 0 to Count-1 do
  641. begin
  642. Column := GetColumn(Index);
  643. Properties := GetProperties(Index);
  644. Properties.Alignment := Column.Alignment;
  645. Properties.Caption := Column.Caption;
  646. if Properties.Visible then
  647. begin
  648. Properties.Width := Column.Width;
  649. if Column.MaxWidth > 0 then
  650. Properties.MaxWidth := Column.MaxWidth;
  651. if Column.MinWidth > 0 then
  652. Properties.MinWidth := Column.MinWidth;
  653. end;
  654. end;
  655. UpdateOrderFromListView;
  656. end;
  657. end.