]> git.lyx.org Git - lyx.git/blob - src/graphics/XPM_Renderer.C
Cleaned up cruft in InsetGraphics.
[lyx.git] / src / graphics / XPM_Renderer.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 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 #include <config.h>
17 #include "XPM_Renderer.h"
18 #include "frontends/support/LyXImage.h"
19
20 #include FORMS_H_LOCATION
21 #include XPM_H_LOCATION
22 #include <iostream>
23 #include <fstream>
24
25 #include "support/LAssert.h"
26 #include "debug.h"
27
28 using std::endl;
29 using std::ios;
30
31
32 XPM_Renderer::XPM_Renderer()
33         : Renderer()
34 {}
35
36
37 bool XPM_Renderer::renderImage()
38 {
39         Pixmap pixmap;
40         Pixmap mask;
41         XpmAttributes attrib;
42         attrib.valuemask = 0;
43         
44         Display * display = fl_get_display();
45
46 //(BE 2000-08-05)
47 //#warning This might be a dirty thing, but I dont know any other solution.
48         Screen * screen = ScreenOfDisplay(display, fl_screen);
49
50         int status = XpmReadFileToPixmap(
51                         display, 
52                         XRootWindowOfScreen(screen), 
53                         const_cast<char *>(getFilename().c_str()), 
54                         &pixmap, &mask, &attrib);
55
56         if (status != XpmSuccess) {
57                 lyxerr << "Error reading XPM file '" 
58                         << XpmGetErrorString(status) 
59                         << endl;
60                 return false;
61         }
62         
63         // This should have been set by the XpmReadFileToPixmap call!
64         Assert(attrib.valuemask & XpmSize);
65
66         setPixmap(new LyXImage(pixmap), attrib.width, attrib.height);
67
68         XpmFreeAttributes(&attrib);
69
70         return true;
71 }
72
73
74 bool XPM_Renderer::isImageFormatOK(string const & filename) const
75 {
76         std::ifstream is(filename.c_str(), ios::in);
77
78         // The signature of the file without the spaces.
79         static const char str[] = "/*XPM*/";
80         const char * ptr = str;
81
82         do {
83                 char c;
84                 is >> c;
85
86                 if (c != *ptr)
87                         return false;
88                 
89                 ++ptr;
90         } while (*ptr != '\0');
91
92         return true;
93 }