no real change; just reorganize the sample splitting it in two source files
[wxWidgets.git] / samples / image / image.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: samples/image/image.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
9 // License: wxWindows licence
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 #if defined(__WXMSW__)
37 #ifdef wxHAVE_RAW_BITMAP
38 #include "wx/rawbmp.h"
39 #endif
40 #endif
41
42 #if defined(__WXMAC__) || defined(__WXGTK__)
43 #define wxHAVE_RAW_BITMAP
44 #include "wx/rawbmp.h"
45 #endif
46
47 #include "canvas.h"
48
49
50 // ============================================================================
51 // declarations
52 // ============================================================================
53
54 //-----------------------------------------------------------------------------
55 // MyApp
56 //-----------------------------------------------------------------------------
57
58 class MyApp: public wxApp
59 {
60 public:
61 virtual bool OnInit();
62 };
63
64 // ----------------------------------------------------------------------------
65 // MyFrame
66 // ----------------------------------------------------------------------------
67
68 class MyFrame: public wxFrame
69 {
70 public:
71 MyFrame();
72
73 void OnAbout( wxCommandEvent &event );
74 void OnNewFrame( wxCommandEvent &event );
75 void OnImageInfo( wxCommandEvent &event );
76 void OnThumbnail( wxCommandEvent &event );
77
78 #ifdef wxHAVE_RAW_BITMAP
79 void OnTestRawBitmap( wxCommandEvent &event );
80 #endif // wxHAVE_RAW_BITMAP
81 void OnQuit( wxCommandEvent &event );
82
83 #if wxUSE_CLIPBOARD
84 void OnCopy(wxCommandEvent& event);
85 void OnPaste(wxCommandEvent& event);
86 #endif // wxUSE_CLIPBOARD
87
88 MyCanvas *m_canvas;
89
90 private:
91 // ask user for the file name and try to load an image from it
92 //
93 // return the file path on success, empty string if we failed to load the
94 // image or were cancelled by user
95 static wxString LoadUserImage(wxImage& image);
96
97
98 DECLARE_DYNAMIC_CLASS(MyFrame)
99 DECLARE_EVENT_TABLE()
100 };
101
102 // ----------------------------------------------------------------------------
103 // Frame used for showing a standalone image
104 // ----------------------------------------------------------------------------
105
106 enum
107 {
108 ID_ROTATE_LEFT = wxID_HIGHEST+1,
109 ID_ROTATE_RIGHT,
110 ID_RESIZE,
111 ID_PAINT_BG
112 };
113
114 class MyImageFrame : public wxFrame
115 {
116 public:
117 MyImageFrame(wxFrame *parent, const wxString& desc, const wxBitmap& bitmap)
118 : wxFrame(parent, wxID_ANY,
119 wxString::Format(_T("Image from %s"), desc.c_str()),
120 wxDefaultPosition, wxDefaultSize,
121 wxDEFAULT_FRAME_STYLE | wxFULL_REPAINT_ON_RESIZE),
122 m_bitmap(bitmap)
123 {
124 wxMenu *menu = new wxMenu;
125 menu->Append(wxID_SAVE);
126 menu->AppendSeparator();
127 m_pClearBgMenu = menu->AppendCheckItem(ID_PAINT_BG, _T("&Paint background"));
128 menu->AppendSeparator();
129 menu->Append(ID_RESIZE, _T("&Fit to window\tCtrl-F"));
130 menu->AppendSeparator();
131 menu->Append(ID_ROTATE_LEFT, _T("Rotate &left\tCtrl-L"));
132 menu->Append(ID_ROTATE_RIGHT, _T("Rotate &right\tCtrl-R"));
133
134 wxMenuBar *mbar = new wxMenuBar;
135 mbar->Append(menu, _T("&Image"));
136 SetMenuBar(mbar);
137
138 CreateStatusBar();
139
140 SetClientSize(bitmap.GetWidth(), bitmap.GetHeight());
141
142 UpdateStatusBar();
143
144 // SetBackgroundColour(*wxWHITE);
145 }
146
147 void OnEraseBackground(wxEraseEvent& WXUNUSED(event))
148 {
149 // do nothing here to be able to see how transparent images are shown
150 }
151
152 void OnPaint(wxPaintEvent& WXUNUSED(event))
153 {
154 wxPaintDC dc(this);
155
156 if (m_pClearBgMenu->IsChecked())
157 ClearBackground();
158
159 const wxSize size = GetClientSize();
160 dc.DrawBitmap(m_bitmap,
161 (size.x - m_bitmap.GetWidth())/2,
162 (size.y - m_bitmap.GetHeight())/2,
163 true /* use mask */);
164 }
165
166 void OnSave(wxCommandEvent& WXUNUSED(event))
167 {
168 #if wxUSE_FILEDLG
169 wxImage image = m_bitmap.ConvertToImage();
170
171 wxString savefilename = wxFileSelector( wxT("Save Image"),
172 wxEmptyString,
173 wxEmptyString,
174 (const wxChar *)NULL,
175 wxT("BMP files (*.bmp)|*.bmp|")
176 wxT("PNG files (*.png)|*.png|")
177 wxT("JPEG files (*.jpg)|*.jpg|")
178 wxT("GIF files (*.gif)|*.gif|")
179 wxT("TIFF files (*.tif)|*.tif|")
180 wxT("PCX files (*.pcx)|*.pcx|")
181 wxT("ICO files (*.ico)|*.ico|")
182 wxT("CUR files (*.cur)|*.cur"),
183 wxFD_SAVE,
184 this);
185
186 if ( savefilename.empty() )
187 return;
188
189 wxString extension;
190 wxFileName::SplitPath(savefilename, NULL, NULL, &extension);
191
192 bool saved = false;
193 if ( extension == _T("bmp") )
194 {
195 static const int bppvalues[] =
196 {
197 wxBMP_1BPP,
198 wxBMP_1BPP_BW,
199 wxBMP_4BPP,
200 wxBMP_8BPP,
201 wxBMP_8BPP_GREY,
202 wxBMP_8BPP_RED,
203 wxBMP_8BPP_PALETTE,
204 wxBMP_24BPP
205 };
206
207 const wxString bppchoices[] =
208 {
209 _T("1 bpp color"),
210 _T("1 bpp B&W"),
211 _T("4 bpp color"),
212 _T("8 bpp color"),
213 _T("8 bpp greyscale"),
214 _T("8 bpp red"),
215 _T("8 bpp own palette"),
216 _T("24 bpp")
217 };
218
219 int bppselection = wxGetSingleChoiceIndex(_T("Set BMP BPP"),
220 _T("Image sample: save file"),
221 WXSIZEOF(bppchoices),
222 bppchoices,
223 this);
224 if ( bppselection != -1 )
225 {
226 int format = bppvalues[bppselection];
227 image.SetOption(wxIMAGE_OPTION_BMP_FORMAT, format);
228
229 if ( format == wxBMP_8BPP_PALETTE )
230 {
231 unsigned char *cmap = new unsigned char [256];
232 for ( int i = 0; i < 256; i++ )
233 cmap[i] = (unsigned char)i;
234 image.SetPalette(wxPalette(256, cmap, cmap, cmap));
235
236 delete[] cmap;
237 }
238 }
239 }
240 else if ( extension == _T("png") )
241 {
242 static const int pngvalues[] =
243 {
244 wxPNG_TYPE_COLOUR,
245 wxPNG_TYPE_COLOUR,
246 wxPNG_TYPE_GREY,
247 wxPNG_TYPE_GREY,
248 wxPNG_TYPE_GREY_RED,
249 wxPNG_TYPE_GREY_RED,
250 };
251
252 const wxString pngchoices[] =
253 {
254 _T("Colour 8bpp"),
255 _T("Colour 16bpp"),
256 _T("Grey 8bpp"),
257 _T("Grey 16bpp"),
258 _T("Grey red 8bpp"),
259 _T("Grey red 16bpp"),
260 };
261
262 int sel = wxGetSingleChoiceIndex(_T("Set PNG format"),
263 _T("Image sample: save file"),
264 WXSIZEOF(pngchoices),
265 pngchoices,
266 this);
267 if ( sel != -1 )
268 {
269 image.SetOption(wxIMAGE_OPTION_PNG_FORMAT, pngvalues[sel]);
270 image.SetOption(wxIMAGE_OPTION_PNG_BITDEPTH, sel % 2 ? 16 : 8);
271 }
272 }
273 else if ( extension == _T("cur") )
274 {
275 image.Rescale(32,32);
276 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, 0);
277 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, 0);
278 // This shows how you can save an image with explicitly
279 // specified image format:
280 saved = image.SaveFile(savefilename, wxBITMAP_TYPE_CUR);
281 }
282
283 if ( !saved )
284 {
285 // This one guesses image format from filename extension
286 // (it may fail if the extension is not recognized):
287 image.SaveFile(savefilename);
288 }
289 #endif // wxUSE_FILEDLG
290 }
291
292 void OnResize(wxCommandEvent& WXUNUSED(event))
293 {
294 wxImage img(m_bitmap.ConvertToImage());
295
296 const wxSize size = GetClientSize();
297 img.Rescale(size.x, size.y, wxIMAGE_QUALITY_HIGH);
298 m_bitmap = wxBitmap(img);
299
300 UpdateStatusBar();
301 Refresh();
302 }
303
304 void OnRotate(wxCommandEvent& event)
305 {
306 double angle = 5;
307 if ( event.GetId() == ID_ROTATE_LEFT )
308 angle = -angle;
309
310 wxImage img(m_bitmap.ConvertToImage());
311 img = img.Rotate(angle, wxPoint(img.GetWidth() / 2, img.GetHeight() / 2));
312 if ( !img.Ok() )
313 {
314 wxLogWarning(_T("Rotation failed"));
315 return;
316 }
317
318 m_bitmap = wxBitmap(img);
319
320 UpdateStatusBar();
321 Refresh();
322 }
323
324 private:
325 void UpdateStatusBar()
326 {
327 wxLogStatus(this, _T("Image size: (%d, %d)"),
328 m_bitmap.GetWidth(),
329 m_bitmap.GetHeight());
330 }
331
332 wxBitmap m_bitmap;
333 wxMenuItem* m_pClearBgMenu;
334
335 DECLARE_EVENT_TABLE()
336 };
337
338 #ifdef wxHAVE_RAW_BITMAP
339
340 #include "wx/rawbmp.h"
341
342 class MyRawBitmapFrame : public wxFrame
343 {
344 public:
345 enum
346 {
347 BORDER = 15,
348 SIZE = 150,
349 REAL_SIZE = SIZE - 2*BORDER
350 };
351
352 MyRawBitmapFrame(wxFrame *parent)
353 : wxFrame(parent, wxID_ANY, _T("Raw bitmaps (how exciting)")),
354 m_bitmap(SIZE, SIZE, 24),
355 m_alphaBitmap(SIZE, SIZE, 32)
356 {
357 SetClientSize(SIZE, SIZE*2+25);
358
359 InitAlphaBitmap();
360 InitBitmap();
361
362 }
363
364 void InitAlphaBitmap()
365 {
366 // First, clear the whole bitmap by making it alpha
367 {
368 wxAlphaPixelData data( m_alphaBitmap, wxPoint(0,0), wxSize(SIZE, SIZE) );
369 if ( !data )
370 {
371 wxLogError(_T("Failed to gain raw access to bitmap data"));
372 return;
373 }
374 wxAlphaPixelData::Iterator p(data);
375 for ( int y = 0; y < SIZE; ++y )
376 {
377 wxAlphaPixelData::Iterator rowStart = p;
378 for ( int x = 0; x < SIZE; ++x )
379 {
380 p.Alpha() = 0;
381 ++p; // same as p.OffsetX(1)
382 }
383 p = rowStart;
384 p.OffsetY(data, 1);
385 }
386 }
387
388 // Then, draw colourful alpha-blended stripes
389 wxAlphaPixelData data(m_alphaBitmap, wxPoint(BORDER, BORDER),
390 wxSize(REAL_SIZE, REAL_SIZE));
391 if ( !data )
392 {
393 wxLogError(_T("Failed to gain raw access to bitmap data"));
394 return;
395 }
396
397 wxAlphaPixelData::Iterator p(data);
398
399 for ( int y = 0; y < REAL_SIZE; ++y )
400 {
401 wxAlphaPixelData::Iterator rowStart = p;
402
403 int r = y < REAL_SIZE/3 ? 255 : 0,
404 g = (REAL_SIZE/3 <= y) && (y < 2*(REAL_SIZE/3)) ? 255 : 0,
405 b = 2*(REAL_SIZE/3) <= y ? 255 : 0;
406
407 for ( int x = 0; x < REAL_SIZE; ++x )
408 {
409 // note that RGB must be premultiplied by alpha
410 unsigned a = (wxAlphaPixelData::Iterator::ChannelType)((x*255.)/REAL_SIZE);
411 p.Red() = r * a / 256;
412 p.Green() = g * a / 256;
413 p.Blue() = b * a / 256;
414 p.Alpha() = a;
415
416 ++p; // same as p.OffsetX(1)
417 }
418
419 p = rowStart;
420 p.OffsetY(data, 1);
421 }
422 }
423
424 void InitBitmap()
425 {
426 // draw some colourful stripes without alpha
427 wxNativePixelData data(m_bitmap);
428 if ( !data )
429 {
430 wxLogError(_T("Failed to gain raw access to bitmap data"));
431 return;
432 }
433
434 wxNativePixelData::Iterator p(data);
435 for ( int y = 0; y < SIZE; ++y )
436 {
437 wxNativePixelData::Iterator rowStart = p;
438
439 int r = y < SIZE/3 ? 255 : 0,
440 g = (SIZE/3 <= y) && (y < 2*(SIZE/3)) ? 255 : 0,
441 b = 2*(SIZE/3) <= y ? 255 : 0;
442
443 for ( int x = 0; x < SIZE; ++x )
444 {
445 p.Red() = r;
446 p.Green() = g;
447 p.Blue() = b;
448 ++p; // same as p.OffsetX(1)
449 }
450
451 p = rowStart;
452 p.OffsetY(data, 1);
453 }
454 }
455
456 void OnPaint(wxPaintEvent& WXUNUSED(event))
457 {
458 wxPaintDC dc( this );
459 dc.DrawText(_T("This is alpha and raw bitmap test"), 0, BORDER);
460 dc.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE/2 - BORDER);
461 dc.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE - 2*BORDER);
462 dc.DrawBitmap( m_alphaBitmap, 0, 0, true /* use mask */ );
463
464 dc.DrawText(_T("Raw bitmap access without alpha"), 0, SIZE+5);
465 dc.DrawBitmap( m_bitmap, 0, SIZE+5+dc.GetCharHeight());
466 }
467
468 private:
469 wxBitmap m_bitmap;
470 wxBitmap m_alphaBitmap;
471
472 DECLARE_EVENT_TABLE()
473 };
474
475 #endif // wxHAVE_RAW_BITMAP
476
477
478 // ============================================================================
479 // implementations
480 // ============================================================================
481
482 //-----------------------------------------------------------------------------
483 // MyImageFrame
484 //-----------------------------------------------------------------------------
485
486 BEGIN_EVENT_TABLE(MyImageFrame, wxFrame)
487 EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground)
488 EVT_PAINT(MyImageFrame::OnPaint)
489
490 EVT_MENU(wxID_SAVE, MyImageFrame::OnSave)
491 EVT_MENU_RANGE(ID_ROTATE_LEFT, ID_ROTATE_RIGHT, MyImageFrame::OnRotate)
492 EVT_MENU(ID_RESIZE, MyImageFrame::OnResize)
493 END_EVENT_TABLE()
494
495 //-----------------------------------------------------------------------------
496 // MyRawBitmapFrame
497 //-----------------------------------------------------------------------------
498
499 #ifdef wxHAVE_RAW_BITMAP
500
501 BEGIN_EVENT_TABLE(MyRawBitmapFrame, wxFrame)
502 EVT_PAINT(MyRawBitmapFrame::OnPaint)
503 END_EVENT_TABLE()
504
505 #endif // wxHAVE_RAW_BITMAP
506
507 //-----------------------------------------------------------------------------
508 // MyFrame
509 //-----------------------------------------------------------------------------
510
511 enum
512 {
513 ID_QUIT = wxID_EXIT,
514 ID_ABOUT = wxID_ABOUT,
515 ID_NEW = 100,
516 ID_INFO,
517 ID_SHOWRAW,
518 ID_SHOWTHUMBNAIL
519 };
520
521 IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
522 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
523 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
524 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
525 EVT_MENU (ID_NEW, MyFrame::OnNewFrame)
526 EVT_MENU (ID_INFO, MyFrame::OnImageInfo)
527 EVT_MENU (ID_SHOWTHUMBNAIL, MyFrame::OnThumbnail)
528 #ifdef wxHAVE_RAW_BITMAP
529 EVT_MENU (ID_SHOWRAW, MyFrame::OnTestRawBitmap)
530 #endif
531 #if wxUSE_CLIPBOARD
532 EVT_MENU(wxID_COPY, MyFrame::OnCopy)
533 EVT_MENU(wxID_PASTE, MyFrame::OnPaste)
534 #endif // wxUSE_CLIPBOARD
535 END_EVENT_TABLE()
536
537 MyFrame::MyFrame()
538 : wxFrame( (wxFrame *)NULL, wxID_ANY, _T("wxImage sample"),
539 wxPoint(20, 20), wxSize(950, 700) )
540 {
541 wxMenuBar *menu_bar = new wxMenuBar();
542
543 wxMenu *menuImage = new wxMenu;
544 menuImage->Append( ID_NEW, _T("&Show any image...\tCtrl-O"));
545 menuImage->Append( ID_INFO, _T("Show image &information...\tCtrl-I"));
546 #ifdef wxHAVE_RAW_BITMAP
547 menuImage->AppendSeparator();
548 menuImage->Append( ID_SHOWRAW, _T("Test &raw bitmap...\tCtrl-R"));
549 #endif
550 menuImage->AppendSeparator();
551 menuImage->Append( ID_SHOWTHUMBNAIL, _T("Test &thumbnail...\tCtrl-T"),
552 "Test scaling the image during load (try with JPEG)");
553 menuImage->AppendSeparator();
554 menuImage->Append( ID_ABOUT, _T("&About..."));
555 menuImage->AppendSeparator();
556 menuImage->Append( ID_QUIT, _T("E&xit\tCtrl-Q"));
557 menu_bar->Append(menuImage, _T("&Image"));
558
559 #if wxUSE_CLIPBOARD
560 wxMenu *menuClipboard = new wxMenu;
561 menuClipboard->Append(wxID_COPY, _T("&Copy test image\tCtrl-C"));
562 menuClipboard->Append(wxID_PASTE, _T("&Paste image\tCtrl-V"));
563 menu_bar->Append(menuClipboard, _T("&Clipboard"));
564 #endif // wxUSE_CLIPBOARD
565
566 SetMenuBar( menu_bar );
567
568 #if wxUSE_STATUSBAR
569 CreateStatusBar(2);
570 int widths[] = { -1, 100 };
571 SetStatusWidths( 2, widths );
572 #endif // wxUSE_STATUSBAR
573
574 m_canvas = new MyCanvas( this, wxID_ANY, wxPoint(0,0), wxSize(10,10) );
575
576 // 500 width * 2750 height
577 m_canvas->SetScrollbars( 10, 10, 50, 275 );
578 }
579
580 void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
581 {
582 Close( true );
583 }
584
585 void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
586 {
587 (void)wxMessageBox( _T("wxImage demo\n")
588 _T("Robert Roebling (c) 1998,2000"),
589 _T("About wxImage Demo"), wxICON_INFORMATION | wxOK );
590 }
591
592 wxString MyFrame::LoadUserImage(wxImage& image)
593 {
594 wxString filename;
595
596 #if wxUSE_FILEDLG
597 filename = wxFileSelector(_T("Select image file"));
598 if ( !filename.empty() )
599 {
600 if ( !image.LoadFile(filename) )
601 {
602 wxLogError(_T("Couldn't load image from '%s'."), filename.c_str());
603
604 return wxEmptyString;
605 }
606 }
607 #endif // wxUSE_FILEDLG
608
609 return filename;
610 }
611
612 void MyFrame::OnNewFrame( wxCommandEvent &WXUNUSED(event) )
613 {
614 wxImage image;
615 wxString filename = LoadUserImage(image);
616 if ( !filename.empty() )
617 (new MyImageFrame(this, filename, wxBitmap(image)))->Show();
618 }
619
620 void MyFrame::OnImageInfo( wxCommandEvent &WXUNUSED(event) )
621 {
622 wxImage image;
623 if ( !LoadUserImage(image).empty() )
624 {
625 // TODO: show more information about the file
626 wxString info = wxString::Format("Image size: %dx%d",
627 image.GetWidth(),
628 image.GetHeight());
629
630 int xres = image.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX),
631 yres = image.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY);
632 if ( xres || yres )
633 {
634 info += wxString::Format("\nResolution: %dx%d", xres, yres);
635 switch ( image.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT) )
636 {
637 default:
638 wxFAIL_MSG( "unknown image resolution units" );
639 // fall through
640
641 case wxIMAGE_RESOLUTION_NONE:
642 info += " in default units";
643 break;
644
645 case wxIMAGE_RESOLUTION_INCHES:
646 info += " in";
647 break;
648
649 case wxIMAGE_RESOLUTION_CM:
650 info += " cm";
651 break;
652 }
653 }
654
655 wxLogMessage("%s", info);
656 }
657 }
658
659 #ifdef wxHAVE_RAW_BITMAP
660
661 void MyFrame::OnTestRawBitmap( wxCommandEvent &WXUNUSED(event) )
662 {
663 (new MyRawBitmapFrame(this))->Show();
664 }
665
666 #endif // wxHAVE_RAW_BITMAP
667
668 #if wxUSE_CLIPBOARD
669
670 void MyFrame::OnCopy(wxCommandEvent& WXUNUSED(event))
671 {
672 wxBitmapDataObject *dobjBmp = new wxBitmapDataObject;
673 dobjBmp->SetBitmap(m_canvas->my_horse_png);
674
675 wxTheClipboard->Open();
676
677 if ( !wxTheClipboard->SetData(dobjBmp) )
678 {
679 wxLogError(_T("Failed to copy bitmap to clipboard"));
680 }
681
682 wxTheClipboard->Close();
683 }
684
685 void MyFrame::OnPaste(wxCommandEvent& WXUNUSED(event))
686 {
687 wxBitmapDataObject dobjBmp;
688
689 wxTheClipboard->Open();
690 if ( !wxTheClipboard->GetData(dobjBmp) )
691 {
692 wxLogMessage(_T("No bitmap data in the clipboard"));
693 }
694 else
695 {
696 (new MyImageFrame(this, _T("Clipboard"), dobjBmp.GetBitmap()))->Show();
697 }
698 wxTheClipboard->Close();
699 }
700
701 #endif // wxUSE_CLIPBOARD
702
703 void MyFrame::OnThumbnail( wxCommandEvent &WXUNUSED(event) )
704 {
705 #if wxUSE_FILEDLG
706 wxString filename = wxFileSelector(_T("Select image file"));
707 if ( filename.empty() )
708 return;
709
710 static const int THUMBNAIL_WIDTH = 320;
711 static const int THUMBNAIL_HEIGHT = 240;
712
713 wxImage image;
714 image.SetOption(wxIMAGE_OPTION_MAX_WIDTH, THUMBNAIL_WIDTH);
715 image.SetOption(wxIMAGE_OPTION_MAX_HEIGHT, THUMBNAIL_HEIGHT);
716
717 wxStopWatch sw;
718 if ( !image.LoadFile(filename) )
719 {
720 wxLogError(_T("Couldn't load image from '%s'."), filename.c_str());
721 return;
722 }
723
724 const long loadTime = sw.Time();
725
726 MyImageFrame * const
727 frame = new MyImageFrame(this, filename, wxBitmap(image));
728 frame->Show();
729 wxLogStatus(frame, "Loaded \"%s\" in %ldms", filename, loadTime);
730 #else
731 wxLogError( _T("Couldn't create file selector dialog") );
732 return;
733 #endif // wxUSE_FILEDLG
734 }
735
736 //-----------------------------------------------------------------------------
737 // MyApp
738 //-----------------------------------------------------------------------------
739
740 IMPLEMENT_APP(MyApp)
741
742 bool MyApp::OnInit()
743 {
744 if ( !wxApp::OnInit() )
745 return false;
746
747 wxInitAllImageHandlers();
748
749 wxFrame *frame = new MyFrame();
750 frame->Show( true );
751
752 return true;
753 }