]> git.saurik.com Git - wxWidgets.git/blame - samples/image/image.cpp
don't call Disconnect() if the connection had been already lost (closes #10747)
[wxWidgets.git] / samples / image / image.cpp
CommitLineData
5a566d89
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: samples/image/image.cpp
3// Purpose: sample showing operations with wxImage
4// Author: Robert Roebling
09ddabf7 5// Modified by: Francesco Montorsi
5a566d89
VZ
6// Created: 1998
7// RCS-ID: $Id$
8// Copyright: (c) 1998-2005 Robert Roebling
a2bbedf0 9// (c) 2005-2009 Vadim Zeitlin
5a566d89
VZ
10// License: wxWindows licence
11///////////////////////////////////////////////////////////////////////////////
01111366 12
3d05544e
JS
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
9061c15c 21 #include "wx/wx.h"
3d05544e
JS
22#endif
23
01111366 24#include "wx/image.h"
a23fd0e1 25#include "wx/file.h"
5a566d89 26#include "wx/filename.h"
a8d4e3ac
VS
27#include "wx/mstream.h"
28#include "wx/wfstream.h"
1971d23c 29#include "wx/quantize.h"
95706ac1 30#include "wx/stopwatch.h"
a23fd0e1 31
da9df1f5
VZ
32#if wxUSE_CLIPBOARD
33 #include "wx/dataobj.h"
34 #include "wx/clipbrd.h"
35#endif // wxUSE_CLIPBOARD
36
284f2b59 37#if defined(__WXMSW__)
481721e8 38 #ifdef wxHAVE_RAW_BITMAP
9b24b388 39 #include "wx/rawbmp.h"
481721e8 40 #endif
c698eae5 41#endif
a8d4e3ac 42
284f2b59
RR
43#if defined(__WXMAC__) || defined(__WXGTK__)
44 #define wxHAVE_RAW_BITMAP
45 #include "wx/rawbmp.h"
46#endif
47
09ddabf7 48#include "canvas.h"
01111366 49
41f02b9a
FM
50#ifndef __WXMSW__
51 #include "../sample.xpm"
52#endif
01111366 53
09ddabf7
FM
54// ============================================================================
55// declarations
56// ============================================================================
01111366 57
09ddabf7
FM
58//-----------------------------------------------------------------------------
59// MyApp
60//-----------------------------------------------------------------------------
61
62class MyApp: public wxApp
01111366 63{
1d5b7a0b 64public:
09ddabf7 65 virtual bool OnInit();
01111366
RR
66};
67
09ddabf7 68// ----------------------------------------------------------------------------
01111366 69// MyFrame
09ddabf7 70// ----------------------------------------------------------------------------
18134a1c 71
01111366
RR
72class MyFrame: public wxFrame
73{
1d5b7a0b
VZ
74public:
75 MyFrame();
01111366 76
01111366 77 void OnAbout( wxCommandEvent &event );
ab0f0386 78 void OnNewFrame( wxCommandEvent &event );
37ba70a5 79 void OnImageInfo( wxCommandEvent &event );
95706ac1
VZ
80 void OnThumbnail( wxCommandEvent &event );
81
b3f04dc2
VZ
82#ifdef wxHAVE_RAW_BITMAP
83 void OnTestRawBitmap( wxCommandEvent &event );
84#endif // wxHAVE_RAW_BITMAP
01111366 85 void OnQuit( wxCommandEvent &event );
1d5b7a0b 86
dd38c875 87#if wxUSE_CLIPBOARD
da9df1f5
VZ
88 void OnCopy(wxCommandEvent& event);
89 void OnPaste(wxCommandEvent& event);
90#endif // wxUSE_CLIPBOARD
91
01111366 92 MyCanvas *m_canvas;
1d5b7a0b 93
60a41aee 94private:
37ba70a5
VZ
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
1d5b7a0b
VZ
102 DECLARE_DYNAMIC_CLASS(MyFrame)
103 DECLARE_EVENT_TABLE()
01111366
RR
104};
105
1d8acb7d
VZ
106// ----------------------------------------------------------------------------
107// Frame used for showing a standalone image
108// ----------------------------------------------------------------------------
109
110enum
111{
8b6264b4 112 ID_ROTATE_LEFT = wxID_HIGHEST+1,
05a8831a 113 ID_ROTATE_RIGHT,
8b6264b4
FM
114 ID_RESIZE,
115 ID_PAINT_BG
1d8acb7d
VZ
116};
117
ab0f0386
VZ
118class MyImageFrame : public wxFrame
119{
120public:
85fcb94f
VZ
121 MyImageFrame(wxFrame *parent, const wxString& desc, const wxImage& image)
122 {
123 Create(parent, desc, wxBitmap(image), image.GetImageCount(desc));
124 }
125
1d8acb7d 126 MyImageFrame(wxFrame *parent, const wxString& desc, const wxBitmap& bitmap)
ab0f0386 127 {
85fcb94f
VZ
128 Create(parent, desc, bitmap);
129 }
130
a2bbedf0 131private:
85fcb94f
VZ
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;
a2bbedf0 144 m_zoom = 1.;
85fcb94f 145
1d8acb7d
VZ
146 wxMenu *menu = new wxMenu;
147 menu->Append(wxID_SAVE);
148 menu->AppendSeparator();
437950ce
VZ
149 menu->AppendCheckItem(ID_PAINT_BG, _T("&Paint background"),
150 "Uncheck this for transparent images");
8b6264b4 151 menu->AppendSeparator();
05a8831a 152 menu->Append(ID_RESIZE, _T("&Fit to window\tCtrl-F"));
a2bbedf0
VZ
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");
05a8831a 156 menu->AppendSeparator();
1d8acb7d
VZ
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
437950ce
VZ
164 mbar->Check(ID_PAINT_BG, true);
165
85fcb94f
VZ
166 CreateStatusBar(2);
167 if ( numImages != 1 )
168 SetStatusText(wxString::Format("%d images", numImages), 1);
05a8831a 169
ab0f0386 170 SetClientSize(bitmap.GetWidth(), bitmap.GetHeight());
05a8831a
VZ
171
172 UpdateStatusBar();
8b6264b4 173
85fcb94f
VZ
174 Show();
175
176 return true;
ab0f0386
VZ
177 }
178
0c543b7a
VZ
179 void OnEraseBackground(wxEraseEvent& WXUNUSED(event))
180 {
181 // do nothing here to be able to see how transparent images are shown
182 }
183
ab0f0386
VZ
184 void OnPaint(wxPaintEvent& WXUNUSED(event))
185 {
05a8831a 186 wxPaintDC dc(this);
8b6264b4 187
437950ce 188 if ( GetMenuBar()->IsChecked(ID_PAINT_BG) )
8b6264b4
FM
189 ClearBackground();
190
a2bbedf0
VZ
191 dc.SetUserScale(m_zoom, m_zoom);
192
05a8831a 193 const wxSize size = GetClientSize();
a2bbedf0
VZ
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 );
ab0f0386
VZ
201 }
202
1d8acb7d 203 void OnSave(wxCommandEvent& WXUNUSED(event))
1971d23c 204 {
71307412 205#if wxUSE_FILEDLG
368d59f0 206 wxImage image = m_bitmap.ConvertToImage();
1971d23c 207
4693b20c 208 wxString savefilename = wxFileSelector( wxT("Save Image"),
71307412
WS
209 wxEmptyString,
210 wxEmptyString,
4693b20c 211 (const wxChar *)NULL,
d0ee33f5
WS
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"),
ff3e84ff 220 wxFD_SAVE,
47f797bd 221 this);
1971d23c
VZ
222
223 if ( savefilename.empty() )
224 return;
225
5a566d89
VZ
226 wxString extension;
227 wxFileName::SplitPath(savefilename, NULL, NULL, &extension);
1971d23c 228
5a566d89 229 bool saved = false;
c66972cc 230 if ( extension == _T("bmp") )
5a566d89
VZ
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
24207dfc 244 const wxString bppchoices[] =
5a566d89
VZ
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"),
09ddabf7
FM
257 _T("Image sample: save file"),
258 WXSIZEOF(bppchoices),
259 bppchoices,
260 this);
5a566d89
VZ
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
d0ee33f5 273 delete[] cmap;
5a566d89
VZ
274 }
275 }
1971d23c 276 }
5a566d89
VZ
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
24207dfc 289 const wxString pngchoices[] =
5a566d89
VZ
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"),
09ddabf7
FM
300 _T("Image sample: save file"),
301 WXSIZEOF(pngchoices),
302 pngchoices,
303 this);
5a566d89
VZ
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);
d19ce8c4
FM
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 }
5a566d89
VZ
345 }
346 }
347 else if ( extension == _T("cur") )
45647dcf 348 {
51b07644
VZ
349 image.Rescale(32,32);
350 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, 0);
45647dcf
VS
351 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, 0);
352 // This shows how you can save an image with explicitly
353 // specified image format:
5a566d89 354 saved = image.SaveFile(savefilename, wxBITMAP_TYPE_CUR);
45647dcf 355 }
5a566d89
VZ
356
357 if ( !saved )
45647dcf
VS
358 {
359 // This one guesses image format from filename extension
360 // (it may fail if the extension is not recognized):
5a566d89 361 image.SaveFile(savefilename);
45647dcf 362 }
71307412 363#endif // wxUSE_FILEDLG
1971d23c
VZ
364 }
365
05a8831a
VZ
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();
a2bbedf0
VZ
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();
05a8831a
VZ
387 }
388
1d8acb7d
VZ
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());
05a8831a 396 img = img.Rotate(angle, wxPoint(img.GetWidth() / 2, img.GetHeight() / 2));
1d8acb7d
VZ
397 if ( !img.Ok() )
398 {
399 wxLogWarning(_T("Rotation failed"));
400 return;
401 }
402
403 m_bitmap = wxBitmap(img);
05a8831a
VZ
404
405 UpdateStatusBar();
1d8acb7d
VZ
406 }
407
05a8831a
VZ
408 void UpdateStatusBar()
409 {
a2bbedf0 410 wxLogStatus(this, _T("Image size: (%d, %d), zoom %.2f"),
05a8831a 411 m_bitmap.GetWidth(),
a2bbedf0
VZ
412 m_bitmap.GetHeight(),
413 m_zoom);
414 Refresh();
05a8831a
VZ
415 }
416
ab0f0386 417 wxBitmap m_bitmap;
a2bbedf0 418 double m_zoom;
ab0f0386
VZ
419
420 DECLARE_EVENT_TABLE()
421};
422
b3f04dc2
VZ
423#ifdef wxHAVE_RAW_BITMAP
424
425#include "wx/rawbmp.h"
426
427class MyRawBitmapFrame : public wxFrame
428{
429public:
430 enum
431 {
432 BORDER = 15,
433 SIZE = 150,
434 REAL_SIZE = SIZE - 2*BORDER
435 };
436
437 MyRawBitmapFrame(wxFrame *parent)
47f797bd 438 : wxFrame(parent, wxID_ANY, _T("Raw bitmaps (how exciting)")),
09ddabf7
FM
439 m_bitmap(SIZE, SIZE, 24),
440 m_alphaBitmap(SIZE, SIZE, 32)
b3f04dc2 441 {
8ef08948 442 SetClientSize(SIZE, SIZE*2+25);
b3f04dc2 443
8ef08948
RD
444 InitAlphaBitmap();
445 InitBitmap();
66df4ec6 446
8ef08948
RD
447 }
448
449 void InitAlphaBitmap()
450 {
6e5551ad
RR
451 // First, clear the whole bitmap by making it alpha
452 {
8ef08948 453 wxAlphaPixelData data( m_alphaBitmap, wxPoint(0,0), wxSize(SIZE, SIZE) );
6e5551ad
RR
454 if ( !data )
455 {
456 wxLogError(_T("Failed to gain raw access to bitmap data"));
457 return;
458 }
8ef08948 459 wxAlphaPixelData::Iterator p(data);
6e5551ad
RR
460 for ( int y = 0; y < SIZE; ++y )
461 {
8ef08948 462 wxAlphaPixelData::Iterator rowStart = p;
6e5551ad
RR
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 }
095980e1 472
6e5551ad 473 // Then, draw colourful alpha-blended stripes
8ef08948 474 wxAlphaPixelData data(m_alphaBitmap, wxPoint(BORDER, BORDER),
09ddabf7 475 wxSize(REAL_SIZE, REAL_SIZE));
b3f04dc2
VZ
476 if ( !data )
477 {
478 wxLogError(_T("Failed to gain raw access to bitmap data"));
479 return;
480 }
481
8ef08948 482 wxAlphaPixelData::Iterator p(data);
b3f04dc2 483
b3f04dc2
VZ
484 for ( int y = 0; y < REAL_SIZE; ++y )
485 {
8ef08948 486 wxAlphaPixelData::Iterator rowStart = p;
b3f04dc2
VZ
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 {
b068dfe2 494 // note that RGB must be premultiplied by alpha
8ef08948 495 unsigned a = (wxAlphaPixelData::Iterator::ChannelType)((x*255.)/REAL_SIZE);
5c99abbe
MW
496 p.Red() = r * a / 256;
497 p.Green() = g * a / 256;
498 p.Blue() = b * a / 256;
b068dfe2 499 p.Alpha() = a;
b3f04dc2
VZ
500
501 ++p; // same as p.OffsetX(1)
502 }
503
504 p = rowStart;
1d2f48b6 505 p.OffsetY(data, 1);
b3f04dc2
VZ
506 }
507 }
508
8ef08948
RD
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
b3f04dc2
VZ
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);
8ef08948
RD
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());
b3f04dc2
VZ
551 }
552
553private:
554 wxBitmap m_bitmap;
8ef08948 555 wxBitmap m_alphaBitmap;
b3f04dc2
VZ
556
557 DECLARE_EVENT_TABLE()
558};
559
560#endif // wxHAVE_RAW_BITMAP
561
01111366 562
09ddabf7
FM
563// ============================================================================
564// implementations
565// ============================================================================
01111366 566
09ddabf7
FM
567//-----------------------------------------------------------------------------
568// MyImageFrame
569//-----------------------------------------------------------------------------
01111366 570
ab0f0386 571BEGIN_EVENT_TABLE(MyImageFrame, wxFrame)
0c543b7a
VZ
572 EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground)
573 EVT_PAINT(MyImageFrame::OnPaint)
1d8acb7d
VZ
574
575 EVT_MENU(wxID_SAVE, MyImageFrame::OnSave)
576 EVT_MENU_RANGE(ID_ROTATE_LEFT, ID_ROTATE_RIGHT, MyImageFrame::OnRotate)
05a8831a 577 EVT_MENU(ID_RESIZE, MyImageFrame::OnResize)
a2bbedf0
VZ
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)
ab0f0386
VZ
582END_EVENT_TABLE()
583
09ddabf7
FM
584//-----------------------------------------------------------------------------
585// MyRawBitmapFrame
586//-----------------------------------------------------------------------------
587
b3f04dc2
VZ
588#ifdef wxHAVE_RAW_BITMAP
589
590BEGIN_EVENT_TABLE(MyRawBitmapFrame, wxFrame)
591 EVT_PAINT(MyRawBitmapFrame::OnPaint)
592END_EVENT_TABLE()
593
594#endif // wxHAVE_RAW_BITMAP
595
09ddabf7 596//-----------------------------------------------------------------------------
01111366 597// MyFrame
09ddabf7 598//-----------------------------------------------------------------------------
01111366 599
b3f04dc2
VZ
600enum
601{
91b07357
JS
602 ID_QUIT = wxID_EXIT,
603 ID_ABOUT = wxID_ABOUT,
604 ID_NEW = 100,
37ba70a5 605 ID_INFO,
95706ac1
VZ
606 ID_SHOWRAW,
607 ID_SHOWTHUMBNAIL
b3f04dc2 608};
01111366
RR
609
610IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
37ba70a5 611BEGIN_EVENT_TABLE(MyFrame, wxFrame)
09ddabf7
FM
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)
b3f04dc2 617#ifdef wxHAVE_RAW_BITMAP
09ddabf7 618 EVT_MENU (ID_SHOWRAW, MyFrame::OnTestRawBitmap)
b3f04dc2 619#endif
dd38c875 620#if wxUSE_CLIPBOARD
da9df1f5
VZ
621 EVT_MENU(wxID_COPY, MyFrame::OnCopy)
622 EVT_MENU(wxID_PASTE, MyFrame::OnPaste)
623#endif // wxUSE_CLIPBOARD
01111366
RR
624END_EVENT_TABLE()
625
1d5b7a0b 626MyFrame::MyFrame()
09ddabf7
FM
627 : wxFrame( (wxFrame *)NULL, wxID_ANY, _T("wxImage sample"),
628 wxPoint(20, 20), wxSize(950, 700) )
01111366 629{
41f02b9a
FM
630 SetIcon(wxICON(sample));
631
09ddabf7 632 wxMenuBar *menu_bar = new wxMenuBar();
da9df1f5 633
09ddabf7
FM
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"));
b3f04dc2 637#ifdef wxHAVE_RAW_BITMAP
09ddabf7
FM
638 menuImage->AppendSeparator();
639 menuImage->Append( ID_SHOWRAW, _T("Test &raw bitmap...\tCtrl-R"));
b3f04dc2 640#endif
09ddabf7
FM
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"));
da9df1f5 649
dd38c875 650#if wxUSE_CLIPBOARD
09ddabf7
FM
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"));
da9df1f5 655#endif // wxUSE_CLIPBOARD
3d05544e 656
09ddabf7 657 SetMenuBar( menu_bar );
1d5b7a0b 658
8520f137 659#if wxUSE_STATUSBAR
09ddabf7
FM
660 CreateStatusBar(2);
661 int widths[] = { -1, 100 };
662 SetStatusWidths( 2, widths );
8520f137 663#endif // wxUSE_STATUSBAR
1d5b7a0b 664
09ddabf7 665 m_canvas = new MyCanvas( this, wxID_ANY, wxPoint(0,0), wxSize(10,10) );
cbd4be25 666
09ddabf7
FM
667 // 500 width * 2750 height
668 m_canvas->SetScrollbars( 10, 10, 50, 275 );
01111366
RR
669}
670
671void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
672{
09ddabf7 673 Close( true );
01111366
RR
674}
675
676void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
677{
a2bbedf0
VZ
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 );
fb1585ae
RR
683}
684
37ba70a5 685wxString MyFrame::LoadUserImage(wxImage& image)
ab0f0386 686{
37ba70a5
VZ
687 wxString filename;
688
71307412 689#if wxUSE_FILEDLG
37ba70a5
VZ
690 filename = wxFileSelector(_T("Select image file"));
691 if ( !filename.empty() )
692 {
693 if ( !image.LoadFile(filename) )
694 {
695 wxLogError(_T("Couldn't load image from '%s'."), filename.c_str());
f6bcfd97 696
37ba70a5
VZ
697 return wxEmptyString;
698 }
699 }
700#endif // wxUSE_FILEDLG
701
702 return filename;
703}
704
705void MyFrame::OnNewFrame( wxCommandEvent &WXUNUSED(event) )
706{
f6bcfd97 707 wxImage image;
37ba70a5
VZ
708 wxString filename = LoadUserImage(image);
709 if ( !filename.empty() )
85fcb94f 710 new MyImageFrame(this, filename, image);
37ba70a5
VZ
711}
712
713void MyFrame::OnImageInfo( wxCommandEvent &WXUNUSED(event) )
714{
715 wxImage image;
716 if ( !LoadUserImage(image).empty() )
f6bcfd97 717 {
37ba70a5
VZ
718 // TODO: show more information about the file
719 wxString info = wxString::Format("Image size: %dx%d",
09ddabf7
FM
720 image.GetWidth(),
721 image.GetHeight());
37ba70a5
VZ
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
f6bcfd97 733
37ba70a5
VZ
734 case wxIMAGE_RESOLUTION_NONE:
735 info += " in default units";
736 break;
f6bcfd97 737
37ba70a5
VZ
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 }
ab0f0386
VZ
750}
751
b3f04dc2
VZ
752#ifdef wxHAVE_RAW_BITMAP
753
481721e8 754void MyFrame::OnTestRawBitmap( wxCommandEvent &WXUNUSED(event) )
b3f04dc2
VZ
755{
756 (new MyRawBitmapFrame(this))->Show();
757}
758
759#endif // wxHAVE_RAW_BITMAP
760
dd38c875 761#if wxUSE_CLIPBOARD
da9df1f5
VZ
762
763void MyFrame::OnCopy(wxCommandEvent& WXUNUSED(event))
764{
765 wxBitmapDataObject *dobjBmp = new wxBitmapDataObject;
bd981f27 766 dobjBmp->SetBitmap(m_canvas->my_horse_png);
da9df1f5 767
dd38c875
MB
768 wxTheClipboard->Open();
769
da9df1f5
VZ
770 if ( !wxTheClipboard->SetData(dobjBmp) )
771 {
772 wxLogError(_T("Failed to copy bitmap to clipboard"));
773 }
dd38c875
MB
774
775 wxTheClipboard->Close();
da9df1f5
VZ
776}
777
778void MyFrame::OnPaste(wxCommandEvent& WXUNUSED(event))
779{
780 wxBitmapDataObject dobjBmp;
dd38c875
MB
781
782 wxTheClipboard->Open();
da9df1f5
VZ
783 if ( !wxTheClipboard->GetData(dobjBmp) )
784 {
785 wxLogMessage(_T("No bitmap data in the clipboard"));
786 }
787 else
788 {
85fcb94f 789 new MyImageFrame(this, _T("Clipboard"), dobjBmp.GetBitmap());
da9df1f5 790 }
dd38c875 791 wxTheClipboard->Close();
da9df1f5
VZ
792}
793
794#endif // wxUSE_CLIPBOARD
795
95706ac1
VZ
796void MyFrame::OnThumbnail( wxCommandEvent &WXUNUSED(event) )
797{
798#if wxUSE_FILEDLG
799 wxString filename = wxFileSelector(_T("Select image file"));
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
85fcb94f 819 MyImageFrame * const frame = new MyImageFrame(this, filename, image);
95706ac1
VZ
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
09ddabf7
FM
827//-----------------------------------------------------------------------------
828// MyApp
829//-----------------------------------------------------------------------------
830
831IMPLEMENT_APP(MyApp)
832
833bool 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}