]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: pngdemo.cpp | |
3 | // Purpose: Demos PNG reading | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
6aa89a22 | 8 | // Copyright: (c) Julian Smart |
2f6c54eb | 9 | // Licence: wxWindows license |
c801d85f KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "pngdemo.h" | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
79144b8a | 23 | #include "wx/image.h" |
c801d85f KB |
24 | |
25 | #include "pngdemo.h" | |
26 | ||
c67daf87 UR |
27 | MyFrame *frame = (MyFrame *) NULL; |
28 | wxBitmap *g_TestBitmap = (wxBitmap *) NULL; | |
c801d85f KB |
29 | |
30 | IMPLEMENT_APP(MyApp) | |
31 | ||
32 | MyApp::MyApp() | |
33 | { | |
34 | } | |
35 | ||
36 | bool MyApp::OnInit(void) | |
37 | { | |
79144b8a | 38 | wxImage::AddHandler(new wxPNGHandler); |
c801d85f KB |
39 | |
40 | // Create the main frame window | |
600683ca | 41 | frame = new MyFrame((wxFrame *) NULL, _T("wxPNGBitmap Demo"), wxPoint(0, 0), wxSize(300, 300)); |
c801d85f | 42 | |
8520f137 | 43 | #if wxUSE_STATUSBAR |
c801d85f KB |
44 | // Give it a status line |
45 | frame->CreateStatusBar(2); | |
8520f137 | 46 | #endif // wxUSE_STATUSBAR |
c801d85f KB |
47 | |
48 | // Make a menubar | |
49 | wxMenu *file_menu = new wxMenu; | |
50 | wxMenu *help_menu = new wxMenu; | |
51 | ||
600683ca MB |
52 | file_menu->Append(PNGDEMO_LOAD_FILE, _T("&Load file"), _T("Load file")); |
53 | file_menu->Append(PNGDEMO_SAVE_FILE, _T("&Save file"), _T("Save file")); | |
54 | file_menu->Append(PNGDEMO_QUIT, _T("E&xit"), _T("Quit program")); | |
55 | help_menu->Append(PNGDEMO_ABOUT, _T("&About"), _T("About PNG demo")); | |
c801d85f KB |
56 | |
57 | wxMenuBar *menu_bar = new wxMenuBar; | |
58 | ||
600683ca MB |
59 | menu_bar->Append(file_menu, _T("&File")); |
60 | menu_bar->Append(help_menu, _T("&Help")); | |
c801d85f KB |
61 | |
62 | // Associate the menu bar with the frame | |
63 | frame->SetMenuBar(menu_bar); | |
64 | ||
65 | MyCanvas *canvas = new MyCanvas(frame, wxPoint(0, 0), wxSize(100, 100)); | |
66 | ||
67 | // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction | |
68 | // canvas->SetScrollbars(20, 20, 50, 50, 4, 4); | |
69 | frame->canvas = canvas; | |
70 | ||
bc2ec626 | 71 | frame->Show(true); |
c801d85f | 72 | |
8520f137 | 73 | #if wxUSE_STATUSBAR |
be5a51fb | 74 | frame->SetStatusText(_T("Hello, wxWidgets")); |
8520f137 | 75 | #endif // wxUSE_STATUSBAR |
c801d85f | 76 | |
bc2ec626 | 77 | return true; |
c801d85f KB |
78 | } |
79 | ||
80 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
81 | EVT_MENU(PNGDEMO_QUIT, MyFrame::OnQuit) | |
82 | EVT_MENU(PNGDEMO_ABOUT, MyFrame::OnAbout) | |
83 | EVT_MENU(PNGDEMO_LOAD_FILE, MyFrame::OnLoadFile) | |
cf7a7e13 | 84 | EVT_MENU(PNGDEMO_SAVE_FILE, MyFrame::OnSaveFile) |
c801d85f KB |
85 | END_EVENT_TABLE() |
86 | ||
87 | // Define my frame constructor | |
88 | MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size): | |
bc2ec626 | 89 | wxFrame(frame, wxID_ANY, title, pos, size) |
c801d85f | 90 | { |
c67daf87 | 91 | canvas = (MyCanvas *) NULL; |
c801d85f KB |
92 | } |
93 | ||
2f6c54eb VZ |
94 | // frame destructor |
95 | MyFrame::~MyFrame() | |
96 | { | |
97 | if (g_TestBitmap) | |
98 | { | |
99 | delete g_TestBitmap; | |
100 | g_TestBitmap = (wxBitmap *) NULL; | |
101 | } | |
102 | } | |
103 | ||
e3e65dac | 104 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
c801d85f | 105 | { |
bc2ec626 | 106 | Close(true); |
c801d85f KB |
107 | } |
108 | ||
e3e65dac | 109 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
c801d85f | 110 | { |
600683ca MB |
111 | (void)wxMessageBox(_T("PNG demo\nJulian Smart (c) 1998"), |
112 | _T("About PNG Demo"), wxOK); | |
c801d85f KB |
113 | } |
114 | ||
cf7a7e13 RR |
115 | void MyFrame::OnSaveFile(wxCommandEvent& WXUNUSED(event)) |
116 | { | |
4693b20c MB |
117 | wxString f = wxFileSelector( wxT("Save Image"), (const wxChar *)NULL, |
118 | (const wxChar *)NULL, | |
119 | wxT("png"), wxT("PNG files (*.png)|*.png") ); | |
cf7a7e13 | 120 | |
600683ca | 121 | if (f == _T("")) return; |
cf7a7e13 RR |
122 | |
123 | wxBitmap *backstore = new wxBitmap( 150, 150 ); | |
124 | ||
125 | wxMemoryDC memDC; | |
126 | memDC.SelectObject( *backstore ); | |
127 | memDC.Clear(); | |
128 | memDC.SetBrush( *wxBLACK_BRUSH ); | |
129 | memDC.SetPen( *wxWHITE_PEN ); | |
130 | memDC.DrawRectangle( 0, 0, 150, 150 ); | |
131 | memDC.SetPen( *wxBLACK_PEN ); | |
132 | memDC.DrawLine( 0, 0, 0, 10 ); | |
133 | memDC.SetTextForeground( *wxWHITE ); | |
600683ca | 134 | memDC.DrawText( _T("This is a memory dc."), 10, 10 ); |
cf7a7e13 RR |
135 | |
136 | memDC.SelectObject( wxNullBitmap ); | |
137 | ||
138 | backstore->SaveFile( f, wxBITMAP_TYPE_PNG, (wxPalette*)NULL ); | |
139 | ||
140 | delete backstore; | |
141 | } | |
142 | ||
e3e65dac | 143 | void MyFrame::OnLoadFile(wxCommandEvent& WXUNUSED(event)) |
c801d85f | 144 | { |
2f6c54eb VZ |
145 | // Show file selector. |
146 | wxString f = wxFileSelector(wxT("Open Image"), (const wxChar *) NULL, | |
4693b20c MB |
147 | (const wxChar *) NULL, wxT("png"), |
148 | wxT("PNG files (*.png)|*.png")); | |
c801d85f | 149 | |
600683ca | 150 | if (f == _T("")) |
2f6c54eb | 151 | return; |
c801d85f KB |
152 | |
153 | if ( g_TestBitmap ) | |
154 | delete g_TestBitmap; | |
2f6c54eb | 155 | |
c801d85f KB |
156 | g_TestBitmap = new wxBitmap(f, wxBITMAP_TYPE_PNG); |
157 | if (!g_TestBitmap->Ok()) | |
158 | { | |
159 | delete g_TestBitmap; | |
c67daf87 | 160 | g_TestBitmap = (wxBitmap *) NULL; |
c801d85f KB |
161 | } |
162 | ||
163 | canvas->Refresh(); | |
164 | } | |
165 | ||
166 | BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) | |
167 | EVT_PAINT(MyCanvas::OnPaint) | |
168 | END_EVENT_TABLE() | |
169 | ||
170 | // Define a constructor for my canvas | |
171 | MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size): | |
bc2ec626 | 172 | wxScrolledWindow(parent, wxID_ANY, pos, size) |
c801d85f KB |
173 | { |
174 | } | |
175 | ||
176 | MyCanvas::~MyCanvas(void) | |
177 | { | |
178 | } | |
179 | ||
180 | // Define the repainting behaviour | |
e3e65dac | 181 | void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event)) |
c801d85f KB |
182 | { |
183 | wxPaintDC dc(this); | |
c030b70f | 184 | dc.SetPen(* wxRED_PEN); |
c801d85f KB |
185 | |
186 | int i; | |
187 | for ( i = 0; i < 500; i += 10) | |
188 | { | |
189 | dc.DrawLine(0, i, 800, i); | |
190 | } | |
191 | if ( g_TestBitmap && g_TestBitmap->Ok() ) | |
192 | { | |
193 | wxMemoryDC memDC; | |
6be663cf | 194 | if ( g_TestBitmap->GetPalette() ) |
c801d85f | 195 | { |
6be663cf JS |
196 | memDC.SetPalette(* g_TestBitmap->GetPalette()); |
197 | dc.SetPalette(* g_TestBitmap->GetPalette()); | |
c801d85f | 198 | } |
c030b70f | 199 | memDC.SelectObject(* g_TestBitmap); |
c801d85f KB |
200 | |
201 | // Normal, non-transparent blitting | |
bc2ec626 | 202 | dc.Blit(20, 20, g_TestBitmap->GetWidth(), g_TestBitmap->GetHeight(), & memDC, 0, 0, wxCOPY, false); |
c801d85f | 203 | |
c03b8ea8 | 204 | memDC.SelectObject(wxNullBitmap); |
c801d85f KB |
205 | } |
206 | ||
207 | if ( g_TestBitmap && g_TestBitmap->Ok() ) | |
208 | { | |
209 | wxMemoryDC memDC; | |
c030b70f | 210 | memDC.SelectObject(* g_TestBitmap); |
c801d85f KB |
211 | |
212 | // Transparent blitting if there's a mask in the bitmap | |
213 | dc.Blit(20 + g_TestBitmap->GetWidth() + 20, 20, g_TestBitmap->GetWidth(), g_TestBitmap->GetHeight(), & memDC, | |
bc2ec626 | 214 | 0, 0, wxCOPY, true); |
c801d85f | 215 | |
6e5ae051 | 216 | memDC.SelectObject(wxNullBitmap); |
c801d85f KB |
217 | } |
218 | } | |
219 | ||
c801d85f | 220 |