GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/tabs/setup_video.cpp Lines: 93 211 44.1 %
Date: 2018-11-12 Branches: 115 436 26.4 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2018  The ManaPlus Developers
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/tabs/setup_video.h"
24
25
#include "gui/windowmanager.h"
26
27
#include "gui/windows/okdialog.h"
28
#include "gui/windows/textdialog.h"
29
30
#include "gui/widgets/button.h"
31
#include "gui/widgets/checkbox.h"
32
#include "gui/widgets/containerplacer.h"
33
#include "gui/widgets/createwidget.h"
34
#include "gui/widgets/label.h"
35
#include "gui/widgets/layouthelper.h"
36
#include "gui/widgets/listbox.h"
37
#include "gui/widgets/scrollarea.h"
38
#include "gui/widgets/slider.h"
39
#include "gui/widgets/dropdown.h"
40
41
#include "utils/delete2.h"
42
43
#if defined(USE_OPENGL) && !defined(ANDROID) && !defined(__APPLE__)
44
#include "graphicsmanager.h"
45
46
#include "test/testmain.h"
47
#endif  // defined(USE_OPENGL) && !defined(ANDROID) && !defined(__APPLE__)
48
49
#if defined(ANDROID) || defined(__APPLE__) || !defined(USE_OPENGL)
50
#include "configuration.h"
51
#endif  // defined(ANDROID) || defined(__APPLE__) || !defined(USE_OPENGL)
52
53
#if defined(ANDROID) || defined(__APPLE__)
54
#include "utils/stringutils.h"
55
#endif  // defined(ANDROID) || defined(__APPLE__)
56
57
#include "gui/models/modelistmodel.h"
58
#include "gui/models/opengllistmodel.h"
59
60
#include <sstream>
61
62
#include "debug.h"
63
64
2
Setup_Video::Setup_Video(const Widget2 *const widget) :
65
    SetupTab(widget),
66
    KeyListener(),
67

8
    mFullScreenEnabled(config.getBoolValue("screen")),
68

8
    mOpenGLEnabled(intToRenderType(config.getIntValue("opengl"))),
69

8
    mFps(config.getIntValue("fpslimit")),
70

8
    mAltFps(config.getIntValue("altfpslimit")),
71

2
    mModeListModel(new ModeListModel),
72
4
    mOpenGLListModel(new OpenGLListModel),
73


8
    mModeList(CREATEWIDGETR(ListBox, widget, mModeListModel, "")),
74
    // TRANSLATORS: video settings checkbox
75
4
    mFsCheckBox(new CheckBox(this, _("Full screen"), mFullScreenEnabled,
76

6
        nullptr, std::string())),
77
2
    mOpenGLDropDown(new DropDown(widget, mOpenGLListModel,
78

6
        false, Modal_false, nullptr, std::string())),
79
    // TRANSLATORS: video settings checkbox
80
2
    mFpsCheckBox(new CheckBox(this, _("FPS limit:"), false,
81

4
        nullptr, std::string())),
82

2
    mFpsSlider(new Slider(this, 2.0, 160.0, 1.0)),
83

2
    mFpsLabel(new Label(this)),
84

2
    mAltFpsSlider(new Slider(this, 2.0, 160.0, 1.0)),
85
    // TRANSLATORS: video settings label
86

4
    mAltFpsLabel(new Label(this, _("Alt FPS limit: "))),
87
#if !defined(ANDROID) && !defined(__APPLE__) && !defined(__native_client__)
88
    // TRANSLATORS: video settings button
89
2
    mDetectButton(new Button(this, _("Detect best mode"), "detect",
90

2
        BUTTON_SKIN, this)),
91
#endif  // !defined(ANDROID) && !defined(__APPLE__) &&
92
        // !defined(__native_client__)
93
    mDialog(nullptr),
94

8
    mCustomCursorEnabled(config.getBoolValue("customcursor")),
95

8
    mEnableResize(config.getBoolValue("enableresize")),
96

