Skip to content

Commit

Permalink
#5568: Assign the value to the target control, to make the preview ad…
Browse files Browse the repository at this point in the history
…just automatically to the current selection
  • Loading branch information
codereader committed Mar 23, 2021
1 parent 3b8358a commit 0c663f5
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 11 deletions.
58 changes: 54 additions & 4 deletions radiant/ui/common/ImageFileSelector.cpp
Expand Up @@ -10,8 +10,10 @@ namespace
const char* const WINDOW_TITLE = N_("Select Image File");
}

ImageFileSelector::ImageFileSelector(wxWindow* parent) :
DialogBase(_(WINDOW_TITLE), parent)
ImageFileSelector::ImageFileSelector(wxWindow* parent, wxTextCtrl* targetControl) :
DialogBase(_(WINDOW_TITLE), parent),
_okButton(nullptr),
_targetControl(targetControl)
{
SetSizer(new wxBoxSizer(wxVERTICAL));

Expand All @@ -22,9 +24,14 @@ ImageFileSelector::ImageFileSelector(wxWindow* parent) :

_treeView->AddSearchColumn(_columns.iconAndName);
_treeView->SetExpandTopLevelItemsAfterPopulation(true);
_treeView->Bind(wxEVT_DATAVIEW_SELECTION_CHANGED, &ImageFileSelector::onTreeSelectionChanged, this);

auto dialogButtons = CreateStdDialogButtonSizer(wxOK | wxCANCEL);
_okButton = dialogButtons->GetAffirmativeButton();
_okButton->Disable();

GetSizer()->Add(_treeView, 1, wxALL|wxEXPAND, 12);
GetSizer()->Add(CreateStdDialogButtonSizer(wxOK | wxCANCEL), 0, wxALIGN_RIGHT | wxBOTTOM | wxRIGHT, 12);
GetSizer()->Add(dialogButtons, 0, wxALIGN_RIGHT | wxBOTTOM | wxRIGHT, 12);

FitToScreen(0.5f, 0.7f);
}
Expand All @@ -33,7 +40,50 @@ int ImageFileSelector::ShowModal()
{
_treeView->Populate(std::make_shared<ImageFilePopulator>(_columns));

return DialogBase::ShowModal();
_previousValue = _targetControl->GetValue().ToStdString();

auto result = DialogBase::ShowModal();

if (result != wxID_OK)
{
// Restore the previous value on cancel
_targetControl->SetValue(_previousValue);
}

return result;
}

std::string ImageFileSelector::GetSelection()
{
auto item = _treeView->GetSelection();
if (!item.IsOk()) return std::string();

wxutil::TreeModel::Row row(item, *_treeView->GetModel());

if (row[_columns.isFolder].getBool()) return std::string();

return row[_columns.fullName];
}

void ImageFileSelector::onTreeSelectionChanged(wxDataViewEvent& ev)
{
auto item = _treeView->GetSelection();

if (!item.IsOk())
{
_okButton->Disable();
return;
}

wxutil::TreeModel::Row row(item, *_treeView->GetModel());

bool isFolder = row[_columns.isFolder].getBool();
_okButton->Enable(!isFolder);

if (!isFolder)
{
_targetControl->SetValue(row[_columns.fullName].getString());
}
}

}
13 changes: 12 additions & 1 deletion radiant/ui/common/ImageFileSelector.h
Expand Up @@ -2,6 +2,8 @@

#include "wxutil/dialog/DialogBase.h"
#include "wxutil/dataview/ResourceTreeView.h"
#include <wx/button.h>
#include <wx/textctrl.h>

namespace ui
{
Expand All @@ -16,10 +18,19 @@ class ImageFileSelector :
wxutil::ResourceTreeView::Columns _columns;
wxutil::ResourceTreeView* _treeView;

wxButton* _okButton;
wxTextCtrl* _targetControl;
std::string _previousValue;

public:
ImageFileSelector(wxWindow* parent);
ImageFileSelector(wxWindow* parent, wxTextCtrl* targetControl);

int ShowModal() override;

std::string GetSelection();

private:
void onTreeSelectionChanged(wxDataViewEvent& ev);
};

}
9 changes: 3 additions & 6 deletions radiant/ui/materials/MapExpressionEntry.h
Expand Up @@ -46,12 +46,9 @@ class MapExpressionEntry :
private:
void onBrowseButtonClick(wxCommandEvent& ev)
{
auto selector = new ImageFileSelector(this);

if (selector->ShowModal() == wxID_OK)
{
// TODO: assign expression
}
auto selector = new ImageFileSelector(this, _textEntry);
selector->ShowModal();
selector->Destroy();
}
};

Expand Down

0 comments on commit 0c663f5

Please sign in to comment.