/******************************************************************************/
/*                                                                            */
/*  MESSAGES - Assorted small workers for message stuff                       */
/*                                                                            */
/*  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"

// These are in MAIN.CPP
extern HINSTANCE hInst ;             // Global copy of instance handle
extern HWND hwndMain ;               // Main window
extern HWND hwndWaitMessage ;        // In MESSAGES.CPP
extern int HelpDialogIsActive ;      // True when user selected a dialog box
extern int escape_key_pressed ;      // user_pressed_escape()

static char wait_title[256] ;

void display_error_message ( char *msg )
{
   MessageBox ( hwndMain , msg , "ERROR (Press <Enter> to continue)" ,
                MB_ICONSTOP | MB_OK ) ;
}

static HCURSOR hCurs = NULL ;

void begin_wait_message ( char *msg , int ok_to_abort )
{
   RECT rcNewClip ;

   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" ,
                                       hwndMain , DialogWaitMessage ) ;
      hCurs = NULL ;
      }
   else {
      hwndWaitMessage = CreateDialog ( hInst , "DialogBoxWaitMessageNoAbort" ,
                                       hwndMain , DialogWaitMessage ) ;
      GetWindowRect ( hwndWaitMessage , &rcNewClip ) ;
      SetCapture ( hwndWaitMessage ) ;
      hCurs = LoadCursor ( NULL , IDC_WAIT ) ;
      SetCursor ( hCurs ) ;
      }

   ShowWindow ( hwndWaitMessage , SW_SHOW ) ;

   return ;
}

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 ;
         else if ((TCHAR) msg.wParam == KEY_CTRLQ) // Or Ctrl-Q
            escape_key_pressed = ERROR_ABORT ;
         }
      TranslateMessage ( &msg ) ;
      DispatchMessage ( &msg ) ;
      }

   if (escape_key_pressed) {
      k = escape_key_pressed ;
      escape_key_pressed = 0 ;  // Prevents multiple unwanted escapes
      return k ;
      }

   return 0 ;
}

void reset_user_abort ()
{
   escape_key_pressed = 0 ;
}

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

   Callback for DialogWaitMessage

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

extern "C"
INT_PTR 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 (HelpDialogIsActive)  // Do not allow cancel during 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
}

