]>
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"
27 MyFrame
*frame
= (MyFrame
*) NULL
;
28 wxBitmap
*g_TestBitmap
= (wxBitmap
*) NULL
;
36 bool MyApp::OnInit(void)
38 wxImage::AddHandler(new wxPNGHandler
);
40 // Create the main frame window
41 frame
= new MyFrame((wxFrame
*) NULL
, "wxPNGBitmap Demo", wxPoint(0, 0), wxSize(300, 300));
43 // Give it a status line
44 frame
->CreateStatusBar(2);
47 wxMenu
*file_menu
= new wxMenu
;
48 wxMenu
*help_menu
= new wxMenu
;
50 file_menu
->Append(PNGDEMO_LOAD_FILE
, "&Load file", "Load file");
51 file_menu
->Append(PNGDEMO_SAVE_FILE
, "&Save file", "Save file");
52 file_menu
->Append(PNGDEMO_QUIT
, "E&xit", "Quit program");
53 help_menu
->Append(PNGDEMO_ABOUT
, "&About", "About PNG demo");
55 wxMenuBar
*menu_bar
= new wxMenuBar
;
57 menu_bar
->Append(file_menu
, "&File");
58 menu_bar
->Append(help_menu
, "&Help");
60 // Associate the menu bar with the frame
61 frame
->SetMenuBar(menu_bar
);
63 MyCanvas
*canvas
= new MyCanvas(frame
, wxPoint(0, 0), wxSize(100, 100));
65 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
66 // canvas->SetScrollbars(20, 20, 50, 50, 4, 4);
67 frame
->canvas
= canvas
;
71 frame
->SetStatusText("Hello, wxWindows");
76 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
77 EVT_MENU(PNGDEMO_QUIT
, MyFrame::OnQuit
)
78 EVT_MENU(PNGDEMO_ABOUT
, MyFrame::OnAbout
)
79 EVT_MENU(PNGDEMO_LOAD_FILE
, MyFrame::OnLoadFile
)
80 EVT_MENU(PNGDEMO_SAVE_FILE
, MyFrame::OnSaveFile
)
83 // Define my frame constructor
84 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
85 wxFrame(frame
, -1, title
, pos
, size
)
87 canvas
= (MyCanvas
*) NULL
;
90 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
95 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
97 (void)wxMessageBox("PNG demo\nJulian Smart (c) 1998",
98 "About PNG Demo", wxOK
);
101 void MyFrame::OnSaveFile(wxCommandEvent
& WXUNUSED(event
))
103 wxString f
= wxFileSelector( "Save Image", (const char *)NULL
, (const char *)NULL
,
104 "png", "PNG files (*.png)|*.png" );
108 wxBitmap
*backstore
= new wxBitmap( 150, 150 );
111 memDC
.SelectObject( *backstore
);
113 memDC
.SetBrush( *wxBLACK_BRUSH
);
114 memDC
.SetPen( *wxWHITE_PEN
);
115 memDC
.DrawRectangle( 0, 0, 150, 150 );
116 memDC
.SetPen( *wxBLACK_PEN
);
117 memDC
.DrawLine( 0, 0, 0, 10 );
118 memDC
.SetTextForeground( *wxWHITE
);
119 memDC
.DrawText( "This is a memory dc.", 10, 10 );
121 memDC
.SelectObject( wxNullBitmap
);
123 backstore
->SaveFile( f
, wxBITMAP_TYPE_PNG
, (wxPalette
*)NULL
);
128 void MyFrame::OnLoadFile(wxCommandEvent
& WXUNUSED(event
))
130 // Show file selector.
131 wxString f
= wxFileSelector("Open Image", (const char *) NULL
, (const char *) NULL
,"png",
132 "PNG files (*.png)|*.png");
139 g_TestBitmap
= new wxBitmap(f
, wxBITMAP_TYPE_PNG
);
140 if (!g_TestBitmap
->Ok())
143 g_TestBitmap
= (wxBitmap
*) NULL
;
149 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
150 EVT_PAINT(MyCanvas::OnPaint
)
153 // Define a constructor for my canvas
154 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
):
155 wxScrolledWindow(parent
, -1, pos
, size
)
159 MyCanvas::~MyCanvas(void)
163 // Define the repainting behaviour
164 void MyCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
167 dc
.SetPen(* wxRED_PEN
);
170 for ( i
= 0; i
< 500; i
+= 10)
172 dc
.DrawLine(0, i
, 800, i
);
174 if ( g_TestBitmap
&& g_TestBitmap
->Ok() )
177 if ( g_TestBitmap
->GetPalette() )
179 memDC
.SetPalette(* g_TestBitmap
->GetPalette());
180 dc
.SetPalette(* g_TestBitmap
->GetPalette());
182 memDC
.SelectObject(* g_TestBitmap
);
184 // Normal, non-transparent blitting
185 dc
.Blit(20, 20, g_TestBitmap
->GetWidth(), g_TestBitmap
->GetHeight(), & memDC
, 0, 0, wxCOPY
, FALSE
);
187 memDC
.SelectObject(wxNullBitmap
);
190 if ( g_TestBitmap
&& g_TestBitmap
->Ok() )
193 memDC
.SelectObject(* g_TestBitmap
);
195 // Transparent blitting if there's a mask in the bitmap
196 dc
.Blit(20 + g_TestBitmap
->GetWidth() + 20, 20, g_TestBitmap
->GetWidth(), g_TestBitmap
->GetHeight(), & memDC
,
199 memDC
.SelectObject(wxNullBitmap
);