GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/setupitem.cpp Lines: 343 583 58.8 %
Date: 2018-07-14 Branches: 108 402 26.9 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-2018  The ManaPlus Developers
4
 *
5
 *  This file is part of The ManaPlus Client.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "gui/widgets/setupitem.h"
22
23
#include "configuration.h"
24
#include "soundmanager.h"
25
26
#include "gui/gui.h"
27
28
#include "gui/fonts/font.h"
29
30
#include "gui/models/listmodel.h"
31
32
#include "gui/windows/editdialog.h"
33
34
#include "gui/widgets/button.h"
35
#include "gui/widgets/checkbox.h"
36
#include "gui/widgets/createwidget.h"
37
#include "gui/widgets/dropdown.h"
38
#include "gui/widgets/horizontcontainer.h"
39
#include "gui/widgets/inttextfield.h"
40
#include "gui/widgets/label.h"
41
#include "gui/widgets/slider.h"
42
#include "gui/widgets/sliderlist.h"
43
#include "gui/widgets/vertcontainer.h"
44
45
#include "gui/widgets/tabs/setuptabscroll.h"
46
47
#include "utils/base64.h"
48
#include "utils/gettext.h"
49
#include "utils/stdmove.h"
50
#include "utils/mathutils.h"
51
52
#include "debug.h"
53
54
404
SetupItem::SetupItem(const std::string &restrict text,
55
                     const std::string &restrict description,
56
                     const std::string &restrict keyName,
57
                     SetupTabScroll *restrict const parent,
58
                     const std::string &restrict eventName,
59
404
                     const MainConfig mainConfig) :
60
    ActionListener(),
61
    Widget2(parent),
62
    mText(text),
63
    mDescription(description),
64
    mKeyName(keyName),
65
    mParent(parent),
66
    mEventName(eventName),
67
    mValue(),
68
    mDefault(),
69
    mWidget(nullptr),
70
    mTempWidgets(),
71
    mValueType(VBOOL),
72
    mMainConfig(mainConfig),
73
4040
    mUseDefault(false)
74
{
75
404
}
76
77
82
SetupItem::SetupItem(const std::string &restrict text,
78
                     const std::string &restrict description,
79
                     const std::string &restrict keyName,
80
                     SetupTabScroll *restrict const parent,
81
                     const std::string &restrict eventName,
82
                     const std::string &restrict def,
83
82
                     const MainConfig mainConfig) :
84
    ActionListener(),
85
    Widget2(parent),
86
    mText(text),
87
    mDescription(description),
88
    mKeyName(keyName),
89
    mParent(parent),
90
    mEventName(eventName),
91
    mValue(),
92
    mDefault(def),
93
    mWidget(nullptr),
94
    mTempWidgets(),
95
    mValueType(VBOOL),
96
    mMainConfig(mainConfig),
97
820
    mUseDefault(true)
98
{
99
82
}
100
101
3888
SetupItem::~SetupItem()
102
{
103
486
}
104
105
Configuration *SetupItem::getConfig() const
106
{
107


408
    if (mMainConfig == MainConfig_true)
108
        return &config;
109
    return &serverConfig;
110
}
111
112
408
void SetupItem::load()
113
{
114
816
    if (mKeyName.empty())
115
        return;
116
117
408
    const Configuration *const cfg = getConfig();
118
408
    if (mUseDefault)
119
    {
120
12
        mValue = cfg->getValue(mKeyName, mDefault);
121
    }
122
    else
123
    {
124

402
        switch (mValueType)
125
        {
126
            case VBOOL:
127
272
                if (cfg->getBoolValue(mKeyName))
128
160
                    mValue = "1";
129
                else
130
112
                    mValue = "0";
131
                break;
132
            case VSTR:
133
            default:
134
260
                mValue = cfg->getStringValue(mKeyName);
135
130
                break;
136
            case VINT:
137
                mValue = toString(cfg->getIntValue(mKeyName));
138
                break;
139
            case VNONE:
140
                break;
141
        }
142
    }
143
}
144
145
void SetupItem::save()
146
{
147
    if (mKeyName.empty())
148
        return;
149
150
    Configuration *const cfg = getConfig();
151
    cfg->setValue(mKeyName, mValue);
152
}
153
154
486
std::string SetupItem::getActionEventId() const
155
{
156
486
    if (mWidget == nullptr)
157
        return std::string();
158
159
486
    return mWidget->getActionEventId();
160
}
161
162
void SetupItem::action(const ActionEvent &event)
163
{
164
    if (mWidget == nullptr)
165
        return;
166
167
    if (event.getId() == mWidget->getActionEventId())
168
        doAction();
169
}
170
171
void SetupItem::doAction()
172
{
173
    fromWidget();
174
}
175
176
void SetupItem::apply(const std::string &eventName A_UNUSED)
177
{
178
    save();
179
}
180
181
void SetupItem::cancel(const std::string &eventName A_UNUSED)
182
{
183
    load();
184
    toWidget();
185
}
186
187
void SetupItem::externalUpdated(const std::string &eventName A_UNUSED)
188
{
189
    load();
190
    toWidget();
191
}
192
193
void SetupItem::externalUnloaded(const std::string &eventName A_UNUSED)
194
{
195
}
196
197
26
void SetupItem::fixFirstItemSize(Widget *const widget)
198
{
199




130
    if (widget == nullptr)
200
        return;
201
260
    const int maxSize = mParent->getPreferredFirstItemSize();
202




130
    if (widget->getWidth() < maxSize)
203
116
        widget->setWidth(maxSize);
204
}
205
206
void SetupItem::rereadValue()
207
{
208
    load();
209
    toWidget();
210
}
211
212
272
SetupItemCheckBox::SetupItemCheckBox(const std::string &restrict text,
213
                                     const std::string &restrict description,
214
                                     const std::string &restrict keyName,
215
                                     SetupTabScroll *restrict const parent,
216
                                     const std::string &restrict eventName,
217
272
                                     const MainConfig mainConfig) :
