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 #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
);
98 // Define a new frame type: this is going to the frame showing the
99 // effect of wxFRAME_SHAPED
100 class ShapedFrame
: public wxFrame
104 ShapedFrame(wxFrame
*parent
);
105 void SetWindowShape();
107 // event handlers (these functions should _not_ be virtual)
108 void OnDoubleClick(wxMouseEvent
& evt
);
109 void OnLeftDown(wxMouseEvent
& evt
);
110 void OnLeftUp(wxMouseEvent
& evt
);
111 void OnMouseMove(wxMouseEvent
& evt
);
112 void OnExit(wxMouseEvent
& evt
);
113 void OnPaint(wxPaintEvent
& evt
);
120 // any class wishing to process wxWidgets events must use this macro
121 DECLARE_EVENT_TABLE()
124 // Define a new frame type: this is going to the frame showing the
125 // effect of wxWindow::SetTransparent and of
126 // wxWindow::SetBackgroundStyle(wxBG_STYLE_TRANSPARENT)
127 class SeeThroughFrame
: public wxFrame
133 // event handlers (these functions should _not_ be virtual)
134 void OnDoubleClick(wxMouseEvent
& evt
);
135 void OnPaint(wxPaintEvent
& evt
);
146 State m_currentState
;
148 // any class wishing to process wxWidgets events must use this macro
149 DECLARE_EVENT_TABLE()
152 class EffectFrame
: public wxFrame
155 EffectFrame(wxWindow
*parent
,
157 // TODO: add menu command to the main frame to allow changing
159 unsigned timeout
= 1000)
160 : wxFrame(parent
, wxID_ANY
,
161 wxString::Format("Frame shown with %s effect",
162 GetEffectName(effect
)),
163 wxDefaultPosition
, wxSize(450, 300)),
167 new wxStaticText(this, wxID_ANY
,
168 wxString::Format("Effect: %s", GetEffectName(effect
)),
170 new wxStaticText(this, wxID_ANY
,
171 wxString::Format("Timeout: %ums", m_timeout
),
174 ShowWithEffect(m_effect
, m_timeout
);
176 Connect(wxEVT_CLOSE_WINDOW
, wxCloseEventHandler(EffectFrame::OnClose
));
180 static const char *GetEffectName(wxShowEffect effect
)
182 static const char *names
[] =
196 wxCOMPILE_TIME_ASSERT( WXSIZEOF(names
) == wxSHOW_EFFECT_MAX
,
197 EffectNamesMismatch
);
199 return names
[effect
];
202 void OnClose(wxCloseEvent
& event
)
204 HideWithEffect(m_effect
, m_timeout
);
209 wxShowEffect m_effect
;
213 // ============================================================================
215 // ============================================================================
217 // ----------------------------------------------------------------------------
218 // the application class
219 // ----------------------------------------------------------------------------
223 // `Main program' equivalent: the program execution "starts" here
226 if ( !wxApp::OnInit() )
229 wxInitAllImageHandlers();
233 // success: wxApp::OnRun() will be called which will enter the main message
234 // loop and the application will run. If we returned false here, the
235 // application would exit immediately.
239 // ----------------------------------------------------------------------------
241 // ----------------------------------------------------------------------------
243 BEGIN_EVENT_TABLE(MainFrame
, wxFrame
)
244 EVT_MENU(Show_Shaped
, MainFrame::OnShowShaped
)
245 EVT_MENU(Show_Transparent
, MainFrame::OnShowTransparent
)
246 EVT_MENU_RANGE(Show_Effect_First
, Show_Effect_Last
, MainFrame::OnShowEffect
)
249 MainFrame::MainFrame()
250 : wxFrame(NULL
, wxID_ANY
, "wxWidgets Shaped Sample",
251 wxDefaultPosition
, wxSize(200, 100))
253 SetIcon(wxICON(sample
));
255 wxMenuBar
* const mbar
= new wxMenuBar
;
256 wxMenu
* const menuFrames
= new wxMenu
;
257 menuFrames
->Append(Show_Shaped
, "Show &shaped window\tCtrl-S");
258 menuFrames
->Append(Show_Transparent
, "Show &transparent window\tCtrl-T");
259 menuFrames
->AppendSeparator();
260 menuFrames
->Append(Show_Effect_Roll
, "Show &rolled effect\tCtrl-R");
261 menuFrames
->Append(Show_Effect_Slide
, "Show s&lide effect\tCtrl-L");
262 menuFrames
->Append(Show_Effect_Blend
, "Show &fade effect\tCtrl-F");
263 menuFrames
->Append(Show_Effect_Expand
, "Show &expand effect\tCtrl-E");
264 menuFrames
->AppendSeparator();
265 menuFrames
->Append(wxID_EXIT
, "E&xit");
267 mbar
->Append(menuFrames
, "&Show");
273 void MainFrame::OnShowShaped(wxCommandEvent
& WXUNUSED(event
))
275 ShapedFrame
*shapedFrame
= new ShapedFrame(this);
276 shapedFrame
->Show(true);
279 void MainFrame::OnShowTransparent(wxCommandEvent
& WXUNUSED(event
))
281 SeeThroughFrame
*seeThroughFrame
= new SeeThroughFrame();
282 seeThroughFrame
->Show(true);
285 void MainFrame::OnShowEffect(wxCommandEvent
& event
)
287 int effect
= event
.GetId();
288 static wxDirection direction
= wxLEFT
;
289 direction
= (wxDirection
)(((int)direction
)<< 1);
290 if ( direction
> wxDOWN
)
296 case Show_Effect_Roll
:
300 eff
= wxSHOW_EFFECT_ROLL_TO_LEFT
;
303 eff
= wxSHOW_EFFECT_ROLL_TO_RIGHT
;
306 eff
= wxSHOW_EFFECT_ROLL_TO_TOP
;
309 eff
= wxSHOW_EFFECT_ROLL_TO_BOTTOM
;
312 wxFAIL_MSG( "invalid direction" );
316 case Show_Effect_Slide
:
320 eff
= wxSHOW_EFFECT_SLIDE_TO_LEFT
;
323 eff
= wxSHOW_EFFECT_SLIDE_TO_RIGHT
;
326 eff
= wxSHOW_EFFECT_SLIDE_TO_TOP
;
329 eff
= wxSHOW_EFFECT_SLIDE_TO_BOTTOM
;
332 wxFAIL_MSG( "invalid direction" );
337 case Show_Effect_Blend
:
338 eff
= wxSHOW_EFFECT_BLEND
;
341 case Show_Effect_Expand
:
342 eff
= wxSHOW_EFFECT_EXPAND
;
346 wxFAIL_MSG( "invalid effect" );
350 new EffectFrame(this, eff
, 1000);
353 // ----------------------------------------------------------------------------
355 // ----------------------------------------------------------------------------
357 BEGIN_EVENT_TABLE(ShapedFrame
, wxFrame
)
358 EVT_LEFT_DCLICK(ShapedFrame::OnDoubleClick
)
359 EVT_LEFT_DOWN(ShapedFrame::OnLeftDown
)
360 EVT_LEFT_UP(ShapedFrame::OnLeftUp
)
361 EVT_MOTION(ShapedFrame::OnMouseMove
)
362 EVT_RIGHT_UP(ShapedFrame::OnExit
)
363 EVT_PAINT(ShapedFrame::OnPaint
)
368 ShapedFrame::ShapedFrame(wxFrame
*parent
)
369 : wxFrame(parent
, wxID_ANY
, wxEmptyString
,
370 wxDefaultPosition
, wxSize(100, 100),
379 m_bmp
= wxBitmap(wxT("star.png"), wxBITMAP_TYPE_PNG
);
380 SetSize(wxSize(m_bmp
.GetWidth(), m_bmp
.GetHeight()));
381 SetToolTip(wxT("Right-click to close"));
385 void ShapedFrame::SetWindowShape()
387 wxRegion
region(m_bmp
, *wxWHITE
);
388 m_hasShape
= SetShape(region
);
391 void ShapedFrame::OnDoubleClick(wxMouseEvent
& WXUNUSED(evt
))
403 void ShapedFrame::OnLeftDown(wxMouseEvent
& evt
)
406 wxPoint pos
= ClientToScreen(evt
.GetPosition());
407 wxPoint origin
= GetPosition();
408 int dx
= pos
.x
- origin
.x
;
409 int dy
= pos
.y
- origin
.y
;
410 m_delta
= wxPoint(dx
, dy
);
413 void ShapedFrame::OnLeftUp(wxMouseEvent
& WXUNUSED(evt
))
421 void ShapedFrame::OnMouseMove(wxMouseEvent
& evt
)
423 wxPoint pt
= evt
.GetPosition();
424 if (evt
.Dragging() && evt
.LeftIsDown())
426 wxPoint pos
= ClientToScreen(pt
);
427 Move(wxPoint(pos
.x
- m_delta
.x
, pos
.y
- m_delta
.y
));
431 void ShapedFrame::OnExit(wxMouseEvent
& WXUNUSED(evt
))
436 void ShapedFrame::OnPaint(wxPaintEvent
& WXUNUSED(evt
))
439 dc
.DrawBitmap(m_bmp
, 0, 0, true);
442 // ----------------------------------------------------------------------------
444 // ----------------------------------------------------------------------------
446 BEGIN_EVENT_TABLE(SeeThroughFrame
, wxFrame
)
447 EVT_LEFT_DCLICK(SeeThroughFrame::OnDoubleClick
)
448 EVT_PAINT(SeeThroughFrame::OnPaint
)
451 SeeThroughFrame::SeeThroughFrame()
452 : wxFrame(NULL
, wxID_ANY
, "Transparency test: double click here",
453 wxPoint(100, 30), wxSize(300, 300),
454 wxDEFAULT_FRAME_STYLE
|
455 wxFULL_REPAINT_ON_RESIZE
|
457 m_currentState(STATE_SEETHROUGH
)
459 SetBackgroundColour(wxColour(255, 255, 255, 255));
460 SetBackgroundStyle(wxBG_STYLE_TRANSPARENT
);
463 // Paints a grid of varying hue and alpha
464 void SeeThroughFrame::OnPaint(wxPaintEvent
& WXUNUSED(evt
))
467 dc
.SetPen(wxNullPen
);
472 float xstep
= 1. / xcount
;
473 float ystep
= 1. / ycount
;
475 int width
= GetClientSize().GetWidth();
476 int height
= GetClientSize().GetHeight();
478 for ( float x
= 0.; x
< 1.; x
+= xstep
)
480 for ( float y
= 0.; y
< 1.; y
+= ystep
)
482 wxImage::RGBValue v
= wxImage::HSVtoRGB(wxImage::HSVValue(x
, 1., 1.));
483 dc
.SetBrush(wxBrush(wxColour(v
.red
, v
.green
, v
.blue
,
484 (int)(255*(1. - y
)))));
485 int x1
= (int)(x
* width
);
486 int y1
= (int)(y
* height
);
487 int x2
= (int)((x
+ xstep
) * width
);
488 int y2
= (int)((y
+ ystep
) * height
);
489 dc
.DrawRectangle(x1
, y1
, x2
- x1
, y2
- y1
);
494 // Switches between colour and transparent background on doubleclick
495 void SeeThroughFrame::OnDoubleClick(wxMouseEvent
& WXUNUSED(evt
))
497 m_currentState
= (State
)((m_currentState
+ 1) % STATE_MAX
);
499 switch ( m_currentState
)
502 SetBackgroundStyle(wxBG_STYLE_COLOUR
);
507 case STATE_SEETHROUGH
:
508 SetBackgroundStyle(wxBG_STYLE_TRANSPARENT
);
510 SetTitle("See through");
513 case STATE_TRANSPARENT
:
514 SetBackgroundStyle(wxBG_STYLE_COLOUR
);
516 SetTitle("Semi-transparent");
520 wxFAIL_MSG( "unreachable" );