home *** CD-ROM | disk | FTP | other *** search
/ Inside Multimedia 1995 July / IMM0795.ISO / share / os2 / pmfract / src / pmfrdlg2.c < prev    next >
Text File  |  1994-01-24  |  36KB  |  906 lines

  1. /*--------------------------------------------------
  2.    PMFRDLG2.C -- FRACTINT for PM
  3.  
  4.    External Interfaces Dialogs Module
  5.  
  6.    03/30/91      Code by Donald P. Egen (with help)
  7.  
  8.    This module contains all functions backing dialog
  9.    boxes that handle non-Fractal Calculation Engine
  10.    actions, such as Load, Save, Print, and About.
  11.  ---------------------------------------------------*/
  12.  
  13. #define INCL_WIN
  14. #define INCL_GPI
  15. #define INCL_DOS
  16. #include <os2.h>
  17. #include <process.h>
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <smplhelp.h>
  21. #include <string.h>
  22.  
  23. #include <opendlg.h>
  24.  
  25. #include "pmfract.h"
  26. #include "fractint.h"
  27. #include "fractype.h"
  28.  
  29. /* Declare local subroutines */
  30. static int get_formula_names(PGENSEL); /* get the fractal formula names */
  31. static int get_lsys_name(PGENSEL);     /* get the Lsystem formula name */
  32.  
  33. /*--------------------------------------------------
  34.   This function backs the About... dialog box.
  35.  
  36.   Any dismiss is a dismiss.  No significant result
  37.   is returned.
  38.  --------------------------------------------------*/
  39. MRESULT EXPENTRY AboutDlgProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  40.      {
  41.      switch (msg)
  42.           {
  43.           case WM_INITDLG:
  44.                CenterDialogBox(hwnd);
  45.  
  46.                return 0;
  47.  
  48.           case WM_COMMAND:
  49.                switch (COMMANDMSG(&msg)->cmd)
  50.                     {
  51.                     case DID_OK:
  52.                     case DID_CANCEL:
  53.                          WinDismissDlg (hwnd, TRUE) ;
  54.                          return 0 ;
  55.                     }
  56.                break ;
  57.           }
  58.      return WinDefDlgProc (hwnd, msg, mp1, mp2) ;
  59.      }
  60.  
  61. /*--------------------------------------------------
  62.   This function backs the File/New... dialog box.
  63.  
  64.   The current fractal type is set during initialization.
  65.   The user may select from the list box.
  66.   The result is returned on OK or double-click in the list box.
  67.  
  68.   Help sends an appropriate message using the
  69.   SMPLHELP system.
  70.  --------------------------------------------------*/
  71. MRESULT EXPENTRY SelFractalDlgProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  72.      {
  73.      SHORT i, isSelected;
  74.  
  75.      switch (msg)
  76.           {
  77.           case WM_INITDLG:
  78.  
  79.                CenterDialogBox(hwnd);
  80.  
  81.                InitNewParms(&npTempParms);
  82.                /* Load the list box from the list of names */
  83.                WinSendDlgItemMsg (hwnd, IDD_SET_FRACTTYPE_LB,
  84.                           LM_DELETEALL, MPFROMP(NULL), MPFROMP(NULL) );
  85.  
  86.                isSelected = MANDEL;  /* default? */
  87.  
  88.                for (i = 0; i < CountFractalList; i++)
  89.                    {
  90.                    WinSendDlgItemMsg (hwnd, IDD_SET_FRACTTYPE_LB,
  91.                           LM_INSERTITEM, MPFROM2SHORT(LIT_END, 0),
  92.                           MPFROMP(fractalspecific[asFracTypeList[i]].name) );
  93.                    if (asFracTypeList[i] == npTempParms.iFractType ||
  94.                        fractalspecific[asFracTypeList[i]].tofloat == npTempParms.iFractType)
  95.                         isSelected = i;
  96.                    }
  97.  
  98.                /* Flag the current Fractal Type in the list box */
  99.                WinSendDlgItemMsg (hwnd, IDD_SET_FRACTTYPE_LB,
  100.                           LM_SELECTITEM, MPFROMSHORT (isSelected),
  101.                                  MPFROMSHORT(TRUE) ) ;
  102.                /* and put it near the middle */
  103.                WinSendDlgItemMsg (hwnd, IDD_SET_FRACTTYPE_LB,
  104.                           LM_SETTOPINDEX,
  105.                           MPFROMSHORT(max(isSelected-4, 0) ),
  106.                           MPFROMP(NULL) ) ;
  107.  
  108.                return 0;
  109.  
  110.           case WM_CONTROL:
  111.                {
  112.                SHORT sWho = SHORT1FROMMP(mp1);
  113.                SHORT sWhat = SHORT2FROMMP(mp1);
  114.  
  115.                switch (sWho)
  116.                     {
  117.                     case IDD_SET_FRACTTYPE_LB:
  118.                          switch (sWhat)
  119.                               {
  120.                               case LN_ENTER:
  121.                                    /* fake the OK button on double click */
  122.                                    WinSendMsg(hwnd, WM_COMMAND,
  123.                                    MPFROMSHORT(DID_OK),
  124.                                    MPFROM2SHORT(CMDSRC_PUSHBUTTON, FALSE) ) ;
  125.                               return 0;
  126.  
  127.                               }
  128.                     }
  129.                break;
  130.                }
  131.  
  132.           case WM_COMMAND:
  133.                switch (COMMANDMSG(&msg)->cmd)
  134.                     {
  135.                     DLF dlf;
  136.                     HFILE hfDummy;
  137.  
  138.                     case DID_OK:
  139.                          isSelected = (SHORT) WinSendDlgItemMsg(hwnd,
  140.                              IDD_SET_FRACTTYPE_LB, LM_QUERYSELECTION,
  141.                              MPFROMSHORT(0), MPFROMP(NULL) );
  142.                          npTempParms.iFractType = asFracTypeList[isSelected];
  143.                          npTempParms.mxXL = fractalspecific[npTempParms.iFractType].xmin;
  144.                          npTempParms.mxXR = fractalspecific[npTempParms.iFractType].xmax;
  145.                          npTempParms.mxYT = fractalspecific[npTempParms.iFractType].ymax;
  146.                          npTempParms.mxYB = fractalspecific[npTempParms.iFractType].ymin;
  147.                          npTempParms.param[0] = fractalspecific[npTempParms.iFractType].paramvalue[0];
  148.                          npTempParms.param[1] = fractalspecific[npTempParms.iFractType].paramvalue[1];
  149.                          npTempParms.param[2] = fractalspecific[npTempParms.iFractType].paramvalue[2];
  150.                          npTempParms.param[3] = fractalspecific[npTempParms.iFractType].paramvalue[3];
  151.                          npTempParms.trigndx[0] = SIN;
  152.                          npTempParms.trigndx[1] = SQR;
  153.                          npTempParms.trigndx[2] = SINH;
  154.                          npTempParms.trigndx[3] = COSH;
  155.  
  156.                          /* zoom out to maximum range */
  157.                          npTempParms.XL = npTempParms.mxXL;
  158.                          npTempParms.XR = npTempParms.mxXR;
  159.                          npTempParms.YT = npTempParms.mxYT;
  160.                          npTempParms.YB = npTempParms.mxYB;
  161.                          npTempParms.XCenter = (npTempParms.XL + npTempParms.XR)/2.0;
  162.                          npTempParms.YCenter = (npTempParms.YT + npTempParms.YB)/2.0;
  163.  
  164.                          /* handle special fractal types */
  165.                          npTempParms.fNewParms = FALSE;       /* assume cancel below */
  166.                          switch (npTempParms.iFractType)
  167.                             {
  168.                             case IFS:
  169.                                  /* get the file name for IFS */
  170.                                  SetupDLF (&dlf, DLG_OPENDLG, &hfDummy,
  171.                                      "\\*.ifs", szTitleBar,
  172.                                      szIFSTitle, szIFSHelp);
  173.                                  _fstrcpy(dlf.szOpenFile, npTempParms.szIfsFileName);
  174.  
  175.                                  if (TDF_OLDOPEN == DlgFile(hwnd, &dlf) )
  176.                                     {
  177.                                     /* close the dummy file handle */
  178.                                     DosClose(hfDummy);
  179.                                     /* set the name */
  180.                                     _fstrcpy(npTempParms.szIfsFileName, dlf.szOpenFile);
  181.                                     /* flag OK */
  182.                                     npTempParms.fNewParms = TRUE;
  183.                                     }
  184.                                  break;
  185.  
  186.                             case IFS3D:
  187.                                  /* get the file name for IFS3D */
  188.                                  SetupDLF (&dlf, DLG_OPENDLG, &hfDummy,
  189.                                      "\\*.ifs", szTitleBar,
  190.                                      szIFS3DTitle, szIFS3DHelp);
  191.                                  _fstrcpy(dlf.szOpenFile, npTempParms.szIfs3dFileName);
  192.  
  193.                                  if (TDF_OLDOPEN == DlgFile(hwnd, &dlf) )
  194.                                     {
  195.                                     /* close the dummy file handle */
  196.                                     DosClose(hfDummy);
  197.                                     /* set the name */
  198.                                     _fstrcpy(npTempParms.szIfs3dFileName, dlf.szOpenFile);
  199.                                     /* flag OK */
  200.                                     npTempParms.fNewParms = TRUE;
  201.                                     }
  202.                                  break;
  203.  
  204.                             case FORMULA:
  205.                             case FFORMULA:
  206.                                  /* get the name of the Formula file */
  207.                                  SetupDLF (&dlf, DLG_OPENDLG, &hfDummy,
  208.                                      "\\*.frm", szTitleBar,
  209.                                      szFormTitle, szFormHelp);
  210.                                  _fstrcpy(dlf.szOpenFile, npTempParms.szFormFileName);
  211.  
  212.                                  if (TDF_OLDOPEN == DlgFile(hwnd, &dlf) )
  213.                                     {
  214.  
  215.                                     /* close the dummy file handle */
  216.                                     DosClose(hfDummy);
  217.                                     /* set the name */
  218.                                     _fstrcpy(npTempParms.szFormFileName, dlf.szOpenFile);
  219.                                     /* read the formula names */
  220.                                     get_formula_names(&gs);
  221.                                     /* now select */
  222.                                     _fstrcpy(gs.szSelected, npTempParms.szFormName);
  223.                                     gs.pszTitle = szSelFormula;
  224.                                     if (WinDlgBox (HWND_DESKTOP, hwnd, SelWhatDlgProc,
  225.                                               (HMODULE) 0, IDD_SEL_LIST, &gs) )
  226.                                        {
  227.                                        /* set the name selected */
  228.                                        _fstrcpy(npTempParms.szFormName, gs.szSelected );
  229.                                        /* flag OK */
  230.                                        npTempParms.fNewParms = TRUE;
  231.                                        }
  232.                                     }
  233.                                  break;
  234.  
  235.                             case LSYSTEM:
  236.                                  /* get the name of the L-System Formula file */
  237.                                  SetupDLF (&dlf, DLG_OPENDLG, &hfDummy,
  238.                                      "\\*.l", szTitleBar,
  239.                                      szLsysTitle, szLsysHelp);
  240.                                  _fstrcpy(dlf.szOpenFile, npTempParms.szLSysFileName);
  241.  
  242.                                  if (TDF_OLDOPEN == DlgFile(hwnd, &dlf) )
  243.                                     {
  244.  
  245.                                     /* close the dummy file handle */
  246.                                     DosClose(hfDummy);
  247.                                     /* set the name */
  248.                                     _fstrcpy(npTempParms.szLSysFileName, dlf.szOpenFile);
  249.                                     /* read the formula names */
  250.                                     get_lsys_name(&gs);
  251.                                     /* now select */
  252.                                     _fstrcpy(gs.szSelected, npTempParms.szLSysName);
  253.                                     if (WinDlgBox (HWND_DESKTOP, hwnd, SelWhatDlgProc,
  254.                                               (HMODULE) 0, IDD_SEL_LIST, &gs) )
  255.                                        {
  256.                                        /* set the name selected */
  257.                                        _fstrcpy(npTempParms.szLSysName, gs.szSelected );
  258.                                        npTempParms.fNewParms = TRUE;
  259.                                        }
  260.                                     }
  261.                                  break;
  262.  
  263.                             default:
  264.                                  /* just flag OK */
  265.                                  npTempParms.fNewParms = TRUE;
  266.                                  break;
  267.                             }
  268.  
  269.                          npNewParms = npTempParms;
  270.                          WinDismissDlg (hwnd, npNewParms.fNewParms) ;
  271.                          return 0 ;
  272.  
  273.                     case DID_CANCEL:
  274.                          npTempParms.fNewParms = FALSE;
  275.                          WinDismissDlg (hwnd, FALSE) ;
  276.                          return 0 ;
  277.                     }
  278.                break ;
  279.  
  280.           case WM_HELP:
  281.                SimpleHelp(hab, hwnd, szTitleBar,
  282.                        (HMODULE) 0, IDT_TEXT, IDT_HELP_TYPES);
  283.  
  284.                return 0;
  285.  
  286.                break;
  287.           }
  288.      return WinDefDlgProc (hwnd, msg, mp1, mp2) ;
  289.      }
  290.  
  291. /*--------------------------------------------------
  292.   This function backs the Options/Set Palette... dialog box.
  293.  
  294.   This dialog effects immediate change to the display
  295.   by allowing the user to select from the palettes that
  296.   may be available to him.
  297.  
  298.   Note that a richer (more colors) palette will be attempted
  299.   to be translated by the GPI bitmap handling.  The result
  300.   will be as good as the driver/display combination is capable of.
  301.  
  302.   Help sends an appropriate message using the
  303.   SMPLHELP system.
  304.  --------------------------------------------------*/
  305. MRESULT EXPENTRY SetPaletteDlgProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  306.      {
  307.  
  308.      switch (msg)
  309.           {
  310.           case WM_INITDLG:
  311.  
  312.                CenterDialogBox(hwnd);
  313.  
  314.                /* Flick the button showing where we are now. */
  315.                WinSendDlgItemMsg (hwnd, cp.sCurrentPalette,
  316.                           BM_SETCHECK, MPFROMSHORT(1), MPFROMP(NULL) );
  317.                WinSetFocus(HWND_DESKTOP, WinWindowFromID(hwnd,
  318.                            cp.sCurrentPalette) );
  319.  
  320.                /* If we are running 2-color, turn off the
  321.                   16-color, 256-color, and physical options.
  322.                   They are inappropriate for 2-color mode */
  323.                if (cp.colors < 16)
  324.                   {
  325.                   WinEnableWindow ( WinWindowFromID(hwnd, IDD_PAL_VGA16), FALSE);
  326.                   WinEnableWindow ( WinWindowFromID(hwnd, IDD_PAL_VGA256), FALSE);
  327.                   WinEnableWindow ( WinWindowFromID(hwnd, IDD_PAL_PHYS), FALSE);
  328.                   }
  329.  
  330.                /* If we have read a user palette, turn on the user_palette
  331.                   button.   */
  332.  
  333.                if (cp.fHaveUserPalette)
  334.                   {
  335.                   WinEnableWindow ( WinWindowFromID(hwnd, IDD_PAL_USER), TRUE);
  336.                   }
  337.  
  338.                /* and go away */
  339.                return ((MRESULT) TRUE);
  340.  
  341.           case WM_CONTROL:
  342.                {
  343.                SHORT sWho = SHORT1FROMMP(mp1);
  344.                SHORT sWhat = SHORT2FROMMP(mp1);
  345.  
  346.                /* switch the palette pointer */
  347.                /* save new state for next time */
  348.                switch (sWhat)
  349.                      {
  350.                      case BN_CLICKED:
  351.                           switch (sWho)
  352.                                {
  353.                                case IDD_PAL_BW:
  354.                                     cp.pbmiMemory = &bmiColorTableBW;
  355.                                     cp.sCurrentPalette = IDD_PAL_BW;
  356.                                     break;
  357.                                case IDD_PAL_WB:
  358.                                     cp.pbmiMemory = &bmiColorTableWB;
  359.                                     cp.sCurrentPalette = IDD_PAL_WB;
  360.                                     break;
  361.                                case IDD_PAL_VGA16:
  362.                                     cp.pbmiMemory = &bmiColorTableVGA16;
  363.                                     cp.sCurrentPalette = IDD_PAL_VGA16;
  364.                                     break;
  365.                                case IDD_PAL_VGA256:
  366.                                     cp.pbmiMemory = &bmiColorTableVGA256;
  367.                                     cp.sCurrentPalette = IDD_PAL_VGA256;
  368.                                     break;
  369.                                case IDD_PAL_PHYS:
  370.                                     cp.pbmiMemory = &bmiColorTablePhys;
  371.                                     cp.sCurrentPalette = IDD_PAL_PHYS;
  372.                                     break;
  373.                                case IDD_PAL_USER:
  374.                                     cp.pbmiMemory = &bmiColorTableUser;
  375.                                     cp.sCurrentPalette = IDD_PAL_USER;
  376.                                     break;
  377.                                }
  378.  
  379.                           /* now hose the display */
  380.                           cp.fNewBits = TRUE;
  381.                           WinInvalidateRect(cp.hwnd, NULL, FALSE);
  382.  
  383.                           /* and go away */
  384.                           return 0;
  385.                      }
  386.                }
  387.  
  388.           case WM_COMMAND:
  389.                switch (COMMANDMSG(&msg)->cmd)
  390.                     {
  391.                     case DID_OK:
  392.                     case DID_CANCEL:
  393.                          /* any exit is an exit */
  394.                          WinDismissDlg (hwnd, TRUE) ;
  395.                          return 0 ;
  396.                     }
  397.                break ;
  398.  
  399.           case WM_HELP:
  400.                SimpleHelp(hab, hwnd, szTitleBar,
  401.                        (HMODULE) 0, IDT_TEXT, IDT_HELP_OPERATE);
  402.  
  403.                return 0;
  404.  
  405.                break;
  406.           }
  407.      return WinDefDlgProc (hwnd, msg, mp1, mp2) ;
  408.      }
  409.  
  410. /*--------------------------------------------------
  411.   This function backs the File/Print... dialog box.
  412.  
  413.   We must determine if the user wants to print to the PM Printer
  414.   or direct to an HP PaintJet.
  415.   If the user clicks OK, we return TRUE and the main line will
  416.   schedule the print; else return FALSE on CANCEL.
  417.  
  418.   The Help button sends an appropriate message using the
  419.   SMPLHELP system.
  420.  --------------------------------------------------*/
  421. MRESULT EXPENTRY PrintOptionsDlgProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  422.      {
  423.  
  424.      char szPortName[9];
  425.      DLF dlf;    /* OpenDlg parm */
  426.      HFILE hfDummy;
  427.      int rc;
  428.  
  429.      switch (msg)
  430.           {
  431.           case WM_INITDLG:
  432.  
  433.                CenterDialogBox(hwnd);
  434.  
  435.                /* set up the dialog box indicating the PM Printer
  436.                   is the suggested output.  */
  437.                WinSendDlgItemMsg(hwnd, IDD_PRINT_PM, BM_SETCHECK,
  438.                     MPFROMSHORT(1), NULL);
  439.                WinSendDlgItemMsg(hwnd, IDD_PRINT_PORT, BM_SETCHECK,
  440.                     MPFROMSHORT(1), NULL);
  441.  
  442.                /* and go back */
  443.                return 0;
  444.  
  445.           case WM_CONTROL:
  446.                {
  447.                SHORT sWho = SHORT1FROMMP(mp1);
  448.                SHORT sWhat = SHORT2FROMMP(mp1);
  449.  
  450.                switch (sWho)
  451.                     {
  452.                     case IDD_PRINT_PM:
  453.                          switch (sWhat)
  454.                               {
  455.                               case BN_CLICKED:
  456.                                    /* if you want PM, then disable
  457.                                       the PJ entry fields */
  458.                                    WinEnableWindow (
  459.                                        WinWindowFromID(hwnd, IDD_PRINT_FILE),
  460.                                              FALSE);
  461.                                    WinEnableWindow (
  462.                                        WinWindowFromID(hwnd, IDD_PRINT_PORT),
  463.                                              FALSE);
  464.                                    WinEnableWindow (
  465.                                        WinWindowFromID(hwnd, IDD_PRINT_PORTNAME),
  466.                                              FALSE);
  467.                               return 0;
  468.  
  469.                               }
  470.                     case IDD_PRINT_PJ:
  471.                          switch (sWhat)
  472.                               {
  473.                               case BN_CLICKED:
  474.                                    /* if you want PJ, then enable
  475.                                       the PJ entry fields */
  476.                                    WinEnableWindow (
  477.                                        WinWindowFromID(hwnd, IDD_PRINT_FILE),
  478.                                              TRUE);
  479.                                    WinEnableWindow (
  480.                                        WinWindowFromID(hwnd, IDD_PRINT_PORT),
  481.                                              TRUE);
  482.                                    WinEnableWindow (
  483.                                        WinWindowFromID(hwnd, IDD_PRINT_PORTNAME),
  484.                                              TRUE);
  485.                               return 0;
  486.  
  487.                               }
  488.                     }
  489.                break;
  490.                }
  491.  
  492.           case WM_COMMAND:
  493.                switch (COMMANDMSG(&msg)->cmd)
  494.                     {
  495.                     case DID_OK:
  496.                          /* on OK, we need to find out what was set
  497.                             and save it in CALCPARM. */
  498.                          if (1 == (USHORT) WinSendDlgItemMsg(hwnd, IDD_PRINT_PM,
  499.                                              BM_QUERYCHECK, NULL, NULL) )
  500.                             {
  501.                             /* we are going to PM */
  502.                             cp.sSubFunction = SUB_PRINT_PM;
  503.                             }
  504.                          else
  505.                             {
  506.                             /* we are going to PaintJet, but File or Port? */
  507.                             if (1 == (USHORT) WinSendDlgItemMsg(hwnd, IDD_PRINT_FILE,
  508.                                                BM_QUERYCHECK, NULL, NULL) )
  509.                                {
  510.                                /* going to File */
  511.                                SetupDLF(&dlf, DLG_SAVEDLG, &hfDummy,
  512.                                         "\\*.prn", szTitleBar,
  513.                                         szPrintAction, szPrintHelp);
  514.                                _fstrcpy(dlf.szOpenFile, cp.szPrintName);
  515.                                rc = DlgFile(hwnd, &dlf);
  516.                                if (rc == TDF_NOSAVE)
  517.                                   {
  518.                                   /* user hit cancel */
  519.                                   WinDismissDlg (hwnd, FALSE);
  520.                                   return 0;
  521.                                   }
  522.                                DosClose(hfDummy);   /* close testing handle */
  523.                                _fstrcpy(cp.szPrintName, dlf.szOpenFile);
  524.                                cp.sSubFunction = SUB_PRINT_PJ;
  525.                                }
  526.                             else
  527.                                {
  528.                                /* we are going to PORT LPTn */
  529.                                cp.sSubFunction = SUB_PRINT_PJ;
  530.                                WinQueryDlgItemText(hwnd, IDD_PRINT_PORTNAME,
  531.                                        sizeof(szPortName), szPortName);
  532.                                _fstrcpy(cp.szPrintName, szPortName);
  533.                                }
  534.                             }
  535.  
  536.                          /* Indicate good return to caller */
  537.                          WinDismissDlg (hwnd, TRUE) ;
  538.                          return 0 ;
  539.  
  540.                     case DID_CANCEL:
  541.                          /* just bag it on CANCEL */
  542.                          WinDismissDlg (hwnd, FALSE) ;
  543.                          return 0 ;
  544.                     }
  545.                break ;
  546.  
  547.           case WM_HELP:
  548.                SimpleHelp(hab, hwnd, szTitleBar,
  549.                        (HMODULE) 0, IDT_TEXT, IDT_HELP_TYPES);
  550.  
  551.                return 0;
  552.  
  553.                break;
  554.           }
  555.      return WinDefDlgProc (hwnd, msg, mp1, mp2) ;
  556.      }
  557.  
  558. /*--------------------------------------------------
  559.   This function backs the General Selection dialog box.
  560.   It is called during processing of File/New to
  561.   get the L-System or Formula name after a list has
  562.   been generated from the file selected by the user.
  563.  
  564.   The user may select from the list box.
  565.   The result is returned on OK or double-click in the list box.
  566.  
  567.   Help sends an appropriate message using the
  568.   SMPLHELP system.
  569.  --------------------------------------------------*/
  570. MRESULT EXPENTRY SelWhatDlgProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  571.      {
  572.      SHORT i, isSelected;
  573.  
  574.      switch (msg)
  575.           {
  576.           case WM_INITDLG:
  577.  
  578.                CenterDialogBox(hwnd);
  579.  
  580.                /* Begin by setting the Title */
  581.                WinSetDlgItemText(hwnd, IDD_SEL_LIST_WHAT, gs.pszTitle);
  582.  
  583.                /* Load the list box from the list of names */
  584.                WinSendDlgItemMsg (hwnd, IDD_SEL_LIST_LB,
  585.                           LM_DELETEALL, MPFROMP(NULL), MPFROMP(NULL) );
  586.                isSelected = 0;   /* default if old not found in current list */
  587.                for (i = 0; i < gs.sCountNames; i++)
  588.                    {
  589.                    WinSendDlgItemMsg (hwnd, IDD_SEL_LIST_LB,
  590.                           LM_INSERTITEM, MPFROM2SHORT(LIT_END, 0),
  591.                           MPFROMP(gs.szNames[i]) );
  592.                    if (0 == _fstrcmp(gs.szNames[i], gs.szSelected) )
  593.                         isSelected = i;
  594.                    }
  595.  
  596.                /* Flag the current Selection in the list box */
  597.                WinSendDlgItemMsg (hwnd, IDD_SEL_LIST_LB,
  598.                           LM_SELECTITEM, MPFROMSHORT (isSelected),
  599.                                  MPFROMSHORT(TRUE) ) ;
  600.                /* and put it near the middle */
  601.                WinSendDlgItemMsg (hwnd, IDD_SEL_LIST_LB,
  602.                           LM_SETTOPINDEX,
  603.                           MPFROMSHORT(max(isSelected-4, 0) ),
  604.                           MPFROMP(NULL) ) ;
  605.  
  606.                return 0;
  607.  
  608.           case WM_CONTROL:
  609.                {
  610.                SHORT sWho = SHORT1FROMMP(mp1);
  611.                SHORT sWhat = SHORT2FROMMP(mp1);
  612.  
  613.                switch (sWho)
  614.                     {
  615.                     case IDD_SEL_LIST_LB:
  616.                          switch (sWhat)
  617.                               {
  618.                               case LN_ENTER:
  619.                                    /* fake the OK button on double click */
  620.                                    WinSendMsg(hwnd, WM_COMMAND,
  621.                                    MPFROMSHORT(DID_OK),
  622.                                    MPFROM2SHORT(CMDSRC_PUSHBUTTON, FALSE) ) ;
  623.                               return 0;
  624.  
  625.                               }
  626.                     }
  627.                break;
  628.                }
  629.  
  630.           case WM_COMMAND:
  631.                switch (COMMANDMSG(&msg)->cmd)
  632.                     {
  633.                     case DID_OK:
  634.                          isSelected = (SHORT) WinSendDlgItemMsg(hwnd,
  635.                              IDD_SEL_LIST_LB, LM_QUERYSELECTION,
  636.                              MPFROMSHORT(0), MPFROMP(NULL) );
  637.  
  638.                          _fstrcpy(gs.szSelected, gs.szNames[isSelected]);
  639.  
  640.                          npTempParms.fNewParms = TRUE;
  641.                          WinDismissDlg (hwnd, TRUE) ;
  642.                          return 0 ;
  643.  
  644.                     case DID_CANCEL:
  645.                          npTempParms.fNewParms = FALSE;
  646.                          WinDismissDlg (hwnd, FALSE) ;
  647.                          return 0 ;
  648.                     }
  649.                break ;
  650.  
  651.           case WM_HELP:
  652.                SimpleHelp(hab, hwnd, szTitleBar,
  653.                        (HMODULE) 0, IDT_TEXT, IDT_HELP_TYPES);
  654.  
  655.                return 0;
  656.  
  657.                break;
  658.           }
  659.      return WinDefDlgProc (hwnd, msg, mp1, mp2) ;
  660.      }
  661.  
  662. /*--------------------------------------------------
  663.   This function backs the Load/Save Format dialog box.
  664.   It is called during processing of File/Open or File/Save as
  665.   to define the format before proceeding to select a file name.
  666.   Besides setting the format for the Load or Save operation,
  667.   it eventually serves to set the default expected extension.
  668.  
  669.   The user may select from radio buttons presented.
  670.   The result is returned on OK.
  671.  
  672.   Help sends an appropriate message using the
  673.   SMPLHELP system.
  674.  --------------------------------------------------*/
  675. MRESULT EXPENTRY LoadSaveFmtDlgProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  676.      {
  677.      USHORT i;
  678.  
  679.      switch (msg)
  680.           {
  681.           case WM_INITDLG:
  682.  
  683.                CenterDialogBox(hwnd);
  684.  
  685.                /* Begin by setting the Title */
  686.                WinSetDlgItemText(hwnd, IDD_LST_WHAT,
  687.                           (PSZ) PVOIDFROMMP(mp2) );
  688.  
  689.                /* suggest the last format we used */
  690.                switch (cp.sLastLoadSaveType)
  691.                   {
  692.                   case SUB_LOADSAVE_GIF: i = IDD_LST_GIF; break;
  693.                   case SUB_LOADSAVE_BMP: i = IDD_LST_BMP; break;
  694.                   case SUB_LOADSAVE_MET: i = IDD_LST_MET; break;
  695.                   case SUB_LOADSAVE_WIN3BMP: i = IDD_LST_WIN3BMP; break;
  696.                   case SUB_LOADSAVE_PCX: i = IDD_LST_PCX; break;
  697.                   default: i = IDD_LST_GIF;
  698.                            cp.sLastLoadSaveType = SUB_LOADSAVE_GIF;
  699.                            break;
  700.                   }
  701.  
  702.                WinSendDlgItemMsg (hwnd, i, BM_SETCHECK,
  703.                     MPFROMSHORT((USHORT) 1), NULL);
  704.  
  705.                return 0;
  706.  
  707.           case WM_CONTROL:
  708.                {
  709.                SHORT sWho = SHORT1FROMMP(mp1);
  710.                SHORT sWhat = SHORT2FROMMP(mp1);
  711.  
  712.                switch (sWhat)
  713.                     {
  714.                     case BN_CLICKED:
  715.                          switch (sWho)
  716.                               {
  717.                               case IDD_LST_GIF:
  718.                                    cp.sLastLoadSaveType = SUB_LOADSAVE_GIF;
  719.                                    return 0;
  720.                               case IDD_LST_BMP:
  721.                                    cp.sLastLoadSaveType = SUB_LOADSAVE_BMP;
  722.                                    return 0;
  723.                               case IDD_LST_MET:
  724.                                    cp.sLastLoadSaveType = SUB_LOADSAVE_MET;
  725.                                    return 0;
  726.                               case IDD_LST_WIN3BMP:
  727.                                    cp.sLastLoadSaveType = SUB_LOADSAVE_WIN3BMP;
  728.                                    return 0;
  729.                               case IDD_LST_PCX:
  730.                                    cp.sLastLoadSaveType = SUB_LOADSAVE_PCX;
  731.                                    return 0;
  732.                               }
  733.                     }
  734.                break;
  735.                }
  736.  
  737.           case WM_COMMAND:
  738.                switch (COMMANDMSG(&msg)->cmd)
  739.                     {
  740.                     case DID_OK:
  741.                          WinDismissDlg (hwnd, TRUE) ;
  742.                          return 0 ;
  743.  
  744.                     case DID_CANCEL:
  745.                          WinDismissDlg (hwnd, FALSE) ;
  746.                          return 0 ;
  747.                     }
  748.                break ;
  749.  
  750.           case WM_HELP:
  751.                SimpleHelp(hab, hwnd, szTitleBar,
  752.                        (HMODULE) 0, IDT_TEXT, IDT_HELP_OPERATE);
  753.  
  754.                return 0;
  755.  
  756.                break;
  757.           }
  758.      return WinDefDlgProc (hwnd, msg, mp1, mp2) ;
  759.      }
  760.  
  761. /* --------------------------------------------------------------------- */
  762.  
  763. /*
  764.    The following helper routine sorts the General Selection
  765.    Names after they have been collected one of the following
  766.    funcitons.
  767.  
  768.    Bubble Sort, Algorithm B, p. 107, Knuth, The Art of Computer
  769.    Programming, Vol 3, Sorting and Searching.
  770. */
  771.  
  772. static void sel_sort(PGENSEL gs)
  773.    {
  774.  
  775.    SHORT bound, j, t;
  776.    char  temp[21];
  777.  
  778.    /* B1: initialize BOUND.  We correct for zero origin here. */
  779.    bound = gs->sCountNames - 1;
  780.  
  781.    do
  782.      {
  783.      /* B2: Loop on j */
  784.      for (t = -1, j = 0; j < bound; j++)
  785.          {
  786.          /* B3: Compare/Exchange R[j], R[j+1] */
  787.          /*   if R[j] > R[j+1] ... */
  788.          if (0 < _fstrcmp(gs->szNames[j], gs->szNames[j+1]) )
  789.             {
  790.             /* swap */
  791.             _fstrcpy(temp, gs->szNames[j]);
  792.             _fstrcpy(gs->szNames[j], gs->szNames[j+1]);
  793.             _fstrcpy(gs->szNames[j+1], temp);
  794.             t = j;
  795.             }
  796.          }
  797.          bound = t;  /* B4: set for restart ... */
  798.       }
  799.                      /*     if we swapped anything */
  800.       while (t >= 0);
  801.  
  802.    }
  803.  
  804. /* --------------------------------------------------------------------- */
  805.  
  806. /*
  807.   Read a formula file, picking off the formula names.
  808.   Formulas use the format "  name = { ... }  name = { ... } "
  809. */
  810.  
  811. static int get_formula_names(PGENSEL gs)  /* get the fractal formula names */
  812. {
  813.    int numformulas, i;
  814.    FILE *File;
  815.    char msg[81], tempstring[201];
  816.  
  817.    gs->szSelected[0] = 0;             /* start by declaring failure */
  818.    for (i = 0; i < MAX_SEL; i++) {
  819.       gs->szNames[i][0] = 0;
  820.       }
  821.  
  822.    _fstrcpy(tempstring, npTempParms.szFormFileName);
  823.    if((File = fopen(tempstring, "rt")) == NULL) {
  824.       sprintf(msg,"I Can't find %s", tempstring);
  825.       stopmsg(1,msg);
  826.       return(-1);
  827.    }
  828.  
  829.    numformulas = 0;
  830.    while(fscanf(File, " %F20[^ \n\t({]", gs->szNames[numformulas]) != EOF) {
  831.       int c;
  832.  
  833.       while(c = getc(File)) {
  834.          if(c == EOF || c == '{' || c == '\n')
  835.             break;
  836.       }
  837.       if(c == EOF)
  838.          break;
  839.       else if(c != '\n'){
  840.          numformulas++;
  841.          if (numformulas >= MAX_SEL-1) break;
  842. skipcomments:
  843.          if(fscanf(File, "%200[^}]", tempstring) == EOF) break;
  844.          if (getc(File) != '}') goto skipcomments;
  845.          if (_fstricmp(gs->szNames[numformulas-1],"") == 0 ||
  846.              _fstricmp(gs->szNames[numformulas-1],"comment") == 0)
  847.                  numformulas--;
  848.       }
  849.    }
  850.    fclose(File);
  851.  
  852.    gs->sCountNames = numformulas;
  853.    /* now sort the names */
  854.    sel_sort(gs);
  855.    return(0);
  856. }
  857.  
  858. /* --------------------------------------------------------------------- */
  859.  
  860. static int get_lsys_name(PGENSEL gs)         /* get the Lsystem formula name */
  861. {
  862.    int numformulas, i;
  863.    FILE *File;
  864.    char buf[201], tempstring[201];
  865.  
  866.    for (i = 0; i < MAX_SEL; i++) {
  867.       gs->szNames[i][0] = 0;
  868.       }
  869.  
  870.    _fstrcpy(tempstring, npTempParms.szLSysFileName);
  871.    if ((File = fopen(tempstring, "rt")) == NULL) {
  872.       sprintf(buf,"I Can't find %s", tempstring);
  873.       stopmsg(1,buf);
  874.       gs->szSelected[0] = 0;
  875.       return(-1);
  876.       }
  877.  
  878.    numformulas = 0;
  879.    while (1) {
  880.       int c;
  881.       gs->szNames[numformulas][0] = 0;
  882.       if (fscanf(File, " %F20[^ \n\t({]", gs->szNames[numformulas]) == EOF)
  883.          break;
  884.       while(c = getc(File)) {
  885.          if(c == EOF || c == '{' || c == '\n')
  886.             break;
  887.          }
  888.       if(c == EOF)
  889.          break;
  890.       else if(c != '\n') {
  891. skipcomments:
  892.          if(fscanf(File, "%200[^}]", buf) == EOF) break;
  893.          if (getc(File) != '}') goto skipcomments;
  894.          if (_fstricmp(gs->szNames[numformulas],"") != 0 &&
  895.              _fstricmp(gs->szNames[numformulas],"comment") != 0)
  896.                  if (++numformulas >= MAX_SEL) break;
  897.          }
  898.       }
  899.    fclose(File);
  900.  
  901.    gs->sCountNames = numformulas;
  902.    /* now sort the names */
  903.    sel_sort(gs);
  904.    return(0);
  905. }
  906.