]> git.saurik.com Git - wxWidgets.git/blob - samples/png/pngdemo.cpp
Dialog Editor corrections for latest wxWin changes; removed wxProp files
[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
27 #include "pngdemo.h"
28
29 MyFrame *frame = (MyFrame *) NULL;
30 wxBitmap *g_TestBitmap = (wxBitmap *) NULL;
31
32 IMPLEMENT_APP(MyApp)
33
34 MyApp::MyApp()
35 {
36 }
37
38 bool MyApp::OnInit(void)
39 {
40 #ifdef __WXMSW__
41 wxBitmap::AddHandler(new wxPNGFileHandler);
42 #endif
43
44 // Create the main frame window
45 frame = new MyFrame((wxFrame *) NULL, "wxPNGBitmap Demo", wxPoint(0, 0), wxSize(300, 300));
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");
55 file_menu->Append(PNGDEMO_SAVE_FILE, "&Save file", "Save file");
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
80 BEGIN_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)
84 EVT_MENU(PNGDEMO_SAVE_FILE, MyFrame::OnSaveFile)
85 END_EVENT_TABLE()
86
87 // Define my frame constructor
88 MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size):
89 wxFrame(frame, -1, title, pos, size)
90 {
91 canvas = (MyCanvas *) NULL;
92 }
93
94 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
95 {
96 Close(TRUE);
97 }
98
99 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
100 {
101 (void)wxMessageBox("PNG demo\nJulian Smart (c) 1998",
102 "About PNG Demo", wxOK);
103 }
104
105 void 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
132 void MyFrame::OnLoadFile(wxCommandEvent& WXUNUSED(event))
133 {
134 // Show file selector.
135 char *f = wxFileSelector("Open Image", (const char *) NULL, (const char *) NULL,"png",
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;
147 g_TestBitmap = (wxBitmap *) NULL;
148 }
149
150 canvas->Refresh();
151 }
152
153 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
154 EVT_PAINT(MyCanvas::OnPaint)
155 END_EVENT_TABLE()
156
157 // Define a constructor for my canvas
158 MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size):
159 wxScrolledWindow(parent, -1, pos, size)
160 {
161 }
162
163 MyCanvas::~MyCanvas(void)
164 {
165 }
166
167 // Define the repainting behaviour
168 void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
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.SetPalette(* g_TestBitmap->GetColourMap());
184 dc.SetPalette(* 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
191 memDC.SelectObject(wxNullBitmap);
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
203 memDC.SelectObject(wxNullBitmap);
204 }
205 }
206
207 // Define the behaviour for the frame closing
208 // - must delete all frames except for the main one.
209 bool MyFrame::OnClose(void)
210 {
211 Show(FALSE);
212
213 return TRUE;
214 }
215