Ignore:
Timestamp:
May 13, 2024, 6:00:06 PM (4 months 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.
Location:
trunk/Packages/DpiControls
Files:
3 edited

Legend:

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

    r559 r568  
    227227begin
    228228  Result := ScreenInfo.Lookup[Value];
    229   // Round and Trunc are fast. Ceil and Floor slow.
     229  // Round and Trunc are fast. Ceil and Floor are slow.
    230230  // Without lookup table we would use:
    231231  // Result := Ceil(Value * ScreenInfo.ToNative);
  • trunk/Packages/DpiControls/Dpi.Graphics.pas

    r560 r568  
    88const
    99  clBlack = TColor($000000);
     10  clWhite = TColor($ffffff);
    1011
    1112type
     
    9293
    9394  TBrushStyle = Graphics.TBrushStyle;
     95  TPenStyle = Graphics.TPenStyle;
    9496
    9597  { TBrush }
     
    131133    function GetHandle: HDC;
    132134    function GetPixel(X, Y: Integer): TColor;
     135    function GetTextStyle: TTextStyle;
    133136    procedure SetBrush(AValue: TBrush);
    134137    procedure SetFont(AValue: TFont);
     
    137140    procedure SetPixel(X, Y: Integer; AValue: TColor);
    138141    procedure SetNativeCanvas(AValue: Graphics.TCanvas);
     142    procedure SetTextStyle(AValue: TTextStyle);
    139143  protected
    140144    procedure DoLine(X1, Y1, X2, Y2: Integer); virtual;
     
    182186    function TextExtent(const Text: string): TSize; virtual;
    183187    procedure TextOut(X, Y: Integer; const Text: string); virtual;
    184     procedure TextRect(ARect: TRect; X, Y: Integer; Text: string);
     188    procedure TextRect(ARect: TRect; X, Y: Integer; Text: string); overload;
     189    procedure TextRect(ARect: TRect; X, Y: integer; const Text: string;
     190      const Style: TTextStyle); overload;
    185191    procedure MoveTo(X, Y: Integer);
    186192    procedure LineTo(X, Y: Integer);
     
    196202    property Width: Integer read GetWidth;
    197203    property Height: Integer read GetHeight;
     204    property TextStyle: TTextStyle read GetTextStyle write SetTextStyle;
    198205  published
    199206    property Brush: TBrush read FBrush write SetBrush;
     
    966973end;
    967974
     975function TCanvas.GetTextStyle: TTextStyle;
     976begin
     977  Result := GetNativeCanvas.TextStyle;
     978end;
     979
    968980function TCanvas.GetWidth: Integer;
    969981begin
     
    10391051end;
    10401052
     1053procedure TCanvas.SetTextStyle(AValue: TTextStyle);
     1054begin
     1055  GetNativeCanvas.TextStyle := AValue;
     1056end;
     1057
    10411058procedure TCanvas.DoLine(X1, Y1, X2, Y2: Integer);
    10421059begin
     
    12131230procedure TCanvas.TextRect(ARect: TRect; X, Y: Integer; Text: string);
    12141231begin
    1215   GetNativeCanvas.TextRect(ScaleRectToNative(ARect), ScaleToNative(X), ScaleToNative(Y), Text);
     1232  GetNativeCanvas.TextRect(ScaleRectToNative(ARect), ScaleToNative(X),
     1233    ScaleToNative(Y), Text);
     1234end;
     1235
     1236procedure TCanvas.TextRect(ARect: TRect; X, Y: integer; const Text: string;
     1237  const Style: TTextStyle);
     1238begin
     1239  GetNativeCanvas.TextRect(ScaleRectToNative(ARect), ScaleToNative(X),
     1240    ScaleToNative(Y), Text, TextStyle);
    12161241end;
    12171242
  • 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.