2 * \file GraphicsCache.C
3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
6 * \author Baruch Even <baruch.even@writeme.com>
7 * \author Angus Leeming <a.leeming@ic.ac.uk>
13 #pragma implementation
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"
25 GCache & GCache::get()
27 static bool start = true;
30 lyx_gui::init_graphics();
33 // Now return the cache
34 static GCache singleton;
41 cache = new CacheType;
45 // all elements are destroyed by the shared_ptr's in the map.
52 void GCache::update(InsetGraphics const & inset, string const & filepath)
54 // A subset only of InsetGraphicsParams is needed for display purposes.
55 // The GraphicsParams c-tor also interrogates lyxrc to ascertain whether
57 GParams params = inset.params().asGParams(filepath);
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);
64 if (it != cache->end()) {
65 CacheItemType item = it->second;
66 if (item->filename() != params.filename) {
73 // Are we adding a new file or modifying the display of an existing one?
74 it = cache->find(params.filename);
76 if (it != cache->end()) {
77 it->second->modify(inset, params);
81 CacheItemType item(new GCacheItem(inset, params));
83 (*cache)[params.filename] = item;
87 void GCache::remove(InsetGraphics const & inset)
89 CacheType::iterator it = find(inset);
90 if (it == cache->end())
93 CacheItemType item = it->second;
101 void GCache::startLoading(InsetGraphics const & inset)
103 CacheType::iterator it = find(inset);
104 if (it == cache->end())
107 it->second->startLoading(inset);
111 ImagePtr const GCache::image(InsetGraphics const & inset) const
113 CacheType::const_iterator it = find(inset);
114 if (it == cache->end())
117 return it->second->image(inset);
121 ImageStatus GCache::status(InsetGraphics const & inset) const
123 CacheType::const_iterator it = find(inset);
124 if (it == cache->end())
127 return it->second->status(inset);
131 void GCache::changeDisplay(bool changed_background)
133 CacheType::iterator it = cache->begin();
134 CacheType::iterator end = cache->end();
135 for(; it != end; ++it)
136 it->second->changeDisplay(changed_background);
140 GCache::CacheType::iterator
141 GCache::find(InsetGraphics const & inset)
143 CacheType::iterator it = cache->begin();
144 CacheType::iterator end = cache->end();
145 for (; it != end; ++it) {
146 if (it->second->referencedBy(inset))
154 GCache::CacheType::const_iterator
155 GCache::find(InsetGraphics const & inset) const
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))
168 unsigned int GCache::raw_width(string const & filename) const
170 CacheType::const_iterator it = cache->find(filename);
171 if (it == cache->end())
174 return it->second->raw_width();
178 unsigned int GCache::raw_height(string const & filename) const
180 CacheType::const_iterator it = cache->find(filename);
181 if (it == cache->end())
184 return it->second->raw_height();
188 std::vector<string> GCache::loadableFormats() const {
189 return GImage::loadableFormats();