summaryrefslogtreecommitdiff
path: root/src/gui/setup_video.cpp
blob: db2dabcd16ead094612ea37f1f501576aa9be4e9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
/*
 *  The ManaPlus Client
 *  Copyright (C) 2004-2009  The Mana World Development Team
 *  Copyright (C) 2009-2010  The Mana Developers
 *  Copyright (C) 2011  The ManaPlus Developers
 *
 *  This file is part of The ManaPlus Client.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "gui/setup_video.h"

#include "configuration.h"
#include "game.h"
#include "graphics.h"
#include "localplayer.h"
#include "log.h"
#include "main.h"
#include "particle.h"

#include "gui/gui.h"
#include "gui/okdialog.h"
#include "gui/textdialog.h"

#include "gui/widgets/checkbox.h"
#include "gui/widgets/label.h"
#include "gui/widgets/layouthelper.h"
#include "gui/widgets/listbox.h"
#include "gui/widgets/scrollarea.h"
#include "gui/widgets/slider.h"
#include "gui/widgets/textfield.h"
#include "gui/widgets/dropdown.h"

#include "resources/image.h"

#include "utils/gettext.h"
#include "utils/stringutils.h"

#include <guichan/key.hpp>
#include <guichan/listmodel.hpp>

#include <SDL.h>

#include <algorithm>
#include <string>
#include <vector>

#include "debug.h"

extern Graphics *graphics;

/**
 * The list model for mode list.
 *
 * \ingroup Interface
 */
class ModeListModel : public gcn::ListModel
{
    public:
        /**
         * Constructor.
         */
        ModeListModel();

        /**
         * Destructor.
         */
        virtual ~ModeListModel()
        { }

        /**
         * Returns the number of elements in container.
         */
        int getNumberOfElements()
        { return static_cast<int>(mVideoModes.size()); }

        /**
         * Returns element from container.
         */
        std::string getElementAt(int i)
        { return mVideoModes[i]; }

        /**
         * Returns the index corresponding to the given video mode.
         * E.g.: "800x600".
         * or -1 if not found.
         */
        int getIndexOf(const std::string &widthXHeightMode);

    private:
        void addCustomMode(std::string mode);

        std::vector<std::string> mVideoModes;
};

bool modeSorter(std::string mode1, std::string mode2);

bool modeSorter(std::string mode1, std::string mode2)
{
    const int width1 = atoi(mode1.substr(0, mode1.find("x")).c_str());
    const int height1 = atoi(mode1.substr(mode1.find("x") + 1).c_str());
    if (!width1 || !height1)
        return false;

    const int width2 = atoi(mode2.substr(0, mode2.find("x")).c_str());
    const int height2 = atoi(mode2.substr(mode2.find("x") + 1).c_str());
    if (!width2 || !height2)
        return false;
    if (width1 != width2)
        return width1 < width2;

    if (height1 != height2)
        return height1 < height2;

    return false;
}

ModeListModel::ModeListModel()
{
    /* Get available fullscreen/hardware modes */
    SDL_Rect **modes = SDL_ListModes(NULL, SDL_FULLSCREEN | SDL_HWSURFACE);

    /* Check which modes are available */
    if (modes == static_cast<SDL_Rect **>(0))
    {
        logger->log1("No modes available");
    }
    else if (modes == reinterpret_cast<SDL_Rect **>(-1))
    {
        logger->log1("All resolutions available");
    }
    else
    {
        for (int i = 0; modes[i]; ++i)
        {
            const std::string modeString =
                toString(static_cast<int>(modes[i]->w)) + "x"
                    + toString(static_cast<int>(modes[i]->h));
            mVideoModes.push_back(modeString);
        }
    }
    addCustomMode("640x480");
    addCustomMode("800x600");
    addCustomMode("1024x768");
    addCustomMode("1280x1024");
    addCustomMode("1400x900");
    addCustomMode("1500x990");
    addCustomMode(toString(graphics->getWidth()) + "x"
        + toString(graphics->getHeight()));

    std::sort(mVideoModes.begin(), mVideoModes.end(), modeSorter);
    mVideoModes.push_back("custom");
}

