]> git.saurik.com Git - wxWidgets.git/blame - samples/shaped/shaped.cpp
Made choice convenient dialog more, well, convenient: fixed lack of vertical
[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
1542ea39
RD
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"
6a7e6411 37 #include "wx/image.h"
1542ea39
RD
38#endif
39
40#include "wx/dcclient.h"
3b3ca9be 41#include "wx/image.h"
1542ea39
RD
42
43// ----------------------------------------------------------------------------
44// private classes
45// ----------------------------------------------------------------------------
46
47// Define a new application type, each program should derive a class from wxApp
48class MyApp : public wxApp
49{
50public:
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
62class ShapedFrame : public wxFrame
63{
64public:
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
78private:
79 bool m_hasShape;
80 wxBitmap m_bmp;
81 wxPoint m_delta;
82
be5a51fb 83 // any class wishing to process wxWidgets events must use this macro
1542ea39
RD
84 DECLARE_EVENT_TABLE()
85};
86
87
88// ----------------------------------------------------------------------------
be5a51fb 89// event tables and other macros for wxWidgets
1542ea39
RD
90// ----------------------------------------------------------------------------
91
be5a51fb 92// the event tables connect the wxWidgets events with the functions (event
1542ea39
RD
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.
95BEGIN_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
107END_EVENT_TABLE()
108
109
be5a51fb 110// Create a new application object: this macro will allow wxWidgets to create
1542ea39
RD
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)
115IMPLEMENT_APP(MyApp)
116
117// ============================================================================
118// implementation
119// ============================================================================
120
121// ----------------------------------------------------------------------------
122// the application class
123// ----------------------------------------------------------------------------
124
125// `Main program' equivalent: the program execution "starts" here
126bool MyApp::OnInit()
127{
45e6e6f8
VZ
128 if ( !wxApp::OnInit() )
129 return false;
130
1542ea39
RD
131 wxInitAllImageHandlers();
132
133 // Create the main application window
134 ShapedFrame *frame = new ShapedFrame();
b62ca03d 135 frame->Show(true);
6a7e6411 136 SetTopWindow(frame);
1542ea39
RD
137
138 // success: wxApp::OnRun() will be called which will enter the main message
b62ca03d 139 // loop and the application will run. If we returned false here, the
1542ea39 140 // application would exit immediately.
b62ca03d 141 return true;
1542ea39
RD
142}
143
144// ----------------------------------------------------------------------------
145// main frame
146// ----------------------------------------------------------------------------
147
148// frame constructor
149ShapedFrame::ShapedFrame()
b62ca03d 150 : wxFrame((wxFrame *)NULL, wxID_ANY, wxEmptyString,
6a7e6411
RD
151 wxDefaultPosition, wxSize(100, 100), //wxDefaultSize,
152 0
153 | wxFRAME_SHAPED
154 | wxSIMPLE_BORDER
155 | wxFRAME_NO_TASKBAR
156 | wxSTAY_ON_TOP
157 )
1542ea39 158{
b62ca03d 159 m_hasShape = false;
7df07b10 160 m_bmp = wxBitmap(_T("star.png"), wxBITMAP_TYPE_PNG);
1542ea39
RD
161 SetSize(wxSize(m_bmp.GetWidth(), m_bmp.GetHeight()));
162 SetToolTip(wxT("Right-click to exit"));
94a5c401 163
6a7e6411 164#ifndef __WXGTK__
1542ea39 165 // On wxGTK we can't do this yet because the window hasn't been created
6a7e6411
RD
166 // yet so we wait until the EVT_WINDOW_CREATE event happens. On wxMSW and
167 // wxMac the window has been created at this point so we go ahead and set
168 // the shape now.
1542ea39
RD
169 SetWindowShape();
170#endif
171}
172
173void ShapedFrame::SetWindowShape()
174{
175 wxRegion region(m_bmp, *wxWHITE);
176 m_hasShape = SetShape(region);
177}
178
256b8649 179void ShapedFrame::OnDoubleClick(wxMouseEvent& WXUNUSED(evt))
1542ea39
RD
180{
181 if (m_hasShape)
182 {
183 wxRegion region;
184 SetShape(region);
b62ca03d 185 m_hasShape = false;
1542ea39
RD
186 }
187 else
188 SetWindowShape();
189}
190
191void ShapedFrame::OnLeftDown(wxMouseEvent& evt)
192{
193 CaptureMouse();
6a7e6411 194 //printf("Mouse captured\n");
1542ea39
RD
195 wxPoint pos = ClientToScreen(evt.GetPosition());
196 wxPoint origin = GetPosition();
197 int dx = pos.x - origin.x;
198 int dy = pos.y - origin.y;
199 m_delta = wxPoint(dx, dy);
200}
201
256b8649 202void ShapedFrame::OnLeftUp(wxMouseEvent& WXUNUSED(evt))
1542ea39
RD
203{
204 if (HasCapture())
6a7e6411 205 {
1542ea39 206 ReleaseMouse();
6a7e6411 207 //printf("Mouse released\n");
256b8649 208 }
1542ea39
RD
209}
210
211void ShapedFrame::OnMouseMove(wxMouseEvent& evt)
212{
6a7e6411
RD
213 wxPoint pt = evt.GetPosition();
214 //printf("x:%d y:%d\n", pt.x, pt.y);
1542ea39
RD
215 if (evt.Dragging() && evt.LeftIsDown())
216 {
6a7e6411 217 wxPoint pos = ClientToScreen(pt);
1542ea39
RD
218 Move(wxPoint(pos.x - m_delta.x, pos.y - m_delta.y));
219 }
220}
221
256b8649 222void ShapedFrame::OnExit(wxMouseEvent& WXUNUSED(evt))
1542ea39
RD
223{
224 Close();
225}
226
256b8649 227void ShapedFrame::OnPaint(wxPaintEvent& WXUNUSED(evt))
1542ea39
RD
228{
229 wxPaintDC dc(this);
b62ca03d 230 dc.DrawBitmap(m_bmp, 0, 0, true);
1542ea39
RD
231}
232
256b8649 233void ShapedFrame::OnWindowCreate(wxWindowCreateEvent& WXUNUSED(evt))
1542ea39
RD
234{
235 SetWindowShape();
236}
237