218
    SetupItem(text, description, keyName, parent, eventName, mainConfig),
219
272
    mCheckBox(nullptr)
220
{
221
272
    createControls();
222
272
}
223
224
6
SetupItemCheckBox::SetupItemCheckBox(const std::string &restrict text,
225
                                     const std::string &restrict description,
226
                                     const std::string &restrict keyName,
227
                                     SetupTabScroll *restrict const parent,
228
                                     const std::string &restrict eventName,
229
                                     const std::string &restrict def,
230
6
                                     const MainConfig mainConfig) :
231
    SetupItem(text, description, keyName, parent, eventName, def, mainConfig),
232
6
    mCheckBox(nullptr)
233
{
234
6
    createControls();
235
6
}
236
237
556
SetupItemCheckBox::~SetupItemCheckBox()
238
{
239
278
    mWidget = nullptr;
240
278
}
241
242
278
void SetupItemCheckBox::createControls()
243
{
244
278
    load();
245

556
    mCheckBox = new CheckBox(this, mText, mValue != "0", mParent, mEventName);
246
556
    mCheckBox->setToolTip(mDescription);
247
278
    mWidget = mCheckBox;
248
556
    mParent->getContainer()->add1(mWidget, -1);
249
278
    mParent->addControl(this);
250
278
    mParent->addActionListener(this);
251
278
    mWidget->addActionListener(this);
252
278
}
253
254
void SetupItemCheckBox::fromWidget()
255
{
256
    if (mCheckBox == nullptr)
257
        return;
258
259
    if (mCheckBox->isSelected())
260
        mValue = "1";
261
    else
262
        mValue = "0";
263
}
264
265
void SetupItemCheckBox::toWidget()
266
{
267
    if (mCheckBox == nullptr)
268
        return;
269
270
    mCheckBox->setSelected(mValue != "0");
271
}
272
273
274
16
SetupItemTextField::SetupItemTextField(const std::string &restrict text,
275
                                       const std::string &restrict description,
276
                                       const std::string &restrict keyName,
277
                                       SetupTabScroll *restrict const parent,
278
                                       const std::string &restrict eventName,
279
                                       const MainConfig mainConfig,
280
16
                                       const UseBase64 useBase64) :
281
    SetupItem(text, description, keyName, parent, eventName, mainConfig),
282
    mHorizont(nullptr),
283
    mLabel(nullptr),
284
    mTextField(nullptr),
285
    mButton(nullptr),
286
    mEditDialog(nullptr),
287
16
    mUseBase64(useBase64)
288
{
289
16
    mValueType = VSTR;
290
16
    createControls();
291
16
}
292
293
SetupItemTextField::SetupItemTextField(const std::string &restrict text,
294
                                       const std::string &restrict description,
295
                                       const std::string &restrict keyName,
296
                                       SetupTabScroll *restrict const parent,
297
                                       const std::string &restrict eventName,
298
                                       const std::string &restrict def,
299
                                       const MainConfig mainConfig,
300
                                       const UseBase64 useBase64) :
301
    SetupItem(text, description, keyName, parent, eventName, def, mainConfig),
302
    mHorizont(nullptr),
303
    mLabel(nullptr),
304
    mTextField(nullptr),
305
    mButton(nullptr),
306
    mEditDialog(nullptr),
307
    mUseBase64(useBase64)