void ModeListModel::addCustomMode(std::string mode)
{
    std::vector<std::string>::iterator it = mVideoModes.begin();
    std::vector<std::string>::iterator it_end = mVideoModes.end();
    while (it != it_end)
    {
        if (*it == mode)
            return;
        ++ it;
    }
    mVideoModes.push_back(mode);
}

int ModeListModel::getIndexOf(const std::string &widthXHeightMode)
{
    std::string currentMode = "";
    for (int i = 0; i < getNumberOfElements(); i++)
    {
        currentMode = getElementAt(i);
        if (currentMode == widthXHeightMode)
            return i;
    }
    return -1;
}

const char *SIZE_NAME[6] =
{
    N_("Tiny (10)"),
    N_("Small (11)"),
    N_("Medium (12)"),
    N_("Large (13)"),
    N_("Big (14)"),
    N_("Huge (15)"),
};

class FontSizeChoiceListModel : public gcn::ListModel
{
public:
    virtual ~FontSizeChoiceListModel()
    { }

    virtual int getNumberOfElements()
    { return 6; }

    virtual std::string getElementAt(int i)
    {
        if (i >= getNumberOfElements() || i < 0)
            return _("???");

        return gettext(SIZE_NAME[i]);
    }
};

const char *OPENGL_NAME[3] =
{
    N_("Software"),
    N_("Fast OpenGL"),
    N_("Safe OpenGL"),
};

class OpenGLListModel : public gcn::ListModel
{
public:
    virtual ~OpenGLListModel()
    { }

    virtual int getNumberOfElements()
    { return 3; }

    virtual std::string getElementAt(int i)
    {
        if (i >= getNumberOfElements() || i < 0)
            return _("???");

        return gettext(OPENGL_NAME[i]);
    }
};

static const char *speechModeToString(Being::Speech mode)
{
    switch (mode)
    {
        case Being::NO_SPEECH:
        default:
            return _("No text");
        case Being::TEXT_OVERHEAD:
            return _("Text");
        case Being::NO_NAME_IN_BUBBLE:
            return _("Bubbles, no names");
        case Being::NAME_IN_BUBBLE:
            return _("Bubbles with names");
    }
    return "";
}

const char *Setup_Video::overlayDetailToString(int detail)
{
    if (detail == -1)
        detail = config.getIntValue("OverlayDetail");

    switch (detail)
    {
        case 0:
            return _("off");
        case 1:
            return _("low");
        case 2:
            return _("high");
        default:
            return "";
    }
    return "";
}

const char *Setup_Video::particleDetailToString(int detail)
{
    if (detail == -1)
        detail = 3 - config.getIntValue("particleEmitterSkip");

    switch (detail)
    {
        case 0:
            return _("low");
        case 1:
            return _("medium");
        case 2:
            return _("high");
        case 3:
            return _("max");
        default:
            return "";
    }
    return "";
}

