5 * Created by Toshi Nagata on 2022/09/17.
6 * Copyright 2022 Toshi Nagata. All rights reserved.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation version 2 of the License.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
18 #include "MyToggleButton.h"
19 #include "wx/dcclient.h"
20 #include "wx/toplevel.h"
22 IMPLEMENT_DYNAMIC_CLASS(MyToggleButton, wxToggleButton)
24 BEGIN_EVENT_TABLE(MyToggleButton, wxToggleButton)
25 EVT_PAINT(MyToggleButton::OnPaint)
26 EVT_LEFT_DOWN(MyToggleButton::OnLeftDown)
27 EVT_LEFT_UP(MyToggleButton::OnLeftUp)
31 MyToggleButton::OnPaint(wxPaintEvent &event)
34 wxSize size = GetSize();
35 dc.SetPen(*wxGREY_PEN);
39 // Get the nearest TopLevelWindow and see if it is active or not
41 while (win != NULL && !win->IsKindOf(wxCLASSINFO(wxTopLevelWindow)))
42 win = win->GetParent();
43 if (win != NULL && ((wxTopLevelWindow *)win)->IsActive())
45 else isActive = false;
47 col = (isActive ? 128 : 180);
49 col = (isActive ? 180 : 220);
52 brush = wxTheBrushList->FindOrCreateBrush(wxColour(col, col, col));
54 dc.DrawRectangle(0, 0, size.x, size.y);
55 wxString label = GetLabel();
58 dc.GetTextExtent(label, &w, &h, &descent);
62 dc.SetTextForeground(*wxBLACK);
64 dc.SetTextForeground(wxColour(128, 128, 128));
65 dc.DrawText(label, x, y);
69 MyToggleButton::OnLeftDown(wxMouseEvent &event)
77 MyToggleButton::OnLeftUp(wxMouseEvent &event)
84 wxSize sz = GetSize();
85 if (x > 0 && x < sz.x && y > 0 && y < sz.y) {
86 SetValue(!GetValue());
87 wxCommandEvent cmdevt(wxEVT_TOGGLEBUTTON, GetId());
88 cmdevt.SetInt(GetValue());
89 cmdevt.SetEventObject(this);
90 ProcessCommand(cmdevt);