308
{
309
    mValueType = VSTR;
310
    createControls();
311
}
312
313
32
SetupItemTextField::~SetupItemTextField()
314
{
315
16
    mHorizont = nullptr;
316
16
    mWidget = nullptr;
317
16
    mTextField = nullptr;
318
16
    mLabel = nullptr;
319
16
    mButton = nullptr;
320
16
}
321
322
void SetupItemTextField::save()
323
{
324
    if (mUseBase64 == UseBase64_true)
325
    {
326
        std::string normalValue = mValue;
327
        mValue = encodeBase64String(mValue);
328
        SetupItem::save();
329
        mValue = STD_MOVE(normalValue);
330
    }
331
    else
332
    {
333
        SetupItem::save();
334
    }
335
}
336
337
void SetupItemTextField::cancel(const std::string &eventName A_UNUSED)
338
{
339
    load();
340
    if (mUseBase64 == UseBase64_true)
341
        mValue = decodeBase64String(mValue);
342
    toWidget();
343
}
344
345
void SetupItemTextField::externalUpdated(const std::string &eventName A_UNUSED)
346
{
347
    load();
348
    if (mUseBase64 == UseBase64_true)
349
        mValue = decodeBase64String(mValue);
350
    toWidget();
351
}
352
353
void SetupItemTextField::rereadValue()
354
{
355
    load();
356
    if (mUseBase64 == UseBase64_true)
357
        mValue = decodeBase64String(mValue);
358
    toWidget();
359
}
360
361
16
void SetupItemTextField::createControls()
362
{
363
16
    load();
364
16
    if (mUseBase64 == UseBase64_true)
365
8
        mValue = decodeBase64String(mValue);
366
16
    mHorizont = new HorizontContainer(this, 32, 2);
367
368
16
    mLabel = new Label(this, mText);
369
32
    mLabel->setToolTip(mDescription);
370
16
    mTextField = new TextField(this,
371
        mValue,
372
        LoseFocusOnTab_true,
373
16
        mParent,
374
        mEventName,
375

32
        false);
376
16
    mButton = new Button(this,
377
        // TRANSLATORS: setup item button
378
16
        _("Edit"),
379
32
        mEventName + "_EDIT",
380
        BUTTON_SKIN,
381

80
        mParent);
382
16
    mWidget = mTextField;
383
16
    mTextField->setWidth(200);
384
32
    fixFirstItemSize(mLabel);
385
16
    mHorizont->add(mLabel);
386
16
    mHorizont->add(mTextField);
387
16
    mHorizont->add(mButton);
388
389
32
    mParent->getContainer()->add2(mHorizont, true, 4);
390
16
    mParent->addControl(this);
391
32
    mParent->addControl(this, mEventName + "_EDIT");
392
32
    mParent->addControl(this, mEventName + "_EDIT_OK");
393
16
    mParent->addActionListener(this);
394
16
    mWidget->addActionListener(this);
395
16
    mButton->addActionListener(this);
396
16
}
397
398
void SetupItemTextField::fromWidget()
399
{
400
    if (mTextField == nullptr)
401
        return;
402
403
    mValue = mTextField->getText();
404
}
405
406
void SetupItemTextField::toWidget()
407
{
408
    if (mTextField == nullptr)
409
        return;
410
411
    mTextField->setText(mValue);
412
}
413
414
void SetupItemTextField::action(const ActionEvent &event)
415
{
416
    if (mTextField == nullptr)
417
        return;
418
419
    const std::string &eventId = event.getId();
420
    if ((mWidget != nullptr) && eventId == mWidget->getActionEventId())
421
    {
422
        fromWidget();
423
    }
424
    else if (eventId == mEventName + "_EDIT")
425
    {
426
        mEditDialog = CREATEWIDGETR(EditDialog,
427
            mText,
428
            mTextField->getText(),
429
            mEventName + "_EDIT_OK",
430
            300,
431
            nullptr,
432
            Modal_true);
433
        mEditDialog->addActionListener(this);
434
    }
435
    else if (eventId == mEventName + "_EDIT_OK")
436
    {
437
        mTextField->setText(mEditDialog->getMsg());
438
        mEditDialog = nullptr;
439
    }
440
}
441
442
void SetupItemTextField::apply(const std::string &eventName)
443
{
444
    if (eventName != mEventName)
445
        return;
446
447
    fromWidget();
448
    save();
449
}
450
451
22
SetupItemIntTextField::SetupItemIntTextField(const std::string &restrict text,
452
                                             const std::string &restrict
453
                                             description,
454
                                             const std::string &restrict
455
                                             keyName,
456
                                             SetupTabScroll *restrict
457
                                             const parent,
458
                                             const std::string &restrict
459
                                             eventName,
460
                                             const int min, const int max,
461
22
                                             const MainConfig mainConfig) :
462
    SetupItem(text, description, keyName, parent, eventName, mainConfig),
463
    mHorizont(nullptr),
464
    mLabel(nullptr),
465
    mTextField(nullptr),
466
    mButton(nullptr),
467
    mEditDialog(nullptr),
468
    mMin(min),
469
22
    mMax(max)
470
{
471
22
    mValueType = VSTR;
472
22
    createControls();
473
22
}
474
475
SetupItemIntTextField::SetupItemIntTextField(const std::string &restrict text,
476
                                             const std::string &restrict
477
                                             description,
478
                                             const std::string &restrict
479
                                             keyName,
480
                                             SetupTabScroll *restrict
481
                                             const parent,
482
                                             const std::string &restrict
483
                                             eventName,
484
                                             const int min, const int max,
485
                                             const std::string &restrict def,
486
                                             const MainConfig mainConfig) :
487
    SetupItem(text, description, keyName, parent, eventName, def, mainConfig),
488
    mHorizont(nullptr),
489
    mLabel(nullptr),
490
    mTextField(nullptr),
491
    mButton(nullptr),
492
    mEditDialog(nullptr),
493
    mMin(min),
494
    mMax(max)
