home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 February / CMCD0205.ISO / Linux / gimp-2.2.0.tar.gz / gimp-2.2.0.tar / gimp-2.2.0 / libgimpwidgets / gimpoldwidgets.c < prev    next >
C/C++ Source or Header  |  2004-11-04  |  15KB  |  535 lines

  1. /* LIBGIMP - The GIMP Library
  2.  * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
  3.  *
  4.  * gimpoldwidgets.c
  5.  * Copyright (C) 2000 Michael Natterer <mitch@gimp.org>
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2 of the License, or (at your option) any later version.
  11.  *
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Library General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the
  19.  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20.  * Boston, MA 02111-1307, USA.
  21.  */
  22.  
  23. #include "config.h"
  24.  
  25. #include <string.h>
  26.  
  27. #undef GTK_DISABLE_DEPRECATED
  28. #include <gtk/gtk.h>
  29.  
  30. #include "gimpwidgetstypes.h"
  31.  
  32. #undef GIMP_DISABLE_DEPRECATED
  33. #include "gimpoldwidgets.h"
  34.  
  35.  
  36. /*
  37.  *  Widget Constructors
  38.  */
  39.  
  40. /**
  41.  * gimp_option_menu_new:
  42.  * @menu_only: %TRUE if the function should return a #GtkMenu only.
  43.  * @...:       A %NULL-terminated @va_list describing the menu items.
  44.  *
  45.  * Convenience function to create a #GtkOptionMenu or a #GtkMenu.
  46.  *
  47.  * Returns: A #GtkOptionMenu or a #GtkMenu (depending on @menu_only).
  48.  **/
  49. GtkWidget *
  50. gimp_option_menu_new (gboolean            menu_only,
  51.  
  52.               /* specify menu items as va_list:
  53.                *  const gchar    *label,
  54.                *  GCallback       callback,
  55.                *  gpointer        callback_data,
  56.                *  gpointer        item_data,
  57.                *  GtkWidget     **widget_ptr,
  58.                *  gboolean        active
  59.                */
  60.  
  61.                ...)
  62. {
  63.   GtkWidget *menu;
  64.   GtkWidget *menuitem;
  65.  
  66.   /*  menu item variables  */
  67.   const gchar    *label;
  68.   GCallback       callback;
  69.   gpointer        callback_data;
  70.   gpointer        item_data;
  71.   GtkWidget     **widget_ptr;
  72.   gboolean        active;
  73.  
  74.   va_list args;
  75.   gint    i;
  76.   gint    initial_index;
  77.  
  78.   menu = gtk_menu_new ();
  79.  
  80.   /*  create the menu items  */
  81.   initial_index = 0;
  82.  
  83.   va_start (args, menu_only);
  84.   label = va_arg (args, const gchar *);
  85.  
  86.   for (i = 0; label; i++)
  87.     {
  88.       callback      = va_arg (args, GCallback);
  89.       callback_data = va_arg (args, gpointer);
  90.       item_data     = va_arg (args, gpointer);
  91.       widget_ptr    = va_arg (args, GtkWidget **);
  92.       active        = va_arg (args, gboolean);
  93.  
  94.       if (strcmp (label, "---"))
  95.     {
  96.       menuitem = gtk_menu_item_new_with_label (label);
  97.  
  98.       g_signal_connect (menuitem, "activate",
  99.                 callback,
  100.                 callback_data);
  101.  
  102.       if (item_data)
  103.             {
  104.               g_object_set_data (G_OBJECT (menuitem), "gimp-item-data",
  105.                                  item_data);
  106.  
  107.               /*  backward compat  */
  108.               g_object_set_data (G_OBJECT (menuitem), "user_data", item_data);
  109.             }
  110.     }
  111.       else
  112.     {
  113.       menuitem = gtk_menu_item_new ();
  114.  
  115.       gtk_widget_set_sensitive (menuitem, FALSE);
  116.     }
  117.  
  118.       gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
  119.  
  120.       if (widget_ptr)
  121.     *widget_ptr = menuitem;
  122.  
  123.       gtk_widget_show (menuitem);
  124.  
  125.       /*  remember the initial menu item  */
  126.       if (active)
  127.     initial_index = i;
  128.  
  129.       label = va_arg (args, const gchar *);
  130.     }
  131.   va_end (args);
  132.  
  133.   if (! menu_only)
  134.     {
  135.       GtkWidget *optionmenu;
  136.  
  137.       optionmenu = gtk_option_menu_new ();
  138.       gtk_option_menu_set_menu (GTK_OPTION_MENU (optionmenu), menu);
  139.  
  140.       /*  select the initial menu item  */
  141.       gtk_option_menu_set_history (GTK_OPTION_MENU (optionmenu), initial_index);
  142.  
  143.       return optionmenu;
  144.     }
  145.  
  146.   return menu;
  147. }
  148.  
  149. /**
  150.  * gimp_option_menu_new2:
  151.  * @menu_only:          %TRUE if the function should return a #GtkMenu only.
  152.  * @menu_item_callback: The callback each menu item's "activate" signal will
  153.  *                      be connected with.
  154.  * @menu_item_callback_data:
  155.  *                      The data which will be passed to g_signal_connect().
  156.  * @initial:            The @item_data of the initially selected menu item.
  157.  * @...:                A %NULL-terminated @va_list describing the menu items.
  158.  *
  159.  * Convenience function to create a #GtkOptionMenu or a #GtkMenu.
  160.  *
  161.  * Returns: A #GtkOptionMenu or a #GtkMenu (depending on @menu_only).
  162.  **/
  163. GtkWidget *
  164. gimp_option_menu_new2 (gboolean         menu_only,
  165.                GCallback        menu_item_callback,
  166.                gpointer         callback_data,
  167.                gpointer         initial, /* item_data */
  168.  
  169.                /* specify menu items as va_list:
  170.             *  const gchar *label,
  171.             *  gpointer     item_data,
  172.             *  GtkWidget  **widget_ptr,
  173.             */
  174.  
  175.                ...)
  176. {
  177.   GtkWidget *menu;
  178.   GtkWidget *menuitem;
  179.  
  180.   /*  menu item variables  */
  181.   const gchar  *label;
  182.   gpointer      item_data;
  183.   GtkWidget   **widget_ptr;
  184.  
  185.   va_list args;
  186.   gint    i;
  187.   gint    initial_index;
  188.  
  189.   menu = gtk_menu_new ();
  190.  
  191.   /*  create the menu items  */
  192.   initial_index = 0;
  193.  
  194.   va_start (args, initial);
  195.   label = va_arg (args, const gchar *);
  196.  
  197.   for (i = 0; label; i++)
  198.     {
  199.       item_data  = va_arg (args, gpointer);
  200.       widget_ptr = va_arg (args, GtkWidget **);
  201.  
  202.       if (strcmp (label, "---"))
  203.     {
  204.       menuitem = gtk_menu_item_new_with_label (label);
  205.  
  206.       g_signal_connect (menuitem, "activate",
  207.                 menu_item_callback,
  208.                 callback_data);
  209.  
  210.       if (item_data)
  211.             {
  212.               g_object_set_data (G_OBJECT (menuitem), "gimp-item-data",
  213.                                  item_data);
  214.  
  215.               /*  backward compat  */
  216.               g_object_set_data (G_OBJECT (menuitem), "user_data", item_data);
  217.             }
  218.     }
  219.       else
  220.     {
  221.       menuitem = gtk_menu_item_new ();
  222.  
  223.       gtk_widget_set_sensitive (menuitem, FALSE);
  224.     }
  225.  
  226.       gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
  227.  
  228.       if (widget_ptr)
  229.     *widget_ptr = menuitem;
  230.  
  231.       gtk_widget_show (menuitem);
  232.  
  233.       /*  remember the initial menu item  */
  234.       if (item_data == initial)
  235.     initial_index = i;
  236.  
  237.       label = va_arg (args, const gchar *);
  238.     }
  239.   va_end (args);
  240.  
  241.   if (! menu_only)
  242.     {
  243.       GtkWidget *optionmenu;
  244.  
  245.       optionmenu = gtk_option_menu_new ();
  246.       gtk_option_menu_set_menu (GTK_OPTION_MENU (optionmenu), menu);
  247.  
  248.       /*  select the initial menu item  */
  249.       gtk_option_menu_set_history (GTK_OPTION_MENU (optionmenu), initial_index);
  250.  
  251.       return optionmenu;
  252.     }
  253.  
  254.   return menu;
  255. }
  256.  
  257. /**
  258.  * gimp_int_option_menu_new:
  259.  * @menu_only:          %TRUE if the function should return a #GtkMenu only.
  260.  * @menu_item_callback: The callback each menu item's "activate" signal will
  261.  *                      be connected with.
  262.  * @menu_item_callback_data:
  263.  *                      The data which will be passed to g_signal_connect().
  264.  * @initial:            The @item_data of the initially selected menu item.
  265.  * @...:                A %NULL-terminated @va_list describing the menu items.
  266.  *
  267.  * Convenience function to create a #GtkOptionMenu or a #GtkMenu. This
  268.  * function does the same thing as the deprecated function
  269.  * gimp_option_menu_new2(), but it takes integers as @item_data
  270.  * instead of pointers, since that is a very common case (mapping an
  271.  * enum to a menu).
  272.  *
  273.  * Returns: A #GtkOptionMenu or a #GtkMenu (depending on @menu_only).
  274.  **/
  275. GtkWidget *
  276. gimp_int_option_menu_new (gboolean         menu_only,
  277.                   GCallback        menu_item_callback,
  278.                   gpointer         callback_data,
  279.                   gint             initial, /* item_data */
  280.  
  281.                   /* specify menu items as va_list:
  282.                *  const gchar *label,
  283.                *  gint         item_data,
  284.                *  GtkWidget  **widget_ptr,
  285.                */
  286.  
  287.                   ...)
  288. {
  289.   GtkWidget *menu;
  290.   GtkWidget *menuitem;
  291.  
  292.   /*  menu item variables  */
  293.   const gchar  *label;
  294.   gint          item_data;
  295.   gpointer      item_ptr;
  296.   GtkWidget   **widget_ptr;
  297.  
  298.   va_list args;
  299.   gint    i;
  300.   gint    initial_index;
  301.  
  302.   menu = gtk_menu_new ();
  303.  
  304.   /*  create the menu items  */
  305.   initial_index = 0;
  306.  
  307.   va_start (args, initial);
  308.   label = va_arg (args, const gchar *);
  309.  
  310.   for (i = 0; label; i++)
  311.     {
  312.       item_data  = va_arg (args, gint);
  313.       widget_ptr = va_arg (args, GtkWidget **);
  314.  
  315.       item_ptr = GINT_TO_POINTER (item_data);
  316.  
  317.       if (strcmp (label, "---"))
  318.     {
  319.       menuitem = gtk_menu_item_new_with_label (label);
  320.  
  321.       g_signal_connect (menuitem, "activate",
  322.                 menu_item_callback,
  323.                 callback_data);
  324.  
  325.       if (item_data)
  326.             {
  327.               g_object_set_data (G_OBJECT (menuitem), "gimp-item-data",
  328.                                  item_ptr);
  329.  
  330.               /*  backward compat  */
  331.               g_object_set_data (G_OBJECT (menuitem), "user_data", item_ptr);
  332.             }
  333.     }
  334.       else
  335.     {
  336.       menuitem = gtk_menu_item_new ();
  337.  
  338.       gtk_widget_set_sensitive (menuitem, FALSE);
  339.     }
  340.  
  341.       gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
  342.  
  343.       if (widget_ptr)
  344.     *widget_ptr = menuitem;
  345.  
  346.       gtk_widget_show (menuitem);
  347.  
  348.       /*  remember the initial menu item  */
  349.       if (item_data == initial)
  350.     initial_index = i;
  351.  
  352.       label = va_arg (args, const gchar *);
  353.     }
  354.   va_end (args);
  355.  
  356.   if (! menu_only)
  357.     {
  358.       GtkWidget *optionmenu;
  359.  
  360.       optionmenu = gtk_option_menu_new ();
  361.       gtk_option_menu_set_menu (GTK_OPTION_MENU (optionmenu), menu);
  362.  
  363.       /*  select the initial menu item  */
  364.       gtk_option_menu_set_history (GTK_OPTION_MENU (optionmenu), initial_index);
  365.  
  366.       return optionmenu;
  367.     }
  368.  
  369.   return menu;
  370. }
  371.  
  372. /**
  373.  * gimp_option_menu_set_history:
  374.  * @option_menu: A #GtkOptionMenu as returned by gimp_option_menu_new() or
  375.  *               gimp_option_menu_new2().
  376.  * @item_data:   The @item_data of the menu item you want to select.
  377.  *
  378.  * Iterates over all entries in a #GtkOptionMenu and selects the one
  379.  * with the matching @item_data. Probably only makes sense to use with
  380.  * a #GtkOptionMenu that was created using gimp_option_menu_new() or
  381.  * gimp_option_menu_new2().
  382.  **/
  383. void
  384. gimp_option_menu_set_history (GtkOptionMenu *option_menu,
  385.                   gpointer       item_data)
  386. {
  387.   GtkWidget *menu_item;
  388.   GList     *list;
  389.   gint       history = 0;
  390.  
  391.   g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
  392.  
  393.   for (list = GTK_MENU_SHELL (option_menu->menu)->children;
  394.        list;
  395.        list = g_list_next (list))
  396.     {
  397.       menu_item = GTK_WIDGET (list->data);
  398.  
  399.       if (GTK_IS_LABEL (GTK_BIN (menu_item)->child) &&
  400.       g_object_get_data (G_OBJECT (menu_item),
  401.                              "gimp-item-data") == item_data)
  402.     {
  403.       break;
  404.     }
  405.  
  406.       history++;
  407.     }
  408.  
  409.   if (list)
  410.     gtk_option_menu_set_history (option_menu, history);
  411. }
  412.  
  413. /**
  414.  * gimp_int_option_menu_set_history:
  415.  * @option_menu: A #GtkOptionMenu as returned by gimp_int_option_menu_new().
  416.  * @item_data:   The @item_data of the menu item you want to select.
  417.  *
  418.  * Iterates over all entries in a #GtkOptionMenu and selects the one with the
  419.  * matching @item_data. Probably only makes sense to use with a #GtkOptionMenu
  420.  * that was created using gimp_int_option_menu_new(). This function does the
  421.  * same thing as gimp_option_menu_set_history(), but takes integers as
  422.  * @item_data instead of pointers.
  423.  **/
  424. void
  425. gimp_int_option_menu_set_history (GtkOptionMenu *option_menu,
  426.                       gint           item_data)
  427. {
  428.   g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
  429.  
  430.   gimp_option_menu_set_history (option_menu, GINT_TO_POINTER (item_data));
  431. }
  432.  
  433. /**
  434.  * gimp_option_menu_set_sensitive:
  435.  * @option_menu: a #GtkOptionMenu as returned by gimp_option_menu_new() or
  436.  *            gimp_option_menu_new2().
  437.  * @callback: a function called for each item in the menu to determine the
  438.  *            the sensitivity state.
  439.  * @callback_data: data to pass to the @callback function.
  440.  *
  441.  * Calls the given @callback for each item in the menu and passes it the
  442.  * item_data and the @callback_data. The menu item's sensitivity is set
  443.  * according to the return value of this function.
  444.  **/
  445. void
  446. gimp_option_menu_set_sensitive (GtkOptionMenu                     *option_menu,
  447.                                 GimpOptionMenuSensitivityCallback  callback,
  448.                                 gpointer                           callback_data)
  449. {
  450.   GtkWidget *menu_item;
  451.   GList     *list;
  452.   gpointer   item_data;
  453.   gboolean   sensitive;
  454.  
  455.   g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
  456.   g_return_if_fail (callback != NULL);
  457.  
  458.   for (list = GTK_MENU_SHELL (option_menu->menu)->children;
  459.        list;
  460.        list = g_list_next (list))
  461.     {
  462.       menu_item = GTK_WIDGET (list->data);
  463.  
  464.       if (GTK_IS_LABEL (GTK_BIN (menu_item)->child))
  465.         {
  466.           item_data = g_object_get_data (G_OBJECT (menu_item),
  467.                                          "gimp-item-data");
  468.           sensitive = callback (item_data, callback_data);
  469.           gtk_widget_set_sensitive (menu_item, sensitive);
  470.     }
  471.     }
  472. }
  473.  
  474. /**
  475.  * gimp_int_option_menu_set_sensitive:
  476.  * @option_menu: a #GtkOptionMenu as returned by gimp_option_menu_new() or
  477.  *            gimp_option_menu_new2().
  478.  * @callback: a function called for each item in the menu to determine the
  479.  *            the sensitivity state.
  480.  * @callback_data: data to pass to the @callback function.
  481.  *
  482.  * Calls the given @callback for each item in the menu and passes it the
  483.  * item_data and the @callback_data. The menu item's sensitivity is set
  484.  * according to the return value of this function. This function does the
  485.  * same thing as gimp_option_menu_set_sensitive(), but takes integers as
  486.  * @item_data instead of pointers.
  487.  **/
  488. void
  489. gimp_int_option_menu_set_sensitive (GtkOptionMenu                        *option_menu,
  490.                                     GimpIntOptionMenuSensitivityCallback  callback,
  491.                                     gpointer                              callback_data)
  492. {
  493.   GtkWidget *menu_item;
  494.   GList     *list;
  495.   gint       item_data;
  496.   gboolean   sensitive;
  497.  
  498.   g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
  499.   g_return_if_fail (callback != NULL);
  500.  
  501.   for (list = GTK_MENU_SHELL (option_menu->menu)->children;
  502.        list;
  503.        list = g_list_next (list))
  504.     {
  505.       menu_item = GTK_WIDGET (list->data);
  506.  
  507.       if (GTK_IS_LABEL (GTK_BIN (menu_item)->child))
  508.         {
  509.           item_data = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (menu_item),
  510.                                                           "gimp-item-data"));
  511.           sensitive = callback (item_data, callback_data);
  512.           gtk_widget_set_sensitive (menu_item, sensitive);
  513.     }
  514.     }
  515. }
  516.  
  517.  
  518. /**
  519.  * gimp_menu_item_update:
  520.  * @widget: A #GtkMenuItem.
  521.  * @data:   A pointer to a #gint variable which will store the value of
  522.  *          GPOINTER_TO_INT (g_object_get_data (@widget, "gimp-item-data")).
  523.  **/
  524. void
  525. gimp_menu_item_update (GtkWidget *widget,
  526.                gpointer   data)
  527. {
  528.   gint *item_val;
  529.  
  530.   item_val = (gint *) data;
  531.  
  532.   *item_val = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (widget),
  533.                           "gimp-item-data"));
  534. }
  535.