ManaPlus
desktop.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2009-2010 The Mana World Development Team
4  * Copyright (C) 2011-2017 The ManaPlus Developers
5  *
6  * This file is part of The ManaPlus Client.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include "gui/widgets/desktop.h"
23 
24 #include "configuration.h"
25 #include "main.h"
26 
27 #include "gui/skin.h"
28 
30 
31 #include "input/inputmanager.h"
32 
34 
35 #include "resources/imagehelper.h"
36 #include "resources/wallpaper.h"
37 
38 #include "resources/image/image.h"
39 
41 
43 
44 #include "render/graphics.h"
45 
46 #include "debug.h"
47 
48 Desktop *desktop = nullptr;
49 
50 Desktop::Desktop(const Widget2 *const widget) :
51  Container(widget),
52  LinkHandler(),
54  mWallpaper(nullptr),
55  mVersionLabel(new StaticBrowserBox(this, Opaque_false,
56  "browserbox.xml")),
57  mSkin(nullptr),
58  mBackgroundGrayColor(getThemeColor(ThemeColorId::BACKGROUND_GRAY)),
59  mShowBackground(true)
60 {
62 
63  addWidgetListener(this);
64 
66 
67  if (theme != nullptr)
68  mSkin = theme->load("desktop.xml", "");
69 
70  if (mSkin != nullptr)
71  mShowBackground = (mSkin->getOption("showBackground") != 0);
72 
73  const std::string appName = branding.getValue("appName", std::string());
74  if (appName.empty())
75  {
77  }
78  else
79  {
81  appName.c_str()));
82  }
83  mVersionLabel->addRow("copyright",
84  "(C) ManaPlus developers, http://manaplus.org");
87 }
88 
90 {
91  if (mWallpaper != nullptr)
92  {
93  mWallpaper->decRef();
94  mWallpaper = nullptr;
95  }
96  if (theme != nullptr)
97  theme->unload(mSkin);
98 }
99 
101 {
102  if (mSkin != nullptr)
103  {
105  mSkin->getOption("versionX", 25),
106  mSkin->getOption("versionY", 2));
107  }
108  else
109  {
110  addXY(mVersionLabel, 25, 2);
111  }
112 }
113 
115 {
118 }
119 
121 {
125 }
126 
127 void Desktop::draw(Graphics *const graphics)
128 {
129  BLOCK_START("Desktop::draw")
130  GLDEBUG_START("Desktop::draw")
131 
132  const Rect &rect = mDimension;
133  const int width = rect.width;
134  const int height = rect.height;
135  if (mWallpaper != nullptr)
136  {
137  const int wallpWidth = mWallpaper->getWidth();
138  const int wallpHeight = mWallpaper->getHeight();
139 
140  if (width > wallpWidth || height > wallpHeight)
141  {
142  graphics->setColor(mBackgroundGrayColor);
143  graphics->fillRectangle(Rect(0, 0, width, height));
144  }
145 
147  {
148  graphics->drawImage(mWallpaper,
149  (width - wallpWidth) / 2,
150  (height - wallpHeight) / 2);
151  }
152  else
153  {
154  graphics->drawRescaledImage(mWallpaper, 0, 0, width, height);
155  }
156  }
157  else
158  {
159  graphics->setColor(mBackgroundGrayColor);
160  graphics->fillRectangle(Rect(0, 0, width, height));
161  }
162 
163  Container::draw(graphics);
164  GLDEBUG_END()
165  BLOCK_END("Desktop::draw")
166 }
167 
168 void Desktop::safeDraw(Graphics *const graphics)
169 {
170  Desktop::draw(graphics);
171 }
172 
174 {
175  if (!mShowBackground || !config.getBoolValue("showBackground"))
176  return;
177 
178  const std::string wallpaperName =
180 
181  Image *const nWallPaper = Theme::getImageFromTheme(wallpaperName);
182 
183  if (nWallPaper != nullptr)
184  {
185  if (mWallpaper != nullptr)
186  {
188  mWallpaper = nullptr;
189  }
190 
191  const Rect &rect = mDimension;
192  const int width = rect.width;
193  const int height = rect.height;
194 
196  (nWallPaper->getWidth() != width ||
197  nWallPaper->getHeight() != height))
198  {
199  // We rescale to obtain a fullscreen wallpaper...
200  Image *const newRsclWlPpr = Loader::getRescaled(
201  nWallPaper, width, height);
202 
203  if (newRsclWlPpr != nullptr)
204  {
205  ResourceManager::decRefDelete(nWallPaper);
206  // We replace the resource in the resource manager
207  mWallpaper = newRsclWlPpr;
208  }
209  else
210  {
211  mWallpaper = nWallPaper;
212  }
213  }
214  else
215  {
216  mWallpaper = nWallPaper;
217  }
218  }
219  else
220  {
221  logger->log("Couldn't load %s as wallpaper", wallpaperName.c_str());
222  }
223 }
224 
225 void Desktop::handleLink(const std::string &link, MouseEvent *event A_UNUSED)
226 {
227  if (link == "copyright")
229 }
void setSize(const int width, const int height)
Definition: widget.cpp:366
Configuration branding
virtual RenderType useOpenGL() const
Definition: imagehelper.h:106
int width
Definition: rect.h:218
int getWidth() const
Definition: widget.h:220
ImageHelper * imageHelper
Definition: imagehelper.cpp:43
void unload(Skin *const skin)
Definition: theme.cpp:249
const Color & getThemeColor(const ThemeColorIdT type, const unsigned int alpha=255U) const A_INLINE
Definition: widget2.h:44
Desktop(const Widget2 *const widget)
Definition: desktop.cpp:50
Definition: rect.h:72
bool mShowBackground
Definition: desktop.h:83
Skin * mSkin
Definition: desktop.h:81
void addXY(Widget *const widget, const int x, const int y)
#define BLOCK_START(name)
Definition: perfomance.h:78
Configuration config
#define GLDEBUG_START(text)
Definition: opengldebug.h:34
#define BLOCK_END(name)
Definition: perfomance.h:79
Color mBackgroundGrayColor
Definition: desktop.h:82
void draw(Graphics *const graphics)
int getOption(const std::string &name) const
Definition: skin.h:105
Logger * logger
Definition: logger.cpp:95
void setBestFittingWallpaper()
Definition: desktop.cpp:173
#define new
Definition: debug_new.h:147
void setLinkHandler(LinkHandler *linkHandler)
bool getBoolValue(const std::string &key) const
Color mBackgroundColor
Definition: widget.h:1090
void postInit()
Definition: desktop.cpp:100
virtual void setColor(const Color &color)
Definition: graphics.h:319
std::string strprintf(const char *const format,...)
Definition: stringutils.cpp:99
virtual void drawRescaledImage(const Image *const image, int dstX, int dstY, const int desiredWidth, const int desiredHeight)=0
static Image * getImageFromTheme(const std::string &path)
Definition: theme.cpp:654
#define nullptr
Definition: localconsts.h:44
static void loadWallpapers()
Definition: wallpaper.cpp:87
const bool Opaque_false
Definition: opaque.h:29
int height
Definition: rect.h:223
#define FULL_VERSION
Definition: main.h:163
void safeDraw(Graphics *const graphics)
Definition: desktop.cpp:168
void addWidgetListener(WidgetListener *const widgetListener)
Definition: widget.cpp:301
void widgetResized(const Event &event)
Definition: desktop.cpp:120
Skin * load(const std::string &filename, const std::string &filename2, const bool full=true, const std::string &defaultPath=getThemePath())
Definition: theme.cpp:178
std::string getValue(const std::string &key, const std::string &deflt) const
virtual void fillRectangle(const Rect &rectangle)=0
Theme * theme
Definition: theme.cpp:61
#define A_UNUSED
Definition: localconsts.h:171
virtual void drawImage(const Image *const image, int dstX, int dstY)=0
Image * mWallpaper
Definition: desktop.h:79
void reloadWallpaper()
Definition: desktop.cpp:114
Definition: event.h:77
void handleLink(const std::string &link, MouseEvent *event)
Definition: desktop.cpp:225
Definition: image.h:61
int getHeight() const
Definition: widget.h:239
void draw(Graphics *const graphics)
Definition: desktop.cpp:127
int getWidth() const A_INLINE
Definition: image.h:116
InputManager inputManager
void decRefDelete(Resource *const res)
void log(const char *const log_text,...)
Definition: logger.cpp:243
~Desktop()
Definition: desktop.cpp:89
Desktop * desktop
Definition: desktop.cpp:48
StaticBrowserBox * mVersionLabel
Definition: desktop.h:80
Rect mDimension
Definition: widget.h:1100
static std::string getWallpaper(const int width, const int height)
Definition: wallpaper.cpp:136
void decRef()
Definition: image.cpp:521
void executeAction(const InputActionT keyNum)
#define GLDEBUG_END()
Definition: opengldebug.h:35
Image * getRescaled(const Image *const image, const int width, const int height)
int getHeight() const A_INLINE
Definition: image.h:122
void addRow(const std::string &row, const bool atTop=false)