home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / libgimp / gimpdialog.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-19  |  10.9 KB  |  361 lines

  1. /* LIBGIMP - The GIMP Library                  
  2.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3.  *
  4.  * gimpdialog.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 <gtk/gtk.h>
  24.  
  25. #include "gimpdialog.h"
  26.  
  27. #include "pixmaps/wilber.xpm"
  28.  
  29. /*  local callbacks of gimp_dialog_new ()  */
  30. static gint
  31. gimp_dialog_delete_callback (GtkWidget *widget,
  32.                  GdkEvent  *event,
  33.                  gpointer   data) 
  34. {
  35.   GtkSignalFunc  cancel_callback;
  36.   GtkWidget     *cancel_widget;
  37.  
  38.   cancel_callback =
  39.     (GtkSignalFunc) gtk_object_get_data (GTK_OBJECT (widget),
  40.                      "gimp_dialog_cancel_callback");
  41.   cancel_widget =
  42.     (GtkWidget*) gtk_object_get_data (GTK_OBJECT (widget),
  43.                       "gimp_dialog_cancel_widget");
  44.  
  45.   /*  the cancel callback has to destroy the dialog  */
  46.   if (cancel_callback)
  47.     (* cancel_callback) (cancel_widget, data);
  48.  
  49.   return TRUE;
  50. }
  51.  
  52. static void
  53. gimp_dialog_realize_callback (GtkWidget *widget,
  54.                   gpointer   data) 
  55. {
  56.   static GdkPixmap *wilber_pixmap = NULL;
  57.   static GdkBitmap *wilber_mask   = NULL;
  58.   GtkStyle         *style;
  59.  
  60.   style = gtk_widget_get_style (widget);
  61.  
  62.   if (wilber_pixmap == NULL)
  63.     wilber_pixmap =
  64.       gdk_pixmap_create_from_xpm_d (widget->window,
  65.                     &wilber_mask,
  66.                     &style->bg[GTK_STATE_NORMAL],
  67.                     wilber_xpm);
  68.  
  69.   gdk_window_set_icon (widget->window, NULL,
  70.                wilber_pixmap, wilber_mask);
  71. }
  72.  
  73. /**
  74.  * gimp_dialog_new:
  75.  * @title: The dialog's title which will be set with gtk_window_set_title().
  76.  * @wmclass_name: The dialog's @wmclass_name which will be set with
  77.  *                gtk_window_set_wmclass(). The @wmclass_class will be
  78.  *                automatically set to "Gimp".
  79.  * @help_func: The function which will be called if the user presses "F1".
  80.  * @help_data: The data pointer which will be passed to @help_func.
  81.  * @position: The dialog's initial position which will be set with
  82.  *            gtk_window_set_position().
  83.  * @allow_shrink: The dialog's @allow_shrink flag, ...
  84.  * @allow_grow: ... it't @allow_grow flag and ...
  85.  * @auto_shrink: ... it's @auto_shrink flag which will all be set with
  86.  *               gtk_window_set_policy().
  87.  * @...: A #NULL terminated @va_list destribing the action_area buttons.
  88.  *
  89.  * This function simply packs the action_area arguments passed in "..."
  90.  * into a @va_list variable and passes everything to gimp_dialog_newv().
  91.  *
  92.  * For a description of the format of the @va_list describing the
  93.  * action_area buttons see gimp_dialog_create_action_areav().
  94.  *
  95.  * Returns: A #GtkDialog.
  96.  *
  97.  */
  98. GtkWidget *
  99. gimp_dialog_new (const gchar       *title,
  100.          const gchar       *wmclass_name,
  101.          GimpHelpFunc       help_func,
  102.          const gchar       *help_data,
  103.          GtkWindowPosition  position,
  104.          gint               allow_shrink,
  105.          gint               allow_grow,
  106.          gint               auto_shrink,
  107.  
  108.          /* specify action area buttons as va_list:
  109.           *  const gchar    *label,
  110.           *  GtkSignalFunc   callback,
  111.           *  gpointer        data,
  112.           *  GtkObject      *slot_object,
  113.           *  GtkWidget     **widget_ptr,
  114.           *  gboolean        default_action,
  115.           *  gboolean        connect_delete,
  116.           */
  117.  
  118.          ...)
  119. {
  120.   GtkWidget *dialog;
  121.   va_list    args;
  122.  
  123.   va_start (args, auto_shrink);
  124.  
  125.   dialog = gimp_dialog_newv (title,
  126.                  wmclass_name,
  127.                  help_func,
  128.                  help_data,
  129.                  position,
  130.                  allow_shrink,
  131.                  allow_grow,
  132.                  auto_shrink,
  133.                  args);
  134.  
  135.   va_end (args);
  136.  
  137.   return dialog;
  138. }
  139.  
  140. /**
  141.  * gimp_dialog_newv:
  142.  * @title: The dialog's title which will be set with gtk_window_set_title().
  143.  * @wmclass_name: The dialog's @wmclass_name which will be set with
  144.  *                gtk_window_set_wmclass(). The @wmclass_class will be
  145.  *                automatically set to "Gimp".
  146.  * @help_func: The function which will be called if the user presses "F1".
  147.  * @help_data: The data pointer which will be passed to @help_func.
  148.  * @position: The dialog's initial position which will be set with
  149.  *            gtk_window_set_position().
  150.  * @allow_shrink: The dialog's @allow_shrink flag, ...
  151.  * @allow_grow: ... it't @allow_grow flag and ...
  152.  * @auto_shrink: ... it's @auto_shrink flag which will all be set with
  153.  *               gtk_window_set_policy().
  154.  * @args: A @va_list as obtained with va_start() describing the action_area
  155.  *        buttons.
  156.  *
  157.  * This function performs all neccessary setps to set up a standard GIMP
  158.  * dialog.
  159.  *
  160.  * The @va_list describing the action_area buttons will be passed to
  161.  * gimp_dialog_create_action_areav().
  162.  *
  163.  * Returns: A #GtkDialog.
  164.  *
  165.  */
  166. GtkWidget *
  167. gimp_dialog_newv (const gchar       *title,
  168.           const gchar       *wmclass_name,
  169.           GimpHelpFunc       help_func,
  170.           const gchar       *help_data,
  171.           GtkWindowPosition  position,
  172.           gint               allow_shrink,
  173.           gint               allow_grow,
  174.           gint               auto_shrink,
  175.           va_list            args)
  176. {
  177.   GtkWidget *dialog;
  178.  
  179.   g_return_val_if_fail (title != NULL, NULL);
  180.   g_return_val_if_fail (wmclass_name != NULL, NULL);
  181.  
  182.   dialog = gtk_dialog_new ();
  183.   gtk_window_set_title (GTK_WINDOW (dialog), title);
  184.   gtk_window_set_wmclass (GTK_WINDOW (dialog), wmclass_name, "Gimp");
  185.   gtk_window_set_position (GTK_WINDOW (dialog), position);
  186.   gtk_window_set_policy (GTK_WINDOW (dialog),
  187.              allow_shrink, allow_grow, auto_shrink);
  188.  
  189.   /*  prepare the action_area  */
  190.   gimp_dialog_create_action_areav (GTK_DIALOG (dialog), args);
  191.  
  192.   /*  connect the "F1" help key  */
  193.   if (help_func)
  194.     gimp_help_connect_help_accel (dialog, help_func, help_data);
  195.  
  196.   return dialog;
  197. }
  198.  
  199. /**
  200.  * gimp_dialog_set_icon:
  201.  * @dialog: The #GtkWindow you want to set the pixmap icon for.
  202.  *
  203.  * This function sets the WM pixmap icon for the dialog which will appear
  204.  * e.g. in GNOME's or KDE's window list.
  205.  *
  206.  * Note that this function is automatically called by
  207.  * gimp_help_connect_help_accel() which in turn is called by
  208.  * gimp_dialog_newv(), so you only have to call it for #GtkWindow's which
  209.  * have no help page (like tear-off menus).
  210.  *
  211.  */
  212. void
  213. gimp_dialog_set_icon (GtkWindow *dialog)
  214. {
  215.   g_return_if_fail (dialog);
  216.   g_return_if_fail (GTK_IS_WINDOW (dialog));
  217.  
  218.   if (GTK_WIDGET_REALIZED (GTK_WIDGET (dialog)))
  219.     gimp_dialog_realize_callback (GTK_WIDGET (dialog), NULL);
  220.   else
  221.     gtk_signal_connect (GTK_OBJECT (dialog), "realize",
  222.             GTK_SIGNAL_FUNC (gimp_dialog_realize_callback),
  223.             NULL);
  224. }
  225.  
  226. /**
  227.  * gimp_dialog_create_action_area:
  228.  * @dialog: The #GtkDialog you want to create the action_area for.
  229.  * @...: A #NULL terminated @va_list destribing the action_area buttons.
  230.  *
  231.  * This function simply packs the action_area arguments passed in "..."
  232.  * into a @va_list variable and passes everything to
  233.  * gimp_dialog_create_action_areav().
  234.  *
  235.  */
  236. void
  237. gimp_dialog_create_action_area (GtkDialog *dialog,
  238.  
  239.                 /* specify action area buttons as va_list:
  240.                  *  const gchar    *label,
  241.                  *  GtkSignalFunc   callback,
  242.                  *  gpointer        data,
  243.                  *  GtkObject      *slot_object,
  244.                  *  GtkWidget     **widget_ptr,
  245.                  *  gboolean        default_action,
  246.                  *  gboolean        connect_delete,
  247.                  */
  248.  
  249.                 ...)
  250. {
  251.   va_list args;
  252.  
  253.   va_start (args, dialog);
  254.  
  255.   gimp_dialog_create_action_areav (dialog, args);
  256.  
  257.   va_end (args);
  258. }
  259.  
  260. /**
  261.  * gimp_dialog_create_action_areav:
  262.  * @dialog: The #GtkDialog you want to create the action_area for.
  263.  * @args: A @va_list as obtained with va_start() describing the action_area
  264.  *        buttons.
  265.  *
  266.  */
  267. void
  268. gimp_dialog_create_action_areav (GtkDialog *dialog,
  269.                  va_list    args)
  270. {
  271.   GtkWidget *hbbox = NULL;
  272.   GtkWidget *button;
  273.  
  274.   /*  action area variables  */
  275.   const gchar    *label;
  276.   GtkSignalFunc   callback;
  277.   gpointer        data;
  278.   GtkObject      *slot_object;
  279.   GtkWidget     **widget_ptr;
  280.   gboolean        default_action;
  281.   gboolean        connect_delete;
  282.  
  283.   gboolean delete_connected = FALSE;
  284.  
  285.   g_return_if_fail (dialog != NULL);
  286.   g_return_if_fail (GTK_IS_DIALOG (dialog));
  287.  
  288.   /*  prepare the action_area  */
  289.   label = va_arg (args, const gchar *);
  290.  
  291.   if (label)
  292.     {
  293.       gtk_container_set_border_width (GTK_CONTAINER (dialog->action_area), 2);
  294.       gtk_box_set_homogeneous (GTK_BOX (dialog->action_area), FALSE);
  295.  
  296.       hbbox = gtk_hbutton_box_new ();
  297.       gtk_button_box_set_spacing (GTK_BUTTON_BOX (hbbox), 4);
  298.       gtk_box_pack_end (GTK_BOX (dialog->action_area), hbbox, FALSE, FALSE, 0);
  299.       gtk_widget_show (hbbox);
  300.     }
  301.  
  302.   /*  the action_area buttons  */
  303.   while (label)
  304.     {
  305.       callback       = va_arg (args, GtkSignalFunc);
  306.       data           = va_arg (args, gpointer);
  307.       slot_object    = va_arg (args, GtkObject *);
  308.       widget_ptr     = va_arg (args, GtkWidget **);
  309.       default_action = va_arg (args, gboolean);
  310.       connect_delete = va_arg (args, gboolean);
  311.  
  312.       button = gtk_button_new_with_label (label);
  313.       GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
  314.       gtk_box_pack_start (GTK_BOX (hbbox), button, FALSE, FALSE, 0);
  315.  
  316.       if (slot_object == (GtkObject *) 1)
  317.     slot_object = GTK_OBJECT (dialog);
  318.  
  319.       if (data == NULL)
  320.     data = dialog;
  321.  
  322.       if (callback)
  323.     {
  324.       if (slot_object)
  325.         gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
  326.                        GTK_SIGNAL_FUNC (callback),
  327.                        slot_object);
  328.       else
  329.         gtk_signal_connect (GTK_OBJECT (button), "clicked",
  330.                 GTK_SIGNAL_FUNC (callback),
  331.                 data);
  332.     }
  333.  
  334.       if (widget_ptr)
  335.     *widget_ptr = button;
  336.  
  337.       if (connect_delete && callback && !delete_connected)
  338.     {
  339.       gtk_object_set_data (GTK_OBJECT (dialog),
  340.                    "gimp_dialog_cancel_callback",
  341.                    callback);
  342.       gtk_object_set_data (GTK_OBJECT (dialog),
  343.                    "gimp_dialog_cancel_widget",
  344.                    slot_object ? slot_object : GTK_OBJECT (button));
  345.  
  346.       /*  catch the WM delete event  */
  347.       gtk_signal_connect (GTK_OBJECT (dialog), "delete_event",
  348.                   GTK_SIGNAL_FUNC (gimp_dialog_delete_callback),
  349.                   data);
  350.  
  351.       delete_connected = TRUE;
  352.     }
  353.  
  354.       if (default_action)
  355.     gtk_widget_grab_default (button);
  356.       gtk_widget_show (button);
  357.  
  358.       label = va_arg (args, gchar *);
  359.     }
  360. }
  361.