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_item.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "chrome/browser/gtk/gtk_custom_menu.h" |
| 9 |
| 10 enum { |
| 11 BUTTON_PUSHED, |
| 12 LAST_SIGNAL |
| 13 }; |
| 14 |
| 15 static guint custom_menu_item_signals[LAST_SIGNAL] = { 0 }; |
| 16 |
| 17 G_DEFINE_TYPE(GtkCustomMenuItem, gtk_custom_menu_item, GTK_TYPE_MENU_ITEM) |
| 18 |
| 19 static void set_selected(GtkCustomMenuItem* item, GtkWidget* selected) { |
| 20 if (selected != item->currently_selected_button) { |
| 21 if (item->currently_selected_button) |
| 22 gtk_widget_set_state(item->currently_selected_button, GTK_STATE_NORMAL); |
| 23 |
| 24 item->currently_selected_button = selected; |
| 25 if (item->currently_selected_button) |
| 26 gtk_widget_set_state(item->currently_selected_button, GTK_STATE_SELECTED); |
| 27 } |
| 28 } |
| 29 |
| 30 static void gtk_custom_menu_item_finalize(GObject *object); |
| 31 static gint gtk_custom_menu_item_expose(GtkWidget* widget, |
| 32 GdkEventExpose* event); |
| 33 static void gtk_custom_menu_item_select(GtkItem *item); |
| 34 static void gtk_custom_menu_item_deselect(GtkItem *item); |
| 35 static void gtk_custom_menu_item_activate(GtkMenuItem* menu_item); |
| 36 |
| 37 static void gtk_custom_menu_item_style_set(GtkCustomMenuItem* item, |
| 38 GtkStyle* old_style) { |
| 39 // Because several popular themes have no idea about styling buttons in menus |
| 40 // (it's sort of a weird concept) and look like crap, we manually apply the |
| 41 // menu item's prelight information to the button. |
| 42 GtkStyle* style = gtk_widget_get_style(GTK_WIDGET(item)); |
| 43 |
| 44 for (GList* i = item->button_widgets; i; i = g_list_next(i)) { |
| 45 // Set the button prelight colors. |
| 46 GtkWidget* button = GTK_WIDGET(i->data); |
| 47 gtk_widget_modify_fg(button, GTK_STATE_PRELIGHT, |
| 48 &style->fg[GTK_STATE_PRELIGHT]); |
| 49 gtk_widget_modify_bg(button, GTK_STATE_PRELIGHT, |
| 50 &style->bg[GTK_STATE_PRELIGHT]); |
| 51 gtk_widget_modify_text(button, GTK_STATE_PRELIGHT, |
| 52 &style->text[GTK_STATE_PRELIGHT]); |
| 53 gtk_widget_modify_base(button, GTK_STATE_PRELIGHT, |
| 54 &style->base[GTK_STATE_PRELIGHT]); |
| 55 } |
| 56 } |
| 57 |
| 58 static void gtk_custom_menu_item_init(GtkCustomMenuItem* item) { |
| 59 item->button_widgets = NULL; |
| 60 item->currently_selected_button = NULL; |
| 61 item->previously_selected_button = NULL; |
| 62 |
| 63 GtkWidget* menu_hbox = gtk_hbox_new(FALSE, 0); |
| 64 gtk_container_add(GTK_CONTAINER(item), menu_hbox); |
| 65 |
| 66 item->label = gtk_label_new(NULL); |
| 67 gtk_misc_set_alignment(GTK_MISC(item->label), 0.0, 0.5); |
| 68 gtk_box_pack_start(GTK_BOX(menu_hbox), item->label, TRUE, TRUE, 0); |
| 69 |
| 70 item->hbox = gtk_hbox_new(FALSE, 0); |
| 71 gtk_box_pack_end(GTK_BOX(menu_hbox), item->hbox, FALSE, FALSE, 0); |
| 72 |
| 73 g_signal_connect(item, "style-set", |
| 74 G_CALLBACK(gtk_custom_menu_item_style_set), NULL); |
| 75 |
| 76 gtk_widget_show_all(menu_hbox); |
| 77 } |
| 78 |
| 79 static void gtk_custom_menu_item_class_init(GtkCustomMenuItemClass* klass) { |
| 80 GObjectClass* gobject_class = G_OBJECT_CLASS(klass); |
| 81 GtkWidgetClass* widget_class = GTK_WIDGET_CLASS(klass); |
| 82 GtkItemClass* item_class = GTK_ITEM_CLASS(klass); |
| 83 GtkMenuItemClass* menu_item_class = GTK_MENU_ITEM_CLASS(klass); |
| 84 |
| 85 gobject_class->finalize = gtk_custom_menu_item_finalize; |
| 86 |
| 87 widget_class->expose_event = gtk_custom_menu_item_expose; |
| 88 |
| 89 item_class->select = gtk_custom_menu_item_select; |
| 90 item_class->deselect = gtk_custom_menu_item_deselect; |
| 91 |
| 92 menu_item_class->activate = gtk_custom_menu_item_activate; |
| 93 |
| 94 custom_menu_item_signals[BUTTON_PUSHED] = |
| 95 g_signal_new("button-pushed", |
| 96 G_OBJECT_CLASS_TYPE(gobject_class), |
| 97 G_SIGNAL_RUN_FIRST, |
| 98 0, |
| 99 NULL, NULL, |
| 100 gtk_marshal_NONE__INT, |
| 101 G_TYPE_NONE, 1, GTK_TYPE_INT); |
| 102 } |
| 103 |
| 104 static void gtk_custom_menu_item_finalize(GObject *object) { |
| 105 GtkCustomMenuItem* item = GTK_CUSTOM_MENU_ITEM(object); |
| 106 g_list_free(item->button_widgets); |
| 107 |
| 108 G_OBJECT_CLASS(gtk_custom_menu_item_parent_class)->finalize(object); |
| 109 } |
| 110 |
| 111 static gint gtk_custom_menu_item_expose(GtkWidget* widget, |
| 112 GdkEventExpose* event) { |
| 113 if (GTK_WIDGET_VISIBLE(widget) && |
| 114 GTK_WIDGET_MAPPED(widget) && |
| 115 gtk_bin_get_child(GTK_BIN(widget))) { |
| 116 // We skip the drawing in the GtkMenuItem class it draws the highlighted |
| 117 // background and we don't want that. |
| 118 gtk_container_propagate_expose(GTK_CONTAINER(widget), |
| 119 gtk_bin_get_child(GTK_BIN(widget)), |
| 120 event); |
| 121 } |
| 122 |
| 123 return FALSE; |
| 124 } |
| 125 |
| 126 static void gtk_custom_menu_item_select(GtkItem* item) { |
| 127 GtkCustomMenuItem* custom_item = GTK_CUSTOM_MENU_ITEM(item); |
| 128 |
| 129 // When we are selected, the only thing we do is clear information from |
| 130 // previous selections. Actual selection of a button is done either in the |
| 131 // "mouse-motion-event" or is manually set from GtkCustomMenu's overridden |
| 132 // "move-current" handler. |
| 133 custom_item->previously_selected_button = NULL; |
| 134 |
| 135 gtk_widget_queue_draw(GTK_WIDGET(item)); |
| 136 } |
| 137 |
| 138 static void gtk_custom_menu_item_deselect(GtkItem* item) { |
| 139 GtkCustomMenuItem* custom_item = GTK_CUSTOM_MENU_ITEM(item); |
| 140 |
| 141 // When we are deselected, we store the item that was currently selected so |
| 142 // that it can be acted on. Menu items are first deselected before they are |
| 143 // activated. |
| 144 custom_item->previously_selected_button = |
| 145 custom_item->currently_selected_button; |
| 146 if (custom_item->currently_selected_button) |
| 147 set_selected(custom_item, NULL); |
| 148 |
| 149 gtk_widget_queue_draw(GTK_WIDGET(item)); |
| 150 } |
| 151 |
| 152 static void gtk_custom_menu_item_activate(GtkMenuItem* menu_item) { |
| 153 GtkCustomMenuItem* custom_item = GTK_CUSTOM_MENU_ITEM(menu_item); |
| 154 |
| 155 // We look at |previously_selected_button| because by the time we've been |
| 156 // activated, we've already gone through our deselect handler. |
| 157 if (custom_item->previously_selected_button) { |
| 158 gpointer id_ptr = g_object_get_data( |
| 159 G_OBJECT(custom_item->previously_selected_button), "command-id"); |
| 160 if (id_ptr != NULL) { |
| 161 int command_id = GPOINTER_TO_INT(id_ptr); |
| 162 g_signal_emit(custom_item, custom_menu_item_signals[BUTTON_PUSHED], 0, |
| 163 command_id); |
| 164 set_selected(custom_item, NULL); |
| 165 } |
| 166 } |
| 167 } |
| 168 |
| 169 GtkWidget* gtk_custom_menu_item_new(const char* title) { |
| 170 GtkCustomMenuItem* item = GTK_CUSTOM_MENU_ITEM( |
| 171 g_object_new(GTK_TYPE_CUSTOM_MENU_ITEM, NULL)); |
| 172 |
| 173 char* markup = g_markup_printf_escaped("<b>%s</b>", title); |
| 174 gtk_label_set_markup(GTK_LABEL(item->label), markup); |
| 175 g_free(markup); |
| 176 |
| 177 return GTK_WIDGET(item); |
| 178 } |
| 179 |
| 180 GtkWidget* gtk_custom_menu_item_add_button(GtkCustomMenuItem* menu_item, |
| 181 int command_id) { |
| 182 GtkWidget* button = gtk_button_new(); |
| 183 g_object_set_data(G_OBJECT(button), "command-id", |
| 184 GINT_TO_POINTER(command_id)); |
| 185 gtk_box_pack_start(GTK_BOX(menu_item->hbox), button, FALSE, FALSE, 0); |
| 186 gtk_widget_show(button); |
| 187 |
| 188 menu_item->button_widgets = g_list_append(menu_item->button_widgets, button); |
| 189 return button; |
| 190 } |
| 191 |
| 192 void gtk_custom_menu_item_add_space(GtkCustomMenuItem* menu_item) { |
| 193 GtkWidget* fixed = gtk_fixed_new(); |
| 194 gtk_widget_set_size_request(fixed, 5, -1); |
| 195 |
| 196 gtk_box_pack_start(GTK_BOX(menu_item->hbox), fixed, FALSE, FALSE, 0); |
| 197 gtk_widget_show(fixed); |
| 198 } |
| 199 |
| 200 void gtk_custom_menu_item_receive_motion_event(GtkCustomMenuItem* menu_item, |
| 201 gdouble x, gdouble y) { |
| 202 GtkWidget* new_selected_widget = NULL; |
| 203 GList* current = menu_item->button_widgets; |
| 204 for (; current != NULL; current = current->next) { |
| 205 GtkWidget* current_widget = GTK_WIDGET(current->data); |
| 206 GtkAllocation alloc = current_widget->allocation; |
| 207 int offset_x, offset_y; |
| 208 gtk_widget_translate_coordinates(current_widget, GTK_WIDGET(menu_item), |
| 209 0, 0, &offset_x, &offset_y); |
| 210 if (x >= offset_x && x < (offset_x + alloc.width) && |
| 211 y >= offset_y && y < (offset_y + alloc.height)) { |
| 212 new_selected_widget = current_widget; |
| 213 break; |
| 214 } |
| 215 } |
| 216 |
| 217 set_selected(menu_item, new_selected_widget); |
| 218 } |
| 219 |
| 220 gboolean gtk_custom_menu_item_handle_move(GtkCustomMenuItem* menu_item, |
| 221 GtkMenuDirectionType direction) { |
| 222 GtkWidget* current = menu_item->currently_selected_button; |
| 223 if (menu_item->button_widgets && current) { |
| 224 switch (direction) { |
| 225 case GTK_MENU_DIR_PREV: { |
| 226 if (g_list_first(menu_item->button_widgets)->data == current) |
| 227 return FALSE; |
| 228 |
| 229 set_selected(menu_item, GTK_WIDGET(g_list_previous(g_list_find( |
| 230 menu_item->button_widgets, current))->data)); |
| 231 break; |
| 232 } |
| 233 case GTK_MENU_DIR_NEXT: { |
| 234 if (g_list_last(menu_item->button_widgets)->data == current) |
| 235 return FALSE; |
| 236 |
| 237 set_selected(menu_item, GTK_WIDGET(g_list_next(g_list_find( |
| 238 menu_item->button_widgets, current))->data)); |
| 239 break; |
| 240 } |
| 241 default: |
| 242 break; |
| 243 } |
| 244 } |
| 245 |
| 246 return TRUE; |
| 247 } |
| 248 |
| 249 void gtk_custom_menu_item_select_item_by_direction( |
| 250 GtkCustomMenuItem* menu_item, GtkMenuDirectionType direction) { |
| 251 menu_item->previously_selected_button = NULL; |
| 252 |
| 253 // If we're just told to be selected by the menu system, select the first |
| 254 // item. |
| 255 if (menu_item->button_widgets) { |
| 256 switch (direction) { |
| 257 case GTK_MENU_DIR_PREV: { |
| 258 GtkWidget* last_button = |
| 259 GTK_WIDGET(g_list_last(menu_item->button_widgets)->data); |
| 260 if (last_button) |
| 261 set_selected(menu_item, last_button); |
| 262 break; |
| 263 } |
| 264 case GTK_MENU_DIR_NEXT: { |
| 265 GtkWidget* first_button = |
| 266 GTK_WIDGET(g_list_first(menu_item->button_widgets)->data); |
| 267 if (first_button) |
| 268 set_selected(menu_item, first_button); |
| 269 break; |
| 270 } |
| 271 default: |
| 272 break; |
| 273 } |
| 274 } |
| 275 |
| 276 gtk_widget_queue_draw(GTK_WIDGET(menu_item)); |
| 277 } |
| 278 |
| 279 gboolean gtk_custom_menu_item_is_in_clickable_region( |
| 280 GtkCustomMenuItem* menu_item) { |
| 281 return menu_item->currently_selected_button != NULL; |
| 282 } |
OLD | NEW |