]> git.saurik.com Git - wxWidgets.git/blob - samples/image/image.cpp
12ca058f16ae5a77d90fb96ef821ddbf968a6191
[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 DECLARE_DYNAMIC_CLASS(MyCanvas)
33
34 public:
35
36 MyCanvas(void) {};
37 MyCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size );
38 ~MyCanvas(void);
39 void OnPaint( wxPaintEvent &event );
40
41 wxBitmap *my_horse;
42 wxBitmap *my_square;
43
44 DECLARE_EVENT_TABLE()
45 };
46
47 // MyFrame
48
49 class MyFrame: public wxFrame
50 {
51 DECLARE_DYNAMIC_CLASS(MyFrame)
52
53 public:
54
55 MyFrame(void);
56 void OnSize( wxSizeEvent &event );
57 void OnAbout( wxCommandEvent &event );
58 void OnQuit( wxCommandEvent &event );
59
60 MyCanvas *m_canvas;
61
62 DECLARE_EVENT_TABLE()
63 };
64
65 // MyApp
66
67 class MyApp: public wxApp
68 {
69 public:
70
71 MyApp(void);
72 virtual bool OnInit(void);
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, const wxWindowID id, const wxPoint &pos, const wxSize &size )
88 : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER )
89 {
90 SetBackgroundColour(* wxWHITE);
91
92 wxBitmap bitmap( 100, 100 );
93
94 wxMemoryDC dc;
95 dc.SelectObject( bitmap );
96 dc.SetBrush( wxBrush( "orange", wxSOLID ) );
97 dc.SetPen( *wxWHITE_PEN );
98 dc.DrawRectangle( 0, 0, 100, 100 );
99 dc.SelectObject( wxNullBitmap );
100
101 wxString dir("");
102
103 #ifdef __WXGTK__
104 dir = wxString("../");
105 #endif
106
107 wxImage image( bitmap );
108 image.SaveFile( dir + wxString("test.png"), wxBITMAP_TYPE_PNG );
109
110 image.LoadFile( dir + wxString("horse.png"), wxBITMAP_TYPE_PNG );
111 image.SetRGB( 0, 0, 250, 0, 0 );
112 image.SetRGB( 1, 0, 100, 100, 100 );
113 image.SetRGB( 2, 0, 250, 250, 250 );
114 my_horse = new wxBitmap( image.ConvertToBitmap() );
115
116 image.LoadFile( dir + wxString("test.png"), wxBITMAP_TYPE_PNG );
117 my_square = new wxBitmap( image.ConvertToBitmap() );
118 }
119
120 MyCanvas::~MyCanvas(void)
121 {
122 delete my_horse;
123 delete my_square;
124 }
125
126 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
127 {
128 wxPaintDC dc( this );
129 PrepareDC( dc );
130
131 dc.DrawText( "Loaded image", 30, 100 );
132 if (my_square->Ok()) dc.DrawBitmap( *my_square, 30, 120 );
133
134 dc.DrawText( "Drawn directly", 150, 100 );
135 dc.SetBrush( wxBrush( "orange", wxSOLID ) );
136 dc.SetPen( *wxWHITE_PEN );
137 dc.DrawRectangle( 150, 120, 100, 100 );
138
139 if (my_horse->Ok()) dc.DrawBitmap( *my_horse, 30, 240 );
140 }
141
142 // MyFrame
143
144 const int ID_QUIT = 108;
145 const int ID_ABOUT = 109;
146
147 IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
148
149 BEGIN_EVENT_TABLE(MyFrame,wxFrame)
150 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
151 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
152 EVT_SIZE (MyFrame::OnSize)
153 END_EVENT_TABLE()
154
155 MyFrame::MyFrame(void) :
156 wxFrame( (wxFrame *) NULL, -1, "wxImage sample", wxPoint(20,20), wxSize(470,360) )
157 {
158 wxMenu *file_menu = new wxMenu();
159 file_menu->Append( ID_ABOUT, "About..");
160 file_menu->Append( ID_QUIT, "Exit");
161
162 wxMenuBar *menu_bar = new wxMenuBar();
163 menu_bar->Append(file_menu, "File");
164
165 SetMenuBar( menu_bar );
166
167 CreateStatusBar(2);
168 int widths[] = { -1, 100 };
169 SetStatusWidths( 2, widths );
170
171 m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
172 m_canvas->SetScrollbars( 10, 10, 50, 50 );
173 }
174
175 void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
176 {
177 Close( TRUE );
178 }
179
180 void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
181 {
182 (void) wxMessageBox( "wxImage demo\nRobert Roebling (c) 1998", "About wxImage Demo", wxOK );
183 }
184
185 void MyFrame::OnSize( wxSizeEvent &WXUNUSED(event) )
186 {
187 int w,h;
188 GetClientSize( &w, &h );
189 if (m_canvas)
190 m_canvas->SetSize( w, h );
191 }
192
193 //-----------------------------------------------------------------------------
194 // MyApp
195 //-----------------------------------------------------------------------------
196
197 MyApp::MyApp(void) :
198 wxApp( )
199 {
200 }
201
202 bool MyApp::OnInit(void)
203 {
204 wxImage::AddHandler( new wxPNGHandler );
205
206 wxFrame *frame = new MyFrame();
207 frame->Show( TRUE );
208
209 return TRUE;
210 }
211