home *** CD-ROM | disk | FTP | other *** search
/ swCHIP 1991 January / swCHIP_95-1.bin / utility / gsview13 / gsgrab / gsgdlg.c next >
C/C++ Source or Header  |  1995-12-09  |  9KB  |  263 lines

  1. /* Copyright (C) 1993, 1994, Russell Lang.  All rights reserved.
  2.   
  3.   This file is part of GSview.
  4.   
  5.   This program is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the GSview Free Public Licence 
  9.   (the "Licence") for full details.
  10.   
  11.   Every copy of GSview must include a copy of the Licence, normally in a 
  12.   plain ASCII text file named LICENCE.  The Licence grants you the right 
  13.   to copy, modify and redistribute GSview, but only under certain conditions 
  14.   described in the Licence.  Among other things, the Licence requires that 
  15.   the copyright notice and this notice be preserved on all copies.
  16. */
  17.  
  18. /* gsgdlg.c */
  19. /* Dialog boxes for GSgrab */
  20.  
  21. #include <windows.h>
  22. #include <windowsx.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include "gsgrab.h"
  26.  
  27. /* copyright dialog box */
  28. BOOL CALLBACK _export
  29. AboutDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  30. {
  31.     switch(message) {
  32.         case WM_INITDIALOG:
  33.             SetDlgItemText(hDlg, IDC_VERSION, GSGRAB_VERSION);
  34.             return( TRUE);
  35.         case WM_COMMAND:
  36.             switch(LOWORD(wParam)) {
  37.                 case IDOK:
  38.                     EndDialog(hDlg, TRUE);
  39.                     return(TRUE);
  40.                 default:
  41.                     return(FALSE);
  42.             }
  43.         default:
  44.             return(FALSE);
  45.     }
  46. }
  47.  
  48. void
  49. show_about(void)
  50. {
  51. #ifdef __WIN32__
  52.     DialogBoxParam( phInstance, (LPSTR)ABOUTDLG, hwndgrab, AboutDlgProc, (LPARAM)NULL);
  53. #else
  54.     DLGPROC lpProcAbout;
  55.     lpProcAbout = (DLGPROC)MakeProcInstance((FARPROC)AboutDlgProc, phInstance);
  56.     DialogBoxParam( phInstance, (LPSTR)ABOUTDLG, hwndgrab, lpProcAbout, (LPARAM)NULL);
  57.     FreeProcInstance((FARPROC)lpProcAbout);
  58. #endif
  59. }
  60.  
  61.  
  62. void
  63. profile_create_section(char *section, int id)
  64. {  
  65. HGLOBAL hglobal;
  66. LPSTR entry, value;
  67. char name[128];
  68. char val[256];
  69.     hglobal = LoadResource(phInstance, 
  70.         FindResource(phInstance, MAKEINTRESOURCE(id), RT_RCDATA));
  71.     if ( (entry = (LPSTR)LockResource(hglobal)) == (LPSTR)NULL)
  72.         return;
  73.     while (lstrlen(entry)!=0) {
  74.         for ( value = entry; 
  75.           (*value!='\0') && (*value!=',') && (*value!='='); 
  76.           value++)
  77.         /* nothing */;
  78.         _fstrncpy(name, entry, (int)(value-entry));
  79.         name[(int)(value-entry)] = '\0';
  80.         value++;
  81.         _fstrncpy(val, value, sizeof(val));
  82.         WritePrivateProfileString(section, name, val, szIniName);
  83.         entry = value + lstrlen(value) + 1;
  84.     }
  85.     FreeResource(hglobal);
  86. }
  87.  
  88. #define PROFILE_SIZE  2048
  89.  
  90. /* dialog box for GSgrab setup */
  91. BOOL CALLBACK _export
  92. SetupDlgProc(HWND hDlg, UINT wmsg, WPARAM wParam, LPARAM lParam)
  93. {
  94.     char buf[128];
  95.     int idevice;
  96.     WORD notify_message;
  97.     char *p;
  98.     char *res;
  99.     int numentry;
  100.     char entry[128];
  101.  
  102.     switch (wmsg) {
  103.         case WM_INITDIALOG:
  104.         /* initialise Ghostscript command and Interval */
  105.         SetDlgItemText(hDlg, IDC_GSCOMMAND, szGhostscript);
  106.         SetDlgItemText(hDlg, IDC_INTERVAL, szInterval);
  107.         /* allocate buffer for profile items */
  108.         if ( (res = malloc(PROFILE_SIZE)) == (char *)NULL)
  109.             return TRUE;
  110.         /* initialise printer and resolution items */
  111.         p = res;
  112.         GetPrivateProfileString(szDevSection, NULL, "", res, PROFILE_SIZE, szIniName);
  113.         if (strlen(p) == 0) {
  114.             /* [Devices] section doesn't exist.  Initialise from resources */
  115.             profile_create_section(szDevSection, IDR_DEVICES);
  116.         }
  117.         GetPrivateProfileString(szDevSection, NULL, "", res, PROFILE_SIZE, szIniName);
  118.         for (numentry=0; p!=(char *)NULL && strlen(p)!=0; numentry++) {
  119.             SendDlgItemMessage(hDlg, IDC_PRINTER, CB_ADDSTRING, 0, 
  120.             (LPARAM)((LPSTR)p));
  121.             p += strlen(p) + 1;
  122.         }
  123.         /* initialise ports list */
  124.             SendDlgItemMessage(hDlg, IDC_PORT, CB_ADDSTRING, 0, 
  125.             (LPARAM)(szUnknown));
  126.         GetProfileString("Ports", NULL, "", res, PROFILE_SIZE);
  127.         p = res;
  128.         for (numentry=0; p!=(char *)NULL && strlen(p)!=0; numentry++) {
  129.             if (strcmp(p, "GSGRAB"))    /* don't let user cause infinite loop */
  130.             SendDlgItemMessage(hDlg, IDC_PORT, CB_ADDSTRING, 0, 
  131.             (LPARAM)((LPSTR)p));
  132.             p += strlen(p) + 1;
  133.         }
  134.         /* free profile buffer */
  135.         free(res);
  136.         /* select initial printer */
  137.         if (SendDlgItemMessage(hDlg, IDC_PRINTER, CB_SELECTSTRING, 0, (LPARAM)(LPSTR)szPrinter)
  138.             == CB_ERR)
  139.             SetWindowText(GetDlgItem(hDlg, IDC_PRINTER), szPrinter);
  140. /*
  141.             SendDlgItemMessage(hDlg, IDC_PRINTER, CB_SETCURSEL, 0, 0L);
  142. */
  143.         /* force update of IDC_RES */
  144.         SendDlgNotification(hDlg, IDC_PRINTER, CBN_SELCHANGE);
  145.         if (SendDlgItemMessage(hDlg, IDC_RES, CB_SELECTSTRING, 0, (LPARAM)(LPSTR)szResolution)
  146.             == CB_ERR)
  147.             SetWindowText(GetDlgItem(hDlg, IDC_RES), szResolution);
  148. /*
  149.             SendDlgItemMessage(hDlg, IDC_RES, CB_SETCURSEL, 0, 0L);
  150. */
  151.         /* select initial port */
  152.         if (SendDlgItemMessage(hDlg, IDC_PORT, CB_SELECTSTRING, 0, (LPARAM)(LPSTR)szPort)
  153.             == CB_ERR)
  154.             SendDlgItemMessage(hDlg, IDC_PRINTER, CB_SETCURSEL, 0, 0L);
  155.         return TRUE;
  156.         case WM_COMMAND:
  157.         notify_message = GetNotification(wParam,lParam);
  158.         switch (LOWORD(wParam)) {
  159.             case IDC_GSCOMMAND:
  160.             /* don't have anything to do */
  161.             return FALSE;
  162.             case IDC_PRINTER:
  163.             if (notify_message != CBN_SELCHANGE) {
  164.                 return FALSE;
  165.             }
  166.             idevice = (int)SendDlgItemMessage(hDlg, IDC_PRINTER, CB_GETCURSEL, 0, 0L);
  167.             if (idevice == CB_ERR) {
  168.                 return FALSE;
  169.             }
  170.             SendDlgItemMessage(hDlg, IDC_PRINTER, CB_GETLBTEXT, idevice, (LPARAM)(LPSTR)entry);
  171.             /* now look up entry in gsgrab.ini */
  172.             /* and update IDC_RES list box */
  173.             GetPrivateProfileString(szDevSection, entry, "", buf, sizeof(buf)-2, szIniName);
  174.             buf[strlen(buf)+1] = '\0';    /* double NULL at end */
  175.                 SendDlgItemMessage(hDlg, IDC_RES, CB_RESETCONTENT, 0, 0L);
  176.             p = buf;
  177.             if (*p == '\0') {
  178.                 /* no resolutions can be set */
  179.                 EnableWindow(GetDlgItem(hDlg, IDC_RES), FALSE);
  180.                 EnableWindow(GetDlgItem(hDlg, IDC_RESTEXT), FALSE);
  181.             }
  182.             else {
  183.               EnableWindow(GetDlgItem(hDlg, IDC_RES), TRUE);
  184.               EnableWindow(GetDlgItem(hDlg, IDC_RESTEXT), TRUE);
  185.               while (*p!='\0') {
  186.                 res = p;
  187.                 while ((*p!='\0') && (*p!=','))
  188.                 p++;
  189.                 *p++ = '\0';
  190.                     SendDlgItemMessage(hDlg, IDC_RES, CB_ADDSTRING, 0, 
  191.                     (LPARAM)((LPSTR)res));
  192.               }
  193.             }
  194.             SendDlgItemMessage(hDlg, IDC_RES, CB_SETCURSEL, 0, 0L);
  195.             if (SendDlgItemMessage(hDlg, IDC_RES, CB_GETLBTEXT, 0, (LPARAM)(LPSTR)buf)
  196.                 != CB_ERR)
  197.                     SetDlgItemText(hDlg, IDC_RES, buf);
  198.             return FALSE;
  199.             case IDC_RES:
  200.             /* don't have anything to do */
  201.             return FALSE;
  202.             case IDC_PORT:
  203.             /* don't have anything to do */
  204.             return FALSE;
  205.             case IDC_INTERVAL:
  206.             /* don't have anything to do */
  207.             return FALSE;
  208.             case IDOK:
  209.             /* save dialog items */
  210.                 GetDlgItemText(hDlg, IDC_GSCOMMAND, szGhostscript, sizeof(szGhostscript));
  211.                 GetDlgItemText(hDlg, IDC_PRINTER, szPrinter, sizeof(szPrinter));
  212.                 GetDlgItemText(hDlg, IDC_RES, szResolution, sizeof(szResolution));
  213.                 GetDlgItemText(hDlg, IDC_PORT, szPort, sizeof(szPort));
  214.                 GetDlgItemText(hDlg, IDC_INTERVAL, szInterval, sizeof(szInterval));
  215.             EndDialog(hDlg, TRUE);
  216.             return TRUE;
  217.             case IDCANCEL:
  218.             EndDialog(hDlg, FALSE);
  219.             return TRUE;
  220.             case IDC_HELP:
  221.             WinHelp(hDlg,szHelpName,HELP_KEY,(DWORD)"Setup");
  222.             return FALSE;
  223.         }
  224.         break;
  225.     }
  226.     return FALSE;
  227. }
  228.  
  229.  
  230.  
  231. /* Set up GSgrab options */
  232. BOOL
  233. setup(void)
  234. {
  235.     BOOL flag;
  236.  
  237. #ifdef __WIN32__
  238.     flag = DialogBoxParam( phInstance, GRABDLG, hwndgrab, SetupDlgProc, (LPARAM)NULL);
  239. #else
  240.     DLGPROC lpProcSetup;
  241.     lpProcSetup = (DLGPROC)MakeProcInstance((FARPROC)SetupDlgProc, phInstance);
  242.     flag = DialogBoxParam( phInstance, (LPSTR)GRABDLG, hwndgrab, lpProcSetup, (LPARAM)NULL);
  243.     FreeProcInstance((FARPROC)lpProcSetup);
  244. #endif
  245.     if (!flag)
  246.         return FALSE;
  247.     interval = atoi(szInterval);
  248.     if (interval > 60) {
  249.         interval = 60;
  250.         strcpy(szInterval, "60");
  251.     }
  252.     /* save to INI file */
  253.     WritePrivateProfileString(szOptionSection, "Ghostscript",  szGhostscript,  szIniName);
  254.     WritePrivateProfileString(szOptionSection, "Printer",  szPrinter,  szIniName);
  255.     WritePrivateProfileString(szOptionSection, "Resolution", szResolution,  szIniName);
  256.     WritePrivateProfileString(szOptionSection, "Port", szPort,  szIniName);
  257.     WritePrivateProfileString(szOptionSection, "Interval", szInterval,  szIniName);
  258.         WriteProfileString("ports", szGrabFile, "");
  259.     WritePrivateProfileString(szOptionSection, "Version", GSGRAB_VERSION,  szIniName);
  260.     return TRUE;
  261. }
  262.  
  263.