Skip to content

Commit

Permalink
#5108: OK button is not sensitive until a map file is selected
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Nov 19, 2020
1 parent 0dbd819 commit ae431ff
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
29 changes: 23 additions & 6 deletions radiant/ui/mapselector/MapSelector.cpp
Expand Up @@ -4,6 +4,7 @@
#include "ifilesystem.h"

#include "string/case_conv.h"
#include "os/path.h"
#include <wx/sizer.h>
#include <wx/button.h>

Expand All @@ -19,6 +20,7 @@ namespace
MapSelector::MapSelector() :
DialogBase(_(MAPSELECTOR_TITLE)),
_treeView(nullptr),
_buttons(nullptr),
_handlingSelectionChange(false)
{
SetSizer(new wxBoxSizer(wxVERTICAL));
Expand All @@ -29,13 +31,13 @@ MapSelector::MapSelector() :
setupTreeView(this);
vbox->Add(_treeView, 1, wxEXPAND);

wxStdDialogButtonSizer* buttonSizer = CreateStdDialogButtonSizer(wxOK | wxCANCEL);
_buttons = CreateStdDialogButtonSizer(wxOK | wxCANCEL);
wxButton* reloadButton = new wxButton(this, wxID_ANY, _("Rescan"));
reloadButton->Bind(wxEVT_BUTTON, &MapSelector::onRescanPath, this);

buttonSizer->Prepend(reloadButton, 0, wxRIGHT, 32);
_buttons->Prepend(reloadButton, 0, wxRIGHT, 32);

vbox->Add(buttonSizer, 0, wxALIGN_RIGHT | wxTOP, 12);
vbox->Add(_buttons, 0, wxALIGN_RIGHT | wxTOP, 12);

// Set the default size of the window
_position.connect(this);
Expand All @@ -48,6 +50,7 @@ int MapSelector::ShowModal()
{
// Populate the tree
populateTree();
updateButtonSensitivity();

// Enter the main loop
return DialogBase::ShowModal();
Expand Down Expand Up @@ -81,17 +84,18 @@ void MapSelector::OpenMapFromProject(const cmd::ArgumentList& args)
void MapSelector::setupTreeView(wxWindow* parent)
{
_treeView = wxutil::FileSystemView::Create(parent, wxBORDER_STATIC | wxDV_NO_HEADER);
_treeView->Bind(wxutil::EV_FSVIEW_SELECTION_CHANGED, &MapSelector::onSelectionChanged, this);

// Get the extensions from all possible patterns (e.g. *.map or *.mapx)
FileTypePatterns patterns = GlobalFiletypes().getPatternsForType(filetype::TYPE_MAP);

std::set<std::string> fileExtensions;

for (const auto& pattern : patterns)
{
fileExtensions.insert(pattern.extension);
_mapFileExtensions.insert(pattern.extension);
}

std::set<std::string> fileExtensions = _mapFileExtensions;

// Add all PK extensions too
const auto& pakExtensions = GlobalFileSystem().getArchiveExtensions();

Expand Down Expand Up @@ -119,6 +123,19 @@ void MapSelector::onRescanPath(wxCommandEvent& ev)
populateTree();
}

void MapSelector::updateButtonSensitivity()
{
auto fileExtension = string::to_lower_copy(os::getExtension(_treeView->GetSelectedPath()));
bool okIsEnabled = !_treeView->GetIsFolderSelected() && _mapFileExtensions.count(fileExtension) > 0;

_buttons->GetAffirmativeButton()->Enable(okIsEnabled);
}

void MapSelector::onSelectionChanged(wxutil::FileSystemView::SelectionChangedEvent& ev)
{
updateButtonSensitivity();
}

std::string MapSelector::getSelectedPath()
{
return _treeView->GetSelectedPath();
Expand Down
6 changes: 6 additions & 0 deletions radiant/ui/mapselector/MapSelector.h
Expand Up @@ -6,6 +6,8 @@
#include "wxutil/fsview/FileSystemView.h"
#include "wxutil/WindowPosition.h"

class wxStdDialogButtonSizer;

namespace ui
{

Expand All @@ -15,6 +17,7 @@ class MapSelector :
{
private:
wxPanel* _dialogPanel;
wxStdDialogButtonSizer* _buttons;

// Main tree view with the folder hierarchy
wxutil::FileSystemView* _treeView;
Expand All @@ -23,6 +26,7 @@ class MapSelector :
wxutil::WindowPosition _position;

bool _handlingSelectionChange;
std::set<std::string> _mapFileExtensions;

private:
// Private constructor, creates widgets
Expand All @@ -42,6 +46,8 @@ class MapSelector :
std::string getSelectedPath();

void onRescanPath(wxCommandEvent& ev);
void onSelectionChanged(wxutil::FileSystemView::SelectionChangedEvent& ev);
void updateButtonSensitivity();

public:
int ShowModal() override;
Expand Down

0 comments on commit ae431ff

Please sign in to comment.