2011-10-07 [colin] 3.7.10cvs21
[claws.git] / src / gtk / menu.c
index 1fe647fa5b5977de67fac6edfa5ad6391da3e7de..6ed5205664ff3b68ca6ef79548096d0dd2efb579 100644 (file)
@@ -1,10 +1,10 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2001 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2011 Hiroyuki Yamamoto and the Claws Mail team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
@@ -13,8 +13,8 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 #ifdef HAVE_CONFIG_H
 #endif
 
 #include <glib.h>
-#include <gtk/gtkwidget.h>
-#include <gtk/gtkmenu.h>
-#include <gtk/gtkmenubar.h>
-#include <gtk/gtkitemfactory.h>
-#include <gtk/gtkcheckmenuitem.h>
-#include <gtk/gtkbutton.h>
-
-#include "intl.h"
+#include <glib/gi18n.h>
+#include <gtk/gtk.h>
+
+#if !GTK_CHECK_VERSION(3, 0, 0)
+#include "gtkcmoptionmenu.h"
+#endif
 #include "menu.h"
 #include "utils.h"
+#include "gtkutils.h"
 
-static gchar *menu_translate(const gchar *path, gpointer data);
+#ifdef MAEMO
+#ifdef CHINOOK
+#include <hildon/hildon-program.h>
+#else
+#include <hildon-widgets/hildon-program.h>
+#endif
+#endif
 
-GtkWidget *menubar_create(GtkWidget *window, GtkItemFactoryEntry *entries,
-                         guint n_entries, const gchar *path, gpointer data)
+GtkActionGroup *cm_menu_create_action_group(const gchar *name, GtkActionEntry *entries,
+                                           gint num_entries, gpointer data)
 {
-       GtkItemFactory *factory;
-       GtkAccelGroup *accel_group;
-
-       accel_group = gtk_accel_group_new();
-       factory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, path, accel_group);
-       gtk_item_factory_set_translate_func(factory, menu_translate,
-                                           NULL, NULL);
-       gtk_item_factory_create_items(factory, n_entries, entries, data);
-       gtk_accel_group_attach(accel_group, GTK_OBJECT(window));
-
-       return gtk_item_factory_get_widget(factory, path);
+       GtkActionGroup *group = gtk_action_group_new(name);
+       gtk_action_group_set_translate_func(group, menu_translate, NULL, NULL);
+       gtk_action_group_add_actions(group, entries, num_entries, data);
+       gtk_ui_manager_insert_action_group(gtkut_ui_manager(), group, 0);
+       return group;
 }
 
-GtkWidget *menu_create_items(GtkItemFactoryEntry *entries,
-                            guint n_entries, const gchar *path,
-                            GtkItemFactory **factory, gpointer data)
+GtkActionGroup *cm_menu_create_action_group_full(GtkUIManager *manager, const gchar *name, GtkActionEntry *entries,
+                                           gint num_entries, gpointer data)
 {
-       *factory = gtk_item_factory_new(GTK_TYPE_MENU, path, NULL);
-       gtk_item_factory_set_translate_func(*factory, menu_translate,
-                                           NULL, NULL);
-       gtk_item_factory_create_items(*factory, n_entries, entries, data);
-
-       return gtk_item_factory_get_widget(*factory, path);
+       GtkActionGroup *group = gtk_action_group_new(name);
+       gtk_action_group_set_translate_func(group, menu_translate, NULL, NULL);
+       gtk_action_group_add_actions(group, entries, num_entries, data);
+       gtk_ui_manager_insert_action_group(manager, group, 0);
+       return group;
 }
 
-GtkWidget *popupmenu_create(GtkWidget *window, GtkItemFactoryEntry *entries,
-                            guint n_entries, const gchar *path, gpointer data)
+gchar *menu_translate(const gchar *path, gpointer data)
 {
-       GtkItemFactory *factory;
-       GtkAccelGroup *accel_group;
+       gchar *retval;
 
-       accel_group = gtk_accel_group_new();
-       factory = gtk_item_factory_new(GTK_TYPE_MENU, path, accel_group);
-       gtk_item_factory_set_translate_func(factory, menu_translate,
-                                           NULL, NULL);
-       gtk_item_factory_create_items(factory, n_entries, entries, data);
-       gtk_accel_group_attach(accel_group, GTK_OBJECT(window));
+       retval = gettext(path);
 
-       return gtk_item_factory_get_widget(factory, path);
+       return retval;
 }
 
