/******************************************************************************/
/*                                                                            */
/*  PROG_WIN - Progress window stuff                                          */
/*                                                                            */
/******************************************************************************/

#define STRICT
#include <windows.h>
#include <commctrl.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include "const.h"
#include "classes.h"
#include "funcdefs.h"
#include "vortex.rh"
#include "extern.h"

static char wait_title[256] ;

static HCURSOR hCurs = NULL ;
static HWND hwndProgbar = NULL ;

extern "C" {
   BOOL CALLBACK DialogWaitMessage ( HWND hdwnd , UINT message ,
                                     WPARAM wParam , LPARAM /* lParam */ ) ;
   } ;

void begin_wait_message ( char *msg , int ok_to_abort )
{
   if (hwndWaitMessage) // This should NEVER happen if careful coding elsewhere
      return ;          // But play it safe anyway because nesting possible

   strcpy_s ( wait_title , msg ) ;

   if (ok_to_abort) {
      hwndWaitMessage = CreateDialog ( hInst , "DialogBoxWaitMessage" ,
                                       hwndFrame , (DLGPROC) DialogWaitMessage ) ;
      SetCapture ( hwndWaitMessage ) ;
      hCurs = LoadCursor ( NULL , IDC_WAIT ) ;
      SetCursor ( hCurs ) ;
      hwndProgbar = GetDlgItem ( hwndWaitMessage , IDC_WAIT_MESSAGE_PROGRESS ) ;
      SendMessage ( hwndProgbar , PBM_SETRANGE , 0 , MAKELPARAM ( 0 , 100 ) ) ;
      SendMessage ( hwndProgbar , PBM_SETPOS , 0 , 0 ) ;
      }
   else {
      hwndWaitMessage = CreateDialog ( hInst , "DialogBoxWaitMessageNoAbort" ,
                                       hwndFrame , (DLGPROC) DialogWaitMessage ) ;
      SetCapture ( hwndWaitMessage ) ;
      hCurs = LoadCursor ( NULL , IDC_WAIT ) ;
      SetCursor ( hCurs ) ;
      hwndProgbar = NULL ;
      }

   ShowWindow ( hwndWaitMessage , SW_SHOW ) ;

   return ;
}

void setpos_wait_message ( double pos )
{
   int ipos ;

   if ((hwndWaitMessage == NULL)  ||  (hwndProgbar == NULL))
      return ;

   ipos = (int) (100.0 * pos + 0.5) ;
   if (ipos < 0)
      ipos = 0 ;
   if (ipos > 100)
      ipos = 100 ;

   SendMessage ( hwndProgbar , PBM_SETPOS , (WPARAM) ipos , 0 ) ;
}

void end_wait_message ()
{
   if (hwndWaitMessage == NULL)
      return ;

   if (hCurs != NULL) {
      hCurs = LoadCursor ( NULL , IDC_ARROW ) ;
      SetCursor ( hCurs ) ;
      ReleaseCapture () ;
      hCurs = NULL ;
      }

   DestroyWindow ( hwndWaitMessage ) ;  // Nonmodal dialog requires this
   hwndWaitMessage = NULL ;
   return ;
}


/*
--------------------------------------------------------------------------------

   Check for the user pressing the ESCape key

--------------------------------------------------------------------------------
*/

int user_pressed_escape ()
{
   int k ;
   MSG msg ;

   while (PeekMessage ( &msg , 0 , 0 , 0 , PM_REMOVE )) {
      if (msg.message == WM_CHAR) {
         if ((TCHAR) msg.wParam == KEY_ESCAPE) // Check for user pressing ESCape
            escape_key_pressed = ERROR_ESCAPE ;
         }
      TranslateMessage ( &msg ) ;
      DispatchMessage ( &msg ) ;
      }

   if (escape_key_pressed) {
      k = escape_key_pressed ;
      escape_key_pressed = 0 ;
      return k ;
      }

   return 0 ;
}

void reset_user_abort ()
{
   escape_key_pressed = 0 ;
}

/*
--------------------------------------------------------------------------------

   Callback for DialogWaitMessage

--------------------------------------------------------------------------------
*/

extern "C"
BOOL CALLBACK DialogWaitMessage ( HWND hdwnd , UINT message ,
                                  WPARAM wParam , LPARAM /* lParam */ )
{
   int id ;
   switch ( message ) {
      
      case WM_INITDIALOG:
         SetDlgItemText ( hdwnd , IDC_WAIT_MESSAGE_MSG , wait_title ) ;
         return TRUE ;    // Lets Windows set initial control focus
         
      case WM_COMMAND:
         id = LOWORD ( wParam ) ;
         switch ( id ) {
            case IDCANCEL:
               if (modal_dialog_is_active)  // Do not allow cancel if modal dlg
                  return TRUE ;
               escape_key_pressed = ERROR_ESCAPE ;
               DestroyWindow ( hdwnd ) ;  // Nonmodal dialog requires this
               hwndWaitMessage = NULL ;
               return TRUE ;
            }
      }
      
   return FALSE ;  // Tell Windows we did not process this message
}
