]> git.saurik.com Git - wxWidgets.git/blob - samples/image/image.cpp
Fix to parser.y to make it compile with makefile.unx; wxFileConfig
[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 #include "wx/wx.h"
11 #include "wx/image.h"
12
13 // derived classes
14
15 class MyFrame;
16 class MyApp;
17
18 // MyCanvas
19
20 class MyCanvas: public wxScrolledWindow
21 {
22 DECLARE_DYNAMIC_CLASS(MyCanvas)
23
24 public:
25
26 MyCanvas(void) {};
27 MyCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size );
28 ~MyCanvas(void);
29 void OnPaint( wxPaintEvent &event );
30
31 wxBitmap *my_horse;
32 wxBitmap *my_square;
33
34 DECLARE_EVENT_TABLE()
35 };
36
37 // MyFrame
38
39 class MyFrame: public wxFrame
40 {
41 DECLARE_DYNAMIC_CLASS(MyFrame)
42
43 public:
44
45 MyFrame(void);
46 void OnSize( wxSizeEvent &event );
47 void OnAbout( wxCommandEvent &event );
48 void OnQuit( wxCommandEvent &event );
49
50 MyCanvas *m_canvas;
51
52 DECLARE_EVENT_TABLE()
53 };
54
55 // MyApp
56
57 class MyApp: public wxApp
58 {
59 public:
60
61 MyApp(void);
62 virtual bool OnInit(void);
63 };
64
65 // main program
66
67 IMPLEMENT_APP(MyApp)
68
69 // MyCanvas
70
71 IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow)
72
73 BEGIN_EVENT_TABLE(MyCanvas,wxScrolledWindow)
74 EVT_PAINT (MyCanvas::OnPaint)
75 END_EVENT_TABLE()
76
77 MyCanvas::MyCanvas( wxWindow *parent, const wxWindowID id, const wxPoint &pos, const wxSize &size )
78 : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER )
79 {
80 wxImage image;
81
82 wxBitmap bitmap( 100, 100 );
83
84 wxMemoryDC dc;
85 dc.SelectObject( bitmap );
86 dc.SetBrush( wxBrush( "orange", wxSOLID ) );
87 dc.SetPen( *wxWHITE_PEN );
88 dc.DrawRectangle( 0, 0, 100, 100 );
89 dc.SelectObject( wxNullBitmap );
90
91 image = bitmap.ConvertToImage();
92 image.SaveFile( "../test.png", wxBITMAP_TYPE_PNG );
93
94 image.LoadFile( "../horse.png", wxBITMAP_TYPE_PNG );
95 my_horse = new wxBitmap( image );
96
97 image.LoadFile( "../test.png", wxBITMAP_TYPE_PNG );
98 my_square = new wxBitmap( image );
99 }
100
101 MyCanvas::~MyCanvas(void)
102 {
103 delete my_horse;
104 delete my_square;
105 }
106
107 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
108 {
109 wxPaintDC dc( this );
110 PrepareDC( dc );
111
112 dc.DrawText( "Loaded image", 30, 100 );
113 if (my_square->Ok()) dc.DrawBitmap( *my_square, 30, 120 );
114
115 dc.DrawText( "Drawn directly", 150, 100 );
116 dc.SetBrush( wxBrush( "orange", wxSOLID ) );
117 dc.SetPen( *wxWHITE_PEN );
118 dc.DrawRectangle( 150, 120, 100, 100 );
119
120 if (my_horse->Ok()) dc.DrawBitmap( *my_horse, 30, 240 );
121 }
122
123 // MyFrame
124
125 const ID_QUIT = 108;
126 const ID_ABOUT = 109;
127
128 IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
129
130 BEGIN_EVENT_TABLE(MyFrame,wxFrame)
131 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
132 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
133 EVT_SIZE (MyFrame::OnSize)
134 END_EVENT_TABLE()
135
136 MyFrame::MyFrame(void) :
137 wxFrame( (wxFrame *) NULL, -1, "wxImage sample", wxPoint(20,20), wxSize(470,360) )
138 {
139 wxMenu *file_menu = new wxMenu();
140 file_menu->Append( ID_ABOUT, "About..");
141 file_menu->Append( ID_QUIT, "Exit");
142
143 wxMenuBar *menu_bar = new wxMenuBar();
144 menu_bar->Append(file_menu, "File");
145 menu_bar->Show( TRUE );
146
147 SetMenuBar( menu_bar );
148
149 CreateStatusBar(2);
150 int widths[] = { -1, 100 };
151 SetStatusWidths( 2, widths );
152
153 m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
154 m_canvas->SetScrollbars( 10, 10, 50, 50 );
155 }
156
157 void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
158 {
159 Close( TRUE );
160 }
161
162 void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
163 {
164 (void) wxMessageBox( "wxImage demo\nRobert Roebling (c) 1998", "About wxImage Demo", wxOK );
165 }
166
167 void MyFrame::OnSize( wxSizeEvent &WXUNUSED(event) )
168 {
169 int w,h;
170 GetClientSize( &w, &h );
171 m_canvas->SetSize( w, h );
172 }
173
174 //-----------------------------------------------------------------------------
175 // MyApp
176 //-----------------------------------------------------------------------------
177
178 MyApp::MyApp(void) :
179 wxApp( )
180 {
181 }
182
183 bool MyApp::OnInit(void)
184 {
185 wxImage::AddHandler( new wxPNGHandler );
186
187 wxFrame *frame = new MyFrame();
188 frame->Show( TRUE );
189
190 return TRUE;
191 }
192
193
194
195
196