/* 
    ORPP Motion Controller Server

    2005 Jason Hunt
    huntjas2@msu.edu

    file: display.cpp
*/

#include <windows.h>
#include <stdio.h>
#include "display.h"

extern HWND main_win;

char text_area[text_y_size][text_x_size]; // The data for text display
bool display_reset = false;               // True if the display has been cleared


/* Reset the text area */
static void clear_text_area( void )
{
    memset(text_area, ' ', text_y_size * text_x_size);
    display_reset = true;
}
/* End of clear text area */


/* Adds a line to the text buffer */
void add_line(char *line)
{
    static int text_y = 0;

    // clear the buffer if this is the first call
    if (!display_reset) clear_text_area();

    // scrool the display up
    if (text_y == text_y_size)
        for (int i = 1; i < text_y_size; i++)
            memcpy(text_area[i-1], text_area[i], text_x_size);
    else
        text_y++; // next line

    strcpy(text_area[text_y-1], line);

    // force paint
    InvalidateRect (main_win, NULL, FALSE);
}
/* End of add line */


/* Show the text buffer on the screen */
void display_text(void)
{
    PAINTSTRUCT ps;

    HDC hdc = BeginPaint(main_win, &ps);

    SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));

    RECT r;
    GetClientRect(main_win, &r);

    Rectangle(hdc, 0, 0, text_x_size * font_x_size, text_y_size * font_y_size + 2);
    
    for (int r = 0; r < text_y_size; r++)
        TextOut(hdc, 1, r * font_y_size + 1, text_area[r], strlen(text_area[r]));

    ValidateRect(main_win, NULL);
    
    EndPaint(main_win, &ps);    
}
/* End of display_text_area */






