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 // ----------------------------------------------------------------------------
53 // must be consecutive and in the same order as wxShowEffect enum elements
55 Show_Effect_Roll
= Show_Effect_First
,
59 Show_Effect_Last
= Show_Effect_Expand
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 // Define a new application type, each program should derive a class from wxApp
67 class MyApp
: public wxApp
70 // override base class virtuals
71 // ----------------------------
73 // this one is called on application startup and is a good place for the app
74 // initialization (doing it here and not in the ctor allows to have an error
75 // return: if OnInit() returns false, the application terminates)
76 virtual bool OnInit();
80 // Main frame just contains the menu items invoking the other tests
81 class MainFrame
: public wxFrame
87 void OnShowShaped(wxCommandEvent
& event
);
88 void OnShowTransparent(wxCommandEvent
& event
);
89 void OnShowEffect(wxCommandEvent
& event
);
94 // Define a new frame type: this is going to the frame showing the
95 // effect of wxFRAME_SHAPED
96 class ShapedFrame
: public wxFrame
100 ShapedFrame(wxFrame
*parent
);
101 void SetWindowShape();
103 // event handlers (these functions should _not_ be virtual)
104 void OnDoubleClick(wxMouseEvent
& evt
);
105 void OnLeftDown(wxMouseEvent
& evt
);
106 void OnLeftUp(wxMouseEvent
& evt
);
107 void OnMouseMove(wxMouseEvent
& evt
);
108 void OnExit(wxMouseEvent
& evt
);
109 void OnPaint(wxPaintEvent
& evt
);
110 void OnWindowCreate(wxWindowCreateEvent
& 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 wxDirection dir
= wxBOTTOM
)
158 : wxFrame(parent
, wxID_ANY
,
159 wxString
::Format("Frame shown with %s effect",
160 GetEffectName(effect
)),
161 wxDefaultPosition
, wxSize(450, 300)),
166 new wxStaticText(this, wxID_ANY
,
167 wxString
::Format("Effect: %s", GetEffectName(effect
)),
169 new wxStaticText(this, wxID_ANY
,
170 wxString
::Format("Timeout: %ums", m_timeout
),
173 ShowWithEffect(m_effect
, m_timeout
, m_dir
);
175 Connect(wxEVT_CLOSE_WINDOW
, wxCloseEventHandler(EffectFrame
::OnClose
));
179 static const char *GetEffectName(wxShowEffect effect
)
181 static const char *names
[] =
183 "roll", "slide", "fade", "expand",
185 wxCOMPILE_TIME_ASSERT( WXSIZEOF(names
) == wxSHOW_EFFECT_MAX
,
186 EffectNamesMismatch
);
188 return names
[effect
];
191 void OnClose(wxCloseEvent
& event
)
193 HideWithEffect(m_effect
, m_timeout
, m_dir
);
198 wxShowEffect m_effect
;
203 // ============================================================================
205 // ============================================================================
207 // ----------------------------------------------------------------------------
208 // the application class
209 // ----------------------------------------------------------------------------
213 // `Main program' equivalent: the program execution "starts" here
216 if ( !wxApp
::OnInit() )
219 wxInitAllImageHandlers();
223 // success: wxApp::OnRun() will be called which will enter the main message
224 // loop and the application will run. If we returned false here, the
225 // application would exit immediately.
229 // ----------------------------------------------------------------------------
231 // ----------------------------------------------------------------------------
233 BEGIN_EVENT_TABLE(MainFrame
, wxFrame
)
234 EVT_MENU(Show_Shaped
, MainFrame
::OnShowShaped
)
235 EVT_MENU(Show_Transparent
, MainFrame
::OnShowTransparent
)
236 EVT_MENU_RANGE(Show_Effect_First
, Show_Effect_Last
, MainFrame
::OnShowEffect
)
239 MainFrame
::MainFrame()
240 : wxFrame(NULL
, wxID_ANY
, "wxWidgets Shaped Sample",
241 wxDefaultPosition
, wxSize(200, 100))
243 wxMenuBar
* const mbar
= new wxMenuBar
;
244 wxMenu
* const menuFrames
= new wxMenu
;
245 menuFrames
->Append(Show_Shaped
, "Show &shaped window\tCtrl-S");
246 menuFrames
->Append(Show_Transparent
, "Show &transparent window\tCtrl-T");
247 menuFrames
->AppendSeparator();
248 menuFrames
->Append(Show_Effect_Roll
, "Show &rolled effect\tCtrl-R");
249 menuFrames
->Append(Show_Effect_Slide
, "Show s&lide effect\tCtrl-L");
250 menuFrames
->Append(Show_Effect_Blend
, "Show &fade effect\tCtrl-F");
251 menuFrames
->Append(Show_Effect_Expand
, "Show &expand effect\tCtrl-E");
252 menuFrames
->AppendSeparator();
253 menuFrames
->Append(wxID_EXIT
, "E&xit");
255 mbar
->Append(menuFrames
, "&Show");
261 void MainFrame
::OnShowShaped(wxCommandEvent
& WXUNUSED(event
))
263 ShapedFrame
*shapedFrame
= new ShapedFrame(this);
264 shapedFrame
->Show(true);
267 void MainFrame
::OnShowTransparent(wxCommandEvent
& WXUNUSED(event
))
269 SeeThroughFrame
*seeThroughFrame
= new SeeThroughFrame();
270 seeThroughFrame
->Show(true);
273 void MainFrame
::OnShowEffect(wxCommandEvent
& event
)
275 int effect
= wxSHOW_EFFECT_ROLL
+ event
.GetId() - Show_Effect_Roll
;
276 new EffectFrame(this, wx_static_cast(wxShowEffect
, effect
));
279 // ----------------------------------------------------------------------------
281 // ----------------------------------------------------------------------------
283 BEGIN_EVENT_TABLE(ShapedFrame
, wxFrame
)
284 EVT_LEFT_DCLICK(ShapedFrame
::OnDoubleClick
)
285 EVT_LEFT_DOWN(ShapedFrame
::OnLeftDown
)
286 EVT_LEFT_UP(ShapedFrame
::OnLeftUp
)
287 EVT_MOTION(ShapedFrame
::OnMouseMove
)
288 EVT_RIGHT_UP(ShapedFrame
::OnExit
)
290 EVT_PAINT(ShapedFrame
::OnPaint
)
293 EVT_WINDOW_CREATE(ShapedFrame
::OnWindowCreate
)
299 ShapedFrame
::ShapedFrame(wxFrame
*parent
)
300 : wxFrame(parent
, wxID_ANY
, wxEmptyString
,
301 wxDefaultPosition
, wxSize(100, 100),
310 m_bmp
= wxBitmap(_T("star.png"), wxBITMAP_TYPE_PNG
);
311 SetSize(wxSize(m_bmp
.GetWidth(), m_bmp
.GetHeight()));
312 SetToolTip(wxT("Right-click to close"));
315 // On wxGTK we can't do this yet because the window hasn't been created
316 // yet so we wait until the EVT_WINDOW_CREATE event happens. On wxMSW and
317 // wxMac the window has been created at this point so we go ahead and set
323 void ShapedFrame
::SetWindowShape()
325 wxRegion
region(m_bmp
, *wxWHITE
);
326 m_hasShape
= SetShape(region
);
329 void ShapedFrame
::OnDoubleClick(wxMouseEvent
& WXUNUSED(evt
))
341 void ShapedFrame
::OnLeftDown(wxMouseEvent
& evt
)
344 wxPoint pos
= ClientToScreen(evt
.GetPosition());
345 wxPoint origin
= GetPosition();
346 int dx
= pos
.x
- origin
.x
;
347 int dy
= pos
.y
- origin
.y
;
348 m_delta
= wxPoint(dx
, dy
);
351 void ShapedFrame
::OnLeftUp(wxMouseEvent
& WXUNUSED(evt
))
359 void ShapedFrame
::OnMouseMove(wxMouseEvent
& evt
)
361 wxPoint pt
= evt
.GetPosition();
362 if (evt
.Dragging() && evt
.LeftIsDown())
364 wxPoint pos
= ClientToScreen(pt
);
365 Move(wxPoint(pos
.x
- m_delta
.x
, pos
.y
- m_delta
.y
));
369 void ShapedFrame
::OnExit(wxMouseEvent
& WXUNUSED(evt
))
374 void ShapedFrame
::OnPaint(wxPaintEvent
& WXUNUSED(evt
))
377 dc
.DrawBitmap(m_bmp
, 0, 0, true);
380 void ShapedFrame
::OnWindowCreate(wxWindowCreateEvent
& WXUNUSED(evt
))
385 // ----------------------------------------------------------------------------
387 // ----------------------------------------------------------------------------
389 BEGIN_EVENT_TABLE(SeeThroughFrame
, wxFrame
)
390 EVT_LEFT_DCLICK(SeeThroughFrame
::OnDoubleClick
)
391 EVT_PAINT(SeeThroughFrame
::OnPaint
)
394 SeeThroughFrame
::SeeThroughFrame()
395 : wxFrame(NULL
, wxID_ANY
, "Transparency test: double click here",
396 wxPoint(100, 30), wxSize(300, 300),
397 wxDEFAULT_FRAME_STYLE
|
398 wxFULL_REPAINT_ON_RESIZE
|
400 m_currentState(STATE_SEETHROUGH
)
402 SetBackgroundColour(wxColour(255, 255, 255, 255));
403 SetBackgroundStyle(wxBG_STYLE_TRANSPARENT
);
406 // Paints a grid of varying hue and alpha
407 void SeeThroughFrame
::OnPaint(wxPaintEvent
& WXUNUSED(evt
))
410 dc
.SetPen(wxNullPen
);
415 float xstep
= 1. / xcount
;
416 float ystep
= 1. / ycount
;
418 int width
= GetClientSize().GetWidth();
419 int height
= GetClientSize().GetHeight();
421 for ( float x
= 0.; x
< 1.; x
+= xstep
)
423 for ( float y
= 0.; y
< 1.; y
+= ystep
)
425 wxImage
::RGBValue v
= wxImage
::HSVtoRGB(wxImage
::HSVValue(x
, 1., 1.));
426 dc
.SetBrush(wxBrush(wxColour(v
.red
, v
.green
, v
.blue
,
427 (int)(255*(1. - y
)))));
428 int x1
= (int)(x
* width
);
429 int y1
= (int)(y
* height
);
430 int x2
= (int)((x
+ xstep
) * width
);
431 int y2
= (int)((y
+ ystep
) * height
);
432 dc
.DrawRectangle(x1
, y1
, x2
- x1
, y2
- y1
);
437 // Switches between colour and transparent background on doubleclick
438 void SeeThroughFrame
::OnDoubleClick(wxMouseEvent
& WXUNUSED(evt
))
440 m_currentState
= (State
)((m_currentState
+ 1) % STATE_MAX
);
442 switch ( m_currentState
)
445 SetBackgroundStyle(wxBG_STYLE_COLOUR
);
450 case STATE_SEETHROUGH
:
451 SetBackgroundStyle(wxBG_STYLE_TRANSPARENT
);
453 SetTitle("See through");
456 case STATE_TRANSPARENT
:
457 SetBackgroundStyle(wxBG_STYLE_COLOUR
);
459 SetTitle("Semi-transparent");
463 wxFAIL_MSG( "unreachable" );