]> icculus.org git repositories - mikachu/openbox.git/blob - src/Texture.cc
import from bb-cvs
[mikachu/openbox.git] / src / Texture.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // Texture.cc for Blackbox - an X11 Window manager
3 // Copyright (c) 2001 - 2002 Sean 'Shaleh' Perry <shaleh at debian.org>
4 // Copyright (c) 1997 - 2000, 2002 Bradley T Hughes <bhughes at trolltech.com>
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a
7 // copy of this software and associated documentation files (the "Software"),
8 // to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 // and/or sell copies of the Software, and to permit persons to whom the
11 // Software is furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 // DEALINGS IN THE SOFTWARE.
23
24 #ifdef HAVE_CONFIG_H
25 #  include "../config.h"
26 #endif // HAVE_CONFIG_H
27
28 extern "C" {
29 #include <stdio.h>
30 #ifdef HAVE_CTYPE_H
31 #include <ctype.h>
32 #endif
33 }
34
35 #include "Texture.hh"
36 #include "BaseDisplay.hh"
37 #include "Image.hh"
38 #include "Screen.hh"
39 #include "blackbox.hh"
40
41 using std::string;
42
43
44 BTexture::BTexture(const BaseDisplay * const _display,
45                    unsigned int _screen, BImageControl* _ctrl)
46   : c(_display, _screen), ct(_display, _screen),
47     lc(_display, _screen), sc(_display, _screen), t(0),
48     dpy(_display), ctrl(_ctrl), scrn(_screen) { }
49
50
51 BTexture::BTexture(const string &d, const BaseDisplay * const _display,
52                    unsigned int _screen, BImageControl* _ctrl)
53   : c(_display, _screen), ct(_display, _screen),
54     lc(_display, _screen), sc(_display, _screen), t(0),
55     dpy(_display), ctrl(_ctrl), scrn(_screen) {
56   setDescription(d);
57 }
58
59
60 void BTexture::setColor(const BColor &cc) {
61   c = cc;
62   c.setDisplay(display(), screen());
63
64   unsigned char r, g, b, rr, gg, bb;
65
66   // calculate the light color
67   r = c.red();
68   g = c.green();
69   b = c.blue();
70   rr = r + (r >> 1);
71   gg = g + (g >> 1);
72   bb = b + (b >> 1);
73   if (rr < r) rr = ~0;
74   if (gg < g) gg = ~0;
75   if (bb < b) bb = ~0;
76   lc = BColor(rr, gg, bb, display(), screen());
77
78   // calculate the shadow color
79   r = c.red();
80   g = c.green();
81   b = c.blue();
82   rr = (r >> 2) + (r >> 1);
83   gg = (g >> 2) + (g >> 1);
84   bb = (b >> 2) + (b >> 1);
85   if (rr > r) rr = 0;
86   if (gg > g) gg = 0;
87   if (bb > b) bb = 0;
88   sc = BColor(rr, gg, bb, display(), screen());
89 }
90
91
92 void BTexture::setDescription(const string &d) {
93   descr.erase();
94   descr.reserve(d.length());
95
96   string::const_iterator it = d.begin(), end = d.end();
97   for (; it != end; ++it)
98     descr += tolower(*it);
99
100   if (descr.find("parentrelative") != string::npos) {
101     setTexture(BTexture::Parent_Relative);
102   } else {
103     setTexture(0);
104
105     if (descr.find("gradient") != string::npos) {
106       addTexture(BTexture::Gradient);
107       if (descr.find("crossdiagonal") != string::npos)
108         addTexture(BTexture::CrossDiagonal);
109       else if (descr.find("rectangle") != string::npos)
110         addTexture(BTexture::Rectangle);
111       else if (descr.find("pyramid") != string::npos)
112         addTexture(BTexture::Pyramid);
113       else if (descr.find("pipecross") != string::npos)
114         addTexture(BTexture::PipeCross);
115       else if (descr.find("elliptic") != string::npos)
116         addTexture(BTexture::Elliptic);
117       else if (descr.find("diagonal") != string::npos)
118         addTexture(BTexture::Diagonal);
119       else if (descr.find("horizontal") != string::npos)
120         addTexture(BTexture::Horizontal);
121       else if (descr.find("vertical") != string::npos)
122         addTexture(BTexture::Vertical);
123       else
124         addTexture(BTexture::Diagonal);
125     } else {
126       addTexture(BTexture::Solid);
127     }
128
129     if (descr.find("raised") != string::npos)
130       addTexture(BTexture::Raised);
131     else if (descr.find("sunken") != string::npos)
132       addTexture(BTexture::Sunken);
133     else if (descr.find("flat") != string::npos)
134       addTexture(BTexture::Flat);
135     else
136       addTexture(BTexture::Raised);
137
138     if (! (texture() & BTexture::Flat)) {
139       if (descr.find("bevel2") != string::npos)
140         addTexture(BTexture::Bevel2);
141       else
142         addTexture(BTexture::Bevel1);
143     }
144
145     if (descr.find("interlaced") != string::npos)
146       addTexture(BTexture::Interlaced);
147   }
148 }
149
150 void BTexture::setDisplay(const BaseDisplay * const _display,
151                           const unsigned int _screen) {
152   if (_display == display() && _screen == screen()) {
153     // nothing to do
154     return;
155   }
156
157   dpy = _display;
158   scrn = _screen;
159   c.setDisplay(_display, _screen);
160   ct.setDisplay(_display, _screen);
161   lc.setDisplay(_display, _screen);
162   sc.setDisplay(_display, _screen);
163 }
164
165
166 BTexture& BTexture::operator=(const BTexture &tt) {
167   c  = tt.c;
168   ct = tt.ct;
169   lc = tt.lc;
170   sc = tt.sc;
171   descr = tt.descr;
172   t  = tt.t;
173   dpy = tt.dpy;
174   scrn = tt.scrn;
175   ctrl = tt.ctrl;
176
177   return *this;
178 }
179
180
181 Pixmap BTexture::render(const unsigned int width, const unsigned int height,
182                         const Pixmap old) {
183   assert(display() != 0);
184
185   if (texture() == (BTexture::Flat | BTexture::Solid))
186     return None;
187   if (texture() == BTexture::Parent_Relative)
188     return ParentRelative;
189
190   if (screen() == ~(0u))
191     scrn = DefaultScreen(display()->getXDisplay());
192
193   assert(ctrl != 0);
194   Pixmap ret = ctrl->renderImage(width, height, *this);
195
196   if (old)
197     ctrl->removeImage(old);
198
199   return ret;
200 }