]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsCacheItem_pimpl.C
Baruch's InsetGraphics - LyXImage patch
[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 "graphics/XPM_Renderer.h"
27 #include "graphics/EPS_Renderer.h"
28 #include "support/filetools.h"
29 #include "debug.h"
30 #include "support/LAssert.h"
31
32 using std::endl;
33 using std::map;
34
35
36 GraphicsCacheItem_pimpl::GraphicsCacheItem_pimpl()
37         : height_(-1), width_(-1), imageStatus_(GraphicsCacheItem::Loading),
38           pixmap_(0), renderer(0), refCount(0)
39 {}
40
41
42 GraphicsCacheItem_pimpl::~GraphicsCacheItem_pimpl()
43 {
44         if (imageStatus_ == GraphicsCacheItem::Loaded) {
45                 XFreePixmap(fl_get_display(), pixmap_);
46         }
47         delete renderer;
48 }
49
50
51 bool
52 GraphicsCacheItem_pimpl::setFilename(string const & filename)
53 {
54         imageStatus_ = GraphicsCacheItem::Loading;
55
56         renderer = new XPM_Renderer();
57         if (renderXPM(filename))
58                 return true;
59         
60         return false;
61 }
62
63
64 /*** Callback method ***/
65
66 typedef map<string, GraphicsCacheItem_pimpl*> CallbackMap;
67 static CallbackMap callbackMap;
68
69
70 void
71 callback(string cmd, int retval)
72 {
73         lyxerr << "callback, cmd=" << cmd << ", retval=" << retval << endl;
74
75         GraphicsCacheItem_pimpl * item = callbackMap[cmd];
76         callbackMap.erase(cmd);
77         
78         item->imageConverted(retval);
79 }
80
81
82 void
83 GraphicsCacheItem_pimpl::imageConverted(int retval)
84 {
85         lyxerr << "imageConverted, retval=" << retval << endl;
86
87         if (retval) {
88                 imageStatus_ = GraphicsCacheItem::ErrorConverting;
89                 return;
90         }
91
92         // Do the actual image loading from XPM to memory.
93         loadXPMImage(); 
94 }
95
96 /**********************/
97
98 bool
99 GraphicsCacheItem_pimpl::renderXPM(string const & filename)
100 {
101         // Create the command to do the conversion, this depends on ImageMagicks
102         // convert program.
103         string command = "convert ";
104         command += filename;
105         command += " XPM:";
106
107         // Take only the filename part of the file, without path or extension.
108         string temp = OnlyFilename(filename);
109         temp = ChangeExtension(filename, string());
110         
111         // Add some stuff to have it a unique temp file.
112         xpmfile = TmpFileName(string(), temp);
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         if (!renderer->setFilename(xpmfile)) {
138                 return;
139         }
140
141         if (renderer->renderImage()) {
142                 pixmap_ = renderer->getPixmap();
143                 width_ = renderer->getWidth();
144                 height_ = renderer->getHeight();
145                 imageStatus_ = GraphicsCacheItem::Loaded;
146         } else {
147                 imageStatus_ = GraphicsCacheItem::ErrorReading;
148         }
149
150         // remove the xpm file now.
151         lyx::unlink(xpmfile);
152         // and remove the reference to the filename.
153         xpmfile = string();
154 }