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 "ImageLoaderXPM.h"
27 #include "support/filetools.h"
29 #include "support/LAssert.h"
35 GraphicsCacheItem_pimpl::GraphicsCacheItem_pimpl()
36 : imageStatus_(GraphicsCacheItem::Loading),
37 image_(0), imageLoader(0), refCount(0)
41 GraphicsCacheItem_pimpl::~GraphicsCacheItem_pimpl()
43 delete image_; image_ = 0;
44 delete imageLoader; imageLoader = 0;
49 GraphicsCacheItem_pimpl::setFilename(string const & filename)
51 imageLoader = new ImageLoaderXPM();
52 imageStatus_ = GraphicsCacheItem::Loading;
54 if (renderXPM(filename))
61 /*** Callback method ***/
63 typedef map<string, GraphicsCacheItem_pimpl*> CallbackMap;
64 static CallbackMap callbackMap;
68 void callback(string cmd, int retval)
70 lyxerr << "callback, cmd=" << cmd << ", retval=" << retval << endl;
72 GraphicsCacheItem_pimpl * item = callbackMap[cmd];
73 callbackMap.erase(cmd);
75 item->imageConverted(retval);
80 GraphicsCacheItem_pimpl::imageConverted(int retval)
82 lyxerr << "imageConverted, retval=" << retval << endl;
85 lyxerr << "(GraphicsCacheItem_pimpl::imageConverter) "
86 "Error converting image." << endl;
87 imageStatus_ = GraphicsCacheItem::ErrorConverting;
91 // Do the actual image loading from XPM to memory.
95 /**********************/
98 GraphicsCacheItem_pimpl::renderXPM(string const & filename)
100 // Create the command to do the conversion, this depends on ImageMagicks
102 string command = "convert ";
106 // Take only the filename part of the file, without path or extension.
107 string temp = OnlyFilename(filename);
108 temp = ChangeExtension(filename, string());
110 // Add some stuff to have it a unique temp file.
111 // This tempfile is deleted in loadXPMImage after it is loaded to memory.
112 xpmfile = lyx::tempName(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 lyxerr << "Loading XPM Image... ";
139 if (imageLoader->loadImage(xpmfile) == ImageLoader::OK) {
140 lyxerr << "Success." << endl;
141 image_ = imageLoader->getImage();
142 imageStatus_ = GraphicsCacheItem::Loaded;
144 lyxerr << "Fail." << endl;
145 imageStatus_ = GraphicsCacheItem::ErrorReading;
148 // remove the xpm file now.
149 lyx::unlink(xpmfile);
150 // and remove the reference to the filename.