-static gchar *menu_translate(const gchar *path, gpointer data)
+void cm_menu_set_sensitive(gchar *menu, gboolean sensitive)
 {
-       gchar *retval;
+       GtkUIManager *gui_manager = gtkut_ui_manager();
+       gchar *path = g_strdup_printf("Menus/%s", menu);
 
-       retval = gettext(path);
+       cm_menu_set_sensitive_full(gui_manager, path, sensitive);
+       g_free(path);
+}
 
-       return retval;
+void cm_toggle_menu_set_active(gchar *menu, gboolean active)
+{
+       GtkUIManager *gui_manager = gtkut_ui_manager();
+       gchar *path = g_strdup_printf("Menus/%s", menu);
+
+       cm_toggle_menu_set_active_full(gui_manager, path, active);
+       g_free(path);
 }
 
-void menu_set_sensitive(GtkItemFactory *ifactory, const gchar *path,
-                       gboolean sensitive)
+void cm_menu_set_sensitive_full(GtkUIManager *gui_manager, gchar *menu, gboolean sensitive)
 {
        GtkWidget *widget;
+       gchar *path = g_strdup_printf("/%s/", menu);
 
-       g_return_if_fail(ifactory != NULL);
+       widget = gtk_ui_manager_get_widget(gui_manager, path);
+       if( !GTK_IS_WIDGET(widget) ) {
+               g_message("Blah, '%s' is not a widget.\n", path);
+       }
 
-       widget = gtk_item_factory_get_item(ifactory, path);
-       if(widget == NULL) {
-               debug_print("unknown menu entry %s\n", path);
-               return;
+       if( !GTK_IS_MENU_ITEM(widget) ) {
+               g_message("Blah, '%s' is not a menu item.\n", path);
        }
+
        gtk_widget_set_sensitive(widget, sensitive);
+       g_free(path);
 }
 
-void menu_set_sensitive_all(GtkMenuShell *menu_shell, gboolean sensitive)
+void cm_toggle_menu_set_active_full(GtkUIManager *gui_manager, gchar *menu, gboolean active)
 {
-       GList *cur;
+       GtkWidget *widget;
+       gchar *path = g_strdup_printf("/%s/", menu);
 
-       for (cur = menu_shell->children; cur != NULL; cur = cur->next)
-               gtk_widget_set_sensitive(GTK_WIDGET(cur->data), sensitive);
+       widget = gtk_ui_manager_get_widget(gui_manager, path);
+       if( !GTK_IS_WIDGET(widget) ) {
+               g_message("Blah, '%s' is not a widget.\n", path);
+       }
+
+       if( !GTK_CHECK_MENU_ITEM(widget) ) {
+               g_message("Blah, '%s' is not a check menu item.\n", path);
+       }
+
+       gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), active);
+       g_free(path);
 }
 
-void menu_set_toggle(GtkItemFactory *ifactory, const gchar *path,
-                       gboolean active)
+void menu_set_sensitive_all(GtkMenuShell *menu_shell, gboolean sensitive)
 {
-       GtkWidget *widget;
+       GList *children = gtk_container_get_children(GTK_CONTAINER(menu_shell));
+       GList *cur;
 
-       g_return_if_fail(ifactory != NULL);
+       for (cur = children; cur != NULL; cur = cur->next)
+               gtk_widget_set_sensitive(GTK_WIDGET(cur->data), sensitive);
 
-       widget = gtk_item_factory_get_item(ifactory, path);
-       gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM(widget), active);
+       g_list_free(children);
 }
 
