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