/* Copyright 1996 Acorn Computers Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 * Title: dboxquery.c
 * Purpose: Access to Archimedes keyboard under the Wimp.
 * Author: W. Stoye
 * Status: Arthur-specific
 * History:
 *   13-Oct-87: started
 *   8-Jan-88: AFP converted to C.
 *   17-Nov-88: WRS: converted to keep the dbox static,
 *      to prevent embarassment when malloc fails.
 *      dboxquery(0) will initialise.
 *   02-Jul-91: IDJ: added int. help
 *   01-Aug-91: IDJ: added dboxquery_close and dboxquery_quit
 */

#define BOOL int
#define TRUE 1
#define FALSE 0

#include <stdio.h>
#include "h.dbox"
#include "h.dboxquery"
#include "h.help"

/* The query dialog box: */

#define dboxquery_FYes 0  /* action */
#define dboxquery_FMsg 1  /* string output */
#define dboxquery_FNo  2  /* action */

/* 17-Nov-88: WRS: changed so that the box is present all the time.
Otherwise, a lack of space can prevent you from asking the
question "do you want to quit". */
static dbox q = 0;
static dbox q_close = 0;
static dbox q_quit = 0;

static dboxquery_REPLY dboxquery__box(char *question, char *dbox_name, char *help_tag,
                                      dbox *qq)
{
dbox_field answer;

if (*qq == 0 || question == 0) *qq = dbox_new(dbox_name);
if (*qq == 0) return dboxquery_CANCEL; /* out of space - embarassing */
if (question == 0 || question[0] == 0) return dboxquery_CANCEL;

dbox_raw_eventhandler(*qq, help_dboxrawevents, help_tag);
dbox_setfield(*qq, 1, question);
dbox_show(*qq);
answer = dbox_fillin(*qq);
dbox_hide(*qq);

switch (answer)
    {
    case dboxquery_FYes: return dboxquery_YES;
    case dboxquery_FNo : return dboxquery_NO;
    default            : return dboxquery_CANCEL;
    }
}


dboxquery_REPLY dboxquery(char *question)
{
   return dboxquery__box(question, "query", "QUERY", &q);
}


dboxquery_close_REPLY dboxquery_close(char *question)
{
   switch(dboxquery__box(question, "close", "CLOSE", &q_close))
   {
      case dboxquery_YES:
         return dboxquery_close_SAVE;

      case dboxquery_NO:
         return dboxquery_close_DISCARD;

      default:
         return dboxquery_close_CANCEL;
   }
}

dboxquery_quit_REPLY dboxquery_quit(char *question)
{
   switch(dboxquery__box(question, "quit", "QUIT", &q_quit))
   {
      case dboxquery_YES:
         return dboxquery_quit_DISCARD;

      default:
         return dboxquery_quit_CANCEL;
   }
}

/* end */