• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Return-To-The-Roots / s25client / 44076625557

17 Nov 2026 11:43AM UTC coverage: 51.803% (-0.002%) from 51.805%
44076625557

push

github

web-flow
Merge pull request #2018 from Return-To-The-Roots/refactor/lowerCamelCase-methods

Rename Msg_* handlers to lowerCamelCase

8121 of 16693 new or added lines in 565 files covered. (48.65%)

323 existing lines in 133 files now uncovered.

24236 of 46785 relevant lines covered (51.8%)

49647.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

43.8
/libs/s25main/controls/ctrlTable.cpp
1
// Copyright (C) 2005 - 2021 Settlers Freaks (sf-team at siedler25.org)
2
//
3
// SPDX-License-Identifier: GPL-2.0-or-later
4

5
#include "ctrlTable.h"
6
#include "CollisionDetection.h"
7
#include "ctrlButton.h"
8
#include "ctrlScrollBar.h"
9
#include "driver/KeyEvent.h"
10
#include "driver/MouseCoords.h"
11
#include "helpers/containerUtils.h"
12
#include "helpers/mathFuncs.h"
13
#include "ogl/glFont.h"
14
#include "s25util/StringConversion.h"
15
#include "s25util/strAlgos.h"
16
#include <algorithm>
17
#include <numeric>
18
#include <sstream>
19

20
/// Return <0 for a < b; >0 for a>b and 0 for a==b
21
static int Compare(const std::string& a, const std::string& b, ctrlTable::SortType sortType)
87 ✔
22
{
23
    using SRT = ctrlTable::SortType;
24
    switch(sortType)
87 ✔
25
    {
26
        case SRT::Default:
33 ✔
27
        case SRT::String: return a.compare(b); break;
33 ✔
28
        case SRT::MapSize:
15 ✔
29
        {
30
            // Nach Mapgrößen-String sortieren: ZahlxZahl
31
            s25util::ClassicImbuedStream<std::istringstream> ss_a(a);
30 ✔
32
            s25util::ClassicImbuedStream<std::istringstream> ss_b(b);
15 ✔
33
            char x;
34
            int x_a, y_a, x_b, y_b;
35
            ss_a >> x_a >> x >> y_a;
15 ✔
36
            ss_b >> x_b >> x >> y_b;
15 ✔
37
            RTTR_Assert(ss_a);
15 ✔
38
            RTTR_Assert(ss_b);
15 ✔
39
            const int result = x_a * y_a - x_b * y_b;
15 ✔
40
            // In case of same number of nodes sort by first value
41
            return (result == 0) ? x_a - x_b : result;
15 ✔
42
        }
43
        break;
44
        case SRT::Number:
17 ✔
45
        {
46
            s25util::ClassicImbuedStream<std::istringstream> ss_a(a);
34 ✔
47
            s25util::ClassicImbuedStream<std::istringstream> ss_b(b);
17 ✔
48
            int num_a, num_b;
49
            ss_a >> num_a;
17 ✔
50
            ss_b >> num_b;
17 ✔
51
            RTTR_Assert(ss_a);
17 ✔
52
            RTTR_Assert(ss_b);
17 ✔
53
            return num_a - num_b;
17 ✔
54
        }
55
        break;
56
        case SRT::Date:
22 ✔
57
        {
58
            // Nach Datum im Format dd.mm.yyyy - hh:mm sortieren
59
            s25util::ClassicImbuedStream<std::istringstream> ss_a(a);
44 ✔
60
            s25util::ClassicImbuedStream<std::istringstream> ss_b(b);
44 ✔
61
            int d_a, d_b, m_a, m_b, y_a, y_b;
62
            char c;
63

64
            // "dd.mm.yyyy"
65
            ss_a >> d_a >> c >> m_a >> c >> y_a;
22 ✔
66
            ss_b >> d_b >> c >> m_b >> c >> y_b;
22 ✔
67

68
            RTTR_Assert(ss_a);
22 ✔
69
            RTTR_Assert(ss_b);
22 ✔
70
            if(y_a != y_b)
22 ✔
71
                return y_a - y_b;
5 ✔
72

73
            if(m_a != m_b)
17 ✔
74
                return m_a - m_b;
5 ✔
75

76
            if(d_a != d_b)
12 ✔
77
                return d_a - d_b;
7 ✔
78

79
            // " - "
80
            ss_a >> c;
5 ✔
81
            ss_b >> c;
5 ✔
82

83
            int h_a, h_b, min_a, min_b;
84

85
            // "hh:mm"
86
            ss_a >> h_a >> c >> min_a;
5 ✔
87
            ss_b >> h_b >> c >> min_b;
5 ✔
88

89
            RTTR_Assert(ss_a);
5 ✔
90
            RTTR_Assert(ss_b);
5 ✔
91
            if(h_a != h_b)
5 ✔
92
                return h_a - h_b;
3 ✔
93
            return min_a - min_b;
2 ✔
94
        }
95
        case SRT::Time:
×
96
        {
97
            // Sort by time with format h:mm:ss or mm:ss
98
            s25util::ClassicImbuedStream<std::istringstream> ss_a(a);
×
99
            s25util::ClassicImbuedStream<std::istringstream> ss_b(b);
×
100

101
            int seconds_a = 0;
×
102
            int seconds_b = 0;
×
103
            char c;
104

105
            // "h:mm:ss" or "mm:ss"
106
            int tmp;
107
            while(ss_a >> tmp)
×
108
            {
109
                seconds_a *= 60;
×
110
                seconds_a += tmp;
×
111
                ss_a >> c;
×
112
            }
113
            while(ss_b >> tmp)
×
114
            {
115
                seconds_b *= 60;
×
116
                seconds_b += tmp;
×
117
                ss_b >> c;
×
118
            }
119

120
            return seconds_a - seconds_b;
×
121
        }
122
        break;
123
    }
124
    return 0;
×
125
}
126

