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 "KeyboardPreferencePane.h"
21 
22 #include "Macros.h"
23 #include "Preferences.h"
24 #include "View/ActionManager.h"
25 #include "View/KeyboardShortcutGridTable.h"
26 #include "View/ViewConstants.h"
27 
28 #include <wx/msgdlg.h>
29 #include <wx/sizer.h>
30 #include <wx/statbox.h>
31 #include <wx/stattext.h>
32 
33 #include <cassert>
34 
35 namespace TrenchBroom {
36     namespace View {
KeyboardPreferencePane(wxWindow * parent)37         KeyboardPreferencePane::KeyboardPreferencePane(wxWindow* parent) :
38         PreferencePane(parent),
39         m_grid(NULL),
40         m_table(NULL) {
41             wxWindow* menuShortcutGrid = createMenuShortcutGrid();
42 
43             wxSizer* outerSizer = new wxBoxSizer(wxVERTICAL);
44             outerSizer->Add(menuShortcutGrid, 1, wxEXPAND);
45             outerSizer->SetItemMinSize(menuShortcutGrid, 900, 550);
46             SetSizerAndFit(outerSizer);
47             SetBackgroundColour(*wxWHITE);
48         }
49 
OnGridSize(wxSizeEvent & event)50         void KeyboardPreferencePane::OnGridSize(wxSizeEvent& event) {
51             if (IsBeingDeleted()) return;
52 
53             int width = m_grid->GetClientSize().x;
54             m_grid->AutoSizeColumn(0);
55             m_grid->AutoSizeColumn(1);
56             int colSize = width - m_grid->GetColSize(0) - m_grid->GetColSize(1);
57             if (colSize < -1 || colSize == 0)
58                 colSize = -1;
59             m_grid->SetColSize(2, colSize);
60             event.Skip();
61         }
62 
createMenuShortcutGrid()63         wxWindow* KeyboardPreferencePane::createMenuShortcutGrid() {
64             wxPanel* container = new wxPanel(this);
65             container->SetBackgroundColour(*wxWHITE);
66 
67             m_table = new KeyboardShortcutGridTable();
68             m_grid = new wxGrid(container, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxBORDER_NONE);
69             m_grid->Bind(wxEVT_SIZE, &KeyboardPreferencePane::OnGridSize, this);
70 
71             m_grid->SetTable(m_table, true, wxGrid::wxGridSelectRows);
72             m_grid->SetColLabelSize(18);
73             m_grid->SetDefaultCellBackgroundColour(*wxWHITE);
74             m_grid->HideRowLabels();
75             m_grid->SetCellHighlightPenWidth(0);
76             m_grid->SetCellHighlightROPenWidth(0);
77 
78             m_grid->DisableColResize(0);
79             m_grid->DisableColResize(1);
80             m_grid->DisableColResize(2);
81             m_grid->DisableDragColMove();
82             m_grid->DisableDragCell();
83             m_grid->DisableDragColSize();
84             m_grid->DisableDragGridSize();
85             m_grid->DisableDragRowSize();
86 
87             m_table->update();
88 
89             wxStaticText* infoText = new wxStaticText(container, wxID_ANY, "Click twice on a key combination to edit the shortcut. Press delete or backspace to delete a shortcut.");
90             infoText->SetBackgroundColour(*wxWHITE);
91 #if defined __APPLE__
92             infoText->SetFont(*wxSMALL_FONT);
93 #endif
94 
95             wxSizer* sizer = new wxBoxSizer(wxVERTICAL);
96             sizer->Add(m_grid, 1, wxEXPAND);
97             sizer->AddSpacer(LayoutConstants::WideVMargin);
98             sizer->Add(infoText, 0, wxALIGN_CENTER);
99             sizer->AddSpacer(LayoutConstants::NarrowVMargin);
100             container->SetSizer(sizer);
101 
102             return container;
103         }
104 
doCanResetToDefaults()105         bool KeyboardPreferencePane::doCanResetToDefaults() {
106             return true;
107         }
108 
doResetToDefaults()109         void KeyboardPreferencePane::doResetToDefaults() {
110             ActionManager& actionManager = ActionManager::instance();
111             actionManager.resetShortcutsToDefaults();
112         }
113 
doUpdateControls()114         void KeyboardPreferencePane::doUpdateControls() {
115             m_table->update();
116         }
117 
doValidate()118         bool KeyboardPreferencePane::doValidate() {
119             m_grid->SaveEditControlValue();
120             if (m_table->hasDuplicates()) {
121                 wxMessageBox("Please fix all conflicting shortcuts (highlighted in red).", "Error", wxOK | wxCENTRE, this);
122                 return false;
123             }
124             return true;
125         }
126     }
127 }
128