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