Add warning when disabling secure.enable_security (#9943)
[oweals/minetest.git] / src / gui / guiButtonImage.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "guiButtonImage.h"
21
22 #include "client/guiscalingfilter.h"
23 #include "debug.h"
24 #include "IGUIEnvironment.h"
25 #include "IGUIImage.h"
26 #include "IVideoDriver.h"
27 #include "StyleSpec.h"
28
29 using namespace irr;
30 using namespace gui;
31
32 GUIButtonImage::GUIButtonImage(gui::IGUIEnvironment *environment,
33                 gui::IGUIElement *parent, s32 id, core::rect<s32> rectangle,
34                 ISimpleTextureSource *tsrc, bool noclip)
35         : GUIButton (environment, parent, id, rectangle, tsrc, noclip)
36 {
37         m_image = Environment->addImage(
38                         core::rect<s32>(0,0,rectangle.getWidth(),rectangle.getHeight()), this);
39         m_image->setScaleImage(isScalingImage());
40         sendToBack(m_image);
41 }
42
43 void GUIButtonImage::setForegroundImage(video::ITexture *image)
44 {
45         if (image == m_foreground_image)
46                 return;
47
48         if (image != nullptr)
49                 image->grab();
50
51         if (m_foreground_image != nullptr)
52                 m_foreground_image->drop();
53
54         m_foreground_image = image;
55         m_image->setImage(image);
56 }
57
58 //! Set element properties from a StyleSpec
59 void GUIButtonImage::setFromStyle(const StyleSpec& style)
60 {
61         GUIButton::setFromStyle(style);
62
63         video::IVideoDriver *driver = Environment->getVideoDriver();
64
65         const core::position2di buttonCenter(AbsoluteRect.getCenter());
66         core::position2d<s32> geom(buttonCenter);
67         if (style.isNotDefault(StyleSpec::FGIMG)) {
68                 video::ITexture *texture = style.getTexture(StyleSpec::FGIMG,
69                                 getTextureSource());
70
71                 setForegroundImage(guiScalingImageButton(driver, texture, geom.X, geom.Y));
72                 setScaleImage(true);
73         } else {
74                 setForegroundImage(nullptr);
75         }
76 }
77
78 void GUIButtonImage::setScaleImage(bool scaleImage)
79 {
80         GUIButton::setScaleImage(scaleImage);
81         m_image->setScaleImage(scaleImage);
82 }
83
84 GUIButtonImage *GUIButtonImage::addButton(IGUIEnvironment *environment,
85                 const core::rect<s32> &rectangle, ISimpleTextureSource *tsrc,
86                 IGUIElement *parent, s32 id, const wchar_t *text,
87                 const wchar_t *tooltiptext)
88 {
89         GUIButtonImage *button = new GUIButtonImage(environment,
90                         parent ? parent : environment->getRootGUIElement(), id, rectangle, tsrc);
91
92         if (text)
93                 button->setText(text);
94
95         if (tooltiptext)
96                 button->setToolTipText(tooltiptext);
97
98         button->drop();
99         return button;
100 }