aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk/Image.cc
blob: 277808a4936ee064d491be55e6f3846edf2865db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Image.cc for FbTk - Fluxbox ToolKit
// Copyright (c) 2003 - 2006 Henrik Kinnunen (fluxgen at fluxbox dot org)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

// $Id$

#include "Image.hh"
#include "StringUtil.hh"

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif // HAVE_CONFIG_H

#ifdef HAVE_XPM
#include "ImageXPM.hh"
#endif // HAVE_XPM

#ifdef HAVE_IMLIB2
#include "ImageImlib2.hh"
#endif // HAVE_IMLIB2

#include <list>
#include <iostream>
#include <set>

using std::string;
using std::list;
using std::set;

namespace FbTk {

Image::ImageMap Image::s_image_map;
Image::StringList Image::s_search_paths;


void Image::init() {

// create imagehandlers for their extensions
#ifdef HAVE_XPM
    new ImageXPM();
#endif // HAVE_XPM
#ifdef HAVE_IMLIB2
    new ImageImlib2();
#endif // HAVE_IMLIB2
}

void Image::shutdown() {

    set<ImageBase*> handlers;

    // one imagehandler could be registered
    // for more than one type
    ImageMap::iterator it = s_image_map.begin();
    ImageMap::iterator it_end = s_image_map.end();
    for (; it != it_end; it++) {
        if (it->second)
            handlers.insert(it->second);
    }

    // free the unique handlers
    set<ImageBase*>::iterator handler_it = handlers.begin();
    set<ImageBase*>::iterator handler_it_end = handlers.end();
    for(; handler_it != handler_it_end; handler_it++) {
        delete (*handler_it);
    }

    s_image_map.clear();
}

PixmapWithMask *Image::load(const string &filename, int screen_num) {


    if (filename == "")
        return false;

    // determine file ending
    string extension(StringUtil::toUpper(StringUtil::findExtension(filename)));

    // valid handle?
    if (s_image_map.find(extension) == s_image_map.end())
        return false;

    // load file
    PixmapWithMask *pm = s_image_map[extension]->load(filename, screen_num);
    // failed?, try different search paths
    if (pm == 0 && s_search_paths.size()) {
        // first we need to get basename of current filename
        string base_filename = StringUtil::basename(filename);
        string path = "";
        // append each search path and try to load
        StringList::iterator it = s_search_paths.begin();
        StringList::iterator it_end = s_search_paths.end();
        for (; it != it_end && pm == 0; ++it) {
            // append search path and try load it
            path = StringUtil::expandFilename(*it);
            pm = s_image_map[extension]->load(path + "/" + base_filename, screen_num);
        }

    }

    return pm;
}

bool Image::registerType(const string &type, ImageBase &base) {

    string ucase_type = StringUtil::toUpper(type);

    // not empty and not this base?
    if (s_image_map[ucase_type] != 0 &&
        s_image_map[ucase_type] != &base)
        return false;
    // already registered?
    if (s_image_map[ucase_type] == &base)
        return true;

    s_image_map[ucase_type] = &base;

    return true;
}

void Image::remove(ImageBase &base) {
    // find and remove all referenses to base
    ImageMap::iterator it = s_image_map.begin();
    ImageMap::iterator it_end = s_image_map.end();
    list<string> remove_list;
    for (; it != it_end; ++it) {
        if (it->second == &base)
            remove_list.push_back(it->first);
    }

    while (!remove_list.empty()) {
        s_image_map.erase(remove_list.back());
        remove_list.pop_back();
    }
}

void Image::addSearchPath(const string &search_path) {
    s_search_paths.push_back(search_path);
}

void Image::removeSearchPath(const string &search_path) {
    s_search_paths.remove(search_path);
}

void Image::removeAllSearchPaths() {
    s_search_paths.clear();
}

}; // end namespace FbTk