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