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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
|
{
*****************************************************************************
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
@author(Olivier guilbaud (OG) <golivier@free.fr>), Tomas Gregorovic
@created(24/02/2003)
@lastmod(25/02/2003)
Property editor for TImageList objects
History
26-Feb-2003 OG - Update for use assign.
27-feb-2003 OG - If possible zoom x2 the selected image.
- Fix the superposition of images
27-Jan-2006 TG - Form converted to lfm.
Todo :
- masks and bitmap transparency
}
unit ImageListEditor;
{$MODE OBJFPC}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ComCtrls,
StdCtrls, Buttons, ExtCtrls, Menus, LCLProc, ColorBox, ExtDlgs,
IDEDialogs, PropEdits, ComponentEditors, ObjInspStrConsts, ButtonPanel;
type
TGlyphAdjustment = (gaNone, gaStretch, gaCrop, gaCenter);
PGlyphInfo = ^TGlyphInfo;
TGlyphInfo = record
Bitmap: TBitmap;
Adjustment: TGlyphAdjustment;
TransparentColor: TColor;
end;
{ TImageListEditorDlg }
TImageListEditorDlg = class(TForm)
BtnAdd: TButton;
BtnClear: TButton;
BtnDelete: TButton;
BtnMoveUp: TButton;
BtnMoveDown: TButton;
BtnSave: TButton;
btnSaveAll: TButton;
BtnPanel: TButtonPanel;
ColorBoxTransparent: TColorBox;
GroupBoxL: TGroupBox;
GroupBoxR: TGroupBox;
ImageList: TImageList;
LabelSize: TLabel;
LabelTransparent: TLabel;
OpenDialog: TOpenPictureDialog;
RadioGroup: TRadioGroup;
Preview: TScrollBox;
SaveDialog: TSavePictureDialog;
TreeView: TTreeView;
procedure BtnAddClick(Sender: TObject);
procedure BtnClearClick(Sender: TObject);
procedure BtnDeleteClick(Sender: TObject);
procedure BtnMoveUpClick(Sender: TObject);
procedure btnSaveAllClick(Sender: TObject);
procedure BtnSaveClick(Sender: TObject);
procedure ColorBoxTransparentClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure PreviewPaint(Sender: TObject);
procedure btnApplyClick(Sender: TObject);
procedure TreeViewDeletion(Sender: TObject; Node: TTreeNode);
procedure TreeViewSelectionChanged(Sender: TObject);
private
FImageList: TImageList;
FModified: Boolean;
FPreviewBmp: TBitmap;
procedure SavePicture(Picture: TPicture);
public
procedure LoadFromImageList(AImageList: TImageList);
procedure SaveToImageList;
procedure AddImageToList(const FileName: String);
end;
//Editor call by Lazarus with 1 verbe only
{ TImageListComponentEditor }
TImageListComponentEditor = class(TComponentEditor)
protected
procedure DoShowEditor;
public
procedure ExecuteVerb(Index: Integer); override;
function GetVerb(Index: Integer): String; override;
function GetVerbCount: Integer; override;
end;
implementation
uses
Math, GraphType;
{$R *.lfm}
function EditImageList(AImageList: TImageList): Boolean;
var
ImageListEditorDlg: TImageListEditorDlg;
begin
ImageListEditorDlg := TImageListEditorDlg.Create(Application);
try
ImageListEditorDlg.LoadFromImageList(AImageList);
if ImageListEditorDlg.ShowModal = mrOk then
ImageListEditorDlg.SaveToImageList;
Result := ImageListEditorDlg.FModified;
finally
ImageListEditorDlg.Free;
end;
end;
function CreateGlyph(B: TBitmap; Width, Height: Integer;
Adjustment: TGlyphAdjustment; TransparentColor: TColor = clDefault): TBitmap;
begin
Result := TBitmap.Create;
if (Adjustment = gaNone) then
begin
Result.Assign(B);
end
else
begin
Result.Width := Width;
Result.Height := Height;
Result.Canvas.Brush.Color := TransparentColor;
Result.Canvas.FillRect(Bounds(0, 0, Width, Height));
case Adjustment of
gaStretch: Result.Canvas.StretchDraw(Bounds(0, 0, Width, Height), B);
gaCrop: Result.Canvas.Draw(0, 0, B);
gaCenter: Result.Canvas.Draw((Width - B.Width) div 2, (Height - B.Height) div 2, B);
end;
end;
if TransparentColor = clDefault then
Result.TransparentMode := tmAuto
else
begin
Result.TransparentColor := TransparentColor;
Result.TransparentMode := tmFixed;
end;
Result.Transparent := True;
end;
function CreateGlyphSplit(Src: TBitmap; Width, Height: Integer;
RowIndex, ColIndex: Integer): TBitmap;
var
SrcRect: TRect;
SrcRawImage, DstRawImage: TRawImage;
begin
// Ensure that the returned Bitmap is not bigger than Src
Width := Min(Width, Src.Width);
Height := Min(Height, Src.Height);
SrcRect := Bounds(ColIndex * Width, RowIndex * Height, Width, Height);
// copy raw image, instead of using Canvas functions to preserve transparency
SrcRawImage := Src.RawImage;
DstRawImage.Init;
DstRawImage.Description := SrcRawImage.Description;
DstRawImage.Description.Width := Width;
DstRawImage.Description.Height := Height;
SrcRawImage.ExtractRect(SrcRect, DstRawImage);
Result := TBitmap.Create;
Result.TransparentColor := Src.TransparentColor;
Result.TransparentMode := Src.TransparentMode;
Result.Transparent := True;
Result.LoadFromRawImage(DstRawImage, True);
end;
{ TImageListEditorDlg }
procedure TImageListEditorDlg.FormCreate(Sender: TObject);
begin
Caption := sccsILEdtCaption;
GroupBoxL.Caption := sccsILEdtGrpLCaption;
GroupBoxR.Caption := sccsILEdtGrpRCaption;
BtnAdd.Caption := sccsILEdtAdd;
BtnDelete.Caption := sccsILEdtDelete;
BtnClear.Caption := sccsILEdtClear;
BtnMoveUp.Caption := sccsILEdtMoveUp;
BtnMoveDown.Caption := sccsILEdtMoveDown;
BtnSave.Caption := sccsILEdtSave;
BtnSaveAll.Caption := sccsILEdtSaveAll;
BtnPanel.HelpButton.Caption := oisHelp;
BtnPanel.OKButton.Caption := oisOK;
BtnPanel.CancelButton.Caption := oisCancel;
BtnPanel.CloseButton.Caption := sccsILEdtApply;
BtnPanel.CloseButton.Kind := bkCustom;
BtnPanel.CloseButton.Glyph := nil;
BtnPanel.CloseButton.ModalResult := mrNone;
BtnPanel.CloseButton.OnClick := @btnApplyClick;
LabelTransparent.Caption := sccsILEdtransparentColor;
RadioGroup.Caption := sccsILEdtAdjustment;
RadioGroup.Items[0] := sccsILEdtNone;
RadioGroup.Items[1] := sccsILEdtStretch;
RadioGroup.Items[2] := sccsILEdtCrop;
RadioGroup.Items[3] := sccsILEdtCenter;
OpenDialog.Title := sccsILEdtOpenDialog;
SaveDialog.Title := sccsILEdtSaveDialog;
end;
procedure TImageListEditorDlg.FormDestroy(Sender: TObject);
begin
FreeAndNil(FPreviewBmp);
end;
procedure TImageListEditorDlg.BtnAddClick(Sender: TObject);
var
I: Integer;
begin
if OpenDialog.Execute then
begin
ImageList.BeginUpdate;
TreeView.BeginUpdate;
try
for I := 0 to OpenDialog.Files.Count - 1 do
AddImageToList(TrimRight(OpenDialog.Files[I]));
finally
TreeView.EndUpdate;
ImageList.EndUpdate;
end;
TreeView.SetFocus;
end;
end;
procedure TImageListEditorDlg.BtnClearClick(Sender: TObject);
begin
ImageList.Clear;
TreeView.Items.Clear;
end;
procedure TImageListEditorDlg.BtnDeleteClick(Sender: TObject);
var
Node: TTreeNode;
I, S: Integer;
begin
if Assigned(TreeView.Selected) then
begin
Node := TreeView.Selected.GetNext;
if Node = nil then Node := TreeView.Selected.GetPrev;
S := TreeView.Selected.ImageIndex;
ImageList.Delete(S);
TreeView.BeginUpdate;
try
TreeView.Selected.Delete;
for I := S to TreeView.Items.Count -1 do
begin
TreeView.Items[I].Text := IntToStr(I);
TreeView.Items[I].ImageIndex := I;
TreeView.Items[I].SelectedIndex := I;
end;
finally
TreeView.EndUpdate;
end;
TreeView.Selected := Node;
end;
TreeView.SetFocus;
end;
procedure TImageListEditorDlg.BtnMoveUpClick(Sender: TObject);
var
S, D: Integer;
P: PGlyphInfo;
begin
if Assigned(TreeView.Selected) and (TreeView.Items.Count > 1) then
begin
S := TreeView.Selected.ImageIndex;
D := (Sender as TControl).Tag;
if (S + D >= 0) and (S + D < TreeView.Items.Count) then
begin
ImageList.Move(S, S + D);
P := TreeView.Items[S + D].Data;
TreeView.Items[S + D].Data := TreeView.Items[S].Data;
TreeView.Items[S].Data := P;
TreeView.Selected := TreeView.Items[S + D];
TreeView.SetFocus;
end;
end;
end;
procedure TImageListEditorDlg.SavePicture(Picture: TPicture);
var
FileName, Ext: String;
begin
if SaveDialog.Execute then
begin
FileName := SaveDialog.FileName;
if ExtractFileExt(FileName) = '' then
begin
Ext := SaveDialog.GetFilterExt;
if Ext = '' then
Ext := 'bmp';
if Ext <> '' then
FileName := FileName + '.' + Ext;
end;
Picture.SaveToFile(FileName);
end;
end;
procedure TImageListEditorDlg.btnSaveAllClick(Sender: TObject);
var
Picture: TPicture;
begin
if (ImageList.Count > 0) then
begin
Picture := TPicture.Create;
try
ImageList.GetFullBitmap(Picture.Bitmap);
SavePicture(Picture);
finally
Picture.Free;
end;
end;
end;
procedure TImageListEditorDlg.BtnSaveClick(Sender: TObject);
var
Picture: TPicture;
begin
if Assigned(TreeView.Selected) then
begin
Picture := TPicture.Create;
try
ImageList.GetBitmap(TreeView.Selected.ImageIndex, Picture.Bitmap);
SavePicture(Picture);
finally
Picture.Free;
end;
end;
end;
procedure TImageListEditorDlg.ColorBoxTransparentClick(Sender: TObject);
var
P: PGlyphInfo;
T: TBitmap;
begin
if Assigned(TreeView.Selected) then
begin
if Assigned(TreeView.Selected.Data) then
begin
P := PGlyphInfo(TreeView.Selected.Data);
P^.Adjustment := TGlyphAdjustment(RadioGroup.ItemIndex);
P^.TransparentColor := ColorBoxTransparent.Selected;
T := CreateGlyph(P^.Bitmap, ImageList.Width, ImageList.Height, P^.Adjustment,
P^.TransparentColor);
ImageList.BeginUpdate;
try
ImageList.Delete(TreeView.Selected.ImageIndex);
ImageList.Insert(TreeView.Selected.ImageIndex, T, nil);
finally
ImageList.EndUpdate;
T.Free;
end;
TreeView.Invalidate;
TreeViewSelectionChanged(nil);
end
end;
end;
procedure TImageListEditorDlg.PreviewPaint(Sender: TObject);
begin
if Assigned(FPreviewBmp) then
begin
Preview.Canvas.Draw(0, 0, FPreviewBmp);
end;
end;
procedure TImageListEditorDlg.btnApplyClick(Sender: TObject);
begin
SaveToImageList;
end;
procedure TImageListEditorDlg.TreeViewDeletion(Sender: TObject; Node: TTreeNode);
var
P: PGlyphInfo;
begin
if Assigned(Node) then
begin
if Node.Data <> nil then
begin
P := PGlyphInfo(Node.Data);
P^.Bitmap.Free;
Dispose(P);
end;
end;
end;
procedure TImageListEditorDlg.TreeViewSelectionChanged(Sender: TObject);
var
P: PGlyphInfo;
begin
if Assigned(TreeView.Selected) then
begin
if Assigned(FPreviewBmp) then FPreviewBmp.Free;
FPreviewBmp := TBitmap.Create;
ImageList.GetBitmap(TreeView.Selected.ImageIndex, FPreviewBmp);
if Assigned(TreeView.Selected.Data) then
begin
P := PGlyphInfo(TreeView.Selected.Data);
RadioGroup.Enabled := True;
RadioGroup.OnClick := nil;
RadioGroup.ItemIndex := Integer(P^.Adjustment);
RadioGroup.OnClick := @ColorBoxTransparentClick;
ColorBoxTransparent.Enabled := True;
ColorBoxTransparent.OnChange := nil;
ColorBoxTransparent.Selected := P^.TransparentColor;
ColorBoxTransparent.OnChange := @ColorBoxTransparentClick;
end
else
begin
RadioGroup.Enabled := False;
RadioGroup.OnClick := nil;
RadioGroup.ItemIndex := 0;
RadioGroup.OnClick := @ColorBoxTransparentClick;
ColorBoxTransparent.Enabled := False;
ColorBoxTransparent.OnChange := nil;
ColorBoxTransparent.Selected := clFuchsia;
ColorBoxTransparent.OnChange := @ColorBoxTransparentClick;
end;
LabelSize.Caption := Format('%d x %d', [FPreviewBmp.Width, FPreviewBmp.Height]);
Preview.HorzScrollBar.Range := FPreviewBmp.Width;
Preview.VertScrollBar.Range := FPreviewBmp.Height;
Preview.Invalidate;
end
else
begin
if Assigned(FPreviewBmp) then FreeThenNil(FPreviewBmp);
LabelSize.Caption := '';
RadioGroup.Enabled := False;
RadioGroup.OnClick := nil;
RadioGroup.ItemIndex := 0;
RadioGroup.OnClick := @ColorBoxTransparentClick;
ColorBoxTransparent.Enabled := False;
ColorBoxTransparent.OnChange := nil;
ColorBoxTransparent.Selected := clFuchsia;
ColorBoxTransparent.OnChange := @ColorBoxTransparentClick;
Preview.HorzScrollBar.Range := ImageList.Width;
Preview.VertScrollBar.Range := ImageList.Height;
Preview.Invalidate;
end;
end;
procedure TImageListEditorDlg.LoadFromImageList(AImageList: TImageList);
var
I, C: Integer;
begin
ImageList.Clear;
FImageList := AImageList;
FModified := False;
if Assigned(AImageList) then
begin
ImageList.Assign(AImageList);
C := ImageList.Count;
TreeView.BeginUpdate;
try
TreeView.Items.Clear;
for I := 0 to Pred(C) do
begin
with TreeView.Items.Add(nil, IntToStr(I)) do
begin
ImageIndex := I;
SelectedIndex := I;
Data := nil;
end;
end;
finally
TreeView.EndUpdate;
end;
end;
end;
procedure TImageListEditorDlg.SaveToImageList;
begin
FImageList.Assign(ImageList);
FModified := True;
end;
procedure TImageListEditorDlg.AddImageToList(const FileName: String);
var
SrcBmp: TBitmap;
Picture: TPicture;
Node: TTreeNode;
P: PGlyphInfo;
I: Integer;
ImagesPerColumn: Integer;
ImagesPerRow: Integer;
iRow: Integer;
iCol: Integer;
begin
SaveDialog.InitialDir := ExtractFileDir(FileName);
SrcBmp := nil;
Picture := TPicture.Create;
try
Picture.LoadFromFile(FileName);
SrcBmp := TBitmap.Create;
SrcBmp.Assign(Picture.Graphic);
finally
Picture.Free;
end;
if Assigned(SrcBmp) then
begin
if not SrcBmp.Empty then
begin
//If the height and with of SrcBmp is an exact factor of ImageList height and width
//the image can be split into smaller images
if (SrcBmp.Height mod ImageList.Height = 0)
and (SrcBmp.Width mod ImageList.Width = 0) then
begin
ImagesPerColumn := SrcBmp.Height div ImageList.Height;
ImagesPerRow := SrcBmp.Width div ImageList.Width;
end
else
begin
ImagesPerColumn := 1;
ImagesPerRow := 1;
end;
//Ask the user if wants to split the source image
if ((ImagesPerRow > 1) or (ImagesPerColumn > 1))
and (IDEQuestionDialog(Caption +' - '+ btnAdd.Caption,
s_SuggestSplitImage, mtConfirmation,
[mrNo, s_AddAsSingle, mrYes, s_SplitImage]) <> mrYes) then
begin
//"Add as single" was choosen
ImagesPerColumn := 1;
ImagesPerRow := 1;
end;
//Split image or copy the first image list width/height image data if the file
//is bigger than image list width/height but the user choosen "add as single"
for iRow := 0 to ImagesPerColumn - 1 do
for iCol := 0 to ImagesPerRow - 1 do
begin
New(P);
P^.Bitmap := CreateGlyphSplit(SrcBmp, ImageList.Width, ImageList.Height,
iRow, iCol);
P^.Adjustment := gaNone;
P^.TransparentColor := clDefault;
I := ImageList.Add(P^.Bitmap, nil);
Node := TreeView.Items.AddObject(nil, IntToStr(I), P);
Node.ImageIndex := I;
Node.SelectedIndex := I;
TreeView.Selected := Node;
end;
SrcBmp.Free;
end;
end;
end;
{ TImageListComponentEditor }
procedure TImageListComponentEditor.DoShowEditor;
var
Hook: TPropertyEditorHook;
AImg: TImageList;
begin
if GetComponent is TImageList then
begin
AImg := TImageList(GetComponent);
GetHook(Hook);
if EditImageList(AImg) then
if Assigned(Hook) then Hook.Modified(Self);
end;
DebugLn('TImageListComponentEditor.DoShowEditor END ');
end;
procedure TImageListComponentEditor.ExecuteVerb(Index: Integer);
begin
DoShowEditor;
end;
function TImageListComponentEditor.GetVerb(Index: Integer): String;
begin
Result := oisImageListComponentEditor;
end;
function TImageListComponentEditor.GetVerbCount: Integer;
begin
Result := 1;
end;
initialization
//Register a component editor for TImageList
RegisterComponentEditor(TImageList,TImageListComponentEditor);
end.
|