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