2000-02-04 09:38:32 +00:00
|
|
|
|
/* This file is part of
|
|
|
|
|
* ======================================================
|
|
|
|
|
*
|
|
|
|
|
* LyX, The Document Processor
|
|
|
|
|
* Copyright 1995 Matthias Ettrich
|
2001-04-04 21:37:14 +00:00
|
|
|
|
* Copyright 1995-2001 The LyX Team
|
2000-02-04 09:38:32 +00:00
|
|
|
|
*
|
2001-04-04 21:37:14 +00:00
|
|
|
|
* This file is Copyright 2000-2001
|
2000-02-04 09:38:32 +00:00
|
|
|
|
* Lars Gullik Bj<EFBFBD>nnes
|
|
|
|
|
*
|
|
|
|
|
* ====================================================== */
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
#ifdef __GNUG__
|
|
|
|
|
#pragma implementation
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
#include "TextCache.h"
|
|
|
|
|
#include "buffer.h"
|
|
|
|
|
#include "bufferlist.h"
|
2001-07-29 15:34:18 +00:00
|
|
|
|
#include "debug.h"
|
2000-02-04 09:38:32 +00:00
|
|
|
|
|
2000-04-04 00:19:15 +00:00
|
|
|
|
using std::ostream;
|
2000-02-04 09:38:32 +00:00
|
|
|
|
using std::for_each;
|
|
|
|
|
using std::remove_if;
|
|
|
|
|
using std::find_if;
|
2000-03-28 02:18:55 +00:00
|
|
|
|
using std::endl;
|
2000-06-12 11:27:15 +00:00
|
|
|
|
using std::make_pair;
|
2000-02-04 09:38:32 +00:00
|
|
|
|
|
|
|
|
|
extern BufferList bufferlist;
|
|
|
|
|
|
2001-04-04 21:37:14 +00:00
|
|
|
|
namespace {
|
|
|
|
|
|
2000-02-04 09:38:32 +00:00
|
|
|
|
class text_fits {
|
|
|
|
|
public:
|
2000-06-12 11:27:15 +00:00
|
|
|
|
text_fits(Buffer * b, int p)
|
2000-02-04 09:38:32 +00:00
|
|
|
|
: buf(b), pw(p) {}
|
2000-07-15 23:51:46 +00:00
|
|
|
|
bool operator()(TextCache::value_type const & vt) const {
|
2000-06-12 11:27:15 +00:00
|
|
|
|
if (vt.first == buf && vt.second.first == pw)
|
|
|
|
|
return true;
|
2000-02-04 09:38:32 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
Buffer * buf;
|
2000-06-12 11:27:15 +00:00
|
|
|
|
int pw;
|
2000-02-04 09:38:32 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2001-04-04 21:37:14 +00:00
|
|
|
|
class show_text {
|
|
|
|
|
public:
|
|
|
|
|
show_text(ostream & o) : os(o) {}
|
|
|
|
|
void operator()(TextCache::value_type const & vt) {
|
|
|
|
|
os << "\tBuffer: " << vt.first
|
|
|
|
|
<< "\tWidth: " << vt.second.first << endl;
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
ostream & os;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class delete_text {
|
|
|
|
|
public:
|
|
|
|
|
void operator()(TextCache::value_type & vt) {
|
|
|
|
|
delete vt.second.second;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace anon
|
|
|
|
|
|
|
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
|
LyXText * TextCache::findFit(Buffer * b, int p)
|
2000-02-04 09:38:32 +00:00
|
|
|
|
{
|
|
|
|
|
Cache::iterator it = find_if(cache.begin(), cache.end(),
|
|
|
|
|
text_fits(b, p));
|
|
|
|
|
if (it != cache.end()) {
|
2001-07-12 11:11:10 +00:00
|
|
|
|
LyXText * tmp = it->second.second;
|
2000-02-04 09:38:32 +00:00
|
|
|
|
cache.erase(it);
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void TextCache::show(ostream & os, string const & str)
|
|
|
|
|
{
|
|
|
|
|
os << "TextCache: " << str << endl;
|
|
|
|
|
for_each(cache.begin(), cache.end(), show_text(os));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
|
void TextCache::show(ostream & os, TextCache::value_type const & vt)
|
2000-02-04 09:38:32 +00:00
|
|
|
|
{
|
|
|
|
|
show_text st(os);
|
2000-06-12 11:27:15 +00:00
|
|
|
|
st(vt);
|
2000-02-04 09:38:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2000-07-15 23:51:46 +00:00
|
|
|
|
void TextCache::add(Buffer * buf, int workwidth, LyXText * text)
|
2000-02-04 09:38:32 +00:00
|
|
|
|
{
|
2001-06-27 14:10:35 +00:00
|
|
|
|
lyxerr[Debug::INFO] << "TextCache::add " << text;
|
2000-06-12 11:27:15 +00:00
|
|
|
|
if (bufferlist.isLoaded(buf)) {
|
|
|
|
|
cache[buf] = make_pair(workwidth, text);
|
2001-06-27 14:10:35 +00:00
|
|
|
|
lyxerr[Debug::INFO] << " added" << endl;
|
2000-02-04 09:38:32 +00:00
|
|
|
|
} else {
|
|
|
|
|
delete text;
|
2001-06-27 14:10:35 +00:00
|
|
|
|
lyxerr[Debug::INFO] << " deleted" << endl;
|
2000-02-04 09:38:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void TextCache::clear()
|
|
|
|
|
{
|
|
|
|
|
for_each(cache.begin(), cache.end(), delete_text());
|
|
|
|
|
cache.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void TextCache::removeAllWithBuffer(Buffer * buf)
|
|
|
|
|
{
|
2000-06-12 11:27:15 +00:00
|
|
|
|
cache.erase(buf);
|
2000-02-04 09:38:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Global instance
|
|
|
|
|
TextCache textcache;
|