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 "PopupButton.h" 21 #include "View/ViewConstants.h" 22 #include "View/wxUtils.h" 23 24 #include <wx/frame.h> 25 #include <wx/tglbtn.h> 26 #include <wx/popupwin.h> 27 #include <wx/settings.h> 28 #include <wx/sizer.h> 29 30 namespace TrenchBroom { 31 namespace View { PopupButton(wxWindow * parent,const wxString & caption)32 PopupButton::PopupButton(wxWindow* parent, const wxString& caption) : 33 wxPanel(parent) { 34 m_button = new wxToggleButton(this, wxID_ANY, caption, wxDefaultPosition, wxDefaultSize, LayoutConstants::ToggleButtonStyle | wxBU_EXACTFIT); 35 36 wxFrame* frame = findFrame(this); 37 m_window = new wxPopupTransientWindow(frame); 38 39 #if defined __APPLE__ 40 m_window->SetWindowVariant(wxWINDOW_VARIANT_SMALL); 41 #endif 42 43 wxSizer* sizer = new wxBoxSizer(wxHORIZONTAL); 44 sizer->Add(m_button); 45 #ifdef __APPLE__ 46 sizer->SetItemMinSize(m_button, m_button->GetSize().x, m_button->GetSize().y + 1); 47 #endif 48 #ifdef __linux__ 49 sizer->SetItemMinSize(m_button, m_button->GetSize().x + 3, m_button->GetSize().y); 50 #endif 51 SetSizerAndFit(sizer); 52 53 m_button->Bind(wxEVT_TOGGLEBUTTON, &PopupButton::OnButtonToggled, this); 54 m_window->Bind(wxEVT_SHOW, &PopupButton::OnPopupShow, this); 55 } 56 GetPopupWindow() const57 wxWindow* PopupButton::GetPopupWindow() const { 58 return m_window; 59 } 60 OnButtonToggled(wxCommandEvent & event)61 void PopupButton::OnButtonToggled(wxCommandEvent& event) { 62 if (IsBeingDeleted()) return; 63 64 if (m_button->GetValue()) { 65 wxPoint position = GetScreenRect().GetRightBottom(); 66 position.x -= 2*m_window->GetSize().x; 67 position.y -= m_window->GetSize().y; 68 m_window->Position(position, m_window->GetSize()); 69 m_window->Popup(); 70 } else { 71 m_window->Dismiss(); 72 } 73 } 74 OnPopupShow(wxShowEvent & event)75 void PopupButton::OnPopupShow(wxShowEvent& event) { 76 if (IsBeingDeleted()) return; 77 78 if (m_button->GetValue() != event.IsShown()) 79 m_button->SetValue(event.IsShown()); 80 event.Skip(); 81 } 82 Enable(bool enable)83 bool PopupButton::Enable(bool enable) { 84 if (wxPanel::Enable(enable)) { 85 m_button->Enable(enable); 86 return true; 87 } 88 return false; 89 } 90 } 91 } 92