home *** CD-ROM | disk | FTP | other *** search
/ swCHIP 1991 January / swCHIP_95-1.bin / utility / gsview13 / gsgrab / gsgrab.c < prev    next >
C/C++ Source or Header  |  1995-12-09  |  9KB  |  367 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. /* gsgrab.c */
  19. /* Main window routine for GSgrab */
  20.  
  21. /* Add a line "GSGRAB="  to the [Ports] section of win.ini */
  22. /* Print using a PostScript driver connected to GSGRAB */
  23. /* This program loops, waiting until it can get exclusive access */
  24. /* to c:\windows\gsgrab */
  25. /* When this occurs, it runs Ghostscript, waits until Ghostscript */
  26. /* has exited then deletes c:\windows\gsgrab */
  27.  
  28.  
  29. /* add WM_TIMER */
  30. /* make shift sensing global to EditProc and ButtonProc */
  31. /* make main window a simple status display, with button */
  32. /*  to bring up dialog box to set other options */
  33. /* find location of window directory */
  34. /* start minimized */
  35.  
  36. #define STRICT
  37. #include <windows.h>
  38. #include <windowsx.h>
  39. #include <string.h>
  40. #include <stdlib.h>
  41. #include <stdio.h>
  42. #include "gsgrab.h"
  43.  
  44. HWND hwndgrab;
  45. HICON hicongrab;
  46. HINSTANCE phInstance;
  47. POINT char_size;
  48. HFILE hf;
  49.  
  50. char szAppName[] = "GSgrab";
  51. char szHelpName[] = "gsgrab.hlp";
  52. char szCommand[256];
  53. char szGrabFile[128];
  54. char szStatus[128];
  55.  
  56. char szGhostscript[256];
  57. char szPrinter[64];
  58. char szResolution[64];
  59. char szPort[64];
  60. char szInterval[20];
  61.  
  62. char szIniName[]="gsgrab.ini";
  63. char szOptionSection[]="Options";
  64. char szDevSection[]="Devices";
  65. char szDefCommand[]="c:\\gs\\gswin -Ic:\\gs;c:\\gs\\fonts;c:\\psfonts";
  66. char szUnknown[]="<Unknown>";
  67.  
  68. BOOL printing;
  69. int interval;
  70. BOOL bTimerSet;            /* true if TIMER running */
  71. #define ID_MYTIMER 1
  72.  
  73. HWND hbutton[NBUTTON];
  74. void set_focus(HWND hwnd, WORD id);
  75. void change_focus(HWND hwnd, WORD key, BOOL shift);
  76. LRESULT CALLBACK _export ButtonProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
  77. WNDPROC    lpfnButtonWndProc;
  78.  
  79.  
  80. int PASCAL 
  81. WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int cmdShow)
  82. {
  83.     MSG msg;
  84.     phInstance = hInstance;
  85.  
  86.     if (hPrevInstance) {
  87.         hwndgrab = FindWindow(szAppName, szAppName);
  88.         BringWindowToTop(hwndgrab);
  89.         return 1;
  90.     }
  91.  
  92.     init_window();
  93.     strcpy(szStatus, "Idle");
  94.     /* ShowWindow(hwnd, SW_SHOWMINIMIZED); */
  95.     ShowWindow(hwndgrab, cmdShow);
  96.     init_profile();
  97.  
  98.     if (lpszCmdLine[0] != '\0')
  99.         MessageBox(hwndgrab, "Arguments Ignored", szAppName, MB_OK);
  100.  
  101.     while (GetMessage(&msg, (HWND)NULL, 0, 0)) {
  102.         TranslateMessage(&msg);
  103.         DispatchMessage(&msg);
  104.     }
  105.  
  106.     DestroyWindow(hwndgrab);
  107.     return 0;
  108. }
  109.  
  110. BOOL
  111. start_timer(void)
  112. {
  113.     if (SetTimer(hwndgrab, ID_MYTIMER, interval*1000, NULL) != 0) {
  114.         bTimerSet = TRUE;
  115.         return TRUE;
  116.     }
  117.     bTimerSet = FALSE;
  118.     MessageBox(hwndgrab, "No timers available", szAppName, MB_OK);
  119.     return FALSE;
  120. }
  121.  
  122. void
  123. stop_timer(void)
  124. {
  125.     if (bTimerSet)
  126.         KillTimer(hwndgrab, ID_MYTIMER);
  127.     bTimerSet = FALSE;
  128. }
  129.  
  130. BOOL
  131. call_gs(void)
  132. {
  133.     MSG msg;
  134.     HINSTANCE hinst;
  135.  
  136.     strcpy(szStatus, "Starting Ghostscript");
  137.     InvalidateRect(hwndgrab, NULL, TRUE);
  138.     UpdateWindow(hwndgrab);
  139.  
  140.     printing = TRUE;
  141. /*
  142.     hinst = (HINSTANCE)WinExec(szCommand, SW_SHOWMINNOACTIVE);
  143. */
  144.     hinst = (HINSTANCE)WinExec(szCommand, SW_SHOWNORMAL);
  145. #ifdef __WIN32__
  146.     if (hinst == NULL)
  147. #else
  148.     if (hinst < HINSTANCE_ERROR)
  149. #endif
  150.     {
  151.     char buf[256];
  152.     strcpy(szStatus, "Idle");
  153.     InvalidateRect(hwndgrab, NULL, TRUE);
  154.     strcpy(buf, "Can't run ");
  155.     strcat(buf, szCommand);
  156.     MessageBox(hwndgrab, buf, szAppName, MB_OK);
  157.     hinst = (HINSTANCE)NULL;
  158.     printing = FALSE;
  159.     return FALSE;
  160.     }
  161.  
  162.     strcpy(szStatus, "Printing with Ghostscript");
  163.     InvalidateRect(hwndgrab, NULL, TRUE);
  164.     UpdateWindow(hwndgrab);
  165.  
  166.     /* wait until Ghostscript finishes */
  167.     while (GetModuleUsage(hinst)) {
  168.         while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
  169.             TranslateMessage(&msg);
  170.             DispatchMessage(&msg);
  171.         }
  172.     }
  173.  
  174.     strcpy(szStatus, "Idle");
  175.     InvalidateRect(hwndgrab, NULL, TRUE);
  176.     UpdateWindow(hwndgrab);
  177.  
  178.     printing = FALSE;
  179.     return TRUE;
  180. }
  181.  
  182.  
  183. /* subclass button WndProc to give focus back to parent window */
  184. LRESULT CALLBACK _export
  185. ButtonProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  186. {
  187. static BOOL shift = FALSE;
  188.     switch(message) {
  189.         case WM_KEYDOWN:
  190.         switch(LOWORD(wParam)) {
  191.             case VK_TAB:
  192.             case VK_UP:
  193.             case VK_DOWN:
  194.             case VK_LEFT:
  195.             case VK_RIGHT:
  196.             change_focus(hwnd, LOWORD(wParam), shift);
  197.             return 0;
  198.             case VK_RETURN:
  199.             wParam = VK_SPACE;
  200.             break;
  201.             case VK_SHIFT:
  202.             shift = TRUE;
  203.             break;
  204.         }
  205.         break;
  206.         case WM_KEYUP:
  207.         switch(LOWORD(wParam)) {
  208.             case VK_SHIFT:
  209.             shift = FALSE;
  210.             break;
  211.         }
  212.         break;
  213.     }
  214.     return CallWindowProc(lpfnButtonWndProc, hwnd, message, wParam, lParam);
  215. }
  216.  
  217.  
  218. void
  219. set_focus(HWND hwnd, WORD newid)
  220. {
  221. /* int id = GetWindowWord(hwnd, GWW_ID); */
  222. HWND newhwnd = GetDlgItem(GetParent(hwnd), newid); 
  223.     /* may need to do some fudging if it gives focus to the wrong window */
  224.     SetFocus(newhwnd);
  225. }
  226.  
  227. #define VK_BACKTAB 11    /* vertical tab code */
  228. void
  229. change_focus(HWND hwnd, WORD key, BOOL shift)
  230. {
  231. int id = GetWindowWord(hwnd, GWW_ID);
  232.     if (shift && (key==VK_TAB))
  233.     key = VK_BACKTAB;
  234.     
  235.     switch(id) {
  236.         case IDC_SETUP:
  237.         switch(key) {
  238.         case VK_TAB:
  239.         case VK_RIGHT:
  240.         case VK_DOWN:
  241.             set_focus(hwnd, IDC_CANCEL);
  242.             break;
  243.         case VK_BACKTAB:
  244.         case VK_LEFT:
  245.         case VK_UP:
  246.             set_focus(hwnd, IDC_ABOUT);
  247.             break;
  248.         }
  249.         break;
  250.         case IDC_CANCEL:
  251.         switch(key) {
  252.         case VK_TAB:
  253.         case VK_RIGHT:
  254.         case VK_DOWN:
  255.             set_focus(hwnd, IDC_HELP);
  256.             break;
  257.         case VK_BACKTAB:
  258.         case VK_LEFT:
  259.         case VK_UP:
  260.             set_focus(hwnd, IDC_SETUP);
  261.             break;
  262.         }
  263.         break;
  264.         case IDC_HELP:
  265.         switch(key) {
  266.         case VK_TAB:
  267.         case VK_RIGHT:
  268.         case VK_DOWN:
  269.             set_focus(hwnd, IDC_ABOUT);
  270.             break;
  271.         case VK_BACKTAB:
  272.         case VK_LEFT:
  273.         case VK_UP:
  274.             set_focus(hwnd, IDC_CANCEL);
  275.             break;
  276.         }
  277.         break;
  278.         case IDC_ABOUT:
  279.         switch(key) {
  280.         case VK_TAB:
  281.         case VK_RIGHT:
  282.         case VK_DOWN:
  283.             set_focus(hwnd, IDC_SETUP);
  284.             break;
  285.         case VK_BACKTAB:
  286.         case VK_LEFT:
  287.         case VK_UP:
  288.             set_focus(hwnd, IDC_HELP);
  289.             break;
  290.         }
  291.         break;
  292.     }
  293. }
  294.  
  295. /* Window Procedure */
  296. LRESULT CALLBACK _export
  297. WndGSgrabProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  298. {
  299. HMENU hmenu;
  300. PAINTSTRUCT ps;
  301. HDC hdc;
  302.     switch(message) {
  303.     case WM_PAINT:
  304.         hdc = BeginPaint(hwnd, &ps);
  305.         TextOut(hdc, 1*char_size.x, 6*char_size.y, "Status:", 7);
  306.         TextOut(hdc, 8*char_size.x, 6*char_size.y, szStatus, strlen(szStatus));
  307.         DrawIcon(hdc, 22*char_size.x, char_size.y, hicongrab);
  308.         EndPaint(hwnd, &ps);
  309.         return 0;
  310.     case WM_CREATE:
  311.         hmenu = GetSystemMenu(hwnd,FALSE);
  312.         EnableMenuItem(hmenu, SC_SIZE, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
  313.         EnableMenuItem(hmenu, SC_MAXIMIZE, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
  314.         AppendMenu(hmenu, MF_SEPARATOR, 0, NULL);
  315.         AppendMenu(hmenu, MF_ENABLED, IDC_ABOUT, "&About");
  316.         break;
  317. /*    case WM_CLOSE:   */
  318.     case WM_DESTROY:
  319.         PostQuitMessage(0);
  320.         break;
  321.     case WM_TIMER:
  322.         if ( (hf = _lopen(szGrabFile, READ | OF_SHARE_EXCLUSIVE))
  323.           != HFILE_ERROR ) {
  324.             stop_timer();
  325.         _lclose(hf);
  326.             if (call_gs()) {
  327.             unlink(szGrabFile);
  328.                 start_timer();
  329.         }
  330.             else {
  331.             if (MessageBox(hwndgrab, "Restart Timer?", szAppName, MB_YESNO) == IDYES)
  332.                     start_timer();
  333.             }
  334.         }
  335.         break;
  336.     case WM_COMMAND:
  337.         switch(LOWORD(wParam)) {
  338.         case IDC_SETUP:
  339.             stop_timer();
  340.             if (setup())
  341.             init_profile();
  342.             start_timer();
  343.             return 0;
  344.         case IDC_CANCEL:
  345.             PostQuitMessage(0);
  346.             return 0;
  347.         case IDC_HELP:
  348.             WinHelp(hwndgrab,szHelpName,HELP_CONTENTS,(DWORD)NULL);
  349.             return 0;
  350.         case IDC_ABOUT:
  351.             show_about();
  352.             return 0;
  353.         }
  354.     case WM_SYSCOMMAND:
  355.             switch(LOWORD(wParam)) {
  356.             case IDC_ABOUT:
  357.             show_about();
  358.             return 0;
  359.         }
  360.         break;
  361.     case WM_SETFOCUS:
  362.         set_focus(hbutton[0], IDC_SETUP);
  363.         break;
  364.     }
  365.     return DefWindowProc(hwnd, message, wParam, lParam);
  366. }
  367.