Permalink
Browse files

Remember 'filter through command' entries in settings.

  • Loading branch information...
1 parent dbcbe2d commit 2be5f497b80c593ab20a94f7b1bad063e4f30346 @adamv adamv committed May 30, 2009
Showing with 122 additions and 9 deletions.
  1. +1 −1 src/EditorCtrl.cpp
  2. +58 −5 src/RunCmdDlg.cpp
  3. +9 −1 src/RunCmdDlg.h
  4. +41 −0 src/eSettings.cpp
  5. +13 −2 src/eSettings.h
View
@@ -1362,7 +1362,7 @@ bool EditorCtrl::DoTabTrigger(unsigned int wordstart, unsigned int wordend) {
}
void EditorCtrl::FilterThroughCommand() {
- RunCmdDlg dlg(this);
+ RunCmdDlg dlg(this, eGetSettings());
if (dlg.ShowModal() == wxID_OK) {
const tmCommand cmd = dlg.GetCommand();
DoAction(cmd, NULL, false);
View
@@ -12,8 +12,11 @@
******************************************************************************/
#include "RunCmdDlg.h"
+
#include <wx/gbsizer.h>
+
#include "tmCommand.h"
+#include "eSettings.h"
// control ids
enum
@@ -22,8 +25,29 @@ enum
};
-RunCmdDlg::RunCmdDlg(wxWindow *parent)
-: wxDialog (parent, -1, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER) {
+class CommandEvtHandler : public wxEvtHandler {
+public:
+ CommandEvtHandler(RunCmdDlg* parent): m_parent(parent) {};
+private:
+ void OnClose(wxCommandEvent& event);
+ DECLARE_EVENT_TABLE();
+
+ RunCmdDlg* m_parent;
+};
+
+BEGIN_EVENT_TABLE(CommandEvtHandler, wxEvtHandler)
+ EVT_BUTTON(wxID_OK, CommandEvtHandler::OnClose)
+END_EVENT_TABLE()
+
+void CommandEvtHandler::OnClose(wxCommandEvent& event){
+ m_parent->UpdateCommandHistory();
+ event.Skip();
+}
+
+RunCmdDlg::RunCmdDlg(wxWindow *parent, eSettings& settings):
+ wxDialog (parent, -1, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER),
+ m_settings(settings)
+{
SetTitle (_("Filter Through Command"));
wxGridBagSizer* gridBagSizer = new wxGridBagSizer(5,5);
@@ -32,6 +56,8 @@ RunCmdDlg::RunCmdDlg(wxWindow *parent)
gridBagSizer->Add(cmdLabel, wxGBPosition(0,0), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
m_cmdCtrl = new wxComboBox(this, COMMAND_BOX);
+ RefreshCommandHistory();
+
gridBagSizer->Add(m_cmdCtrl, wxGBPosition(0,1), wxGBSpan(1,2), wxGROW);
wxArrayString inputOptions;
@@ -56,11 +82,14 @@ RunCmdDlg::RunCmdDlg(wxWindow *parent)
gridBagSizer->Add(m_outputBox, wxGBPosition(1,2), wxDefaultSpan, wxGROW);
wxButton* cancelButton = new wxButton(this, wxID_CANCEL);
- wxButton* executeButton = new wxButton(this, wxID_OK, _("Execute"));
- executeButton->SetDefault();
+
+ m_executeButton = new wxButton(this, wxID_OK, _("Execute"));
+ m_executeButton->SetDefault();
+ m_executeButton->PushEventHandler(new CommandEvtHandler(this));
+
wxStdDialogButtonSizer* buttonSizer = new wxStdDialogButtonSizer;
buttonSizer->AddButton(cancelButton);
- buttonSizer->AddButton(executeButton);
+ buttonSizer->AddButton(m_executeButton);
buttonSizer->Realize();
gridBagSizer->Add(buttonSizer, wxGBPosition(2,1), wxGBSpan(1,2), wxALIGN_BOTTOM);
@@ -72,6 +101,10 @@ RunCmdDlg::RunCmdDlg(wxWindow *parent)
Centre();
}
+RunCmdDlg::~RunCmdDlg() {
+ m_executeButton->PopEventHandler(true);
+}
+
tmCommand RunCmdDlg::GetCommand() const {
tmCommand cmd;
@@ -124,3 +157,23 @@ tmCommand RunCmdDlg::GetCommand() const {
return cmd;
}
+
+void RunCmdDlg::RefreshCommandHistory() {
+ // Clear() also deletes the text in search box, so we have to cache it
+ const wxString repText = m_cmdCtrl->GetValue();
+ m_cmdCtrl->Clear();
+ m_cmdCtrl->SetValue(repText);
+
+ for (size_t i = 0; i < m_settings.GetFilterCommandHistoryCount(); i++) {
+ const wxString pattern = m_settings.GetFilterCommand(i);
+ m_cmdCtrl->Append(pattern);
+ }
+}
+
+void RunCmdDlg::UpdateCommandHistory() {
+ if (m_cmdCtrl->GetValue().IsEmpty()) return;
+
+ if (m_settings.AddFilterCommand(m_cmdCtrl->GetValue())) {
+ RefreshCommandHistory();
+ }
+}
View
@@ -20,14 +20,22 @@
#endif
class tmCommand;
+class eSettings;
class RunCmdDlg : public wxDialog {
public:
- RunCmdDlg(wxWindow *parent);
+ RunCmdDlg(wxWindow *parent, eSettings& settings);
+ virtual ~RunCmdDlg();
tmCommand GetCommand() const;
+ void UpdateCommandHistory();
+ void RefreshCommandHistory();
+
private:
+ eSettings& m_settings;
+
+ wxButton* m_executeButton;
wxComboBox* m_cmdCtrl;
wxRadioBox* m_inputBox;
wxRadioBox* m_outputBox;
View
@@ -603,3 +603,44 @@ bool eSettings::AddReplace(const wxString& pattern) {
return true;
}
+
+
+size_t eSettings::GetFilterCommandHistoryCount() const {
+ if (!m_jsonRoot.HasMember(wxT("filterCommandHistory"))) return 0;
+
+ const wxJSONValue values = m_jsonRoot.ItemAt(wxT("filterCommandHistory"));
+ return values.Size();
+}
+
+wxString eSettings::GetFilterCommand(size_t ndx) const {
+ const wxJSONValue values = m_jsonRoot.ItemAt(wxT("filterCommandHistory"));
+ wxASSERT((int)ndx < values.Size());
+
+ return values.ItemAt(ndx).AsString();
+}
+
+bool eSettings::AddFilterCommand(const wxString& command) {
+ wxJSONValue& values = m_jsonRoot[wxT("filterCommandHistory")];
+
+ // Don't add duplicates
+ if (values.Size() > 0) {
+ const wxJSONValue last = values.ItemAt(0);
+ if (last.AsString() == command) return false;
+
+ // Check if there should be a duplicate lower down
+ for (int i = 0; i < values.Size(); ++i) {
+ if (values[i].AsString() == command) {
+ values.Remove(i);
+ break;
+ }
+ }
+ }
+
+ // Add the new item
+ values.Insert(0, command);
+
+ // Limit number of items to save
+ if (values.Size() > 20) values.Remove(values.Size()-1);
+
+ return true;
+}
View
@@ -14,7 +14,11 @@
#ifndef __ESETTINGS_H__
#define __ESETTINGS_H__
-#include "wx/wxprec.h" // For compilers that support precompilation, includes "wx/wx.h".
+#include "wx/wxprec.h"
+#ifndef WX_PRECOMP
+ #include <wx/string.h>
+#endif
+
#include "jsonval.h"
#include "Catalyst.h"
#include "auto_vector.h"
@@ -70,10 +74,17 @@ class eSettings {
size_t GetSearchCount() const;
void GetSearch(size_t item, wxString& pattern, bool& isRegex, bool& matchCase) const;
bool AddSearch(const wxString& pattern, bool isRegex, bool matchCase);
+
+ // Replace History
size_t GetReplaceCount() const;
- wxString GetReplace(size_t item) const;
+ wxString GetReplace(size_t ndx) const;
bool AddReplace(const wxString& pattern);
+ // Filter through command History
+ size_t GetFilterCommandHistoryCount() const;
+ wxString GetFilterCommand(size_t ndx) const;
+ bool AddFilterCommand(const wxString& command);
+
private:
// Recent files (support functions)
void AddToRecent(const wxString& key, wxJSONValue& jsonArray, size_t max);

0 comments on commit 2be5f49

Please sign in to comment.