/******************************************************************************/
/*                                                                            */
/*  MAGNIFY - All routines, including DialogMagnify callback, for magnify     */
/*                                                                            */
/******************************************************************************/

#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"

void show_magnif ()
{
   int row_index, col_index, idwell ;
   char msg[256] ;

   sprintf_s ( msg , "%.10lf" , MagMinR ) ;
   SetDlgItemText ( hwndMagnify , IDC_MAGNIFY_REAL_LOWER , msg ) ;
   sprintf_s ( msg , "%.10lf" , MagMaxR ) ;
   SetDlgItemText ( hwndMagnify , IDC_MAGNIFY_REAL_UPPER , msg ) ;
   sprintf_s ( msg , "%.10lf" , MagMinI ) ;
   SetDlgItemText ( hwndMagnify , IDC_MAGNIFY_IMAG_LOWER , msg ) ;
   sprintf_s ( msg , "%.10lf" , MagMaxI ) ;
   SetDlgItemText ( hwndMagnify , IDC_MAGNIFY_IMAG_UPPER , msg ) ;

   MagCentR = 0.5 * (MagMinR + MagMaxR) ;
   MagCentI = 0.5 * (MagMinI + MagMaxI) ;
   MagMagR = 2. / (MagMaxR - MagMinR) ;
   MagMagI = 2. / (MagMaxI - MagMinI) ;

   sprintf_s ( msg , "%.10lf" , MagCentR ) ;
   SetDlgItemText ( hwndMagnify , IDC_MAGNIFY_REAL_CENTER , msg ) ;
   sprintf_s ( msg , "%.8le" , MagMagR ) ;
   SetDlgItemText ( hwndMagnify , IDC_MAGNIFY_REAL_MAGNIF , msg ) ;
   sprintf_s ( msg , "%.10lf" , MagCentI ) ;
   SetDlgItemText ( hwndMagnify , IDC_MAGNIFY_IMAG_CENTER , msg ) ;
   sprintf_s ( msg , "%.8le" , MagMagI ) ;
   SetDlgItemText ( hwndMagnify , IDC_MAGNIFY_IMAG_MAGNIF , msg ) ;

   col_index = (int) ((MagCentR - MinR) / (MaxR - MinR) * (ColResolution-1) + 0.5) ;
   row_index = (int) ((MaxI - MagCentI) / (MaxI - MinI) * (RowResolution-1) + 0.5) ;
   idwell = dwells[row_index*ColResolution+col_index] ;
   SetDlgItemInt ( hwndMagnify , IDC_MAGNIFY_DWELL , idwell , FALSE ) ;
}

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

   Callback for DialogMagnify

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

extern "C"
INT_PTR CALLBACK DialogMagnify ( HWND hdwnd , UINT message ,
                              WPARAM wParam , LPARAM /* lParam */ )
{
   int id ;
   switch ( message ) {
      
      case WM_INITDIALOG:
         MagCentR = CentR ;  // Init rectangle to be at same center, smaller
         MagCentI = CentI ;
         MagMagR = 4.0 * MagR ;
         MagMagI = 4.0 * MagI ;
         MagMinR = MagCentR - 1. / MagMagR ;
         MagMinI = MagCentI - 1. / MagMagI ;
         MagMaxR = MagCentR + 1. / MagMagR ;
         MagMaxI = MagCentI + 1. / MagMagI ;
         // The next statement allows show_magnif() to set displays
         // even though CreateDialog has not yet returned to set hwndMagnify.
         hwndMagnify = hdwnd ;
         show_magnif () ;
         return TRUE ;    // Lets Windows set initial control focus

      case WM_COMMAND:
         id = LOWORD ( wParam ) ;
         switch ( id ) {
            case IDOK:
               MinR = MagMinR ;
               MaxR = MagMaxR ;
               MinI = MagMinI ;
               MaxI = MagMaxI ;
               CentR = MagCentR ;
               MagR = MagMagR ;
               CentI = MagCentI ;
               MagI = MagMagI ;
               if (DisplayedObjectType == 1) {     // If magnifying Mandelbrot, set Julia param
                  JuliaR = CentR ;
                  JuliaI = CentI ;
                  }
               DestroyWindow ( hdwnd ) ;  // Nonmodal dialog requires this
               hwndMagnify = NULL ;
               return TRUE ;
            case IDCANCEL:
               DestroyWindow ( hdwnd ) ;  // Nonmodal dialog requires this
               hwndMagnify = NULL ;
               return TRUE ;
            }
      }
      
   return FALSE ;  // Tell Windows we did not process this message
}

