]>
Commit | Line | Data |
---|---|---|
5a566d89 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: samples/image/image.cpp | |
3 | // Purpose: sample showing operations with wxImage | |
4 | // Author: Robert Roebling | |
5 | // Modified by: | |
6 | // Created: 1998 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998-2005 Robert Roebling | |
9 | // License: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
01111366 | 11 | |
3d05544e JS |
12 | // For compilers that support precompilation, includes "wx/wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
16 | #pragma hdrstop | |
17 | #endif | |
18 | ||
19 | #ifndef WX_PRECOMP | |
01111366 | 20 | #include "wx/wx.h" |
3d05544e JS |
21 | #endif |
22 | ||
01111366 | 23 | #include "wx/image.h" |
a23fd0e1 | 24 | #include "wx/file.h" |
5a566d89 | 25 | #include "wx/filename.h" |
a8d4e3ac VS |
26 | #include "wx/mstream.h" |
27 | #include "wx/wfstream.h" | |
1971d23c | 28 | #include "wx/quantize.h" |
a23fd0e1 | 29 | |
da9df1f5 VZ |
30 | #if wxUSE_CLIPBOARD |
31 | #include "wx/dataobj.h" | |
32 | #include "wx/clipbrd.h" | |
33 | #endif // wxUSE_CLIPBOARD | |
34 | ||
dc1dbfc6 | 35 | #include "smile.xbm" |
3ef37e7f | 36 | #include "smile.xpm" |
dc1dbfc6 | 37 | |
284f2b59 | 38 | #if defined(__WXMSW__) |
481721e8 | 39 | #ifdef wxHAVE_RAW_BITMAP |
9b24b388 | 40 | #include "wx/rawbmp.h" |
481721e8 | 41 | #endif |
c698eae5 | 42 | #endif |
a8d4e3ac | 43 | |
284f2b59 RR |
44 | #if defined(__WXMAC__) || defined(__WXGTK__) |
45 | #define wxHAVE_RAW_BITMAP | |
46 | #include "wx/rawbmp.h" | |
47 | #endif | |
48 | ||
01111366 RR |
49 | // derived classes |
50 | ||
51 | class MyFrame; | |
52 | class MyApp; | |
53 | ||
54 | // MyCanvas | |
55 | ||
56 | class MyCanvas: public wxScrolledWindow | |
57 | { | |
1d5b7a0b | 58 | public: |
51b07644 | 59 | MyCanvas() {} |
01111366 | 60 | MyCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size ); |
1d5b7a0b | 61 | ~MyCanvas(); |
01111366 | 62 | void OnPaint( wxPaintEvent &event ); |
9390a202 | 63 | void CreateAntiAliasedBitmap(); |
1d5b7a0b | 64 | |
e1929140 RR |
65 | wxBitmap *my_horse_png; |
66 | wxBitmap *my_horse_jpeg; | |
67 | wxBitmap *my_horse_gif; | |
ca26177c | 68 | wxBitmap *my_horse_bmp; |
51b07644 | 69 | wxBitmap *my_horse_bmp2; |
cbd4be25 | 70 | wxBitmap *my_horse_pcx; |
6319afe3 | 71 | wxBitmap *my_horse_pnm; |
60a41aee | 72 | wxBitmap *my_horse_tiff; |
9d8bdf2d | 73 | wxBitmap *my_horse_xpm; |
bf504f98 VS |
74 | wxBitmap *my_horse_ico32; |
75 | wxBitmap *my_horse_ico16; | |
76 | wxBitmap *my_horse_ico; | |
77 | wxBitmap *my_horse_cur; | |
2b5f62a0 | 78 | wxBitmap *my_horse_ani; |
51b07644 | 79 | |
dc1dbfc6 | 80 | wxBitmap *my_smile_xbm; |
e8fdc264 | 81 | wxBitmap *my_square; |
9390a202 | 82 | wxBitmap *my_anti; |
1d5b7a0b | 83 | |
bf504f98 | 84 | int xH, yH ; |
2b5f62a0 | 85 | int m_ani_images ; |
bf504f98 | 86 | |
bea56879 VZ |
87 | protected: |
88 | wxBitmap m_bmpSmileXpm; | |
89 | wxIcon m_iconSmileXpm; | |
90 | ||
60a41aee | 91 | private: |
1d5b7a0b VZ |
92 | DECLARE_DYNAMIC_CLASS(MyCanvas) |
93 | DECLARE_EVENT_TABLE() | |
01111366 RR |
94 | }; |
95 | ||
18134a1c | 96 | |
01111366 RR |
97 | // MyFrame |
98 | ||
18134a1c | 99 | |
01111366 RR |
100 | class MyFrame: public wxFrame |
101 | { | |
1d5b7a0b VZ |
102 | public: |
103 | MyFrame(); | |
01111366 | 104 | |
01111366 | 105 | void OnAbout( wxCommandEvent &event ); |
ab0f0386 | 106 | void OnNewFrame( wxCommandEvent &event ); |
b3f04dc2 VZ |
107 | #ifdef wxHAVE_RAW_BITMAP |
108 | void OnTestRawBitmap( wxCommandEvent &event ); | |
109 | #endif // wxHAVE_RAW_BITMAP | |
01111366 | 110 | void OnQuit( wxCommandEvent &event ); |
1d5b7a0b | 111 | |
dd38c875 | 112 | #if wxUSE_CLIPBOARD |
da9df1f5 VZ |
113 | void OnCopy(wxCommandEvent& event); |
114 | void OnPaste(wxCommandEvent& event); | |
115 | #endif // wxUSE_CLIPBOARD | |
116 | ||
01111366 | 117 | MyCanvas *m_canvas; |
1d5b7a0b | 118 | |
60a41aee | 119 | private: |
1d5b7a0b VZ |
120 | DECLARE_DYNAMIC_CLASS(MyFrame) |
121 | DECLARE_EVENT_TABLE() | |
01111366 RR |
122 | }; |
123 | ||
ab0f0386 VZ |
124 | class MyImageFrame : public wxFrame |
125 | { | |
126 | public: | |
127 | MyImageFrame(wxFrame *parent, const wxBitmap& bitmap) | |
47f797bd | 128 | : wxFrame(parent, wxID_ANY, _T("Double click to save"), |
ab0f0386 | 129 | wxDefaultPosition, wxDefaultSize, |
40c79fab | 130 | wxCAPTION | wxSYSTEM_MENU | wxCLOSE_BOX), |
1971d23c | 131 | m_bitmap(bitmap) |
ab0f0386 VZ |
132 | { |
133 | SetClientSize(bitmap.GetWidth(), bitmap.GetHeight()); | |
134 | } | |
135 | ||
0c543b7a VZ |
136 | void OnEraseBackground(wxEraseEvent& WXUNUSED(event)) |
137 | { | |
138 | // do nothing here to be able to see how transparent images are shown | |
139 | } | |
140 | ||
ab0f0386 VZ |
141 | void OnPaint(wxPaintEvent& WXUNUSED(event)) |
142 | { | |
143 | wxPaintDC dc( this ); | |
47f797bd | 144 | dc.DrawBitmap( m_bitmap, 0, 0, true /* use mask */ ); |
ab0f0386 VZ |
145 | } |
146 | ||
c5c843e0 | 147 | void OnSave(wxMouseEvent& WXUNUSED(event)) |
1971d23c | 148 | { |
368d59f0 | 149 | wxImage image = m_bitmap.ConvertToImage(); |
1971d23c | 150 | |
4693b20c MB |
151 | wxString savefilename = wxFileSelector( wxT("Save Image"), |
152 | wxT(""), | |
5a566d89 | 153 | wxT(""), |
4693b20c MB |
154 | (const wxChar *)NULL, |
155 | wxT("BMP files (*.bmp)|*.bmp|") | |
156 | wxT("PNG files (*.png)|*.png|") | |
157 | wxT("JPEG files (*.jpg)|*.jpg|") | |
158 | wxT("GIF files (*.gif)|*.gif|") | |
159 | wxT("TIFF files (*.tif)|*.tif|") | |
bf504f98 VS |
160 | wxT("PCX files (*.pcx)|*.pcx|") |
161 | wxT("ICO files (*.ico)|*.ico|") | |
162 | wxT("CUR files (*.cur)|*.cur"), | |
47f797bd WS |
163 | wxSAVE, |
164 | this); | |
1971d23c VZ |
165 | |
166 | if ( savefilename.empty() ) | |
167 | return; | |
168 | ||
5a566d89 VZ |
169 | wxString extension; |
170 | wxFileName::SplitPath(savefilename, NULL, NULL, &extension); | |
1971d23c | 171 | |
5a566d89 VZ |
172 | bool saved = false; |
173 | if ( extension == _T("bpp") ) | |
174 | { | |
175 | static const int bppvalues[] = | |
176 | { | |
177 | wxBMP_1BPP, | |
178 | wxBMP_1BPP_BW, | |
179 | wxBMP_4BPP, | |
180 | wxBMP_8BPP, | |
181 | wxBMP_8BPP_GREY, | |
182 | wxBMP_8BPP_RED, | |
183 | wxBMP_8BPP_PALETTE, | |
184 | wxBMP_24BPP | |
185 | }; | |
186 | ||
187 | static const wxString bppchoices[] = | |
188 | { | |
189 | _T("1 bpp color"), | |
190 | _T("1 bpp B&W"), | |
191 | _T("4 bpp color"), | |
192 | _T("8 bpp color"), | |
193 | _T("8 bpp greyscale"), | |
194 | _T("8 bpp red"), | |
195 | _T("8 bpp own palette"), | |
196 | _T("24 bpp") | |
197 | }; | |
198 | ||
199 | int bppselection = wxGetSingleChoiceIndex(_T("Set BMP BPP"), | |
200 | _T("Image sample: save file"), | |
201 | WXSIZEOF(bppchoices), | |
202 | bppchoices, | |
203 | this); | |
204 | if ( bppselection != -1 ) | |
205 | { | |
206 | int format = bppvalues[bppselection]; | |
207 | image.SetOption(wxIMAGE_OPTION_BMP_FORMAT, format); | |
208 | ||
209 | if ( format == wxBMP_8BPP_PALETTE ) | |
210 | { | |
211 | unsigned char *cmap = new unsigned char [256]; | |
212 | for ( int i = 0; i < 256; i++ ) | |
213 | cmap[i] = (unsigned char)i; | |
214 | image.SetPalette(wxPalette(256, cmap, cmap, cmap)); | |
215 | ||
216 | delete cmap; | |
217 | } | |
218 | } | |
1971d23c | 219 | } |
5a566d89 VZ |
220 | else if ( extension == _T("png") ) |
221 | { | |
222 | static const int pngvalues[] = | |
223 | { | |
224 | wxPNG_TYPE_COLOUR, | |
225 | wxPNG_TYPE_COLOUR, | |
226 | wxPNG_TYPE_GREY, | |
227 | wxPNG_TYPE_GREY, | |
228 | wxPNG_TYPE_GREY_RED, | |
229 | wxPNG_TYPE_GREY_RED, | |
230 | }; | |
231 | ||
232 | static const wxString pngchoices[] = | |
233 | { | |
234 | _T("Colour 8bpp"), | |
235 | _T("Colour 16bpp"), | |
236 | _T("Grey 8bpp"), | |
237 | _T("Grey 16bpp"), | |
238 | _T("Grey red 8bpp"), | |
239 | _T("Grey red 16bpp"), | |
240 | }; | |
241 | ||
242 | int sel = wxGetSingleChoiceIndex(_T("Set PNG format"), | |
243 | _T("Image sample: save file"), | |
244 | WXSIZEOF(pngchoices), | |
245 | pngchoices, | |
246 | this); | |
247 | if ( sel != -1 ) | |
248 | { | |
249 | image.SetOption(wxIMAGE_OPTION_PNG_FORMAT, pngvalues[sel]); | |
250 | image.SetOption(wxIMAGE_OPTION_PNG_BITDEPTH, sel % 2 ? 16 : 8); | |
251 | } | |
252 | } | |
253 | else if ( extension == _T("cur") ) | |
45647dcf | 254 | { |
51b07644 VZ |
255 | image.Rescale(32,32); |
256 | image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, 0); | |
45647dcf VS |
257 | image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, 0); |
258 | // This shows how you can save an image with explicitly | |
259 | // specified image format: | |
5a566d89 | 260 | saved = image.SaveFile(savefilename, wxBITMAP_TYPE_CUR); |
45647dcf | 261 | } |
5a566d89 VZ |
262 | |
263 | if ( !saved ) | |
45647dcf VS |
264 | { |
265 | // This one guesses image format from filename extension | |
266 | // (it may fail if the extension is not recognized): | |
5a566d89 | 267 | image.SaveFile(savefilename); |
45647dcf | 268 | } |
1971d23c VZ |
269 | } |
270 | ||
ab0f0386 VZ |
271 | private: |
272 | wxBitmap m_bitmap; | |
273 | ||
274 | DECLARE_EVENT_TABLE() | |
275 | }; | |
276 | ||
b3f04dc2 VZ |
277 | #ifdef wxHAVE_RAW_BITMAP |
278 | ||
279 | #include "wx/rawbmp.h" | |
280 | ||
281 | class MyRawBitmapFrame : public wxFrame | |
282 | { | |
283 | public: | |
284 | enum | |
285 | { | |
286 | BORDER = 15, | |
287 | SIZE = 150, | |
288 | REAL_SIZE = SIZE - 2*BORDER | |
289 | }; | |
290 | ||
291 | MyRawBitmapFrame(wxFrame *parent) | |
47f797bd | 292 | : wxFrame(parent, wxID_ANY, _T("Raw bitmaps (how exciting)")), |
b3f04dc2 VZ |
293 | m_bitmap(SIZE, SIZE, 32) |
294 | { | |
295 | SetClientSize(SIZE, SIZE); | |
296 | ||
095980e1 VZ |
297 | // another possibility: wxNativePixelData (don't forget to remove code |
298 | // setting alpha in the loop below then) | |
299 | typedef wxAlphaPixelData Data; | |
300 | ||
301 | Data data(m_bitmap, wxPoint(BORDER, BORDER), wxSize(REAL_SIZE, REAL_SIZE)); | |
b3f04dc2 VZ |
302 | if ( !data ) |
303 | { | |
304 | wxLogError(_T("Failed to gain raw access to bitmap data")); | |
305 | return; | |
306 | } | |
307 | ||
284f2b59 | 308 | // data.UseAlpha(); |
10b41b53 | 309 | |
095980e1 | 310 | Data::Iterator p(data); |
b3f04dc2 | 311 | |
b3f04dc2 VZ |
312 | for ( int y = 0; y < REAL_SIZE; ++y ) |
313 | { | |
095980e1 | 314 | Data::Iterator rowStart = p; |
b3f04dc2 VZ |
315 | |
316 | int r = y < REAL_SIZE/3 ? 255 : 0, | |
317 | g = (REAL_SIZE/3 <= y) && (y < 2*(REAL_SIZE/3)) ? 255 : 0, | |
318 | b = 2*(REAL_SIZE/3) <= y ? 255 : 0; | |
319 | ||
320 | for ( int x = 0; x < REAL_SIZE; ++x ) | |
321 | { | |
322 | p.Red() = r; | |
323 | p.Green() = g; | |
324 | p.Blue() = b; | |
095980e1 | 325 | p.Alpha() = (Data::Iterator::ChannelType)((x*255.)/REAL_SIZE); |
b3f04dc2 VZ |
326 | |
327 | ++p; // same as p.OffsetX(1) | |
328 | } | |
329 | ||
330 | p = rowStart; | |
1d2f48b6 | 331 | p.OffsetY(data, 1); |
b3f04dc2 VZ |
332 | } |
333 | } | |
334 | ||
335 | void OnPaint(wxPaintEvent& WXUNUSED(event)) | |
336 | { | |
337 | wxPaintDC dc( this ); | |
338 | dc.DrawText(_T("This is alpha and raw bitmap test"), 0, BORDER); | |
339 | dc.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE/2 - BORDER); | |
340 | dc.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE - 2*BORDER); | |
47f797bd | 341 | dc.DrawBitmap( m_bitmap, 0, 0, true /* use mask */ ); |
b3f04dc2 VZ |
342 | } |
343 | ||
344 | private: | |
345 | wxBitmap m_bitmap; | |
346 | ||
347 | DECLARE_EVENT_TABLE() | |
348 | }; | |
349 | ||
350 | #endif // wxHAVE_RAW_BITMAP | |
351 | ||
01111366 RR |
352 | // MyApp |
353 | ||
354 | class MyApp: public wxApp | |
355 | { | |
1d5b7a0b VZ |
356 | public: |
357 | virtual bool OnInit(); | |
01111366 RR |
358 | }; |
359 | ||
360 | // main program | |
361 | ||
362 | IMPLEMENT_APP(MyApp) | |
363 | ||
364 | // MyCanvas | |
365 | ||
366 | IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow) | |
367 | ||
ab0f0386 | 368 | BEGIN_EVENT_TABLE(MyImageFrame, wxFrame) |
0c543b7a VZ |
369 | EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground) |
370 | EVT_PAINT(MyImageFrame::OnPaint) | |
371 | EVT_LEFT_DCLICK(MyImageFrame::OnSave) | |
ab0f0386 VZ |
372 | END_EVENT_TABLE() |
373 | ||
b3f04dc2 VZ |
374 | #ifdef wxHAVE_RAW_BITMAP |
375 | ||
376 | BEGIN_EVENT_TABLE(MyRawBitmapFrame, wxFrame) | |
377 | EVT_PAINT(MyRawBitmapFrame::OnPaint) | |
378 | END_EVENT_TABLE() | |
379 | ||
380 | #endif // wxHAVE_RAW_BITMAP | |
381 | ||
1d5b7a0b VZ |
382 | BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) |
383 | EVT_PAINT(MyCanvas::OnPaint) | |
01111366 RR |
384 | END_EVENT_TABLE() |
385 | ||
acbd13a3 | 386 | MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id, |
1d5b7a0b | 387 | const wxPoint &pos, const wxSize &size ) |
81278df2 | 388 | : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER ) |
81278df2 VZ |
389 | , m_bmpSmileXpm((const char **) smile_xpm) |
390 | , m_iconSmileXpm((const char **) smile_xpm) | |
01111366 | 391 | { |
60a41aee RR |
392 | my_horse_png = (wxBitmap*) NULL; |
393 | my_horse_jpeg = (wxBitmap*) NULL; | |
394 | my_horse_gif = (wxBitmap*) NULL; | |
395 | my_horse_bmp = (wxBitmap*) NULL; | |
51b07644 | 396 | my_horse_bmp2 = (wxBitmap*) NULL; |
60a41aee RR |
397 | my_horse_pcx = (wxBitmap*) NULL; |
398 | my_horse_pnm = (wxBitmap*) NULL; | |
399 | my_horse_tiff = (wxBitmap*) NULL; | |
9d8bdf2d | 400 | my_horse_xpm = (wxBitmap*) NULL; |
bf504f98 VS |
401 | my_horse_ico32 = (wxBitmap*) NULL; |
402 | my_horse_ico16 = (wxBitmap*) NULL; | |
403 | my_horse_ico = (wxBitmap*) NULL; | |
404 | my_horse_cur = (wxBitmap*) NULL; | |
2b5f62a0 | 405 | my_horse_ani = (wxBitmap*) NULL; |
bf504f98 | 406 | |
dc1dbfc6 | 407 | my_smile_xbm = (wxBitmap*) NULL; |
60a41aee RR |
408 | my_square = (wxBitmap*) NULL; |
409 | my_anti = (wxBitmap*) NULL; | |
410 | ||
2b5f62a0 VZ |
411 | m_ani_images = 0 ; |
412 | ||
60a41aee RR |
413 | SetBackgroundColour(* wxWHITE); |
414 | ||
415 | wxBitmap bitmap( 100, 100 ); | |
416 | ||
417 | wxMemoryDC dc; | |
418 | dc.SelectObject( bitmap ); | |
a60b1f5d | 419 | dc.SetBrush( wxBrush( wxT("orange"), wxSOLID ) ); |
cf3da716 | 420 | dc.SetPen( *wxBLACK_PEN ); |
60a41aee | 421 | dc.DrawRectangle( 0, 0, 100, 100 ); |
cf3da716 RR |
422 | dc.SetBrush( *wxWHITE_BRUSH ); |
423 | dc.DrawRectangle( 20, 20, 60, 60 ); | |
60a41aee RR |
424 | dc.SelectObject( wxNullBitmap ); |
425 | ||
426 | // try to find the directory with our images | |
427 | wxString dir; | |
4693b20c | 428 | if ( wxFile::Exists(wxT("./horse.png")) ) |
2b5f62a0 | 429 | dir = wxT("./"); |
4693b20c | 430 | else if ( wxFile::Exists(wxT("../horse.png")) ) |
2b5f62a0 | 431 | dir = wxT("../"); |
60a41aee | 432 | else |
4693b20c | 433 | wxLogWarning(wxT("Can't find image files in either '.' or '..'!")); |
60a41aee | 434 | |
a8d4e3ac | 435 | wxImage image = bitmap.ConvertToImage(); |
60a41aee | 436 | |
bea56879 | 437 | #if wxUSE_LIBPNG |
51b07644 | 438 | if ( !image.SaveFile( dir + _T("test.png"), wxBITMAP_TYPE_PNG )) |
4693b20c | 439 | wxLogError(wxT("Can't save file")); |
f6bcfd97 | 440 | |
176fe5af | 441 | image.Destroy(); |
60a41aee | 442 | |
30ad0a14 VZ |
443 | if ( image.LoadFile( dir + _T("test.png") ) ) |
444 | my_square = new wxBitmap( image ); | |
f6bcfd97 | 445 | |
176fe5af | 446 | image.Destroy(); |
cc977e5f | 447 | |
51b07644 | 448 | if ( !image.LoadFile( dir + _T("horse.png")) ) |
4693b20c | 449 | wxLogError(wxT("Can't load PNG image")); |
60a41aee | 450 | else |
a8d4e3ac | 451 | my_horse_png = new wxBitmap( image ); |
bea56879 | 452 | #endif // wxUSE_LIBPNG |
60a41aee | 453 | |
bea56879 | 454 | #if wxUSE_LIBJPEG |
176fe5af | 455 | image.Destroy(); |
279ababf | 456 | |
51b07644 | 457 | if ( !image.LoadFile( dir + _T("horse.jpg")) ) |
4693b20c | 458 | wxLogError(wxT("Can't load JPG image")); |
60a41aee | 459 | else |
9d8bdf2d | 460 | my_horse_jpeg = new wxBitmap( image ); |
bea56879 | 461 | #endif // wxUSE_LIBJPEG |
6e47faf1 JS |
462 | |
463 | #if wxUSE_GIF | |
176fe5af | 464 | image.Destroy(); |
279ababf | 465 | |
2b5f62a0 | 466 | if ( !image.LoadFile( dir + _T("horse.gif" )) ) |
4693b20c | 467 | wxLogError(wxT("Can't load GIF image")); |
60a41aee | 468 | else |
9d8bdf2d | 469 | my_horse_gif = new wxBitmap( image ); |
6e47faf1 | 470 | #endif |
cbd4be25 | 471 | |
6e47faf1 | 472 | #if wxUSE_PCX |
176fe5af | 473 | image.Destroy(); |
279ababf | 474 | |
51b07644 | 475 | if ( !image.LoadFile( dir + _T("horse.pcx"), wxBITMAP_TYPE_PCX ) ) |
4693b20c | 476 | wxLogError(wxT("Can't load PCX image")); |
60a41aee | 477 | else |
9d8bdf2d | 478 | my_horse_pcx = new wxBitmap( image ); |
6e47faf1 | 479 | #endif |
cbd4be25 | 480 | |
176fe5af | 481 | image.Destroy(); |
279ababf | 482 | |
51b07644 | 483 | if ( !image.LoadFile( dir + _T("horse.bmp"), wxBITMAP_TYPE_BMP ) ) |
4693b20c | 484 | wxLogError(wxT("Can't load BMP image")); |
60a41aee | 485 | else |
9d8bdf2d | 486 | my_horse_bmp = new wxBitmap( image ); |
6319afe3 | 487 | |
a8d4e3ac | 488 | #if wxUSE_XPM |
9d8bdf2d VS |
489 | image.Destroy(); |
490 | ||
51b07644 | 491 | if ( !image.LoadFile( dir + _T("horse.xpm"), wxBITMAP_TYPE_XPM ) ) |
4693b20c | 492 | wxLogError(wxT("Can't load XPM image")); |
9d8bdf2d VS |
493 | else |
494 | my_horse_xpm = new wxBitmap( image ); | |
495 | ||
51b07644 | 496 | if ( !image.SaveFile( dir + _T("test.xpm"), wxBITMAP_TYPE_XPM )) |
4693b20c | 497 | wxLogError(wxT("Can't save file")); |
a8d4e3ac VS |
498 | #endif |
499 | ||
6e47faf1 | 500 | #if wxUSE_PNM |
176fe5af | 501 | image.Destroy(); |
279ababf | 502 | |
51b07644 | 503 | if ( !image.LoadFile( dir + _T("horse.pnm"), wxBITMAP_TYPE_PNM ) ) |
4693b20c | 504 | wxLogError(wxT("Can't load PNM image")); |
60a41aee | 505 | else |
9d8bdf2d | 506 | my_horse_pnm = new wxBitmap( image ); |
6e47faf1 | 507 | #endif |
f048e32f | 508 | |
60a41aee | 509 | #if wxUSE_LIBTIFF |
176fe5af | 510 | image.Destroy(); |
279ababf | 511 | |
51b07644 | 512 | if ( !image.LoadFile( dir + _T("horse.tif"), wxBITMAP_TYPE_TIF ) ) |
4693b20c | 513 | wxLogError(wxT("Can't load TIFF image")); |
60a41aee | 514 | else |
9d8bdf2d | 515 | my_horse_tiff = new wxBitmap( image ); |
60a41aee RR |
516 | #endif |
517 | ||
60a41aee | 518 | CreateAntiAliasedBitmap(); |
dc1dbfc6 RL |
519 | |
520 | my_smile_xbm = new wxBitmap( (const char*)smile_bits, smile_width, | |
521 | smile_height, 1 ); | |
bfef20bf GRG |
522 | |
523 | // demonstrates XPM automatically using the mask when saving | |
524 | if ( m_bmpSmileXpm.Ok() ) | |
51b07644 | 525 | m_bmpSmileXpm.SaveFile(_T("saved.xpm"), wxBITMAP_TYPE_XPM); |
bf504f98 VS |
526 | |
527 | #if wxUSE_ICO_CUR | |
528 | image.Destroy(); | |
529 | ||
51b07644 | 530 | if ( !image.LoadFile( dir + _T("horse.ico"), wxBITMAP_TYPE_ICO, 0 ) ) |
bf504f98 VS |
531 | wxLogError(wxT("Can't load first ICO image")); |
532 | else | |
533 | my_horse_ico32 = new wxBitmap( image ); | |
51b07644 | 534 | |
bf504f98 VS |
535 | image.Destroy(); |
536 | ||
51b07644 | 537 | if ( !image.LoadFile( dir + _T("horse.ico"), wxBITMAP_TYPE_ICO, 1 ) ) |
bf504f98 VS |
538 | wxLogError(wxT("Can't load second ICO image")); |
539 | else | |
540 | my_horse_ico16 = new wxBitmap( image ); | |
541 | ||
542 | image.Destroy(); | |
543 | ||
51b07644 | 544 | if ( !image.LoadFile( dir + _T("horse.ico") ) ) |
bf504f98 VS |
545 | wxLogError(wxT("Can't load best ICO image")); |
546 | else | |
547 | my_horse_ico = new wxBitmap( image ); | |
548 | ||
549 | image.Destroy(); | |
550 | ||
51b07644 | 551 | if ( !image.LoadFile( dir + _T("horse.cur"), wxBITMAP_TYPE_CUR ) ) |
bf504f98 VS |
552 | wxLogError(wxT("Can't load best ICO image")); |
553 | else | |
51b07644 | 554 | { |
bf504f98 | 555 | my_horse_cur = new wxBitmap( image ); |
fd94e8aa VS |
556 | xH = 30 + image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X) ; |
557 | yH = 2420 + image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y) ; | |
51b07644 | 558 | } |
2b5f62a0 | 559 | |
08811762 | 560 | m_ani_images = wxImage::GetImageCount ( dir + _T("horse3.ani"), wxBITMAP_TYPE_ANI ); |
2b5f62a0 VZ |
561 | if (m_ani_images==0) |
562 | wxLogError(wxT("No ANI-format images found")); | |
563 | else | |
564 | my_horse_ani = new wxBitmap [m_ani_images]; | |
565 | int i ; | |
566 | for (i=0; i < m_ani_images; i++) | |
567 | { | |
568 | image.Destroy(); | |
08811762 | 569 | if (!image.LoadFile( dir + _T("horse3.ani"), wxBITMAP_TYPE_ANI, i )) |
2b5f62a0 VZ |
570 | { |
571 | wxString tmp = wxT("Can't load image number "); | |
572 | tmp << i ; | |
573 | wxLogError(tmp); | |
574 | } | |
925e9792 | 575 | else |
2b5f62a0 VZ |
576 | my_horse_ani [i] = wxBitmap( image ); |
577 | } | |
da9df1f5 | 578 | #endif // wxUSE_ICO_CUR |
bf504f98 | 579 | |
51b07644 VZ |
580 | image.Destroy(); |
581 | ||
582 | // test image loading from stream | |
583 | wxFile file(dir + _T("horse.bmp")); | |
30ad0a14 | 584 | if ( file.IsOpened() ) |
51b07644 | 585 | { |
9386cb75 WS |
586 | wxFileOffset len = file.Length(); |
587 | size_t dataSize = (size_t)len; | |
588 | void *data = malloc(dataSize); | |
589 | if ( file.Read(data, dataSize) != len ) | |
30ad0a14 | 590 | wxLogError(_T("Reading bitmap file failed")); |
51b07644 | 591 | else |
30ad0a14 | 592 | { |
9386cb75 | 593 | wxMemoryInputStream mis(data, dataSize); |
30ad0a14 VZ |
594 | if ( !image.LoadFile(mis) ) |
595 | wxLogError(wxT("Can't load BMP image from stream")); | |
596 | else | |
597 | my_horse_bmp2 = new wxBitmap( image ); | |
598 | } | |
51b07644 | 599 | |
30ad0a14 VZ |
600 | free(data); |
601 | } | |
01111366 RR |
602 | } |
603 | ||
1d5b7a0b | 604 | MyCanvas::~MyCanvas() |
01111366 | 605 | { |
60a41aee RR |
606 | delete my_horse_pnm; |
607 | delete my_horse_png; | |
608 | delete my_horse_jpeg; | |
609 | delete my_horse_gif; | |
610 | delete my_horse_bmp; | |
51b07644 | 611 | delete my_horse_bmp2; |
60a41aee RR |
612 | delete my_horse_pcx; |
613 | delete my_horse_tiff; | |
9d8bdf2d | 614 | delete my_horse_xpm; |
bf504f98 VS |
615 | delete my_horse_ico32; |
616 | delete my_horse_ico16; | |
617 | delete my_horse_ico; | |
2f6c54eb | 618 | delete my_horse_cur; |
2b5f62a0 | 619 | delete [] my_horse_ani; |
dc1dbfc6 | 620 | delete my_smile_xbm; |
60a41aee RR |
621 | delete my_square; |
622 | delete my_anti; | |
01111366 RR |
623 | } |
624 | ||
625 | void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) ) | |
626 | { | |
be25e480 RR |
627 | wxPaintDC dc( this ); |
628 | PrepareDC( dc ); | |
a91b47e8 | 629 | |
51b07644 | 630 | dc.DrawText( _T("Loaded image"), 30, 10 ); |
be25e480 | 631 | if (my_square && my_square->Ok()) dc.DrawBitmap( *my_square, 30, 30 ); |
f048e32f | 632 | |
51b07644 | 633 | dc.DrawText( _T("Drawn directly"), 150, 10 ); |
a60b1f5d | 634 | dc.SetBrush( wxBrush( wxT("orange"), wxSOLID ) ); |
cf3da716 | 635 | dc.SetPen( *wxBLACK_PEN ); |
be25e480 | 636 | dc.DrawRectangle( 150, 30, 100, 100 ); |
cf3da716 RR |
637 | dc.SetBrush( *wxWHITE_BRUSH ); |
638 | dc.DrawRectangle( 170, 50, 60, 60 ); | |
be25e480 | 639 | |
408b4168 | 640 | if (my_anti && my_anti->Ok()) |
be25e480 RR |
641 | dc.DrawBitmap( *my_anti, 280, 30 ); |
642 | ||
51b07644 | 643 | dc.DrawText( _T("PNG handler"), 30, 135 ); |
be25e480 | 644 | if (my_horse_png && my_horse_png->Ok()) |
408b4168 | 645 | { |
be25e480 RR |
646 | dc.DrawBitmap( *my_horse_png, 30, 150 ); |
647 | wxRect rect(0,0,100,100); | |
648 | wxBitmap sub( my_horse_png->GetSubBitmap(rect) ); | |
51b07644 | 649 | dc.DrawText( _T("GetSubBitmap()"), 280, 190 ); |
be25e480 RR |
650 | dc.DrawBitmap( sub, 280, 210 ); |
651 | } | |
652 | ||
51b07644 | 653 | dc.DrawText( _T("JPEG handler"), 30, 365 ); |
408b4168 | 654 | if (my_horse_jpeg && my_horse_jpeg->Ok()) |
be25e480 RR |
655 | dc.DrawBitmap( *my_horse_jpeg, 30, 380 ); |
656 | ||
51b07644 | 657 | dc.DrawText( _T("GIF handler"), 30, 595 ); |
408b4168 | 658 | if (my_horse_gif && my_horse_gif->Ok()) |
be25e480 RR |
659 | dc.DrawBitmap( *my_horse_gif, 30, 610 ); |
660 | ||
51b07644 | 661 | dc.DrawText( _T("PCX handler"), 30, 825 ); |
408b4168 | 662 | if (my_horse_pcx && my_horse_pcx->Ok()) |
be25e480 RR |
663 | dc.DrawBitmap( *my_horse_pcx, 30, 840 ); |
664 | ||
51b07644 | 665 | dc.DrawText( _T("BMP handler"), 30, 1055 ); |
408b4168 | 666 | if (my_horse_bmp && my_horse_bmp->Ok()) |
be25e480 RR |
667 | dc.DrawBitmap( *my_horse_bmp, 30, 1070 ); |
668 | ||
51b07644 VZ |
669 | dc.DrawText( _T("BMP read from memory"), 280, 1055 ); |
670 | if (my_horse_bmp2 && my_horse_bmp2->Ok()) | |
671 | dc.DrawBitmap( *my_horse_bmp2, 280, 1070 ); | |
672 | ||
673 | dc.DrawText( _T("PNM handler"), 30, 1285 ); | |
408b4168 | 674 | if (my_horse_pnm && my_horse_pnm->Ok()) |
be25e480 | 675 | dc.DrawBitmap( *my_horse_pnm, 30, 1300 ); |
408b4168 | 676 | |
51b07644 | 677 | dc.DrawText( _T("TIFF handler"), 30, 1515 ); |
408b4168 | 678 | if (my_horse_tiff && my_horse_tiff->Ok()) |
4b05d973 | 679 | dc.DrawBitmap( *my_horse_tiff, 30, 1530 ); |
be25e480 | 680 | |
51b07644 | 681 | dc.DrawText( _T("XPM handler"), 30, 1745 ); |
9d8bdf2d VS |
682 | if (my_horse_xpm && my_horse_xpm->Ok()) |
683 | dc.DrawBitmap( *my_horse_xpm, 30, 1760 ); | |
684 | ||
bf504f98 | 685 | |
408b4168 | 686 | if (my_smile_xbm && my_smile_xbm->Ok()) |
be25e480 | 687 | { |
51b07644 VZ |
688 | dc.DrawText( _T("XBM bitmap"), 30, 1975 ); |
689 | dc.DrawText( _T("(green on red)"), 30, 1990 ); | |
690 | dc.SetTextForeground( _T("GREEN") ); | |
691 | dc.SetTextBackground( _T("RED") ); | |
9d8bdf2d | 692 | dc.DrawBitmap( *my_smile_xbm, 30, 2010 ); |
408b4168 | 693 | |
a60b1f5d | 694 | dc.SetTextForeground( wxT("BLACK") ); |
51b07644 VZ |
695 | dc.DrawText( _T("After wxImage conversion"), 150, 1975 ); |
696 | dc.DrawText( _T("(red on white)"), 150, 1990 ); | |
a60b1f5d | 697 | dc.SetTextForeground( wxT("RED") ); |
a8d4e3ac | 698 | wxImage i = my_smile_xbm->ConvertToImage(); |
176fe5af GRG |
699 | i.SetMaskColour( 255, 255, 255 ); |
700 | i.Replace( 0, 0, 0, | |
be25e480 RR |
701 | wxRED_PEN->GetColour().Red(), |
702 | wxRED_PEN->GetColour().Green(), | |
703 | wxRED_PEN->GetColour().Blue() ); | |
47f797bd | 704 | dc.DrawBitmap( wxBitmap(i), 150, 2010, true ); |
a60b1f5d | 705 | dc.SetTextForeground( wxT("BLACK") ); |
be25e480 | 706 | } |
176fe5af | 707 | |
408b4168 | 708 | |
41fbc841 | 709 | wxBitmap mono( 60,50,1 ); |
82ea63e6 RR |
710 | wxMemoryDC memdc; |
711 | memdc.SelectObject( mono ); | |
bfef20bf | 712 | memdc.SetPen( *wxBLACK_PEN ); |
82ea63e6 | 713 | memdc.SetBrush( *wxWHITE_BRUSH ); |
41fbc841 RR |
714 | memdc.DrawRectangle( 0,0,60,50 ); |
715 | memdc.SetTextForeground( *wxBLACK ); | |
9fe7d430 RR |
716 | #ifndef __WXGTK20__ |
717 | // I cannot convince GTK2 to draw into mono bitmaps | |
51b07644 | 718 | memdc.DrawText( _T("Hi!"), 5, 5 ); |
9fe7d430 | 719 | #endif |
41fbc841 RR |
720 | memdc.SetBrush( *wxBLACK_BRUSH ); |
721 | memdc.DrawRectangle( 33,5,20,20 ); | |
722 | memdc.SetPen( *wxRED_PEN ); | |
723 | memdc.DrawLine( 5, 42, 50, 42 ); | |
82ea63e6 | 724 | memdc.SelectObject( wxNullBitmap ); |
176fe5af | 725 | |
408b4168 | 726 | if (mono.Ok()) |
82ea63e6 | 727 | { |
51b07644 VZ |
728 | dc.DrawText( _T("Mono bitmap"), 30, 2095 ); |
729 | dc.DrawText( _T("(red on green)"), 30, 2110 ); | |
a60b1f5d MB |
730 | dc.SetTextForeground( wxT("RED") ); |
731 | dc.SetTextBackground( wxT("GREEN") ); | |
9d8bdf2d | 732 | dc.DrawBitmap( mono, 30, 2130 ); |
408b4168 | 733 | |
a60b1f5d | 734 | dc.SetTextForeground( wxT("BLACK") ); |
51b07644 VZ |
735 | dc.DrawText( _T("After wxImage conversion"), 150, 2095 ); |
736 | dc.DrawText( _T("(red on white)"), 150, 2110 ); | |
a60b1f5d | 737 | dc.SetTextForeground( wxT("RED") ); |
a8d4e3ac | 738 | wxImage i = mono.ConvertToImage(); |
408b4168 VZ |
739 | i.SetMaskColour( 255,255,255 ); |
740 | i.Replace( 0,0,0, | |
82ea63e6 RR |
741 | wxRED_PEN->GetColour().Red(), |
742 | wxRED_PEN->GetColour().Green(), | |
743 | wxRED_PEN->GetColour().Blue() ); | |
47f797bd | 744 | dc.DrawBitmap( wxBitmap(i), 150, 2130, true ); |
a60b1f5d | 745 | dc.SetTextForeground( wxT("BLACK") ); |
bea56879 VZ |
746 | } |
747 | ||
f9ebac93 RR |
748 | // For testing transparency |
749 | dc.SetBrush( *wxRED_BRUSH ); | |
750 | dc.DrawRectangle( 20, 2220, 560, 68 ); | |
925e9792 | 751 | |
f9ebac93 | 752 | dc.DrawText(_T("XPM bitmap"), 30, 2230 ); |
bea56879 | 753 | if ( m_bmpSmileXpm.Ok() ) |
47f797bd | 754 | dc.DrawBitmap(m_bmpSmileXpm, 30, 2250, true); |
bea56879 | 755 | |
f9ebac93 | 756 | dc.DrawText(_T("XPM icon"), 110, 2230 ); |
bea56879 | 757 | if ( m_iconSmileXpm.Ok() ) |
f9ebac93 | 758 | dc.DrawIcon(m_iconSmileXpm, 110, 2250); |
925e9792 WS |
759 | |
760 | // testing icon -> bitmap conversion | |
f9ebac93 | 761 | wxBitmap to_blit( m_iconSmileXpm ); |
3ef37e7f JS |
762 | if (to_blit.Ok()) |
763 | { | |
764 | dc.DrawText( _T("SubBitmap"), 170, 2230 ); | |
765 | wxBitmap sub = to_blit.GetSubBitmap( wxRect(0,0,15,15) ); | |
766 | if (sub.Ok()) | |
47f797bd | 767 | dc.DrawBitmap( sub, 170, 2250, true ); |
3ef37e7f JS |
768 | |
769 | dc.DrawText( _T("Enlarged"), 250, 2230 ); | |
770 | dc.SetUserScale( 1.5, 1.5 ); | |
47f797bd | 771 | dc.DrawBitmap( to_blit, (int)(250/1.5), (int)(2250/1.5), true ); |
3ef37e7f | 772 | dc.SetUserScale( 2, 2 ); |
47f797bd | 773 | dc.DrawBitmap( to_blit, (int)(300/2), (int)(2250/2), true ); |
3ef37e7f | 774 | dc.SetUserScale( 1.0, 1.0 ); |
925e9792 | 775 | |
3ef37e7f JS |
776 | dc.DrawText( _T("Blit"), 400, 2230); |
777 | wxMemoryDC blit_dc; | |
778 | blit_dc.SelectObject( to_blit ); | |
47f797bd | 779 | dc.Blit( 400, 2250, to_blit.GetWidth(), to_blit.GetHeight(), &blit_dc, 0, 0, wxCOPY, true ); |
3ef37e7f | 780 | dc.SetUserScale( 1.5, 1.5 ); |
47f797bd | 781 | dc.Blit( (int)(450/1.5), (int)(2250/1.5), to_blit.GetWidth(), to_blit.GetHeight(), &blit_dc, 0, 0, wxCOPY, true ); |
3ef37e7f | 782 | dc.SetUserScale( 2, 2 ); |
47f797bd | 783 | dc.Blit( (int)(500/2), (int)(2250/2), to_blit.GetWidth(), to_blit.GetHeight(), &blit_dc, 0, 0, wxCOPY, true ); |
3ef37e7f JS |
784 | dc.SetUserScale( 1.0, 1.0 ); |
785 | } | |
bf504f98 | 786 | |
51b07644 | 787 | dc.DrawText( _T("ICO handler (1st image)"), 30, 2290 ); |
bf504f98 | 788 | if (my_horse_ico32 && my_horse_ico32->Ok()) |
47f797bd | 789 | dc.DrawBitmap( *my_horse_ico32, 30, 2330, true ); |
bf504f98 | 790 | |
51b07644 | 791 | dc.DrawText( _T("ICO handler (2nd image)"), 230, 2290 ); |
bf504f98 | 792 | if (my_horse_ico16 && my_horse_ico16->Ok()) |
47f797bd | 793 | dc.DrawBitmap( *my_horse_ico16, 230, 2330, true ); |
51b07644 VZ |
794 | |
795 | dc.DrawText( _T("ICO handler (best image)"), 430, 2290 ); | |
bf504f98 | 796 | if (my_horse_ico && my_horse_ico->Ok()) |
47f797bd | 797 | dc.DrawBitmap( *my_horse_ico, 430, 2330, true ); |
bf504f98 | 798 | |
51b07644 | 799 | dc.DrawText( _T("CUR handler"), 30, 2390 ); |
bf504f98 | 800 | if (my_horse_cur && my_horse_cur->Ok()) |
51b07644 | 801 | { |
47f797bd | 802 | dc.DrawBitmap( *my_horse_cur, 30, 2420, true ); |
bf504f98 VS |
803 | dc.SetPen (*wxRED_PEN); |
804 | dc.DrawLine (xH-10,yH,xH+10,yH); | |
805 | dc.DrawLine (xH,yH-10,xH,yH+10); | |
51b07644 | 806 | } |
2b5f62a0 VZ |
807 | dc.DrawText( _T("ANI handler"), 230, 2390 ); |
808 | int i ; | |
809 | for (i=0; i < m_ani_images; i ++) | |
810 | if (my_horse_ani[i].Ok()) | |
811 | { | |
47f797bd | 812 | dc.DrawBitmap( my_horse_ani[i], 230 + i * 2 * my_horse_ani[i].GetWidth() , 2420, true ); |
2b5f62a0 | 813 | } |
9390a202 | 814 | } |
1d5b7a0b | 815 | |
9390a202 RR |
816 | void MyCanvas::CreateAntiAliasedBitmap() |
817 | { | |
818 | wxBitmap bitmap( 300, 300 ); | |
a91b47e8 | 819 | |
9390a202 | 820 | wxMemoryDC dc; |
a91b47e8 | 821 | |
9390a202 | 822 | dc.SelectObject( bitmap ); |
a91b47e8 | 823 | |
9390a202 | 824 | dc.Clear(); |
f048e32f | 825 | |
6e47faf1 | 826 | dc.SetFont( wxFont( 24, wxDECORATIVE, wxNORMAL, wxNORMAL) ); |
a60b1f5d | 827 | dc.SetTextForeground( wxT("RED") ); |
51b07644 VZ |
828 | dc.DrawText( _T("This is anti-aliased Text."), 20, 20 ); |
829 | dc.DrawText( _T("And a Rectangle."), 20, 60 ); | |
f048e32f | 830 | |
9390a202 | 831 | dc.SetBrush( *wxRED_BRUSH ); |
91b8de8d | 832 | dc.SetPen( *wxTRANSPARENT_PEN ); |
9390a202 | 833 | dc.DrawRoundedRectangle( 20, 100, 200, 180, 20 ); |
f048e32f | 834 | |
a8d4e3ac | 835 | wxImage original= bitmap.ConvertToImage(); |
9390a202 | 836 | wxImage anti( 150, 150 ); |
a91b47e8 | 837 | |
9390a202 | 838 | /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */ |
f048e32f | 839 | |
9390a202 RR |
840 | for (int y = 1; y < 149; y++) |
841 | for (int x = 1; x < 149; x++) | |
842 | { | |
843 | int red = original.GetRed( x*2, y*2 ) + | |
f048e32f VZ |
844 | original.GetRed( x*2-1, y*2 ) + |
845 | original.GetRed( x*2, y*2+1 ) + | |
9390a202 | 846 | original.GetRed( x*2+1, y*2+1 ); |
f048e32f VZ |
847 | red = red/4; |
848 | ||
9390a202 | 849 | int green = original.GetGreen( x*2, y*2 ) + |
f048e32f VZ |
850 | original.GetGreen( x*2-1, y*2 ) + |
851 | original.GetGreen( x*2, y*2+1 ) + | |
9390a202 | 852 | original.GetGreen( x*2+1, y*2+1 ); |
f048e32f VZ |
853 | green = green/4; |
854 | ||
9390a202 | 855 | int blue = original.GetBlue( x*2, y*2 ) + |
f048e32f VZ |
856 | original.GetBlue( x*2-1, y*2 ) + |
857 | original.GetBlue( x*2, y*2+1 ) + | |
9390a202 | 858 | original.GetBlue( x*2+1, y*2+1 ); |
f048e32f | 859 | blue = blue/4; |
925e9792 | 860 | anti.SetRGB( x, y, (unsigned char)red, (unsigned char)green, (unsigned char)blue ); |
9390a202 | 861 | } |
368d59f0 | 862 | my_anti = new wxBitmap(anti); |
01111366 RR |
863 | } |
864 | ||
865 | // MyFrame | |
866 | ||
b3f04dc2 VZ |
867 | enum |
868 | { | |
869 | ID_QUIT = 108, | |
870 | ID_ABOUT, | |
871 | ID_NEW, | |
872 | ID_SHOWRAW | |
873 | }; | |
01111366 RR |
874 | |
875 | IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame ) | |
876 | ||
877 | BEGIN_EVENT_TABLE(MyFrame,wxFrame) | |
878 | EVT_MENU (ID_ABOUT, MyFrame::OnAbout) | |
879 | EVT_MENU (ID_QUIT, MyFrame::OnQuit) | |
ab0f0386 | 880 | EVT_MENU (ID_NEW, MyFrame::OnNewFrame) |
b3f04dc2 VZ |
881 | #ifdef wxHAVE_RAW_BITMAP |
882 | EVT_MENU (ID_SHOWRAW, MyFrame::OnTestRawBitmap) | |
883 | #endif | |
da9df1f5 | 884 | |
dd38c875 | 885 | #if wxUSE_CLIPBOARD |
da9df1f5 VZ |
886 | EVT_MENU(wxID_COPY, MyFrame::OnCopy) |
887 | EVT_MENU(wxID_PASTE, MyFrame::OnPaste) | |
888 | #endif // wxUSE_CLIPBOARD | |
01111366 RR |
889 | END_EVENT_TABLE() |
890 | ||
1d5b7a0b | 891 | MyFrame::MyFrame() |
47f797bd | 892 | : wxFrame( (wxFrame *)NULL, wxID_ANY, _T("wxImage sample"), |
1d5b7a0b | 893 | wxPoint(20,20), wxSize(470,360) ) |
01111366 | 894 | { |
da9df1f5 VZ |
895 | wxMenuBar *menu_bar = new wxMenuBar(); |
896 | ||
897 | wxMenu *menuImage = new wxMenu; | |
898 | menuImage->Append( ID_NEW, _T("&Show any image...\tCtrl-O")); | |
1e3c12d7 | 899 | |
b3f04dc2 | 900 | #ifdef wxHAVE_RAW_BITMAP |
da9df1f5 | 901 | menuImage->Append( ID_SHOWRAW, _T("Test &raw bitmap...\tCtrl-R")); |
b3f04dc2 | 902 | #endif |
da9df1f5 VZ |
903 | menuImage->AppendSeparator(); |
904 | menuImage->Append( ID_ABOUT, _T("&About...")); | |
905 | menuImage->AppendSeparator(); | |
906 | menuImage->Append( ID_QUIT, _T("E&xit\tCtrl-Q")); | |
907 | menu_bar->Append(menuImage, _T("&Image")); | |
908 | ||
dd38c875 | 909 | #if wxUSE_CLIPBOARD |
da9df1f5 VZ |
910 | wxMenu *menuClipboard = new wxMenu; |
911 | menuClipboard->Append(wxID_COPY, _T("&Copy test image\tCtrl-C")); | |
912 | menuClipboard->Append(wxID_PASTE, _T("&Paste image\tCtrl-V")); | |
913 | menu_bar->Append(menuClipboard, _T("&Clipboard")); | |
914 | #endif // wxUSE_CLIPBOARD | |
3d05544e | 915 | |
01111366 | 916 | SetMenuBar( menu_bar ); |
1d5b7a0b | 917 | |
8520f137 | 918 | #if wxUSE_STATUSBAR |
917e533b RR |
919 | CreateStatusBar(2); |
920 | int widths[] = { -1, 100 }; | |
921 | SetStatusWidths( 2, widths ); | |
8520f137 | 922 | #endif // wxUSE_STATUSBAR |
1d5b7a0b | 923 | |
47f797bd | 924 | m_canvas = new MyCanvas( this, wxID_ANY, wxPoint(0,0), wxSize(10,10) ); |
cbd4be25 | 925 | |
bf504f98 VS |
926 | // 500 width * 2500 height |
927 | m_canvas->SetScrollbars( 10, 10, 50, 250 ); | |
01111366 RR |
928 | } |
929 | ||
930 | void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) ) | |
931 | { | |
47f797bd | 932 | Close( true ); |
01111366 RR |
933 | } |
934 | ||
935 | void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) ) | |
936 | { | |
51b07644 VZ |
937 | (void)wxMessageBox( _T("wxImage demo\n") |
938 | _T("Robert Roebling (c) 1998,2000"), | |
939 | _T("About wxImage Demo"), wxICON_INFORMATION | wxOK ); | |
fb1585ae RR |
940 | } |
941 | ||
ab0f0386 VZ |
942 | void MyFrame::OnNewFrame( wxCommandEvent &WXUNUSED(event) ) |
943 | { | |
f6bcfd97 BP |
944 | wxString filename = wxFileSelector(_T("Select image file")); |
945 | if ( !filename ) | |
946 | return; | |
947 | ||
948 | wxImage image; | |
949 | if ( !image.LoadFile(filename) ) | |
950 | { | |
951 | wxLogError(_T("Couldn't load image from '%s'."), filename.c_str()); | |
952 | ||
953 | return; | |
954 | } | |
955 | ||
368d59f0 | 956 | (new MyImageFrame(this, wxBitmap(image)))->Show(); |
ab0f0386 VZ |
957 | } |
958 | ||
b3f04dc2 VZ |
959 | #ifdef wxHAVE_RAW_BITMAP |
960 | ||
481721e8 | 961 | void MyFrame::OnTestRawBitmap( wxCommandEvent &WXUNUSED(event) ) |
b3f04dc2 VZ |
962 | { |
963 | (new MyRawBitmapFrame(this))->Show(); | |
964 | } | |
965 | ||
966 | #endif // wxHAVE_RAW_BITMAP | |
967 | ||
dd38c875 | 968 | #if wxUSE_CLIPBOARD |
da9df1f5 VZ |
969 | |
970 | void MyFrame::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
971 | { | |
972 | wxBitmapDataObject *dobjBmp = new wxBitmapDataObject; | |
973 | dobjBmp->SetBitmap(*m_canvas->my_horse_png); | |
974 | ||
dd38c875 MB |
975 | wxTheClipboard->Open(); |
976 | ||
da9df1f5 VZ |
977 | if ( !wxTheClipboard->SetData(dobjBmp) ) |
978 | { | |
979 | wxLogError(_T("Failed to copy bitmap to clipboard")); | |
980 | } | |
dd38c875 MB |
981 | |
982 | wxTheClipboard->Close(); | |
da9df1f5 VZ |
983 | } |
984 | ||
985 | void MyFrame::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
986 | { | |
987 | wxBitmapDataObject dobjBmp; | |
dd38c875 MB |
988 | |
989 | wxTheClipboard->Open(); | |
da9df1f5 VZ |
990 | if ( !wxTheClipboard->GetData(dobjBmp) ) |
991 | { | |
992 | wxLogMessage(_T("No bitmap data in the clipboard")); | |
993 | } | |
994 | else | |
995 | { | |
996 | (new MyImageFrame(this, dobjBmp.GetBitmap()))->Show(); | |
997 | } | |
dd38c875 | 998 | wxTheClipboard->Close(); |
da9df1f5 VZ |
999 | } |
1000 | ||
1001 | #endif // wxUSE_CLIPBOARD | |
1002 | ||
01111366 RR |
1003 | //----------------------------------------------------------------------------- |
1004 | // MyApp | |
1005 | //----------------------------------------------------------------------------- | |
1006 | ||
1d5b7a0b | 1007 | bool MyApp::OnInit() |
01111366 | 1008 | { |
e9c4b1a2 JS |
1009 | #if wxUSE_LIBPNG |
1010 | wxImage::AddHandler( new wxPNGHandler ); | |
1011 | #endif | |
1012 | ||
1013 | #if wxUSE_LIBJPEG | |
1014 | wxImage::AddHandler( new wxJPEGHandler ); | |
1015 | #endif | |
1016 | ||
60a41aee RR |
1017 | #if wxUSE_LIBTIFF |
1018 | wxImage::AddHandler( new wxTIFFHandler ); | |
1019 | #endif | |
1020 | ||
6e47faf1 | 1021 | #if wxUSE_GIF |
e1929140 | 1022 | wxImage::AddHandler( new wxGIFHandler ); |
6e47faf1 JS |
1023 | #endif |
1024 | ||
1025 | #if wxUSE_PCX | |
cbd4be25 | 1026 | wxImage::AddHandler( new wxPCXHandler ); |
6e47faf1 JS |
1027 | #endif |
1028 | ||
1029 | #if wxUSE_PNM | |
6319afe3 | 1030 | wxImage::AddHandler( new wxPNMHandler ); |
6e47faf1 | 1031 | #endif |
e1929140 | 1032 | |
a8d4e3ac VS |
1033 | #if wxUSE_XPM |
1034 | wxImage::AddHandler( new wxXPMHandler ); | |
1035 | #endif | |
1036 | ||
bf504f98 VS |
1037 | #if wxUSE_ICO_CUR |
1038 | wxImage::AddHandler( new wxICOHandler ); | |
1039 | wxImage::AddHandler( new wxCURHandler ); | |
2b5f62a0 | 1040 | wxImage::AddHandler( new wxANIHandler ); |
bf504f98 VS |
1041 | #endif |
1042 | ||
01111366 | 1043 | wxFrame *frame = new MyFrame(); |
47f797bd | 1044 | frame->Show( true ); |
1d5b7a0b | 1045 | |
47f797bd | 1046 | return true; |
01111366 RR |
1047 | } |
1048 |