lcl: add new property TBitBtn.GlyphShowMode and new function TBitBtn.CanShowGlyph

git-svn-id: trunk@19909 -
This commit is contained in:
paul 2009-05-10 15:52:24 +00:00
parent a24043f0e8
commit f3dfb636c4
9 changed files with 119 additions and 47 deletions

View File

@ -79,9 +79,16 @@ type
gtmTransparent // transparent = true
);
TGlyphShowMode = (
gsmAlways, // always show
gsmNever, // never show
gsmApplication, // depends on application settings
gsmSystem // depends on system settings
);
TButtonGlyph = class(TObject, IUnknown, IImageCacheListener)
private
FShowMode: TGlyphShowMode;
FImageIndexes: array[TButtonState] of Integer;
FImages: TCustomImageList;
FOriginal: TBitmap;
@ -93,6 +100,7 @@ type
function GetWidth: Integer;
procedure SetGlyph(Value: TBitmap);
procedure SetNumGlyphs(Value: TNumGlyphs);
procedure SetShowMode(const AValue: TGlyphShowMode);
procedure ClearImages;
protected
// IUnknown
@ -120,6 +128,7 @@ type
property Images: TCustomImageList read FImages;
property Width: Integer read GetWidth;
property Height: Integer read GetHeight;
property ShowMode: TGlyphShowMode read FShowMode write SetShowMode;
public
property OnChange: TNotifyEvent read FOnChange write FOnChange;
end;
@ -140,9 +149,11 @@ type
FMargin: integer;
FSpacing: Integer;
function GetGlyph: TBitmap;
function GetGlyphShowMode: TGlyphShowMode;
function GetNumGlyphs: Integer;
function IsGlyphStored: Boolean;
procedure SetGlyph(AValue: TBitmap);
procedure SetGlyphShowMode(const AValue: TGlyphShowMode);
procedure SetKind(AValue: TBitBtnKind);
procedure SetLayout(AValue: TButtonLayout);
procedure SetMargin(const AValue: integer);
@ -165,6 +176,7 @@ type
procedure Click; override;
procedure LoadGlyphFromLazarusResource(const AName: String);
procedure LoadGlyphFromStock(idButton: Integer);
function CanShowGlyph: Boolean;
public
property Glyph: TBitmap read GetGlyph write SetGlyph stored IsGlyphStored;
property NumGlyphs: Integer read GetNumGlyphs write SetNumGlyphs default 1;
@ -172,6 +184,7 @@ type
property Layout: TButtonLayout read FLayout write SetLayout default blGlyphLeft;
property Margin: integer read FMargin write SetMargin default -1;
property Spacing: Integer read FSpacing write SetSpacing default 3;
property GlyphShowMode: TGlyphShowMode read GetGlyphShowMode write SetGlyphShowMode default gsmApplication;
end;
{ TBitBtn }
@ -193,6 +206,7 @@ type
property Enabled;
property Font;
property Glyph;
property GlyphShowMode;
property Kind;
property Layout;
property Margin;

View File

@ -66,11 +66,21 @@ begin
Buttons.LoadGlyphFromStock(FButtonGlyph, idButton);
end;
function TCustomBitBtn.CanShowGlyph: Boolean;
begin
Result := not Glyph.Empty and (FButtonGlyph.Images <> nil);
end;
function TCustomBitBtn.GetGlyph : TBitmap;
begin
Result := FButtonGlyph.Glyph;
end;
function TCustomBitBtn.GetGlyphShowMode: TGlyphShowMode;
begin
Result := FButtonGlyph.ShowMode;
end;
function TCustomBitBtn.GetNumGlyphs: Integer;
begin
Result := FButtonGlyph.FNumGlyphs;
@ -90,12 +100,15 @@ begin
AdjustSize;
end;
procedure TCustomBitBtn.SetGlyphShowMode(const AValue: TGlyphShowMode);
begin
FButtonGlyph.ShowMode := AValue;
end;
procedure TCustomBitBtn.GlyphChanged(Sender: TObject);
begin
if HandleAllocated
then begin
if HandleAllocated then
TWSBitBtnClass(WidgetSetClass).SetGlyph(Self, FButtonGlyph);
end;
InvalidatePreferredSize;
AdjustSize;
end;
@ -103,10 +116,12 @@ end;
procedure TCustomBitBtn.ActionChange(Sender: TObject; CheckDefaults: Boolean);
begin
inherited ActionChange(Sender,CheckDefaults);
if Sender is TCustomAction then begin
with TCustomAction(Sender) do begin
if (Glyph.Empty) and (ActionList <> nil) and (ActionList.Images <> nil)
and (ImageIndex >= 0) and (ImageIndex < ActionList.Images.Count) then
if Sender is TCustomAction then
begin
with TCustomAction(Sender) do
begin
if (Glyph.Empty) and (ActionList <> nil) and (ActionList.Images <> nil) and
(ImageIndex >= 0) and (ImageIndex < ActionList.Images.Count) then
ActionList.Images.GetBitmap(ImageIndex, Glyph);
end;
end;

