Move the find_widget code to a new header.

Part of cleaning up and polishing the existing widget code.
This commit is contained in:
Mark de Wever 2013-03-09 09:20:57 +00:00
parent 5ad5a523e0
commit 7dca188c77
39 changed files with 138 additions and 78 deletions

View file

@ -0,0 +1,101 @@
/* $Id$ */
/*
Copyright (C) 2007 - 2013 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.
*/
#ifndef GUI_AUXILIARY_FIND_WIDGET_TPP_INCLUDED
#define GUI_AUXILIARY_FIND_WIDGET_TPP_INCLUDED
#include "gui/widgets/widget.hpp"
namespace gui2 {
/**
* Returns the first parent of a widget with a certain type.
*
* @param widget The widget to get the parent from,
* @tparam T The class of the widget to return.
*
* @returns The parent widget.
*/
template<class T> T* get_parent(twidget* widget)
{
T* result;
do {
widget = widget->parent();
result = dynamic_cast<T*>(widget);
} while (widget && !result);
assert(result);
return result;
}
/**
* Gets a widget with the wanted id.
*
* This template function doesn't return a pointer to a generic widget but
* returns the wanted type and tests for its existence if required.
*
* @param widget The widget test or find a child with the wanted
* id.
* @param id The id of the widget to find.
* @param must_be_active The widget should be active, not all widgets
* have an active flag, those who don't ignore
* flag.
* @param must_exist The widget should be exist, the function will
* fail if the widget doesn't exist or is
* inactive and must be active. Upon failure a
* wml_error is thrown.
*
* @returns The widget with the id.
*/
template<class T>
T* find_widget(typename utils::tconst_clone<twidget, T>::pointer widget
, const std::string& id
, const bool must_be_active
, const bool must_exist)
{
T* result =
dynamic_cast<T*>(widget->find(id, must_be_active));
VALIDATE(!must_exist || result, missing_widget(id));
return result;
}
/**
* Gets a widget with the wanted id.
*
* This template function doesn't return a reference to a generic widget but
* returns a reference to the wanted type
*
* @param widget The widget test or find a child with the wanted
* id.
* @param id The id of the widget to find.
* @param must_be_active The widget should be active, not all widgets
* have an active flag, those who don't ignore
* flag.
*
* @returns The widget with the id.
*/
template<class T>
T& find_widget(typename utils::tconst_clone<twidget, T>::pointer widget
, const std::string& id
, const bool must_be_active)
{
return *find_widget<T>(widget, id, must_be_active, true);
}
} // namespace gui2
#endif

View file

@ -16,6 +16,7 @@
#include "gui/dialogs/addon/uninstall_list.hpp"
#include "addon/info.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/widgets/grid.hpp"
#ifdef GUI2_EXPERIMENTAL_LISTBOX
#include "gui/widgets/list.hpp"

View file

@ -17,6 +17,7 @@
#include "gui/dialogs/addon_connect.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/widgets/button.hpp"
#include "gui/widgets/window.hpp"
#include "gui/widgets/settings.hpp"

View file

@ -19,6 +19,7 @@
#include "gettext.hpp"
#include "gui/auxiliary/filter.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/widgets/button.hpp"
#include "gui/widgets/label.hpp"
#ifdef GUI2_EXPERIMENTAL_LISTBOX

View file

@ -17,6 +17,7 @@
#include "gui/dialogs/campaign_difficulty.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/auxiliary/old_markup.hpp"
#ifdef GUI2_EXPERIMENTAL_LISTBOX
#include "gui/widgets/list.hpp"

View file

@ -17,6 +17,7 @@
#include "gui/dialogs/campaign_selection.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/dialogs/helper.hpp"
#include "gui/widgets/image.hpp"
#ifdef GUI2_EXPERIMENTAL_LISTBOX

View file

@ -17,6 +17,7 @@
#include "gui/dialogs/chat_log.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/dialogs/helper.hpp"
#include "gui/widgets/button.hpp"
#ifdef GUI2_EXPERIMENTAL_LISTBOX

View file

@ -17,6 +17,7 @@
#include "gui/dialogs/debug_clock.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/dialogs/dialog.hpp"
#include "gui/widgets/integer_selector.hpp"
#include "gui/widgets/window.hpp"

View file

@ -16,6 +16,7 @@
#include "gui/dialogs/editor_generate_map.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/dialogs/helper.hpp"
#include "gui/widgets/button.hpp"

View file

@ -21,6 +21,7 @@
#include "formatter.hpp"
#include "formula_string_utils.hpp"
#include "gettext.hpp"
#include "gui/auxiliary/find_widget.tpp"
#ifdef GUI2_EXPERIMENTAL_LISTBOX
#include "gui/widgets/list.hpp"
#else

