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