View File

@ -63,11 +63,12 @@ end;
{------------------------------------------------------------------------------}
{ TButtonGlyph Constructor }
{ TButtonGlyph Constructor }
{------------------------------------------------------------------------------}
constructor TButtonGlyph.Create;
begin
FImagesCache := nil;
FShowMode:= gsmApplication;
FOriginal := TGlyphBitmap.Create(Self);
FOriginal.OnChange := @GlyphChanged;
end;
@ -151,6 +152,13 @@ begin
GlyphChanged(FOriginal);
end;
procedure TButtonGlyph.SetShowMode(const AValue: TGlyphShowMode);
begin
if FShowMode = AValue then Exit;
FShowMode := AValue;
GlyphChanged(FOriginal);
end;
function TButtonGlyph.GetHeight: Integer;
begin
if FImages <> nil then
@ -168,6 +176,22 @@ begin
end;
procedure TButtonGlyph.GlyphChanged(Sender: TObject);
function CanShow: Boolean;
begin
case ShowMode of
gsmAlways: Result := True;
gsmNever: Result := False;
gsmApplication: Result := True; // Application.ShowGlyphs
gsmSystem:
{$ifdef Windows}
Result := False;
{$else}
Result := True;
{$endif}
end;
end;
begin
if FImagesCache <> nil then
begin
@ -176,7 +200,7 @@ begin
ClearImages;
end;
if (FOriginal.Width > 0) and (FOriginal.Height > 0) then
if CanShow and (FOriginal.Width > 0) and (FOriginal.Height > 0) then
begin
FImagesCache := GetImageListCache;
FImagesCache.RegisterListener(Self);

View File

