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 "KeyboardGridCellEditor.h"
21 
22 #include "View/KeyboardShortcut.h"
23 #include "View/KeyboardShortcutEditor.h"
24 #include "View/KeyboardShortcutEvent.h"
25 
26 #include <wx/msgdlg.h>
27 
28 namespace TrenchBroom {
29     namespace View {
KeyboardGridCellEditor()30         KeyboardGridCellEditor::KeyboardGridCellEditor() :
31         wxGridCellEditor(),
32         m_editor(NULL),
33         m_evtHandler(NULL) {}
34 
KeyboardGridCellEditor(wxWindow * parent,wxWindowID windowId,wxEvtHandler * evtHandler,const int key,const int modifier1,const int modifier2,const int modifier3)35         KeyboardGridCellEditor::KeyboardGridCellEditor(wxWindow* parent, wxWindowID windowId, wxEvtHandler* evtHandler, const int key, const int modifier1, const int modifier2, const int modifier3) :
36         wxGridCellEditor(),
37         m_editor(NULL),
38         m_evtHandler(NULL) {
39             Create(parent, windowId, evtHandler);
40             m_editor->SetShortcut(key, modifier1, modifier2, modifier3);
41         }
42 
Create(wxWindow * parent,wxWindowID windowId,wxEvtHandler * evtHandler)43         void KeyboardGridCellEditor::Create(wxWindow* parent, wxWindowID windowId, wxEvtHandler* evtHandler) {
44             m_evtHandler = evtHandler;
45             m_editor = new KeyboardShortcutEditor(parent, wxID_ANY);
46             SetControl(m_editor);
47             // wxGridCellEditor::Create(parent, windowId, evtHandler);
48         }
49 
Clone() const50         wxGridCellEditor* KeyboardGridCellEditor::Clone() const {
51             return new KeyboardGridCellEditor(m_editor->GetParent(), wxID_ANY, m_evtHandler,
52                                               m_editor->key(),
53                                               m_editor->modifier1(),
54                                               m_editor->modifier2(),
55                                               m_editor->modifier3());
56         }
57 
BeginEdit(int row,int col,wxGrid * grid)58         void KeyboardGridCellEditor::BeginEdit(int row, int col, wxGrid* grid) {
59             int modifier1, modifier2, modifier3, key;
60             KeyboardShortcut::parseShortcut(grid->GetCellValue(row, col),
61                                             key,
62                                             modifier1,
63                                             modifier2,
64                                             modifier3);
65             m_editor->SetShortcut(key, modifier1, modifier2, modifier3);
66             m_editor->SetFocus();
67         }
68 
EndEdit(int row,int col,const wxGrid * grid,const wxString & oldValue,wxString * newValue)69         bool KeyboardGridCellEditor::EndEdit(int row, int col, const wxGrid* grid, const wxString& oldValue, wxString* newValue) {
70             *newValue = KeyboardShortcut::shortcutDisplayString(m_editor->key(),
71                                                                 m_editor->modifier1(),
72                                                                 m_editor->modifier2(),
73                                                                 m_editor->modifier3());
74             if (*newValue == oldValue)
75                 return false;
76             return true;
77         }
78 
ApplyEdit(int row,int col,wxGrid * grid)79         void KeyboardGridCellEditor::ApplyEdit(int row, int col, wxGrid* grid) {
80             wxString newValue = KeyboardShortcut::shortcutDisplayString(m_editor->key(),
81                                                                         m_editor->modifier1(),
82                                                                         m_editor->modifier2(),
83                                                                         m_editor->modifier3());
84             grid->SetCellValue(row, col, newValue);
85         }
86 
HandleReturn(wxKeyEvent & event)87         void KeyboardGridCellEditor::HandleReturn(wxKeyEvent& event) {
88             event.Skip();
89         }
90 
Reset()91         void KeyboardGridCellEditor::Reset() {
92             m_editor->SetShortcut();
93         }
94 
Show(bool show,wxGridCellAttr * attr)95         void KeyboardGridCellEditor::Show(bool show, wxGridCellAttr* attr) {
96             m_editor->Show(show);
97         }
98 
GetValue() const99         wxString KeyboardGridCellEditor::GetValue() const {
100             return KeyboardShortcut::shortcutDisplayString(m_editor->key(),
101                                                            m_editor->modifier1(),
102                                                            m_editor->modifier2(),
103                                                            m_editor->modifier3());
104         }
105     }
106 }
107