~ubuntu-branches/ubuntu/trusty/fluxbox/trusty-proposed

« back to all changes in this revision

Viewing changes to src/FbTk/ThemeItems.cc

  • Committer: Bazaar Package Importer
  • Author(s): Dmitry E. Oboukhov
  • Date: 2008-07-01 10:38:14 UTC
  • mfrom: (2.1.12 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080701103814-khx2b6il152x9p93
Tags: 1.0.0+deb1-8
* x-dev has been removed from build-depends (out-of-date package).
* Standards-Version bumped to 3.8.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// ThemeItems.cc for FbTk - Fluxbox ToolKit
2
 
// Copyright (c) 2002 - 2005 Henrik Kinnunen (fluxgen at fluxbox dot org)
3
 
//
4
 
// Permission is hereby granted, free of charge, to any person obtaining a
5
 
// copy of this software and associated documentation files (the "Software"),
6
 
// to deal in the Software without restriction, including without limitation
7
 
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
 
// and/or sell copies of the Software, and to permit persons to whom the
9
 
// Software is furnished to do so, subject to the following conditions:
10
 
//
11
 
// The above copyright notice and this permission notice shall be included in
12
 
// all copies or substantial portions of the Software.
13
 
//
14
 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
 
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17
 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
 
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20
 
// DEALINGS IN THE SOFTWARE.
21
 
 
22
 
// $Id: ThemeItems.cc 4107 2005-09-14 19:22:59Z fluxgen $
23
 
 
24
 
/// @file implements common theme items
25
 
 
26
 
#ifndef THEMEITEMS_HH
27
 
#define THEMEITEMS_HH
28
 
 
29
 
#ifdef    HAVE_CONFIG_H
30
 
#include "config.h"
31
 
#endif // HAVE_CONFIG_H
32
 
 
33
 
#include "Theme.hh"
34
 
#include "Color.hh"
35
 
#include "Texture.hh"
36
 
#include "Font.hh"
37
 
#include "GContext.hh"
38
 
#include "PixmapWithMask.hh"
39
 
#include "Image.hh"
40
 
#include "StringUtil.hh"
41
 
 
42
 
#include <string>
43
 
#ifdef HAVE_CSTDIO
44
 
  #include <cstdio>
45
 
#else
46
 
  #include <stdio.h>
47
 
#endif
48
 
 
49
 
#include <iostream>
50
 
#include <memory>
51
 
 
52
 
namespace FbTk {
53
 
 
54
 
using std::string;
55
 
using std::cerr;
56
 
using std::endl;
57
 
 
58
 
 
59
 
// create default handlers for Color, Font, Texture, int and string
60
 
template <>
61
 
void ThemeItem<string>::load(const string *name, const string *altname) { }
62
 
 
63
 
template <>
64
 
void ThemeItem<string>::setDefaultValue() {
65
 
    *(*this) = "";
66
 
}
67
 
 
68
 
template <>
69
 
void ThemeItem<string>::setFromString(const char *str) {
70
 
    *(*this) = (str ? str : "");
71
 
}
72
 
 
73
 
template <>
74
 
void ThemeItem<int>::load(const string *name, const string *altname) { }
75
 
 
76
 
template<>
77
 
void ThemeItem<int>::setDefaultValue() {
78
 
    *(*this) = 0;
79
 
}
80
 
 
81
 
template <>
82
 
void ThemeItem<int>::setFromString(const char *str) {
83
 
    if (str == 0) {
84
 
        setDefaultValue();
85
 
        return;
86
 
    }
87
 
 
88
 
    if (sscanf(str, "%d", &m_value) < 1)
89
 
        setDefaultValue();
90
 
}
91
 
template<>
92
 
void FbTk::ThemeItem<bool>::load(const std::string *name, const std::string *altname) { }
93
 
 
94
 
template<>
95
 
void FbTk::ThemeItem<bool>::setDefaultValue() {
96
 
    *(*this) = false;
97
 
}
98
 
 
99
 
template<>
100
 
void FbTk::ThemeItem<bool>::setFromString(char const *strval) {
101
 
    if (strcasecmp(strval, "true")==0)
102
 
        *(*this) = true;
103
 
    else
104
 
        *(*this) = false;
105
 
}
106
 
 
107
 
template <>
108
 
void ThemeItem<unsigned int>::setDefaultValue() {
109
 
    m_value = 0;
110
 
}
111
 
 
112
 
template <>
113
 
void ThemeItem<unsigned int>::setFromString(const char *str) {
114
 
    sscanf(str, "%d", &m_value);
115
 
}
116
 
 
117
 
template <>
118
 
void ThemeItem<unsigned int>::load(const std::string *name, const std::string *altname) {
119
 
}
120
 
 
121
 
template <>
122
 
void ThemeItem<Font>::setDefaultValue() {
123
 
    if (!m_value.load("fixed")) {
124
 
        cerr<<"ThemeItem<Font>: Warning! Failed to load default value 'fixed'"<<endl;
125
 
    } else {
126
 
        string effect(ThemeManager::instance().resourceValue(name()+".effect", altName()+".Effect"));
127
 
        if (effect == "halo") {
128
 
            m_value.setHalo(true);
129
 
            FbTk::Color halo_color(ThemeManager::instance().resourceValue(name()+".halo.color", altName()+".Halo.Color").c_str(), 
130
 
                    theme().screenNum());
131
 
            m_value.setHaloColor(halo_color);
132
 
 
133
 
        } else if (effect == "shadow" ) {
134
 
            FbTk::Color shadow_color(ThemeManager::instance().resourceValue(name()+".shadow.color", altName()+".Shadow.Color").c_str(), 
135
 
                    theme().screenNum());
136
 
            
137
 
            m_value.setShadow(true);
138
 
            m_value.setShadowColor(shadow_color);
139
 
            m_value.setShadowOffX(atoi(ThemeManager::instance().resourceValue(name()+".shadow.x", altName()+".Shadow.X").c_str()));
140
 
            m_value.setShadowOffY(atoi(ThemeManager::instance().resourceValue(name()+".shadow.y", altName()+".Shadow.Y").c_str()));
141
 
        }
142
 
    }
143
 
}
144
 
 
145
 
template <>
146
 
void ThemeItem<Font>::setFromString(const char *str) {
147
 
 
148
 
    if (str == 0 || m_value.load(str) == false) {
149
 
        if (ThemeManager::instance().verbose()) {
150
 
            cerr<<"Theme: Error loading font "<<
151
 
                ((m_value.utf8()) ? "(utf8)" : "")<<
152
 
                "for \""<<name()<<"\" or \""<<altName()<<"\": "<<str<<endl;
153
 
 
154
 
            cerr<<"Theme: Setting default value"<<endl;
155
 
        }
156
 
        setDefaultValue();
157
 
    } else {
158
 
        string effect(ThemeManager::instance().resourceValue(name()+".effect", altName()+".Effect"));
159
 
        if (effect == "halo") {
160
 
            m_value.setHalo(true);
161
 
            FbTk::Color halo_color(ThemeManager::instance().resourceValue(name()+".halo.color", altName()+".Halo.Color").c_str(), 
162
 
                    theme().screenNum());
163
 
            m_value.setHaloColor(halo_color);
164
 
 
165
 
        } else if (effect == "shadow" ) {
166
 
            FbTk::Color shadow_color(ThemeManager::instance().resourceValue(name()+".shadow.color", altName()+".Shadow.Color").c_str(), 
167
 
                    theme().screenNum());
168
 
            
169
 
            m_value.setShadow(true);
170
 
            m_value.setShadowColor(shadow_color);
171
 
 
172
 
            int offset_x = atoi(ThemeManager::instance().resourceValue(name()+".shadow.x", altName()+".Shadow.X").c_str());
173
 
            int offset_y = atoi(ThemeManager::instance().resourceValue(name()+".shadow.y", altName()+".Shadow.Y").c_str());
174
 
            if (offset_x != 0)
175
 
                m_value.setShadowOffX(offset_x);
176
 
            if (offset_y != 0)
177
 
                m_value.setShadowOffY(offset_y);
178
 
        }
179
 
    }
180
 
}
181
 
 
182
 
// do nothing
183
 
template <>
184
 
void ThemeItem<Font>::load(const string *name, const string *altname) {
185
 
}
186
 
 
187
 
 
188
 
template <>
189
 
void ThemeItem<Texture>::load(const string *o_name, const string *o_altname) {
190
 
    const string &m_name = (o_name==0)?name():*o_name;
191
 
    const string &m_altname = (o_altname==0)?altName():*o_altname;
192
 
 
193
 
    string color_name(ThemeManager::instance().
194
 
                      resourceValue(m_name+".color", m_altname+".Color"));
195
 
    string colorto_name(ThemeManager::instance().
196
 
                        resourceValue(m_name+".colorTo", m_altname+".ColorTo"));
197
 
    string pixmap_name(ThemeManager::instance().
198
 
                       resourceValue(m_name+".pixmap", m_altname+".Pixmap"));
199
 
 
200
 
 
201
 
    // set default value if we failed to load color
202
 
    if (!m_value.color().setFromString(color_name.c_str(),
203
 
                                       m_tm.screenNum()))
204
 
        m_value.color().setFromString("darkgray", m_tm.screenNum());
205
 
 
206
 
    if (!m_value.colorTo().setFromString(colorto_name.c_str(),
207
 
                                         m_tm.screenNum()))
208
 
        m_value.colorTo().setFromString("white", m_tm.screenNum());
209
 
 
210
 
 
211
 
    if ((m_value.type() & Texture::SOLID) != 0 && (m_value.type() & Texture::FLAT) == 0)
212
 
        m_value.calcHiLoColors(m_tm.screenNum());
213
 
 
214
 
    StringUtil::removeFirstWhitespace(pixmap_name);
215
 
    StringUtil::removeTrailingWhitespace(pixmap_name);
216
 
    if (pixmap_name.empty()) {
217
 
        m_value.pixmap() = 0;
218
 
        return;
219
 
    }
220
 
 
221
 
    std::auto_ptr<PixmapWithMask> pm(Image::load(pixmap_name,
222
 
                                                             m_tm.screenNum()));
223
 
    if (pm.get() == 0) {
224
 
        if (ThemeManager::instance().verbose()) {
225
 
            cerr<<"Resource("<<m_name+".pixmap"
226
 
                <<"): Failed to load image: "<<pixmap_name<<endl;
227
 
        }
228
 
        m_value.pixmap() = 0;
229
 
    } else
230
 
        m_value.pixmap() = pm->pixmap().release();
231
 
 
232
 
}
233
 
 
234
 
template <>
235
 
void ThemeItem<Texture>::setDefaultValue() {
236
 
    m_value.setType(Texture::FLAT | Texture::SOLID);
237
 
    load(); // one might forget to add line something:  so we try to load something.*:  too
238
 
}
239
 
 
240
 
template <>
241
 
void ThemeItem<Texture>::setFromString(const char *str) {
242
 
    m_value.setFromString(str);
243
 
    if (m_value.type() == 0) // failed to set value
244
 
        setDefaultValue();
245
 
}
246
 
 
247
 
 
248
 
 
249
 
// not used
250
 
template <>
251
 
void ThemeItem<PixmapWithMask>::load(const string *name, const string *altname) { }
252
 
 
253
 
template <>
254
 
void ThemeItem<PixmapWithMask>::setDefaultValue() {
255
 
    // create empty pixmap
256
 
    (*this)->pixmap() = 0;
257
 
    (*this)->mask() = 0;
258
 
}
259
 
 
260
 
template <>
261
 
void ThemeItem<PixmapWithMask>::
262
 
setFromString(const char *str) {
263
 
    if (str == 0)
264
 
        setDefaultValue();
265
 
    else {
266
 
        string filename(str);
267
 
 
268
 
        StringUtil::removeFirstWhitespace(filename);
269
 
        StringUtil::removeTrailingWhitespace(filename);
270
 
 
271
 
        std::auto_ptr<PixmapWithMask> pm(Image::load(filename, m_tm.screenNum()));
272
 
        if (pm.get() == 0)
273
 
            setDefaultValue();
274
 
        else {
275
 
            (*this)->pixmap() = pm->pixmap().release();
276
 
            (*this)->mask() = pm->mask().release();
277
 
        }
278
 
    }
279
 
}
280
 
 
281
 
 
282
 
template <>
283
 
void ThemeItem<Color>::setDefaultValue() {
284
 
    m_value.setFromString("white", m_tm.screenNum());
285
 
}
286
 
 
287
 
template <>
288
 
void ThemeItem<Color>::setFromString(const char *str) {
289
 
    if (!m_value.setFromString(str, m_tm.screenNum())) {
290
 
        if (ThemeManager::instance().verbose())
291
 
            cerr<<"Theme: Error loading color value for \""<<name()<<"\" or \""<<altName()<<"\"."<<endl;
292
 
        setDefaultValue();
293
 
    }
294
 
}
295
 
 
296
 
// does nothing
297
 
template <>
298
 
void ThemeItem<Color>::load(const string *name, const string *altname) { }
299
 
 
300
 
template<>
301
 
void ThemeItem<GContext::LineStyle>::setDefaultValue() {
302
 
    *(*this) = GContext::LINESOLID;
303
 
}
304
 
 
305
 
template<>
306
 
void ThemeItem<GContext::LineStyle>::setFromString(char const *strval) { 
307
 
 
308
 
    if (strcasecmp(strval, "LineSolid") == 0 )
309
 
        m_value = GContext::LINESOLID;
310
 
    else if (strcasecmp(strval, "LineOnOffDash") == 0 )
311
 
        m_value = GContext::LINEONOFFDASH;
312
 
    else if (strcasecmp(strval, "LineDoubleDash") == 0)
313
 
        m_value = GContext::LINEDOUBLEDASH;
314
 
    else
315
 
        setDefaultValue();
316
 
}
317
 
 
318
 
template<>
319
 
void ThemeItem<GContext::LineStyle>::load(const string *name, const string *altname) { }
320
 
 
321
 
 
322
 
template<>
323
 
void ThemeItem<GContext::JoinStyle>::setDefaultValue() {
324
 
    *(*this) = GContext::JOINMITER;
325
 
}
326
 
 
327
 
template<>
328
 
void ThemeItem<GContext::JoinStyle>::setFromString(char const *strval) { 
329
 
 
330
 
    if (strcasecmp(strval, "JoinRound") == 0 )
331
 
        m_value = GContext::JOINROUND;
332
 
    else if (strcasecmp(strval, "JoinMiter") == 0 )
333
 
        m_value = GContext::JOINMITER;
334
 
    else if (strcasecmp(strval, "JoinBevel") == 0) 
335
 
        m_value = GContext::JOINBEVEL;
336
 
    else
337
 
        setDefaultValue();
338
 
}
339
 
 
340
 
template<>
341
 
void ThemeItem<GContext::JoinStyle>::load(const string *name, const string *altname) { }
342
 
 
343
 
template<>
344
 
void ThemeItem<GContext::CapStyle>::setDefaultValue() {
345
 
    *(*this) = GContext::CAPNOTLAST;
346
 
}
347
 
 
348
 
template<>
349
 
void ThemeItem<GContext::CapStyle>::setFromString(char const *strval) { 
350
 
 
351
 
    if (strcasecmp(strval, "CapNotLast") == 0 )
352
 
        m_value = GContext::CAPNOTLAST;
353
 
    else if (strcasecmp(strval, "CapProjecting") == 0 )
354
 
        m_value = GContext::CAPPROJECTING;
355
 
    else if (strcasecmp(strval, "CapRound") == 0) 
356
 
        m_value = GContext::CAPROUND;
357
 
    else if (strcasecmp(strval, "CapButt" ) == 0)
358
 
        m_value = GContext::CAPBUTT;
359
 
    else
360
 
        setDefaultValue();
361
 
}
362
 
 
363
 
template<>
364
 
void ThemeItem<GContext::CapStyle>::load(const string *name, const string *altname) { }
365
 
 
366
 
 
367
 
 
368
 
} // end namespace FbTk
369
 
 
370
 
#endif // THEMEITEMS_HH