/**********************************************************************************/
/*                                                                                */
/*  DLG_AG - dialog callbacks for all parameter dialogs                           */
/*                                                                                */
/**********************************************************************************/

#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 "lin.rh"
#include "extern.h"

/*
   Local routine compresses out illegal characters
*/

static void compress ( char *text )
{
   char *source, *dest ;

   _strupr_s ( text , 2048 ) ;
   source = dest = text ;
   while (*source) {
      if (*source == '+'  ||  *source == '-'  ||  *source == 'P'  ||  *source == 'S'  || 
          *source == '['  ||  *source == ']'  ||  *source == 'V')
         *dest++ = *source ;
      ++source ;
      }
   *dest = 0 ;
}


extern "C"

INT_PTR CALLBACK DialogAGParams ( HWND hdwnd , UINT message ,
                                  WPARAM wParam , LPARAM /* lParam */ )
{
   int id ;
   char msg[2048] ;

   switch ( message ) {
      
      case WM_INITDIALOG:
         SetDlgItemInt ( hdwnd , IDC_IMAGE_ROWS , AG_params.nrows , TRUE ) ;
         SetDlgItemInt ( hdwnd , IDC_IMAGE_COLS , AG_params.ncols , TRUE ) ;
         SetDlgItemInt ( hdwnd , IDC_LINE_WIDTH , AG_params.width , TRUE ) ;
         SetDlgItemInt ( hdwnd , IDC_DEPTH , AG_params.depth , TRUE ) ;
         sprintf_s ( msg , "%.2lf", AG_params.angle ) ;
         SetDlgItemText ( hdwnd , IDC_ANGLE , msg ) ;
         sprintf_s ( msg , "%.2lf", AG_params.shrink ) ;
         SetDlgItemText ( hdwnd , IDC_SHRINK , msg ) ;
         SetDlgItemText ( hdwnd , IDC_AXIOM , AG_params.axiom ) ;
         SetDlgItemText ( hdwnd , IDC_PRIMARY , AG_params.primary ) ;
         SetDlgItemText ( hdwnd , IDC_SECONDARY , AG_params.secondary ) ;
         return TRUE ;
         
      case WM_COMMAND:
         id = LOWORD ( wParam ) ;
         switch ( id ) {
            case IDOK:
               AG_params.bad_rule = 0 ;
               AG_params.nrows = GetDlgItemInt ( hdwnd , IDC_IMAGE_ROWS , NULL , FALSE ) ;
               AG_params.ncols = GetDlgItemInt ( hdwnd , IDC_IMAGE_COLS , NULL , FALSE ) ;
               AG_params.width = GetDlgItemInt ( hdwnd , IDC_LINE_WIDTH , NULL , FALSE ) ;
               AG_params.depth = GetDlgItemInt ( hdwnd , IDC_DEPTH , NULL , FALSE ) ;
               GetDlgItemText ( hdwnd , IDC_ANGLE , msg , 2048 ) ;
               AG_params.angle = atof ( msg ) ;
               GetDlgItemText ( hdwnd , IDC_SHRINK , msg , 2048 ) ;
               AG_params.shrink = atof ( msg ) ;
               GetDlgItemText ( hdwnd , IDC_AXIOM , msg , 2048 ) ;
               compress ( msg ) ;  // Remove illegal characters, especially blanks
               if (strlen(msg) <= MAX_RULE)
                  strcpy_s ( AG_params.axiom , msg ) ;
               else {
                  sprintf_s ( AG_params.axiom , "Too long; max characters=%d", MAX_RULE ) ;
                  AG_params.bad_rule = 1 ;
                  }
               GetDlgItemText ( hdwnd , IDC_PRIMARY , msg , 2048 ) ;
               compress ( msg ) ;  // Remove illegal characters, especially blanks
               if (strlen(msg) <= MAX_RULE)
                  strcpy_s ( AG_params.primary , msg ) ;
               else {
                  sprintf_s ( AG_params.primary , "Too long; max characters=%d", MAX_RULE ) ;
                  AG_params.bad_rule = 1 ;
                  }
               GetDlgItemText ( hdwnd , IDC_SECONDARY , msg , 2048 ) ;
               compress ( msg ) ;  // Remove illegal characters, especially blanks
               if (strlen(msg) <= MAX_RULE)
                  strcpy_s ( AG_params.secondary , msg ) ;
               else {
                  sprintf_s ( AG_params.secondary , "Too long; max characters=%d", MAX_RULE ) ;
                  AG_params.bad_rule = 1 ;
                  }
               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
}
