1 /* ImageIcon.cpp
2 * Copyright (C) 2019 Sven Jähnichen
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18 #include "ImageIcon.hpp"
19
20 namespace BWidgets
21 {
ImageIcon()22 ImageIcon::ImageIcon () : ImageIcon (0.0, 0.0, BWIDGETS_DEFAULT_WIDTH, BWIDGETS_DEFAULT_HEIGHT, "icon") {}
23
ImageIcon(const double x,const double y,const double width,const double height,const std::string & name)24 ImageIcon::ImageIcon (const double x, const double y, const double width, const double height,
25 const std::string& name) :
26 Icon (x, y, width, height, name) {}
27
ImageIcon(const double x,const double y,const double width,const double height,const std::string & name,cairo_surface_t * surface)28 ImageIcon::ImageIcon (const double x, const double y, const double width, const double height,
29 const std::string& name, cairo_surface_t* surface) :
30 Icon (x, y, width, height, name)
31 {
32 // Fill all standard states with the image from surface
33 for (int i = BColors::NORMAL; i < BColors::USER_DEFINED; ++i)
34 {
35 loadImage (BColors::State (i), surface);
36 }
37 }
38
ImageIcon(const double x,const double y,const double width,const double height,const std::string & name,const std::string & filename)39 ImageIcon::ImageIcon (const double x, const double y, const double width, const double height,
40 const std::string& name, const std::string& filename) :
41 Icon (x, y, width, height, name)
42 {
43 // Fill all standard states with the image from filename
44 for (unsigned int i = BColors::NORMAL; i < BColors::USER_DEFINED; ++i)
45 {
46 loadImage (BColors::State (i), filename);
47 }
48 }
49
ImageIcon(const double x,const double y,const double width,const double height,const std::string & name,const std::vector<cairo_surface_t * > & surfaces)50 ImageIcon::ImageIcon (const double x, const double y, const double width, const double height,
51 const std::string& name, const std::vector<cairo_surface_t*>& surfaces) :
52 Icon (x, y, width, height, name)
53 {
54 for (unsigned int i = 0; i < surfaces.size (); ++i) loadImage (BColors::State (i), surfaces[i]);
55 }
56
ImageIcon(const double x,const double y,const double width,const double height,const std::string & name,const std::vector<std::string> & filenames)57 ImageIcon::ImageIcon (const double x, const double y, const double width, const double height,
58 const std::string& name, const std::vector<std::string>& filenames) :
59 Icon (x, y, width, height, name)
60 {
61 for (unsigned int i = 0; i < filenames.size (); ++i) loadImage (BColors::State (i), filenames[i]);
62 }
63
clone() const64 Widget* ImageIcon::clone () const {return new ImageIcon (*this);}
65
loadImage(BColors::State state,cairo_surface_t * surface)66 void ImageIcon::loadImage (BColors::State state, cairo_surface_t* surface)
67 {
68 // Fill empty states with nullptr
69 while (state >= iconSurface.size ()) iconSurface.push_back (nullptr);
70
71 // Clear old surface
72 if (iconSurface[state] && (cairo_surface_status (iconSurface[state]) == CAIRO_STATUS_SUCCESS))
73 {
74 cairo_surface_destroy (iconSurface[state]);
75 iconSurface[state] = nullptr;
76 }
77
78 iconSurface[state] = cairo_image_surface_clone_from_image_surface (surface);
79 }
80
loadImage(BColors::State state,const std::string & filename)81 void ImageIcon::loadImage (BColors::State state, const std::string& filename)
82 {
83 // Fill empty states with nullptr
84 while (state >= iconSurface.size ()) iconSurface.push_back (nullptr);
85
86 // Clear old surface
87 if (iconSurface[state] && (cairo_surface_status (iconSurface[state]) == CAIRO_STATUS_SUCCESS))
88 {
89 cairo_surface_destroy (iconSurface[state]);
90 iconSurface[state] = nullptr;
91 }
92
93 iconSurface[state] = cairo_image_surface_create_from_png (filename.c_str());
94 }
95
96 }
97