home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / windows / winsrc.zip / DIALOG.C < prev    next >
C/C++ Source or Header  |  1990-10-20  |  14KB  |  428 lines

  1. /*
  2.  
  3.     various dialog-box code - there's more in DIALOG2.C
  4.  
  5. */
  6.  
  7. #include "windows.h"
  8. #include "winfract.h"
  9. #include "fractint.h"
  10. #include <string.h>
  11. #include <math.h>
  12. #include <stdio.h>
  13. #include <sys\types.h>
  14. #include <sys\stat.h>
  15.  
  16. extern HWND hwnd;                               /* handle to main window */
  17. extern char szHelpFileName[];                   /* Help file name*/
  18.  
  19. char DialogTitle[128];
  20. char FileName[128];
  21. char PathName[128];
  22. char OpenName[128];
  23. char DefPath[128];
  24. char DefSpec[13];
  25. char DefExt[10];
  26. char str[255];
  27.  
  28. OFSTRUCT OfStruct;                        /* information from OpenFile()     */
  29. struct stat FileStatus;                   /* information from fstat()      */
  30.  
  31.  
  32. /****************************************************************************
  33.  
  34.     FUNCTION: SaveAsDlg(HWND, unsigned, WORD, LONG)
  35.  
  36.     PURPOSE: Allows user to change name to save file to
  37.  
  38.     COMMENTS:
  39.  
  40.         This will initialize the window class if it is the first time this
  41.         application is run.  It then creates the window, and processes the
  42.         message loop until a PostQuitMessage is received.  It exits the
  43.         application by returning the value passed by the PostQuitMessage.
  44.  
  45. ****************************************************************************/
  46.  
  47. BOOL bSaveEnabled = FALSE;
  48.  
  49. extern int time_to_save;
  50.  
  51. int FAR PASCAL SaveAsDlg(hDlg, message, wParam, lParam)
  52. HWND hDlg;
  53. unsigned message;
  54. WORD wParam;
  55. LONG lParam;
  56. {
  57.     char TempName[128];
  58.  
  59.     switch (message) {
  60.  
  61.          case WM_KEYDOWN:
  62.              switch (wParam) {
  63.                  case VK_F1:
  64.                  /* F1, shifted F1 bring up the Help Index */
  65.                      WinHelp(hwnd,szHelpFileName,HELP_INDEX,0L);
  66.                      break;
  67.                  }
  68.                
  69.         case WM_INITDIALOG:
  70.  
  71.             SetDlgItemText(hDlg, ID_FILETITLE, DialogTitle);
  72.  
  73.             /* If no filename is entered, don't allow the user to save to it */
  74.  
  75.             if (!FileName[0])
  76.                 bSaveEnabled = FALSE;
  77.             else {
  78.                 bSaveEnabled = TRUE;
  79.  
  80.                 /* Process the path to fit within the IDC_PATH field */
  81.  
  82.                 DlgDirList(hDlg, DefPath, NULL, IDC_PATH, 0x4010);
  83.  
  84.                 /* Send the current filename to the edit control */
  85.  
  86.                 SetDlgItemText(hDlg, IDC_EDIT, FileName);
  87.  
  88.                 /* Accept all characters in the edit control */
  89.  
  90.                 SendDlgItemMessage(hDlg, IDC_EDIT, EM_SETSEL, 0,
  91.                     MAKELONG(0, 0x7fff));
  92.             }
  93.  
  94.             /* Enable or disable the save control depending on whether the
  95.              * filename exists.
  96.              */
  97.  
  98.             EnableWindow(GetDlgItem(hDlg, IDOK), bSaveEnabled);
  99.  
  100.             /* Set the focus to the edit control within the dialog box */
  101.  
  102.             SetFocus(GetDlgItem(hDlg, IDC_EDIT));
  103.             return (FALSE);                 /* FALSE since Focus was changed */
  104.  
  105.         case WM_COMMAND:
  106.             switch (wParam) {
  107.                 case IDC_EDIT:
  108.  
  109.                     /* If there was previously no filename in the edit
  110.                      * control, then the save control must be enabled as soon as
  111.                      * a character is entered.
  112.                      */
  113.  
  114.                     if (HIWORD(lParam) == EN_CHANGE && !bSaveEnabled)
  115.                     EnableWindow(GetDlgItem(hDlg, IDOK), bSaveEnabled = TRUE);
  116.                     return (TRUE);
  117.  
  118.                 case IDOK:
  119.  
  120.                    /* Get the filename from the edit control */
  121.  
  122.                     GetDlgItemText(hDlg, IDC_EDIT, TempName, 128);
  123.  
  124.                     /* If there are no wildcards, then separate the name into
  125.                      * path and name.  If a path was specified, replace the
  126.                      * default path with the new path.
  127.                      */
  128.  
  129.                     if (CheckFileName(hDlg, FileName, TempName)) {
  130.                         SeparateFile(hDlg, (LPSTR) str, (LPSTR) DefSpec,
  131.                             (LPSTR) FileName);
  132.                         if (str[0])
  133.                             strcpy(DefPath, str);
  134.  
  135.                         /* Tell the caller a filename was selected */
  136.  
  137.                         EndDialog(hDlg, IDOK);
  138.                         time_to_save = 1;
  139.                     }
  140.                     return (TRUE);
  141.  
  142.                 case IDCANCEL:
  143.  
  144.                     /* Tell the caller the user canceled the SaveAs function */
  145.  
  146.                     EndDialog(hDlg, IDCANCEL);
  147.                     time_to_save = 1;
  148.                     return (FALSE);
  149.             }
  150.             break;
  151.  
  152.     }
  153.     return (FALSE);
  154. }
  155.  
  156. /****************************************************************************
  157.  
  158.     FUNCTION: OpenDlg(HWND, unsigned, WORD, LONG)
  159.  
  160.     PURPOSE: Let user select a file, and return.  Open code not provided.
  161.  
  162. ****************************************************************************/
  163.  
  164. HANDLE FAR PASCAL OpenDlg(hDlg, message, wParam, lParam)
  165. HWND hDlg;
  166. unsigned message;
  167. WORD wParam;
  168. LONG lParam;
  169. {
  170.     WORD index;
  171.     PSTR pTptr;
  172.     HANDLE hFile=1;     /* Temp value for return */
  173.  
  174.     switch (message) {
  175.  
  176.         case WM_KEYDOWN:
  177.             switch (wParam) {
  178.                 case VK_F1:
  179.                 /* F1, shifted F1 bring up the Help Index */
  180.                     WinHelp(hwnd,szHelpFileName,HELP_INDEX,0L);
  181.                     break;
  182.                 }
  183.  
  184.         case WM_COMMAND:
  185.             switch (wParam) {
  186.  
  187.                 case IDC_LISTBOX:
  188.                     switch (HIWORD(lParam)) {
  189.  
  190.                         case LBN_SELCHANGE:
  191.                             /* If item is a directory name, append "*.*" */
  192.                             if (DlgDirSelect(hDlg, str, IDC_LISTBOX)) 
  193.                                 strcat(str, DefSpec);
  194.  
  195.                             SetDlgItemText(hDlg, IDC_EDIT, str);
  196.                             SendDlgItemMessage(hDlg,
  197.                                 IDC_EDIT,
  198.                                 EM_SETSEL,
  199.                                 NULL,
  200.                                 MAKELONG(0, 0x7fff));
  201.                             break;
  202.  
  203.                         case LBN_DBLCLK:
  204.                             goto openfile;
  205.                     }
  206.                     return (TRUE);
  207.  
  208.                 case IDOK:
  209. openfile:
  210.                     GetDlgItemText(hDlg, IDC_EDIT, OpenName, 128);
  211.                     if (strchr(OpenName, '*') || strchr(OpenName, '?')) {
  212.                         SeparateFile(hDlg, (LPSTR) str, (LPSTR) DefSpec,
  213.                             (LPSTR) OpenName);
  214.                         if (str[0])
  215.                             strcpy(DefPath, str);
  216.                         ChangeDefExt(DefExt, DefSpec);
  217.                         UpdateListBox(hDlg);
  218.                         return (TRUE);
  219.                     }
  220.  
  221.                     if (!OpenName[0]) {
  222.                         MessageBox(hDlg, "No filename specified.",
  223.                             NULL, MB_OK | MB_ICONHAND);
  224.                         return (TRUE);
  225.                     }
  226.  
  227.                     AddExt(OpenName, DefExt);
  228.                     strcpy(FileName, OpenName);
  229.  
  230.                     /* The routine to open the file would go here, and the */
  231.                     /* file handle would be returned instead of NULL.           */
  232.                     EndDialog(hDlg, hFile);
  233.                     return (TRUE);
  234.  
  235.                 case IDCANCEL:
  236.                     EndDialog(hDlg, NULL);
  237.                     return (FALSE);
  238.             }
  239.             break;
  240.  
  241.         case WM_INITDIALOG:                        /* message: initialize    */
  242.             UpdateListBox(hDlg);
  243.             SetDlgItemText(hDlg, ID_FILETITLE, DialogTitle);
  244.             SetDlgItemText(hDlg, IDC_EDIT, DefSpec);
  245.             SendDlgItemMessage(hDlg,               /* dialog handle      */
  246.                 IDC_EDIT,                          /* where to send message  */
  247.                 EM_SETSEL,                         /* select characters      */
  248.                 NULL,                              /* additional information */
  249.                 MAKELONG(0, 0x7fff));              /* entire contents      */
  250.             SetFocus(GetDlgItem(hDlg, IDC_EDIT));
  251.             return (FALSE); /* Indicates the focus is set to a control */
  252.     }
  253.     return FALSE;
  254. }
  255.  
  256. /****************************************************************************
  257.  
  258.     FUNCTION: UpdateListBox(HWND);
  259.  
  260.     PURPOSE: Update the list box of OpenDlg
  261.  
  262. ****************************************************************************/
  263.  
  264. void UpdateListBox(hDlg)
  265. HWND hDlg;
  266. {
  267.     strcpy(str, DefPath);
  268.     strcat(str, DefSpec);
  269.     DlgDirList(hDlg, str, IDC_LISTBOX, IDC_PATH, 0x4010);
  270.  
  271.     /* To ensure that the listing is made for a subdir. of
  272.      * current drive dir...
  273.      */
  274.     if (!strchr (DefPath, ':'))
  275.     DlgDirList(hDlg, DefSpec, IDC_LISTBOX, IDC_PATH, 0x4010);
  276.  
  277.     /* Remove the '..' character from path if it exists, since this
  278.      * will make DlgDirList move us up an additional level in the tree
  279.      * when UpdateListBox() is called again.
  280.      */
  281.     if (strstr (DefPath, ".."))
  282.     DefPath[0] = '\0';
  283.  
  284.     SetDlgItemText(hDlg, IDC_EDIT, DefSpec);
  285. }
  286.  
  287. /****************************************************************************
  288.  
  289.     FUNCTION: ChangeDefExt(PSTR, PSTR);
  290.  
  291.     PURPOSE: Change the default extension
  292.  
  293. ****************************************************************************/
  294.  
  295. void ChangeDefExt(Ext, Name)
  296. PSTR Ext, Name;
  297. {
  298.     PSTR pTptr;
  299.  
  300.     pTptr = Name;
  301.     while (*pTptr && *pTptr != '.')
  302.         pTptr++;
  303.     if (*pTptr)
  304.         if (!strchr(pTptr, '*') && !strchr(pTptr, '?'))
  305.             strcpy(Ext, pTptr);
  306. }
  307.  
  308. /****************************************************************************
  309.  
  310.     FUNCTION: SeparateFile(HWND, LPSTR, LPSTR, LPSTR)
  311.  
  312.     PURPOSE: Separate filename and pathname
  313.  
  314. ****************************************************************************/
  315.  
  316. void SeparateFile(hDlg, lpDestPath, lpDestFileName, lpSrcFileName)
  317. HWND hDlg;
  318. LPSTR lpDestPath, lpDestFileName, lpSrcFileName;
  319. {
  320.     LPSTR lpTmp;
  321.     char  cTmp;
  322.  
  323.     lpTmp = lpSrcFileName + (long) lstrlen(lpSrcFileName);
  324.     while (*lpTmp != ':' && *lpTmp != '\\' && lpTmp > lpSrcFileName)
  325.         lpTmp = AnsiPrev(lpSrcFileName, lpTmp);
  326.     if (*lpTmp != ':' && *lpTmp != '\\') {
  327.         lstrcpy(lpDestFileName, lpSrcFileName);
  328.         lpDestPath[0] = 0;
  329.         return;
  330.     }
  331.     lstrcpy(lpDestFileName, lpTmp + 1);
  332.     cTmp = *(lpTmp + 1);
  333.     lstrcpy(lpDestPath, lpSrcFileName);
  334.      *(lpTmp + 1) = cTmp;
  335.     lpDestPath[(lpTmp - lpSrcFileName) + 1] = 0;
  336. }
  337.  
  338. /****************************************************************************
  339.  
  340.     FUNCTION: AddExt(PSTR, PSTR);
  341.  
  342.     PURPOSE: Add default extension
  343.  
  344. ***************************************************************************/
  345.  
  346. void AddExt(Name, Ext)
  347. PSTR Name, Ext;
  348. {
  349.     PSTR pTptr;
  350.  
  351.     pTptr = Name;
  352.     while (*pTptr && *pTptr != '.')
  353.         pTptr++;
  354.     if (*pTptr != '.')
  355.         strcat(Name, Ext);
  356. }
  357.  
  358. /****************************************************************************
  359.  
  360.     FUNCTION: CheckFileName(HWND, PSTR, PSTR)
  361.  
  362.     PURPOSE: Check for wildcards, add extension if needed
  363.  
  364.     COMMENTS:
  365.  
  366.         Make sure you have a filename and that it does not contain any
  367.         wildcards.  If needed, add the default extension.  This function is
  368.         called whenever your application wants to save a file.
  369.  
  370. ****************************************************************************/
  371.  
  372. BOOL CheckFileName(hWnd, pDest, pSrc)
  373. HWND hWnd;
  374. PSTR pDest, pSrc;
  375. {
  376.     PSTR pTmp;
  377.  
  378.     if (!pSrc[0])
  379.         return (FALSE);               /* Indicates no filename was specified */
  380.  
  381.     pTmp = pSrc;
  382.     while (*pTmp) {                     /* Searches the string for wildcards */
  383.         switch (*pTmp++) {
  384.             case '*':
  385.             case '?':
  386.                 MessageBox(hWnd, "Wildcards not allowed.",
  387.                     NULL, MB_OK | MB_ICONEXCLAMATION);
  388.                 return (FALSE);
  389.         }
  390.     }
  391.  
  392.     AddExt(pSrc, DefExt);            /* Adds the default extension if needed */
  393.  
  394.     if (OpenFile(pSrc, (LPOFSTRUCT) &OfStruct, OF_EXIST) >= 0) {
  395.         sprintf(str, "Replace existing %s?", pSrc);
  396.         if (MessageBox(hWnd, str, "PrntFile",
  397.                 MB_OKCANCEL | MB_ICONEXCLAMATION) == IDCANCEL)
  398.             return (FALSE);
  399.     }
  400.     strcpy(pDest, pSrc);
  401.     return (TRUE);
  402. }
  403.  
  404. /****************************************************************************
  405.  *                                        *
  406.  *  FUNCTION   : GetPrinterDC()                         *
  407.  *                                        *
  408.  *  PURPOSE    : Read WIN.INI for default printer and create a DC for it.   *
  409.  *                                        *
  410.  *  RETURNS    : A handle to the DC if successful or NULL otherwise.        *
  411.  *                                        *
  412.  ****************************************************************************/
  413. HDC PASCAL GetPrinterDC()
  414. {
  415.     static char szPrinter [80];
  416.     char    *szDevice, *szDriver, *szOutput;
  417.  
  418.     GetProfileString ("windows", "device", "", szPrinter, sizeof(szPrinter));
  419.  
  420.     if ((szDevice = strtok (szPrinter, "," )) &&
  421.     (szDriver = strtok (NULL,      ", ")) &&
  422.     (szOutput = strtok (NULL,      ", ")))
  423.  
  424.     return CreateDC (szDriver, szDevice, szOutput, NULL) ;
  425.  
  426.     return NULL;
  427. }
  428.