]>
git.saurik.com Git - wxWidgets.git/blob - samples/shaped/shaped.cpp
c43c71ff4552e070ccae9415195e039b8d1cb26e
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 #if defined(__GNUG__) && !defined(__APPLE__)
21 #pragma implementation "shaped.cpp"
22 #pragma interface "shaped.cpp"
25 // For compilers that support precompilation, includes "wx/wx.h".
26 #include "wx/wxprec.h"
32 // for all others, include the necessary headers
38 #include "wx/stattext.h"
40 #include "wx/layout.h"
41 #include "wx/msgdlg.h"
44 #include "wx/dcclient.h"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 // Define a new application type, each program should derive a class from wxApp
51 class MyApp
: public wxApp
54 // override base class virtuals
55 // ----------------------------
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();
64 // Define a new frame type: this is going to be our main frame
65 class ShapedFrame
: public wxFrame
70 void SetWindowShape();
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
);
86 // any class wishing to process wxWindows events must use this macro
91 // ----------------------------------------------------------------------------
92 // event tables and other macros for wxWindows
93 // ----------------------------------------------------------------------------
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.
98 BEGIN_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
)
105 EVT_PAINT(ShapedFrame::OnPaint
)
108 EVT_WINDOW_CREATE(ShapedFrame::OnWindowCreate
)
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
120 // ============================================================================
122 // ============================================================================
124 // ----------------------------------------------------------------------------
125 // the application class
126 // ----------------------------------------------------------------------------
128 // `Main program' equivalent: the program execution "starts" here
131 wxInitAllImageHandlers();
133 // Create the main application window
134 ShapedFrame
*frame
= new ShapedFrame();
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.
143 // ----------------------------------------------------------------------------
145 // ----------------------------------------------------------------------------
148 ShapedFrame::ShapedFrame()
149 : wxFrame((wxFrame
*)NULL
, -1, wxEmptyString
,
150 wxDefaultPosition
, wxDefaultSize
,
151 wxSIMPLE_BORDER
| wxFRAME_NO_TASKBAR
)
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"));
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.
165 void ShapedFrame::SetWindowShape()
167 wxRegion
region(m_bmp
, *wxWHITE
);
168 m_hasShape
= SetShape(region
);
171 void ShapedFrame::OnDoubleClick(wxMouseEvent
& evt
)
183 void ShapedFrame::OnLeftDown(wxMouseEvent
& evt
)
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
);
193 void ShapedFrame::OnLeftUp(wxMouseEvent
& evt
)
199 void ShapedFrame::OnMouseMove(wxMouseEvent
& evt
)
201 if (evt
.Dragging() && evt
.LeftIsDown())
203 wxPoint pos
= ClientToScreen(evt
.GetPosition());
204 Move(wxPoint(pos
.x
- m_delta
.x
, pos
.y
- m_delta
.y
));
208 void ShapedFrame::OnExit(wxMouseEvent
& evt
)
213 void ShapedFrame::OnPaint(wxPaintEvent
& evt
)
216 dc
.DrawBitmap(m_bmp
, 0, 0, TRUE
);
219 void ShapedFrame::OnWindowCreate(wxWindowCreateEvent
& evt
)