1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
/**********************************************************************
Audacity: A Digital Audio Editor
AButton.cpp
Dominic Mazzoni
This is a custom button class for Audacity. The main feature it
supports that a wxButton does not is mouseovers. It uses an image
for all of its states: up, over, down, and disabled, allowing any
sort of customization you want. Currently it does not support
transparency effects, so the images must be rectangular and
opaque.
**********************************************************************/
#include <wx/dcclient.h>
#include <wx/dcmemory.h>
#include <wx/image.h>
#include "AButton.h"
BEGIN_EVENT_TABLE(AButton, wxWindow)
EVT_MOUSE_EVENTS(AButton::OnMouseEvent)
EVT_PAINT(AButton::OnPaint)
END_EVENT_TABLE()
AButton::AButton(wxWindow * parent, wxWindowID id,
const wxPoint & pos,
const wxSize & size,
char **upXPM,
char **overXPM,
char **downXPM,
char **disXPM):wxWindow(parent, id, pos, size)
{
mButtonIsDown = false;
mButtonState = AButtonUp;
mIsClicking = false;
mEnabled = true;
mBitmap[0] = new wxBitmap((const char **) upXPM);
mBitmap[1] = new wxBitmap((const char **) overXPM);
mBitmap[2] = new wxBitmap((const char **) downXPM);
mBitmap[3] = new wxBitmap((const char **) disXPM);
GetSize(&mWidth, &mHeight);
}
AButton::~AButton()
{
delete mBitmap[0];
delete mBitmap[1];
delete mBitmap[2];
delete mBitmap[3];
}
void AButton::OnPaint(wxPaintEvent & event)
{
wxPaintDC dc(this);
wxMemoryDC memDC;
memDC.SelectObject(*mBitmap[mButtonState]);
dc.Blit(0, 0, mWidth, mHeight, &memDC, 0, 0, wxCOPY, FALSE);
}
void AButton::OnMouseEvent(wxMouseEvent & event)
{
if (mButtonIsDown || !mEnabled)
return;
if (event.ButtonUp()) {
mIsClicking = false;
ReleaseMouse();
if (event.m_x >= 0 && event.m_y >= 0 &&
event.m_x < mWidth && event.m_y < mHeight) {
mButtonState = AButtonDown;
mButtonIsDown = true;
wxCommandEvent *e =
new wxCommandEvent(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
GetParent()->ProcessEvent(*e);
delete e;
}
this->Refresh(false);
return;
}
if (event.ButtonDown()) {
mIsClicking = true;
CaptureMouse();
}
if (mIsClicking) {
if (event.m_x >= 0 && event.m_y >= 0 &&
event.m_x < mWidth && event.m_y < mHeight) {
mButtonState = AButtonDown;
} else
mButtonState = AButtonUp;
this->Refresh(false);
} else {
if (event.Entering()) {
mButtonState = AButtonOver;
this->Refresh(false);
}
if (event.Leaving()) {
mButtonState = AButtonUp;
this->Refresh(false);
}
}
}
void AButton::Enable()
{
mEnabled = true;
if (mButtonIsDown)
mButtonState = AButtonDown;
else
mButtonState = AButtonUp;
this->Refresh(false);
}
void AButton::Disable()
{
mEnabled = false;
mButtonState = AButtonDis;
this->Refresh(false);
}
void AButton::PushDown()
{
mButtonIsDown = true;
mButtonState = AButtonDown;
this->Refresh(false);
}
void AButton::PopUp()
{
mButtonIsDown = false;
mButtonState = AButtonUp;
this->Refresh(false);
}
|