2003-08-22 21:25:14 +00:00
|
|
|
// Image.cc for FbTk - Fluxbox ToolKit
|
2006-02-16 06:53:05 +00:00
|
|
|
// Copyright (c) 2003 - 2006 Henrik Kinnunen (fluxgen at fluxbox dot org)
|
2003-08-22 21:25:14 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
#include "Image.hh"
|
|
|
|
#include "StringUtil.hh"
|
2008-10-07 01:16:26 +00:00
|
|
|
#include "FileUtil.hh"
|
2003-08-22 21:25:14 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif // HAVE_CONFIG_H
|
|
|
|
|
|
|
|
#ifdef HAVE_XPM
|
|
|
|
#include "ImageXPM.hh"
|
2004-12-21 23:42:09 +00:00
|
|
|
#endif // HAVE_XPM
|
|
|
|
|
|
|
|
#ifdef HAVE_IMLIB2
|
|
|
|
#include "ImageImlib2.hh"
|
|
|
|
#endif // HAVE_IMLIB2
|
2003-08-22 21:25:14 +00:00
|
|
|
|
|
|
|
#include <list>
|
2004-12-21 23:42:09 +00:00
|
|
|
#include <set>
|
2006-10-27 06:57:43 +00:00
|
|
|
|
|
|
|
using std::string;
|
|
|
|
using std::list;
|
|
|
|
using std::set;
|
2003-08-22 21:25:14 +00:00
|
|
|
|
|
|
|
|
2008-10-09 07:38:42 +00:00
|
|
|
namespace {
|
2003-08-22 21:25:14 +00:00
|
|
|
|
2008-10-09 07:38:42 +00:00
|
|
|
typedef std::map<std::string, FbTk::ImageBase *> ImageMap;
|
|
|
|
typedef std::list<std::string> StringList;
|
2003-08-22 21:25:14 +00:00
|
|
|
|
2008-10-09 07:38:42 +00:00
|
|
|
ImageMap s_image_map;
|
|
|
|
StringList s_search_paths;
|
2004-12-21 23:42:09 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_IMLIB2
|
2008-10-09 07:38:42 +00:00
|
|
|
FbTk::ImageImlib2 imlib2_loader;
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_XPM
|
|
|
|
FbTk::ImageXPM xpm_loader;
|
|
|
|
#endif
|
2006-10-27 06:57:43 +00:00
|
|
|
|
2004-12-21 23:42:09 +00:00
|
|
|
|
2008-10-09 07:38:42 +00:00
|
|
|
}; // end of anonymous namespace
|
2004-12-21 23:42:09 +00:00
|
|
|
|
2008-10-09 07:38:42 +00:00
|
|
|
namespace FbTk {
|
2004-12-21 23:42:09 +00:00
|
|
|
|
2006-10-27 06:57:43 +00:00
|
|
|
PixmapWithMask *Image::load(const string &filename, int screen_num) {
|
2004-12-21 23:42:09 +00:00
|
|
|
|
2003-08-22 21:25:14 +00:00
|
|
|
|
2008-10-09 07:38:42 +00:00
|
|
|
if (filename.empty())
|
2003-08-22 21:25:14 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// determine file ending
|
2006-10-27 06:57:43 +00:00
|
|
|
string extension(StringUtil::toUpper(StringUtil::findExtension(filename)));
|
|
|
|
|
2003-08-22 21:25:14 +00:00
|
|
|
// valid handle?
|
2005-01-02 06:21:45 +00:00
|
|
|
if (s_image_map.find(extension) == s_image_map.end())
|
2003-08-22 21:25:14 +00:00
|
|
|
return false;
|
2006-10-27 06:57:43 +00:00
|
|
|
|
2008-10-07 01:16:26 +00:00
|
|
|
string path = locateFile(filename);
|
|
|
|
if (!path.empty())
|
|
|
|
return s_image_map[extension]->load(path, screen_num);
|
2004-01-02 22:01:08 +00:00
|
|
|
|
2008-10-07 01:16:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2003-08-22 21:25:14 +00:00
|
|
|
|
2008-10-07 01:16:26 +00:00
|
|
|
string Image::locateFile(const string &filename) {
|
|
|
|
string path = StringUtil::expandFilename(filename);
|
|
|
|
if (FileUtil::isRegularFile(path.c_str()))
|
|
|
|
return path;
|
|
|
|
string base = StringUtil::basename(filename);
|
|
|
|
StringList::iterator it = s_search_paths.begin();
|
|
|
|
StringList::iterator it_end = s_search_paths.end();
|
|
|
|
for (; it != it_end; ++it) {
|
|
|
|
path = StringUtil::expandFilename(*it) + "/" + base;
|
|
|
|
if (FileUtil::isRegularFile(path.c_str()))
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
return "";
|
2003-08-22 21:25:14 +00:00
|
|
|
}
|
|
|
|
|
2006-10-27 06:57:43 +00:00
|
|
|
bool Image::registerType(const string &type, ImageBase &base) {
|
2003-08-22 21:25:14 +00:00
|
|
|
|
|
|
|
string ucase_type = StringUtil::toUpper(type);
|
|
|
|
|
|
|
|
// not empty and not this base?
|
|
|
|
if (s_image_map[ucase_type] != 0 &&
|
2006-10-27 06:57:43 +00:00
|
|
|
s_image_map[ucase_type] != &base)
|
2003-08-22 21:25:14 +00:00
|
|
|
return false;
|
|
|
|
// already registered?
|
2003-12-16 17:06:52 +00:00
|
|
|
if (s_image_map[ucase_type] == &base)
|
2003-08-22 21:25:14 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
s_image_map[ucase_type] = &base;
|
2003-12-16 17:06:52 +00:00
|
|
|
|
|
|
|
return true;
|
2003-08-22 21:25:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2006-10-27 06:57:43 +00:00
|
|
|
list<string> remove_list;
|
2003-08-22 21:25:14 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-27 06:57:43 +00:00
|
|
|
void Image::addSearchPath(const string &search_path) {
|
2003-08-22 21:25:14 +00:00
|
|
|
s_search_paths.push_back(search_path);
|
|
|
|
}
|
|
|
|
|
2006-10-27 06:57:43 +00:00
|
|
|
void Image::removeSearchPath(const string &search_path) {
|
2003-11-16 22:33:56 +00:00
|
|
|
s_search_paths.remove(search_path);
|
|
|
|
}
|
|
|
|
|
2003-08-22 21:25:14 +00:00
|
|
|
void Image::removeAllSearchPaths() {
|
|
|
|
s_search_paths.clear();
|
|
|
|
}
|
|
|
|
|
2008-04-21 22:43:10 +00:00
|
|
|
} // end namespace FbTk
|