Setup_Video::Setup_Video():
    mFullScreenEnabled(config.getBoolValue("screen")),
    mOpenGLEnabled(config.getIntValue("opengl")),
    mHwAccelEnabled(config.getBoolValue("hwaccel")),
    mCustomCursorEnabled(config.getBoolValue("customcursor")),
    mVisibleNamesEnabled(config.getBoolValue("visiblenames")),
    mParticleEffectsEnabled(config.getBoolValue("particleeffects")),
    mNPCLogEnabled(config.getBoolValue("logNpcInGui")),
    mPickupChatEnabled(config.getBoolValue("showpickupchat")),
    mPickupParticleEnabled(config.getBoolValue("showpickupparticle")),
    mOpacity(config.getFloatValue("guialpha")),
    mFps(config.getIntValue("fpslimit")),
    mAltFps(config.getIntValue("altfpslimit")),
    mHideShieldSprite(config.getBoolValue("hideShield")),
    mLowTraffic(config.getBoolValue("lowTraffic")),
    mSyncPlayerMove(config.getBoolValue("syncPlayerMove")),
    mDrawHotKeys(config.getBoolValue("drawHotKeys")),
    mDrawPath(config.getBoolValue("drawPath")),
    mShowJob(serverConfig.getBoolValue("showJob")),
    mAlphaCache(config.getBoolValue("alphaCache")),
    mShowBackground(config.getBoolValue("showBackground")),
    mSpeechMode(static_cast<Being::Speech>(
        config.getIntValue("speech"))),
    mModeListModel(new ModeListModel),
    mModeList(new ListBox(mModeListModel)),
    mFsCheckBox(new CheckBox(_("Full screen"), mFullScreenEnabled)),
    mHwAccelCheckBox(new CheckBox(_("Hw acceleration"), mHwAccelEnabled)),
    mCustomCursorCheckBox(new CheckBox(_("Custom cursor"),
                          mCustomCursorEnabled)),
    mVisibleNamesCheckBox(new CheckBox(_("Visible names"),
                          mVisibleNamesEnabled)),
    mParticleEffectsCheckBox(new CheckBox(_("Particle effects"),
                             mParticleEffectsEnabled)),
    mNPCLogCheckBox(new CheckBox(_("Log NPC dialogue"), mNPCLogEnabled)),
    mPickupNotifyLabel(new Label(_("Show pickup notification"))),
    // TRANSLATORS: Refers to "Show own name"
    mPickupChatCheckBox(new CheckBox(_("in chat"), mPickupChatEnabled)),
    // TRANSLATORS: Refers to "Show own name"
    mPickupParticleCheckBox(new CheckBox(_("as particle"),
                            mPickupParticleEnabled)),
    mHideShieldSpriteCheckBox(new CheckBox(_("Hide shield sprite"),
                              mHideShieldSprite)),
    mLowTrafficCheckBox(new CheckBox(_("Low traffic mode"),
                        mLowTraffic)),
    mSyncPlayerMoveCheckBox(new CheckBox(_("Sync player move"),
                            mSyncPlayerMove)),
    mDrawHotKeysCheckBox(new CheckBox(_("Draw hotkeys on map"),
                         mDrawHotKeys)),
    mDrawPathCheckBox(new CheckBox(_("Draw path"), mDrawPath)),
    mShowJobCheckBox(new CheckBox(_("Show job"), mShowJob)),
    mAlphaCacheCheckBox(new CheckBox(_("Enable opacity cache"), mAlphaCache)),
    mShowBackgroundCheckBox(new CheckBox(_("Show background"),
                            mShowBackground)),
    mSpeechSlider(new Slider(0, 3)),
    mSpeechLabel(new Label("")),
    mAlphaSlider(new Slider(0.1, 1.0)),
    mFpsCheckBox(new CheckBox(_("FPS limit:"))),
    mFpsSlider(new Slider(2, 160)),
    mFpsLabel(new Label),
    mAltFpsSlider(new Slider(2, 160)),
    mAltFpsLabel(new Label(_("Alt FPS limit: "))),
    mOverlayDetail(config.getIntValue("OverlayDetail")),
    mOverlayDetailSlider(new Slider(0, 2)),
    mOverlayDetailField(new Label),
    mParticleDetail(3 - config.getIntValue("particleEmitterSkip")),
    mParticleDetailSlider(new Slider(0, 3)),
    mParticleDetailField(new Label),
    mFontSize(config.getIntValue("fontSize")),
    mDialog(0)
{
    setName(_("Video"));

    ScrollArea *scrollArea = new ScrollArea(mModeList);
    scrollArea->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER);

    speechLabel = new Label(_("Overhead text"));
    alphaLabel = new Label(_("Gui opacity"));
    overlayDetailLabel = new Label(_("Ambient FX"));
    particleDetailLabel = new Label(_("Particle detail"));
    fontSizeLabel = new Label(_("Font size"));

    mOpenGLListModel = new OpenGLListModel;
    mOpenGLDropDown = new DropDown(mOpenGLListModel),
    mOpenGLDropDown->setSelected(mOpenGLEnabled);

    mFontSizeListModel = new FontSizeChoiceListModel;
    mFontSizeDropDown = new DropDown(mFontSizeListModel);

    mModeList->setEnabled(true);

#ifndef USE_OPENGL
    mOpenGLDropDown->setSelected(0);
