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 "graphics/XPM_Renderer.h"
26 #include "graphics/EPS_Renderer.h"
27 #include "support/filetools.h"
29 #include "support/LAssert.h"
35 GraphicsCacheItem_pimpl::GraphicsCacheItem_pimpl()
36 : height_(-1), width_(-1), imageStatus_(GraphicsCacheItem::Loading),
37 pixmap_(0), renderer(0), refCount(0)
41 GraphicsCacheItem_pimpl::~GraphicsCacheItem_pimpl()
43 if (imageStatus_ == GraphicsCacheItem::Loaded) {
44 XFreePixmap(fl_display, pixmap_);
52 GraphicsCacheItem_pimpl::setFilename(string const & filename)
54 imageStatus_ = GraphicsCacheItem::Loading;
56 renderer = new XPM_Renderer();
57 if (renderXPM(filename))
64 /*** Callback method ***/
66 typedef map<string, GraphicsCacheItem_pimpl*> CallbackMap;
67 static CallbackMap callbackMap;
71 callback(string cmd, int retval)
73 lyxerr << "callback, cmd=" << cmd << ", retval=" << retval << endl;
75 GraphicsCacheItem_pimpl * item = callbackMap[cmd];
76 callbackMap.erase(cmd);
78 item->imageConverted(retval);
83 GraphicsCacheItem_pimpl::imageConverted(int retval)
85 lyxerr << "imageConverted, retval=" << retval << endl;
88 imageStatus_ = GraphicsCacheItem::ErrorConverting;
92 // Do the actual image loading from XPM to memory.
96 /**********************/
99 GraphicsCacheItem_pimpl::renderXPM(string const & filename)
101 // Create the command to do the conversion, this depends on ImageMagicks
103 string command = "convert ";
107 // Take only the filename part of the file, without path or extension.
108 string temp = OnlyFilename(filename);
109 temp = ChangeExtension(filename, string());
111 // Add some stuff to have it a unique temp file.
112 xpmfile = TmpFileName(string(), temp);
113 xpmfile = ChangeExtension(xpmfile, ".xpm");
117 // Set the callback mapping to point to us.
118 callbackMap[command] = this;
120 // Run the convertor.
121 // There is a problem with running it asyncronously, it doesn't return
122 // to call the callback, so until the Systemcalls mechanism is fixed
123 // I use the syncronous method.
124 lyxerr << "Launching convert to xpm, command=" << command << endl;
125 // syscall.startscript(Systemcalls::DontWait, command, &callback);
126 syscall.startscript(Systemcalls::Wait, command, &callback);
132 // This function gets called from the callback after the image has been
133 // converted successfully.
135 GraphicsCacheItem_pimpl::loadXPMImage()
137 if (!renderer->setFilename(xpmfile)) {
141 if (renderer->renderImage()) {
142 pixmap_ = renderer->getPixmap();
143 width_ = renderer->getWidth();
144 height_ = renderer->getHeight();
145 imageStatus_ = GraphicsCacheItem::Loaded;
147 imageStatus_ = GraphicsCacheItem::ErrorReading;
150 // remove the xpm file now.
151 lyx::unlink(xpmfile);
152 // and remove the reference to the filename.