127
ctrlTable::ctrlTable(Window* parent, unsigned id, const DrawPoint& pos, const Extent& size, TextureColor tc,
35 ✔
128
                     const glFont* font, Columns columns)
35 ✔
129
    : Window(parent, id, pos, elMax(size, Extent(20, 30))), tc(tc), font(font), columns_(std::move(columns)),
35 ✔
130
      selection_(-1), sortColumn_(-1), sortDir_(TableSortDir::Ascending)
70 ✔
131
{
132
    // We use unsigned short when handling the column count
133
    if(columns_.size() > std::numeric_limits<unsigned short>::max())
35 ✔
134
        throw std::range_error("Maximum amount of columns exceeded");
×
135

136
    header_height = font->getHeight() + 10;
35 ✔
137
    line_count = (getSize().y - header_height - 2) / font->getHeight();
35 ✔
138

139
    // Scrollbar hinzufügen
140
    addScrollBar(0, DrawPoint(getSize().x - 20, 0), Extent(20, getSize().y), 20, tc, line_count);
35 ✔
141

142
    for(unsigned i = 0; i < columns_.size(); ++i)
107 ✔
143
    {
144
        addTextButton(i + 1, DrawPoint(0, 0), Extent(0, header_height), tc, columns_[i].title, font);
72 ✔
145
    }
146

147
    resetButtonWidths();
35 ✔
148
}
35 ✔
149

150
/**
151
 *  Größe verändern
152
 */
NEW
153
void ctrlTable::resize(const Extent& newSize)
×
154
{
NEW
155
    const bool heightIncreased = newSize.y > getSize().y;
×
NEW
156
    Window::resize(newSize);
×
157
    // Make header buttons keep their size
158
    for(unsigned i = 1; i <= columns_.size(); ++i)
×
NEW
159
        getCtrl<ctrlButton>(i)->setHeight(header_height);
×
160

NEW
161
    auto* scrollbar = getCtrl<ctrlScrollBar>(0);
×
162

163
    // changed height
164

NEW
165
    scrollbar->resize(Extent(20, newSize.y));
×
NEW
166
    scrollbar->setPos(DrawPoint(newSize.x - scrollbar->getSize().x, 0));
×
167

168
    line_count = (newSize.y - header_height - 2) / font->getHeight();
×
NEW
169
    scrollbar->setPageSize(line_count);
×
170

171
    // If the size was enlarged we have to check that we don't try to
172
    // display more lines than present
173
    if(heightIncreased)
×
NEW
174
        while(rows_.size() - scrollbar->getScrollPos() < line_count && scrollbar->getScrollPos() > 0)
×
NEW
175
            scrollbar->setScrollPos(scrollbar->getScrollPos() - 1);
×
176

177
    // changed width
178

NEW
179
    resetButtonWidths();
×
NEW
180
    if(scrollbar->isVisible())
×
NEW
181
        msgScrollShow(0, true);
×
UNCOV
182
}
×
183

184
/**
185
 *  löscht alle Items.
186
 */
187
void ctrlTable::deleteAllItems()
37 ✔
188
{
189
    rows_.clear();
37 ✔
190

191
    getCtrl<ctrlScrollBar>(0)->setRange(0);
37 ✔
192

193
    setSelection(std::nullopt);
37 ✔
194
    sortColumn_ = -1;
37 ✔
195
    sortDir_ = TableSortDir::Ascending;
37 ✔
196
}
37 ✔
197

