]>
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)
43 wxImage::AddHandler(new wxPNGHandler
);
45 // Create the main frame window
46 frame
= new MyFrame((wxFrame
*) NULL
, "wxPNGBitmap Demo", wxPoint(0, 0), wxSize(300, 300));
48 // Give it a status line
49 frame
->CreateStatusBar(2);
52 wxMenu
*file_menu
= new wxMenu
;
53 wxMenu
*help_menu
= new wxMenu
;
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");
60 wxMenuBar
*menu_bar
= new wxMenuBar
;
62 menu_bar
->Append(file_menu
, "&File");
63 menu_bar
->Append(help_menu
, "&Help");
65 // Associate the menu bar with the frame
66 frame
->SetMenuBar(menu_bar
);
68 MyCanvas
*canvas
= new MyCanvas(frame
, wxPoint(0, 0), wxSize(100, 100));
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
;
76 frame
->SetStatusText("Hello, wxWindows");
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
)
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
)
92 canvas
= (MyCanvas
*) NULL
;
95 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
100 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
102 (void)wxMessageBox("PNG demo\nJulian Smart (c) 1998",
103 "About PNG Demo", wxOK
);
106 void MyFrame::OnSaveFile(wxCommandEvent
& WXUNUSED(event
))
108 wxString f
= wxFileSelector( "Save Image", (const char *)NULL
, (const char *)NULL
,
109 "png", "PNG files (*.png)|*.png" );
113 wxBitmap
*backstore
= new wxBitmap( 150, 150 );
116 memDC
.SelectObject( *backstore
);
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 );
126 memDC
.SelectObject( wxNullBitmap
);
128 backstore
->SaveFile( f
, wxBITMAP_TYPE_PNG
, (wxPalette
*)NULL
);
133 void MyFrame::OnLoadFile(wxCommandEvent
& WXUNUSED(event
))
135 // Show file selector.
136 wxString f
= wxFileSelector("Open Image", (const char *) NULL
, (const char *) NULL
,"png",
137 "PNG files (*.png)|*.png");
144 g_TestBitmap
= new wxBitmap(f
, wxBITMAP_TYPE_PNG
);
145 if (!g_TestBitmap
->Ok())
148 g_TestBitmap
= (wxBitmap
*) NULL
;
154 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
155 EVT_PAINT(MyCanvas::OnPaint
)
158 // Define a constructor for my canvas
159 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
):
160 wxScrolledWindow(parent
, -1, pos
, size
)
164 MyCanvas::~MyCanvas(void)
168 // Define the repainting behaviour
169 void MyCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
172 dc
.SetPen(* wxRED_PEN
);
175 for ( i
= 0; i
< 500; i
+= 10)
177 dc
.DrawLine(0, i
, 800, i
);
179 if ( g_TestBitmap
&& g_TestBitmap
->Ok() )
182 if ( g_TestBitmap
->GetPalette() )
184 memDC
.SetPalette(* g_TestBitmap
->GetPalette());
185 dc
.SetPalette(* g_TestBitmap
->GetPalette());
187 memDC
.SelectObject(* g_TestBitmap
);
189 // Normal, non-transparent blitting
190 dc
.Blit(20, 20, g_TestBitmap
->GetWidth(), g_TestBitmap
->GetHeight(), & memDC
, 0, 0, wxCOPY
, FALSE
);
192 memDC
.SelectObject(wxNullBitmap
);
195 if ( g_TestBitmap
&& g_TestBitmap
->Ok() )
198 memDC
.SelectObject(* g_TestBitmap
);
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
,
204 memDC
.SelectObject(wxNullBitmap
);