]>
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>
29 MyFrame
*frame
= (MyFrame
*) NULL
;
30 wxBitmap
*g_TestBitmap
= (wxBitmap
*) NULL
;
38 bool MyApp::OnInit(void)
41 wxBitmap::AddHandler(new wxPNGFileHandler
);
44 // Create the main frame window
45 frame
= new MyFrame((wxFrame
*) NULL
, "wxPNGBitmap Demo", wxPoint(0, 0), wxSize(300, 300));
47 // Give it a status line
48 frame
->CreateStatusBar(2);
51 wxMenu
*file_menu
= new wxMenu
;
52 wxMenu
*help_menu
= new wxMenu
;
54 file_menu
->Append(PNGDEMO_LOAD_FILE
, "&Load file", "Load file");
55 file_menu
->Append(PNGDEMO_SAVE_FILE
, "&Save file", "Save file");
56 file_menu
->Append(PNGDEMO_QUIT
, "E&xit", "Quit program");
57 help_menu
->Append(PNGDEMO_ABOUT
, "&About", "About PNG demo");
59 wxMenuBar
*menu_bar
= new wxMenuBar
;
61 menu_bar
->Append(file_menu
, "&File");
62 menu_bar
->Append(help_menu
, "&Help");
64 // Associate the menu bar with the frame
65 frame
->SetMenuBar(menu_bar
);
67 MyCanvas
*canvas
= new MyCanvas(frame
, wxPoint(0, 0), wxSize(100, 100));
69 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
70 // canvas->SetScrollbars(20, 20, 50, 50, 4, 4);
71 frame
->canvas
= canvas
;
75 frame
->SetStatusText("Hello, wxWindows");
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
, -1, title
, pos
, size
)
91 canvas
= (MyCanvas
*) NULL
;
94 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
99 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
101 (void)wxMessageBox("PNG demo\nJulian Smart (c) 1998",
102 "About PNG Demo", wxOK
);
105 void MyFrame::OnSaveFile(wxCommandEvent
& WXUNUSED(event
))
107 char *f
= wxFileSelector( "Save Image", (const char *)NULL
, (const char *)NULL
,
108 "png", "PNG files (*.png)|*.png" );
112 wxBitmap
*backstore
= new wxBitmap( 150, 150 );
115 memDC
.SelectObject( *backstore
);
117 memDC
.SetBrush( *wxBLACK_BRUSH
);
118 memDC
.SetPen( *wxWHITE_PEN
);
119 memDC
.DrawRectangle( 0, 0, 150, 150 );
120 memDC
.SetPen( *wxBLACK_PEN
);
121 memDC
.DrawLine( 0, 0, 0, 10 );
122 memDC
.SetTextForeground( *wxWHITE
);
123 memDC
.DrawText( "This is a memory dc.", 10, 10 );
125 memDC
.SelectObject( wxNullBitmap
);
127 backstore
->SaveFile( f
, wxBITMAP_TYPE_PNG
, (wxPalette
*)NULL
);
132 void MyFrame::OnLoadFile(wxCommandEvent
& WXUNUSED(event
))
134 // Show file selector.
135 char *f
= wxFileSelector("Open Image", (const char *) NULL
, (const char *) NULL
,"png",
136 "PNG files (*.png)|*.png");
143 g_TestBitmap
= new wxBitmap(f
, wxBITMAP_TYPE_PNG
);
144 if (!g_TestBitmap
->Ok())
147 g_TestBitmap
= (wxBitmap
*) NULL
;
153 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
154 EVT_PAINT(MyCanvas::OnPaint
)
157 // Define a constructor for my canvas
158 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
):
159 wxScrolledWindow(parent
, -1, pos
, size
)
163 MyCanvas::~MyCanvas(void)
167 // Define the repainting behaviour
168 void MyCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
171 dc
.SetPen(wxRED_PEN
);
174 for ( i
= 0; i
< 500; i
+= 10)
176 dc
.DrawLine(0, i
, 800, i
);
178 if ( g_TestBitmap
&& g_TestBitmap
->Ok() )
181 if ( g_TestBitmap
->GetColourMap() )
183 memDC
.SetColourMap(g_TestBitmap
->GetColourMap());
184 dc
.SetColourMap(g_TestBitmap
->GetColourMap());
186 memDC
.SelectObject(g_TestBitmap
);
188 // Normal, non-transparent blitting
189 dc
.Blit(20, 20, g_TestBitmap
->GetWidth(), g_TestBitmap
->GetHeight(), & memDC
, 0, 0, wxCOPY
, FALSE
);
191 memDC
.SelectObject(wxNullBitmap
);
194 if ( g_TestBitmap
&& g_TestBitmap
->Ok() )
197 memDC
.SelectObject(g_TestBitmap
);
199 // Transparent blitting if there's a mask in the bitmap
200 dc
.Blit(20 + g_TestBitmap
->GetWidth() + 20, 20, g_TestBitmap
->GetWidth(), g_TestBitmap
->GetHeight(), & memDC
,
203 memDC
.SelectObject(wxNullBitmap
);
207 // Define the behaviour for the frame closing
208 // - must delete all frames except for the main one.
209 bool MyFrame::OnClose(void)