/*
**    Name: chcs.c
**
**    Date: Fri Nov  7 14:58:02 2003
**
*/

#include "odbcdm.h"
#include "support.h"
#include "chcs.h"
#include "Choices.h"
#include "window.h"
#include "stringset.h"
#include "numberrange.h"
#include <stdio.h>

/* Create window if not open already */

toolbox_o choices_window = NULL;

osbool choices_create(void)
    {
    if (choices_window == NULL)
        {

/* Create window and attach handlers */

        REPERRTRUE(xtoolbox_create_object((toolbox_create_flags)0,
                                             (toolbox_id)"Choices",
                                             &choices_window))

        ERRFALSETRUE(event_register_toolbox_handler(choices_window,
                                               action_WINDOW_DIALOGUE_COMPLETED,
                                               choices_window_dialogue_completed,
                                               NULL))

        ERRFALSETRUE(event_register_wimp_handler(choices_window,
                                               wimp_CLOSE_WINDOW_REQUEST,
                                               choices_close_window,
                                               NULL))

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

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

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

        REPERRTRUE(xstringset_set_selected_index((bits)0,
                                      choices_window,
                                      (toolbox_c)CHOICES_NICE,
                                      choices.nice))

        REPERRTRUE(xnumberrange_set_value((bits)0,
                                             choices_window,
                                             (toolbox_c)CHOICES_QUITTIME,
                                             choices.quittime))

/* Register OK button handler */

        ERRFALSETRUE(event_register_toolbox_handler(
                       choices_window,
                       choices_OK,
                       choices_ok,
                       NULL))
        }

    return FALSE;
    }


osbool choices_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,
                                    choices_window))


    return TRUE;
    }


/* Window has been hidden */

osbool choices_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 (choices_window != NULL)
        {
        event_deregister_wimp_handler(choices_window,
                                      wimp_CLOSE_WINDOW_REQUEST,
                                      choices_close_window,
                                      NULL);

        event_deregister_toolbox_handler(
            choices_window,
            action_WINDOW_DIALOGUE_COMPLETED,
            choices_window_dialogue_completed,
            NULL);

/* Deregister OK button handler */

        event_deregister_toolbox_handler(
                       choices_window,
                       choices_OK,
                       choices_ok,
                       NULL);

        REPERRTRUE(xtoolbox_delete_object((toolbox_delete_flags)0,
                                          choices_window))

        choices_window = NULL;
        }

    return TRUE;
    }


/* OK clicked */

osbool choices_ok(bits event_code,
                toolbox_action *action,
                toolbox_block *block,
                void *handle)
    {
    FILE *ch;

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

    if (choices_window != NULL)
        {
        REPERRTRUE(xstringset_get_selected_index((bits)0,
                                      choices_window,
                                      (toolbox_c)CHOICES_NICE,
                                      &choices.nice))

        REPERRTRUE(xnumberrange_get_value((bits)0,
                                             choices_window,
                                             (toolbox_c)CHOICES_QUITTIME,
                                             &choices.quittime))


        if ((ch = fopen("<Choices$Write>.ODBC.ODBC","w")) != NULL)
            {
            fprintf(ch,"QuitTime:%d\n",choices.quittime);
            fprintf(ch,"Nice:%d\n",choices.nice);
            fclose(ch);
            }

        choices.nicecount = choices.nice;
        }

    return TRUE;
    }
