GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/desktop.cpp Lines: 29 85 34.1 %
Date: 2018-06-18 21:15:20 Branches: 29 104 27.9 %

Line Branch Exec Source
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
29
#include "gui/widgets/staticbrowserbox.h"
30
31
#include "input/inputmanager.h"
32
33
#include "render/opengl/opengldebug.h"
34
35
#include "resources/imagehelper.h"
36
#include "resources/wallpaper.h"
37
38
#include "resources/image/image.h"
39
40
#include "resources/loaders/rescaledloader.h"
41
42
#include "resources/resourcemanager/resourcemanager.h"
43
44
#include "render/graphics.h"
45
46
#include "debug.h"
47
48
Desktop *desktop = nullptr;
49
50
2
Desktop::Desktop(const Widget2 *const widget) :
51
    Container(widget),
52
    LinkHandler(),
53
    WidgetListener(),
54
    mWallpaper(nullptr),
55
    mVersionLabel(new StaticBrowserBox(this, Opaque_false,
56

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

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

8
        mShowBackground = (mSkin->getOption("showBackground") != 0);
77
78

12
    const std::string appName = branding.getValue("appName", std::string());
79
2
    if (appName.empty())
80
    {
81

8
        mVersionLabel->addRow(FULL_VERSION,
82
            false);
83
    }
84
    else
85
    {
86
        mVersionLabel->addRow(strprintf("%s (%s)", FULL_VERSION,
87
            appName.c_str()),
88
            false);
89
    }
90

8
    mVersionLabel->addRow("copyright",
91
        "(C) ManaPlus developers, http://manaplus.org");
92
2
    mVersionLabel->setLinkHandler(this);
93
2
    mVersionLabel->updateHeight();
94
2
}
95
96
10
Desktop::~Desktop()
97
{
98
2
    if (mWallpaper != nullptr)
99
    {
100
        mWallpaper->decRef();
101
        mWallpaper = nullptr;
102
    }
103
2
    if (theme != nullptr)
104
2
        theme->unload(mSkin);
105
4
}
106
107
2
void Desktop::postInit()
108
{
109
2
    if (mSkin != nullptr)
110
    {
111


16
        addXY(mVersionLabel,
112
2
              mSkin->getOption("versionX", 25),
113
              mSkin->getOption("versionY", 2));
114
    }
115
    else
116
    {
117
        addXY(mVersionLabel, 25, 2);
118
    }
119
2
}
120
121
void Desktop::reloadWallpaper()
122
{
123
    Wallpaper::loadWallpapers();
124
    setBestFittingWallpaper();
125
}
126
127
void Desktop::widgetResized(const Event &event A_UNUSED)
128
{
129
    mVersionLabel->setSize(getWidth(), getHeight());
130
    mVersionLabel->updateHeight();
131
    setBestFittingWallpaper();
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
153
        if (imageHelper->useOpenGL() == RENDER_SOFTWARE)
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
180
void Desktop::setBestFittingWallpaper()
181
{
182
    if (!mShowBackground || !config.getBoolValue("showBackground"))
183
        return;
184
185
    const std::string wallpaperName =
186
        Wallpaper::getWallpaper(getWidth(), getHeight());
187
188
    Image *const nWallPaper = Theme::getImageFromTheme(wallpaperName);
189
190
    if (nWallPaper != nullptr)
191
    {
192
        if (mWallpaper != nullptr)
193
        {
194
            ResourceManager::decRefDelete(mWallpaper);
195
            mWallpaper = nullptr;
196
        }
197
198
        const Rect &rect = mDimension;
199
        const int width = rect.width;
200
        const int height = rect.height;
201
202
        if (imageHelper->useOpenGL() == RENDER_SOFTWARE &&
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")
235
        inputManager.executeAction(InputAction::WINDOW_ABOUT);
236
}