diff options
author | fluxgen <fluxgen> | 2003-08-22 21:25:14 (GMT) |
---|---|---|
committer | fluxgen <fluxgen> | 2003-08-22 21:25:14 (GMT) |
commit | 834645f54563fc9a73a845e83ad687700ffecee1 (patch) | |
tree | 8ba1c6c2843c0d6fe82195f54c7027ef9d199ea9 | |
parent | 7b059d2399956a7d9753c92d8df00f9b7b508201 (diff) | |
download | fluxbox-834645f54563fc9a73a845e83ad687700ffecee1.zip fluxbox-834645f54563fc9a73a845e83ad687700ffecee1.tar.bz2 |
image handling
-rw-r--r-- | src/FbTk/Image.cc | 120 | ||||
-rw-r--r-- | src/FbTk/Image.hh | 70 | ||||
-rw-r--r-- | src/FbTk/ImageXPM.cc | 54 | ||||
-rw-r--r-- | src/FbTk/ImageXPM.hh | 38 | ||||
-rw-r--r-- | src/FbTk/PixmapWithMask.hh | 53 |
5 files changed, 335 insertions, 0 deletions
diff --git a/src/FbTk/Image.cc b/src/FbTk/Image.cc new file mode 100644 index 0000000..e4ab6ac --- /dev/null +++ b/src/FbTk/Image.cc | |||
@@ -0,0 +1,120 @@ | |||
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.1 2003/08/22 21:25:14 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 | // append each search path and try to load | ||
69 | StringList::iterator it = s_search_paths.begin(); | ||
70 | StringList::iterator it_end = s_search_paths.end(); | ||
71 | for (; it != it_end && pm == 0; ++it) { | ||
72 | // append search path and try load it | ||
73 | std::string path = StringUtil::expandFilename(*it); | ||
74 | pm = s_image_map[extension]->load(path + "/" + base_filename, screen_num); | ||
75 | } | ||
76 | } | ||
77 | |||
78 | return pm; | ||
79 | } | ||
80 | |||
81 | bool Image::registerType(const std::string &type, ImageBase &base) { | ||
82 | |||
83 | string ucase_type = StringUtil::toUpper(type); | ||
84 | |||
85 | // not empty and not this base? | ||
86 | if (s_image_map[ucase_type] != 0 && | ||
87 | s_image_map[ucase_type] != &base) | ||
88 | return false; | ||
89 | // already registered? | ||
90 | if (s_image_map[ucase_type] = &base) | ||
91 | return true; | ||
92 | |||
93 | s_image_map[ucase_type] = &base; | ||
94 | } | ||
95 | |||
96 | void Image::remove(ImageBase &base) { | ||
97 | // find and remove all referenses to base | ||
98 | ImageMap::iterator it = s_image_map.begin(); | ||
99 | ImageMap::iterator it_end = s_image_map.end(); | ||
100 | std::list<std::string> remove_list; | ||
101 | for (; it != it_end; ++it) { | ||
102 | if (it->second == &base) | ||
103 | remove_list.push_back(it->first); | ||
104 | } | ||
105 | |||
106 | while (!remove_list.empty()) { | ||
107 | s_image_map.erase(remove_list.back()); | ||
108 | remove_list.pop_back(); | ||
109 | } | ||
110 | } | ||
111 | |||
112 | void Image::addSearchPath(const std::string &search_path) { | ||
113 | s_search_paths.push_back(search_path); | ||
114 | } | ||
115 | |||
116 | void Image::removeAllSearchPaths() { | ||
117 | s_search_paths.clear(); | ||
118 | } | ||
119 | |||
120 | }; // end namespace FbTk | ||
diff --git a/src/FbTk/Image.hh b/src/FbTk/Image.hh new file mode 100644 index 0000000..c655a2e --- /dev/null +++ b/src/FbTk/Image.hh | |||
@@ -0,0 +1,70 @@ | |||
1 | // Image.hh 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.hh,v 1.1 2003/08/22 21:25:14 fluxgen Exp $ | ||
23 | |||
24 | #ifndef FBTK_IMAGE_HH | ||
25 | #define FBTK_IMAGE_HH | ||
26 | |||
27 | #include <string> | ||
28 | #include <list> | ||
29 | #include <map> | ||
30 | |||
31 | namespace FbTk { | ||
32 | |||
33 | class ImageBase; | ||
34 | class PixmapWithMask; | ||
35 | |||
36 | /// loads images | ||
37 | class Image { | ||
38 | public: | ||
39 | /// @return an instance of PixmapWithMask on success, 0 on failure | ||
40 | static PixmapWithMask *load(const std::string &filename, int screen_num); | ||
41 | /// for register file type and imagebase | ||
42 | /// @return false on failure | ||
43 | static bool registerType(const std::string &type, ImageBase &base); | ||
44 | /// removes a imagebase class from register | ||
45 | /// @return false on failure | ||
46 | static void remove(ImageBase &base); | ||
47 | /// adds a path to search images from | ||
48 | static void addSearchPath(const std::string &search_path); | ||
49 | /// adds a path to search images from | ||
50 | static void removeAllSearchPaths(); | ||
51 | private: | ||
52 | typedef std::map<std::string, ImageBase *> ImageMap; | ||
53 | typedef std::list<std::string> StringList; | ||
54 | |||
55 | static ImageMap s_image_map; | ||
56 | static StringList s_search_paths; | ||
57 | }; | ||
58 | |||
59 | /// common interface for all image classes | ||
60 | class ImageBase { | ||
61 | public: | ||
62 | virtual ~ImageBase() { Image::remove(*this); } | ||
63 | virtual PixmapWithMask *load(const std::string &name, int screen_num) const = 0; | ||
64 | }; | ||
65 | |||
66 | }; // end namespace FbTk | ||
67 | |||
68 | #endif // IMAGE_HH | ||
69 | |||
70 | |||
diff --git a/src/FbTk/ImageXPM.cc b/src/FbTk/ImageXPM.cc new file mode 100644 index 0000000..b24cfe5 --- /dev/null +++ b/src/FbTk/ImageXPM.cc | |||
@@ -0,0 +1,54 @@ | |||
1 | // ImageXPM.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: ImageXPM.cc,v 1.1 2003/08/22 21:25:14 fluxgen Exp $ | ||
23 | |||
24 | #include "ImageXPM.hh" | ||
25 | |||
26 | #include "App.hh" | ||
27 | #include "PixmapWithMask.hh" | ||
28 | |||
29 | #include <X11/xpm.h> | ||
30 | |||
31 | namespace FbTk { | ||
32 | |||
33 | ImageXPM::ImageXPM() { | ||
34 | Image::registerType("XPM", *this); | ||
35 | } | ||
36 | |||
37 | PixmapWithMask *ImageXPM::load(const std::string &filename, int screen_num) const { | ||
38 | |||
39 | XpmAttributes xpm_attr; | ||
40 | xpm_attr.valuemask = 0; | ||
41 | Display *dpy = FbTk::App::instance()->display(); | ||
42 | Pixmap pm = 0, mask = 0; | ||
43 | int retvalue = XpmReadFileToPixmap(dpy, | ||
44 | RootWindow(dpy, screen_num), | ||
45 | const_cast<char *>(filename.c_str()), | ||
46 | &pm, | ||
47 | &mask, &xpm_attr); | ||
48 | if (retvalue == 0) // success | ||
49 | return new PixmapWithMask(pm, mask); | ||
50 | else // failure | ||
51 | return 0; | ||
52 | } | ||
53 | |||
54 | } // end namespace FbTk | ||
diff --git a/src/FbTk/ImageXPM.hh b/src/FbTk/ImageXPM.hh new file mode 100644 index 0000000..d6afdf3 --- /dev/null +++ b/src/FbTk/ImageXPM.hh | |||
@@ -0,0 +1,38 @@ | |||
1 | // ImageXPM.hh 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: ImageXPM.hh,v 1.1 2003/08/22 21:25:14 fluxgen Exp $ | ||
23 | |||
24 | #ifndef FBTK_IMAGEXPM_HH | ||
25 | #define FBTK_IMAGEXPM_HH | ||
26 | |||
27 | #include "Image.hh" | ||
28 | namespace FbTk { | ||
29 | |||
30 | class ImageXPM: public ImageBase { | ||
31 | public: | ||
32 | ImageXPM(); | ||
33 | PixmapWithMask *load(const std::string &filename, int screen_num) const; | ||
34 | }; | ||
35 | |||
36 | } // end namespace FbTk | ||
37 | |||
38 | #endif // FBTK_IMAGEXPM_HH | ||
diff --git a/src/FbTk/PixmapWithMask.hh b/src/FbTk/PixmapWithMask.hh new file mode 100644 index 0000000..6803f3f --- /dev/null +++ b/src/FbTk/PixmapWithMask.hh | |||
@@ -0,0 +1,53 @@ | |||
1 | // PixmapWithMask.hh 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: PixmapWithMask.hh,v 1.1 2003/08/22 21:25:14 fluxgen Exp $ | ||
23 | |||
24 | #ifndef FBTK_PIXMAPWITHMASK_HH | ||
25 | #define FBTK_PIXMAPWITHMASK_HH | ||
26 | |||
27 | #include "FbPixmap.hh" | ||
28 | namespace FbTk { | ||
29 | |||
30 | class PixmapWithMask { | ||
31 | public: | ||
32 | PixmapWithMask() { } | ||
33 | PixmapWithMask(Pixmap pm, Pixmap mask):m_pixmap(pm), m_mask(mask) { } | ||
34 | |||
35 | void scale(unsigned int width, unsigned int height) { | ||
36 | pixmap().scale(width, height); | ||
37 | mask().scale(width, height); | ||
38 | } | ||
39 | |||
40 | FbPixmap &pixmap() { return m_pixmap; } | ||
41 | FbPixmap &mask() { return m_mask; } | ||
42 | |||
43 | const FbPixmap &pixmap() const { return m_pixmap; } | ||
44 | const FbPixmap &mask() const { return m_mask; } | ||
45 | |||
46 | private: | ||
47 | FbPixmap m_pixmap; | ||
48 | FbPixmap m_mask; | ||
49 | }; | ||
50 | |||
51 | } // end namespace FbTk | ||
52 | |||
53 | #endif // FBTK_PIXMAPWITHMASK_HH | ||