]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
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" | |
0c589ad0 BM |
29 | #include "wx/icon.h" |
30 | #include "wx/dc.h" | |
31 | #include "wx/string.h" | |
2bda0e17 KB |
32 | #endif |
33 | ||
ce3ed50d JS |
34 | #include "wx/log.h" |
35 | #include "wx/intl.h" | |
36 | ||
2bda0e17 KB |
37 | #include "wx/msw/imaglist.h" |
38 | #include "wx/msw/private.h" | |
39 | ||
65fd5cb0 | 40 | #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS) |
2bda0e17 KB |
41 | #include <commctrl.h> |
42 | #endif | |
43 | ||
2bda0e17 | 44 | IMPLEMENT_DYNAMIC_CLASS(wxImageList, wxObject) |
2bda0e17 KB |
45 | |
46 | wxImageList::wxImageList(void) | |
47 | { | |
48 | m_hImageList = 0; | |
49 | } | |
50 | ||
51 | wxImageList::~wxImageList(void) | |
52 | { | |
53 | if ( m_hImageList ) | |
54 | ImageList_Destroy((HIMAGELIST) m_hImageList); | |
55 | m_hImageList = 0; | |
56 | } | |
57 | ||
58 | ||
59 | // Attributes | |
60 | //////////////////////////////////////////////////////////////////////////// | |
61 | ||
62 | // Returns the number of images in the image list. | |
63 | int wxImageList::GetImageCount(void) const | |
64 | { | |
65 | return ImageList_GetImageCount((HIMAGELIST) m_hImageList); | |
66 | } | |
67 | ||
68 | // Operations | |
69 | //////////////////////////////////////////////////////////////////////////// | |
70 | ||
71 | // Creates an image list | |
debe6624 | 72 | bool wxImageList::Create(int width, int height, bool mask, int initial) |
2bda0e17 KB |
73 | { |
74 | UINT flags = 0; | |
75 | if ( mask ) | |
76 | flags |= ILC_MASK; | |
77 | ||
78 | // Grow by 1, I guess this is reasonable behaviour most of the time | |
79 | m_hImageList = (WXHIMAGELIST) ImageList_Create(width, height, flags, initial, 1); | |
80 | return (m_hImageList != 0); | |
81 | } | |
82 | ||
83 | // Adds a bitmap, and optionally a mask bitmap. | |
84 | // Note that wxImageList creates new bitmaps, so you may delete | |
85 | // 'bitmap' and 'mask'. | |
86 | int wxImageList::Add(const wxBitmap& bitmap, const wxBitmap& mask) | |
87 | { | |
88 | HBITMAP hBitmap1 = (HBITMAP) bitmap.GetHBITMAP(); | |
89 | HBITMAP hBitmap2 = 0; | |
90 | if ( mask.Ok() ) | |
91 | hBitmap2 = (HBITMAP) mask.GetHBITMAP(); | |
54b37e2e UC |
92 | else if (bitmap.GetMask()) |
93 | hBitmap2 = (HBITMAP) bitmap.GetMask()->GetMaskBitmap(); | |
94 | ||
95 | HBITMAP hBitmapI=0; | |
96 | if(hBitmap2!=0) { | |
97 | // Microsoft imagelist masks are inverted from wxWindows mask standard (white is mask color) | |
98 | BITMAP bm; | |
99 | ::GetObject(hBitmap2,sizeof(BITMAP),(LPVOID)&bm); | |
100 | int w=bm.bmWidth; | |
101 | int h=bm.bmHeight; | |
102 | HDC hdc = ::CreateCompatibleDC(NULL); | |
103 | HDC hdci = ::CreateCompatibleDC(NULL); | |
104 | hBitmapI = ::CreateCompatibleBitmap(hdci, w, h); | |
105 | ::SelectObject(hdc, hBitmap2); | |
106 | ::SelectObject(hdci, hBitmapI); | |
107 | ::BitBlt(hdci, 0, 0, w, h, hdc, 0, 0, NOTSRCCOPY); | |
108 | ::DeleteDC(hdc); | |
109 | ::DeleteDC(hdci); | |
110 | } | |
e29b83a4 | 111 | |
54b37e2e | 112 | int index = ImageList_Add((HIMAGELIST) GetHIMAGELIST(), hBitmap1, hBitmapI); |
e29b83a4 VZ |
113 | if ( index == -1 ) |
114 | { | |
115 | wxLogError(_("Couldn't add an image to the image list.")); | |
116 | } | |
117 | ||
54b37e2e UC |
118 | // Clean up inverted mask |
119 | if(hBitmapI!=0) | |
120 | ::DeleteObject(hBitmapI); | |
121 | ||
e29b83a4 | 122 | return index; |
2bda0e17 KB |
123 | } |
124 | ||
125 | // Adds a bitmap, using the specified colour to create the mask bitmap | |
126 | // Note that wxImageList creates new bitmaps, so you may delete | |
127 | // 'bitmap'. | |
128 | int wxImageList::Add(const wxBitmap& bitmap, const wxColour& maskColour) | |
129 | { | |
57c208c5 JS |
130 | #ifdef __TWIN32__ |
131 | wxFAIL_MSG("ImageList_AddMasked not implemented in TWIN32"); | |
132 | return -1; | |
133 | #else | |
2bda0e17 KB |
134 | HBITMAP hBitmap1 = (HBITMAP) bitmap.GetHBITMAP(); |
135 | COLORREF colorRef = PALETTERGB(maskColour.Red(), maskColour.Green(), maskColour.Blue()); | |
136 | return ImageList_AddMasked((HIMAGELIST) GetHIMAGELIST(), hBitmap1, colorRef); | |
57c208c5 | 137 | #endif |
2bda0e17 KB |
138 | } |
139 | ||
140 | // Adds a bitmap and mask from an icon. | |
141 | int wxImageList::Add(const wxIcon& icon) | |
142 | { | |
143 | HICON hIcon = (HICON) icon.GetHICON(); | |
144 | return ImageList_AddIcon((HIMAGELIST) GetHIMAGELIST(), hIcon); | |
145 | } | |
146 | ||
147 | // Replaces a bitmap, optionally passing a mask bitmap. | |
148 | // Note that wxImageList creates new bitmaps, so you may delete | |
149 | // 'bitmap' and 'mask'. | |
debe6624 | 150 | bool wxImageList::Replace(int index, const wxBitmap& bitmap, const wxBitmap& mask) |
2bda0e17 | 151 | { |
57c208c5 JS |
152 | #ifdef __TWIN32__ |
153 | wxFAIL_MSG("ImageList_Replace not implemented in TWIN32"); | |
154 | return FALSE; | |
155 | #else | |
2bda0e17 KB |
156 | HBITMAP hBitmap1 = (HBITMAP) bitmap.GetHBITMAP(); |
157 | HBITMAP hBitmap2 = 0; | |
158 | if ( mask.Ok() ) | |
159 | hBitmap2 = (HBITMAP) mask.GetHBITMAP(); | |
160 | return (ImageList_Replace((HIMAGELIST) GetHIMAGELIST(), index, hBitmap1, hBitmap2) != 0); | |
57c208c5 | 161 | #endif |
2bda0e17 KB |
162 | } |
163 | ||
164 | /* Not supported by Win95 | |
165 | // Replacing a bitmap, using the specified colour to create the mask bitmap | |
166 | // Note that wxImageList creates new bitmaps, so you may delete | |
167 | // 'bitmap'. | |
debe6624 | 168 | bool wxImageList::Replace(int index, const wxBitmap& bitmap, const wxColour& maskColour) |
2bda0e17 KB |
169 | { |
170 | HBITMAP hBitmap1 = (HBITMAP) bitmap.GetHBITMAP(); | |
171 | COLORREF colorRef = PALETTERGB(maskColour.Red(), maskColour.Green(), maskColour.Blue()); | |
172 | return (bool) ImageList_ReplaceMasked((HIMAGELIST) GetHIMAGELIST(), index, hBitmap1, colorRef); | |
173 | } | |
174 | */ | |
175 | ||
176 | // Replaces a bitmap and mask from an icon. | |
debe6624 | 177 | bool wxImageList::Replace(int index, const wxIcon& icon) |
2bda0e17 KB |
178 | { |
179 | HICON hIcon = (HICON) icon.GetHICON(); | |
180 | return (ImageList_ReplaceIcon((HIMAGELIST) GetHIMAGELIST(), index, hIcon) != 0); | |
181 | } | |
182 | ||
183 | // Removes the image at the given index. | |
debe6624 | 184 | bool wxImageList::Remove(int index) |
2bda0e17 | 185 | { |
57c208c5 JS |
186 | #ifdef __TWIN32__ |
187 | wxFAIL_MSG("ImageList_Replace not implemented in TWIN32"); | |
188 | return FALSE; | |
189 | #else | |
2bda0e17 | 190 | return (ImageList_Remove((HIMAGELIST) GetHIMAGELIST(), index) != 0); |
57c208c5 | 191 | #endif |
2bda0e17 KB |
192 | } |
193 | ||
194 | // Remove all images | |
195 | bool wxImageList::RemoveAll(void) | |
196 | { | |
197 | // TODO: Is this correct? | |
198 | while ( GetImageCount() > 0 ) | |
199 | { | |
200 | Remove(0); | |
201 | } | |
202 | return TRUE; | |
203 | } | |
204 | ||
205 | // Draws the given image on a dc at the specified position. | |
206 | // If 'solidBackground' is TRUE, Draw sets the image list background | |
207 | // colour to the background colour of the wxDC, to speed up | |
208 | // drawing by eliminating masked drawing where possible. | |
debe6624 JS |
209 | bool wxImageList::Draw(int index, wxDC& dc, int x, int y, |
210 | int flags, bool solidBackground) | |
2bda0e17 | 211 | { |
57c208c5 JS |
212 | #ifdef __TWIN32__ |
213 | wxFAIL_MSG("ImageList_Replace not implemented in TWIN32"); | |
214 | return FALSE; | |
215 | #else | |
2bda0e17 KB |
216 | HDC hDC = (HDC) dc.GetHDC(); |
217 | if ( !hDC ) | |
218 | return FALSE; | |
219 | ||
220 | if ( solidBackground ) | |
221 | { | |
c0ed460c | 222 | wxBrush *brush = & dc.GetBackground(); |
2bda0e17 KB |
223 | if ( brush && brush->Ok()) |
224 | { | |
225 | wxColour col(brush->GetColour()); | |
226 | ImageList_SetBkColor((HIMAGELIST) GetHIMAGELIST(), | |
227 | PALETTERGB(col.Red(), col.Green(), col.Blue())); | |
228 | } | |
229 | else | |
230 | ImageList_SetBkColor((HIMAGELIST) GetHIMAGELIST(), | |
231 | CLR_NONE); | |
232 | } | |
233 | else | |
234 | ImageList_SetBkColor((HIMAGELIST) GetHIMAGELIST(), | |
235 | CLR_NONE); | |
236 | ||
237 | UINT style = 0; | |
238 | if ( flags & wxIMAGELIST_DRAW_NORMAL ) | |
239 | style |= ILD_NORMAL; | |
240 | if ( flags & wxIMAGELIST_DRAW_TRANSPARENT ) | |
241 | style |= ILD_TRANSPARENT; | |
242 | if ( flags & wxIMAGELIST_DRAW_SELECTED ) | |
243 | style |= ILD_SELECTED; | |
244 | if ( flags & wxIMAGELIST_DRAW_FOCUSED ) | |
245 | style |= ILD_FOCUS; | |
246 | ||
247 | return (ImageList_Draw((HIMAGELIST) GetHIMAGELIST(), index, hDC, | |
248 | x, y, style) != 0); | |
57c208c5 | 249 | #endif |
2bda0e17 KB |
250 | } |
251 | ||
252 | #endif | |
253 |