]>
Commit | Line | Data |
---|---|---|
1542ea39 RD |
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__) | |
11fdee42 WS |
21 | #pragma implementation |
22 | #pragma interface | |
1542ea39 RD |
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" | |
6a7e6411 | 42 | #include "wx/image.h" |
1542ea39 RD |
43 | #endif |
44 | ||
45 | #include "wx/dcclient.h" | |
3b3ca9be | 46 | #include "wx/image.h" |
1542ea39 RD |
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 | ||
be5a51fb | 88 | // any class wishing to process wxWidgets events must use this macro |
1542ea39 RD |
89 | DECLARE_EVENT_TABLE() |
90 | }; | |
91 | ||
92 | ||
93 | // ---------------------------------------------------------------------------- | |
be5a51fb | 94 | // event tables and other macros for wxWidgets |
1542ea39 RD |
95 | // ---------------------------------------------------------------------------- |
96 | ||
be5a51fb | 97 | // the event tables connect the wxWidgets events with the functions (event |
1542ea39 RD |
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 | ||
be5a51fb | 115 | // Create a new application object: this macro will allow wxWidgets to create |
1542ea39 RD |
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(); | |
b62ca03d | 137 | frame->Show(true); |
6a7e6411 | 138 | SetTopWindow(frame); |
1542ea39 RD |
139 | |
140 | // success: wxApp::OnRun() will be called which will enter the main message | |
b62ca03d | 141 | // loop and the application will run. If we returned false here, the |
1542ea39 | 142 | // application would exit immediately. |
b62ca03d | 143 | return true; |
1542ea39 RD |
144 | } |
145 | ||
146 | // ---------------------------------------------------------------------------- | |
147 | // main frame | |
148 | // ---------------------------------------------------------------------------- | |
149 | ||
150 | // frame constructor | |
151 | ShapedFrame::ShapedFrame() | |
b62ca03d | 152 | : wxFrame((wxFrame *)NULL, wxID_ANY, wxEmptyString, |
6a7e6411 RD |
153 | wxDefaultPosition, wxSize(100, 100), //wxDefaultSize, |
154 | 0 | |
155 | | wxFRAME_SHAPED | |
156 | | wxSIMPLE_BORDER | |
157 | | wxFRAME_NO_TASKBAR | |
158 | | wxSTAY_ON_TOP | |
159 | ) | |
1542ea39 | 160 | { |
b62ca03d | 161 | m_hasShape = false; |
7df07b10 | 162 | m_bmp = wxBitmap(_T("star.png"), wxBITMAP_TYPE_PNG); |
1542ea39 | 163 | SetSize(wxSize(m_bmp.GetWidth(), m_bmp.GetHeight())); |
6a7e6411 RD |
164 | #ifndef __WXMAC__ |
165 | // On wxMac the tooltip gets clipped by the window shape, YUCK!! | |
166 | #if wxUSE_TOOLTOP | |
1542ea39 | 167 | SetToolTip(wxT("Right-click to exit")); |
c3b3654e | 168 | #endif |
6a7e6411 RD |
169 | #endif |
170 | #ifndef __WXGTK__ | |
1542ea39 | 171 | // On wxGTK we can't do this yet because the window hasn't been created |
6a7e6411 RD |
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. | |
1542ea39 RD |
175 | SetWindowShape(); |
176 | #endif | |
177 | } | |
178 | ||
179 | void ShapedFrame::SetWindowShape() | |
180 | { | |
181 | wxRegion region(m_bmp, *wxWHITE); | |
182 | m_hasShape = SetShape(region); | |
183 | } | |
184 | ||
256b8649 | 185 | void ShapedFrame::OnDoubleClick(wxMouseEvent& WXUNUSED(evt)) |
1542ea39 RD |
186 | { |
187 | if (m_hasShape) | |
188 | { | |
189 | wxRegion region; | |
190 | SetShape(region); | |
b62ca03d | 191 | m_hasShape = false; |
1542ea39 RD |
192 | } |
193 | else | |
194 | SetWindowShape(); | |
195 | } | |
196 | ||
197 | void ShapedFrame::OnLeftDown(wxMouseEvent& evt) | |
198 | { | |
199 | CaptureMouse(); | |
6a7e6411 | 200 | //printf("Mouse captured\n"); |
1542ea39 RD |
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 | ||
256b8649 | 208 | void ShapedFrame::OnLeftUp(wxMouseEvent& WXUNUSED(evt)) |
1542ea39 RD |
209 | { |
210 | if (HasCapture()) | |
6a7e6411 | 211 | { |
1542ea39 | 212 | ReleaseMouse(); |
6a7e6411 | 213 | //printf("Mouse released\n"); |
256b8649 | 214 | } |
1542ea39 RD |
215 | } |
216 | ||
217 | void ShapedFrame::OnMouseMove(wxMouseEvent& evt) | |
218 | { | |
6a7e6411 RD |
219 | wxPoint pt = evt.GetPosition(); |
220 | //printf("x:%d y:%d\n", pt.x, pt.y); | |
1542ea39 RD |
221 | if (evt.Dragging() && evt.LeftIsDown()) |
222 | { | |
6a7e6411 | 223 | wxPoint pos = ClientToScreen(pt); |
1542ea39 RD |
224 | Move(wxPoint(pos.x - m_delta.x, pos.y - m_delta.y)); |
225 | } | |
226 | } | |
227 | ||
256b8649 | 228 | void ShapedFrame::OnExit(wxMouseEvent& WXUNUSED(evt)) |
1542ea39 RD |
229 | { |
230 | Close(); | |
231 | } | |
232 | ||
256b8649 | 233 | void ShapedFrame::OnPaint(wxPaintEvent& WXUNUSED(evt)) |
1542ea39 RD |
234 | { |
235 | wxPaintDC dc(this); | |
b62ca03d | 236 | dc.DrawBitmap(m_bmp, 0, 0, true); |
1542ea39 RD |
237 | } |
238 | ||
256b8649 | 239 | void ShapedFrame::OnWindowCreate(wxWindowCreateEvent& WXUNUSED(evt)) |
1542ea39 RD |
240 | { |
241 | SetWindowShape(); | |
242 | } | |
243 |