]>
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 and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "pngdemo.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
24 #include "wx/pnghand.h"
32 MyFrame
*frame
= (MyFrame
*) NULL
;
33 wxBitmap
*g_TestBitmap
= (wxBitmap
*) NULL
;
41 bool MyApp::OnInit(void)
44 wxBitmap::AddHandler(new wxPNGFileHandler
);
47 wxImage::AddHandler(new wxPNGHandler
);
50 // Create the main frame window
51 frame
= new MyFrame((wxFrame
*) NULL
, "wxPNGBitmap Demo", wxPoint(0, 0), wxSize(300, 300));
53 // Give it a status line
54 frame
->CreateStatusBar(2);
57 wxMenu
*file_menu
= new wxMenu
;
58 wxMenu
*help_menu
= new wxMenu
;
60 file_menu
->Append(PNGDEMO_LOAD_FILE
, "&Load file", "Load file");
61 file_menu
->Append(PNGDEMO_SAVE_FILE
, "&Save file", "Save file");
62 file_menu
->Append(PNGDEMO_QUIT
, "E&xit", "Quit program");
63 help_menu
->Append(PNGDEMO_ABOUT
, "&About", "About PNG demo");
65 wxMenuBar
*menu_bar
= new wxMenuBar
;
67 menu_bar
->Append(file_menu
, "&File");
68 menu_bar
->Append(help_menu
, "&Help");
70 // Associate the menu bar with the frame
71 frame
->SetMenuBar(menu_bar
);
73 MyCanvas
*canvas
= new MyCanvas(frame
, wxPoint(0, 0), wxSize(100, 100));
75 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
76 // canvas->SetScrollbars(20, 20, 50, 50, 4, 4);
77 frame
->canvas
= canvas
;
81 frame
->SetStatusText("Hello, wxWindows");
86 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
87 EVT_MENU(PNGDEMO_QUIT
, MyFrame::OnQuit
)
88 EVT_MENU(PNGDEMO_ABOUT
, MyFrame::OnAbout
)
89 EVT_MENU(PNGDEMO_LOAD_FILE
, MyFrame::OnLoadFile
)
90 EVT_MENU(PNGDEMO_SAVE_FILE
, MyFrame::OnSaveFile
)
93 // Define my frame constructor
94 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
95 wxFrame(frame
, -1, title
, pos
, size
)
97 canvas
= (MyCanvas
*) NULL
;
100 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
105 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
107 (void)wxMessageBox("PNG demo\nJulian Smart (c) 1998",
108 "About PNG Demo", wxOK
);
111 void MyFrame::OnSaveFile(wxCommandEvent
& WXUNUSED(event
))
113 wxString f
= wxFileSelector( "Save Image", (const char *)NULL
, (const char *)NULL
,
114 "png", "PNG files (*.png)|*.png" );
118 wxBitmap
*backstore
= new wxBitmap( 150, 150 );
121 memDC
.SelectObject( *backstore
);
123 memDC
.SetBrush( *wxBLACK_BRUSH
);
124 memDC
.SetPen( *wxWHITE_PEN
);
125 memDC
.DrawRectangle( 0, 0, 150, 150 );
126 memDC
.SetPen( *wxBLACK_PEN
);
127 memDC
.DrawLine( 0, 0, 0, 10 );
128 memDC
.SetTextForeground( *wxWHITE
);
129 memDC
.DrawText( "This is a memory dc.", 10, 10 );
131 memDC
.SelectObject( wxNullBitmap
);
133 backstore
->SaveFile( f
, wxBITMAP_TYPE_PNG
, (wxPalette
*)NULL
);
138 void MyFrame::OnLoadFile(wxCommandEvent
& WXUNUSED(event
))
140 // Show file selector.
141 wxString f
= wxFileSelector("Open Image", (const char *) NULL
, (const char *) NULL
,"png",
142 "PNG files (*.png)|*.png");
149 g_TestBitmap
= new wxBitmap(f
, wxBITMAP_TYPE_PNG
);
150 if (!g_TestBitmap
->Ok())
153 g_TestBitmap
= (wxBitmap
*) NULL
;
159 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
160 EVT_PAINT(MyCanvas::OnPaint
)
163 // Define a constructor for my canvas
164 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
):
165 wxScrolledWindow(parent
, -1, pos
, size
)
169 MyCanvas::~MyCanvas(void)
173 // Define the repainting behaviour
174 void MyCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
177 dc
.SetPen(* wxRED_PEN
);
180 for ( i
= 0; i
< 500; i
+= 10)
182 dc
.DrawLine(0, i
, 800, i
);
184 if ( g_TestBitmap
&& g_TestBitmap
->Ok() )
187 if ( g_TestBitmap
->GetPalette() )
189 memDC
.SetPalette(* g_TestBitmap
->GetPalette());
190 dc
.SetPalette(* g_TestBitmap
->GetPalette());
192 memDC
.SelectObject(* g_TestBitmap
);
194 // Normal, non-transparent blitting
195 dc
.Blit(20, 20, g_TestBitmap
->GetWidth(), g_TestBitmap
->GetHeight(), & memDC
, 0, 0, wxCOPY
, FALSE
);
197 memDC
.SelectObject(wxNullBitmap
);
200 if ( g_TestBitmap
&& g_TestBitmap
->Ok() )
203 memDC
.SelectObject(* g_TestBitmap
);
205 // Transparent blitting if there's a mask in the bitmap
206 dc
.Blit(20 + g_TestBitmap
->GetWidth() + 20, 20, g_TestBitmap
->GetWidth(), g_TestBitmap
->GetHeight(), & memDC
,
209 memDC
.SelectObject(wxNullBitmap
);