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 * ================================================= */
11 #pragma implementation
15 #include "ImageLoaderXPM.h"
16 #include "ColorHandler.h"
18 #include "frontends/support/LyXImage.h"
19 #include "frontends/GUIRunTime.h"
20 #include "support/filetools.h"
22 #include XPM_H_LOCATION
26 #include "support/LAssert.h"
32 bool ImageLoaderXPM::isImageFormatOK(string const & filename) const
34 std::ifstream is(filename.c_str(), ios::in);
36 // The signature of the file without the spaces.
37 static char const str[] = "/*XPM*/";
38 char const * ptr = str;
40 for (; *ptr != '\0'; ++ptr) {
51 ImageLoaderXPM::FormatList const
52 ImageLoaderXPM::loadableFormats() const
55 formats.push_back("xpm");
61 ImageLoaderXPM::runImageLoader(string const & filename)
63 Display * display = GUIRunTime::x11Display();
66 // This might be a dirty thing, but I dont know any other solution.
67 Screen * screen = ScreenOfDisplay(display, GUIRunTime::x11Screen());
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;
76 xpm_col.value = "none";
77 xpm_col.pixel = lyxColorHandler->colorPixel(LColor::graphicsbg);
80 attrib.valuemask = XpmCloseness | XpmColorSymbols;
82 attrib.closeness = 10000;
84 attrib.numsymbols = 1;
85 attrib.colorsymbols = &xpm_col;
87 // Set color_key to monochrome, grayscale or color
88 // (Angus 21 Sep 2001)
90 if (lyxrc.display_graphics == "color") {
91 color_key = XPM_COLOR;
93 } else if (lyxrc.display_graphics == "gray") {
96 } else if (lyxrc.display_graphics == "mono") {
100 // If setting color_key failed, then fail gracefully!
101 if (color_key != 0) {
102 attrib.valuemask |= XpmColorKey;
103 attrib.color_key = color_key;
106 lyxerr << "Warning in ImageLoaderXPM::runImageLoader"
107 << "lyxrc.display_graphics == \""
108 << lyxrc.display_graphics
113 // Load up the pixmap
114 int status = XpmReadFileToPixmap(
116 XRootWindowOfScreen(screen),
117 const_cast<char *>(filename.c_str()),
118 &pixmap, &mask, &attrib);
120 if (status != XpmSuccess) {
121 lyxerr << "Error reading XPM file '"
122 << XpmGetErrorString(status) << "'"
124 return ErrorWhileLoading;
127 // This should have been set by the XpmReadFileToPixmap call!
128 lyx::Assert(attrib.valuemask & XpmSize);
130 setImage(new LyXImage(pixmap, attrib.width, attrib.height));
132 XpmFreeAttributes(&attrib);