]> git.saurik.com Git - wxWidgets.git/blame - samples/image/image.cpp
Got Penguin sample running under Windows.
[wxWidgets.git] / samples / image / image.cpp
CommitLineData
01111366
RR
1/*
2 * Program: image
3 *
4 * Author: Robert Roebling
5 *
6 * Copyright: (C) 1998, Robert Roebling
7 *
8 */
9
3d05544e
JS
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
01111366 18#include "wx/wx.h"
3d05544e
JS
19#endif
20
01111366
RR
21#include "wx/image.h"
22
23// derived classes
24
25class MyFrame;
26class MyApp;
27
28// MyCanvas
29
30class 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;
e8fdc264 42 wxBitmap *my_square;
01111366
RR
43
44 DECLARE_EVENT_TABLE()
45};
46
47// MyFrame
48
49class 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
67class MyApp: public wxApp
68{
69 public:
70
71 MyApp(void);
72 virtual bool OnInit(void);
73};
74
75// main program
76
77IMPLEMENT_APP(MyApp)
78
79// MyCanvas
80
81IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow)
82
83BEGIN_EVENT_TABLE(MyCanvas,wxScrolledWindow)
84 EVT_PAINT (MyCanvas::OnPaint)
85END_EVENT_TABLE()
86
87MyCanvas::MyCanvas( wxWindow *parent, const wxWindowID id, const wxPoint &pos, const wxSize &size )
88 : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER )
89{
3d05544e
JS
90 SetBackgroundColour(* wxWHITE);
91
dc86cb34
RR
92 wxBitmap bitmap( 100, 100 );
93
94 wxMemoryDC dc;
95 dc.SelectObject( bitmap );
910276d7 96 dc.SetBrush( wxBrush( "orange", wxSOLID ) );
e8fdc264 97 dc.SetPen( *wxWHITE_PEN );
dc86cb34
RR
98 dc.DrawRectangle( 0, 0, 100, 100 );
99 dc.SelectObject( wxNullBitmap );
3d05544e
JS
100
101 wxString dir("");
102
103#ifdef __WXGTK__
104 dir = wxString("../");
105#endif
106
8b53e5a2 107 wxImage image( bitmap );
3d05544e 108 image.SaveFile( dir + wxString("test.png"), wxBITMAP_TYPE_PNG );
910276d7 109
3d05544e 110 image.LoadFile( dir + wxString("horse.png"), wxBITMAP_TYPE_PNG );
8b53e5a2 111 my_horse = new wxBitmap( image.ConvertToBitmap() );
e8fdc264 112
3d05544e 113 image.LoadFile( dir + wxString("test.png"), wxBITMAP_TYPE_PNG );
8b53e5a2 114 my_square = new wxBitmap( image.ConvertToBitmap() );
01111366
RR
115}
116
117MyCanvas::~MyCanvas(void)
118{
119 delete my_horse;
e8fdc264 120 delete my_square;
01111366
RR
121}
122
123void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
124{
125 wxPaintDC dc( this );
126 PrepareDC( dc );
127
910276d7 128 dc.DrawText( "Loaded image", 30, 100 );
e8fdc264 129 if (my_square->Ok()) dc.DrawBitmap( *my_square, 30, 120 );
910276d7
RR
130
131 dc.DrawText( "Drawn directly", 150, 100 );
132 dc.SetBrush( wxBrush( "orange", wxSOLID ) );
e8fdc264 133 dc.SetPen( *wxWHITE_PEN );
910276d7 134 dc.DrawRectangle( 150, 120, 100, 100 );
e8fdc264
RR
135
136 if (my_horse->Ok()) dc.DrawBitmap( *my_horse, 30, 240 );
01111366
RR
137}
138
139// MyFrame
140
79490c3d
VZ
141const int ID_QUIT = 108;
142const int ID_ABOUT = 109;
01111366
RR
143
144IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
145
146BEGIN_EVENT_TABLE(MyFrame,wxFrame)
147 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
148 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
fb1585ae 149 EVT_SIZE (MyFrame::OnSize)
01111366
RR
150END_EVENT_TABLE()
151
152MyFrame::MyFrame(void) :
910276d7 153 wxFrame( (wxFrame *) NULL, -1, "wxImage sample", wxPoint(20,20), wxSize(470,360) )
01111366
RR
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");
3d05544e 161
01111366
RR
162 SetMenuBar( menu_bar );
163
917e533b
RR
164 CreateStatusBar(2);
165 int widths[] = { -1, 100 };
166 SetStatusWidths( 2, widths );
167
fb1585ae 168 m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
01111366
RR
169 m_canvas->SetScrollbars( 10, 10, 50, 50 );
170}
171
172void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
173{
174 Close( TRUE );
175}
176
177void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
178{
910276d7 179 (void) wxMessageBox( "wxImage demo\nRobert Roebling (c) 1998", "About wxImage Demo", wxOK );
01111366
RR
180}
181
fb1585ae
RR
182void MyFrame::OnSize( wxSizeEvent &WXUNUSED(event) )
183{
184 int w,h;
185 GetClientSize( &w, &h );
3d05544e
JS
186 if (m_canvas)
187 m_canvas->SetSize( w, h );
fb1585ae
RR
188}
189
01111366
RR
190//-----------------------------------------------------------------------------
191// MyApp
192//-----------------------------------------------------------------------------
193
194MyApp::MyApp(void) :
195 wxApp( )
196{
197}
198
199bool 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