]> git.saurik.com Git - wxWidgets.git/blob - samples/image/image.cpp
3546f1f1e67ddb855ab9915f583301be3295a495
[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 my_horse = new wxBitmap( image.ConvertToBitmap() );
112
113 image.LoadFile( dir + wxString("test.png"), wxBITMAP_TYPE_PNG );
114 my_square = new wxBitmap( image.ConvertToBitmap() );
115 }
116
117 MyCanvas::~MyCanvas(void)
118 {
119 delete my_horse;
120 delete my_square;
121 }
122
123 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
124 {
125 wxPaintDC dc( this );
126 PrepareDC( dc );
127
128 dc.DrawText( "Loaded image", 30, 100 );
129 if (my_square->Ok()) dc.DrawBitmap( *my_square, 30, 120 );
130
131 dc.DrawText( "Drawn directly", 150, 100 );
132 dc.SetBrush( wxBrush( "orange", wxSOLID ) );
133 dc.SetPen( *wxWHITE_PEN );
134 dc.DrawRectangle( 150, 120, 100, 100 );
135
136 if (my_horse->Ok()) dc.DrawBitmap( *my_horse, 30, 240 );
137 }
138
139 // MyFrame
140
141 const int ID_QUIT = 108;
142 const int ID_ABOUT = 109;
143
144 IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
145
146 BEGIN_EVENT_TABLE(MyFrame,wxFrame)
147 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
148 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
149 EVT_SIZE (MyFrame::OnSize)
150 END_EVENT_TABLE()
151
152 MyFrame::MyFrame(void) :
153 wxFrame( (wxFrame *) NULL, -1, "wxImage sample", wxPoint(20,20), wxSize(470,360) )
154 {
155 wxMenu *file_menu = new wxMenu();
156 file_menu->Append( ID_ABOUT, "About..");
157 file_menu->Append( ID_QUIT, "Exit");
158
159 wxMenuBar *menu_bar = new wxMenuBar();
160 menu_bar->Append(file_menu, "File");
161
162 SetMenuBar( menu_bar );
163
164 CreateStatusBar(2);
165 int widths[] = { -1, 100 };
166 SetStatusWidths( 2, widths );
167
168 m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
169 m_canvas->SetScrollbars( 10, 10, 50, 50 );
170 }
171
172 void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
173 {
174 Close( TRUE );
175 }
176
177 void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
178 {
179 (void) wxMessageBox( "wxImage demo\nRobert Roebling (c) 1998", "About wxImage Demo", wxOK );
180 }
181
182 void MyFrame::OnSize( wxSizeEvent &WXUNUSED(event) )
183 {
184 int w,h;
185 GetClientSize( &w, &h );
186 if (m_canvas)
187 m_canvas->SetSize( w, h );
188 }
189
190 //-----------------------------------------------------------------------------
191 // MyApp
192 //-----------------------------------------------------------------------------
193
194 MyApp::MyApp(void) :
195 wxApp( )
196 {
197 }
198
199 bool MyApp::OnInit(void)
200 {
201 wxImage::AddHandler( new wxPNGHandler );
202
203 wxFrame *frame = new MyFrame();
204 frame->Show( TRUE );
205
206 return TRUE;
207 }
208