//
// Copyright (c) 1997 Jean-Marc Zucconi
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer
//    in this position and unchanged.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
// 3. The name of the author may not be used to endorse or promote products
//    derived from this software without specific prior written permission
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
#include <c++tk.h>
#include <stdio.h>

#include "cd-write.h"

static Label Elabel, Hlabel;
void
Error (char *s)
{
    TkBell ();
    if (*TkWinfo ("exists", ".error") == '0') {
	TkToplevel (".error -class Dialog");
	TkWm ("geometry .error", "-10+10");
	TkWm ("title .error", "Error");
	Elabel = TkLabel (".error.msg -wraplength 4i -justify left -text {", s, "}");
	TkPack (".error.msg -side top -padx 1c -pady 1c");
	TkButton (".error.b", "-text Dismiss -command {destroy .error}");
	TkPack (".error.b", "-side top -padx 10 -pady 10 -fill both"); 
    } else {
	char *p = new char [strlen(s)+20];
	sprintf (p, "-text {%s}", s);
	Elabel.configure (p);
	delete [] p;
    }
}
static ScrolledListBox info;
static char str[5000];
static int istr;
static Button button;
static Meter pm;
static int ptot, p100;

void
start_info ()
{
    if (*TkWinfo ("exists", ".info") == '0') {
	TkToplevel (".info");
	TkWm ("geometry .info", "-10+10");
	TkWm ("title .info", "Info");
	pm = tixMeter (".info.m -text { }");
	TkPack (".info.m", "-side top");
	info = tixScrolledListBox(".info.f", "-relief raised -bd 1 -scrollbar auto \
                 -options {listbox.width 60 listbox.height 15}");
	button = TkButton  (".info.b", "-text Dismiss -command {destroy .info} -state disabled");
	TkPack (".info.f .info.b", "-side top -expand 1 -fill both");
	TkWait ("visibility", ".info"); 
    } else {
	info.subwidget ("listbox", "delete 0 end");
	button.configure ("-state disabled");
    }
    istr = 0;
}
void
Info (char *s, int l)
{
    char *q;
    int nl = 0;

    if (*TkWinfo ("exists", ".info") == '0') {
	start_info ();
	button.configure ("-state normal");
    }
    q = &str[istr];
    if (istr == 0) {
	*q++ = '{';
	istr++;
    }
    if (l == -1) {
	nl = 1;
	l = strlen (s);
    }
    while (l--) {
	if (*s == '\n') {
	    *q++ = '}';
	    *q = 0;
	    info.subwidget ("listbox insert end", str);
	    info.subwidget ("listbox", "see end");
	    TkUpdate ("idletasks");
	    q = str;
	    istr = 1;
	    *q++ = '{';
	    s++;
	} else {
	    istr++;
	    *q++ = *s++;
	}
    }
    if (nl)
	Info ("\n", 1);
}
void
end_info ()
{
    if (istr > 1) {
	str[istr++] = '}';
	str[istr] = 0;
	info.subwidget ("listbox insert end", str);
	info.subwidget ("listbox", "see end");
	TkUpdate ("idletasks");
    }
    button.configure ("-state normal");
}
void 
copy_progress_start (int max)
{
    ptot = max;
    p100 = -1;
    if (*TkWinfo ("exists", ".info") == '0')
	start_info ();
    pm.configure("-text {} -value 0");
    TkUpdate ();
}
void
copy_progress_msg (int n)
{
    float x = ptot;
    char s[30];
    int p = int(100*(1.-n/x));
    if (p == p100)
	return;
    p100 = p;
    sprintf (s, "-value %g",1.-n/x);
    pm.configure(s);
    TkUpdate ();
}
void
help (int argc, char **argv)
{
    char *p = (char *) atoi (argv[0]);
    if (*TkWinfo ("exists", ".help") == '0') {
	TkToplevel (".help -class Dialog");
	TkWm ("title .help", "Help");
	Hlabel = TkLabel (".help.msg -wraplength 4i -justify left -text", p);
	TkPack (".help.msg -side top -padx 1c -pady 1c");
	TkButton (".help.b", "-text Dismiss -command {destroy .help}");
	TkPack (".help.b", "-side top -padx 10 -pady 10 -fill both"); 
    } else 
	Hlabel.configure ("-text", p);
}
char *
ADDR (char *s)
{
    static char p[10];
    sprintf (p, "%d", s);
    return p;
}
