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