summaryrefslogtreecommitdiff
path: root/libgtk_kylix/GTKPixbuf.pas
blob: c199ac4c52f1b245d23e280c6fc1af0c5442c721 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
(*
    GTK-Kylix Library: GTKPixbuf - Image handling routines
    Copyright (C) 2003 Tomas Bzatek <tbzatek@users.sourceforge.net>

    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.