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);
111 #warning When is this tempfile unlinked? (Lgb)
112 xpmfile = ChangeExtension(xpmfile, ".xpm");
116 // Set the callback mapping to point to us.
117 callbackMap[command] = this;
119 // Run the convertor.
120 // There is a problem with running it asyncronously, it doesn't return
121 // to call the callback, so until the Systemcalls mechanism is fixed
122 // I use the syncronous method.
123 lyxerr << "Launching convert to xpm, command=" << command << endl;
124 // syscall.startscript(Systemcalls::DontWait, command, &callback);
125 syscall.startscript(Systemcalls::Wait, command, &callback);
131 // This function gets called from the callback after the image has been
132 // converted successfully.
134 GraphicsCacheItem_pimpl::loadXPMImage()
136 if (!renderer->setFilename(xpmfile)) {
140 if (renderer->renderImage()) {
141 pixmap_ = renderer->getPixmap();
142 width_ = renderer->getWidth();
143 height_ = renderer->getHeight();
144 imageStatus_ = GraphicsCacheItem::Loaded;
146 imageStatus_ = GraphicsCacheItem::ErrorReading;
149 // remove the xpm file now.
150 lyx::unlink(xpmfile);
151 // and remove the reference to the filename.