]>
git.saurik.com Git - wxWidgets.git/blob - samples/shaped/shaped.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Shaped Window sample
6 // Created: 28-Mar-2003
8 // Copyright: (c) Robin Dunn
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers
33 #include "wx/stattext.h"
35 #include "wx/layout.h"
36 #include "wx/msgdlg.h"
40 #include "wx/dcclient.h"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 // Define a new application type, each program should derive a class from wxApp
48 class MyApp
: public wxApp
51 // override base class virtuals
52 // ----------------------------
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();
61 // Define a new frame type: this is going to be our main frame
62 class ShapedFrame
: public wxFrame
67 void SetWindowShape();
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
);
83 // any class wishing to process wxWidgets events must use this macro
88 // ----------------------------------------------------------------------------
89 // event tables and other macros for wxWidgets
90 // ----------------------------------------------------------------------------
92 // the event tables connect the wxWidgets events with the functions (event
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.
95 BEGIN_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
)
102 EVT_PAINT(ShapedFrame::OnPaint
)
105 EVT_WINDOW_CREATE(ShapedFrame::OnWindowCreate
)
110 // Create a new application object: this macro will allow wxWidgets to create
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
117 // ============================================================================
119 // ============================================================================
121 // ----------------------------------------------------------------------------
122 // the application class
123 // ----------------------------------------------------------------------------
125 // `Main program' equivalent: the program execution "starts" here
128 if ( !wxApp::OnInit() )
131 wxInitAllImageHandlers();
133 // Create the main application window
134 ShapedFrame
*frame
= new ShapedFrame();
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.
144 // ----------------------------------------------------------------------------
146 // ----------------------------------------------------------------------------
149 ShapedFrame::ShapedFrame()
150 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, wxEmptyString
,
151 wxDefaultPosition
, wxSize(100, 100), //wxDefaultSize,
160 m_bmp
= wxBitmap(_T("star.png"), wxBITMAP_TYPE_PNG
);
161 SetSize(wxSize(m_bmp
.GetWidth(), m_bmp
.GetHeight()));
162 SetToolTip(wxT("Right-click to exit"));
165 // On wxGTK we can't do this yet because the window hasn't been created
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
173 void ShapedFrame::SetWindowShape()
175 wxRegion
region(m_bmp
, *wxWHITE
);
176 m_hasShape
= SetShape(region
);
179 void ShapedFrame::OnDoubleClick(wxMouseEvent
& WXUNUSED(evt
))
191 void ShapedFrame::OnLeftDown(wxMouseEvent
& evt
)
194 //printf("Mouse captured\n");
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
);
202 void ShapedFrame::OnLeftUp(wxMouseEvent
& WXUNUSED(evt
))
207 //printf("Mouse released\n");
211 void ShapedFrame::OnMouseMove(wxMouseEvent
& evt
)
213 wxPoint pt
= evt
.GetPosition();
214 //printf("x:%d y:%d\n", pt.x, pt.y);
215 if (evt
.Dragging() && evt
.LeftIsDown())
217 wxPoint pos
= ClientToScreen(pt
);
218 Move(wxPoint(pos
.x
- m_delta
.x
, pos
.y
- m_delta
.y
));
222 void ShapedFrame::OnExit(wxMouseEvent
& WXUNUSED(evt
))
227 void ShapedFrame::OnPaint(wxPaintEvent
& WXUNUSED(evt
))
230 dc
.DrawBitmap(m_bmp
, 0, 0, true);
233 void ShapedFrame::OnWindowCreate(wxWindowCreateEvent
& WXUNUSED(evt
))