]> git.saurik.com Git - wxWidgets.git/blob - samples/shaped/shaped.cpp
use wxEvent::ShouldPropagate() instead of its own propgation control mechanism in...
[wxWidgets.git] / samples / shaped / shaped.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: shaped.cpp
3 // Purpose: Shaped Window sample
4 // Author: Robin Dunn
5 // Modified by:
6 // Created: 28-Mar-2003
7 // RCS-ID: $Id$
8 // Copyright: (c) Robin Dunn
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(__APPLE__)
21 #pragma implementation "shaped.cpp"
22 #pragma interface "shaped.cpp"
23 #endif
24
25 // For compilers that support precompilation, includes "wx/wx.h".
26 #include "wx/wxprec.h"
27
28 #ifdef __BORLANDC__
29 #pragma hdrstop
30 #endif
31
32 // for all others, include the necessary headers
33 #ifndef WX_PRECOMP
34 #include "wx/app.h"
35 #include "wx/log.h"
36 #include "wx/frame.h"
37 #include "wx/panel.h"
38 #include "wx/stattext.h"
39 #include "wx/menu.h"
40 #include "wx/layout.h"
41 #include "wx/msgdlg.h"
42 #include "wx/image.h"
43 #endif
44
45 #include "wx/dcclient.h"
46 #include "wx/image.h"
47
48 // ----------------------------------------------------------------------------
49 // private classes
50 // ----------------------------------------------------------------------------
51
52 // Define a new application type, each program should derive a class from wxApp
53 class MyApp : public wxApp
54 {
55 public:
56 // override base class virtuals
57 // ----------------------------
58
59 // this one is called on application startup and is a good place for the app
60 // initialization (doing it here and not in the ctor allows to have an error
61 // return: if OnInit() returns false, the application terminates)
62 virtual bool OnInit();
63 };
64
65
66 // Define a new frame type: this is going to be our main frame
67 class ShapedFrame : public wxFrame
68 {
69 public:
70 // ctor(s)
71 ShapedFrame();
72 void SetWindowShape();
73
74 // event handlers (these functions should _not_ be virtual)
75 void OnDoubleClick(wxMouseEvent& evt);
76 void OnLeftDown(wxMouseEvent& evt);
77 void OnLeftUp(wxMouseEvent& evt);
78 void OnMouseMove(wxMouseEvent& evt);
79 void OnExit(wxMouseEvent& evt);
80 void OnPaint(wxPaintEvent& evt);
81 void OnWindowCreate(wxWindowCreateEvent& evt);
82
83 private:
84 bool m_hasShape;
85 wxBitmap m_bmp;
86 wxPoint m_delta;
87
88 // any class wishing to process wxWindows events must use this macro
89 DECLARE_EVENT_TABLE()
90 };
91
92
93 // ----------------------------------------------------------------------------
94 // event tables and other macros for wxWindows
95 // ----------------------------------------------------------------------------
96
97 // the event tables connect the wxWindows events with the functions (event
98 // handlers) which process them. It can be also done at run-time, but for the
99 // simple menu events like this the static method is much simpler.
100 BEGIN_EVENT_TABLE(ShapedFrame, wxFrame)
101 EVT_LEFT_DCLICK(ShapedFrame::OnDoubleClick)
102 EVT_LEFT_DOWN(ShapedFrame::OnLeftDown)
103 EVT_LEFT_UP(ShapedFrame::OnLeftUp)
104 EVT_MOTION(ShapedFrame::OnMouseMove)
105 EVT_RIGHT_UP(ShapedFrame::OnExit)
106
107 EVT_PAINT(ShapedFrame::OnPaint)
108
109 #ifdef __WXGTK__
110 EVT_WINDOW_CREATE(ShapedFrame::OnWindowCreate)
111 #endif
112 END_EVENT_TABLE()
113
114
115 // Create a new application object: this macro will allow wxWindows to create
116 // the application object during program execution (it's better than using a
117 // static object for many reasons) and also declares the accessor function
118 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
119 // not wxApp)
120 IMPLEMENT_APP(MyApp)
121
122 // ============================================================================
123 // implementation
124 // ============================================================================
125
126 // ----------------------------------------------------------------------------
127 // the application class
128 // ----------------------------------------------------------------------------
129
130 // `Main program' equivalent: the program execution "starts" here
131 bool MyApp::OnInit()
132 {
133 wxInitAllImageHandlers();
134
135 // Create the main application window
136 ShapedFrame *frame = new ShapedFrame();
137 frame->Show(TRUE);
138 SetTopWindow(frame);
139
140 // success: wxApp::OnRun() will be called which will enter the main message
141 // loop and the application will run. If we returned FALSE here, the
142 // application would exit immediately.
143 return TRUE;
144 }
145
146 // ----------------------------------------------------------------------------
147 // main frame
148 // ----------------------------------------------------------------------------
149
150 // frame constructor
151 ShapedFrame::ShapedFrame()
152 : wxFrame((wxFrame *)NULL, -1, wxEmptyString,
153 wxDefaultPosition, wxSize(100, 100), //wxDefaultSize,
154 0
155 | wxFRAME_SHAPED
156 | wxSIMPLE_BORDER
157 | wxFRAME_NO_TASKBAR
158 | wxSTAY_ON_TOP
159 )
160 {
161 m_hasShape = FALSE;
162 m_bmp = wxBitmap(_T("star.png"), wxBITMAP_TYPE_PNG);
163 SetSize(wxSize(m_bmp.GetWidth(), m_bmp.GetHeight()));
164 #ifndef __WXMAC__
165 // On wxMac the tooltip gets clipped by the window shape, YUCK!!
166 #if wxUSE_TOOLTOP
167 SetToolTip(wxT("Right-click to exit"));
168 #endif
169 #endif
170 #ifndef __WXGTK__
171 // On wxGTK we can't do this yet because the window hasn't been created
172 // yet so we wait until the EVT_WINDOW_CREATE event happens. On wxMSW and
173 // wxMac the window has been created at this point so we go ahead and set
174 // the shape now.
175 SetWindowShape();
176 #endif
177 }
178
179 void ShapedFrame::SetWindowShape()
180 {
181 wxRegion region(m_bmp, *wxWHITE);
182 m_hasShape = SetShape(region);
183 }
184
185 void ShapedFrame::OnDoubleClick(wxMouseEvent& WXUNUSED(evt))
186 {
187 if (m_hasShape)
188 {
189 wxRegion region;
190 SetShape(region);
191 m_hasShape = FALSE;
192 }
193 else
194 SetWindowShape();
195 }
196
197 void ShapedFrame::OnLeftDown(wxMouseEvent& evt)
198 {
199 CaptureMouse();
200 //printf("Mouse captured\n");
201 wxPoint pos = ClientToScreen(evt.GetPosition());
202 wxPoint origin = GetPosition();
203 int dx = pos.x - origin.x;
204 int dy = pos.y - origin.y;
205 m_delta = wxPoint(dx, dy);
206 }
207
208 void ShapedFrame::OnLeftUp(wxMouseEvent& WXUNUSED(evt))
209 {
210 if (HasCapture())
211 {
212 ReleaseMouse();
213 //printf("Mouse released\n");
214 }
215 }
216
217 void ShapedFrame::OnMouseMove(wxMouseEvent& evt)
218 {
219 wxPoint pt = evt.GetPosition();
220 //printf("x:%d y:%d\n", pt.x, pt.y);
221 if (evt.Dragging() && evt.LeftIsDown())
222 {
223 wxPoint pos = ClientToScreen(pt);
224 Move(wxPoint(pos.x - m_delta.x, pos.y - m_delta.y));
225 }
226 }
227
228 void ShapedFrame::OnExit(wxMouseEvent& WXUNUSED(evt))
229 {
230 Close();
231 }
232
233 void ShapedFrame::OnPaint(wxPaintEvent& WXUNUSED(evt))
234 {
235 wxPaintDC dc(this);
236 dc.DrawBitmap(m_bmp, 0, 0, TRUE);
237 }
238
239 void ShapedFrame::OnWindowCreate(wxWindowCreateEvent& WXUNUSED(evt))
240 {
241 SetWindowShape();
242 }
243