Some changes in a vain attempt to make Salford C++ work; added FAQ files;
[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 #ifndef __GNUWIN32__
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 HBITMAP hBitmap1 = (HBITMAP) bitmap.GetHBITMAP();
108 COLORREF colorRef = PALETTERGB(maskColour.Red(), maskColour.Green(), maskColour.Blue());
109 return ImageList_AddMasked((HIMAGELIST) GetHIMAGELIST(), hBitmap1, colorRef);
110 }
111
112 // Adds a bitmap and mask from an icon.
113 int wxImageList::Add(const wxIcon& icon)
114 {
115 HICON hIcon = (HICON) icon.GetHICON();
116 return ImageList_AddIcon((HIMAGELIST) GetHIMAGELIST(), hIcon);
117 }
118
119 // Replaces a bitmap, optionally passing a mask bitmap.
120 // Note that wxImageList creates new bitmaps, so you may delete
121 // 'bitmap' and 'mask'.
122 bool wxImageList::Replace(int index, const wxBitmap& bitmap, const wxBitmap& mask)
123 {
124 HBITMAP hBitmap1 = (HBITMAP) bitmap.GetHBITMAP();
125 HBITMAP hBitmap2 = 0;
126 if ( mask.Ok() )
127 hBitmap2 = (HBITMAP) mask.GetHBITMAP();
128 return (ImageList_Replace((HIMAGELIST) GetHIMAGELIST(), index, hBitmap1, hBitmap2) != 0);
129 }
130
131 /* Not supported by Win95
132 // Replacing a bitmap, using the specified colour to create the mask bitmap
133 // Note that wxImageList creates new bitmaps, so you may delete
134 // 'bitmap'.
135 bool wxImageList::Replace(int index, const wxBitmap& bitmap, const wxColour& maskColour)
136 {
137 HBITMAP hBitmap1 = (HBITMAP) bitmap.GetHBITMAP();
138 COLORREF colorRef = PALETTERGB(maskColour.Red(), maskColour.Green(), maskColour.Blue());
139 return (bool) ImageList_ReplaceMasked((HIMAGELIST) GetHIMAGELIST(), index, hBitmap1, colorRef);
140 }
141 */
142
143 // Replaces a bitmap and mask from an icon.
144 bool wxImageList::Replace(int index, const wxIcon& icon)
145 {
146 HICON hIcon = (HICON) icon.GetHICON();
147 return (ImageList_ReplaceIcon((HIMAGELIST) GetHIMAGELIST(), index, hIcon) != 0);
148 }
149
150 // Removes the image at the given index.
151 bool wxImageList::Remove(int index)
152 {
153 return (ImageList_Remove((HIMAGELIST) GetHIMAGELIST(), index) != 0);
154 }
155
156 // Remove all images
157 bool wxImageList::RemoveAll(void)
158 {
159 // TODO: Is this correct?
160 while ( GetImageCount() > 0 )
161 {
162 Remove(0);
163 }
164 return TRUE;
165 }
166
167 // Draws the given image on a dc at the specified position.
168 // If 'solidBackground' is TRUE, Draw sets the image list background
169 // colour to the background colour of the wxDC, to speed up
170 // drawing by eliminating masked drawing where possible.
171 bool wxImageList::Draw(int index, wxDC& dc, int x, int y,
172 int flags, bool solidBackground)
173 {
174 HDC hDC = (HDC) dc.GetHDC();
175 if ( !hDC )
176 return FALSE;
177
178 if ( solidBackground )
179 {
180 wxBrush *brush = & dc.GetBackground();
181 if ( brush && brush->Ok())
182 {
183 wxColour col(brush->GetColour());
184 ImageList_SetBkColor((HIMAGELIST) GetHIMAGELIST(),
185 PALETTERGB(col.Red(), col.Green(), col.Blue()));
186 }
187 else
188 ImageList_SetBkColor((HIMAGELIST) GetHIMAGELIST(),
189 CLR_NONE);
190 }
191 else
192 ImageList_SetBkColor((HIMAGELIST) GetHIMAGELIST(),
193 CLR_NONE);
194
195 UINT style = 0;
196 if ( flags & wxIMAGELIST_DRAW_NORMAL )
197 style |= ILD_NORMAL;
198 if ( flags & wxIMAGELIST_DRAW_TRANSPARENT )
199 style |= ILD_TRANSPARENT;
200 if ( flags & wxIMAGELIST_DRAW_SELECTED )
201 style |= ILD_SELECTED;
202 if ( flags & wxIMAGELIST_DRAW_FOCUSED )
203 style |= ILD_FOCUS;
204
205 return (ImageList_Draw((HIMAGELIST) GetHIMAGELIST(), index, hDC,
206 x, y, style) != 0);
207 }
208
209 #endif
210