]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsCacheItem_pimpl.C
get rid of turds, no.po update and remove some warnings
[lyx.git] / src / graphics / GraphicsCacheItem_pimpl.C
1 // -*- C++ -*-
2 /* This file is part of
3  * =================================================
4  * 
5  *          LyX, The Document Processor
6  *          Copyright 1995 Matthias Ettrich.
7  *          Copyright 1995-2000 The LyX Team.
8  *
9  *          This file Copyright 2000 Baruch Even
10  * ================================================= */
11
12 #include <config.h>
13
14 #include <map>
15
16 #include FORMS_H_LOCATION
17
18 #ifdef __GNUG__
19 #pragma implementation
20 #endif
21
22 #include "GraphicsCacheItem.h"
23 #include "GraphicsCacheItem_pimpl.h"
24
25 #include "frontends/support/LyXImage.h"
26 #include "graphics/XPM_Renderer.h"
27 #include "graphics/EPS_Renderer.h"
28 #include "support/filetools.h"
29 #include "debug.h"
30 #include "support/LAssert.h"
31
32 using std::endl;
33 using std::map;
34
35
36 GraphicsCacheItem_pimpl::GraphicsCacheItem_pimpl()
37         : height_(-1), width_(-1), imageStatus_(GraphicsCacheItem::Loading),
38           pixmap_(0), renderer(0), refCount(0)
39 {}
40
41
42 GraphicsCacheItem_pimpl::~GraphicsCacheItem_pimpl()
43 {
44         delete pixmap_;
45         delete renderer;
46 }
47
48
49 bool
50 GraphicsCacheItem_pimpl::setFilename(string const & filename)
51 {
52         imageStatus_ = GraphicsCacheItem::Loading;
53
54         renderer = new XPM_Renderer();
55         if (renderXPM(filename))
56                 return true;
57         
58         return false;
59 }
60
61
62 /*** Callback method ***/
63
64 typedef map<string, GraphicsCacheItem_pimpl*> CallbackMap;
65 static CallbackMap callbackMap;
66
67
68 void
69 callback(string cmd, int retval)
70 {
71         lyxerr << "callback, cmd=" << cmd << ", retval=" << retval << endl;
72
73         GraphicsCacheItem_pimpl * item = callbackMap[cmd];
74         callbackMap.erase(cmd);
75         
76         item->imageConverted(retval);
77 }
78
79
80 void
81 GraphicsCacheItem_pimpl::imageConverted(int retval)
82 {
83         lyxerr << "imageConverted, retval=" << retval << endl;
84
85         if (retval) {
86                 imageStatus_ = GraphicsCacheItem::ErrorConverting;
87                 return;
88         }
89
90         // Do the actual image loading from XPM to memory.
91         loadXPMImage(); 
92 }
93
94 /**********************/
95
96 bool
97 GraphicsCacheItem_pimpl::renderXPM(string const & filename)
98 {
99         // Create the command to do the conversion, this depends on ImageMagicks
100         // convert program.
101         string command = "convert ";
102         command += filename;
103         command += " XPM:";
104
105         // Take only the filename part of the file, without path or extension.
106         string temp = OnlyFilename(filename);
107         temp = ChangeExtension(filename, string());
108         
109         // Add some stuff to have it a unique temp file.
110         xpmfile = lyx::tempName(string(), temp);
111 #warning When is this tempfile unlinked? (Lgb)
112         xpmfile = ChangeExtension(xpmfile, ".xpm");     
113         
114         command += xpmfile;
115
116         // Set the callback mapping to point to us.
117         callbackMap[command] = this;
118
119         // Run the convertor.
120         // There is a problem with running it asyncronously, it doesn't return
121         // to call the callback, so until the Systemcalls mechanism is fixed
122         // I use the syncronous method.
123         lyxerr << "Launching convert to xpm, command=" << command << endl;
124 //      syscall.startscript(Systemcalls::DontWait, command, &callback);
125         syscall.startscript(Systemcalls::Wait, command, &callback);
126
127         return true;
128 }
129
130
131 // This function gets called from the callback after the image has been
132 // converted successfully.
133 void
134 GraphicsCacheItem_pimpl::loadXPMImage()
135 {
136         if (!renderer->setFilename(xpmfile)) {
137                 return;
138         }
139
140         if (renderer->renderImage()) {
141                 pixmap_ = renderer->getPixmap();
142                 width_ = renderer->getWidth();
143                 height_ = renderer->getHeight();
144                 imageStatus_ = GraphicsCacheItem::Loaded;
145         } else {
146                 imageStatus_ = GraphicsCacheItem::ErrorReading;
147         }
148
149         // remove the xpm file now.
150         lyx::unlink(xpmfile);
151         // and remove the reference to the filename.
152         xpmfile = string();
153 }