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