]>
git.saurik.com Git - wxWidgets.git/blob - samples/shaped/shaped.cpp
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
);
116 // any class wishing to process wxWidgets events must use this macro
117 DECLARE_EVENT_TABLE()
120 // Define a new frame type: this is going to the frame showing the
121 // effect of wxWindow::SetTransparent and of
122 // wxWindow::SetBackgroundStyle(wxBG_STYLE_TRANSPARENT)
123 class SeeThroughFrame
: public wxFrame
129 // event handlers (these functions should _not_ be virtual)
130 void OnDoubleClick(wxMouseEvent
& evt
);
131 void OnPaint(wxPaintEvent
& evt
);
142 State m_currentState
;
144 // any class wishing to process wxWidgets events must use this macro
145 DECLARE_EVENT_TABLE()
148 class EffectFrame
: public wxFrame
151 EffectFrame(wxWindow
*parent
,
153 // TODO: add menu command to the main frame to allow changing
155 unsigned timeout
= 1000,
156 wxDirection dir
= wxBOTTOM
)
157 : wxFrame(parent
, wxID_ANY
,
158 wxString::Format("Frame shown with %s effect",
159 GetEffectName(effect
)),
160 wxDefaultPosition
, wxSize(450, 300)),
165 new wxStaticText(this, wxID_ANY
,
166 wxString::Format("Effect: %s", GetEffectName(effect
)),
168 new wxStaticText(this, wxID_ANY
,
169 wxString::Format("Timeout: %ums", m_timeout
),
172 ShowWithEffect(m_effect
, m_timeout
, m_dir
);
174 Connect(wxEVT_CLOSE_WINDOW
, wxCloseEventHandler(EffectFrame::OnClose
));
178 static const char *GetEffectName(wxShowEffect effect
)
180 static const char *names
[] =
182 "roll", "slide", "fade", "expand",
184 wxCOMPILE_TIME_ASSERT( WXSIZEOF(names
) == wxSHOW_EFFECT_MAX
,
185 EffectNamesMismatch
);
187 return names
[effect
];
190 void OnClose(wxCloseEvent
& event
)
192 HideWithEffect(m_effect
, m_timeout
, m_dir
);
197 wxShowEffect m_effect
;
202 // ============================================================================
204 // ============================================================================
206 // ----------------------------------------------------------------------------
207 // the application class
208 // ----------------------------------------------------------------------------
212 // `Main program' equivalent: the program execution "starts" here
215 if ( !wxApp::OnInit() )
218 wxInitAllImageHandlers();
222 // success: wxApp::OnRun() will be called which will enter the main message
223 // loop and the application will run. If we returned false here, the
224 // application would exit immediately.
228 // ----------------------------------------------------------------------------
230 // ----------------------------------------------------------------------------
232 BEGIN_EVENT_TABLE(MainFrame
, wxFrame
)
233 EVT_MENU(Show_Shaped
, MainFrame::OnShowShaped
)
234 EVT_MENU(Show_Transparent
, MainFrame::OnShowTransparent
)
235 EVT_MENU_RANGE(Show_Effect_First
, Show_Effect_Last
, MainFrame::OnShowEffect
)
238 MainFrame::MainFrame()
239 : wxFrame(NULL
, wxID_ANY
, "wxWidgets Shaped Sample",
240 wxDefaultPosition
, wxSize(200, 100))
242 wxMenuBar
* const mbar
= new wxMenuBar
;
243 wxMenu
* const menuFrames
= new wxMenu
;
244 menuFrames
->Append(Show_Shaped
, "Show &shaped window\tCtrl-S");
245 menuFrames
->Append(Show_Transparent
, "Show &transparent window\tCtrl-T");
246 menuFrames
->AppendSeparator();
247 menuFrames
->Append(Show_Effect_Roll
, "Show &rolled effect\tCtrl-R");
248 menuFrames
->Append(Show_Effect_Slide
, "Show s&lide effect\tCtrl-L");
249 menuFrames
->Append(Show_Effect_Blend
, "Show &fade effect\tCtrl-F");
250 menuFrames
->Append(Show_Effect_Expand
, "Show &expand effect\tCtrl-E");
251 menuFrames
->AppendSeparator();
252 menuFrames
->Append(wxID_EXIT
, "E&xit");
254 mbar
->Append(menuFrames
, "&Show");
260 void MainFrame::OnShowShaped(wxCommandEvent
& WXUNUSED(event
))
262 ShapedFrame
*shapedFrame
= new ShapedFrame(this);
263 shapedFrame
->Show(true);
266 void MainFrame::OnShowTransparent(wxCommandEvent
& WXUNUSED(event
))
268 SeeThroughFrame
*seeThroughFrame
= new SeeThroughFrame();
269 seeThroughFrame
->Show(true);
272 void MainFrame::OnShowEffect(wxCommandEvent
& event
)
274 int effect
= wxSHOW_EFFECT_ROLL
+ event
.GetId() - Show_Effect_Roll
;
275 static wxDirection direction
= wxLEFT
;
276 direction
= (wxDirection
)(((int)direction
)<< 1);
277 if ( direction
> wxDOWN
)
279 new EffectFrame(this, wx_static_cast(wxShowEffect
, effect
),1000,direction
);
282 // ----------------------------------------------------------------------------
284 // ----------------------------------------------------------------------------
286 BEGIN_EVENT_TABLE(ShapedFrame
, wxFrame
)
287 EVT_LEFT_DCLICK(ShapedFrame::OnDoubleClick
)
288 EVT_LEFT_DOWN(ShapedFrame::OnLeftDown
)
289 EVT_LEFT_UP(ShapedFrame::OnLeftUp
)
290 EVT_MOTION(ShapedFrame::OnMouseMove
)
291 EVT_RIGHT_UP(ShapedFrame::OnExit
)
292 EVT_PAINT(ShapedFrame::OnPaint
)
297 ShapedFrame::ShapedFrame(wxFrame
*parent
)
298 : wxFrame(parent
, wxID_ANY
, wxEmptyString
,
299 wxDefaultPosition
, wxSize(100, 100),
308 m_bmp
= wxBitmap(_T("star.png"), wxBITMAP_TYPE_PNG
);
309 SetSize(wxSize(m_bmp
.GetWidth(), m_bmp
.GetHeight()));
310 SetToolTip(wxT("Right-click to close"));
314 void ShapedFrame::SetWindowShape()
316 wxRegion
region(m_bmp
, *wxWHITE
);
317 m_hasShape
= SetShape(region
);
320 void ShapedFrame::OnDoubleClick(wxMouseEvent
& WXUNUSED(evt
))
332 void ShapedFrame::OnLeftDown(wxMouseEvent
& evt
)
335 wxPoint pos
= ClientToScreen(evt
.GetPosition());
336 wxPoint origin
= GetPosition();
337 int dx
= pos
.x
- origin
.x
;
338 int dy
= pos
.y
- origin
.y
;
339 m_delta
= wxPoint(dx
, dy
);
342 void ShapedFrame::OnLeftUp(wxMouseEvent
& WXUNUSED(evt
))
350 void ShapedFrame::OnMouseMove(wxMouseEvent
& evt
)
352 wxPoint pt
= evt
.GetPosition();
353 if (evt
.Dragging() && evt
.LeftIsDown())
355 wxPoint pos
= ClientToScreen(pt
);
356 Move(wxPoint(pos
.x
- m_delta
.x
, pos
.y
- m_delta
.y
));
360 void ShapedFrame::OnExit(wxMouseEvent
& WXUNUSED(evt
))
365 void ShapedFrame::OnPaint(wxPaintEvent
& WXUNUSED(evt
))
368 dc
.DrawBitmap(m_bmp
, 0, 0, true);
371 // ----------------------------------------------------------------------------
373 // ----------------------------------------------------------------------------
375 BEGIN_EVENT_TABLE(SeeThroughFrame
, wxFrame
)
376 EVT_LEFT_DCLICK(SeeThroughFrame::OnDoubleClick
)
377 EVT_PAINT(SeeThroughFrame::OnPaint
)
380 SeeThroughFrame::SeeThroughFrame()
381 : wxFrame(NULL
, wxID_ANY
, "Transparency test: double click here",
382 wxPoint(100, 30), wxSize(300, 300),
383 wxDEFAULT_FRAME_STYLE
|
384 wxFULL_REPAINT_ON_RESIZE
|
386 m_currentState(STATE_SEETHROUGH
)
388 SetBackgroundColour(wxColour(255, 255, 255, 255));
389 SetBackgroundStyle(wxBG_STYLE_TRANSPARENT
);
392 // Paints a grid of varying hue and alpha
393 void SeeThroughFrame::OnPaint(wxPaintEvent
& WXUNUSED(evt
))
396 dc
.SetPen(wxNullPen
);
401 float xstep
= 1. / xcount
;
402 float ystep
= 1. / ycount
;
404 int width
= GetClientSize().GetWidth();
405 int height
= GetClientSize().GetHeight();
407 for ( float x
= 0.; x
< 1.; x
+= xstep
)
409 for ( float y
= 0.; y
< 1.; y
+= ystep
)
411 wxImage::RGBValue v
= wxImage::HSVtoRGB(wxImage::HSVValue(x
, 1., 1.));
412 dc
.SetBrush(wxBrush(wxColour(v
.red
, v
.green
, v
.blue
,
413 (int)(255*(1. - y
)))));
414 int x1
= (int)(x
* width
);
415 int y1
= (int)(y
* height
);
416 int x2
= (int)((x
+ xstep
) * width
);
417 int y2
= (int)((y
+ ystep
) * height
);
418 dc
.DrawRectangle(x1
, y1
, x2
- x1
, y2
- y1
);
423 // Switches between colour and transparent background on doubleclick
424 void SeeThroughFrame::OnDoubleClick(wxMouseEvent
& WXUNUSED(evt
))
426 m_currentState
= (State
)((m_currentState
+ 1) % STATE_MAX
);
428 switch ( m_currentState
)
431 SetBackgroundStyle(wxBG_STYLE_COLOUR
);
436 case STATE_SEETHROUGH
:
437 SetBackgroundStyle(wxBG_STYLE_TRANSPARENT
);
439 SetTitle("See through");
442 case STATE_TRANSPARENT
:
443 SetBackgroundStyle(wxBG_STYLE_COLOUR
);
445 SetTitle("Semi-transparent");
449 wxFAIL_MSG( "unreachable" );