GtkMessageDialog



Hi,

I'm sort of losing track, but I don't think I've posted this one yet.
Of course it's a subclass of GtkDialog, which I posted earlier.  I've
made two changes to GtkDialog since posting it: I changed
GtkActionType and the "action" signal to GtkResponseType and the
"response" signal; and now "delete_event" inside gtk_dialog_run() will
emit the "response" signal, in order to ensure that under normal
circumstances "response" always gets emitted inside gtk_dialog_run().
(The abnormal circumstance is the programmer destroying the dialog
inside gtk_dialog_run(), which seems a bit silly on the programmer's
part.)

Typical sample usage of GtkMessageDialog:

 dialog = gtk_message_dialog_new (parent, 
                                  GTK_MESSAGE_ERROR,
                                  GTK_BUTTONS_YES_NO,
                                  GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_MODAL,
                                  "File not found: %s. Continue?", filename);

 /* At this point you could either connect to the "response" signal
  * and "destroy" signal, * or use gtk_dialog_run().  
  */

 result = gtk_dialog_run (GTK_DIALOG (dialog));
 
 if (result == GTK_RESPONSE_ACCEPT)
   /* Yes was clicked */ ;

 gtk_widget_destroy (dialog);

Note that this is not a fire-and-forget dialog like
gnome_ok_dialog(). It's an open question whether GTK should have
that, I don't know if it's been discussed. GtkMessageDialog is just a
convenience thing to set up the label and image in the dialog, and a
default title like "Error".

GtkMessageDialog basically looks like GnomeDialog, right now the
differences are maybe slightly different padding, no bevel on the
outside edge, and the buttons are GTK_BUTTONBOX_SPREAD instead of
GTK_BUTTONBOX_END. 

Havoc

#ifndef __GTK_MESSAGE_DIALOG_H__
#define __GTK_MESSAGE_DIALOG_H__

#include <gtk/gtkdialog.h>

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

typedef enum
{
  GTK_MESSAGE_INFO,
  GTK_MESSAGE_WARNING,
  GTK_MESSAGE_QUESTION,
  GTK_MESSAGE_ERROR
} GtkMessageType;

typedef enum
{
  GTK_BUTTONS_NONE,
  GTK_BUTTONS_OK,
  GTK_BUTTONS_CLOSE,
  GTK_BUTTONS_CANCEL,
  GTK_BUTTONS_YES_NO,
  GTK_BUTTONS_OK_CANCEL
} GtkButtonsType;

#define GTK_TYPE_MESSAGE_DIALOG                  (gtk_message_dialog_get_type ())
#define GTK_MESSAGE_DIALOG(obj)                  (GTK_CHECK_CAST ((obj), GTK_TYPE_MESSAGE_DIALOG, GtkMessageDialog))
#define GTK_MESSAGE_DIALOG_CLASS(klass)          (GTK_CHECK_CLASS_CAST ((klass), GTK_TYPE_MESSAGE_DIALOG, GtkMessageDialogClass))
#define GTK_IS_MESSAGE_DIALOG(obj)               (GTK_CHECK_TYPE ((obj), GTK_TYPE_MESSAGE_DIALOG))
#define GTK_IS_MESSAGE_DIALOG_CLASS(klass)       (GTK_CHECK_CLASS_TYPE ((klass), GTK_TYPE_MESSAGE_DIALOG))
#define GTK_MESSAGE_DIALOG_GET_CLASS(obj)        (GTK_CHECK_GET_CLASS ((obj), GTK_TYPE_MESSAGE_DIALOG, GtkMessageDialogClass))


typedef struct _GtkMessageDialog        GtkMessageDialog;
typedef struct _GtkMessageDialogClass   GtkMessageDialogClass;

struct _GtkMessageDialog
{
  GtkDialog parent_instance;

  GtkWidget *image;
  GtkWidget *label;
};

struct _GtkMessageDialogClass
{
  GtkDialogClass parent_class;

  
};

GtkType    gtk_message_dialog_get_type (void);

GtkWidget* gtk_message_dialog_new      (GtkWindow      *parent,
                                        GtkMessageType  type,
                                        GtkButtonsType  buttons,
                                        GtkDialogFlags  flags,
                                        const gchar    *message_format,
                                        ...) G_GNUC_PRINTF (5, 6);
 



#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* __GTK_MESSAGE_DIALOG_H__ */

#include "gtkmessagedialog.h"
#include "gtklabel.h"
#include "gtkhbox.h"
#include "gtkimage.h"
#include "gtkstock.h"

static void gtk_message_dialog_class_init (GtkMessageDialogClass *klass);
static void gtk_message_dialog_init       (GtkMessageDialog      *dialog);


GtkType
gtk_message_dialog_get_type (void)
{
  static GtkType dialog_type = 0;

  if (!dialog_type)
    {
      static const GtkTypeInfo dialog_info =
      {
	"GtkMessageDialog",
	sizeof (GtkMessageDialog),
	sizeof (GtkMessageDialogClass),
	(GtkClassInitFunc) gtk_message_dialog_class_init,
	(GtkObjectInitFunc) gtk_message_dialog_init,
	/* reserved_1 */ NULL,
        /* reserved_2 */ NULL,
        (GtkClassInitFunc) NULL,
      };

      dialog_type = gtk_type_unique (GTK_TYPE_DIALOG, &dialog_info);
    }

  return dialog_type;
}

