OSDN Git Service

Handling key and mouse events in listctrl is improved
[molby/Molby.git] / wxSources / MyToggleButton.cpp
1 /*
2  *  MyToggleButton
3  *  Molby
4  *
5  *  Created by Toshi Nagata on 2022/09/17.
6  *  Copyright 2022 Toshi Nagata. All rights reserved.
7  *
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.
11  
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.
16  */
17
18 #include "MyToggleButton.h"
19 #include "wx/dcclient.h"
20 #include "wx/toplevel.h"
21
22 IMPLEMENT_DYNAMIC_CLASS(MyToggleButton, wxToggleButton)
23
24 BEGIN_EVENT_TABLE(MyToggleButton, wxToggleButton)
25 EVT_PAINT(MyToggleButton::OnPaint)
26 EVT_LEFT_DOWN(MyToggleButton::OnLeftDown)
27 EVT_LEFT_UP(MyToggleButton::OnLeftUp)
28 END_EVENT_TABLE()
29
30 void
31 MyToggleButton::OnPaint(wxPaintEvent &event)
32 {
33     wxPaintDC dc(this);
34     wxSize size = GetSize();
35     dc.SetPen(*wxGREY_PEN);
36     const wxBrush *brush;
37     bool isActive;
38     int col;
39     //  Get the nearest TopLevelWindow and see if it is active or not
40     wxWindow *win = this;
41     while (win != NULL && !win->IsKindOf(wxCLASSINFO(wxTopLevelWindow)))
42         win = win->GetParent();
43     if (win != NULL && ((wxTopLevelWindow *)win)->IsActive())
44         isActive = true;
45     else isActive = false;
46     if (IsPressed())
47         col = (isActive ? 128 : 180);
48     else if (GetValue())
49         col = (isActive ? 180 : 220);
50     else
51         col = 240;
52     brush = wxTheBrushList->FindOrCreateBrush(wxColour(col, col, col));
53     dc.SetBrush(*brush);
54     dc.DrawRectangle(0, 0, size.x, size.y);
55     wxString label = GetLabel();
56     int w, h, descent;
57     int x, y;
58     dc.GetTextExtent(label, &w, &h, &descent);
59     x = (size.x - w) / 2;
60     y = (size.y - h) / 2;
61     if (isActive)
62         dc.SetTextForeground(*wxBLACK);
63     else
64         dc.SetTextForeground(wxColour(128, 128, 128));
65     dc.DrawText(label, x, y);
66 }
67
68 void
69 MyToggleButton::OnLeftDown(wxMouseEvent &event)
70 {
71     CaptureMouse();
72     SetPressed(true);
73     Refresh();
74 }
75
76 void
77 MyToggleButton::OnLeftUp(wxMouseEvent &event)
78 {
79     if (HasCapture()) {
80         ReleaseMouse();
81         SetPressed(false);
82         int x = event.GetX();
83         int y = event.GetY();
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);
91             Refresh();
92         }
93     }
94 }