1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
#include "GotoAnythingDlg.h"
#include "bitmap_loader.h"
#include "clAnagram.h"
#include "clKeyboardManager.h"
#include "cl_config.h"
#include "codelite_events.h"
#include "event_notifier.h"
#include "file_logger.h"
#include "globals.h"
#include "imanager.h"
#include "macros.h"
#include "windowattrmanager.h"
#include <algorithm>
#include <wx/app.h>
GotoAnythingDlg::GotoAnythingDlg(wxWindow* parent, const std::vector<clGotoEntry>& entries)
: GotoAnythingBaseDlg(parent)
, m_allEntries(entries)
{
DoPopulate(m_allEntries);
CallAfter(&GotoAnythingDlg::UpdateLastSearch);
WindowAttrManager::Load(this);
}
GotoAnythingDlg::~GotoAnythingDlg()
{
// clConfig::Get().Write("GotoAnything/LastSearch", m_textCtrlSearch->GetValue());
}
void GotoAnythingDlg::OnKeyDown(wxKeyEvent& event)
{
event.Skip();
if(event.GetKeyCode() == WXK_ESCAPE) {
event.Skip(false);
EndModal(wxID_CANCEL);
} else if(event.GetKeyCode() == WXK_DOWN) {
event.Skip(false);
int row = m_dvListCtrl->GetSelectedRow();
if((row + 1) < m_dvListCtrl->GetItemCount()) {
row++;
DoSelectItem(m_dvListCtrl->RowToItem(row));
}
} else if(event.GetKeyCode() == WXK_UP) {
event.Skip(false);
int row = m_dvListCtrl->GetSelectedRow();
if((row - 1) >= 0) {
row--;
DoSelectItem(m_dvListCtrl->RowToItem(row));
}
}
}
void GotoAnythingDlg::OnEnter(wxCommandEvent& event)
{
wxUnusedVar(event);
DoExecuteActionAndClose();
}
void GotoAnythingDlg::DoPopulate(const std::vector<clGotoEntry>& entries, const std::vector<int>& indexes)
{
m_dvListCtrl->DeleteAllItems();
static wxBitmap placeHolderBmp = clGetManager()->GetStdIcons()->LoadBitmap("placeholder");
for(size_t i = 0; i < entries.size(); ++i) {
const clGotoEntry& entry = entries[i];
wxVector<wxVariant> cols;
cols.push_back(::MakeIconText(entry.GetDesc(), entry.GetBitmap().IsOk() ? entry.GetBitmap() : placeHolderBmp));
cols.push_back(entry.GetKeyboardShortcut());
m_dvListCtrl->AppendItem(cols, indexes.empty() ? i : indexes[i]);
}
if(!entries.empty()) { m_dvListCtrl->SelectRow(0); }
}
void GotoAnythingDlg::DoExecuteActionAndClose()
{
int row = m_dvListCtrl->GetSelectedRow();
if(row == wxNOT_FOUND) return;
// Execute the action
int index = m_dvListCtrl->GetItemData(m_dvListCtrl->RowToItem(row));
const clGotoEntry& entry = m_allEntries[index];
clDEBUG() << "GotoAnythingDlg: action selected:" << entry.GetDesc() << clEndl;
clGotoEvent evtAction(wxEVT_GOTO_ANYTHING_SELECTED);
evtAction.SetEntry(entry);
EventNotifier::Get()->AddPendingEvent(evtAction);
EndModal(wxID_OK);
}
void GotoAnythingDlg::OnIdle(wxIdleEvent& e)
{
e.Skip();
ApplyFilter();
}
void GotoAnythingDlg::UpdateLastSearch() {}
void GotoAnythingDlg::ApplyFilter()
{
// Create a list the matches the typed text
wxString filter = m_textCtrlSearch->GetValue();
if(m_currentFilter == filter) return;
// Update the last applied filter
m_currentFilter = filter;
if(filter.IsEmpty()) {
DoPopulate(m_allEntries);
} else {
// Filter the list
clAnagram anagram(filter);
std::vector<clGotoEntry> matchedEntries;
std::vector<int> matchedEntriesIndex;
for(size_t i = 0; i < m_allEntries.size(); ++i) {
const clGotoEntry& entry = m_allEntries[i];
if(anagram.MatchesInOrder(entry.GetDesc())) {
matchedEntries.push_back(entry);
matchedEntriesIndex.push_back(i);
}
}
// And populate the list
DoPopulate(matchedEntries, matchedEntriesIndex);
}
}
void GotoAnythingDlg::OnItemActivated(wxDataViewEvent& event)
{
wxUnusedVar(event);
DoExecuteActionAndClose();
}
void GotoAnythingDlg::DoSelectItem(const wxDataViewItem& item)
{
CHECK_ITEM_RET(item);
m_dvListCtrl->UnselectAll();
m_dvListCtrl->Select(item);
m_dvListCtrl->EnsureVisible(item);
}
|