495
{
496
    mValueType = VSTR;
497
    createControls();
498
}
499
500
44
SetupItemIntTextField::~SetupItemIntTextField()
501
{
502
22
    mHorizont = nullptr;
503
22
    mWidget = nullptr;
504
22
    mTextField = nullptr;
505
22
    mLabel = nullptr;
506
22
    mButton = nullptr;
507
22
}
508
509
22
void SetupItemIntTextField::createControls()
510
{
511
22
    load();
512
22
    mHorizont = new HorizontContainer(this, 32, 2);
513
514
22
    mLabel = new Label(this, mText);
515
44
    mLabel->setToolTip(mDescription);
516
88
    mTextField = new IntTextField(this, atoi(mValue.c_str()),
517
22
        mMin, mMax, Enable_true, 30);
518
44
    mTextField->setActionEventId(mEventName);
519
22
    mTextField->addActionListener(mParent);
520
521
22
    mButton = new Button(this,
522
        // TRANSLATORS: setup item button
523
22
        _("Edit"),
524
44
        mEventName + "_EDIT",
525
        BUTTON_SKIN,
526

110
        mParent);
527
22
    mWidget = mTextField;
528
22
    mTextField->setWidth(50);
529
44
    fixFirstItemSize(mLabel);
530
22
    mHorizont->add(mLabel);
531
22
    mHorizont->add(mTextField);
532
22
    mHorizont->add(mButton);
533
534
44
    mParent->getContainer()->add2(mHorizont, true, 4);
535
22
    mParent->addControl(this);
536
44
    mParent->addControl(this, mEventName + "_EDIT");
537
44
    mParent->addControl(this, mEventName + "_EDIT_OK");
538
22
    mParent->addActionListener(this);
539
22
    mWidget->addActionListener(this);
540
22
    mButton->addActionListener(this);
541
22
}
542
543
void SetupItemIntTextField::fromWidget()
544
{
545
    if (mTextField == nullptr)
546
        return;
547
548
    mValue = mTextField->getText();
549
}
550
551
void SetupItemIntTextField::toWidget()
552
{
553
    if (mTextField == nullptr)
554
        return;
555
556
    mTextField->setText(mValue);
557
}
558
559
void SetupItemIntTextField::action(const ActionEvent &event)
560
{
561
    if (mTextField == nullptr)
562
        return;
563
564
    const std::string &eventId = event.getId();
565
    if ((mWidget != nullptr) && eventId == mWidget->getActionEventId())
566
    {
567
        fromWidget();
568
    }
569
    else if (eventId == mEventName + "_EDIT")
570
    {
571
        mEditDialog = CREATEWIDGETR(EditDialog,
572
            mText,
573
            mTextField->getText(),
574
            mEventName + "_EDIT_OK",
575
            300,
576
            nullptr,
577
            Modal_true);
578
        mEditDialog->addActionListener(this);
579
    }
580
    else if (eventId == mEventName + "_EDIT_OK")
581
    {
582
        mTextField->setValue(atoi(mEditDialog->getMsg().c_str()));
583
        mEditDialog = nullptr;
584
    }
585
}
586
587
void SetupItemIntTextField::apply(const std::string &eventName)
588
{
589
    if (eventName != mEventName)
590
        return;
591
592
    fromWidget();
593
    save();
594
}
595
596
597
76
SetupItemLabel::SetupItemLabel(const std::string &restrict text,
598
                               const std::string &restrict description,
599
                               SetupTabScroll *restrict const parent,
600
76
                               const Separator separator) :
601
    SetupItem(text, description, "", parent, "", "", MainConfig_true),
602
    mLabel(nullptr),
603

760
    mIsSeparator(separator)
604
{
605
76
    mValueType = VNONE;
606
76
    createControls();
607
76
}
608
609
152
SetupItemLabel::~SetupItemLabel()
610
{
611
76
    mWidget = nullptr;
612
76
    mLabel = nullptr;
613
76
}
614
615
76
void SetupItemLabel::createControls()
616
{
617
76
    if (mIsSeparator == Separator_true)
618
    {
619
        const std::string str(" \342\200\225\342\200\225\342\200\225"
620
304
            "\342\200\225\342\200\225 ");
621

380
        mLabel = new Label(this, std::string(str).append(mText).append(str));
622
    }
623
    else
624
    {
625
        mLabel = new Label(this, mText);
626
    }
627
152
    mLabel->setToolTip(mDescription);
628
629
76
    mWidget = mLabel;
630
152
    mParent->getContainer()->add1(mWidget, -1);
631
76
    mParent->addControl(this);
632
76
    mParent->addActionListener(this);
633
76
    mWidget->addActionListener(this);
634
76
}
635
636
void SetupItemLabel::fromWidget()
637
{
638
}
639
640
void SetupItemLabel::toWidget()
641
{
642
}
643
644
void SetupItemLabel::action(const ActionEvent &event A_UNUSED)
645
{
646
}
647
648
void SetupItemLabel::apply(const std::string &eventName A_UNUSED)
649
{
650
}
651
652
653
28
SetupItemDropDown::SetupItemDropDown(const std::string &restrict text,
654
                                     const std::string &restrict description,
655
                                     const std::string &restrict keyName,
656
                                     SetupTabScroll *restrict const parent,
657
                                     const std::string &restrict eventName,
658
                                     ListModel *restrict const model,
659
                                     const int width,
660
28
                                     const MainConfig mainConfig) :
661
    SetupItem(text, description, keyName, parent, eventName, mainConfig),
662
    mHorizont(nullptr),
663
    mLabel(nullptr),
664
    mModel(model),
665
    mDropDown(nullptr),
666
28
    mWidth(width)
667
{
668
28
    mValueType = VSTR;
669
28
    createControls();
670
28
}
671
672
SetupItemDropDown::SetupItemDropDown(const std::string &restrict text,
673
                                     const std::string &restrict description,
674
                                     const std::string &restrict keyName,
675
                                     SetupTabScroll *restrict const parent,
676
                                     const std::string &restrict eventName,
677
                                     ListModel *restrict const model,
678
                                     const int width,
679
                                     const std::string &restrict def,
680
                                     const MainConfig mainConfig) :
681
    SetupItem(text, description, keyName, parent, eventName, def, mainConfig),
682
    mHorizont(nullptr),
683
    mLabel(nullptr),
684
    mModel(model),
685
    mDropDown(nullptr),
686
    mWidth(width)
