1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: samples/image/image.cpp
3 // Purpose: sample showing operations with wxImage
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi
7 // Copyright: (c) 1998-2005 Robert Roebling
8 // (c) 2005-2009 Vadim Zeitlin
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
25 #include "wx/filename.h"
26 #include "wx/graphics.h"
27 #include "wx/mstream.h"
28 #include "wx/wfstream.h"
29 #include "wx/quantize.h"
30 #include "wx/scopedptr.h"
31 #include "wx/stopwatch.h"
32 #include "wx/versioninfo.h"
35 #include "wx/dataobj.h"
36 #include "wx/clipbrd.h"
37 #endif // wxUSE_CLIPBOARD
39 #if defined(__WXMSW__)
40 #ifdef wxHAVE_RAW_BITMAP
41 #include "wx/rawbmp.h"
45 #if defined(__WXMAC__) || defined(__WXGTK__)
46 #define wxHAVE_RAW_BITMAP
47 #include "wx/rawbmp.h"
52 #ifndef wxHAS_IMAGES_IN_RESOURCES
53 #include "../sample.xpm"
56 // ============================================================================
58 // ============================================================================
60 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
64 class MyApp
: public wxApp
67 virtual bool OnInit();
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 class MyFrame
: public wxFrame
79 void OnAbout( wxCommandEvent
&event
);
80 void OnNewFrame( wxCommandEvent
&event
);
81 void OnImageInfo( wxCommandEvent
&event
);
82 void OnThumbnail( wxCommandEvent
&event
);
84 #ifdef wxHAVE_RAW_BITMAP
85 void OnTestRawBitmap( wxCommandEvent
&event
);
86 #endif // wxHAVE_RAW_BITMAP
87 #if wxUSE_GRAPHICS_CONTEXT
88 void OnTestGraphics(wxCommandEvent
& event
);
89 #endif // wxUSE_GRAPHICS_CONTEXT
90 void OnQuit( wxCommandEvent
&event
);
93 void OnCopy(wxCommandEvent
& event
);
94 void OnPaste(wxCommandEvent
& event
);
95 #endif // wxUSE_CLIPBOARD
100 // ask user for the file name and try to load an image from it
102 // return the file path on success, empty string if we failed to load the
103 // image or were cancelled by user
104 static wxString
LoadUserImage(wxImage
& image
);
107 DECLARE_DYNAMIC_CLASS(MyFrame
)
108 DECLARE_EVENT_TABLE()
111 // ----------------------------------------------------------------------------
112 // Frame used for showing a standalone image
113 // ----------------------------------------------------------------------------
117 ID_ROTATE_LEFT
= wxID_HIGHEST
+1,
123 class MyImageFrame
: public wxFrame
126 MyImageFrame(wxFrame
*parent
, const wxString
& desc
, const wxImage
& image
)
128 Create(parent
, desc
, wxBitmap(image
), image
.GetImageCount(desc
));
131 MyImageFrame(wxFrame
*parent
, const wxString
& desc
, const wxBitmap
& bitmap
)
133 Create(parent
, desc
, bitmap
);
137 bool Create(wxFrame
*parent
,
138 const wxString
& desc
,
139 const wxBitmap
& bitmap
,
142 if ( !wxFrame::Create(parent
, wxID_ANY
,
143 wxString::Format(wxT("Image from %s"), desc
),
144 wxDefaultPosition
, wxDefaultSize
,
145 wxDEFAULT_FRAME_STYLE
| wxFULL_REPAINT_ON_RESIZE
) )
151 wxMenu
*menu
= new wxMenu
;
152 menu
->Append(wxID_SAVEAS
);
153 menu
->AppendSeparator();
154 menu
->AppendCheckItem(ID_PAINT_BG
, wxT("&Paint background"),
155 "Uncheck this for transparent images");
156 menu
->AppendSeparator();
157 menu
->Append(ID_RESIZE
, wxT("&Fit to window\tCtrl-F"));
158 menu
->Append(wxID_ZOOM_IN
, "Zoom &in\tCtrl-+");
159 menu
->Append(wxID_ZOOM_OUT
, "Zoom &out\tCtrl--");
160 menu
->Append(wxID_ZOOM_100
, "Reset zoom to &100%\tCtrl-1");
161 menu
->AppendSeparator();
162 menu
->Append(ID_ROTATE_LEFT
, wxT("Rotate &left\tCtrl-L"));
163 menu
->Append(ID_ROTATE_RIGHT
, wxT("Rotate &right\tCtrl-R"));
165 wxMenuBar
*mbar
= new wxMenuBar
;
166 mbar
->Append(menu
, wxT("&Image"));
169 mbar
->Check(ID_PAINT_BG
, true);
172 if ( numImages
!= 1 )
173 SetStatusText(wxString::Format("%d images", numImages
), 1);
175 SetClientSize(bitmap
.GetWidth(), bitmap
.GetHeight());
184 void OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
186 // do nothing here to be able to see how transparent images are shown
189 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
193 if ( GetMenuBar()->IsChecked(ID_PAINT_BG
) )
196 dc
.SetUserScale(m_zoom
, m_zoom
);
198 const wxSize size
= GetClientSize();
202 dc
.DeviceToLogicalX((size
.x
- m_zoom
*m_bitmap
.GetWidth())/2),
203 dc
.DeviceToLogicalY((size
.y
- m_zoom
*m_bitmap
.GetHeight())/2),
208 void OnSave(wxCommandEvent
& WXUNUSED(event
))
211 wxImage image
= m_bitmap
.ConvertToImage();
213 wxString savefilename
= wxFileSelector( wxT("Save Image"),
217 wxT("BMP files (*.bmp)|*.bmp|")
219 wxT("PNG files (*.png)|*.png|")
222 wxT("JPEG files (*.jpg)|*.jpg|")
225 wxT("GIF files (*.gif)|*.gif|")
228 wxT("TIFF files (*.tif)|*.tif|")
231 wxT("PCX files (*.pcx)|*.pcx|")
234 wxT("X PixMap files (*.xpm)|*.xpm|")
236 wxT("ICO files (*.ico)|*.ico|")
237 wxT("CUR files (*.cur)|*.cur"),
238 wxFD_SAVE
| wxFD_OVERWRITE_PROMPT
,
241 if ( savefilename
.empty() )
245 wxFileName::SplitPath(savefilename
, NULL
, NULL
, &extension
);
248 if ( extension
== wxT("bmp") )
250 static const int bppvalues
[] =
262 const wxString bppchoices
[] =
268 wxT("8 bpp greyscale"),
270 wxT("8 bpp own palette"),
274 int bppselection
= wxGetSingleChoiceIndex(wxT("Set BMP BPP"),
275 wxT("Image sample: save file"),
276 WXSIZEOF(bppchoices
),
279 if ( bppselection
!= -1 )
281 int format
= bppvalues
[bppselection
];
282 image
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, format
);
284 if ( format
== wxBMP_8BPP_PALETTE
)
286 unsigned char *cmap
= new unsigned char [256];
287 for ( int i
= 0; i
< 256; i
++ )
288 cmap
[i
] = (unsigned char)i
;
289 image
.SetPalette(wxPalette(256, cmap
, cmap
, cmap
));
296 else if ( extension
== wxT("png") )
298 static const int pngvalues
[] =
308 const wxString pngchoices
[] =
314 wxT("Grey red 8bpp"),
315 wxT("Grey red 16bpp"),
318 int sel
= wxGetSingleChoiceIndex(wxT("Set PNG format"),
319 wxT("Image sample: save file"),
320 WXSIZEOF(pngchoices
),
325 image
.SetOption(wxIMAGE_OPTION_PNG_FORMAT
, pngvalues
[sel
]);
326 image
.SetOption(wxIMAGE_OPTION_PNG_BITDEPTH
, sel
% 2 ? 16 : 8);
328 // these values are taken from OptiPNG with -o3 switch
329 const wxString compressionChoices
[] =
331 wxT("compression = 9, memory = 8, strategy = 0, filter = 0"),
332 wxT("compression = 9, memory = 9, strategy = 0, filter = 0"),
333 wxT("compression = 9, memory = 8, strategy = 1, filter = 0"),
334 wxT("compression = 9, memory = 9, strategy = 1, filter = 0"),
335 wxT("compression = 1, memory = 8, strategy = 2, filter = 0"),
336 wxT("compression = 1, memory = 9, strategy = 2, filter = 0"),
337 wxT("compression = 9, memory = 8, strategy = 0, filter = 5"),
338 wxT("compression = 9, memory = 9, strategy = 0, filter = 5"),
339 wxT("compression = 9, memory = 8, strategy = 1, filter = 5"),
340 wxT("compression = 9, memory = 9, strategy = 1, filter = 5"),
341 wxT("compression = 1, memory = 8, strategy = 2, filter = 5"),
342 wxT("compression = 1, memory = 9, strategy = 2, filter = 5"),
345 int sel
= wxGetSingleChoiceIndex(wxT("Select compression option (Cancel to use default)\n"),
346 wxT("PNG Compression Options"),
347 WXSIZEOF(compressionChoices
),
352 const int zc
[] = {9, 9, 9, 9, 1, 1, 9, 9, 9, 9, 1, 1};
353 const int zm
[] = {8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9};
354 const int zs
[] = {0, 0, 1, 1, 2, 2, 0, 0, 1, 1, 2, 2};
355 const int f
[] = {0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
356 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8};
358 image
.SetOption(wxIMAGE_OPTION_PNG_COMPRESSION_LEVEL
, zc
[sel
]);
359 image
.SetOption(wxIMAGE_OPTION_PNG_COMPRESSION_MEM_LEVEL
, zm
[sel
]);
360 image
.SetOption(wxIMAGE_OPTION_PNG_COMPRESSION_STRATEGY
, zs
[sel
]);
361 image
.SetOption(wxIMAGE_OPTION_PNG_FILTER
, f
[sel
]);
362 image
.SetOption(wxIMAGE_OPTION_PNG_COMPRESSION_BUFFER_SIZE
, 1048576); // 1 MB
366 #endif // wxUSE_LIBPNG
367 else if ( extension
== wxT("cur") )
369 image
.Rescale(32,32);
370 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
, 0);
371 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
, 0);
372 // This shows how you can save an image with explicitly
373 // specified image format:
374 saved
= image
.SaveFile(savefilename
, wxBITMAP_TYPE_CUR
);
379 // This one guesses image format from filename extension
380 // (it may fail if the extension is not recognized):
381 image
.SaveFile(savefilename
);
383 #endif // wxUSE_FILEDLG
386 void OnResize(wxCommandEvent
& WXUNUSED(event
))
388 wxImage
img(m_bitmap
.ConvertToImage());
390 const wxSize size
= GetClientSize();
391 img
.Rescale(size
.x
, size
.y
, wxIMAGE_QUALITY_HIGH
);
392 m_bitmap
= wxBitmap(img
);
397 void OnZoom(wxCommandEvent
& event
)
399 if ( event
.GetId() == wxID_ZOOM_IN
)
401 else if ( event
.GetId() == wxID_ZOOM_OUT
)
403 else // wxID_ZOOM_100
409 void OnRotate(wxCommandEvent
& event
)
412 if ( event
.GetId() == ID_ROTATE_LEFT
)
415 wxImage
img(m_bitmap
.ConvertToImage());
416 img
= img
.Rotate(angle
, wxPoint(img
.GetWidth() / 2, img
.GetHeight() / 2));
419 wxLogWarning(wxT("Rotation failed"));
423 m_bitmap
= wxBitmap(img
);
428 void UpdateStatusBar()
430 wxLogStatus(this, wxT("Image size: (%d, %d), zoom %.2f"),
432 m_bitmap
.GetHeight(),
440 DECLARE_EVENT_TABLE()
443 #ifdef wxHAVE_RAW_BITMAP
445 #include "wx/rawbmp.h"
447 class MyRawBitmapFrame
: public wxFrame
454 REAL_SIZE
= SIZE
- 2*BORDER
457 MyRawBitmapFrame(wxFrame
*parent
)
458 : wxFrame(parent
, wxID_ANY
, wxT("Raw bitmaps (how exciting)")),
459 m_bitmap(SIZE
, SIZE
, 24),
460 m_alphaBitmap(SIZE
, SIZE
, 32)
462 SetClientSize(SIZE
, SIZE
*2+25);
469 void InitAlphaBitmap()
471 // First, clear the whole bitmap by making it alpha
473 wxAlphaPixelData
data( m_alphaBitmap
, wxPoint(0,0), wxSize(SIZE
, SIZE
) );
476 wxLogError(wxT("Failed to gain raw access to bitmap data"));
479 wxAlphaPixelData::Iterator
p(data
);
480 for ( int y
= 0; y
< SIZE
; ++y
)
482 wxAlphaPixelData::Iterator rowStart
= p
;
483 for ( int x
= 0; x
< SIZE
; ++x
)
486 ++p
; // same as p.OffsetX(1)
493 // Then, draw colourful alpha-blended stripes
494 wxAlphaPixelData
data(m_alphaBitmap
, wxPoint(BORDER
, BORDER
),
495 wxSize(REAL_SIZE
, REAL_SIZE
));
498 wxLogError(wxT("Failed to gain raw access to bitmap data"));
502 wxAlphaPixelData::Iterator
p(data
);
504 for ( int y
= 0; y
< REAL_SIZE
; ++y
)
506 wxAlphaPixelData::Iterator rowStart
= p
;
508 int r
= y
< REAL_SIZE
/3 ? 255 : 0,
509 g
= (REAL_SIZE
/3 <= y
) && (y
< 2*(REAL_SIZE
/3)) ? 255 : 0,
510 b
= 2*(REAL_SIZE
/3) <= y
? 255 : 0;
512 for ( int x
= 0; x
< REAL_SIZE
; ++x
)
514 // note that RGB must be premultiplied by alpha
515 unsigned a
= (wxAlphaPixelData::Iterator::ChannelType
)((x
*255.)/REAL_SIZE
);
516 p
.Red() = r
* a
/ 256;
517 p
.Green() = g
* a
/ 256;
518 p
.Blue() = b
* a
/ 256;
521 ++p
; // same as p.OffsetX(1)
531 // draw some colourful stripes without alpha
532 wxNativePixelData
data(m_bitmap
);
535 wxLogError(wxT("Failed to gain raw access to bitmap data"));
539 wxNativePixelData::Iterator
p(data
);
540 for ( int y
= 0; y
< SIZE
; ++y
)
542 wxNativePixelData::Iterator rowStart
= p
;
544 int r
= y
< SIZE
/3 ? 255 : 0,
545 g
= (SIZE
/3 <= y
) && (y
< 2*(SIZE
/3)) ? 255 : 0,
546 b
= 2*(SIZE
/3) <= y
? 255 : 0;
548 for ( int x
= 0; x
< SIZE
; ++x
)
553 ++p
; // same as p.OffsetX(1)
561 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
563 wxPaintDC
dc( this );
564 dc
.DrawText(wxT("This is alpha and raw bitmap test"), 0, BORDER
);
565 dc
.DrawText(wxT("This is alpha and raw bitmap test"), 0, SIZE
/2 - BORDER
);
566 dc
.DrawText(wxT("This is alpha and raw bitmap test"), 0, SIZE
- 2*BORDER
);
567 dc
.DrawBitmap( m_alphaBitmap
, 0, 0, true /* use mask */ );
569 dc
.DrawText(wxT("Raw bitmap access without alpha"), 0, SIZE
+5);
570 dc
.DrawBitmap( m_bitmap
, 0, SIZE
+5+dc
.GetCharHeight());
575 wxBitmap m_alphaBitmap
;
577 DECLARE_EVENT_TABLE()
580 #endif // wxHAVE_RAW_BITMAP
583 // ============================================================================
585 // ============================================================================
587 //-----------------------------------------------------------------------------
589 //-----------------------------------------------------------------------------
591 BEGIN_EVENT_TABLE(MyImageFrame
, wxFrame
)
592 EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground
)
593 EVT_PAINT(MyImageFrame::OnPaint
)
595 EVT_MENU(wxID_SAVEAS
, MyImageFrame::OnSave
)
596 EVT_MENU_RANGE(ID_ROTATE_LEFT
, ID_ROTATE_RIGHT
, MyImageFrame::OnRotate
)
597 EVT_MENU(ID_RESIZE
, MyImageFrame::OnResize
)
599 EVT_MENU(wxID_ZOOM_IN
, MyImageFrame::OnZoom
)
600 EVT_MENU(wxID_ZOOM_OUT
, MyImageFrame::OnZoom
)
601 EVT_MENU(wxID_ZOOM_100
, MyImageFrame::OnZoom
)
604 //-----------------------------------------------------------------------------
606 //-----------------------------------------------------------------------------
608 #ifdef wxHAVE_RAW_BITMAP
610 BEGIN_EVENT_TABLE(MyRawBitmapFrame
, wxFrame
)
611 EVT_PAINT(MyRawBitmapFrame::OnPaint
)
614 #endif // wxHAVE_RAW_BITMAP
616 //-----------------------------------------------------------------------------
618 //-----------------------------------------------------------------------------
623 ID_ABOUT
= wxID_ABOUT
,
631 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
632 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
633 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
634 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
635 EVT_MENU (ID_NEW
, MyFrame::OnNewFrame
)
636 EVT_MENU (ID_INFO
, MyFrame::OnImageInfo
)
637 EVT_MENU (ID_SHOWTHUMBNAIL
, MyFrame::OnThumbnail
)
638 #ifdef wxHAVE_RAW_BITMAP
639 EVT_MENU (ID_SHOWRAW
, MyFrame::OnTestRawBitmap
)
641 #if wxUSE_GRAPHICS_CONTEXT
642 EVT_MENU (ID_GRAPHICS
, MyFrame::OnTestGraphics
)
643 #endif // wxUSE_GRAPHICS_CONTEXT
645 EVT_MENU(wxID_COPY
, MyFrame::OnCopy
)
646 EVT_MENU(wxID_PASTE
, MyFrame::OnPaste
)
647 #endif // wxUSE_CLIPBOARD
651 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, wxT("wxImage sample"),
652 wxPoint(20, 20), wxSize(950, 700) )
654 SetIcon(wxICON(sample
));
656 wxMenuBar
*menu_bar
= new wxMenuBar();
658 wxMenu
*menuImage
= new wxMenu
;
659 menuImage
->Append( ID_NEW
, wxT("&Show any image...\tCtrl-O"));
660 menuImage
->Append( ID_INFO
, wxT("Show image &information...\tCtrl-I"));
661 #ifdef wxHAVE_RAW_BITMAP
662 menuImage
->AppendSeparator();
663 menuImage
->Append( ID_SHOWRAW
, wxT("Test &raw bitmap...\tCtrl-R"));
665 #if wxUSE_GRAPHICS_CONTEXT
666 menuImage
->AppendSeparator();
667 menuImage
->Append(ID_GRAPHICS
, "Test &graphics context...\tCtrl-G");
668 #endif // wxUSE_GRAPHICS_CONTEXT
669 menuImage
->AppendSeparator();
670 menuImage
->Append( ID_SHOWTHUMBNAIL
, wxT("Test &thumbnail...\tCtrl-T"),
671 "Test scaling the image during load (try with JPEG)");
672 menuImage
->AppendSeparator();
673 menuImage
->Append( ID_ABOUT
, wxT("&About\tF1"));
674 menuImage
->AppendSeparator();
675 menuImage
->Append( ID_QUIT
, wxT("E&xit\tCtrl-Q"));
676 menu_bar
->Append(menuImage
, wxT("&Image"));
679 wxMenu
*menuClipboard
= new wxMenu
;
680 menuClipboard
->Append(wxID_COPY
, wxT("&Copy test image\tCtrl-C"));
681 menuClipboard
->Append(wxID_PASTE
, wxT("&Paste image\tCtrl-V"));
682 menu_bar
->Append(menuClipboard
, wxT("&Clipboard"));
683 #endif // wxUSE_CLIPBOARD
685 SetMenuBar( menu_bar
);
689 int widths
[] = { -1, 100 };
690 SetStatusWidths( 2, widths
);
691 #endif // wxUSE_STATUSBAR
693 m_canvas
= new MyCanvas( this, wxID_ANY
, wxPoint(0,0), wxSize(10,10) );
695 // 500 width * 2750 height
696 m_canvas
->SetScrollbars( 10, 10, 50, 275 );
697 m_canvas
->SetCursor(wxImage("cursor.png"));
700 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
705 #if wxUSE_ZLIB && wxUSE_STREAMS
706 #include "wx/zstream.h"
709 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
713 array
.Add("wxImage demo");
714 array
.Add("(c) Robert Roebling 1998-2005");
715 array
.Add("(c) Vadim Zeitlin 2005-2009");
717 array
.Add(wxEmptyString
);
718 array
.Add("Version of the libraries used:");
721 array
.Add(wxPNGHandler::GetLibraryVersionInfo().ToString());
724 array
.Add(wxJPEGHandler::GetLibraryVersionInfo().ToString());
727 array
.Add(wxTIFFHandler::GetLibraryVersionInfo().ToString());
729 #if wxUSE_ZLIB && wxUSE_STREAMS
730 // zlib is used by libpng
731 array
.Add(wxGetZlibVersionInfo().ToString());
733 (void)wxMessageBox( wxJoin(array
, '\n'),
734 "About wxImage Demo",
735 wxICON_INFORMATION
| wxOK
);
738 wxString
MyFrame::LoadUserImage(wxImage
& image
)
743 filename
= wxLoadFileSelector(wxT("image"), wxEmptyString
);
744 if ( !filename
.empty() )
746 if ( !image
.LoadFile(filename
) )
748 wxLogError(wxT("Couldn't load image from '%s'."), filename
.c_str());
750 return wxEmptyString
;
753 #endif // wxUSE_FILEDLG
758 void MyFrame::OnNewFrame( wxCommandEvent
&WXUNUSED(event
) )
761 wxString filename
= LoadUserImage(image
);
762 if ( !filename
.empty() )
763 new MyImageFrame(this, filename
, image
);
766 void MyFrame::OnImageInfo( wxCommandEvent
&WXUNUSED(event
) )
769 if ( !LoadUserImage(image
).empty() )
771 // TODO: show more information about the file
772 wxString info
= wxString::Format("Image size: %dx%d",
776 int xres
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX
),
777 yres
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY
);
780 info
+= wxString::Format("\nResolution: %dx%d", xres
, yres
);
781 switch ( image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT
) )
784 wxFAIL_MSG( "unknown image resolution units" );
787 case wxIMAGE_RESOLUTION_NONE
:
788 info
+= " in default units";
791 case wxIMAGE_RESOLUTION_INCHES
:
795 case wxIMAGE_RESOLUTION_CM
:
801 wxLogMessage("%s", info
);
805 #ifdef wxHAVE_RAW_BITMAP
807 void MyFrame::OnTestRawBitmap( wxCommandEvent
&WXUNUSED(event
) )
809 (new MyRawBitmapFrame(this))->Show();
812 #endif // wxHAVE_RAW_BITMAP
814 #if wxUSE_GRAPHICS_CONTEXT
816 class MyGraphicsFrame
: public wxFrame
825 MyGraphicsFrame(wxWindow
* parent
) :
826 wxFrame(parent
, wxID_ANY
, "Graphics context test"),
827 m_image(WIDTH
, HEIGHT
, false)
829 // Create a test image: it has 3 horizontal primary colour bands with
830 // alpha increasing from left to right.
832 unsigned char* alpha
= m_image
.GetAlpha();
833 unsigned char* data
= m_image
.GetData();
835 for ( int y
= 0; y
< HEIGHT
; y
++ )
842 else if ( y
< (2*HEIGHT
)/3 )
847 for ( int x
= 0; x
< WIDTH
; x
++ )
856 m_bitmap
= wxBitmap(m_image
);
858 Connect(wxEVT_PAINT
, wxPaintEventHandler(MyGraphicsFrame::OnPaint
));
864 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
867 wxScopedPtr
<wxGraphicsContext
> gc(wxGraphicsContext::Create(dc
));
868 wxGraphicsBitmap
gb(gc
->CreateBitmapFromImage(m_image
));
870 gc
->SetFont(*wxNORMAL_FONT
, *wxBLACK
);
871 gc
->DrawText("Bitmap", 0, HEIGHT
/2);
872 gc
->DrawBitmap(m_bitmap
, 0, 0, WIDTH
, HEIGHT
);
874 wxGraphicsFont gf
= gc
->CreateFont(wxNORMAL_FONT
->GetPixelSize().y
, "");
876 gc
->DrawText("Graphics bitmap", 0, (3*HEIGHT
)/2);
877 gc
->DrawBitmap(gb
, 0, HEIGHT
, WIDTH
, HEIGHT
);
883 wxDECLARE_NO_COPY_CLASS(MyGraphicsFrame
);
886 void MyFrame::OnTestGraphics(wxCommandEvent
& WXUNUSED(event
))
888 new MyGraphicsFrame(this);
891 #endif // wxUSE_GRAPHICS_CONTEXT
895 void MyFrame::OnCopy(wxCommandEvent
& WXUNUSED(event
))
897 wxBitmapDataObject
*dobjBmp
= new wxBitmapDataObject
;
898 dobjBmp
->SetBitmap(m_canvas
->my_horse_png
);
900 wxTheClipboard
->Open();
902 if ( !wxTheClipboard
->SetData(dobjBmp
) )
904 wxLogError(wxT("Failed to copy bitmap to clipboard"));
907 wxTheClipboard
->Close();
910 void MyFrame::OnPaste(wxCommandEvent
& WXUNUSED(event
))
912 wxBitmapDataObject dobjBmp
;
914 wxTheClipboard
->Open();
915 if ( !wxTheClipboard
->GetData(dobjBmp
) )
917 wxLogMessage(wxT("No bitmap data in the clipboard"));
921 new MyImageFrame(this, wxT("Clipboard"), dobjBmp
.GetBitmap());
923 wxTheClipboard
->Close();
926 #endif // wxUSE_CLIPBOARD
928 void MyFrame::OnThumbnail( wxCommandEvent
&WXUNUSED(event
) )
931 wxString filename
= wxLoadFileSelector(wxT("image"), wxEmptyString
, wxEmptyString
, this);
932 if ( filename
.empty() )
935 static const int THUMBNAIL_WIDTH
= 320;
936 static const int THUMBNAIL_HEIGHT
= 240;
939 image
.SetOption(wxIMAGE_OPTION_MAX_WIDTH
, THUMBNAIL_WIDTH
);
940 image
.SetOption(wxIMAGE_OPTION_MAX_HEIGHT
, THUMBNAIL_HEIGHT
);
943 if ( !image
.LoadFile(filename
) )
945 wxLogError(wxT("Couldn't load image from '%s'."), filename
.c_str());
949 int origWidth
= image
.GetOptionInt( wxIMAGE_OPTION_ORIGINAL_WIDTH
);
950 int origHeight
= image
.GetOptionInt( wxIMAGE_OPTION_ORIGINAL_HEIGHT
);
952 const long loadTime
= sw
.Time();
954 MyImageFrame
* const frame
= new MyImageFrame(this, filename
, image
);
955 wxLogStatus(frame
, "Loaded \"%s\" in %ldms; original size was (%d, %d)",
956 filename
, loadTime
, origWidth
, origHeight
);
958 wxLogError( wxT("Couldn't create file selector dialog") );
960 #endif // wxUSE_FILEDLG
963 //-----------------------------------------------------------------------------
965 //-----------------------------------------------------------------------------
971 if ( !wxApp::OnInit() )
974 wxInitAllImageHandlers();
976 wxFrame
*frame
= new MyFrame();