8
    mNoFrame(config.getBoolValue("noframe")),
97
#ifdef USE_SDL2
98
    mAllowHighDPI(config.getBoolValue("allowHighDPI")),
99
    // TRANSLATORS: video settings checkbox
100
    mAllowHighDPICheckBox(new CheckBox(this, _("High DPI"), mAllowHighDPI,
101
        nullptr, std::string())),
102
#endif  // USE_SDL2
103
    mCustomCursorCheckBox(new CheckBox(this,
104
#ifdef ANDROID
105
        // TRANSLATORS: video settings checkbox
106
        _("Show cursor"),
107
#else  // ANDROID
108
        // TRANSLATORS: video settings checkbox
109
2
        _("Custom cursor"),
110
#endif  // ANDROID
111
2
        mCustomCursorEnabled,
112

6
        nullptr, std::string())),
113
    // TRANSLATORS: video settings checkbox
114
2
    mEnableResizeCheckBox(new CheckBox(this, _("Enable resize"),
115

4
        mEnableResize, nullptr, std::string())),
116
    // TRANSLATORS: video settings checkbox
117
4
    mNoFrameCheckBox(new CheckBox(this, _("No frame"), mNoFrame,
118





104
        nullptr, std::string()))
119
{
120
    // TRANSLATORS: video settings tab name
121
10
    setName(_("Video"));
122
123
2
    ScrollArea *const scrollArea = new ScrollArea(this, mModeList,
124

10
        Opaque_true, "setup_video_background.xml");
125
2
    scrollArea->setWidth(150);
126
2
    scrollArea->setHorizontalScrollPolicy(ScrollArea::SHOW_NEVER);
127
128
2
    mOpenGLDropDown->setSelected(renderToIndex[mOpenGLEnabled]);
129
130
4
    mModeList->setEnabled(true);
131
132
    // TRANSLATORS: video settings label
133


4
    mFpsLabel->setCaption(mFps > 0 ? toString(mFps) : _("None"));
134
2
    mFpsLabel->setWidth(60);
135
    // TRANSLATORS: video settings label
136


6
    mAltFpsLabel->setCaption(_("Alt FPS limit: ") + (mAltFps > 0 ?
137
        // TRANSLATORS: video settings label value
138
2
        toString(mAltFps) : _("None")));
139
2
    mAltFpsLabel->setWidth(150);
140
4
    mFpsSlider->setEnabled(mFps > 0);
141
2
    mFpsSlider->setValue(mFps);
142
4
    mAltFpsSlider->setEnabled(mAltFps > 0);
143
2
    mAltFpsSlider->setValue(mAltFps);
144
4
    mFpsCheckBox->setSelected(mFps > 0);
145
146
    // Pre-select the current video mode.
147
4
    const std::string videoMode = toString(
148
2
        mainGraphics->mActualWidth).append("x").append(
149
10
        toString(mainGraphics->mActualHeight));
150

2
    mModeList->setSelected(mModeListModel->getIndexOf(videoMode));
151
152
10
    mModeList->setActionEventId("videomode");
153
10
    mCustomCursorCheckBox->setActionEventId("customcursor");
154
10
    mFpsCheckBox->setActionEventId("fpslimitcheckbox");
155
10
    mFpsSlider->setActionEventId("fpslimitslider");
156
10
    mAltFpsSlider->setActionEventId("altfpslimitslider");
157
10
    mOpenGLDropDown->setActionEventId("opengl");
158
10
    mEnableResizeCheckBox->setActionEventId("enableresize");
159
10
    mNoFrameCheckBox->setActionEventId("noframe");
160
#ifdef USE_SDL2
161
    mAllowHighDPICheckBox->setActionEventId("allowHighDPI");
162
    mAllowHighDPICheckBox->addActionListener(this);
163
#endif  // USE_SDL2
164
165
2
    mModeList->addActionListener(this);
166
2
    mCustomCursorCheckBox->addActionListener(this);
167
2
    mFpsCheckBox->addActionListener(this);
168
2
    mFpsSlider->addActionListener(this);
169
2
    mAltFpsSlider->addActionListener(this);
170
2
    mOpenGLDropDown->addActionListener(this);
171
2
    mEnableResizeCheckBox->addActionListener(this);
172
2
    mNoFrameCheckBox->addActionListener(this);
173
174
    // Do the layout
175
4
    LayoutHelper h(this);
176
2
    ContainerPlacer place = h.getPlacer(0, 0);
177
178
4
    place(0, 0, scrollArea, 1, 5).setPadding(2);
179

2
    place(0, 5, mOpenGLDropDown, 1, 1);
180
181
2
    place(1, 0, mFsCheckBox, 2, 1);
182
183
2
    place(1, 1, mCustomCursorCheckBox, 3, 1);
184
185
2
    place(1, 2, mEnableResizeCheckBox, 2, 1);
186
2
    place(1, 3, mNoFrameCheckBox, 2, 1);
187
#ifdef USE_SDL2
188
    place(1, 4, mAllowHighDPICheckBox, 2, 1);
189
#endif  // USE_SDL2
190
191
2
    place(0, 6, mFpsSlider, 1, 1);
192
4
    place(1, 6, mFpsCheckBox, 1, 1).setPadding(3);
193
4
    place(2, 6, mFpsLabel, 1, 1).setPadding(1);
194
195
2
    place(0, 7, mAltFpsSlider, 1, 1);
196
4
    place(1, 7, mAltFpsLabel, 1, 1).setPadding(3);
197
198
#if !defined(ANDROID) && !defined(__APPLE__) && !defined(__native_client__)
199
2
    place(0, 8, mDetectButton, 1, 1);
200
#else  // !defined(ANDROID) && !defined(__APPLE__) &&
201
       // !defined(__native_client__)
202
    mNoFrameCheckBox->setEnabled(false);
203
    mEnableResizeCheckBox->setEnabled(false);
204
#ifndef __native_client__
205
    mFsCheckBox->setEnabled(false);
206
#endif  // __native_client__
207
#endif  // !defined(ANDROID) && !defined(__APPLE__) &&
208
        // !defined(__native_client__)
209
210
2
    int width = 600;
211
212

8
    if (config.getIntValue("screenwidth") >= 730)
213
2
        width += 100;
214
215
2
    setDimension(Rect(0, 0, width, 300));
216
2
}
217
218
8
Setup_Video::~Setup_Video()
219
{
220
4
    delete2(mModeListModel);
221
2
    delete2(mModeList);
222
4
    delete2(mOpenGLListModel);
223
2
    delete2(mDialog);
224
4
}
225
226
void Setup_Video::apply()
227
{
228
    // Full screen changes
229
    bool fullscreen = mFsCheckBox->isSelected();
230
    if (fullscreen != config.getBoolValue("screen"))
231
    {
232
        /* The OpenGL test is only necessary on Windows, since switching
233
         * to/from full screen works fine on Linux. On Windows we'd have to
234
         * reinitialize the OpenGL state and reload all textures.
235
         *
236
         * See http://libsdl.org/cgi/docwiki.cgi/SDL_SetVideoMode
237
         */
238
239
#if defined(WIN32) || defined(__APPLE__) || defined(ANDROID)
240
        // checks for opengl usage
241
        if (intToRenderType(config.getIntValue("opengl")) == RENDER_SOFTWARE)
242
        {
243
#endif  // defined(WIN32) || defined(__APPLE__) || defined(ANDROID)
244
            if (!WindowManager::setFullScreen(fullscreen))
245
            {
246
                fullscreen = !fullscreen;
247
                if (!WindowManager::setFullScreen(fullscreen))
248
                {
249
                    std::stringstream errorMsg;
250
                    if (fullscreen)
251
                    {
252
                        // TRANSLATORS: video error message
253
                        errorMsg << _("Failed to switch to windowed mode "
254
                            "and restoration of old mode also "
255
                            "failed!") << std::endl;
256
                    }
257
                    else
258
                    {
259
                        // TRANSLATORS: video error message
260
                        errorMsg << _("Failed to switch to fullscreen mode"
261
                            " and restoration of old mode also "
262
                            "failed!") << std::endl;
263
                    }
264
                    logger->safeError(errorMsg.str());
265
                }
266
            }
267
#if defined(WIN32) || defined(__APPLE__) || defined(ANDROID)
268
        }
269
        else
270
        {
271
            CREATEWIDGET(OkDialog,
272
                // TRANSLATORS: video settings warning
273
                _("Switching to Full Screen"),
274
                // TRANSLATORS: video settings warning
275
                _("Restart needed for changes to take effect."),
276
                // TRANSLATORS: ok dialog button
277
                _("OK"),
278
                DialogType::OK,
279
                Modal_true,
280
                ShowCenter_true,
281
                nullptr,
282
                260);
283
        }
284
#endif  // defined(WIN32) || defined(__APPLE__) || defined(ANDROID)
285
286
        config.setValue("screen", fullscreen);
287
    }
288
289
    const int sel = mOpenGLDropDown->getSelected();
290
    RenderType mode = RENDER_SOFTWARE;
291
    if (sel >= 0 && CAST_U32(sel) < sizeof(indexToRender))
292
        mode = indexToRender[mOpenGLDropDown->getSelected()];
293
294
    // OpenGL change
295
    if (mode != mOpenGLEnabled)
296
    {
297
        config.setValue("opengl", CAST_S32(mode));
298
299
        // OpenGL can currently only be changed by restarting, notify user.
300
        CREATEWIDGET(OkDialog,
301
            // TRANSLATORS: video settings warning
302
            _("Changing to OpenGL"),
303
            // TRANSLATORS: video settings warning
304
            _("Applying change to OpenGL requires restart."),
305
            // TRANSLATORS: ok dialog button
306
            _("OK"),
307
            DialogType::OK,
308
            Modal_true,
309
            ShowCenter_true,
310
            nullptr,
311
            260);
312
    }
313
314
    mFps = mFpsCheckBox->isSelected() ?
315
        CAST_S32(mFpsSlider->getValue()) : 0;
316
317
    mAltFps = CAST_S32(mAltFpsSlider->getValue());
318
319
    mFpsSlider->setEnabled(mFps > 0);
320
321
    mAltFpsSlider->setEnabled(mAltFps > 0);
322
323
    // FPS change
324
    config.setValue("fpslimit", mFps);
325
    config.setValue("altfpslimit", mAltFps);
326
327
    // We sync old and new values at apply time
328
    mFullScreenEnabled = config.getBoolValue("screen");
329
    mCustomCursorEnabled = config.getBoolValue("customcursor");
330
331
    mOpenGLEnabled = intToRenderType(config.getIntValue("opengl"));
332
    mEnableResize = config.getBoolValue("enableresize");
333
    mNoFrame = config.getBoolValue("noframe");
334
#ifdef USE_SDL2
335
    mAllowHighDPI = config.getBoolValue("allowHighDPI");
336
#endif  // USE_SDL2
337
}
338
339
void Setup_Video::cancel()
340
{
341
    mFpsCheckBox->setSelected(mFps > 0);
342
    mFsCheckBox->setSelected(mFullScreenEnabled);
343
    mOpenGLDropDown->setSelected(renderToIndex[mOpenGLEnabled]);
344
    mCustomCursorCheckBox->setSelected(mCustomCursorEnabled);
345
    mFpsSlider->setEnabled(mFps > 0);
346
    mFpsSlider->setValue(mFps);
347
    mAltFpsSlider->setEnabled(mAltFps > 0);
348
    mAltFpsSlider->setValue(mAltFps);
349
    mFpsLabel->setCaption(mFpsCheckBox->isSelected()
350
    // TRANSLATORS: video settings label
351
                          ? toString(mFps) : _("None"));
352
    // TRANSLATORS: video settings label
353
    mAltFpsLabel->setCaption(_("Alt FPS limit: ") + toString(mAltFps));
354
    mEnableResizeCheckBox->setSelected(mEnableResize);
355
    mNoFrameCheckBox->setSelected(mNoFrame);
356
#ifdef USE_SDL2
357
    mAllowHighDPICheckBox->setSelected(mAllowHighDPI);
358
#endif  // USE_SDL2
359
360
    config.setValue("screen", mFullScreenEnabled);
361
362
    // Set back to the current video mode.
363
    std::string videoMode = toString(mainGraphics->mActualWidth).append("x")
364
        .append(toString(mainGraphics->mActualHeight));
365
    mModeList->setSelected(mModeListModel->getIndexOf(videoMode));
366
    config.setValue("screenwidth", mainGraphics->mActualWidth);
367
    config.setValue("screenheight", mainGraphics->mActualHeight);
368
369
    config.setValue("customcursor", mCustomCursorEnabled);
370
    config.setValue("opengl", CAST_S32(mOpenGLEnabled));
371
    config.setValue("enableresize", mEnableResize);
372
#ifdef USE_SDL2
373
    config.setValue("allowHighDPI", mAllowHighDPI);
374
#endif  // USE_SDL2
375
}
376
377
void Setup_Video::action(const ActionEvent &event)
378
{
379
    const std::string &id = event.getId();
380
381
    if (id == "videomode")
382
    {
383
        std::string mode = mModeListModel->getElementAt(
384
            mModeList->getSelected());
385
386
        if (mode == "custom")
387
        {
388
            if (mDialog != nullptr)
389
            {
390
                mode = mDialog->getText();
391
                mDialog = nullptr;
392
            }
393
            else
394
            {
395
                CREATEWIDGETV(mDialog, TextDialog,
396
                    // TRANSLATORS: resolution question dialog
397
                    _("Custom resolution (example: 1024x768)"),
398
                    // TRANSLATORS: resolution question dialog
399
                    _("Enter new resolution:                "),
400
                    nullptr,
401
                    false);
402
                mDialog->setActionEventId("videomode");
403
                mDialog->addActionListener(this);
404
                return;
405
            }
406
        }
407
        const int width = atoi(mode.substr(0, mode.find('x')).c_str());
408
        const int height = atoi(mode.substr(mode.find('x') + 1).c_str());
409
        if ((width == 0) || (height == 0))
410
            return;
411
412
        if (width != mainGraphics->mActualWidth
413
            || height != mainGraphics->mActualHeight)
414
        {
415
#if defined(WIN32) || defined(__APPLE__) || defined(ANDROID)
416
            if (intToRenderType(config.getIntValue("opengl"))
417
                == RENDER_SOFTWARE)
418
            {
419
                WindowManager::doResizeVideo(width, height, false);
420
            }
421
            else
422
            {
423
                if (width < mainGraphics->mActualWidth
424
                    || height < mainGraphics->mActualHeight)
425
                {
426
                    CREATEWIDGET(OkDialog,
427
                        // TRANSLATORS: video settings warning
428
                        _("Screen Resolution Changed"),
429
                        // TRANSLATORS: video settings warning
430
                       _("Restart your client for the change to take effect.")
431
                        // TRANSLATORS: video settings warning
432
                       + std::string("\n") + _("Some windows may be moved to "
433
                        "fit the lowered resolution."),
434
                        // TRANSLATORS: ok dialog button
435
                        _("OK"),
436
                        DialogType::OK,
437
                        Modal_true,
438
                        ShowCenter_true,
439
                        nullptr,
440
                        260);
441
                }
442
                else
443
                {
444
                    CREATEWIDGET(OkDialog,
445
                        // TRANSLATORS: video settings warning
446
                        _("Screen Resolution Changed"),
447
                        // TRANSLATORS: video settings warning
448
                        _("Restart your client for the change"
449
                        " to take effect."),
450
                        // TRANSLATORS: ok dialog button
451
                        _("OK"),
452
                        DialogType::OK,
453
                        Modal_true,
454
                        ShowCenter_true,
455
                        nullptr,
456
                        260);
457
                }
458
            }
459
#else  // defined(WIN32) || defined(__APPLE__) || defined(ANDROID)
460
461
            mainGraphics->setWindowSize(width, height);
462
            WindowManager::doResizeVideo(width, height, false);
463
#endif  // defined(WIN32) || defined(__APPLE__) || defined(ANDROID)
464
        }
465
466
        config.setValue("oldscreen", config.getBoolValue("screen"));
467
        config.setValue("oldscreenwidth", mainGraphics->mActualWidth);
468
        config.setValue("oldscreenheight", mainGraphics->mActualHeight);
469
        config.setValue("screenwidth", width);
470
        config.setValue("screenheight", height);
471
    }
472
    if (id == "~videomode")
473
    {
474
        mDialog = nullptr;
475
    }
476
    else if (id == "customcursor")
477
    {
478
        config.setValue("customcursor", mCustomCursorCheckBox->isSelected());
479
    }
480
    else if (id == "fpslimitcheckbox" || id == "fpslimitslider")
481
    {
482
        int tempFps = CAST_S32(mFpsSlider->getValue());
483
        if (id == "fpslimitcheckbox" && !mFpsSlider->isEnabled())
484
            tempFps = 60;
485
        else
486
            tempFps = tempFps > 0 ? tempFps : 60;
487
        mFps = mFpsCheckBox->isSelected() ? tempFps : 0;
488
        // TRANSLATORS: video settings label
489
        const std::string text = mFps > 0 ? toString(mFps) : _("None");
490
491
        mFpsLabel->setCaption(text);
492
        mFpsSlider->setEnabled(mFps > 0);
493
        mFpsSlider->setValue(mFps);
494
    }
495
    else if (id == "altfpslimitslider")
496
    {
497
        int tempFps = CAST_S32(mAltFpsSlider->getValue());
498
        tempFps = tempFps > 0 ? tempFps : CAST_S32(
499
            mAltFpsSlider->getScaleStart());
500
        mAltFps = tempFps;
501
        // TRANSLATORS: video settings label
502
        const std::string text = mAltFps > 0 ? toString(mAltFps) : _("None");
503
504
        // TRANSLATORS: video settings label
505
        mAltFpsLabel->setCaption(_("Alt FPS limit: ") + text);
506
        mAltFpsSlider->setEnabled(mAltFps > 0);
507
        mAltFpsSlider->setValue(mAltFps);
508
    }
509
    else if (id == "enableresize")
510
    {
511
        config.setValue("enableresize", mEnableResizeCheckBox->isSelected());
512
    }
513
    else if (id == "noframe")
514
    {
515
        config.setValue("noframe", mNoFrameCheckBox->isSelected());
516
    }
517
#ifdef USE_SDL2
518
    else if (id == "allowHighDPI")
519
    {
520
        config.setValue("allowHighDPI", mAllowHighDPICheckBox->isSelected());
521
    }
522
#endif  // USE_SDL2
523
#if defined(USE_OPENGL) && !defined(ANDROID) && !defined(__APPLE__)
524
    else if (id == "detect")
525
    {
526
        TestMain *test = graphicsManager.startDetection();
527
        if (test != nullptr)
528
        {
529
            Configuration &conf = test->getConfig();
530
            const int val = conf.getValueInt("opengl", -1);
531
            if (val >= 0 && CAST_U32(val)
532
                < sizeof(renderToIndex) / sizeof(int))
533
            {
534
                mOpenGLDropDown->setSelected(renderToIndex[val]);
535
            }
536
            config.setValue("textureSize",
537
                conf.getValue("textureSize", "1024,1024,1024,1024,1024,1024"));
538
            config.setValue("testInfo", conf.getValue("testInfo", ""));
539
            delete test;
540
        }
541
    }
542
#endif  // defined(USE_OPENGL) && !defined(ANDROID) && !defined(__APPLE__)
543

3
}