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