]> git.lyx.org Git - lyx.git/blob - src/TextCache.C
"Inter-word Space"
[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-2001 The LyX Team
7  *
8  *           This file is Copyright 2000-2001
9  *           Lars Gullik Bjønnes
10  *
11  * ====================================================== */
12
13 #include <config.h>
14
15 #include "TextCache.h"
16 #include "buffer.h"
17 #include "lyxtext.h"
18 #include "bufferlist.h"
19 #include "debug.h"
20
21 #include <algorithm>
22
23 using std::ostream;
24 using std::for_each;
25 using std::remove_if;
26 using std::find_if;
27 using std::endl;
28 using std::make_pair;
29
30 extern BufferList bufferlist;
31
32 namespace {
33
34 class text_fits {
35 public:
36         text_fits(Buffer * b, int p)
37                 : buf(b), pw(p) {}
38         bool operator()(TextCache::value_type const & vt) const {
39                 if (vt.first == buf && vt.second.first == pw)
40                         return true;
41                 return false;
42         }
43 private:
44         Buffer * buf;
45         int pw;
46 };
47
48
49 class show_text {
50 public:
51         show_text(ostream & o) : os(o) {}
52         void operator()(TextCache::value_type const & vt) {
53                 os << "\tBuffer: " << vt.first
54                    << "\tWidth: " << vt.second.first << endl;
55         }
56 private:
57         ostream & os;
58 };
59
60
61 class delete_text {
62 public:
63         void operator()(TextCache::value_type & vt) {
64                 delete vt.second.second;
65         }
66 };
67
68 } // namespace anon
69
70
71 LyXText * TextCache::findFit(Buffer * b, int p)
72 {
73         Cache::iterator it = find_if(cache.begin(), cache.end(),
74                                      text_fits(b, p));
75         if (it != cache.end()) {
76                 LyXText * tmp = it->second.second;
77                 cache.erase(it);
78                 return tmp;
79         }
80         return 0;
81 }
82
83
84 void TextCache::show(ostream & os, string const & str)
85 {
86         os << "TextCache: " << str << endl;
87         for_each(cache.begin(), cache.end(), show_text(os));
88 }
89
90
91 void TextCache::show(ostream & os, TextCache::value_type const & vt)
92 {
93         show_text st(os);
94         st(vt);
95 }
96
97
98 void TextCache::add(Buffer * buf, int workwidth, LyXText * text)
99 {
100         lyxerr[Debug::INFO] << "TextCache::add " << text;
101         if (bufferlist.isLoaded(buf)) {
102                 cache[buf] = make_pair(workwidth, text);
103                 lyxerr[Debug::INFO] << " added" << endl;
104         } else {
105                 delete text;
106                 lyxerr[Debug::INFO] << " deleted" << endl;
107         }
108 }
109
110
111 void TextCache::clear()
112 {
113         for_each(cache.begin(), cache.end(), delete_text());
114         cache.clear();
115 }
116
117
118 void TextCache::removeAllWithBuffer(Buffer * buf)
119 {
120         cache.erase(buf);
121 }
122
123 // Global instance
124 TextCache textcache;