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