]>
git.saurik.com Git - wxWidgets.git/blob - samples/png/pngdemo.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Demos PNG reading
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "pngdemo.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
27 MyFrame
*frame
= (MyFrame
*) NULL
;
28 wxBitmap
*g_TestBitmap
= (wxBitmap
*) NULL
;
32 bool MyApp::OnInit(void)
34 wxImage::AddHandler(new wxPNGHandler
);
36 // Create the main frame window
37 frame
= new MyFrame((wxFrame
*) NULL
, _T("wxPNGBitmap Demo"), wxPoint(0, 0), wxSize(300, 300));
40 // Give it a status line
41 frame
->CreateStatusBar(2);
42 #endif // wxUSE_STATUSBAR
45 wxMenu
*file_menu
= new wxMenu
;
46 wxMenu
*help_menu
= new wxMenu
;
48 file_menu
->Append(PNGDEMO_LOAD_FILE
, _T("&Load file"), _T("Load file"));
49 file_menu
->Append(PNGDEMO_SAVE_FILE
, _T("&Save file"), _T("Save file"));
50 file_menu
->Append(PNGDEMO_QUIT
, _T("E&xit"), _T("Quit program"));
51 help_menu
->Append(PNGDEMO_ABOUT
, _T("&About"), _T("About PNG demo"));
53 wxMenuBar
*menu_bar
= new wxMenuBar
;
55 menu_bar
->Append(file_menu
, _T("&File"));
56 menu_bar
->Append(help_menu
, _T("&Help"));
58 // Associate the menu bar with the frame
59 frame
->SetMenuBar(menu_bar
);
61 MyCanvas
*canvas
= new MyCanvas(frame
, wxPoint(0, 0), wxSize(100, 100));
63 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
64 // canvas->SetScrollbars(20, 20, 50, 50, 4, 4);
65 frame
->canvas
= canvas
;
70 frame
->SetStatusText(_T("Hello, wxWidgets"));
71 #endif // wxUSE_STATUSBAR
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
)
80 EVT_MENU(PNGDEMO_SAVE_FILE
, MyFrame::OnSaveFile
)
83 // Define my frame constructor
84 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
85 wxFrame(frame
, wxID_ANY
, title
, pos
, size
)
87 canvas
= (MyCanvas
*) NULL
;
96 g_TestBitmap
= (wxBitmap
*) NULL
;
100 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
105 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
107 (void)wxMessageBox(_T("PNG demo\nJulian Smart (c) 1998"),
108 _T("About PNG Demo"), wxOK
);
111 void MyFrame::OnSaveFile(wxCommandEvent
& WXUNUSED(event
))
114 wxString f
= wxFileSelector( wxT("Save Image"), (const wxChar
*)NULL
,
115 (const wxChar
*)NULL
,
116 wxT("png"), wxT("PNG files (*.png)|*.png") );
118 if (f
.empty()) return;
120 wxBitmap
*backstore
= new wxBitmap( 150, 150 );
123 memDC
.SelectObject( *backstore
);
125 memDC
.SetBrush( *wxBLACK_BRUSH
);
126 memDC
.SetPen( *wxWHITE_PEN
);
127 memDC
.DrawRectangle( 0, 0, 150, 150 );
128 memDC
.SetPen( *wxBLACK_PEN
);
129 memDC
.DrawLine( 0, 0, 0, 10 );
130 memDC
.SetTextForeground( *wxWHITE
);
131 memDC
.DrawText( _T("This is a memory dc."), 10, 10 );
133 memDC
.SelectObject( wxNullBitmap
);
135 backstore
->SaveFile( f
, wxBITMAP_TYPE_PNG
, (wxPalette
*)NULL
);
138 #endif // wxUSE_FILEDLG
141 void MyFrame::OnLoadFile(wxCommandEvent
& WXUNUSED(event
))
144 // Show file selector.
145 wxString f
= wxFileSelector(wxT("Open Image"), (const wxChar
*) NULL
,
146 (const wxChar
*) NULL
, wxT("png"),
147 wxT("PNG files (*.png)|*.png"));
155 g_TestBitmap
= new wxBitmap(f
, wxBITMAP_TYPE_PNG
);
156 if (!g_TestBitmap
->Ok())
159 g_TestBitmap
= (wxBitmap
*) NULL
;
163 #endif // wxUSE_FILEDLG
166 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
167 EVT_PAINT(MyCanvas::OnPaint
)
170 // Define a constructor for my canvas
171 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
):
172 wxScrolledWindow(parent
, wxID_ANY
, pos
, size
)
176 // Define the repainting behaviour
177 void MyCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
180 dc
.SetPen(* wxRED_PEN
);
183 for ( i
= 0; i
< 500; i
+= 10)
185 dc
.DrawLine(0, i
, 800, i
);
187 if ( g_TestBitmap
&& g_TestBitmap
->Ok() )
190 if ( g_TestBitmap
->GetPalette() )
192 memDC
.SetPalette(* g_TestBitmap
->GetPalette());
193 dc
.SetPalette(* g_TestBitmap
->GetPalette());
195 memDC
.SelectObject(* g_TestBitmap
);
197 // Normal, non-transparent blitting
198 dc
.Blit(20, 20, g_TestBitmap
->GetWidth(), g_TestBitmap
->GetHeight(), & memDC
, 0, 0, wxCOPY
, false);
200 memDC
.SelectObject(wxNullBitmap
);
203 if ( g_TestBitmap
&& g_TestBitmap
->Ok() )
206 memDC
.SelectObject(* g_TestBitmap
);
208 // Transparent blitting if there's a mask in the bitmap
209 dc
.Blit(20 + g_TestBitmap
->GetWidth() + 20, 20, g_TestBitmap
->GetWidth(), g_TestBitmap
->GetHeight(), & memDC
,
212 memDC
.SelectObject(wxNullBitmap
);