198
/**
199
 *  setzt die Auswahl.
200
 *
201
 *  @param[in] selection Der Auswahlindex
202
 */
203
void ctrlTable::setSelection(const std::optional<unsigned>& selection)
75 ✔
204
{
205
    if(!selection)
75 ✔
206
        selection_ = std::nullopt;
58 ✔
207
    else if(*selection >= rows_.size())
17 ✔
208
        return;
×
209
    else
210
    {
211
        selection_ = selection;
17 ✔
212
        // Scroll into view
213
        auto* scrollbar = getCtrl<ctrlScrollBar>(0);
17 ✔
214
        if(*selection_ < scrollbar->getScrollPos())
17 ✔
NEW
215
            scrollbar->setScrollPos(*selection_);
×
216
        else if(*selection_ >= static_cast<unsigned>(scrollbar->getScrollPos() + scrollbar->getPageSize()))
17 ✔
NEW
217
            scrollbar->setScrollPos(*selection_ - scrollbar->getPageSize() + 1);
×
218
    }
219

220
    if(getParent())
75 ✔
221
        getParent()->msgTableSelectItem(getID(), selection_);
75 ✔
222
}
223

224
void ctrlTable::addRow(std::vector<std::string> row)
38 ✔
225
{
226
    // We use unsigned short when handling the row count
227
    if(rows_.size() == std::numeric_limits<unsigned short>::max())
38 ✔
228
        throw std::range_error("Maximum amount of rows exceeded");
×
229
    if(row.size() > getNumColumns())
38 ✔
230
        throw std::logic_error("Invalid number of columns for added row");
×
231
    for(unsigned i = row.size(); i < getNumColumns(); ++i)
38 ✔
232
    {
233
        row.push_back("");
×
234
    }
235

236
    rows_.emplace_back(Row{std::move(row)});
38 ✔
237
    getCtrl<ctrlScrollBar>(0)->setRange(getNumRows());
38 ✔
238
}
38 ✔
239

NEW
240
void ctrlTable::removeRow(unsigned rowIdx)
×
241
{
242
    if(rowIdx >= rows_.size())
×
243
        return;
×
244
    rows_.erase(rows_.begin() + rowIdx);
×
NEW
245
    getCtrl<ctrlScrollBar>(0)->setRange(static_cast<unsigned short>(rows_.size()));
×
246
    if(selection_ && *selection_ >= rows_.size())
×
247
    {
248
        if(rows_.empty())
×
249
            selection_.reset();
×
250
        else
251
            selection_ = rows_.size() - 1u;
×
252
    }
NEW
253
    setSelection(selection_);
×
254
}
255

256
/**
257
 *  liefert den Wert eines Feldes.
258
 *
259
 *  @param[in] row    Die Zeile
260
 *  @param[in] column Die Spalte
261
 *
262
 *  @return Text in Feld <@p column,@p row>
263
 */
264
const std::string& ctrlTable::getItemText(unsigned short row, unsigned short column) const
158 ✔
265
{
266
    static std::string empty;
158 ✔
267
    if(row >= rows_.size() || column >= columns_.size())
158 ✔
268
        return empty;
×
269

270
    return rows_[row].columns[column];
158 ✔
271
}
272

273
/**
274
 *  sortiert die Zeilen.
275
 *
276
 *  @param[in] column    Die Spalte nach der sortiert werden soll.
277
 *  @param[in] ascending Die Richtung in die sortiert werden soll
278
 *                         @p true  - A-Z,
279
 *                         @p false - Z-A
280
 */
281
void ctrlTable::sortRows(unsigned column, const TableSortDir sortDir)
42 ✔
282
{
283
    sortDir_ = sortDir;
42 ✔
284
    sortColumn_ = column;
42 ✔
285

286
    if(sortColumn_ >= getNumColumns())
42 ✔
287
    {
288
        sortColumn_ = -1;
×
289
        return;
×
290
    }
291

292
    const TableSortType sortType = columns_[column].sortType;
42 ✔
293
    const auto compareFunc = [sortType, sortDir, column](const Row& lhs, const Row& rhs) {
348 ✔
294
        const std::string a = s25util::toLower(lhs.columns[column]);
174 ✔
295
        const std::string b = s25util::toLower(rhs.columns[column]);
87 ✔
296
        const int cmpResult = Compare(a, b, sortType);
87 ✔
297
        return (sortDir == TableSortDir::Ascending) ? cmpResult < 0 : cmpResult > 0;
174 ✔
298
    };
42 ✔
299
    helpers::sort(rows_, compareFunc);
42 ✔
300
}
301