#endif

    mAlphaSlider->setValue(mOpacity);
    mAlphaSlider->setWidth(90);
    mAlphaCacheCheckBox->setEnabled(mOpenGLDropDown->getSelected() == 0);

    mFpsLabel->setCaption(mFps > 0 ? toString(mFps) : _("None"));
    mFpsLabel->setWidth(60);
    mAltFpsLabel->setCaption(_("Alt FPS limit: ") + (mAltFps > 0
                             ? toString(mAltFps) : _("None")));
    mAltFpsLabel->setWidth(150);
    mFpsSlider->setValue(mFps);
    mFpsSlider->setEnabled(mFps > 0);
    mAltFpsSlider->setValue(mAltFps);
    mAltFpsSlider->setEnabled(mAltFps > 0);
    mFpsCheckBox->setSelected(mFps > 0);

    // Pre-select the current video mode.
    std::string videoMode = toString(graphics->getWidth()) + "x"
                            + toString(graphics->getHeight());
    mModeList->setSelected(mModeListModel->getIndexOf(videoMode));

    mModeList->setActionEventId("videomode");
    mCustomCursorCheckBox->setActionEventId("customcursor");
    mVisibleNamesCheckBox->setActionEventId("visiblenames");
    mParticleEffectsCheckBox->setActionEventId("particleeffects");
    mPickupChatCheckBox->setActionEventId("pickupchat");
    mPickupParticleCheckBox->setActionEventId("pickupparticle");
    mNPCLogCheckBox->setActionEventId("lognpc");
    mAlphaSlider->setActionEventId("guialpha");
    mFpsCheckBox->setActionEventId("fpslimitcheckbox");
    mSpeechSlider->setActionEventId("speech");
    mFpsSlider->setActionEventId("fpslimitslider");
    mAltFpsSlider->setActionEventId("altfpslimitslider");
    mOverlayDetailSlider->setActionEventId("overlaydetailslider");
    mOverlayDetailField->setActionEventId("overlaydetailfield");
    mParticleDetailSlider->setActionEventId("particledetailslider");
    mParticleDetailField->setActionEventId("particledetailfield");
    mHideShieldSpriteCheckBox->setActionEventId("hideshieldsprite1");
    mLowTrafficCheckBox->setActionEventId("lowTraffic1");
    mSyncPlayerMoveCheckBox->setActionEventId("syncPlayerMove1");
    mDrawHotKeysCheckBox->setActionEventId("drawHotKeys");
    mDrawPathCheckBox->setActionEventId("drawPath1");
    mShowJobCheckBox->setActionEventId("showJob");
    mAlphaCacheCheckBox->setActionEventId("alphaCache");
    mOpenGLDropDown->setActionEventId("opengl");

    mModeList->addActionListener(this);
    mCustomCursorCheckBox->addActionListener(this);
    mVisibleNamesCheckBox->addActionListener(this);
    mParticleEffectsCheckBox->addActionListener(this);
    mPickupChatCheckBox->addActionListener(this);
    mPickupParticleCheckBox->addActionListener(this);
    mNPCLogCheckBox->addActionListener(this);
    mAlphaSlider->addActionListener(this);
    mFpsCheckBox->addActionListener(this);
    mSpeechSlider->addActionListener(this);
    mFpsSlider->addActionListener(this);
    mAltFpsSlider->addActionListener(this);
    mOverlayDetailSlider->addActionListener(this);
    mOverlayDetailField->addKeyListener(this);
    mParticleDetailSlider->addActionListener(this);
    mParticleDetailField->addKeyListener(this);
    mHideShieldSpriteCheckBox->addKeyListener(this);
    mLowTrafficCheckBox->addKeyListener(this);
    mSyncPlayerMoveCheckBox->addKeyListener(this);
    mDrawHotKeysCheckBox->addKeyListener(this);
    mDrawPathCheckBox->addKeyListener(this);
    mShowJobCheckBox->addKeyListener(this);
    mOpenGLDropDown->addActionListener(this);

    mAlphaCacheCheckBox->addKeyListener(this);

    mSpeechLabel->setCaption(speechModeToString(mSpeechMode));
    mSpeechSlider->setValue(mSpeechMode);

    mOverlayDetailField->setCaption(overlayDetailToString(mOverlayDetail));
    mOverlayDetailSlider->setValue(mOverlayDetail);

    mParticleDetailField->setCaption(particleDetailToString(mParticleDetail));
    mParticleDetailSlider->setValue(mParticleDetail);

    mFontSizeDropDown->setSelected(mFontSize - 10);
    mFontSizeDropDown->adjustHeight();

    // Do the layout
    LayoutHelper h(this);
    ContainerPlacer place = h.getPlacer(0, 0);

    place(0, 0, scrollArea, 1, 5).setPadding(2);
    place(1, 0, mFsCheckBox, 2);
    place(3, 0, mOpenGLDropDown, 1);

    place(1, 1, mCustomCursorCheckBox, 3);
    place(3, 1, mHwAccelCheckBox, 1);

    place(1, 2, mVisibleNamesCheckBox, 3);

    place(3, 2, mAlphaCacheCheckBox, 4);

    place(1, 3, mParticleEffectsCheckBox, 2);

    place(1, 4, mPickupNotifyLabel, 4);

    place(1, 5, mPickupChatCheckBox, 1);
    place(2, 5, mPickupParticleCheckBox, 2);

    place(0, 6, fontSizeLabel, 3);
    place(1, 6, mFontSizeDropDown, 1);

    place(0, 7, mAlphaSlider);
    place(1, 7, alphaLabel, 3);

    place(0, 8, mFpsSlider);
    place(1, 8, mFpsCheckBox).setPadding(3);
    place(2, 8, mFpsLabel).setPadding(1);

    place(0, 9, mAltFpsSlider);
    place(1, 9, mAltFpsLabel).setPadding(3);

    place(0, 10, mSpeechSlider);
    place(1, 10, speechLabel);
    place(2, 10, mSpeechLabel, 3).setPadding(2);

    place(0, 11, mOverlayDetailSlider);
    place(1, 11, overlayDetailLabel);
    place(2, 11, mOverlayDetailField, 3).setPadding(2);

    place(0, 12, mParticleDetailSlider);
    place(1, 12, particleDetailLabel);
    place(2, 12, mParticleDetailField, 3).setPadding(2);

    place(3, 7, mHideShieldSpriteCheckBox);
    place(3, 8, mLowTrafficCheckBox);
    place(0, 13, mShowBackgroundCheckBox);
    place(0, 14, mSyncPlayerMoveCheckBox);
    place(0, 15, mDrawHotKeysCheckBox);
    place(2, 13, mDrawPathCheckBox, 2);
    place(2, 14, mShowJobCheckBox, 2);
    place(2, 15, mNPCLogCheckBox, 2);

    int width = 600;

    if (config.getIntValue("screenwidth") >= 730)
        width += 100;

    setDimension(gcn::Rectangle(0, 0, width, 300));
}

