]> git.lyx.org Git - lyx.git/blob - src/mathed/preview.C
Painter and scrollbar API patches
[lyx.git] / src / mathed / preview.C
1
2 #include <config.h>
3
4 #include "formula.h"
5 #include "debug.h"
6 #include "frontends/Painter.h"
7 #include "support/systemcall.h"
8 #include "graphics/GraphicsTypes.h"
9 #include "graphics/GraphicsImage.h"
10 #include "graphics/GraphicsImageXPM.h"
11
12 #include <fstream>
13 #include <map>
14
15
16 using namespace std;
17
18
19 namespace {
20
21         typedef map<string, grfx::ImagePtr> previews_map;
22
23         // cache for computed previews
24         previews_map thePreviews;
25
26         // cache for scedule previews
27         vector<string> theSchedule;
28 }
29
30
31 void imageLoaded()
32 {
33 }
34
35
36 grfx::ImagePtr preview(string const & str)
37 {
38         // do we already have access to a rendered version?
39         previews_map::const_iterator it = thePreviews.find(str);
40         if (it != thePreviews.end())
41                 return it->second;      
42
43         // constructing new item
44         grfx::ImagePtr & im = thePreviews[str];
45
46         lyxerr << "writing: " << str << endl;
47         std::ofstream of("/tmp/previewlyx.tex");
48         of << "\\documentclass{article}"
49            << "\\usepackage{amssymb}"
50            << "\\thispagestyle{empty}"
51            << "\\begin{document}"
52            << str
53            << "\\end{document}\n";
54         of.close();
55
56         Systemcall sc1;
57         sc1.startscript(Systemcall::Wait,
58                 "(cd /tmp ; latex previewlyx.tex ; dvips previewlyx.dvi)");
59
60         Systemcall sc2;
61         sc2.startscript(Systemcall::Wait,
62                 "(cd /tmp ; convert previewlyx.ps previewlyx.xpm)");
63
64         //grfx::SignalLoadTypePtr on_finish;
65         //on_finish.reset(new SignalLoadType);
66         //on_finish->connect(SigC::slot(this, &imageLoaded));
67
68         // load image
69         XpmImage * xpm_image = new XpmImage;
70         int const success =
71                 XpmReadFileToXpmImage("/tmp/previewlyx.ps", xpm_image, 0);
72
73         switch (success) {
74         case XpmOpenFailed:
75                 lyxerr[Debug::GRAPHICS]
76                         << "No XPM image file found." << std::endl;
77                 break;
78
79         case XpmFileInvalid:
80                 lyxerr[Debug::GRAPHICS]
81                         << "File format is invalid" << std::endl;
82                 break;
83
84         case XpmNoMemory:
85                 lyxerr[Debug::GRAPHICS]
86                         << "Insufficient memory to read in XPM file"
87                         << std::endl;
88                 break;
89         }
90
91         if (success != XpmSuccess) {
92                 XpmFreeXpmImage(xpm_image);
93                 delete xpm_image;
94
95                 lyxerr[Debug::GRAPHICS]
96                         << "Error reading XPM file '"
97                         << XpmGetErrorString(success) << "'"
98                         << std::endl;
99         } else {
100                 //grfx::GImageXPM * xim = static_cast<grfx::GImageXPM *>(im.get());
101                 //xim->image_.reset(*xpm_image);
102         }
103
104         return im;
105 }
106