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
[] =
195 wxCOMPILE_TIME_ASSERT( WXSIZEOF(names
) == wxSHOW_EFFECT_MAX
,
196 EffectNamesMismatch
);
198 return names
[effect
];
201 void OnClose(wxCloseEvent
& event
)
203 HideWithEffect(m_effect
, m_timeout
);
208 wxShowEffect m_effect
;
212 // ============================================================================
214 // ============================================================================
216 // ----------------------------------------------------------------------------
217 // the application class
218 // ----------------------------------------------------------------------------
222 // `Main program' equivalent: the program execution "starts" here
225 if ( !wxApp::OnInit() )
228 wxInitAllImageHandlers();
232 // success: wxApp::OnRun() will be called which will enter the main message
233 // loop and the application will run. If we returned false here, the
234 // application would exit immediately.
238 // ----------------------------------------------------------------------------
240 // ----------------------------------------------------------------------------
242 BEGIN_EVENT_TABLE(MainFrame
, wxFrame
)
243 EVT_MENU(Show_Shaped
, MainFrame::OnShowShaped
)
244 EVT_MENU(Show_Transparent
, MainFrame::OnShowTransparent
)
245 EVT_MENU_RANGE(Show_Effect_First
, Show_Effect_Last
, MainFrame::OnShowEffect
)
248 MainFrame::MainFrame()
249 : wxFrame(NULL
, wxID_ANY
, "wxWidgets Shaped Sample",
250 wxDefaultPosition
, wxSize(200, 100))
252 SetIcon(wxICON(sample
));
254 wxMenuBar
* const mbar
= new wxMenuBar
;
255 wxMenu
* const menuFrames
= new wxMenu
;
256 menuFrames
->Append(Show_Shaped
, "Show &shaped window\tCtrl-S");
257 menuFrames
->Append(Show_Transparent
, "Show &transparent window\tCtrl-T");
258 menuFrames
->AppendSeparator();
259 menuFrames
->Append(Show_Effect_Roll
, "Show &rolled effect\tCtrl-R");
260 menuFrames
->Append(Show_Effect_Slide
, "Show s&lide effect\tCtrl-L");
261 menuFrames
->Append(Show_Effect_Blend
, "Show &fade effect\tCtrl-F");
262 menuFrames
->Append(Show_Effect_Expand
, "Show &expand effect\tCtrl-E");
263 menuFrames
->AppendSeparator();
264 menuFrames
->Append(wxID_EXIT
, "E&xit");
266 mbar
->Append(menuFrames
, "&Show");
272 void MainFrame::OnShowShaped(wxCommandEvent
& WXUNUSED(event
))
274 ShapedFrame
*shapedFrame
= new ShapedFrame(this);
275 shapedFrame
->Show(true);
278 void MainFrame::OnShowTransparent(wxCommandEvent
& WXUNUSED(event
))
280 SeeThroughFrame
*seeThroughFrame
= new SeeThroughFrame();
281 seeThroughFrame
->Show(true);
284 void MainFrame::OnShowEffect(wxCommandEvent
& event
)
286 int effect
= event
.GetId();
287 static wxDirection direction
= wxLEFT
;
288 direction
= (wxDirection
)(((int)direction
)<< 1);
289 if ( direction
> wxDOWN
)
295 case Show_Effect_Roll
:
299 eff
= wxSHOW_EFFECT_ROLL_TO_LEFT
;
302 eff
= wxSHOW_EFFECT_ROLL_TO_RIGHT
;
305 eff
= wxSHOW_EFFECT_ROLL_TO_TOP
;
308 eff
= wxSHOW_EFFECT_ROLL_TO_BOTTOM
;
311 wxFAIL_MSG( "invalid direction" );
315 case Show_Effect_Slide
:
319 eff
= wxSHOW_EFFECT_SLIDE_TO_LEFT
;
322 eff
= wxSHOW_EFFECT_SLIDE_TO_RIGHT
;
325 eff
= wxSHOW_EFFECT_SLIDE_TO_TOP
;
328 eff
= wxSHOW_EFFECT_SLIDE_TO_BOTTOM
;
331 wxFAIL_MSG( "invalid direction" );
336 case Show_Effect_Blend
:
337 eff
= wxSHOW_EFFECT_BLEND
;
340 case Show_Effect_Expand
:
341 eff
= wxSHOW_EFFECT_EXPAND
;
345 wxFAIL_MSG( "invalid effect" );
349 new EffectFrame(this, eff
,1000);
352 // ----------------------------------------------------------------------------
354 // ----------------------------------------------------------------------------
356 BEGIN_EVENT_TABLE(ShapedFrame
, wxFrame
)
357 EVT_LEFT_DCLICK(ShapedFrame::OnDoubleClick
)
358 EVT_LEFT_DOWN(ShapedFrame::OnLeftDown
)
359 EVT_LEFT_UP(ShapedFrame::OnLeftUp
)
360 EVT_MOTION(ShapedFrame::OnMouseMove
)
361 EVT_RIGHT_UP(ShapedFrame::OnExit
)
362 EVT_PAINT(ShapedFrame::OnPaint
)
367 ShapedFrame::ShapedFrame(wxFrame
*parent
)
368 : wxFrame(parent
, wxID_ANY
, wxEmptyString
,
369 wxDefaultPosition
, wxSize(100, 100),
378 m_bmp
= wxBitmap(wxT("star.png"), wxBITMAP_TYPE_PNG
);
379 SetSize(wxSize(m_bmp
.GetWidth(), m_bmp
.GetHeight()));
380 SetToolTip(wxT("Right-click to close"));
384 void ShapedFrame::SetWindowShape()
386 wxRegion
region(m_bmp
, *wxWHITE
);
387 m_hasShape
= SetShape(region
);
390 void ShapedFrame::OnDoubleClick(wxMouseEvent
& WXUNUSED(evt
))
402 void ShapedFrame::OnLeftDown(wxMouseEvent
& evt
)
405 wxPoint pos
= ClientToScreen(evt
.GetPosition());
406 wxPoint origin
= GetPosition();
407 int dx
= pos
.x
- origin
.x
;
408 int dy
= pos
.y
- origin
.y
;
409 m_delta
= wxPoint(dx
, dy
);
412 void ShapedFrame::OnLeftUp(wxMouseEvent
& WXUNUSED(evt
))
420 void ShapedFrame::OnMouseMove(wxMouseEvent
& evt
)
422 wxPoint pt
= evt
.GetPosition();
423 if (evt
.Dragging() && evt
.LeftIsDown())
425 wxPoint pos
= ClientToScreen(pt
);
426 Move(wxPoint(pos
.x
- m_delta
.x
, pos
.y
- m_delta
.y
));
430 void ShapedFrame::OnExit(wxMouseEvent
& WXUNUSED(evt
))
435 void ShapedFrame::OnPaint(wxPaintEvent
& WXUNUSED(evt
))
438 dc
.DrawBitmap(m_bmp
, 0, 0, true);
441 // ----------------------------------------------------------------------------
443 // ----------------------------------------------------------------------------
445 BEGIN_EVENT_TABLE(SeeThroughFrame
, wxFrame
)
446 EVT_LEFT_DCLICK(SeeThroughFrame::OnDoubleClick
)
447 EVT_PAINT(SeeThroughFrame::OnPaint
)
450 SeeThroughFrame::SeeThroughFrame()
451 : wxFrame(NULL
, wxID_ANY
, "Transparency test: double click here",
452 wxPoint(100, 30), wxSize(300, 300),
453 wxDEFAULT_FRAME_STYLE
|
454 wxFULL_REPAINT_ON_RESIZE
|
456 m_currentState(STATE_SEETHROUGH
)
458 SetBackgroundColour(wxColour(255, 255, 255, 255));
459 SetBackgroundStyle(wxBG_STYLE_TRANSPARENT
);
462 // Paints a grid of varying hue and alpha
463 void SeeThroughFrame::OnPaint(wxPaintEvent
& WXUNUSED(evt
))
466 dc
.SetPen(wxNullPen
);
471 float xstep
= 1. / xcount
;
472 float ystep
= 1. / ycount
;
474 int width
= GetClientSize().GetWidth();
475 int height
= GetClientSize().GetHeight();
477 for ( float x
= 0.; x
< 1.; x
+= xstep
)
479 for ( float y
= 0.; y
< 1.; y
+= ystep
)
481 wxImage::RGBValue v
= wxImage::HSVtoRGB(wxImage::HSVValue(x
, 1., 1.));
482 dc
.SetBrush(wxBrush(wxColour(v
.red
, v
.green
, v
.blue
,
483 (int)(255*(1. - y
)))));
484 int x1
= (int)(x
* width
);
485 int y1
= (int)(y
* height
);
486 int x2
= (int)((x
+ xstep
) * width
);
487 int y2
= (int)((y
+ ystep
) * height
);
488 dc
.DrawRectangle(x1
, y1
, x2
- x1
, y2
- y1
);
493 // Switches between colour and transparent background on doubleclick
494 void SeeThroughFrame::OnDoubleClick(wxMouseEvent
& WXUNUSED(evt
))
496 m_currentState
= (State
)((m_currentState
+ 1) % STATE_MAX
);
498 switch ( m_currentState
)
501 SetBackgroundStyle(wxBG_STYLE_COLOUR
);
506 case STATE_SEETHROUGH
:
507 SetBackgroundStyle(wxBG_STYLE_TRANSPARENT
);
509 SetTitle("See through");
512 case STATE_TRANSPARENT
:
513 SetBackgroundStyle(wxBG_STYLE_COLOUR
);
515 SetTitle("Semi-transparent");
519 wxFAIL_MSG( "unreachable" );