]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsCache.C
New lyx_gui namespace. Some working out to be done still ..
[lyx.git] / src / graphics / GraphicsCache.C
1 /*
2  * \file GraphicsCache.C
3  * Copyright 2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author Baruch Even <baruch.even@writeme.com>
7  * \author Angus Leeming <a.leeming@ic.ac.uk>
8  */
9
10 #include <config.h>
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 #include "GraphicsCache.h"
17 #include "GraphicsCacheItem.h"
18 #include "GraphicsImage.h"
19 #include "GraphicsParams.h"
20 #include "insets/insetgraphics.h"
21 #include "frontends/lyx_gui.h"
22
23 namespace grfx {
24
25 GCache & GCache::get()
26 {
27         static bool start = true;
28         if (start) {
29                 start = false;
30                 lyx_gui::init_graphics();
31         }
32
33         // Now return the cache
34         static GCache singleton;
35         return singleton;
36 }
37
38
39 GCache::GCache()
40 {
41         cache = new CacheType;
42 }
43
44
45 // all elements are destroyed by the shared_ptr's in the map.
46 GCache::~GCache()
47 {
48         delete cache;
49 }
50
51
52 void GCache::update(InsetGraphics const & inset, string const & filepath)
53 {
54         // A subset only of InsetGraphicsParams is needed for display purposes.
55         // The GraphicsParams c-tor also interrogates lyxrc to ascertain whether
56         // to display or not.
57         GParams params = inset.params().asGParams(filepath);
58
59         // Each inset can reference only one file, so check the cache for any
60         // graphics files referenced by inset. If the name of this file is
61         // different from that in params, then remove the reference.
62         CacheType::iterator it = find(inset);
63
64         if (it != cache->end()) {
65                 CacheItemType item = it->second;
66                 if (item->filename() != params.filename) {
67                         item->remove(inset);
68                         if (item->empty())
69                                 cache->erase(it);
70                 }
71         }
72
73         // Are we adding a new file or modifying the display of an existing one?
74         it = cache->find(params.filename);
75
76         if (it != cache->end()) {
77                 it->second->modify(inset, params);
78                 return;
79         }
80
81         CacheItemType item(new GCacheItem(inset, params));
82         if (item.get() != 0)
83                 (*cache)[params.filename] = item;
84 }
85
86
87 void GCache::remove(InsetGraphics const & inset)
88 {
89         CacheType::iterator it = find(inset);
90         if (it == cache->end())
91                 return;
92
93         CacheItemType item = it->second;
94         item->remove(inset);
95         if (item->empty()) {
96                 cache->erase(it);
97         }
98 }
99
100
101 void GCache::startLoading(InsetGraphics const & inset)
102 {
103         CacheType::iterator it = find(inset);
104         if (it == cache->end())
105                 return;
106
107         it->second->startLoading(inset);
108 }
109
110
111 ImagePtr const GCache::image(InsetGraphics const & inset) const
112 {
113         CacheType::const_iterator it = find(inset);
114         if (it == cache->end())
115                 return ImagePtr();
116
117         return it->second->image(inset);
118 }
119
120
121 ImageStatus GCache::status(InsetGraphics const & inset) const
122 {
123         CacheType::const_iterator it = find(inset);
124         if (it == cache->end())
125                 return ErrorUnknown;
126
127         return it->second->status(inset);
128 }
129
130
131 void GCache::changeDisplay(bool changed_background)
132 {
133         CacheType::iterator it = cache->begin();
134         CacheType::iterator end = cache->end();
135         for(; it != end; ++it)
136                 it->second->changeDisplay(changed_background);
137 }
138
139
140 GCache::CacheType::iterator
141 GCache::find(InsetGraphics const & inset)
142 {
143         CacheType::iterator it  = cache->begin();
144         CacheType::iterator end = cache->end();
145         for (; it != end; ++it) {
146                 if (it->second->referencedBy(inset))
147                         return it;
148         }
149
150         return cache->end();
151 }
152
153
154 GCache::CacheType::const_iterator
155 GCache::find(InsetGraphics const & inset) const
156 {
157         CacheType::const_iterator it  = cache->begin();
158         CacheType::const_iterator end = cache->end();
159         for (; it != end; ++it) {
160                 if (it->second->referencedBy(inset))
161                         return it;
162         }
163
164         return cache->end();
165 }
166
167
168 unsigned int GCache::raw_width(string const & filename) const
169 {
170         CacheType::const_iterator it = cache->find(filename);
171         if (it == cache->end())
172                 return 0;
173
174         return it->second->raw_width();
175 }
176
177
178 unsigned int GCache::raw_height(string const & filename) const
179 {
180         CacheType::const_iterator it = cache->find(filename);
181         if (it == cache->end())
182                 return 0;
183
184         return it->second->raw_height();
185 }
186
187
188 std::vector<string> GCache::loadableFormats() const {
189         return GImage::loadableFormats();
190 }
191
192 } // namespace grfx
193