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