]> git.saurik.com Git - wxWidgets.git/blame - samples/image/canvas.cpp
Don't call wxTextMeasure::BeginMeasuring() when using non-native wxDC.
[wxWidgets.git] / samples / image / canvas.cpp
CommitLineData
09ddabf7
FM
1///////////////////////////////////////////////////////////////////////////////
2// Name: samples/image/canvas.cpp
3// Purpose: sample showing operations with wxImage
4// Author: Robert Roebling
5// Modified by: Francesco Montorsi
6// Created: 1998
7// RCS-ID: $Id$
8// Copyright: (c) 1998-2005 Robert Roebling
526954c5 9// Licence: wxWindows licence
09ddabf7
FM
10///////////////////////////////////////////////////////////////////////////////
11
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
20 #include "wx/wx.h"
21#endif
22
23#include "wx/image.h"
24#include "wx/file.h"
25#include "wx/filename.h"
26#include "wx/mstream.h"
27#include "wx/wfstream.h"
28#include "wx/quantize.h"
29#include "wx/stopwatch.h"
30
31#if wxUSE_CLIPBOARD
32 #include "wx/dataobj.h"
33 #include "wx/clipbrd.h"
34#endif // wxUSE_CLIPBOARD
35
36#include "smile.xbm"
37#include "smile.xpm"
c3f641cb 38#include "cursor_png.c"
09ddabf7
FM
39
40#include "canvas.h"
41
42
43//-----------------------------------------------------------------------------
44// MyCanvas
45//-----------------------------------------------------------------------------
46
47BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
48 EVT_PAINT(MyCanvas::OnPaint)
49END_EVENT_TABLE()
50
51MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id,
52 const wxPoint &pos, const wxSize &size )
53 : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER )
54 , m_bmpSmileXpm(smile_xpm)
55 , m_iconSmileXpm(smile_xpm)
56{
57 my_horse_ani = NULL;
58 m_ani_images = 0 ;
59
60 SetBackgroundColour(* wxWHITE);
61
62 wxBitmap bitmap( 100, 100 );
63
64 wxMemoryDC dc;
65 dc.SelectObject( bitmap );
de914436 66 dc.SetBrush( wxBrush( wxS("orange") ) );
09ddabf7
FM
67 dc.SetPen( *wxBLACK_PEN );
68 dc.DrawRectangle( 0, 0, 100, 100 );
69 dc.SetBrush( *wxWHITE_BRUSH );
70 dc.DrawRectangle( 20, 20, 60, 60 );
71 dc.SelectObject( wxNullBitmap );
72
73 // try to find the directory with our images
74 wxString dir;
75 if ( wxFile::Exists(wxT("./horse.png")) )
76 dir = wxT("./");
77 else if ( wxFile::Exists(wxT("../horse.png")) )
78 dir = wxT("../");
79 else
80 wxLogWarning(wxT("Can't find image files in either '.' or '..'!"));
81
82 wxImage image = bitmap.ConvertToImage();
83
84#if wxUSE_LIBPNG
9a83f860 85 if ( !image.SaveFile( dir + wxT("test.png"), wxBITMAP_TYPE_PNG ))
43b2d5e7 86 {
09ddabf7 87 wxLogError(wxT("Can't save file"));
43b2d5e7 88 }
09ddabf7
FM
89
90 image.Destroy();
91
9a83f860 92 if ( image.LoadFile( dir + wxT("test.png") ) )
09ddabf7
FM
93 my_square = wxBitmap( image );
94
95 image.Destroy();
96
9a83f860 97 if ( !image.LoadFile( dir + wxT("horse.png")) )
43b2d5e7 98 {
09ddabf7 99 wxLogError(wxT("Can't load PNG image"));
43b2d5e7 100 }
09ddabf7 101 else
43b2d5e7 102 {
09ddabf7 103 my_horse_png = wxBitmap( image );
43b2d5e7 104 }
09ddabf7 105
9a83f860 106 if ( !image.LoadFile( dir + wxT("toucan.png")) )
43b2d5e7 107 {
09ddabf7 108 wxLogError(wxT("Can't load PNG image"));
43b2d5e7 109 }
09ddabf7 110 else
43b2d5e7 111 {
09ddabf7 112 my_toucan = wxBitmap(image);
43b2d5e7 113 }
09ddabf7
FM
114
115 my_toucan_flipped_horiz = wxBitmap(image.Mirror(true));
116 my_toucan_flipped_vert = wxBitmap(image.Mirror(false));
117 my_toucan_flipped_both = wxBitmap(image.Mirror(true).Mirror(false));
118 my_toucan_grey = wxBitmap(image.ConvertToGreyscale());
119 my_toucan_head = wxBitmap(image.GetSubImage(wxRect(40, 7, 80, 60)));
120 my_toucan_scaled_normal = wxBitmap(image.Scale(110,90,wxIMAGE_QUALITY_NORMAL));
121 my_toucan_scaled_high = wxBitmap(image.Scale(110,90,wxIMAGE_QUALITY_HIGH));
122 my_toucan_blur = wxBitmap(image.Blur(10));
123
124#endif // wxUSE_LIBPNG
125
126#if wxUSE_LIBJPEG
127 image.Destroy();
128
9a83f860 129 if ( !image.LoadFile( dir + wxT("horse.jpg")) )
43b2d5e7 130 {
09ddabf7 131 wxLogError(wxT("Can't load JPG image"));
43b2d5e7 132 }
09ddabf7
FM
133 else
134 {
135 my_horse_jpeg = wxBitmap( image );
136
137 // Colorize by rotating green hue to red
138 wxImage::HSVValue greenHSV = wxImage::RGBtoHSV(wxImage::RGBValue(0, 255, 0));
139 wxImage::HSVValue redHSV = wxImage::RGBtoHSV(wxImage::RGBValue(255, 0, 0));
140 image.RotateHue(redHSV.hue - greenHSV.hue);
141 colorized_horse_jpeg = wxBitmap( image );
142 }
143
9a83f860 144 if ( !image.LoadFile( dir + wxT("cmyk.jpg")) )
43b2d5e7 145 {
9a83f860 146 wxLogError(wxT("Can't load CMYK JPG image"));
43b2d5e7 147 }
09ddabf7 148 else
43b2d5e7 149 {
09ddabf7 150 my_cmyk_jpeg = wxBitmap(image);
43b2d5e7 151 }
09ddabf7
FM
152#endif // wxUSE_LIBJPEG
153
154#if wxUSE_GIF
155 image.Destroy();
156
9a83f860 157 if ( !image.LoadFile( dir + wxT("horse.gif" )) )
43b2d5e7 158 {
09ddabf7 159 wxLogError(wxT("Can't load GIF image"));
43b2d5e7 160 }
09ddabf7 161 else
43b2d5e7 162 {
09ddabf7 163 my_horse_gif = wxBitmap( image );
43b2d5e7 164 }
09ddabf7
FM
165#endif
166
167#if wxUSE_PCX
168 image.Destroy();
169
9a83f860 170 if ( !image.LoadFile( dir + wxT("horse.pcx"), wxBITMAP_TYPE_PCX ) )
43b2d5e7 171 {
09ddabf7 172 wxLogError(wxT("Can't load PCX image"));
43b2d5e7 173 }
09ddabf7 174 else
43b2d5e7 175 {
09ddabf7 176 my_horse_pcx = wxBitmap( image );
43b2d5e7 177 }
09ddabf7
FM
178#endif
179
180 image.Destroy();
181
9a83f860 182 if ( !image.LoadFile( dir + wxT("horse.bmp"), wxBITMAP_TYPE_BMP ) )
43b2d5e7 183 {
09ddabf7 184 wxLogError(wxT("Can't load BMP image"));
43b2d5e7 185 }
09ddabf7 186 else
43b2d5e7 187 {
09ddabf7 188 my_horse_bmp = wxBitmap( image );
43b2d5e7 189 }
09ddabf7
FM
190
191#if wxUSE_XPM
192 image.Destroy();
193
9a83f860 194 if ( !image.LoadFile( dir + wxT("horse.xpm"), wxBITMAP_TYPE_XPM ) )
43b2d5e7 195 {
09ddabf7 196 wxLogError(wxT("Can't load XPM image"));
43b2d5e7 197 }
09ddabf7 198 else
43b2d5e7 199 {
09ddabf7 200 my_horse_xpm = wxBitmap( image );
43b2d5e7 201 }
09ddabf7 202
9a83f860 203 if ( !image.SaveFile( dir + wxT("test.xpm"), wxBITMAP_TYPE_XPM ))
43b2d5e7 204 {
09ddabf7 205 wxLogError(wxT("Can't save file"));
43b2d5e7 206 }
09ddabf7
FM
207#endif
208
209#if wxUSE_PNM
210 image.Destroy();
211
9a83f860 212 if ( !image.LoadFile( dir + wxT("horse.pnm"), wxBITMAP_TYPE_PNM ) )
43b2d5e7 213 {
09ddabf7 214 wxLogError(wxT("Can't load PNM image"));
43b2d5e7 215 }
09ddabf7 216 else
43b2d5e7 217 {
09ddabf7 218 my_horse_pnm = wxBitmap( image );
43b2d5e7 219 }
09ddabf7
FM
220
221 image.Destroy();
222
9a83f860 223 if ( !image.LoadFile( dir + wxT("horse_ag.pnm"), wxBITMAP_TYPE_PNM ) )
43b2d5e7 224 {
09ddabf7 225 wxLogError(wxT("Can't load PNM image"));
43b2d5e7 226 }
09ddabf7 227 else
43b2d5e7 228 {
09ddabf7 229 my_horse_asciigrey_pnm = wxBitmap( image );
43b2d5e7 230 }
09ddabf7
FM
231
232 image.Destroy();
233
9a83f860 234 if ( !image.LoadFile( dir + wxT("horse_rg.pnm"), wxBITMAP_TYPE_PNM ) )
43b2d5e7 235 {
09ddabf7 236 wxLogError(wxT("Can't load PNM image"));
43b2d5e7 237 }
09ddabf7 238 else
43b2d5e7 239 {
09ddabf7 240 my_horse_rawgrey_pnm = wxBitmap( image );
43b2d5e7 241 }
09ddabf7
FM
242#endif
243
244#if wxUSE_LIBTIFF
245 image.Destroy();
246
4ca8531f 247 if ( !image.LoadFile( dir + wxT("horse.tif"), wxBITMAP_TYPE_TIFF ) )
43b2d5e7 248 {
09ddabf7 249 wxLogError(wxT("Can't load TIFF image"));
43b2d5e7 250 }
09ddabf7 251 else
43b2d5e7 252 {
09ddabf7 253 my_horse_tiff = wxBitmap( image );
43b2d5e7 254 }
09ddabf7
FM
255#endif
256
257#if wxUSE_LIBTIFF
258 image.Destroy();
259
9a83f860 260 if ( !image.LoadFile( dir + wxT("horse.tga"), wxBITMAP_TYPE_TGA ) )
43b2d5e7 261 {
09ddabf7 262 wxLogError(wxT("Can't load TGA image"));
43b2d5e7 263 }
09ddabf7 264 else
43b2d5e7 265 {
09ddabf7 266 my_horse_tga = wxBitmap( image );
43b2d5e7 267 }
09ddabf7
FM
268#endif
269
270 CreateAntiAliasedBitmap();
271
272 my_smile_xbm = wxBitmap( (const char*)smile_bits, smile_width,
273 smile_height, 1 );
274
275 // demonstrates XPM automatically using the mask when saving
a1b806b9 276 if ( m_bmpSmileXpm.IsOk() )
9a83f860 277 m_bmpSmileXpm.SaveFile(wxT("saved.xpm"), wxBITMAP_TYPE_XPM);
09ddabf7
FM
278
279#if wxUSE_ICO_CUR
280 image.Destroy();
281
9a83f860 282 if ( !image.LoadFile( dir + wxT("horse.ico"), wxBITMAP_TYPE_ICO, 0 ) )
43b2d5e7 283 {
09ddabf7 284 wxLogError(wxT("Can't load first ICO image"));
43b2d5e7 285 }
09ddabf7 286 else
43b2d5e7 287 {
09ddabf7 288 my_horse_ico32 = wxBitmap( image );
43b2d5e7 289 }
09ddabf7
FM
290
291 image.Destroy();
292
9a83f860 293 if ( !image.LoadFile( dir + wxT("horse.ico"), wxBITMAP_TYPE_ICO, 1 ) )
43b2d5e7 294 {
09ddabf7 295 wxLogError(wxT("Can't load second ICO image"));
43b2d5e7 296 }
09ddabf7 297 else
43b2d5e7 298 {
09ddabf7 299 my_horse_ico16 = wxBitmap( image );
43b2d5e7 300 }
09ddabf7
FM
301
302 image.Destroy();
303
9a83f860 304 if ( !image.LoadFile( dir + wxT("horse.ico") ) )
43b2d5e7 305 {
09ddabf7 306 wxLogError(wxT("Can't load best ICO image"));
43b2d5e7 307 }
09ddabf7 308 else
43b2d5e7 309 {
09ddabf7 310 my_horse_ico = wxBitmap( image );
43b2d5e7 311 }
09ddabf7
FM
312
313 image.Destroy();
314
9a83f860 315 if ( !image.LoadFile( dir + wxT("horse.cur"), wxBITMAP_TYPE_CUR ) )
43b2d5e7 316 {
09ddabf7 317 wxLogError(wxT("Can't load best ICO image"));
43b2d5e7 318 }
09ddabf7
FM
319 else
320 {
321 my_horse_cur = wxBitmap( image );
322 xH = 30 + image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X) ;
323 yH = 2420 + image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y) ;
324 }
325
9a83f860 326 m_ani_images = wxImage::GetImageCount ( dir + wxT("horse3.ani"), wxBITMAP_TYPE_ANI );
09ddabf7 327 if (m_ani_images==0)
43b2d5e7 328 {
09ddabf7 329 wxLogError(wxT("No ANI-format images found"));
43b2d5e7 330 }
09ddabf7 331 else
43b2d5e7 332 {
09ddabf7 333 my_horse_ani = new wxBitmap [m_ani_images];
43b2d5e7 334 }
09ddabf7
FM
335
336 int i;
337 for (i=0; i < m_ani_images; i++)
338 {
339 image.Destroy();
9a83f860 340 if (!image.LoadFile( dir + wxT("horse3.ani"), wxBITMAP_TYPE_ANI, i ))
09ddabf7
FM
341 {
342 wxString tmp = wxT("Can't load image number ");
343 tmp << i ;
344 wxLogError(tmp);
345 }
346 else
347 my_horse_ani [i] = wxBitmap( image );
348 }
349#endif // wxUSE_ICO_CUR
350
351
352 image.Destroy();
353
354 // test image loading from stream
9a83f860 355 wxFile file(dir + wxT("horse.bmp"));
09ddabf7
FM
356 if ( file.IsOpened() )
357 {
358 wxFileOffset len = file.Length();
359 size_t dataSize = (size_t)len;
360 void *data = malloc(dataSize);
361 if ( file.Read(data, dataSize) != len )
43b2d5e7 362 {
9a83f860 363 wxLogError(wxT("Reading bitmap file failed"));
43b2d5e7 364 }
09ddabf7
FM
365 else
366 {
367 wxMemoryInputStream mis(data, dataSize);
368 if ( !image.LoadFile(mis) )
43b2d5e7 369 {
09ddabf7 370 wxLogError(wxT("Can't load BMP image from stream"));
43b2d5e7 371 }
09ddabf7 372 else
43b2d5e7 373 {
09ddabf7 374 my_horse_bmp2 = wxBitmap( image );
43b2d5e7 375 }
09ddabf7
FM
376 }
377
378 free(data);
379 }
c3f641cb
VZ
380
381 // This macro loads PNG from either resources on the platforms that support
382 // this (Windows and OS X) or from in-memory data (coming from cursor_png.c
383 // included above in our case).
384 my_png_from_res = wxBITMAP_PNG(cursor);
385
386 // This one always loads PNG from memory but exists for consistency with
387 // the above one and also because it frees you from the need to specify the
388 // length explicitly, without it you'd have to do it and also spell the
389 // array name in full, like this:
390 //
391 // my_png_from_mem = wxBitmap::NewFromPNGData(cursor_png, WXSIZEOF(cursor_png));
392 my_png_from_mem = wxBITMAP_PNG_FROM_DATA(cursor);
09ddabf7
FM
393}
394
395MyCanvas::~MyCanvas()
396{
397 delete [] my_horse_ani;
398}
399
400void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
401{
402 wxPaintDC dc( this );
403 PrepareDC( dc );
404
9a83f860 405 dc.DrawText( wxT("Loaded image"), 30, 10 );
a1b806b9 406 if (my_square.IsOk())
09ddabf7
FM
407 dc.DrawBitmap( my_square, 30, 30 );
408
9a83f860 409 dc.DrawText( wxT("Drawn directly"), 150, 10 );
de914436 410 dc.SetBrush( wxBrush( wxS("orange") ) );
09ddabf7
FM
411 dc.SetPen( *wxBLACK_PEN );
412 dc.DrawRectangle( 150, 30, 100, 100 );
413 dc.SetBrush( *wxWHITE_BRUSH );
414 dc.DrawRectangle( 170, 50, 60, 60 );
415
a1b806b9 416 if (my_anti.IsOk())
09ddabf7
FM
417 dc.DrawBitmap( my_anti, 280, 30 );
418
9a83f860 419 dc.DrawText( wxT("PNG handler"), 30, 135 );
a1b806b9 420 if (my_horse_png.IsOk())
09ddabf7
FM
421 {
422 dc.DrawBitmap( my_horse_png, 30, 150 );
423 wxRect rect(0,0,100,100);
424 wxBitmap sub( my_horse_png.GetSubBitmap(rect) );
9a83f860 425 dc.DrawText( wxT("GetSubBitmap()"), 280, 175 );
09ddabf7
FM
426 dc.DrawBitmap( sub, 280, 195 );
427 }
428
9a83f860 429 dc.DrawText( wxT("JPEG handler"), 30, 365 );
a1b806b9 430 if (my_horse_jpeg.IsOk())
09ddabf7
FM
431 dc.DrawBitmap( my_horse_jpeg, 30, 380 );
432
9a83f860 433 dc.DrawText( wxT("Green rotated to red"), 280, 365 );
a1b806b9 434 if (colorized_horse_jpeg.IsOk())
09ddabf7
FM
435 dc.DrawBitmap( colorized_horse_jpeg, 280, 380 );
436
9a83f860 437 dc.DrawText( wxT("CMYK JPEG image"), 530, 365 );
a1b806b9 438 if (my_cmyk_jpeg.IsOk())
09ddabf7
FM
439 dc.DrawBitmap( my_cmyk_jpeg, 530, 380 );
440
9a83f860 441 dc.DrawText( wxT("GIF handler"), 30, 595 );
a1b806b9 442 if (my_horse_gif.IsOk())
09ddabf7
FM
443 dc.DrawBitmap( my_horse_gif, 30, 610 );
444
9a83f860 445 dc.DrawText( wxT("PCX handler"), 30, 825 );
a1b806b9 446 if (my_horse_pcx.IsOk())
09ddabf7
FM
447 dc.DrawBitmap( my_horse_pcx, 30, 840 );
448
9a83f860 449 dc.DrawText( wxT("BMP handler"), 30, 1055 );
a1b806b9 450 if (my_horse_bmp.IsOk())
09ddabf7
FM
451 dc.DrawBitmap( my_horse_bmp, 30, 1070 );
452
9a83f860 453 dc.DrawText( wxT("BMP read from memory"), 280, 1055 );
a1b806b9 454 if (my_horse_bmp2.IsOk())
09ddabf7
FM
455 dc.DrawBitmap( my_horse_bmp2, 280, 1070 );
456
9a83f860 457 dc.DrawText( wxT("PNM handler"), 30, 1285 );
a1b806b9 458 if (my_horse_pnm.IsOk())
09ddabf7
FM
459 dc.DrawBitmap( my_horse_pnm, 30, 1300 );
460
9a83f860 461 dc.DrawText( wxT("PNM handler (ascii grey)"), 280, 1285 );
a1b806b9 462 if (my_horse_asciigrey_pnm.IsOk())
09ddabf7
FM
463 dc.DrawBitmap( my_horse_asciigrey_pnm, 280, 1300 );
464
9a83f860 465 dc.DrawText( wxT("PNM handler (raw grey)"), 530, 1285 );
a1b806b9 466 if (my_horse_rawgrey_pnm.IsOk())
09ddabf7
FM
467 dc.DrawBitmap( my_horse_rawgrey_pnm, 530, 1300 );
468
9a83f860 469 dc.DrawText( wxT("TIFF handler"), 30, 1515 );
a1b806b9 470 if (my_horse_tiff.IsOk())
09ddabf7
FM
471 dc.DrawBitmap( my_horse_tiff, 30, 1530 );
472
9a83f860 473 dc.DrawText( wxT("TGA handler"), 30, 1745 );
a1b806b9 474 if (my_horse_tga.IsOk())
09ddabf7
FM
475 dc.DrawBitmap( my_horse_tga, 30, 1760 );
476
9a83f860 477 dc.DrawText( wxT("XPM handler"), 30, 1975 );
a1b806b9 478 if (my_horse_xpm.IsOk())
09ddabf7
FM
479 dc.DrawBitmap( my_horse_xpm, 30, 2000 );
480
481 // toucans
482 {
483 int x = 750, y = 10, yy = 170;
484
485 dc.DrawText(wxT("Original toucan"), x+50, y);
486 dc.DrawBitmap(my_toucan, x, y+15, true);
487 y += yy;
488 dc.DrawText(wxT("Flipped horizontally"), x+50, y);
489 dc.DrawBitmap(my_toucan_flipped_horiz, x, y+15, true);
490 y += yy;
491 dc.DrawText(wxT("Flipped vertically"), x+50, y);
492 dc.DrawBitmap(my_toucan_flipped_vert, x, y+15, true);
493 y += yy;
494 dc.DrawText(wxT("Flipped both h&v"), x+50, y);
495 dc.DrawBitmap(my_toucan_flipped_both, x, y+15, true);
496
497 y += yy;
498 dc.DrawText(wxT("In greyscale"), x+50, y);
499 dc.DrawBitmap(my_toucan_grey, x, y+15, true);
500
501 y += yy;
502 dc.DrawText(wxT("Toucan's head"), x+50, y);
503 dc.DrawBitmap(my_toucan_head, x, y+15, true);
504
505 y += yy;
506 dc.DrawText(wxT("Scaled with normal quality"), x+50, y);
507 dc.DrawBitmap(my_toucan_scaled_normal, x, y+15, true);
508
509 y += yy;
510 dc.DrawText(wxT("Scaled with high quality"), x+50, y);
511 dc.DrawBitmap(my_toucan_scaled_high, x, y+15, true);
512
513 y += yy;
514 dc.DrawText(wxT("Blured"), x+50, y);
515 dc.DrawBitmap(my_toucan_blur, x, y+15, true);
516 }
517
a1b806b9 518 if (my_smile_xbm.IsOk())
09ddabf7
FM
519 {
520 int x = 300, y = 1800;
521
9a83f860
VZ
522 dc.DrawText( wxT("XBM bitmap"), x, y );
523 dc.DrawText( wxT("(green on red)"), x, y + 15 );
524 dc.SetTextForeground( wxT("GREEN") );
525 dc.SetTextBackground( wxT("RED") );
09ddabf7
FM
526 dc.DrawBitmap( my_smile_xbm, x, y + 30 );
527
528 dc.SetTextForeground( *wxBLACK );
9a83f860
VZ
529 dc.DrawText( wxT("After wxImage conversion"), x + 120, y );
530 dc.DrawText( wxT("(red on white)"), x + 120, y + 15 );
09ddabf7
FM
531 dc.SetTextForeground( wxT("RED") );
532 wxImage i = my_smile_xbm.ConvertToImage();
533 i.SetMaskColour( 255, 255, 255 );
534 i.Replace( 0, 0, 0,
535 wxRED_PEN->GetColour().Red(),
536 wxRED_PEN->GetColour().Green(),
537 wxRED_PEN->GetColour().Blue() );
538 dc.DrawBitmap( wxBitmap(i), x + 120, y + 30, true );
539 dc.SetTextForeground( *wxBLACK );
540 }
541
542
543 wxBitmap mono( 60,50,1 );
544 wxMemoryDC memdc;
545 memdc.SelectObject( mono );
546 memdc.SetPen( *wxBLACK_PEN );
547 memdc.SetBrush( *wxWHITE_BRUSH );
548 memdc.DrawRectangle( 0,0,60,50 );
549 memdc.SetTextForeground( *wxBLACK );
550#ifndef __WXGTK20__
551 // I cannot convince GTK2 to draw into mono bitmaps
9a83f860 552 memdc.DrawText( wxT("Hi!"), 5, 5 );
09ddabf7
FM
553#endif
554 memdc.SetBrush( *wxBLACK_BRUSH );
555 memdc.DrawRectangle( 33,5,20,20 );
556 memdc.SetPen( *wxRED_PEN );
557 memdc.DrawLine( 5, 42, 50, 42 );
558 memdc.SelectObject( wxNullBitmap );
559
a1b806b9 560 if (mono.IsOk())
09ddabf7
FM
561 {
562 int x = 300, y = 1900;
563
9a83f860
VZ
564 dc.DrawText( wxT("Mono bitmap"), x, y );
565 dc.DrawText( wxT("(red on green)"), x, y + 15 );
09ddabf7
FM
566 dc.SetTextForeground( wxT("RED") );
567 dc.SetTextBackground( wxT("GREEN") );
568 dc.DrawBitmap( mono, x, y + 30 );
569
570 dc.SetTextForeground( *wxBLACK );
9a83f860
VZ
571 dc.DrawText( wxT("After wxImage conversion"), x + 120, y );
572 dc.DrawText( wxT("(red on white)"), x + 120, y + 15 );
09ddabf7
FM
573 dc.SetTextForeground( wxT("RED") );
574 wxImage i = mono.ConvertToImage();
575 i.SetMaskColour( 255,255,255 );
576 i.Replace( 0,0,0,
577 wxRED_PEN->GetColour().Red(),
578 wxRED_PEN->GetColour().Green(),
579 wxRED_PEN->GetColour().Blue() );
580 dc.DrawBitmap( wxBitmap(i), x + 120, y + 30, true );
581 dc.SetTextForeground( *wxBLACK );
582 }
583
584 // For testing transparency
585 dc.SetBrush( *wxRED_BRUSH );
586 dc.DrawRectangle( 20, 2220, 560, 68 );
587
9a83f860 588 dc.DrawText(wxT("XPM bitmap"), 30, 2230 );
a1b806b9 589 if ( m_bmpSmileXpm.IsOk() )
09ddabf7
FM
590 dc.DrawBitmap(m_bmpSmileXpm, 30, 2250, true);
591
9a83f860 592 dc.DrawText(wxT("XPM icon"), 110, 2230 );
a1b806b9 593 if ( m_iconSmileXpm.IsOk() )
09ddabf7
FM
594 dc.DrawIcon(m_iconSmileXpm, 110, 2250);
595
596 // testing icon -> bitmap conversion
597 wxBitmap to_blit( m_iconSmileXpm );
a1b806b9 598 if (to_blit.IsOk())
09ddabf7 599 {
9a83f860 600 dc.DrawText( wxT("SubBitmap"), 170, 2230 );
09ddabf7 601 wxBitmap sub = to_blit.GetSubBitmap( wxRect(0,0,15,15) );
a1b806b9 602 if (sub.IsOk())
09ddabf7
FM
603 dc.DrawBitmap( sub, 170, 2250, true );
604
9a83f860 605 dc.DrawText( wxT("Enlarged"), 250, 2230 );
09ddabf7
FM
606 dc.SetUserScale( 1.5, 1.5 );
607 dc.DrawBitmap( to_blit, (int)(250/1.5), (int)(2250/1.5), true );
608 dc.SetUserScale( 2, 2 );
609 dc.DrawBitmap( to_blit, (int)(300/2), (int)(2250/2), true );
610 dc.SetUserScale( 1.0, 1.0 );
611
9a83f860 612 dc.DrawText( wxT("Blit"), 400, 2230);
09ddabf7
FM
613 wxMemoryDC blit_dc;
614 blit_dc.SelectObject( to_blit );
615 dc.Blit( 400, 2250, to_blit.GetWidth(), to_blit.GetHeight(), &blit_dc, 0, 0, wxCOPY, true );
616 dc.SetUserScale( 1.5, 1.5 );
617 dc.Blit( (int)(450/1.5), (int)(2250/1.5), to_blit.GetWidth(), to_blit.GetHeight(), &blit_dc, 0, 0, wxCOPY, true );
618 dc.SetUserScale( 2, 2 );
619 dc.Blit( (int)(500/2), (int)(2250/2), to_blit.GetWidth(), to_blit.GetHeight(), &blit_dc, 0, 0, wxCOPY, true );
620 dc.SetUserScale( 1.0, 1.0 );
621 }
622
9a83f860 623 dc.DrawText( wxT("ICO handler (1st image)"), 30, 2290 );
a1b806b9 624 if (my_horse_ico32.IsOk())
09ddabf7
FM
625 dc.DrawBitmap( my_horse_ico32, 30, 2330, true );
626
9a83f860 627 dc.DrawText( wxT("ICO handler (2nd image)"), 230, 2290 );
a1b806b9 628 if (my_horse_ico16.IsOk())
09ddabf7
FM
629 dc.DrawBitmap( my_horse_ico16, 230, 2330, true );
630
9a83f860 631 dc.DrawText( wxT("ICO handler (best image)"), 430, 2290 );
a1b806b9 632 if (my_horse_ico.IsOk())
09ddabf7
FM
633 dc.DrawBitmap( my_horse_ico, 430, 2330, true );
634
9a83f860 635 dc.DrawText( wxT("CUR handler"), 30, 2390 );
a1b806b9 636 if (my_horse_cur.IsOk())
09ddabf7
FM
637 {
638 dc.DrawBitmap( my_horse_cur, 30, 2420, true );
639 dc.SetPen (*wxRED_PEN);
640 dc.DrawLine (xH-10,yH,xH+10,yH);
641 dc.DrawLine (xH,yH-10,xH,yH+10);
642 }
643
9a83f860 644 dc.DrawText( wxT("ANI handler"), 230, 2390 );
09ddabf7
FM
645 for ( int i=0; i < m_ani_images; i++ )
646 {
a1b806b9 647 if (my_horse_ani[i].IsOk())
09ddabf7
FM
648 {
649 dc.DrawBitmap( my_horse_ani[i], 230 + i * 2 * my_horse_ani[i].GetWidth() , 2420, true );
650 }
651 }
c3f641cb
VZ
652
653 dc.DrawText("PNG from resources", 30, 2460);
654 if ( my_png_from_res.IsOk() )
655 dc.DrawBitmap(my_png_from_res, 30, 2480, true);
656 dc.DrawText("PNG from memory", 230, 2460);
657 if ( my_png_from_mem.IsOk() )
658 dc.DrawBitmap(my_png_from_mem, 230, 2480, true);
09ddabf7
FM
659}
660
661void MyCanvas::CreateAntiAliasedBitmap()
662{
663 wxBitmap bitmap( 300, 300 );
664
5ed0415e
VZ
665 {
666 wxMemoryDC dc(bitmap);
09ddabf7 667
5ed0415e 668 dc.Clear();
09ddabf7 669
5ed0415e
VZ
670 dc.SetFont( wxFont( 24, wxDECORATIVE, wxNORMAL, wxNORMAL) );
671 dc.SetTextForeground( wxT("RED") );
672 dc.DrawText( wxT("This is anti-aliased Text."), 20, 5 );
673 dc.DrawText( wxT("And a Rectangle."), 20, 45 );
09ddabf7 674
5ed0415e
VZ
675 dc.SetBrush( *wxRED_BRUSH );
676 dc.SetPen( *wxTRANSPARENT_PEN );
677 dc.DrawRoundedRectangle( 20, 85, 200, 180, 20 );
678 }
09ddabf7 679
5ed0415e 680 wxImage original = bitmap.ConvertToImage();
09ddabf7
FM
681 wxImage anti( 150, 150 );
682
683 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
684
685 for (int y = 1; y < 149; y++)
686 for (int x = 1; x < 149; x++)
687 {
688 int red = original.GetRed( x*2, y*2 ) +
689 original.GetRed( x*2-1, y*2 ) +
690 original.GetRed( x*2, y*2+1 ) +
691 original.GetRed( x*2+1, y*2+1 );
692 red = red/4;
693
694 int green = original.GetGreen( x*2, y*2 ) +
695 original.GetGreen( x*2-1, y*2 ) +
696 original.GetGreen( x*2, y*2+1 ) +
697 original.GetGreen( x*2+1, y*2+1 );
698 green = green/4;
699
700 int blue = original.GetBlue( x*2, y*2 ) +
701 original.GetBlue( x*2-1, y*2 ) +
702 original.GetBlue( x*2, y*2+1 ) +
703 original.GetBlue( x*2+1, y*2+1 );
704 blue = blue/4;
705 anti.SetRGB( x, y, (unsigned char)red, (unsigned char)green, (unsigned char)blue );
706 }
707
708 my_anti = wxBitmap(anti);
709}