Difference between revisions of "ATListbox"

From Free Pascal wiki
Jump to navigationJump to search
 
(13 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
= About =
 
= About =
  
ATListbox is OS-independant listbox control. It's made for dialog like Sublime Text's command list. Fully owner-drawn: you must not set captions, but must add OnDrawItem event, and set ItemCount, ItemIndex.
+
ATListbox is OS-independant listbox control. It's made for dialog like Sublime Text's command list. If OnDrawItem is set, listbox is fully owner-drawn: you must not set items, just set ItemCount, ItemIndex.
  
Listbox don't get focus by click; to get focus on click set CanGetFocus=true (if focused, it handles keys Up/Down/PgUp/PgDn/Home/End, else you need to handle OnKeyDown in form and set listbox's ItemIndex by hand, which is boring).  
+
Listbox doesn't get focus by click; to get focus on click set CanGetFocus=true (if focused, it handles keys Up/Down/PgUp/PgDn/Home/End, else you need to handle OnKeyDown in form and set listbox's ItemIndex by hand, which is boring).  
  
 
Author: Alexey Torgashin
 
Author: Alexey Torgashin
Line 13: Line 13:
 
= Download =
 
= Download =
  
Homepage at github: https://github.com/Alexey-T/ATButtons
+
Homepage at github: https://github.com/Alexey-T/ATFlatControls
  
 
= License =
 
= License =
  
 
MPL 2.0 or LGPL.
 
MPL 2.0 or LGPL.
 
= Requirements =
 
 
Lazarus: 1.4.0.
 
 
Tested on: Linux gtk2 (Ubuntu 14.04), Win32 (Win7), QT.
 
  
 
= Properties =
 
= Properties =
  
* ItemCount: number of items
+
* Items: StringList to store items, or you can ignore it if you use OnDrawItem event (listbox will be fully virtual)
 +
* ItemCount: count of items
 
* ItemIndex: index of active item
 
* ItemIndex: index of active item
 
* ItemTop: index of item visible at the top
 
* ItemTop: index of item visible at the top
* ItemHeight: height in pixels of each item
+
* ItemHeight: height (in pixels) of each item
 
* VisibleItems: count of items fully visible inside control
 
* VisibleItems: count of items fully visible inside control
* CanGetFocus: set to True to allow control to be focused on click
+
* CanGetFocus: allow control to be focused on click
 +
 
 +
* ThemedScrollbar: allows control to use [[ATScrollBar]] instead of default OS scrollbar
 +
* ThemedColors: not used (made to use in CudaText app)
  
 
Events
 
Events
  
* OnDrawItem
 
 
* OnClick
 
* OnClick
 +
* OnDblClick
 +
* OnDrawItem: allows to owner-draw all items
 +
* OnChangedSel: fired on changed ItemIndex
  
 
= Example =
 
= Example =
Line 43: Line 43:
 
Let's create list:
 
Let's create list:
  
<syntaxhighlight>
+
<syntaxhighlight lang="pascal">
 +
uses Math, ATListbox; //package atflatcontrols_package.lpk
 +
 
 
var
 
var
 
   b: TATListbox;
 
   b: TATListbox;
Line 54: Line 56:
  
 
   b.OnDrawItem:= @ListDraw;
 
   b.OnDrawItem:= @ListDraw;
   b.OnClick:= @ListClick;
+
   b.OnClick:= @ListClick;   //see example in github https://github.com/Alexey-T/ATFlatControls/blob/master/app/demo_listbox/unit1.pas
  
 
   b.Color:= $e0e0e0;
 
   b.Color:= $e0e0e0;
Line 63: Line 65:
 
Let's set OnDrawItem to this (simply paint "itemNN"):
 
Let's set OnDrawItem to this (simply paint "itemNN"):
  
<syntaxhighlight>
+
<syntaxhighlight lang="pascal">
 
procedure TfmMain.ListDraw(Sender: TObject; C: TCanvas; AIndex: integer;
 
procedure TfmMain.ListDraw(Sender: TObject; C: TCanvas; AIndex: integer;
 
   const ARect: TRect);
 
   const ARect: TRect);
Line 77: Line 79:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
+
[[Category:Lazarus]]
 
[[Category:Components]]
 
[[Category:Components]]

Latest revision as of 14:07, 16 December 2021

About

ATListbox is OS-independant listbox control. It's made for dialog like Sublime Text's command list. If OnDrawItem is set, listbox is fully owner-drawn: you must not set items, just set ItemCount, ItemIndex.

Listbox doesn't get focus by click; to get focus on click set CanGetFocus=true (if focused, it handles keys Up/Down/PgUp/PgDn/Home/End, else you need to handle OnKeyDown in form and set listbox's ItemIndex by hand, which is boring).

Author: Alexey Torgashin

Screenshot:

atlistbox.png

Download

Homepage at github: https://github.com/Alexey-T/ATFlatControls

License

MPL 2.0 or LGPL.

Properties

  • Items: StringList to store items, or you can ignore it if you use OnDrawItem event (listbox will be fully virtual)
  • ItemCount: count of items
  • ItemIndex: index of active item
  • ItemTop: index of item visible at the top
  • ItemHeight: height (in pixels) of each item
  • VisibleItems: count of items fully visible inside control
  • CanGetFocus: allow control to be focused on click
  • ThemedScrollbar: allows control to use ATScrollBar instead of default OS scrollbar
  • ThemedColors: not used (made to use in CudaText app)

Events

  • OnClick
  • OnDblClick
  • OnDrawItem: allows to owner-draw all items
  • OnChangedSel: fired on changed ItemIndex

Example

Let's create list:

uses Math, ATListbox; //package atflatcontrols_package.lpk

var
  b: TATListbox;

procedure TfmMain.FormCreate(Sender: TObject);
begin
  b:= TATListbox.Create(Self);
  b.Parent:= Self;
  b.Align:= alClient;

  b.OnDrawItem:= @ListDraw;
  b.OnClick:= @ListClick;   //see example in github https://github.com/Alexey-T/ATFlatControls/blob/master/app/demo_listbox/unit1.pas

  b.Color:= $e0e0e0;
  b.ItemCount:= 21;
end;

Let's set OnDrawItem to this (simply paint "itemNN"):

procedure TfmMain.ListDraw(Sender: TObject; C: TCanvas; AIndex: integer;
  const ARect: TRect);
begin
  C.Brush.Color:= IfThen(AIndex=b.ItemIndex, clMedGray, b.Color);
  C.FillRect(ARect);

  C.Pen.Color:= clMedGray;
  C.Line(ARect.Left+2, ARect.Bottom-1, ARect.Right-2, ARect.Bottom-1);

  C.TextOut(ARect.Left+6, ARect.Top+2, 'item '+inttostr(AIndex));
end;