]> git.lyx.org Git - lyx.git/blob - src/TextCache.C
several small and larger changes, read the Changelog
[lyx.git] / src / TextCache.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor         
5  *           Copyright 1995 Matthias Ettrich
6  *           Copyright 1995-2000 The LyX Team
7  *
8  *           This file is Copyright 2000
9  *           Lars Gullik Bjønnes
10  *
11  * ====================================================== */
12
13 #include <config.h>
14
15 #ifdef __GNUG__
16 #pragma implementation
17 #endif
18
19 #include <algorithm>
20
21 #include "TextCache.h"
22 #include "buffer.h"
23 #include "bufferlist.h"
24
25 using std::for_each;
26 using std::remove_if;
27 using std::find_if;
28
29 extern BufferList bufferlist;
30
31 class text_fits {
32 public:
33         text_fits(Buffer * b, unsigned short p)
34                 : buf(b), pw(p) {}
35         bool operator()(TextCache::value_type & vt) {
36                 if (vt->params == buf && vt->paperWidth() == pw) return true;
37                 return false;
38         }
39 private:
40         Buffer * buf;
41         unsigned short pw;
42 };
43
44
45 LyXText * TextCache::findFit(Buffer * b, unsigned short p)
46 {
47         Cache::iterator it = find_if(cache.begin(), cache.end(),
48                                      text_fits(b, p));
49         if (it != cache.end()) {
50                 LyXText * tmp = (*it);
51                 cache.erase(it);
52                 return tmp;
53         }
54         return 0;
55 }
56
57
58 class show_text {
59 public:
60         show_text(ostream & o) : os(o) {}
61         void operator()(TextCache::value_type & vt) {
62                 os << "\tBuffer: " << vt->params
63                    << "\tWidth: " << vt->paperWidth() << endl;
64         }
65 private:
66         ostream & os;
67 };
68
69 void TextCache::show(ostream & os, string const & str)
70 {
71         os << "TextCache: " << str << endl;
72         for_each(cache.begin(), cache.end(), show_text(os));
73 }
74
75
76 void TextCache::show(ostream & os, LyXText const * lt)
77 {
78         show_text st(os);
79         st(const_cast<LyXText*>(lt));
80 }
81
82
83 void TextCache::add(LyXText * text)
84 {
85         lyxerr.debug() << "TextCache::add " << text;
86         if (bufferlist.isLoaded(text->params)) {
87                 cache.push_back(text);
88                 lyxerr.debug() << " added" << endl;
89         } else {
90                 delete text;
91                 lyxerr.debug() << " deleted" << endl;
92         }
93 }
94
95
96 class delete_text {
97 public:
98         void operator()(TextCache::value_type & vt) {
99                 delete vt;
100         }
101 };
102
103
104 void TextCache::clear()
105 {
106         for_each(cache.begin(), cache.end(), delete_text());
107         cache.clear();
108 }
109
110
111 class has_buffer {
112 public:
113         has_buffer(Buffer * b)
114                 : buf(b) {}
115         bool operator()(TextCache::value_type & vt) {
116                 if (vt->params == buf) return true;
117                 return false;
118         }
119 private:
120         Buffer * buf;
121 };
122
123
124 void TextCache::removeAllWithBuffer(Buffer * buf)
125 {
126         Cache::iterator it = remove_if(cache.begin(), cache.end(),
127                                        has_buffer(buf));
128         if (it != cache.end()) {
129                 if (lyxerr.debugging()) {
130                         lyxerr.debug() << "TextCache::removeAllWithbuffer "
131                                 "Removing:\n";
132                         for_each(it, cache.end(), show_text(lyxerr));
133                         lyxerr << endl;
134                 }
135                 for_each(it, cache.end(), delete_text());
136                 cache.erase(it, cache.end());
137         }
138 }
139
140 // Global instance
141 TextCache textcache;