]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsCache.C
* GraphicsImageXPM.[Ch]: more rigorous use of types (signed/unsigned).
[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
22 // I think that graphicsInit should become Dialogs::graphicsInit.
23 // These #includes would then be moved there.
24 // Angus 25 Feb 2002
25 #include "GraphicsImageXPM.h"
26 //#include "xformsGraphicsImage.h"
27
28 namespace {
29
30 void graphicsInit() 
31 {
32         using namespace grfx;
33         using SigC::slot;
34     
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));
41 }
42     
43 } // namespace anon
44  
45
46 namespace grfx {
47
48 GCache & GCache::get()
49 {
50         static bool start = true;
51         if (start) {
52                 start = false;
53                 graphicsInit();
54         }
55
56         // Now return the cache
57         static GCache singleton;
58         return singleton;
59 }
60
61
62 GCache::GCache()
63 {
64         cache = new CacheType;
65 }
66
67
68 // all elements are destroyed by the shared_ptr's in the map.
69 GCache::~GCache()
70 {
71         delete cache;
72 }
73
74
75 void GCache::update(InsetGraphics const & inset)
76 {
77         // A subset only of InsetGraphicsParams is needed for display purposes.
78         // The GraphicsParams c-tor also interrogates lyxrc to ascertain whether
79         // to display or not.
80         GParams params(inset.params());
81
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);
86
87         if (it != cache->end()) {
88                 CacheItemType item = it->second;
89                 if (item->filename() != params.filename) {
90                         item->remove(inset);
91                         if (item->empty())
92                                 cache->erase(it);
93                 }
94         }
95
96         // Are we adding a new file or modifying the display of an existing one?
97         it = cache->find(params.filename);
98
99         if (it != cache->end()) {
100                 it->second->modify(inset, params);
101                 return;
102         }
103
104         CacheItemType item(new GCacheItem(inset, params));
105         if (item.get() != 0)
106                 (*cache)[params.filename] = item;
107 }
108
109
110 void GCache::remove(InsetGraphics const & inset)
111 {
112         CacheType::iterator it = find(inset);
113         if (it == cache->end())
114                 return;
115
116         CacheItemType item = it->second;
117         item->remove(inset);
118         if (item->empty()) {
119                 cache->erase(it);
120         }
121 }
122
123
124 void GCache::startLoading(InsetGraphics const & inset)
125 {
126         CacheType::iterator it = find(inset);
127         if (it == cache->end())
128                 return;
129
130         it->second->startLoading(inset);
131 }
132
133
134 ImagePtr const GCache::image(InsetGraphics const & inset) const
135 {
136         CacheType::const_iterator it = find(inset);
137         if (it == cache->end())
138                 return ImagePtr();
139
140         return it->second->image(inset);
141 }
142
143
144 ImageStatus GCache::status(InsetGraphics const & inset) const
145 {
146         CacheType::const_iterator it = find(inset);
147         if (it == cache->end())
148                 return ErrorUnknown;
149
150         return it->second->status(inset);
151 }
152
153
154 void GCache::changeDisplay(bool changed_background)
155 {
156         CacheType::iterator it = cache->begin();
157         CacheType::iterator end = cache->end();
158         for(; it != end; ++it)
159                 it->second->changeDisplay(changed_background);
160 }
161
162
163 GCache::CacheType::iterator
164 GCache::find(InsetGraphics const & inset)
165 {
166         CacheType::iterator it = cache->begin();
167         for (; it != cache->end(); ++it) {
168                 if (it->second->referencedBy(inset))
169                         return it;
170         }
171         
172         return cache->end();
173 }
174
175
176 GCache::CacheType::const_iterator
177 GCache::find(InsetGraphics const & inset) const
178 {
179         CacheType::const_iterator it = cache->begin();
180         for (; it != cache->end(); ++it) {
181                 if (it->second->referencedBy(inset))
182                         return it;
183         }
184         
185         return cache->end();
186 }
187
188 } // namespace grfx