The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
horizontal_list.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2017 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
18 
19 #include "gui/core/point.hpp"
20 
21 #include <cassert>
22 #include <numeric>
23 
24 namespace gui2
25 {
26 
27 namespace implementation
28 {
29 
31  : maximum_rows_(maximum_rows)
32  , rows_(maximum_rows, 0)
33  , columns_(1, std::make_pair(0, 0))
34  , row_(0)
35  , column_(0)
36 {
37  assert(maximum_rows_ > 0);
38 }
39 
41 {
42  std::fill(rows_.begin(), rows_.end(), 0);
43  columns_.clear();
44  columns_.emplace_back(0, 0);
45  row_ = 0;
46  column_ = 0;
47 }
48 
50 {
51  if(size.x > columns_[column_].second) {
52  columns_[column_].second = size.x;
53  }
54 
55  if(size.y > rows_[row_]) {
56  rows_[row_] = size.y;
57  }
58 
59  ++row_;
60  if(row_ == maximum_rows_) {
61  row_ = 0;
62  ++column_;
63 
64  const int origin = columns_.back().first + columns_.back().second;
65  columns_.emplace_back(origin, 0);
66  }
67 }
68 
70 {
71  const int width = columns_.back().first + columns_.back().second;
72  const int height = std::accumulate(rows_.begin(), rows_.end(), 0);
73  return point(width, height);
74 }
75 
77 {
78  const unsigned row = index % maximum_rows_;
79  const unsigned column = index / maximum_rows_;
80 
81  const int height
82  = row == 0 ? 0
83  : std::accumulate(rows_.begin(), rows_.begin() + row, 0);
84 
85  return point(columns_[column].first, height);
86 }
87 
88 } // namespace implementation
89 
90 } // namespace gui2
size_t index(const utf8::string &str, const size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:71
Placement helper for the horizontal list.
unsigned column_
The column to add an item to.
virtual void initialize()
Initialises the placer.
virtual point get_size() const
Gets the required size of all items.
unsigned maximum_rows_
The maximum number of rows to use.
int x
x coordinate.
Definition: point.hpp:34
placer_horizontal_list(const unsigned maximum_rows)
Holds a 2D point.
Definition: point.hpp:23
int y
y coordinate.
Definition: point.hpp:37
STL namespace.
virtual void add_item(const point &size)
Adds a item to be placed.
Generic file dialog.
Definition: text.hpp:37
std::vector< std::pair< int, int > > columns_
Holds the column sizes.
virtual point get_origin(const unsigned index) const
Gets the origin for an item.
size_t size(const utf8::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
std::vector< int > rows_
Holds the heights of the rows.
unsigned row_
The row to add an item to.
Contains the implementation details for lexical_cast and shouldn't be used directly.