@ -90,8 +90,11 @@ class procedure TCarbonWSBitBtn.SetGlyph(const ABitBtn: TCustomBitBtn;
const AValue: TButtonGlyph);
begin
if not CheckHandle(ABitBtn, Self, 'SetGlyph') then Exit;
TCarbonBitBtn(ABitBtn.Handle).SetGlyph(AValue.Glyph);
if ABitBtn.CabShowGlyph then
TCarbonBitBtn(ABitBtn.Handle).SetGlyph(AValue.Glyph)
else
TCarbonBitBtn(ABitBtn.Handle).SetGlyph(nil);
end;
{------------------------------------------------------------------------------

View File

@ -285,12 +285,18 @@ begin
WidgetInfo := GetWidgetInfo(Pointer(ABitBtn.Handle));
BitBtnInfo := WidgetInfo^.UserData;
AGlyph := TBitmap.Create;
AValue.GetImageIndexAndEffect(AButtonState, AIndex, AEffect);
if (AIndex <> -1) and (AValue.Images <> nil) then
AValue.Images.GetBitmap(AIndex, AGlyph, AEffect);
if ABitBtn.CanShowGlyph then
begin
AGlyph := TBitmap.Create;
AValue.GetImageIndexAndEffect(AButtonState, AIndex, AEffect);
if (AIndex <> -1) and (AValue.Images <> nil) then
AValue.Images.GetBitmap(AIndex, AGlyph, AEffect);
end
else
AGlyph := nil;
// check if an image is needed
if AGlyph.Empty
if (AGlyph = nil) or AGlyph.Empty
then begin
if BitBtnInfo^.ImageWidget <> nil
then begin

View File

@ -75,12 +75,17 @@ begin
WidgetInfo := GetWidgetInfo(Pointer(ABitBtn.Handle));
BitBtnInfo := WidgetInfo^.UserData;
AGlyph := TBitmap.Create;
AValue.GetImageIndexAndEffect(AButtonState, AIndex, AEffect);
if (AIndex <> -1) and (AValue.Images <> nil) then
AValue.Images.GetBitmap(AIndex, AGlyph, AEffect);
if ABitBtn.CanShowGlyph then
begin
AGlyph := TBitmap.Create;
AValue.GetImageIndexAndEffect(AButtonState, AIndex, AEffect);
if (AIndex <> -1) and (AValue.Images <> nil) then
AValue.Images.GetBitmap(AIndex, AGlyph, AEffect);
end
else
AGlyph := nil;
// check if an image is needed
if AGlyph.Empty
if (AGlyph = nil) or AGlyph.Empty
then begin
if BitBtnInfo^.ImageWidget <> nil
then begin

View File

@ -85,28 +85,32 @@ var
Mode: QIconMode;
ASize: TSize;
begin
if not WSCheckHandleAllocated(ABitBtn, 'SetGlyph') or
not Assigned(AValue.Images) then
if not WSCheckHandleAllocated(ABitBtn, 'SetGlyph') then
Exit;
AIcon := QIcon_create();
AGlyph := TBitmap.Create;
APixmap := QPixmap_create();
for Mode := QIconNormal to QIconSelected do
if ABitBtn.CanShowGlyph then
begin
AValue.GetImageIndexAndEffect(IconModeToButtonState[Mode], AIndex, AEffect);
AValue.Images.GetBitmap(AIndex, AGlyph, AEffect);
QPixmap_fromImage(APixmap, TQtImage(AGlyph.Handle).Handle);
QIcon_addPixmap(AIcon, APixmap, Mode, QIconOn);
end;
QPixmap_destroy(APixmap);
AGlyph.Free;
AGlyph := TBitmap.Create;
APixmap := QPixmap_create();
for Mode := QIconNormal to QIconSelected do
begin
AValue.GetImageIndexAndEffect(IconModeToButtonState[Mode], AIndex, AEffect);
AValue.Images.GetBitmap(AIndex, AGlyph, AEffect);
QPixmap_fromImage(APixmap, TQtImage(AGlyph.Handle).Handle);
QIcon_addPixmap(AIcon, APixmap, Mode, QIconOn);
end;
QPixmap_destroy(APixmap);
AGlyph.Free;
ASize.cx := AValue.Images.Width;
ASize.cy := AValue.Images.Height;
TQtAbstractButton(ABitBtn.Handle).setIconSize(@ASize);
end;
ASize.cx := AValue.Images.Width;
ASize.cy := AValue.Images.Height;
TQtAbstractButton(ABitBtn.Handle).setIconSize(@ASize);
TQtAbstractButton(ABitBtn.Handle).setIcon(AIcon);
QIcon_destroy(AIcon);
end;
end.

View File

@ -234,16 +234,16 @@ var
begin
// gather info about bitbtn
BitBtnHandle := BitBtn.Handle;
if BitBtn.Glyph.Empty then
begin
srcWidth := 0;
srcHeight := 0;
end else
if BitBtn.CanShowGlyph then
begin
srcWidth := BitBtn.Glyph.Width;
srcHeight := BitBtn.Glyph.Height;
if BitBtn.NumGlyphs > 1 then
srcWidth := srcWidth div BitBtn.NumGlyphs;
end else
begin
srcWidth := 0;
srcHeight := 0;
end;
BitBtnLayout := BitBtn.Layout;
BitBtnDC := GetDC(BitBtnHandle);

View File

@ -159,15 +159,16 @@ begin
InflateRect(DrawRect, -4, -4);
ButtonCaption := PWideChar(UTF8Decode(BitBtn.Caption));
// gather info about bitbtn
if BitBtn.Glyph.Empty then
begin
srcWidth := 0;
srcHeight := 0;
end else
if BitBtn.CanShowGlyph then
begin
srcWidth := TBitBtnAceess(BitBtn).FButtonGlyph.Images.Width;
srcHeight := TBitBtnAceess(BitBtn).FButtonGlyph.Images.Height;
end else
begin
srcWidth := 0;
srcHeight := 0;
end;
BitBtnLayout := BitBtn.Layout;