Setup_Video::~Setup_Video()
{
    delete mModeListModel;
    mModeListModel = 0;
    delete mModeList;
    mModeList = 0;
    delete mFontSizeListModel;
    mFontSizeListModel = 0;
    delete mOpenGLListModel;
    mOpenGLListModel = 0;
    delete mDialog;
    mDialog = 0;
}

void Setup_Video::apply()
{
    // Full screen changes
    bool fullscreen = mFsCheckBox->isSelected();
    if (fullscreen != config.getBoolValue("screen"))
    {
        /* The OpenGL test is only necessary on Windows, since switching
         * to/from full screen works fine on Linux. On Windows we'd have to
         * reinitialize the OpenGL state and reload all textures.
         *
         * See http://libsdl.org/cgi/docwiki.cgi/SDL_SetVideoMode
         */

#if defined(WIN32) || defined(__APPLE__)
        // checks for opengl usage
        if (!config.getIntValue("opengl"))
        {
#endif
            if (!graphics->setFullscreen(fullscreen))
            {
                fullscreen = !fullscreen;
                if (!graphics->setFullscreen(fullscreen))
                {
                    std::stringstream errorMessage;
                    if (fullscreen)
                    {
                        errorMessage << _("Failed to switch to windowed mode "
                                          "and restoration of old mode also "
                                          "failed!") << std::endl;
                    }
                    else
                    {
                        errorMessage << _("Failed to switch to fullscreen mode"
                                          " and restoration of old mode also "
                                          "failed!") << std::endl;
                    }
                    logger->error(errorMessage.str());
                }
            }
#if defined(WIN32) || defined(__APPLE__)
        }
        else
        {
            new OkDialog(_("Switching to Full Screen"),
                         _("Restart needed for changes to take effect."));
        }
#endif
        config.setValue("screen", fullscreen);
    }

    // OpenGL change
    if (mOpenGLDropDown->getSelected() != mOpenGLEnabled)
    {
        config.setValue("opengl", mOpenGLDropDown->getSelected());

        // OpenGL can currently only be changed by restarting, notify user.
        new OkDialog(_("Changing to OpenGL"),
                     _("Applying change to OpenGL requires restart."));
    }

    mFps = mFpsCheckBox->isSelected() ?
        static_cast<int>(mFpsSlider->getValue()) : 0;

    mAltFps = static_cast<int>(mAltFpsSlider->getValue());

    mFpsSlider->setEnabled(mFps > 0);

    mAltFpsSlider->setEnabled(mAltFps > 0);

    config.setValue("hwaccel", mHwAccelCheckBox->isSelected());

    // FPS change
    config.setValue("fpslimit", mFps);
    config.setValue("altfpslimit", mAltFps);

    if (config.getIntValue("fontSize")
        != static_cast<int>(mFontSizeDropDown->getSelected()) + 10)
    {
        config.setValue("fontSize", mFontSizeDropDown->getSelected() + 10);
        gui->updateFonts();
    }

    config.setValue("hideShield", mHideShieldSpriteCheckBox->isSelected());
    config.setValue("lowTraffic", mLowTrafficCheckBox->isSelected());
    config.setValue("syncPlayerMove", mSyncPlayerMoveCheckBox->isSelected());
    config.setValue("drawHotKeys", mDrawHotKeysCheckBox->isSelected());
    config.setValue("drawPath", mDrawPathCheckBox->isSelected());
    serverConfig.setValue("showJob", mShowJobCheckBox->isSelected());
    config.setValue("alphaCache", mAlphaCacheCheckBox->isSelected());
    config.setValue("showBackground", mShowBackgroundCheckBox->isSelected());

    // We sync old and new values at apply time
    mFullScreenEnabled = config.getBoolValue("screen");
    mCustomCursorEnabled = config.getBoolValue("customcursor");
    mVisibleNamesEnabled = config.getBoolValue("visiblenames");
    mParticleEffectsEnabled = config.getBoolValue("particleeffects");
    mNPCLogEnabled = config.getBoolValue("logNpcInGui");
    mHideShieldSprite = config.getBoolValue("hideShield");
    mLowTraffic = config.getBoolValue("lowTraffic");
    mSyncPlayerMove = config.getBoolValue("syncPlayerMove");
    mDrawHotKeys = config.getBoolValue("drawHotKeys");
    mDrawPath = config.getBoolValue("drawPath");
    mShowJob = serverConfig.getBoolValue("showJob");
    mAlphaCache = config.getBoolValue("alphaCache");
    mShowBackground = config.getBoolValue("showBackground");

    mSpeechMode = static_cast<Being::Speech>(
        config.getIntValue("speech"));
    mOpacity = config.getFloatValue("guialpha");
    mOverlayDetail = config.getIntValue("OverlayDetail");
    mOpenGLEnabled = config.getIntValue("opengl");
    mHwAccelEnabled = config.getBoolValue("hwaccel");
    mPickupChatEnabled = config.getBoolValue("showpickupchat");
    mPickupParticleEnabled = config.getBoolValue("showpickupparticle");
}