687
{
688
    mValueType = VSTR;
689
    createControls();
690
}
691
692
56
SetupItemDropDown::~SetupItemDropDown()
693
{
694
28
    mHorizont = nullptr;
695
28
    mWidget = nullptr;
696
28
    mModel = nullptr;
697
28
    mDropDown = nullptr;
698
28
    mLabel = nullptr;
699
28
}
700
701
28
void SetupItemDropDown::createControls()
702
{
703
28
    load();
704
28
    mHorizont = new HorizontContainer(this, 32, 2);
705
706
28
    mLabel = new Label(this, mText);
707
56
    mLabel->setToolTip(mDescription);
708
28
    mDropDown = new DropDown(this,
709
        mModel,
710
        false,
711
        Modal_false,
712
        nullptr,
713

56
        std::string());
714
56
    mDropDown->setActionEventId(mEventName);
715
28
    mDropDown->addActionListener(mParent);
716
28
    mDropDown->setWidth(mWidth);
717
28
    mDropDown->setSelected(selectionByValue());
718
719
28
    mWidget = mDropDown;
720
56
    if (!mText.empty())
721
28
        fixFirstItemSize(mLabel);
722
28
    mHorizont->add(mLabel);
723
28
    mHorizont->add(mDropDown);
724
725
56
    mParent->getContainer()->add2(mHorizont, true, 4);
726
28
    mParent->addControl(this);
727
28
    mParent->addActionListener(this);
728
28
    mWidget->addActionListener(this);
729
28
}
730
731
int SetupItemDropDown::selectionByValue()
732
{
733
84
    return atoi(mValue.c_str());
734
}
735
736
void SetupItemDropDown::fromWidget()
737
{
738
    if (mDropDown == nullptr)
739
        return;
740
741
    mValue = toString(mDropDown->getSelected());
742
}
743
744
void SetupItemDropDown::toWidget()
745
{
746
    if (mDropDown == nullptr)
747
        return;
748
749
    mDropDown->setSelected(selectionByValue());
750
}
751
752
753
SetupItemDropDownStr::SetupItemDropDownStr(const std::string &restrict text,
754
                                           const std::string &restrict
755
                                           description,
756
                                           const std::string &restrict keyName,
757
                                           SetupTabScroll *restrict const
758
                                           parent,
759
                                           const std::string &restrict
760
                                           eventName,
761
                                           ListModel *restrict const model,
762
                                           const int width,
763
                                           const MainConfig mainConfig) :
764
    SetupItem(text, description, keyName, parent, eventName, mainConfig),
765
    mHorizont(nullptr),
766
    mLabel(nullptr),
767
    mModel(model),
768
    mDropDown(nullptr),
769
    mWidth(width)
770
{
771
    mValueType = VSTR;
772
    createControls();
773
}
774
775
SetupItemDropDownStr::SetupItemDropDownStr(const std::string &restrict text,
776
                                           const std::string &restrict
777
                                           description,
778
                                           const std::string &restrict keyName,
779
                                           SetupTabScroll *restrict const
780
                                           parent,
781
                                           const std::string &restrict
782
                                           eventName,
783
                                           ListModel *restrict const model,
784
                                           const int width,
785
                                           const std::string &restrict def,
786
                                           const MainConfig mainConfig) :
787
    SetupItem(text, description, keyName, parent, eventName, def, mainConfig),
788
    mHorizont(nullptr),
789
    mLabel(nullptr),
790
    mModel(model),
791
    mDropDown(nullptr),
792
    mWidth(width)
793
{
794
    mValueType = VSTR;
795
    createControls();
796
}
797
798
SetupItemDropDownStr::~SetupItemDropDownStr()
799
{
800
    mHorizont = nullptr;
801
    mWidget = nullptr;
802
    mModel = nullptr;
803
    mDropDown = nullptr;
804
    mLabel = nullptr;
805
}
806
807
void SetupItemDropDownStr::createControls()
808
{
809
    load();
810
    mHorizont = new HorizontContainer(this, 32, 2);
811
812
    mLabel = new Label(this, mText);
813
    mLabel->setToolTip(mDescription);
814
    mDropDown = new DropDown(this,
815
        mModel,
816
        false,
817
        Modal_false,
818
        nullptr,
819
        std::string());
820
    mDropDown->setActionEventId(mEventName);
821
    mDropDown->addActionListener(mParent);
822
    mDropDown->setWidth(mWidth);
823
    mDropDown->setSelected(selectionByValue());
824
825
    mWidget = mDropDown;
826
    if (!mText.empty())
827
        fixFirstItemSize(mLabel);
828
    mHorizont->add(mLabel);
829
    mHorizont->add(mDropDown);
830
831
    mParent->getContainer()->add2(mHorizont, true, 4);
832
    mParent->addControl(this);
833
    mParent->addActionListener(this);
834
    mWidget->addActionListener(this);
835
}
836
837
int SetupItemDropDownStr::selectionByValue()
838
{
839
    const int sz = mModel->getNumberOfElements();
840
    for (int f = 0; f < sz; f ++)
841
    {
842
        if (mModel->getElementAt(f) == mValue)
843
        {
844
            return f;
845
        }
846
    }
847
    return 0;
848
}
849
850
void SetupItemDropDownStr::fromWidget()
851
{
852
    if (mDropDown == nullptr)
853
        return;
854
855
    const int sel = mDropDown->getSelected();
856
    // use first element in model as empty string
857
    if (sel == 0 || sel >= mModel->getNumberOfElements())
858
        mValue.clear();
859
    else
860
        mValue = mModel->getElementAt(sel);
861
}
862
863
void SetupItemDropDownStr::toWidget()
864
{
865
    if (mDropDown == nullptr)
866
        return;
867
868
    mDropDown->setSelected(selectionByValue());
869
}
870
871
872
8
SetupItemSlider::SetupItemSlider(const std::string &restrict text,
873
                                 const std::string &restrict description,
874
                                 const std::string &restrict keyName,
875
                                 SetupTabScroll *restrict const parent,
876
                                 const std::string &restrict eventName,
877
                                 const double min,
878
                                 const double max,
879
                                 const double step,
880
                                 const int width,
881
                                 const OnTheFly onTheFly,
882
8
                                 const MainConfig mainConfig) :
883
    SetupItem(text, description, keyName, parent, eventName, mainConfig),
884
    mHorizont(nullptr),
885
    mLabel(nullptr),
886
    mSlider(nullptr),
887
    mMin(min),
