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