1
0
mirror of synced 2024-11-05 11:09:20 +00:00
x2cl/Source/X2CLMenuBar.pas
Mark van Renswoude b5eb0c6ad3 Added: OnExpand/Collapse events
Added: Fade + Fade/Slide animations
Changed: Animation properties moved from Painter to MenuBar
Changed: concrete animation classes moved to separate unit
Changed: custom Scroller replaced for standard Windows scrollbar
2006-04-02 20:00:02 +00:00

1957 lines
55 KiB
ObjectPascal

{
:: X2CLMenuBar is a generic group/items menu. Through the various painters,
:: it can resemble styles such as the musikCube or BBox/Uname-IT menu bars.
::
:: Last changed: $Date$
:: Revision: $Rev$
:: Author: $Author$
}
unit X2CLMenuBar;
interface
uses
Classes,
Contnrs,
Controls,
Forms,
Graphics,
ImgList,
Messages,
Types,
Windows;
type
TX2MenuBarAnimationStyle = (asNone, asSlide, asDissolve, asFade,
asSlideFade);
const
DefaultAnimationStyle = asSlide;
DefaultAnimationTime = 250;
type
// #ToDo1 (MvR) 25-3-2006: various Select methods for key support
// #ToDo1 (MvR) 1-4-2006: scroll wheel support
// #ToDo1 (MvR) 2-4-2006: OnSelectionChanging event
// #ToDo1 (MvR) 2-4-2006: OnSelectionChanged event
// #ToDo1 (MvR) 2-4-2006: disabled drawing
// #ToDo1 (MvR) 2-4-2006: OnGetAnimationClass event
TX2CustomMenuBarAnimatorClass = class of TX2CustomMenuBarAnimator;
TX2CustomMenuBarAnimator = class;
TX2CustomMenuBarPainterClass = class of TX2CustomMenuBarPainter;
TX2CustomMenuBarPainter = class;
TX2CustomMenuBarItem = class;
TX2MenuBarItem = class;
TX2MenuBarGroup = class;
TX2CustomMenuBar = class;
TX2MenuBarHitTest = record
HitTestCode: Integer;
Item: TX2CustomMenuBarItem;
end;
TX2MenuBarDrawState = (mdsHot, mdsSelected, mdsGroupHot,
mdsGroupSelected);
TX2MenuBarDrawStates = set of TX2MenuBarDrawState;
TX2MenuBarSpacingElement = (seBeforeGroupHeader, seAfterGroupHeader,
seBeforeFirstItem, seAfterLastItem,
seBeforeItem, seAfterItem);
TX2MenuBarOnExpandingEvent = procedure(Sender: TObject;
Group: TX2MenuBarGroup;
var Allowed: Boolean) of object;
TX2MenuBarOnExpandedEvent = procedure(Sender: TObject;
Group: TX2MenuBarGroup) of object;
TX2MenuBarItemBoundsProc = procedure(Sender: TObject;
Item: TX2CustomMenuBarItem;
const MenuBounds: TRect;
const ItemBounds: TRect;
Data: Pointer;
var Abort: Boolean) of object;
TCollectionUpdateEvent = procedure(Sender: TObject;
Item: TCollectionItem) of object;
IX2MenuBarPainterObserver = interface
['{22DE60C9-49A1-4E7D-B547-901BEDCC0FB7}']
procedure PainterUpdate(Sender: TX2CustomMenuBarPainter);
end;
{
:$ Abstract animation class
:: Descendants implement the animation-specific drawing code.
}
TX2CustomMenuBarAnimator = class(TObject)
private
FAnimationTime: Cardinal;
FExpanding: Boolean;
FGroup: TX2MenuBarGroup;
FStartTime: Cardinal;
FItemsBuffer: Graphics.TBitmap;
FTerminated: Boolean;
protected
function GetTimeElapsed(): Cardinal; virtual;
function GetHeight(): Integer; virtual;
procedure SetExpanding(const Value: Boolean); virtual;
procedure Terminate(); virtual;
property ItemsBuffer: Graphics.TBitmap read FItemsBuffer;
property TimeElapsed: Cardinal read GetTimeElapsed;
public
constructor Create(AItemsBuffer: Graphics.TBitmap); virtual;
destructor Destroy(); override;
procedure Update(); virtual;
procedure Draw(ACanvas: TCanvas; const ABounds: TRect); virtual; abstract;
property AnimationTime: Cardinal read FAnimationTime write FAnimationTime;
property Expanding: Boolean read FExpanding write SetExpanding;
property Group: TX2MenuBarGroup read FGroup write FGroup;
property Terminated: Boolean read FTerminated;
property Height: Integer read GetHeight;
end;
{
:$ Abstract painter class.
:: Descendants must implement the actual drawing code.
}
TX2CustomMenuBarPainter = class(TComponent)
private
FMenuBar: TX2CustomMenuBar;
FObservers: TInterfaceList;
function GetMenuBar(): TX2CustomMenuBar;
protected
procedure BeginPaint(const AMenuBar: TX2CustomMenuBar);
procedure EndPaint();
function ApplyMargins(const ABounds: TRect): TRect; virtual;
function GetSpacing(AElement: TX2MenuBarSpacingElement): Integer; virtual;
function GetGroupHeaderHeight(AGroup: TX2MenuBarGroup): Integer; virtual; abstract;
function GetGroupHeight(AGroup: TX2MenuBarGroup): Integer; virtual;
function GetItemHeight(AItem: TX2MenuBarItem): Integer; virtual; abstract;
procedure DrawBackground(ACanvas: TCanvas; const ABounds: TRect); virtual; abstract;
procedure DrawGroupHeader(ACanvas: TCanvas; AGroup: TX2MenuBarGroup; const ABounds: TRect; AState: TX2MenuBarDrawStates); virtual; abstract;
procedure DrawItem(ACanvas: TCanvas; AItem: TX2MenuBarItem; const ABounds: TRect; AState: TX2MenuBarDrawStates); virtual; abstract;
procedure FindHit(Sender: TObject; Item: TX2CustomMenuBarItem; const MenuBounds: TRect; const ItemBounds: TRect; Data: Pointer; var Abort: Boolean);
procedure NotifyObservers();
property MenuBar: TX2CustomMenuBar read GetMenuBar;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy(); override;
function HitTest(const APoint: TPoint): TX2MenuBarHitTest; overload; virtual;
function HitTest(AX, AY: Integer): TX2MenuBarHitTest; overload;
procedure AttachObserver(AObserver: IX2MenuBarPainterObserver);
procedure DetachObserver(AObserver: IX2MenuBarPainterObserver);
end;
{
:$ Base class for menu items and groups.
}
TX2CustomMenuBarItem = class(TCollectionItem)
private
FCaption: String;
FData: TObject;
FEnabled: Boolean;
FImageIndex: TImageIndex;
FOwnsData: Boolean;
FVisible: Boolean;
protected
function GetMenuBar(): TX2CustomMenuBar; virtual;
procedure SetCaption(const Value: String); virtual;
procedure SetData(const Value: TObject); virtual;
procedure SetEnabled(const Value: Boolean); virtual;
procedure SetImageIndex(const Value: TImageIndex); virtual;
procedure SetVisible(const Value: Boolean); virtual;
public
constructor Create(Collection: TCollection); override;
destructor Destroy(); override;
procedure Assign(Source: TPersistent); override;
property Data: TObject read FData write SetData;
property OwnsData: Boolean read FOwnsData write FOwnsData;
property MenuBar: TX2CustomMenuBar read GetMenuBar;
published
property Caption: String read FCaption write SetCaption;
property Enabled: Boolean read FEnabled write SetEnabled default True;
property ImageIndex: TImageIndex read FImageIndex write SetImageIndex default -1;
property Visible: Boolean read FVisible write SetVisible default True;
end;
{
:$ Base class for menu collections.
}
TX2CustomMenuBarItems = class(TOwnedCollection)
private
FOnUpdate: TCollectionUpdateEvent;
protected
procedure Update(Item: TCollectionItem); override;
property OnUpdate: TCollectionUpdateEvent read FOnUpdate write FOnUpdate;
end;
{
:$ Contains a single menu item.
}
TX2MenuBarItem = class(TX2CustomMenuBarItem)
private
function GetGroup(): TX2MenuBarGroup;
public
property Group: TX2MenuBarGroup read GetGroup;
end;
{
:$ Manages a collection of menu items.
}
TX2MenuBarItems = class(TX2CustomMenuBarItems)
private
function GetItem(Index: Integer): TX2MenuBarItem;
procedure SetItem(Index: Integer; const Value: TX2MenuBarItem);
public
constructor Create(AOwner: TPersistent);
function Add(const ACaption: TCaption = ''): TX2MenuBarItem;
property Items[Index: Integer]: TX2MenuBarItem read GetItem write SetItem; default;
end;
{
:$ Contains a single menu group.
}
TX2MenuBarGroup = class(TX2CustomMenuBarItem)
private
FExpanded: Boolean;
FItems: TX2MenuBarItems;
FSelectedItem: Integer;
function GetSelectedItem(): Integer;
procedure SetExpanded(const Value: Boolean);
procedure SetItems(const Value: TX2MenuBarItems);
protected
procedure SetEnabled(const Value: Boolean); override;
procedure InternalSetExpanded(const Value: Boolean);
procedure ItemsUpdate(Sender: TObject; Item: TCollectionItem);
property SelectedItem: Integer read GetSelectedItem write FSelectedItem;
public
constructor Create(Collection: TCollection); override;
destructor Destroy(); override;
procedure Assign(Source: TPersistent); override;
published
property Expanded: Boolean read FExpanded write SetExpanded;
property Items: TX2MenuBarItems read FItems write SetItems;
end;
{
:$ Manages a collection of menu groups.
}
TX2MenuBarGroups = class(TX2CustomMenuBarItems)
private
function GetItem(Index: Integer): TX2MenuBarGroup;
procedure SetItem(Index: Integer; const Value: TX2MenuBarGroup);
public
constructor Create(AOwner: TPersistent);
function Add(const ACaption: TCaption = ''): TX2MenuBarGroup;
property Items[Index: Integer]: TX2MenuBarGroup read GetItem write SetItem; default;
end;
{
:$ Implements the menu bar.
:: The menu bar is the visual container for the menu. It manages the groups
:: and items, and implements the switching between menu items. It does not
:: paint itself, instead it delegates this to it's linked Painter.
}
TX2CustomMenuBar = class(TCustomControl, IX2MenuBarPainterObserver)
private
FAllowCollapseAll: Boolean;
FAnimationStyle: TX2MenuBarAnimationStyle;
FAnimationTime: Cardinal;
FAnimator: TX2CustomMenuBarAnimator;
FAutoCollapse: Boolean;
FAutoSelectItem: Boolean;
FBorderStyle: TBorderStyle;
FExpandingGroups: TStringList;
FGroups: TX2MenuBarGroups;
FHideScrollbar: Boolean;
FHotItem: TX2CustomMenuBarItem;
FImageList: TCustomImageList;
FLastMousePos: TPoint;
FOnCollapsed: TX2MenuBarOnExpandedEvent;
FOnCollapsing: TX2MenuBarOnExpandingEvent;
FOnExpanded: TX2MenuBarOnExpandedEvent;
FOnExpanding: TX2MenuBarOnExpandingEvent;
FPainter: TX2CustomMenuBarPainter;
FScrollbar: Boolean;
FScrollOffset: Integer;
FSelectedItem: TX2CustomMenuBarItem;
procedure SetAllowCollapseAll(const Value: Boolean);
procedure SetAnimator(const Value: TX2CustomMenuBarAnimator);
procedure SetAutoCollapse(const Value: Boolean);
procedure SetAutoSelectItem(const Value: Boolean);
procedure SetBorderStyle(const Value: TBorderStyle);
procedure SetGroups(const Value: TX2MenuBarGroups);
procedure SetHideScrollbar(const Value: Boolean);
procedure SetImageList(const Value: TCustomImageList);
procedure SetScrollbar(const Value: Boolean);
protected
procedure CreateParams(var Params: TCreateParams); override;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
procedure PainterUpdate(Sender: TX2CustomMenuBarPainter);
procedure GroupsUpdate(Sender: TObject; Item: TCollectionItem);
procedure UpdateScrollbar();
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
// procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure CMMouseLeave(var Msg: TMessage); message CM_MOUSELEAVE;
procedure WMVScroll(var Msg: TWMVScroll); message WM_VSCROLL;
procedure TestMousePos(); virtual;
function GetMenuHeight(): Integer; virtual;
protected
procedure SetPainter(const Value: TX2CustomMenuBarPainter); virtual;
procedure WMEraseBkgnd(var Msg: TWMEraseBkgnd); message WM_ERASEBKGND;
procedure Paint(); override;
function GetDrawState(AItem: TX2CustomMenuBarItem): TX2MenuBarDrawStates;
procedure DrawMenu(ACanvas: TCanvas); virtual;
procedure DrawMenuItem(Sender: TObject; Item: TX2CustomMenuBarItem; const MenuBounds, ItemBounds: TRect; Data: Pointer; var Abort: Boolean); virtual;
procedure DrawMenuItems(ACanvas: TCanvas; AGroup: TX2MenuBarGroup; const ABounds: TRect); virtual;
procedure DrawNoPainter(ACanvas: TCanvas; const ABounds: TRect); virtual;
function GetAnimatorClass(): TX2CustomMenuBarAnimatorClass; virtual;
function IterateItemBounds(ACallback: TX2MenuBarItemBoundsProc; AData: Pointer = nil): TX2CustomMenuBarItem;
function AllowInteraction(): Boolean; virtual;
function ItemVisible(AItem: TX2CustomMenuBarItem): Boolean; virtual;
property AllowCollapseAll: Boolean read FAllowCollapseAll write SetAllowCollapseAll default True;
property AnimationStyle: TX2MenuBarAnimationStyle read FAnimationStyle write FAnimationStyle default DefaultAnimationStyle;
property AnimationTime: Cardinal read FAnimationTime write FAnimationTime default DefaultAnimationTime;
property Animator: TX2CustomMenuBarAnimator read FAnimator write SetAnimator;
property AutoCollapse: Boolean read FAutoCollapse write SetAutoCollapse default False;
property AutoSelectItem: Boolean read FAutoSelectItem write SetAutoSelectItem default False;
property BorderStyle: TBorderStyle read FBorderStyle write SetBorderStyle default bsNone;
property HideScrollbar: Boolean read FHideScrollbar write SetHideScrollbar default True;
property OnCollapsed: TX2MenuBarOnExpandedEvent read FOnCollapsed write FOnCollapsed;
property OnCollapsing: TX2MenuBarOnExpandingEvent read FOnCollapsing write FOnCollapsing;
property OnExpanded: TX2MenuBarOnExpandedEvent read FOnExpanded write FOnExpanded;
property OnExpanding: TX2MenuBarOnExpandingEvent read FOnExpanding write FOnExpanding;
property Scrollbar: Boolean read FScrollbar write SetScrollbar default True;
protected
procedure DoAutoCollapse(AGroup: TX2MenuBarGroup);
procedure DoAutoSelectItem(AGroup: TX2MenuBarGroup);
procedure DoExpand(AGroup: TX2MenuBarGroup; AExpanding: Boolean);
procedure DoExpandedChanging(AGroup: TX2MenuBarGroup; AExpanding: Boolean); virtual;
procedure DoExpandedChanged(AGroup: TX2MenuBarGroup); virtual;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy(); override;
function HitTest(const APoint: TPoint): TX2MenuBarHitTest; overload;
function HitTest(AX, AY: Integer): TX2MenuBarHitTest; overload;
property Groups: TX2MenuBarGroups read FGroups write SetGroups;
property ImageList: TCustomImageList read FImageList write SetImageList;
property Painter: TX2CustomMenuBarPainter read FPainter write SetPainter;
end;
{
:$ Exposes the menu bar's published properties.
}
TX2MenuBar = class(TX2CustomMenuBar)
published
property Align;
property AllowCollapseAll;
property AnimationStyle;
property AnimationTime;
property AutoCollapse;
property AutoSelectItem;
property BevelEdges;
property BevelInner;
property BevelKind;
property BevelOuter;
property BorderStyle;
property BorderWidth;
property Groups;
property HideScrollbar;
property ImageList;
property OnClick;
property OnCollapsed;
property OnCollapsing;
property OnDblClick;
property OnEnter;
property OnExit;
property OnExpanded;
property OnExpanding;
property OnMouseActivate;
property OnMouseDown;
property OnMouseEnter;
property OnMouseLeave;
property OnMouseMove;
property OnMouseUp;
property OnResize;
property Painter;
property Scrollbar;
end;
{
:$ Provides a wrapper for the DrawText API.
}
TDrawTextClipStyle = (csNone, csEllipsis, csPathEllipsis);
procedure DrawText(ACanvas: TCanvas; const AText: String;
const ABounds: TRect;
AHorzAlignment: TAlignment = taLeftJustify;
AVertAlignment: TVerticalAlignment = taVerticalCenter;
AMultiLine: Boolean = False;
AClipStyle: TDrawTextClipStyle = csNone);
{
:$ Returns a pointer to the first physical scanline.
:: In bottom-up bitmaps, the most common kind, the Scanline property
:: compensates for this by returning the last physical row for Scanline[0];
:: the first visual row. For most effects, the order in which the rows are
:: processed is not important; speed is. This function returns the first
:: physical scanline, which can be used as a single big array for the whole
:: bitmap.
:! Note that every scanline is padded until it is a multiple of 4 bytes
:! (32 bits). For true lineair access, ensure the bitmap has a PixelFormat
:! of pf32bit.
}
function GetScanlinePointer(ABitmap: Graphics.TBitmap): Pointer;
{
:$ Wrapper for DrawFocusRect.
:: Ensures the canvas is set up correctly for a standard focus rectangle.
}
procedure DrawFocusRect(ACanvas: TCanvas; const ABounds: TRect);
{
:$ Draws one bitmap over another with the specified Alpha transparency.
:: Both bitmaps must be the same size.
}
procedure DrawBlended(ABackground, AForeground: Graphics.TBitmap; AAlpha: Byte);
const
{ HitTest Codes }
htUnknown = 0;
htBackground = 1;
htGroup = 2;
htItem = 3;
htScroller = 4;
type
PRGBAArray = ^TRGBAArray;
TRGBAArray = array[Word] of TRGBQuad;
implementation
uses
SysUtils,
X2CLMenuBarAnimators;
const
SDefaultItemCaption = 'Menu Item';
SDefaultGroupCaption = 'Group';
SNoPainter = 'Painter property not set';
type
TProtectedCollection = class(TCollection);
{ Convenience functions }
procedure DrawText(ACanvas: TCanvas; const AText: String;
const ABounds: TRect; AHorzAlignment: TAlignment;
AVertAlignment: TVerticalAlignment;
AMultiLine: Boolean; AClipStyle: TDrawTextClipStyle);
const
HorzAlignmentFlags: array[TAlignment] of Cardinal =
(DT_LEFT, DT_RIGHT, DT_CENTER);
VertAlignmentFlags: array[TVerticalAlignment] of Cardinal =
(DT_TOP, DT_BOTTOM, DT_VCENTER);
MultiLineFlags: array[Boolean] of Cardinal =
(DT_SINGLELINE, 0);
ClipStyleFlags: array[TDrawTextClipStyle] of Cardinal =
(0, DT_END_ELLIPSIS, DT_PATH_ELLIPSIS);
var
flags: Cardinal;
bounds: TRect;
begin
flags := HorzAlignmentFlags[AHorzAlignment] or
VertAlignmentFlags[AVertAlignment] or
MultiLineFlags[AMultiLine] or
ClipStyleFlags[AClipStyle];
if AMultiLine and (AClipStyle <> csNone) then
flags := flags or DT_EDITCONTROL;
bounds := ABounds;
Windows.DrawText(ACanvas.Handle, PChar(AText), Length(AText), bounds, flags);
end;
function GetScanlinePointer(ABitmap: Graphics.TBitmap): Pointer;
var
firstScanline: Pointer;
lastScanline: Pointer;
begin
firstScanline := ABitmap.ScanLine[0];
lastScanline := ABitmap.ScanLine[Pred(ABitmap.Height)];
if Cardinal(firstScanline) > Cardinal(lastScanline) then
Result := lastScanline
else
Result := firstScanline;
end;
procedure DrawFocusRect(ACanvas: TCanvas; const ABounds: TRect);
begin
SetTextColor(ACanvas.Handle, ColorToRGB(clBlack));
Windows.DrawFocusRect(ACanvas.Handle, ABounds);
end;
procedure DrawBlended(ABackground, AForeground: Graphics.TBitmap; AAlpha: Byte);
var
sourcePixels: PRGBAArray;
destPixels: PRGBAArray;
sourcePixel: PRGBQuad;
pixelCount: Integer;
pixelIndex: Integer;
backAlpha: Integer;
foreAlpha: Integer;
begin
backAlpha := AAlpha;
foreAlpha := 256 - AAlpha;
pixelCount := AForeground.Width * AForeground.Height;
sourcePixels := GetScanlinePointer(AForeground);
destPixels := GetScanlinePointer(ABackground);
for pixelIndex := Pred(pixelCount) downto 0 do
with destPixels^[pixelIndex] do
begin
sourcePixel := @sourcePixels^[pixelIndex];
rgbRed := ((rgbRed * backAlpha) +
(sourcePixel^.rgbRed * foreAlpha)) shr 8;
rgbGreen := ((rgbGreen * backAlpha) +
(sourcePixel^.rgbGreen * foreAlpha)) shr 8;
rgbBlue := ((rgbBlue * backAlpha) +
(sourcePixel^.rgbBlue * foreAlpha)) shr 8;
end;
end;
{ TX2CustomMenuBarPainter }
constructor TX2CustomMenuBarPainter.Create(AOwner: TComponent);
begin
inherited;
if AOwner is TX2CustomMenuBar then
FMenuBar := TX2CustomMenuBar(AOwner);
end;
destructor TX2CustomMenuBarPainter.Destroy();
begin
FreeAndNil(FObservers);
inherited;
end;
procedure TX2CustomMenuBarPainter.AttachObserver(AObserver: IX2MenuBarPainterObserver);
begin
if not Assigned(FObservers) then
FObservers := TInterfaceList.Create();
if FObservers.IndexOf(AObserver) = -1 then
FObservers.Add(AObserver);
end;
procedure TX2CustomMenuBarPainter.DetachObserver(AObserver: IX2MenuBarPainterObserver);
begin
if Assigned(FObservers) then
FObservers.Remove(AObserver);
end;
procedure TX2CustomMenuBarPainter.BeginPaint(const AMenuBar: TX2CustomMenuBar);
begin
Assert(not Assigned(FMenuBar), 'BeginPaint already called');
FMenuBar := AMenuBar;
end;
procedure TX2CustomMenuBarPainter.EndPaint();
begin
Assert(Assigned(FMenuBar), 'EndPaint without BeginPaint');
FMenuBar := nil;
end;
procedure TX2CustomMenuBarPainter.NotifyObservers();
var
observerIndex: Integer;
begin
if Assigned(FObservers) then
for observerIndex := 0 to Pred(FObservers.Count) do
(FObservers[observerIndex] as IX2MenuBarPainterObserver).PainterUpdate(Self);
end;
function TX2CustomMenuBarPainter.ApplyMargins(const ABounds: TRect): TRect;
begin
Result := ABounds;
end;
function TX2CustomMenuBarPainter.GetGroupHeight(AGroup: TX2MenuBarGroup): Integer;
var
itemIndex: Integer;
begin
Result := GetSpacing(seBeforeFirstItem) +
GetSpacing(seAfterLastItem);
for itemIndex := 0 to Pred(AGroup.Items.Count) do
if MenuBar.ItemVisible(AGroup.Items[itemIndex]) then
Inc(Result, GetSpacing(seBeforeItem) +
GetItemHeight(AGroup.Items[itemIndex]) +
GetSpacing(seAfterItem));
end;
procedure TX2CustomMenuBarPainter.FindHit(Sender: TObject; Item: TX2CustomMenuBarItem; const MenuBounds, ItemBounds: TRect; Data: Pointer; var Abort: Boolean);
var
hitPoint: PPoint;
begin
hitPoint := Data;
Abort := PtInRect(ItemBounds, hitPoint^);
end;
function TX2CustomMenuBarPainter.HitTest(const APoint: TPoint): TX2MenuBarHitTest;
var
hitPoint: TPoint;
begin
hitPoint := APoint;
Result.HitTestCode := htUnknown;
Result.Item := MenuBar.IterateItemBounds(FindHit, @hitPoint);
if Assigned(Result.Item) then
if Result.Item is TX2MenuBarGroup then
Result.HitTestCode := htGroup
else if Result.Item is TX2MenuBarItem then
Result.HitTestCode := htItem;
end;
function TX2CustomMenuBarPainter.HitTest(AX, AY: Integer): TX2MenuBarHitTest;
begin
Result := HitTest(Point(AX, AY));
end;
function TX2CustomMenuBarPainter.GetMenuBar(): TX2CustomMenuBar;
begin
Assert(Assigned(FMenuBar), 'BeginPaint not called');
Result := FMenuBar;
end;
function TX2CustomMenuBarPainter.GetSpacing(AElement: TX2MenuBarSpacingElement): Integer;
begin
Result := 0;
end;
{ TX2CustomMenuBarAnimator }
constructor TX2CustomMenuBarAnimator.Create(AItemsBuffer: Graphics.TBitmap);
begin
inherited Create();
FStartTime := GetTickCount();
FItemsBuffer := Graphics.TBitmap.Create();
FItemsBuffer.Assign(AItemsBuffer);
end;
destructor TX2CustomMenuBarAnimator.Destroy();
begin
FreeAndNil(FItemsBuffer);
inherited;
end;
function TX2CustomMenuBarAnimator.GetHeight(): Integer;
begin
Result := ItemsBuffer.Height;
end;
function TX2CustomMenuBarAnimator.GetTimeElapsed(): Cardinal;
var
currentTime: Cardinal;
begin
currentTime := GetTickCount();
Result := currentTime - FStartTime;
if currentTime < FStartTime then
Inc(Result, High(Cardinal));
end;
procedure TX2CustomMenuBarAnimator.SetExpanding(const Value: Boolean);
begin
FExpanding := Value;
end;
procedure TX2CustomMenuBarAnimator.Terminate();
begin
FTerminated := True;
end;
procedure TX2CustomMenuBarAnimator.Update();
begin
end;
{ TX2CustomMenuBarItem }
constructor TX2CustomMenuBarItem.Create(Collection: TCollection);
begin
inherited;
FCaption := SDefaultItemCaption;
FEnabled := True;
FImageIndex := -1;
FOwnsData := True;
FVisible := True;
end;
destructor TX2CustomMenuBarItem.Destroy();
begin
Data := nil;
inherited;
end;
function TX2CustomMenuBarItem.GetMenuBar(): TX2CustomMenuBar;
var
parentCollection: TCollection;
parentOwner: TPersistent;
begin
Result := nil;
parentCollection := Collection;
{ Traverse up the tree of CollectionItems and OwnedCollections until
we find a MenuBar... or not. }
while Assigned(parentCollection) do
begin
parentOwner := parentCollection.Owner;
if Assigned(parentOwner) then
begin
if parentOwner is TX2CustomMenuBar then
begin
Result := TX2CustomMenuBar(parentCollection.Owner);
break;
end else if parentOwner is TCollectionItem then
parentCollection := TCollectionItem(parentOwner).Collection;
end else
break;
end;
end;
procedure TX2CustomMenuBarItem.Assign(Source: TPersistent);
begin
if Source is TX2CustomMenuBarItem then
with TX2CustomMenuBarItem(Source) do
begin
Self.Caption := Caption;
Self.Data := Data;
Self.OwnsData := OwnsData;
end
else
inherited;
end;
procedure TX2CustomMenuBarItem.SetCaption(const Value: String);
begin
if Value <> FCaption then
begin
FCaption := Value;
Changed(False);
end;
end;
procedure TX2CustomMenuBarItem.SetData(const Value: TObject);
begin
if Value <> FData then
begin
if FOwnsData then
FreeAndNil(FData);
FData := Value;
end;
end;
procedure TX2CustomMenuBarItem.SetEnabled(const Value: Boolean);
begin
if Value <> FEnabled then
begin
FEnabled := Value;
Changed(False);
end;
end;
procedure TX2CustomMenuBarItem.SetImageIndex(const Value: TImageIndex);
begin
if Value <> FImageIndex then
begin
FImageIndex := Value;
Changed(False);
end;
end;
procedure TX2CustomMenuBarItem.SetVisible(const Value: Boolean);
begin
if Value <> FVisible then
begin
FVisible := Value;
Changed(False);
end;
end;
{ TX2CustomMenuBarItems }
procedure TX2CustomMenuBarItems.Update(Item: TCollectionItem);
begin
inherited;
if Assigned(FOnUpdate) then
FOnUpdate(Self, Item);
end;
{ TX2MenuBarItem }
function TX2MenuBarItem.GetGroup(): TX2MenuBarGroup;
begin
Result := nil;
if Assigned(Collection) and (Collection.Owner <> nil) and
(Collection.Owner is TX2MenuBarGroup) then
Result := TX2MenuBarGroup(Collection.Owner);
end;
{ TX2MenuBarItems }
constructor TX2MenuBarItems.Create(AOwner: TPersistent);
begin
inherited Create(AOwner, TX2MenuBarItem);
end;
function TX2MenuBarItems.Add(const ACaption: TCaption): TX2MenuBarItem;
begin
Result := TX2MenuBarItem(inherited Add());
Result.Caption := ACaption;
end;
function TX2MenuBarItems.GetItem(Index: Integer): TX2MenuBarItem;
begin
Result := TX2MenuBarItem(inherited GetItem(Index));
end;
procedure TX2MenuBarItems.SetItem(Index: Integer; const Value: TX2MenuBarItem);
begin
inherited SetItem(Index, Value);
end;
{ TX2MenuBarGroup }
constructor TX2MenuBarGroup.Create(Collection: TCollection);
begin
inherited;
FCaption := SDefaultGroupCaption;
FItems := TX2MenuBarItems.Create(Self);
FItems.OnUpdate := ItemsUpdate;
end;
destructor TX2MenuBarGroup.Destroy();
begin
FreeAndNil(FItems);
if OwnsData then
FreeAndNil(FData);
inherited;
end;
procedure TX2MenuBarGroup.Assign(Source: TPersistent);
begin
if Source is TX2MenuBarGroup then
with TX2MenuBarGroup(Source) do
Self.Items.Assign(Items);
inherited;
end;
function TX2MenuBarGroup.GetSelectedItem(): Integer;
begin
Result := -1;
if Items.Count > 0 then
begin
if (FSelectedItem >= 0) and (FSelectedItem < Items.Count) then
Result := FSelectedItem
else
Result := 0;
end;
end;
procedure TX2MenuBarGroup.InternalSetExpanded(const Value: Boolean);
var
menu: TX2CustomMenuBar;
begin
if Value <> FExpanded then
begin
FExpanded := Value;
Changed(False);
menu := MenuBar;
if Assigned(menu) then
menu.DoExpandedChanged(Self);
end;
end;
procedure TX2MenuBarGroup.ItemsUpdate(Sender: TObject; Item: TCollectionItem);
var
groupCollection: TProtectedCollection;
begin
groupCollection := TProtectedCollection(Self.Collection);
if Assigned(groupCollection) and (groupCollection.UpdateCount = 0) then
groupCollection.Update(Item);
end;
procedure TX2MenuBarGroup.SetEnabled(const Value: Boolean);
begin
inherited;
if not Value then
Expanded := False;
end;
procedure TX2MenuBarGroup.SetExpanded(const Value: Boolean);
var
menu: TX2CustomMenuBar;
begin
if (Value <> FExpanded) and
((not Value) or Enabled) then
begin
menu := MenuBar;
if Assigned(menu) then
menu.DoExpandedChanging(Self, Value)
else
InternalSetExpanded(Value);
end;
end;
procedure TX2MenuBarGroup.SetItems(const Value: TX2MenuBarItems);
begin
if Value <> FItems then
begin
FItems.Assign(Value);
Changed(False);
end;
end;
{ TX2MenuBarGroups }
constructor TX2MenuBarGroups.Create(AOwner: TPersistent);
begin
inherited Create(AOwner, TX2MenuBarGroup);
end;
function TX2MenuBarGroups.Add(const ACaption: TCaption): TX2MenuBarGroup;
begin
Result := TX2MenuBarGroup(inherited Add());
if Length(ACaption) > 0 then
Result.Caption := ACaption;
end;
function TX2MenuBarGroups.GetItem(Index: Integer): TX2MenuBarGroup;
begin
Result := TX2MenuBarGroup(inherited GetItem(Index));
end;
procedure TX2MenuBarGroups.SetItem(Index: Integer; const Value: TX2MenuBarGroup);
begin
inherited SetItem(Index, Value);
end;
{ TX2CustomMenuBar }
constructor TX2CustomMenuBar.Create(AOwner: TComponent);
begin
inherited;
FAllowCollapseAll := True;
FAnimationStyle := DefaultAnimationStyle;
FAnimationTime := DefaultAnimationTime;
FBorderStyle := bsNone;
FExpandingGroups := TStringList.Create();
FGroups := TX2MenuBarGroups.Create(Self);
FGroups.OnUpdate := GroupsUpdate;
FHideScrollbar := True;
FScrollbar := True;
end;
procedure TX2CustomMenuBar.CreateParams(var Params: TCreateParams);
const
BorderStyles: array[TBorderStyle] of DWORD = (0, WS_BORDER);
begin
inherited;
{ Source: TScrollBox.CreateParams
Applies the BorderStyle property. }
with Params do
begin
Style := Style or WS_VSCROLL or BorderStyles[FBorderStyle];
if NewStyleControls and Ctl3D and (FBorderStyle = bsSingle) then
begin
Style := Style and not WS_BORDER;
ExStyle := ExStyle or WS_EX_CLIENTEDGE;
end;
end;
end;
destructor TX2CustomMenuBar.Destroy();
begin
Animator := nil;
FreeAndNil(FExpandingGroups);
FreeAndNil(FGroups);
inherited;
end;
procedure TX2CustomMenuBar.WMEraseBkgnd(var Msg: TWMEraseBkgnd);
begin
Msg.Result := 0;
end;
procedure TX2CustomMenuBar.Paint();
var
buffer: Graphics.TBitmap;
bufferRect: TRect;
expand: Boolean;
group: TX2MenuBarGroup;
begin
if Assigned(Painter) then
begin
buffer := Graphics.TBitmap.Create();
try
buffer.PixelFormat := pf32bit;
buffer.Width := Self.ClientWidth;
buffer.Height := Self.ClientHeight;
bufferRect := Rect(0, 0, buffer.Width, buffer.Height);
buffer.Canvas.Font.Assign(Self.Font);
if Assigned(Animator) then
Animator.Update();
UpdateScrollbar();
Painter.BeginPaint(Self);
try
Painter.DrawBackground(buffer.Canvas, bufferRect);
DrawMenu(buffer.Canvas);
finally
Painter.EndPaint();
end;
finally
Self.Canvas.Draw(0, 0, buffer);
FreeAndNil(buffer);
end;
if Assigned(Animator) then
begin
if Animator.Terminated then
begin
Animator.Group.InternalSetExpanded(Animator.Expanding);
Animator := nil;
end
else
{ Prevent 100% CPU usage }
Sleep(5);
TestMousePos();
Invalidate();
end
else
{ Process animation queue }
if FExpandingGroups.Count > 0 then
begin
expand := (FExpandingGroups[0] = #1);
group := TX2MenuBarGroup(FExpandingGroups.Objects[0]);
FExpandingGroups.Delete(0);
group.Expanded := expand;
end;
end
else
DrawNoPainter(Self.Canvas, Self.ClientRect);
end;
function TX2CustomMenuBar.GetDrawState(AItem: TX2CustomMenuBarItem): TX2MenuBarDrawStates;
function ItemGroup(AGroupItem: TX2CustomMenuBarItem): TX2MenuBarGroup;
begin
Result := nil;
if AGroupItem is TX2MenuBarItem then
Result := TX2MenuBarItem(AGroupItem).Group;
end;
begin
Result := [];
if AItem = FHotItem then
Include(Result, mdsHot);
if AItem = FSelectedItem then
Include(Result, mdsSelected);
if Assigned(FHotItem) and (AItem = ItemGroup(FHotItem)) then
Include(Result, mdsGroupHot);
if Assigned(FSelectedItem) and (AItem = ItemGroup(FSelectedItem)) then
Include(Result, mdsGroupSelected);
end;
procedure TX2CustomMenuBar.DrawMenuItem(Sender: TObject;
Item: TX2CustomMenuBarItem;
const MenuBounds, ItemBounds: TRect;
Data: Pointer; var Abort: Boolean);
var
canvas: TCanvas;
drawState: TX2MenuBarDrawStates;
group: TX2MenuBarGroup;
groupBounds: TRect;
begin
if ItemBounds.Top > MenuBounds.Bottom then
begin
Abort := True;
exit;
end;
canvas := TCanvas(Data);
drawState := GetDrawState(Item);
if Item is TX2MenuBarGroup then
begin
group := TX2MenuBarGroup(Item);
Painter.DrawGroupHeader(canvas, group, ItemBounds,
drawState);
if Assigned(Animator) and (Animator.Group = group) then
begin
groupBounds := MenuBounds;
groupBounds.Top := ItemBounds.Bottom +
Painter.GetSpacing(seAfterGroupHeader) +
Painter.GetSpacing(seBeforeFirstItem);
groupBounds.Bottom := groupBounds.Top + Animator.Height;
Animator.Draw(canvas, groupBounds);
end;
end else if Item is TX2MenuBarItem then
Painter.DrawItem(canvas, TX2MenuBarItem(Item), ItemBounds, drawState);
end;
procedure TX2CustomMenuBar.DrawMenuItems(ACanvas: TCanvas; AGroup: TX2MenuBarGroup; const ABounds: TRect);
var
itemBounds: TRect;
itemIndex: Integer;
item: TX2MenuBarItem;
drawState: TX2MenuBarDrawStates;
begin
Assert(Assigned(Painter), 'No Painter assigned');
itemBounds := ABounds;
Inc(itemBounds.Top, Painter.GetSpacing(seBeforeFirstItem));
for itemIndex := 0 to Pred(AGroup.Items.Count) do
begin
item := AGroup.Items[itemIndex];
if not ItemVisible(item) then
continue;
Inc(itemBounds.Top, Painter.GetSpacing(seBeforeItem));
itemBounds.Bottom := itemBounds.Top + Painter.GetItemHeight(item);
drawState := GetDrawState(item);
Painter.DrawItem(ACanvas, item, itemBounds, drawState);
itemBounds.Top := itemBounds.Bottom + Painter.GetSpacing(seAfterItem);
end;
end;
procedure TX2CustomMenuBar.DrawMenu(ACanvas: TCanvas);
begin
IterateItemBounds(DrawMenuItem, Pointer(ACanvas));
end;
procedure TX2CustomMenuBar.DrawNoPainter(ACanvas: TCanvas; const ABounds: TRect);
const
XorColor = $00FFD8CE; // RGB(206, 216, 255)
begin
with ACanvas do
begin
Brush.Color := clBtnFace;
FillRect(ABounds);
Pen.Style := psDot;
Pen.Mode := pmXor;
Pen.Color := XorColor;
Brush.Style := bsClear;
Rectangle(ABounds);
DrawText(ACanvas, SNoPainter, ABounds, taCenter);
end;
end;
function TX2CustomMenuBar.GetAnimatorClass(): TX2CustomMenuBarAnimatorClass;
begin
Result := nil;
case AnimationStyle of
asSlide: Result := TX2MenuBarSlideAnimator;
asDissolve: Result := TX2MenuBarDissolveAnimator;
asFade: Result := TX2MenuBarFadeAnimator;
asSlideFade: Result := TX2MenuBarSlideFadeAnimator;
end;
end;
function TX2CustomMenuBar.IterateItemBounds(ACallback: TX2MenuBarItemBoundsProc;
AData: Pointer): TX2CustomMenuBarItem;
var
groupIndex: Integer;
group: TX2MenuBarGroup;
menuBounds: TRect;
itemBounds: TRect;
itemIndex: Integer;
item: TX2MenuBarItem;
abort: Boolean;
begin
Assert(Assigned(Painter), 'No Painter assigned');
Result := nil;
menuBounds := Painter.ApplyMargins(Self.ClientRect);
itemBounds := menuBounds;
OffsetRect(itemBounds, 0, -FScrollOffset);
abort := False;
for groupIndex := 0 to Pred(Groups.Count) do
begin
{ Group }
group := Groups[groupIndex];
if not ItemVisible(group) then
continue;
Inc(itemBounds.Top, Painter.GetSpacing(seBeforeGroupHeader));
itemBounds.Bottom := itemBounds.Top +
Painter.GetGroupHeaderHeight(group);
ACallback(Self, group, menuBounds, itemBounds, AData, abort);
if abort then
begin
Result := group;
break;
end;
itemBounds.Top := itemBounds.Bottom +
Painter.GetSpacing(seAfterGroupHeader);
if Assigned(Animator) and (Animator.Group = group) then
begin
{ Animated group }
Inc(itemBounds.Top, Animator.Height);
end else if group.Expanded then
begin
Inc(itemBounds.Top, Painter.GetSpacing(seBeforeFirstItem));
for itemIndex := 0 to Pred(group.Items.Count) do
begin
{ Item }
item := group.Items[itemIndex];
if not ItemVisible(item) then
continue;
Inc(itemBounds.Top, Painter.GetSpacing(seBeforeItem));
itemBounds.Bottom := itemBounds.Top + Painter.GetItemHeight(item);
ACallback(Self, item, menuBounds, itemBounds, AData, abort);
if abort then
begin
Result := item;
break;
end;
itemBounds.Top := itemBounds.Bottom +
Painter.GetSpacing(seAfterItem);
end;
Inc(itemBounds.Top, Painter.GetSpacing(seAfterLastItem));
end;
if abort then
break;
end;
end;
procedure TX2CustomMenuBar.DoExpandedChanging(AGroup: TX2MenuBarGroup;
AExpanding: Boolean);
function ExpandedGroupsCount(): Integer;
var
groupIndex: Integer;
begin
Result := 0;
for groupIndex := 0 to Pred(Groups.Count) do
if Groups[groupIndex].Expanded then
Inc(Result);
end;
var
allowed: Boolean;
begin
if csLoading in ComponentState then
begin
AGroup.InternalSetExpanded(AExpanding);
exit;
end;
allowed := True;
if AExpanding then
begin
if Assigned(FOnExpanding) then
FOnExpanding(Self, AGroup, allowed);
end else
if Assigned(FOnCollapsing) then
FOnCollapsing(Self, AGroup, allowed);
if not allowed then
exit;
{ Auto select item }
if AutoSelectItem then
DoAutoSelectItem(AGroup);
{ Allow collapse all }
if not (AExpanding or AllowCollapseAll) then
if ExpandedGroupsCount() = 1 then
exit;
{ Auto collapse }
if AutoCollapse then
if AExpanding then
DoAutoCollapse(AGroup);
DoExpand(AGroup, AExpanding);
end;
procedure TX2CustomMenuBar.DoExpandedChanged(AGroup: TX2MenuBarGroup);
begin
if AGroup.Expanded then
begin
if Assigned(FOnExpanded) then
FOnExpanded(Self, AGroup);
end else
if Assigned(FOnCollapsed) then
FOnCollapsed(Self, AGroup);
end;
function TX2CustomMenuBar.AllowInteraction(): Boolean;
begin
Result := not Assigned(Animator);
end;
function TX2CustomMenuBar.ItemVisible(AItem: TX2CustomMenuBarItem): Boolean;
begin
Result := AItem.Visible or (csDesigning in ComponentState);
end;
procedure TX2CustomMenuBar.DoExpand(AGroup: TX2MenuBarGroup; AExpanding: Boolean);
var
animatorClass: TX2CustomMenuBarAnimatorClass;
itemsBuffer: Graphics.TBitmap;
itemsBounds: TRect;
begin
if not Assigned(Painter) then
exit;
if Assigned(Animator) then
begin
FExpandingGroups.AddObject(Chr(Ord(AExpanding)), AGroup);
end else
begin
animatorClass := GetAnimatorClass();
if Assigned(animatorClass) and not (csDesigning in ComponentState) then
begin
Painter.BeginPaint(Self);
try
itemsBuffer := Graphics.TBitmap.Create();
try
itemsBounds := Painter.ApplyMargins(Self.ClientRect);
itemsBuffer.PixelFormat := pf32bit;
itemsBuffer.Width := itemsBounds.Right - itemsBounds.Left;
itemsBuffer.Height := Painter.GetGroupHeight(AGroup);
itemsBounds := Rect(0, 0, itemsBuffer.Width, itemsBuffer.Height);
itemsBuffer.Canvas.Font.Assign(Self.Font);
// #ToDo3 (MvR) 23-3-2006: this will probably cause problems if we ever
// want a bitmapped/customdrawn background.
// Maybe we can trick around a bit with the
// canvas offset? think about it later.
Painter.DrawBackground(itemsBuffer.Canvas, itemsBounds);
DrawMenuItems(itemsBuffer.Canvas, AGroup, itemsBounds);
Animator := animatorClass.Create(itemsBuffer);
Animator.AnimationTime := AnimationTime;
Animator.Expanding := AExpanding;
Animator.Group := AGroup;
finally
FreeAndNil(itemsBuffer);
end;
finally
Painter.EndPaint();
Invalidate();
end;
end else
AGroup.InternalSetExpanded(AExpanding);
end;
end;
procedure TX2CustomMenuBar.DoAutoCollapse(AGroup: TX2MenuBarGroup);
var
expandedGroup: TX2MenuBarGroup;
groupIndex: Integer;
group: TX2MenuBarGroup;
begin
expandedGroup := AGroup;
if not Assigned(expandedGroup) then
begin
for groupIndex := 0 to Pred(Groups.Count) do
if Groups[groupIndex].Expanded then
begin
expandedGroup := Groups[groupIndex];
break;
end;
if not Assigned(expandedGroup) then
if Groups.Count > 0 then
begin
expandedGroup := Groups[0];
expandedGroup.Expanded := True;
end else
exit;
end;
for groupIndex := 0 to Pred(Groups.Count) do
begin
group := Groups[groupIndex];
if (group <> expandedGroup) and (group.Expanded) then
DoExpand(group, False);
end;
end;
procedure TX2CustomMenuBar.DoAutoSelectItem(AGroup: TX2MenuBarGroup);
var
group: TX2MenuBarGroup;
groupIndex: Integer;
begin
group := AGroup;
if not Assigned(group) then
begin
for groupIndex := 0 to Pred(Groups.Count) do
if Groups[groupIndex].Expanded then
begin
group := Groups[groupIndex];
break;
end;
if (not Assigned(group)) and (Groups.Count > 0) then
begin
group := Groups[0];
group.Expanded := True;
end;
if not Assigned(group) then
exit;
end;
if group.Items.Count > 0 then
begin
FSelectedItem := group.Items[group.SelectedItem];
Invalidate();
end;
end;
function TX2CustomMenuBar.HitTest(const APoint: TPoint): TX2MenuBarHitTest;
var
hitPoint: TPoint;
begin
Result.HitTestCode := htUnknown;
Result.Item := nil;
hitPoint := APoint;
if PtInRect(Self.ClientRect, APoint) then
begin
Painter.BeginPaint(Self);
try
Result := Painter.HitTest(hitPoint);
finally
Painter.EndPaint();
end;
end;
end;
function TX2CustomMenuBar.HitTest(AX, AY: Integer): TX2MenuBarHitTest;
begin
Result := HitTest(Point(AX, AY));
end;
procedure TX2CustomMenuBar.Notification(AComponent: TComponent; Operation: TOperation);
begin
if Operation = opRemove then
if AComponent = FPainter then
begin
FPainter := nil;
Invalidate();
end else if AComponent = FImageList then
begin
FImageList := nil;
Invalidate();
end;
inherited;
end;
procedure TX2CustomMenuBar.PainterUpdate(Sender: TX2CustomMenuBarPainter);
begin
Invalidate();
end;
procedure TX2CustomMenuBar.GroupsUpdate(Sender: TObject; Item: TCollectionItem);
begin
if Assigned(FSelectedItem) and (not FSelectedItem.Enabled) then
FSelectedItem := nil;
Invalidate();
end;
procedure TX2CustomMenuBar.MouseDown(Button: TMouseButton; Shift: TShiftState;
X, Y: Integer);
var
hitTest: TX2MenuBarHitTest;
group: TX2MenuBarGroup;
begin
if Button = mbLeft then
if AllowInteraction then
begin
hitTest := Self.HitTest(X, Y);
if hitTest.HitTestCode = htGroup then
begin
group := TX2MenuBarGroup(hitTest.Item);
if group.Enabled and (group.Items.Count > 0) then
begin
group.Expanded := not group.Expanded;
hitTest.Item := FSelectedItem;
Invalidate();
end;
end;
if Assigned(hitTest.Item) and (hitTest.Item <> FSelectedItem) and
hitTest.Item.Enabled then
begin
if hitTest.HitTestCode = htItem then
TX2MenuBarItem(hitTest.Item).Group.SelectedItem := hitTest.Item.Index;
FSelectedItem := hitTest.Item;
Invalidate();
end;
end;
inherited;
end;
procedure TX2CustomMenuBar.MouseMove(Shift: TShiftState; X, Y: Integer);
begin
FLastMousePos := Point(X, Y);
TestMousePos();
inherited;
end;
//procedure TX2CustomMenuBar.MouseUp(Button: TMouseButton; Shift: TShiftState;
// X, Y: Integer);
//begin
// inherited;
//end;
procedure TX2CustomMenuBar.CMMouseLeave(var Msg: TMessage);
begin
FLastMousePos := Point(-1, -1);
FHotItem := nil;
Invalidate();
end;
procedure TX2CustomMenuBar.WMVScroll(var Msg: TWMVScroll);
var
scrollInfo: TScrollInfo;
scrollPos: Integer;
begin
Msg.Result := 0;
if Msg.ScrollCode = SB_ENDSCROLL then
exit;
scrollPos := -1;
FillChar(scrollInfo, SizeOf(TScrollInfo), #0);
scrollInfo.cbSize := SizeOf(TScrollInfo);
if Msg.ScrollCode = SB_THUMBTRACK then
begin
scrollInfo.fMask := SIF_TRACKPOS;
if GetScrollInfo(Self.Handle, SB_VERT, scrollInfo) then
scrollPos := scrollInfo.nTrackPos;
end else
begin
scrollInfo.fMask := SIF_RANGE or SIF_POS or SIF_PAGE;
if GetScrollInfo(Self.Handle, SB_VERT, scrollInfo) then
case Msg.ScrollCode of
SB_BOTTOM:
scrollPos := scrollInfo.nMax;
// #ToDo2 (MvR) 2-4-2006: scroll to the next item
// (needs GetTopItem implementation)
SB_LINEDOWN:
begin
scrollPos := scrollInfo.nPos + 40;
if scrollPos > scrollInfo.nMax then
scrollPos := scrollInfo.nMax;
end;
SB_LINEUP:
begin
scrollPos := scrollInfo.nPos - 40;
if scrollPos < scrollInfo.nMin then
scrollPos := scrollInfo.nMin;
end;
SB_PAGEDOWN:
begin
scrollPos := scrollInfo.nPos + Integer(scrollInfo.nPage);
if scrollPos > scrollInfo.nMax then
scrollPos := scrollInfo.nMax;
end;
SB_PAGEUP:
begin
scrollPos := scrollInfo.nPos - Integer(scrollInfo.nPage);
if scrollPos < scrollInfo.nMin then
scrollPos := scrollInfo.nMin;
end;
SB_TOP:
scrollPos := 0;
end;
end;
if scrollPos <> -1 then
begin
FillChar(scrollInfo, SizeOf(TScrollInfo), #0);
scrollInfo.cbSize := SizeOf(TScrollInfo);
scrollInfo.fMask := SIF_POS;
scrollInfo.nPos := scrollPos;
SetScrollInfo(Self.Handle, SB_VERT, scrollInfo, False);
Invalidate();
end;
end;
procedure TX2CustomMenuBar.TestMousePos();
var
hitTest: TX2MenuBarHitTest;
begin
hitTest := Self.HitTest(FLastMousePos.X, FLastMousePos.Y);
if hitTest.Item <> FHotItem then
begin
FHotItem := hitTest.Item;
Invalidate();
end;
end;
function TX2CustomMenuBar.GetMenuHeight(): Integer;
var
groupIndex: Integer;
group: TX2MenuBarGroup;
menuBounds: TRect;
itemIndex: Integer;
item: TX2MenuBarItem;
begin
if not Assigned(Painter) then
begin
Result := -1;
exit;
end;
menuBounds := Painter.ApplyMargins(Self.ClientRect);
Result := Self.ClientHeight - (menuBounds.Bottom - menuBounds.Top);
for groupIndex := 0 to Pred(Groups.Count) do
begin
{ Group }
group := Groups[groupIndex];
if not ItemVisible(group) then
continue;
Inc(Result, Painter.GetSpacing(seBeforeGroupHeader) +
Painter.GetGroupHeaderHeight(group) +
Painter.GetSpacing(seAfterGroupHeader));
if Assigned(Animator) and (Animator.Group = group) then
begin
{ Animated group }
Inc(Result, Animator.Height);
end else if group.Expanded then
begin
Inc(Result, Painter.GetSpacing(seBeforeFirstItem));
for itemIndex := 0 to Pred(group.Items.Count) do
begin
{ Item }
item := group.Items[itemIndex];
if not ItemVisible(item) then
continue;
Inc(Result, Painter.GetSpacing(seBeforeItem) +
Painter.GetItemHeight(item) +
Painter.GetSpacing(seAfterItem));
end;
Inc(Result, Painter.GetSpacing(seAfterLastItem));
end;
end;
end;
procedure TX2CustomMenuBar.UpdateScrollbar();
var
scrollInfo: TScrollInfo;
begin
{ Don't update the scrollbar while animating, prevents issues with the
items buffer width if the scrollbar happens to show/hide during animation. }
if Assigned(Animator) then
exit;
FillChar(scrollInfo, SizeOf(TScrollInfo), #0);
scrollInfo.cbSize := SizeOf(TScrollInfo);
scrollInfo.fMask := SIF_PAGE or SIF_RANGE;
if Scrollbar then
begin
scrollInfo.nMin := 0;
scrollInfo.nMax := GetMenuHeight();
scrollInfo.nPage := Self.ClientHeight;
if not HideScrollbar then
scrollInfo.fMask := scrollInfo.fMask or SIF_DISABLENOSCROLL;
end else
begin
scrollInfo.nMin := 0;
scrollInfo.nMax := 0;
scrollInfo.nPage := 0;
end;
SetScrollInfo(Self.Handle, SB_VERT, scrollInfo, True);
FillChar(scrollInfo, SizeOf(TScrollInfo), #0);
scrollInfo.cbSize := SizeOf(TScrollInfo);
scrollInfo.fMask := SIF_POS;
FScrollOffset := 0;
if GetScrollInfo(Self.Handle, SB_VERT, scrollInfo) then
FScrollOffset := scrollInfo.nPos;
end;
procedure TX2CustomMenuBar.SetAllowCollapseAll(const Value: Boolean);
begin
if Value <> FAllowCollapseAll then
begin
FAllowCollapseAll := Value;
end;
end;
procedure TX2CustomMenuBar.SetAnimator(const Value: TX2CustomMenuBarAnimator);
begin
if Value <> FAnimator then
begin
FreeAndNil(FAnimator);
FAnimator := Value;
end;
end;
procedure TX2CustomMenuBar.SetAutoCollapse(const Value: Boolean);
begin
if Value <> FAutoCollapse then
begin
FAutoCollapse := Value;
if Value then
DoAutoCollapse(nil);
end;
end;
procedure TX2CustomMenuBar.SetAutoSelectItem(const Value: Boolean);
begin
if Value <> FAutoSelectItem then
begin
FAutoSelectItem := Value;
if Value and (not Assigned(FSelectedItem)) then
DoAutoSelectItem(nil);
end;
end;
procedure TX2CustomMenuBar.SetBorderStyle(const Value: TBorderStyle);
begin
if Value <> FBorderStyle then
begin
FBorderStyle := Value;
RecreateWnd();
end;
end;
procedure TX2CustomMenuBar.SetGroups(const Value: TX2MenuBarGroups);
begin
if Value <> FGroups then
FGroups.Assign(Value);
end;
procedure TX2CustomMenuBar.SetHideScrollbar(const Value: Boolean);
begin
if Value <> FHideScrollbar then
begin
FHideScrollbar := Value;
RecreateWnd();
end;
end;
procedure TX2CustomMenuBar.SetImageList(const Value: TCustomImageList);
begin
if Value <> FImageList then
begin
if Assigned(FImageList) then
FImageList.RemoveFreeNotification(Self);
FImageList := Value;
if Assigned(FImageList) then
FImageList.FreeNotification(Self);
Invalidate();
end;
end;
procedure TX2CustomMenuBar.SetPainter(const Value: TX2CustomMenuBarPainter);
begin
if FPainter <> Value then
begin
if Assigned(FPainter) then
begin
FPainter.DetachObserver(Self);
FPainter.RemoveFreeNotification(Self);
end;
Animator := nil;
FPainter := Value;
if Assigned(FPainter) then
begin
FPainter.FreeNotification(Self);
FPainter.AttachObserver(Self);
end;
Invalidate();
end;
end;
procedure TX2CustomMenuBar.SetScrollbar(const Value: Boolean);
begin
if Value <> FScrollbar then
begin
FScrollbar := Value;
RecreateWnd();
end;
end;
end.