]>
git.saurik.com Git - wxWidgets.git/blob - samples/shaped/shaped.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Shaped Window sample
6 // Created: 28-Mar-2003
8 // Copyright: (c) Robin Dunn
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers
33 #include "wx/stattext.h"
35 #include "wx/layout.h"
36 #include "wx/msgdlg.h"
40 #include "wx/dcclient.h"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 // Define a new application type, each program should derive a class from wxApp
48 class MyApp
: public wxApp
51 // override base class virtuals
52 // ----------------------------
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();
61 // Define a new frame type: this is going to the frame showing the
62 // effect of wxFRAME_SHAPED
63 class ShapedFrame
: public wxFrame
67 ShapedFrame(wxFrame
*parent
);
68 void SetWindowShape();
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
);
84 // any class wishing to process wxWidgets events must use this macro
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
97 // event handlers (these functions should _not_ be virtual)
98 void OnDoubleClick(wxMouseEvent
& evt
);
99 void OnPaint(wxPaintEvent
& evt
);
100 void OnSize(wxSizeEvent
& evt
);
111 State m_currentState
;
113 // any class wishing to process wxWidgets events must use this macro
114 DECLARE_EVENT_TABLE()
118 // ----------------------------------------------------------------------------
119 // event tables and other macros for wxWidgets
120 // ----------------------------------------------------------------------------
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
)
132 EVT_PAINT(ShapedFrame::OnPaint
)
135 EVT_WINDOW_CREATE(ShapedFrame::OnWindowCreate
)
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
147 // ============================================================================
149 // ============================================================================
151 // ----------------------------------------------------------------------------
152 // the application class
153 // ----------------------------------------------------------------------------
155 // `Main program' equivalent: the program execution "starts" here
158 if ( !wxApp::OnInit() )
161 wxInitAllImageHandlers();
163 // Create the transparent window
164 SeeThroughFrame
*seeThroughFrame
= new SeeThroughFrame();
165 seeThroughFrame
->Show(true);
166 SetTopWindow(seeThroughFrame
);
168 // Create the shaped window
169 ShapedFrame
*shapedFrame
= new ShapedFrame(seeThroughFrame
);
170 shapedFrame
->Show(true);
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.
178 // ----------------------------------------------------------------------------
180 // ----------------------------------------------------------------------------
183 ShapedFrame::ShapedFrame(wxFrame
*parent
)
184 : wxFrame(parent
, wxID_ANY
, wxEmptyString
,
185 wxDefaultPosition
, wxSize(100, 100), //wxDefaultSize,
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"));
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
207 void ShapedFrame::SetWindowShape()
209 wxRegion
region(m_bmp
, *wxWHITE
);
210 m_hasShape
= SetShape(region
);
213 void ShapedFrame::OnDoubleClick(wxMouseEvent
& WXUNUSED(evt
))
225 void ShapedFrame::OnLeftDown(wxMouseEvent
& evt
)
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
);
236 void ShapedFrame::OnLeftUp(wxMouseEvent
& WXUNUSED(evt
))
241 //printf("Mouse released\n");
245 void ShapedFrame::OnMouseMove(wxMouseEvent
& evt
)
247 wxPoint pt
= evt
.GetPosition();
248 //printf("x:%d y:%d\n", pt.x, pt.y);
249 if (evt
.Dragging() && evt
.LeftIsDown())
251 wxPoint pos
= ClientToScreen(pt
);
252 Move(wxPoint(pos
.x
- m_delta
.x
, pos
.y
- m_delta
.y
));
256 void ShapedFrame::OnExit(wxMouseEvent
& WXUNUSED(evt
))
261 void ShapedFrame::OnPaint(wxPaintEvent
& WXUNUSED(evt
))
264 dc
.DrawBitmap(m_bmp
, 0, 0, true);
267 void ShapedFrame::OnWindowCreate(wxWindowCreateEvent
& WXUNUSED(evt
))
272 // ----------------------------------------------------------------------------
274 // ----------------------------------------------------------------------------
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
)
283 SetBackgroundColour(wxColour(255, 255, 255, 255));
284 SetBackgroundStyle(wxBG_STYLE_TRANSPARENT
);
287 // Redraws the whole window on resize
288 void SeeThroughFrame::OnSize(wxSizeEvent
& WXUNUSED(evt
))
293 // Paints a grid of varying hue and alpha
294 void SeeThroughFrame::OnPaint(wxPaintEvent
& WXUNUSED(evt
))
297 dc
.SetPen(wxNullPen
);
302 float xstep
= 1. / xcount
;
303 float ystep
= 1. / ycount
;
305 int width
= GetClientSize().GetWidth();
306 int height
= GetClientSize().GetHeight();
308 for ( float x
= 0.; x
< 1.; x
+= xstep
)
310 for ( float y
= 0.; y
< 1.; y
+= ystep
)
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
);
324 // Switches between colour and transparent background on doubleclick
325 void SeeThroughFrame::OnDoubleClick(wxMouseEvent
& WXUNUSED(evt
))
327 m_currentState
= (State
)((m_currentState
+ 1) % STATE_MAX
);
329 switch ( m_currentState
)
332 SetBackgroundStyle(wxBG_STYLE_COLOUR
);
337 case STATE_SEETHROUGH
:
338 SetBackgroundStyle(wxBG_STYLE_TRANSPARENT
);
340 SetTitle("See through");
343 case STATE_TRANSPARENT
:
344 SetBackgroundStyle(wxBG_STYLE_COLOUR
);
346 SetTitle("Semi-transparent");
350 wxFAIL_MSG( "unreachable" );
356 BEGIN_EVENT_TABLE(SeeThroughFrame
, wxFrame
)
357 EVT_LEFT_DCLICK(SeeThroughFrame::OnDoubleClick
)
358 EVT_PAINT(SeeThroughFrame::OnPaint
)
359 EVT_SIZE(SeeThroughFrame::OnSize
)