GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/desktop.cpp Lines: 32 89 36.0 %
Date: 2021-03-17 Branches: 28 106 26.4 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2009-2010  The Mana World Development Team
4
 *  Copyright (C) 2011-2019  The ManaPlus Developers
5
 *  Copyright (C) 2019-2021  Andrei Karas
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#include "gui/widgets/desktop.h"
24
25
#include "configuration.h"
26
#include "main.h"
27
28
#include "gui/skin.h"
29
30
#include "gui/widgets/staticbrowserbox.h"
31
32
#include "input/inputmanager.h"
33
34
#include "render/opengl/opengldebug.h"
35
36
#include "resources/imagehelper.h"
37
#include "resources/wallpaper.h"
38
39
#include "resources/image/image.h"
40
41
#include "resources/loaders/rescaledloader.h"
42
43
#include "resources/resourcemanager/resourcemanager.h"
44
45
#include "render/graphics.h"
46
47
#include "debug.h"
48
49
Desktop *desktop = nullptr;
50
51
1
Desktop::Desktop(const Widget2 *const widget) :
52
    Container(widget),
53
    LinkHandler(),
54
    WidgetListener(),
55
    mWallpaper(nullptr),
56
    mVersionLabel(new StaticBrowserBox(this, Opaque_false,
57

1
        "browserbox.xml")),
58
    mSkin(nullptr),
59
2
    mBackgroundGrayColor(getThemeColor(ThemeColorId::BACKGROUND_GRAY, 255U)),
60
8
    mShowBackground(true)
61
{
62
2
    mBackgroundColor = getThemeColor(ThemeColorId::BACKGROUND, 128);
63
64
1
    addWidgetListener(this);
65
66
1
    Wallpaper::loadWallpapers();
67
68
1
    if (theme != nullptr)
69
    {
70

7
        mSkin = theme->load("desktop.xml",
71
            "",
72
            true,
73
1
            Theme::getThemePath());
74
    }
75
76
1
    if (mSkin != nullptr)
77

4
        mShowBackground = (mSkin->getOption("showBackground") != 0);
78
79

6
    const std::string appName = branding.getValue("appName", std::string());
80
1
    if (appName.empty())
81
    {
82
4
        mVersionLabel->addRow(FULL_VERSION,
83
1
            false);
84
    }
85
    else
86
    {
87
        mVersionLabel->addRow(strprintf("%s (%s)", FULL_VERSION,
88
            appName.c_str()),
89
            false);
90
    }
91
4
    mVersionLabel->addRow("copyright",
92
1
        "(C) ManaPlus developers, http://manaplus.org");
93
1
    mVersionLabel->setLinkHandler(this);
94
1
    mVersionLabel->updateHeight();
95
1
}
96
97
5
Desktop::~Desktop()
98
{
99
1
    if (mWallpaper != nullptr)
100
    {
101
        mWallpaper->decRef();
102
        mWallpaper = nullptr;
103
    }
104
1
    if (theme != nullptr)
105
1
        theme->unload(mSkin);
106
2
}
107
108
1
void Desktop::postInit()
109
{
110
1
    if (mSkin != nullptr)
111
    {
112

9
        addXY(mVersionLabel,
113
1
              mSkin->getOption("versionX", 25),
114
2
              mSkin->getOption("versionY", 2));
115
    }
116
    else
117
    {
118
        addXY(mVersionLabel, 25, 2);
119
    }
120
1
}
121
122
void Desktop::reloadWallpaper()
123
{
124
    Wallpaper::loadWallpapers();
125
    setBestFittingWallpaper();
126
}
127
128
void Desktop::widgetResized(const Event &event A_UNUSED)
129
{
130
    mVersionLabel->setSize(getWidth(), getHeight());
131
    mVersionLabel->updateHeight();
132
    setBestFittingWallpaper();
133
}
134
135
void Desktop::draw(Graphics *const graphics)
136
{
137
    BLOCK_START("Desktop::draw")
138
    GLDEBUG_START("Desktop::draw")
139
140
    const Rect &rect = mDimension;
141
    const int width = rect.width;
142
    const int height = rect.height;
143
    if (mWallpaper != nullptr)
144
    {
145
        const int wallpWidth = mWallpaper->getWidth();
146
        const int wallpHeight = mWallpaper->getHeight();
147
148
        if (width > wallpWidth || height > wallpHeight)
149
        {
150
            graphics->setColor(mBackgroundGrayColor);
151
            graphics->fillRectangle(Rect(0, 0, width, height));
152
        }
153
154
        if (imageHelper->useOpenGL() == RENDER_SOFTWARE)
155
        {
156
            graphics->drawImage(mWallpaper,
157
                (width - wallpWidth) / 2,
158
                (height - wallpHeight) / 2);
159
        }
160
        else
161
        {
162
            graphics->drawRescaledImage(mWallpaper, 0, 0, width, height);
163
        }
164
    }
165
    else
166
    {
167
        graphics->setColor(mBackgroundGrayColor);
168
        graphics->fillRectangle(Rect(0, 0, width, height));
169
    }
170
171
    Container::draw(graphics);
172
    GLDEBUG_END()
173
    BLOCK_END("Desktop::draw")
174
}
175
176
void Desktop::safeDraw(Graphics *const graphics)
177
{
178
    Desktop::draw(graphics);
179
}
180
181
void Desktop::setBestFittingWallpaper()
182
{
183
    if (!mShowBackground || !config.getBoolValue("showBackground"))
184
        return;
185
186
    const std::string wallpaperName =
187
        Wallpaper::getWallpaper(getWidth(), getHeight());
188
189
    Image *const nWallPaper = Theme::getImageFromTheme(wallpaperName);
190
191
    if (nWallPaper != nullptr)
192
    {
193
        if (mWallpaper != nullptr)
194
        {
195
            ResourceManager::decRefDelete(mWallpaper);
196
            mWallpaper = nullptr;
197
        }
198
199
        const Rect &rect = mDimension;
200
        const int width = rect.width;
201
        const int height = rect.height;
202
203
        if (imageHelper->useOpenGL() == RENDER_SOFTWARE &&
204
            (nWallPaper->getWidth() != width ||
205
            nWallPaper->getHeight() != height))
206
        {
207
            // We rescale to obtain a fullscreen wallpaper...
208
            Image *const newRsclWlPpr = Loader::getRescaled(
209
                nWallPaper, width, height);
210
211
            if (newRsclWlPpr != nullptr)
212
            {
213
                ResourceManager::decRefDelete(nWallPaper);
214
                // We replace the resource in the resource manager
215
                mWallpaper = newRsclWlPpr;
216
            }
217
            else
218
            {
219
                mWallpaper = nWallPaper;
220
            }
221
        }
222
        else
223
        {
224
            mWallpaper = nWallPaper;
225
        }
226
    }
227
    else
228
    {
229
        logger->log("Couldn't load %s as wallpaper", wallpaperName.c_str());
230
    }
231
}
232
233
void Desktop::handleLink(const std::string &link, MouseEvent *event A_UNUSED)
234
{
235
    if (link == "copyright")
236
        inputManager.executeAction(InputAction::WINDOW_ABOUT);
237
}