]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsCacheItem_pimpl.C
Cleaned up cruft in InsetGraphics.
[lyx.git] / src / graphics / GraphicsCacheItem_pimpl.C
1 // -*- C++ -*-
2 /* This file is part of
3  * =================================================
4  * 
5  *          LyX, The Document Processor
6  *          Copyright 1995 Matthias Ettrich.
7  *          Copyright 1995-2000 The LyX Team.
8  *
9  *          This file Copyright 2000 Baruch Even
10  * ================================================= */
11
12 #include <config.h>
13
14 #include <map>
15
16 #include FORMS_H_LOCATION
17
18 #ifdef __GNUG__
19 #pragma implementation
20 #endif
21
22 #include "GraphicsCacheItem.h"
23 #include "GraphicsCacheItem_pimpl.h"
24
25 #include "frontends/support/LyXImage.h"
26 #include "ImageLoaderXPM.h"
27 #include "support/filetools.h"
28 #include "debug.h"
29 #include "support/LAssert.h"
30
31 using std::endl;
32 using std::map;
33
34
35 GraphicsCacheItem_pimpl::GraphicsCacheItem_pimpl()
36         : imageStatus_(GraphicsCacheItem::Loading),
37           image_(0), imageLoader(0), refCount(0)
38 {}
39
40
41 GraphicsCacheItem_pimpl::~GraphicsCacheItem_pimpl()
42 {
43         delete image_; image_ = 0;
44         delete imageLoader; imageLoader = 0;
45 }
46
47
48 bool
49 GraphicsCacheItem_pimpl::setFilename(string const & filename)
50 {
51         imageLoader = new ImageLoaderXPM();
52         imageStatus_ = GraphicsCacheItem::Loading;
53         
54         if (renderXPM(filename))
55                 return true;
56         
57         return false;
58 }
59
60
61 /*** Callback method ***/
62
63 typedef map<string, GraphicsCacheItem_pimpl*> CallbackMap;
64 static CallbackMap callbackMap;
65
66
67 void
68 static callback(string cmd, int retval)
69 {
70         lyxerr << "callback, cmd=" << cmd << ", retval=" << retval << endl;
71
72         GraphicsCacheItem_pimpl * item = callbackMap[cmd];
73         callbackMap.erase(cmd);
74         
75         item->imageConverted(retval);
76 }
77
78
79 void
80 GraphicsCacheItem_pimpl::imageConverted(int retval)
81 {
82         lyxerr << "imageConverted, retval=" << retval << endl;
83
84         if (retval) {
85                 lyxerr << "(GraphicsCacheItem_pimpl::imageConverter) "
86                         "Error converting image." << endl;
87                 imageStatus_ = GraphicsCacheItem::ErrorConverting;
88                 return;
89         }
90
91         // Do the actual image loading from XPM to memory.
92         loadXPMImage(); 
93 }
94
95 /**********************/
96
97 bool
98 GraphicsCacheItem_pimpl::renderXPM(string const & filename)
99 {
100         // Create the command to do the conversion, this depends on ImageMagicks
101         // convert program.
102         string command = "convert ";
103         command += filename;
104         command += " XPM:";
105
106         // Take only the filename part of the file, without path or extension.
107         string temp = OnlyFilename(filename);
108         temp = ChangeExtension(filename, string());
109         
110         // Add some stuff to have it a unique temp file.
111         xpmfile = lyx::tempName(string(), temp);
112 #warning When is this tempfile unlinked? (Lgb)
113         xpmfile = ChangeExtension(xpmfile, ".xpm");     
114         
115         command += xpmfile;
116
117         // Set the callback mapping to point to us.
118         callbackMap[command] = this;
119
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);
127
128         return true;
129 }
130
131
132 // This function gets called from the callback after the image has been
133 // converted successfully.
134 void
135 GraphicsCacheItem_pimpl::loadXPMImage()
136 {
137         lyxerr << "Loading XPM Image... ";
138         
139         if (imageLoader->loadImage(xpmfile)) {
140                 lyxerr << "Success." << endl;
141                 image_ = imageLoader->getImage();
142                 imageStatus_ = GraphicsCacheItem::Loaded;
143         } else {
144                 lyxerr << "Fail." << endl;
145                 imageStatus_ = GraphicsCacheItem::ErrorReading;
146         }
147
148         // remove the xpm file now.
149         lyx::unlink(xpmfile);
150         // and remove the reference to the filename.
151         xpmfile = string();
152 }