GUI2: moved common swap_grid util function into new widget_helpers file

This commit is contained in:
Charles Dang 2017-05-22 14:50:16 +11:00
parent 8a1f292222
commit 246ea98dd9
8 changed files with 87 additions and 105 deletions

View file

@ -776,6 +776,8 @@
<Unit filename="../../src/gui/widgets/viewport.hpp" />
<Unit filename="../../src/gui/widgets/widget.cpp" />
<Unit filename="../../src/gui/widgets/widget.hpp" />
<Unit filename="../../src/gui/widgets/widget_helpers.cpp" />
<Unit filename="../../src/gui/widgets/widget_helpers.hpp" />
<Unit filename="../../src/gui/widgets/window.cpp" />
<Unit filename="../../src/gui/widgets/window.hpp" />
<Unit filename="../../src/gui/widgets/window_private.hpp" />

View file

@ -288,6 +288,7 @@ gui/widgets/unit_preview_pane.cpp
gui/widgets/vertical_scrollbar.cpp
gui/widgets/viewport.cpp
gui/widgets/widget.cpp
gui/widgets/widget_helpers.cpp
gui/widgets/window.cpp
halo.cpp
help/help.cpp

View file

@ -28,6 +28,7 @@
#include "gui/widgets/pane.hpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/selectable_item.hpp"
#include "gui/widgets/widget_helpers.hpp"
#include "gui/widgets/toggle_button.hpp"
#include "gui/widgets/viewport.hpp"
#include "gui/widgets/window.hpp"
@ -548,40 +549,6 @@ void listbox::handle_key_right_arrow(SDL_Keymod modifier, bool& handled)
}
}
namespace
{
/**
* Swaps an item in a grid for another one.*/
void swap_grid(grid* g,
grid* content_grid,
widget* wgt,
const std::string& id)
{
assert(content_grid);
assert(wgt);
// Make sure the new child has same id.
wgt->set_id(id);
// Get the container containing the wanted widget.
grid* parent_grid = nullptr;
if(g) {
parent_grid = find_widget<grid>(g, id, false, false);
}
if(!parent_grid) {
parent_grid = find_widget<grid>(content_grid, id, true, false);
}
parent_grid = dynamic_cast<grid*>(parent_grid->parent());
assert(parent_grid);
// Replace the child.
auto old = parent_grid->swap_child(id, wgt, false);
assert(old);
}
} // namespace
void listbox::finalize(builder_grid_const_ptr header,
builder_grid_const_ptr footer,
const std::vector<std::map<std::string, string_map>>& list_data)

View file

@ -19,6 +19,7 @@
#include "gui/auxiliary/find_widget.hpp"
#include "gui/core/register_widget.hpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/widget_helpers.hpp"
#include "gui/widgets/generator.hpp"
#include "gettext.hpp"
@ -136,40 +137,6 @@ unsigned multi_page::get_state() const
return 0;
}
namespace
{
/**
* Swaps an item in a grid for another one.*/
void swap_grid(grid* g,
grid* content_grid,
widget* widget,
const std::string& id)
{
assert(content_grid);
assert(widget);
// Make sure the new child has same id.
widget->set_id(id);
// Get the container containing the wanted widget.
grid* parent_grid = nullptr;
if(g) {
parent_grid = find_widget<grid>(g, id, false, false);
}
if(!parent_grid) {
parent_grid = find_widget<grid>(content_grid, id, true, false);
}
parent_grid = dynamic_cast<grid*>(parent_grid->parent());
assert(parent_grid);
// Replace the child.
auto old = parent_grid->swap_child(id, widget, false);
assert(old);
}
} // namespace
void multi_page::finalize(const std::vector<string_map>& page_data)
{
assert(generator_);

View file

@ -19,6 +19,7 @@
#include "gui/auxiliary/find_widget.hpp"
#include "gui/core/register_widget.hpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/widget_helpers.hpp"
#include "gui/widgets/generator.hpp"
#include "gettext.hpp"
#include "utils/general.hpp"
@ -57,40 +58,6 @@ void stacked_widget::layout_children()
}
}
namespace
{
/**
* Swaps an item in a grid for another one.*/
void swap_grid(grid* g,
grid* content_grid,
widget* widget,
const std::string& id)
{
assert(content_grid);
assert(widget);
// Make sure the new child has same id.
widget->set_id(id);
// Get the container containing the wanted widget.
grid* parent_grid = nullptr;
if(g) {
parent_grid = find_widget<grid>(g, id, false, false);
}
if(!parent_grid) {
parent_grid = find_widget<grid>(content_grid, id, true, false);
}
parent_grid = dynamic_cast<grid*>(parent_grid->parent());
assert(parent_grid);
// Replace the child.
auto old = parent_grid->swap_child(id, widget, false);
assert(old);
}
} // namespace
void
stacked_widget::finalize(std::vector<builder_grid_const_ptr> widget_builder)
{

View file

@ -0,0 +1,48 @@
/*
Copyright (C) 2008 - 2017 by Mark de Wever <koraq@xs4all.nl>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "gui/auxiliary/find_widget.hpp"
#include "gui/widgets/grid.hpp"
#include "gui/widgets/widget.hpp"
#include <cassert>
namespace gui2
{
void swap_grid(grid* g, grid* content_grid, widget* widget, const std::string& id)
{
assert(content_grid);
assert(widget);
// Make sure the new child has same id.
widget->set_id(id);
// Get the container containing the wanted widget.
grid* parent_grid = nullptr;
if(g) {
parent_grid = find_widget<grid>(g, id, false, false);
}
if(!parent_grid) {
parent_grid = find_widget<grid>(content_grid, id, true, false);
}
parent_grid = dynamic_cast<grid*>(parent_grid->parent());
assert(parent_grid);
// Replace the child.
auto old = parent_grid->swap_child(id, widget, false);
assert(old);
}
}

View file

@ -0,0 +1,28 @@
/*
Copyright (C) 2008 - 2017 by Mark de Wever <koraq@xs4all.nl>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#pragma once
#include <string>
namespace gui2
{
class grid;
class widget;
/**
* Swaps an item in a grid for another one.
*/
void swap_grid(grid* g, grid* content_grid, widget* widget, const std::string& id);
}

View file

@ -1195,8 +1195,10 @@ namespace
{
/**
* Swaps an item in a grid for another one.*/
void swap_grid(grid* g,
* Swaps an item in a grid for another one.
* This differs slightly from the standard swap_grid utility, so it's defined by itself here.
*/
void window_swap_grid(grid* g,
grid* content_grid,
widget* widget,
const std::string& id)
@ -1231,7 +1233,7 @@ void swap_grid(grid* g,
void window::finalize(const std::shared_ptr<builder_grid>& content_grid)
{
swap_grid(nullptr, &get_grid(), content_grid->build(), "_window_content_grid");
window_swap_grid(nullptr, &get_grid(), content_grid->build(), "_window_content_grid");
}
#ifdef DEBUG_WINDOW_LAYOUT_GRAPHS