1 //
2 //  Mixer.app
3 //
4 //  Copyright (c) 1998-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.
10 //
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.
15 //
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 //  USA.
20 //
21 
22 #include <X11/Xlib.h>
23 #include <X11/xpm.h>
24 #include <X11/extensions/shape.h>
25 #include <iostream>
26 #include <cstdlib>
27 #include <cstring>
28 #include "Mixer.h"
29 #include "Xpm.h"
30 
31 using namespace std;
32 
Xpm(Display * display,Window root,char ** data)33 Xpm::Xpm(Display* display, Window root, char** data)
34 {
35    int error;
36 
37    mDisplay = display;
38 
39    mAttributes.valuemask = 0;
40    error = XpmCreatePixmapFromData(mDisplay, root, data, &mImage, &mMask, &mAttributes);
41 
42    switch (error) {
43    case XpmColorError:
44       cerr << APPNAME << ": xpm image loaded but did not get all colors needed" << endl;
45       break;
46 
47    case XpmColorFailed:
48       cerr << APPNAME << ": could not load xpm image (not enough colors available)" << endl;
49       exit(0);
50       break;
51 
52    case XpmNoMemory:
53       cerr << APPNAME << ": could not load xpm image (not enough memory available)" << endl;
54       exit(0);
55       break;
56 
57    case XpmOpenFailed:
58    case XpmFileInvalid:
59       cerr << APPNAME << ": could not load xpm image (image broken or corrupt)" << endl;
60       exit(0);
61       break;
62 
63    case XpmSuccess:
64    default:
65       // Image loaded ok
66       break;
67    }
68 }
69 
~Xpm()70 Xpm::~Xpm()
71 {
72    if (mImage) {
73       XFreePixmap(mDisplay, mImage);
74    }
75 
76    if (mMask) {
77       XFreePixmap(mDisplay, mMask);
78    }
79 }
80 
setWindowPixmap(Window win)81 void Xpm::setWindowPixmap(Window win)
82 {
83    XResizeWindow(mDisplay, win, mAttributes.width, mAttributes.height);
84    XSetWindowBackgroundPixmap(mDisplay, win, mImage);
85 }
86 
setWindowPixmapShaped(Window win)87 void Xpm::setWindowPixmapShaped(Window win)
88 {
89    XResizeWindow(mDisplay, win, mAttributes.width, mAttributes.height);
90    XSetWindowBackgroundPixmap(mDisplay, win, mImage);
91    XShapeCombineMask(mDisplay, win, ShapeBounding, 0, 0, mMask, ShapeSet);
92 }
93 
drawString(int x,int y,char * text)94 void Xpm::drawString(int x, int y, char* text)
95 {
96    Font      font;
97    GC        gc;
98    XGCValues gcv;
99 
100    font = XLoadFont(mDisplay, LABEL_FONT);
101    gcv.font = font;
102    gcv.foreground = WhitePixel(mDisplay, DefaultScreen(mDisplay));
103    gc = XCreateGC(mDisplay, mImage, GCFont | GCForeground, &gcv);
104    XDrawString(mDisplay, mImage, gc, x, y, text, strlen(text));
105    XFreeGC(mDisplay, gc);
106    XUnloadFont(mDisplay, font);
107 }
108