| 1 |
#include "file_picker_ctrl.h" |
| 2 |
#include <wx/filedlg.h> |
| 3 |
#include <wx/filename.h> |
| 4 |
|
| 5 |
const wxEventType wxEVT_FILE_PICKED = wxNewEventType(); |
| 6 |
|
| 7 |
FilePickerCtrl::FilePickerCtrl(wxWindow* parent, const wxString& projectPath, const wxString& value) |
| 8 |
: wxTextCtrl(parent, wxID_ANY, value, wxDefaultPosition, wxDefaultSize, wxTE_RICH2 | wxTE_PROCESS_ENTER) |
| 9 |
, m_bmpPath(value) |
| 10 |
, m_projectPath(projectPath) |
| 11 |
{ |
| 12 |
this->Connect(wxEVT_COMMAND_TEXT_ENTER, wxCommandEventHandler(FilePickerCtrl::OnTextEnter), NULL, this); |
| 13 |
this->Connect(wxEVT_LEFT_DCLICK, wxMouseEventHandler(FilePickerCtrl::OnMouseLeft), NULL, this); |
| 14 |
wxTextCtrl::ChangeValue(value); |
| 15 |
} |
| 16 |
|
| 17 |
FilePickerCtrl::~FilePickerCtrl() |
| 18 |
{ |
| 19 |
this->Disconnect(wxEVT_COMMAND_TEXT_ENTER, wxCommandEventHandler(FilePickerCtrl::OnTextEnter), NULL, this); |
| 20 |
this->Disconnect(wxEVT_LEFT_DCLICK, wxMouseEventHandler(FilePickerCtrl::OnMouseLeft), NULL, this); |
| 21 |
} |
| 22 |
|
| 23 |
void FilePickerCtrl::OnTextEnter(wxCommandEvent& e) { DoNotify(); } |
| 24 |
|
| 25 |
void FilePickerCtrl::OnMouseLeft(wxMouseEvent& event) { DoEdit(); } |
| 26 |
|
| 27 |
void FilePickerCtrl::DoEdit() |
| 28 |
{ |
| 29 |
wxString str = wxTextCtrl::GetValue(); |
| 30 |
str.Trim().Trim(false); |
| 31 |
|
| 32 |
wxFileName fn; |
| 33 |
if(m_bmpPath.IsEmpty()) { |
| 34 |
fn = wxFileName(m_projectPath, wxT("")); |
| 35 |
|
| 36 |
} else { |
| 37 |
fn = wxFileName(m_bmpPath); |
| 38 |
fn.MakeAbsolute(m_projectPath); |
| 39 |
} |
| 40 |
|
| 41 |
wxString wildcard = wxT("PNG files (*.png)|*.png|BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif|All files (*)|*"); |
| 42 |
wxString newPath = wxFileSelector(_("Select bitmap"), wxT(""), wxT(""), wxEmptyString, wildcard, wxFD_OPEN); |
| 43 |
if(newPath.IsEmpty() == false) { |
| 44 |
wxFileName newFilePath(newPath); |
| 45 |
if(m_projectPath.IsEmpty() == false) { newFilePath.MakeRelativeTo(m_projectPath); } |
| 46 |
m_bmpPath = newFilePath.GetFullPath(wxPATH_UNIX); |
| 47 |
wxTextCtrl::ChangeValue(m_bmpPath); |
| 48 |
DoNotify(); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
void FilePickerCtrl::DoNotify() |
| 53 |
{ |
| 54 |
wxCommandEvent event(wxEVT_FILE_PICKED); |
| 55 |
event.SetString(GetValue()); |
| 56 |
event.SetEventObject(this); |
| 57 |
GetEventHandler()->AddPendingEvent(event); |
| 58 |
} |