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
|
{ $Id: win32wsimglist.pp 57164 2018-01-27 18:12:35Z ondrej $}
{
*****************************************************************************
* Win32WSImgList.pp *
* ----------------- *
* *
* *
*****************************************************************************
*****************************************************************************
This file is part of the Lazarus Component Library (LCL)
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
}
unit Win32WSImgList;
{$mode objfpc}{$H+}
interface
uses
////////////////////////////////////////////////////
// I M P O R T A N T
////////////////////////////////////////////////////
// To get as little as posible circles,
// uncomment only when needed for registration
////////////////////////////////////////////////////
// rtl
CommCtrl, Windows, SysUtils, Classes,
// lcl
ImgList, GraphType, Graphics, LCLType,
// ws
Win32Extra, Win32Int, Win32Proc, InterfaceBase,
WSImgList, WSLCLClasses, WSProc, WSReferences;
type
{ TWin32WSCustomImageList }
TWin32WSCustomImageListResolution = class(TWSCustomImageListResolution)
protected
class procedure AddData(AListHandle: TLCLIntfHandle;
ACount, AReplaceIndex, AWidth, AHeight: Integer; AData: PRGBAQuad);
published
class procedure Clear(AList: TCustomImageListResolution); override;
class function CreateReference(AList: TCustomImageListResolution; ACount, AGrow, AWidth,
AHeight: Integer; AData: PRGBAQuad): TWSCustomImageListReference; override;
class procedure Delete(AList: TCustomImageListResolution; AIndex: Integer); override;
class procedure DestroyReference(AComponent: TComponent); override;
class procedure Draw(AList: TCustomImageListResolution; AIndex: Integer; ACanvas: TCanvas;
ABounds: TRect; ABkColor, ABlendColor: TColor; ADrawEffect: TGraphicsDrawEffect;
AStyle: TDrawingStyle; AImageType: TImageType); override;
class procedure DrawToDC(AList: TCustomImageListResolution; AIndex: Integer; ADC: HDC;
ABounds: TRect; ABkColor, ABlendColor: TColor; ADrawEffect: TGraphicsDrawEffect;
AStyle: TDrawingStyle; AImageType: TImageType);
class procedure Insert(AList: TCustomImageListResolution; AIndex: Integer; AData: PRGBAQuad); override;
class procedure Move(AList: TCustomImageListResolution; ACurIndex, ANewIndex: Integer); override;
class procedure Replace(AList: TCustomImageListResolution; AIndex: Integer; AData: PRGBAQuad); override;
end;
implementation
uses
intfgraphics;
const
DRAWINGSTYLEMAP: array[TDrawingStyle] of DWord = (
{ dsFocus } ILD_FOCUS,
{ dsSelected } ILD_SELECTED,
{ dsNormal } ILD_NORMAL,
{ dsTransparent } ILD_TRANSPARENT
);
IMAGETPYEMAP: array[TImageType] of DWord = (
{ itImage } ILD_NORMAL,
{ itMask } ILD_MASK
);
function ColorToImagelistColor(AColor: TColor): TColorRef;
begin
case AColor of
clNone: Result := CLR_NONE;
clDefault: Result := CLR_DEFAULT;
else
Result := ColorToRGB(AColor);
end;
end;
function GetColorDepth(ADC: HDC): Integer; inline; overload;
begin
Result := GetDeviceCaps(ADC, BITSPIXEL) * GetDeviceCaps(ADC, PLANES);
end;
function GetColorDepth: Integer; inline; overload;
var
DC: HDC;
begin
DC := GetDC(0);
Result := GetColorDepth(DC);
ReleaseDC(0, DC);
end;
class procedure TWin32WSCustomImageListResolution.AddData(AListHandle: TLCLIntfHandle;
ACount, AReplaceIndex, AWidth, AHeight: Integer; AData: PRGBAQuad);
procedure DoAddAlpha;
var
Info: Windows.TBitmapInfo;
BitsPtr: Pointer;
bmp: HBITMAP;
DC: HDC;
DataCount, DataSize: Integer;
begin
FillChar(Info, SizeOf(Info), 0);
Info.bmiHeader.biSize := SizeOf(Info.bmiHeader);
Info.bmiHeader.biWidth := AWidth;
Info.bmiHeader.biHeight := -AHeight; // request top down
Info.bmiHeader.biPlanes := 1;
Info.bmiHeader.biBitCount := 32;
Info.bmiHeader.biCompression := BI_RGB;
BitsPtr := nil;
DC := GetDC(0);
bmp := Windows.CreateDIBSection(DC, Info, DIB_RGB_COLORS, BitsPtr, 0, 0);
ReleaseDC(0, DC);
if BitsPtr = nil
then begin
DeleteObject(bmp);
Exit;
end;
DataCount := AWidth * AHeight;
DataSize := DataCount * SizeOf(AData^);
while ACount > 0 do
begin
System.Move(AData^, BitsPtr^, DataSize);
if AReplaceIndex = -1
then ImageList_Add(AListHandle, bmp, 0)
else ImageList_Replace(AListHandle, AReplaceIndex, bmp, 0);
Inc(AData, DataCount);
Dec(ACount);
end;
DeleteObject(bmp);
end;
procedure DoAdd;
var
Info: Windows.TBitmapInfo;
BitsPtr, MaskPtr: Pointer;
P, LinePtr: PByte;
bmp, msk: HBITMAP;
DC: HDC;
DataCount, DataSize, x, y, MaskStride: Integer;
begin
FillChar(Info, SizeOf(Info), 0);
Info.bmiHeader.biSize := SizeOf(Info.bmiHeader);
Info.bmiHeader.biWidth := AWidth;
Info.bmiHeader.biHeight := -AHeight; // request top down
Info.bmiHeader.biPlanes := 1;
Info.bmiHeader.biBitCount := 32;
Info.bmiHeader.biCompression := BI_RGB;
BitsPtr := nil;
MaskPtr := nil;
msk := 0;
bmp := 0;
DC := GetDC(0);
bmp := Windows.CreateDIBSection(DC, Info, DIB_RGB_COLORS, BitsPtr, 0, 0);
ReleaseDC(0, DC);
if (bmp = 0) or (BitsPtr = nil) then
begin
DeleteObject(bmp);
Exit;
end;
DataCount := AWidth * AHeight;
DataSize := DataCount * SizeOf(AData^);
MaskStride := ((AWidth + 15) shr 4) shl 1; // align to Word
MaskPtr := AllocMem(AHeight * MaskStride);
while ACount > 0 do
begin
System.Move(AData^, BitsPtr^, DataSize);
// create mask
LinePtr := MaskPtr;
for y := 1 to AHeight do
begin
p := LinePtr;
for x := 1 to AWidth do
begin
P^ := (P^ and $FE) or ((not AData^.Alpha) shr 7);
if x and $7 = 0
then Inc(p)
else P^ := Byte(P^ shl 1);
Inc(AData);
end;
// finish mask shifting
if (AWidth and $7) <> 0 then
P^ := P^ shl (7 - (AWidth and 7));
Inc(LinePtr, MaskStride);
end;
msk := CreateBitmap(AWidth, AHeight, 1, 1, MaskPtr);
if AReplaceIndex = -1
then ImageList_Add(AListHandle, bmp, msk)
else ImageList_Replace(AListHandle, AReplaceIndex, bmp, msk);
Dec(ACount);
DeleteObject(msk);
end;
FreeMem(MaskPtr);
DeleteObject(bmp);
end;
begin
if Win32WidgetSet.CommonControlsVersion >= ComCtlVersionIE6
then DoAddAlpha
else DoAdd;
end;
class procedure TWin32WSCustomImageListResolution.Clear(
AList: TCustomImageListResolution);
begin
if not WSCheckReferenceAllocated(AList, 'Clear')
then Exit;
ImageList_SetImageCount(AList.Reference._Handle, 0);
end;
class function TWin32WSCustomImageListResolution.CreateReference(
AList: TCustomImageListResolution; ACount, AGrow, AWidth, AHeight: Integer;
AData: PRGBAQuad): TWSCustomImageListReference;
var
Flags: DWord;
begin
if Win32WidgetSet.CommonControlsVersion >= ComCtlVersionIE6
then begin
Flags := ILC_COLOR32;
end
else begin
case GetColorDepth of
04: FLAGS := ILC_COLOR4 or ILC_MASK;
08: FLAGS := ILC_COLOR8 or ILC_MASK;
16: FLAGS := ILC_COLOR16 or ILC_MASK;
24: FLAGS := ILC_COLOR24 or ILC_MASK;
32: FLAGS := ILC_COLOR32 or ILC_MASK;
else
FLAGS := ILC_COLOR or ILC_MASK;
end;
end;
{$WARNINGS OFF}
Result._Init(ImageList_Create(AWidth, AHeight, Flags, ACount, AGrow));
if Result.Allocated and (ACount > 0) then
AddData(Result._Handle, ACount, -1, AWidth, AHeight, AData);
{$WARNINGS ON}
end;
class procedure TWin32WSCustomImageListResolution.Delete(
AList: TCustomImageListResolution; AIndex: Integer);
begin
if not WSCheckReferenceAllocated(AList, 'Delete')
then Exit;
ImageList_Remove(AList.Reference._Handle, AIndex);
end;
class procedure TWin32WSCustomImageListResolution.DestroyReference(AComponent: TComponent);
begin
if not WSCheckReferenceAllocated(TCustomImageListResolution(AComponent), 'DestroyReference')
then Exit;
ImageList_Destroy(TCustomImageListResolution(AComponent).Reference._Handle);
end;
class procedure TWin32WSCustomImageListResolution.Draw(AList: TCustomImageListResolution;
AIndex: Integer; ACanvas: TCanvas; ABounds: TRect; ABkColor,
ABlendColor: TColor; ADrawEffect: TGraphicsDrawEffect; AStyle: TDrawingStyle;
AImageType: TImageType);
begin
if not WSCheckReferenceAllocated(AList, 'Draw')
then Exit;
DrawToDC(AList, AIndex, ACanvas.Handle, ABounds, ABkColor, ABlendColor, ADrawEffect, AStyle, AImageType);
end;
class procedure TWin32WSCustomImageListResolution.DrawToDC(
AList: TCustomImageListResolution; AIndex: Integer; ADC: HDC; ABounds: TRect;
ABkColor, ABlendColor: TColor; ADrawEffect: TGraphicsDrawEffect;
AStyle: TDrawingStyle; AImageType: TImageType);
var
DrawParams: TImageListDrawParams;
RawImg: TRawImage;
ListImg, DeviceImg: TLazIntfImage;
OldBmp, ImgHandle, MskHandle: HBitmap;
ImgDC: HDC;
HasComCtl6: Boolean;
begin
HasComCtl6 := Win32WidgetSet.CommonControlsVersion >= ComCtlVersionIE6;
// If we are using comctl > 6 then COLOR_32 is supported and alpha bitmaps will
// be drawn correctly. If version is lower than our alpha bitmaps will be drawn
// with mask and with no alpha. But if we draw with effect different fron normal
// we will draw using another method with alpha even using comctl < 6. To prevent
// such inconsistency in drawing lets check whether we need alpha drawing first
// and whether imagelist has native alpha drawing. If it has then we will use
// ImageList_DrawEx in other case we will draw alpha bitmap ourself.
if (ADrawEffect = gdeNormal) and (HasComCtl6 or (GetColorDepth(ADC) < 32)) then
begin
ImageList_DrawEx(AList.Reference._Handle, AIndex, ADC, ABounds.Left,
ABounds.Top, ABounds.Right, ABounds.Bottom, ColorToImagelistColor(ABkColor),
ColorToImagelistColor(ABlendColor), DRAWINGSTYLEMAP[AStyle] or IMAGETPYEMAP[AImageType]);
end
else
if (ADrawEffect = gdeDisabled) and HasComCtl6 then
begin
// if it is manifested exe then use winXP algoriphm of gray painting
FillChar(DrawParams, SizeOf(DrawParams), 0);
DrawParams.cbSize := SizeOf(DrawParams);
DrawParams.himl := AList.Reference._Handle;
DrawParams.i := AIndex;
DrawParams.hdcDst := ADC;
DrawParams.x := ABounds.Left;
DrawParams.y := ABounds.Top;
DrawParams.cx := ABounds.Right;
DrawParams.cy := ABounds.Bottom;
DrawParams.rgbBk := ColorToImagelistColor(ABkColor);
DrawParams.rgbFg := ColorToImagelistColor(ABlendColor);
DrawParams.fStyle := DRAWINGSTYLEMAP[AStyle] or IMAGETPYEMAP[AImageType];
DrawParams.fState := ILS_SATURATE; // draw greyed
ImageList_DrawIndirect(@DrawParams);
end
else
begin
if ABounds.Right = 0 then
ABounds.Right := AList.Width;
if ABounds.Bottom = 0 then
ABounds.Bottom := AList.Height;
// use RawImage_PerformEffect to perform drawing effect
AList.GetRawImage(AIndex, RawImg);
RawImg.PerformEffect(ADrawEffect, True);
if not Widgetset.RawImage_CreateBitmaps(RawImg, ImgHandle, MskHandle, True)
then begin
// bummer, the widgetset doesn't support our 32bit format, try device
ListImg := TLazIntfImage.Create(RawImg, False);
DeviceImg := TLazIntfImage.Create(0,0,[]);
DeviceImg.DataDescription := GetDescriptionFromDevice(0, AList.Width, AList.Height);
DeviceImg.CopyPixels(ListImg);
DeviceImg.GetRawImage(RawImg);
Widgetset.RawImage_CreateBitmaps(RawImg, ImgHandle, MskHandle);
DeviceImg.Free;
ListImg.Free;
end;
ImgDC := CreateCompatibleDC(ADC);
OldBmp := SelectObject(ImgDC, ImgHandle);
WidgetSet.StretchMaskBlt(ADC, ABounds.Left, ABounds.Top, ABounds.Right, ABounds.Bottom,
ImgDC, 0, 0, ABounds.Right, ABounds.Bottom, MskHandle, 0, 0, SRCCOPY);
RawImg.FreeData;
SelectObject(ImgDC, OldBmp);
if ImgHandle<>0 then DeleteObject(ImgHandle);
if MskHandle<>0 then DeleteObject(MskHandle);
DeleteDC(ImgDC);
end;
end;
class procedure TWin32WSCustomImageListResolution.Insert(
AList: TCustomImageListResolution; AIndex: Integer; AData: PRGBAQuad);
var
ImageList: HImageList;
Count: Integer;
begin
if not WSCheckReferenceAllocated(AList, 'Insert')
then Exit;
ImageList := AList.Reference._Handle;
Count := ImageList_GetImageCount(ImageList);
if (AIndex <= Count) and (AIndex >= 0) then
begin
AddData(ImageList, 1, -1, AList.Width, AList.Height, AData);
if AIndex <> Count
then Move(AList, Count, AIndex);
end;
end;
class procedure TWin32WSCustomImageListResolution.Move(AList: TCustomImageListResolution;
ACurIndex, ANewIndex: Integer);
var
n: integer;
Handle: THandle;
begin
if not WSCheckReferenceAllocated(AList, 'Move')
then Exit;
if ACurIndex = ANewIndex
then Exit;
Handle := AList.Reference._Handle;
if ACurIndex < ANewIndex
then begin
for n := ACurIndex to ANewIndex - 1 do
ImageList_Copy(Handle, n + 1, Handle, n, ILCF_SWAP);
end
else begin
for n := ACurIndex downto ANewIndex + 1 do
ImageList_Copy(Handle, n - 1, Handle, n, ILCF_SWAP);
end;
end;
class procedure TWin32WSCustomImageListResolution.Replace(
AList: TCustomImageListResolution; AIndex: Integer; AData: PRGBAQuad);
var
ImageList: HImageList;
Count: Integer;
begin
if not WSCheckReferenceAllocated(AList, 'Replace')
then Exit;
ImageList := AList.Reference._Handle;
Count := ImageList_GetImageCount(ImageList);
if (AIndex < Count) and (AIndex >= 0)
then AddData(ImageList, 1, AIndex, AList.Width, AList.Height, AData);
end;
end.
|