added an error message if a bitmap can't be addedto the image list
[wxWidgets.git] / src / msw / imaglist.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: imaglist.cpp
3 // Purpose: wxImageList
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "imaglist.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #if defined(__WIN95__)
24
25 #ifndef WX_PRECOMP
26 #include <stdio.h>
27 #include "wx/setup.h"
28 #include "wx/window.h"
29 #include "wx/dcclient.h"
30 #endif
31
32 #include "wx/msw/imaglist.h"
33 #include "wx/msw/private.h"
34
35 #ifndef __GNUWIN32__
36 #include <commctrl.h>
37 #endif
38
39 #if !USE_SHARED_LIBRARY
40 IMPLEMENT_DYNAMIC_CLASS(wxImageList, wxObject)
41 #endif
42
43 wxImageList::wxImageList(void)
44 {
45 m_hImageList = 0;
46 }
47
48 wxImageList::~wxImageList(void)
49 {
50 if ( m_hImageList )
51 ImageList_Destroy((HIMAGELIST) m_hImageList);
52 m_hImageList = 0;
53 }
54
55
56 // Attributes
57 ////////////////////////////////////////////////////////////////////////////
58
59 // Returns the number of images in the image list.
60 int wxImageList::GetImageCount(void) const
61 {
62 return ImageList_GetImageCount((HIMAGELIST) m_hImageList);
63 }
64
65 // Operations
66 ////////////////////////////////////////////////////////////////////////////
67
68 // Creates an image list
69 bool wxImageList::Create(int width, int height, bool mask, int initial)
70 {
71 UINT flags = 0;
72 if ( mask )
73 flags |= ILC_MASK;
74
75 // Grow by 1, I guess this is reasonable behaviour most of the time
76 m_hImageList = (WXHIMAGELIST) ImageList_Create(width, height, flags, initial, 1);
77 return (m_hImageList != 0);
78 }
79
80 // Adds a bitmap, and optionally a mask bitmap.
81 // Note that wxImageList creates new bitmaps, so you may delete
82 // 'bitmap' and 'mask'.
83 int wxImageList::Add(const wxBitmap& bitmap, const wxBitmap& mask)
84 {
85 HBITMAP hBitmap1 = (HBITMAP) bitmap.GetHBITMAP();
86 HBITMAP hBitmap2 = 0;
87 if ( mask.Ok() )
88 hBitmap2 = (HBITMAP) mask.GetHBITMAP();
89
90 int index = ImageList_Add((HIMAGELIST) GetHIMAGELIST(), hBitmap1, hBitmap2);
91 if ( index == -1 )
92 {
93 wxLogError(_("Couldn't add an image to the image list."));
94 }
95
96 return index;
97 }
98
99 // Adds a bitmap, using the specified colour to create the mask bitmap
100 // Note that wxImageList creates new bitmaps, so you may delete
101 // 'bitmap'.
102 int wxImageList::Add(const wxBitmap& bitmap, const wxColour& maskColour)
103 {
104 HBITMAP hBitmap1 = (HBITMAP) bitmap.GetHBITMAP();
105 COLORREF colorRef = PALETTERGB(maskColour.Red(), maskColour.Green(), maskColour.Blue());
106 return ImageList_AddMasked((HIMAGELIST) GetHIMAGELIST(), hBitmap1, colorRef);
107 }
108
109 // Adds a bitmap and mask from an icon.
110 int wxImageList::Add(const wxIcon& icon)
111 {
112 HICON hIcon = (HICON) icon.GetHICON();
113 return ImageList_AddIcon((HIMAGELIST) GetHIMAGELIST(), hIcon);
114 }
115
116 // Replaces a bitmap, optionally passing a mask bitmap.
117 // Note that wxImageList creates new bitmaps, so you may delete
118 // 'bitmap' and 'mask'.
119 bool wxImageList::Replace(int index, const wxBitmap& bitmap, const wxBitmap& mask)
120 {
121 HBITMAP hBitmap1 = (HBITMAP) bitmap.GetHBITMAP();
122 HBITMAP hBitmap2 = 0;
123 if ( mask.Ok() )
124 hBitmap2 = (HBITMAP) mask.GetHBITMAP();
125 return (ImageList_Replace((HIMAGELIST) GetHIMAGELIST(), index, hBitmap1, hBitmap2) != 0);
126 }
127
128 /* Not supported by Win95
129 // Replacing a bitmap, using the specified colour to create the mask bitmap
130 // Note that wxImageList creates new bitmaps, so you may delete
131 // 'bitmap'.
132 bool wxImageList::Replace(int index, const wxBitmap& bitmap, const wxColour& maskColour)
133 {
134 HBITMAP hBitmap1 = (HBITMAP) bitmap.GetHBITMAP();
135 COLORREF colorRef = PALETTERGB(maskColour.Red(), maskColour.Green(), maskColour.Blue());
136 return (bool) ImageList_ReplaceMasked((HIMAGELIST) GetHIMAGELIST(), index, hBitmap1, colorRef);
137 }
138 */
139
140 // Replaces a bitmap and mask from an icon.
141 bool wxImageList::Replace(int index, const wxIcon& icon)
142 {
143 HICON hIcon = (HICON) icon.GetHICON();
144 return (ImageList_ReplaceIcon((HIMAGELIST) GetHIMAGELIST(), index, hIcon) != 0);
145 }
146
147 // Removes the image at the given index.
148 bool wxImageList::Remove(int index)
149 {
150 return (ImageList_Remove((HIMAGELIST) GetHIMAGELIST(), index) != 0);
151 }
152
153 // Remove all images
154 bool wxImageList::RemoveAll(void)
155 {
156 // TODO: Is this correct?
157 while ( GetImageCount() > 0 )
158 {
159 Remove(0);
160 }
161 return TRUE;
162 }
163
164 // Draws the given image on a dc at the specified position.
165 // If 'solidBackground' is TRUE, Draw sets the image list background
166 // colour to the background colour of the wxDC, to speed up
167 // drawing by eliminating masked drawing where possible.
168 bool wxImageList::Draw(int index, wxDC& dc, int x, int y,
169 int flags, bool solidBackground)
170 {
171 HDC hDC = (HDC) dc.GetHDC();
172 if ( !hDC )
173 return FALSE;
174
175 if ( solidBackground )
176 {
177 wxBrush *brush = & dc.GetBackground();
178 if ( brush && brush->Ok())
179 {
180 wxColour col(brush->GetColour());
181 ImageList_SetBkColor((HIMAGELIST) GetHIMAGELIST(),
182 PALETTERGB(col.Red(), col.Green(), col.Blue()));
183 }
184 else
185 ImageList_SetBkColor((HIMAGELIST) GetHIMAGELIST(),
186 CLR_NONE);
187 }
188 else
189 ImageList_SetBkColor((HIMAGELIST) GetHIMAGELIST(),
190 CLR_NONE);
191
192 UINT style = 0;
193 if ( flags & wxIMAGELIST_DRAW_NORMAL )
194 style |= ILD_NORMAL;
195 if ( flags & wxIMAGELIST_DRAW_TRANSPARENT )
196 style |= ILD_TRANSPARENT;
197 if ( flags & wxIMAGELIST_DRAW_SELECTED )
198 style |= ILD_SELECTED;
199 if ( flags & wxIMAGELIST_DRAW_FOCUSED )
200 style |= ILD_FOCUS;
201
202 return (ImageList_Draw((HIMAGELIST) GetHIMAGELIST(), index, hDC,
203 x, y, style) != 0);
204 }
205
206 #endif
207