]>
Commit | Line | Data |
---|---|---|
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__ |
79144b8a RR |
24 | #include "wx/pnghand.h" |
25 | #endif | |
26 | #ifdef __WXGTK__ | |
27 | #include "wx/image.h" | |
c801d85f KB |
28 | #endif |
29 | ||
30 | #include "pngdemo.h" | |
31 | ||
c67daf87 UR |
32 | MyFrame *frame = (MyFrame *) NULL; |
33 | wxBitmap *g_TestBitmap = (wxBitmap *) NULL; | |
c801d85f KB |
34 | |
35 | IMPLEMENT_APP(MyApp) | |
36 | ||
37 | MyApp::MyApp() | |
38 | { | |
39 | } | |
40 | ||
41 | bool MyApp::OnInit(void) | |
42 | { | |
2049ba38 | 43 | #ifdef __WXMSW__ |
c801d85f | 44 | wxBitmap::AddHandler(new wxPNGFileHandler); |
79144b8a RR |
45 | #endif |
46 | #ifdef __WXGTK__ | |
47 | wxImage::AddHandler(new wxPNGHandler); | |
c801d85f KB |
48 | #endif |
49 | ||
50 | // Create the main frame window | |
c67daf87 | 51 | frame = new MyFrame((wxFrame *) NULL, "wxPNGBitmap Demo", wxPoint(0, 0), wxSize(300, 300)); |
c801d85f KB |
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"); | |
cf7a7e13 | 61 | file_menu->Append(PNGDEMO_SAVE_FILE, "&Save file", "Save file"); |
c801d85f KB |
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) | |
cf7a7e13 | 90 | EVT_MENU(PNGDEMO_SAVE_FILE, MyFrame::OnSaveFile) |
c801d85f KB |
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 | { | |
c67daf87 | 97 | canvas = (MyCanvas *) NULL; |
c801d85f KB |
98 | } |
99 | ||
e3e65dac | 100 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
c801d85f KB |
101 | { |
102 | Close(TRUE); | |
103 | } | |
104 | ||
e3e65dac | 105 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
c801d85f KB |
106 | { |
107 | (void)wxMessageBox("PNG demo\nJulian Smart (c) 1998", | |
108 | "About PNG Demo", wxOK); | |
109 | } | |
110 | ||
cf7a7e13 RR |
111 | void MyFrame::OnSaveFile(wxCommandEvent& WXUNUSED(event)) |
112 | { | |
227869da | 113 | wxString f = wxFileSelector( "Save Image", (const char *)NULL, (const char *)NULL, |
cf7a7e13 RR |
114 | "png", "PNG files (*.png)|*.png" ); |
115 | ||
227869da | 116 | if (f == "") return; |
cf7a7e13 RR |
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 | ||
e3e65dac | 138 | void MyFrame::OnLoadFile(wxCommandEvent& WXUNUSED(event)) |
c801d85f KB |
139 | { |
140 | // Show file selector. | |
227869da | 141 | wxString f = wxFileSelector("Open Image", (const char *) NULL, (const char *) NULL,"png", |
c801d85f KB |
142 | "PNG files (*.png)|*.png"); |
143 | ||
227869da | 144 | if (f == "") |
c801d85f KB |
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; | |
c67daf87 | 153 | g_TestBitmap = (wxBitmap *) NULL; |
c801d85f KB |
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 | |
e3e65dac | 174 | void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event)) |
c801d85f KB |
175 | { |
176 | wxPaintDC dc(this); | |
c030b70f | 177 | dc.SetPen(* wxRED_PEN); |
c801d85f KB |
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; | |
6be663cf | 187 | if ( g_TestBitmap->GetPalette() ) |
c801d85f | 188 | { |
6be663cf JS |
189 | memDC.SetPalette(* g_TestBitmap->GetPalette()); |
190 | dc.SetPalette(* g_TestBitmap->GetPalette()); | |
c801d85f | 191 | } |
c030b70f | 192 | memDC.SelectObject(* g_TestBitmap); |
c801d85f KB |
193 | |
194 | // Normal, non-transparent blitting | |
195 | dc.Blit(20, 20, g_TestBitmap->GetWidth(), g_TestBitmap->GetHeight(), & memDC, 0, 0, wxCOPY, FALSE); | |
196 | ||
c03b8ea8 | 197 | memDC.SelectObject(wxNullBitmap); |
c801d85f KB |
198 | } |
199 | ||
200 | if ( g_TestBitmap && g_TestBitmap->Ok() ) | |
201 | { | |
202 | wxMemoryDC memDC; | |
c030b70f | 203 | memDC.SelectObject(* g_TestBitmap); |
c801d85f KB |
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 | ||
6e5ae051 | 209 | memDC.SelectObject(wxNullBitmap); |
c801d85f KB |
210 | } |
211 | } | |
212 | ||
c801d85f | 213 |