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

« back to all changes in this revision

Viewing changes to src/FbTk/Image.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
 
// Image.cc for FbTk - Fluxbox ToolKit
2
 
// Copyright (c) 2003 - 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: Image.cc 3864 2005-01-24 18:02:34Z mathias $
23
 
 
24
 
#include "Image.hh"
25
 
#include "StringUtil.hh"
26
 
 
27
 
#ifdef HAVE_CONFIG_H
28
 
#include "config.h"
29
 
#endif // HAVE_CONFIG_H
30
 
 
31
 
#ifdef HAVE_XPM
32
 
#include "ImageXPM.hh"
33
 
#endif // HAVE_XPM
34
 
 
35
 
#ifdef HAVE_IMLIB2
36
 
#include "ImageImlib2.hh"
37
 
#endif // HAVE_IMLIB2
38
 
 
39
 
#include <list>
40
 
#include <iostream>
41
 
#include <set>
42
 
using namespace std;
43
 
 
44
 
namespace FbTk {
45
 
 
46
 
Image::ImageMap Image::s_image_map;
47
 
Image::StringList Image::s_search_paths;
48
 
 
49
 
 
50
 
void Image::init() {
51
 
 
52
 
// create imagehandlers for their extensions
53
 
#ifdef HAVE_XPM
54
 
    new ImageXPM();
55
 
#endif // HAVE_XPM
56
 
#ifdef HAVE_IMLIB2
57
 
    new ImageImlib2();
58
 
#endif // HAVE_IMLIB2
59
 
}
60
 
 
61
 
void Image::shutdown() {
62
 
 
63
 
    std::set<ImageBase*> handlers;
64
 
    
65
 
    // one imagehandler could be registered 
66
 
    // for more than one type
67
 
    ImageMap::iterator it = s_image_map.begin();
68
 
    ImageMap::iterator it_end = s_image_map.end();
69
 
    for (; it != it_end; it++) {
70
 
        if (it->second)
71
 
            handlers.insert(it->second);
72
 
    }
73
 
 
74
 
    // free the unique handlers
75
 
    std::set<ImageBase*>::iterator handler_it = handlers.begin();
76
 
    std::set<ImageBase*>::iterator handler_it_end = handlers.end();
77
 
    for(; handler_it != handler_it_end; handler_it++) {
78
 
        delete (*handler_it);
79
 
    }
80
 
 
81
 
    s_image_map.clear();
82
 
}
83
 
 
84
 
PixmapWithMask *Image::load(const std::string &filename, int screen_num) {
85
 
 
86
 
 
87
 
    if (filename == "")
88
 
        return false;
89
 
 
90
 
    // determine file ending
91
 
    std::string extension(StringUtil::toUpper(StringUtil::findExtension(filename)));
92
 
    
93
 
    // valid handle?
94
 
    if (s_image_map.find(extension) == s_image_map.end())
95
 
        return false;
96
 
    
97
 
    // load file
98
 
    PixmapWithMask *pm = s_image_map[extension]->load(filename, screen_num);
99
 
    // failed?, try different search paths
100
 
    if (pm == 0 && s_search_paths.size()) {
101
 
        // first we need to get basename of current filename
102
 
        std::string base_filename = StringUtil::basename(filename);
103
 
        std::string path = "";
104
 
        // append each search path and try to load
105
 
        StringList::iterator it = s_search_paths.begin();
106
 
        StringList::iterator it_end = s_search_paths.end();
107
 
        for (; it != it_end && pm == 0; ++it) {
108
 
            // append search path and try load it
109
 
            path = StringUtil::expandFilename(*it);
110
 
            pm = s_image_map[extension]->load(path + "/" + base_filename, screen_num);
111
 
        }
112
 
 
113
 
    }
114
 
 
115
 
    return pm;
116
 
}
117
 
 
118
 
bool Image::registerType(const std::string &type, ImageBase &base) {
119
 
 
120
 
    string ucase_type = StringUtil::toUpper(type);
121
 
 
122
 
    // not empty and not this base?
123
 
    if (s_image_map[ucase_type] != 0 &&
124
 
        s_image_map[ucase_type] != &base) 
125
 
        return false;
126
 
    // already registered?
127
 
    if (s_image_map[ucase_type] == &base)
128
 
        return true;
129
 
 
130
 
    s_image_map[ucase_type] = &base;
131
 
 
132
 
    return true;
133
 
}
134
 
 
135
 
void Image::remove(ImageBase &base) {
136
 
    // find and remove all referenses to base
137
 
    ImageMap::iterator it = s_image_map.begin();
138
 
    ImageMap::iterator it_end = s_image_map.end();
139
 
    std::list<std::string> remove_list;
140
 
    for (; it != it_end; ++it) {
141
 
        if (it->second == &base)
142
 
            remove_list.push_back(it->first);
143
 
    }
144
 
 
145
 
    while (!remove_list.empty()) {
146
 
        s_image_map.erase(remove_list.back());
147
 
        remove_list.pop_back();
148
 
    }
149
 
}
150
 
 
151
 
void Image::addSearchPath(const std::string &search_path) {
152
 
    s_search_paths.push_back(search_path);
153
 
}
154
 
 
155
 
void Image::removeSearchPath(const std::string &search_path) {
156
 
    s_search_paths.remove(search_path);
157
 
}
158
 
 
159
 
void Image::removeAllSearchPaths() {
160
 
    s_search_paths.clear();
161
 
}
162
 
 
163
 
}; // end namespace FbTk