move searching to Menu class, also remember last constraint

this improvement also make that list of found items is not cleared
if one switches to another screen. it's remebered until the content
of screen changes (e.g. item is deleted from playlist)
This commit is contained in:
Andrzej Rybczak
2009-03-07 18:23:31 +01:00
parent c5f24f402f
commit b21ede24f4
11 changed files with 76 additions and 94 deletions

View File

@@ -74,7 +74,6 @@ void Browser::SwitchTo()
if (hasToBeResized)
Resize();
CLEAR_FIND_HISTORY;
w->Empty() ? myBrowser->GetDirectory(itsBrowsedDir) : myBrowser->UpdateItemList();
myScreen = this;
redraw_header = 1;
@@ -97,7 +96,6 @@ void Browser::EnterPressed()
{
case itDirectory:
{
CLEAR_FIND_HISTORY;
GetDirectory(item.name, itsBrowsedDir);
redraw_header = 1;
break;

View File

@@ -83,7 +83,6 @@ void Clock::SwitchTo()
if (hasToBeResized)
Resize();
CLEAR_FIND_HISTORY;
myScreen = this;
myPlaylist->Main()->Hide();
redraw_header = 1;

View File

@@ -53,9 +53,6 @@ namespace Global
extern bool redraw_header;
extern std::string volume_state;
extern std::vector<int> vFoundPositions;
extern int found_pos;
}
#endif

View File

@@ -112,7 +112,6 @@ void MediaLibrary::SwitchTo()
if (hasToBeResized)
Resize();
CLEAR_FIND_HISTORY;
myScreen = this;
redraw_header = 1;
Refresh();
@@ -128,7 +127,6 @@ void MediaLibrary::Update()
{
if (Artists->Empty())
{
CLEAR_FIND_HISTORY;
TagList list;
Albums->Clear(0);
Songs->Clear(0);
@@ -284,7 +282,6 @@ void MediaLibrary::GetSelectedSongs(MPD::SongList &v)
void MediaLibrary::NextColumn()
{
CLEAR_FIND_HISTORY;
if (w == Artists)
{
if (Songs->Empty())
@@ -307,7 +304,6 @@ void MediaLibrary::NextColumn()
void MediaLibrary::PrevColumn()
{
CLEAR_FIND_HISTORY;
if (w == Songs)
{
Songs->HighlightColor(Config.main_highlight_color);

View File

@@ -21,6 +21,8 @@
#ifndef _MENU_H
#define _MENU_H
#include <set>
#include "window.h"
#include "strbuffer.h"
#include "misc.h"
@@ -52,6 +54,11 @@ namespace NCurses
void ReverseSelection(size_t = 0);
bool Deselect();
virtual bool Search(const std::string &, size_t = 0, bool = 0) = 0;
virtual const std::string &GetSearchConstraint() = 0;
virtual void NextFound(bool) = 0;
virtual void PrevFound(bool) = 0;
virtual void ApplyFilter(const std::string &, size_t = 0, bool = 0) = 0;
virtual const std::string &GetFilter() = 0;
virtual std::string GetOption(size_t) = 0;
@@ -119,6 +126,11 @@ namespace NCurses
virtual size_t Choice() const;
virtual size_t RealChoice() const;
virtual bool Search(const std::string &constraint, size_t beginning = 0, bool case_sensitive = 0);
virtual const std::string &GetSearchConstraint() { return itsSearchConstraint; }
virtual void NextFound(bool wrap);
virtual void PrevFound(bool wrap);
virtual void ApplyFilter(const std::string &filter, size_t beginning = 0, bool case_sensitive = 0);
virtual const std::string &GetFilter();
virtual std::string GetOption(size_t pos);
@@ -170,11 +182,13 @@ namespace NCurses
void *itsGetStringFunctionUserData;
std::string itsFilter;
std::string itsSearchConstraint;
std::vector<Option *> *itsOptionsPtr;
std::vector<Option *> itsOptions;
std::vector<Option *> itsFilteredOptions;
std::vector<size_t> itsFilteredRealPositions;
std::set<size_t> itsFound;
int itsBeginning;
int itsHighlight;
@@ -279,6 +293,7 @@ template <class T> void NCurses::Menu<T>::DeleteOption(size_t pos)
delete itsOptions.at(pos);
itsOptions.erase(itsOptions.begin()+pos);
}
itsFound.clear();
if (itsOptionsPtr->empty())
Window::Clear();
}
@@ -490,6 +505,7 @@ template <class T> void NCurses::Menu<T>::Clear(bool clrscr)
for (option_iterator it = itsOptions.begin(); it != itsOptions.end(); it++)
delete *it;
itsOptions.clear();
itsFound.clear();
ClearFiltered();
itsOptionsPtr = &itsOptions;
if (clrscr)
@@ -574,10 +590,55 @@ template <class T> size_t NCurses::Menu<T>::RealChoice() const
return result;
}
template <class T> bool NCurses::Menu<T>::Search(const std::string &constraint, size_t beginning, bool case_sensitive)
{
itsFound.clear();
if (constraint.empty())
return false;
itsSearchConstraint = constraint;
std::string option;
for (size_t i = beginning; i < itsOptionsPtr->size(); i++)
{
option = GetOption(i);
if (!case_sensitive)
ToLower(option);
if (option.find(itsSearchConstraint) != std::string::npos)
itsFound.insert(i);
}
return !itsFound.empty();
}
template <class T> void NCurses::Menu<T>::NextFound(bool wrap)
{
if (itsFound.empty())
return;
std::set<size_t>::iterator next = itsFound.upper_bound(itsHighlight);
if (next != itsFound.end())
Highlight(*next);
else if (wrap)
Highlight(*itsFound.begin());
}
template <class T> void NCurses::Menu<T>::PrevFound(bool wrap)
{
if (itsFound.empty())
return;
std::set<size_t>::iterator prev = itsFound.lower_bound(itsHighlight);
if (prev != itsFound.begin())
{
if (*prev == size_t(itsHighlight))
prev--;
Highlight(*prev);
}
else if (wrap)
Highlight(*itsFound.rbegin());
}
template <class T> void NCurses::Menu<T>::ApplyFilter(const std::string &filter, size_t beginning, bool case_sensitive)
{
if (filter == itsFilter)
return;
itsFound.clear();
ClearFiltered();
itsFilter = filter;
if (!case_sensitive)

View File

@@ -79,9 +79,6 @@ bool Global::block_item_list_update = 0;
bool Global::messages_allowed = 0;
bool Global::redraw_header = 1;
vector<int> Global::vFoundPositions;
int Global::found_pos = 0;
int main(int argc, char *argv[])
{
CreateConfigDir();
@@ -1462,8 +1459,6 @@ int main(int argc, char *argv[])
if (!mList)
continue;
CLEAR_FIND_HISTORY;
LockStatusbar();
Statusbar() << fmtBold << "Apply filter: " << fmtBoldEnd;
wFooter->SetGetStringHelper(StatusbarApplyFilterImmediately);
@@ -1491,83 +1486,36 @@ int main(int argc, char *argv[])
if (!mList)
continue;
string how = Keypressed(input, Key.FindForward) ? "forward" : "backward";
LockStatusbar();
Statusbar() << "Find " << how << ": ";
string findme = wFooter->GetString();
Statusbar() << "Find " << (Keypressed(input, Key.FindForward) ? "forward" : "backward") << ": ";
string findme = wFooter->GetString(mList->GetSearchConstraint());
UnlockStatusbar();
time(&timer);
if (findme.empty())
continue;
ToLower(findme);
CLEAR_FIND_HISTORY;
ShowMessage("Searching...");
for (size_t i = (myScreen == mySearcher ? SearchEngine::StaticOptions : 0); i < mList->Size(); i++)
{
string name = mList->GetOption(i);
ToLower(name);
if (name.find(findme) != string::npos && !mList->isStatic(i))
{
vFoundPositions.push_back(i);
if (Keypressed(input, Key.FindForward)) // forward
{
if (found_pos < 0 && i >= mList->Choice())
found_pos = vFoundPositions.size()-1;
}
else // backward
{
if (i <= mList->Choice())
found_pos = vFoundPositions.size()-1;
}
}
}
if (mList->Search(findme, myScreen == mySearcher ? SearchEngine::StaticOptions : 0))
ShowMessage("Searching finished!");
if (Config.wrapped_search ? vFoundPositions.empty() : found_pos < 0)
ShowMessage("Unable to find \"%s\"", findme.c_str());
else
{
mList->Highlight(vFoundPositions[found_pos < 0 ? 0 : found_pos]);
if (myScreen == myPlaylist)
{
time(&timer);
myPlaylist->Main()->Highlighting(1);
}
}
ShowMessage("Unable to find \"%s\"", findme.c_str());
if (Keypressed(input, Key.FindForward))
mList->NextFound(Config.wrapped_search);
else
mList->PrevFound(Config.wrapped_search);
}
else if (Keypressed(input, Key.NextFoundPosition) || Keypressed(input, Key.PrevFoundPosition))
{
List *mList = myScreen->GetList();
if (!mList || vFoundPositions.empty())
if (!mList)
continue;
bool next = Keypressed(input, Key.NextFoundPosition);
try
{
mList->Highlight(vFoundPositions.at(next ? ++found_pos : --found_pos));
}
catch (std::out_of_range)
{
if (Config.wrapped_search)
{
if (next)
{
mList->Highlight(vFoundPositions.front());
found_pos = 0;
}
else // prev
{
mList->Highlight(vFoundPositions.back());
found_pos = vFoundPositions.size()-1;
}
}
if (Keypressed(input, Key.NextFoundPosition))
mList->NextFound(Config.wrapped_search);
else
found_pos = next ? vFoundPositions.size()-1 : 0;
}
mList->PrevFound(Config.wrapped_search);
}
else if (Keypressed(input, Key.ToggleFindMode))
{

View File

@@ -27,12 +27,6 @@
#include "menu.h"
#include "scrollpad.h"
#define CLEAR_FIND_HISTORY \
do { \
found_pos = -1; \
vFoundPositions.clear(); \
} while (0)
using namespace NCurses;
typedef std::pair<std::string, std::string> string_pair;

View File

@@ -89,7 +89,6 @@ void Playlist::SwitchTo()
if (hasToBeResized)
Resize();
CLEAR_FIND_HISTORY;
myScreen = this;
w->Window::Clear();
redraw_header = 1;

View File

@@ -98,7 +98,6 @@ void PlaylistEditor::SwitchTo()
if (hasToBeResized)
Resize();
CLEAR_FIND_HISTORY;
myScreen = this;
redraw_header = 1;
Refresh();
@@ -168,7 +167,6 @@ void PlaylistEditor::NextColumn()
{
if (w == Playlists)
{
CLEAR_FIND_HISTORY;
Playlists->HighlightColor(Config.main_highlight_color);
w->Refresh();
w = Content;
@@ -180,7 +178,6 @@ void PlaylistEditor::PrevColumn()
{
if (w == Content)
{
CLEAR_FIND_HISTORY;
Content->HighlightColor(Config.main_highlight_color);
w->Refresh();
w = Playlists;

View File

@@ -67,7 +67,6 @@ void SearchEngine::SwitchTo()
if (hasToBeResized)
Resize();
CLEAR_FIND_HISTORY;
if (w->Empty())
Prepare();
myScreen = this;
@@ -214,7 +213,6 @@ void SearchEngine::EnterPressed()
}
case 16:
{
CLEAR_FIND_HISTORY;
itsPattern.Clear();
w->Reset();
Prepare();

View File

@@ -366,8 +366,6 @@ void TagEditor::SwitchTo()
if (hasToBeResized)
Resize();
CLEAR_FIND_HISTORY;
myScreen = this;
redraw_header = 1;
Refresh();
@@ -410,7 +408,6 @@ void TagEditor::Update()
{
if (LeftColumn->Empty())
{
CLEAR_FIND_HISTORY;
LeftColumn->Window::Clear();
Tags->Clear();
TagList list;
@@ -763,7 +760,6 @@ List *TagEditor::GetList()
void TagEditor::NextColumn()
{
CLEAR_FIND_HISTORY;
if (w == LeftColumn)
{
LeftColumn->HighlightColor(Config.main_highlight_color);
@@ -782,7 +778,6 @@ void TagEditor::NextColumn()
void TagEditor::PrevColumn()
{
CLEAR_FIND_HISTORY;
if (w == Tags)
{
Tags->HighlightColor(Config.main_highlight_color);