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