]>
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
= NULL
;
30 wxBitmap
*g_TestBitmap
= NULL
;
38 bool MyApp::OnInit(void)
41 wxBitmap::AddHandler(new wxPNGFileHandler
);
44 // Create the main frame window
45 frame
= new MyFrame(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_QUIT
, "E&xit", "Quit program");
56 help_menu
->Append(PNGDEMO_ABOUT
, "&About", "About PNG demo");
58 wxMenuBar
*menu_bar
= new wxMenuBar
;
60 menu_bar
->Append(file_menu
, "&File");
61 menu_bar
->Append(help_menu
, "&Help");
63 // Associate the menu bar with the frame
64 frame
->SetMenuBar(menu_bar
);
66 MyCanvas
*canvas
= new MyCanvas(frame
, wxPoint(0, 0), wxSize(100, 100));
68 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
69 // canvas->SetScrollbars(20, 20, 50, 50, 4, 4);
70 frame
->canvas
= canvas
;
74 frame
->SetStatusText("Hello, wxWindows");
79 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
80 EVT_MENU(PNGDEMO_QUIT
, MyFrame::OnQuit
)
81 EVT_MENU(PNGDEMO_ABOUT
, MyFrame::OnAbout
)
82 EVT_MENU(PNGDEMO_LOAD_FILE
, MyFrame::OnLoadFile
)
85 // Define my frame constructor
86 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
87 wxFrame(frame
, -1, title
, pos
, size
)
92 void MyFrame::OnQuit(wxCommandEvent
& event
)
97 void MyFrame::OnAbout(wxCommandEvent
& event
)
99 (void)wxMessageBox("PNG demo\nJulian Smart (c) 1998",
100 "About PNG Demo", wxOK
);
103 void MyFrame::OnLoadFile(wxCommandEvent
& event
)
105 // Show file selector.
106 char *f
= wxFileSelector("Open Image", NULL
, NULL
,"png",
107 "PNG files (*.png)|*.png");
114 g_TestBitmap
= new wxBitmap(f
, wxBITMAP_TYPE_PNG
);
115 if (!g_TestBitmap
->Ok())
124 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
125 EVT_PAINT(MyCanvas::OnPaint
)
128 // Define a constructor for my canvas
129 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
):
130 wxScrolledWindow(parent
, -1, pos
, size
)
134 MyCanvas::~MyCanvas(void)
138 // Define the repainting behaviour
139 void MyCanvas::OnPaint(wxPaintEvent
& event
)
142 dc
.SetPen(wxRED_PEN
);
145 for ( i
= 0; i
< 500; i
+= 10)
147 dc
.DrawLine(0, i
, 800, i
);
149 if ( g_TestBitmap
&& g_TestBitmap
->Ok() )
152 if ( g_TestBitmap
->GetColourMap() )
154 memDC
.SetColourMap(g_TestBitmap
->GetColourMap());
155 dc
.SetColourMap(g_TestBitmap
->GetColourMap());
157 memDC
.SelectObject(g_TestBitmap
);
159 // Normal, non-transparent blitting
160 dc
.Blit(20, 20, g_TestBitmap
->GetWidth(), g_TestBitmap
->GetHeight(), & memDC
, 0, 0, wxCOPY
, FALSE
);
162 memDC
.SelectObject(wxNullBitmap
);
165 if ( g_TestBitmap
&& g_TestBitmap
->Ok() )
168 memDC
.SelectObject(g_TestBitmap
);
170 // Transparent blitting if there's a mask in the bitmap
171 dc
.Blit(20 + g_TestBitmap
->GetWidth() + 20, 20, g_TestBitmap
->GetWidth(), g_TestBitmap
->GetHeight(), & memDC
,
174 memDC
.SelectObject(wxNullBitmap
);
178 // Define the behaviour for the frame closing
179 // - must delete all frames except for the main one.
180 bool MyFrame::OnClose(void)