/******************************************************************************/
/*                                                                            */
/*  PROGRESS - Display wait message with progress bar and abort button        */
/*                                                                            */
/*  It is important that number-crunchers regularly call user_pressed_escape  */
/*  in order to avoid hanging the system.                                     */
/*                                                                            */
/******************************************************************************/

#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 <malloc.h>
#include <new.h>
#include <float.h>

#include "const.h"
#include "classes.h"
#include "extern.h"
#include "funcdefs.h"
#include "julia.rh"

static char progress_title[256] ;
static HWND hwndProgbar = NULL ;

void begin_progress_message ( char *msg )
{
   if (hwndProgress) // This should NEVER happen if careful coding elsewhere
      return ;       // But play it safe anyway because nesting possible

   strcpy_s ( progress_title , msg ) ;

   hwndProgress = CreateDialog ( hInst , "DialogBoxProgress" ,
                                 hwndMain , DialogProgress ) ;
   assert ( hwndProgress != NULL ) ;
   hwndProgbar = GetDlgItem ( hwndProgress , IDC_PROGRESS_BAR ) ;
   assert ( hwndProgbar != NULL ) ;
   SendMessage ( hwndProgbar , PBM_SETRANGE , 0 , MAKELPARAM ( 0 , 100 ) ) ;
   SendMessage ( hwndProgbar , PBM_SETPOS , 0 , 0 ) ;
   ShowWindow ( hwndProgress , SW_SHOW ) ;
   return ;
}

void end_progress_message ()
{
   if (hwndProgress == NULL)
      return ;

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

void setpos_progress_message ( double pos )
{
   int ipos ;

   if ((hwndProgress == 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 ) ;
}

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

   Callback for DialogProgress

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

extern "C"
INT_PTR CALLBACK DialogProgress ( HWND hdwnd , UINT message ,
                               WPARAM /* wParam */ , LPARAM /* lParam */ )
{
   switch ( message ) {
      
      case WM_INITDIALOG:
         SetDlgItemText ( hdwnd , IDC_PROGRESS_MSG , progress_title ) ;
         return TRUE ;    // Lets Windows set initial control focus

#if 0         
      case WM_COMMAND:
         id = LOWORD ( wParam ) ;
         switch ( id ) {
            case IDCANCEL:
               if (HelpDialogIsActive)  // Do not allow cancel during modal dlg
                  return TRUE ;
               DestroyWindow ( hdwnd ) ;  // Nonmodal dialog requires this
               hwndProgress = NULL ;
               return TRUE ;
            }
#endif
      }
      
   return FALSE ;  // Tell Windows we did not process this message
}