302
/**
303
 *  Zeichenmethode
304
 *
305
 *  @return @p true bei Erfolg, @p false bei Fehler
306
 */
NEW
307
void ctrlTable::drawImpl()
×
308
{
NEW
309
    draw3D(Rect(getDrawPos(), getSize()), tc, false);
×
310

NEW
311
    Window::drawImpl();
×
312

313
    const auto lines = static_cast<unsigned>(line_count > rows_.size() ? rows_.size() : line_count);
×
NEW
314
    const auto* scroll = getCtrl<ctrlScrollBar>(0);
×
NEW
315
    DrawPoint curPos = getDrawPos() + DrawPoint(2, 2 + header_height);
×
UNCOV
316
    for(unsigned i = 0; i < lines; ++i)
×
317
    {
NEW
318
        const unsigned curRow = i + scroll->getScrollPos();
×
NEW
319
        RTTR_Assert(curRow < getNumRows());
×
320
        const bool isSelected = selection_ == curRow;
×
321
        if(isSelected)
×
322
        {
323
            // durchsichtig schwarze Markierung malen
NEW
324
            drawRectangle(Rect(curPos, getSize().x - 4 - (scroll->isVisible() ? 24 : 0), font->getHeight()),
×
325
                          0x80000000);
326
        }
327

328
        DrawPoint colPos = curPos;
×
329
        for(unsigned c = 0; c < columns_.size(); ++c)
×
330
        {
331
            if(columns_[c].width == 0)
×
332
                continue;
×
333

NEW
334
            const auto* bt = getCtrl<ctrlButton>(c + 1);
×
NEW
335
            font->draw(colPos, rows_[curRow].columns[c], FontStyle{}, (isSelected ? 0xFFFFAA00 : COLOR_YELLOW),
×
NEW
336
                       bt->getSize().x, "");
×
NEW
337
            colPos.x += bt->getSize().x;
×
338
        }
339
        curPos.y += font->getHeight();
×
340
    }
341
}
×
342

NEW
343
void ctrlTable::msgButtonClick(const unsigned ctrl_id)
×
344
{
345
    RTTR_Assert(ctrl_id > 0);
×
346
    const int column = ctrl_id - 1;
×
NEW
347
    sortRows(column, column == getSortColumn() && getSortDirection() == TableSortDir::Ascending ?
×
348
                       TableSortDir::Descending :
349
                       TableSortDir::Ascending);
NEW
350
    setSelection(selection_);
×
351
}
×
352

NEW
353
Rect ctrlTable::getContentDrawArea() const
×
354
{
NEW
355
    DrawPoint orig = getDrawPos();
×
356
    orig.y += header_height;
×
NEW
357
    Extent size = getSize() - Extent(20, header_height);
×
358
    return Rect(orig, size);
×
359
}
360

NEW
361
Rect ctrlTable::getFullDrawArea() const
×
362
{
NEW
363
    DrawPoint orig = getDrawPos() + DrawPoint(2, 2);
×
NEW
364
    Extent size = getSize() - Extent(2, 4);
×
UNCOV
365
    return Rect(orig, size);
×
366
}
367

NEW
368
bool ctrlTable::msgLeftDown(const MouseCoords& mc)
×
369
{
NEW
370
    if(IsPointInRect(mc.pos, getContentDrawArea()))
×
371
    {
NEW
372
        setSelection(getSelectionFromMouse(mc));
×
NEW
373
        if(getParent())
×
NEW
374
            getParent()->msgTableLeftButton(this->getID(), selection_);
×
375

376
        return true;
×
377
    } else
NEW
378
        return relayMouseMessage(&Window::msgLeftDown, mc);
×
379
}
380

NEW
381
bool ctrlTable::msgRightDown(const MouseCoords& mc)
×
382
{
NEW
383
    if(IsPointInRect(mc.pos, getContentDrawArea()))
×
384
    {
NEW
385
        setSelection(getSelectionFromMouse(mc));
×
NEW
386
        if(getParent())
×
NEW
387
            getParent()->msgTableRightButton(this->getID(), selection_);
×
388

389
        return true;
×
390
    } else
NEW
391
        return relayMouseMessage(&Window::msgRightDown, mc);
×
392
}
393

NEW
394
std::optional<unsigned> ctrlTable::getSelectionFromMouse(const MouseCoords& mc) const
×
395
{
NEW
396
    const int visibleItem = (mc.pos.y - getContentDrawArea().top) / font->getHeight();
×
397
    if(visibleItem < 0)
×
398
        return std::nullopt;
×
NEW
399
    const unsigned itemIdx = static_cast<unsigned>(visibleItem) + getCtrl<ctrlScrollBar>(0)->getScrollPos();
×
NEW
400
    if(itemIdx >= getNumRows())
×
401
        return std::nullopt;
×
402
    return itemIdx;
×
403
}
404

