Skip to content

Commit

Permalink
GUI2/Tree View: use NOTIFY_MODIFIED events instead of manual callbacks
Browse files Browse the repository at this point in the history
Unlike other widgets where `this` is the event target, the tree nodes fire the event
with the tree widget itself as the target.

I had added a fire-event call in f97dc8a, but with
the target as the node. I don't think there was any way that would have worked...
changed.
  • Loading branch information
Vultraz committed Oct 19, 2019
1 parent e8fb922 commit 2adac53
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/gui/dialogs/campaign_selection.cpp
Expand Up @@ -249,7 +249,8 @@ void campaign_selection::pre_show(window& window)
/***** Setup campaign tree. *****/
tree_view& tree = find_widget<tree_view>(&window, "campaign_tree", false);

tree.set_selection_change_callback(std::bind(&campaign_selection::campaign_selected, this, std::ref(window)));
connect_signal_notify_modified(tree,
std::bind(&campaign_selection::campaign_selected, this, std::ref(window)));

toggle_button& sort_name = find_widget<toggle_button>(&window, "sort_name", false);
toggle_button& sort_time = find_widget<toggle_button>(&window, "sort_time", false);
Expand Down
3 changes: 2 additions & 1 deletion src/gui/dialogs/gamestate_inspector.cpp
Expand Up @@ -398,7 +398,8 @@ class gamestate_inspector::controller
auto left_button = find_widget<button>(&window, "page_left", false, true);
auto right_button = find_widget<button>(&window, "page_right", false, true);

stuff_list->set_selection_change_callback(std::bind(&gamestate_inspector::controller::handle_stuff_list_item_clicked, this, _1));
connect_signal_notify_modified(*stuff_list,
std::bind(&gamestate_inspector::controller::handle_stuff_list_item_clicked, this, _1));

connect_signal_mouse_left_click(
*copy_button,
Expand Down
3 changes: 2 additions & 1 deletion src/gui/dialogs/help_browser.cpp
Expand Up @@ -55,7 +55,8 @@ void help_browser::pre_show(window& window)
tree_view& topic_tree = find_widget<tree_view>(&window, "topic_tree", false);
multi_page& topic_pages = find_widget<multi_page>(&window, "topic_text_pages", false);

topic_tree.set_selection_change_callback(std::bind(&help_browser::on_topic_select, this, std::ref(window)));
connect_signal_notify_modified(topic_tree,
std::bind(&help_browser::on_topic_select, this, std::ref(window)));

window.keyboard_capture(&topic_tree);

Expand Down
1 change: 0 additions & 1 deletion src/gui/widgets/tree_view.cpp
Expand Up @@ -41,7 +41,6 @@ tree_view::tree_view(const implementation::builder_tree_view& builder)
, need_layout_(false)
, root_node_(new tree_view_node("root", nullptr, *this, std::map<std::string, string_map>()))
, selected_item_(nullptr)
, selection_change_callback_()
{
connect_signal<event::LEFT_BUTTON_DOWN>(
std::bind(&tree_view::signal_handler_left_button_down, this, _2), event::dispatcher::back_pre_child);
Expand Down
7 changes: 0 additions & 7 deletions src/gui/widgets/tree_view.hpp
Expand Up @@ -102,11 +102,6 @@ class tree_view : public scrollbar_container
return selected_item_;
}

void set_selection_change_callback(std::function<void(widget&)> callback)
{
selection_change_callback_ = callback;
}

const std::vector<node_definition>& get_node_definitions() const
{
return node_definitions_;
Expand Down Expand Up @@ -143,8 +138,6 @@ class tree_view : public scrollbar_container

tree_view_node* selected_item_;

std::function<void(widget&)> selection_change_callback_;

/**
* Resizes the content.
*
Expand Down
10 changes: 3 additions & 7 deletions src/gui/widgets/tree_view_node.cpp
Expand Up @@ -594,7 +594,7 @@ void tree_view_node::signal_handler_left_button_click(const event::ui_event even
unfolded_ = unfolded_new;
is_folded() ? fold_internal() : unfold_internal();

fire(event::NOTIFY_MODIFIED, *this, nullptr);
fire(event::NOTIFY_MODIFIED, get_tree_view(), nullptr);
}

void tree_view_node::signal_handler_label_left_button_click(const event::ui_event event, bool& handled, bool& halt)
Expand All @@ -618,9 +618,7 @@ void tree_view_node::signal_handler_label_left_button_click(const event::ui_even

get_tree_view().selected_item_ = this;

if(get_tree_view().selection_change_callback_) {
get_tree_view().selection_change_callback_(get_tree_view());
}
fire(event::NOTIFY_MODIFIED, get_tree_view(), nullptr);
}

void tree_view_node::init_grid(grid* g, const std::map<std::string /* widget id */, string_map>& data)
Expand Down Expand Up @@ -809,9 +807,7 @@ void tree_view_node::select_node(bool expand_parents)

get_tree_view().selected_item_ = this;

if(get_tree_view().selection_change_callback_) {
get_tree_view().selection_change_callback_(get_tree_view());
}
fire(event::NOTIFY_MODIFIED, get_tree_view(), nullptr);

label_->set_value_bool(true);
}
Expand Down
2 changes: 1 addition & 1 deletion src/scripting/lua_gui2.cpp
Expand Up @@ -768,7 +768,7 @@ int intf_set_dialog_callback(lua_State* L)
static dialog_callback_wrapper wrapper;
connect_signal_notify_modified(*l, std::bind(&dialog_callback_wrapper::forward, wrapper, w, _3, _4));
} else if(gui2::tree_view* tv = dynamic_cast<gui2::tree_view*>(w)) {
tv->set_selection_change_callback(&dialog_callback);
connect_signal_notify_modified(*tv, std::bind(dialog_callback, _1));
} else {
return luaL_argerror(L, lua_gettop(L), "unsupported widget");
}
Expand Down

0 comments on commit 2adac53

Please sign in to comment.