Recent

Author Topic: Small Q (no issue): ListView inplace editor bounds and selection  (Read 1412 times)

d7_2_laz

  • Hero Member
  • *****
  • Posts: 657
(On Windows x64, Lazarus 2.0.12):
It's only aesthetics, but i'm wondering if it's possible to make the inplace editor for ListView to appear just like as in Delphi: the bounds of the inplace editor field are autosized here to the Length of the text. In Laz. it's determined by the boundaries of the column cell; the edit field does fill the whole cell.
TreeVuew EditNode similar.

I guess it's not done by the API, as for example such has no effect:
Code: Pascal  [Select][+][-]
  1. hEdit := ListView_GetEditControl(Handle);
  2. SendMessage(hEdit, EM_SETMARGINS, 1,MAKELONG(15,0));
  3. PostMessage(hEdit, EM_SETSEL, 0, myLength);  // defiine lenght of the selection marker

Not a big thing, but maybe somebody does know if it's possible to influence that.
Lazarus 4.4  FPC 3.2.2 Win10 64bit

d7_2_laz

  • Hero Member
  • *****
  • Posts: 657
Re: Small Q (no issue): ListView inplace editor bounds and selection
« Reply #1 on: April 26, 2021, 12:57:04 pm »
I add a screenshot about what i would like to achieve (behavious similar to windows explorer)
Lazarus 4.4  FPC 3.2.2 Win10 64bit

ASerge

  • Hero Member
  • *****
  • Posts: 2477
Re: Small Q (no issue): ListView inplace editor bounds and selection
« Reply #2 on: April 27, 2021, 04:31:33 am »
the bounds of the inplace editor field are autosized here to the Length of the text
If you really want to, you can take a chance:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, ComCtrls;
  9.  
  10. type
  11.   TForm1 = class(TForm)
  12.     ListView1: TListView;
  13.     procedure FormCreate(Sender: TObject);
  14.   end;
  15.  
  16. var
  17.   Form1: TForm1;
  18.  
  19. implementation
  20.  
  21. {$R *.lfm}
  22.  
  23. uses LMessages;
  24.  
  25. type
  26.   TMyListViewEditor = class(TCustomListViewEditor)
  27.   protected
  28.     procedure WMSetFocus(var Message: TLMSetFocus); message LM_SETFOCUS;
  29.   end;
  30.  
  31.   TOpenListView = class helper for TListView
  32.     function GetEditor: TCustomListViewEditor;
  33.   end;
  34.  
  35.   TCustomListViewEmulator = class(TWinControl)
  36.   private
  37.     FEditor: TCustomListViewEditor;
  38.   end;
  39.  
  40. // Caution: no check
  41. // Use only for descendants of Instance class without new members
  42. procedure ChangeClassForInstance(Instance: TObject; NewClass: TClass);
  43. begin
  44.   TClass(Pointer(Instance)^) := NewClass;
  45. end;
  46.  
  47. procedure TForm1.FormCreate(Sender: TObject);
  48. begin
  49.   ChangeClassForInstance(ListView1.GetEditor, TMyListViewEditor);
  50. end;
  51.  
  52. function TOpenListView.GetEditor: TCustomListViewEditor;
  53. begin
  54.   {$WARNING Very unsafe code}
  55.   // Two unsafe assumes
  56.   // 1. FEditor is the first field in TCustomListView (TListView)
  57.   // 2. The compiler arranges the fields in the order of declaration
  58.   Result := TCustomListViewEmulator(Pointer(Self)).FEditor;
  59. end;
  60.  
  61. procedure TMyListViewEditor.WMSetFocus(var Message: TLMSetFocus);
  62. const
  63.   CExtensionForWidth = 6;
  64. var
  65.   W: Integer;
  66. begin
  67.   W := (Owner as TListView).Canvas.TextWidth(Text);
  68.   if Width > W + CExtensionForWidth then
  69.     Width := W + CExtensionForWidth;
  70.   inherited;
  71. end;
  72.  
  73. end.

d7_2_laz

  • Hero Member
  • *****
  • Posts: 657
Re: Small Q (no issue): ListView inplace editor bounds and selection
« Reply #3 on: April 27, 2021, 11:16:41 am »
Hello ASerge, thank you!
Applied the helper functions to a custom listview and indeed it works, but (my agreement here) i doubt if it's worthy to go such a hacky bridge.

Would it be (complementary or alternatively) cost such an effort too to simply restrict the initial selecion of an edit field content to a portion of it?
Like in the screenshot: mark only a file name as selected, not it's extenson?
As an SendMessage EM_SETSEL (with a lenght parameter) won't work here.
An approach for that would be simplier or would it be based on the same technique?
Lazarus 4.4  FPC 3.2.2 Win10 64bit

d7_2_laz

  • Hero Member
  • *****
  • Posts: 657
Re: Small Q (no issue): ListView inplace editor bounds and selection
« Reply #4 on: April 27, 2021, 03:23:01 pm »
Just for interest:
Within the TMyListViewEditor.WMSetFocus it would work:
...
PostMessage(Handle, EM_SETSEL, 0, 9);   // Example only

(of course nobody wants to embed business logic within TMyListViewEditor.WMSetFocus. But by principle it's possible though)
Lazarus 4.4  FPC 3.2.2 Win10 64bit

d7_2_laz

  • Hero Member
  • *****
  • Posts: 657
Re: Small Q (no issue): ListView inplace editor bounds and selection
« Reply #5 on: April 27, 2021, 04:45:12 pm »
Better to do:
within TMyCustomListView.Editing something lke:
...
  hEdit := (TListView(Self)).GetEditor.Handle;
  PostMessage(hEdit, EM_SETSEL, 0, ln);

Will work
Lazarus 4.4  FPC 3.2.2 Win10 64bit

 

TinyPortal © 2005-2018