888
    mMax(max),
889
    mStep(step),
890
    mWidth(width),
891
8
    mOnTheFly(onTheFly)
892
{
893
8
    mValueType = VSTR;
894
8
    createControls();
895
8
}
896
897
SetupItemSlider::SetupItemSlider(const std::string &restrict text,
898
                                 const std::string &restrict description,
899
                                 const std::string &restrict keyName,
900
                                 SetupTabScroll *restrict const parent,
901
                                 const std::string &restrict eventName,
902
                                 const double min,
903
                                 const double max,
904
                                 const double step,
905
                                 const std::string &restrict def,
906
                                 const int width,
907
                                 const OnTheFly onTheFly,
908
                                 const MainConfig mainConfig) :
909
    SetupItem(text, description, keyName, parent, eventName, def, mainConfig),
910
    mHorizont(nullptr),
911
    mLabel(nullptr),
912
    mSlider(nullptr),
913
    mMin(min),
914
    mMax(max),
915
    mStep(step),
916
    mWidth(width),
917
    mOnTheFly(onTheFly)
918
{
919
    mValueType = VSTR;
920
    createControls();
921
}
922
923
16
SetupItemSlider::~SetupItemSlider()
924
{
925
8
    mHorizont = nullptr;
926
8
    mWidget = nullptr;
927
8
    mSlider = nullptr;
928
8
    mLabel = nullptr;
929
8
}
930
931
8
void SetupItemSlider::createControls()
932
{
933
8
    load();
934
8
    mHorizont = new HorizontContainer(this, 32, 2);
935
936
8
    mLabel = new Label(this, mText);
937
16
    mLabel->setToolTip(mDescription);
938
8
    mSlider = new Slider(this, mMin, mMax, mStep);
939
16
    mSlider->setActionEventId(mEventName);
940
8
    mSlider->addActionListener(mParent);
941
24
    mSlider->setValue(atof(mValue.c_str()));
942
8
    mSlider->setHeight(30);
943
944
8
    mWidget = mSlider;
945
8
    mSlider->setWidth(mWidth);
946
8
    mSlider->setHeight(40);
947
16
    fixFirstItemSize(mLabel);
948
8
    mHorizont->add(mLabel);
949
8
    mHorizont->add(mSlider, -10);
950
951
16
    mParent->getContainer()->add2(mHorizont, true, 4);
952
8
    mParent->addControl(this);
953
8
    mParent->addActionListener(this);
954
8
    mWidget->addActionListener(this);
955
8
}
956
957
void SetupItemSlider::fromWidget()
958
{
959
    if (mSlider == nullptr)
960
        return;
961
962
    mValue = toString(mSlider->getValue());
963
}
964
965
void SetupItemSlider::toWidget()
966
{
967
    if (mSlider == nullptr)
968
        return;
969
970
    mSlider->setValue(atof(mValue.c_str()));
971
}
972
973
void SetupItemSlider::action(const ActionEvent &event A_UNUSED)
974
{
975
    fromWidget();
976
    if (mOnTheFly == OnTheFly_true)
977
        save();
978
}
979
980
void SetupItemSlider::apply(const std::string &eventName)
981
{
982
    if (eventName != mEventName)
983
        return;
984
985
    fromWidget();
986
    save();
987
}
988
989
990
4
SetupItemSlider2::SetupItemSlider2(const std::string &restrict text,
991
                                   const std::string &restrict description,
992
                                   const std::string &restrict keyName,
993
                                   SetupTabScroll *restrict const parent,
994
                                   const std::string &restrict eventName,
995
                                   const int min,
996
                                   const int max,
997
                                   const int step,
998
                                   SetupItemNames *restrict const values,
999
                                   const OnTheFly onTheFly,
1000
                                   const MainConfig mainConfig,
1001
4
                                   const DoNotAlign doNotAlign) :
1002
    SetupItem(text, description, keyName, parent, eventName, mainConfig),
1003
    mHorizont(nullptr),
1004
    mLabel(nullptr),
1005
    mLabel2(nullptr),
1006
    mSlider(nullptr),
1007
    mValues(values),
1008
    mMin(min),
1009
    mMax(max),
1010
    mStep(step),
1011
    mInvertValue(0),
1012
    mInvert(false),
1013
    mOnTheFly(onTheFly),
1014
4
    mDoNotAlign(doNotAlign)
1015
{
1016
4
    mValueType = VSTR;
1017
4
    createControls();
1018
4
}
1019
1020
SetupItemSlider2::SetupItemSlider2(const std::string &restrict text,
1021
                                   const std::string &restrict description,
1022
                                   const std::string &restrict keyName,
1023
                                   SetupTabScroll *restrict const parent,
1024
                                   const std::string &restrict eventName,
1025
                                   const int min,
1026
                                   const int max,
1027
                                   const int step,
1028
                                   SetupItemNames *restrict const values,
1029
                                   const std::string &restrict def,
1030
                                   const OnTheFly onTheFly,
1031
                                   const MainConfig mainConfig,
1032
                                   const DoNotAlign doNotAlign) :
1033
    SetupItem(text, description, keyName, parent, eventName, def, mainConfig),
1034
    mHorizont(nullptr),
1035
    mLabel(nullptr),
1036
    mLabel2(nullptr),
1037
    mSlider(nullptr),
1038
    mValues(values),
1039
    mMin(min),
1040
    mMax(max),
1041
    mStep(step),
1042
    mInvertValue(0),
1043
    mInvert(false),
1044
    mOnTheFly(onTheFly),
1045
    mDoNotAlign(doNotAlign)
