1 /* This file is part of
2 * =================================================
4 * LyX, The Document Processor
5 * Copyright 1995 Matthias Ettrich.
6 * Copyright 1995-2001 The LyX Team.
8 * ================================================= */
13 #pragma implementation
16 #include "ImageLoaderXPM.h"
17 #include "ColorHandler.h"
21 #include "frontends/support/LyXImage.h"
22 #include "frontends/GUIRunTime.h"
24 #include "support/filetools.h"
25 #include "support/LAssert.h"
27 #include XPM_H_LOCATION
37 bool ImageLoaderXPM::isImageFormatOK(string const & filename) const
39 ifstream is(filename.c_str(), ios::in);
41 // The signature of the file without the spaces.
42 static char const str[] = "/*XPM*/";
43 char const * ptr = str;
45 for (; *ptr != '\0'; ++ptr) {
57 ImageLoaderXPM::FormatList const
58 ImageLoaderXPM::loadableFormats() const
61 formats.push_back("xpm");
68 ImageLoaderXPM::runImageLoader(string const & filename)
70 Display * display = GUIRunTime::x11Display();
73 // This might be a dirty thing, but I dont know any other solution.
74 Screen * screen = ScreenOfDisplay(display, GUIRunTime::x11Screen());
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;
83 xpm_col.value = "none";
84 xpm_col.pixel = lyxColorHandler->colorPixel(LColor::graphicsbg);
87 attrib.valuemask = XpmCloseness | XpmColorSymbols;
89 attrib.closeness = 10000;
91 attrib.numsymbols = 1;
92 attrib.colorsymbols = &xpm_col;
94 // Set color_key to monochrome, grayscale or color
95 // (Angus 21 Sep 2001)
97 if (lyxrc.display_graphics == "color") {
98 color_key = XPM_COLOR;
100 } else if (lyxrc.display_graphics == "gray") {
101 color_key = XPM_GRAY;
103 } else if (lyxrc.display_graphics == "mono") {
104 color_key = XPM_MONO;
107 // If setting color_key failed, then fail gracefully!
108 if (color_key != 0) {
109 attrib.valuemask |= XpmColorKey;
110 attrib.color_key = color_key;
113 lyxerr << "Warning in ImageLoaderXPM::runImageLoader"
114 << "lyxrc.display_graphics == \""
115 << lyxrc.display_graphics
120 // Load up the pixmap
121 int status = XpmReadFileToPixmap(
123 XRootWindowOfScreen(screen),
124 const_cast<char *>(filename.c_str()),
125 &pixmap, &mask, &attrib);
127 if (status != XpmSuccess) {
128 lyxerr << "Error reading XPM file '"
129 << XpmGetErrorString(status) << "'"
131 return ErrorWhileLoading;
134 // This should have been set by the XpmReadFileToPixmap call!
135 lyx::Assert(attrib.valuemask & XpmSize);
137 setImage(new LyXImage(pixmap, attrib.width, attrib.height));
139 XpmFreeAttributes(&attrib);