/*
    ####             #    #     # #
    #   #            #    #       #          The FreeWare C library for 
    #   #  ##   ###  #  # #     # ###             RISC OS machines
    #   # #  # #     # #  #     # #  #   ___________________________________
    #   # ####  ###  ##   #     # #  #                                      
    #   # #        # # #  #     # #  #    Please refer to the accompanying
    ####   ### ####  #  # ##### # ###    documentation for conditions of use
    ________________________________________________________________________

    File:    Dialog.Wait4Click.c
    Author:  Copyright  1993 Tim Browse and Jason Williams
    Version: 1.00 (10 Jul 1993)
    Purpose: Very high level window (dialogue) handling -
             Processing events for dialogue windows
*/


#include "Desk.Wimp.h"
#include "Desk.WimpSWIs.h"

#include "Desk.Dialog.h"
#include "Desk.Event.h"


extern Desk_icon_handle Desk_Dialog_WaitForClick(dialog dbox)
/*  Wait for choice to be made by the user, or for the window to close.
 *  If user closes window by clicking outside, Desk_dialog_CLOSE is returned, else
 *  the icon which was clicked is returned.
 *  This function may be called repeatedly while you wait for a particular
 *  icon to be clicked.
 *
 *  Some care is taken to ensure that the 'dbox->lastchoice' value will
 *  always be a valid icon handle except when no icns have yet been clicked.
 *  (i.e. if you click icon 3, then close the window, lastchoice will be 3
 *  while this function will return Desk_dialog_CLOSE)
 */
{
  Desk_window_state wstate;
  Desk_icon_handle  choice, lastchoice;

  lastchoice = dbox->lastclicked;
  dbox->lastclicked = choice = Desk_dialog_NOCHOICE;

  do
  {
    Desk_Event_Poll();
    
    /*  If user has clicked outside window, hit return, or clicked the close
     *  box, causing it to close, we take that as a 'Cancel': Desk_dialog_CLOSE.
     */
    Desk_Wimp_GetWindowState(dbox->window, &wstate);
    if (wstate.flags.data.open)                /* Window has not been closed */
      choice = dbox->lastclicked;
    else
    {
      choice = Desk_dialog_CLOSE;
      dbox->state.persist = Desk_bool_FALSE;
      dbox->state.stillopen =Desk_bool_FALSE;
    }

  } while (choice == Desk_dialog_NOCHOICE);    /* Repeat until close or click     */

  if (dbox->lastclicked < 0)              /* Ensure return last clicked icon */
    dbox->lastclicked = lastchoice;       /* when window is closed etc       */

  return(choice);
}

