Using the Dialog Library
2003-01-29 19:02:34
Category: c:linux
Description: Example on using the C implemented dialog library.
Author: mind
Viewed: 3837
Rating: (12 votes)


/* dialog.c by mind [mind@metalshell.com]
 *
 * Example on using the C implemented dialog library.
 *
 * To compile with gcc add -ldialog to your compile line.
 *
 * 01/28/03 
 *
 * http://www.metalshell.com/
 *
 */
 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <dialog.h>
 
int cnt(char *res);
 
/* quick note; in all most all the dialog functions the first argument
 * is the title of the dialog and the second is either the message to
 * be placed within the dialog, the command to use, or a filename to
 * use, but in almost all case's it's used for the message in the dialog */
int main()
{
        char            *ptr, *p = NULL, q[30];
        int             x;
 
        /* initialize our dialog setup */
        init_dialog();
 
        /* dialog_prgbox(unsigned char *title, const unsigned char *line,
         *               int height, int width, int pause, int use_shell);
         *
         * line: is the command we want to execute
         * pause: confirmation apon termination
         * use_shell: use `sh' instead of exec();
         */
        dialog_prgbox("Example #1", "id", 7, 90, 1, 0);
        cnt(NULL);
 
        /* This contains a message and a OK button */
        dialog_mesgbox("Example #2", "\nExample using dialog_mesgbox()", 10, 30);
        cnt(NULL);
 
        /* This contains a message and YES/NO buttons
         * the return value is stored to x; YES = 0, NO = 1 */
        x = dialog_yesno("Example #3", "\nExample using dialog_yesno()", 10, 30);
        if (x == 0)
                cnt("yes");
        else
                cnt("no");
 
        dialog_notify("On the next dialog, use the up & down arrow keys");
 
        /* This contains text from a file, with an OK button to
         * continue.. The filename is the second argument */
        dialog_textbox("Example #4", "/etc/passwd", 10, 80);
        cnt(NULL);
 
        ptr = (char *)malloc(MAX_LEN); 
        memset(ptr, 0, sizeof(MAX_LEN));
        /* This contains `Enter something' and a user/keyboard input box
         * entered text is stored to ptr, NULL if nothing entered */
        dialog_inputbox("Example #5", "Enter something", 10, 60, ptr);
 
        dialog_clear();
        snprintf(q, sizeof(q), "You entered -- %s", ptr);
        dialog_notify(ptr);
 
        dialog_notify("The next example, is an example on using dialog_fselect");
 
        /* This contains 5 fields: 1. file mask  2. current directory
         * 3. directory listing  4. file listing  5. file selection  and
         * OK/CANCEL buttons.. file selection is stored to `p` NULL for nada.. */
        p = dialog_fselect("/etc", "*.*");
        if (p != NULL)
        {
                snprintf(q, sizeof(q), "You picked: %s", p);
 
                dialog_clear();
                dialog_notify(q);
        } else {
                cnt("nothing");
        }
 
        dialog_notify("The next example, is an example on using dialog_dselect");
 
        /* This contains basicly the same as fselect */
        dialog_dselect("/etc", "*.*");
 
        dialog_clear();
        dialog_notify("Example #8 using dialog_notify()");
 
        /* dialog_gauge(char *title, char *prompt, int y, int x, int height,
         *              int width, int perc);
         *
         * y - height of bar
         * x - width of bar
         * perc - current percentage
         */
 
        /* This contains a message and a status/gauge bar */
        dialog_gauge("Example #9", "example progress..", 10, 20, 10, 30, 0);
 
        for (x = 0; x < 100; x++)
        {
                usleep(4300);
                dialog_gauge("Example #9", "example progress..", 10, 20, 10, 30, x);
        }
        dialog_gauge("Example #9", "example complete", 10, 20, 10, 30, 100);
 
        /* end our dialog setup */
        end_dialog();
        return 1;
}
 
/* this was just thrown in so I didn't have
 * to type all of it for every example */
int cnt(char *res)
{
        char q[30];
 
        /* clear the dialog */
        dialog_clear();
        if (res != NULL)
        {
                snprintf(q, sizeof(q), "You picked/entered -- %s", res);
                dialog_notify(q);
        } else {
                dialog_notify("Press OK to continue to the next example");
        }
 
        return 1;
}