]> git.lyx.org Git - lyx.git/blob - src/graphics/ImageLoaderXPM.C
small fix with footnote, use stringstream some more
[lyx.git] / src / graphics / ImageLoaderXPM.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  * ================================================= */
10
11 #ifdef __GNUG__
12 #pragma implementation
13 #endif
14
15 #include <config.h>
16 #include "ImageLoaderXPM.h"
17 #include "frontends/support/LyXImage.h"
18 #include "support/filetools.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 bool ImageLoaderXPM::isImageFormatOK(string const & filename) const
32 {
33         std::ifstream is(filename.c_str(), ios::in);
34
35         // The signature of the file without the spaces.
36         static char const str[] = "/*XPM*/";
37         char const * ptr = str;
38
39         for (; *ptr != '\0'; ++ptr) {
40                 char c;
41                 is >> c;
42
43                 if (c != *ptr)
44                         return false;
45         }
46
47         return true;
48 }
49
50 ImageLoaderXPM::FormatList const 
51 ImageLoaderXPM::loadableFormats() const 
52 {
53         FormatList formats;
54         formats.push_back("xpm");
55
56         return formats;
57 }
58         
59 ImageLoader::Result 
60 ImageLoaderXPM::runImageLoader(string const & filename)
61 {
62         Display * display = fl_get_display();
63
64 //(BE 2000-08-05)
65 #ifdef WITH_WARNINGS
66 #warning This might be a dirty thing, but I dont know any other solution.
67 #endif
68         Screen * screen = ScreenOfDisplay(display, fl_screen);
69
70         Pixmap pixmap;
71         Pixmap mask;
72         XpmAttributes attrib;
73         attrib.valuemask = 0;
74         
75         int status = XpmReadFileToPixmap(
76                         display, 
77                         XRootWindowOfScreen(screen), 
78                         const_cast<char *>(filename.c_str()), 
79                         &pixmap, &mask, &attrib);
80
81         if (status != XpmSuccess) {
82                 lyxerr << "Error reading XPM file '" 
83                         << XpmGetErrorString(status) 
84                         << endl;
85                 return ErrorWhileLoading;
86         }
87         
88         // This should have been set by the XpmReadFileToPixmap call!
89         lyx::Assert(attrib.valuemask & XpmSize);
90
91         setImage(new LyXImage(pixmap, attrib.width, attrib.height));
92
93         XpmFreeAttributes(&attrib);
94
95         return OK;
96 }