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 "GamesPreferencePane.h"
21 
22 #include "PreferenceManager.h"
23 #include "Preferences.h"
24 #include "IO/Path.h"
25 #include "Model/Game.h"
26 #include "Model/GameFactory.h"
27 #include "View/BorderLine.h"
28 #include "View/GameListBox.h"
29 #include "View/GameSelectionCommand.h"
30 #include "View/ViewConstants.h"
31 
32 #include <wx/button.h>
33 #include <wx/dirdlg.h>
34 #include <wx/sizer.h>
35 #include <wx/settings.h>
36 #include <wx/stattext.h>
37 
38 namespace TrenchBroom {
39     namespace View {
GamesPreferencePane(wxWindow * parent)40         GamesPreferencePane::GamesPreferencePane(wxWindow* parent) :
41         PreferencePane(parent) {
42             createGui();
43             bindEvents();
44         }
45 
OnGameSelectionChanged(GameSelectionCommand & event)46         void GamesPreferencePane::OnGameSelectionChanged(GameSelectionCommand& event) {
47             if (IsBeingDeleted()) return;
48 
49             updateControls();
50         }
51 
OnChooseGamePathClicked(wxCommandEvent & event)52         void GamesPreferencePane::OnChooseGamePathClicked(wxCommandEvent& event) {
53             if (IsBeingDeleted()) return;
54 
55             const wxString pathStr = ::wxDirSelector("Choose game directory", wxEmptyString, wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST);
56             if (!pathStr.empty()) {
57                 const IO::Path gamePath(pathStr.ToStdString());
58                 const String gameName = m_gameListBox->selectedGameName();
59                 Model::GameFactory& gameFactory = Model::GameFactory::instance();
60                 gameFactory.setGamePath(gameName, gamePath);
61 
62                 updateControls();
63             }
64         }
65 
createGui()66         void GamesPreferencePane::createGui() {
67             m_gameListBox = new GameListBox(this, wxBORDER_NONE);
68             m_gameListBox->selectGame(0);
69 
70             wxWindow* gamePreferences = createGamePreferences();
71 
72             wxSizer* prefMarginSizer = new wxBoxSizer(wxVERTICAL);
73             prefMarginSizer->AddSpacer(LayoutConstants::WideVMargin);
74             prefMarginSizer->Add(gamePreferences, 0, wxEXPAND);
75             prefMarginSizer->AddSpacer(LayoutConstants::ChoiceSizeDelta);
76 
77             wxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
78             sizer->Add(m_gameListBox, 0, wxEXPAND);
79             sizer->Add(new BorderLine(this, BorderLine::Direction_Vertical), 0, wxEXPAND);
80             sizer->AddSpacer(LayoutConstants::WideVMargin);
81             sizer->Add(prefMarginSizer, 1, wxEXPAND);
82             sizer->AddSpacer(LayoutConstants::WideVMargin);
83             sizer->SetItemMinSize(m_gameListBox, 200, 200);
84 
85             SetSizer(sizer);
86             SetMinSize(wxSize(600, 300));
87             SetBackgroundColour(*wxWHITE);
88         }
89 
createGamePreferences()90         wxWindow* GamesPreferencePane::createGamePreferences() {
91             wxPanel* box = new wxPanel(this);
92             box->SetBackgroundColour(*wxWHITE);
93 
94             wxStaticText* gamePathLabel = new wxStaticText(box, wxID_ANY, "Game Path");
95             gamePathLabel->SetFont(gamePathLabel->GetFont().Bold());
96             m_gamePathValueLabel = new wxStaticText(box, wxID_ANY, "Not set");
97             m_chooseGamePathButton = new wxButton(box, wxID_ANY, "Choose...");
98 
99             wxFlexGridSizer* sizer = new wxFlexGridSizer(3, LayoutConstants::WideHMargin, LayoutConstants::WideVMargin);
100             sizer->AddGrowableCol(1);
101             sizer->Add(gamePathLabel, 0, wxALIGN_CENTER_VERTICAL);
102             sizer->Add(m_gamePathValueLabel, 0, wxALIGN_CENTER_VERTICAL);
103             sizer->Add(m_chooseGamePathButton, 0, wxALIGN_CENTER_VERTICAL);
104             sizer->SetItemMinSize(m_chooseGamePathButton, wxDefaultCoord, m_chooseGamePathButton->GetSize().y + 1);
105 
106             box->SetSizer(sizer);
107             return box;
108         }
109 
bindEvents()110         void GamesPreferencePane::bindEvents() {
111             m_gameListBox->Bind(GAME_SELECTION_CHANGE_EVENT, &GamesPreferencePane::OnGameSelectionChanged, this);
112             m_chooseGamePathButton->Bind(wxEVT_BUTTON, &GamesPreferencePane::OnChooseGamePathClicked, this);
113         }
114 
doCanResetToDefaults()115         bool GamesPreferencePane::doCanResetToDefaults() {
116             return false;
117         }
118 
doResetToDefaults()119         void GamesPreferencePane::doResetToDefaults() {}
120 
doUpdateControls()121         void GamesPreferencePane::doUpdateControls() {
122             const String gameName = m_gameListBox->selectedGameName();
123             Model::GameFactory& gameFactory = Model::GameFactory::instance();
124             const IO::Path gamePath = gameFactory.gamePath(gameName);
125             m_gamePathValueLabel->SetLabel(gamePath.isEmpty() ? "not set" : gamePath.asString());
126             m_gameListBox->reloadGameInfos();
127             Layout();
128         }
129 
doValidate()130         bool GamesPreferencePane::doValidate() {
131             return true;
132         }
133     }
134 }
135