rewrite the sample to use a main frame to make it more understandable and also make...
[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 // constants
45 // ----------------------------------------------------------------------------
46
47 // menu ids
48 enum
49 {
50 Show_Shaped,
51 Show_Transparent
52 };
53
54 // ----------------------------------------------------------------------------
55 // private classes
56 // ----------------------------------------------------------------------------
57
58 // Define a new application type, each program should derive a class from wxApp
59 class MyApp : public wxApp
60 {
61 public:
62 // override base class virtuals
63 // ----------------------------
64
65 // this one is called on application startup and is a good place for the app
66 // initialization (doing it here and not in the ctor allows to have an error
67 // return: if OnInit() returns false, the application terminates)
68 virtual bool OnInit();
69 };
70
71
72 // Main frame just contains the menu items invoking the other tests
73 class MainFrame : public wxFrame
74 {
75 public:
76 MainFrame();
77
78 private:
79 void OnShowShaped(wxCommandEvent& event);
80 void OnShowTransparent(wxCommandEvent& event);
81
82 DECLARE_EVENT_TABLE()
83 };
84
85 // Define a new frame type: this is going to the frame showing the
86 // effect of wxFRAME_SHAPED
87 class ShapedFrame : public wxFrame
88 {
89 public:
90 // ctor(s)
91 ShapedFrame(wxFrame *parent);
92 void SetWindowShape();
93
94 // event handlers (these functions should _not_ be virtual)
95 void OnDoubleClick(wxMouseEvent& evt);
96 void OnLeftDown(wxMouseEvent& evt);
97 void OnLeftUp(wxMouseEvent& evt);
98 void OnMouseMove(wxMouseEvent& evt);
99 void OnExit(wxMouseEvent& evt);
100 void OnPaint(wxPaintEvent& evt);
101 void OnWindowCreate(wxWindowCreateEvent& evt);
102
103 private:
104 bool m_hasShape;
105 wxBitmap m_bmp;
106 wxPoint m_delta;
107
108 // any class wishing to process wxWidgets events must use this macro
109 DECLARE_EVENT_TABLE()
110 };
111
112 // Define a new frame type: this is going to the frame showing the
113 // effect of wxWindow::SetTransparent and of
114 // wxWindow::SetBackgroundStyle(wxBG_STYLE_TRANSPARENT)
115 class SeeThroughFrame : public wxFrame
116 {
117 public:
118 // ctor(s)
119 SeeThroughFrame();
120
121 // event handlers (these functions should _not_ be virtual)
122 void OnDoubleClick(wxMouseEvent& evt);
123 void OnPaint(wxPaintEvent& evt);
124
125 private:
126 enum State
127 {
128 STATE_SEETHROUGH,
129 STATE_TRANSPARENT,
130 STATE_OPAQUE,
131 STATE_MAX
132 };
133
134 State m_currentState;
135
136 // any class wishing to process wxWidgets events must use this macro
137 DECLARE_EVENT_TABLE()
138 };
139
140 // ============================================================================
141 // implementation
142 // ============================================================================
143
144 // ----------------------------------------------------------------------------
145 // the application class
146 // ----------------------------------------------------------------------------
147
148 IMPLEMENT_APP(MyApp)
149
150 // `Main program' equivalent: the program execution "starts" here
151 bool MyApp::OnInit()
152 {
153 if ( !wxApp::OnInit() )
154 return false;
155
156 wxInitAllImageHandlers();
157
158 new MainFrame;
159
160 // success: wxApp::OnRun() will be called which will enter the main message
161 // loop and the application will run. If we returned false here, the
162 // application would exit immediately.
163 return true;
164 }
165
166 // ----------------------------------------------------------------------------
167 // main frame
168 // ----------------------------------------------------------------------------
169
170 BEGIN_EVENT_TABLE(MainFrame, wxFrame)
171 EVT_MENU(Show_Shaped, MainFrame::OnShowShaped)
172 EVT_MENU(Show_Transparent, MainFrame::OnShowTransparent)
173 END_EVENT_TABLE()
174
175 MainFrame::MainFrame()
176 : wxFrame(NULL, wxID_ANY, "wxWidgets Shaped Sample",
177 wxDefaultPosition, wxSize(200, 100))
178 {
179 wxMenuBar * const mbar = new wxMenuBar;
180 wxMenu * const menuFrames = new wxMenu;
181 menuFrames->Append(Show_Shaped, "Show &shaped window\tCtrl-S");
182 menuFrames->Append(Show_Transparent, "Show &transparent window\tCtrl-T");
183 menuFrames->AppendSeparator();
184 menuFrames->Append(wxID_EXIT, "E&xit");
185
186 mbar->Append(menuFrames, "&Show");
187 SetMenuBar(mbar);
188
189 Show();
190 }
191
192 void MainFrame::OnShowShaped(wxCommandEvent& WXUNUSED(event))
193 {
194 ShapedFrame *shapedFrame = new ShapedFrame(this);
195 shapedFrame->Show(true);
196 }
197
198 void MainFrame::OnShowTransparent(wxCommandEvent& WXUNUSED(event))
199 {
200 SeeThroughFrame *seeThroughFrame = new SeeThroughFrame();
201 seeThroughFrame->Show(true);
202 }
203
204 // ----------------------------------------------------------------------------
205 // shaped frame
206 // ----------------------------------------------------------------------------
207
208 BEGIN_EVENT_TABLE(ShapedFrame, wxFrame)
209 EVT_LEFT_DCLICK(ShapedFrame::OnDoubleClick)
210 EVT_LEFT_DOWN(ShapedFrame::OnLeftDown)
211 EVT_LEFT_UP(ShapedFrame::OnLeftUp)
212 EVT_MOTION(ShapedFrame::OnMouseMove)
213 EVT_RIGHT_UP(ShapedFrame::OnExit)
214
215 EVT_PAINT(ShapedFrame::OnPaint)
216
217 #ifdef __WXGTK__
218 EVT_WINDOW_CREATE(ShapedFrame::OnWindowCreate)
219 #endif
220 END_EVENT_TABLE()
221
222
223 // frame constructor
224 ShapedFrame::ShapedFrame(wxFrame *parent)
225 : wxFrame(parent, wxID_ANY, wxEmptyString,
226 wxDefaultPosition, wxSize(100, 100),
227 0
228 | wxFRAME_SHAPED
229 | wxSIMPLE_BORDER
230 | wxFRAME_NO_TASKBAR
231 | wxSTAY_ON_TOP
232 )
233 {
234 m_hasShape = false;
235 m_bmp = wxBitmap(_T("star.png"), wxBITMAP_TYPE_PNG);
236 SetSize(wxSize(m_bmp.GetWidth(), m_bmp.GetHeight()));
237 SetToolTip(wxT("Right-click to close"));
238
239 #ifndef __WXGTK__
240 // On wxGTK we can't do this yet because the window hasn't been created
241 // yet so we wait until the EVT_WINDOW_CREATE event happens. On wxMSW and
242 // wxMac the window has been created at this point so we go ahead and set
243 // the shape now.
244 SetWindowShape();
245 #endif
246 }
247
248 void ShapedFrame::SetWindowShape()
249 {
250 wxRegion region(m_bmp, *wxWHITE);
251 m_hasShape = SetShape(region);
252 }
253
254 void ShapedFrame::OnDoubleClick(wxMouseEvent& WXUNUSED(evt))
255 {
256 if (m_hasShape)
257 {
258 wxRegion region;
259 SetShape(region);
260 m_hasShape = false;
261 }
262 else
263 SetWindowShape();
264 }
265
266 void ShapedFrame::OnLeftDown(wxMouseEvent& evt)
267 {
268 CaptureMouse();
269 wxPoint pos = ClientToScreen(evt.GetPosition());
270 wxPoint origin = GetPosition();
271 int dx = pos.x - origin.x;
272 int dy = pos.y - origin.y;
273 m_delta = wxPoint(dx, dy);
274 }
275
276 void ShapedFrame::OnLeftUp(wxMouseEvent& WXUNUSED(evt))
277 {
278 if (HasCapture())
279 {
280 ReleaseMouse();
281 }
282 }
283
284 void ShapedFrame::OnMouseMove(wxMouseEvent& evt)
285 {
286 wxPoint pt = evt.GetPosition();
287 if (evt.Dragging() && evt.LeftIsDown())
288 {
289 wxPoint pos = ClientToScreen(pt);
290 Move(wxPoint(pos.x - m_delta.x, pos.y - m_delta.y));
291 }
292 }
293
294 void ShapedFrame::OnExit(wxMouseEvent& WXUNUSED(evt))
295 {
296 Close();
297 }
298
299 void ShapedFrame::OnPaint(wxPaintEvent& WXUNUSED(evt))
300 {
301 wxPaintDC dc(this);
302 dc.DrawBitmap(m_bmp, 0, 0, true);
303 }
304
305 void ShapedFrame::OnWindowCreate(wxWindowCreateEvent& WXUNUSED(evt))
306 {
307 SetWindowShape();
308 }
309
310 // ----------------------------------------------------------------------------
311 // see-through frame
312 // ----------------------------------------------------------------------------
313
314 BEGIN_EVENT_TABLE(SeeThroughFrame, wxFrame)
315 EVT_LEFT_DCLICK(SeeThroughFrame::OnDoubleClick)
316 EVT_PAINT(SeeThroughFrame::OnPaint)
317 END_EVENT_TABLE()
318
319 SeeThroughFrame::SeeThroughFrame()
320 : wxFrame(NULL, wxID_ANY, "Transparency test: double click here",
321 wxPoint(100, 30), wxSize(300, 300),
322 wxDEFAULT_FRAME_STYLE |
323 wxFULL_REPAINT_ON_RESIZE |
324 wxSTAY_ON_TOP),
325 m_currentState(STATE_SEETHROUGH)
326 {
327 SetBackgroundColour(wxColour(255, 255, 255, 255));
328 SetBackgroundStyle(wxBG_STYLE_TRANSPARENT);
329 }
330
331 // Paints a grid of varying hue and alpha
332 void SeeThroughFrame::OnPaint(wxPaintEvent& WXUNUSED(evt))
333 {
334 wxPaintDC dc(this);
335 dc.SetPen(wxNullPen);
336
337 int xcount = 8;
338 int ycount = 8;
339
340 float xstep = 1. / xcount;
341 float ystep = 1. / ycount;
342
343 int width = GetClientSize().GetWidth();
344 int height = GetClientSize().GetHeight();
345
346 for ( float x = 0.; x < 1.; x += xstep )
347 {
348 for ( float y = 0.; y < 1.; y += ystep )
349 {
350 wxImage::RGBValue v = wxImage::HSVtoRGB(wxImage::HSVValue(x, 1., 1.));
351 dc.SetBrush(wxBrush(wxColour(v.red, v.green, v.blue,
352 (int)(255*(1. - y)))));
353 int x1 = (int)(x * width);
354 int y1 = (int)(y * height);
355 int x2 = (int)((x + xstep) * width);
356 int y2 = (int)((y + ystep) * height);
357 dc.DrawRectangle(x1, y1, x2 - x1, y2 - y1);
358 }
359 }
360 }
361
362 // Switches between colour and transparent background on doubleclick
363 void SeeThroughFrame::OnDoubleClick(wxMouseEvent& WXUNUSED(evt))
364 {
365 m_currentState = (State)((m_currentState + 1) % STATE_MAX);
366
367 switch ( m_currentState )
368 {
369 case STATE_OPAQUE:
370 SetBackgroundStyle(wxBG_STYLE_COLOUR);
371 SetTransparent(255);
372 SetTitle("Opaque");
373 break;
374
375 case STATE_SEETHROUGH:
376 SetBackgroundStyle(wxBG_STYLE_TRANSPARENT);
377 SetTransparent(255);
378 SetTitle("See through");
379 break;
380
381 case STATE_TRANSPARENT:
382 SetBackgroundStyle(wxBG_STYLE_COLOUR);
383 SetTransparent(128);
384 SetTitle("Semi-transparent");
385 break;
386
387 case STATE_MAX:
388 wxFAIL_MSG( "unreachable" );
389 }
390
391 Refresh();
392 }
393