X-Git-Url: https://git.distorted.org.uk/~mdw/mgLib/blobdiff_plain/1c48db0c647c38c9dcb8b8e48d48b3ebee761786..refs/heads/master:/msg.c diff --git a/msg.c b/msg.c index 6c0ca9d..2623225 100644 --- a/msg.c +++ b/msg.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: msg.c,v 1.1 1998/12/11 09:44:21 mdw Exp $ + * $Id$ * * Display a message and get an answer * @@ -12,26 +12,19 @@ * 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. + * it under the terms of the GNU Library 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. + * GNU Library 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. - * + * You should have received a copy of the GNU Library 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. */ /*----- Header files ------------------------------------------------------*/ @@ -44,6 +37,7 @@ #include #include +#include #include "cancel.h" #include "mdwfocus.h" @@ -58,13 +52,22 @@ static int creply; /* --- @msg@ --- * * - * Arguments: @char *buttons@ = the button strings to display - * @char *msg@ = the message skeleton string + * Arguments: @const char *title@ = the title for the message box + * @const char *buttons@ = the button strings to display + * @const 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. + * + * The @msg@ argument is a @printf@-style format string, which + * contains the message to actually be shown. The @buttons@ + * argument is a comma-separated list of buttons to be drawn, + * from right to left. A button name can be preceded with `:' + * to indicate that it's the default, or `~' if it's the + * `cancel' button. The return value is the zero-based index + * of the button selected. */ static int close(GtkWidget *w, gpointer p) @@ -80,13 +83,14 @@ static void click(GtkWidget *w, gpointer p) gtk_main_quit(); } -int msg(const char *buttons, const char *msg, ...) +int msg(const char *title, const char *buttons, const char *msg, ...) { GtkWidget *dbox, *w; /* --- Make most of the dialogue box --- */ dbox = gtk_dialog_new(); + gtk_window_set_title(GTK_WINDOW(dbox), title); 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); @@ -94,21 +98,19 @@ int msg(const char *buttons, const char *msg, ...) /* --- Set up the message string --- */ { - char buf[1024]; + dstr d = DSTR_INIT; va_list ap; va_start(ap, msg); -#ifdef HAVE_VSNPRINTF - vsnprintf(buf, sizeof(buf), msg, ap); -#else - vsprintf(buf, sizeof(buf), msg, ap); -#endif + dstr_vputf(&d, msg, &ap); va_end(ap); - w = gtk_label_new(buf); + w = gtk_label_new(d.buf); + gtk_label_set_justify(GTK_LABEL(w), GTK_JUSTIFY_LEFT); 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); + dstr_destroy(&d); } /* --- Set up the buttons --- */ @@ -118,11 +120,9 @@ int msg(const char *buttons, const char *msg, ...) unsigned f = 0; int i = 0; - enum { - f_ok = 1, - f_cancel = 2, - f_mdwfocus = 4 - }; +#define f_ok 1u +#define f_cancel 2u +#define f_mdwfocus 4u if (*p == '!') { f |= f_mdwfocus; @@ -153,10 +153,17 @@ int msg(const char *buttons, const char *msg, ...) cancel(GTK_WINDOW(dbox), w); gtk_widget_show(w); } + xfree(p); + + /* --- Preflight checklist --- */ gtk_widget_realize(dbox); if (f & f_mdwfocus) mdwfocus(dbox); + +#undef f_ok +#undef f_cancel +#undef f_mdwfocus } /* --- Ready --- */