]> git.saurik.com Git - wxWidgets.git/blob - src/msw/imaglist.cpp
support for incorrect entries in .mime.types added (world is not perfect, alas)
[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/log.h"
33 #include "wx/intl.h"
34
35 #include "wx/msw/imaglist.h"
36 #include "wx/msw/private.h"
37
38 #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__)
39 #include <commctrl.h>
40 #endif
41
42 #if !USE_SHARED_LIBRARY
43 IMPLEMENT_DYNAMIC_CLASS(wxImageList, wxObject)
44 #endif
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
72 bool wxImageList::Create(int width, int height, bool mask, int initial)
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();
92
93 int index = ImageList_Add((HIMAGELIST) GetHIMAGELIST(), hBitmap1, hBitmap2);
94 if ( index == -1 )
95 {
96 wxLogError(_("Couldn't add an image to the image list."));
97 }
98
99 return index;
100 }
101
102 // Adds a bitmap, using the specified colour to create the mask bitmap
103 // Note that wxImageList creates new bitmaps, so you may delete
104 // 'bitmap'.
105 int wxImageList::Add(const wxBitmap& bitmap, const wxColour& maskColour)
106 {
107 #ifdef __TWIN32__
108 wxFAIL_MSG("ImageList_AddMasked not implemented in TWIN32");
109 return -1;
110 #else
111 HBITMAP hBitmap1 = (HBITMAP) bitmap.GetHBITMAP();
112 COLORREF colorRef = PALETTERGB(maskColour.Red(), maskColour.Green(), maskColour.Blue());
113 return ImageList_AddMasked((HIMAGELIST) GetHIMAGELIST(), hBitmap1, colorRef);
114 #endif
115 }
116
117 // Adds a bitmap and mask from an icon.
118 int wxImageList::Add(const wxIcon& icon)
119 {
120 HICON hIcon = (HICON) icon.GetHICON();
121 return ImageList_AddIcon((HIMAGELIST) GetHIMAGELIST(), hIcon);
122 }
123
124 // Replaces a bitmap, optionally passing a mask bitmap.
125 // Note that wxImageList creates new bitmaps, so you may delete
126 // 'bitmap' and 'mask'.
127 bool wxImageList::Replace(int index, const wxBitmap& bitmap, const wxBitmap& mask)
128 {
129 #ifdef __TWIN32__
130 wxFAIL_MSG("ImageList_Replace not implemented in TWIN32");
131 return FALSE;
132 #else
133 HBITMAP hBitmap1 = (HBITMAP) bitmap.GetHBITMAP();
134 HBITMAP hBitmap2 = 0;
135 if ( mask.Ok() )
136 hBitmap2 = (HBITMAP) mask.GetHBITMAP();
137 return (ImageList_Replace((HIMAGELIST) GetHIMAGELIST(), index, hBitmap1, hBitmap2) != 0);
138 #endif
139 }
140
141 /* Not supported by Win95
142 // Replacing a bitmap, using the specified colour to create the mask bitmap
143 // Note that wxImageList creates new bitmaps, so you may delete
144 // 'bitmap'.
145 bool wxImageList::Replace(int index, const wxBitmap& bitmap, const wxColour& maskColour)
146 {
147 HBITMAP hBitmap1 = (HBITMAP) bitmap.GetHBITMAP();
148 COLORREF colorRef = PALETTERGB(maskColour.Red(), maskColour.Green(), maskColour.Blue());
149 return (bool) ImageList_ReplaceMasked((HIMAGELIST) GetHIMAGELIST(), index, hBitmap1, colorRef);
150 }
151 */
152
153 // Replaces a bitmap and mask from an icon.
154 bool wxImageList::Replace(int index, const wxIcon& icon)
155 {
156 HICON hIcon = (HICON) icon.GetHICON();
157 return (ImageList_ReplaceIcon((HIMAGELIST) GetHIMAGELIST(), index, hIcon) != 0);
158 }
159
160 // Removes the image at the given index.
161 bool wxImageList::Remove(int index)
162 {
163 #ifdef __TWIN32__
164 wxFAIL_MSG("ImageList_Replace not implemented in TWIN32");
165 return FALSE;
166 #else
167 return (ImageList_Remove((HIMAGELIST) GetHIMAGELIST(), index) != 0);
168 #endif
169 }
170
171 // Remove all images
172 bool wxImageList::RemoveAll(void)
173 {
174 // TODO: Is this correct?
175 while ( GetImageCount() > 0 )
176 {
177 Remove(0);
178 }
179 return TRUE;
180 }
181
182 // Draws the given image on a dc at the specified position.
183 // If 'solidBackground' is TRUE, Draw sets the image list background
184 // colour to the background colour of the wxDC, to speed up
185 // drawing by eliminating masked drawing where possible.
186 bool wxImageList::Draw(int index, wxDC& dc, int x, int y,
187 int flags, bool solidBackground)
188 {
189 #ifdef __TWIN32__
190 wxFAIL_MSG("ImageList_Replace not implemented in TWIN32");
191 return FALSE;
192 #else
193 HDC hDC = (HDC) dc.GetHDC();
194 if ( !hDC )
195 return FALSE;
196
197 if ( solidBackground )
198 {
199 wxBrush *brush = & dc.GetBackground();
200 if ( brush && brush->Ok())
201 {
202 wxColour col(brush->GetColour());
203 ImageList_SetBkColor((HIMAGELIST) GetHIMAGELIST(),
204 PALETTERGB(col.Red(), col.Green(), col.Blue()));
205 }
206 else
207 ImageList_SetBkColor((HIMAGELIST) GetHIMAGELIST(),
208 CLR_NONE);
209 }
210 else
211 ImageList_SetBkColor((HIMAGELIST) GetHIMAGELIST(),
212 CLR_NONE);
213
214 UINT style = 0;
215 if ( flags & wxIMAGELIST_DRAW_NORMAL )
216 style |= ILD_NORMAL;
217 if ( flags & wxIMAGELIST_DRAW_TRANSPARENT )
218 style |= ILD_TRANSPARENT;
219 if ( flags & wxIMAGELIST_DRAW_SELECTED )
220 style |= ILD_SELECTED;
221 if ( flags & wxIMAGELIST_DRAW_FOCUSED )
222 style |= ILD_FOCUS;
223
224 return (ImageList_Draw((HIMAGELIST) GetHIMAGELIST(), index, hDC,
225 x, y, style) != 0);
226 #endif
227 }
228
229 #endif
230