2000-02-04 09:38:32 +00:00
|
|
|
|
/* This file is part of
|
|
|
|
|
* ======================================================
|
|
|
|
|
*
|
|
|
|
|
* LyX, The Document Processor
|
|
|
|
|
* Copyright 1995 Matthias Ettrich
|
|
|
|
|
* Copyright 1995-2000 The LyX Team
|
|
|
|
|
*
|
|
|
|
|
* This file is Copyright 2000
|
|
|
|
|
* Lars Gullik Bj<EFBFBD>nnes
|
|
|
|
|
*
|
|
|
|
|
* ====================================================== */
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
#ifdef __GNUG__
|
|
|
|
|
#pragma implementation
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
#include "TextCache.h"
|
|
|
|
|
#include "buffer.h"
|
|
|
|
|
#include "bufferlist.h"
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
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()) {
|
2000-06-12 11:27:15 +00:00
|
|
|
|
LyXText * tmp = (*it).second.second;
|
2000-02-04 09:38:32 +00:00
|
|
|
|
cache.erase(it);
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class show_text {
|
|
|
|
|
public:
|
|
|
|
|
show_text(ostream & o) : os(o) {}
|
2000-06-12 11:27:15 +00:00
|
|
|
|
void operator()(TextCache::value_type const & vt) {
|
|
|
|
|
os << "\tBuffer: " << vt.first
|
|
|
|
|
<< "\tWidth: " << vt.second.first << endl;
|
2000-02-04 09:38:32 +00:00
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
ostream & os;
|
|
|
|
|
};
|
|
|
|
|
|
2000-07-15 23:51:46 +00:00
|
|
|
|
|
2000-02-04 09:38:32 +00:00
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
lyxerr.debug() << "TextCache::add " << text;
|
2000-06-12 11:27:15 +00:00
|
|
|
|
if (bufferlist.isLoaded(buf)) {
|
|
|
|
|
cache[buf] = make_pair(workwidth, text);
|
2000-02-04 09:38:32 +00:00
|
|
|
|
lyxerr.debug() << " added" << endl;
|
|
|
|
|
} else {
|
|
|
|
|
delete text;
|
|
|
|
|
lyxerr.debug() << " deleted" << endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class delete_text {
|
|
|
|
|
public:
|
|
|
|
|
void operator()(TextCache::value_type & vt) {
|
2000-06-12 11:27:15 +00:00
|
|
|
|
delete vt.second.second;
|
2000-02-04 09:38:32 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void TextCache::clear()
|
|
|
|
|
{
|
|
|
|
|
for_each(cache.begin(), cache.end(), delete_text());
|
|
|
|
|
cache.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class has_buffer {
|
|
|
|
|
public:
|
|
|
|
|
has_buffer(Buffer * b)
|
|
|
|
|
: buf(b) {}
|
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) return true;
|
2000-02-04 09:38:32 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
private:
|
2000-06-12 11:27:15 +00:00
|
|
|
|
Buffer const * buf;
|
2000-02-04 09:38:32 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|