]> git.saurik.com Git - wxWidgets.git/blob - samples/shaped/shaped.cpp
cleanup
[wxWidgets.git] / samples / shaped / shaped.cpp
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
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"
37 #include "wx/image.h"
38 #endif
39
40 #include "wx/dcclient.h"
41 #include "wx/image.h"
42
43 // ----------------------------------------------------------------------------
44 // private classes
45 // ----------------------------------------------------------------------------
46
47 // Define a new application type, each program should derive a class from wxApp
48 class MyApp : public wxApp
49 {
50 public:
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
61 // Define a new frame type: this is going to the frame showing the
62 // effect of wxFRAME_SHAPED
63 class ShapedFrame : public wxFrame
64 {
65 public:
66 // ctor(s)
67 ShapedFrame(wxFrame *parent);
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
79 private:
80 bool m_hasShape;
81 wxBitmap m_bmp;
82 wxPoint m_delta;
83
84 // any class wishing to process wxWidgets events must use this macro
85 DECLARE_EVENT_TABLE()
86 };
87
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)
91 class SeeThroughFrame : public wxFrame
92 {
93 public:
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
102 private:
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
117
118 // ----------------------------------------------------------------------------
119 // event tables and other macros for wxWidgets
120 // ----------------------------------------------------------------------------
121
122 // the event tables connect the wxWidgets events with the functions (event
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.
125 BEGIN_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
137 END_EVENT_TABLE()
138
139
140 // Create a new application object: this macro will allow wxWidgets to create
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)
145 IMPLEMENT_APP(MyApp)
146
147 // ============================================================================
148 // implementation
149 // ============================================================================
150
151 // ----------------------------------------------------------------------------
152 // the application class
153 // ----------------------------------------------------------------------------
154
155 // `Main program' equivalent: the program execution "starts" here
156 bool MyApp::OnInit()
157 {
158 if ( !wxApp::OnInit() )
159 return false;
160
161 wxInitAllImageHandlers();
162
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);
171
172 // success: wxApp::OnRun() will be called which will enter the main message
173 // loop and the application will run. If we returned false here, the
174 // application would exit immediately.
175 return true;
176 }
177
178 // ----------------------------------------------------------------------------
179 // shaped frame
180 // ----------------------------------------------------------------------------
181
182 // frame constructor
183 ShapedFrame::ShapedFrame(wxFrame *parent)
184 : wxFrame(parent, wxID_ANY, wxEmptyString,
185 wxDefaultPosition, wxSize(100, 100), //wxDefaultSize,
186 0
187 | wxFRAME_SHAPED
188 | wxSIMPLE_BORDER
189 | wxFRAME_NO_TASKBAR
190 | wxSTAY_ON_TOP
191 )
192 {
193 m_hasShape = false;
194 m_bmp = wxBitmap(_T("star.png"), wxBITMAP_TYPE_PNG);
195 SetSize(wxSize(m_bmp.GetWidth(), m_bmp.GetHeight()));
196 SetToolTip(wxT("Right-click to exit"));
197
198 #ifndef __WXGTK__
199 // On wxGTK we can't do this yet because the window hasn't been created
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.
203 SetWindowShape();
204 #endif
205 }
206
207 void ShapedFrame::SetWindowShape()
208 {
209 wxRegion region(m_bmp, *wxWHITE);
210 m_hasShape = SetShape(region);
211 }
212
213 void ShapedFrame::OnDoubleClick(wxMouseEvent& WXUNUSED(evt))
214 {
215 if (m_hasShape)
216 {
217 wxRegion region;
218 SetShape(region);
219 m_hasShape = false;
220 }
221 else
222 SetWindowShape();
223 }
224
225 void ShapedFrame::OnLeftDown(wxMouseEvent& evt)
226 {
227 CaptureMouse();
228 //printf("Mouse captured\n");
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
236 void ShapedFrame::OnLeftUp(wxMouseEvent& WXUNUSED(evt))
237 {
238 if (HasCapture())
239 {
240 ReleaseMouse();
241 //printf("Mouse released\n");
242 }
243 }
244
245 void ShapedFrame::OnMouseMove(wxMouseEvent& evt)
246 {
247 wxPoint pt = evt.GetPosition();
248 //printf("x:%d y:%d\n", pt.x, pt.y);
249 if (evt.Dragging() && evt.LeftIsDown())
250 {
251 wxPoint pos = ClientToScreen(pt);
252 Move(wxPoint(pos.x - m_delta.x, pos.y - m_delta.y));
253 }
254 }
255
256 void ShapedFrame::OnExit(wxMouseEvent& WXUNUSED(evt))
257 {
258 Close();
259 }
260
261 void ShapedFrame::OnPaint(wxPaintEvent& WXUNUSED(evt))
262 {
263 wxPaintDC dc(this);
264 dc.DrawBitmap(m_bmp, 0, 0, true);
265 }
266
267 void ShapedFrame::OnWindowCreate(wxWindowCreateEvent& WXUNUSED(evt))
268 {
269 SetWindowShape();
270 }
271
272 // ----------------------------------------------------------------------------
273 // see-through frame
274 // ----------------------------------------------------------------------------
275
276 // frame constructor
277 SeeThroughFrame::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
288 void SeeThroughFrame::OnSize(wxSizeEvent& WXUNUSED(evt))
289 {
290 Refresh();
291 }
292
293 // Paints a grid of varying hue and alpha
294 void 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
325 void 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
356 BEGIN_EVENT_TABLE(SeeThroughFrame, wxFrame)
357 EVT_LEFT_DCLICK(SeeThroughFrame::OnDoubleClick)
358 EVT_PAINT(SeeThroughFrame::OnPaint)
359 EVT_SIZE(SeeThroughFrame::OnSize)
360 END_EVENT_TABLE()
361