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"
20 #include "graphics/XPM_Renderer.h"
21 #include "support/filetools.h"
23 #include "support/LAssert.h"
24 #include <unistd.h> // unlink
28 #include FORMS_H_LOCATION
32 GraphicsCacheItem::GraphicsCacheItem()
33 : height_(-1), width_(-1), imageStatus_(Loading),
34 pixmap_(0), renderer(0)
37 GraphicsCacheItem::~GraphicsCacheItem()
39 if (imageStatus_ == Loaded) {
40 XFreePixmap(fl_display, pixmap_);
47 GraphicsCacheItem::setFilename(string const & filename)
49 imageStatus_ = Loading;
51 renderer = new XPM_Renderer();
52 if (renderXPM(filename))
58 /*** Callback method ***/
60 typedef map<string, GraphicsCacheItem*> CallbackMap;
61 static CallbackMap callbackMap;
64 callback(string cmd, int retval)
66 lyxerr << "callback, cmd="<<cmd<<", retval="<<retval<<endl;
68 GraphicsCacheItem * item = callbackMap[cmd];
69 callbackMap.erase(cmd);
71 item->imageConverted(retval);
75 GraphicsCacheItem::imageConverted(int retval)
77 lyxerr << "imageConverted, retval="<<retval<<endl;
80 imageStatus_ = ErrorConverting;
84 // Do the actual image loading from XPM to memory.
88 /**********************/
91 GraphicsCacheItem::renderXPM(string const & filename)
93 // Create the command to do the conversion, this depends on ImageMagicks
95 string command = "convert ";
99 // Take only the filename part of the file, without path or extension.
100 string temp = OnlyFilename(filename);
101 temp = ChangeExtension(filename , string());
103 // Add some stuff to have it a unique temp file.
104 xpmfile = TmpFileName(string(), temp);
105 xpmfile = ChangeExtension(xpmfile, ".xpm");
109 // Set the callback mapping to point to us.
110 callbackMap[command] = this;
112 // Run the convertor.
113 // There is a problem with running it asyncronously, it doesn't return
114 // to call the callback, so until the Systemcalls mechanism is fixed
115 // I use the syncronous method.
116 lyxerr << "Launching convert to xpm, command="<<command<<endl;
117 // syscall.startscript(Systemcalls::DontWait, command, &callback);
118 syscall.startscript(Systemcalls::Wait, command, &callback);
123 // This function gets called from the callback after the image has been
124 // converted successfully.
126 GraphicsCacheItem::loadXPMImage()
128 if (! renderer->setFilename(xpmfile)) {
132 if (renderer->renderImage()) {
133 pixmap_ = renderer->getPixmap();
134 width_ = renderer->getWidth();
135 height_ = renderer->getHeight();
136 imageStatus_ = Loaded;
138 imageStatus_ = ErrorReading;
143 // remove the xpm file now.
144 ::unlink(xpmfile.c_str());
145 // and remove the reference to the filename.