void Setup_Video::cancel()
{
    mFpsCheckBox->setSelected(mFps > 0);
    mFsCheckBox->setSelected(mFullScreenEnabled);
    mOpenGLDropDown->setSelected(mOpenGLEnabled);
    mHwAccelCheckBox->setSelected(mHwAccelEnabled);
    mCustomCursorCheckBox->setSelected(mCustomCursorEnabled);
    mVisibleNamesCheckBox->setSelected(mVisibleNamesEnabled);
    mParticleEffectsCheckBox->setSelected(mParticleEffectsEnabled);
    mFpsSlider->setValue(mFps);
    mFpsSlider->setEnabled(mFps > 0);
    mAltFpsSlider->setValue(mAltFps);
    mAltFpsSlider->setEnabled(mAltFps > 0);
    mSpeechSlider->setValue(mSpeechMode);
    mNPCLogCheckBox->setSelected(mNPCLogEnabled);
    mHideShieldSpriteCheckBox->setSelected(mHideShieldSprite);
    mLowTrafficCheckBox->setSelected(mLowTraffic);
    mSyncPlayerMoveCheckBox->setSelected(mSyncPlayerMove);
    mDrawHotKeysCheckBox->setSelected(mDrawHotKeys);
    mDrawPathCheckBox->setSelected(mDrawPath);
    mShowJobCheckBox->setSelected(mShowJob);
    mAlphaCacheCheckBox->setSelected(mAlphaCache);
    mShowBackgroundCheckBox->setSelected(mShowBackground);
    mAlphaSlider->setValue(mOpacity);
    mOverlayDetailSlider->setValue(mOverlayDetail);
    mParticleDetailSlider->setValue(mParticleDetail);
    mFpsLabel->setCaption(mFpsCheckBox->isSelected()
                          ? toString(mFps) : _("None"));
    mAltFpsLabel->setCaption(_("Alt FPS limit: ") + toString(mAltFps));

    config.setValue("screen", mFullScreenEnabled);

    // Set back to the current video mode.
    std::string videoMode = toString(graphics->getWidth()) + "x"
                            + toString(graphics->getHeight());
    mModeList->setSelected(mModeListModel->getIndexOf(videoMode));
    config.setValue("screenwidth", graphics->getWidth());
    config.setValue("screenheight", graphics->getHeight());

    config.setValue("customcursor", mCustomCursorEnabled);
    config.setValue("visiblenames", mVisibleNamesEnabled);
    config.setValue("particleeffects", mParticleEffectsEnabled);
    config.setValue("speech", static_cast<int>(mSpeechMode));
    config.setValue("logNpcInGui", mNPCLogEnabled);
    config.setValue("hideShield", mHideShieldSprite);
    config.setValue("lowTraffic", mLowTraffic);
    config.setValue("syncPlayerMove", mSyncPlayerMove);
    config.setValue("drawHotKeys", mDrawHotKeys);
    config.setValue("drawPath", mDrawPath);
    serverConfig.setValue("showJob", mShowJob);
    config.setValue("alphaCache", mAlphaCache);
    config.setValue("showBackground", mShowBackground);
    config.setValue("guialpha", mOpacity);
    Image::setEnableAlpha(mOpacity != 1.0f);
    config.setValue("opengl", mOpenGLEnabled);
    config.setValue("hwaccel", mHwAccelEnabled);
    config.setValue("showpickupchat", mPickupChatEnabled);
    config.setValue("showpickupparticle", mPickupParticleEnabled);
    mAlphaCacheCheckBox->setEnabled(mOpenGLDropDown->getSelected() == 0);
}