static void
gtk_message_dialog_class_init (GtkMessageDialogClass *class)
{
}

static void
gtk_message_dialog_init (GtkMessageDialog *dialog)
{
  GtkWidget *hbox;
  
  dialog->label = gtk_label_new ("");
  dialog->image = gtk_image_new_from_stock (NULL, GTK_ICON_DIALOG);
  
  gtk_label_set_line_wrap (GTK_LABEL (dialog->label), TRUE);

  hbox = gtk_hbox_new (FALSE, 10);

  gtk_container_set_border_width (GTK_CONTAINER (hbox), 10);
  
  gtk_box_pack_start (GTK_BOX (hbox), dialog->image,
                      FALSE, FALSE, 2);

  gtk_box_pack_start (GTK_BOX (hbox), dialog->label,
                      TRUE, TRUE, 2);

  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
                      hbox,
                      FALSE, FALSE, 10);

  gtk_widget_show_all (hbox);
}

static void
setup_type(GtkMessageDialog *dialog, GtkMessageType type)
{
  /* Note: this function can be called more than once,
   * and after showing the dialog, due to object args
   */
  
  const gchar *stock_id = NULL;
  GtkStockItem item;
  
  switch (type)
    {
    case GTK_MESSAGE_INFO:
      stock_id = GTK_STOCK_DIALOG_INFO;
      break;

    case GTK_MESSAGE_QUESTION:
      stock_id = GTK_STOCK_DIALOG_QUESTION;
      break;

    case GTK_MESSAGE_WARNING:
      stock_id = GTK_STOCK_DIALOG_WARNING;
      break;
      
    case GTK_MESSAGE_ERROR:
      stock_id = GTK_STOCK_DIALOG_ERROR;
      break;
      
    default:
      g_warning ("Unknown GtkMessageType");
      break;
    }

  if (stock_id == NULL)
    stock_id = GTK_STOCK_DIALOG_INFO;

  if (gtk_stock_lookup (stock_id, &item))
    {
      gtk_image_set_from_stock (GTK_IMAGE (dialog->image), stock_id, GTK_ICON_DIALOG);
      
      gtk_window_set_title (GTK_WINDOW (dialog), item.label);
    }
  else
    g_warning ("Stock dialog ID doesn't exist?");  
}

GtkWidget*
gtk_message_dialog_new (GtkWindow     *parent,
                        GtkMessageType type,
                        GtkButtonsType buttons,
                        GtkDialogFlags flags,
                        const gchar   *message_format,
                        ...)
{
  GtkWidget *widget;
  GtkDialog *dialog;
  gchar* msg;
  va_list args;
  
  widget = GTK_WIDGET (gtk_type_new (GTK_TYPE_MESSAGE_DIALOG));
  dialog = GTK_DIALOG (widget);

  va_start (args, message_format);
  msg = g_strdup_vprintf(message_format, args);
  va_end (args);

  gtk_label_set_text (GTK_LABEL (GTK_MESSAGE_DIALOG (widget)->label),
                      msg);

  g_free (msg);

  if (parent != NULL)
    gtk_window_set_transient_for (GTK_WINDOW (widget),
                                  GTK_WINDOW (parent));
  

  if (flags & GTK_DIALOG_MODAL)
    gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);

  if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
    gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
  
  setup_type (GTK_MESSAGE_DIALOG (dialog), type);
  
  switch (buttons)
    {
    case GTK_BUTTONS_NONE:
      /* nothing */
      break;

    case GTK_BUTTONS_OK:
      gtk_dialog_add_button (dialog,
                             GTK_STOCK_BUTTON_OK,
                             GTK_RESPONSE_ACCEPT);
      break;

    case GTK_BUTTONS_CLOSE:
      gtk_dialog_add_button (dialog,
                             GTK_STOCK_BUTTON_CLOSE,
                             GTK_RESPONSE_ACCEPT);
      break;

    case GTK_BUTTONS_CANCEL:
      gtk_dialog_add_button (dialog,
                             GTK_STOCK_BUTTON_CANCEL,
                             GTK_RESPONSE_REJECT);
      break;

    case GTK_BUTTONS_YES_NO:
      gtk_dialog_add_button (dialog,
                             GTK_STOCK_BUTTON_YES,
                             GTK_RESPONSE_ACCEPT);
      gtk_dialog_add_button (dialog,
                             GTK_STOCK_BUTTON_NO,
                             GTK_RESPONSE_REJECT);
      break;

    case GTK_BUTTONS_OK_CANCEL:
      gtk_dialog_add_button (dialog,
                             GTK_STOCK_BUTTON_OK,
                             GTK_RESPONSE_ACCEPT);
      gtk_dialog_add_button (dialog,
                             GTK_STOCK_BUTTON_CANCEL,
                             GTK_RESPONSE_REJECT);
      break;
      
    default:
      g_warning ("Unknown GtkButtonsType");
      break;
    }

  return widget;
}




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]