-void menu_toggle_toggle(GtkItemFactory *ifactory, const gchar *path)
+void menu_button_position(GtkMenu *menu, gint *x, gint *y, gboolean *push_in,
+                         gpointer user_data)
 {
-       GtkWidget *widget;
-       
-       g_return_if_fail(ifactory != NULL);
+        GtkWidget *widget;
+        gint wheight;
+        gint wx, wy;
+       GtkAllocation allocation;
+       GtkRequisition mreq, wreq;
+       GdkScreen *screen;
+       GdkRectangle monitor;
+       gint monitor_num;
+
+       cm_return_if_fail(x && y);
+       cm_return_if_fail(GTK_IS_BUTTON(user_data));
+
+       widget = GTK_WIDGET(user_data);
+
+        gdk_window_get_origin(gtk_widget_get_window(widget), x, y);
+        gtk_widget_get_requisition(widget, &wreq);
+        wheight = wreq.height;
+        gtk_widget_get_allocation(widget, &allocation);
+        wx = allocation.x;
+        wy = allocation.y;
+        
+       gtk_widget_size_request(GTK_WIDGET(menu), &mreq);
+       screen = gtk_widget_get_screen (widget);
+       monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
+       gdk_screen_get_monitor_geometry (screen, monitor_num, 
+                                        &monitor);
+
+        *x = *x + wx;
+        *y = *y + wy + wheight;
        
-       widget = gtk_item_factory_get_item(ifactory, path);
-       gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), !((GTK_CHECK_MENU_ITEM(widget))->active));
+       if (*y + mreq.height >= monitor.height)
+               *y -= mreq.height;
 }
 
-void menu_button_position(GtkMenu *menu, gint *x, gint *y, gpointer user_data)
-{
-       GtkWidget *button;
-       GtkRequisition requisition;
-       gint button_xpos, button_ypos;
-       gint xpos, ypos;
-       gint width, height;
-       gint scr_width, scr_height;
-
-       g_return_if_fail(user_data != NULL);
-       g_return_if_fail(GTK_IS_BUTTON(user_data));
-
-       button = GTK_WIDGET(user_data);
-
-       gtk_widget_get_child_requisition(GTK_WIDGET(menu), &requisition);
-       width = requisition.width;
-       height = requisition.height;
-       gdk_window_get_origin(button->window, &button_xpos, &button_ypos);
-
-       xpos = button_xpos;
-       ypos = button_ypos + button->allocation.height;
-
-       scr_width = gdk_screen_width();
-       scr_height = gdk_screen_height();
-
-       if (xpos + width > scr_width)
-               xpos -= (xpos + width) - scr_width;
-       if (ypos + height > scr_height)
-               ypos = button_ypos - height;
-       if (xpos < 0)
-               xpos = 0;
-       if (ypos < 0)
-               ypos = 0;
-
-       *x = xpos;
-       *y = ypos;
-}
-
-gint menu_find_option_menu_index(GtkOptionMenu *optmenu, gpointer data,
+#if !GTK_CHECK_VERSION(3, 0, 0)
+gint menu_find_option_menu_index(GtkCMOptionMenu *optmenu, gpointer data,
                                 GCompareFunc func)
 {
        GtkWidget *menu;
        GtkWidget *menuitem;
        gpointer menu_data;
+       GList *children;
        GList *cur;
        gint n;
 
-       menu = gtk_option_menu_get_menu(optmenu);
+       menu = gtk_cmoption_menu_get_menu(optmenu);
+       children = gtk_container_get_children(GTK_CONTAINER(GTK_MENU_SHELL(menu)));
 
-       for (cur = GTK_MENU_SHELL(menu)->children, n = 0;
+       for (cur = children, n = 0;
             cur != NULL; cur = cur->next, n++) {
                menuitem = GTK_WIDGET(cur->data);
-               menu_data = gtk_object_get_user_data(GTK_OBJECT(menuitem));
+               menu_data = g_object_get_data(G_OBJECT(menuitem),
+                                             MENU_VAL_ID);
                if (func) {
                        if (func(menu_data, data) == 0)
                                return n;
@@ -192,6 +197,8 @@ gint menu_find_option_menu_index(GtkOptionMenu *optmenu, gpointer data,
                        return n;
        }
 
+       g_list_free(children);
+
        return -1;
 }
-
+#endif