+/* -*-c-*-
+ *
+ * $Id: msg.c,v 1.1 1998/12/11 09:44:21 mdw Exp $
+ *
+ * Display a message and get an answer
+ *
+ * (c) 1998 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of the mgLib GTK utilities library.
+ *
+ * mgLib 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
+ * (at your option) any later version.
+ *
+ * mgLib is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with mgLib; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+/*----- Revision history --------------------------------------------------*
+ *
+ * $Log: msg.c,v $
+ * Revision 1.1 1998/12/11 09:44:21 mdw
+ * Initial version.
+ *
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <gtk/gtk.h>
+
+#include <mLib/alloc.h>
+
+#include "cancel.h"
+#include "mdwfocus.h"
+#include "msg.h"
+
+/*----- Static variables --------------------------------------------------*/
+
+static int reply;
+static int creply;
+
+/*----- Main code ---------------------------------------------------------*/
+
+/* --- @msg@ --- *
+ *
+ * Arguments: @char *buttons@ = the button strings to display
+ * @char *msg@ = the message skeleton string
+ *
+ * Returns: Index of the button selected.
+ *
+ * Use: Displays a message to the user in a nice dialogue box and
+ * returns the index of the button selected.
+ */
+
+static int close(GtkWidget *w, gpointer p)
+{
+ reply = creply;
+ gtk_main_quit();
+ return (1);
+}
+
+static void click(GtkWidget *w, gpointer p)
+{
+ reply = (int)p;
+ gtk_main_quit();
+}
+
+int msg(const char *buttons, const char *msg, ...)
+{
+ GtkWidget *dbox, *w;
+
+ /* --- Make most of the dialogue box --- */
+
+ dbox = gtk_dialog_new();
+ gtk_signal_connect(GTK_OBJECT(dbox), "delete_event",
+ GTK_SIGNAL_FUNC(close), 0);
+ gtk_box_set_homogeneous(GTK_BOX(GTK_DIALOG(dbox)->action_area), 0);
+
+ /* --- Set up the message string --- */
+
+ {
+ char buf[1024];
+ va_list ap;
+
+ va_start(ap, msg);
+#ifdef HAVE_VSNPRINTF
+ vsnprintf(buf, sizeof(buf), msg, ap);
+#else
+ vsprintf(buf, sizeof(buf), msg, ap);
+#endif
+ va_end(ap);
+ w = gtk_label_new(buf);
+ gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dbox)->vbox), w, 1, 1, 0);
+ gtk_window_position(GTK_WINDOW(dbox), GTK_WIN_POS_MOUSE);
+ gtk_widget_show(w);
+ gtk_misc_set_padding(GTK_MISC(w), 16, 16);
+ }
+
+ /* --- Set up the buttons --- */
+
+ {
+ char *p = xstrdup(buttons);
+ unsigned f = 0;
+ int i = 0;
+
+ enum {
+ f_ok = 1,
+ f_cancel = 2,
+ f_mdwfocus = 4
+ };
+
+ if (*p == '!') {
+ f |= f_mdwfocus;
+ p++;
+ }
+
+ creply = -1;
+
+ for (p = strtok(p, ","); p; p = strtok(0, ","), i++) {
+ unsigned ff = 0;
+ if (*p == ':') {
+ ff |= f_ok;
+ p++;
+ }
+ if (*p == '~') {
+ ff |= f_cancel;
+ creply = i;
+ p++;
+ }
+ w = gtk_button_new_with_label(p);
+ GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
+ gtk_box_pack_end(GTK_BOX(GTK_DIALOG(dbox)->action_area), w, 0, 0, 0);
+ gtk_signal_connect(GTK_OBJECT(w), "clicked",
+ GTK_SIGNAL_FUNC(click), (gpointer)i);
+ if (ff & f_ok)
+ gtk_widget_grab_default(w);
+ if (ff & f_cancel)
+ cancel(GTK_WINDOW(dbox), w);
+ gtk_widget_show(w);
+ }
+
+ gtk_widget_realize(dbox);
+ if (f & f_mdwfocus)
+ mdwfocus(dbox);
+ }
+
+ /* --- Ready --- */
+
+ gtk_grab_add(GTK_WIDGET(dbox));
+ gtk_widget_show(dbox);
+ gtk_main();
+ gtk_grab_remove(GTK_WIDGET(dbox));
+ gtk_widget_destroy(dbox);
+ return (reply);
+}
+
+/*----- That's all, folks -------------------------------------------------*/