]> git.saurik.com Git - wxWidgets.git/blame - samples/shaped/shaped.cpp
64 bit fixes
[wxWidgets.git] / samples / shaped / shaped.cpp
CommitLineData
1542ea39
RD
1/////////////////////////////////////////////////////////////////////////////
2// Name: shaped.cpp
3// Purpose: Shaped Window sample
4// Author: Robin Dunn
5// Modified by:
6// Created: 28-Mar-2003
7// RCS-ID: $Id$
8// Copyright: (c) Robin Dunn
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
1542ea39
RD
20// For compilers that support precompilation, includes "wx/wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
27// for all others, include the necessary headers
28#ifndef WX_PRECOMP
29 #include "wx/app.h"
30 #include "wx/log.h"
31 #include "wx/frame.h"
32 #include "wx/panel.h"
33 #include "wx/stattext.h"
34 #include "wx/menu.h"
35 #include "wx/layout.h"
36 #include "wx/msgdlg.h"
6a7e6411 37 #include "wx/image.h"
1542ea39
RD
38#endif
39
40#include "wx/dcclient.h"
3b3ca9be 41#include "wx/image.h"
1542ea39
RD
42
43// ----------------------------------------------------------------------------
44// private classes
45// ----------------------------------------------------------------------------
46
47// Define a new application type, each program should derive a class from wxApp
48class MyApp : public wxApp
49{
50public:
51 // override base class virtuals
52 // ----------------------------
53
54 // this one is called on application startup and is a good place for the app
55 // initialization (doing it here and not in the ctor allows to have an error
56 // return: if OnInit() returns false, the application terminates)
57 virtual bool OnInit();
58};
59
60
4488a1d3
VZ
61// Define a new frame type: this is going to the frame showing the
62// effect of wxFRAME_SHAPED
1542ea39
RD
63class ShapedFrame : public wxFrame
64{
65public:
66 // ctor(s)
4488a1d3 67 ShapedFrame(wxFrame *parent);
1542ea39
RD
68 void SetWindowShape();
69
70 // event handlers (these functions should _not_ be virtual)
71 void OnDoubleClick(wxMouseEvent& evt);
72 void OnLeftDown(wxMouseEvent& evt);
73 void OnLeftUp(wxMouseEvent& evt);
74 void OnMouseMove(wxMouseEvent& evt);
75 void OnExit(wxMouseEvent& evt);
76 void OnPaint(wxPaintEvent& evt);
77 void OnWindowCreate(wxWindowCreateEvent& evt);
78
79private:
80 bool m_hasShape;
81 wxBitmap m_bmp;
82 wxPoint m_delta;
83
be5a51fb 84 // any class wishing to process wxWidgets events must use this macro
1542ea39
RD
85 DECLARE_EVENT_TABLE()
86};
87
4488a1d3
VZ
88// Define a new frame type: this is going to the frame showing the
89// effect of wxWindow::SetTransparent and of
90// wxWindow::SetBackgroundStyle(wxBG_STYLE_TRANSPARENT)
91class SeeThroughFrame : public wxFrame
92{
93public:
94 // ctor(s)
95 SeeThroughFrame();
96
97 // event handlers (these functions should _not_ be virtual)
98 void OnDoubleClick(wxMouseEvent& evt);
99 void OnPaint(wxPaintEvent& evt);
100 void OnSize(wxSizeEvent& evt);
101
102private:
103 enum State
104 {
105 STATE_SEETHROUGH,
106 STATE_TRANSPARENT,
107 STATE_OPAQUE,
108 STATE_MAX
109 };
110
111 State m_currentState;
112
113 // any class wishing to process wxWidgets events must use this macro
114 DECLARE_EVENT_TABLE()
115};
116
1542ea39
RD
117
118// ----------------------------------------------------------------------------
be5a51fb 119// event tables and other macros for wxWidgets
1542ea39
RD
120// ----------------------------------------------------------------------------
121
be5a51fb 122// the event tables connect the wxWidgets events with the functions (event
1542ea39
RD
123// handlers) which process them. It can be also done at run-time, but for the
124// simple menu events like this the static method is much simpler.
125BEGIN_EVENT_TABLE(ShapedFrame, wxFrame)
126 EVT_LEFT_DCLICK(ShapedFrame::OnDoubleClick)
127 EVT_LEFT_DOWN(ShapedFrame::OnLeftDown)
128 EVT_LEFT_UP(ShapedFrame::OnLeftUp)
129 EVT_MOTION(ShapedFrame::OnMouseMove)
130 EVT_RIGHT_UP(ShapedFrame::OnExit)
131
132 EVT_PAINT(ShapedFrame::OnPaint)
133
134#ifdef __WXGTK__
135 EVT_WINDOW_CREATE(ShapedFrame::OnWindowCreate)
136#endif
137END_EVENT_TABLE()
138
139
be5a51fb 140// Create a new application object: this macro will allow wxWidgets to create
1542ea39
RD
141// the application object during program execution (it's better than using a
142// static object for many reasons) and also declares the accessor function
143// wxGetApp() which will return the reference of the right type (i.e. MyApp and
144// not wxApp)
145IMPLEMENT_APP(MyApp)
146
147// ============================================================================
148// implementation
149// ============================================================================
150
151// ----------------------------------------------------------------------------
152// the application class
153// ----------------------------------------------------------------------------
154
155// `Main program' equivalent: the program execution "starts" here
156bool MyApp::OnInit()
157{
45e6e6f8
VZ
158 if ( !wxApp::OnInit() )
159 return false;
160
1542ea39
RD
161 wxInitAllImageHandlers();
162
4488a1d3
VZ
163 // Create the transparent window
164 SeeThroughFrame *seeThroughFrame = new SeeThroughFrame();
165 seeThroughFrame->Show(true);
166 SetTopWindow(seeThroughFrame);
167
168 // Create the shaped window
169 ShapedFrame *shapedFrame = new ShapedFrame(seeThroughFrame);
170 shapedFrame->Show(true);
1542ea39
RD
171
172 // success: wxApp::OnRun() will be called which will enter the main message
b62ca03d 173 // loop and the application will run. If we returned false here, the
1542ea39 174 // application would exit immediately.
b62ca03d 175 return true;
1542ea39
RD
176}
177
178// ----------------------------------------------------------------------------
4488a1d3 179// shaped frame
1542ea39
RD
180// ----------------------------------------------------------------------------
181
182// frame constructor
4488a1d3
VZ
183ShapedFrame::ShapedFrame(wxFrame *parent)
184 : wxFrame(parent, wxID_ANY, wxEmptyString,
6a7e6411
RD
185 wxDefaultPosition, wxSize(100, 100), //wxDefaultSize,
186 0
187 | wxFRAME_SHAPED
188 | wxSIMPLE_BORDER
189 | wxFRAME_NO_TASKBAR
190 | wxSTAY_ON_TOP
191 )
1542ea39 192{
b62ca03d 193 m_hasShape = false;
7df07b10 194 m_bmp = wxBitmap(_T("star.png"), wxBITMAP_TYPE_PNG);
1542ea39
RD
195 SetSize(wxSize(m_bmp.GetWidth(), m_bmp.GetHeight()));
196 SetToolTip(wxT("Right-click to exit"));
94a5c401 197
6a7e6411 198#ifndef __WXGTK__
1542ea39 199 // On wxGTK we can't do this yet because the window hasn't been created
6a7e6411
RD
200 // yet so we wait until the EVT_WINDOW_CREATE event happens. On wxMSW and
201 // wxMac the window has been created at this point so we go ahead and set
202 // the shape now.
1542ea39
RD
203 SetWindowShape();
204#endif
205}
206
207void ShapedFrame::SetWindowShape()
208{
209 wxRegion region(m_bmp, *wxWHITE);
210 m_hasShape = SetShape(region);
211}
212
256b8649 213void ShapedFrame::OnDoubleClick(wxMouseEvent& WXUNUSED(evt))
1542ea39
RD
214{
215 if (m_hasShape)
216 {
217 wxRegion region;
218 SetShape(region);
b62ca03d 219 m_hasShape = false;
1542ea39
RD
220 }
221 else
222 SetWindowShape();
223}
224
225void ShapedFrame::OnLeftDown(wxMouseEvent& evt)
226{
227 CaptureMouse();
6a7e6411 228 //printf("Mouse captured\n");
1542ea39
RD
229 wxPoint pos = ClientToScreen(evt.GetPosition());
230 wxPoint origin = GetPosition();
231 int dx = pos.x - origin.x;
232 int dy = pos.y - origin.y;
233 m_delta = wxPoint(dx, dy);
234}
235
256b8649 236void ShapedFrame::OnLeftUp(wxMouseEvent& WXUNUSED(evt))
1542ea39
RD
237{
238 if (HasCapture())
6a7e6411 239 {
1542ea39 240 ReleaseMouse();
6a7e6411 241 //printf("Mouse released\n");
256b8649 242 }
1542ea39
RD
243}
244
245void ShapedFrame::OnMouseMove(wxMouseEvent& evt)
246{
6a7e6411
RD
247 wxPoint pt = evt.GetPosition();
248 //printf("x:%d y:%d\n", pt.x, pt.y);
1542ea39
RD
249 if (evt.Dragging() && evt.LeftIsDown())
250 {
6a7e6411 251 wxPoint pos = ClientToScreen(pt);
1542ea39
RD
252 Move(wxPoint(pos.x - m_delta.x, pos.y - m_delta.y));
253 }
254}
255
256b8649 256void ShapedFrame::OnExit(wxMouseEvent& WXUNUSED(evt))
1542ea39
RD
257{
258 Close();
259}
260
256b8649 261void ShapedFrame::OnPaint(wxPaintEvent& WXUNUSED(evt))
1542ea39
RD
262{
263 wxPaintDC dc(this);
b62ca03d 264 dc.DrawBitmap(m_bmp, 0, 0, true);
1542ea39
RD
265}
266
256b8649 267void ShapedFrame::OnWindowCreate(wxWindowCreateEvent& WXUNUSED(evt))
1542ea39
RD
268{
269 SetWindowShape();
270}
271
4488a1d3
VZ
272// ----------------------------------------------------------------------------
273// see-through frame
274// ----------------------------------------------------------------------------
275
276// frame constructor
277SeeThroughFrame::SeeThroughFrame()
278 : wxFrame(NULL, wxID_ANY, "Transparency test: double click here",
279 wxPoint(100, 30), wxSize(300, 300),
280 wxDEFAULT_FRAME_STYLE | wxSTAY_ON_TOP),
281 m_currentState(STATE_SEETHROUGH)
282{
283 SetBackgroundColour(wxColour(255, 255, 255, 255));
284 SetBackgroundStyle(wxBG_STYLE_TRANSPARENT);
285}
286
287// Redraws the whole window on resize
288void SeeThroughFrame::OnSize(wxSizeEvent& WXUNUSED(evt))
289{
290 Refresh();
291}
292
293// Paints a grid of varying hue and alpha
294void SeeThroughFrame::OnPaint(wxPaintEvent& WXUNUSED(evt))
295{
296 wxPaintDC dc(this);
297 dc.SetPen(wxNullPen);
298
299 int xcount = 8;
300 int ycount = 8;
301
302 float xstep = 1. / xcount;
303 float ystep = 1. / ycount;
304
305 int width = GetClientSize().GetWidth();
306 int height = GetClientSize().GetHeight();
307
308 for ( float x = 0.; x < 1.; x += xstep )
309 {
310 for ( float y = 0.; y < 1.; y += ystep )
311 {
312 wxImage::RGBValue v = wxImage::HSVtoRGB(wxImage::HSVValue(x, 1., 1.));
313 dc.SetBrush(wxBrush(wxColour(v.red, v.green, v.blue,
314 (int)(255*(1. - y)))));
315 int x1 = (int)(x * width);
316 int y1 = (int)(y * height);
317 int x2 = (int)((x + xstep) * width);
318 int y2 = (int)((y + ystep) * height);
319 dc.DrawRectangle(x1, y1, x2 - x1, y2 - y1);
320 }
321 }
322}
323
324// Switches between colour and transparent background on doubleclick
325void SeeThroughFrame::OnDoubleClick(wxMouseEvent& WXUNUSED(evt))
326{
327 m_currentState = (State)((m_currentState + 1) % STATE_MAX);
328
329 switch ( m_currentState )
330 {
331 case STATE_OPAQUE:
332 SetBackgroundStyle(wxBG_STYLE_COLOUR);
333 SetTransparent(255);
334 SetTitle("Opaque");
335 break;
336
337 case STATE_SEETHROUGH:
338 SetBackgroundStyle(wxBG_STYLE_TRANSPARENT);
339 SetTransparent(255);
340 SetTitle("See through");
341 break;
342
343 case STATE_TRANSPARENT:
344 SetBackgroundStyle(wxBG_STYLE_COLOUR);
345 SetTransparent(128);
346 SetTitle("Semi-transparent");
347 break;
348
349 case STATE_MAX:
350 wxFAIL_MSG( "unreachable" );
351 }
352
353 Refresh();
354}
355
356BEGIN_EVENT_TABLE(SeeThroughFrame, wxFrame)
357 EVT_LEFT_DCLICK(SeeThroughFrame::OnDoubleClick)
358 EVT_PAINT(SeeThroughFrame::OnPaint)
359 EVT_SIZE(SeeThroughFrame::OnSize)
360END_EVENT_TABLE()
361