]>
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"
45 #include "wx/dcclient.h"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 // Define a new application type, each program should derive a class from wxApp
53 class MyApp
: public wxApp
56 // override base class virtuals
57 // ----------------------------
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();
66 // Define a new frame type: this is going to be our main frame
67 class ShapedFrame
: public wxFrame
72 void SetWindowShape();
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
);
88 // any class wishing to process wxWindows events must use this macro
93 // ----------------------------------------------------------------------------
94 // event tables and other macros for wxWindows
95 // ----------------------------------------------------------------------------
97 // the event tables connect the wxWindows events with the functions (event
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.
100 BEGIN_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
)
107 EVT_PAINT(ShapedFrame::OnPaint
)
110 EVT_WINDOW_CREATE(ShapedFrame::OnWindowCreate
)
115 // Create a new application object: this macro will allow wxWindows to create
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
122 // ============================================================================
124 // ============================================================================
126 // ----------------------------------------------------------------------------
127 // the application class
128 // ----------------------------------------------------------------------------
130 // `Main program' equivalent: the program execution "starts" here
133 wxInitAllImageHandlers();
135 // Create the main application window
136 ShapedFrame
*frame
= new ShapedFrame();
140 // success: wxApp::OnRun() will be called which will enter the main message
141 // loop and the application will run. If we returned FALSE here, the
142 // application would exit immediately.
146 // ----------------------------------------------------------------------------
148 // ----------------------------------------------------------------------------
151 ShapedFrame::ShapedFrame()
152 : wxFrame((wxFrame
*)NULL
, -1, wxEmptyString
,
153 wxDefaultPosition
, wxSize(100, 100), //wxDefaultSize,
162 m_bmp
= wxBitmap("star.png", wxBITMAP_TYPE_PNG
);
163 SetSize(wxSize(m_bmp
.GetWidth(), m_bmp
.GetHeight()));
165 // On wxMac the tooltip gets clipped by the window shape, YUCK!!
167 SetToolTip(wxT("Right-click to exit"));
171 // On wxGTK we can't do this yet because the window hasn't been created
172 // yet so we wait until the EVT_WINDOW_CREATE event happens. On wxMSW and
173 // wxMac the window has been created at this point so we go ahead and set
179 void ShapedFrame::SetWindowShape()
181 wxRegion
region(m_bmp
, *wxWHITE
);
182 m_hasShape
= SetShape(region
);
185 void ShapedFrame::OnDoubleClick(wxMouseEvent
& evt
)
197 void ShapedFrame::OnLeftDown(wxMouseEvent
& evt
)
200 //printf("Mouse captured\n");
201 wxPoint pos
= ClientToScreen(evt
.GetPosition());
202 wxPoint origin
= GetPosition();
203 int dx
= pos
.x
- origin
.x
;
204 int dy
= pos
.y
- origin
.y
;
205 m_delta
= wxPoint(dx
, dy
);
208 void ShapedFrame::OnLeftUp(wxMouseEvent
& evt
)
213 //printf("Mouse released\n");
217 void ShapedFrame::OnMouseMove(wxMouseEvent
& evt
)
219 wxPoint pt
= evt
.GetPosition();
220 //printf("x:%d y:%d\n", pt.x, pt.y);
221 if (evt
.Dragging() && evt
.LeftIsDown())
223 wxPoint pos
= ClientToScreen(pt
);
224 Move(wxPoint(pos
.x
- m_delta
.x
, pos
.y
- m_delta
.y
));
228 void ShapedFrame::OnExit(wxMouseEvent
& evt
)
233 void ShapedFrame::OnPaint(wxPaintEvent
& evt
)
236 dc
.DrawBitmap(m_bmp
, 0, 0, TRUE
);
239 void ShapedFrame::OnWindowCreate(wxWindowCreateEvent
& evt
)