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-2018 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, 255U)),
59  mShowBackground(true)
60 {
62 
63  addWidgetListener(this);
64 
66 
67  if (theme != nullptr)
68  {
69  mSkin = theme->load("desktop.xml",
70  "",
71  true,
73  }
74 
75  if (mSkin != nullptr)
76  mShowBackground = (mSkin->getOption("showBackground") != 0);
77 
78  const std::string appName = branding.getValue("appName", std::string());
79  if (appName.empty())
80  {
82  false);
83  }
84  else
85  {
87  appName.c_str()),
88  false);
89  }
90  mVersionLabel->addRow("copyright",
91  "(C) ManaPlus developers, http://manaplus.org");
94 }
95 
97 {
98  if (mWallpaper != nullptr)
99  {
100  mWallpaper->decRef();
101  mWallpaper = nullptr;
102  }
103  if (theme != nullptr)
104  theme->unload(mSkin);
105 }
106 
108 {
109  if (mSkin != nullptr)
110  {
112  mSkin->getOption("versionX", 25),
113  mSkin->getOption("versionY", 2));
114  }
115  else
116  {
117  addXY(mVersionLabel, 25, 2);
118  }
119 }
120 
122 {
125 }
126 
128 {
132 }
133 
134 void Desktop::draw(Graphics *const graphics)
135 {
136  BLOCK_START("Desktop::draw")
137  GLDEBUG_START("Desktop::draw")
138 
139  const Rect &rect = mDimension;
140  const int width = rect.width;
141  const int height = rect.height;
142  if (mWallpaper != nullptr)
143  {
144  const int wallpWidth = mWallpaper->getWidth();
145  const int wallpHeight = mWallpaper->getHeight();
146 
147  if (width > wallpWidth || height > wallpHeight)
148  {
149  graphics->setColor(mBackgroundGrayColor);
150  graphics->fillRectangle(Rect(0, 0, width, height));
151  }
152 
154  {
155  graphics->drawImage(mWallpaper,
156  (width - wallpWidth) / 2,
157  (height - wallpHeight) / 2);
158  }
159  else
160  {
161  graphics->drawRescaledImage(mWallpaper, 0, 0, width, height);
162  }
163  }
164  else
165  {
166  graphics->setColor(mBackgroundGrayColor);
167  graphics->fillRectangle(Rect(0, 0, width, height));
168  }
169 
170  Container::draw(graphics);
171  GLDEBUG_END()
172  BLOCK_END("Desktop::draw")
173 }
174 
175 void Desktop::safeDraw(Graphics *const graphics)
176 {
177  Desktop::draw(graphics);
178 }
179 
181 {
182  if (!mShowBackground || !config.getBoolValue("showBackground"))
183  return;
184 
185  const std::string wallpaperName =
187 
188  Image *const nWallPaper = Theme::getImageFromTheme(wallpaperName);
189 
190  if (nWallPaper != nullptr)
191  {
192  if (mWallpaper != nullptr)
193  {
195  mWallpaper = nullptr;
196  }
197 
198  const Rect &rect = mDimension;
199  const int width = rect.width;
200  const int height = rect.height;
201 
203  (nWallPaper->getWidth() != width ||
204  nWallPaper->getHeight() != height))
205  {
206  // We rescale to obtain a fullscreen wallpaper...
207  Image *const newRsclWlPpr = Loader::getRescaled(
208  nWallPaper, width, height);
209 
210  if (newRsclWlPpr != nullptr)
211  {
212  ResourceManager::decRefDelete(nWallPaper);
213  // We replace the resource in the resource manager
214  mWallpaper = newRsclWlPpr;
215  }
216  else
217  {
218  mWallpaper = nWallPaper;
219  }
220  }
221  else
222  {
223  mWallpaper = nWallPaper;
224  }
225  }
226  else
227  {
228  logger->log("Couldn't load %s as wallpaper", wallpaperName.c_str());
229  }
230 }
231 
232 void Desktop::handleLink(const std::string &link, MouseEvent *event A_UNUSED)
233 {
234  if (link == "copyright")
236 }
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
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
static std::string getThemePath()
Definition: theme.h:66
#define GLDEBUG_START(text)
Definition: opengldebug.h:34
Skin * load(const std::string &filename, const std::string &filename2, const bool full, const std::string &defaultPath)
Definition: theme.cpp:178
#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:88
void setBestFittingWallpaper()
Definition: desktop.cpp:180
#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:107
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:175
void addWidgetListener(WidgetListener *const widgetListener)
Definition: widget.cpp:301
void widgetResized(const Event &event)
Definition: desktop.cpp:127
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:151
virtual void drawImage(const Image *const image, int dstX, int dstY)=0
Image * mWallpaper
Definition: desktop.h:79
void reloadWallpaper()
Definition: desktop.cpp:121
Definition: event.h:77
void handleLink(const std::string &link, MouseEvent *event)
Definition: desktop.cpp:232
Definition: image.h:61
int getHeight() const
Definition: widget.h:239
void draw(Graphics *const graphics)
Definition: desktop.cpp:134
int getWidth() const A_INLINE
Definition: image.h:116
const Color & getThemeColor(const ThemeColorIdT type, const unsigned int alpha) const A_INLINE
Definition: widget2.h:44
InputManager inputManager
void decRefDelete(Resource *const res)
void log(const char *const log_text,...)
Definition: logger.cpp:264
~Desktop()
Definition: desktop.cpp:96
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:522
void executeAction(const InputActionT keyNum)
#define GLDEBUG_END()
Definition: opengldebug.h:35
void addRow(const std::string &row, const bool atTop)
Image * getRescaled(const Image *const image, const int width, const int height)
int getHeight() const A_INLINE
Definition: image.h:122