Repository: godotengine/godot
File: scene/gui/scroll_container.cpp (C++)

Code before:
	Size2 vmin = v_scroll->get_combined_minimum_size();

	h_scroll->set_anchor_and_offset(SIDE_LEFT, ANCHOR_BEGIN, 0);
	h_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
	h_scroll->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, -hmin.height);
	h_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);

	v_scroll->set_anchor_and_offset(SIDE_LEFT, ANCHOR_END, -vmin.width);
	v_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
	v_scroll->set_anchor_and_offset(SIDE_TOP, ANCHOR_BEGIN, 0);
	v_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);

	h_scroll->raise();
	v_scroll->raise();

	_updating_scrollbars = false;
}

void ScrollContainer::_gui_focus_changed(Control *p_control) {
	if (follow_focus && is_a_parent_of(p_control)) {
		ensure_control_visible(p_control);
	}
}

void ScrollContainer::ensure_control_visible(Control *p_control) {
	ERR_FAIL_COND_MSG(!is_a_parent_of(p_control), "Must be a parent of the control.");

	Rect2 global_rect = get_global_rect();
	Rect2 other_rect = p_control->get_global_rect();
	float right_margin = v_scroll->is_visible() ? v_scroll->get_size().x : 0.0f;
	float bottom_margin = h_scroll->is_visible() ? h_scroll->get_size().y : 0.0f;

	Vector2 diff = Vector2(MAX(MIN(other_rect.position.x, global_rect.position.x), other_rect.position.x + other_rect.size.x - global_rect.size.x + (!is_layout_rtl() ? right_margin : 0.0f)),
			MAX(MIN(other_rect.position.y, global_rect.position.y), other_rect.position.y + other_rect.size.y - global_rect.size.y + bottom_margin));

	set_h_scroll(get_h_scroll() + (diff.x - global_rect.position.x));
	set_v_scroll(get_v_scroll() + (diff.y - global_rect.position.y));
}

void ScrollContainer::_update_dimensions() {
	child_max_size = Size2(0, 0);
	Size2 size = get_size();
	Point2 ofs;

	Ref<StyleBox> sb = get_theme_stylebox("bg");
	size -= sb->get_minimum_size();
	ofs += sb->get_offset();
	bool rtl = is_layout_rtl();

	if (h_scroll->is_visible_in_tree() && h_scroll->get_parent() == this) { //scrolls may have been moved out for reasons
		size.y -= h_scroll->get_minimum_size().y;

Review comment (bug):
```suggestion
	ERR_FAIL_COND_MSG(!is_ancestor_of(p_control), "Must be an ancestor of the control.");
```

Maybe the text message should be changed to "ancestor" too?

Code after:
	Size2 vmin = v_scroll->get_combined_minimum_size();

	h_scroll->set_anchor_and_offset(SIDE_LEFT, ANCHOR_BEGIN, 0);
	h_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
	h_scroll->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, -hmin.height);
	h_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);

	v_scroll->set_anchor_and_offset(SIDE_LEFT, ANCHOR_END, -vmin.width);
	v_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
	v_scroll->set_anchor_and_offset(SIDE_TOP, ANCHOR_BEGIN, 0);
	v_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);

	h_scroll->raise();
	v_scroll->raise();

	_updating_scrollbars = false;
}

void ScrollContainer::_gui_focus_changed(Control *p_control) {
	if (follow_focus && is_ancestor_of(p_control)) {
		ensure_control_visible(p_control);
	}
}

void ScrollContainer::ensure_control_visible(Control *p_control) {
	ERR_FAIL_COND_MSG(!is_ancestor_of(p_control), "Must be an ancestor of the control.");

	Rect2 global_rect = get_global_rect();
	Rect2 other_rect = p_control->get_global_rect();
	float right_margin = v_scroll->is_visible() ? v_scroll->get_size().x : 0.0f;
	float bottom_margin = h_scroll->is_visible() ? h_scroll->get_size().y : 0.0f;

	Vector2 diff = Vector2(MAX(MIN(other_rect.position.x, global_rect.position.x), other_rect.position.x + other_rect.size.x - global_rect.size.x + (!is_layout_rtl() ? right_margin : 0.0f)),
			MAX(MIN(other_rect.position.y, global_rect.position.y), other_rect.position.y + other_rect.size.y - global_rect.size.y + bottom_margin));

	set_h_scroll(get_h_scroll() + (diff.x - global_rect.position.x));
	set_v_scroll(get_v_scroll() + (diff.y - global_rect.position.y));
}

void ScrollContainer::_update_dimensions() {
	child_max_size = Size2(0, 0);
	Size2 size = get_size();
	Point2 ofs;

	Ref<StyleBox> sb = get_theme_stylebox("bg");
	size -= sb->get_minimum_size();
	ofs += sb->get_offset();
	bool rtl = is_layout_rtl();

	if (h_scroll->is_visible_in_tree() && h_scroll->get_parent() == this) { //scrolls may have been moved out for reasons
		size.y -= h_scroll->get_minimum_size().y;