]> git.saurik.com Git - wxWidgets.git/blob - samples/png/pngdemo.cpp
Changed \ to /
[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 #ifdef __WXGTK__
27 #include "wx/image.h"
28 #endif
29
30 #include "pngdemo.h"
31
32 MyFrame *frame = (MyFrame *) NULL;
33 wxBitmap *g_TestBitmap = (wxBitmap *) NULL;
34
35 IMPLEMENT_APP(MyApp)
36
37 MyApp::MyApp()
38 {
39 }
40
41 bool MyApp::OnInit(void)
42 {
43 wxImage::AddHandler(new wxPNGHandler);
44
45 // Create the main frame window
46 frame = new MyFrame((wxFrame *) NULL, "wxPNGBitmap Demo", wxPoint(0, 0), wxSize(300, 300));
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");
56 file_menu->Append(PNGDEMO_SAVE_FILE, "&Save file", "Save file");
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)
85 EVT_MENU(PNGDEMO_SAVE_FILE, MyFrame::OnSaveFile)
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 {
92 canvas = (MyCanvas *) NULL;
93 }
94
95 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
96 {
97 Close(TRUE);
98 }
99
100 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
101 {
102 (void)wxMessageBox("PNG demo\nJulian Smart (c) 1998",
103 "About PNG Demo", wxOK);
104 }
105
106 void MyFrame::OnSaveFile(wxCommandEvent& WXUNUSED(event))
107 {
108 wxString f = wxFileSelector( "Save Image", (const char *)NULL, (const char *)NULL,
109 "png", "PNG files (*.png)|*.png" );
110
111 if (f == "") return;
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
133 void MyFrame::OnLoadFile(wxCommandEvent& WXUNUSED(event))
134 {
135 // Show file selector.
136 wxString f = wxFileSelector("Open Image", (const char *) NULL, (const char *) NULL,"png",
137 "PNG files (*.png)|*.png");
138
139 if (f == "")
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;
148 g_TestBitmap = (wxBitmap *) NULL;
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
169 void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
170 {
171 wxPaintDC dc(this);
172 dc.SetPen(* wxRED_PEN);
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;
182 if ( g_TestBitmap->GetPalette() )
183 {
184 memDC.SetPalette(* g_TestBitmap->GetPalette());
185 dc.SetPalette(* g_TestBitmap->GetPalette());
186 }
187 memDC.SelectObject(* g_TestBitmap);
188
189 // Normal, non-transparent blitting
190 dc.Blit(20, 20, g_TestBitmap->GetWidth(), g_TestBitmap->GetHeight(), & memDC, 0, 0, wxCOPY, FALSE);
191
192 memDC.SelectObject(wxNullBitmap);
193 }
194
195 if ( g_TestBitmap && g_TestBitmap->Ok() )
196 {
197 wxMemoryDC memDC;
198 memDC.SelectObject(* g_TestBitmap);
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
204 memDC.SelectObject(wxNullBitmap);
205 }
206 }
207
208