-
Notifications
You must be signed in to change notification settings - Fork 0
/
wxOnOffBitmapButton.cpp
131 lines (110 loc) · 3.68 KB
/
wxOnOffBitmapButton.cpp
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
/***************************************************************
* Name: wxOnOffBitmapButton.cpp
* Purpose: wxWidgets Toggle On/Off button
* Author: Benoit BOUCHEZ (BEB)
* Created: 2020-04-11
* Copyright: (c) Benoit BOUCHEZ
* License: wxWindows licence
**************************************************************/
/* This wxWidget is an equivalent of the OnOffButton from VSTGUI framework.
Button is activated by a first click (value = true) and deactivated by a
second click (value = false). Each state is represented by a different
bitmap.
The two bitmaps must have the same size */
#include "wxOnOffBitmapButton.h"
IMPLEMENT_DYNAMIC_CLASS(wxOnOffBitmapButton, wxControl)
BEGIN_EVENT_TABLE(wxOnOffBitmapButton, wxControl)
EVT_MOUSE_EVENTS(wxOnOffBitmapButton::OnMouse)
EVT_PAINT(wxOnOffBitmapButton::OnPaint)
END_EVENT_TABLE()
wxOnOffBitmapButton::wxOnOffBitmapButton (wxWindow *parent,
wxWindowID id,
wxBitmap& offBitmap,
wxBitmap& onBitmap,
const wxPoint& pos,
const wxSize& size,
long style,
const wxValidator& WXUNUSED(validator),
const wxString& WXUNUSED(name)) : wxControl(parent, id, pos, size, style)
{
Init();
mId=id;
mOffBitmap=&offBitmap;
mOnBitmap=&onBitmap;
if (parent)
SetBackgroundColour(parent->GetBackgroundColour());
else
SetBackgroundColour(*wxLIGHT_GREY);
} // wxOnOffBitmapButton::wxOnOffBitmapButton
// ----------------------------------------------------------------------------
/*
wxOnOffBitmapButton::~wxOnOffBitmapButton()
{
printf ("wxOnOffBitmapButton destructor called\n");
} // wxOnOffBitmapButton::~wxOnOffBitmapButton
// ----------------------------------------------------------------------------
*/
void wxOnOffBitmapButton::Init (void)
{
mOffBitmap=0;
mOnBitmap=0;
mId=0;
MouseInControl=false;
UserToggleAllowed=true;
mState=false;
} // wxOnOffBitmapButton::Init
// ----------------------------------------------------------------------------
void wxOnOffBitmapButton::SetValue(bool state)
{
if (state==mState) return; // Value does not change : no need to update
mState=state;
Refresh();
} // wxOnOffBitmapButton::SetValue
// ----------------------------------------------------------------------------
void wxOnOffBitmapButton::OnPaint(wxPaintEvent& WXUNUSED(event))
{
wxPaintDC dc(this);
if (mState)
dc.DrawBitmap(*mOnBitmap, 0, 0, TRUE);
else
dc.DrawBitmap(*mOffBitmap, 0, 0, TRUE);
} // wxOnOffBitmapButton::OnPaint
// ----------------------------------------------------------------------------
void wxOnOffBitmapButton::OnMouse(wxMouseEvent& event)
{
if (event.Entering())
{
MouseInControl=true;
return;
}
if (event.Leaving())
{
MouseInControl=false;
return;
}
// Toggle state when clicked
if (event.LeftDown() && MouseInControl)
{
if (UserToggleAllowed)
{
if (mState) mState=false;
else mState=true;
Refresh();
}
Click();
}
event.Skip();
} // wxOnOffBitmapButton::OnMouse
// ----------------------------------------------------------------------------
void wxOnOffBitmapButton::Click (void)
{
wxCommandEvent event(wxEVT_BUTTON, mId);
event.SetEventObject(this);
ProcessEvent (event);
} // wxOnOffBitmapButton::Click
// ----------------------------------------------------------------------------
void wxOnOffBitmapButton::AllowUserChange (bool Allow)
{
UserToggleAllowed=Allow;
} // wxOnOffBitmapButton::AllowChange
// ----------------------------------------------------------------------------