void Setup_Video::action(const gcn::ActionEvent &event)
{
    const std::string &id = event.getId();

    if (id == "videomode")
    {
        std::string mode = mModeListModel->getElementAt(
            mModeList->getSelected());

        if (mode == "custom")
        {
            if (mDialog)
            {
                mode = mDialog->getText();
                mDialog = 0;
            }
            else
            {
                mDialog = new TextDialog(
                    _("Custom resolution (example: 1024x768)"),
                    _("Enter new resolution:                "));
                mDialog->setActionEventId("videomode");
                mDialog->addActionListener(this);
                return;
            }
        }
        const int width = atoi(mode.substr(0, mode.find("x")).c_str());
        const int height = atoi(mode.substr(mode.find("x") + 1).c_str());
        if (!width || !height)
            return;

        // TODO: Find out why the drawing area doesn't resize without a restart.
        if (width != graphics->getWidth() || height != graphics->getHeight())
        {
            if (width < graphics->getWidth() || height < graphics->getHeight())
                new OkDialog(_("Screen Resolution Changed"),
                       _("Restart your client for the change to take effect.")
                       + std::string("\n") +
                _("Some windows may be moved to fit the lowered resolution."));
            else
                new OkDialog(_("Screen Resolution Changed"),
                     _("Restart your client for the change to take effect."));
        }

        config.setValue("oldscreen", config.getBoolValue("screen"));
        config.setValue("oldscreenwidth", graphics->getWidth());
        config.setValue("oldscreenheight", graphics->getHeight());
        config.setValue("screenwidth", width);
        config.setValue("screenheight", height);
    }
    if (id == "~videomode")
    {
        mDialog = 0;
    }
    else if (id == "guialpha")
    {
        config.setValue("guialpha", mAlphaSlider->getValue());
        Image::setEnableAlpha(config.getFloatValue("guialpha") != 1.0f);
    }
    else if (id == "customcursor")
    {
        config.setValue("customcursor", mCustomCursorCheckBox->isSelected());
    }
    else if (id == "visiblenames")
    {
        config.setValue("visiblenames", mVisibleNamesCheckBox->isSelected());
    }
    else if (id == "particleeffects")
    {
        config.setValue("particleeffects",
                        mParticleEffectsCheckBox->isSelected());
        Particle::enabled = mParticleEffectsCheckBox->isSelected();

        if (Game::instance())
        {
            new OkDialog(_("Particle Effect Settings Changed."),
                         _("Changes will take effect on map change."));
        }
    }
    else if (id == "pickupchat")
    {
        config.setValue("showpickupchat", mPickupChatCheckBox->isSelected());
    }
    else if (id == "pickupparticle")
    {
        config.setValue("showpickupparticle",
                        mPickupParticleCheckBox->isSelected());
    }
    else if (id == "speech")
    {
        Being::Speech val = static_cast<Being::Speech>(
            mSpeechSlider->getValue());
        mSpeechLabel->setCaption(speechModeToString(val));
        mSpeechSlider->setValue(val);
        config.setValue("speech", static_cast<int>(val));
    }
    else if (id == "lognpc")
    {
        config.setValue("logNpcInGui", mNPCLogCheckBox->isSelected());
    }
    else if (id == "overlaydetailslider")
    {
        int val = static_cast<int>(mOverlayDetailSlider->getValue());
        mOverlayDetailField->setCaption(overlayDetailToString(val));
        config.setValue("OverlayDetail", val);
    }
    else if (id == "particledetailslider")
    {
        int val = static_cast<int>(mParticleDetailSlider->getValue());
        mParticleDetailField->setCaption(particleDetailToString(val));
        config.setValue("particleEmitterSkip", 3 - val);
        Particle::emitterSkip = 4 - val;
    }
    else if (id == "fpslimitcheckbox" || id == "fpslimitslider")
    {
        int fps = static_cast<int>(mFpsSlider->getValue());
        if (id == "fpslimitcheckbox" && !mFpsSlider->isEnabled())
            fps = 60;
        else
            fps = fps > 0 ? fps : 60;
        mFps = mFpsCheckBox->isSelected() ? fps : 0;
        const std::string text = mFps > 0 ? toString(mFps) : _("None");

        mFpsLabel->setCaption(text);
        mFpsSlider->setValue(mFps);
        mFpsSlider->setEnabled(mFps > 0);
    }
    else if (id == "altfpslimitslider")
    {
        int fps = static_cast<int>(mAltFpsSlider->getValue());
        fps = fps > 0 ? fps : static_cast<int>(mAltFpsSlider->getScaleStart());
        mAltFps = fps;
        const std::string text = mAltFps > 0 ? toString(mAltFps) : _("None");

        mAltFpsLabel->setCaption(_("Alt FPS limit: ") + text);
        mAltFpsSlider->setValue(mAltFps);
        mAltFpsSlider->setEnabled(mAltFps > 0);
    }
    else if (id == "opengl")
    {
        bool isSoftware = (mOpenGLDropDown->getSelected() == 0);
        mAlphaCacheCheckBox->setEnabled(isSoftware);
    }
}

void Setup_Video::externalUpdated()
{
    mShowJob = serverConfig.getBoolValue("showJob");
    mShowJobCheckBox->setSelected(mShowJob);
}