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
33 GraphicsCacheItem::GraphicsCacheItem()
34 : height_(-1), width_(-1), imageStatus_(Loading),
35 pixmap_(0), renderer(0)
38 GraphicsCacheItem::~GraphicsCacheItem()
40 if (imageStatus_ == Loaded) {
41 XFreePixmap(fl_display, pixmap_);
48 GraphicsCacheItem::setFilename(string const & filename)
50 imageStatus_ = Loading;
52 renderer = new XPM_Renderer();
53 if (renderXPM(filename))
59 /*** Callback method ***/
61 typedef map<string, GraphicsCacheItem*> CallbackMap;
62 static CallbackMap callbackMap;
65 callback(string cmd, int retval)
67 lyxerr << "callback, cmd="<<cmd<<", retval="<<retval<<endl;
69 GraphicsCacheItem * item = callbackMap[cmd];
70 callbackMap.erase(cmd);
72 item->imageConverted(retval);
76 GraphicsCacheItem::imageConverted(int retval)
78 lyxerr << "imageConverted, retval="<<retval<<endl;
81 imageStatus_ = ErrorConverting;
85 // Do the actual image loading from XPM to memory.
89 /**********************/
92 GraphicsCacheItem::renderXPM(string const & filename)
94 // Create the command to do the conversion, this depends on ImageMagicks
96 string command = "convert ";
100 // Take only the filename part of the file, without path or extension.
101 string temp = OnlyFilename(filename);
102 temp = ChangeExtension(filename , string());
104 // Add some stuff to have it a unique temp file.
105 xpmfile = TmpFileName(string(), temp);
106 xpmfile = ChangeExtension(xpmfile, ".xpm");
110 // Set the callback mapping to point to us.
111 callbackMap[command] = this;
113 // Run the convertor.
114 // There is a problem with running it asyncronously, it doesn't return
115 // to call the callback, so until the Systemcalls mechanism is fixed
116 // I use the syncronous method.
117 lyxerr << "Launching convert to xpm, command="<<command<<endl;
118 // syscall.startscript(Systemcalls::DontWait, command, &callback);
119 syscall.startscript(Systemcalls::Wait, command, &callback);
124 // This function gets called from the callback after the image has been
125 // converted successfully.
127 GraphicsCacheItem::loadXPMImage()
129 if (! renderer->setFilename(xpmfile)) {
133 if (renderer->renderImage()) {
134 pixmap_ = renderer->getPixmap();
135 width_ = renderer->getWidth();
136 height_ = renderer->getHeight();
137 imageStatus_ = Loaded;
139 imageStatus_ = ErrorReading;
144 // remove the xpm file now.
145 ::unlink(xpmfile.c_str());
146 // and remove the reference to the filename.