#include <errno.h>
#include <newt.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "intl.h"
#include "windows.h"

void errorWindow(char * str) {
    char * a;

    a = alloca(strlen(str) + 5);
    strcpy(a, str);
    strcat(a, ": %s");

    newtWinMessage(_("Error"), _("Ok"), str, strerror(errno));
}

void winStatus(int width, int height, char * title,
		char * text, ...) {
    newtComponent t, f;
    char * buf = NULL;
    int size = 0;
    int i = 0;
    va_list args;
    int len = 0;
    int MAX_WIDTH = 50;

    va_start(args, text);

    do {
	size += 1000;
	if (buf) free(buf);
	buf = malloc(size);
	i = vsnprintf(buf, size, text, args);
    } while (i == size);
   
    len = strlen(buf);
    if (len > MAX_WIDTH) {
        width = MAX_WIDTH + 3;
        height = (len / MAX_WIDTH) + 3;
    } else {
        width = len + 3;
        height = 3;
    }

    va_end(args);

    newtCenteredWindow(width, height, title);

    t = newtTextbox(1, 1, width - 2, height - 2, NEWT_TEXTBOX_WRAP);
    newtTextboxSetText(t, buf);
    f = newtForm(NULL, NULL, 0);

    free(buf);

    newtFormAddComponent(f, t);

    newtDrawForm(f);
    newtRefresh();
    newtFormDestroy(f);
}
