]> git.saurik.com Git - wxWidgets.git/blob - samples/image/image.cpp
Changed setup.h so that global memory operators are switched off for GnuWin32;
[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 END_EVENT_TABLE()
134
135 MyFrame::MyFrame(void) :
136 wxFrame( (wxFrame *) NULL, -1, "wxImage sample", wxPoint(20,20), wxSize(470,360) )
137 {
138 wxMenu *file_menu = new wxMenu();
139 file_menu->Append( ID_ABOUT, "About..");
140 file_menu->Append( ID_QUIT, "Exit");
141
142 wxMenuBar *menu_bar = new wxMenuBar();
143 menu_bar->Append(file_menu, "File");
144 menu_bar->Show( TRUE );
145
146 SetMenuBar( menu_bar );
147
148 CreateStatusBar(2);
149 int widths[] = { -1, 100 };
150 SetStatusWidths( 2, widths );
151
152 m_canvas = new MyCanvas( this, -1, wxPoint(2,62), wxSize(300-4,120-4) );
153 m_canvas->SetScrollbars( 10, 10, 50, 50 );
154 }
155
156 void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
157 {
158 Close( TRUE );
159 }
160
161 void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
162 {
163 (void) wxMessageBox( "wxImage demo\nRobert Roebling (c) 1998", "About wxImage Demo", wxOK );
164 }
165
166 //-----------------------------------------------------------------------------
167 // MyApp
168 //-----------------------------------------------------------------------------
169
170 MyApp::MyApp(void) :
171 wxApp( )
172 {
173 }
174
175 bool MyApp::OnInit(void)
176 {
177 wxImage::AddHandler( new wxPNGHandler );
178
179 wxFrame *frame = new MyFrame();
180 frame->Show( TRUE );
181
182 return TRUE;
183 }
184
185
186
187
188