]> git.saurik.com Git - wxWidgets.git/blame - samples/png/pngdemo.cpp
Bool replaced by bool
[wxWidgets.git] / samples / png / pngdemo.cpp
CommitLineData
c801d85f
KB
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 and Markus Holzem
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "pngdemo.h"
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20#pragma hdrstop
21#endif
22
23#ifdef __WINDOWS__
24#include <wx/pnghand.h>
25#endif
26
27#include "pngdemo.h"
28
29MyFrame *frame = NULL;
30wxBitmap *g_TestBitmap = NULL;
31
32IMPLEMENT_APP(MyApp)
33
34MyApp::MyApp()
35{
36}
37
38bool MyApp::OnInit(void)
39{
40#ifdef __WINDOWS__
41 wxBitmap::AddHandler(new wxPNGFileHandler);
42#endif
43
44 // Create the main frame window
45 frame = new MyFrame(NULL, "wxPNGBitmap Demo", wxPoint(0, 0), wxSize(300, 300));
46
47 // Give it a status line
48 frame->CreateStatusBar(2);
49
50 // Make a menubar
51 wxMenu *file_menu = new wxMenu;
52 wxMenu *help_menu = new wxMenu;
53
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");
57
58 wxMenuBar *menu_bar = new wxMenuBar;
59
60 menu_bar->Append(file_menu, "&File");
61 menu_bar->Append(help_menu, "&Help");
62
63 // Associate the menu bar with the frame
64 frame->SetMenuBar(menu_bar);
65
66 MyCanvas *canvas = new MyCanvas(frame, wxPoint(0, 0), wxSize(100, 100));
67
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;
71
72 frame->Show(TRUE);
73
74 frame->SetStatusText("Hello, wxWindows");
75
76 return TRUE;
77}
78
79BEGIN_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)
83END_EVENT_TABLE()
84
85// Define my frame constructor
86MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size):
87 wxFrame(frame, -1, title, pos, size)
88{
89 canvas = NULL;
90}
91
92void MyFrame::OnQuit(wxCommandEvent& event)
93{
94 Close(TRUE);
95}
96
97void MyFrame::OnAbout(wxCommandEvent& event)
98{
99 (void)wxMessageBox("PNG demo\nJulian Smart (c) 1998",
100 "About PNG Demo", wxOK);
101}
102
103void MyFrame::OnLoadFile(wxCommandEvent& event)
104{
105 // Show file selector.
106 char *f = wxFileSelector("Open Image", NULL, NULL,"png",
107 "PNG files (*.png)|*.png");
108
109 if (!f)
110 return;
111
112 if ( g_TestBitmap )
113 delete g_TestBitmap;
114 g_TestBitmap = new wxBitmap(f, wxBITMAP_TYPE_PNG);
115 if (!g_TestBitmap->Ok())
116 {
117 delete g_TestBitmap;
118 g_TestBitmap = NULL;
119 }
120
121 canvas->Refresh();
122}
123
124BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
125 EVT_PAINT(MyCanvas::OnPaint)
126END_EVENT_TABLE()
127
128// Define a constructor for my canvas
129MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size):
130 wxScrolledWindow(parent, -1, pos, size)
131{
132}
133
134MyCanvas::~MyCanvas(void)
135{
136}
137
138// Define the repainting behaviour
139void MyCanvas::OnPaint(wxPaintEvent& event)
140{
141 wxPaintDC dc(this);
142 dc.SetPen(wxRED_PEN);
143
144 int i;
145 for ( i = 0; i < 500; i += 10)
146 {
147 dc.DrawLine(0, i, 800, i);
148 }
149 if ( g_TestBitmap && g_TestBitmap->Ok() )
150 {
151 wxMemoryDC memDC;
152 if ( g_TestBitmap->GetColourMap() )
153 {
154 memDC.SetColourMap(g_TestBitmap->GetColourMap());
155 dc.SetColourMap(g_TestBitmap->GetColourMap());
156 }
157 memDC.SelectObject(g_TestBitmap);
158
159 // Normal, non-transparent blitting
160 dc.Blit(20, 20, g_TestBitmap->GetWidth(), g_TestBitmap->GetHeight(), & memDC, 0, 0, wxCOPY, FALSE);
161
162 memDC.SelectObject(NULL);
163 }
164
165 if ( g_TestBitmap && g_TestBitmap->Ok() )
166 {
167 wxMemoryDC memDC;
168 memDC.SelectObject(g_TestBitmap);
169
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,
172 0, 0, wxCOPY, TRUE);
173
6e5ae051 174 memDC.SelectObject(wxNullBitmap);
c801d85f
KB
175 }
176}
177
178// Define the behaviour for the frame closing
179// - must delete all frames except for the main one.
180bool MyFrame::OnClose(void)
181{
182 Show(FALSE);
183
184 return TRUE;
185}
186