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 "CollapsibleTitledPanel.h"
21 
22 #include "View/BorderLine.h"
23 #include "View/ViewConstants.h"
24 
25 #include <wx/sizer.h>
26 #include <wx/stattext.h>
27 
28 wxDEFINE_EVENT(TITLE_BAR_CLICK, wxCommandEvent);
29 
30 namespace TrenchBroom {
31     namespace View {
CollapsibleTitleBar(wxWindow * parent,const wxString & title,const wxString & stateText)32         CollapsibleTitleBar::CollapsibleTitleBar(wxWindow* parent, const wxString& title, const wxString& stateText) :
33         TitleBar(parent, title, LayoutConstants::NarrowHMargin, LayoutConstants::NarrowVMargin),
34         m_stateText(new wxStaticText(this, wxID_ANY, stateText)) {
35             m_stateText->SetFont(m_titleText->GetFont());
36             m_stateText->SetForegroundColour(*wxLIGHT_GREY);
37 
38             GetSizer()->Add(m_stateText, 0, wxTOP | wxBOTTOM, LayoutConstants::NarrowVMargin);
39             GetSizer()->AddSpacer(LayoutConstants::NarrowHMargin);
40             Layout();
41 
42             Bind(wxEVT_LEFT_DOWN, &CollapsibleTitleBar::OnClick, this);
43             m_titleText->Bind(wxEVT_LEFT_DOWN, &CollapsibleTitleBar::OnClick, this);
44             m_stateText->Bind(wxEVT_LEFT_DOWN, &CollapsibleTitleBar::OnClick, this);
45         }
46 
setStateText(const wxString & stateText)47         void CollapsibleTitleBar::setStateText(const wxString& stateText) {
48             m_stateText->SetLabel(stateText);
49             Layout();
50         }
51 
OnClick(wxMouseEvent & event)52         void CollapsibleTitleBar::OnClick(wxMouseEvent& event) {
53             if (IsBeingDeleted()) return;
54 
55             wxCommandEvent newEvent(TITLE_BAR_CLICK, GetId());
56             newEvent.SetEventObject(this);
57             wxPostEvent(this, newEvent);
58         }
59 
CollapsibleTitledPanel(wxWindow * parent,const wxString & title,const bool initiallyExpanded)60         CollapsibleTitledPanel::CollapsibleTitledPanel(wxWindow* parent, const wxString& title, const bool initiallyExpanded) :
61         wxPanel(parent),
62         m_titleBar(new CollapsibleTitleBar(this, title, "hide")),
63         m_divider(new BorderLine(this, BorderLine::Direction_Horizontal)),
64         m_panel(new wxPanel(this)),
65         m_expanded(initiallyExpanded) {
66             wxSizer* sizer = new wxBoxSizer(wxVERTICAL);
67             sizer->Add(m_titleBar, 0, wxEXPAND);
68             sizer->Add(m_divider, 0, wxEXPAND);
69             sizer->Add(m_panel, 1, wxEXPAND);
70             SetSizer(sizer);
71 
72             m_titleBar->Bind(TITLE_BAR_CLICK, &CollapsibleTitledPanel::OnTitleBarClick, this);
73 
74             update();
75         }
76 
getPanel() const77         wxWindow* CollapsibleTitledPanel::getPanel() const {
78             return m_panel;
79         }
80 
expand()81         void CollapsibleTitledPanel::expand() {
82             setExpanded(true);
83         }
84 
collapse()85         void CollapsibleTitledPanel::collapse() {
86             setExpanded(false);
87         }
88 
expanded() const89         bool CollapsibleTitledPanel::expanded() const {
90             return m_expanded;
91         }
92 
setExpanded(const bool expanded)93         void CollapsibleTitledPanel::setExpanded(const bool expanded) {
94             if (expanded == m_expanded)
95                 return;
96 
97             m_expanded = expanded;
98             update();
99         }
100 
OnTitleBarClick(wxCommandEvent & event)101         void CollapsibleTitledPanel::OnTitleBarClick(wxCommandEvent& event) {
102             if (IsBeingDeleted()) return;
103 
104             setExpanded(!m_expanded);
105         }
106 
update()107         void CollapsibleTitledPanel::update() {
108             if (m_expanded) {
109                 m_divider->Show();
110                 m_panel->wxWindowBase::ShowWithEffect(wxSHOW_EFFECT_ROLL_TO_BOTTOM);
111                 m_titleBar->setStateText("hide");
112             } else {
113                 m_divider->Hide();
114                 m_panel->wxWindowBase::HideWithEffect(wxSHOW_EFFECT_ROLL_TO_TOP);
115                 m_titleBar->setStateText("show");
116             }
117 
118             wxWindow* window = this;
119             while (window != NULL) {
120                 window->Layout();
121                 window = window->GetParent();
122             }
123 
124         }
125     }
126 }
127