1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
|
{%MainUnit ../stdctrls.pp}
{
*****************************************************************************
This file is part of the Lazarus Component Library (LCL)
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
}
{ if not HandleAllocated then
FItems contains a TExtendedStringList
else
FItems contains an interface specific TStrings descendent
}
type
TCustomListBoxItemRecord = record
TheObject: TObject;
Selected: Boolean;
end;
PCustomListBoxItemRecord = ^TCustomListBoxItemRecord;
{------------------------------------------------------------------------------
procedure TCustomListBox.AssignCacheToItemData
------------------------------------------------------------------------------}
procedure TCustomListBox.AssignCacheToItemData(const AIndex: Integer;
const AData: Pointer);
var
SelectedItem: Boolean;
begin
if MultiSelect then
SelectedItem := PCustomListBoxItemRecord(AData)^.Selected
else
SelectedItem := FItemIndex = AIndex;
if SelectedItem then
begin
LockSelectionChange;
SendItemSelected(AIndex, True);
UnlockSelectionChange;
end;
end;
procedure TCustomListBox.BeforeDragStart;
begin
if HandleAllocated then
TWSCustomListBoxClass(WidgetSetClass).DragStart(Self);
end;
procedure TCustomListBox.BeginAutoDrag;
begin
BeginDrag(False);
end;
function TCustomListBox.CalculateStandardItemHeight: Integer;
var
B: TBitmap;
begin
// Paul: This will happen only once if Style = lbStandard then CheckListBox is
// OwnerDrawFixed in real (under windows). Handle is not allocated and we
// can not use Canvas since it will cause recursion but we need correct font height
B := TBitmap.Create;
try
B.Canvas.Font := Font;
Result := B.Canvas.TextHeight('Fj');
finally
B.Free;
end;
end;
procedure TCustomListBox.CreateParams(var Params: TCreateParams);
const
MultiSelectStyle: array[Boolean] of DWord = (LBS_MULTIPLESEL, LBS_EXTENDEDSEL);
begin
inherited CreateParams(Params);
if Sorted then
Params.Style := Params.Style or LBS_SORT;
if MultiSelect then
Params.Style := Params.Style or MultiSelectStyle[ExtendedSelect];
if Columns > 1 then
Params.Style := Params.Style or LBS_MULTICOLUMN;
case Style of
lbOwnerDrawFixed: Params.Style := Params.Style or LBS_OWNERDRAWFIXED;
lbOwnerDrawVariable: Params.Style := Params.Style or LBS_OWNERDRAWVARIABLE;
end;
Params.Style := Params.Style or
(WS_HSCROLL or WS_VSCROLL or LBS_NOINTEGRALHEIGHT or LBS_HASSTRINGS or LBS_NOTIFY);
end;
{------------------------------------------------------------------------------
procedure TCustomListBox.AssignItemDataToCache
------------------------------------------------------------------------------}
procedure TCustomListBox.AssignItemDataToCache(const AIndex: Integer;
const AData: Pointer);
begin
PCustomListBoxItemRecord(AData)^.Selected := Selected[AIndex];
end;
procedure TCustomListBox.ClearSelectedCache;
var
i: Integer;
CacheItems: TExtendedStringList;
begin
if not FCacheValid then
raise EInvalidOperation.Create('Cache is not valid.');
CacheItems := FItems as TExtendedStringList;
for i := 0 to CacheItems.Count - 1 do
PCustomListBoxItemRecord(CacheItems.Records[i])^.Selected := False;
end;
procedure TCustomListBox.SetSelectedCache(Index: Integer; IsSelected: Boolean);
var
CacheItems: TExtendedStringList;
begin
if not FCacheValid then
raise EInvalidOperation.Create('Cache is not valid.');
CacheItems := FItems as TExtendedStringList;
PCustomListBoxItemRecord(CacheItems.Records[Index])^.Selected := IsSelected;
end;
function TCustomListBox.GetSelectedCache(Index: Integer): Boolean;
var
CacheItems: TExtendedStringList;
begin
if not FCacheValid then
raise EInvalidOperation.Create('Cache is not valid.');
CacheItems := FItems as TExtendedStringList;
Result := PCustomListBoxItemRecord(CacheItems.Records[Index])^.Selected;
end;
{------------------------------------------------------------------------------
procedure TCustomListBox.InitializeWnd
------------------------------------------------------------------------------}
procedure TCustomListBox.InitializeWnd;
var
NewStrings: TStrings;
OldItems: TExtendedStringList;
i: integer;
begin
LockSelectionChange;
inherited InitializeWnd;
// fetch the interface item list
NewStrings := TWSCustomListBoxClass(WidgetSetClass).GetStrings(Self);
// copy the items (text+objects)
OldItems := FItems as TExtendedStringList;
OldItems.Sorted := False;// make sure the items are not reordered (needed for ItemIndex and attributes)
NewStrings.Assign(FItems);
// new item list is the interface item list
FItems := NewStrings;
FCacheValid := False;
// don't reset item index without a need - on windows this may cause an undesired selection of
// item for multiselect listbox
if FItemIndex <> TWSCustomListBoxClass(WidgetSetClass).GetItemIndex(Self) then
SendItemIndex;
// copy items attributes
for i := 0 to OldItems.Count - 1 do
AssignCacheToItemData(i, OldItems.Records[i]);
// free old items
OldItems.Free;
TWSCustomListBoxClass(WidgetSetClass).SetSorted(Self, FItems, FSorted);
TWSCustomListBoxClass(WidgetSetClass).SetScrollWidth(Self, FScrollWidth);
UnlockSelectionChange;
end;
procedure TCustomListBox.DestroyWnd;
begin
inherited;
if FCanvas <> nil then
TControlCanvas(FCanvas).FreeHandle;
end;
{------------------------------------------------------------------------------
procedure TCustomListBox.FinalizeWnd
------------------------------------------------------------------------------}
procedure TCustomListBox.FinalizeWnd;
var
NewStrings: TExtendedStringList;
i: integer;
begin
LockSelectionChange;
// save ItemIndex on destroy handle
if ([csDestroying,csLoading]*ComponentState=[]) then
GetItemIndex;
// create internal item list
if Assigned(FItems) then
begin
NewStrings := TExtendedStringList.Create(GetCachedDataSize);
// copy items (text+objects) from the interface items list
NewStrings.Assign(Items);
// copy items attributes
for i:=0 to Items.Count-1 do
AssignItemDataToCache(i, NewStrings.Records[i]);
// free the interface items list
TWSCustomListBoxClass(WidgetSetClass).FreeStrings(FItems);
// new item list is the internal item list
NewStrings.Sorted:=FSorted;
FItems:= NewStrings;
FCacheValid := True;
end;
inherited FinalizeWnd;
UnlockSelectionChange;
end;
class function TCustomListBox.GetControlClassDefaultSize: TSize;
begin
Result.CX := 100;
Result.CY := 80;
end;
{------------------------------------------------------------------------------
procedure TCustomListBox.UpdateSelectionMode
------------------------------------------------------------------------------}
procedure TCustomListBox.UpdateSelectionMode;
begin
if not HandleAllocated then exit;
LockSelectionChange;
TWSCustomListBoxClass(WidgetSetClass).SetSelectionMode(Self,
ExtendedSelect, MultiSelect);
UnlockSelectionChange;
end;
{------------------------------------------------------------------------------
function TCustomListBox.GetTopIndex: Integer;
------------------------------------------------------------------------------}
function TCustomListBox.GetTopIndex: Integer;
begin
if HandleAllocated then
FTopIndex := TWSCustomListBoxClass(WidgetSetClass).GetTopIndex(Self);
Result := FTopIndex;
end;
procedure TCustomListBox.RaiseIndexOutOfBounds(AIndex: integer);
begin
raise Exception.CreateFmt(rsIndexOutOfBounds, [ClassName, AIndex, FItems.Count-1]);
end;
procedure TCustomListBox.SetColumns(const AValue: Integer);
begin
if (FColumns = AValue) or (AValue < 0) then
Exit;
FColumns := AValue;
if HandleAllocated then
TWSCustomListBoxClass(WidgetSetClass).SetColumnCount(Self, FColumns);
end;
procedure TCustomListBox.SetScrollWidth(const AValue: Integer);
begin
FScrollWidth := AValue;
if HandleAllocated then
TWSCustomListBoxClass(WidgetSetClass).SetScrollWidth(Self, FScrollWidth);
end;
{------------------------------------------------------------------------------
function TCustomListBox.GetCount: Integer;
------------------------------------------------------------------------------}
function TCustomListBox.GetCount: Integer;
begin
Result := Items.Count;
end;
function TCustomListBox.GetScrollWidth: Integer;
begin
Result := FScrollWidth;
if (Result > 0) or (csDesigning in ComponentState) then Exit;
if HandleAllocated then
Result := TWSCustomListBoxClass(WidgetSetClass).GetScrollWidth(Self);
end;
{------------------------------------------------------------------------------
procedure TCustomListBox.SetTopIndex(const AValue: Integer);
------------------------------------------------------------------------------}
procedure TCustomListBox.SetTopIndex(const AValue: Integer);
begin
// don't check if changed. If the item is only partly visible, the message
// will make it complete visible.
FTopIndex := AValue;
if HandleAllocated and ([csLoading,csDestroying]*ComponentState=[]) then
TWSCustomListBoxClass(WidgetSetClass).SetTopIndex(Self, AValue);
end;
{------------------------------------------------------------------------------
procedure TCustomListBox.UpdateSorted;
------------------------------------------------------------------------------}
procedure TCustomListBox.UpdateSorted;
begin
if HandleAllocated then
begin
LockSelectionChange;
TWSCustomListBoxClass(WidgetSetClass).SetSorted(Self, FItems, FSorted);
UnlockSelectionChange;
end
else
TExtendedStringList(FItems).Sorted := FSorted;
end;
{------------------------------------------------------------------------------
procedure TCustomListBox.LMDrawListItem(var TheMessage: TLMDrawListItem);
Handler for custom drawing items.
------------------------------------------------------------------------------}
procedure TCustomListBox.LMDrawListItem(var TheMessage: TLMDrawListItem);
begin
with TheMessage.DrawListItemStruct^ do
begin
FCanvas.Handle := DC;
if Assigned(Font) then
begin
FCanvas.Font := Font;
FCanvas.Font.PixelsPerInch := Font.PixelsPerInch;
end;
if Assigned(Brush) then
FCanvas.Brush := Brush;
if (ItemID <> UINT(-1)) and (odSelected in ItemState) then
begin
FCanvas.Brush.Color := clHighlight;
FCanvas.Font.Color := clHighlightText
end else
begin
FCanvas.Brush.Color := GetColorResolvingParent;
FCanvas.Font.Color := clWindowText;
end;
DrawItem(ItemID, Area, ItemState);
if (odFocused in ItemState) and (lboDrawFocusRect in FOptions) then
DrawFocusRect(DC, Area);
FCanvas.Handle := 0;
end;
end;
procedure TCustomListBox.LMMeasureItem(var TheMessage: TLMMeasureItem);
var
AHeight: Integer;
begin
with TheMessage.MeasureItemStruct^ do
begin
// don't call GetItemHeight: causes errors on Windows due to recursion on control creation
if FItemHeight <> 0 then
AHeight := FItemHeight
else
AHeight:= CalculateStandardItemHeight;
if FStyle = lbOwnerDrawVariable then
MeasureItem(Integer(ItemId), AHeight);
if AHeight > 0 then
ItemHeight := AHeight;
end;
end;
{------------------------------------------------------------------------------
procedure TCustomListBox.LMSelChange(var TheMessage);
------------------------------------------------------------------------------}
procedure TCustomListBox.LMSelChange(var TheMessage);
begin
if [csLoading,csDestroying,csDesigning]*ComponentState<>[] then exit;
DoSelectionChange(FLockSelectionChange = 0);
end;
procedure TCustomListBox.WMLButtonUp(var Message: TLMLButtonUp);
begin
// prevent Click to be called twice when using selchange as click
if ClickOnSelChange and FClickTriggeredBySelectionChange then
Exclude(FControlState, csClicked);
inherited WMLButtonUp(Message);
// reset flag
FClickTriggeredBySelectionChange := False;
end;
{------------------------------------------------------------------------------
procedure TCustomListBox.SendItemSelected(Index: integer; IsSelected: boolean);
Tell the interface whether an item is selected.
------------------------------------------------------------------------------}
procedure TCustomListBox.SendItemSelected(Index: integer; IsSelected: boolean);
begin
if HandleAllocated then
TWSCustomListBoxClass(WidgetSetClass).SelectItem(Self, Index, IsSelected);
end;
class procedure TCustomListBox.WSRegisterClass;
begin
inherited WSRegisterClass;
RegisterCustomListBox;
end;
{------------------------------------------------------------------------------}
{ procedure TCustomListBox.SetExtendedSelect }
{------------------------------------------------------------------------------}
procedure TCustomListBox.SetExtendedSelect(Val: boolean);
begin
if Val <> FExtendedSelect then
begin
FExtendedSelect:= Val;
UpdateSelectionMode;
end;
end;
{------------------------------------------------------------------------------}
{ procedure TCustomListBox.SetMultiSelect }
{------------------------------------------------------------------------------}
procedure TCustomListBox.SetMultiSelect(Val: boolean);
begin
if Val <> FMultiSelect then
begin
FMultiSelect := Val;
UpdateSelectionMode;
end;
end;
{------------------------------------------------------------------------------}
{ procedure TCustomListBox.SetSelected }
{------------------------------------------------------------------------------}
procedure TCustomListBox.SetSelected(Index: integer; Val: boolean);
begin
CheckIndex(Index);
if not MultiSelect then
begin
if Val then
ItemIndex := Index
else
if Index = ItemIndex then
ItemIndex := -1;
end else
begin
if HandleAllocated then
SendItemSelected(Index, Val)
else
SetSelectedCache(Index, Val);
end;
end;
{------------------------------------------------------------------------------}
{ function TCustomListBox.GetSelected }
{------------------------------------------------------------------------------}
function TCustomListBox.GetSelected(Index: integer): boolean;
begin
CheckIndex(Index);
if HandleAllocated then
Result := TWSCustomListBoxClass(WidgetSetClass).GetSelected(Self, Index)
else
Result := GetSelectedCache(Index);
end;
{------------------------------------------------------------------------------}
{ function TCustomListBox.GetSelCount }
{------------------------------------------------------------------------------}
function TCustomListBox.GetSelCount: integer;
var
i: Integer;
begin
if HandleAllocated then
Result := TWSCustomListBoxClass(WidgetSetClass).GetSelCount(Self)
else
begin
if not MultiSelect then
Result := Ord(FItemIndex >- 1) //use FItemIndex since HandleAllocated = False
else
begin
Result := 0;
if FCacheValid then //otherwise GetSelectedCache raises an exception
for i := 0 to Items.Count - 1 do
if GetSelectedCache(i) then Inc(Result);
end;
end;
end;
function TCustomListBox.GetItemHeight: Integer;
begin
if HandleAllocated and (Style = lbStandard) then
begin
with ItemRect(TopIndex) do
Result := Bottom - Top;
end
else
Result := FItemHeight;
end;
procedure TCustomListBox.SetItemHeight(Value: Integer);
begin
if (FItemHeight <> Value) and (Value >= 0) then
begin
FItemHeight := Value;
if (not HandleAllocated) or (csLoading in ComponentState) then exit;
// TODO: remove RecreateWnd
RecreateWnd(Self);
end;
end;
{------------------------------------------------------------------------------}
{ procedure TCustomListBox.SetSorted }
{------------------------------------------------------------------------------}
procedure TCustomListBox.SetSorted(Val: boolean);
begin
if Val <> FSorted then
begin
FSorted:= Val;
UpdateSorted;
end;
end;
{------------------------------------------------------------------------------
procedure TCustomListBox.SetStyle
------------------------------------------------------------------------------}
procedure TCustomListBox.SetStyle(Val: TListBoxStyle);
begin
if Val <> FStyle then
begin
FStyle:= Val;
if HandleAllocated then
TWSCustomListBoxClass(WidgetSetClass).SetStyle(Self);
end;
end;
procedure TCustomListBox.DrawItem(Index: Integer; ARect: TRect;
State: TOwnerDrawState);
begin
if Assigned(FOnDrawItem) then
FOnDrawItem(Self, Index, ARect, State)
else
begin
if not(odBackgroundPainted in State) then
FCanvas.FillRect(ARect);
if (Index>=0) and (Index < Items.Count) then
InternalDrawItem(Self, FCanvas, ARect, Items[Index]);
end;
end;
procedure TCustomListBox.DoAutoAdjustLayout(
const AMode: TLayoutAdjustmentPolicy; const AXProportion, AYProportion: Double
);
begin
inherited;
if AMode in [lapAutoAdjustWithoutHorizontalScrolling, lapAutoAdjustForDPI] then
begin
if FItemHeight > 0 then
ItemHeight := Round(ItemHeight * AYProportion);
end;
end;
procedure TCustomListBox.DoSelectionChange(User: Boolean);
begin
if Assigned(OnSelectionChange) then
OnSelectionChange(Self, User);
if User and ClickOnSelChange then
begin
Click;
// set flag, that we triggered a Click, so that a possible MouseClick will
// not trigger it again
FClickTriggeredBySelectionChange := True;
end;
end;
procedure TCustomListBox.SendItemIndex;
begin
TWSCustomListBoxClass(WidgetSetClass).SetItemIndex(Self, FItemIndex);
end;
procedure TCustomListBox.WMGetDlgCode(var Message: TLMNoParams);
begin
inherited;
Message.Result := Message.Result or DLGC_WANTARROWS;
end;
{------------------------------------------------------------------------------
function TCustomListBox.GetCachedData
------------------------------------------------------------------------------}
function TCustomListBox.GetCachedData(const AIndex: Integer): Pointer;
begin
if not FCacheValid then
raise EInvalidOperation.Create('Reading form invalid cache');
Result := TExtendedStringList(FItems).Records[AIndex];
end;
{------------------------------------------------------------------------------
function TCustomListBox.GetCachedDataSize
Returns the amount of data needed when the widged isn't realized in the
interface
------------------------------------------------------------------------------}
function TCustomListBox.GetCachedDataSize: Integer;
begin
Result := SizeOf(TCustomListBoxItemRecord);
end;
{------------------------------------------------------------------------------
function TCustomListBox.SetItems
------------------------------------------------------------------------------}
procedure TCustomListBox.SetItems(Value: TStrings);
begin
if (Value <> FItems) then
begin
LockSelectionChange;
FItems.Assign(Value);
UnlockSelectionChange;
end;
end;
{------------------------------------------------------------------------------
function TCustomListBox.Create
------------------------------------------------------------------------------}
constructor TCustomListBox.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
fCompStyle := csListBox;
BorderStyle:= bsSingle;
FItems := TExtendedStringList.Create(GetCachedDataSize);
FCacheValid := True;
FClickOnSelChange:= True;
FItemIndex:=-1;
FExtendedSelect := true;
//FScrollWidth := 0;
FOptions := DefOptions;
FCanvas := TControlCanvas.Create;
TControlCanvas(FCanvas).Control := Self;
ParentColor := false;
TabStop := true;
with GetControlClassDefaultSize do
SetInitialBounds(0, 0, CX, CY);
end;
{------------------------------------------------------------------------------
function TCustomListBox.Destroy
------------------------------------------------------------------------------}
destructor TCustomListBox.Destroy;
begin
FreeAndNil(FCanvas);
FreeAndNil(FItems);
inherited Destroy;
end;
procedure TCustomListBox.AddItem(const Item: String; AnObject: TObject);
begin
Items.AddObject(Item, AnObject);
end;
function TCustomListBox.GetItemIndex: integer;
begin
if HandleAllocated then
begin
Result := TWSCustomListBoxClass(WidgetSetClass).GetItemIndex(Self);
if (Result < 0) or (Result >= Count) then
Result := -1;
FItemIndex := Result;
end
else
Result := FItemIndex;
end;
procedure TCustomListBox.SetItemIndex(AIndex: integer);
begin
if AIndex=GetItemIndex then
exit;
if (AIndex >= FItems.Count) then
RaiseIndexOutOfBounds(AIndex);
if AIndex < 0 then AIndex := -1;
FItemIndex := AIndex;
if HandleAllocated then
begin
if ([csLoading,csDestroying]*ComponentState=[]) then
SendItemIndex;
end
else
begin
ClearSelectedCache;
if FItemIndex >= 0 then
SetSelectedCache(FItemIndex, True);
end;
DoSelectionChange(false);
end;
{------------------------------------------------------------------------------
procedure TCustomListBox.CheckIndex
------------------------------------------------------------------------------}
procedure TCustomListBox.CheckIndex(const AIndex: Integer);
begin
if (AIndex < 0) or (AIndex >= Items.Count) then
RaiseIndexOutOfBounds(AIndex);
end;
{------------------------------------------------------------------------------
procedure TCustomListBox.Clear
Delete all items.
------------------------------------------------------------------------------}
procedure TCustomListBox.Clear;
begin
FItems.Clear;
FItemIndex := -1;
end;
procedure TCustomListBox.ClearSelection;
var
i: integer;
begin
if MultiSelect then
for i := 0 to Items.Count - 1 do
Selected[i] := False
else
ItemIndex := -1; // no need to traverse all items - look at SetSelected
end;
procedure TCustomListBox.LockSelectionChange;
begin
inc(FLockSelectionChange);
end;
procedure TCustomListBox.UnlockSelectionChange;
begin
dec(FLockSelectionChange);
end;
procedure TCustomListBox.Click;
begin
inherited Click;
Changed;
end;
{------------------------------------------------------------------------------
procedure TCustomListBox.MeasureItem(Index: Integer; var TheHeight: Integer);
------------------------------------------------------------------------------}
procedure TCustomListBox.MeasureItem(Index: Integer; var TheHeight: Integer);
begin
if Assigned(OnMeasureItem) then
OnMeasureItem(Self, Index, TheHeight);
end;
procedure TCustomListBox.SelectAll;
var
i: Integer;
begin
if (Count=0) then
Exit;
SelectRange(0, Count-1, True);
end;
procedure TCustomListBox.SelectRange(ALow, AHigh: integer; ASelected: boolean);
var
i: Integer;
begin
if (ALow<0) or (AHigh>=Items.Count) then Exit;
if MultiSelect then
begin
TWSCustomListBoxClass(WidgetSetClass).SelectRange(Self, ALow, AHigh, ASelected);
DoSelectionChange(false);
end
else
begin
i := ItemIndex;
if (i>=ALow) and (i<=AHigh) then
Selected[i] := ASelected;
end;
end;
procedure TCustomListBox.DeleteSelected;
var
i: Integer;
begin
if MultiSelect then
begin
i := Items.Count;
while i > 0 do
begin
dec(i);
if Selected[i] then
FItems.Delete(i);
end;
end else
if ItemIndex>=0 then
Items.Delete(ItemIndex);
end;
{------------------------------------------------------------------------------
function TCustomListBox.GetIndexAtXY(X, Y: integer): integer;
Returns item index at x, y coordinate (including scrolling)
------------------------------------------------------------------------------}
function TCustomListBox.GetIndexAtXY(X, Y: integer): integer;
begin
Result := -1;
if (not HandleAllocated) then Exit;
Result := TWSCustomListBoxClass(WidgetSetClass).GetIndexAtXY(Self, X, Y);
end;
function TCustomListBox.GetIndexAtY(Y: integer): integer;
begin
Result := GetIndexAtXY(1, Y);
end;
{------------------------------------------------------------------------------
function TCustomListBox.GetSelectedText: string;
Returns Text of all selected items, separated by LineEnding
------------------------------------------------------------------------------}
function TCustomListBox.GetSelectedText: string;
var
i: Integer;
begin
Result := '';
if ItemIndex < 0 then
Exit;
for i := 0 to Items.Count - 1 do
if Selected[i] then
if Result = '' then
Result := Items[i]
else
Result := Result + LineEnding + Items[i]
end;
{------------------------------------------------------------------------------
function TCustomListBox.ItemAtPos(const Pos: TPoint; Existing: Boolean
): Integer;
Returns item index at y coordinate (including scrolling)
------------------------------------------------------------------------------}
function TCustomListBox.ItemAtPos(const Pos: TPoint; Existing: Boolean
): Integer;
begin
Result := GetIndexAtXY(Pos.X, Pos.Y);
if Existing then
begin
if Result >= Items.Count then
Result := -1;
end else
begin
if (Result < 0) and (Result > Items.Count) and PtInRect(ClientRect, Pos) then
Result := Items.Count;
end;
end;
{------------------------------------------------------------------------------
function TCustomListBox.ItemRect(Index: Integer): TRect;
Returns coordinates of an item (including scrolling)
Special: If Index=Count the rectangle is guessed (like VCL).
------------------------------------------------------------------------------}
function TCustomListBox.ItemRect(Index: Integer): TRect;
begin
FillChar(Result, SizeOf(Result), 0);
if not HandleAllocated then
Exit;
if (Index >= 0) and (Index < Items.Count) then
TWSCustomListBoxClass(WidgetSetClass).GetItemRect(Self, Index, Result)
else
if (Index=Items.Count) and (Index>0) then
begin
TWSCustomListBoxClass(WidgetSetClass).GetItemRect(Self, Index - 1, Result);
Types.OffsetRect(Result, 0, Result.Bottom - Result.Top);
end;
end;
{------------------------------------------------------------------------------
function TCustomListBox.ItemVisible(Index: Integer): boolean;
Returns true if Item is partially visible.
------------------------------------------------------------------------------}
function TCustomListBox.ItemVisible(Index: Integer): boolean;
var
ARect: TRect;
begin
Result := False;
if (Index < 0) or (Index >= Items.Count) then Exit;
if not TWSCustomListBoxClass(WidgetSetClass).GetItemRect(Self, Index, ARect) then
Exit;
if (ARect.Bottom < 0) or (ARect.Top > ClientHeight) then
Exit;
Result := True;
end;
{------------------------------------------------------------------------------
function TCustomListBox.ItemFullyVisible(Index: Integer): boolean;
Returns true if Item is fully visible.
------------------------------------------------------------------------------}
function TCustomListBox.ItemFullyVisible(Index: Integer): boolean;
var
ARect: TRect;
begin
Result := False;
if (Index < 0) or (Index >= Items.Count) then Exit;
if not TWSCustomListBoxClass(WidgetSetClass).GetItemRect(Self, Index, ARect) then
Exit;
if (ARect.Top < 0) or (ARect.Bottom > ClientHeight) then
Exit;
Result := True;
end;
procedure TCustomListBox.MakeCurrentVisible;
var
i: Integer;
begin
i := ItemIndex;
if (i < 0) or (i >= Items.Count) then Exit;
// don't change top index if items is already fully visible
if ItemFullyVisible(i) then Exit;
TopIndex := ItemIndex;
end;
// back to stdctrls.pp
|