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