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"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
54 // must be consecutive and in the same order as wxShowEffect enum elements
56 Show_Effect_Roll
= Show_Effect_First
,
60 Show_Effect_Last
= Show_Effect_Expand
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 // Define a new application type, each program should derive a class from wxApp
68 class MyApp
: public wxApp
71 // override base class virtuals
72 // ----------------------------
74 // this one is called on application startup and is a good place for the app
75 // initialization (doing it here and not in the ctor allows to have an error
76 // return: if OnInit() returns false, the application terminates)
77 virtual bool OnInit();
81 // Main frame just contains the menu items invoking the other tests
82 class MainFrame
: public wxFrame
88 void OnShowShaped(wxCommandEvent
& event
);
89 void OnShowTransparent(wxCommandEvent
& event
);
90 void OnShowEffect(wxCommandEvent
& event
);
95 // Define a new frame type: this is going to the frame showing the
96 // effect of wxFRAME_SHAPED
97 class ShapedFrame
: public wxFrame
101 ShapedFrame(wxFrame
*parent
);
102 void SetWindowShape();
104 // event handlers (these functions should _not_ be virtual)
105 void OnDoubleClick(wxMouseEvent
& evt
);
106 void OnLeftDown(wxMouseEvent
& evt
);
107 void OnLeftUp(wxMouseEvent
& evt
);
108 void OnMouseMove(wxMouseEvent
& evt
);
109 void OnExit(wxMouseEvent
& evt
);
110 void OnPaint(wxPaintEvent
& evt
);
117 // any class wishing to process wxWidgets events must use this macro
118 DECLARE_EVENT_TABLE()
121 // Define a new frame type: this is going to the frame showing the
122 // effect of wxWindow::SetTransparent and of
123 // wxWindow::SetBackgroundStyle(wxBG_STYLE_TRANSPARENT)
124 class SeeThroughFrame
: public wxFrame
130 // event handlers (these functions should _not_ be virtual)
131 void OnDoubleClick(wxMouseEvent
& evt
);
132 void OnPaint(wxPaintEvent
& evt
);
143 State m_currentState
;
145 // any class wishing to process wxWidgets events must use this macro
146 DECLARE_EVENT_TABLE()
149 class EffectFrame
: public wxFrame
152 EffectFrame(wxWindow
*parent
,
154 // TODO: add menu command to the main frame to allow changing
156 unsigned timeout
= 1000)
157 : wxFrame(parent
, wxID_ANY
,
158 wxString::Format("Frame shown with %s effect",
159 GetEffectName(effect
)),
160 wxDefaultPosition
, wxSize(450, 300)),
164 new wxStaticText(this, wxID_ANY
,
165 wxString::Format("Effect: %s", GetEffectName(effect
)),
167 new wxStaticText(this, wxID_ANY
,
168 wxString::Format("Timeout: %ums", m_timeout
),
171 ShowWithEffect(m_effect
, m_timeout
);
173 Connect(wxEVT_CLOSE_WINDOW
, wxCloseEventHandler(EffectFrame::OnClose
));
177 static const char *GetEffectName(wxShowEffect effect
)
179 static const char *names
[] =
192 wxCOMPILE_TIME_ASSERT( WXSIZEOF(names
) == wxSHOW_EFFECT_MAX
,
193 EffectNamesMismatch
);
195 return names
[effect
];
198 void OnClose(wxCloseEvent
& event
)
200 HideWithEffect(m_effect
, m_timeout
);
205 wxShowEffect m_effect
;
209 // ============================================================================
211 // ============================================================================
213 // ----------------------------------------------------------------------------
214 // the application class
215 // ----------------------------------------------------------------------------
219 // `Main program' equivalent: the program execution "starts" here
222 if ( !wxApp::OnInit() )
225 wxInitAllImageHandlers();
229 // success: wxApp::OnRun() will be called which will enter the main message
230 // loop and the application will run. If we returned false here, the
231 // application would exit immediately.
235 // ----------------------------------------------------------------------------
237 // ----------------------------------------------------------------------------
239 BEGIN_EVENT_TABLE(MainFrame
, wxFrame
)
240 EVT_MENU(Show_Shaped
, MainFrame::OnShowShaped
)
241 EVT_MENU(Show_Transparent
, MainFrame::OnShowTransparent
)
242 EVT_MENU_RANGE(Show_Effect_First
, Show_Effect_Last
, MainFrame::OnShowEffect
)
245 MainFrame::MainFrame()
246 : wxFrame(NULL
, wxID_ANY
, "wxWidgets Shaped Sample",
247 wxDefaultPosition
, wxSize(200, 100))
249 wxMenuBar
* const mbar
= new wxMenuBar
;
250 wxMenu
* const menuFrames
= new wxMenu
;
251 menuFrames
->Append(Show_Shaped
, "Show &shaped window\tCtrl-S");
252 menuFrames
->Append(Show_Transparent
, "Show &transparent window\tCtrl-T");
253 menuFrames
->AppendSeparator();
254 menuFrames
->Append(Show_Effect_Roll
, "Show &rolled effect\tCtrl-R");
255 menuFrames
->Append(Show_Effect_Slide
, "Show s&lide effect\tCtrl-L");
256 menuFrames
->Append(Show_Effect_Blend
, "Show &fade effect\tCtrl-F");
257 menuFrames
->Append(Show_Effect_Expand
, "Show &expand effect\tCtrl-E");
258 menuFrames
->AppendSeparator();
259 menuFrames
->Append(wxID_EXIT
, "E&xit");
261 mbar
->Append(menuFrames
, "&Show");
267 void MainFrame::OnShowShaped(wxCommandEvent
& WXUNUSED(event
))
269 ShapedFrame
*shapedFrame
= new ShapedFrame(this);
270 shapedFrame
->Show(true);
273 void MainFrame::OnShowTransparent(wxCommandEvent
& WXUNUSED(event
))
275 SeeThroughFrame
*seeThroughFrame
= new SeeThroughFrame();
276 seeThroughFrame
->Show(true);
279 void MainFrame::OnShowEffect(wxCommandEvent
& event
)
281 int effect
= event
.GetId();
282 static wxDirection direction
= wxLEFT
;
283 direction
= (wxDirection
)(((int)direction
)<< 1);
284 if ( direction
> wxDOWN
)
290 case Show_Effect_Roll
:
294 eff
= wxSHOW_EFFECT_ROLL_TO_LEFT
;
297 eff
= wxSHOW_EFFECT_ROLL_TO_RIGHT
;
300 eff
= wxSHOW_EFFECT_ROLL_TO_TOP
;
303 eff
= wxSHOW_EFFECT_ROLL_TO_BOTTOM
;
306 wxFAIL_MSG( "invalid direction" );
310 case Show_Effect_Slide
:
314 eff
= wxSHOW_EFFECT_SLIDE_TO_LEFT
;
317 eff
= wxSHOW_EFFECT_SLIDE_TO_RIGHT
;
320 eff
= wxSHOW_EFFECT_SLIDE_TO_TOP
;
323 eff
= wxSHOW_EFFECT_SLIDE_TO_BOTTOM
;
326 wxFAIL_MSG( "invalid direction" );
331 case Show_Effect_Blend
:
332 eff
= wxSHOW_EFFECT_BLEND
;
335 case Show_Effect_Expand
:
336 eff
= wxSHOW_EFFECT_EXPAND
;
340 wxFAIL_MSG( "invalid effect" );
344 new EffectFrame(this, eff
,1000);
347 // ----------------------------------------------------------------------------
349 // ----------------------------------------------------------------------------
351 BEGIN_EVENT_TABLE(ShapedFrame
, wxFrame
)
352 EVT_LEFT_DCLICK(ShapedFrame::OnDoubleClick
)
353 EVT_LEFT_DOWN(ShapedFrame::OnLeftDown
)
354 EVT_LEFT_UP(ShapedFrame::OnLeftUp
)
355 EVT_MOTION(ShapedFrame::OnMouseMove
)
356 EVT_RIGHT_UP(ShapedFrame::OnExit
)
357 EVT_PAINT(ShapedFrame::OnPaint
)
362 ShapedFrame::ShapedFrame(wxFrame
*parent
)
363 : wxFrame(parent
, wxID_ANY
, wxEmptyString
,
364 wxDefaultPosition
, wxSize(100, 100),
373 m_bmp
= wxBitmap(_T("star.png"), wxBITMAP_TYPE_PNG
);
374 SetSize(wxSize(m_bmp
.GetWidth(), m_bmp
.GetHeight()));
375 SetToolTip(wxT("Right-click to close"));
379 void ShapedFrame::SetWindowShape()
381 wxRegion
region(m_bmp
, *wxWHITE
);
382 m_hasShape
= SetShape(region
);
385 void ShapedFrame::OnDoubleClick(wxMouseEvent
& WXUNUSED(evt
))
397 void ShapedFrame::OnLeftDown(wxMouseEvent
& evt
)
400 wxPoint pos
= ClientToScreen(evt
.GetPosition());
401 wxPoint origin
= GetPosition();
402 int dx
= pos
.x
- origin
.x
;
403 int dy
= pos
.y
- origin
.y
;
404 m_delta
= wxPoint(dx
, dy
);
407 void ShapedFrame::OnLeftUp(wxMouseEvent
& WXUNUSED(evt
))
415 void ShapedFrame::OnMouseMove(wxMouseEvent
& evt
)
417 wxPoint pt
= evt
.GetPosition();
418 if (evt
.Dragging() && evt
.LeftIsDown())
420 wxPoint pos
= ClientToScreen(pt
);
421 Move(wxPoint(pos
.x
- m_delta
.x
, pos
.y
- m_delta
.y
));
425 void ShapedFrame::OnExit(wxMouseEvent
& WXUNUSED(evt
))
430 void ShapedFrame::OnPaint(wxPaintEvent
& WXUNUSED(evt
))
433 dc
.DrawBitmap(m_bmp
, 0, 0, true);
436 // ----------------------------------------------------------------------------
438 // ----------------------------------------------------------------------------
440 BEGIN_EVENT_TABLE(SeeThroughFrame
, wxFrame
)
441 EVT_LEFT_DCLICK(SeeThroughFrame::OnDoubleClick
)
442 EVT_PAINT(SeeThroughFrame::OnPaint
)
445 SeeThroughFrame::SeeThroughFrame()
446 : wxFrame(NULL
, wxID_ANY
, "Transparency test: double click here",
447 wxPoint(100, 30), wxSize(300, 300),
448 wxDEFAULT_FRAME_STYLE
|
449 wxFULL_REPAINT_ON_RESIZE
|
451 m_currentState(STATE_SEETHROUGH
)
453 SetBackgroundColour(wxColour(255, 255, 255, 255));
454 SetBackgroundStyle(wxBG_STYLE_TRANSPARENT
);
457 // Paints a grid of varying hue and alpha
458 void SeeThroughFrame::OnPaint(wxPaintEvent
& WXUNUSED(evt
))
461 dc
.SetPen(wxNullPen
);
466 float xstep
= 1. / xcount
;
467 float ystep
= 1. / ycount
;
469 int width
= GetClientSize().GetWidth();
470 int height
= GetClientSize().GetHeight();
472 for ( float x
= 0.; x
< 1.; x
+= xstep
)
474 for ( float y
= 0.; y
< 1.; y
+= ystep
)
476 wxImage::RGBValue v
= wxImage::HSVtoRGB(wxImage::HSVValue(x
, 1., 1.));
477 dc
.SetBrush(wxBrush(wxColour(v
.red
, v
.green
, v
.blue
,
478 (int)(255*(1. - y
)))));
479 int x1
= (int)(x
* width
);
480 int y1
= (int)(y
* height
);
481 int x2
= (int)((x
+ xstep
) * width
);
482 int y2
= (int)((y
+ ystep
) * height
);
483 dc
.DrawRectangle(x1
, y1
, x2
- x1
, y2
- y1
);
488 // Switches between colour and transparent background on doubleclick
489 void SeeThroughFrame::OnDoubleClick(wxMouseEvent
& WXUNUSED(evt
))
491 m_currentState
= (State
)((m_currentState
+ 1) % STATE_MAX
);
493 switch ( m_currentState
)
496 SetBackgroundStyle(wxBG_STYLE_COLOUR
);
501 case STATE_SEETHROUGH
:
502 SetBackgroundStyle(wxBG_STYLE_TRANSPARENT
);
504 SetTitle("See through");
507 case STATE_TRANSPARENT
:
508 SetBackgroundStyle(wxBG_STYLE_COLOUR
);
510 SetTitle("Semi-transparent");
514 wxFAIL_MSG( "unreachable" );