]> git.saurik.com Git - wxWidgets.git/blame - samples/shaped/shaped.cpp
condensed 2 very similar trace messages into one
[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__)
11fdee42
WS
21 #pragma implementation
22 #pragma interface
1542ea39
RD
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"
6a7e6411 42 #include "wx/image.h"
1542ea39
RD
43#endif
44
45#include "wx/dcclient.h"
3b3ca9be 46#include "wx/image.h"
1542ea39
RD
47
48// ----------------------------------------------------------------------------
49// private classes
50// ----------------------------------------------------------------------------
51
52// Define a new application type, each program should derive a class from wxApp
53class MyApp : public wxApp
54{
55public:
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
67class ShapedFrame : public wxFrame
68{
69public:
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
83private:
84 bool m_hasShape;
85 wxBitmap m_bmp;
86 wxPoint m_delta;
87
be5a51fb 88 // any class wishing to process wxWidgets events must use this macro
1542ea39
RD
89 DECLARE_EVENT_TABLE()
90};
91
92
93// ----------------------------------------------------------------------------
be5a51fb 94// event tables and other macros for wxWidgets
1542ea39
RD
95// ----------------------------------------------------------------------------
96
be5a51fb 97// the event tables connect the wxWidgets events with the functions (event
1542ea39
RD
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.
100BEGIN_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
112END_EVENT_TABLE()
113
114
be5a51fb 115// Create a new application object: this macro will allow wxWidgets to create
1542ea39
RD
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)
120IMPLEMENT_APP(MyApp)
121
122// ============================================================================
123// implementation
124// ============================================================================
125
126// ----------------------------------------------------------------------------
127// the application class
128// ----------------------------------------------------------------------------
129
130// `Main program' equivalent: the program execution "starts" here
131bool MyApp::OnInit()
132{
133 wxInitAllImageHandlers();
134
135 // Create the main application window
136 ShapedFrame *frame = new ShapedFrame();
b62ca03d 137 frame->Show(true);
6a7e6411 138 SetTopWindow(frame);
1542ea39
RD
139
140 // success: wxApp::OnRun() will be called which will enter the main message
b62ca03d 141 // loop and the application will run. If we returned false here, the
1542ea39 142 // application would exit immediately.
b62ca03d 143 return true;
1542ea39
RD
144}
145
146// ----------------------------------------------------------------------------
147// main frame
148// ----------------------------------------------------------------------------
149
150// frame constructor
151ShapedFrame::ShapedFrame()
b62ca03d 152 : wxFrame((wxFrame *)NULL, wxID_ANY, wxEmptyString,
6a7e6411
RD
153 wxDefaultPosition, wxSize(100, 100), //wxDefaultSize,
154 0
155 | wxFRAME_SHAPED
156 | wxSIMPLE_BORDER
157 | wxFRAME_NO_TASKBAR
158 | wxSTAY_ON_TOP
159 )
1542ea39 160{
b62ca03d 161 m_hasShape = false;
7df07b10 162 m_bmp = wxBitmap(_T("star.png"), wxBITMAP_TYPE_PNG);
1542ea39
RD
163 SetSize(wxSize(m_bmp.GetWidth(), m_bmp.GetHeight()));
164 SetToolTip(wxT("Right-click to exit"));
94a5c401 165
6a7e6411 166#ifndef __WXGTK__
1542ea39 167 // On wxGTK we can't do this yet because the window hasn't been created
6a7e6411
RD
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.
1542ea39
RD
171 SetWindowShape();
172#endif
173}
174
175void ShapedFrame::SetWindowShape()
176{
177 wxRegion region(m_bmp, *wxWHITE);
178 m_hasShape = SetShape(region);
179}
180
256b8649 181void ShapedFrame::OnDoubleClick(wxMouseEvent& WXUNUSED(evt))
1542ea39
RD
182{
183 if (m_hasShape)
184 {
185 wxRegion region;
186 SetShape(region);
b62ca03d 187 m_hasShape = false;
1542ea39
RD
188 }
189 else
190 SetWindowShape();
191}
192
193void ShapedFrame::OnLeftDown(wxMouseEvent& evt)
194{
195 CaptureMouse();
6a7e6411 196 //printf("Mouse captured\n");
1542ea39
RD
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
256b8649 204void ShapedFrame::OnLeftUp(wxMouseEvent& WXUNUSED(evt))
1542ea39
RD
205{
206 if (HasCapture())
6a7e6411 207 {
1542ea39 208 ReleaseMouse();
6a7e6411 209 //printf("Mouse released\n");
256b8649 210 }
1542ea39
RD
211}
212
213void ShapedFrame::OnMouseMove(wxMouseEvent& evt)
214{
6a7e6411
RD
215 wxPoint pt = evt.GetPosition();
216 //printf("x:%d y:%d\n", pt.x, pt.y);
1542ea39
RD
217 if (evt.Dragging() && evt.LeftIsDown())
218 {
6a7e6411 219 wxPoint pos = ClientToScreen(pt);
1542ea39
RD
220 Move(wxPoint(pos.x - m_delta.x, pos.y - m_delta.y));
221 }
222}
223
256b8649 224void ShapedFrame::OnExit(wxMouseEvent& WXUNUSED(evt))
1542ea39
RD
225{
226 Close();
227}
228
256b8649 229void ShapedFrame::OnPaint(wxPaintEvent& WXUNUSED(evt))
1542ea39
RD
230{
231 wxPaintDC dc(this);
b62ca03d 232 dc.DrawBitmap(m_bmp, 0, 0, true);
1542ea39
RD
233}
234
256b8649 235void ShapedFrame::OnWindowCreate(wxWindowCreateEvent& WXUNUSED(evt))
1542ea39
RD
236{
237 SetWindowShape();
238}
239