// status_b window - to show what's happening, and to allow download process to be cancelled!


#include <string.h>

#include <math.h>


#ifndef GLOBAL_DECL

#include "global.h"     // Global data types and variables

#endif


config*     settings_b; // Pointer to program settings object

statuswnd*  status_b;   // Pointer to the status window object


LRESULT CALLBACK WndStatProc(HWND, UINT, WPARAM, LPARAM);   // Window procedure


HWND hCancel;                                               // Cancel button handle

HWND hstatusText;                                           // status text handle


statuswnd::statuswnd(config* setts)
{
    settings_b = setts;
    status_b = this;

    show_status = settings_b -> show_status;

    lock_sema = CreateSemaphore( 0, 1, 1, 0 );
    
    strcpy(szStAppName, "status");                           // Set Window Name

    strcpy(szStCaption, "Web News Speak - Download status"); // Set Window Caption


    status_head = 0;

    create();
}

void statuswnd::create()
{
    WNDCLASSEX  wndclass;
    
    // Define class features

    wndclass.cbSize        = sizeof (wndclass);
    wndclass.style         = CS_NOCLOSE;
    wndclass.lpfnWndProc   = WndStatProc;

    wndclass.cbClsExtra    = 0;
    wndclass.cbWndExtra    = 0;
    wndclass.hInstance     = hInst;

    wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION);
    wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW);

    wndclass.lpszMenuName  = NULL;
    wndclass.lpszClassName = szStAppName;
    wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION);

    // Register the class with Windows

    RegisterClassEx (&wndclass);

    hwndStat = CreateWindow
                  (szStAppName,             // window class name

                   szStCaption,             // window caption

                   WS_OVERLAPPED,           // window style


                   50,                      // x position on screen

                   50,                      // y position on screen

                   450,                     // width

                   300,                     // height


                   hwndMain,                // parent window handle

                   NULL,                    // window menu handle

                   hInst,                   // program instance handle

                   NULL);                   // creation parameters

}

void statuswnd::activate()
{
    int loop;

    show_status = settings_b -> show_status;

    for (loop=0; loop<STAT_LINES; loop++)
        status_lines[loop][0] = NULL;

    status_text[0] = NULL;

    if (!show_status)
        strcpy(status_text, "\n\nWeb News Speak\n\n\n\nDownloading, please wait..\n\n");

    SetWindowText(hCancel, "Cancel");

    ShowWindow (hwndStat, SW_SHOWNORMAL);
    UpdateWindow (hwndStat);
}

void statuswnd::deactivate()
{
    ShowWindow(hwndStat, SW_HIDE);
}

void statuswnd::finished()
{
    if (show_status)
        addline("Completed downloading pages.");
    else
    {
        strcpy(status_text, "\n\nWeb News Speak\n\n\n\nDownloading, please wait..\n\nCompleted Downloading pages.");
        SendMessage ( hwndStat, WM_PAINT, 0, 0 );
    }
}

void statuswnd::cancelled()
{
    if (show_status)
        addline("Download Cancelled.");
    else
    {
        strcpy(status_text, "\n\nWeb News Speak\n\n\n\nDownloading, please wait..\n\nDownload cancelled.");
        SendMessage ( hwndStat, WM_PAINT, 0, 0 );
    }

    SetWindowText(hCancel, "Close");
}

void statuswnd::addline(char* line)
{
    int loop, latest;

    if (show_status)
    {
        WaitForSingleObject(lock_sema, INFINITE);

        strcpy(status_lines[status_head], line);

        latest = status_head;

        status_head++;
        if (status_head >= STAT_LINES) status_head-=STAT_LINES;

        loop = status_head;

        strcpy(status_text, status_lines[loop]);
        strcat(status_text, "\n");

        do
        {
            loop++;
            if (loop >= STAT_LINES) loop-=STAT_LINES;
            strcat(status_text, status_lines[loop]);
            strcat(status_text, "\n");
        
        } while (loop != latest);


        SendMessage ( hwndStat, WM_PAINT, 0, 0 );

        ReleaseSemaphore(lock_sema, 1, 0);
    }
}

LRESULT CALLBACK WndStatProc
    (HWND hwndStat, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
    HDC         hdc;        
    PAINTSTRUCT ps;
    static RECT rectText;

    switch (iMessage)
    {
        case WM_CREATE:
            hCancel = CreateWindow
                ("button",              // window class name

                 "Cancel",              // window caption

                 WS_CHILD |             // window style

                 WS_VISIBLE |
                 WS_BORDER |
                 BS_PUSHBUTTON,
                 150, 230, 130, 30,     // Position and Size

                 hwndStat,              // parent window handle

                 0,                     // Button identifier

                 hInst,                 // program instance handle

                 NULL);                 // creation parameters


            rectText.left   = 10;
            rectText.right  = 435;
            rectText.top    = 10;
            rectText.bottom = 220;
            return 0;
        case WM_SETFOCUS:
            // Ensure the cancel button always has the focus

            SetFocus(hCancel);
        case WM_PAINT:
            // Paint objects on the screen

            InvalidateRect( hwndStat, &rectText, TRUE ); // Make sure window is re-painted


            hdc = BeginPaint (hwndStat, &ps);

            // Draw rectangle around the status_b text

            Rectangle(hdc, rectText.left-3, rectText.top-1, rectText.right+2, rectText.bottom+1);
            // Set text to text color as set in control panel

            SetTextColor(hdc, COLOR_WINDOWTEXT);
            // Draw Title

            SetBkMode(hdc, OPAQUE);
            SelectObject(hdc, GetStockObject(SYSTEM_FONT));
            DrawText(hdc, status_b -> status_text, strlen(status_b -> status_text), &rectText, 0);

            // End painting

            DeleteObject(SelectObject(hdc, GetStockObject(SYSTEM_FONT)));
            EndPaint (hwndMain, &ps);
            return 0;

        case WM_COMMAND:
            if (LOWORD(wParam) == 0)
            {
                if ((settings_b -> downloading) && (!settings_b -> cancel))
                {
                    status_b -> addline("Cancelling downloads, please wait...");
                    settings_b -> cancel = true;
                }
                else if ((!settings_b -> downloading) && (!settings_b -> cancel))
                    ShowWindow (hwndStat, SW_HIDE); break;
            }
            return 0;
    }

    return DefWindowProc(hwndStat, iMessage, wParam, lParam);
}

syntax highlighted by Code2HTML, v. 0.8.11