]>
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
;
36 bool MyApp::OnInit(void)
38 wxImage::AddHandler(new wxPNGHandler
);
40 // Create the main frame window
41 frame
= new MyFrame((wxFrame
*) NULL
, _T("wxPNGBitmap Demo"), wxPoint(0, 0), wxSize(300, 300));
44 // Give it a status line
45 frame
->CreateStatusBar(2);
46 #endif // wxUSE_STATUSBAR
49 wxMenu
*file_menu
= new wxMenu
;
50 wxMenu
*help_menu
= new wxMenu
;
52 file_menu
->Append(PNGDEMO_LOAD_FILE
, _T("&Load file"), _T("Load file"));
53 file_menu
->Append(PNGDEMO_SAVE_FILE
, _T("&Save file"), _T("Save file"));
54 file_menu
->Append(PNGDEMO_QUIT
, _T("E&xit"), _T("Quit program"));
55 help_menu
->Append(PNGDEMO_ABOUT
, _T("&About"), _T("About PNG demo"));
57 wxMenuBar
*menu_bar
= new wxMenuBar
;
59 menu_bar
->Append(file_menu
, _T("&File"));
60 menu_bar
->Append(help_menu
, _T("&Help"));
62 // Associate the menu bar with the frame
63 frame
->SetMenuBar(menu_bar
);
65 MyCanvas
*canvas
= new MyCanvas(frame
, wxPoint(0, 0), wxSize(100, 100));
67 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
68 // canvas->SetScrollbars(20, 20, 50, 50, 4, 4);
69 frame
->canvas
= canvas
;
74 frame
->SetStatusText(_T("Hello, wxWidgets"));
75 #endif // wxUSE_STATUSBAR
80 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
81 EVT_MENU(PNGDEMO_QUIT
, MyFrame::OnQuit
)
82 EVT_MENU(PNGDEMO_ABOUT
, MyFrame::OnAbout
)
83 EVT_MENU(PNGDEMO_LOAD_FILE
, MyFrame::OnLoadFile
)
84 EVT_MENU(PNGDEMO_SAVE_FILE
, MyFrame::OnSaveFile
)
87 // Define my frame constructor
88 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
89 wxFrame(frame
, wxID_ANY
, title
, pos
, size
)
91 canvas
= (MyCanvas
*) NULL
;
100 g_TestBitmap
= (wxBitmap
*) NULL
;
104 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
109 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
111 (void)wxMessageBox(_T("PNG demo\nJulian Smart (c) 1998"),
112 _T("About PNG Demo"), wxOK
);
115 void MyFrame::OnSaveFile(wxCommandEvent
& WXUNUSED(event
))
117 wxString f
= wxFileSelector( wxT("Save Image"), (const wxChar
*)NULL
,
118 (const wxChar
*)NULL
,
119 wxT("png"), wxT("PNG files (*.png)|*.png") );
121 if (f
== _T("")) return;
123 wxBitmap
*backstore
= new wxBitmap( 150, 150 );
126 memDC
.SelectObject( *backstore
);
128 memDC
.SetBrush( *wxBLACK_BRUSH
);
129 memDC
.SetPen( *wxWHITE_PEN
);
130 memDC
.DrawRectangle( 0, 0, 150, 150 );
131 memDC
.SetPen( *wxBLACK_PEN
);
132 memDC
.DrawLine( 0, 0, 0, 10 );
133 memDC
.SetTextForeground( *wxWHITE
);
134 memDC
.DrawText( _T("This is a memory dc."), 10, 10 );
136 memDC
.SelectObject( wxNullBitmap
);
138 backstore
->SaveFile( f
, wxBITMAP_TYPE_PNG
, (wxPalette
*)NULL
);
143 void MyFrame::OnLoadFile(wxCommandEvent
& WXUNUSED(event
))
145 // Show file selector.
146 wxString f
= wxFileSelector(wxT("Open Image"), (const wxChar
*) NULL
,
147 (const wxChar
*) NULL
, wxT("png"),
148 wxT("PNG files (*.png)|*.png"));
156 g_TestBitmap
= new wxBitmap(f
, wxBITMAP_TYPE_PNG
);
157 if (!g_TestBitmap
->Ok())
160 g_TestBitmap
= (wxBitmap
*) NULL
;
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 MyCanvas::~MyCanvas(void)
180 // Define the repainting behaviour
181 void MyCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
184 dc
.SetPen(* wxRED_PEN
);
187 for ( i
= 0; i
< 500; i
+= 10)
189 dc
.DrawLine(0, i
, 800, i
);
191 if ( g_TestBitmap
&& g_TestBitmap
->Ok() )
194 if ( g_TestBitmap
->GetPalette() )
196 memDC
.SetPalette(* g_TestBitmap
->GetPalette());
197 dc
.SetPalette(* g_TestBitmap
->GetPalette());
199 memDC
.SelectObject(* g_TestBitmap
);
201 // Normal, non-transparent blitting
202 dc
.Blit(20, 20, g_TestBitmap
->GetWidth(), g_TestBitmap
->GetHeight(), & memDC
, 0, 0, wxCOPY
, false);
204 memDC
.SelectObject(wxNullBitmap
);
207 if ( g_TestBitmap
&& g_TestBitmap
->Ok() )
210 memDC
.SelectObject(* g_TestBitmap
);
212 // Transparent blitting if there's a mask in the bitmap
213 dc
.Blit(20 + g_TestBitmap
->GetWidth() + 20, 20, g_TestBitmap
->GetWidth(), g_TestBitmap
->GetHeight(), & memDC
,
216 memDC
.SelectObject(wxNullBitmap
);