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 Jul 14, 2018
1 parent 996551e commit e62ec5f
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 @@ -201,7 +201,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 @@ -396,7 +396,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 @@ -56,7 +56,8 @@ void help_browser::pre_show(window& window)
connect_signal_mouse_left_click(next_button,
std::bind(&help_browser::on_history_navigate, this, std::ref(window), 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 @@ -40,7 +40,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 @@ -97,11 +97,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 @@ -138,8 +133,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 @@ -577,7 +577,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 @@ -601,9 +601,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 @@ -792,9 +790,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 @@ -746,7 +746,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));
} 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 e62ec5f

Please sign in to comment.