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"
22 // I think that graphicsInit should become Dialogs::graphicsInit.
23 // These #includes would then be moved there.
25 #include "GraphicsImageXPM.h"
26 //#include "xformsGraphicsImage.h"
35 // connect the image loader based on the XPM library
36 GImage::newImage.connect(slot(&GImageXPM::newImage));
37 GImage::loadableFormats.connect(slot(&GImageXPM::loadableFormats));
38 // connect the image loader based on the xforms library
39 // GImage::newImage.connect(slot(&xformsGImage::newImage));
40 // GImage::loadableFormats.connect(slot(&xformsGImage::loadableFormats));
48 GCache & GCache::get()
50 static bool start = true;
56 // Now return the cache
57 static GCache singleton;
64 cache = new CacheType;
68 // all elements are destroyed by the shared_ptr's in the map.
75 void GCache::update(InsetGraphics const & inset)
77 // A subset only of InsetGraphicsParams is needed for display purposes.
78 // The GraphicsParams c-tor also interrogates lyxrc to ascertain whether
80 GParams params(inset.params());
82 // Each inset can reference only one file, so check the cache for any
83 // graphics files referenced by inset. If the name of this file is
84 // different from that in params, then remove the reference.
85 CacheType::iterator it = find(inset);
87 if (it != cache->end()) {
88 CacheItemType item = it->second;
89 if (item->filename() != params.filename) {
96 // Are we adding a new file or modifying the display of an existing one?
97 it = cache->find(params.filename);
99 if (it != cache->end()) {
100 it->second->modify(inset, params);
104 CacheItemType item(new GCacheItem(inset, params));
106 (*cache)[params.filename] = item;
110 void GCache::remove(InsetGraphics const & inset)
112 CacheType::iterator it = find(inset);
113 if (it == cache->end())
116 CacheItemType item = it->second;
124 void GCache::startLoading(InsetGraphics const & inset)
126 CacheType::iterator it = find(inset);
127 if (it == cache->end())
130 it->second->startLoading(inset);
134 ImagePtr const GCache::image(InsetGraphics const & inset) const
136 CacheType::const_iterator it = find(inset);
137 if (it == cache->end())
140 return it->second->image(inset);
144 ImageStatus GCache::status(InsetGraphics const & inset) const
146 CacheType::const_iterator it = find(inset);
147 if (it == cache->end())
150 return it->second->status(inset);
154 void GCache::changeDisplay(bool changed_background)
156 CacheType::iterator it = cache->begin();
157 CacheType::iterator end = cache->end();
158 for(; it != end; ++it)
159 it->second->changeDisplay(changed_background);
163 GCache::CacheType::iterator
164 GCache::find(InsetGraphics const & inset)
166 CacheType::iterator it = cache->begin();
167 for (; it != cache->end(); ++it) {
168 if (it->second->referencedBy(inset))
176 GCache::CacheType::const_iterator
177 GCache::find(InsetGraphics const & inset) const
179 CacheType::const_iterator it = cache->begin();
180 for (; it != cache->end(); ++it) {
181 if (it->second->referencedBy(inset))