1 /*
2  Copyright (C) 2010-2014 Kristian Duske
3 
4  This file is part of TrenchBroom.
5 
6  TrenchBroom is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  TrenchBroom is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with TrenchBroom. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "BitmapToggleButton.h"
21 
22 #include <wx/dcclient.h>
23 #include <wx/log.h>
24 
25 #include <algorithm>
26 
27 namespace TrenchBroom {
28     namespace View {
BitmapToggleButton(wxWindow * parent,wxWindowID windowId,const wxBitmap & upBitmap,const wxBitmap & downBitmap)29         BitmapToggleButton::BitmapToggleButton(wxWindow* parent, wxWindowID windowId, const wxBitmap& upBitmap, const wxBitmap& downBitmap) :
30         wxPanel(parent, windowId),
31         m_upBitmap(upBitmap),
32         m_downBitmap(downBitmap),
33         m_upDisabledBitmap(m_upBitmap.ConvertToDisabled()),
34         m_downDisabledBitmap(m_downBitmap.ConvertToDisabled()),
35         m_state(false) {
36             assert(m_upBitmap.IsOk());
37             assert(m_downBitmap.IsOk());
38 
39             SetMinClientSize(bitmapSize());
40 
41             Bind(wxEVT_PAINT, &BitmapToggleButton::OnPaint, this);
42             Bind(wxEVT_LEFT_DOWN, &BitmapToggleButton::OnMouseDown, this);
43         }
44 
OnPaint(wxPaintEvent & event)45         void BitmapToggleButton::OnPaint(wxPaintEvent& event) {
46             if (IsBeingDeleted()) return;
47 
48             const wxSize size = GetClientSize();
49             const wxSize bmpSize = bitmapSize();
50             const wxSize delta = size - bmpSize;
51             const wxPoint offset(delta.x / 2, delta.y / 2);
52 
53             wxPaintDC dc(this);
54             dc.DrawBitmap(currentBitmap(), offset);
55         }
56 
OnMouseDown(wxMouseEvent & event)57         void BitmapToggleButton::OnMouseDown(wxMouseEvent& event) {
58             if (IsBeingDeleted()) return;
59 
60             if (!IsEnabled())
61                 return;
62 
63             m_state = !m_state;
64             Refresh();
65 
66             wxCommandEvent buttonEvent(wxEVT_BUTTON, GetId());
67             buttonEvent.SetEventObject(this);
68             buttonEvent.SetInt(static_cast<int>(m_state));
69 
70             ProcessEvent(buttonEvent);
71         }
72 
DoUpdateWindowUI(wxUpdateUIEvent & event)73         void BitmapToggleButton::DoUpdateWindowUI(wxUpdateUIEvent& event) {
74             if (event.GetSetEnabled() && IsEnabled() != event.GetEnabled()) {
75                 Enable(event.GetEnabled());
76                 Refresh();
77             }
78             if (event.GetSetChecked()) {
79                 if (m_state != event.GetChecked()) {
80                     m_state = event.GetChecked();
81                     Refresh();
82                 }
83             }
84         }
85 
bitmapSize() const86         wxSize BitmapToggleButton::bitmapSize() const {
87             return wxSize(std::max(m_upBitmap.GetWidth(), m_downBitmap.GetWidth()),
88                           std::max(m_upBitmap.GetHeight(), m_downBitmap.GetHeight()));
89         }
90 
currentBitmap() const91         wxBitmap BitmapToggleButton::currentBitmap() const {
92             if (IsEnabled())
93                 return m_state ? m_downBitmap : m_upBitmap;
94             return m_state ? m_downDisabledBitmap : m_upDisabledBitmap;
95         }
96     }
97 }
98