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