1046
{
1047
    mValueType = VSTR;
1048
    createControls();
1049
}
1050
1051
8
SetupItemSlider2::~SetupItemSlider2()
1052
{
1053
4
    mHorizont = nullptr;
1054
4
    mWidget = nullptr;
1055
4
    mSlider = nullptr;
1056
4
    mLabel = nullptr;
1057
4
}
1058
1059
4
void SetupItemSlider2::createControls()
1060
{
1061
4
    load();
1062
4
    mHorizont = new HorizontContainer(this, 32, 2);
1063
1064
4
    const int width = getMaxWidth();
1065
1066
4
    mLabel = new Label(this, mText);
1067
8
    mLabel->setToolTip(mDescription);
1068

16
    mLabel2 = new Label(this, "");
1069
4
    mLabel2->setWidth(width);
1070
4
    mSlider = new Slider(this, mMin, mMax, mStep);
1071
8
    mSlider->setActionEventId(mEventName);
1072
4
    mSlider->addActionListener(mParent);
1073
12
    mSlider->setValue(atof(mValue.c_str()));
1074
4
    mSlider->setHeight(30);
1075
1076
4
    mWidget = mSlider;
1077
4
    mSlider->setWidth(150);
1078
4
    mSlider->setHeight(40);
1079
4
    if (mDoNotAlign == DoNotAlign_false)
1080
4
        fixFirstItemSize(mLabel);
1081
4
    mHorizont->add(mLabel);
1082
4
    mHorizont->add(mSlider, -10);
1083
4
    mHorizont->add(mLabel2);
1084
1085
8
    mParent->getContainer()->add2(mHorizont, true, 4);
1086
4
    mParent->addControl(this);
1087
4
    mParent->addActionListener(this);
1088
4
    mWidget->addActionListener(this);
1089
4
    updateLabel();
1090
4
}
1091
1092
4
int SetupItemSlider2::getMaxWidth()
1093
{
1094

4
    if ((mValues == nullptr) || (gui == nullptr))
1095
        return 1;
1096
1097
4
    int maxWidth = 0;
1098
12
    SetupItemNamesConstIter it = mValues->begin();
1099
12
    const SetupItemNamesConstIter it_end = mValues->end();
1100
8
    const Font *const font = gui->getFont();
1101
1102
20
    while (it != it_end)
1103
    {
1104
16
        const int w = font->getWidth(*it);
1105
16
        if (w > maxWidth)
1106
12
            maxWidth = w;
1107
1108
        ++ it;
1109
    }
1110
    return maxWidth;
1111
}
1112
1113
void SetupItemSlider2::fromWidget()
1114
{
1115
    if (mSlider == nullptr)
1116
        return;
1117
1118
    int val = roundDouble(mSlider->getValue());
1119
    if (mInvert)
1120
        val = mInvertValue - val;
1121
    mValue = toString(val);
1122
}
1123
1124
2
void SetupItemSlider2::toWidget()
1125
{
1126
2
    if (mSlider == nullptr)
1127
        return;
1128
1129
8
    int val = roundDouble(atof(mValue.c_str()));
1130
2
    if (mInvert)
1131
2
        val = mInvertValue - val;
1132
2
    mSlider->setValue(val);
1133
2
    updateLabel();
1134
}
1135
1136
void SetupItemSlider2::action(const ActionEvent &event A_UNUSED)
1137
{
1138
    fromWidget();
1139
    updateLabel();
1140
    if (mOnTheFly == OnTheFly_true)
1141
        save();
1142
}
1143
1144
6
void SetupItemSlider2::updateLabel()
1145
{
1146
6
    int val = CAST_S32(mSlider->getValue()) - mMin;
1147
6
    if (val < 0)
1148
    {
1149
        val = 0;
1150
    }
1151
    else
1152
    {
1153
12
        const int sz = CAST_S32(mValues->size());
1154
6
        if (val >= sz)
1155
            val = sz - 1;
1156
    }
1157
24
    std::string str = mValues->at(val);
1158
6
    mLabel2->setCaption(str);
1159
6
}
1160
1161
void SetupItemSlider2::apply(const std::string &eventName)
1162
{
1163
    if (eventName != mEventName)
1164
        return;
1165
1166
    fromWidget();
1167
    save();
1168
}
1169
1170
2
void SetupItemSlider2::setInvertValue(const int v)
1171
{
1172
2
    mInvert = true;
1173
2
    mInvertValue = v;
1174
2
    toWidget();
1175
2
}
1176
1177
1178
SetupItemSliderList::SetupItemSliderList(const std::string &restrict text,
1179
                                         const std::string &restrict
1180
                                         description,
1181
                                         const std::string &restrict keyName,
1182
                                         SetupTabScroll *restrict const parent,
1183
                                         const std::string &restrict eventName,
1184
                                         ListModel *restrict const model,
1185
                                         const int width,
1186
                                         const OnTheFly onTheFly,
1187
26
                                         const MainConfig mainConfig) :
1188
    SetupItem(text, description, keyName, parent, eventName, mainConfig),
1189
    mHorizont(nullptr),
1190
    mLabel(nullptr),
1191
    mSlider(nullptr),
1192
    mModel(model),
1193
    mWidth(width),
1194
26
    mOnTheFly(onTheFly)
1195
{
1196
26
    mValueType = VSTR;
1197
}
1198
1199
SetupItemSliderList::SetupItemSliderList(const std::string &restrict text,
1200
                                         const std::string &restrict
1201
                                         description,
1202
                                         const std::string &restrict keyName,
1203
                                         SetupTabScroll *restrict const parent,
1204
                                         const std::string &restrict eventName,
1205
                                         ListModel *restrict const model,
1206
                                         const std::string &restrict def,
1207
                                         const int width,
1208
                                         const OnTheFly onTheFly,
1209
                                         const MainConfig mainConfig) :
