]> git.saurik.com Git - wxWidgets.git/blame - samples/image/image.cpp
Added conversion of menu labels (&->~)
[wxWidgets.git] / samples / image / image.cpp
CommitLineData
01111366
RR
1/*
2 * Program: image
1d5b7a0b 3 *
01111366
RR
4 * Author: Robert Roebling
5 *
6 * Copyright: (C) 1998, Robert Roebling
7 *
8 */
9
3d05544e
JS
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
01111366 18#include "wx/wx.h"
3d05544e
JS
19#endif
20
01111366 21#include "wx/image.h"
a23fd0e1
VZ
22#include "wx/file.h"
23
dc1dbfc6 24#include "smile.xbm"
bea56879 25#include "smile.xpm"
dc1dbfc6 26
01111366
RR
27// derived classes
28
29class MyFrame;
30class MyApp;
31
32// MyCanvas
33
34class MyCanvas: public wxScrolledWindow
35{
1d5b7a0b
VZ
36public:
37 MyCanvas() {};
01111366 38 MyCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size );
1d5b7a0b 39 ~MyCanvas();
01111366 40 void OnPaint( wxPaintEvent &event );
9390a202 41 void CreateAntiAliasedBitmap();
1d5b7a0b 42
e1929140
RR
43 wxBitmap *my_horse_png;
44 wxBitmap *my_horse_jpeg;
45 wxBitmap *my_horse_gif;
ca26177c 46 wxBitmap *my_horse_bmp;
cbd4be25 47 wxBitmap *my_horse_pcx;
6319afe3 48 wxBitmap *my_horse_pnm;
60a41aee 49 wxBitmap *my_horse_tiff;
dc1dbfc6 50 wxBitmap *my_smile_xbm;
e8fdc264 51 wxBitmap *my_square;
9390a202 52 wxBitmap *my_anti;
1d5b7a0b 53
bea56879
VZ
54protected:
55 wxBitmap m_bmpSmileXpm;
56 wxIcon m_iconSmileXpm;
57
60a41aee 58private:
1d5b7a0b
VZ
59 DECLARE_DYNAMIC_CLASS(MyCanvas)
60 DECLARE_EVENT_TABLE()
01111366
RR
61};
62
63// MyFrame
64
65class MyFrame: public wxFrame
66{
1d5b7a0b
VZ
67public:
68 MyFrame();
01111366 69
01111366 70 void OnAbout( wxCommandEvent &event );
ab0f0386 71 void OnNewFrame( wxCommandEvent &event );
01111366 72 void OnQuit( wxCommandEvent &event );
1d5b7a0b 73
01111366 74 MyCanvas *m_canvas;
1d5b7a0b 75
60a41aee 76private:
1d5b7a0b
VZ
77 DECLARE_DYNAMIC_CLASS(MyFrame)
78 DECLARE_EVENT_TABLE()
01111366
RR
79};
80
ab0f0386
VZ
81class MyImageFrame : public wxFrame
82{
83public:
84 MyImageFrame(wxFrame *parent, const wxBitmap& bitmap)
85 : wxFrame(parent, -1, _T("Frame with image"),
86 wxDefaultPosition, wxDefaultSize,
87 wxCAPTION),
88 m_bitmap(bitmap)
89 {
90 SetClientSize(bitmap.GetWidth(), bitmap.GetHeight());
91 }
92
93 void OnPaint(wxPaintEvent& WXUNUSED(event))
94 {
95 wxPaintDC dc( this );
96 dc.DrawBitmap( m_bitmap, 0, 0 );
97 }
98
99private:
100 wxBitmap m_bitmap;
101
102 DECLARE_EVENT_TABLE()
103};
104
01111366
RR
105// MyApp
106
107class MyApp: public wxApp
108{
1d5b7a0b
VZ
109public:
110 virtual bool OnInit();
01111366
RR
111};
112
113// main program
114
115IMPLEMENT_APP(MyApp)
116
117// MyCanvas
118
119IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow)
120
ab0f0386
VZ
121BEGIN_EVENT_TABLE(MyImageFrame, wxFrame)
122 EVT_PAINT(MyImageFrame::OnPaint)
123END_EVENT_TABLE()
124
1d5b7a0b
VZ
125BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
126 EVT_PAINT(MyCanvas::OnPaint)
01111366
RR
127END_EVENT_TABLE()
128
acbd13a3 129MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id,
1d5b7a0b 130 const wxPoint &pos, const wxSize &size )
81278df2
VZ
131 : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER )
132#if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
133 , m_bmpSmileXpm((const char **) smile_xpm)
134 , m_iconSmileXpm((const char **) smile_xpm)
135#endif
01111366 136{
60a41aee
RR
137 my_horse_png = (wxBitmap*) NULL;
138 my_horse_jpeg = (wxBitmap*) NULL;
139 my_horse_gif = (wxBitmap*) NULL;
140 my_horse_bmp = (wxBitmap*) NULL;
141 my_horse_pcx = (wxBitmap*) NULL;
142 my_horse_pnm = (wxBitmap*) NULL;
143 my_horse_tiff = (wxBitmap*) NULL;
dc1dbfc6 144 my_smile_xbm = (wxBitmap*) NULL;
60a41aee
RR
145 my_square = (wxBitmap*) NULL;
146 my_anti = (wxBitmap*) NULL;
147
148 SetBackgroundColour(* wxWHITE);
149
150 wxBitmap bitmap( 100, 100 );
151
152 wxMemoryDC dc;
153 dc.SelectObject( bitmap );
154 dc.SetBrush( wxBrush( "orange", wxSOLID ) );
cf3da716 155 dc.SetPen( *wxBLACK_PEN );
60a41aee 156 dc.DrawRectangle( 0, 0, 100, 100 );
cf3da716
RR
157 dc.SetBrush( *wxWHITE_BRUSH );
158 dc.DrawRectangle( 20, 20, 60, 60 );
60a41aee
RR
159 dc.SelectObject( wxNullBitmap );
160
161 // try to find the directory with our images
162 wxString dir;
163 if ( wxFile::Exists("./horse.png") )
81278df2 164 dir = "./";
60a41aee
RR
165 else if ( wxFile::Exists("../horse.png") )
166 dir = "../";
167 else
168 wxLogWarning("Can't find image files in either '.' or '..'!");
169
170 wxImage image( bitmap );
171
bea56879 172#if wxUSE_LIBPNG
176fe5af 173 if ( !image.SaveFile( dir + wxString("test.png"), wxBITMAP_TYPE_PNG ))
60a41aee 174 wxLogError("Can't save file");
279ababf 175
176fe5af 176 image.Destroy();
60a41aee 177
cc977e5f
RR
178 image.LoadFile( dir + wxString("test.png") );
179 my_square = new wxBitmap( image.ConvertToBitmap() );
180
176fe5af 181 image.Destroy();
cc977e5f 182
176fe5af 183 if ( !image.LoadFile( dir + wxString("horse.png")) )
60a41aee
RR
184 wxLogError("Can't load PNG image");
185 else
186 my_horse_png = new wxBitmap( image.ConvertToBitmap() );
bea56879 187#endif // wxUSE_LIBPNG
60a41aee 188
bea56879 189#if wxUSE_LIBJPEG
176fe5af 190 image.Destroy();
279ababf 191
176fe5af 192 if ( !image.LoadFile( dir + wxString("horse.jpg")) )
60a41aee
RR
193 wxLogError("Can't load JPG image");
194 else
195 my_horse_jpeg = new wxBitmap( image.ConvertToBitmap() );
bea56879 196#endif // wxUSE_LIBJPEG
6e47faf1
JS
197
198#if wxUSE_GIF
176fe5af 199 image.Destroy();
279ababf 200
176fe5af 201 if ( !image.LoadFile( dir + wxString("horse.gif")) )
60a41aee
RR
202 wxLogError("Can't load GIF image");
203 else
204 my_horse_gif = new wxBitmap( image.ConvertToBitmap() );
6e47faf1 205#endif
cbd4be25 206
6e47faf1 207#if wxUSE_PCX
176fe5af 208 image.Destroy();
279ababf 209
60a41aee
RR
210 if ( !image.LoadFile( dir + wxString("horse.pcx"), wxBITMAP_TYPE_PCX ) )
211 wxLogError("Can't load PCX image");
212 else
213 my_horse_pcx = new wxBitmap( image.ConvertToBitmap() );
6e47faf1 214#endif
cbd4be25 215
176fe5af 216 image.Destroy();
279ababf 217
60a41aee
RR
218 if ( !image.LoadFile( dir + wxString("horse.bmp"), wxBITMAP_TYPE_BMP ) )
219 wxLogError("Can't load BMP image");
220 else
221 my_horse_bmp = new wxBitmap( image.ConvertToBitmap() );
6319afe3 222
6e47faf1 223#if wxUSE_PNM
176fe5af 224 image.Destroy();
279ababf 225
60a41aee
RR
226 if ( !image.LoadFile( dir + wxString("horse.pnm"), wxBITMAP_TYPE_PNM ) )
227 wxLogError("Can't load PNM image");
228 else
229 my_horse_pnm = new wxBitmap( image.ConvertToBitmap() );
6e47faf1 230#endif
f048e32f 231
60a41aee 232#if wxUSE_LIBTIFF
176fe5af 233 image.Destroy();
279ababf 234
60a41aee
RR
235 if ( !image.LoadFile( dir + wxString("horse.tif"), wxBITMAP_TYPE_TIF ) )
236 wxLogError("Can't load TIFF image");
237 else
238 my_horse_tiff = new wxBitmap( image.ConvertToBitmap() );
239#endif
240
60a41aee 241 CreateAntiAliasedBitmap();
dc1dbfc6
RL
242
243 my_smile_xbm = new wxBitmap( (const char*)smile_bits, smile_width,
244 smile_height, 1 );
bfef20bf 245
279ababf 246#ifndef __WXGTK__
81278df2 247#if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
bfef20bf
GRG
248 // demonstrates XPM automatically using the mask when saving
249 if ( m_bmpSmileXpm.Ok() )
250 m_bmpSmileXpm.SaveFile("saved.xpm", wxBITMAP_TYPE_XPM);
81278df2 251#endif
279ababf 252#endif
01111366
RR
253}
254
1d5b7a0b 255MyCanvas::~MyCanvas()
01111366 256{
60a41aee
RR
257 delete my_horse_pnm;
258 delete my_horse_png;
259 delete my_horse_jpeg;
260 delete my_horse_gif;
261 delete my_horse_bmp;
262 delete my_horse_pcx;
263 delete my_horse_tiff;
dc1dbfc6 264 delete my_smile_xbm;
60a41aee
RR
265 delete my_square;
266 delete my_anti;
01111366
RR
267}
268
269void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
270{
be25e480
RR
271 wxPaintDC dc( this );
272 PrepareDC( dc );
a91b47e8 273
be25e480
RR
274 dc.DrawText( "Loaded image", 30, 10 );
275 if (my_square && my_square->Ok()) dc.DrawBitmap( *my_square, 30, 30 );
f048e32f 276
be25e480
RR
277 dc.DrawText( "Drawn directly", 150, 10 );
278 dc.SetBrush( wxBrush( "orange", wxSOLID ) );
cf3da716 279 dc.SetPen( *wxBLACK_PEN );
be25e480 280 dc.DrawRectangle( 150, 30, 100, 100 );
cf3da716
RR
281 dc.SetBrush( *wxWHITE_BRUSH );
282 dc.DrawRectangle( 170, 50, 60, 60 );
be25e480 283
408b4168 284 if (my_anti && my_anti->Ok())
be25e480
RR
285 dc.DrawBitmap( *my_anti, 280, 30 );
286
287 dc.DrawText( "PNG handler", 30, 135 );
288 if (my_horse_png && my_horse_png->Ok())
408b4168 289 {
be25e480
RR
290 dc.DrawBitmap( *my_horse_png, 30, 150 );
291 wxRect rect(0,0,100,100);
292 wxBitmap sub( my_horse_png->GetSubBitmap(rect) );
293 dc.DrawText( "GetSubBitmap()", 280, 190 );
294 dc.DrawBitmap( sub, 280, 210 );
295 }
296
297 dc.DrawText( "JPEG handler", 30, 365 );
408b4168 298 if (my_horse_jpeg && my_horse_jpeg->Ok())
be25e480
RR
299 dc.DrawBitmap( *my_horse_jpeg, 30, 380 );
300
301 dc.DrawText( "GIF handler", 30, 595 );
408b4168 302 if (my_horse_gif && my_horse_gif->Ok())
be25e480
RR
303 dc.DrawBitmap( *my_horse_gif, 30, 610 );
304
305 dc.DrawText( "PCX handler", 30, 825 );
408b4168 306 if (my_horse_pcx && my_horse_pcx->Ok())
be25e480
RR
307 dc.DrawBitmap( *my_horse_pcx, 30, 840 );
308
309 dc.DrawText( "BMP handler", 30, 1055 );
408b4168 310 if (my_horse_bmp && my_horse_bmp->Ok())
be25e480
RR
311 dc.DrawBitmap( *my_horse_bmp, 30, 1070 );
312
313 dc.DrawText( "PNM handler", 30, 1285 );
408b4168 314 if (my_horse_pnm && my_horse_pnm->Ok())
be25e480 315 dc.DrawBitmap( *my_horse_pnm, 30, 1300 );
408b4168 316
be25e480 317 dc.DrawText( "TIFF handler", 30, 1515 );
408b4168 318 if (my_horse_tiff && my_horse_tiff->Ok())
4b05d973 319 dc.DrawBitmap( *my_horse_tiff, 30, 1530 );
be25e480 320
408b4168 321 if (my_smile_xbm && my_smile_xbm->Ok())
be25e480
RR
322 {
323 dc.DrawText( "XBM bitmap", 30, 1745 );
176fe5af
GRG
324 dc.DrawText( "(green on red)", 30, 1760 );
325 dc.SetTextForeground( "GREEN" );
326 dc.SetTextBackground( "RED" );
327 dc.DrawBitmap( *my_smile_xbm, 30, 1780 );
408b4168 328
176fe5af 329 dc.SetTextForeground( "BLACK" );
be25e480 330 dc.DrawText( "After wxImage conversion", 150, 1745 );
176fe5af
GRG
331 dc.DrawText( "(red on white)", 150, 1760 );
332 dc.SetTextForeground( "RED" );
be25e480 333 wxImage i( *my_smile_xbm );
176fe5af
GRG
334 i.SetMaskColour( 255, 255, 255 );
335 i.Replace( 0, 0, 0,
be25e480
RR
336 wxRED_PEN->GetColour().Red(),
337 wxRED_PEN->GetColour().Green(),
338 wxRED_PEN->GetColour().Blue() );
176fe5af
GRG
339 dc.DrawBitmap( i.ConvertToBitmap(), 150, 1780, TRUE );
340 dc.SetTextForeground( "BLACK" );
be25e480 341 }
176fe5af 342
408b4168 343
41fbc841 344 wxBitmap mono( 60,50,1 );
82ea63e6
RR
345 wxMemoryDC memdc;
346 memdc.SelectObject( mono );
bfef20bf 347 memdc.SetPen( *wxBLACK_PEN );
82ea63e6 348 memdc.SetBrush( *wxWHITE_BRUSH );
41fbc841
RR
349 memdc.DrawRectangle( 0,0,60,50 );
350 memdc.SetTextForeground( *wxBLACK );
351 memdc.DrawText( "Hi!", 5, 5 );
352 memdc.SetBrush( *wxBLACK_BRUSH );
353 memdc.DrawRectangle( 33,5,20,20 );
354 memdc.SetPen( *wxRED_PEN );
355 memdc.DrawLine( 5, 42, 50, 42 );
82ea63e6 356 memdc.SelectObject( wxNullBitmap );
176fe5af 357
408b4168 358 if (mono.Ok())
82ea63e6 359 {
176fe5af
GRG
360 dc.DrawText( "Mono bitmap", 30, 1865 );
361 dc.DrawText( "(red on green)", 30, 1880 );
82ea63e6
RR
362 dc.SetTextForeground( "RED" );
363 dc.SetTextBackground( "GREEN" );
176fe5af 364 dc.DrawBitmap( mono, 30, 1900 );
408b4168 365
bea56879 366 dc.SetTextForeground( "BLACK" );
176fe5af
GRG
367 dc.DrawText( "After wxImage conversion", 150, 1865 );
368 dc.DrawText( "(red on white)", 150, 1880 );
bea56879 369 dc.SetTextForeground( "RED" );
82ea63e6 370 wxImage i( mono );
408b4168
VZ
371 i.SetMaskColour( 255,255,255 );
372 i.Replace( 0,0,0,
82ea63e6
RR
373 wxRED_PEN->GetColour().Red(),
374 wxRED_PEN->GetColour().Green(),
375 wxRED_PEN->GetColour().Blue() );
176fe5af 376 dc.DrawBitmap( i.ConvertToBitmap(), 150, 1900, TRUE );
bea56879
VZ
377 dc.SetTextForeground( "BLACK" );
378 }
379
176fe5af 380 dc.DrawText("XPM bitmap", 30, 2000);
bea56879
VZ
381 if ( m_bmpSmileXpm.Ok() )
382 {
176fe5af 383 dc.DrawBitmap(m_bmpSmileXpm, 30, 2020, TRUE);
bea56879
VZ
384 }
385
176fe5af 386 dc.DrawText("XPM icon", 150, 2000);
bea56879
VZ
387 if ( m_iconSmileXpm.Ok() )
388 {
176fe5af 389 dc.DrawIcon(m_iconSmileXpm, 150, 2020);
82ea63e6 390 }
9390a202 391}
1d5b7a0b 392
9390a202
RR
393void MyCanvas::CreateAntiAliasedBitmap()
394{
395 wxBitmap bitmap( 300, 300 );
a91b47e8 396
9390a202 397 wxMemoryDC dc;
a91b47e8 398
9390a202 399 dc.SelectObject( bitmap );
a91b47e8 400
9390a202 401 dc.Clear();
f048e32f 402
6e47faf1 403 dc.SetFont( wxFont( 24, wxDECORATIVE, wxNORMAL, wxNORMAL) );
9390a202
RR
404 dc.SetTextForeground( "RED" );
405 dc.DrawText( "This is anti-aliased Text.", 20, 20 );
406 dc.DrawText( "And a Rectangle.", 20, 60 );
f048e32f 407
9390a202 408 dc.SetBrush( *wxRED_BRUSH );
91b8de8d 409 dc.SetPen( *wxTRANSPARENT_PEN );
9390a202 410 dc.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
f048e32f 411
9390a202
RR
412 wxImage original( bitmap );
413 wxImage anti( 150, 150 );
a91b47e8 414
9390a202 415 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
f048e32f 416
9390a202
RR
417 for (int y = 1; y < 149; y++)
418 for (int x = 1; x < 149; x++)
419 {
420 int red = original.GetRed( x*2, y*2 ) +
f048e32f
VZ
421 original.GetRed( x*2-1, y*2 ) +
422 original.GetRed( x*2, y*2+1 ) +
9390a202 423 original.GetRed( x*2+1, y*2+1 );
f048e32f
VZ
424 red = red/4;
425
9390a202 426 int green = original.GetGreen( x*2, y*2 ) +
f048e32f
VZ
427 original.GetGreen( x*2-1, y*2 ) +
428 original.GetGreen( x*2, y*2+1 ) +
9390a202 429 original.GetGreen( x*2+1, y*2+1 );
f048e32f
VZ
430 green = green/4;
431
9390a202 432 int blue = original.GetBlue( x*2, y*2 ) +
f048e32f
VZ
433 original.GetBlue( x*2-1, y*2 ) +
434 original.GetBlue( x*2, y*2+1 ) +
9390a202 435 original.GetBlue( x*2+1, y*2+1 );
f048e32f 436 blue = blue/4;
9390a202
RR
437 anti.SetRGB( x, y, red, green, blue );
438 }
439 my_anti = new wxBitmap( anti.ConvertToBitmap() );
01111366
RR
440}
441
442// MyFrame
443
79490c3d
VZ
444const int ID_QUIT = 108;
445const int ID_ABOUT = 109;
ab0f0386 446const int ID_NEW = 110;
01111366
RR
447
448IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
449
450BEGIN_EVENT_TABLE(MyFrame,wxFrame)
451 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
452 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
ab0f0386 453 EVT_MENU (ID_NEW, MyFrame::OnNewFrame)
01111366
RR
454END_EVENT_TABLE()
455
1d5b7a0b
VZ
456MyFrame::MyFrame()
457 : wxFrame( (wxFrame *)NULL, -1, "wxImage sample",
458 wxPoint(20,20), wxSize(470,360) )
01111366
RR
459{
460 wxMenu *file_menu = new wxMenu();
ab0f0386
VZ
461 file_menu->Append( ID_NEW, "&New frame");
462 file_menu->AppendSeparator();
81278df2 463 file_menu->Append( ID_ABOUT, "&About...");
ab0f0386 464 file_menu->AppendSeparator();
90e58684 465 file_menu->Append( ID_QUIT, "E&xit");
1d5b7a0b 466
01111366 467 wxMenuBar *menu_bar = new wxMenuBar();
90e58684 468 menu_bar->Append(file_menu, "&File");
3d05544e 469
01111366 470 SetMenuBar( menu_bar );
1d5b7a0b 471
917e533b
RR
472 CreateStatusBar(2);
473 int widths[] = { -1, 100 };
474 SetStatusWidths( 2, widths );
1d5b7a0b 475
fb1585ae 476 m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
cbd4be25 477
bea56879 478 // 500 width * 2100 height
176fe5af 479 m_canvas->SetScrollbars( 10, 10, 50, 220 );
01111366
RR
480}
481
482void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
483{
484 Close( TRUE );
485}
486
487void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
488{
1d5b7a0b 489 (void)wxMessageBox( "wxImage demo\n"
41fbc841 490 "Robert Roebling (c) 1998,2000",
1d5b7a0b 491 "About wxImage Demo", wxICON_INFORMATION | wxOK );
fb1585ae
RR
492}
493
ab0f0386
VZ
494void MyFrame::OnNewFrame( wxCommandEvent &WXUNUSED(event) )
495{
496 (new MyImageFrame(this, *m_canvas->my_horse_bmp))->Show();
497}
498
01111366
RR
499//-----------------------------------------------------------------------------
500// MyApp
501//-----------------------------------------------------------------------------
502
1d5b7a0b 503bool MyApp::OnInit()
01111366 504{
e9c4b1a2
JS
505#if wxUSE_LIBPNG
506 wxImage::AddHandler( new wxPNGHandler );
507#endif
508
509#if wxUSE_LIBJPEG
510 wxImage::AddHandler( new wxJPEGHandler );
511#endif
512
60a41aee
RR
513#if wxUSE_LIBTIFF
514 wxImage::AddHandler( new wxTIFFHandler );
515#endif
516
6e47faf1 517#if wxUSE_GIF
e1929140 518 wxImage::AddHandler( new wxGIFHandler );
6e47faf1
JS
519#endif
520
521#if wxUSE_PCX
cbd4be25 522 wxImage::AddHandler( new wxPCXHandler );
6e47faf1
JS
523#endif
524
525#if wxUSE_PNM
6319afe3 526 wxImage::AddHandler( new wxPNMHandler );
6e47faf1 527#endif
e1929140 528
01111366
RR
529 wxFrame *frame = new MyFrame();
530 frame->Show( TRUE );
1d5b7a0b 531
01111366
RR
532 return TRUE;
533}
534