]> git.saurik.com Git - wxWidgets.git/blob - samples/image/image.cpp
I've put live into Vadim's wxNavigationKeyEvent idea
[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 wxBitmap bitmap( 100, 100 );
81
82 wxMemoryDC dc;
83 dc.SelectObject( bitmap );
84 dc.SetBrush( wxBrush( "orange", wxSOLID ) );
85 dc.SetPen( *wxWHITE_PEN );
86 dc.DrawRectangle( 0, 0, 100, 100 );
87 dc.SelectObject( wxNullBitmap );
88
89 wxImage image( bitmap );
90 image.SaveFile( "../test.png", wxBITMAP_TYPE_PNG );
91
92 image.LoadFile( "../horse.png", wxBITMAP_TYPE_PNG );
93 my_horse = new wxBitmap( image.ConvertToBitmap() );
94
95 image.LoadFile( "../test.png", wxBITMAP_TYPE_PNG );
96 my_square = new wxBitmap( image.ConvertToBitmap() );
97 }
98
99 MyCanvas::~MyCanvas(void)
100 {
101 delete my_horse;
102 delete my_square;
103 }
104
105 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
106 {
107 wxPaintDC dc( this );
108 PrepareDC( dc );
109
110 dc.DrawText( "Loaded image", 30, 100 );
111 if (my_square->Ok()) dc.DrawBitmap( *my_square, 30, 120 );
112
113 dc.DrawText( "Drawn directly", 150, 100 );
114 dc.SetBrush( wxBrush( "orange", wxSOLID ) );
115 dc.SetPen( *wxWHITE_PEN );
116 dc.DrawRectangle( 150, 120, 100, 100 );
117
118 if (my_horse->Ok()) dc.DrawBitmap( *my_horse, 30, 240 );
119 }
120
121 // MyFrame
122
123 const int ID_QUIT = 108;
124 const int ID_ABOUT = 109;
125
126 IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
127
128 BEGIN_EVENT_TABLE(MyFrame,wxFrame)
129 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
130 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
131 EVT_SIZE (MyFrame::OnSize)
132 END_EVENT_TABLE()
133
134 MyFrame::MyFrame(void) :
135 wxFrame( (wxFrame *) NULL, -1, "wxImage sample", wxPoint(20,20), wxSize(470,360) )
136 {
137 wxMenu *file_menu = new wxMenu();
138 file_menu->Append( ID_ABOUT, "About..");
139 file_menu->Append( ID_QUIT, "Exit");
140
141 wxMenuBar *menu_bar = new wxMenuBar();
142 menu_bar->Append(file_menu, "File");
143 menu_bar->Show( TRUE );
144
145 SetMenuBar( menu_bar );
146
147 CreateStatusBar(2);
148 int widths[] = { -1, 100 };
149 SetStatusWidths( 2, widths );
150
151 m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
152 m_canvas->SetScrollbars( 10, 10, 50, 50 );
153 }
154
155 void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
156 {
157 Close( TRUE );
158 }
159
160 void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
161 {
162 (void) wxMessageBox( "wxImage demo\nRobert Roebling (c) 1998", "About wxImage Demo", wxOK );
163 }
164
165 void MyFrame::OnSize( wxSizeEvent &WXUNUSED(event) )
166 {
167 int w,h;
168 GetClientSize( &w, &h );
169 m_canvas->SetSize( w, h );
170 }
171
172 //-----------------------------------------------------------------------------
173 // MyApp
174 //-----------------------------------------------------------------------------
175
176 MyApp::MyApp(void) :
177 wxApp( )
178 {
179 }
180
181 bool MyApp::OnInit(void)
182 {
183 wxImage::AddHandler( new wxPNGHandler );
184
185 wxFrame *frame = new MyFrame();
186 frame->Show( TRUE );
187
188 return TRUE;
189 }
190
191
192
193
194