1. MSW message handling simplifications
[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
23 #include "wx/file.h"
24
25 // derived classes
26
27 class MyFrame;
28 class MyApp;
29
30 // MyCanvas
31
32 class MyCanvas: public wxScrolledWindow
33 {
34 public:
35 MyCanvas() {};
36 MyCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size );
37 ~MyCanvas();
38 void OnPaint( wxPaintEvent &event );
39 void CreateAntiAliasedBitmap();
40
41 wxBitmap *my_horse_png;
42 wxBitmap *my_horse_jpeg;
43 wxBitmap *my_horse_gif;
44 wxBitmap *my_square;
45 wxBitmap *my_anti;
46
47 DECLARE_DYNAMIC_CLASS(MyCanvas)
48 DECLARE_EVENT_TABLE()
49 };
50
51 // MyFrame
52
53 class MyFrame: public wxFrame
54 {
55 public:
56 MyFrame();
57
58 void OnAbout( wxCommandEvent &event );
59 void OnQuit( wxCommandEvent &event );
60
61 MyCanvas *m_canvas;
62
63 DECLARE_DYNAMIC_CLASS(MyFrame)
64 DECLARE_EVENT_TABLE()
65 };
66
67 // MyApp
68
69 class MyApp: public wxApp
70 {
71 public:
72 virtual bool OnInit();
73 };
74
75 // main program
76
77 IMPLEMENT_APP(MyApp)
78
79 // MyCanvas
80
81 IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow)
82
83 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
84 EVT_PAINT(MyCanvas::OnPaint)
85 END_EVENT_TABLE()
86
87 MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id,
88 const wxPoint &pos, const wxSize &size )
89 : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER )
90 {
91 my_horse_png = (wxBitmap*) NULL;
92 my_horse_jpeg = (wxBitmap*) NULL;
93 my_horse_gif = (wxBitmap*) NULL;
94 my_square = (wxBitmap*) NULL;
95 my_anti = (wxBitmap*) NULL;
96
97 SetBackgroundColour(* wxWHITE);
98
99 wxBitmap bitmap( 100, 100 );
100
101 wxMemoryDC dc;
102 dc.SelectObject( bitmap );
103 dc.SetBrush( wxBrush( "orange", wxSOLID ) );
104 dc.SetPen( *wxWHITE_PEN );
105 dc.DrawRectangle( 0, 0, 100, 100 );
106 dc.SelectObject( wxNullBitmap );
107
108 // try to find the directory with our images
109 wxString dir;
110 if ( wxFile::Exists("./horse.png") )
111 dir = "./";
112 else if ( wxFile::Exists("../horse.png") )
113 dir = "../";
114 else
115 wxLogWarning("Can't find image files in either '.' or '..'!");
116
117 wxImage image( bitmap );
118 if ( !image.SaveFile( dir + wxString("test.png"), wxBITMAP_TYPE_PNG ) )
119 wxLogError("Can't save file");
120
121 if ( !image.LoadFile( dir + wxString("horse.png"), wxBITMAP_TYPE_PNG ) )
122 wxLogError("Can't load PNG image");
123 else
124 my_horse_png = new wxBitmap( image.ConvertToBitmap() );
125
126 if ( !image.LoadFile( dir + wxString("horse.jpg"), wxBITMAP_TYPE_JPEG ) )
127 wxLogError("Can't load JPG image");
128 else
129 my_horse_jpeg = new wxBitmap( image.ConvertToBitmap() );
130
131 if ( !image.LoadFile( dir + wxString("horse.gif"), wxBITMAP_TYPE_GIF ) )
132 wxLogError("Can't load GIF image");
133 else
134 my_horse_gif = new wxBitmap( image.ConvertToBitmap() );
135
136 image.LoadFile( dir + wxString("test.png"), wxBITMAP_TYPE_PNG );
137 my_square = new wxBitmap( image.ConvertToBitmap() );
138
139 CreateAntiAliasedBitmap();
140 }
141
142 MyCanvas::~MyCanvas()
143 {
144 delete my_horse_png;
145 delete my_horse_jpeg;
146 delete my_horse_gif;
147 delete my_square;
148 delete my_anti;
149 }
150
151 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
152 {
153 wxPaintDC dc( this );
154 PrepareDC( dc );
155
156 dc.DrawText( "Loaded image", 30, 10 );
157 if (my_square && my_square->Ok()) dc.DrawBitmap( *my_square, 30, 30 );
158
159 dc.DrawText( "Drawn directly", 150, 10 );
160 dc.SetBrush( wxBrush( "orange", wxSOLID ) );
161 dc.SetPen( *wxWHITE_PEN );
162 dc.DrawRectangle( 150, 30, 100, 100 );
163
164 if (my_anti && my_anti->Ok()) dc.DrawBitmap( *my_anti, 250, 140 );
165
166 dc.DrawText( "PNG handler", 30, 135 );
167 if (my_horse_png && my_horse_png->Ok()) dc.DrawBitmap( *my_horse_png, 30, 150 );
168
169 dc.DrawText( "JPEG handler", 30, 365 );
170 if (my_horse_jpeg && my_horse_jpeg->Ok()) dc.DrawBitmap( *my_horse_jpeg, 30, 380 );
171
172 dc.DrawText( "GIF handler", 30, 595 );
173 if (my_horse_gif && my_horse_gif->Ok()) dc.DrawBitmap( *my_horse_gif, 30, 610 );
174 }
175
176 void MyCanvas::CreateAntiAliasedBitmap()
177 {
178 wxBitmap bitmap( 300, 300 );
179
180 wxMemoryDC dc;
181
182 dc.SelectObject( bitmap );
183
184 dc.Clear();
185
186 dc.SetFont( wxFont( 24, wxDECORATIVE, wxDEFAULT, wxDEFAULT ) );
187 dc.SetTextForeground( "RED" );
188 dc.DrawText( "This is anti-aliased Text.", 20, 20 );
189 dc.DrawText( "And a Rectangle.", 20, 60 );
190
191 dc.SetBrush( *wxRED_BRUSH );
192 dc.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
193
194 wxImage original( bitmap );
195 wxImage anti( 150, 150 );
196
197 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
198
199 for (int y = 1; y < 149; y++)
200 for (int x = 1; x < 149; x++)
201 {
202 int red = original.GetRed( x*2, y*2 ) +
203 original.GetRed( x*2-1, y*2 ) +
204 original.GetRed( x*2, y*2+1 ) +
205 original.GetRed( x*2+1, y*2+1 );
206 red = red/4;
207
208 int green = original.GetGreen( x*2, y*2 ) +
209 original.GetGreen( x*2-1, y*2 ) +
210 original.GetGreen( x*2, y*2+1 ) +
211 original.GetGreen( x*2+1, y*2+1 );
212 green = green/4;
213
214 int blue = original.GetBlue( x*2, y*2 ) +
215 original.GetBlue( x*2-1, y*2 ) +
216 original.GetBlue( x*2, y*2+1 ) +
217 original.GetBlue( x*2+1, y*2+1 );
218 blue = blue/4;
219 anti.SetRGB( x, y, red, green, blue );
220 }
221 my_anti = new wxBitmap( anti.ConvertToBitmap() );
222 }
223
224 // MyFrame
225
226 const int ID_QUIT = 108;
227 const int ID_ABOUT = 109;
228
229 IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
230
231 BEGIN_EVENT_TABLE(MyFrame,wxFrame)
232 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
233 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
234 END_EVENT_TABLE()
235
236 MyFrame::MyFrame()
237 : wxFrame( (wxFrame *)NULL, -1, "wxImage sample",
238 wxPoint(20,20), wxSize(470,360) )
239 {
240 wxMenu *file_menu = new wxMenu();
241 file_menu->Append( ID_ABOUT, "&About..");
242 file_menu->Append( ID_QUIT, "E&xit");
243
244 wxMenuBar *menu_bar = new wxMenuBar();
245 menu_bar->Append(file_menu, "&File");
246
247 SetMenuBar( menu_bar );
248
249 CreateStatusBar(2);
250 int widths[] = { -1, 100 };
251 SetStatusWidths( 2, widths );
252
253 m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
254 m_canvas->SetScrollbars( 10, 10, 50, 100 );
255 }
256
257 void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
258 {
259 Close( TRUE );
260 }
261
262 void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
263 {
264 (void)wxMessageBox( "wxImage demo\n"
265 "Robert Roebling (c) 1998",
266 "About wxImage Demo", wxICON_INFORMATION | wxOK );
267 }
268
269 //-----------------------------------------------------------------------------
270 // MyApp
271 //-----------------------------------------------------------------------------
272
273 bool MyApp::OnInit()
274 {
275 #if wxUSE_LIBPNG
276 wxImage::AddHandler( new wxPNGHandler );
277 #endif
278
279 #if wxUSE_LIBJPEG
280 wxImage::AddHandler( new wxJPEGHandler );
281 #endif
282
283 wxImage::AddHandler( new wxGIFHandler );
284
285 wxFrame *frame = new MyFrame();
286 frame->Show( TRUE );
287
288 return TRUE;
289 }
290