]> git.saurik.com Git - wxWidgets.git/blob - samples/shaped/shaped.cpp
wxMotif does not have wxBitmapDataObject.
[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 SetToolTip(wxT("Right-click to exit"));
158 #ifdef __WXMSW__
159 // On wxGTK we can't do this yet because the window hasn't been created
160 // yet so we wait until the EVT_WINDOW_CREATE event happens. On wxMSW it
161 // has been created so we set the shape now.
162 SetWindowShape();
163 #endif
164 }
165
166 void ShapedFrame::SetWindowShape()
167 {
168 wxRegion region(m_bmp, *wxWHITE);
169 m_hasShape = SetShape(region);
170 }
171
172 void ShapedFrame::OnDoubleClick(wxMouseEvent& evt)
173 {
174 if (m_hasShape)
175 {
176 wxRegion region;
177 SetShape(region);
178 m_hasShape = FALSE;
179 }
180 else
181 SetWindowShape();
182 }
183
184 void ShapedFrame::OnLeftDown(wxMouseEvent& evt)
185 {
186 CaptureMouse();
187 wxPoint pos = ClientToScreen(evt.GetPosition());
188 wxPoint origin = GetPosition();
189 int dx = pos.x - origin.x;
190 int dy = pos.y - origin.y;
191 m_delta = wxPoint(dx, dy);
192 }
193
194 void ShapedFrame::OnLeftUp(wxMouseEvent& evt)
195 {
196 if (HasCapture())
197 ReleaseMouse();
198 }
199
200 void ShapedFrame::OnMouseMove(wxMouseEvent& evt)
201 {
202 if (evt.Dragging() && evt.LeftIsDown())
203 {
204 wxPoint pos = ClientToScreen(evt.GetPosition());
205 Move(wxPoint(pos.x - m_delta.x, pos.y - m_delta.y));
206 }
207 }
208
209 void ShapedFrame::OnExit(wxMouseEvent& evt)
210 {
211 Close();
212 }
213
214 void ShapedFrame::OnPaint(wxPaintEvent& evt)
215 {
216 wxPaintDC dc(this);
217 dc.DrawBitmap(m_bmp, 0, 0, TRUE);
218 }
219
220 void ShapedFrame::OnWindowCreate(wxWindowCreateEvent& evt)
221 {
222 SetWindowShape();
223 }
224