home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / app / gimpui.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-17  |  5.1 KB  |  196 lines

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3.  *
  4.  * gimpui.c
  5.  * Copyright (C) 1999 Michael Natterer <mitch@gimp.org>
  6.  *
  7.  * This program is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation; either version 2 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * This program 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
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program; if not, write to the Free Software
  19.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20.  */
  21.  
  22. #include "config.h"
  23.  
  24. #include <stdio.h>
  25.  
  26. #include <gtk/gtk.h>
  27.  
  28. #include "gimpui.h"
  29.  
  30. #include "libgimp/gimpintl.h"
  31.  
  32.  
  33. extern gchar *prog_name;
  34.  
  35. static void  gimp_message_box_close_callback  (GtkWidget *widget,
  36.                            gpointer   data);
  37.  
  38. /*
  39.  *  Message Boxes...
  40.  */
  41.  
  42. typedef struct _MessageBox MessageBox;
  43.  
  44. struct _MessageBox
  45. {
  46.   GtkWidget   *mbox;
  47.   GtkWidget   *repeat_label;
  48.   gchar       *message;
  49.   gint         repeat_count;
  50.   GtkCallback  callback;
  51.   gpointer     data;
  52. };
  53.  
  54. /*  the maximum number of concucrrent dialog boxes */
  55. #define MESSAGE_BOX_MAXIMUM  4 
  56.  
  57. static GList *message_boxes = NULL;
  58.  
  59. void
  60. gimp_message_box (gchar       *message,
  61.           GtkCallback  callback,
  62.           gpointer     data)
  63. {
  64.   MessageBox *msg_box;
  65.   GtkWidget  *mbox;
  66.   GtkWidget  *vbox;
  67.   GtkWidget  *label;
  68.   GList      *list;
  69.  
  70.   if (!message)
  71.     return;
  72.  
  73.   if (g_list_length (message_boxes) > MESSAGE_BOX_MAXIMUM)
  74.     {
  75.       fprintf (stderr, "%s: %s\n", prog_name, message);
  76.       return;
  77.     }
  78.  
  79.   for (list = message_boxes; list; list = list->next)
  80.     {
  81.       msg_box = list->data;
  82.       if (strcmp (msg_box->message, message) == 0)
  83.     {
  84.       msg_box->repeat_count++;
  85.       if (msg_box->repeat_count > 1)
  86.         {
  87.           gchar *text = g_strdup_printf (_("Message repeated %d times"), 
  88.                          msg_box->repeat_count);
  89.           gtk_label_set_text (GTK_LABEL (msg_box->repeat_label), text);
  90.           g_free (text);
  91.           gdk_window_raise (msg_box->mbox->window);
  92.         }
  93.       else
  94.         {
  95.           GtkWidget *hbox;
  96.  
  97.           hbox = gtk_hbox_new (FALSE, 0);
  98.           gtk_box_pack_start (GTK_BOX (GTK_DIALOG (msg_box->mbox)->action_area), 
  99.                   hbox, TRUE, FALSE, 4);
  100.           msg_box->repeat_label = gtk_label_new (_("Message repeated once"));
  101.           gtk_container_add (GTK_CONTAINER (hbox), msg_box->repeat_label);
  102.  
  103.           gtk_widget_show (msg_box->repeat_label);
  104.           gtk_widget_show (hbox);
  105.           gdk_window_raise (msg_box->mbox->window);
  106.         }
  107.       return;
  108.     }
  109.     }
  110.  
  111.   if (g_list_length (message_boxes) == MESSAGE_BOX_MAXIMUM)
  112.     {
  113.       fprintf (stderr, "%s: %s\n", prog_name, message);
  114.       message = _("WARNING:\n"
  115.           "Too many open message dialogs.\n"
  116.           "Messages are redirected to stderr.");
  117.     }
  118.   
  119.   msg_box = g_new0 (MessageBox, 1);
  120.  
  121.   mbox = gimp_dialog_new (_("GIMP Message"), "gimp_message",
  122.               NULL, NULL,
  123.               GTK_WIN_POS_MOUSE,
  124.               FALSE, FALSE, FALSE,
  125.  
  126.               _("OK"), gimp_message_box_close_callback,
  127.               msg_box, NULL, NULL, TRUE, TRUE,
  128.  
  129.               NULL);
  130.  
  131.   vbox = gtk_vbox_new (FALSE, 0);
  132.   gtk_container_set_border_width (GTK_CONTAINER (vbox), 6);
  133.   gtk_container_add (GTK_CONTAINER (GTK_DIALOG (mbox)->vbox), vbox);
  134.   gtk_widget_show (vbox);
  135.  
  136.   label = gtk_label_new (message);
  137.   gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
  138.   gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, FALSE, 0);
  139.   gtk_widget_show (label);
  140.  
  141.   msg_box->mbox = mbox;
  142.   msg_box->message = g_strdup (message);
  143.   msg_box->callback = callback;
  144.   msg_box->data = data;
  145.  
  146.   message_boxes = g_list_append (message_boxes, msg_box);
  147.  
  148.   gtk_widget_show (mbox);
  149. }
  150.  
  151. static void
  152. gimp_message_box_close_callback (GtkWidget *widget,
  153.                  gpointer   data)
  154. {
  155.   MessageBox *msg_box;
  156.  
  157.   msg_box = (MessageBox *) data;
  158.  
  159.   /*  If there is a valid callback, invoke it  */
  160.   if (msg_box->callback)
  161.     (* msg_box->callback) (widget, msg_box->data);
  162.  
  163.   /*  Destroy the box  */
  164.   gtk_widget_destroy (msg_box->mbox);
  165.   
  166.   /* make this box available again */
  167.   message_boxes = g_list_remove (message_boxes, msg_box);
  168.  
  169.   g_free (msg_box->message);
  170.   g_free (msg_box);
  171. }
  172.  
  173.  
  174. /*  
  175.  * A workaround for what I think is a GTK+ bug:
  176.  *  If a dialog is hidden using gtk_widget_hide(),
  177.  *  and was iconified before, it is still present 
  178.  *  in the window_list and can be deiconified by
  179.  *  the user later. All subsequent calls to 
  180.  *  gtk_widget_hide() will then fail since the state
  181.  *  of the widget is still INVISIBLE.
  182.  *  Calling gdk_window_withdraw() seems to solve this.
  183.  *                                         --Sven
  184.  */
  185. void
  186. gimp_dialog_hide (GtkWidget *dialog)
  187. {
  188.   g_return_if_fail (dialog != NULL && !GTK_WIDGET_NO_WINDOW (dialog));
  189.   
  190.   gtk_widget_hide (dialog);
  191.   gdk_window_withdraw (dialog->window);
  192. }
  193.  
  194.  
  195.     
  196.