2003-03-28 18:05:05 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: shaped.cpp
|
|
|
|
// Purpose: Shaped Window sample
|
|
|
|
// Author: Robin Dunn
|
|
|
|
// Modified by:
|
|
|
|
// Created: 28-Mar-2003
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) Robin Dunn
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// declarations
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// For compilers that support precompilation, includes "wx/wx.h".
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// for all others, include the necessary headers
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/app.h"
|
|
|
|
#include "wx/log.h"
|
|
|
|
#include "wx/frame.h"
|
|
|
|
#include "wx/panel.h"
|
|
|
|
#include "wx/stattext.h"
|
|
|
|
#include "wx/menu.h"
|
|
|
|
#include "wx/layout.h"
|
|
|
|
#include "wx/msgdlg.h"
|
2003-04-29 20:39:15 -04:00
|
|
|
#include "wx/image.h"
|
2003-03-28 18:05:05 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "wx/dcclient.h"
|
2003-03-28 18:56:19 -05:00
|
|
|
#include "wx/image.h"
|
2003-03-28 18:05:05 -05:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// private classes
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Define a new application type, each program should derive a class from wxApp
|
|
|
|
class MyApp : public wxApp
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// override base class virtuals
|
|
|
|
// ----------------------------
|
|
|
|
|
|
|
|
// this one is called on application startup and is a good place for the app
|
|
|
|
// initialization (doing it here and not in the ctor allows to have an error
|
|
|
|
// return: if OnInit() returns false, the application terminates)
|
|
|
|
virtual bool OnInit();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Define a new frame type: this is going to be our main frame
|
|
|
|
class ShapedFrame : public wxFrame
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// ctor(s)
|
|
|
|
ShapedFrame();
|
|
|
|
void SetWindowShape();
|
|
|
|
|
|
|
|
// event handlers (these functions should _not_ be virtual)
|
|
|
|
void OnDoubleClick(wxMouseEvent& evt);
|
|
|
|
void OnLeftDown(wxMouseEvent& evt);
|
|
|
|
void OnLeftUp(wxMouseEvent& evt);
|
|
|
|
void OnMouseMove(wxMouseEvent& evt);
|
|
|
|
void OnExit(wxMouseEvent& evt);
|
|
|
|
void OnPaint(wxPaintEvent& evt);
|
|
|
|
void OnWindowCreate(wxWindowCreateEvent& evt);
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool m_hasShape;
|
|
|
|
wxBitmap m_bmp;
|
|
|
|
wxPoint m_delta;
|
|
|
|
|
2004-05-25 07:20:37 -04:00
|
|
|
// any class wishing to process wxWidgets events must use this macro
|
2003-03-28 18:05:05 -05:00
|
|
|
DECLARE_EVENT_TABLE()
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2004-05-25 07:20:37 -04:00
|
|
|
// event tables and other macros for wxWidgets
|
2003-03-28 18:05:05 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2004-05-25 07:20:37 -04:00
|
|
|
// the event tables connect the wxWidgets events with the functions (event
|
2003-03-28 18:05:05 -05:00
|
|
|
// handlers) which process them. It can be also done at run-time, but for the
|
|
|
|
// simple menu events like this the static method is much simpler.
|
|
|
|
BEGIN_EVENT_TABLE(ShapedFrame, wxFrame)
|
|
|
|
EVT_LEFT_DCLICK(ShapedFrame::OnDoubleClick)
|
|
|
|
EVT_LEFT_DOWN(ShapedFrame::OnLeftDown)
|
|
|
|
EVT_LEFT_UP(ShapedFrame::OnLeftUp)
|
|
|
|
EVT_MOTION(ShapedFrame::OnMouseMove)
|
|
|
|
EVT_RIGHT_UP(ShapedFrame::OnExit)
|
|
|
|
|
|
|
|
EVT_PAINT(ShapedFrame::OnPaint)
|
|
|
|
|
|
|
|
#ifdef __WXGTK__
|
|
|
|
EVT_WINDOW_CREATE(ShapedFrame::OnWindowCreate)
|
|
|
|
#endif
|
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
|
|
|
|
2004-05-25 07:20:37 -04:00
|
|
|
// Create a new application object: this macro will allow wxWidgets to create
|
2003-03-28 18:05:05 -05:00
|
|
|
// the application object during program execution (it's better than using a
|
|
|
|
// static object for many reasons) and also declares the accessor function
|
|
|
|
// wxGetApp() which will return the reference of the right type (i.e. MyApp and
|
|
|
|
// not wxApp)
|
|
|
|
IMPLEMENT_APP(MyApp)
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// implementation
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// the application class
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// `Main program' equivalent: the program execution "starts" here
|
|
|
|
bool MyApp::OnInit()
|
|
|
|
{
|
2007-02-03 19:34:18 -05:00
|
|
|
if ( !wxApp::OnInit() )
|
|
|
|
return false;
|
|
|
|
|
2003-03-28 18:05:05 -05:00
|
|
|
wxInitAllImageHandlers();
|
|
|
|
|
|
|
|
// Create the main application window
|
|
|
|
ShapedFrame *frame = new ShapedFrame();
|
2004-06-02 13:03:20 -04:00
|
|
|
frame->Show(true);
|
2003-04-29 20:39:15 -04:00
|
|
|
SetTopWindow(frame);
|
2003-03-28 18:05:05 -05:00
|
|
|
|
|
|
|
// success: wxApp::OnRun() will be called which will enter the main message
|
2004-06-02 13:03:20 -04:00
|
|
|
// loop and the application will run. If we returned false here, the
|
2003-03-28 18:05:05 -05:00
|
|
|
// application would exit immediately.
|
2004-06-02 13:03:20 -04:00
|
|
|
return true;
|
2003-03-28 18:05:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// main frame
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// frame constructor
|
|
|
|
ShapedFrame::ShapedFrame()
|
2004-06-02 13:03:20 -04:00
|
|
|
: wxFrame((wxFrame *)NULL, wxID_ANY, wxEmptyString,
|
2003-04-29 20:39:15 -04:00
|
|
|
wxDefaultPosition, wxSize(100, 100), //wxDefaultSize,
|
|
|
|
0
|
|
|
|
| wxFRAME_SHAPED
|
|
|
|
| wxSIMPLE_BORDER
|
|
|
|
| wxFRAME_NO_TASKBAR
|
|
|
|
| wxSTAY_ON_TOP
|
|
|
|
)
|
2003-03-28 18:05:05 -05:00
|
|
|
{
|
2004-06-02 13:03:20 -04:00
|
|
|
m_hasShape = false;
|
2003-07-23 15:04:42 -04:00
|
|
|
m_bmp = wxBitmap(_T("star.png"), wxBITMAP_TYPE_PNG);
|
2003-03-28 18:05:05 -05:00
|
|
|
SetSize(wxSize(m_bmp.GetWidth(), m_bmp.GetHeight()));
|
|
|
|
SetToolTip(wxT("Right-click to exit"));
|
2005-07-20 06:23:05 -04:00
|
|
|
|
2003-04-29 20:39:15 -04:00
|
|
|
#ifndef __WXGTK__
|
2003-03-28 18:05:05 -05:00
|
|
|
// On wxGTK we can't do this yet because the window hasn't been created
|
2003-04-29 20:39:15 -04:00
|
|
|
// yet so we wait until the EVT_WINDOW_CREATE event happens. On wxMSW and
|
|
|
|
// wxMac the window has been created at this point so we go ahead and set
|
|
|
|
// the shape now.
|
2003-03-28 18:05:05 -05:00
|
|
|
SetWindowShape();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShapedFrame::SetWindowShape()
|
|
|
|
{
|
|
|
|
wxRegion region(m_bmp, *wxWHITE);
|
|
|
|
m_hasShape = SetShape(region);
|
|
|
|
}
|
|
|
|
|
2003-09-11 08:52:20 -04:00
|
|
|
void ShapedFrame::OnDoubleClick(wxMouseEvent& WXUNUSED(evt))
|
2003-03-28 18:05:05 -05:00
|
|
|
{
|
|
|
|
if (m_hasShape)
|
|
|
|
{
|
|
|
|
wxRegion region;
|
|
|
|
SetShape(region);
|
2004-06-02 13:03:20 -04:00
|
|
|
m_hasShape = false;
|
2003-03-28 18:05:05 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
SetWindowShape();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShapedFrame::OnLeftDown(wxMouseEvent& evt)
|
|
|
|
{
|
|
|
|
CaptureMouse();
|
2003-04-29 20:39:15 -04:00
|
|
|
//printf("Mouse captured\n");
|
2003-03-28 18:05:05 -05:00
|
|
|
wxPoint pos = ClientToScreen(evt.GetPosition());
|
|
|
|
wxPoint origin = GetPosition();
|
|
|
|
int dx = pos.x - origin.x;
|
|
|
|
int dy = pos.y - origin.y;
|
|
|
|
m_delta = wxPoint(dx, dy);
|
|
|
|
}
|
|
|
|
|
2003-09-11 08:52:20 -04:00
|
|
|
void ShapedFrame::OnLeftUp(wxMouseEvent& WXUNUSED(evt))
|
2003-03-28 18:05:05 -05:00
|
|
|
{
|
|
|
|
if (HasCapture())
|
2003-04-29 20:39:15 -04:00
|
|
|
{
|
2003-03-28 18:05:05 -05:00
|
|
|
ReleaseMouse();
|
2003-04-29 20:39:15 -04:00
|
|
|
//printf("Mouse released\n");
|
2003-09-11 08:52:20 -04:00
|
|
|
}
|
2003-03-28 18:05:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void ShapedFrame::OnMouseMove(wxMouseEvent& evt)
|
|
|
|
{
|
2003-04-29 20:39:15 -04:00
|
|
|
wxPoint pt = evt.GetPosition();
|
|
|
|
//printf("x:%d y:%d\n", pt.x, pt.y);
|
2003-03-28 18:05:05 -05:00
|
|
|
if (evt.Dragging() && evt.LeftIsDown())
|
|
|
|
{
|
2003-04-29 20:39:15 -04:00
|
|
|
wxPoint pos = ClientToScreen(pt);
|
2003-03-28 18:05:05 -05:00
|
|
|
Move(wxPoint(pos.x - m_delta.x, pos.y - m_delta.y));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-11 08:52:20 -04:00
|
|
|
void ShapedFrame::OnExit(wxMouseEvent& WXUNUSED(evt))
|
2003-03-28 18:05:05 -05:00
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
2003-09-11 08:52:20 -04:00
|
|
|
void ShapedFrame::OnPaint(wxPaintEvent& WXUNUSED(evt))
|
2003-03-28 18:05:05 -05:00
|
|
|
{
|
|
|
|
wxPaintDC dc(this);
|
2004-06-02 13:03:20 -04:00
|
|
|
dc.DrawBitmap(m_bmp, 0, 0, true);
|
2003-03-28 18:05:05 -05:00
|
|
|
}
|
|
|
|
|
2003-09-11 08:52:20 -04:00
|
|
|
void ShapedFrame::OnWindowCreate(wxWindowCreateEvent& WXUNUSED(evt))
|
2003-03-28 18:05:05 -05:00
|
|
|
{
|
|
|
|
SetWindowShape();
|
|
|
|
}
|
|
|
|
|