]>
git.saurik.com Git - wxWidgets.git/blob - samples/shaped/shaped.cpp
ec2caf1f5f562ca38fc8f5612b99697bacc43be6
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 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 // Define a new application type, each program should derive a class from wxApp
59 class MyApp
: public wxApp
62 // override base class virtuals
63 // ----------------------------
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();
72 // Main frame just contains the menu items invoking the other tests
73 class MainFrame
: public wxFrame
79 void OnShowShaped(wxCommandEvent
& event
);
80 void OnShowTransparent(wxCommandEvent
& event
);
85 // Define a new frame type: this is going to the frame showing the
86 // effect of wxFRAME_SHAPED
87 class ShapedFrame
: public wxFrame
91 ShapedFrame(wxFrame
*parent
);
92 void SetWindowShape();
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
);
108 // any class wishing to process wxWidgets events must use this macro
109 DECLARE_EVENT_TABLE()
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
121 // event handlers (these functions should _not_ be virtual)
122 void OnDoubleClick(wxMouseEvent
& evt
);
123 void OnPaint(wxPaintEvent
& evt
);
134 State m_currentState
;
136 // any class wishing to process wxWidgets events must use this macro
137 DECLARE_EVENT_TABLE()
140 // ============================================================================
142 // ============================================================================
144 // ----------------------------------------------------------------------------
145 // the application class
146 // ----------------------------------------------------------------------------
150 // `Main program' equivalent: the program execution "starts" here
153 if ( !wxApp::OnInit() )
156 wxInitAllImageHandlers();
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.
166 // ----------------------------------------------------------------------------
168 // ----------------------------------------------------------------------------
170 BEGIN_EVENT_TABLE(MainFrame
, wxFrame
)
171 EVT_MENU(Show_Shaped
, MainFrame::OnShowShaped
)
172 EVT_MENU(Show_Transparent
, MainFrame::OnShowTransparent
)
175 MainFrame::MainFrame()
176 : wxFrame(NULL
, wxID_ANY
, "wxWidgets Shaped Sample",
177 wxDefaultPosition
, wxSize(200, 100))
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");
186 mbar
->Append(menuFrames
, "&Show");
192 void MainFrame::OnShowShaped(wxCommandEvent
& WXUNUSED(event
))
194 ShapedFrame
*shapedFrame
= new ShapedFrame(this);
195 shapedFrame
->Show(true);
198 void MainFrame::OnShowTransparent(wxCommandEvent
& WXUNUSED(event
))
200 SeeThroughFrame
*seeThroughFrame
= new SeeThroughFrame();
201 seeThroughFrame
->Show(true);
204 // ----------------------------------------------------------------------------
206 // ----------------------------------------------------------------------------
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
)
215 EVT_PAINT(ShapedFrame::OnPaint
)
218 EVT_WINDOW_CREATE(ShapedFrame::OnWindowCreate
)
224 ShapedFrame::ShapedFrame(wxFrame
*parent
)
225 : wxFrame(parent
, wxID_ANY
, wxEmptyString
,
226 wxDefaultPosition
, wxSize(100, 100),
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"));
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
248 void ShapedFrame::SetWindowShape()
250 wxRegion
region(m_bmp
, *wxWHITE
);
251 m_hasShape
= SetShape(region
);
254 void ShapedFrame::OnDoubleClick(wxMouseEvent
& WXUNUSED(evt
))
266 void ShapedFrame::OnLeftDown(wxMouseEvent
& evt
)
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
);
276 void ShapedFrame::OnLeftUp(wxMouseEvent
& WXUNUSED(evt
))
284 void ShapedFrame::OnMouseMove(wxMouseEvent
& evt
)
286 wxPoint pt
= evt
.GetPosition();
287 if (evt
.Dragging() && evt
.LeftIsDown())
289 wxPoint pos
= ClientToScreen(pt
);
290 Move(wxPoint(pos
.x
- m_delta
.x
, pos
.y
- m_delta
.y
));
294 void ShapedFrame::OnExit(wxMouseEvent
& WXUNUSED(evt
))
299 void ShapedFrame::OnPaint(wxPaintEvent
& WXUNUSED(evt
))
302 dc
.DrawBitmap(m_bmp
, 0, 0, true);
305 void ShapedFrame::OnWindowCreate(wxWindowCreateEvent
& WXUNUSED(evt
))
310 // ----------------------------------------------------------------------------
312 // ----------------------------------------------------------------------------
314 BEGIN_EVENT_TABLE(SeeThroughFrame
, wxFrame
)
315 EVT_LEFT_DCLICK(SeeThroughFrame::OnDoubleClick
)
316 EVT_PAINT(SeeThroughFrame::OnPaint
)
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
|
325 m_currentState(STATE_SEETHROUGH
)
327 SetBackgroundColour(wxColour(255, 255, 255, 255));
328 SetBackgroundStyle(wxBG_STYLE_TRANSPARENT
);
331 // Paints a grid of varying hue and alpha
332 void SeeThroughFrame::OnPaint(wxPaintEvent
& WXUNUSED(evt
))
335 dc
.SetPen(wxNullPen
);
340 float xstep
= 1. / xcount
;
341 float ystep
= 1. / ycount
;
343 int width
= GetClientSize().GetWidth();
344 int height
= GetClientSize().GetHeight();
346 for ( float x
= 0.; x
< 1.; x
+= xstep
)
348 for ( float y
= 0.; y
< 1.; y
+= ystep
)
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
);
362 // Switches between colour and transparent background on doubleclick
363 void SeeThroughFrame::OnDoubleClick(wxMouseEvent
& WXUNUSED(evt
))
365 m_currentState
= (State
)((m_currentState
+ 1) % STATE_MAX
);
367 switch ( m_currentState
)
370 SetBackgroundStyle(wxBG_STYLE_COLOUR
);
375 case STATE_SEETHROUGH
:
376 SetBackgroundStyle(wxBG_STYLE_TRANSPARENT
);
378 SetTitle("See through");
381 case STATE_TRANSPARENT
:
382 SetBackgroundStyle(wxBG_STYLE_COLOUR
);
384 SetTitle("Semi-transparent");
388 wxFAIL_MSG( "unreachable" );