]> git.saurik.com Git - wxWidgets.git/blob - samples/png/pngdemo.cpp
Add wx.ListBox.HitTest
[wxWidgets.git] / samples / png / pngdemo.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: pngdemo.cpp
3 // Purpose: Demos PNG reading
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #include "wx/image.h"
20
21 #include "pngdemo.h"
22
23 MyFrame *frame = (MyFrame *) NULL;
24 wxBitmap *g_TestBitmap = (wxBitmap *) NULL;
25
26 IMPLEMENT_APP(MyApp)
27
28 bool MyApp::OnInit(void)
29 {
30 wxImage::AddHandler(new wxPNGHandler);
31
32 // Create the main frame window
33 frame = new MyFrame((wxFrame *) NULL, _T("wxPNGBitmap Demo"), wxPoint(0, 0), wxSize(300, 300));
34
35 #if wxUSE_STATUSBAR
36 // Give it a status line
37 frame->CreateStatusBar(2);
38 #endif // wxUSE_STATUSBAR
39
40 // Make a menubar
41 wxMenu *file_menu = new wxMenu;
42 wxMenu *help_menu = new wxMenu;
43
44 file_menu->Append(PNGDEMO_LOAD_FILE, _T("&Load file"), _T("Load file"));
45 file_menu->Append(PNGDEMO_SAVE_FILE, _T("&Save file"), _T("Save file"));
46 file_menu->Append(PNGDEMO_QUIT, _T("E&xit"), _T("Quit program"));
47 help_menu->Append(PNGDEMO_ABOUT, _T("&About"), _T("About PNG demo"));
48
49 wxMenuBar *menu_bar = new wxMenuBar;
50
51 menu_bar->Append(file_menu, _T("&File"));
52 menu_bar->Append(help_menu, _T("&Help"));
53
54 // Associate the menu bar with the frame
55 frame->SetMenuBar(menu_bar);
56
57 MyCanvas *canvas = new MyCanvas(frame, wxPoint(0, 0), wxSize(100, 100));
58
59 // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
60 // canvas->SetScrollbars(20, 20, 50, 50, 4, 4);
61 frame->canvas = canvas;
62
63 frame->Show(true);
64
65 #if wxUSE_STATUSBAR
66 frame->SetStatusText(_T("Hello, wxWidgets"));
67 #endif // wxUSE_STATUSBAR
68
69 return true;
70 }
71
72 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
73 EVT_MENU(PNGDEMO_QUIT, MyFrame::OnQuit)
74 EVT_MENU(PNGDEMO_ABOUT, MyFrame::OnAbout)
75 EVT_MENU(PNGDEMO_LOAD_FILE, MyFrame::OnLoadFile)
76 EVT_MENU(PNGDEMO_SAVE_FILE, MyFrame::OnSaveFile)
77 END_EVENT_TABLE()
78
79 // Define my frame constructor
80 MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size):
81 wxFrame(frame, wxID_ANY, title, pos, size)
82 {
83 canvas = (MyCanvas *) NULL;
84 }
85
86 // frame destructor
87 MyFrame::~MyFrame()
88 {
89 if (g_TestBitmap)
90 {
91 delete g_TestBitmap;
92 g_TestBitmap = (wxBitmap *) NULL;
93 }
94 }
95
96 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
97 {
98 Close(true);
99 }
100
101 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
102 {
103 (void)wxMessageBox(_T("PNG demo\nJulian Smart (c) 1998"),
104 _T("About PNG Demo"), wxOK);
105 }
106
107 void MyFrame::OnSaveFile(wxCommandEvent& WXUNUSED(event))
108 {
109 #if wxUSE_FILEDLG
110 wxString f = wxFileSelector( wxT("Save Image"), (const wxChar *)NULL,
111 (const wxChar *)NULL,
112 wxT("png"), wxT("PNG files (*.png)|*.png") );
113
114 if (f.empty()) return;
115
116 wxBitmap *backstore = new wxBitmap( 150, 150 );
117
118 wxMemoryDC memDC;
119 memDC.SelectObject( *backstore );
120 memDC.Clear();
121 memDC.SetBrush( *wxBLACK_BRUSH );
122 memDC.SetPen( *wxWHITE_PEN );
123 memDC.DrawRectangle( 0, 0, 150, 150 );
124 memDC.SetPen( *wxBLACK_PEN );
125 memDC.DrawLine( 0, 0, 0, 10 );
126 memDC.SetTextForeground( *wxWHITE );
127 memDC.DrawText( _T("This is a memory dc."), 10, 10 );
128
129 memDC.SelectObject( wxNullBitmap );
130
131 backstore->SaveFile( f, wxBITMAP_TYPE_PNG, (wxPalette*)NULL );
132
133 delete backstore;
134 #endif // wxUSE_FILEDLG
135 }
136
137 void MyFrame::OnLoadFile(wxCommandEvent& WXUNUSED(event))
138 {
139 #if wxUSE_FILEDLG
140 // Show file selector.
141 wxString f = wxFileSelector(wxT("Open Image"), (const wxChar *) NULL,
142 (const wxChar *) NULL, wxT("png"),
143 wxT("PNG files (*.png)|*.png"));
144
145 if (f.empty())
146 return;
147
148 if ( g_TestBitmap )
149 delete g_TestBitmap;
150
151 g_TestBitmap = new wxBitmap(f, wxBITMAP_TYPE_PNG);
152 if (!g_TestBitmap->Ok())
153 {
154 delete g_TestBitmap;
155 g_TestBitmap = (wxBitmap *) NULL;
156 }
157
158 canvas->Refresh();
159 #endif // wxUSE_FILEDLG
160 }
161
162 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
163 EVT_PAINT(MyCanvas::OnPaint)
164 END_EVENT_TABLE()
165
166 // Define a constructor for my canvas
167 MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size):
168 wxScrolledWindow(parent, wxID_ANY, pos, size)
169 {
170 }
171
172 // Define the repainting behaviour
173 void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
174 {
175 wxPaintDC dc(this);
176 dc.SetPen(* wxRED_PEN);
177
178 int i;
179 for ( i = 0; i < 500; i += 10)
180 {
181 dc.DrawLine(0, i, 800, i);
182 }
183 if ( g_TestBitmap && g_TestBitmap->Ok() )
184 {
185 wxMemoryDC memDC;
186 if ( g_TestBitmap->GetPalette() )
187 {
188 memDC.SetPalette(* g_TestBitmap->GetPalette());
189 dc.SetPalette(* g_TestBitmap->GetPalette());
190 }
191 memDC.SelectObject(* g_TestBitmap);
192
193 // Normal, non-transparent blitting
194 dc.Blit(20, 20, g_TestBitmap->GetWidth(), g_TestBitmap->GetHeight(), & memDC, 0, 0, wxCOPY, false);
195
196 memDC.SelectObject(wxNullBitmap);
197 }
198
199 if ( g_TestBitmap && g_TestBitmap->Ok() )
200 {
201 wxMemoryDC memDC;
202 memDC.SelectObject(* g_TestBitmap);
203
204 // Transparent blitting if there's a mask in the bitmap
205 dc.Blit(20 + g_TestBitmap->GetWidth() + 20, 20, g_TestBitmap->GetWidth(), g_TestBitmap->GetHeight(), & memDC,
206 0, 0, wxCOPY, true);
207
208 memDC.SelectObject(wxNullBitmap);
209 }
210 }