GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/desktop.cpp Lines: 28 84 33.3 %
Date: 2017-11-29 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-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
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)),
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

16
        mSkin = theme->load("desktop.xml", "");
69
70
2
    if (mSkin != nullptr)
71

8
        mShowBackground = (mSkin->getOption("showBackground") != 0);
72
73

12
    const std::string appName = branding.getValue("appName", std::string());
74
2
    if (appName.empty())
75
    {
76

8
        mVersionLabel->addRow(FULL_VERSION);
77
    }
78
    else
79
    {
80
        mVersionLabel->addRow(strprintf("%s (%s)", FULL_VERSION,
81
            appName.c_str()));
82
    }
83

8
    mVersionLabel->addRow("copyright",
84
        "(C) ManaPlus developers, http://manaplus.org");
85
2
    mVersionLabel->setLinkHandler(this);
86
2
    mVersionLabel->updateHeight();
87
2
}
88
89
10
Desktop::~Desktop()
90
{
91
2
    if (mWallpaper != nullptr)
92
    {
93
        mWallpaper->decRef();
94
        mWallpaper = nullptr;
95
    }
96
2
    if (theme != nullptr)
97
2
        theme->unload(mSkin);
98
4
}
99
100
2
void Desktop::postInit()
101
{
102
2
    if (mSkin != nullptr)
103
    {
104


16
        addXY(mVersionLabel,
105
2
              mSkin->getOption("versionX", 25),
106
              mSkin->getOption("versionY", 2));
107
    }
108
    else
109
    {
110
        addXY(mVersionLabel, 25, 2);
111
    }
112
2
}
113
114
void Desktop::reloadWallpaper()
115
{
116
    Wallpaper::loadWallpapers();
117
    setBestFittingWallpaper();
118
}
119
120
void Desktop::widgetResized(const Event &event A_UNUSED)
121
{
122
    mVersionLabel->setSize(getWidth(), getHeight());
123
    mVersionLabel->updateHeight();
124
    setBestFittingWallpaper();
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
146
        if (imageHelper->useOpenGL() == RENDER_SOFTWARE)
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
173
void Desktop::setBestFittingWallpaper()
174
{
175
    if (!mShowBackground || !config.getBoolValue("showBackground"))
176
        return;
177
178
    const std::string wallpaperName =
179
        Wallpaper::getWallpaper(getWidth(), getHeight());
180
181
    Image *const nWallPaper = Theme::getImageFromTheme(wallpaperName);
182
183
    if (nWallPaper != nullptr)
184
    {
185
        if (mWallpaper != nullptr)
186
        {
187
            ResourceManager::decRefDelete(mWallpaper);
188
            mWallpaper = nullptr;
189
        }
190
191
        const Rect &rect = mDimension;
192
        const int width = rect.width;
193
        const int height = rect.height;
194
195
        if (imageHelper->useOpenGL() == RENDER_SOFTWARE &&
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")
228
        inputManager.executeAction(InputAction::WINDOW_ABOUT);
229
}