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