]>
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 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
23 MyFrame
*frame
= (MyFrame
*) NULL
;
24 wxBitmap
*g_TestBitmap
= (wxBitmap
*) NULL
;
28 bool MyApp::OnInit(void)
30 if ( !wxApp::OnInit() )
33 wxImage::AddHandler(new wxPNGHandler
);
35 // Create the main frame window
36 frame
= new MyFrame((wxFrame
*) NULL
, _T("wxPNGBitmap Demo"), wxPoint(0, 0), wxSize(300, 300));
39 // Give it a status line
40 frame
->CreateStatusBar(2);
41 #endif // wxUSE_STATUSBAR
44 wxMenu
*file_menu
= new wxMenu
;
45 wxMenu
*help_menu
= new wxMenu
;
47 file_menu
->Append(PNGDEMO_LOAD_FILE
, _T("&Load file"), _T("Load file"));
48 file_menu
->Append(PNGDEMO_SAVE_FILE
, _T("&Save file"), _T("Save file"));
49 file_menu
->Append(PNGDEMO_QUIT
, _T("E&xit"), _T("Quit program"));
50 help_menu
->Append(PNGDEMO_ABOUT
, _T("&About"), _T("About PNG demo"));
52 wxMenuBar
*menu_bar
= new wxMenuBar
;
54 menu_bar
->Append(file_menu
, _T("&File"));
55 menu_bar
->Append(help_menu
, _T("&Help"));
57 // Associate the menu bar with the frame
58 frame
->SetMenuBar(menu_bar
);
60 MyCanvas
*canvas
= new MyCanvas(frame
, wxPoint(0, 0), wxSize(100, 100));
62 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
63 // canvas->SetScrollbars(20, 20, 50, 50, 4, 4);
64 frame
->canvas
= canvas
;
69 frame
->SetStatusText(_T("Hello, wxWidgets"));
70 #endif // wxUSE_STATUSBAR
75 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
76 EVT_MENU(PNGDEMO_QUIT
, MyFrame::OnQuit
)
77 EVT_MENU(PNGDEMO_ABOUT
, MyFrame::OnAbout
)
78 EVT_MENU(PNGDEMO_LOAD_FILE
, MyFrame::OnLoadFile
)
79 EVT_MENU(PNGDEMO_SAVE_FILE
, MyFrame::OnSaveFile
)
82 // Define my frame constructor
83 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
84 wxFrame(frame
, wxID_ANY
, title
, pos
, size
)
86 canvas
= (MyCanvas
*) NULL
;
95 g_TestBitmap
= (wxBitmap
*) NULL
;
99 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
104 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
106 (void)wxMessageBox(_T("PNG demo\nJulian Smart (c) 1998"),
107 _T("About PNG Demo"), wxOK
);
110 void MyFrame::OnSaveFile(wxCommandEvent
& WXUNUSED(event
))
113 wxString f
= wxFileSelector( wxT("Save Image"), (const wxChar
*)NULL
,
114 (const wxChar
*)NULL
,
115 wxT("png"), wxT("PNG files (*.png)|*.png") );
117 if (f
.empty()) return;
119 wxBitmap
*backstore
= new wxBitmap( 150, 150 );
122 memDC
.SelectObject( *backstore
);
124 memDC
.SetBrush( *wxBLACK_BRUSH
);
125 memDC
.SetPen( *wxWHITE_PEN
);
126 memDC
.DrawRectangle( 0, 0, 150, 150 );
127 memDC
.SetPen( *wxBLACK_PEN
);
128 memDC
.DrawLine( 0, 0, 0, 10 );
129 memDC
.SetTextForeground( *wxWHITE
);
130 memDC
.DrawText( _T("This is a memory dc."), 10, 10 );
132 memDC
.SelectObject( wxNullBitmap
);
134 backstore
->SaveFile( f
, wxBITMAP_TYPE_PNG
, (wxPalette
*)NULL
);
137 #endif // wxUSE_FILEDLG
140 void MyFrame::OnLoadFile(wxCommandEvent
& WXUNUSED(event
))
143 // Show file selector.
144 wxString f
= wxFileSelector(wxT("Open Image"), (const wxChar
*) NULL
,
145 (const wxChar
*) NULL
, wxT("png"),
146 wxT("PNG files (*.png)|*.png"));
154 g_TestBitmap
= new wxBitmap(f
, wxBITMAP_TYPE_PNG
);
155 if (!g_TestBitmap
->Ok())
158 g_TestBitmap
= (wxBitmap
*) NULL
;
162 #endif // wxUSE_FILEDLG
165 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
166 EVT_PAINT(MyCanvas::OnPaint
)
169 // Define a constructor for my canvas
170 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
):
171 wxScrolledWindow(parent
, wxID_ANY
, pos
, size
)
175 // Define the repainting behaviour
176 void MyCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
179 dc
.SetPen(* wxRED_PEN
);
182 for ( i
= 0; i
< 500; i
+= 10)
184 dc
.DrawLine(0, i
, 800, i
);
186 if ( g_TestBitmap
&& g_TestBitmap
->Ok() )
189 if ( g_TestBitmap
->GetPalette() )
191 memDC
.SetPalette(* g_TestBitmap
->GetPalette());
192 dc
.SetPalette(* g_TestBitmap
->GetPalette());
194 memDC
.SelectObject(* g_TestBitmap
);
196 // Normal, non-transparent blitting
197 dc
.Blit(20, 20, g_TestBitmap
->GetWidth(), g_TestBitmap
->GetHeight(), & memDC
, 0, 0, wxCOPY
, false);
199 memDC
.SelectObject(wxNullBitmap
);
202 if ( g_TestBitmap
&& g_TestBitmap
->Ok() )
205 memDC
.SelectObject(* g_TestBitmap
);
207 // Transparent blitting if there's a mask in the bitmap
208 dc
.Blit(20 + g_TestBitmap
->GetWidth() + 20, 20, g_TestBitmap
->GetWidth(), g_TestBitmap
->GetHeight(), & memDC
,
211 memDC
.SelectObject(wxNullBitmap
);