1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: samples/image/image.cpp
3 // Purpose: sample showing operations with wxImage
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi
8 // Copyright: (c) 1998-2005 Robert Roebling
9 // (c) 2005-2009 Vadim Zeitlin
10 // Licence: wxWindows licence
11 ///////////////////////////////////////////////////////////////////////////////
13 // For compilers that support precompilation, includes "wx/wx.h".
14 #include "wx/wxprec.h"
26 #include "wx/filename.h"
27 #include "wx/graphics.h"
28 #include "wx/mstream.h"
29 #include "wx/wfstream.h"
30 #include "wx/quantize.h"
31 #include "wx/scopedptr.h"
32 #include "wx/stopwatch.h"
33 #include "wx/versioninfo.h"
36 #include "wx/dataobj.h"
37 #include "wx/clipbrd.h"
38 #endif // wxUSE_CLIPBOARD
40 #if defined(__WXMSW__)
41 #ifdef wxHAVE_RAW_BITMAP
42 #include "wx/rawbmp.h"
46 #if defined(__WXMAC__) || defined(__WXGTK__)
47 #define wxHAVE_RAW_BITMAP
48 #include "wx/rawbmp.h"
53 #ifndef wxHAS_IMAGES_IN_RESOURCES
54 #include "../sample.xpm"
57 // ============================================================================
59 // ============================================================================
61 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
65 class MyApp
: public wxApp
68 virtual bool OnInit();
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 class MyFrame
: public wxFrame
80 void OnAbout( wxCommandEvent
&event
);
81 void OnNewFrame( wxCommandEvent
&event
);
82 void OnImageInfo( wxCommandEvent
&event
);
83 void OnThumbnail( wxCommandEvent
&event
);
85 #ifdef wxHAVE_RAW_BITMAP
86 void OnTestRawBitmap( wxCommandEvent
&event
);
87 #endif // wxHAVE_RAW_BITMAP
88 #if wxUSE_GRAPHICS_CONTEXT
89 void OnTestGraphics(wxCommandEvent
& event
);
90 #endif // wxUSE_GRAPHICS_CONTEXT
91 void OnQuit( wxCommandEvent
&event
);
94 void OnCopy(wxCommandEvent
& event
);
95 void OnPaste(wxCommandEvent
& event
);
96 #endif // wxUSE_CLIPBOARD
101 // ask user for the file name and try to load an image from it
103 // return the file path on success, empty string if we failed to load the
104 // image or were cancelled by user
105 static wxString
LoadUserImage(wxImage
& image
);
108 DECLARE_DYNAMIC_CLASS(MyFrame
)
109 DECLARE_EVENT_TABLE()
112 // ----------------------------------------------------------------------------
113 // Frame used for showing a standalone image
114 // ----------------------------------------------------------------------------
118 ID_ROTATE_LEFT
= wxID_HIGHEST
+1,
124 class MyImageFrame
: public wxFrame
127 MyImageFrame(wxFrame
*parent
, const wxString
& desc
, const wxImage
& image
)
129 Create(parent
, desc
, wxBitmap(image
), image
.GetImageCount(desc
));
132 MyImageFrame(wxFrame
*parent
, const wxString
& desc
, const wxBitmap
& bitmap
)
134 Create(parent
, desc
, bitmap
);
138 bool Create(wxFrame
*parent
,
139 const wxString
& desc
,
140 const wxBitmap
& bitmap
,
143 if ( !wxFrame::Create(parent
, wxID_ANY
,
144 wxString::Format(wxT("Image from %s"), desc
),
145 wxDefaultPosition
, wxDefaultSize
,
146 wxDEFAULT_FRAME_STYLE
| wxFULL_REPAINT_ON_RESIZE
) )
152 wxMenu
*menu
= new wxMenu
;
153 menu
->Append(wxID_SAVEAS
);
154 menu
->AppendSeparator();
155 menu
->AppendCheckItem(ID_PAINT_BG
, wxT("&Paint background"),
156 "Uncheck this for transparent images");
157 menu
->AppendSeparator();
158 menu
->Append(ID_RESIZE
, wxT("&Fit to window\tCtrl-F"));
159 menu
->Append(wxID_ZOOM_IN
, "Zoom &in\tCtrl-+");
160 menu
->Append(wxID_ZOOM_OUT
, "Zoom &out\tCtrl--");
161 menu
->Append(wxID_ZOOM_100
, "Reset zoom to &100%\tCtrl-1");
162 menu
->AppendSeparator();
163 menu
->Append(ID_ROTATE_LEFT
, wxT("Rotate &left\tCtrl-L"));
164 menu
->Append(ID_ROTATE_RIGHT
, wxT("Rotate &right\tCtrl-R"));
166 wxMenuBar
*mbar
= new wxMenuBar
;
167 mbar
->Append(menu
, wxT("&Image"));
170 mbar
->Check(ID_PAINT_BG
, true);
173 if ( numImages
!= 1 )
174 SetStatusText(wxString::Format("%d images", numImages
), 1);
176 SetClientSize(bitmap
.GetWidth(), bitmap
.GetHeight());
185 void OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
187 // do nothing here to be able to see how transparent images are shown
190 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
194 if ( GetMenuBar()->IsChecked(ID_PAINT_BG
) )
197 dc
.SetUserScale(m_zoom
, m_zoom
);
199 const wxSize size
= GetClientSize();
203 dc
.DeviceToLogicalX((size
.x
- m_zoom
*m_bitmap
.GetWidth())/2),
204 dc
.DeviceToLogicalY((size
.y
- m_zoom
*m_bitmap
.GetHeight())/2),
209 void OnSave(wxCommandEvent
& WXUNUSED(event
))
212 wxImage image
= m_bitmap
.ConvertToImage();
214 wxString savefilename
= wxFileSelector( wxT("Save Image"),
218 wxT("BMP files (*.bmp)|*.bmp|")
220 wxT("PNG files (*.png)|*.png|")
223 wxT("JPEG files (*.jpg)|*.jpg|")
226 wxT("GIF files (*.gif)|*.gif|")
229 wxT("TIFF files (*.tif)|*.tif|")
232 wxT("PCX files (*.pcx)|*.pcx|")
235 wxT("X PixMap files (*.xpm)|*.xpm|")
237 wxT("ICO files (*.ico)|*.ico|")
238 wxT("CUR files (*.cur)|*.cur"),
239 wxFD_SAVE
| wxFD_OVERWRITE_PROMPT
,
242 if ( savefilename
.empty() )
246 wxFileName::SplitPath(savefilename
, NULL
, NULL
, &extension
);
249 if ( extension
== wxT("bmp") )
251 static const int bppvalues
[] =
263 const wxString bppchoices
[] =
269 wxT("8 bpp greyscale"),
271 wxT("8 bpp own palette"),
275 int bppselection
= wxGetSingleChoiceIndex(wxT("Set BMP BPP"),
276 wxT("Image sample: save file"),
277 WXSIZEOF(bppchoices
),
280 if ( bppselection
!= -1 )
282 int format
= bppvalues
[bppselection
];
283 image
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, format
);
285 if ( format
== wxBMP_8BPP_PALETTE
)
287 unsigned char *cmap
= new unsigned char [256];
288 for ( int i
= 0; i
< 256; i
++ )
289 cmap
[i
] = (unsigned char)i
;
290 image
.SetPalette(wxPalette(256, cmap
, cmap
, cmap
));
297 else if ( extension
== wxT("png") )
299 static const int pngvalues
[] =
309 const wxString pngchoices
[] =
315 wxT("Grey red 8bpp"),
316 wxT("Grey red 16bpp"),
319 int sel
= wxGetSingleChoiceIndex(wxT("Set PNG format"),
320 wxT("Image sample: save file"),
321 WXSIZEOF(pngchoices
),
326 image
.SetOption(wxIMAGE_OPTION_PNG_FORMAT
, pngvalues
[sel
]);
327 image
.SetOption(wxIMAGE_OPTION_PNG_BITDEPTH
, sel
% 2 ? 16 : 8);
329 // these values are taken from OptiPNG with -o3 switch
330 const wxString compressionChoices
[] =
332 wxT("compression = 9, memory = 8, strategy = 0, filter = 0"),
333 wxT("compression = 9, memory = 9, strategy = 0, filter = 0"),
334 wxT("compression = 9, memory = 8, strategy = 1, filter = 0"),
335 wxT("compression = 9, memory = 9, strategy = 1, filter = 0"),
336 wxT("compression = 1, memory = 8, strategy = 2, filter = 0"),
337 wxT("compression = 1, memory = 9, strategy = 2, filter = 0"),
338 wxT("compression = 9, memory = 8, strategy = 0, filter = 5"),
339 wxT("compression = 9, memory = 9, strategy = 0, filter = 5"),
340 wxT("compression = 9, memory = 8, strategy = 1, filter = 5"),
341 wxT("compression = 9, memory = 9, strategy = 1, filter = 5"),
342 wxT("compression = 1, memory = 8, strategy = 2, filter = 5"),
343 wxT("compression = 1, memory = 9, strategy = 2, filter = 5"),
346 int sel
= wxGetSingleChoiceIndex(wxT("Select compression option (Cancel to use default)\n"),
347 wxT("PNG Compression Options"),
348 WXSIZEOF(compressionChoices
),
353 const int zc
[] = {9, 9, 9, 9, 1, 1, 9, 9, 9, 9, 1, 1};
354 const int zm
[] = {8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9};
355 const int zs
[] = {0, 0, 1, 1, 2, 2, 0, 0, 1, 1, 2, 2};
356 const int f
[] = {0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
357 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8};
359 image
.SetOption(wxIMAGE_OPTION_PNG_COMPRESSION_LEVEL
, zc
[sel
]);
360 image
.SetOption(wxIMAGE_OPTION_PNG_COMPRESSION_MEM_LEVEL
, zm
[sel
]);
361 image
.SetOption(wxIMAGE_OPTION_PNG_COMPRESSION_STRATEGY
, zs
[sel
]);
362 image
.SetOption(wxIMAGE_OPTION_PNG_FILTER
, f
[sel
]);
363 image
.SetOption(wxIMAGE_OPTION_PNG_COMPRESSION_BUFFER_SIZE
, 1048576); // 1 MB
367 #endif // wxUSE_LIBPNG
368 else if ( extension
== wxT("cur") )
370 image
.Rescale(32,32);
371 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
, 0);
372 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
, 0);
373 // This shows how you can save an image with explicitly
374 // specified image format:
375 saved
= image
.SaveFile(savefilename
, wxBITMAP_TYPE_CUR
);
380 // This one guesses image format from filename extension
381 // (it may fail if the extension is not recognized):
382 image
.SaveFile(savefilename
);
384 #endif // wxUSE_FILEDLG
387 void OnResize(wxCommandEvent
& WXUNUSED(event
))
389 wxImage
img(m_bitmap
.ConvertToImage());
391 const wxSize size
= GetClientSize();
392 img
.Rescale(size
.x
, size
.y
, wxIMAGE_QUALITY_HIGH
);
393 m_bitmap
= wxBitmap(img
);
398 void OnZoom(wxCommandEvent
& event
)
400 if ( event
.GetId() == wxID_ZOOM_IN
)
402 else if ( event
.GetId() == wxID_ZOOM_OUT
)
404 else // wxID_ZOOM_100
410 void OnRotate(wxCommandEvent
& event
)
413 if ( event
.GetId() == ID_ROTATE_LEFT
)
416 wxImage
img(m_bitmap
.ConvertToImage());
417 img
= img
.Rotate(angle
, wxPoint(img
.GetWidth() / 2, img
.GetHeight() / 2));
420 wxLogWarning(wxT("Rotation failed"));
424 m_bitmap
= wxBitmap(img
);
429 void UpdateStatusBar()
431 wxLogStatus(this, wxT("Image size: (%d, %d), zoom %.2f"),
433 m_bitmap
.GetHeight(),
441 DECLARE_EVENT_TABLE()
444 #ifdef wxHAVE_RAW_BITMAP
446 #include "wx/rawbmp.h"
448 class MyRawBitmapFrame
: public wxFrame
455 REAL_SIZE
= SIZE
- 2*BORDER
458 MyRawBitmapFrame(wxFrame
*parent
)
459 : wxFrame(parent
, wxID_ANY
, wxT("Raw bitmaps (how exciting)")),
460 m_bitmap(SIZE
, SIZE
, 24),
461 m_alphaBitmap(SIZE
, SIZE
, 32)
463 SetClientSize(SIZE
, SIZE
*2+25);
470 void InitAlphaBitmap()
472 // First, clear the whole bitmap by making it alpha
474 wxAlphaPixelData
data( m_alphaBitmap
, wxPoint(0,0), wxSize(SIZE
, SIZE
) );
477 wxLogError(wxT("Failed to gain raw access to bitmap data"));
480 wxAlphaPixelData::Iterator
p(data
);
481 for ( int y
= 0; y
< SIZE
; ++y
)
483 wxAlphaPixelData::Iterator rowStart
= p
;
484 for ( int x
= 0; x
< SIZE
; ++x
)
487 ++p
; // same as p.OffsetX(1)
494 // Then, draw colourful alpha-blended stripes
495 wxAlphaPixelData
data(m_alphaBitmap
, wxPoint(BORDER
, BORDER
),
496 wxSize(REAL_SIZE
, REAL_SIZE
));
499 wxLogError(wxT("Failed to gain raw access to bitmap data"));
503 wxAlphaPixelData::Iterator
p(data
);
505 for ( int y
= 0; y
< REAL_SIZE
; ++y
)
507 wxAlphaPixelData::Iterator rowStart
= p
;
509 int r
= y
< REAL_SIZE
/3 ? 255 : 0,
510 g
= (REAL_SIZE
/3 <= y
) && (y
< 2*(REAL_SIZE
/3)) ? 255 : 0,
511 b
= 2*(REAL_SIZE
/3) <= y
? 255 : 0;
513 for ( int x
= 0; x
< REAL_SIZE
; ++x
)
515 // note that RGB must be premultiplied by alpha
516 unsigned a
= (wxAlphaPixelData::Iterator::ChannelType
)((x
*255.)/REAL_SIZE
);
517 p
.Red() = r
* a
/ 256;
518 p
.Green() = g
* a
/ 256;
519 p
.Blue() = b
* a
/ 256;
522 ++p
; // same as p.OffsetX(1)
532 // draw some colourful stripes without alpha
533 wxNativePixelData
data(m_bitmap
);
536 wxLogError(wxT("Failed to gain raw access to bitmap data"));
540 wxNativePixelData::Iterator
p(data
);
541 for ( int y
= 0; y
< SIZE
; ++y
)
543 wxNativePixelData::Iterator rowStart
= p
;
545 int r
= y
< SIZE
/3 ? 255 : 0,
546 g
= (SIZE
/3 <= y
) && (y
< 2*(SIZE
/3)) ? 255 : 0,
547 b
= 2*(SIZE
/3) <= y
? 255 : 0;
549 for ( int x
= 0; x
< SIZE
; ++x
)
554 ++p
; // same as p.OffsetX(1)
562 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
564 wxPaintDC
dc( this );
565 dc
.DrawText(wxT("This is alpha and raw bitmap test"), 0, BORDER
);
566 dc
.DrawText(wxT("This is alpha and raw bitmap test"), 0, SIZE
/2 - BORDER
);
567 dc
.DrawText(wxT("This is alpha and raw bitmap test"), 0, SIZE
- 2*BORDER
);
568 dc
.DrawBitmap( m_alphaBitmap
, 0, 0, true /* use mask */ );
570 dc
.DrawText(wxT("Raw bitmap access without alpha"), 0, SIZE
+5);
571 dc
.DrawBitmap( m_bitmap
, 0, SIZE
+5+dc
.GetCharHeight());
576 wxBitmap m_alphaBitmap
;
578 DECLARE_EVENT_TABLE()
581 #endif // wxHAVE_RAW_BITMAP
584 // ============================================================================
586 // ============================================================================
588 //-----------------------------------------------------------------------------
590 //-----------------------------------------------------------------------------
592 BEGIN_EVENT_TABLE(MyImageFrame
, wxFrame
)
593 EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground
)
594 EVT_PAINT(MyImageFrame::OnPaint
)
596 EVT_MENU(wxID_SAVEAS
, MyImageFrame::OnSave
)
597 EVT_MENU_RANGE(ID_ROTATE_LEFT
, ID_ROTATE_RIGHT
, MyImageFrame::OnRotate
)
598 EVT_MENU(ID_RESIZE
, MyImageFrame::OnResize
)
600 EVT_MENU(wxID_ZOOM_IN
, MyImageFrame::OnZoom
)
601 EVT_MENU(wxID_ZOOM_OUT
, MyImageFrame::OnZoom
)
602 EVT_MENU(wxID_ZOOM_100
, MyImageFrame::OnZoom
)
605 //-----------------------------------------------------------------------------
607 //-----------------------------------------------------------------------------
609 #ifdef wxHAVE_RAW_BITMAP
611 BEGIN_EVENT_TABLE(MyRawBitmapFrame
, wxFrame
)
612 EVT_PAINT(MyRawBitmapFrame::OnPaint
)
615 #endif // wxHAVE_RAW_BITMAP
617 //-----------------------------------------------------------------------------
619 //-----------------------------------------------------------------------------
624 ID_ABOUT
= wxID_ABOUT
,
632 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
633 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
634 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
635 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
636 EVT_MENU (ID_NEW
, MyFrame::OnNewFrame
)
637 EVT_MENU (ID_INFO
, MyFrame::OnImageInfo
)
638 EVT_MENU (ID_SHOWTHUMBNAIL
, MyFrame::OnThumbnail
)
639 #ifdef wxHAVE_RAW_BITMAP
640 EVT_MENU (ID_SHOWRAW
, MyFrame::OnTestRawBitmap
)
642 #if wxUSE_GRAPHICS_CONTEXT
643 EVT_MENU (ID_GRAPHICS
, MyFrame::OnTestGraphics
)
644 #endif // wxUSE_GRAPHICS_CONTEXT
646 EVT_MENU(wxID_COPY
, MyFrame::OnCopy
)
647 EVT_MENU(wxID_PASTE
, MyFrame::OnPaste
)
648 #endif // wxUSE_CLIPBOARD
652 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, wxT("wxImage sample"),
653 wxPoint(20, 20), wxSize(950, 700) )
655 SetIcon(wxICON(sample
));
657 wxMenuBar
*menu_bar
= new wxMenuBar();
659 wxMenu
*menuImage
= new wxMenu
;
660 menuImage
->Append( ID_NEW
, wxT("&Show any image...\tCtrl-O"));
661 menuImage
->Append( ID_INFO
, wxT("Show image &information...\tCtrl-I"));
662 #ifdef wxHAVE_RAW_BITMAP
663 menuImage
->AppendSeparator();
664 menuImage
->Append( ID_SHOWRAW
, wxT("Test &raw bitmap...\tCtrl-R"));
666 #if wxUSE_GRAPHICS_CONTEXT
667 menuImage
->AppendSeparator();
668 menuImage
->Append(ID_GRAPHICS
, "Test &graphics context...\tCtrl-G");
669 #endif // wxUSE_GRAPHICS_CONTEXT
670 menuImage
->AppendSeparator();
671 menuImage
->Append( ID_SHOWTHUMBNAIL
, wxT("Test &thumbnail...\tCtrl-T"),
672 "Test scaling the image during load (try with JPEG)");
673 menuImage
->AppendSeparator();
674 menuImage
->Append( ID_ABOUT
, wxT("&About\tF1"));
675 menuImage
->AppendSeparator();
676 menuImage
->Append( ID_QUIT
, wxT("E&xit\tCtrl-Q"));
677 menu_bar
->Append(menuImage
, wxT("&Image"));
680 wxMenu
*menuClipboard
= new wxMenu
;
681 menuClipboard
->Append(wxID_COPY
, wxT("&Copy test image\tCtrl-C"));
682 menuClipboard
->Append(wxID_PASTE
, wxT("&Paste image\tCtrl-V"));
683 menu_bar
->Append(menuClipboard
, wxT("&Clipboard"));
684 #endif // wxUSE_CLIPBOARD
686 SetMenuBar( menu_bar
);
690 int widths
[] = { -1, 100 };
691 SetStatusWidths( 2, widths
);
692 #endif // wxUSE_STATUSBAR
694 m_canvas
= new MyCanvas( this, wxID_ANY
, wxPoint(0,0), wxSize(10,10) );
696 // 500 width * 2750 height
697 m_canvas
->SetScrollbars( 10, 10, 50, 275 );
698 m_canvas
->SetCursor(wxImage("cursor.png"));
701 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
706 #if wxUSE_ZLIB && wxUSE_STREAMS
707 #include "wx/zstream.h"
710 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
714 array
.Add("wxImage demo");
715 array
.Add("(c) Robert Roebling 1998-2005");
716 array
.Add("(c) Vadim Zeitlin 2005-2009");
718 array
.Add(wxEmptyString
);
719 array
.Add("Version of the libraries used:");
722 array
.Add(wxPNGHandler::GetLibraryVersionInfo().ToString());
725 array
.Add(wxJPEGHandler::GetLibraryVersionInfo().ToString());
728 array
.Add(wxTIFFHandler::GetLibraryVersionInfo().ToString());
730 #if wxUSE_ZLIB && wxUSE_STREAMS
731 // zlib is used by libpng
732 array
.Add(wxGetZlibVersionInfo().ToString());
734 (void)wxMessageBox( wxJoin(array
, '\n'),
735 "About wxImage Demo",
736 wxICON_INFORMATION
| wxOK
);
739 wxString
MyFrame::LoadUserImage(wxImage
& image
)
744 filename
= wxLoadFileSelector(wxT("image"), wxEmptyString
);
745 if ( !filename
.empty() )
747 if ( !image
.LoadFile(filename
) )
749 wxLogError(wxT("Couldn't load image from '%s'."), filename
.c_str());
751 return wxEmptyString
;
754 #endif // wxUSE_FILEDLG
759 void MyFrame::OnNewFrame( wxCommandEvent
&WXUNUSED(event
) )
762 wxString filename
= LoadUserImage(image
);
763 if ( !filename
.empty() )
764 new MyImageFrame(this, filename
, image
);
767 void MyFrame::OnImageInfo( wxCommandEvent
&WXUNUSED(event
) )
770 if ( !LoadUserImage(image
).empty() )
772 // TODO: show more information about the file
773 wxString info
= wxString::Format("Image size: %dx%d",
777 int xres
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX
),
778 yres
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY
);
781 info
+= wxString::Format("\nResolution: %dx%d", xres
, yres
);
782 switch ( image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT
) )
785 wxFAIL_MSG( "unknown image resolution units" );
788 case wxIMAGE_RESOLUTION_NONE
:
789 info
+= " in default units";
792 case wxIMAGE_RESOLUTION_INCHES
:
796 case wxIMAGE_RESOLUTION_CM
:
802 wxLogMessage("%s", info
);
806 #ifdef wxHAVE_RAW_BITMAP
808 void MyFrame::OnTestRawBitmap( wxCommandEvent
&WXUNUSED(event
) )
810 (new MyRawBitmapFrame(this))->Show();
813 #endif // wxHAVE_RAW_BITMAP
815 #if wxUSE_GRAPHICS_CONTEXT
817 class MyGraphicsFrame
: public wxFrame
826 MyGraphicsFrame(wxWindow
* parent
) :
827 wxFrame(parent
, wxID_ANY
, "Graphics context test"),
828 m_image(WIDTH
, HEIGHT
, false)
830 // Create a test image: it has 3 horizontal primary colour bands with
831 // alpha increasing from left to right.
833 unsigned char* alpha
= m_image
.GetAlpha();
834 unsigned char* data
= m_image
.GetData();
836 for ( int y
= 0; y
< HEIGHT
; y
++ )
843 else if ( y
< (2*HEIGHT
)/3 )
848 for ( int x
= 0; x
< WIDTH
; x
++ )
857 m_bitmap
= wxBitmap(m_image
);
859 Connect(wxEVT_PAINT
, wxPaintEventHandler(MyGraphicsFrame::OnPaint
));
865 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
868 wxScopedPtr
<wxGraphicsContext
> gc(wxGraphicsContext::Create(dc
));
869 wxGraphicsBitmap
gb(gc
->CreateBitmapFromImage(m_image
));
871 gc
->SetFont(*wxNORMAL_FONT
, *wxBLACK
);
872 gc
->DrawText("Bitmap", 0, HEIGHT
/2);
873 gc
->DrawBitmap(m_bitmap
, 0, 0, WIDTH
, HEIGHT
);
875 wxGraphicsFont gf
= gc
->CreateFont(wxNORMAL_FONT
->GetPixelSize().y
, "");
877 gc
->DrawText("Graphics bitmap", 0, (3*HEIGHT
)/2);
878 gc
->DrawBitmap(gb
, 0, HEIGHT
, WIDTH
, HEIGHT
);
884 wxDECLARE_NO_COPY_CLASS(MyGraphicsFrame
);
887 void MyFrame::OnTestGraphics(wxCommandEvent
& WXUNUSED(event
))
889 new MyGraphicsFrame(this);
892 #endif // wxUSE_GRAPHICS_CONTEXT
896 void MyFrame::OnCopy(wxCommandEvent
& WXUNUSED(event
))
898 wxBitmapDataObject
*dobjBmp
= new wxBitmapDataObject
;
899 dobjBmp
->SetBitmap(m_canvas
->my_horse_png
);
901 wxTheClipboard
->Open();
903 if ( !wxTheClipboard
->SetData(dobjBmp
) )
905 wxLogError(wxT("Failed to copy bitmap to clipboard"));
908 wxTheClipboard
->Close();
911 void MyFrame::OnPaste(wxCommandEvent
& WXUNUSED(event
))
913 wxBitmapDataObject dobjBmp
;
915 wxTheClipboard
->Open();
916 if ( !wxTheClipboard
->GetData(dobjBmp
) )
918 wxLogMessage(wxT("No bitmap data in the clipboard"));
922 new MyImageFrame(this, wxT("Clipboard"), dobjBmp
.GetBitmap());
924 wxTheClipboard
->Close();
927 #endif // wxUSE_CLIPBOARD
929 void MyFrame::OnThumbnail( wxCommandEvent
&WXUNUSED(event
) )
932 wxString filename
= wxLoadFileSelector(wxT("image"), wxEmptyString
, wxEmptyString
, this);
933 if ( filename
.empty() )
936 static const int THUMBNAIL_WIDTH
= 320;
937 static const int THUMBNAIL_HEIGHT
= 240;
940 image
.SetOption(wxIMAGE_OPTION_MAX_WIDTH
, THUMBNAIL_WIDTH
);
941 image
.SetOption(wxIMAGE_OPTION_MAX_HEIGHT
, THUMBNAIL_HEIGHT
);
944 if ( !image
.LoadFile(filename
) )
946 wxLogError(wxT("Couldn't load image from '%s'."), filename
.c_str());
950 int origWidth
= image
.GetOptionInt( wxIMAGE_OPTION_ORIGINAL_WIDTH
);
951 int origHeight
= image
.GetOptionInt( wxIMAGE_OPTION_ORIGINAL_HEIGHT
);
953 const long loadTime
= sw
.Time();
955 MyImageFrame
* const frame
= new MyImageFrame(this, filename
, image
);
956 wxLogStatus(frame
, "Loaded \"%s\" in %ldms; original size was (%d, %d)",
957 filename
, loadTime
, origWidth
, origHeight
);
959 wxLogError( wxT("Couldn't create file selector dialog") );
961 #endif // wxUSE_FILEDLG
964 //-----------------------------------------------------------------------------
966 //-----------------------------------------------------------------------------
972 if ( !wxApp::OnInit() )
975 wxInitAllImageHandlers();
977 wxFrame
*frame
= new MyFrame();