patch from Dimitri fixing a few memory leaks and unTABbing the sources
[wxWidgets.git] / samples / image / image.cpp
1 /*
2 * Program: image
3 *
4 * Author: Robert Roebling
5 *
6 * Copyright: (C) 1998, Robert Roebling
7 *
8 */
9
10 // For compilers that support precompilation, includes "wx/wx.h".
11 #include "wx/wxprec.h"
12
13 #ifdef __BORLANDC__
14 #pragma hdrstop
15 #endif
16
17 #ifndef WX_PRECOMP
18 #include "wx/wx.h"
19 #endif
20
21 #include "wx/image.h"
22 #include "wx/file.h"
23 #include "wx/mstream.h"
24 #include "wx/wfstream.h"
25 #include "wx/quantize.h"
26
27 #include "smile.xbm"
28 #include "smile.xpm"
29
30
31 // derived classes
32
33 class MyFrame;
34 class MyApp;
35
36 // MyCanvas
37
38 class MyCanvas: public wxScrolledWindow
39 {
40 public:
41 MyCanvas() {};
42 MyCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size );
43 ~MyCanvas();
44 void OnPaint( wxPaintEvent &event );
45 void CreateAntiAliasedBitmap();
46
47 wxBitmap *my_horse_png;
48 wxBitmap *my_horse_jpeg;
49 wxBitmap *my_horse_gif;
50 wxBitmap *my_horse_bmp;
51 wxBitmap *my_horse_pcx;
52 wxBitmap *my_horse_pnm;
53 wxBitmap *my_horse_tiff;
54 wxBitmap *my_horse_xpm;
55 wxBitmap *my_horse_ico32;
56 wxBitmap *my_horse_ico16;
57 wxBitmap *my_horse_ico;
58 wxBitmap *my_horse_cur;
59
60 wxBitmap *my_smile_xbm;
61 wxBitmap *my_square;
62 wxBitmap *my_anti;
63
64 int xH, yH ;
65
66 protected:
67 wxBitmap m_bmpSmileXpm;
68 wxIcon m_iconSmileXpm;
69
70 private:
71 DECLARE_DYNAMIC_CLASS(MyCanvas)
72 DECLARE_EVENT_TABLE()
73 };
74
75
76 const int nChoices = 8 ;
77 static const wxString bppchoices[nChoices] =
78 {
79 "1 bpp color",
80 "1 bpp B&W",
81 "4 bpp color",
82 "8 bpp color",
83 "8 bpp greyscale",
84 "8 bpp red",
85 "8 bpp own palette",
86 "24 bpp"
87 };
88
89 static const int bppvalues[nChoices] =
90 {
91 wxBMP_1BPP,
92 wxBMP_1BPP_BW,
93 wxBMP_4BPP,
94 wxBMP_8BPP,
95 wxBMP_8BPP_GREY,
96 wxBMP_8BPP_RED,
97 wxBMP_8BPP_PALETTE,
98 wxBMP_24BPP
99 };
100
101 // MyFrame
102
103
104 class MyFrame: public wxFrame
105 {
106 public:
107 MyFrame();
108
109 void OnAbout( wxCommandEvent &event );
110 void OnNewFrame( wxCommandEvent &event );
111 void OnQuit( wxCommandEvent &event );
112
113 MyCanvas *m_canvas;
114
115 private:
116 DECLARE_DYNAMIC_CLASS(MyFrame)
117 DECLARE_EVENT_TABLE()
118 };
119
120 class MyImageFrame : public wxFrame
121 {
122 public:
123 MyImageFrame(wxFrame *parent, const wxBitmap& bitmap)
124 : wxFrame(parent, -1, _T("Double click to save"),
125 wxDefaultPosition, wxDefaultSize,
126 wxCAPTION | wxSYSTEM_MENU),
127 m_bitmap(bitmap)
128 {
129 SetClientSize(bitmap.GetWidth(), bitmap.GetHeight());
130 }
131
132 void OnPaint(wxPaintEvent& WXUNUSED(event))
133 {
134 wxPaintDC dc( this );
135 //TRUE for masked images
136 dc.DrawBitmap( m_bitmap, 0, 0, TRUE );
137 }
138
139 void OnSave(wxCommandEvent& WXUNUSED(event))
140 {
141 wxImage image(m_bitmap);
142
143 int bppselection = wxGetSingleChoiceIndex("Set BMP BPP",
144 "Set BMP BPP",
145 nChoices,
146 bppchoices,
147 this);
148 if ( bppselection == -1 )
149 {
150 // cancelled
151 return;
152 }
153
154 image.SetOption(wxIMAGE_OPTION_BMP_FORMAT, bppvalues[bppselection]);
155
156 wxString deffilename = bppchoices[bppselection];
157 deffilename.Replace(wxT(" "), wxT("_"));
158 deffilename += wxT(".bmp");
159 wxString savefilename = wxFileSelector( wxT("Save Image"),
160 wxT(""),
161 deffilename,
162 (const wxChar *)NULL,
163 wxT("BMP files (*.bmp)|*.bmp|")
164 wxT("PNG files (*.png)|*.png|")
165 wxT("JPEG files (*.jpg)|*.jpg|")
166 wxT("GIF files (*.gif)|*.gif|")
167 wxT("TIFF files (*.tif)|*.tif|")
168 wxT("PCX files (*.pcx)|*.pcx|")
169 wxT("ICO files (*.ico)|*.ico|")
170 wxT("CUR files (*.cur)|*.cur"),
171 wxSAVE);
172
173 if ( savefilename.empty() )
174 return;
175
176 if ( image.GetOptionInt(wxIMAGE_OPTION_BMP_FORMAT) == wxBMP_8BPP_PALETTE )
177 {
178 unsigned char *cmap = new unsigned char [256];
179 for ( int i = 0; i < 256; i++ )
180 cmap[i] = i;
181 image.SetPalette(wxPalette(256, cmap, cmap, cmap));
182
183 delete cmap;
184 }
185
186 bool loaded;
187 wxString extension = savefilename.AfterLast('.').Lower();
188
189 if (extension == "cur")
190 {
191 image.Rescale(32,32);
192 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, 0);
193 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, 0);
194 // This shows how you can save an image with explicitly
195 // specified image format:
196 loaded = image.SaveFile(savefilename, wxBITMAP_TYPE_CUR);
197 }
198 else
199 {
200 // This one guesses image format from filename extension
201 // (it may fail if the extension is not recognized):
202 loaded = image.SaveFile(savefilename);
203 }
204
205 if ( !loaded )
206 wxMessageBox("No handler for this file type.",
207 "File was not saved",
208 wxOK|wxCENTRE, this);
209 }
210
211 private:
212 wxBitmap m_bitmap;
213
214 DECLARE_EVENT_TABLE()
215 };
216
217 // MyApp
218
219 class MyApp: public wxApp
220 {
221 public:
222 virtual bool OnInit();
223 };
224
225 // main program
226
227 IMPLEMENT_APP(MyApp)
228
229 // MyCanvas
230
231 IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow)
232
233 BEGIN_EVENT_TABLE(MyImageFrame, wxFrame)
234 EVT_PAINT(MyImageFrame::OnPaint)
235 EVT_LEFT_DCLICK(MyImageFrame::OnSave)
236 END_EVENT_TABLE()
237
238 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
239 EVT_PAINT(MyCanvas::OnPaint)
240 END_EVENT_TABLE()
241
242 MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id,
243 const wxPoint &pos, const wxSize &size )
244 : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER )
245 #if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
246 , m_bmpSmileXpm((const char **) smile_xpm)
247 , m_iconSmileXpm((const char **) smile_xpm)
248 #endif
249 {
250 my_horse_png = (wxBitmap*) NULL;
251 my_horse_jpeg = (wxBitmap*) NULL;
252 my_horse_gif = (wxBitmap*) NULL;
253 my_horse_bmp = (wxBitmap*) NULL;
254 my_horse_pcx = (wxBitmap*) NULL;
255 my_horse_pnm = (wxBitmap*) NULL;
256 my_horse_tiff = (wxBitmap*) NULL;
257 my_horse_xpm = (wxBitmap*) NULL;
258 my_horse_ico32 = (wxBitmap*) NULL;
259 my_horse_ico16 = (wxBitmap*) NULL;
260 my_horse_ico = (wxBitmap*) NULL;
261 my_horse_cur = (wxBitmap*) NULL;
262
263 my_smile_xbm = (wxBitmap*) NULL;
264 my_square = (wxBitmap*) NULL;
265 my_anti = (wxBitmap*) NULL;
266
267 SetBackgroundColour(* wxWHITE);
268
269 wxBitmap bitmap( 100, 100 );
270
271 wxMemoryDC dc;
272 dc.SelectObject( bitmap );
273 dc.SetBrush( wxBrush( wxT("orange"), wxSOLID ) );
274 dc.SetPen( *wxBLACK_PEN );
275 dc.DrawRectangle( 0, 0, 100, 100 );
276 dc.SetBrush( *wxWHITE_BRUSH );
277 dc.DrawRectangle( 20, 20, 60, 60 );
278 dc.SelectObject( wxNullBitmap );
279
280 // try to find the directory with our images
281 wxString dir;
282 if ( wxFile::Exists(wxT("./horse.png")) )
283 dir = "./";
284 else if ( wxFile::Exists(wxT("../horse.png")) )
285 dir = "../";
286 else
287 wxLogWarning(wxT("Can't find image files in either '.' or '..'!"));
288
289 wxImage image = bitmap.ConvertToImage();
290
291 #if wxUSE_LIBPNG
292 if ( !image.SaveFile( dir + wxString("test.png"), wxBITMAP_TYPE_PNG ))
293 wxLogError(wxT("Can't save file"));
294
295 image.Destroy();
296
297 image.LoadFile( dir + wxString("test.png") );
298 my_square = new wxBitmap( image );
299
300 image.Destroy();
301
302 if ( !image.LoadFile( dir + wxString("horse.png")) )
303 wxLogError(wxT("Can't load PNG image"));
304 else
305 my_horse_png = new wxBitmap( image );
306 #endif // wxUSE_LIBPNG
307
308 #if wxUSE_LIBJPEG
309 image.Destroy();
310
311 if ( !image.LoadFile( dir + wxString("horse.jpg")) )
312 wxLogError(wxT("Can't load JPG image"));
313 else
314 my_horse_jpeg = new wxBitmap( image );
315 #endif // wxUSE_LIBJPEG
316
317 #if wxUSE_GIF
318 image.Destroy();
319
320 if ( !image.LoadFile( dir + wxString("horse.gif")))
321 //if ( !image.LoadFile( wxString("\\slidbar.gif"), wxBITMAP_TYPE_GIF, -2) )
322 wxLogError(wxT("Can't load GIF image"));
323 else
324 my_horse_gif = new wxBitmap( image );
325 #endif
326
327 #if wxUSE_PCX
328 image.Destroy();
329
330 if ( !image.LoadFile( dir + wxString("horse.pcx"), wxBITMAP_TYPE_PCX ) )
331 wxLogError(wxT("Can't load PCX image"));
332 else
333 my_horse_pcx = new wxBitmap( image );
334 #endif
335
336 image.Destroy();
337
338 if ( !image.LoadFile( dir + wxString("horse.bmp"), wxBITMAP_TYPE_BMP ) )
339 wxLogError(wxT("Can't load BMP image"));
340 else
341 my_horse_bmp = new wxBitmap( image );
342
343 #if wxUSE_XPM
344 image.Destroy();
345
346 if ( !image.LoadFile( dir + wxString("horse.xpm"), wxBITMAP_TYPE_XPM ) )
347 wxLogError(wxT("Can't load XPM image"));
348 else
349 my_horse_xpm = new wxBitmap( image );
350
351 if ( !image.SaveFile( dir + wxString("test.xpm"), wxBITMAP_TYPE_XPM ))
352 wxLogError(wxT("Can't save file"));
353 #endif
354
355 #if wxUSE_PNM
356 image.Destroy();
357
358 if ( !image.LoadFile( dir + wxString("horse.pnm"), wxBITMAP_TYPE_PNM ) )
359 wxLogError(wxT("Can't load PNM image"));
360 else
361 my_horse_pnm = new wxBitmap( image );
362 #endif
363
364 #if wxUSE_LIBTIFF
365 image.Destroy();
366
367 if ( !image.LoadFile( dir + wxString("horse.tif"), wxBITMAP_TYPE_TIF ) )
368 wxLogError(wxT("Can't load TIFF image"));
369 else
370 my_horse_tiff = new wxBitmap( image );
371 #endif
372
373 CreateAntiAliasedBitmap();
374
375 my_smile_xbm = new wxBitmap( (const char*)smile_bits, smile_width,
376 smile_height, 1 );
377
378 #if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
379 // demonstrates XPM automatically using the mask when saving
380 if ( m_bmpSmileXpm.Ok() )
381 m_bmpSmileXpm.SaveFile("saved.xpm", wxBITMAP_TYPE_XPM);
382 #endif
383
384 #if wxUSE_ICO_CUR
385 image.Destroy();
386
387 if ( !image.LoadFile( dir + wxString("horse.ico"), wxBITMAP_TYPE_ICO, 0 ) )
388 wxLogError(wxT("Can't load first ICO image"));
389 else
390 my_horse_ico32 = new wxBitmap( image );
391
392 image.Destroy();
393
394 if ( !image.LoadFile( dir + wxString("horse.ico"), wxBITMAP_TYPE_ICO, 1 ) )
395 wxLogError(wxT("Can't load second ICO image"));
396 else
397 my_horse_ico16 = new wxBitmap( image );
398
399 image.Destroy();
400
401 if ( !image.LoadFile( dir + wxString("horse.ico") ) )
402 wxLogError(wxT("Can't load best ICO image"));
403 else
404 my_horse_ico = new wxBitmap( image );
405
406 image.Destroy();
407
408 if ( !image.LoadFile( dir + wxString("horse.cur"), wxBITMAP_TYPE_CUR ) )
409 wxLogError(wxT("Can't load best ICO image"));
410 else
411 {
412 my_horse_cur = new wxBitmap( image );
413 xH = 30 + image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X) ;
414 yH = 2420 + image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y) ;
415 }
416
417 #endif
418
419 }
420
421 MyCanvas::~MyCanvas()
422 {
423 delete my_horse_pnm;
424 delete my_horse_png;
425 delete my_horse_jpeg;
426 delete my_horse_gif;
427 delete my_horse_bmp;
428 delete my_horse_pcx;
429 delete my_horse_tiff;
430 delete my_horse_xpm;
431 delete my_horse_ico32;
432 delete my_horse_ico16;
433 delete my_horse_ico;
434 delete my_horse_cur;
435 delete my_smile_xbm;
436 delete my_square;
437 delete my_anti;
438 }
439
440 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
441 {
442 wxPaintDC dc( this );
443 PrepareDC( dc );
444
445 dc.DrawText( "Loaded image", 30, 10 );
446 if (my_square && my_square->Ok()) dc.DrawBitmap( *my_square, 30, 30 );
447
448 dc.DrawText( "Drawn directly", 150, 10 );
449 dc.SetBrush( wxBrush( wxT("orange"), wxSOLID ) );
450 dc.SetPen( *wxBLACK_PEN );
451 dc.DrawRectangle( 150, 30, 100, 100 );
452 dc.SetBrush( *wxWHITE_BRUSH );
453 dc.DrawRectangle( 170, 50, 60, 60 );
454
455 if (my_anti && my_anti->Ok())
456 dc.DrawBitmap( *my_anti, 280, 30 );
457
458 dc.DrawText( "PNG handler", 30, 135 );
459 if (my_horse_png && my_horse_png->Ok())
460 {
461 dc.DrawBitmap( *my_horse_png, 30, 150 );
462 wxRect rect(0,0,100,100);
463 wxBitmap sub( my_horse_png->GetSubBitmap(rect) );
464 dc.DrawText( "GetSubBitmap()", 280, 190 );
465 dc.DrawBitmap( sub, 280, 210 );
466 }
467
468 dc.DrawText( "JPEG handler", 30, 365 );
469 if (my_horse_jpeg && my_horse_jpeg->Ok())
470 dc.DrawBitmap( *my_horse_jpeg, 30, 380 );
471
472 dc.DrawText( "GIF handler", 30, 595 );
473 if (my_horse_gif && my_horse_gif->Ok())
474 dc.DrawBitmap( *my_horse_gif, 30, 610 );
475
476 dc.DrawText( "PCX handler", 30, 825 );
477 if (my_horse_pcx && my_horse_pcx->Ok())
478 dc.DrawBitmap( *my_horse_pcx, 30, 840 );
479
480 dc.DrawText( "BMP handler", 30, 1055 );
481 if (my_horse_bmp && my_horse_bmp->Ok())
482 dc.DrawBitmap( *my_horse_bmp, 30, 1070 );
483
484 dc.DrawText( "PNM handler", 30, 1285 );
485 if (my_horse_pnm && my_horse_pnm->Ok())
486 dc.DrawBitmap( *my_horse_pnm, 30, 1300 );
487
488 dc.DrawText( "TIFF handler", 30, 1515 );
489 if (my_horse_tiff && my_horse_tiff->Ok())
490 dc.DrawBitmap( *my_horse_tiff, 30, 1530 );
491
492 dc.DrawText( "XPM handler", 30, 1745 );
493 if (my_horse_xpm && my_horse_xpm->Ok())
494 dc.DrawBitmap( *my_horse_xpm, 30, 1760 );
495
496
497 if (my_smile_xbm && my_smile_xbm->Ok())
498 {
499 dc.DrawText( "XBM bitmap", 30, 1975 );
500 dc.DrawText( "(green on red)", 30, 1990 );
501 dc.SetTextForeground( wxT("GREEN") );
502 dc.SetTextBackground( wxT("RED") );
503 dc.DrawBitmap( *my_smile_xbm, 30, 2010 );
504
505 dc.SetTextForeground( wxT("BLACK") );
506 dc.DrawText( "After wxImage conversion", 150, 1975 );
507 dc.DrawText( "(red on white)", 150, 1990 );
508 dc.SetTextForeground( wxT("RED") );
509 wxImage i = my_smile_xbm->ConvertToImage();
510 i.SetMaskColour( 255, 255, 255 );
511 i.Replace( 0, 0, 0,
512 wxRED_PEN->GetColour().Red(),
513 wxRED_PEN->GetColour().Green(),
514 wxRED_PEN->GetColour().Blue() );
515 dc.DrawBitmap( i.ConvertToBitmap(), 150, 2010, TRUE );
516 dc.SetTextForeground( wxT("BLACK") );
517 }
518
519
520 wxBitmap mono( 60,50,1 );
521 wxMemoryDC memdc;
522 memdc.SelectObject( mono );
523 memdc.SetPen( *wxBLACK_PEN );
524 memdc.SetBrush( *wxWHITE_BRUSH );
525 memdc.DrawRectangle( 0,0,60,50 );
526 memdc.SetTextForeground( *wxBLACK );
527 memdc.DrawText( "Hi!", 5, 5 );
528 memdc.SetBrush( *wxBLACK_BRUSH );
529 memdc.DrawRectangle( 33,5,20,20 );
530 memdc.SetPen( *wxRED_PEN );
531 memdc.DrawLine( 5, 42, 50, 42 );
532 memdc.SelectObject( wxNullBitmap );
533
534 if (mono.Ok())
535 {
536 dc.DrawText( "Mono bitmap", 30, 2095 );
537 dc.DrawText( "(red on green)", 30, 2110 );
538 dc.SetTextForeground( wxT("RED") );
539 dc.SetTextBackground( wxT("GREEN") );
540 dc.DrawBitmap( mono, 30, 2130 );
541
542 dc.SetTextForeground( wxT("BLACK") );
543 dc.DrawText( "After wxImage conversion", 150, 2095 );
544 dc.DrawText( "(red on white)", 150, 2110 );
545 dc.SetTextForeground( wxT("RED") );
546 wxImage i = mono.ConvertToImage();
547 i.SetMaskColour( 255,255,255 );
548 i.Replace( 0,0,0,
549 wxRED_PEN->GetColour().Red(),
550 wxRED_PEN->GetColour().Green(),
551 wxRED_PEN->GetColour().Blue() );
552 dc.DrawBitmap( i.ConvertToBitmap(), 150, 2130, TRUE );
553 dc.SetTextForeground( wxT("BLACK") );
554 }
555
556 dc.DrawText("XPM bitmap", 30, 2230);
557 if ( m_bmpSmileXpm.Ok() )
558 {
559 dc.DrawBitmap(m_bmpSmileXpm, 30, 2250, TRUE);
560 }
561
562 dc.DrawText("XPM icon", 150, 2230);
563 if ( m_iconSmileXpm.Ok() )
564 {
565 dc.DrawIcon(m_iconSmileXpm, 150, 2250);
566 }
567
568 dc.DrawText( "ICO handler (1st image)", 30, 2290 );
569 if (my_horse_ico32 && my_horse_ico32->Ok())
570 dc.DrawBitmap( *my_horse_ico32, 30, 2330, TRUE );
571
572 dc.DrawText( "ICO handler (2nd image)", 230, 2290 );
573 if (my_horse_ico16 && my_horse_ico16->Ok())
574 dc.DrawBitmap( *my_horse_ico16, 230, 2330, TRUE );
575
576 dc.DrawText( "ICO handler (best image)", 430, 2290 );
577 if (my_horse_ico && my_horse_ico->Ok())
578 dc.DrawBitmap( *my_horse_ico, 430, 2330, TRUE );
579
580 dc.DrawText( "CUR handler", 30, 2390 );
581 if (my_horse_cur && my_horse_cur->Ok())
582 {
583 dc.DrawBitmap( *my_horse_cur, 30, 2420, TRUE );
584 dc.SetPen (*wxRED_PEN);
585 dc.DrawLine (xH-10,yH,xH+10,yH);
586 dc.DrawLine (xH,yH-10,xH,yH+10);
587 }
588 }
589
590 void MyCanvas::CreateAntiAliasedBitmap()
591 {
592 wxBitmap bitmap( 300, 300 );
593
594 wxMemoryDC dc;
595
596 dc.SelectObject( bitmap );
597
598 dc.Clear();
599
600 dc.SetFont( wxFont( 24, wxDECORATIVE, wxNORMAL, wxNORMAL) );
601 dc.SetTextForeground( wxT("RED") );
602 dc.DrawText( "This is anti-aliased Text.", 20, 20 );
603 dc.DrawText( "And a Rectangle.", 20, 60 );
604
605 dc.SetBrush( *wxRED_BRUSH );
606 dc.SetPen( *wxTRANSPARENT_PEN );
607 dc.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
608
609 wxImage original= bitmap.ConvertToImage();
610 wxImage anti( 150, 150 );
611
612 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
613
614 for (int y = 1; y < 149; y++)
615 for (int x = 1; x < 149; x++)
616 {
617 int red = original.GetRed( x*2, y*2 ) +
618 original.GetRed( x*2-1, y*2 ) +
619 original.GetRed( x*2, y*2+1 ) +
620 original.GetRed( x*2+1, y*2+1 );
621 red = red/4;
622
623 int green = original.GetGreen( x*2, y*2 ) +
624 original.GetGreen( x*2-1, y*2 ) +
625 original.GetGreen( x*2, y*2+1 ) +
626 original.GetGreen( x*2+1, y*2+1 );
627 green = green/4;
628
629 int blue = original.GetBlue( x*2, y*2 ) +
630 original.GetBlue( x*2-1, y*2 ) +
631 original.GetBlue( x*2, y*2+1 ) +
632 original.GetBlue( x*2+1, y*2+1 );
633 blue = blue/4;
634 anti.SetRGB( x, y, red, green, blue );
635 }
636 my_anti = new wxBitmap( anti.ConvertToBitmap() );
637 }
638
639 // MyFrame
640
641 const int ID_QUIT = 108;
642 const int ID_ABOUT = 109;
643 const int ID_NEW = 110;
644
645 IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
646
647 BEGIN_EVENT_TABLE(MyFrame,wxFrame)
648 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
649 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
650 EVT_MENU (ID_NEW, MyFrame::OnNewFrame)
651 END_EVENT_TABLE()
652
653 MyFrame::MyFrame()
654 : wxFrame( (wxFrame *)NULL, -1, "wxImage sample",
655 wxPoint(20,20), wxSize(470,360) )
656 {
657 wxMenu *file_menu = new wxMenu();
658 file_menu->Append( ID_NEW, "&Show image...");
659 file_menu->AppendSeparator();
660 file_menu->Append( ID_ABOUT, "&About...");
661 file_menu->AppendSeparator();
662 file_menu->Append( ID_QUIT, "E&xit");
663
664 wxMenuBar *menu_bar = new wxMenuBar();
665 menu_bar->Append(file_menu, "&File");
666
667 SetMenuBar( menu_bar );
668
669 CreateStatusBar(2);
670 int widths[] = { -1, 100 };
671 SetStatusWidths( 2, widths );
672
673 m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
674
675 // 500 width * 2500 height
676 m_canvas->SetScrollbars( 10, 10, 50, 250 );
677 }
678
679 void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
680 {
681 Close( TRUE );
682 }
683
684 void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
685 {
686 (void)wxMessageBox( "wxImage demo\n"
687 "Robert Roebling (c) 1998,2000",
688 "About wxImage Demo", wxICON_INFORMATION | wxOK );
689 }
690
691 void MyFrame::OnNewFrame( wxCommandEvent &WXUNUSED(event) )
692 {
693 wxString filename = wxFileSelector(_T("Select image file"));
694 if ( !filename )
695 return;
696
697 wxImage image;
698 if ( !image.LoadFile(filename) )
699 {
700 wxLogError(_T("Couldn't load image from '%s'."), filename.c_str());
701
702 return;
703 }
704
705 (new MyImageFrame(this, image.ConvertToBitmap()))->Show();
706 }
707
708 //-----------------------------------------------------------------------------
709 // MyApp
710 //-----------------------------------------------------------------------------
711
712 bool MyApp::OnInit()
713 {
714 #if wxUSE_LIBPNG
715 wxImage::AddHandler( new wxPNGHandler );
716 #endif
717
718 #if wxUSE_LIBJPEG
719 wxImage::AddHandler( new wxJPEGHandler );
720 #endif
721
722 #if wxUSE_LIBTIFF
723 wxImage::AddHandler( new wxTIFFHandler );
724 #endif
725
726 #if wxUSE_GIF
727 wxImage::AddHandler( new wxGIFHandler );
728 #endif
729
730 #if wxUSE_PCX
731 wxImage::AddHandler( new wxPCXHandler );
732 #endif
733
734 #if wxUSE_PNM
735 wxImage::AddHandler( new wxPNMHandler );
736 #endif
737
738 #if wxUSE_XPM
739 wxImage::AddHandler( new wxXPMHandler );
740 #endif
741
742 #if wxUSE_ICO_CUR
743 wxImage::AddHandler( new wxICOHandler );
744 wxImage::AddHandler( new wxCURHandler );
745 #endif
746
747 wxFrame *frame = new MyFrame();
748 frame->Show( TRUE );
749
750 return TRUE;
751 }
752