OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/gtk/menu_gtk.h" | 5 #include "chrome/browser/gtk/menu_gtk.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
9 #include "app/l10n_util.h" | 9 #include "app/l10n_util.h" |
10 #include "app/menus/accelerator_gtk.h" | 10 #include "app/menus/accelerator_gtk.h" |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 UTF16ToUTF8(model->GetLabelAt(index))); | 85 UTF16ToUTF8(model->GetLabelAt(index))); |
86 gtk_button_set_label(GTK_BUTTON(button), label.c_str()); | 86 gtk_button_set_label(GTK_BUTTON(button), label.c_str()); |
87 } | 87 } |
88 | 88 |
89 void SetupDynamicLabelMenuButton(GtkWidget* button, | 89 void SetupDynamicLabelMenuButton(GtkWidget* button, |
90 GtkWidget* menu, | 90 GtkWidget* menu, |
91 menus::ButtonMenuItemModel* model, | 91 menus::ButtonMenuItemModel* model, |
92 int index) { | 92 int index) { |
93 if (model->IsLabelDynamicAt(index)) { | 93 if (model->IsLabelDynamicAt(index)) { |
94 g_object_set_data(G_OBJECT(button), "button-model", | 94 g_object_set_data(G_OBJECT(button), "button-model", |
95 reinterpret_cast<void*>(model)); | 95 model); |
96 g_object_set_data(G_OBJECT(button), "button-model-id", | 96 g_object_set_data(G_OBJECT(button), "button-model-id", |
97 GINT_TO_POINTER(index)); | 97 GINT_TO_POINTER(index)); |
98 g_signal_connect(menu, "show", G_CALLBACK(OnSubmenuShowButtonMenuItem), | 98 g_signal_connect(menu, "show", G_CALLBACK(OnSubmenuShowButtonMenuItem), |
99 button); | 99 button); |
100 } | 100 } |
101 } | 101 } |
102 | 102 |
| 103 void OnSubmenuShowButtonImage(GtkWidget* widget, GtkButton* button) { |
| 104 MenuGtk::Delegate* delegate = reinterpret_cast<MenuGtk::Delegate*>( |
| 105 g_object_get_data(G_OBJECT(button), "menu-gtk-delegate")); |
| 106 int icon_idr = GPOINTER_TO_INT(g_object_get_data( |
| 107 G_OBJECT(button), "button-image-idr")); |
| 108 |
| 109 GtkIconSet* icon_set = delegate->GetIconSetForId(icon_idr); |
| 110 if (icon_set) { |
| 111 gtk_button_set_image( |
| 112 button, gtk_image_new_from_icon_set(icon_set, |
| 113 GTK_ICON_SIZE_MENU)); |
| 114 } |
| 115 } |
| 116 |
| 117 void SetupImageIcon(GtkWidget* button, |
| 118 GtkWidget* menu, |
| 119 int icon_idr, |
| 120 MenuGtk::Delegate* menu_gtk_delegate) { |
| 121 g_object_set_data(G_OBJECT(button), "button-image-idr", |
| 122 GINT_TO_POINTER(icon_idr)); |
| 123 g_object_set_data(G_OBJECT(button), "menu-gtk-delegate", |
| 124 menu_gtk_delegate); |
| 125 |
| 126 g_signal_connect(menu, "show", G_CALLBACK(OnSubmenuShowButtonImage), button); |
| 127 } |
| 128 |
103 // Popup menus may get squished if they open up too close to the bottom of the | 129 // Popup menus may get squished if they open up too close to the bottom of the |
104 // screen. This function takes the size of the screen, the size of the menu, | 130 // screen. This function takes the size of the screen, the size of the menu, |
105 // an optional widget, the Y position of the mouse click, and adjusts the popup | 131 // an optional widget, the Y position of the mouse click, and adjusts the popup |
106 // menu's Y position to make it fit if it's possible to do so. | 132 // menu's Y position to make it fit if it's possible to do so. |
107 // Returns the new Y position of the popup menu. | 133 // Returns the new Y position of the popup menu. |
108 int CalculateMenuYPosition(const GdkRectangle* screen_rect, | 134 int CalculateMenuYPosition(const GdkRectangle* screen_rect, |
109 const GtkRequisition* menu_req, | 135 const GtkRequisition* menu_req, |
110 const GtkWidget* widget, const int y) { | 136 const GtkWidget* widget, const int y) { |
111 CHECK(screen_rect); | 137 CHECK(screen_rect); |
112 CHECK(menu_req); | 138 CHECK(menu_req); |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
322 menus::AcceleratorGtk accelerator; | 348 menus::AcceleratorGtk accelerator; |
323 if (model->GetAcceleratorAt(i, &accelerator)) { | 349 if (model->GetAcceleratorAt(i, &accelerator)) { |
324 gtk_widget_add_accelerator(menu_item, | 350 gtk_widget_add_accelerator(menu_item, |
325 "activate", | 351 "activate", |
326 dummy_accel_group_, | 352 dummy_accel_group_, |
327 accelerator.GetGdkKeyCode(), | 353 accelerator.GetGdkKeyCode(), |
328 accelerator.gdk_modifier_type(), | 354 accelerator.gdk_modifier_type(), |
329 GTK_ACCEL_VISIBLE); | 355 GTK_ACCEL_VISIBLE); |
330 } | 356 } |
331 | 357 |
332 g_object_set_data(G_OBJECT(menu_item), "model", | 358 g_object_set_data(G_OBJECT(menu_item), "model", model); |
333 reinterpret_cast<void*>(model)); | |
334 AppendMenuItemToMenu(i, menu_item, menu, connect_to_activate); | 359 AppendMenuItemToMenu(i, menu_item, menu, connect_to_activate); |
335 | 360 |
336 if (model->IsLabelDynamicAt(i)) { | 361 if (model->IsLabelDynamicAt(i)) { |
337 g_signal_connect(menu, "show", G_CALLBACK(OnSubmenuShow), | 362 g_signal_connect(menu, "show", G_CALLBACK(OnSubmenuShow), |
338 GINT_TO_POINTER(i)); | 363 GINT_TO_POINTER(i)); |
339 } | 364 } |
340 | 365 |
341 menu_item = NULL; | 366 menu_item = NULL; |
342 } | 367 } |
343 } | 368 } |
344 | 369 |
345 GtkWidget* MenuGtk::BuildButtomMenuItem(menus::ButtonMenuItemModel* model, | 370 GtkWidget* MenuGtk::BuildButtomMenuItem(menus::ButtonMenuItemModel* model, |
346 GtkWidget* menu) { | 371 GtkWidget* menu) { |
347 GtkWidget* menu_item = gtk_custom_menu_item_new( | 372 GtkWidget* menu_item = gtk_custom_menu_item_new( |
348 RemoveWindowsStyleAccelerators(UTF16ToUTF8(model->label())).c_str()); | 373 RemoveWindowsStyleAccelerators(UTF16ToUTF8(model->label())).c_str()); |
349 | 374 |
350 // Set up the callback to the model for when it is clicked. | 375 // Set up the callback to the model for when it is clicked. |
351 g_object_set_data(G_OBJECT(menu_item), "button-model", | 376 g_object_set_data(G_OBJECT(menu_item), "button-model", model); |
352 reinterpret_cast<void*>(model)); | |
353 g_signal_connect(menu_item, "button-pushed", | 377 g_signal_connect(menu_item, "button-pushed", |
354 G_CALLBACK(OnMenuButtonPressed), this); | 378 G_CALLBACK(OnMenuButtonPressed), this); |
355 | 379 |
356 for (int i = 0; i < model->GetItemCount(); ++i) { | 380 for (int i = 0; i < model->GetItemCount(); ++i) { |
357 switch (model->GetTypeAt(i)) { | 381 switch (model->GetTypeAt(i)) { |
358 case menus::ButtonMenuItemModel::TYPE_SPACE: { | 382 case menus::ButtonMenuItemModel::TYPE_SPACE: { |
359 gtk_custom_menu_item_add_space(GTK_CUSTOM_MENU_ITEM(menu_item)); | 383 gtk_custom_menu_item_add_space(GTK_CUSTOM_MENU_ITEM(menu_item)); |
360 break; | 384 break; |
361 } | 385 } |
362 case menus::ButtonMenuItemModel::TYPE_BUTTON: { | 386 case menus::ButtonMenuItemModel::TYPE_BUTTON: { |
363 GtkWidget* button = gtk_custom_menu_item_add_button( | 387 GtkWidget* button = gtk_custom_menu_item_add_button( |
364 GTK_CUSTOM_MENU_ITEM(menu_item), | 388 GTK_CUSTOM_MENU_ITEM(menu_item), |
365 model->GetCommandIdAt(i)); | 389 model->GetCommandIdAt(i)); |
366 | 390 |
367 int icon_idr; | 391 int icon_idr; |
368 if (model->GetIconAt(i, &icon_idr)) { | 392 if (model->GetIconAt(i, &icon_idr)) { |
369 // TODO(erg): This should go through the GtkThemeProvider so we can | 393 SetupImageIcon(button, menu, icon_idr, delegate_); |
370 // get a version tinted to label color. | |
371 gtk_button_set_image( | |
372 GTK_BUTTON(button), | |
373 gtk_image_new_from_pixbuf( | |
374 ResourceBundle::GetSharedInstance(). | |
375 GetPixbufNamed(icon_idr))); | |
376 } else { | 394 } else { |
377 gtk_button_set_label( | 395 gtk_button_set_label( |
378 GTK_BUTTON(button), | 396 GTK_BUTTON(button), |
379 RemoveWindowsStyleAccelerators( | 397 RemoveWindowsStyleAccelerators( |
380 UTF16ToUTF8(model->GetLabelAt(i))).c_str()); | 398 UTF16ToUTF8(model->GetLabelAt(i))).c_str()); |
381 } | 399 } |
382 | 400 |
383 SetupDynamicLabelMenuButton(button, menu, model, i); | 401 SetupDynamicLabelMenuButton(button, menu, model, i); |
384 break; | 402 break; |
385 } | 403 } |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
566 if (GTK_IS_MENU_ITEM(widget)) { | 584 if (GTK_IS_MENU_ITEM(widget)) { |
567 gtk_widget_set_sensitive(widget, model->IsEnabledAt(id)); | 585 gtk_widget_set_sensitive(widget, model->IsEnabledAt(id)); |
568 | 586 |
569 GtkWidget* submenu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(widget)); | 587 GtkWidget* submenu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(widget)); |
570 if (submenu) { | 588 if (submenu) { |
571 gtk_container_foreach(GTK_CONTAINER(submenu), &SetMenuItemInfo, | 589 gtk_container_foreach(GTK_CONTAINER(submenu), &SetMenuItemInfo, |
572 userdata); | 590 userdata); |
573 } | 591 } |
574 } | 592 } |
575 } | 593 } |
OLD | NEW |