OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/gtk/gtk_custom_menu.h" |
| 6 |
| 7 #include "chrome/browser/gtk/gtk_custom_menu_item.h" |
| 8 |
| 9 G_DEFINE_TYPE(GtkCustomMenu, gtk_custom_menu, GTK_TYPE_MENU) |
| 10 |
| 11 // Stolen directly from gtkmenushell.c. I'd love to call the library version |
| 12 // instead, but it's static and isn't exported. :( |
| 13 static gint gtk_menu_shell_is_item(GtkMenuShell* menu_shell, |
| 14 GtkWidget* child) { |
| 15 GtkWidget *parent; |
| 16 |
| 17 g_return_val_if_fail(GTK_IS_MENU_SHELL(menu_shell), FALSE); |
| 18 g_return_val_if_fail(child != NULL, FALSE); |
| 19 |
| 20 parent = child->parent; |
| 21 while (GTK_IS_MENU_SHELL(parent)) { |
| 22 if (parent == reinterpret_cast<GtkWidget*>(menu_shell)) |
| 23 return TRUE; |
| 24 parent = GTK_MENU_SHELL(parent)->parent_menu_shell; |
| 25 } |
| 26 |
| 27 return FALSE; |
| 28 } |
| 29 |
| 30 // Stolen directly from gtkmenushell.c. I'd love to call the library version |
| 31 // instead, but it's static and isn't exported. :( |
| 32 static GtkWidget* gtk_menu_shell_get_item(GtkMenuShell* menu_shell, |
| 33 GdkEvent* event) { |
| 34 GtkWidget* menu_item = gtk_get_event_widget(event); |
| 35 |
| 36 while (menu_item && !GTK_IS_MENU_ITEM(menu_item)) |
| 37 menu_item = menu_item->parent; |
| 38 |
| 39 if (menu_item && gtk_menu_shell_is_item(menu_shell, menu_item)) |
| 40 return menu_item; |
| 41 else |
| 42 return NULL; |
| 43 } |
| 44 |
| 45 // When processing a button event, abort processing if the cursor isn't in a |
| 46 // clickable region. |
| 47 static gboolean gtk_custom_menu_button_press(GtkWidget* widget, |
| 48 GdkEventButton* event) { |
| 49 GtkWidget* menu_item = gtk_menu_shell_get_item( |
| 50 GTK_MENU_SHELL(widget), reinterpret_cast<GdkEvent*>(event)); |
| 51 if (GTK_IS_CUSTOM_MENU_ITEM(menu_item)) { |
| 52 if (!gtk_custom_menu_item_is_in_clickable_region( |
| 53 GTK_CUSTOM_MENU_ITEM(menu_item))) { |
| 54 return TRUE; |
| 55 } |
| 56 } |
| 57 |
| 58 return GTK_WIDGET_CLASS(gtk_custom_menu_parent_class)-> |
| 59 button_press_event(widget, event); |
| 60 } |
| 61 |
| 62 // When processing a button event, abort processing if the cursor isn't in a |
| 63 // clickable region. |
| 64 static gboolean gtk_custom_menu_button_release(GtkWidget* widget, |
| 65 GdkEventButton* event) { |
| 66 GtkWidget* menu_item = gtk_menu_shell_get_item( |
| 67 GTK_MENU_SHELL(widget), reinterpret_cast<GdkEvent*>(event)); |
| 68 if (GTK_IS_CUSTOM_MENU_ITEM(menu_item)) { |
| 69 if (!gtk_custom_menu_item_is_in_clickable_region( |
| 70 GTK_CUSTOM_MENU_ITEM(menu_item))) { |
| 71 // Stop processing this event. This isn't a clickable region. |
| 72 return TRUE; |
| 73 } |
| 74 } |
| 75 |
| 76 return GTK_WIDGET_CLASS(gtk_custom_menu_parent_class)-> |
| 77 button_release_event(widget, event); |
| 78 } |
| 79 |
| 80 // Manually forward button press events to the menu item (and then do what we'd |
| 81 // do normally). |
| 82 static gboolean gtk_custom_menu_motion_notify(GtkWidget* widget, |
| 83 GdkEventMotion* event) { |
| 84 GtkWidget* menu_item = gtk_menu_shell_get_item( |
| 85 GTK_MENU_SHELL(widget), (GdkEvent*)event); |
| 86 if (GTK_IS_CUSTOM_MENU_ITEM(menu_item)) { |
| 87 gtk_custom_menu_item_receive_motion_event(GTK_CUSTOM_MENU_ITEM(menu_item), |
| 88 event->x, event->y); |
| 89 } |
| 90 |
| 91 return GTK_WIDGET_CLASS(gtk_custom_menu_parent_class)-> |
| 92 motion_notify_event(widget, event); |
| 93 } |
| 94 |
| 95 static void gtk_custom_menu_move_current(GtkMenuShell* menu_shell, |
| 96 GtkMenuDirectionType direction) { |
| 97 // If the currently selected item is custom, we give it first chance to catch |
| 98 // up/down events. |
| 99 |
| 100 // TODO(erg): We are breaking a GSEAL by directly accessing this. We'll need |
| 101 // to fix this by the time gtk3 comes out. |
| 102 GtkWidget* menu_item = GTK_MENU_SHELL(menu_shell)->active_menu_item; |
| 103 if (GTK_IS_CUSTOM_MENU_ITEM(menu_item)) { |
| 104 switch (direction) { |
| 105 case GTK_MENU_DIR_PREV: |
| 106 case GTK_MENU_DIR_NEXT: |
| 107 if (gtk_custom_menu_item_handle_move(GTK_CUSTOM_MENU_ITEM(menu_item), |
| 108 direction)) |
| 109 return; |
| 110 break; |
| 111 default: |
| 112 break; |
| 113 } |
| 114 } |
| 115 |
| 116 GTK_MENU_SHELL_CLASS(gtk_custom_menu_parent_class)-> |
| 117 move_current(menu_shell, direction); |
| 118 |
| 119 // In the case of hitting PREV and transitioning to a custom menu, we want to |
| 120 // make sure we're selecting the final item in the list, not the first one. |
| 121 menu_item = GTK_MENU_SHELL(menu_shell)->active_menu_item; |
| 122 if (GTK_IS_CUSTOM_MENU_ITEM(menu_item)) { |
| 123 gtk_custom_menu_item_select_item_by_direction( |
| 124 GTK_CUSTOM_MENU_ITEM(menu_item), direction); |
| 125 } |
| 126 } |
| 127 |
| 128 static void gtk_custom_menu_init(GtkCustomMenu* menu) { |
| 129 } |
| 130 |
| 131 static void gtk_custom_menu_class_init(GtkCustomMenuClass* klass) { |
| 132 GtkWidgetClass* widget_class = GTK_WIDGET_CLASS(klass); |
| 133 GtkMenuShellClass* menu_shell_class = GTK_MENU_SHELL_CLASS(klass); |
| 134 |
| 135 widget_class->button_press_event = gtk_custom_menu_button_press; |
| 136 widget_class->button_release_event = gtk_custom_menu_button_release; |
| 137 widget_class->motion_notify_event = gtk_custom_menu_motion_notify; |
| 138 |
| 139 menu_shell_class->move_current = gtk_custom_menu_move_current; |
| 140 } |
| 141 |
| 142 GtkWidget* gtk_custom_menu_new() { |
| 143 return GTK_WIDGET(g_object_new(GTK_TYPE_CUSTOM_MENU, NULL)); |
| 144 } |
OLD | NEW |