wmbiff: bumped to 0.4.34.
[dockapps.git] / Temperature.app / Xpm.cc
blob734556a8a4bb372e39b0e423444f5d81e556ade7
1 //
2 // Temperature.app
3 //
4 // Copyright (c) 2000-2002 Per Liden
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307,
19 // USA.
22 #include <X11/Xlib.h>
23 #include <X11/xpm.h>
24 #include <X11/extensions/shape.h>
25 #include <iostream>
26 #include <stdlib.h>
27 #include <string.h>
28 #include "Temperature.h"
29 #include "Xpm.h"
31 Xpm::Xpm(Display* display, Window root, char** data)
33 int error;
35 mDisplay = display;
37 mAttributes.valuemask = 0;
38 error = XpmCreatePixmapFromData(mDisplay, root, data, &mImage, &mMask, &mAttributes);
40 switch (error) {
41 case XpmColorError:
42 std::cerr << APPNAME << ": xpm image loaded but did not get all colors needed" << std::endl;
43 break;
45 case XpmColorFailed:
46 std::cerr << APPNAME << ": could not load xpm image (not enough colors available)" << std::endl;
47 exit(0);
48 break;
50 case XpmNoMemory:
51 std::cerr << APPNAME << ": could not load xpm image (not enough memory available)" << std::endl;
52 exit(0);
53 break;
55 case XpmOpenFailed:
56 case XpmFileInvalid:
57 std::cerr << APPNAME << ": could not load xpm image (image broken or corrupt)" << std::endl;
58 exit(0);
59 break;
61 case XpmSuccess:
62 default:
63 // Image loaded ok
64 break;
68 Xpm::~Xpm()
70 if (mImage) {
71 XFreePixmap(mDisplay, mImage);
74 if (mMask) {
75 XFreePixmap(mDisplay, mMask);
79 void Xpm::setWindowPixmap(Window win)
81 XResizeWindow(mDisplay, win, mAttributes.width, mAttributes.height);
82 XSetWindowBackgroundPixmap(mDisplay, win, mImage);
83 XClearWindow(mDisplay, win);
86 void Xpm::setWindowPixmapShaped(Window win)
88 XResizeWindow(mDisplay, win, mAttributes.width, mAttributes.height);
89 XSetWindowBackgroundPixmap(mDisplay, win, mImage);
90 XShapeCombineMask(mDisplay, win, ShapeBounding, 0, 0, mMask, ShapeSet);
91 XClearWindow(mDisplay, win);
94 void Xpm::drawString(int pos, char* font, char* str)
96 XFontStruct* fontStruct;
97 GC gc;
98 XGCValues gcv;
100 if ((fontStruct = XLoadQueryFont(mDisplay, font)) == 0) {
101 cerr << APPNAME << ": could not load font '" << font << "'" << endl;
102 exit(0);
105 gcv.foreground = WhitePixel(mDisplay, DefaultScreen(mDisplay));
106 gc = XCreateGC(mDisplay, mImage, GCForeground, &gcv);
108 int strLength = strlen(str);
109 int strWidth = XTextWidth(fontStruct, str, strLength);
111 int x = (64 / 2) - (strWidth / 2);
112 XSetFont(mDisplay, gc, fontStruct->fid);
113 XDrawString(mDisplay, mImage, gc, x, pos, str, strLength);
115 XFreeGC(mDisplay, gc);
116 XFreeFont(mDisplay, fontStruct);
119 void Xpm::drawComposedString(int pos, char* font1, char* str1, char* font2, char* str2)
121 XFontStruct* fontStruct1;
122 XFontStruct* fontStruct2;
123 GC gc;
124 XGCValues gcv;
126 if ((fontStruct1 = XLoadQueryFont(mDisplay, font1)) == 0) {
127 cerr << APPNAME << ": could not load font '" << font1 << "'" << endl;
128 exit(0);
131 if ((fontStruct2 = XLoadQueryFont(mDisplay, font2)) == 0) {
132 cerr << APPNAME << ": could not load font '" << font2 << "'" << endl;
133 exit(0);
136 gcv.foreground = WhitePixel(mDisplay, DefaultScreen(mDisplay));
137 gc = XCreateGC(mDisplay, mImage, GCForeground, &gcv);
139 int str1Length = strlen(str1);
140 int str1Width = XTextWidth(fontStruct1, str1, str1Length);
141 int str2Length = strlen(str2);
142 int str2Width = XTextWidth(fontStruct2, str2, str2Length);
144 int x = (64 / 2) - ((str1Width + str2Width) / 2);
145 XSetFont(mDisplay, gc, fontStruct1->fid);
146 XDrawString(mDisplay, mImage, gc, x, pos, str1, str1Length);
148 x += str1Width;
149 XSetFont(mDisplay, gc, fontStruct2->fid);
150 XDrawString(mDisplay, mImage, gc, x, pos, str2, str2Length);
152 XFreeGC(mDisplay, gc);
153 XFreeFont(mDisplay, fontStruct1);
154 XFreeFont(mDisplay, fontStruct2);