The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
helper.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2008 - 2025
3 by Mark de Wever <koraq@xs4all.nl>
4 Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY.
12
13 See the COPYING file for more details.
14*/
15
16#define GETTEXT_DOMAIN "wesnoth-lib"
17
19
20#include "config.hpp"
21#include "gui/core/log.hpp"
22#include "gui/widgets/grid.hpp"
24
25namespace gui2
26{
27
28namespace implementation
29{
30
31static std::map<std::string, scrollbar_mode> scrollbar_mode_map {
32 { "always", scrollbar_mode::ALWAYS_VISIBLE },
33 { "never", scrollbar_mode::ALWAYS_INVISIBLE },
34 { "auto", scrollbar_mode::AUTO_VISIBLE },
35 { "initial_auto", scrollbar_mode::AUTO_VISIBLE_FIRST_RUN },
36};
37
38unsigned get_v_align(const std::string& v_align)
39{
40 if(v_align == "top") {
42 } else if(v_align == "bottom") {
44 } else {
45 if(!v_align.empty() && v_align != "center") {
46 ERR_GUI_E << "Invalid vertical alignment '" << v_align
47 << "' falling back to 'center'.";
48 }
50 }
51}
52
53unsigned get_h_align(const std::string& h_align)
54{
55 if(h_align == "left") {
57 } else if(h_align == "right") {
59 } else {
60 if(!h_align.empty() && h_align != "center") {
61 ERR_GUI_E << "Invalid horizontal alignment '" << h_align
62 << "' falling back to 'center'.";
63 }
65 }
66}
67
68unsigned get_border(const std::vector<std::string>& borders)
69{
70 unsigned result = 0;
71 for(const auto & border : borders)
72 {
73 if(border == "all") {
74 return grid::BORDER_ALL;
75 } else if(border == "top") {
76 result |= grid::BORDER_TOP;
77 } else if(border == "bottom") {
78 result |= grid::BORDER_BOTTOM;
79 } else if(border == "left") {
80 result |= grid::BORDER_LEFT;
81 } else if(border == "right") {
82 result |= grid::BORDER_RIGHT;
83 }
84 }
85 return result;
86}
87
88unsigned read_flags(const config& cfg)
89{
90 unsigned flags = 0;
91
92 const unsigned v_flags = get_v_align(cfg["vertical_alignment"]);
93 const unsigned h_flags = get_h_align(cfg["horizontal_alignment"]);
94 flags |= get_border(utils::split(cfg["border"]));
95
96 if(cfg["vertical_grow"].to_bool()) {
98
99 if(!(cfg["vertical_alignment"]).empty()) {
100 ERR_GUI_P << "vertical_grow and vertical_alignment "
101 "can't be combined, alignment is ignored.";
102 }
103 } else {
104 flags |= v_flags;
105 }
106
107 if(cfg["horizontal_grow"].to_bool()) {
109
110 if(!(cfg["horizontal_alignment"]).empty()) {
111 ERR_GUI_P << "horizontal_grow and horizontal_alignment "
112 "can't be combined, alignment is ignored.";
113 }
114 } else {
115 flags |= h_flags;
116 }
117
118 return flags;
119}
120
122{
123 if(scrollbar_mode.empty()) {
125 }
126
128 ERR_GUI_E << "Invalid scrollbar mode '" << scrollbar_mode << "'."
129 << "Falling back to 'initial_auto'.";
130
132 }
133
135}
136
137int get_retval(const std::string& retval_id,
138 const int retval,
139 const std::string& id)
140{
141 if(!retval_id.empty()) {
142 int result = window::get_retval_by_id(retval_id);
143 if(result) {
144 return result;
145 } else {
146 ERR_GUI_E << "Window builder: retval_id '" << retval_id
147 << "' is unknown.";
148 }
149 }
150
151 if(retval) {
152 return retval;
153 } else {
154 return window::get_retval_by_id(id);
155 }
156}
157
158} // namespace implementation
159
160} // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition config.hpp:157
static const unsigned HORIZONTAL_GROW_SEND_TO_CLIENT
Definition grid.hpp:56
static const unsigned HORIZONTAL_ALIGN_RIGHT
Definition grid.hpp:59
static const unsigned VERTICAL_ALIGN_BOTTOM
Definition grid.hpp:52
static const unsigned BORDER_TOP
Definition grid.hpp:62
static const unsigned VERTICAL_ALIGN_CENTER
Definition grid.hpp:51
static const unsigned VERTICAL_GROW_SEND_TO_CLIENT
Definition grid.hpp:49
static const unsigned BORDER_BOTTOM
Definition grid.hpp:63
static const unsigned BORDER_RIGHT
Definition grid.hpp:65
static const unsigned HORIZONTAL_ALIGN_CENTER
Definition grid.hpp:58
static const unsigned VERTICAL_ALIGN_TOP
Definition grid.hpp:50
static const unsigned BORDER_LEFT
Definition grid.hpp:64
static const unsigned BORDER_ALL
Definition grid.hpp:66
static const unsigned HORIZONTAL_ALIGN_LEFT
Definition grid.hpp:57
scrollbar_mode
The way to handle the showing or hiding of the scrollbar.
@ AUTO_VISIBLE_FIRST_RUN
Like AUTO_VISIBLE, but when not needed upon the initial layout phase, the bars are not shown and no s...
static retval get_retval_by_id(const std::string &id)
Gets the retval for the default buttons.
Definition window.cpp:412
Definitions for the interface to Wesnoth Markup Language (WML).
@ border
The border of the map.
const config * cfg
Define the common log macros for the gui toolkit.
#define ERR_GUI_P
Definition log.hpp:69
#define ERR_GUI_E
Definition log.hpp:38
This file contains the window object, this object is a top level container which has the event manage...
unsigned get_v_align(const std::string &v_align)
Returns the vertical alignment.
Definition helper.cpp:38
int get_retval(const std::string &retval_id, const int retval, const std::string &id)
Returns the return value for a widget.
Definition helper.cpp:137
unsigned get_h_align(const std::string &h_align)
Returns the horizontal alignment.
Definition helper.cpp:53
unsigned get_border(const std::vector< std::string > &borders)
Returns the border flags.
Definition helper.cpp:68
unsigned read_flags(const config &cfg)
Returns the placement/resize flags.
Definition helper.cpp:88
scrollbar_container::scrollbar_mode scrollbar_mode
Definition helper.hpp:31
static std::map< std::string, scrollbar_mode > scrollbar_mode_map
Definition helper.cpp:31
scrollbar_mode get_scrollbar_mode(const std::string &scrollbar_mode)
Returns the scrollbar mode flags.
Definition helper.cpp:121
Generic file dialog.
retval
Default window/dialog return values.
Definition retval.hpp:30
Contains the implementation details for lexical_cast and shouldn't be used directly.
std::vector< std::string > split(const config_attribute_value &val)