-// -*- C++ -*-
-/* This file is part of
- * =================================================
- *
- * LyX, The Document Processor
- * Copyright 1995 Matthias Ettrich.
- * Copyright 1995-2000 The LyX Team.
+/*
+ * \file GraphicsCache.C
+ * Copyright 2002 the LyX Team
+ * Read the file COPYING
*
- * This file Copyright 2000 Baruch Even
- * ================================================= */
+ * \author Baruch Even <baruch.even@writeme.com>
+ * \author Angus Leeming <a.leeming@ic.ac.uk>
+ */
#include <config.h>
#endif
#include "GraphicsCache.h"
+#include "GraphicsCacheItem.h"
+#include "GraphicsImage.h"
+#include "GraphicsParams.h"
+#include "insets/insetgraphics.h"
+#include "frontends/Dialogs.h"
-#include "support/LAssert.h"
-GraphicsCache * GraphicsCache::singleton = 0;
+namespace grfx {
-
-GraphicsCache *
-GraphicsCache::getInstance()
+GCache & GCache::get()
{
- if (!singleton) {
- singleton = new GraphicsCache;
- Assert(singleton != 0);
+ static bool start = true;
+ if (start) {
+ start = false;
+ Dialogs::initialiseGraphics();
}
+ // Now return the cache
+ static GCache singleton;
return singleton;
}
-GraphicsCache::~GraphicsCache()
+GCache::GCache()
{
- // Free the map.
- //std::foreach(map.begin(), map.end(), ...);
-#warning This is a bogus reason to not clean up after your self. (Lgb)
- // Clean up and be done with it. (Lgb)
-
- // This is not really needed, it will only happen on program close and in
- // any case the OS will release those resources (not doing it may have
- // a good effect on closing time).
-
- delete singleton;
+ cache = new CacheType;
}
-GraphicsCacheItem *
-GraphicsCache::addFile(string const & filename)
+// all elements are destroyed by the shared_ptr's in the map.
+GCache::~GCache()
{
- CacheType::const_iterator it = cache.find(filename);
-
- if (it != cache.end()) {
- return new GraphicsCacheItem( *((*it).second) );
+ delete cache;
+}
+
+
+void GCache::update(InsetGraphics const & inset)
+{
+ // A subset only of InsetGraphicsParams is needed for display purposes.
+ // The GraphicsParams c-tor also interrogates lyxrc to ascertain whether
+ // to display or not.
+ GParams params(inset.params());
+
+ // Each inset can reference only one file, so check the cache for any
+ // graphics files referenced by inset. If the name of this file is
+ // different from that in params, then remove the reference.
+ CacheType::iterator it = find(inset);
+
+ if (it != cache->end()) {
+ CacheItemType item = it->second;
+ if (item->filename() != params.filename) {
+ item->remove(inset);
+ if (item->empty())
+ cache->erase(it);
+ }
}
-
- GraphicsCacheItem * cacheItem = new GraphicsCacheItem();
- if (cacheItem == 0)
- return 0;
-
- cacheItem->setFilename(filename);
-
- cache[filename] = cacheItem;
-
- // We do not want to return the main cache object, otherwise when the
- // will destroy their copy they will destroy the main copy.
- return new GraphicsCacheItem( *cacheItem );
+
+ // Are we adding a new file or modifying the display of an existing one?
+ it = cache->find(params.filename);
+
+ if (it != cache->end()) {
+ it->second->modify(inset, params);
+ return;
+ }
+
+ CacheItemType item(new GCacheItem(inset, params));
+ if (item.get() != 0)
+ (*cache)[params.filename] = item;
}
-void
-GraphicsCache::removeFile(string const & filename)
+void GCache::remove(InsetGraphics const & inset)
{
- // We do not destroy the GraphicsCacheItem since we are here because
- // the last copy of it is being erased.
+ CacheType::iterator it = find(inset);
+ if (it == cache->end())
+ return;
- if (cache.find(filename) != cache.end())
- cache.erase(filename);
+ CacheItemType item = it->second;
+ item->remove(inset);
+ if (item->empty()) {
+ cache->erase(it);
+ }
}
+
+
+void GCache::startLoading(InsetGraphics const & inset)
+{
+ CacheType::iterator it = find(inset);
+ if (it == cache->end())
+ return;
+
+ it->second->startLoading(inset);
+}
+
+
+ImagePtr const GCache::image(InsetGraphics const & inset) const
+{
+ CacheType::const_iterator it = find(inset);
+ if (it == cache->end())
+ return ImagePtr();
+
+ return it->second->image(inset);
+}
+
+
+ImageStatus GCache::status(InsetGraphics const & inset) const
+{
+ CacheType::const_iterator it = find(inset);
+ if (it == cache->end())
+ return ErrorUnknown;
+
+ return it->second->status(inset);
+}
+
+
+void GCache::changeDisplay(bool changed_background)
+{
+ CacheType::iterator it = cache->begin();
+ CacheType::iterator end = cache->end();
+ for(; it != end; ++it)
+ it->second->changeDisplay(changed_background);
+}
+
+
+GCache::CacheType::iterator
+GCache::find(InsetGraphics const & inset)
+{
+ CacheType::iterator it = cache->begin();
+ for (; it != cache->end(); ++it) {
+ if (it->second->referencedBy(inset))
+ return it;
+ }
+
+ return cache->end();
+}
+
+
+GCache::CacheType::const_iterator
+GCache::find(InsetGraphics const & inset) const
+{
+ CacheType::const_iterator it = cache->begin();
+ for (; it != cache->end(); ++it) {
+ if (it->second->referencedBy(inset))
+ return it;
+ }
+
+ return cache->end();
+}
+
+} // namespace grfx