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