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