The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
vertical_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 
30 placer_vertical_list::placer_vertical_list(const unsigned maximum_columns)
31  : maximum_columns_(maximum_columns)
32  , rows_(1, std::make_pair(0, 0))
33  , columns_(maximum_columns, 0)
34  , row_(0)
35  , column_(0)
36 {
37  assert(maximum_columns_ > 0);
38 }
39 
41 {
42  rows_.clear();
43  rows_.emplace_back(0, 0);
44  std::fill(columns_.begin(), columns_.end(), 0);
45  row_ = 0;
46  column_ = 0;
47 }
48 
50 {
51  if(size.x > columns_[column_]) {
52  columns_[column_] = size.x;
53  }
54 
55  if(size.y > rows_[row_].second) {
56  rows_[row_].second = size.y;
57  }
58 
59  ++column_;
60  if(column_ == maximum_columns_) {
61  column_ = 0;
62  ++row_;
63 
64  const int origin = rows_.back().first + rows_.back().second;
65  rows_.emplace_back(origin, 0);
66  }
67 }
68 
70 {
71  const int width = std::accumulate(columns_.begin(), columns_.end(), 0);
72  const int height = rows_.back().first + rows_.back().second;
73  return point(width, height);
74 }
75 
77 {
78  const unsigned row = index / maximum_columns_;
79  const unsigned column = index % maximum_columns_;
80 
81  const int width = column == 0 ? 0
82  : std::accumulate(columns_.begin(),
83  columns_.begin() + column,
84  0);
85 
86  return point(width, rows_[row].first);
87 }
88 
89 } // namespace implementation
90 
91 } // namespace gui2
unsigned column_
The column to add an item to.
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
int x
x coordinate.
Definition: point.hpp:34
Placement helper for the vertical list.
virtual void initialize()
Initialises the placer.
Holds a 2D point.
Definition: point.hpp:23
int y
y coordinate.
Definition: point.hpp:37
std::vector< int > columns_
Holds the widths of the columns.
STL namespace.
Generic file dialog.
Definition: text.hpp:37
size_t size(const utf8::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
std::vector< std::pair< int, int > > rows_
Holds the row sizes.
virtual point get_size() const
Gets the required size of all items.
virtual point get_origin(const unsigned index) const
Gets the origin for an item.
placer_vertical_list(const unsigned maximum_columns)
unsigned maximum_columns_
The maximum number of columns to use.
unsigned row_
The row to add an item to.
virtual void add_item(const point &size)
Adds a item to be placed.
Contains the implementation details for lexical_cast and shouldn't be used directly.