]>
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
);
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 static wxDirection direction
= wxLEFT
;
277 direction
= (wxDirection
)(((int)direction
)<< 1);
278 if ( direction
> wxDOWN
)
280 new EffectFrame(this, wx_static_cast(wxShowEffect
, effect
),1000,direction
);
283 // ----------------------------------------------------------------------------
285 // ----------------------------------------------------------------------------
287 BEGIN_EVENT_TABLE(ShapedFrame
, wxFrame
)
288 EVT_LEFT_DCLICK(ShapedFrame::OnDoubleClick
)
289 EVT_LEFT_DOWN(ShapedFrame::OnLeftDown
)
290 EVT_LEFT_UP(ShapedFrame::OnLeftUp
)
291 EVT_MOTION(ShapedFrame::OnMouseMove
)
292 EVT_RIGHT_UP(ShapedFrame::OnExit
)
294 EVT_PAINT(ShapedFrame::OnPaint
)
297 EVT_WINDOW_CREATE(ShapedFrame::OnWindowCreate
)
303 ShapedFrame::ShapedFrame(wxFrame
*parent
)
304 : wxFrame(parent
, wxID_ANY
, wxEmptyString
,
305 wxDefaultPosition
, wxSize(100, 100),
314 m_bmp
= wxBitmap(_T("star.png"), wxBITMAP_TYPE_PNG
);
315 SetSize(wxSize(m_bmp
.GetWidth(), m_bmp
.GetHeight()));
316 SetToolTip(wxT("Right-click to close"));
319 // On wxGTK we can't do this yet because the window hasn't been created
320 // yet so we wait until the EVT_WINDOW_CREATE event happens. On wxMSW and
321 // wxMac the window has been created at this point so we go ahead and set
327 void ShapedFrame::SetWindowShape()
329 wxRegion
region(m_bmp
, *wxWHITE
);
330 m_hasShape
= SetShape(region
);
333 void ShapedFrame::OnDoubleClick(wxMouseEvent
& WXUNUSED(evt
))
345 void ShapedFrame::OnLeftDown(wxMouseEvent
& evt
)
348 wxPoint pos
= ClientToScreen(evt
.GetPosition());
349 wxPoint origin
= GetPosition();
350 int dx
= pos
.x
- origin
.x
;
351 int dy
= pos
.y
- origin
.y
;
352 m_delta
= wxPoint(dx
, dy
);
355 void ShapedFrame::OnLeftUp(wxMouseEvent
& WXUNUSED(evt
))
363 void ShapedFrame::OnMouseMove(wxMouseEvent
& evt
)
365 wxPoint pt
= evt
.GetPosition();
366 if (evt
.Dragging() && evt
.LeftIsDown())
368 wxPoint pos
= ClientToScreen(pt
);
369 Move(wxPoint(pos
.x
- m_delta
.x
, pos
.y
- m_delta
.y
));
373 void ShapedFrame::OnExit(wxMouseEvent
& WXUNUSED(evt
))
378 void ShapedFrame::OnPaint(wxPaintEvent
& WXUNUSED(evt
))
381 dc
.DrawBitmap(m_bmp
, 0, 0, true);
384 void ShapedFrame::OnWindowCreate(wxWindowCreateEvent
& WXUNUSED(evt
))
389 // ----------------------------------------------------------------------------
391 // ----------------------------------------------------------------------------
393 BEGIN_EVENT_TABLE(SeeThroughFrame
, wxFrame
)
394 EVT_LEFT_DCLICK(SeeThroughFrame::OnDoubleClick
)
395 EVT_PAINT(SeeThroughFrame::OnPaint
)
398 SeeThroughFrame::SeeThroughFrame()
399 : wxFrame(NULL
, wxID_ANY
, "Transparency test: double click here",
400 wxPoint(100, 30), wxSize(300, 300),
401 wxDEFAULT_FRAME_STYLE
|
402 wxFULL_REPAINT_ON_RESIZE
|
404 m_currentState(STATE_SEETHROUGH
)
406 SetBackgroundColour(wxColour(255, 255, 255, 255));
407 SetBackgroundStyle(wxBG_STYLE_TRANSPARENT
);
410 // Paints a grid of varying hue and alpha
411 void SeeThroughFrame::OnPaint(wxPaintEvent
& WXUNUSED(evt
))
414 dc
.SetPen(wxNullPen
);
419 float xstep
= 1. / xcount
;
420 float ystep
= 1. / ycount
;
422 int width
= GetClientSize().GetWidth();
423 int height
= GetClientSize().GetHeight();
425 for ( float x
= 0.; x
< 1.; x
+= xstep
)
427 for ( float y
= 0.; y
< 1.; y
+= ystep
)
429 wxImage::RGBValue v
= wxImage::HSVtoRGB(wxImage::HSVValue(x
, 1., 1.));
430 dc
.SetBrush(wxBrush(wxColour(v
.red
, v
.green
, v
.blue
,
431 (int)(255*(1. - y
)))));
432 int x1
= (int)(x
* width
);
433 int y1
= (int)(y
* height
);
434 int x2
= (int)((x
+ xstep
) * width
);
435 int y2
= (int)((y
+ ystep
) * height
);
436 dc
.DrawRectangle(x1
, y1
, x2
- x1
, y2
- y1
);
441 // Switches between colour and transparent background on doubleclick
442 void SeeThroughFrame::OnDoubleClick(wxMouseEvent
& WXUNUSED(evt
))
444 m_currentState
= (State
)((m_currentState
+ 1) % STATE_MAX
);
446 switch ( m_currentState
)
449 SetBackgroundStyle(wxBG_STYLE_COLOUR
);
454 case STATE_SEETHROUGH
:
455 SetBackgroundStyle(wxBG_STYLE_TRANSPARENT
);
457 SetTitle("See through");
460 case STATE_TRANSPARENT
:
461 SetBackgroundStyle(wxBG_STYLE_COLOUR
);
463 SetTitle("Semi-transparent");
467 wxFAIL_MSG( "unreachable" );