diff options
Diffstat (limited to 'src/FbTk/Image.cc')
-rw-r--r-- | src/FbTk/Image.cc | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/src/FbTk/Image.cc b/src/FbTk/Image.cc new file mode 100644 index 0000000..4ccda5b --- /dev/null +++ b/src/FbTk/Image.cc | |||
@@ -0,0 +1,128 @@ | |||
1 | // Image.cc for FbTk - Fluxbox ToolKit | ||
2 | // Copyright (c) 2003 Henrik Kinnunen (fluxgen at users.sourceforge.net) | ||
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,v 1.4 2004/01/02 22:01:08 fluxgen Exp $ | ||
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 | #include <list> | ||
36 | #include <iostream> | ||
37 | using namespace std; | ||
38 | |||
39 | namespace FbTk { | ||
40 | |||
41 | Image::ImageMap Image::s_image_map; | ||
42 | Image::StringList Image::s_search_paths; | ||
43 | |||
44 | PixmapWithMask *Image::load(const std::string &filename, int screen_num) { | ||
45 | |||
46 | #ifdef HAVE_XPM | ||
47 | // we must do this because static linkage with libFbTk will not init | ||
48 | // a static autoreg variable for it | ||
49 | static ImageXPM xpm; | ||
50 | #endif // HAVE_XPM | ||
51 | |||
52 | if (filename == "") | ||
53 | return false; | ||
54 | |||
55 | // determine file ending | ||
56 | std::string extension(StringUtil::toUpper(StringUtil::findExtension(filename))); | ||
57 | |||
58 | // valid handle? | ||
59 | if (s_image_map[extension] == 0) | ||
60 | return false; | ||
61 | |||
62 | // load file | ||
63 | PixmapWithMask *pm = s_image_map[extension]->load(filename, screen_num); | ||
64 | // failed?, try different search paths | ||
65 | if (pm == 0 && s_search_paths.size()) { | ||
66 | // first we need to get basename of current filename | ||
67 | std::string base_filename = StringUtil::basename(filename); | ||
68 | std::string path = ""; | ||
69 | // append each search path and try to load | ||
70 | StringList::iterator it = s_search_paths.begin(); | ||
71 | StringList::iterator it_end = s_search_paths.end(); | ||
72 | for (; it != it_end && pm == 0; ++it) { | ||
73 | // append search path and try load it | ||
74 | path = StringUtil::expandFilename(*it); | ||
75 | pm = s_image_map[extension]->load(path + "/" + base_filename, screen_num); | ||
76 | } | ||
77 | |||
78 | } | ||
79 | |||
80 | return pm; | ||
81 | } | ||
82 | |||
83 | bool Image::registerType(const std::string &type, ImageBase &base) { | ||
84 | |||
85 | string ucase_type = StringUtil::toUpper(type); | ||
86 | |||
87 | // not empty and not this base? | ||
88 | if (s_image_map[ucase_type] != 0 && | ||
89 | s_image_map[ucase_type] != &base) | ||
90 | return false; | ||
91 | // already registered? | ||
92 | if (s_image_map[ucase_type] == &base) | ||
93 | return true; | ||
94 | |||
95 | s_image_map[ucase_type] = &base; | ||
96 | |||
97 | return true; | ||
98 | } | ||
99 | |||
100 | void Image::remove(ImageBase &base) { | ||
101 | // find and remove all referenses to base | ||
102 | ImageMap::iterator it = s_image_map.begin(); | ||
103 | ImageMap::iterator it_end = s_image_map.end(); | ||
104 | std::list<std::string> remove_list; | ||
105 | for (; it != it_end; ++it) { | ||
106 | if (it->second == &base) | ||
107 | remove_list.push_back(it->first); | ||
108 | } | ||
109 | |||
110 | while (!remove_list.empty()) { | ||
111 | s_image_map.erase(remove_list.back()); | ||
112 | remove_list.pop_back(); | ||
113 | } | ||
114 | } | ||
115 | |||
116 | void Image::addSearchPath(const std::string &search_path) { | ||
117 | s_search_paths.push_back(search_path); | ||
118 | } | ||
119 | |||
120 | void Image::removeSearchPath(const std::string &search_path) { | ||
121 | s_search_paths.remove(search_path); | ||
122 | } | ||
123 | |||
124 | void Image::removeAllSearchPaths() { | ||
125 | s_search_paths.clear(); | ||
126 | } | ||
127 | |||
128 | }; // end namespace FbTk | ||