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