]> git.lyx.org Git - lyx.git/blob - src/graphics/ImageLoaderXPM.C
Added new FINISED states FINISHED_RIGHT, FINISHED_UP, FINISHED_DOWN.
[lyx.git] / src / graphics / ImageLoaderXPM.C
1 /* This file is part of
2  * =================================================
3  * 
4  *          LyX, The Document Processor
5  *          Copyright 1995 Matthias Ettrich.
6  *          Copyright 1995-2001 The LyX Team.
7  *
8  * ================================================= */
9
10 #ifdef __GNUG__
11 #pragma implementation
12 #endif
13
14 #include <config.h>
15 #include "ImageLoaderXPM.h"
16 #include "frontends/support/LyXImage.h"
17 #include "frontends/GUIRunTime.h"
18 #include "support/filetools.h"
19
20 #include XPM_H_LOCATION
21 #include <iostream>
22 #include <fstream>
23
24 #include "support/LAssert.h"
25 #include "debug.h"
26
27 using std::endl;
28 using std::ios;
29
30 bool ImageLoaderXPM::isImageFormatOK(string const & filename) const
31 {
32         std::ifstream is(filename.c_str(), ios::in);
33
34         // The signature of the file without the spaces.
35         static char const str[] = "/*XPM*/";
36         char const * ptr = str;
37
38         for (; *ptr != '\0'; ++ptr) {
39                 char c;
40                 is >> c;
41
42                 if (c != *ptr)
43                         return false;
44         }
45
46         return true;
47 }
48
49 ImageLoaderXPM::FormatList const 
50 ImageLoaderXPM::loadableFormats() const 
51 {
52         FormatList formats;
53         formats.push_back("xpm");
54
55         return formats;
56 }
57         
58 ImageLoader::Result 
59 ImageLoaderXPM::runImageLoader(string const & filename)
60 {
61         Display * display = GUIRunTime::x11Display();
62
63 //(BE 2000-08-05)
64 #ifdef WITH_WARNINGS
65 #warning This might be a dirty thing, but I dont know any other solution.
66 #endif
67         Screen * screen = ScreenOfDisplay(display, GUIRunTime::x11Screen());
68
69         Pixmap pixmap;
70         Pixmap mask;
71         XpmAttributes attrib;
72         
73         attrib.valuemask = XpmCloseness | XpmAllocColor;
74         attrib.closeness = 10000;
75         
76         int status = XpmReadFileToPixmap(
77                         display, 
78                         XRootWindowOfScreen(screen), 
79                         const_cast<char *>(filename.c_str()), 
80                         &pixmap, &mask, &attrib);
81
82         if (status != XpmSuccess) {
83                 lyxerr << "Error reading XPM file '" 
84                         << XpmGetErrorString(status) << "'"
85                         << endl;
86                 return ErrorWhileLoading;
87         }
88         
89         // This should have been set by the XpmReadFileToPixmap call!
90         lyx::Assert(attrib.valuemask & XpmSize);
91
92         setImage(new LyXImage(pixmap, attrib.width, attrib.height));
93
94         XpmFreeAttributes(&attrib);
95
96         return OK;
97 }