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