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-02-04 09:38:32 +00:00
|
|
|
|
|
|
|
|
|
extern BufferList bufferlist;
|
|
|
|
|
|
|
|
|
|
class text_fits {
|
|
|
|
|
public:
|
|
|
|
|
text_fits(Buffer * b, unsigned short p)
|
|
|
|
|
: buf(b), pw(p) {}
|
|
|
|
|
bool operator()(TextCache::value_type & vt) {
|
2000-06-08 23:16:16 +00:00
|
|
|
|
if (vt->buffer() == buf && vt->paperWidth() == pw) return true;
|
2000-02-04 09:38:32 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
Buffer * buf;
|
|
|
|
|
unsigned short pw;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LyXText * TextCache::findFit(Buffer * b, unsigned short p)
|
|
|
|
|
{
|
|
|
|
|
Cache::iterator it = find_if(cache.begin(), cache.end(),
|
|
|
|
|
text_fits(b, p));
|
|
|
|
|
if (it != cache.end()) {
|
|
|
|
|
LyXText * tmp = (*it);
|
|
|
|
|
cache.erase(it);
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class show_text {
|
|
|
|
|
public:
|
|
|
|
|
show_text(ostream & o) : os(o) {}
|
|
|
|
|
void operator()(TextCache::value_type & vt) {
|
2000-06-08 23:16:16 +00:00
|
|
|
|
os << "\tBuffer: " << vt->buffer()
|
2000-02-04 09:38:32 +00:00
|
|
|
|
<< "\tWidth: " << vt->paperWidth() << endl;
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
ostream & os;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void TextCache::show(ostream & os, string const & str)
|
|
|
|
|
{
|
|
|
|
|
os << "TextCache: " << str << endl;
|
|
|
|
|
for_each(cache.begin(), cache.end(), show_text(os));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2000-02-07 20:17:03 +00:00
|
|
|
|
void TextCache::show(ostream & os, LyXText * lt)
|
2000-02-04 09:38:32 +00:00
|
|
|
|
{
|
|
|
|
|
show_text st(os);
|
2000-02-07 20:17:03 +00:00
|
|
|
|
st(lt);
|
2000-02-04 09:38:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void TextCache::add(LyXText * text)
|
|
|
|
|
{
|
|
|
|
|
lyxerr.debug() << "TextCache::add " << text;
|
2000-06-08 23:16:16 +00:00
|
|
|
|
if (bufferlist.isLoaded(text->buffer())) {
|
2000-02-04 09:38:32 +00:00
|
|
|
|
cache.push_back(text);
|
|
|
|
|
lyxerr.debug() << " added" << endl;
|
|
|
|
|
} else {
|
|
|
|
|
delete text;
|
|
|
|
|
lyxerr.debug() << " deleted" << endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class delete_text {
|
|
|
|
|
public:
|
|
|
|
|
void operator()(TextCache::value_type & vt) {
|
|
|
|
|
delete vt;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void TextCache::clear()
|
|
|
|
|
{
|
|
|
|
|
for_each(cache.begin(), cache.end(), delete_text());
|
|
|
|
|
cache.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class has_buffer {
|
|
|
|
|
public:
|
|
|
|
|
has_buffer(Buffer * b)
|
|
|
|
|
: buf(b) {}
|
|
|
|
|
bool operator()(TextCache::value_type & vt) {
|
2000-06-08 23:16:16 +00:00
|
|
|
|
if (vt->buffer() == buf) return true;
|
2000-02-04 09:38:32 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
Buffer * buf;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void TextCache::removeAllWithBuffer(Buffer * buf)
|
|
|
|
|
{
|
|
|
|
|
Cache::iterator it = remove_if(cache.begin(), cache.end(),
|
|
|
|
|
has_buffer(buf));
|
|
|
|
|
if (it != cache.end()) {
|
|
|
|
|
if (lyxerr.debugging()) {
|
|
|
|
|
lyxerr.debug() << "TextCache::removeAllWithbuffer "
|
|
|
|
|
"Removing:\n";
|
|
|
|
|
for_each(it, cache.end(), show_text(lyxerr));
|
|
|
|
|
lyxerr << endl;
|
|
|
|
|
}
|
|
|
|
|
for_each(it, cache.end(), delete_text());
|
|
|
|
|
cache.erase(it, cache.end());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Global instance
|
|
|
|
|
TextCache textcache;
|