]> git.saurik.com Git - wxWidgets.git/blob - samples/shaped/shaped.cpp
corrected Get() return value
[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
22 #pragma interface
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 wxWidgets events must use this macro
89 DECLARE_EVENT_TABLE()
90 };
91
92
93 // ----------------------------------------------------------------------------
94 // event tables and other macros for wxWidgets
95 // ----------------------------------------------------------------------------
96
97 // the event tables connect the wxWidgets 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 wxWidgets 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, wxID_ANY, 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 SetToolTip(wxT("Right-click to exit"));
165
166 #ifndef __WXGTK__
167 // On wxGTK we can't do this yet because the window hasn't been created
168 // yet so we wait until the EVT_WINDOW_CREATE event happens. On wxMSW and
169 // wxMac the window has been created at this point so we go ahead and set
170 // the shape now.
171 SetWindowShape();
172 #endif
173 }
174
175 void ShapedFrame::SetWindowShape()
176 {
177 wxRegion region(m_bmp, *wxWHITE);
178 m_hasShape = SetShape(region);
179 }
180
181 void ShapedFrame::OnDoubleClick(wxMouseEvent& WXUNUSED(evt))
182 {
183 if (m_hasShape)
184 {
185 wxRegion region;
186 SetShape(region);
187 m_hasShape = false;
188 }
189 else
190 SetWindowShape();
191 }
192
193 void ShapedFrame::OnLeftDown(wxMouseEvent& evt)
194 {
195 CaptureMouse();
196 //printf("Mouse captured\n");
197 wxPoint pos = ClientToScreen(evt.GetPosition());
198 wxPoint origin = GetPosition();
199 int dx = pos.x - origin.x;
200 int dy = pos.y - origin.y;
201 m_delta = wxPoint(dx, dy);
202 }
203
204 void ShapedFrame::OnLeftUp(wxMouseEvent& WXUNUSED(evt))
205 {
206 if (HasCapture())
207 {
208 ReleaseMouse();
209 //printf("Mouse released\n");
210 }
211 }
212
213 void ShapedFrame::OnMouseMove(wxMouseEvent& evt)
214 {
215 wxPoint pt = evt.GetPosition();
216 //printf("x:%d y:%d\n", pt.x, pt.y);
217 if (evt.Dragging() && evt.LeftIsDown())
218 {
219 wxPoint pos = ClientToScreen(pt);
220 Move(wxPoint(pos.x - m_delta.x, pos.y - m_delta.y));
221 }
222 }
223
224 void ShapedFrame::OnExit(wxMouseEvent& WXUNUSED(evt))
225 {
226 Close();
227 }
228
229 void ShapedFrame::OnPaint(wxPaintEvent& WXUNUSED(evt))
230 {
231 wxPaintDC dc(this);
232 dc.DrawBitmap(m_bmp, 0, 0, true);
233 }
234
235 void ShapedFrame::OnWindowCreate(wxWindowCreateEvent& WXUNUSED(evt))
236 {
237 SetWindowShape();
238 }
239