/*
**    Name: status.c
**
**    Date: Sun Jul 20 08:21:09 2003
**
*/

#include "odbcdm.h"
#include "status.h"
#include "support.h"
#include "window.h"
#include <string.h>
#include <stdio.h>

/* Create window if not open already */

toolbox_o status_window = NULL;
toolbox_o status_toolbar = NULL;

osbool status_create(void)
    {
    os_box extent;                  /* Extent block */

    if (status_window == NULL)
        {

/* Create window and attach handlers */

        REPERRTRUE(xtoolbox_create_object((toolbox_create_flags)0,
                                             (toolbox_id)"Status",
                                             &status_window))

/* Set the window extent */

        extent.x0 = 0;
        extent.x1 = 904;
        extent.y1 = 0;
        extent.y0 = -(76 + (36 * status_count()));

        REPERRTRUE(xwindow_set_extent((bits)0,
                                  status_window,
                                  &extent))

        ERRFALSETRUE(event_register_toolbox_handler(status_window,
                                               action_WINDOW_DIALOGUE_COMPLETED,
                                               status_window_dialogue_completed,
                                               NULL))

        ERRFALSETRUE(event_register_wimp_handler(status_window,
                                               wimp_CLOSE_WINDOW_REQUEST,
                                               status_close_window,
                                               NULL))

        ERRFALSETRUE(event_register_wimp_handler(status_window,
                                               wimp_REDRAW_WINDOW_REQUEST,
                                               status_redraw_window_request,
                                               NULL))

/* Ensure client handle is set for this object */

         REPERRTRUE(xtoolbox_set_client_handle((toolbox_handle_flags)0,
                                                  status_window,
                                                  NULL))

/* Create a toolbar for this object */

        REPERRTRUE(xtoolbox_create_object((toolbox_create_flags)0,
                                          (toolbox_id)"Toolbar",
                                          &status_toolbar))

/* Now attach this toolbar to the main window */

        REPERRTRUE(xwindow_set_tool_bars(
                       (window_tool_bar_flags)window_TOOL_BAR_ITL,
                       status_window,
                       NULL,
                       status_toolbar,
                       NULL,
                       NULL))

        REPERRTRUE(xtoolbox_show_object((toolbox_show_flags)0,
                                           status_window,
                                           toolbox_POSITION_CENTRED,
                                           NULL,
                                           (toolbox_o)status_window,
                                           (toolbox_c)-1))
        }

    return FALSE;
    }


osbool status_close_window(wimp_event_no event_code,
                           wimp_block *poll_block,
                           toolbox_block *block,
                           void *handle)
    {
    NOT_USED(event_code);
    NOT_USED(poll_block);
    NOT_USED(block);
    NOT_USED(handle);

/* Hide window */

    REPERRTRUE(xtoolbox_hide_object((toolbox_hide_flags)0,
                                    status_window))


    return TRUE;
    }


/* Window has been hidden */

osbool status_window_dialogue_completed(bits event_code,
                                      toolbox_action *action,
                                      toolbox_block *block,
                                      void *handle)
    {
    NOT_USED(event_code);
    NOT_USED(action);
    NOT_USED(block);
    NOT_USED(handle);

    if (status_window != NULL)
        {
/* Remove this toolbar to the main window */

        REPERRTRUE(xwindow_set_tool_bars(
                       (window_tool_bar_flags)window_TOOL_BAR_ITL,
                       status_window,
                       NULL,
                       NULL,
                       NULL,
                       NULL))

        status_toolbar = NULL;

        event_deregister_wimp_handler(status_window,
                                     wimp_CLOSE_WINDOW_REQUEST,
                                     status_close_window,
                                     NULL);

        event_deregister_wimp_handler(status_window,
                                      wimp_REDRAW_WINDOW_REQUEST,
                                      status_redraw_window_request,
                                      NULL);

        event_deregister_toolbox_handler(
            status_window,
            action_WINDOW_DIALOGUE_COMPLETED,
            status_window_dialogue_completed,
            NULL);

        REPERRTRUE(xtoolbox_delete_object((toolbox_delete_flags)0,
                                          status_window))

        status_window = NULL;
        }

    return TRUE;
    }



/* Window requires redrawing */

