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/GUIRunTime.h"
26 GCache & GCache::get()
28 static bool start = true;
31 GUIRunTime::initialiseGraphics();
34 // Now return the cache
35 static GCache singleton;
42 cache = new CacheType;
46 // all elements are destroyed by the shared_ptr's in the map.
53 void GCache::update(InsetGraphics const & inset, string const & filepath)
55 // A subset only of InsetGraphicsParams is needed for display purposes.
56 // The GraphicsParams c-tor also interrogates lyxrc to ascertain whether
58 GParams params(inset.params(), filepath);
60 // Each inset can reference only one file, so check the cache for any
61 // graphics files referenced by inset. If the name of this file is
62 // different from that in params, then remove the reference.
63 CacheType::iterator it = find(inset);
65 if (it != cache->end()) {
66 CacheItemType item = it->second;
67 if (item->filename() != params.filename) {
74 // Are we adding a new file or modifying the display of an existing one?
75 it = cache->find(params.filename);
77 if (it != cache->end()) {
78 it->second->modify(inset, params);
82 CacheItemType item(new GCacheItem(inset, params));
84 (*cache)[params.filename] = item;
88 void GCache::remove(InsetGraphics const & inset)
90 CacheType::iterator it = find(inset);
91 if (it == cache->end())
94 CacheItemType item = it->second;
102 void GCache::startLoading(InsetGraphics const & inset)
104 CacheType::iterator it = find(inset);
105 if (it == cache->end())
108 it->second->startLoading(inset);
112 ImagePtr const GCache::image(InsetGraphics const & inset) const
114 CacheType::const_iterator it = find(inset);
115 if (it == cache->end())
118 return it->second->image(inset);
122 ImageStatus GCache::status(InsetGraphics const & inset) const
124 CacheType::const_iterator it = find(inset);
125 if (it == cache->end())
128 return it->second->status(inset);
132 void GCache::changeDisplay(bool changed_background)
134 CacheType::iterator it = cache->begin();
135 CacheType::iterator end = cache->end();
136 for(; it != end; ++it)
137 it->second->changeDisplay(changed_background);
141 GCache::CacheType::iterator
142 GCache::find(InsetGraphics const & inset)
144 CacheType::iterator it = cache->begin();
145 CacheType::iterator end = cache->end();
146 for (; it != end; ++it) {
147 if (it->second->referencedBy(inset))
155 GCache::CacheType::const_iterator
156 GCache::find(InsetGraphics const & inset) const
158 CacheType::const_iterator it = cache->begin();
159 CacheType::const_iterator end = cache->end();
160 for (; it != end; ++it) {
161 if (it->second->referencedBy(inset))