OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
imagelst.cpp
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// ObjectWindows
3// Copyright (c) 1995, 1996 by Borland International, All Rights Reserved
4//
5/// \file
6/// Implementation of class TImageList, an ImageList 'common control' wrapper
7//----------------------------------------------------------------------------
8#include <owl/pch.h>
9#include <owl/imagelst.h>
10
11#if defined(__BORLANDC__)
12# pragma option -w-ccc // Disable "Condition is always true/false"
13#endif
14
15namespace owl {
16
18DIAG_DECLARE_GROUP(OwlCommCtrl); // Common Controls diagnostic group
19
20//----------------------------------------------------------------------------
21
23{
24 memset(this,0,sizeof(IMAGELISTDRAWPARAMS));
25 cbSize = sizeof(IMAGELISTDRAWPARAMS);
26}
27
28//----------------------------------------------------------------------------
29
30//
31/// Constructs an empty ImageList of a given size.
32/// The `imageSize` is the individual image size, not the total size of the list.
33/// The `initCapacity` parameter describes the initial capacity of the list, i.e.
34/// how many images it can hold before it needs to grow (perform a reallocation).
35/// Note that the list remains empty after construction, regardless of capacity.
36/// The `growBy` parameter specifies how much the capacity will grow when needed.
37//
39{
40 if (!initCapacity)
41 initCapacity = 1;
42 ImageSize = TSize(imageSize.cx, imageSize.cy);
43 Handle = ImageList_Create(imageSize.cx, imageSize.cy, flags, initCapacity, growBy);
44 WARNX(OwlCommCtrl, !Handle, 0, "Cannot create ImageList");
45 Bitmap = nullptr;
46 CheckValid();
47}
48
49//
50/// Constructs a TImageList from a bitmap, slicing it up into a horizontal array of
51/// the given number of evenly sized images.
52//
54{
55 if (!imageCount)
56 imageCount = 1;
57 ImageSize = TSize(bmp.Width() / imageCount, bmp.Height());
58 Handle = ImageList_Create(ImageSize.cx, ImageSize.cy, flags, imageCount, growBy);
59 WARNX(OwlCommCtrl, !Handle, 0, "Cannot create ImageList");
60 Bitmap = nullptr;
61 CheckValid();
62
63 // Use masked support with 3dFace color as background color.
64 //
66}
67
68//
69/// Constructs a TImageList from a DIB, slicing the bitmap up into a horizontal array of
70/// the given number of evenly sized images.
71//
73{
74 if (!imageCount)
75 imageCount = 1;
76 ImageSize = TSize(dib.Width() / imageCount, dib.Height());
77 Handle = ImageList_Create(ImageSize.cx, ImageSize.cy, flags, imageCount, growBy);
78 WARNX(OwlCommCtrl, !Handle, 0, "Cannot create ImageList");
79 Bitmap = nullptr;
80 CheckValid();
81
82 // Use masked support with 3dFace color as background color.
83 //
86}
87
88
89//
90/// Constructs an ImageList from a bitmap resource.
91/// `type` must be IMAGE_BITMAP. `flags` can be one of the flags specified for the
92/// ImageList_LoadImage function in the Windows API. The default is LR_CREATEDIBSECTION which will
93/// load the bitmap as a device-independent bitmap (DIB) with no colour mapping performed.
94/// \see http://msdn.microsoft.com/en-us/library/windows/desktop/bb761557.aspx
95//
97{
98 Handle = ImageList_LoadImage(h, id, imageWidth, growBy, mask, type, flags);
99 WARNX(OwlCommCtrl, !Handle, 0, "Cannot create ImageList");
100 ImageSize = TSize(0);
101 Bitmap = nullptr;
102 CheckValid();
103}
104
105
106//
107/// Constructs a C++ alias for an existing imagelist.
108/// The object takes ownership of the given handle, i.e. it will be destroyed in the
109/// destructor at the end of the object's lifetime.
110//
112{
113 Handle = imageList;
114 WARNX(OwlCommCtrl, !Handle, 0, "Cannot create ImageList");
115 int cx = 0;
116 int cy = 0;
117 ImageList_GetIconSize(Handle, &cx, &cy);
118 ImageSize.cx = cx;
119 ImageSize.cy = cy;
120 Bitmap = nullptr;
121 CheckValid();
122}
123
124//
125/// Constructs a wrapper for the current drag imagelist and specifies the location
126/// and hotspot of the imagelist.
127//
129{
130 ImageSize = TSize(0);
131 Handle = ImageList_GetDragImage(&pt, &hotspot);
132 WARNX(OwlCommCtrl, !Handle, 0, "Cannot create ImageList");
133 Bitmap = nullptr;
134 CheckValid();
135}
136
137//
138/// Creates a duplicate of the given image list.
139/// \sa <a href="https://docs.microsoft.com/en-us/windows/win32/api/commctrl/nf-commctrl-imagelist_duplicate">
140/// ImageList_Duplicate</a> in the Windows API.
141//
143 : Handle{ImageList_Duplicate(src)}, Bitmap{}, ImageSize{TImageInfo{src}.GetImageRect().Size()}
144{
145 WARNX(OwlCommCtrl, !Handle, 0, _T("TImageList::TImageList: ImageList_Duplicate failed"));
146 CheckValid();
147}
148
149//
150/// Destructs the ImageList and cleans up the image list handle.
151//
153{
154 ImageList_Destroy(Handle);
155 if (Bitmap)
156 delete Bitmap;
157}
158
159//
160/// Throws an exception if this image list handle is invalid
161//
162void
164{
165 if (!Handle)
167}
168
169//
170/// Replaces the image list by a duplicate of the given image list.
171/// \sa <a href="https://docs.microsoft.com/en-us/windows/win32/api/commctrl/nf-commctrl-imagelist_duplicate">
172/// ImageList_Duplicate</a> in the Windows API.
173//
176{
177 if (this == &src) return *this;
178 const auto d = ImageList_Duplicate(src);
179 WARNX(OwlCommCtrl, !d, 0, _T("TImageList::operator =: ImageList_Duplicate failed"));
180 if (!d) throw TXOwl{_T("TImageList::operator = failed")};
181 if (Handle)
182 {
183 [[maybe_unused]] const auto ok = ImageList_Destroy(Handle) != FALSE;
184 WARNX(OwlCommCtrl, !ok, 0, _T("TImageList::operator =: ImageList_Destroy failed"));
185 }
186 Handle = d;
187 if (Bitmap)
188 {
189 delete Bitmap;
190 Bitmap = nullptr;
191 }
192 ImageSize = TImageInfo{src}.GetImageRect().Size();
193 return *this;
194}
195
196//
197/// Returns the image bitmap used by this ImageList
198//
199TImageList::operator TBitmap&()
200{
201 // Build the bitmap object on the fly if needed
202 // To work around a win95 bug, making whole copy for now.
203 //
204 TImageInfo ii(*this);
205 if (!Bitmap || (HBITMAP)*Bitmap != ii.GetImageBM()) {
206 delete Bitmap;
207 Bitmap = new TBitmap(TScreenDC(), TDib(TBitmap(ii.GetImageBM())));
208 }
209 return *Bitmap;
210}
211
212
213
214//
215/// Draws an image onto a target DC at a given coordinate and with a given style.
216//
217bool
218TImageList::Draw(int index, TDC& dc, int x, int y, uint style, int overlay)
219{
220 PRECONDITION(Handle);
221 if (overlay)
222 style |= INDEXTOOVERLAYMASK(overlay);
223 return ImageList_Draw(Handle, index, dc, x, y, style);
224}
225
226
227//
228/// Extended version of draw that takes a foreground color and background color.
229//
230bool
231TImageList::Draw(int index, TDC& dc, int x, int y, int dx, int dy,
232 const TColor& bgClr, const TColor& fgClr, uint style,
233 int overlay)
234{
235 PRECONDITION(Handle);
236 if (overlay)
237 style |= INDEXTOOVERLAYMASK(overlay);
238 COLORREF bgCr = (bgClr == TColor::None) ? static_cast<COLORREF>(CLR_NONE) : static_cast<COLORREF>(bgClr);
239 COLORREF fgCr = (fgClr == TColor::None) ? static_cast<COLORREF>(CLR_NONE) : static_cast<COLORREF>(fgClr);
240
241 return ImageList_DrawEx(Handle, index, dc, x, y, dx, dy, bgCr, fgCr, style);
242}
243
244//
245/// Returns number of images currently in this ImageList.
246//
247int
249{
250 PRECONDITION(Handle);
251 return ImageList_GetImageCount(Handle);
252}
253
254//
255/// Adds new image(s) to the ImageList. Returns index of new addition. No mask
256/// bitmap is added.
257//
258int
260{
261 PRECONDITION(Handle);
262 return ImageList_Add(Handle, image, nullptr);
263}
264
265
266//
267/// Adds new image/mask pair(s) to the ImageList. Returns index of new addition.
268//
269int
271{
272 PRECONDITION(Handle);
273 return ImageList_Add(Handle, image, mask);
274}
275
276
277//
278/// Adds new image(s) to ImageList, specifying a mask color to generate a mask.
279/// Returns index of the new addition.
280//
281int
283{
284 PRECONDITION(Handle);
285 return ImageList_AddMasked(Handle, image, mskColor);
286}
287
288//
289/// Adds an icon to the ImageList. Returns index of new addition.
290//
291int
293{
294 PRECONDITION(Handle);
295 return ImageList_AddIcon(Handle, icon);
296}
297
298//
299/// Removes an image (or all images if index is -1) from this ImageList.
300//
301bool
303{
304 PRECONDITION(Handle);
305 return ImageList_Remove(Handle, index);
306}
307
308//
309/// Replaces an image in this ImageList.
310//
311bool
313{
314 PRECONDITION(Handle);
315 return ImageList_Replace(Handle, index, image, nullptr);
316}
317
318//
319/// Replaces an image and mask in the ImageList.
320//
321bool
322TImageList::Replace(int index, const TBitmap& image, const TBitmap& mask)
323{
324 PRECONDITION(Handle);
325 return ImageList_Replace(Handle, index, image, mask);
326}
327
328//
329/// Creates and retrieves an icon from an image and mask in the ImageList.
330//
331HICON
332TImageList::GetIcon(int index, uint flags) const
333{
334 PRECONDITION(Handle);
335 return ImageList_GetIcon(Handle, index, flags);
336}
337
338//
339/// Replaces the image at index 'index' with the icon or cursor.
340//
341int
343{
344 PRECONDITION(Handle);
345 return ImageList_ReplaceIcon(Handle, index, icon);
346}
347
348//
349/// Returns the icon size.
350/// \todo What about a TSize TImageList::GetIconSize() version?
351bool
352TImageList::GetIconSize(int& cx, int& cy)
353{
354 PRECONDITION(Handle);
355 return ImageList_GetIconSize(Handle, &cx, &cy);
356}
357
358//
359/// Gets general information about a given image.
360//
361bool
363{
364 PRECONDITION(Handle);
365 return ImageList_GetImageInfo(Handle, index, static_cast<IMAGEINFO*>(&imageInfo));
366}
367
368//
369/// Functional-style overload
370//
373{
374 auto i = TImageInfo{};
375 const auto r = GetImageInfo(index, i);
376 if (!r) throw TXOwl{_T("GetImageInfo failed")};
377 return i;
378}
379
380//
381/// Gets the current background color for this ImageList.
382//
383TColor
385{
386 PRECONDITION(Handle);
387 return ImageList_GetBkColor(Handle);
388}
389
390//
391/// Sets the current background color for this ImageList, returning the previous
392/// color.
393//
394TColor
396{
397 PRECONDITION(Handle);
398 return ImageList_SetBkColor(Handle, color);
399}
400
401//
402/// Selects an image for use as an overlay. Up to four can be selected.
403//
404bool
406{
407 PRECONDITION(Handle);
408 return ImageList_SetOverlayImage(Handle, index, overlay);
409}
410
411
412//
413/// Combines the current drag image with another image in the list. Typically, a
414/// mouse cursor would be added to the image list and merged with the drag image
415/// list.
416//
417bool
423
424//
425/// BeginDrag sets this imagelist to be the drag imagelist. There can only be one
426/// drag imagelist at any time.
427//
428bool
430{
431 PRECONDITION(Handle);
432 return ImageList_BeginDrag(Handle, index, dxHotspot, dyHotspot);
433}
434
435//
436/// Typically, this function is called in response to a WM_LBUTTONDOWN message. The
437/// 'x' and 'y' parameters are relative to the upper-left corner of the window's
438/// rectangle and NOT the client area. The window 'hWndLock' is locked from further
439/// updates.
440//
441bool
443{
444 return ImageList_DragEnter(hWndLock, x, y);
445}
446
447//
448/// DragMove is typically called when receiving a WM_MOUSEMOVE message. The 'x' and
449/// 'y' parameters are generally passed from the message to this function.
450//
451bool
453{
454 return ImageList_DragMove(x, y);
455}
456
457//
458/// DragLeave is typically called when receiving a WM_LBUTTONUP message. The
459/// 'hWndLock' window is unlocked from updates.
460//
461bool
466
467//
468/// EndDrag removes the current drag imagelist from the system.
469//
470void
475
476//
477/// Locks or unlocks the window from updates.
478//
479bool
484
485//
486// Version 4.70
487//
488bool
490{
491 PRECONDITION(Handle);
492 return ImageList_Copy(Handle, ito, src, ifrom, flags);
493}
494
495//
496// Version 4.70
497//
498bool
504
505//
506// Version 4.71
507//
510{
511 PRECONDITION(Handle);
513 return (hImg) ? new TImageList(hImg) : nullptr;
514}
515
516//
517// Version 4.70
518//
519bool
524
525} // OWL namespace
526
#define WARNX(group, condition, level, message)
Definition checks.h:277
#define PRECONDITION(condition)
Definition checks.h:227
#define DIAG_DECLARE_GROUP(group)
Definition checks.h:404
TBitmap is the GDI bitmap class derived from TGdiObject.
Definition gdiobjec.h:510
Class wrapper for management of color values.
Definition color.h:245
static const TColor None
not-a-color
Definition color.h:318
static const TColor Sys3dFace
The symbolic system color value for the face color of 3-dimensional display elements.
Definition color.h:339
TDC is the root class for GDI DC wrappers.
Definition dc.h:64
Pseudo-GDI object Device Independent Bitmap (DIB) class.
Definition gdiobjec.h:795
TIcon, derived from TGdiObject, represents the GDI object icon class.
Definition gdiobjec.h:670
TImageInfo is a wrapper class for a structure that describes an image within an image list.
Definition imagelst.h:40
TImageList is a wrapper class for the ImageList common "control".
Definition imagelst.h:64
bool BeginDrag(int index, int dxHotspot, int dyHotspot)
BeginDrag sets this imagelist to be the drag imagelist.
Definition imagelst.cpp:429
static bool DragMove(int x, int y)
DragMove is typically called when receiving a WM_MOUSEMOVE message.
Definition imagelst.cpp:452
bool GetIconSize(int &cx, int &cy)
Returns the icon size.
Definition imagelst.cpp:352
TColor SetBkColor(const TColor &newColor)
Sets the current background color for this ImageList, returning the previous color.
Definition imagelst.cpp:395
TImageList * Duplicate()
Definition imagelst.cpp:509
bool SetDragCursorImage(int drag, int dxHotspot, int dyHotspot)
Combines the current drag image with another image in the list.
Definition imagelst.cpp:418
static bool DragEnter(HWND hwndLock, int x, int y)
Typically, this function is called in response to a WM_LBUTTONDOWN message.
Definition imagelst.cpp:442
TImageList & operator=(const TImageList &)
Replaces the image list by a duplicate of the given image list.
Definition imagelst.cpp:175
bool SetImageCount(uint newcount)
Definition imagelst.cpp:520
static bool DragShowNolock(bool show)
Locks or unlocks the window from updates.
Definition imagelst.cpp:480
bool DrawIndirect(const TImageListDrawParam &imldp)
Definition imagelst.cpp:499
static bool DragLeave(HWND hwndLock)
DragLeave is typically called when receiving a WM_LBUTTONUP message.
Definition imagelst.cpp:462
TColor GetBkColor() const
Gets the current background color for this ImageList.
Definition imagelst.cpp:384
int GetImageCount() const
Returns number of images currently in this ImageList.
Definition imagelst.cpp:248
TImageList(const TSize &imageSize, uint flags, int initCapacity, int growBy)
Constructs an empty ImageList of a given size.
Definition imagelst.cpp:38
int Add(const TBitmap &image)
Adds new image(s) to the ImageList.
Definition imagelst.cpp:259
void CheckValid()
Throws an exception if this image list handle is invalid.
Definition imagelst.cpp:163
bool Copy(TImageList &imgsrc, int ifrom, int ito, uint flags=ILCF_MOVE)
Definition imagelst.cpp:489
bool Remove(int index)
Removes an image (or all images if index is -1) from this ImageList.
Definition imagelst.cpp:302
HICON GetIcon(int index, uint flags) const
Creates and retrieves an icon from an image and mask in the ImageList.
Definition imagelst.cpp:332
~TImageList()
Destructs the ImageList and cleans up the image list handle.
Definition imagelst.cpp:152
bool GetImageInfo(int index, TImageInfo &celInfo) const
Gets general information about a given image.
Definition imagelst.cpp:362
bool Draw(int index, TDC &, int x, int y, uint style=ILD_NORMAL, int overlay=0)
Draws an image onto a target DC at a given coordinate and with a given style.
Definition imagelst.cpp:218
bool Replace(int index, const TBitmap &image)
Replaces an image in this ImageList.
Definition imagelst.cpp:312
bool SetOverlayImage(int index, int overlay)
Selects an image for use as an overlay. Up to four can be selected.
Definition imagelst.cpp:405
static void EndDrag()
EndDrag removes the current drag imagelist from the system.
Definition imagelst.cpp:471
int ReplaceIcon(int index, HICON hicon)
Replaces the image at index 'index' with the icon or cursor.
Definition imagelst.cpp:342
TPalette is the GDI Palette class derived from TGdiObject.
Definition gdiobjec.h:413
TPoint is a support class, derived from tagPOINT.
Definition geometry.h:87
Derived from TWindowDC, TScreenDC is a DC class that provides direct access to the screen bitmap.
Definition dc.h:610
The tagSIZE struct is defined as.
Definition geometry.h:234
static void Raise()
Constructs a TXCommCtrl exception from scratch, and throws it.
Definition commctrl.cpp:74
TXOwl is root class of the ObjectWindows exception hierarchy.
Definition except.h:38
#define _T(x)
Definition cygwin.h:51
Definition of class TImageList, an ImageList Common Control wrapper.
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
OWL_DIAGINFO
Definition animctrl.cpp:14
unsigned int uint
Definition number.h:25