1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Shaped Window sample
6 // Created: 28-Mar-2003
7 // Copyright: (c) Robin Dunn
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
26 // for all others, include the necessary headers
32 #include "wx/stattext.h"
34 #include "wx/layout.h"
35 #include "wx/msgdlg.h"
39 #include "wx/dcclient.h"
40 #include "wx/graphics.h"
43 #ifndef wxHAS_IMAGES_IN_RESOURCES
44 #include "../sample.xpm"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
57 // must be consecutive and in the same order as wxShowEffect enum elements
59 Show_Effect_Roll
= Show_Effect_First
,
63 Show_Effect_Last
= Show_Effect_Expand
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 // Define a new application type, each program should derive a class from wxApp
71 class MyApp
: public wxApp
74 // override base class virtuals
75 // ----------------------------
77 // this one is called on application startup and is a good place for the app
78 // initialization (doing it here and not in the ctor allows to have an error
79 // return: if OnInit() returns false, the application terminates)
80 virtual bool OnInit();
84 // Main frame just contains the menu items invoking the other tests
85 class MainFrame
: public wxFrame
91 void OnShowShaped(wxCommandEvent
& event
);
92 void OnShowTransparent(wxCommandEvent
& event
);
93 void OnShowEffect(wxCommandEvent
& event
);
94 void OnExit(wxCommandEvent
& event
);
99 // Define a new frame type: this is going to the frame showing the
100 // effect of wxFRAME_SHAPED
101 class ShapedFrame
: public wxFrame
105 ShapedFrame(wxFrame
*parent
);
106 void SetWindowShape();
108 // event handlers (these functions should _not_ be virtual)
109 void OnDoubleClick(wxMouseEvent
& evt
);
110 void OnLeftDown(wxMouseEvent
& evt
);
111 void OnLeftUp(wxMouseEvent
& evt
);
112 void OnMouseMove(wxMouseEvent
& evt
);
113 void OnExit(wxMouseEvent
& evt
);
114 void OnPaint(wxPaintEvent
& evt
);
121 #if wxUSE_GRAPHICS_CONTEXT
123 #endif // wxUSE_GRAPHICS_CONTEXT
130 // any class wishing to process wxWidgets events must use this macro
131 DECLARE_EVENT_TABLE()
134 // Define a new frame type: this is going to the frame showing the
135 // effect of wxWindow::SetTransparent and of
136 // wxWindow::SetBackgroundStyle(wxBG_STYLE_TRANSPARENT)
137 class SeeThroughFrame
: public wxFrame
143 // event handlers (these functions should _not_ be virtual)
144 void OnDoubleClick(wxMouseEvent
& evt
);
145 void OnPaint(wxPaintEvent
& evt
);
156 State m_currentState
;
158 // any class wishing to process wxWidgets events must use this macro
159 DECLARE_EVENT_TABLE()
162 class EffectFrame
: public wxFrame
165 EffectFrame(wxWindow
*parent
,
167 // TODO: add menu command to the main frame to allow changing
169 unsigned timeout
= 1000)
170 : wxFrame(parent
, wxID_ANY
,
171 wxString::Format("Frame shown with %s effect",
172 GetEffectName(effect
)),
173 wxDefaultPosition
, wxSize(450, 300)),
177 new wxStaticText(this, wxID_ANY
,
178 wxString::Format("Effect: %s", GetEffectName(effect
)),
180 new wxStaticText(this, wxID_ANY
,
181 wxString::Format("Timeout: %ums", m_timeout
),
184 ShowWithEffect(m_effect
, m_timeout
);
186 Connect(wxEVT_CLOSE_WINDOW
, wxCloseEventHandler(EffectFrame::OnClose
));
190 static const char *GetEffectName(wxShowEffect effect
)
192 static const char *names
[] =
206 wxCOMPILE_TIME_ASSERT( WXSIZEOF(names
) == wxSHOW_EFFECT_MAX
,
207 EffectNamesMismatch
);
209 return names
[effect
];
212 void OnClose(wxCloseEvent
& event
)
214 HideWithEffect(m_effect
, m_timeout
);
219 wxShowEffect m_effect
;
223 // ============================================================================
225 // ============================================================================
227 // ----------------------------------------------------------------------------
228 // the application class
229 // ----------------------------------------------------------------------------
233 // `Main program' equivalent: the program execution "starts" here
236 if ( !wxApp::OnInit() )
239 wxInitAllImageHandlers();
243 // success: wxApp::OnRun() will be called which will enter the main message
244 // loop and the application will run. If we returned false here, the
245 // application would exit immediately.
249 // ----------------------------------------------------------------------------
251 // ----------------------------------------------------------------------------
253 BEGIN_EVENT_TABLE(MainFrame
, wxFrame
)
254 EVT_MENU(Show_Shaped
, MainFrame::OnShowShaped
)
255 EVT_MENU(Show_Transparent
, MainFrame::OnShowTransparent
)
256 EVT_MENU_RANGE(Show_Effect_First
, Show_Effect_Last
, MainFrame::OnShowEffect
)
257 EVT_MENU(wxID_EXIT
, MainFrame::OnExit
)
260 MainFrame::MainFrame()
261 : wxFrame(NULL
, wxID_ANY
, "wxWidgets Shaped Sample",
262 wxDefaultPosition
, wxSize(200, 100))
264 SetIcon(wxICON(sample
));
266 wxMenuBar
* const mbar
= new wxMenuBar
;
267 wxMenu
* const menuFrames
= new wxMenu
;
268 menuFrames
->Append(Show_Shaped
, "Show &shaped window\tCtrl-S");
269 menuFrames
->Append(Show_Transparent
, "Show &transparent window\tCtrl-T");
270 menuFrames
->AppendSeparator();
271 menuFrames
->Append(Show_Effect_Roll
, "Show &rolled effect\tCtrl-R");
272 menuFrames
->Append(Show_Effect_Slide
, "Show s&lide effect\tCtrl-L");
273 menuFrames
->Append(Show_Effect_Blend
, "Show &fade effect\tCtrl-F");
274 menuFrames
->Append(Show_Effect_Expand
, "Show &expand effect\tCtrl-E");
275 menuFrames
->AppendSeparator();
276 menuFrames
->Append(wxID_EXIT
, "E&xit");
278 mbar
->Append(menuFrames
, "&Show");
284 void MainFrame::OnShowShaped(wxCommandEvent
& WXUNUSED(event
))
286 ShapedFrame
*shapedFrame
= new ShapedFrame(this);
287 shapedFrame
->Show(true);
290 void MainFrame::OnShowTransparent(wxCommandEvent
& WXUNUSED(event
))
292 SeeThroughFrame
*seeThroughFrame
= new SeeThroughFrame();
293 seeThroughFrame
->Show(true);
296 void MainFrame::OnShowEffect(wxCommandEvent
& event
)
298 int effect
= event
.GetId();
299 static wxDirection direction
= wxLEFT
;
300 direction
= (wxDirection
)(((int)direction
)<< 1);
301 if ( direction
> wxDOWN
)
307 case Show_Effect_Roll
:
311 eff
= wxSHOW_EFFECT_ROLL_TO_LEFT
;
314 eff
= wxSHOW_EFFECT_ROLL_TO_RIGHT
;
317 eff
= wxSHOW_EFFECT_ROLL_TO_TOP
;
320 eff
= wxSHOW_EFFECT_ROLL_TO_BOTTOM
;
323 wxFAIL_MSG( "invalid direction" );
327 case Show_Effect_Slide
:
331 eff
= wxSHOW_EFFECT_SLIDE_TO_LEFT
;
334 eff
= wxSHOW_EFFECT_SLIDE_TO_RIGHT
;
337 eff
= wxSHOW_EFFECT_SLIDE_TO_TOP
;
340 eff
= wxSHOW_EFFECT_SLIDE_TO_BOTTOM
;
343 wxFAIL_MSG( "invalid direction" );
348 case Show_Effect_Blend
:
349 eff
= wxSHOW_EFFECT_BLEND
;
352 case Show_Effect_Expand
:
353 eff
= wxSHOW_EFFECT_EXPAND
;
357 wxFAIL_MSG( "invalid effect" );
361 new EffectFrame(this, eff
, 1000);
364 void MainFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
369 // ----------------------------------------------------------------------------
371 // ----------------------------------------------------------------------------
373 BEGIN_EVENT_TABLE(ShapedFrame
, wxFrame
)
374 EVT_LEFT_DCLICK(ShapedFrame::OnDoubleClick
)
375 EVT_LEFT_DOWN(ShapedFrame::OnLeftDown
)
376 EVT_LEFT_UP(ShapedFrame::OnLeftUp
)
377 EVT_MOTION(ShapedFrame::OnMouseMove
)
378 EVT_RIGHT_UP(ShapedFrame::OnExit
)
379 EVT_PAINT(ShapedFrame::OnPaint
)
384 ShapedFrame::ShapedFrame(wxFrame
*parent
)
385 : wxFrame(parent
, wxID_ANY
, wxEmptyString
,
386 wxDefaultPosition
, wxSize(100, 100),
394 m_shapeKind
= Shape_None
;
395 m_bmp
= wxBitmap(wxT("star.png"), wxBITMAP_TYPE_PNG
);
396 SetSize(wxSize(m_bmp
.GetWidth(), m_bmp
.GetHeight()));
397 SetToolTip(wxT("Right-click to close, double click to cycle shape"));
401 void ShapedFrame::SetWindowShape()
403 switch ( m_shapeKind
)
406 SetShape(wxRegion());
410 SetShape(wxRegion(m_bmp
, *wxWHITE
));
413 #if wxUSE_GRAPHICS_CONTEXT
417 path
= wxGraphicsRenderer::GetDefaultRenderer()->CreatePath();
418 path
.AddCircle(m_bmp
.GetWidth()/2, m_bmp
.GetHeight()/2, 30);
422 #endif // wxUSE_GRAPHICS_CONTEXT
425 wxFAIL_MSG( "invalid shape kind" );
430 void ShapedFrame::OnDoubleClick(wxMouseEvent
& WXUNUSED(evt
))
432 m_shapeKind
= static_cast<ShapeKind
>((m_shapeKind
+ 1) % Shape_Max
);
436 void ShapedFrame::OnLeftDown(wxMouseEvent
& evt
)
439 wxPoint pos
= ClientToScreen(evt
.GetPosition());
440 wxPoint origin
= GetPosition();
441 int dx
= pos
.x
- origin
.x
;
442 int dy
= pos
.y
- origin
.y
;
443 m_delta
= wxPoint(dx
, dy
);
446 void ShapedFrame::OnLeftUp(wxMouseEvent
& WXUNUSED(evt
))
454 void ShapedFrame::OnMouseMove(wxMouseEvent
& evt
)
456 wxPoint pt
= evt
.GetPosition();
457 if (evt
.Dragging() && evt
.LeftIsDown())
459 wxPoint pos
= ClientToScreen(pt
);
460 Move(wxPoint(pos
.x
- m_delta
.x
, pos
.y
- m_delta
.y
));
464 void ShapedFrame::OnExit(wxMouseEvent
& WXUNUSED(evt
))
469 void ShapedFrame::OnPaint(wxPaintEvent
& WXUNUSED(evt
))
472 dc
.DrawBitmap(m_bmp
, 0, 0, true);
475 // ----------------------------------------------------------------------------
477 // ----------------------------------------------------------------------------
479 BEGIN_EVENT_TABLE(SeeThroughFrame
, wxFrame
)
480 EVT_LEFT_DCLICK(SeeThroughFrame::OnDoubleClick
)
481 EVT_PAINT(SeeThroughFrame::OnPaint
)
484 SeeThroughFrame::SeeThroughFrame()
485 : wxFrame(NULL
, wxID_ANY
, "Transparency test: double click here",
486 wxPoint(100, 30), wxSize(300, 300),
487 wxDEFAULT_FRAME_STYLE
|
488 wxFULL_REPAINT_ON_RESIZE
|
490 m_currentState(STATE_SEETHROUGH
)
492 SetBackgroundColour(*wxWHITE
);
493 SetBackgroundStyle(wxBG_STYLE_TRANSPARENT
);
496 // Paints a grid of varying hue and alpha
497 void SeeThroughFrame::OnPaint(wxPaintEvent
& WXUNUSED(evt
))
500 dc
.SetPen(wxNullPen
);
505 float xstep
= 1. / xcount
;
506 float ystep
= 1. / ycount
;
508 int width
= GetClientSize().GetWidth();
509 int height
= GetClientSize().GetHeight();
511 for ( float x
= 0.; x
< 1.; x
+= xstep
)
513 for ( float y
= 0.; y
< 1.; y
+= ystep
)
515 wxImage::RGBValue v
= wxImage::HSVtoRGB(wxImage::HSVValue(x
, 1., 1.));
516 dc
.SetBrush(wxBrush(wxColour(v
.red
, v
.green
, v
.blue
,
517 (int)(255*(1. - y
)))));
518 int x1
= (int)(x
* width
);
519 int y1
= (int)(y
* height
);
520 int x2
= (int)((x
+ xstep
) * width
);
521 int y2
= (int)((y
+ ystep
) * height
);
522 dc
.DrawRectangle(x1
, y1
, x2
- x1
, y2
- y1
);
527 // Switches between colour and transparent background on doubleclick
528 void SeeThroughFrame::OnDoubleClick(wxMouseEvent
& WXUNUSED(evt
))
530 m_currentState
= (State
)((m_currentState
+ 1) % STATE_MAX
);
532 switch ( m_currentState
)
535 SetBackgroundStyle(wxBG_STYLE_COLOUR
);
540 case STATE_SEETHROUGH
:
541 SetBackgroundStyle(wxBG_STYLE_TRANSPARENT
);
543 SetTitle("See through");
546 case STATE_TRANSPARENT
:
547 SetBackgroundStyle(wxBG_STYLE_COLOUR
);
549 SetTitle("Semi-transparent");
553 wxFAIL_MSG( "unreachable" );