(* GTK-Kylix Library: GTKPixbuf - Image handling routines Copyright (C) 2003 Tomas Bzatek This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307 USA. *) unit GTKPixbuf; interface uses lazglib2, lazgobject2, lazgdk3, lazgtk3, lazgdkpixbuf2, Classes, GTKControls; type TGTKIconSize = (isInvalid, isMenu, isSmallToolbar, isLargeToolbar, isButton, isDND, isDialog); (****************************************** TGDKPIXBUF **************************************************************************) TGDKPixbuf = class (TComponent) private function GetWidth: integer; function GetHeight: integer; function GetBPP: integer; protected public FPixbuf: PGdkPixbuf; constructor Create(AOwner: TComponent); override; destructor Destroy; override; function LoadFromFile(const FileName: string): boolean; function LoadFromResource(ResourcePath: string): boolean; function LoadFromIconTheme(IconName: string; IconSize: Integer): boolean; procedure Fill(const Pixel: Cardinal); procedure ScaleSimple(const DestWidth, DestHeight: integer); function Copy: PGdkPixbuf; procedure CopyArea(Source: TGDKPixbuf; SourceX, SourceY, SourceWidth, SourceHeight, DestX, DestY: integer); published property Width: integer read GetWidth; property Height: integer read GetHeight; property BPP: integer read GetBPP; end; (****************************************** TGTKIMAGE ***************************************************************************) TGTKImage = class(TGTKControl) private protected public constructor Create(AOwner: TComponent); override; destructor Destroy; override; procedure LoadFromFile(const FileName: string); procedure SetFromPixbuf(Pixbuf: TGDKPixbuf); function GetPixbuf: PGdkPixbuf; procedure CopyFromPixbuf(Pixbuf: TGDKPixbuf); procedure SetFromIconName(IconName: string; IconSize: TGTKIconSize); end; (********************************************************************************************************************************) (********************************************************************************************************************************) implementation uses SysUtils, DateUtils; (********************************************************************************************************************************) (********************************************************************************************************************************) constructor TGDKPixbuf.Create(AOwner: TComponent); begin inherited Create(AOwner); FPixbuf := nil; end; destructor TGDKPixbuf.Destroy; begin if FPixbuf <> nil then g_object_unref(FPixbuf); inherited Destroy; end; function TGDKPixbuf.GetWidth: integer; begin Result := gdk_pixbuf_get_width(FPixbuf); end; function TGDKPixbuf.GetHeight: integer; begin Result := gdk_pixbuf_get_height(FPixbuf); end; function TGDKPixbuf.GetBPP: integer; begin Result := gdk_pixbuf_get_bits_per_sample(FPixbuf); end; function TGDKPixbuf.LoadFromFile(const FileName: string): boolean; var P: Pointer; Error: PGError; begin Error := nil; P := gdk_pixbuf_new_from_file(PChar(FileName), @Error); Result := P <> nil; if P <> nil then FPixbuf := P; end; function TGDKPixbuf.LoadFromResource(ResourcePath: string): boolean; var P: Pointer; Error: PGError; begin Error := nil; P := gdk_pixbuf_new_from_resource(PChar(ResourcePath), @Error); Result := P <> nil; if Error <> nil then begin WriteLn('TGDKPixbuf.LoadFromResource error: ', Error^.message); g_error_free(Error); end; if P <> nil then FPixbuf := P; end; function TGDKPixbuf.LoadFromIconTheme(IconName: string; IconSize: Integer): boolean; var P: Pointer; Error: PGError; begin Error := nil; P := gtk_icon_theme_load_icon(gtk_icon_theme_get_default(), PChar(IconName), IconSize, [], @Error); Result := P <> nil; if Error <> nil then begin WriteLn('TGDKPixbuf.LoadFromIconTheme error: ', Error^.message); g_error_free(Error); end; if P <> nil then FPixbuf := P; end; procedure TGDKPixbuf.Fill(const Pixel: Cardinal); begin gdk_pixbuf_fill(FPixbuf, Pixel); end; procedure TGDKPixbuf.ScaleSimple(const DestWidth, DestHeight: integer); begin FPixbuf := gdk_pixbuf_scale_simple(FPixbuf, DestWidth, DestHeight, GDK_INTERP_BILINEAR); end; function TGDKPixbuf.Copy: PGdkPixbuf; begin Result := gdk_pixbuf_copy(FPixbuf); end; procedure TGDKPixbuf.CopyArea(Source: TGDKPixbuf; SourceX, SourceY, SourceWidth, SourceHeight, DestX, DestY: integer); begin gdk_pixbuf_copy_area(Source.FPixbuf, SourceX, SourceY, SourceWidth, SourceHeight, FPixbuf, DestX, DestY); end; (********************************************************************************************************************************) (********************************************************************************************************************************) constructor TGTKImage.Create(AOwner: TComponent); begin inherited Create(AOwner); FWidget := gtk_image_new; Show; end; destructor TGTKImage.Destroy; begin inherited Destroy; end; procedure TGTKImage.LoadFromFile(const FileName: string); begin gtk_image_set_from_file(PGtkImage(FWidget), PChar(FileName)); end; function TGTKImage.GetPixbuf: PGdkPixbuf; begin Result := gtk_image_get_pixbuf(PGtkImage(FWidget)); end; procedure TGTKImage.SetFromPixbuf(Pixbuf: TGDKPixbuf); begin gtk_image_set_from_pixbuf(PGtkImage(FWidget), Pixbuf.FPixbuf); end; procedure TGTKImage.CopyFromPixbuf(Pixbuf: TGDKPixbuf); begin gtk_image_set_from_pixbuf(PGtkImage(FWidget), Pixbuf.Copy); end; procedure TGTKImage.SetFromIconName(IconName: string; IconSize: TGTKIconSize); begin gtk_image_set_from_icon_name(PGtkImage(FWidget), PChar(IconName), lazgtk3.TGtkIconSize(IconSize)); end; (********************************************************************************************************************************) end.