]> git.lyx.org Git - lyx.git/blob - src/graphics/ImageLoaderXPM.C
In for a penny, in for a pound. Consistent use of // -*- C++ -*-
[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 "support/filetools.h"
18
19 #include FORMS_H_LOCATION
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 = fl_get_display();
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, fl_screen);
68
69         Pixmap pixmap;
70         Pixmap mask;
71         XpmAttributes attrib;
72         attrib.valuemask = 0;
73         
74         int status = XpmReadFileToPixmap(
75                         display, 
76                         XRootWindowOfScreen(screen), 
77                         const_cast<char *>(filename.c_str()), 
78                         &pixmap, &mask, &attrib);
79
80         if (status != XpmSuccess) {
81                 lyxerr << "Error reading XPM file '" 
82                         << XpmGetErrorString(status) 
83                         << endl;
84                 return ErrorWhileLoading;
85         }
86         
87         // This should have been set by the XpmReadFileToPixmap call!
88         lyx::Assert(attrib.valuemask & XpmSize);
89
90         setImage(new LyXImage(pixmap, attrib.width, attrib.height));
91
92         XpmFreeAttributes(&attrib);
93
94         return OK;
95 }