/******************************************************************************/
/*                                                                            */
/*  DLG_PARM - Dialog callback for parameters entry                           */
/*                                                                            */
/******************************************************************************/

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

extern "C"

INT_PTR CALLBACK DialogParams ( HWND hdwnd , UINT message ,
                             WPARAM wParam , LPARAM /* lParam */ )
{
   int id ;
   char msg[256] ;
   switch ( message ) {
      
      case WM_INITDIALOG:

         SetDlgItemInt ( hdwnd , IDC_ROWS , RowResolution , FALSE ) ;
         SetDlgItemInt ( hdwnd , IDC_COLS , ColResolution , FALSE ) ;

         if (ObjectType == 1  &&  DwellVsBinary)
            CheckRadioButton ( hdwnd , IDC_TYPE_MANDEL_DWELL , IDC_TYPE_J_QUATERNION , IDC_TYPE_MANDEL_DWELL ) ;
         else if (ObjectType == 1  &&  ! DwellVsBinary)
            CheckRadioButton ( hdwnd , IDC_TYPE_MANDEL_DWELL , IDC_TYPE_J_QUATERNION , IDC_TYPE_MANDEL_BINARY ) ;
         else if (ObjectType == 2  &&  DwellVsBinary)
            CheckRadioButton ( hdwnd , IDC_TYPE_MANDEL_DWELL , IDC_TYPE_J_QUATERNION , IDC_TYPE_JULIA_DWELL ) ;
         else if (ObjectType == 2  &&  ! DwellVsBinary)
            CheckRadioButton ( hdwnd , IDC_TYPE_MANDEL_DWELL , IDC_TYPE_J_QUATERNION , IDC_TYPE_JULIA_BINARY ) ;
         else if (ObjectType == 3)
            CheckRadioButton ( hdwnd , IDC_TYPE_MANDEL_DWELL , IDC_TYPE_J_QUATERNION , IDC_TYPE_M_QUATERNION ) ;
         else if (ObjectType == 4)
            CheckRadioButton ( hdwnd , IDC_TYPE_MANDEL_DWELL , IDC_TYPE_J_QUATERNION , IDC_TYPE_J_QUATERNION ) ;

         sprintf_s ( msg , "%.3lf" , BinaryThreshold ) ;
         SetDlgItemText ( hdwnd , IDC_BINARY_THRESHOLD , msg ) ;

         sprintf_s ( msg , "%.10lf" , JuliaR ) ;
         SetDlgItemText ( hdwnd , IDC_JULIA_REAL , msg ) ;
         sprintf_s ( msg , "%.10lf" , JuliaI ) ;
         SetDlgItemText ( hdwnd , IDC_JULIA_IMAG , msg ) ;

         SetDlgItemInt ( hdwnd , IDC_DWELL_LIMIT , (int) DwellLimit , FALSE ) ;
         sprintf_s ( msg , "%.2lf" , EscapeConstant ) ;
         SetDlgItemText ( hdwnd , IDC_ESCAPE_CONSTANT , msg ) ;

         if (LocateHow)
            CheckRadioButton ( hdwnd , IDC_LOCATE_CENTER , IDC_LOCATE_LIMITS , IDC_LOCATE_LIMITS ) ;
         else
            CheckRadioButton ( hdwnd , IDC_LOCATE_CENTER , IDC_LOCATE_LIMITS , IDC_LOCATE_CENTER ) ;

         sprintf_s ( msg , "%.10lf" , CentR ) ;
         SetDlgItemText ( hdwnd , IDC_CENTER_REAL , msg ) ;
         sprintf_s ( msg , "%.10lf" , CentI ) ;
         SetDlgItemText ( hdwnd , IDC_CENTER_IMAG , msg ) ;
         sprintf_s ( msg , "%.10lf" , MagR ) ;
         SetDlgItemText ( hdwnd , IDC_MAGNIF_REAL , msg ) ;
         sprintf_s ( msg , "%.10lf" , MagI ) ;
         SetDlgItemText ( hdwnd , IDC_MAGNIF_IMAG , msg ) ;
         sprintf_s ( msg , "%.10lf" , MinR ) ;
         SetDlgItemText ( hdwnd , IDC_MIN_REAL , msg ) ;
         sprintf_s ( msg , "%.10lf" , MinI ) ;
         SetDlgItemText ( hdwnd , IDC_MIN_IMAG , msg ) ;
         sprintf_s ( msg , "%.10lf" , MaxR ) ;
         SetDlgItemText ( hdwnd , IDC_MAX_REAL , msg ) ;
         sprintf_s ( msg , "%.10lf" , MaxI ) ;
         SetDlgItemText ( hdwnd , IDC_MAX_IMAG , msg ) ;

         if (TrueAspect)
            CheckDlgButton ( hdwnd , IDC_TRUE_ASPECT , TRUE ) ;
         else 
            CheckDlgButton ( hdwnd , IDC_TRUE_ASPECT , FALSE ) ;

         if (Collapse == 0)
            CheckRadioButton ( hdwnd , IDC_COLLAPSE_R , IDC_COLLAPSE_K , IDC_COLLAPSE_R ) ;
         else if (Collapse == 1)
            CheckRadioButton ( hdwnd , IDC_COLLAPSE_R , IDC_COLLAPSE_K , IDC_COLLAPSE_I ) ;
         else if (Collapse == 2)
            CheckRadioButton ( hdwnd , IDC_COLLAPSE_R , IDC_COLLAPSE_K , IDC_COLLAPSE_J ) ;
         else 
            CheckRadioButton ( hdwnd , IDC_COLLAPSE_R , IDC_COLLAPSE_K , IDC_COLLAPSE_K ) ;

         sprintf_s ( msg , "%.4lf" , Azimuth ) ;
         SetDlgItemText ( hdwnd , IDC_AZIMUTH , msg ) ;
         sprintf_s ( msg , "%.4lf" , Elevation ) ;
         SetDlgItemText ( hdwnd , IDC_ELEVATION , msg ) ;

         if (Lighting == 0)
            CheckRadioButton ( hdwnd , IDC_LIGHT_0 , IDC_LIGHT_4 , IDC_LIGHT_0 ) ;
         else if (Lighting == 1)
            CheckRadioButton ( hdwnd , IDC_LIGHT_0 , IDC_LIGHT_4 , IDC_LIGHT_1 ) ;
         else if (Lighting == 2)
            CheckRadioButton ( hdwnd , IDC_LIGHT_0 , IDC_LIGHT_4 , IDC_LIGHT_2 ) ;
         else if (Lighting == 3)
            CheckRadioButton ( hdwnd , IDC_LIGHT_0 , IDC_LIGHT_4 , IDC_LIGHT_3 ) ;
         else if (Lighting == 4)
            CheckRadioButton ( hdwnd , IDC_LIGHT_0 , IDC_LIGHT_4 , IDC_LIGHT_4 ) ;

         if (BinaryDecomp)
            CheckDlgButton ( hdwnd , IDC_BINARY_DECOMP , TRUE ) ;
         else 
            CheckDlgButton ( hdwnd , IDC_BINARY_DECOMP , FALSE ) ;

         if (QuatSmooth)
            CheckDlgButton ( hdwnd , IDC_QUAT_SMOOTH , TRUE ) ;
         else 
            CheckDlgButton ( hdwnd , IDC_QUAT_SMOOTH , FALSE ) ;

         return TRUE ;
         
      case WM_COMMAND:
         id = LOWORD ( wParam ) ;
         switch ( id ) {

            case IDOK:

               RowResolution = (unsigned int) GetDlgItemInt ( hdwnd , IDC_ROWS , NULL , FALSE ) ;
               ColResolution = (unsigned int) GetDlgItemInt ( hdwnd , IDC_COLS , NULL , FALSE ) ;

               if (IsDlgButtonChecked ( hdwnd , IDC_TYPE_MANDEL_DWELL )) {
                  ObjectType = 1 ;
                  DwellVsBinary = 1 ;
                  }
               else if (IsDlgButtonChecked ( hdwnd , IDC_TYPE_MANDEL_BINARY )) {
                  ObjectType = 1 ;
                  DwellVsBinary = 0 ;
                  }
               else if (IsDlgButtonChecked ( hdwnd , IDC_TYPE_JULIA_DWELL)) {
                  if (DisplayedObjectType == 1) {  // When we go from Mandelbrot to Julia, reset range
                     CentR = CentI = 0.0 ;
                     MagR =  MagI = 0.5 ;
                     MinR =  MinI = -2.0 ;
                     MaxR =  MaxI = 2.0 ;
                     sprintf_s ( msg , "%.10lf" , CentR ) ;
                     SetDlgItemText ( hdwnd , IDC_CENTER_REAL , msg ) ;
                     sprintf_s ( msg , "%.10lf" , CentI ) ;
                     SetDlgItemText ( hdwnd , IDC_CENTER_IMAG , msg ) ;
                     sprintf_s ( msg , "%.10lf" , MagR ) ;
                     SetDlgItemText ( hdwnd , IDC_MAGNIF_REAL , msg ) ;
                     sprintf_s ( msg , "%.10lf" , MagI ) ;
                     SetDlgItemText ( hdwnd , IDC_MAGNIF_IMAG , msg ) ;
                     sprintf_s ( msg , "%.10lf" , MinR ) ;
                     SetDlgItemText ( hdwnd , IDC_MIN_REAL , msg ) ;
                     sprintf_s ( msg , "%.10lf" , MinI ) ;
                     SetDlgItemText ( hdwnd , IDC_MIN_IMAG , msg ) ;
                     sprintf_s ( msg , "%.10lf" , MaxR ) ;
                     SetDlgItemText ( hdwnd , IDC_MAX_REAL , msg ) ;
                     sprintf_s ( msg , "%.10lf" , MaxI ) ;
                     SetDlgItemText ( hdwnd , IDC_MAX_IMAG , msg ) ;
                     }
                  ObjectType = 2 ;
                  DwellVsBinary = 1 ;
                  }
               else if (IsDlgButtonChecked ( hdwnd , IDC_TYPE_JULIA_BINARY)) {
                  if (DisplayedObjectType == 1) {  // When we go from Mandelbrot to Julia, reset range
                     CentR = CentI = 0.0 ;
                     MagR =  MagI = 0.5 ;
                     MinR =  MinI = -2.0 ;
                     MaxR =  MaxI = 2.0 ;
                     sprintf_s ( msg , "%.10lf" , CentR ) ;
                     SetDlgItemText ( hdwnd , IDC_CENTER_REAL , msg ) ;
                     sprintf_s ( msg , "%.10lf" , CentI ) ;
                     SetDlgItemText ( hdwnd , IDC_CENTER_IMAG , msg ) ;
                     sprintf_s ( msg , "%.10lf" , MagR ) ;
                     SetDlgItemText ( hdwnd , IDC_MAGNIF_REAL , msg ) ;
                     sprintf_s ( msg , "%.10lf" , MagI ) ;
                     SetDlgItemText ( hdwnd , IDC_MAGNIF_IMAG , msg ) ;
                     sprintf_s ( msg , "%.10lf" , MinR ) ;
                     SetDlgItemText ( hdwnd , IDC_MIN_REAL , msg ) ;
                     sprintf_s ( msg , "%.10lf" , MinI ) ;
                     SetDlgItemText ( hdwnd , IDC_MIN_IMAG , msg ) ;
                     sprintf_s ( msg , "%.10lf" , MaxR ) ;
                     SetDlgItemText ( hdwnd , IDC_MAX_REAL , msg ) ;
                     sprintf_s ( msg , "%.10lf" , MaxI ) ;
                     SetDlgItemText ( hdwnd , IDC_MAX_IMAG , msg ) ;
                     }
                  ObjectType = 2 ;
                  DwellVsBinary = 0 ;
                  }
               else if (IsDlgButtonChecked ( hdwnd , IDC_TYPE_M_QUATERNION))
                  ObjectType = 3 ;
               else if (IsDlgButtonChecked ( hdwnd , IDC_TYPE_J_QUATERNION)) {
                  if (DisplayedObjectType == 1) {  // When we go from Mandelbrot to Julia, reset range
                     CentR = CentI = 0.0 ;
                     MagR =  MagI = 0.5 ;
                     MinR =  MinI = -2.0 ;
                     MaxR =  MaxI = 2.0 ;
                     sprintf_s ( msg , "%.10lf" , CentR ) ;
                     SetDlgItemText ( hdwnd , IDC_CENTER_REAL , msg ) ;
                     sprintf_s ( msg , "%.10lf" , CentI ) ;
                     SetDlgItemText ( hdwnd , IDC_CENTER_IMAG , msg ) ;
                     sprintf_s ( msg , "%.10lf" , MagR ) ;
                     SetDlgItemText ( hdwnd , IDC_MAGNIF_REAL , msg ) ;
                     sprintf_s ( msg , "%.10lf" , MagI ) ;
                     SetDlgItemText ( hdwnd , IDC_MAGNIF_IMAG , msg ) ;
                     sprintf_s ( msg , "%.10lf" , MinR ) ;
                     SetDlgItemText ( hdwnd , IDC_MIN_REAL , msg ) ;
                     sprintf_s ( msg , "%.10lf" , MinI ) ;
                     SetDlgItemText ( hdwnd , IDC_MIN_IMAG , msg ) ;
                     sprintf_s ( msg , "%.10lf" , MaxR ) ;
                     SetDlgItemText ( hdwnd , IDC_MAX_REAL , msg ) ;
                     sprintf_s ( msg , "%.10lf" , MaxI ) ;
                     SetDlgItemText ( hdwnd , IDC_MAX_IMAG , msg ) ;
                     }
                  ObjectType = 4 ;
                  }

               GetDlgItemText ( hdwnd , IDC_BINARY_THRESHOLD , msg , 255 ) ;
               BinaryThreshold = atof ( msg ) ;

               GetDlgItemText ( hdwnd , IDC_JULIA_REAL , msg , 255 ) ;
               JuliaR = atof ( msg ) ;
               GetDlgItemText ( hdwnd , IDC_JULIA_IMAG , msg , 255 ) ;
               JuliaI = atof ( msg ) ;

               DwellLimit = (unsigned int) GetDlgItemInt ( hdwnd , IDC_DWELL_LIMIT , NULL , FALSE ) ;
               GetDlgItemText ( hdwnd , IDC_ESCAPE_CONSTANT , msg , 255 ) ;
               EscapeConstant = atof ( msg ) ;

               if (IsDlgButtonChecked ( hdwnd , IDC_LOCATE_CENTER)) {
                  LocateHow = 0 ;
                  GetDlgItemText ( hdwnd , IDC_CENTER_REAL , msg , 255 ) ;
                  CentR = atof ( msg ) ;
                  GetDlgItemText ( hdwnd , IDC_CENTER_IMAG , msg , 255 ) ;
                  CentI = atof ( msg ) ;
                  GetDlgItemText ( hdwnd , IDC_MAGNIF_REAL , msg , 255 ) ;
                  MagR = atof ( msg ) ;
                  GetDlgItemText ( hdwnd , IDC_MAGNIF_IMAG , msg , 255 ) ;
                  MagI = atof ( msg ) ;
                  MinR = CentR - 1. / MagR ;
                  MinI = CentI - 1. / MagI ;
                  MaxR = CentR + 1. / MagR ;
                  MaxI = CentI + 1. / MagI ;
                  }
               else {
                  LocateHow = 1 ;
                  GetDlgItemText ( hdwnd , IDC_MIN_REAL , msg , 255 ) ;
                  MinR = atof ( msg ) ;
                  GetDlgItemText ( hdwnd , IDC_MIN_IMAG , msg , 255 ) ;
                  MinI = atof ( msg ) ;
                  GetDlgItemText ( hdwnd , IDC_MAX_REAL , msg , 255 ) ;
                  MaxR = atof ( msg ) ;
                  GetDlgItemText ( hdwnd , IDC_MAX_IMAG , msg , 255 ) ;
                  MaxI = atof ( msg ) ;
                  CentR = 0.5 * (MinR + MaxR) ;
                  CentI = 0.5 * (MinI + MaxI) ;
                  MagR = 2. / (MaxR - MinR) ;
                  MagI = 2. / (MaxI - MinI) ;
                  }

               if (IsDlgButtonChecked ( hdwnd , IDC_TRUE_ASPECT))
                  TrueAspect = 1 ;
               else 
                  TrueAspect = 0 ;

               if (IsDlgButtonChecked ( hdwnd , IDC_COLLAPSE_R))
                  Collapse = 0 ;
               else if (IsDlgButtonChecked ( hdwnd , IDC_COLLAPSE_I))
                  Collapse = 1 ;
               else if (IsDlgButtonChecked ( hdwnd , IDC_COLLAPSE_J))
                  Collapse = 2 ;
               else 
                  Collapse = 3 ;

               GetDlgItemText ( hdwnd , IDC_AZIMUTH , msg , 255 ) ;
               Azimuth = atof ( msg ) ;
               GetDlgItemText ( hdwnd , IDC_ELEVATION , msg , 255 ) ;
               Elevation = atof ( msg ) ;

               if (IsDlgButtonChecked ( hdwnd , IDC_LIGHT_0))
                  Lighting = 0 ;
               else if (IsDlgButtonChecked ( hdwnd , IDC_LIGHT_1))
                  Lighting = 1 ;
               else if (IsDlgButtonChecked ( hdwnd , IDC_LIGHT_2))
                  Lighting = 2 ;
               else if (IsDlgButtonChecked ( hdwnd , IDC_LIGHT_3))
                  Lighting = 3 ;
               else if (IsDlgButtonChecked ( hdwnd , IDC_LIGHT_4))
                  Lighting = 4 ;

               if (IsDlgButtonChecked ( hdwnd , IDC_BINARY_DECOMP))
                  BinaryDecomp = 1 ;
               else 
                  BinaryDecomp = 0 ;

               if (IsDlgButtonChecked ( hdwnd , IDC_QUAT_SMOOTH))
                  QuatSmooth = 1 ;
               else 
                  QuatSmooth = 0 ;

               EndDialog ( hdwnd , TRUE ) ;
               return TRUE ;  // Tell Windows we processed this message

            case IDCANCEL:
               EndDialog ( hdwnd , FALSE ) ;
               return TRUE ;  // Tell Windows we processed this message
            }
      }
      
   return FALSE ;  // Tell Windows we did not process this message
}
