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