This source file includes following definitions.
- PopulateSubmenus
- MotionIsOverMenu
- delegate_
- Add
- Remove
- Clear
- MenuStartedShowing
- OnMenuMotionNotify
- OnMenuHiddenOrDestroyed
- OnMenuMoveCurrent
#include "chrome/browser/ui/gtk/menu_bar_helper.h"
#include <algorithm>
#include "base/logging.h"
#include "chrome/browser/ui/gtk/gtk_util.h"
#include "ui/base/gtk/gtk_signal_registrar.h"
namespace {
void PopulateSubmenus(GtkWidget* child, gpointer data) {
std::vector<GtkWidget*>* submenus =
static_cast<std::vector<GtkWidget*>*>(data);
GtkWidget* submenu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(child));
if (submenu) {
submenus->push_back(submenu);
gtk_container_foreach(GTK_CONTAINER(submenu), PopulateSubmenus, submenus);
}
}
bool MotionIsOverMenu(GtkWidget* menu, GdkEventMotion* motion) {
GtkAllocation allocation;
gtk_widget_get_allocation(menu, &allocation);
if (motion->x >= 0 && motion->y >= 0 &&
motion->x < allocation.width &&
motion->y < allocation.height) {
return true;
}
while (menu) {
GtkWidget* menu_item = gtk_menu_get_attach_widget(GTK_MENU(menu));
if (!menu_item)
return false;
GtkWidget* parent = gtk_widget_get_parent(menu_item);
if (gtk_util::WidgetContainsCursor(parent))
return true;
menu = parent;
}
return false;
}
}
MenuBarHelper::MenuBarHelper(Delegate* delegate)
: button_showing_menu_(NULL),
showing_menu_(NULL),
delegate_(delegate) {
DCHECK(delegate_);
}
MenuBarHelper::~MenuBarHelper() {
}
void MenuBarHelper::Add(GtkWidget* button) {
buttons_.push_back(button);
}
void MenuBarHelper::Remove(GtkWidget* button) {
std::vector<GtkWidget*>::iterator iter =
find(buttons_.begin(), buttons_.end(), button);
if (iter == buttons_.end()) {
NOTREACHED();
return;
}
buttons_.erase(iter);
}
void MenuBarHelper::Clear() {
buttons_.clear();
}
void MenuBarHelper::MenuStartedShowing(GtkWidget* button, GtkWidget* menu) {
DCHECK(GTK_IS_MENU(menu));
button_showing_menu_ = button;
showing_menu_ = menu;
signal_handlers_.reset(new ui::GtkSignalRegistrar());
signal_handlers_->Connect(menu, "destroy",
G_CALLBACK(OnMenuHiddenOrDestroyedThunk), this);
signal_handlers_->Connect(menu, "hide",
G_CALLBACK(OnMenuHiddenOrDestroyedThunk), this);
signal_handlers_->Connect(menu, "motion-notify-event",
G_CALLBACK(OnMenuMotionNotifyThunk), this);
signal_handlers_->Connect(menu, "move-current",
G_CALLBACK(OnMenuMoveCurrentThunk), this);
gtk_container_foreach(GTK_CONTAINER(menu), PopulateSubmenus, &submenus_);
for (size_t i = 0; i < submenus_.size(); ++i) {
signal_handlers_->Connect(submenus_[i], "motion-notify-event",
G_CALLBACK(OnMenuMotionNotifyThunk), this);
}
}
gboolean MenuBarHelper::OnMenuMotionNotify(GtkWidget* menu,
GdkEventMotion* motion) {
if (MotionIsOverMenu(menu, motion))
return FALSE;
if (buttons_.empty())
return FALSE;
gint x = 0;
gint y = 0;
GtkWidget* last_button = NULL;
for (size_t i = 0; i < buttons_.size(); ++i) {
GtkWidget* button = buttons_[i];
if (i == 0) {
gtk_widget_get_pointer(buttons_[0], &x, &y);
} else {
gint last_x = x;
gint last_y = y;
if (!gtk_widget_translate_coordinates(
last_button, button, last_x, last_y, &x, &y)) {
continue;
}
}
last_button = button;
GtkAllocation allocation;
gtk_widget_get_allocation(button, &allocation);
if (x >= 0 && y >= 0 && x < allocation.width && y < allocation.height) {
if (button != button_showing_menu_)
delegate_->PopupForButton(button);
return TRUE;
}
}
return FALSE;
}
void MenuBarHelper::OnMenuHiddenOrDestroyed(GtkWidget* menu) {
DCHECK_EQ(showing_menu_, menu);
signal_handlers_.reset();
showing_menu_ = NULL;
button_showing_menu_ = NULL;
submenus_.clear();
}
void MenuBarHelper::OnMenuMoveCurrent(GtkWidget* menu,
GtkMenuDirectionType dir) {
switch (dir) {
case GTK_MENU_DIR_CHILD: {
GtkWidget* active_item = GTK_MENU_SHELL(menu)->active_menu_item;
if (active_item && gtk_menu_item_get_submenu(GTK_MENU_ITEM(active_item)))
return;
}
case GTK_MENU_DIR_PARENT: {
delegate_->PopupForButtonNextTo(button_showing_menu_, dir);
break;
}
default:
return;
}
g_signal_stop_emission_by_name(menu, "move-current");
}