]>
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 #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"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 // Define a new application type, each program should derive a class from wxApp
52 class MyApp
: public wxApp
55 // override base class virtuals
56 // ----------------------------
58 // this one is called on application startup and is a good place for the app
59 // initialization (doing it here and not in the ctor allows to have an error
60 // return: if OnInit() returns false, the application terminates)
61 virtual bool OnInit();
65 // Define a new frame type: this is going to be our main frame
66 class ShapedFrame
: public wxFrame
71 void SetWindowShape();
73 // event handlers (these functions should _not_ be virtual)
74 void OnDoubleClick(wxMouseEvent
& evt
);
75 void OnLeftDown(wxMouseEvent
& evt
);
76 void OnLeftUp(wxMouseEvent
& evt
);
77 void OnMouseMove(wxMouseEvent
& evt
);
78 void OnExit(wxMouseEvent
& evt
);
79 void OnPaint(wxPaintEvent
& evt
);
80 void OnWindowCreate(wxWindowCreateEvent
& evt
);
87 // any class wishing to process wxWindows events must use this macro
92 // ----------------------------------------------------------------------------
93 // event tables and other macros for wxWindows
94 // ----------------------------------------------------------------------------
96 // the event tables connect the wxWindows events with the functions (event
97 // handlers) which process them. It can be also done at run-time, but for the
98 // simple menu events like this the static method is much simpler.
99 BEGIN_EVENT_TABLE(ShapedFrame
, wxFrame
)
100 EVT_LEFT_DCLICK(ShapedFrame::OnDoubleClick
)
101 EVT_LEFT_DOWN(ShapedFrame::OnLeftDown
)
102 EVT_LEFT_UP(ShapedFrame::OnLeftUp
)
103 EVT_MOTION(ShapedFrame::OnMouseMove
)
104 EVT_RIGHT_UP(ShapedFrame::OnExit
)
106 EVT_PAINT(ShapedFrame::OnPaint
)
109 EVT_WINDOW_CREATE(ShapedFrame::OnWindowCreate
)
114 // Create a new application object: this macro will allow wxWindows to create
115 // the application object during program execution (it's better than using a
116 // static object for many reasons) and also declares the accessor function
117 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
121 // ============================================================================
123 // ============================================================================
125 // ----------------------------------------------------------------------------
126 // the application class
127 // ----------------------------------------------------------------------------
129 // `Main program' equivalent: the program execution "starts" here
132 wxInitAllImageHandlers();
134 // Create the main application window
135 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
, -1, wxEmptyString
,
151 wxDefaultPosition
, wxDefaultSize
,
152 wxSIMPLE_BORDER
| wxFRAME_NO_TASKBAR
)
155 m_bmp
= wxBitmap("star.png", wxBITMAP_TYPE_PNG
);
156 SetSize(wxSize(m_bmp
.GetWidth(), m_bmp
.GetHeight()));
158 SetToolTip(wxT("Right-click to exit"));
161 // On wxGTK we can't do this yet because the window hasn't been created
162 // yet so we wait until the EVT_WINDOW_CREATE event happens. On wxMSW it
163 // has been created so we set the shape now.
168 void ShapedFrame::SetWindowShape()
170 wxRegion
region(m_bmp
, *wxWHITE
);
171 m_hasShape
= SetShape(region
);
174 void ShapedFrame::OnDoubleClick(wxMouseEvent
& evt
)
186 void ShapedFrame::OnLeftDown(wxMouseEvent
& evt
)
189 wxPoint pos
= ClientToScreen(evt
.GetPosition());
190 wxPoint origin
= GetPosition();
191 int dx
= pos
.x
- origin
.x
;
192 int dy
= pos
.y
- origin
.y
;
193 m_delta
= wxPoint(dx
, dy
);
196 void ShapedFrame::OnLeftUp(wxMouseEvent
& evt
)
202 void ShapedFrame::OnMouseMove(wxMouseEvent
& evt
)
204 if (evt
.Dragging() && evt
.LeftIsDown())
206 wxPoint pos
= ClientToScreen(evt
.GetPosition());
207 Move(wxPoint(pos
.x
- m_delta
.x
, pos
.y
- m_delta
.y
));
211 void ShapedFrame::OnExit(wxMouseEvent
& evt
)
216 void ShapedFrame::OnPaint(wxPaintEvent
& evt
)
219 dc
.DrawBitmap(m_bmp
, 0, 0, TRUE
);
222 void ShapedFrame::OnWindowCreate(wxWindowCreateEvent
& evt
)