2 /* This file is part of
3 * =================================================
5 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich.
7 * Copyright 1995-2000 The LyX Team.
9 * This file Copyright 2000 Baruch Even
10 * ================================================= */
15 #pragma implementation
18 #include "GraphicsCacheItem.h"
19 #include "GraphicsCacheItem_pimpl.h"
21 #include "graphics/XPM_Renderer.h"
22 #include "graphics/EPS_Renderer.h"
23 #include "support/filetools.h"
25 #include "support/LAssert.h"
26 #include <unistd.h> // unlink
30 #include FORMS_H_LOCATION
36 GraphicsCacheItem_pimpl::GraphicsCacheItem_pimpl()
37 : height_(-1), width_(-1), imageStatus_(GraphicsCacheItem::Loading),
38 pixmap_(0), renderer(0), refCount(0)
41 GraphicsCacheItem_pimpl::~GraphicsCacheItem_pimpl()
43 if (imageStatus_ == GraphicsCacheItem::Loaded) {
44 XFreePixmap(fl_display, pixmap_);
51 GraphicsCacheItem_pimpl::setFilename(string const & filename)
53 imageStatus_ = GraphicsCacheItem::Loading;
55 renderer = new XPM_Renderer();
56 if (renderXPM(filename))
62 /*** Callback method ***/
64 typedef map<string, GraphicsCacheItem_pimpl*> CallbackMap;
65 static CallbackMap callbackMap;
68 callback(string cmd, int retval)
70 lyxerr << "callback, cmd="<<cmd<<", retval="<<retval<<endl;
72 GraphicsCacheItem_pimpl * item = callbackMap[cmd];
73 callbackMap.erase(cmd);
75 item->imageConverted(retval);
79 GraphicsCacheItem_pimpl::imageConverted(int retval)
81 lyxerr << "imageConverted, retval="<<retval<<endl;
84 imageStatus_ = GraphicsCacheItem::ErrorConverting;
88 // Do the actual image loading from XPM to memory.
92 /**********************/
95 GraphicsCacheItem_pimpl::renderXPM(string const & filename)
97 // Create the command to do the conversion, this depends on ImageMagicks
99 string command = "convert ";
103 // Take only the filename part of the file, without path or extension.
104 string temp = OnlyFilename(filename);
105 temp = ChangeExtension(filename , string());
107 // Add some stuff to have it a unique temp file.
108 xpmfile = TmpFileName(string(), temp);
109 xpmfile = ChangeExtension(xpmfile, ".xpm");
113 // Set the callback mapping to point to us.
114 callbackMap[command] = this;
116 // Run the convertor.
117 // There is a problem with running it asyncronously, it doesn't return
118 // to call the callback, so until the Systemcalls mechanism is fixed
119 // I use the syncronous method.
120 lyxerr << "Launching convert to xpm, command="<<command<<endl;
121 // syscall.startscript(Systemcalls::DontWait, command, &callback);
122 syscall.startscript(Systemcalls::Wait, command, &callback);
127 // This function gets called from the callback after the image has been
128 // converted successfully.
130 GraphicsCacheItem_pimpl::loadXPMImage()
132 if (! renderer->setFilename(xpmfile)) {
136 if (renderer->renderImage()) {
137 pixmap_ = renderer->getPixmap();
138 width_ = renderer->getWidth();
139 height_ = renderer->getHeight();
140 imageStatus_ = GraphicsCacheItem::Loaded;
142 imageStatus_ = GraphicsCacheItem::ErrorReading;
145 // remove the xpm file now.
146 ::unlink(xpmfile.c_str());
147 // and remove the reference to the filename.