View file

@ -24,6 +24,7 @@
#ifndef GUI_DIALOGS_FIELD_HPP_INCLUDED
#define GUI_DIALOGS_FIELD_HPP_INCLUDED
#include "gui/auxiliary/find_widget.tpp"
#include "gui/dialogs/field-fwd.hpp"
#include "gui/widgets/control.hpp"
#include "gui/widgets/selectable.hpp"

View file

@ -17,6 +17,7 @@
#include "gui/dialogs/formula_debugger.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/dialogs/helper.hpp"
#include "gui/widgets/button.hpp"
#include "gui/widgets/settings.hpp"

View file

@ -17,6 +17,7 @@
#include "gui/dialogs/gamestate_inspector.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/dialogs/helper.hpp"
#include "gui/widgets/button.hpp"
#ifdef GUI2_EXPERIMENTAL_LISTBOX

View file

@ -17,6 +17,7 @@
#include "gui/dialogs/language_selection.hpp"
#include "gui/auxiliary/find_widget.tpp"
#ifdef GUI2_EXPERIMENTAL_LISTBOX
#include "gui/widgets/list.hpp"
#else

View file

@ -13,6 +13,7 @@
See the COPYING file for more details.
*/
#include "gui/auxiliary/find_widget.tpp"
#include "gui/dialogs/helper.hpp"
#include "gui/dialogs/lobby_player_info.hpp"
#include "gui/widgets/button.hpp"

View file

@ -18,6 +18,7 @@
#include "gui/dialogs/message.hpp"
#include "gettext.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/widgets/button.hpp"
#include "gui/widgets/image.hpp"
#include "gui/widgets/label.hpp"

View file

@ -16,6 +16,7 @@
#include "gui/dialogs/mp_change_control.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/dialogs/helper.hpp"
#include "gui/widgets/button.hpp"
#ifdef GUI2_EXPERIMENTAL_LISTBOX

View file

@ -18,6 +18,7 @@
#include "gui/dialogs/mp_create_game_choose_mods.hpp"
#include "game_preferences.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/toggle_button.hpp"
#include "gui/widgets/window.hpp"

View file

@ -17,6 +17,7 @@
#include "gui/dialogs/mp_depcheck_select_new.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/window.hpp"
#include "gui/widgets/listbox.hpp"

View file

@ -18,6 +18,7 @@
#include "gui/dialogs/mp_login.hpp"
#include "game_preferences.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/widgets/button.hpp"
#include "gui/widgets/password_box.hpp"
#include "gui/widgets/settings.hpp"

View file

@ -18,6 +18,7 @@
#include "gui/dialogs/mp_method_selection.hpp"
#include "game_preferences.hpp"
#include "gui/auxiliary/find_widget.tpp"
#ifdef GUI2_EXPERIMENTAL_LISTBOX
#include "gui/widgets/list.hpp"
#else

View file

@ -19,6 +19,7 @@
#include "formula_string_utils.hpp"
#include "gettext.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/widgets/button.hpp"
#include "gui/widgets/progress_bar.hpp"
#include "gui/widgets/label.hpp"

View file

@ -17,6 +17,7 @@
#include "gui/dialogs/simple_item_selector.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/widgets/button.hpp"
#include "gui/widgets/label.hpp"
#ifdef GUI2_EXPERIMENTAL_LISTBOX

View file

@ -18,6 +18,7 @@
#include "gui/dialogs/tip.hpp"
#include "gui/dialogs/dialog.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/dialogs/popup.hpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/window.hpp"

View file

@ -22,6 +22,7 @@
#include "game_preferences.hpp"
#include "gettext.hpp"
#include "log.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/auxiliary/timer.hpp"
#include "gui/auxiliary/tips.hpp"
#include "gui/dialogs/debug_clock.hpp"

View file

@ -17,6 +17,7 @@
#include "gui/dialogs/unit_attack.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/widgets/image.hpp"
#ifdef GUI2_EXPERIMENTAL_LISTBOX
#include "gui/widgets/list.hpp"

View file

@ -17,6 +17,7 @@
#include "gui/dialogs/unit_create.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/auxiliary/log.hpp"
#include "gui/dialogs/helper.hpp"
#ifdef GUI2_EXPERIMENTAL_LISTBOX

View file

@ -17,6 +17,7 @@
#include "gui/dialogs/wml_message.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/auxiliary/old_markup.hpp"
#include "gui/widgets/button.hpp"
#include "gui/widgets/label.hpp"

View file

