diff --git a/src/ChangeLog b/src/ChangeLog index 712744be8..8657882ab 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,5 +1,11 @@ 2003-10-25 Pavel Roskin + * util.c (msglen): Constify first argument. + * wtools.c (query_dialog): Constify string argments. + (create_message): New function, forked from message(). + (message): Return void. Allocate memory dynamically. + * wtools.h: Eliminate D_INSERT. Adjust all users. + * background.c: Replace all message stubs with a new function mc_message(). Protect against strlen(MSG_ERROR). Adjust all dependencies. diff --git a/src/learn.c b/src/learn.c index 14e21c0d9..d09a21331 100644 --- a/src/learn.c +++ b/src/learn.c @@ -76,7 +76,7 @@ static char* learn_title = N_("Learn keys"); static int learn_button (int action) { unsigned char *seq; - Dlg_head *d = message (D_INSERT | 1, _(" Teach me a key "), + Dlg_head *d = create_message (D_ERROR, _(" Teach me a key "), _("Please press the %s\n" "and then wait until this message disappears.\n\n" "Then, press it again to see if OK appears\n" diff --git a/src/util.c b/src/util.c index 461b1d0c5..9dca1cbc4 100644 --- a/src/util.c +++ b/src/util.c @@ -90,7 +90,7 @@ is_printable (int c) } /* Returns the message dimensions (lines and columns) */ -int msglen (char *text, int *lines) +int msglen (const char *text, int *lines) { int max = 0; int line_len = 0; diff --git a/src/util.h b/src/util.h index 8c1f84450..db63f2c98 100644 --- a/src/util.h +++ b/src/util.h @@ -7,7 +7,7 @@ /* String managing functions */ int is_printable (int c); -int msglen (char *text, int *lines); +int msglen (const char *text, int *lines); char *trim (char *s, char *d, int len); char *name_quote (const char *c, int quote_percent); char *fake_name_quote (const char *c, int quote_percent); diff --git a/src/view.c b/src/view.c index da9aceb31..4d646eb21 100644 --- a/src/view.c +++ b/src/view.c @@ -1595,7 +1595,7 @@ search (WView *view, char *text, got_interrupt (); if (verbose) { - d = message (D_INSERT, _("Search"), _("Searching %s"), text); + d = create_message (D_NORMAL, _("Search"), _("Searching %s"), text); mc_refresh (); } diff --git a/src/wtools.c b/src/wtools.c index 88efa36ed..6099791d4 100644 --- a/src/wtools.c +++ b/src/wtools.c @@ -1,7 +1,3 @@ -/* {{{ */ - -/* {{{ Copyright Notice */ - /* Widget based utility functions. Copyright (C) 1994, 1995 the Free Software Foundation @@ -26,8 +22,6 @@ */ -/* }}} */ - #include #include #include @@ -45,9 +39,6 @@ #include "key.h" /* For mi_getch() */ #include "complete.h" /* INPUT_COMPLETE_CD */ -/* }}} */ - -/* {{{ Listbox utility functions */ Listbox * create_listbox_window (int cols, int lines, char *title, char *help) @@ -100,17 +91,14 @@ int run_listbox (Listbox *l) return val; } -/* }}} */ - -/* {{{ Query Dialog functions */ static Dlg_head *last_query_dlg; static int sel_pos = 0; /* Used to ask questions to the user */ int -query_dialog (char *header, char *text, int flags, int count, ...) +query_dialog (const char *header, const char *text, int flags, int count, ...) { va_list ap; Dlg_head *query_dlg; @@ -205,38 +193,70 @@ void query_set_sel (int new_sel) sel_pos = new_sel; } -/* }}} */ -/* {{{ The message function */ - -/* To show nice messages to the users */ -struct Dlg_head * -message (int error, char *header, const char *text, ...) +/* Create message dialog */ +static struct Dlg_head * +do_create_message (int flags, const char *title, const char *text) { - va_list args; - char buffer [4096]; + char *p; Dlg_head *d; - /* Setup the display information */ - strcpy (buffer, "\n"); - va_start (args, text); - g_vsnprintf (&buffer [1], sizeof (buffer) - 2, text, args); - strcat (buffer, "\n"); - va_end (args); - - query_dialog (header, buffer, error, 0); - + /* Add empty lines before and after the message */ + p = g_strdup_printf ("\n%s\n", text); + query_dialog (title, p, flags, 0); d = last_query_dlg; init_dlg (d); - if (!(error & D_INSERT)){ - mi_getch (); - dlg_run_done (d); - destroy_dlg (d); - } else - return d; - return 0; + g_free (p); + + return d; } -/* }}} */ + + +/* + * Create message dialog. The caller must call dlg_run_done() and + * destroy_dlg() to dismiss it. Not safe to call from background. + */ +struct Dlg_head * +create_message (int flags, const char *title, const char *text, ...) +{ + va_list args; + Dlg_head *d; + char *p; + + va_start (args, text); + p = g_strdup_vprintf (text, args); + va_end (args); + + d = do_create_message (flags, title, p); + g_free (p); + + return d; +} + + +/* + * Show message dialog. Dismiss it when any key is pressed. + * Not safe to call from background. + */ +void +message (int flags, const char *title, const char *text, ...) +{ + va_list args; + Dlg_head *d; + char *p; + + va_start (args, text); + p = g_strdup_vprintf (text, args); + va_end (args); + + d = do_create_message (flags, title, p); + g_free (p); + + mi_getch (); + dlg_run_done (d); + destroy_dlg (d); +} + /* {{{ Quick dialog routines */ diff --git a/src/wtools.h b/src/wtools.h index 851444da1..1aa4a18cf 100644 --- a/src/wtools.h +++ b/src/wtools.h @@ -68,19 +68,21 @@ char *real_input_dialog_help (char *header, char *text, char *help, char *def_te void query_set_sel (int new_sel); -struct Dlg_head *message (int error, char *header, const char *text, ...) +struct Dlg_head *create_message (int flags, const char *title, + const char *text, ...) + __attribute__ ((format (printf, 3, 4))); +void message (int flags, const char *title, const char *text, ...) __attribute__ ((format (printf, 3, 4))); /* Use this as header for message() - it expands to "Error" */ #define MSG_ERROR ((char *) -1) -int query_dialog (char *header, char *text, int flags, int count, ...); +int query_dialog (const char *header, const char *text, int flags, int count, ...); /* flags for message() and query_dialog() */ enum { D_NORMAL = 0, D_ERROR = 1, - D_INSERT = 2 } /* dialog options */; #endif /* __WTOOLS_H */