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