]> git.lyx.org Git - features.git/blob - src/graphics/ImageLoaderXPM.C
to much stuff for my liking...
[features.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 #include <config.h>
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 #include "ImageLoaderXPM.h"
17 #include "ColorHandler.h"
18 #include "lyxrc.h"
19 #include "debug.h"
20
21 #include "frontends/support/LyXImage.h"
22 #include "frontends/GUIRunTime.h"
23
24 #include "support/filetools.h"
25 #include "support/LAssert.h"
26
27 #include XPM_H_LOCATION
28 #include <iostream>
29 #include <fstream>
30
31
32 using std::ifstream;
33 using std::endl;
34 using std::ios;
35
36
37 bool ImageLoaderXPM::isImageFormatOK(string const & filename) const
38 {
39         ifstream is(filename.c_str(), ios::in);
40
41         // The signature of the file without the spaces.
42         static char const str[] = "/*XPM*/";
43         char const * ptr = str;
44
45         for (; *ptr != '\0'; ++ptr) {
46                 char c;
47                 is >> c;
48
49                 if (c != *ptr)
50                         return false;
51         }
52
53         return true;
54 }
55
56
57 ImageLoaderXPM::FormatList const 
58 ImageLoaderXPM::loadableFormats() const 
59 {
60         FormatList formats;
61         formats.push_back("xpm");
62
63         return formats;
64 }
65
66
67 ImageLoader::Result 
68 ImageLoaderXPM::runImageLoader(string const & filename)
69 {
70         Display * display = GUIRunTime::x11Display();
71
72         //(BE 2000-08-05)
73         // This might be a dirty thing, but I dont know any other solution.
74         Screen * screen = ScreenOfDisplay(display, GUIRunTime::x11Screen());
75
76         Pixmap pixmap;
77         Pixmap mask;
78
79         // If the pixmap contains a transparent colour, then set it to the
80         // colour of the background (Angus 21 Sep 2001)
81         XpmColorSymbol xpm_col;
82         xpm_col.name = 0;
83         xpm_col.value = "none";
84         xpm_col.pixel = lyxColorHandler->colorPixel(LColor::graphicsbg);
85
86         XpmAttributes attrib;
87         attrib.valuemask = XpmCloseness | XpmColorSymbols;
88
89         attrib.closeness = 10000;
90
91         attrib.numsymbols = 1;
92         attrib.colorsymbols = &xpm_col;
93
94         // Set color_key to monochrome, grayscale or color
95         // (Angus 21 Sep 2001)
96         int color_key = 0;
97         if (lyxrc.display_graphics == "color") {
98                 color_key = XPM_COLOR;
99
100         } else if (lyxrc.display_graphics == "gray") {
101                 color_key = XPM_GRAY;
102
103         } else if (lyxrc.display_graphics == "mono") {
104                 color_key = XPM_MONO;
105         }
106
107         // If setting color_key failed, then fail gracefully!
108         if (color_key != 0) {
109                 attrib.valuemask |= XpmColorKey;
110                 attrib.color_key = color_key;
111
112         } else {
113                 lyxerr << "Warning in ImageLoaderXPM::runImageLoader"
114                        << "lyxrc.display_graphics == \""
115                        << lyxrc.display_graphics
116                        << "\""
117                        << endl;
118         }               
119
120         // Load up the pixmap
121         int status = XpmReadFileToPixmap(
122                         display, 
123                         XRootWindowOfScreen(screen), 
124                         const_cast<char *>(filename.c_str()), 
125                         &pixmap, &mask, &attrib);
126
127         if (status != XpmSuccess) {
128                 lyxerr << "Error reading XPM file '" 
129                        << XpmGetErrorString(status) << "'"
130                        << endl;
131                 return ErrorWhileLoading;
132         }
133
134         // This should have been set by the XpmReadFileToPixmap call!
135         lyx::Assert(attrib.valuemask & XpmSize);
136
137         setImage(new LyXImage(pixmap, attrib.width, attrib.height));
138
139         XpmFreeAttributes(&attrib);
140
141         return OK;
142 }