/*******************************************
**                                        **
**           NUCGUI version 2             **
** -------------------------------------- **
** Classes for 13h gfx controls in dos    **
**                                        **
** requires video.h                       **
**                                        **
*******************************************/

#include "c:\headers\video.h"

#define _HIRESGFX_H_

#define MaxX 320
#define MaxY 200
#define blight 254
#define wlight 252
#define bshadow 255
#define wshadow 253
#define loopdelay 1000000

extern VGA *video;
extern long i;

class BUTTON {
private:
    int x,y;
    int xsz, ysz;
    int bttncolor;
    char* caption;
    int border;

    int startx, starty, endx, endy;

public:

    BUTTON(char* cap, int xpos, int ypos, int xlen, int ylen, int col, int bor);  //constructor
    void Show();
    void Pressed();
};

// window class
class WINDOW {
private:
    int x,y,xsz,ysz;
    int startx, endx, starty, endy;
    char* caption;
    int border;
    int color, titlecolor;

public:
    WINDOW(char* cap, int x, int y, int xsz, int ysz, int col, int tcol, int bor);
    void Show();
};


// ******************* IMPLEMENTATIONS ***********************

BUTTON::BUTTON(char* cap,
               int xpos, int ypos,
               int xlen, int ylen,
               int col,
               int bor) {
    caption = cap;
    x=xpos;
    y=ypos;
    xsz=xlen;
    ysz=ylen;
    bttncolor=col;
    border=bor;
    startx=x-xsz/2;
    starty=y-ysz/2;
    endx=startx+xsz;
    endy=starty+ysz;

    video->SetPal(0,0,0, bshadow);
    video->SetPal(63,63,63, blight);
}

void BUTTON::Show() {
    for(i=0;i<ysz;i++) {
        video->HLine(startx, startx+xsz, starty+i, bttncolor, video->virt_scr);
    }

    if(border) {
        for(i=0;i<border;i++) {
            video->HLine(startx-i, endx+i, starty-i, blight, video->virt_scr);
            video->Line(startx-i, starty-i, startx-i, endy+i, blight, video->virt_scr);
            //shadow
            video->HLine(startx-i, endx+i, endy+i, bshadow, video->virt_scr);
            video->Line(endx+i, starty-i, endx+i, endy+i, bshadow, video->virt_scr);
        }
    }
    //correction
    for(i=0;i<border;i++) {
        video->PutPixel(endx+i, endy+i, wshadow, video->virt_scr);
    }
    video->ScrUpdate();
}

void BUTTON::Pressed() {
    video->SetPal(0,0,0, blight);
    video->SetPal(63,63,63, bshadow);
    for(i=0;i<loopdelay;i++);
    video->SetPal(63,63,63, blight);
    video->SetPal(0,0,0, bshadow);
}


// ========================= Window functions =======================

WINDOW::WINDOW(char* cap, int xc, int yc, int xszc, int yszc, int col, int tcol, int bor) {
    caption=cap;
    x=xc;
    y=yc;
    xsz=xszc;
    ysz=yszc;
    color=col;
    titlecolor=tcol;
    border=bor;

    startx = x - xsz/2;
    starty = y - ysz/2;
    endx = startx+xsz;
    endy = starty+ysz;

    video->SetPal(0,0,0, wshadow);
    video->SetPal(63,63,63, wlight);
}

void WINDOW::Show() {
    for(i=0;i<ysz;i++) {
        video->HLine(startx, endx, starty+i, color, video->virt_scr);
    }
    if(border) {
        for(i=0;i<border;i++) {
            video->HLine(startx-i, endx+i, starty-i, wlight, video->virt_scr);
            video->Line(startx-i, starty-i, startx-i, endy+i, wlight, video->virt_scr);
            //shadow
            video->HLine(startx-i, endx+i, endy+i, wshadow, video->virt_scr);
            video->Line(endx+i, starty-i, endx+i, endy+i, wshadow, video->virt_scr);
        }
    }

    //title bar
    for(i=0;i<5;i++) {
        video->HLine(startx + 1, endx-1, starty +1 + i, titlecolor, video->virt_scr);
    }

    //correction
    for(i=0;i<border;i++) {
        video->PutPixel(endx+i, endy+i, wshadow, video->virt_scr);
    }
    video->ScrUpdate();
}