]>
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>
25 // #include <wx/xpmhand.h>
30 MyFrame
*frame
= (MyFrame
*) NULL
;
31 wxBitmap
*g_TestBitmap
= (wxBitmap
*) NULL
;
39 bool MyApp::OnInit(void)
42 wxBitmap::AddHandler(new wxPNGFileHandler
);
43 // wxBitmap::AddHandler(new wxXPMFileHandler);
44 // wxBitmap::AddHandler(new wxXPMDataHandler);
47 // Create the main frame window
48 frame
= new MyFrame((wxFrame
*) NULL
, "wxPNGBitmap Demo", wxPoint(0, 0), wxSize(300, 300));
50 // Give it a status line
51 frame
->CreateStatusBar(2);
54 wxMenu
*file_menu
= new wxMenu
;
55 wxMenu
*help_menu
= new wxMenu
;
57 file_menu
->Append(PNGDEMO_LOAD_FILE
, "&Load file", "Load file");
58 file_menu
->Append(PNGDEMO_SAVE_FILE
, "&Save file", "Save file");
59 file_menu
->Append(PNGDEMO_QUIT
, "E&xit", "Quit program");
60 help_menu
->Append(PNGDEMO_ABOUT
, "&About", "About PNG demo");
62 wxMenuBar
*menu_bar
= new wxMenuBar
;
64 menu_bar
->Append(file_menu
, "&File");
65 menu_bar
->Append(help_menu
, "&Help");
67 // Associate the menu bar with the frame
68 frame
->SetMenuBar(menu_bar
);
70 MyCanvas
*canvas
= new MyCanvas(frame
, wxPoint(0, 0), wxSize(100, 100));
72 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
73 // canvas->SetScrollbars(20, 20, 50, 50, 4, 4);
74 frame
->canvas
= canvas
;
78 frame
->SetStatusText("Hello, wxWindows");
83 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
84 EVT_MENU(PNGDEMO_QUIT
, MyFrame::OnQuit
)
85 EVT_MENU(PNGDEMO_ABOUT
, MyFrame::OnAbout
)
86 EVT_MENU(PNGDEMO_LOAD_FILE
, MyFrame::OnLoadFile
)
87 EVT_MENU(PNGDEMO_SAVE_FILE
, MyFrame::OnSaveFile
)
90 // Define my frame constructor
91 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
92 wxFrame(frame
, -1, title
, pos
, size
)
94 canvas
= (MyCanvas
*) NULL
;
97 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
102 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
104 (void)wxMessageBox("PNG demo\nJulian Smart (c) 1998",
105 "About PNG Demo", wxOK
);
108 void MyFrame::OnSaveFile(wxCommandEvent
& WXUNUSED(event
))
110 wxString f
= wxFileSelector( "Save Image", (const char *)NULL
, (const char *)NULL
,
111 "png", "PNG files (*.png)|*.png" );
115 wxBitmap
*backstore
= new wxBitmap( 150, 150 );
118 memDC
.SelectObject( *backstore
);
120 memDC
.SetBrush( *wxBLACK_BRUSH
);
121 memDC
.SetPen( *wxWHITE_PEN
);
122 memDC
.DrawRectangle( 0, 0, 150, 150 );
123 memDC
.SetPen( *wxBLACK_PEN
);
124 memDC
.DrawLine( 0, 0, 0, 10 );
125 memDC
.SetTextForeground( *wxWHITE
);
126 memDC
.DrawText( "This is a memory dc.", 10, 10 );
128 memDC
.SelectObject( wxNullBitmap
);
130 backstore
->SaveFile( f
, wxBITMAP_TYPE_PNG
, (wxPalette
*)NULL
);
135 void MyFrame::OnLoadFile(wxCommandEvent
& WXUNUSED(event
))
137 // Show file selector.
138 wxString f
= wxFileSelector("Open Image", (const char *) NULL
, (const char *) NULL
,"png",
139 "PNG files (*.png)|*.png");
146 g_TestBitmap
= new wxBitmap(f
, wxBITMAP_TYPE_PNG
);
147 if (!g_TestBitmap
->Ok())
150 g_TestBitmap
= (wxBitmap
*) NULL
;
156 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
157 EVT_PAINT(MyCanvas::OnPaint
)
160 // Define a constructor for my canvas
161 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
):
162 wxScrolledWindow(parent
, -1, pos
, size
)
166 MyCanvas::~MyCanvas(void)
170 // Define the repainting behaviour
171 void MyCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
174 dc
.SetPen(* wxRED_PEN
);
177 for ( i
= 0; i
< 500; i
+= 10)
179 dc
.DrawLine(0, i
, 800, i
);
181 if ( g_TestBitmap
&& g_TestBitmap
->Ok() )
184 if ( g_TestBitmap
->GetPalette() )
186 memDC
.SetPalette(* g_TestBitmap
->GetPalette());
187 dc
.SetPalette(* g_TestBitmap
->GetPalette());
189 memDC
.SelectObject(* g_TestBitmap
);
191 // Normal, non-transparent blitting
192 dc
.Blit(20, 20, g_TestBitmap
->GetWidth(), g_TestBitmap
->GetHeight(), & memDC
, 0, 0, wxCOPY
, FALSE
);
194 memDC
.SelectObject(wxNullBitmap
);
197 if ( g_TestBitmap
&& g_TestBitmap
->Ok() )
200 memDC
.SelectObject(* g_TestBitmap
);
202 // Transparent blitting if there's a mask in the bitmap
203 dc
.Blit(20 + g_TestBitmap
->GetWidth() + 20, 20, g_TestBitmap
->GetWidth(), g_TestBitmap
->GetHeight(), & memDC
,
206 memDC
.SelectObject(wxNullBitmap
);