@ -19,6 +19,7 @@
#include "gui/widgets/listbox.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/auxiliary/layout_exception.hpp"
#include "gui/auxiliary/log.hpp"
#include "gui/auxiliary/widget_definition/listbox.hpp"

View file

@ -17,6 +17,7 @@
#include "gui/widgets/matrix.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/auxiliary/log.hpp"
#include "gui/auxiliary/widget_definition/matrix.hpp"
#include "gui/auxiliary/window_builder/matrix.hpp"

View file

@ -17,6 +17,7 @@
#include "gui/widgets/multi_page.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/auxiliary/widget_definition/multi_page.hpp"
#include "gui/auxiliary/window_builder/multi_page.hpp"
#include "gui/widgets/detail/register.tpp"

View file

@ -17,6 +17,7 @@
#include "gui/widgets/pane.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/auxiliary/log.hpp"
#include "gui/widgets/grid.hpp"
#include "gui/widgets/window.hpp"

View file

@ -18,6 +18,7 @@
#include "gui/widgets/scroll_label.hpp"
#include "gui/widgets/label.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/auxiliary/log.hpp"
#include "gui/auxiliary/widget_definition/scroll_label.hpp"
#include "gui/auxiliary/window_builder/scroll_label.hpp"

View file

@ -17,6 +17,7 @@
#include "gui/widgets/scrollbar_container_private.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/auxiliary/log.hpp"
#include "gui/auxiliary/layout_exception.hpp"
#include "gui/widgets/clickable.hpp"

View file

@ -17,6 +17,7 @@
#include "gui/widgets/stacked_widget.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/auxiliary/widget_definition/stacked_widget.hpp"
#include "gui/auxiliary/window_builder/stacked_widget.hpp"
#include "gui/widgets/detail/register.tpp"

View file

@ -18,6 +18,7 @@
#include "gui/widgets/tree_view_node.hpp"
#include "gettext.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/auxiliary/log.hpp"
#include "gui/widgets/toggle_button.hpp"
#include "gui/widgets/toggle_panel.hpp"

View file

@ -739,84 +739,6 @@ private:
bool is_at(const tpoint& coordinate, const bool must_be_active) const;
};
/**
* Returns the first parent of a widget with a certain type.
*
* @param widget The widget to get the parent from,
* @tparam T The class of the widget to return.
*
* @returns The parent widget.
*/
template<class T> T* get_parent(twidget* widget)
{
T* result;
do {
widget = widget->parent();
result = dynamic_cast<T*>(widget);
} while (widget && !result);
assert(result);
return result;
}
/**
* Gets a widget with the wanted id.
*
* This template function doesn't return a pointer to a generic widget but
* returns the wanted type and tests for its existence if required.
*
* @param widget The widget test or find a child with the wanted
* id.
* @param id The id of the widget to find.
* @param must_be_active The widget should be active, not all widgets
* have an active flag, those who don't ignore
* flag.
* @param must_exist The widget should be exist, the function will
* fail if the widget doesn't exist or is
* inactive and must be active. Upon failure a
* wml_error is thrown.
*
* @returns The widget with the id.
*/
template<class T>
T* find_widget(typename utils::tconst_clone<twidget, T>::pointer widget
, const std::string& id
, const bool must_be_active
, const bool must_exist)
{
T* result =
dynamic_cast<T*>(widget->find(id, must_be_active));
VALIDATE(!must_exist || result, missing_widget(id));
return result;
}
/**
* Gets a widget with the wanted id.
*
* This template function doesn't return a reference to a generic widget but
* returns a reference to the wanted type
*
* @param widget The widget test or find a child with the wanted
* id.
* @param id The id of the widget to find.
* @param must_be_active The widget should be active, not all widgets
* have an active flag, those who don't ignore
* flag.
*
* @returns The widget with the id.
*/
template<class T>
T& find_widget(typename utils::tconst_clone<twidget, T>::pointer widget
, const std::string& id
, const bool must_be_active)
{
return *find_widget<T>(widget, id, must_be_active, true);
}
} // namespace gui2
#endif

View file

@ -28,6 +28,7 @@
#include "log.hpp"
#include "gui/auxiliary/event/distributor.hpp"
#include "gui/auxiliary/event/message.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/auxiliary/log.hpp"
#include "gui/auxiliary/layout_exception.hpp"
#include "gui/auxiliary/window_builder/control.hpp"

View file

@ -15,6 +15,7 @@
#include "mp_options.hpp"
#include "gettext.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/auxiliary/window_builder.hpp"
#include "gui/dialogs/transient_message.hpp"
#include "gui/widgets/button.hpp"