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 * ================================================= */
16 #include <unistd.h> // unlink
18 #include FORMS_H_LOCATION
21 #pragma implementation
24 #include "GraphicsCacheItem.h"
25 #include "GraphicsCacheItem_pimpl.h"
27 #include "graphics/XPM_Renderer.h"
28 #include "graphics/EPS_Renderer.h"
29 #include "support/filetools.h"
31 #include "support/LAssert.h"
37 GraphicsCacheItem_pimpl::GraphicsCacheItem_pimpl()
38 : height_(-1), width_(-1), imageStatus_(GraphicsCacheItem::Loading),
39 pixmap_(0), renderer(0), refCount(0)
43 GraphicsCacheItem_pimpl::~GraphicsCacheItem_pimpl()
45 if (imageStatus_ == GraphicsCacheItem::Loaded) {
46 XFreePixmap(fl_display, pixmap_);
54 GraphicsCacheItem_pimpl::setFilename(string const & filename)
56 imageStatus_ = GraphicsCacheItem::Loading;
58 renderer = new XPM_Renderer();
59 if (renderXPM(filename))
66 /*** Callback method ***/
68 typedef map<string, GraphicsCacheItem_pimpl*> CallbackMap;
69 static CallbackMap callbackMap;
73 callback(string cmd, int retval)
75 lyxerr << "callback, cmd=" << cmd << ", retval=" << retval << endl;
77 GraphicsCacheItem_pimpl * item = callbackMap[cmd];
78 callbackMap.erase(cmd);
80 item->imageConverted(retval);
85 GraphicsCacheItem_pimpl::imageConverted(int retval)
87 lyxerr << "imageConverted, retval=" << retval << endl;
90 imageStatus_ = GraphicsCacheItem::ErrorConverting;
94 // Do the actual image loading from XPM to memory.
98 /**********************/
101 GraphicsCacheItem_pimpl::renderXPM(string const & filename)
103 // Create the command to do the conversion, this depends on ImageMagicks
105 string command = "convert ";
109 // Take only the filename part of the file, without path or extension.
110 string temp = OnlyFilename(filename);
111 temp = ChangeExtension(filename, string());
113 // Add some stuff to have it a unique temp file.
114 xpmfile = TmpFileName(string(), temp);
115 xpmfile = ChangeExtension(xpmfile, ".xpm");
119 // Set the callback mapping to point to us.
120 callbackMap[command] = this;
122 // Run the convertor.
123 // There is a problem with running it asyncronously, it doesn't return
124 // to call the callback, so until the Systemcalls mechanism is fixed
125 // I use the syncronous method.
126 lyxerr << "Launching convert to xpm, command=" << command << endl;
127 // syscall.startscript(Systemcalls::DontWait, command, &callback);
128 syscall.startscript(Systemcalls::Wait, command, &callback);
134 // This function gets called from the callback after the image has been
135 // converted successfully.
137 GraphicsCacheItem_pimpl::loadXPMImage()
139 if (!renderer->setFilename(xpmfile)) {
143 if (renderer->renderImage()) {
144 pixmap_ = renderer->getPixmap();
145 width_ = renderer->getWidth();
146 height_ = renderer->getHeight();
147 imageStatus_ = GraphicsCacheItem::Loaded;
149 imageStatus_ = GraphicsCacheItem::ErrorReading;
152 // remove the xpm file now.
153 ::unlink(xpmfile.c_str());
154 // and remove the reference to the filename.