osbool status_redraw_window_request(wimp_event_no event_code,
                                  wimp_block *poll_block,
                                  toolbox_block *block,
                                  void *handle)
    {
    osbool more;            /* Indicates more rectangles */

    int index,
        counter,
        ox,               /* Origin                    */
        oy;

    char text[256];

    wimp_draw redraw;     /* Redraw structure          */

    wimp_icon text_icon;

    NOT_USED(event_code);
    NOT_USED(block);
    NOT_USED(handle);

/* Get window details */

    redraw.w = poll_block->redraw.w;

    REPERRTRUE(xwimp_redraw_window(&redraw,&more));

/* Work out screen origin */

    ox = (redraw.box.x0 - redraw.xscroll);
    oy = (redraw.box.y1 - redraw.yscroll);

/* Text */

    text_icon.data.indirected_text.validation = "";
    text_icon.data.indirected_text.size       = 256;
    text_icon.data.indirected_text.text       = text;


    while (more)
        {
        if (riscosdlrefinit == 1)
            {
            counter = 0;
            index = 0;

            while (counter < 64)
                {
                if (riscosdlref[counter].count != 0)
                    {
                    text_icon.extent.y1 = -(72 + (index * 36));
                    text_icon.extent.y0 = text_icon.extent.y1 - 36;

                    text_icon.flags = (bits)(wimp_ICON_TEXT       |
                             wimp_ICON_VCENTRED   |
                             wimp_ICON_INDIRECTED |
                             (wimp_COLOUR_BLACK << wimp_ICON_FG_COLOUR_SHIFT) |
                             (wimp_COLOUR_WHITE << wimp_ICON_BG_COLOUR_SHIFT));

                    text_icon.extent.x0 = 4;
                    text_icon.extent.x1 = 676;

                    strcpy(text,riscosdlref[counter].dll);
                    xwimp_plot_icon(&text_icon);

                    text_icon.flags = (bits)(wimp_ICON_TEXT       |
                             wimp_ICON_VCENTRED   |
                             wimp_ICON_HCENTRED   |
                             wimp_ICON_INDIRECTED |
                             (wimp_COLOUR_BLACK << wimp_ICON_FG_COLOUR_SHIFT) |
                             (wimp_COLOUR_WHITE << wimp_ICON_BG_COLOUR_SHIFT));

                    text_icon.extent.x0 = 676;
                    text_icon.extent.x1 = 908;

                    sprintf(text,"%d",riscosdlref[counter].count);
                    xwimp_plot_icon(&text_icon);

                    index++;
                    }

                counter++;
                }

            if (index == 0)
                {
                strcpy(text,messagetrans_simplelookup("None"));

                text_icon.flags = (bits)(wimp_ICON_TEXT       |
                             wimp_ICON_VCENTRED   |
                             wimp_ICON_HCENTRED   |
                             wimp_ICON_INDIRECTED |
                             (wimp_COLOUR_BLACK << wimp_ICON_FG_COLOUR_SHIFT) |
                             (wimp_COLOUR_WHITE << wimp_ICON_BG_COLOUR_SHIFT));

                text_icon.extent.y1 = -72;
                text_icon.extent.y0 = text_icon.extent.y1 - 36;
                text_icon.extent.x0 = 4;
                text_icon.extent.x1 = 904;

                xwimp_plot_icon(&text_icon);
                }
            }
        else
            {
            strcpy(text,messagetrans_simplelookup("NotInit"));

            text_icon.flags = (bits)(wimp_ICON_TEXT       |
                             wimp_ICON_VCENTRED   |
                             wimp_ICON_HCENTRED   |
                             wimp_ICON_INDIRECTED |
                             (wimp_COLOUR_BLACK << wimp_ICON_FG_COLOUR_SHIFT) |
                             (wimp_COLOUR_WHITE << wimp_ICON_BG_COLOUR_SHIFT));

            text_icon.extent.y1 = -72;
            text_icon.extent.y0 = text_icon.extent.y1 - 36;
            text_icon.extent.x0 = 4;
            text_icon.extent.x1 = 904;

            xwimp_plot_icon(&text_icon);
            }

        xwimp_get_rectangle(&redraw,
                            &more);
        }

    return TRUE;
    }

osbool status_force_redraw(void)
    {
    wimp_window_info info;  /* Window info */

    static int old_extent = -1;

    int index;

/* Get window size */

    REPERRTRUE(xwindow_get_wimp_handle((bits)0,
                                       status_window,
                                       &info.w))

    REPERRTRUE(xwimp_get_window_info_header_only(&info))

    index = status_count();

    if (index != old_extent)
        {
/* Set the window extent */

        info.extent.y0 = -(76 + (36 * index));

        REPERRTRUE(xwindow_set_extent((bits)0,
                                  status_window,
                                  &info.extent))

        old_extent = index;
        }

    REPERRTRUE(xwindow_force_redraw((bits)0,
                                       status_window,
                                       &info.extent))

    return TRUE;
    }


int status_count(void)
    {
    int counter,
        index = 0;

/* Check number of loaded drivers */

    if (riscosdlrefinit == 1)
        {
        counter = 0;
        index = 0;

        while (counter < 64)
            {
            if (riscosdlref[counter].count != 0)
                {
                index++;
                }

            counter++;
            }

        if (index < 3)
            {
            index = 3;
            }
        }

    return index;
    }
