]> git.saurik.com Git - wxWidgets.git/blame - samples/png/pngdemo.cpp
1. Empty() now doesn't free memory - Clear() does
[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$
8// Copyright: (c) Julian Smart and Markus Holzem
9// Licence: wxWindows license
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
2049ba38 23#ifdef __WXMSW__
c801d85f
KB
24#include <wx/pnghand.h>
25#endif
26
27#include "pngdemo.h"
28
c67daf87
UR
29MyFrame *frame = (MyFrame *) NULL;
30wxBitmap *g_TestBitmap = (wxBitmap *) NULL;
c801d85f
KB
31
32IMPLEMENT_APP(MyApp)
33
34MyApp::MyApp()
35{
36}
37
38bool MyApp::OnInit(void)
39{
2049ba38 40#ifdef __WXMSW__
c801d85f
KB
41 wxBitmap::AddHandler(new wxPNGFileHandler);
42#endif
43
44 // Create the main frame window
c67daf87 45 frame = new MyFrame((wxFrame *) NULL, "wxPNGBitmap Demo", wxPoint(0, 0), wxSize(300, 300));
c801d85f
KB
46
47 // Give it a status line
48 frame->CreateStatusBar(2);
49
50 // Make a menubar
51 wxMenu *file_menu = new wxMenu;
52 wxMenu *help_menu = new wxMenu;
53
54 file_menu->Append(PNGDEMO_LOAD_FILE, "&Load file", "Load file");
cf7a7e13 55 file_menu->Append(PNGDEMO_SAVE_FILE, "&Save file", "Save file");
c801d85f
KB
56 file_menu->Append(PNGDEMO_QUIT, "E&xit", "Quit program");
57 help_menu->Append(PNGDEMO_ABOUT, "&About", "About PNG demo");
58
59 wxMenuBar *menu_bar = new wxMenuBar;
60
61 menu_bar->Append(file_menu, "&File");
62 menu_bar->Append(help_menu, "&Help");
63
64 // Associate the menu bar with the frame
65 frame->SetMenuBar(menu_bar);
66
67 MyCanvas *canvas = new MyCanvas(frame, wxPoint(0, 0), wxSize(100, 100));
68
69 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
70// canvas->SetScrollbars(20, 20, 50, 50, 4, 4);
71 frame->canvas = canvas;
72
73 frame->Show(TRUE);
74
75 frame->SetStatusText("Hello, wxWindows");
76
77 return TRUE;
78}
79
80BEGIN_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
85END_EVENT_TABLE()
86
87// Define my frame constructor
88MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size):
89 wxFrame(frame, -1, title, pos, size)
90{
c67daf87 91 canvas = (MyCanvas *) NULL;
c801d85f
KB
92}
93
e3e65dac 94void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
c801d85f
KB
95{
96 Close(TRUE);
97}
98
e3e65dac 99void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
c801d85f
KB
100{
101 (void)wxMessageBox("PNG demo\nJulian Smart (c) 1998",
102 "About PNG Demo", wxOK);
103}
104
cf7a7e13
RR
105void MyFrame::OnSaveFile(wxCommandEvent& WXUNUSED(event))
106{
107 char *f = wxFileSelector( "Save Image", (const char *)NULL, (const char *)NULL,
108 "png", "PNG files (*.png)|*.png" );
109
110 if (!f) return;
111
112 wxBitmap *backstore = new wxBitmap( 150, 150 );
113
114 wxMemoryDC memDC;
115 memDC.SelectObject( *backstore );
116 memDC.Clear();
117 memDC.SetBrush( *wxBLACK_BRUSH );
118 memDC.SetPen( *wxWHITE_PEN );
119 memDC.DrawRectangle( 0, 0, 150, 150 );
120 memDC.SetPen( *wxBLACK_PEN );
121 memDC.DrawLine( 0, 0, 0, 10 );
122 memDC.SetTextForeground( *wxWHITE );
123 memDC.DrawText( "This is a memory dc.", 10, 10 );
124
125 memDC.SelectObject( wxNullBitmap );
126
127 backstore->SaveFile( f, wxBITMAP_TYPE_PNG, (wxPalette*)NULL );
128
129 delete backstore;
130}
131
e3e65dac 132void MyFrame::OnLoadFile(wxCommandEvent& WXUNUSED(event))
c801d85f
KB
133{
134 // Show file selector.
c67daf87 135 char *f = wxFileSelector("Open Image", (const char *) NULL, (const char *) NULL,"png",
c801d85f
KB
136 "PNG files (*.png)|*.png");
137
138 if (!f)
139 return;
140
141 if ( g_TestBitmap )
142 delete g_TestBitmap;
143 g_TestBitmap = new wxBitmap(f, wxBITMAP_TYPE_PNG);
144 if (!g_TestBitmap->Ok())
145 {
146 delete g_TestBitmap;
c67daf87 147 g_TestBitmap = (wxBitmap *) NULL;
c801d85f
KB
148 }
149
150 canvas->Refresh();
151}
152
153BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
154 EVT_PAINT(MyCanvas::OnPaint)
155END_EVENT_TABLE()
156
157// Define a constructor for my canvas
158MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size):
159 wxScrolledWindow(parent, -1, pos, size)
160{
161}
162
163MyCanvas::~MyCanvas(void)
164{
165}
166
167// Define the repainting behaviour
e3e65dac 168void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
c801d85f
KB
169{
170 wxPaintDC dc(this);
171 dc.SetPen(wxRED_PEN);
172
173 int i;
174 for ( i = 0; i < 500; i += 10)
175 {
176 dc.DrawLine(0, i, 800, i);
177 }
178 if ( g_TestBitmap && g_TestBitmap->Ok() )
179 {
180 wxMemoryDC memDC;
181 if ( g_TestBitmap->GetColourMap() )
182 {
183 memDC.SetColourMap(g_TestBitmap->GetColourMap());
184 dc.SetColourMap(g_TestBitmap->GetColourMap());
185 }
186 memDC.SelectObject(g_TestBitmap);
187
188 // Normal, non-transparent blitting
189 dc.Blit(20, 20, g_TestBitmap->GetWidth(), g_TestBitmap->GetHeight(), & memDC, 0, 0, wxCOPY, FALSE);
190
c03b8ea8 191 memDC.SelectObject(wxNullBitmap);
c801d85f
KB
192 }
193
194 if ( g_TestBitmap && g_TestBitmap->Ok() )
195 {
196 wxMemoryDC memDC;
197 memDC.SelectObject(g_TestBitmap);
198
199 // Transparent blitting if there's a mask in the bitmap
200 dc.Blit(20 + g_TestBitmap->GetWidth() + 20, 20, g_TestBitmap->GetWidth(), g_TestBitmap->GetHeight(), & memDC,
201 0, 0, wxCOPY, TRUE);
202
6e5ae051 203 memDC.SelectObject(wxNullBitmap);
c801d85f
KB
204 }
205}
206
207// Define the behaviour for the frame closing
208// - must delete all frames except for the main one.
209bool MyFrame::OnClose(void)
210{
211 Show(FALSE);
212
213 return TRUE;
214}
215