NEW
405
bool ctrlTable::msgWheelUp(const MouseCoords& mc)
×
406
{
407
    // If mouse in list or scrollbar
NEW
408
    if(IsPointInRect(mc.pos, getFullDrawArea()))
×
409
    {
NEW
410
        auto* scrollbar = getCtrl<ctrlScrollBar>(0);
×
NEW
411
        scrollbar->scroll(-1);
×
UNCOV
412
        return true;
×
413
    } else
414
        return false;
×
415
}
416

NEW
417
bool ctrlTable::msgWheelDown(const MouseCoords& mc)
×
418
{
NEW
419
    if(IsPointInRect(mc.pos, getFullDrawArea()))
×
420
    {
NEW
421
        auto* scrollbar = getCtrl<ctrlScrollBar>(0);
×
NEW
422
        scrollbar->scroll(+1);
×
UNCOV
423
        return true;
×
424
    } else
425
        return false;
×
426
}
427

NEW
428
bool ctrlTable::msgLeftUp(const MouseCoords& mc)
×
429
{
NEW
430
    if(IsPointInRect(mc.pos, getContentDrawArea()))
×
431
    {
NEW
432
        if(mc.dbl_click && getParent())
×
433
        {
NEW
434
            const auto oldSelection = getSelection();
×
NEW
435
            setSelection(getSelectionFromMouse(mc));
×
UNCOV
436
            if(selection_ && oldSelection == selection_)
×
437
            {
NEW
438
                getParent()->msgTableChooseItem(this->getID(), *selection_);
×
439
                return true;
×
440
            }
441
        }
442
    }
NEW
443
    return relayMouseMessage(&Window::msgLeftUp, mc);
×
444
}
445

NEW
446
bool ctrlTable::msgMouseMove(const MouseCoords& mc)
×
447
{
448
    // ButtonMessages weiterleiten
NEW
449
    return relayMouseMessage(&Window::msgMouseMove, mc);
×
450
}
451

NEW
452
void ctrlTable::msgScrollShow(const unsigned /*ctrl_id*/, const bool /*visible*/)
×
453
{
NEW
454
    resetButtonWidths();
×
455
}
×
456

457
void ctrlTable::resetButtonWidths()
35 ✔
458
{
459
    auto addColumnWidth = [](unsigned cur, const Column& c) { return cur + c.width; };
72 ✔
460
    const unsigned sumWidth = std::max(1u, std::accumulate(columns_.begin(), columns_.end(), 0u, addColumnWidth));
35 ✔
461
    const auto* scrollbar = getCtrl<ctrlScrollBar>(0);
35 ✔
462
    const unsigned availableSize =
463
      std::max<int>(0, getSize().x - (scrollbar->isVisible() ? scrollbar->getSize().x : 0));
35 ✔
464
    const double sizeFactor = static_cast<double>(availableSize) / static_cast<double>(sumWidth);
35 ✔
465
    DrawPoint btPos(0, 0);
35 ✔
466
    for(unsigned i = 0; i < columns_.size(); ++i)
107 ✔
467
    {
468
        auto* button = getCtrl<ctrlButton>(i + 1);
72 ✔
469
        button->setWidth(helpers::iround<unsigned>(columns_[i].width * sizeFactor));
72 ✔
470
        button->setPos(btPos);
72 ✔
471
        btPos.x += button->getSize().x;
72 ✔
472
    }
473

474
    // Adjust last column size to cater for rounding error
475
    for(unsigned i = columns_.size(); i > 0; --i)
69 ✔
476
    {
477
        if(columns_[i - 1].width)
69 ✔
478
        {
479
            auto* bt = getCtrl<ctrlButton>(i);
35 ✔
480
            bt->setWidth(std::max(0, static_cast<int>(availableSize) - bt->getPos().x));
35 ✔
481
            break;
35 ✔
482
        }
483
    }
484
}
35 ✔
485

NEW
486
bool ctrlTable::msgKeyDown(const KeyEvent& ke)
×
487
{
488
    switch(ke.kt)
×
489
    {
490
        default: return false;
×
491
        case KeyType::Up:
×
492
            if(selection_.value_or(0u) > 0u)
×
NEW
493
                setSelection(*selection_ - 1);
×
494
            return true;
×
NEW
495
        case KeyType::Down: setSelection(selection_.value_or(0u) + 1u); return true;
×
496
    }
497
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc