/*
    Specific.c
*/

/// Includes
#include <stdarg.h>
#include <proto/dos.h>
#include <proto/exec.h>
#include <proto/intuition.h>
#include <dos/dosextens.h>
#include <dos/rdargs.h>
#include "tmiga.h"
#include "protos.h"
///
/// Prototypes
Prototype char *ConfigFilename(void);
Prototype int AlertUser (char *body, char *gad, ...);
Local     int EasyIntuitionRequest (char *body, char *gad, void *va);
Prototype char *ErrorMessage (void);
Prototype ULONG AlertF (char *fmt, ... );
///
/// Global variables
struct Window *ReqWindow = NULL;
///

/// ConfigFilename
char *ConfigFilename(void)
{
  const name = "ENV:Tag-o-Miga/Config";

  return name;
}
///
/// AlertUser
/* ------------------------------ AlertUser ------------------------------

 Comment:

*/

int AlertUser (char *body, char *gad, ...)
{
    //struct bguiRequest req = {
    //    BREQF_CENTERWINDOW|BREQF_LOCKWINDOW|BREQF_AUTO_ASPECT|BREQF_FAST_KEYS,
    //    "Tag-o-Miga", NULL, NULL, 0, NULL, '_', 0, 0, 0, NULL, 0, 0, 0, 0
    //};

    va_list va;
    int n;

    va_start(va, gad);

    /* if (ReqWindow == NULL) */ n = EasyIntuitionRequest (body, gad, va);
    /* else
        req.br_TextFormat = body;
        req.br_GadgetFormat = gad;
        n = BGUI_RequestA (ReqWindow, &req, va);
    */

    va_end(va);

    return n;
}
///
/// EasyIntuitionRequest
/* --------------------------- EasyIntuitionRequest ----------------------------

 Comment:

*/

int
EasyIntuitionRequest(char *body, char *gad, void *va)
{
    struct Process *proc;
    struct Window *win;
    struct EasyStruct es = {
        sizeof (struct EasyStruct), 0,
        "Tag-o-Miga", NULL, NULL };
    char ngad[256];
    int p, q;

    for (p = 0, q = 0; gad[p] != 0; p++)
        if (gad[p] != '_') ngad[q++] = gad[p];
    ngad[q] = 0;

    es.es_TextFormat = body;
    es.es_GadgetFormat = ngad;
    proc = (struct Process *)FindTask (0L);
    win = proc->pr_WindowPtr;

    return EasyRequestArgs (win, &es, NULL, va);
}
///
/// ErrorMessage
/* --------------------------------- ErrorMessage ---------------------------------

 Comment: return an error string derived from IoErr()

*/

char *ErrorMessage ()
{
    static char errmsg[256];

    Fault (IoErr (), "DOS error", errmsg, 256);

    return errmsg;
}
///
/// AlertF (char *fmt, ... )
ULONG AlertF (char *fmt, ... )
{
    va_list va;
    ULONG ret, count1 = 0, count2 = 0;
    UBYTE y = 12;
    char buf1[512];
    char buf2[512];

    va_start (va, fmt);
    vsprintf (buf1, fmt, va);

    do {
        buf2[count2++] = 0;
        buf2[count2++] = 16;
        buf2[count2++] = y;
        y += 10;
        while (buf1[count1] != '\0' && buf1[count1] != '\n') {
            buf2[count2++] = buf1[count1++];
        }
        count1++;
        buf2[count2++] = 0;
        buf2[count2++] = ~0;
    } while (buf1[count1] != '\0');
    buf2[count2-1] = 0;

    ret = DisplayAlert (RECOVERY_ALERT, buf2, y);

    va_end (va);

    return ret;
}
///


