added wxBG_STYLE_TRANSPARENT and implemented it for wxMac; documented various transparency-related functions

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@49341 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2007-10-22 21:15:04 +00:00
parent 5a2b31a0d7
commit 4488a1d332
7 changed files with 250 additions and 28 deletions

View File

@@ -58,12 +58,13 @@ public:
};
// Define a new frame type: this is going to be our main frame
// Define a new frame type: this is going to the frame showing the
// effect of wxFRAME_SHAPED
class ShapedFrame : public wxFrame
{
public:
// ctor(s)
ShapedFrame();
ShapedFrame(wxFrame *parent);
void SetWindowShape();
// event handlers (these functions should _not_ be virtual)
@@ -84,6 +85,35 @@ private:
DECLARE_EVENT_TABLE()
};
// Define a new frame type: this is going to the frame showing the
// effect of wxWindow::SetTransparent and of
// wxWindow::SetBackgroundStyle(wxBG_STYLE_TRANSPARENT)
class SeeThroughFrame : public wxFrame
{
public:
// ctor(s)
SeeThroughFrame();
// event handlers (these functions should _not_ be virtual)
void OnDoubleClick(wxMouseEvent& evt);
void OnPaint(wxPaintEvent& evt);
void OnSize(wxSizeEvent& evt);
private:
enum State
{
STATE_SEETHROUGH,
STATE_TRANSPARENT,
STATE_OPAQUE,
STATE_MAX
};
State m_currentState;
// any class wishing to process wxWidgets events must use this macro
DECLARE_EVENT_TABLE()
};
// ----------------------------------------------------------------------------
// event tables and other macros for wxWidgets
@@ -130,10 +160,14 @@ bool MyApp::OnInit()
wxInitAllImageHandlers();
// Create the main application window
ShapedFrame *frame = new ShapedFrame();
frame->Show(true);
SetTopWindow(frame);
// Create the transparent window
SeeThroughFrame *seeThroughFrame = new SeeThroughFrame();
seeThroughFrame->Show(true);
SetTopWindow(seeThroughFrame);
// Create the shaped window
ShapedFrame *shapedFrame = new ShapedFrame(seeThroughFrame);
shapedFrame->Show(true);
// success: wxApp::OnRun() will be called which will enter the main message
// loop and the application will run. If we returned false here, the
@@ -142,12 +176,12 @@ bool MyApp::OnInit()
}
// ----------------------------------------------------------------------------
// main frame
// shaped frame
// ----------------------------------------------------------------------------
// frame constructor
ShapedFrame::ShapedFrame()
: wxFrame((wxFrame *)NULL, wxID_ANY, wxEmptyString,
ShapedFrame::ShapedFrame(wxFrame *parent)
: wxFrame(parent, wxID_ANY, wxEmptyString,
wxDefaultPosition, wxSize(100, 100), //wxDefaultSize,
0
| wxFRAME_SHAPED
@@ -235,3 +269,93 @@ void ShapedFrame::OnWindowCreate(wxWindowCreateEvent& WXUNUSED(evt))
SetWindowShape();
}
// ----------------------------------------------------------------------------
// see-through frame
// ----------------------------------------------------------------------------
// frame constructor
SeeThroughFrame::SeeThroughFrame()
: wxFrame(NULL, wxID_ANY, "Transparency test: double click here",
wxPoint(100, 30), wxSize(300, 300),
wxDEFAULT_FRAME_STYLE | wxSTAY_ON_TOP),
m_currentState(STATE_SEETHROUGH)
{
SetBackgroundColour(wxColour(255, 255, 255, 255));
SetBackgroundStyle(wxBG_STYLE_TRANSPARENT);
}
// Redraws the whole window on resize
void SeeThroughFrame::OnSize(wxSizeEvent& WXUNUSED(evt))
{
Refresh();
}
// Paints a grid of varying hue and alpha
void SeeThroughFrame::OnPaint(wxPaintEvent& WXUNUSED(evt))
{
wxPaintDC dc(this);
dc.SetPen(wxNullPen);
int xcount = 8;
int ycount = 8;
float xstep = 1. / xcount;
float ystep = 1. / ycount;
int width = GetClientSize().GetWidth();
int height = GetClientSize().GetHeight();
for ( float x = 0.; x < 1.; x += xstep )
{
for ( float y = 0.; y < 1.; y += ystep )
{
wxImage::RGBValue v = wxImage::HSVtoRGB(wxImage::HSVValue(x, 1., 1.));
dc.SetBrush(wxBrush(wxColour(v.red, v.green, v.blue,
(int)(255*(1. - y)))));
int x1 = (int)(x * width);
int y1 = (int)(y * height);
int x2 = (int)((x + xstep) * width);
int y2 = (int)((y + ystep) * height);
dc.DrawRectangle(x1, y1, x2 - x1, y2 - y1);
}
}
}
// Switches between colour and transparent background on doubleclick
void SeeThroughFrame::OnDoubleClick(wxMouseEvent& WXUNUSED(evt))
{
m_currentState = (State)((m_currentState + 1) % STATE_MAX);
switch ( m_currentState )
{
case STATE_OPAQUE:
SetBackgroundStyle(wxBG_STYLE_COLOUR);
SetTransparent(255);
SetTitle("Opaque");
break;
case STATE_SEETHROUGH:
SetBackgroundStyle(wxBG_STYLE_TRANSPARENT);
SetTransparent(255);
SetTitle("See through");
break;
case STATE_TRANSPARENT:
SetBackgroundStyle(wxBG_STYLE_COLOUR);
SetTransparent(128);
SetTitle("Semi-transparent");
break;
case STATE_MAX:
wxFAIL_MSG( "unreachable" );
}
Refresh();
}
BEGIN_EVENT_TABLE(SeeThroughFrame, wxFrame)
EVT_LEFT_DCLICK(SeeThroughFrame::OnDoubleClick)
EVT_PAINT(SeeThroughFrame::OnPaint)
EVT_SIZE(SeeThroughFrame::OnSize)
END_EVENT_TABLE()