]> git.saurik.com Git - wxWidgets.git/blame - samples/image/image.cpp
by default the ctrl accepts keyboard input now (why wouldn't it?)
[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
RR
21#include "wx/image.h"
22
23// derived classes
24
25class MyFrame;
26class MyApp;
27
28// MyCanvas
29
30class MyCanvas: public wxScrolledWindow
31{
1d5b7a0b
VZ
32public:
33 MyCanvas() {};
01111366 34 MyCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size );
1d5b7a0b 35 ~MyCanvas();
01111366 36 void OnPaint( wxPaintEvent &event );
9390a202 37 void CreateAntiAliasedBitmap();
1d5b7a0b 38
01111366 39 wxBitmap *my_horse;
e8fdc264 40 wxBitmap *my_square;
9390a202 41 wxBitmap *my_anti;
1d5b7a0b
VZ
42
43 DECLARE_DYNAMIC_CLASS(MyCanvas)
44 DECLARE_EVENT_TABLE()
01111366
RR
45};
46
47// MyFrame
48
49class MyFrame: public wxFrame
50{
1d5b7a0b
VZ
51public:
52 MyFrame();
01111366 53
01111366
RR
54 void OnAbout( wxCommandEvent &event );
55 void OnQuit( wxCommandEvent &event );
1d5b7a0b 56
01111366 57 MyCanvas *m_canvas;
1d5b7a0b
VZ
58
59 DECLARE_DYNAMIC_CLASS(MyFrame)
60 DECLARE_EVENT_TABLE()
01111366
RR
61};
62
63// MyApp
64
65class MyApp: public wxApp
66{
1d5b7a0b
VZ
67public:
68 virtual bool OnInit();
01111366
RR
69};
70
71// main program
72
73IMPLEMENT_APP(MyApp)
74
75// MyCanvas
76
77IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow)
78
1d5b7a0b
VZ
79BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
80 EVT_PAINT(MyCanvas::OnPaint)
01111366
RR
81END_EVENT_TABLE()
82
1d5b7a0b
VZ
83MyCanvas::MyCanvas( wxWindow *parent, const wxWindowID id,
84 const wxPoint &pos, const wxSize &size )
85 : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER )
01111366 86{
3d05544e
JS
87 SetBackgroundColour(* wxWHITE);
88
dc86cb34 89 wxBitmap bitmap( 100, 100 );
1d5b7a0b 90
dc86cb34
RR
91 wxMemoryDC dc;
92 dc.SelectObject( bitmap );
910276d7 93 dc.SetBrush( wxBrush( "orange", wxSOLID ) );
e8fdc264 94 dc.SetPen( *wxWHITE_PEN );
dc86cb34
RR
95 dc.DrawRectangle( 0, 0, 100, 100 );
96 dc.SelectObject( wxNullBitmap );
3d05544e
JS
97
98 wxString dir("");
99
100#ifdef __WXGTK__
101 dir = wxString("../");
102#endif
103
8b53e5a2 104 wxImage image( bitmap );
3d05544e 105 image.SaveFile( dir + wxString("test.png"), wxBITMAP_TYPE_PNG );
b75867a6 106
3d05544e 107 image.LoadFile( dir + wxString("horse.png"), wxBITMAP_TYPE_PNG );
8b53e5a2 108 my_horse = new wxBitmap( image.ConvertToBitmap() );
b75867a6 109
3d05544e 110 image.LoadFile( dir + wxString("test.png"), wxBITMAP_TYPE_PNG );
8b53e5a2 111 my_square = new wxBitmap( image.ConvertToBitmap() );
9390a202
RR
112
113 CreateAntiAliasedBitmap();
01111366
RR
114}
115
1d5b7a0b 116MyCanvas::~MyCanvas()
01111366
RR
117{
118 delete my_horse;
e8fdc264 119 delete my_square;
9390a202 120 delete my_anti;
01111366
RR
121}
122
123void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
124{
125 wxPaintDC dc( this );
126 PrepareDC( dc );
127
9390a202
RR
128 dc.DrawText( "Loaded image", 30, 10 );
129 if (my_square->Ok()) dc.DrawBitmap( *my_square, 30, 30 );
1d5b7a0b 130
9390a202 131 dc.DrawText( "Drawn directly", 150, 10 );
910276d7 132 dc.SetBrush( wxBrush( "orange", wxSOLID ) );
e8fdc264 133 dc.SetPen( *wxWHITE_PEN );
9390a202
RR
134 dc.DrawRectangle( 150, 30, 100, 100 );
135
136 if (my_horse->Ok()) dc.DrawBitmap( *my_horse, 30, 140 );
137
138 if (my_anti->Ok()) dc.DrawBitmap( *my_anti, 250, 140 );
139}
1d5b7a0b 140
9390a202
RR
141void MyCanvas::CreateAntiAliasedBitmap()
142{
143 wxBitmap bitmap( 300, 300 );
144 wxMemoryDC dc;
145 dc.SelectObject( bitmap );
146
147 dc.SetPen( *wxTRANSPARENT_PEN );
148
149 dc.Clear();
150
151 dc.SetFont( wxFont( 24, wxDECORATIVE, wxDEFAULT, wxDEFAULT ) );
152 dc.SetTextForeground( "RED" );
153 dc.DrawText( "This is anti-aliased Text.", 20, 20 );
154 dc.DrawText( "And a Rectangle.", 20, 60 );
155
156 dc.SetBrush( *wxRED_BRUSH );
157 dc.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
158
159 wxImage original( bitmap );
160 wxImage anti( 150, 150 );
161
162 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
163
164 for (int y = 1; y < 149; y++)
165 for (int x = 1; x < 149; x++)
166 {
167 int red = original.GetRed( x*2, y*2 ) +
168 original.GetRed( x*2-1, y*2 ) +
169 original.GetRed( x*2, y*2+1 ) +
170 original.GetRed( x*2+1, y*2+1 );
171 red = red/4;
172
173 int green = original.GetGreen( x*2, y*2 ) +
174 original.GetGreen( x*2-1, y*2 ) +
175 original.GetGreen( x*2, y*2+1 ) +
176 original.GetGreen( x*2+1, y*2+1 );
177 green = green/4;
178
179 int blue = original.GetBlue( x*2, y*2 ) +
180 original.GetBlue( x*2-1, y*2 ) +
181 original.GetBlue( x*2, y*2+1 ) +
182 original.GetBlue( x*2+1, y*2+1 );
183 blue = blue/4;
184 anti.SetRGB( x, y, red, green, blue );
185 }
186 my_anti = new wxBitmap( anti.ConvertToBitmap() );
01111366
RR
187}
188
189// MyFrame
190
79490c3d
VZ
191const int ID_QUIT = 108;
192const int ID_ABOUT = 109;
01111366
RR
193
194IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
195
196BEGIN_EVENT_TABLE(MyFrame,wxFrame)
197 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
198 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
199END_EVENT_TABLE()
200
1d5b7a0b
VZ
201MyFrame::MyFrame()
202 : wxFrame( (wxFrame *)NULL, -1, "wxImage sample",
203 wxPoint(20,20), wxSize(470,360) )
01111366
RR
204{
205 wxMenu *file_menu = new wxMenu();
206 file_menu->Append( ID_ABOUT, "About..");
207 file_menu->Append( ID_QUIT, "Exit");
1d5b7a0b 208
01111366
RR
209 wxMenuBar *menu_bar = new wxMenuBar();
210 menu_bar->Append(file_menu, "File");
3d05544e 211
01111366 212 SetMenuBar( menu_bar );
1d5b7a0b 213
917e533b
RR
214 CreateStatusBar(2);
215 int widths[] = { -1, 100 };
216 SetStatusWidths( 2, widths );
1d5b7a0b 217
fb1585ae 218 m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
01111366
RR
219 m_canvas->SetScrollbars( 10, 10, 50, 50 );
220}
221
222void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
223{
224 Close( TRUE );
225}
226
227void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
228{
1d5b7a0b
VZ
229 (void)wxMessageBox( "wxImage demo\n"
230 "Robert Roebling (c) 1998",
231 "About wxImage Demo", wxICON_INFORMATION | wxOK );
fb1585ae
RR
232}
233
01111366
RR
234//-----------------------------------------------------------------------------
235// MyApp
236//-----------------------------------------------------------------------------
237
1d5b7a0b 238bool MyApp::OnInit()
01111366
RR
239{
240 wxImage::AddHandler( new wxPNGHandler );
1d5b7a0b 241
01111366
RR
242 wxFrame *frame = new MyFrame();
243 frame->Show( TRUE );
1d5b7a0b 244
01111366
RR
245 return TRUE;
246}
247