NiceSidebar: Cleanup. Less hints. Tested to work in win32/64, gtk2, gtk3, qt5 and cocoa widget sets.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@8866 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2023-06-28 21:07:52 +00:00
parent d22992ea64
commit 7c8f15a4bf
4 changed files with 62 additions and 76 deletions

View File

@ -562,7 +562,7 @@ object Form1: TForm1
000000000000} 000000000000}
end end
object ImageList2: TImageList object ImageList2: TImageList
Left = 256 Left = 296
Top = 136 Top = 136
Bitmap = { Bitmap = {
494C010107000900040010001000FFFFFFFFFF10FFFFFFFFFFFFFFFF424D3600 494C010107000900040010001000FFFFFFFFFF10FFFFFFFFFFFFFFFF424D3600

View File

@ -14,6 +14,9 @@ uses
{$ENDIF} {$ENDIF}
SysUtils, Variants, Classes, Graphics, Controls, Forms, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, NiceSideBar, ImgList; Dialogs, StdCtrls, ExtCtrls, NiceSideBar, ImgList;
// When switching compilation from Delphi XE11 to Delphi 7, the automatically
// added unit System.ImageList must be removed manually.
type type

View File

@ -33,6 +33,7 @@ unit NiceSideBar;
{$IFDEF FPC} {$IFDEF FPC}
{$MODE Delphi} {$MODE Delphi}
{$WARN 4055 off : Conversion between ordinals and pointers is not portable}
{$ENDIF} {$ENDIF}
interface interface
@ -43,7 +44,7 @@ uses
{$ELSE} {$ELSE}
Windows, Messages, Windows, Messages,
{$ENDIF} {$ENDIF}
Graphics, SysUtils, Controls, Classes, ImgList, Math, Graphics, SysUtils, Types, Controls, Classes, ImgList, Math,
ExtCtrls, Forms; ExtCtrls, Forms;
const const
@ -304,7 +305,6 @@ type
procedure ClearList; procedure ClearList;
procedure ListChange(RebuildItems: Boolean); procedure ListChange(RebuildItems: Boolean);
procedure DoDrawItem(Index: Integer); procedure DoDrawItem(Index: Integer);
procedure InvalidateItem(Index: Integer); // <---
function GetIndexAtPos(X, Y: Integer): Integer; function GetIndexAtPos(X, Y: Integer): Integer;
function CreateItem: TSideBarItem; function CreateItem: TSideBarItem;
procedure UpdateItem(Index: Integer); procedure UpdateItem(Index: Integer);
@ -327,6 +327,7 @@ type
procedure DrawSubItem(ACanvas: TCanvas; Rc: TRect; Str: string; States: TSideBarStates); virtual; procedure DrawSubItem(ACanvas: TCanvas; Rc: TRect; Str: string; States: TSideBarStates); virtual;
procedure DrawNonItem(ACanvas: TCanvas; Rc: TRect); virtual; procedure DrawNonItem(ACanvas: TCanvas; Rc: TRect); virtual;
procedure DrawScroller(ACanvas: TCanvas; Rc: TRect; Up: Boolean; Hover: Boolean); virtual; procedure DrawScroller(ACanvas: TCanvas; Rc: TRect; Up: Boolean; Hover: Boolean); virtual;
procedure InvalidateItem(Index: Integer); virtual;
procedure KeyDown(var Key: Word; Shift: TShiftState); override; procedure KeyDown(var Key: Word; Shift: TShiftState); override;
{$IFDEF FPC} {$IFDEF FPC}
procedure DoAutoAdjustLayout(const AMode: TLayoutAdjustmentPolicy; const AXProportion, AYProportion: Double); override; procedure DoAutoAdjustLayout(const AMode: TLayoutAdjustmentPolicy; const AXProportion, AYProportion: Double); override;
@ -412,6 +413,11 @@ const
SBITEM_STATE_DISABLED = $00000001; SBITEM_STATE_DISABLED = $00000001;
SBITEM_STATE_HIDDEN = $00000004; SBITEM_STATE_HIDDEN = $00000004;
{$HINTS OFF}
procedure Unused(const A1);
begin
end;
{$HINTS ON}
{ TSideBarItem } { TSideBarItem }
@ -515,16 +521,16 @@ function TSideBarItem.GetItemEnabled(Index: Integer): Boolean;
begin begin
Result := True; Result := True;
if (FStates.Count > Index) if (FStates.Count > Index)
then Result := (Integer(FStates[Index]) and SBITEM_STATE_DISABLED) = 0; then Result := (NativeUInt(FStates[Index]) and SBITEM_STATE_DISABLED) = 0;
end; end;
procedure TSideBarItem.SetItemEnabled(Index: Integer; const Value: Boolean); procedure TSideBarItem.SetItemEnabled(Index: Integer; const Value: Boolean);
var var
State: Integer; State: NativeUInt;
begin begin
while (FStates.Count <= Index) while (FStates.Count <= Index)
do FStates.Add(nil); do FStates.Add(nil);
State := Integer(FStates[Index]); State := NativeUInt(FStates[Index]);
if Value if Value
then State := State and not SBITEM_STATE_DISABLED then State := State and not SBITEM_STATE_DISABLED
else State := State or SBITEM_STATE_DISABLED; else State := State or SBITEM_STATE_DISABLED;
@ -536,16 +542,16 @@ function TSideBarItem.GetItemVisible(Index: Integer): Boolean;
begin begin
Result := True; Result := True;
if (FStates.Count > Index) if (FStates.Count > Index)
then Result := (Integer(FStates[Index]) and SBITEM_STATE_HIDDEN) = 0; then Result := (NativeUInt(FStates[Index]) and SBITEM_STATE_HIDDEN) = 0;
end; end;
procedure TSideBarItem.SetItemVisible(Index: Integer; const Value: Boolean); procedure TSideBarItem.SetItemVisible(Index: Integer; const Value: Boolean);
var var
State: Integer; State: NativeUInt;
begin begin
while (FStates.Count <= Index) while (FStates.Count <= Index)
do FStates.Add(nil); do FStates.Add(nil);
State := Integer(FStates[Index]); State := NativeUInt(FStates[Index]);
if Value if Value
then State := State and not SBITEM_STATE_HIDDEN then State := State and not SBITEM_STATE_HIDDEN
else State := State or SBITEM_STATE_HIDDEN; else State := State or SBITEM_STATE_HIDDEN;
@ -845,10 +851,6 @@ begin
FSubItemIndex := P^.SubIndex; FSubItemIndex := P^.SubIndex;
InvalidateItem(LastSubIndex); InvalidateItem(LastSubIndex);
InvalidateItem(i); InvalidateItem(i);
{
DoDrawItem(LastSubIndex);
DoDrawItem(i);
}
LastSubIndex := i; LastSubIndex := i;
end; end;
end; end;
@ -864,11 +866,6 @@ begin
InvalidateItem(LastIndex); InvalidateItem(LastIndex);
InvalidateItem(LastSubIndex); InvalidateItem(LastSubIndex);
InvalidateItem(i); InvalidateItem(i);
{
DoDrawItem(LastIndex);
DoDrawItem(LastSubIndex);
DoDrawItem(i);
}
LastIndex := i; LastIndex := i;
LastSubIndex := -1; LastSubIndex := -1;
end else end else
@ -886,12 +883,6 @@ begin
InvalidateItem(LastSubIndex); InvalidateItem(LastSubIndex);
InvalidateItem(i); InvalidateItem(i);
InvalidateItem(i - FSubItemIndex - 1); InvalidateItem(i - FSubItemIndex - 1);
{
DoDrawItem(LastIndex);
DoDrawItem(LastSubIndex);
DoDrawItem(i);
DoDrawItem(i - FSubItemIndex - 1);
}
LastSubIndex := i; LastSubIndex := i;
LastIndex := i - FSubItemIndex - 1; LastIndex := i - FSubItemIndex - 1;
end; end;
@ -908,6 +899,7 @@ procedure TNiceSidebar.MouseMove(Shift: TShiftState; X, Y: Integer);
var var
i: Integer; i: Integer;
P: PSBInfo; P: PSBInfo;
Rc, tmpRc: TRect;
begin begin
if ScTopVisible then if ScTopVisible then
@ -919,10 +911,6 @@ begin
HoverIndex := SCTOPINDEX; HoverIndex := SCTOPINDEX;
InvalidateItem(LastHover); InvalidateItem(LastHover);
InvalidateItem(HoverIndex); InvalidateItem(HoverIndex);
{
DoDrawItem(LastHover);
DoDrawItem(HoverIndex);
}
LastHover := SCTOPINDEX; LastHover := SCTOPINDEX;
end; end;
Exit; Exit;
@ -938,10 +926,6 @@ begin
HoverIndex := SCBOTTOMINDEX; HoverIndex := SCBOTTOMINDEX;
InvalidateItem(LastHover); InvalidateItem(LastHover);
InvalidateItem(HoverIndex); InvalidateItem(HoverIndex);
{
DoDrawItem(LastHover);
DoDrawItem(HoverIndex);
}
LastHover := SCBOTTOMINDEX; LastHover := SCBOTTOMINDEX;
end; end;
Exit; Exit;
@ -953,8 +937,8 @@ begin
if (i > -1) then if (i > -1) then
begin begin
P := PSBInfo(FList[i]); P := PSBInfo(FList[i]);
if (P^.Level = 0) and FAlwaysExpand if (P^.Level = 0) and FAlwaysExpand then
then i := -1; i := -1;
end; end;
if FHandPointCursor then if FHandPointCursor then
@ -967,15 +951,23 @@ begin
if (i <> HoverIndex) then if (i <> HoverIndex) then
begin begin
HoverIndex := i; HoverIndex := i;
if (LastHover >= 0) and (LastHover < FList.Count) if (LastHover >= 0) and (LastHover < FList.Count) then
then InvalidateItem(LastHover); //DoDrawItem(LastHover); InvalidateItem(LastHover);
if (HoverIndex > -1) then if (HoverIndex > -1) then
begin begin
InvalidateItem(HoverIndex); InvalidateItem(HoverIndex);
//DoDrawItem(HoverIndex);
P := PSBInfo(FList[i]); P := PSBInfo(FList[i]);
if Assigned(FOnHover) if Assigned(FOnHover)
then FOnHover(Self, P^.ItemIndex, P^.SubIndex, P^.Caption); then FOnHover(Self, P^.ItemIndex, P^.SubIndex, P^.Caption);
Rc := P^.Rc;
OffsetRect(Rc, 0, -DeltaY);
tmpRc := Rect(0, 0, 0, 0); // To silence the compiler
if IntersectRect(tmpRc, ScTop, Rc) then
InvalidateItem(SCTOPINDEX);
if IntersectRect(tmpRc, ScBottom, Rc) then
InvalidateItem(SCBOTTOMINDEX);
end; end;
LastHover := HoverIndex; LastHover := HoverIndex;
end; end;
@ -986,11 +978,12 @@ end;
procedure TNiceSideBar.CMMouseLeave(var Msg: {$IFDEF FPC}TLMessage{$ELSE}TMessage{$ENDIF}); procedure TNiceSideBar.CMMouseLeave(var Msg: {$IFDEF FPC}TLMessage{$ELSE}TMessage{$ENDIF});
begin begin
Unused(Msg);
if (HoverIndex <> -1) then if (HoverIndex <> -1) then
begin begin
HoverIndex := -1; HoverIndex := -1;
if (LastHover >= 0) and (LastHover < FList.Count) if (LastHover >= 0) and (LastHover < FList.Count) then
then InvalidateItem(LastHover); //DoDrawItem(LastHover); InvalidateItem(LastHover);
LastHover := -1; LastHover := -1;
end; end;
if Assigned(FOnHover) if Assigned(FOnHover)
@ -1076,10 +1069,9 @@ begin
BottomIndex := FList.Count-1; BottomIndex := FList.Count-1;
ScBottomVisible := False; ScBottomVisible := False;
end; end;
{$IFDEF FPC}
delta := Scale96ToFont(12);
{$ELSE}
delta := 12; delta := 12;
{$IFDEF FPC}
delta := Scale96ToFont(delta);
{$ENDIF} {$ENDIF}
if (FAlignment = saRight) then if (FAlignment = saRight) then
begin begin
@ -1109,7 +1101,7 @@ begin
else else
begin begin
Info := PSBInfo(FList[Index]); Info := PSBInfo(FList[Index]);
CopyRect(Rc, Info^.Rc); Rc := Info^.Rc;
OffsetRect(Rc, 0, -DeltaY); OffsetRect(Rc, 0, -DeltaY);
end; end;
InvalidateRect(Handle, @Rc, false); InvalidateRect(Handle, @Rc, false);
@ -1120,9 +1112,7 @@ var
Info: PSBInfo; Info: PSBInfo;
States: TSideBarStates; States: TSideBarStates;
Rc, Tmp: TRect; Rc, Tmp: TRect;
begin begin
if (Index = SCTOPINDEX) then if (Index = SCTOPINDEX) then
begin begin
if ScTopVisible then if ScTopVisible then
@ -1149,7 +1139,7 @@ begin
then Exit; then Exit;
Info := PSBInfo(FList[Index]); Info := PSBInfo(FList[Index]);
CopyRect(Rc, Info^.Rc); Rc := Info^.Rc;
OffsetRect(Rc, 0, -DeltaY); OffsetRect(Rc, 0, -DeltaY);
if (Index = HoverIndex) if (Index = HoverIndex)
@ -1179,6 +1169,7 @@ begin
else DrawItem(Canvas, Rc, Info^.Caption, States, FItems[Info^.ItemIndex].FImageIndex); else DrawItem(Canvas, Rc, Info^.Caption, States, FItems[Info^.ItemIndex].FImageIndex);
end; end;
Tmp := Rect(0, 0, 0, 0); // to silence the compiler
if IntersectRect(Tmp, Rc, ScTop) and ScTopVisible then if IntersectRect(Tmp, Rc, ScTop) and ScTopVisible then
begin begin
if Assigned(FOnCustomDrawScroller) if Assigned(FOnCustomDrawScroller)
@ -1208,7 +1199,7 @@ var
ppi: Integer; ppi: Integer;
{$ENDIF} {$ENDIF}
begin begin
CopyRect(RcItem, Rc); RcItem := Rc;
with ACanvas do with ACanvas do
begin begin
Brush.Style := bsSolid; Brush.Style := bsSolid;
@ -1290,6 +1281,10 @@ begin
ImgWidth := Img.Width; ImgWidth := Img.Width;
ImgHeight := Img.Height; ImgHeight := Img.Height;
{$ENDIF} {$ENDIF}
end else
begin
ImgWidth := 0;
ImgHeight := 0;
end; end;
w := TextWidth(Str); w := TextWidth(Str);
@ -1335,9 +1330,9 @@ var
x, y, w, h, i: Integer; x, y, w, h, i: Integer;
Old: TColor; Old: TColor;
begin begin
CopyRect(RcItem, Rc); RcItem := Rc;
CopyRect(Rc2, Rc); Rc2 := Rc;
Rc2.Bottom := Rc2.Bottom + 1; inc(Rc2.Bottom);
case FAlignment of case FAlignment of
saLeft: saLeft:
begin begin
@ -1610,6 +1605,7 @@ end;
procedure TNiceSideBar.WMSize(var Msg: {$IFDEF FPC}TLMSize{$ELSE}TWMSize{$ENDIF}); procedure TNiceSideBar.WMSize(var Msg: {$IFDEF FPC}TLMSize{$ELSE}TWMSize{$ENDIF});
begin begin
Unused(Msg);
TopIndex := 0; TopIndex := 0;
ListChange(False); ListChange(False);
Invalidate; Invalidate;
@ -1617,6 +1613,7 @@ end;
procedure TNiceSidebar.CMColorChanged(var Msg: {$IFDEF FPC}TLMessage{$ELSE}TMessage{$ENDIF}); procedure TNiceSidebar.CMColorChanged(var Msg: {$IFDEF FPC}TLMessage{$ELSE}TMessage{$ENDIF});
begin begin
Unused(Msg);
Invalidate; Invalidate;
end; end;
@ -1670,7 +1667,6 @@ begin
end; end;
end; end;
InvalidateItem(i); InvalidateItem(i);
//DoDrawItem(i);
end; end;
procedure TNiceSideBar.UpdateItems; procedure TNiceSideBar.UpdateItems;
@ -1721,10 +1717,6 @@ begin
begin begin
InvalidateItem(LastIndex); InvalidateItem(LastIndex);
InvalidateItem(LastSubIndex); InvalidateItem(LastSubIndex);
{
DoDrawItem(LastIndex);
DoDrawItem(LastSubIndex);
}
end else end else
begin begin
FItems[FItemIndex].Expand; FItems[FItemIndex].Expand;
@ -1734,10 +1726,6 @@ begin
begin begin
InvalidateItem(LastIndex); InvalidateItem(LastIndex);
InvalidateItem(LastSubIndex); InvalidateItem(LastSubIndex);
{
DoDrawItem(LastIndex);
DoDrawItem(LastSubIndex);
}
end; end;
if IsUpdating then if IsUpdating then
@ -1757,8 +1745,8 @@ begin
Break; Break;
end; end;
end; end;
if Redraw if Redraw then
then InvalidateItem(LastIndex); //DoDrawItem(LastIndex); InvalidateItem(LastIndex);
end; end;
end; end;
@ -1792,10 +1780,9 @@ begin
end; end;
end; end;
InvalidateItem(LastSubIndex); InvalidateItem(LastSubIndex);
//DoDrawItem(LastSubIndex);
LastSubIndex := i; LastSubIndex := i;
if (i > -1) if (i > -1) then
then InvalidateItem(i); //DoDrawItem(i); InvalidateItem(i);
end; end;
end; end;
@ -2027,14 +2014,12 @@ begin
LastHover := TopIndex; LastHover := TopIndex;
HoverIndex := TopIndex; HoverIndex := TopIndex;
InvalidateItem(HoverIndex); InvalidateItem(HoverIndex);
//DoDrawItem(HoverIndex);
end else end else
begin begin
HoverIndex := Min(FList.Count-1, HoverIndex + 1); HoverIndex := Min(FList.Count-1, HoverIndex + 1);
if (LastHover >= 0) and (LastHover < FList.Count) if (LastHover >= 0) and (LastHover < FList.Count) then
then InvalidateItem(LastHover); //DoDrawItem(LastHover); InvalidateItem(LastHover);
InvalidateItem(HoverIndex); InvalidateItem(HoverIndex);
//DoDrawItem(HoverIndex);
LastHover := HoverIndex; LastHover := HoverIndex;
end; end;
if (HoverIndex >= BottomIndex-1) and ScBottomVisible then if (HoverIndex >= BottomIndex-1) and ScBottomVisible then
@ -2059,14 +2044,12 @@ begin
LastHover := BottomIndex; LastHover := BottomIndex;
HoverIndex := BottomIndex; HoverIndex := BottomIndex;
InvalidateItem(HoverIndex); InvalidateItem(HoverIndex);
//DoDrawItem(HoverIndex);
end else end else
begin begin
HoverIndex := Max(0, HoverIndex - 1); HoverIndex := Max(0, HoverIndex - 1);
if (LastHover >= 0) and (LastHover < FList.Count) if (LastHover >= 0) and (LastHover < FList.Count) then
then InvalidateItem(LastHover); //DoDrawItem(LastHover); InvalidateItem(LastHover);
InvalidateItem(HoverIndex); InvalidateItem(HoverIndex);
//DoDrawItem(HoverIndex);
LastHover := HoverIndex; LastHover := HoverIndex;
end; end;
end else end else
@ -2075,7 +2058,7 @@ begin
if (HoverIndex < TopIndex) or (HoverIndex > BottomIndex) if (HoverIndex < TopIndex) or (HoverIndex > BottomIndex)
or (HoverIndex < 0) or (HoverIndex >= FList.Count) or (HoverIndex < 0) or (HoverIndex >= FList.Count)
then Exit; then Exit;
CopyRect(Rc, PSBInfo(FList[HoverIndex])^.Rc); Rc := PSBInfo(FList[HoverIndex])^.Rc;
OffsetRect(Rc, 0, -DeltaY); OffsetRect(Rc, 0, -DeltaY);
MouseDown(mbLeft, [], Rc.Left + 1, Rc.Top + 1); MouseDown(mbLeft, [], Rc.Left + 1, Rc.Top + 1);
end; end;

View File

@ -24,10 +24,10 @@ uses
type type
TNiceSideBarEditor = class(TComponentEditor) TNiceSideBarEditor = class(TComponentEditor)
protected public
procedure ExecuteVerb(Index: Integer); override;
function GetVerbCount: Integer; override; function GetVerbCount: Integer; override;
function GetVerb(Index: Integer): string; override; function GetVerb(Index: Integer): string; override;
procedure ExecuteVerb(Index: Integer); override;
end; end;
@ -59,7 +59,7 @@ function TNiceSideBarEditor.GetVerb(Index: Integer): string;
begin begin
case Index of case Index of
0: Result := 'Edit Items ...'; 0: Result := 'Edit Items ...';
1: Result := 'About'; 1: Result := 'About TNiceSideBar...';
end; end;
end; end;