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 "FlagsPopupEditor.h"
21 #include "View/FlagsEditor.h"
22 #include "View/FlagChangedCommand.h"
23 #include "View/ViewConstants.h"
24 #include "View/PopupButton.h"
25 
26 #include <wx/settings.h>
27 #include <wx/sizer.h>
28 #include <wx/stattext.h>
29 
30 namespace TrenchBroom {
31     namespace View {
FlagsPopupEditor(wxWindow * parent,const size_t numCols,const wxString & buttonLabel,const bool showFlagsText)32         FlagsPopupEditor::FlagsPopupEditor(wxWindow* parent, const size_t numCols, const wxString& buttonLabel , const bool showFlagsText) :
33         wxPanel(parent),
34         m_flagsTxt(NULL),
35         m_button(NULL),
36         m_editor(NULL) {
37             wxPanel* flagsPanel = NULL;
38             if (showFlagsText) {
39                 flagsPanel = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxBORDER_SUNKEN);
40                 flagsPanel->SetBackgroundColour(*wxWHITE);
41 
42                 m_flagsTxt = new wxStaticText(flagsPanel, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxALIGN_LEFT | wxST_ELLIPSIZE_END);
43 
44                 wxSizer* flagsPanelSizer = new wxBoxSizer(wxVERTICAL);
45                 flagsPanelSizer->AddStretchSpacer();
46                 flagsPanelSizer->Add(m_flagsTxt, 0, wxEXPAND | wxLEFT | wxRIGHT, LayoutConstants::TextBoxInnerMargin);
47                 flagsPanelSizer->AddStretchSpacer();
48                 flagsPanel->SetSizer(flagsPanelSizer);
49             }
50 
51             m_button = new PopupButton(this, buttonLabel);
52             m_button->SetToolTip("Click to edit flags");
53 
54             wxPanel* editorContainer = new wxPanel(m_button->GetPopupWindow(), wxID_ANY, wxDefaultPosition, wxDefaultSize, wxBORDER_SIMPLE);
55             m_editor = new FlagsEditor(editorContainer, numCols);
56 
57             wxSizer* editorContainerSizer = new wxBoxSizer(wxVERTICAL);
58             editorContainerSizer->Add(m_editor, 1, wxEXPAND | wxALL, LayoutConstants::DialogOuterMargin);
59             editorContainer->SetSizer(editorContainerSizer);
60 
61             wxSizer* popupSizer = new wxBoxSizer(wxVERTICAL);
62             popupSizer->Add(editorContainer, 1, wxEXPAND);
63             m_button->GetPopupWindow()->SetSizerAndFit(popupSizer);
64 
65             wxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
66             if (flagsPanel != NULL) {
67                 sizer->Add(flagsPanel, 1, wxEXPAND);
68                 sizer->AddSpacer(LayoutConstants::MediumHMargin);
69             }
70             sizer->Add(m_button, 0, wxALIGN_CENTER_VERTICAL);
71             SetSizerAndFit(sizer);
72 
73             m_editor->Bind(FLAG_CHANGED_EVENT, &FlagsPopupEditor::OnFlagChanged, this);
74         }
75 
setFlags(const wxArrayString & labels,const wxArrayString & tooltips)76         void FlagsPopupEditor::setFlags(const wxArrayString& labels, const wxArrayString& tooltips) {
77             m_editor->setFlags(labels, tooltips);
78             m_button->GetPopupWindow()->Fit();
79             updateFlagsText();
80         }
81 
setFlags(const wxArrayInt & values,const wxArrayString & labels,const wxArrayString & tooltips)82         void FlagsPopupEditor::setFlags(const wxArrayInt& values, const wxArrayString& labels, const wxArrayString& tooltips) {
83             m_editor->setFlags(values, labels, tooltips);
84             m_button->GetPopupWindow()->Fit();
85             updateFlagsText();
86         }
87 
setFlagValue(const int set,const int mixed)88         void FlagsPopupEditor::setFlagValue(const int set, const int mixed) {
89             m_editor->setFlagValue(set, mixed);
90             updateFlagsText();
91         }
92 
OnFlagChanged(FlagChangedCommand & event)93         void FlagsPopupEditor::OnFlagChanged(FlagChangedCommand& event) {
94             if (IsBeingDeleted()) return;
95 
96             updateFlagsText();
97             ProcessEvent(event);
98         }
99 
Enable(bool enable)100         bool FlagsPopupEditor::Enable(bool enable) {
101             if (wxPanel::Enable(enable)) {
102                 m_button->Enable(enable);
103                 updateFlagsText();
104                 return true;
105             }
106             return false;
107         }
108 
updateFlagsText()109         void FlagsPopupEditor::updateFlagsText() {
110             if (m_flagsTxt == NULL)
111                 return;
112 
113             if (!IsEnabled()) {
114                 m_flagsTxt->SetForegroundColour(Colors::disabledText());
115                 m_flagsTxt->SetLabel("n/a");
116                 m_flagsTxt->UnsetToolTip();
117                 return;
118             }
119 
120             wxString label;
121             bool first = true;
122             bool mixed = false;
123             for (size_t i = 0; i < m_editor->getNumFlags() && !mixed; ++i) {
124                 if (m_editor->isFlagMixed(i)) {
125                     label = "multi";
126                     mixed = true;
127                 } else if (m_editor->isFlagSet(i)) {
128                     if (!first)
129                         label << ", ";
130                     label << m_editor->getFlagLabel(i);
131                     first = false;
132                 }
133             }
134 
135             m_flagsTxt->SetLabel(label);
136             if (!first)
137                 m_flagsTxt->SetToolTip(label);
138             else
139                 m_flagsTxt->UnsetToolTip();
140 
141             if (mixed)
142                 m_flagsTxt->SetForegroundColour(Colors::disabledText());
143             else
144                 m_flagsTxt->SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT));
145         }
146     }
147 }
148