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 FORMS_H_LOCATION
19 #pragma implementation
22 #include "GraphicsCacheItem.h"
23 #include "GraphicsCacheItem_pimpl.h"
25 #include "frontends/support/LyXImage.h"
26 #include "graphics/XPM_Renderer.h"
27 #include "graphics/EPS_Renderer.h"
28 #include "support/filetools.h"
30 #include "support/LAssert.h"
36 GraphicsCacheItem_pimpl::GraphicsCacheItem_pimpl()
37 : height_(-1), width_(-1), imageStatus_(GraphicsCacheItem::Loading),
38 pixmap_(0), renderer(0), refCount(0)
42 GraphicsCacheItem_pimpl::~GraphicsCacheItem_pimpl()
50 GraphicsCacheItem_pimpl::setFilename(string const & filename)
52 imageStatus_ = GraphicsCacheItem::Loading;
54 renderer = new XPM_Renderer();
55 if (renderXPM(filename))
62 /*** Callback method ***/
64 typedef map<string, GraphicsCacheItem_pimpl*> CallbackMap;
65 static CallbackMap callbackMap;
69 callback(string cmd, int retval)
71 lyxerr << "callback, cmd=" << cmd << ", retval=" << retval << endl;
73 GraphicsCacheItem_pimpl * item = callbackMap[cmd];
74 callbackMap.erase(cmd);
76 item->imageConverted(retval);
81 GraphicsCacheItem_pimpl::imageConverted(int retval)
83 lyxerr << "imageConverted, retval=" << retval << endl;
86 imageStatus_ = GraphicsCacheItem::ErrorConverting;
90 // Do the actual image loading from XPM to memory.
94 /**********************/
97 GraphicsCacheItem_pimpl::renderXPM(string const & filename)
99 // Create the command to do the conversion, this depends on ImageMagicks
101 string command = "convert ";
105 // Take only the filename part of the file, without path or extension.
106 string temp = OnlyFilename(filename);
107 temp = ChangeExtension(filename, string());
109 // Add some stuff to have it a unique temp file.
110 xpmfile = lyx::tempName(string(), temp); //TmpFileName(string(), temp);
111 xpmfile = ChangeExtension(xpmfile, ".xpm");
115 // Set the callback mapping to point to us.
116 callbackMap[command] = this;
118 // Run the convertor.
119 // There is a problem with running it asyncronously, it doesn't return
120 // to call the callback, so until the Systemcalls mechanism is fixed
121 // I use the syncronous method.
122 lyxerr << "Launching convert to xpm, command=" << command << endl;
123 // syscall.startscript(Systemcalls::DontWait, command, &callback);
124 syscall.startscript(Systemcalls::Wait, command, &callback);
130 // This function gets called from the callback after the image has been
131 // converted successfully.
133 GraphicsCacheItem_pimpl::loadXPMImage()
135 if (!renderer->setFilename(xpmfile)) {
139 if (renderer->renderImage()) {
140 pixmap_ = renderer->getPixmap();
141 width_ = renderer->getWidth();
142 height_ = renderer->getHeight();
143 imageStatus_ = GraphicsCacheItem::Loaded;
145 imageStatus_ = GraphicsCacheItem::ErrorReading;
148 // remove the xpm file now.
149 lyx::unlink(xpmfile);
150 // and remove the reference to the filename.