GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/setupitem.cpp Lines: 337 577 58.4 %
Date: 2017-11-29 Branches: 114 400 28.5 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-2017  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
796
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
796
                     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
7960
    mUseDefault(false)
74
{
75
796
}
76
77
164
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
164
                     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
1640
    mUseDefault(true)
98
{
99
164
}
100
101
7680
SetupItem::~SetupItem()
102
{
103
960
}
104
105
Configuration *SetupItem::getConfig() const
106
{
107


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

792
        switch (mValueType)
125
        {
126
540
            case VBOOL:
127
540
                if (cfg->getBoolValue(mKeyName))
128
320
                    mValue = "1";
129
                else
130
220
                    mValue = "0";
131
                break;
132
252
            case VSTR:
133
            default:
134
504
                mValue = cfg->getStringValue(mKeyName);
135
252
                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
960
std::string SetupItem::getActionEventId() const
155
{
156
960
    if (mWidget == nullptr)
157
        return std::string();
158
159
960
    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
52
void SetupItem::fixFirstItemSize(Widget *const widget)
198
{
199




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




252
    if (widget->getWidth() < maxSize)
203
208
        widget->setWidth(maxSize);
204
}
205
206
void SetupItem::rereadValue()
207
{
208
    load();
209
    toWidget();
210
}
211
212
540
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
540
                                     const MainConfig mainConfig) :
218
    SetupItem(text, description, keyName, parent, eventName, mainConfig),
219
540
    mCheckBox(nullptr)
220
{
221
540
    createControls();
222
540
}
223
224
12
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
12
                                     const MainConfig mainConfig) :
231
    SetupItem(text, description, keyName, parent, eventName, def, mainConfig),
232
12
    mCheckBox(nullptr)
233
{
234
12
    createControls();
235
12
}
236
237
1104
SetupItemCheckBox::~SetupItemCheckBox()
238
{
239
552
    mWidget = nullptr;
240
552
}
241
242
552
void SetupItemCheckBox::createControls()
243
{
244
552
    load();
245

1104
    mCheckBox = new CheckBox(this, mText, mValue != "0", mParent, mEventName);
246
1104
    mCheckBox->setToolTip(mDescription);
247
552
    mWidget = mCheckBox;
248
1104
    mParent->getContainer()->add1(mWidget);
249
552
    mParent->addControl(this);
250
552
    mParent->addActionListener(this);
251
552
    mWidget->addActionListener(this);
252
552
}
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
32
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
32
                                       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
32
    mUseBase64(useBase64)
288
{
289
32
    mValueType = VSTR;
290
32
    createControls();
291
32
}
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
64
SetupItemTextField::~SetupItemTextField()
314
{
315
32
    mHorizont = nullptr;
316
32
    mWidget = nullptr;
317
32
    mTextField = nullptr;
318
32
    mLabel = nullptr;
319
32
    mButton = nullptr;
320
32
}
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
32
void SetupItemTextField::createControls()
362
{
363
32
    load();
364
32
    if (mUseBase64 == UseBase64_true)
365
16
        mValue = decodeBase64String(mValue);
366
32
    mHorizont = new HorizontContainer(this, 32, 2);
367
368
32
    mLabel = new Label(this, mText);
369
64
    mLabel->setToolTip(mDescription);
370
32
    mTextField = new TextField(this,
371
        mValue,
372
        LoseFocusOnTab_true,
373
32
        mParent,
374

64
        mEventName);
375
    // TRANSLATORS: setup item button
376


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


220
    mButton = new Button(this, _("Edit"), mEventName + "_EDIT", mParent);
515
44
    mWidget = mTextField;
516
44
    mTextField->setWidth(50);
517
88
    fixFirstItemSize(mLabel);
518
44
    mHorizont->add(mLabel);
519
44
    mHorizont->add(mTextField);
520
44
    mHorizont->add(mButton);
521
522
88
    mParent->getContainer()->add2(mHorizont, true, 4);
523
44
    mParent->addControl(this);
524
88
    mParent->addControl(this, mEventName + "_EDIT");
525
88
    mParent->addControl(this, mEventName + "_EDIT_OK");
526
44
    mParent->addActionListener(this);
527
44
    mWidget->addActionListener(this);
528
44
    mButton->addActionListener(this);
529
44
}
530
531
void SetupItemIntTextField::fromWidget()
532
{
533
    if (mTextField == nullptr)
534
        return;
535
536
    mValue = mTextField->getText();
537
}
538
539
void SetupItemIntTextField::toWidget()
540
{
541
    if (mTextField == nullptr)
542
        return;
543
544
    mTextField->setText(mValue);
545
}
546
547
void SetupItemIntTextField::action(const ActionEvent &event)
548
{
549
    if (mTextField == nullptr)
550
        return;
551
552
    const std::string &eventId = event.getId();
553
    if ((mWidget != nullptr) && eventId == mWidget->getActionEventId())
554
    {
555
        fromWidget();
556
    }
557
    else if (eventId == mEventName + "_EDIT")
558
    {
559
        mEditDialog = CREATEWIDGETR(EditDialog,
560
            mText,
561
            mTextField->getText(),
562
            mEventName + "_EDIT_OK");
563
        mEditDialog->addActionListener(this);
564
    }
565
    else if (eventId == mEventName + "_EDIT_OK")
566
    {
567
        mTextField->setValue(atoi(mEditDialog->getMsg().c_str()));
568
        mEditDialog = nullptr;
569
    }
570
}
571
572
void SetupItemIntTextField::apply(const std::string &eventName)
573
{
574
    if (eventName != mEventName)
575
        return;
576
577
    fromWidget();
578
    save();
579
}
580
581
582
152
SetupItemLabel::SetupItemLabel(const std::string &restrict text,
583
                               const std::string &restrict description,
584
                               SetupTabScroll *restrict const parent,
585
152
                               const Separator separator) :
586
    SetupItem(text, description, "", parent, "", "", MainConfig_true),
587
    mLabel(nullptr),
588


1520
    mIsSeparator(separator)
589
{
590
152
    mValueType = VNONE;
591
152
    createControls();
592
152
}
593
594
304
SetupItemLabel::~SetupItemLabel()
595
{
596
152
    mWidget = nullptr;
597
152
    mLabel = nullptr;
598
152
}
599
600
152
void SetupItemLabel::createControls()
601
{
602
152
    if (mIsSeparator == Separator_true)
603
    {
604
        const std::string str(" \342\200\225\342\200\225\342\200\225"
605
608
            "\342\200\225\342\200\225 ");
606

608
        mLabel = new Label(this, std::string(str).append(mText).append(str));
607
    }
608
    else
609
    {
610
        mLabel = new Label(this, mText);
611
    }
612
304
    mLabel->setToolTip(mDescription);
613
614
152
    mWidget = mLabel;
615
304
    mParent->getContainer()->add1(mWidget);
616
152
    mParent->addControl(this);
617
152
    mParent->addActionListener(this);
618
152
    mWidget->addActionListener(this);
619
152
}
620
621
void SetupItemLabel::fromWidget()
622
{
623
}
624
625
void SetupItemLabel::toWidget()
626
{
627
}
628
629
void SetupItemLabel::action(const ActionEvent &event A_UNUSED)
630
{
631
}
632
633
void SetupItemLabel::apply(const std::string &eventName A_UNUSED)
634
{
635
}
636
637
638
56
SetupItemDropDown::SetupItemDropDown(const std::string &restrict text,
639
                                     const std::string &restrict description,
640
                                     const std::string &restrict keyName,
641
                                     SetupTabScroll *restrict const parent,
642
                                     const std::string &restrict eventName,
643
                                     ListModel *restrict const model,
644
                                     const int width,
645
56
                                     const MainConfig mainConfig) :
646
    SetupItem(text, description, keyName, parent, eventName, mainConfig),
647
    mHorizont(nullptr),
648
    mLabel(nullptr),
649
    mModel(model),
650
    mDropDown(nullptr),
651
56
    mWidth(width)
652
{
653
56
    mValueType = VSTR;
654
56
    createControls();
655
56
}
656
657
SetupItemDropDown::SetupItemDropDown(const std::string &restrict text,
658
                                     const std::string &restrict description,
659
                                     const std::string &restrict keyName,
660
                                     SetupTabScroll *restrict const parent,
661
                                     const std::string &restrict eventName,
662
                                     ListModel *restrict const model,
663
                                     const int width,
664
                                     const std::string &restrict def,
665
                                     const MainConfig mainConfig) :
666
    SetupItem(text, description, keyName, parent, eventName, def, mainConfig),
667
    mHorizont(nullptr),
668
    mLabel(nullptr),
669
    mModel(model),
670
    mDropDown(nullptr),
671
    mWidth(width)
672
{
673
    mValueType = VSTR;
674
    createControls();
675
}
676
677
112
SetupItemDropDown::~SetupItemDropDown()
678
{
679
56
    mHorizont = nullptr;
680
56
    mWidget = nullptr;
681
56
    mModel = nullptr;
682
56
    mDropDown = nullptr;
683
56
    mLabel = nullptr;
684
56
}
685
686
56
void SetupItemDropDown::createControls()
687
{
688
56
    load();
689
56
    mHorizont = new HorizontContainer(this, 32, 2);
690
691
56
    mLabel = new Label(this, mText);
692
112
    mLabel->setToolTip(mDescription);
693

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

32
    mLabel2 = new Label(this, "");
1044
8
    mLabel2->setWidth(width);
1045
8
    mSlider = new Slider(this, mMin, mMax, mStep);
1046
16
    mSlider->setActionEventId(mEventName);
1047
8
    mSlider->addActionListener(mParent);
1048
24
    mSlider->setValue(atof(mValue.c_str()));
1049
8
    mSlider->setHeight(30);
1050
1051
8
    mWidget = mSlider;
1052
8
    mSlider->setWidth(150);
1053
8
    mSlider->setHeight(40);
1054
8
    if (mDoNotAlign == DoNotAlign_false)
1055
8
        fixFirstItemSize(mLabel);
1056
8
    mHorizont->add(mLabel);
1057
8
    mHorizont->add(mSlider, -10);
1058
8
    mHorizont->add(mLabel2);
1059
1060
16
    mParent->getContainer()->add2(mHorizont, true, 4);
1061
8
    mParent->addControl(this);
1062
8
    mParent->addActionListener(this);
1063
8
    mWidget->addActionListener(this);
1064
8
    updateLabel();
1065
8
}
1066
1067
8
int SetupItemSlider2::getMaxWidth()
1068
{
1069

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

80
        mEventName + "_PLAY", this);
1281
40
    if (mHorizont != nullptr)
1282
40
        mHorizont->add(mButton);
1283
40
}
1284
1285
void SetupItemSound::action(const ActionEvent &event)
1286
{
1287
    if (event.getId() == mEventName + "_PLAY")
1288
    {
1289
        if ((mSlider != nullptr) && (mSlider->getSelected() != 0))
1290
        {
1291
            soundManager.playGuiSfx(pathJoin(branding.getStringValue(
1292
                "systemsounds"),
1293
                mSlider->getSelectedString()).append(".ogg"));
1294
        }
1295
    }
1296
    else
1297
    {
1298
        SetupItemSliderList::action(event);
1299
    }
1300
}
1301
1302
4
SetupItemSliderInt::SetupItemSliderInt(const std::string &restrict text,
1303
                                       const std::string &restrict description,
1304
                                       const std::string &restrict keyName,
1305
                                       SetupTabScroll *restrict const parent,
1306
                                       const std::string &restrict eventName,
1307
                                       ListModel *restrict const model,
1308
                                       const int min,
1309
                                       const int width,
1310
                                       const OnTheFly onTheFly,
1311
4
                                       const MainConfig mainConfig) :
1312
    SetupItemSliderList(text, description, keyName, parent, eventName,
1313
                        model, width, onTheFly, mainConfig),
1314
8
    mMin(min)
1315
{
1316
4
    createControls();
1317
4
}
1318
1319
4
void SetupItemSliderInt::addMoreControls()
1320
{
1321
4
    toWidget();
1322
4
}
1323
1324
void SetupItemSliderInt::fromWidget()
1325
{
1326
    if (mSlider == nullptr)
1327
        return;
1328
1329
    mValue = toString(mSlider->getSelected() + mMin);
1330
}
1331
1332
4
void SetupItemSliderInt::toWidget()
1333
{
1334
4
    if (mSlider == nullptr)
1335
        return;
1336
1337
12
    mSlider->setSelected(atoi(mValue.c_str()) - mMin);
1338

6
}