1210
    SetupItem(text, description, keyName, parent, eventName, def, mainConfig),
1211
    mHorizont(nullptr),
1212
    mLabel(nullptr),
1213
    mSlider(nullptr),
1214
    mModel(model),
1215
    mWidth(width),
1216
    mOnTheFly(onTheFly)
1217
{
1218
    mValueType = VSTR;
1219
}
1220
1221
52
SetupItemSliderList::~SetupItemSliderList()
1222
{
1223
26
    mHorizont = nullptr;
1224
26
    mWidget = nullptr;
1225
26
    mSlider = nullptr;
1226
26
    mLabel = nullptr;
1227
}
1228
1229
26
void SetupItemSliderList::createControls()
1230
{
1231
26
    load();
1232
26
    mHorizont = new HorizontContainer(this, 32, 2);
1233
1234
26
    mLabel = new Label(this, mText);
1235
52
    mLabel->setToolTip(mDescription);
1236
26
    mSlider = new SliderList(this, mModel);
1237
26
    mSlider->postInit2(mParent, mEventName);
1238
26
    mSlider->setSelectedString(mValue);
1239
26
    mSlider->adjustSize();
1240
1241
26
    mWidget = mSlider;
1242
52
    fixFirstItemSize(mLabel);
1243
26
    mHorizont->add(mLabel, 5);
1244
26
    mHorizont->add(mSlider);
1245
1246
26
    addMoreControls();
1247
1248
52
    mParent->getContainer()->add2(mHorizont, true, 4);
1249
26
    mParent->addControl(this);
1250
26
    mParent->addActionListener(this);
1251
26
    mWidget->addActionListener(this);
1252
26
}
1253
1254
void SetupItemSliderList::fromWidget()
1255
{
1256
    if (mSlider == nullptr)
1257
        return;
1258
1259
    mValue = mSlider->getSelectedString();
1260
}
1261
1262
void SetupItemSliderList::toWidget()
1263
{
1264
    if (mSlider == nullptr)
1265
        return;
1266
1267
    mSlider->setSelectedString(mValue);
1268
}
1269
1270
void SetupItemSliderList::action(const ActionEvent &event A_UNUSED)
1271
{
1272
    fromWidget();
1273
    if (mOnTheFly == OnTheFly_true)
1274
        save();
1275
}
1276
1277
void SetupItemSliderList::apply(const std::string &eventName)
1278
{
1279
    if (eventName != mEventName)
1280
        return;
1281
1282
    fromWidget();
1283
    save();
1284
}
1285
1286
24
SetupItemSound::SetupItemSound(const std::string &restrict text,
1287
                               const std::string &restrict description,
1288
                               const std::string &restrict keyName,
1289
                               SetupTabScroll *restrict const parent,
1290
                               const std::string &restrict eventName,
1291
                               ListModel *restrict const model,
1292
                               const int width,
1293
                               const OnTheFly onTheFly,
1294
24
                               const MainConfig mainConfig) :
1295
    SetupItemSliderList(text, description, keyName, parent, eventName,
1296
                        model, width, onTheFly, mainConfig),
1297
48
    mButton(nullptr)
1298
{
1299
24
    createControls();
1300
24
}
1301
1302
24
void SetupItemSound::addMoreControls()
1303
{
1304
24
    mButton = new Button(this,
1305
        BUTTON_PLAY,
1306
        16, 16,
1307
48
        mEventName + "_PLAY",
1308
        BUTTON_SKIN,
1309

24
        this);
1310
24
    if (mHorizont != nullptr)
1311
24
        mHorizont->add(mButton);
1312
24
}
1313
1314
void SetupItemSound::action(const ActionEvent &event)
1315
{
1316
    if (event.getId() == mEventName + "_PLAY")
1317
    {
1318
        if ((mSlider != nullptr) && (mSlider->getSelected() != 0))
1319
        {
1320
            soundManager.playGuiSfx(pathJoin(branding.getStringValue(
1321
                "systemsounds"),
1322
                mSlider->getSelectedString()).append(".ogg"));
1323
        }
1324
    }
1325
    else
1326
    {
1327
        SetupItemSliderList::action(event);
1328
    }
1329
}
1330
1331
2
SetupItemSliderInt::SetupItemSliderInt(const std::string &restrict text,
1332
                                       const std::string &restrict description,
1333
                                       const std::string &restrict keyName,
1334
                                       SetupTabScroll *restrict const parent,
1335
                                       const std::string &restrict eventName,
1336
                                       ListModel *restrict const model,
1337
                                       const int min,
1338
                                       const int width,
1339
                                       const OnTheFly onTheFly,
1340
2
                                       const MainConfig mainConfig) :
1341
    SetupItemSliderList(text, description, keyName, parent, eventName,
1342
                        model, width, onTheFly, mainConfig),
1343
4
    mMin(min)
1344
{
1345
2
    createControls();
1346
2
}
1347
1348
2
void SetupItemSliderInt::addMoreControls()
1349
{
1350
2
    toWidget();
1351
2
}
1352
1353
void SetupItemSliderInt::fromWidget()
1354
{
1355
    if (mSlider == nullptr)
1356
        return;
1357
1358
    mValue = toString(mSlider->getSelected() + mMin);
1359
}
1360
1361
2
void SetupItemSliderInt::toWidget()
1362
{
1363
2
    if (mSlider == nullptr)
1364
        return;
1365
1366
6
    mSlider->setSelected(atoi(mValue.c_str()) - mMin);
1367

3
}