Ignore:
Timestamp:
May 13, 2024, 6:00:06 PM (5 weeks ago)
Author:
chronos
Message:
  • Fixed: Custom draw ListBox items to keep consistent style on Linux.
  • Fixed: Last game name index error if no saved games.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Packages/DpiControls/Dpi.StdCtrls.pas

    r503 r568  
    44
    55uses
    6   Classes, SysUtils, Controls, StdCtrls, Forms, Dpi.Controls;
     6  Classes, SysUtils, Controls, StdCtrls, Forms, Dpi.Controls, Dpi.Graphics;
    77
    88type
     
    104104  end;
    105105
     106  TDrawItemEvent = procedure(Control: TWinControl; Index: Integer;
     107    ARect: TRect; State: TOwnerDrawState) of object;
     108  TListBoxStyle = StdCtrls.TListBoxStyle;
     109
    106110  { TListBox }
    107111
    108112  TListBox = class(TWinControl)
    109113  private
     114    FCanvas: TCanvas;
     115    FOnDrawItem: TDrawItemEvent;
    110116    function GetBorderStyle: TBorderStyle;
    111117    function GetCount: Integer;
     
    118124    function GetParentFont: Boolean;
    119125    function GetScrollWidth: Integer;
     126    function GetStyle: TListBoxStyle;
    120127    function GetTopIndex: Integer;
    121128    procedure SetBorderStyle(AValue: TBorderStyle);
     
    125132    procedure SetItemIndex(AValue: Integer);
    126133    procedure SetItems(AValue: TStrings);
     134    procedure SetOnDrawItem(AValue: TDrawItemEvent);
    127135    procedure SetOnSelectionChange(AValue: TSelectionChangeEvent);
    128136    procedure SetParentFont(AValue: Boolean);
    129137    procedure SetScrollWidth(AValue: Integer);
     138    procedure SetStyle(AValue: TListBoxStyle);
    130139    procedure SetTopIndex(AValue: Integer);
     140    procedure DoDrawItemNative(Control: Controls.TWinControl; Index: Integer;
     141      ARect: TRect; State: TOwnerDrawState);
    131142  protected
    132143    function GetNativeWinControl: Controls.TWinControl; override;
     
    134145  public
    135146    NativeListBox: StdCtrls.TListBox;
     147    constructor Create(AOwner: TComponent); override;
    136148    destructor Destroy; override;
    137149    property ItemIndex: Integer read GetItemIndex write SetItemIndex;
    138150    property Items: TStrings read GetItems write SetItems;
    139151    property Count: Integer read GetCount;
     152    property Canvas: TCanvas read FCanvas;
    140153  published
    141154    property ParentColor;
     
    151164    property OnSelectionChange: TSelectionChangeEvent read GetOnSelectionChange
    152165                                                      write SetOnSelectionChange;
     166    property Style: TListBoxStyle read GetStyle write SetStyle default lbStandard;
     167    property OnDrawItem: TDrawItemEvent read FOnDrawItem write SetOnDrawItem;
    153168  end;
    154169
     
    497512function TListBox.GetItemHeight: Integer;
    498513begin
    499   Result := GetNativeListBox.ItemHeight;
     514  Result := ScaleFromNative(GetNativeListBox.ItemHeight);
    500515end;
    501516
     
    525540end;
    526541
     542function TListBox.GetStyle: TListBoxStyle;
     543begin
     544  Result := GetNativeListBox.Style;
     545end;
     546
    527547function TListBox.GetTopIndex: Integer;
    528548begin
     
    547567procedure TListBox.SetItemHeight(AValue: Integer);
    548568begin
    549   GetNativeListBox.ItemHeight := AValue;
     569  GetNativeListBox.ItemHeight := ScaleToNative(AValue);
    550570end;
    551571
     
    560580end;
    561581
     582procedure TListBox.SetOnDrawItem(AValue: TDrawItemEvent);
     583begin
     584  FOnDrawItem := AValue;
     585  if Assigned(AValue) then
     586    GetNativeListBox.OnDrawItem := DoDrawItemNative
     587    else GetNativeListBox.OnDrawItem := nil;
     588end;
     589
    562590procedure TListBox.SetOnSelectionChange(AValue: TSelectionChangeEvent);
    563591begin
     
    575603end;
    576604
     605procedure TListBox.SetStyle(AValue: TListBoxStyle);
     606begin
     607  GetNativeListBox.Style := AValue;
     608end;
     609
    577610procedure TListBox.SetTopIndex(AValue: Integer);
    578611begin
    579612  GetNativeListBox.TopIndex := AValue;
     613end;
     614
     615procedure TListBox.DoDrawItemNative(Control: Controls.TWinControl;
     616  Index: Integer; ARect: TRect; State: TOwnerDrawState);
     617begin
     618  if Assigned(FOnDrawItem) then
     619    FOnDrawItem(Self, Index, ScaleRectFromNative(ARect), State);
    580620end;
    581621
     
    591631end;
    592632
     633constructor TListBox.Create(AOwner: TComponent);
     634begin
     635  inherited;
     636  FCanvas := TCanvas.Create;
     637  FCanvas.NativeCanvas := GetNativeListBox.Canvas;
     638end;
     639
    593640destructor TListBox.Destroy;
    594641begin
     642  FreeAndNil(FCanvas);
    595643  FreeAndNil(NativeListBox);
    596644  inherited;
Note: See TracChangeset for help on using the changeset viewer.