home *** CD-ROM | disk | FTP | other *** search
/ Desktop Works 1995 - 1996 / desktopworks1995-1996.iso / scrnsave / winsaver / winsaver.c < prev    next >
C/C++ Source or Header  |  1996-01-01  |  18KB  |  603 lines

  1. /* winsaver.c RHS 1/25/91
  2.  */
  3.  
  4. #include<windows.h>
  5. #include<stdio.h>
  6. #include<mem.h>
  7. #include<string.h>
  8. #include<stdlib.h>
  9.  
  10. #define MAXWINDOWSPARM  256
  11. #define MAXFILENAME     80
  12. #define MAXWINS         256
  13. #define ICONIC          SW_SHOWMINNOACTIVE
  14.  
  15. #define SC_USER         (0xF000-1)
  16. #define SC_ABOUT        SC_USER
  17. #define SC_AUTOSAVE     SC_USER-1
  18. #define SC_AUTOLOAD     SC_USER-2
  19.  
  20. #define MYTIMER         1
  21. #define MYTIMER2        2
  22.  
  23. typedef struct _windows
  24.     {
  25.     RECT rect;
  26.     int winState;
  27.     HWND hWnd;
  28.     char filename[MAXFILENAME];
  29.     } Wins;
  30. Wins windows[MAXWINS];
  31.  
  32. long FAR PASCAL WndProc(HWND, WORD, WORD, LONG);
  33.  
  34. void SaveSession(void);
  35. void RestoreSession(void);
  36. void GetWindowList(void);
  37. void PaintWindowList(void);
  38. int  ReadProfile(void);
  39. void LoadPrograms(int maxwins);
  40. void RestoreSession(void);
  41. void Execute(char *command, int ncmdshow);
  42. HWND GetWindowHandle(char *name);
  43. BOOL HandleUsed(HWND hWnd);
  44. void ActivateProgMan(void);
  45. void UpdateSystemMenu(void);
  46. void Autoload(void);
  47.  
  48. int getint(char **p);
  49. BOOL bufmatch(char *str, char *winbuf, char *fullname);
  50.  
  51. static char szCopyRight[] = " WinSaver, Copyright (C) Richard Hale Shaw, 1991 ";
  52. static char szWinSaver[] = "WinSaver";
  53. static char szAutoSave[] = "Autosave";
  54. static char szAutoLoad[] = "Autoload";
  55.  
  56. char szAppName[MAXFILENAME];
  57.  
  58. BOOL AutoSave = FALSE;
  59. BOOL AutoLoad = FALSE;
  60.  
  61. #define DEFAULTTIMER    1000
  62. WORD TimerVal = DEFAULTTIMER;
  63.  
  64. HCURSOR hNormalCursor, hHourGlassCursor;
  65. HWND WinSaver;
  66. HANDLE hPrev;
  67. LPSTR CmdLine;
  68.  
  69. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  70.     LPSTR lpCmdLine, int nCmdShow)
  71.     {
  72.     MSG msg;
  73.     WNDCLASS wndclass;
  74.  
  75.     hPrev = hPrevInstance;
  76.  
  77.     if(!hPrevInstance)    
  78.         {
  79.         wndclass.style = NULL;
  80.         wndclass.lpfnWndProc = WndProc;
  81.         wndclass.cbClsExtra = 0;       
  82.         wndclass.cbWndExtra = 0;       
  83.         wndclass.hInstance = hInstance;
  84.         wndclass.hIcon = LoadIcon(hInstance, "WINSAVER");
  85.         wndclass.hCursor = NULL;
  86.         wndclass.hbrBackground = GetStockObject(WHITE_BRUSH); 
  87.         wndclass.lpszMenuName =  NULL;
  88.         wndclass.lpszClassName = szWinSaver;
  89.  
  90.         RegisterClass(&wndclass);
  91.         }
  92.  
  93.     hNormalCursor = LoadCursor(NULL, IDC_ARROW);
  94.     hHourGlassCursor = LoadCursor(NULL, IDC_WAIT);
  95.  
  96.     WinSaver = CreateWindow(szWinSaver,
  97.         "Session Saver",
  98.         WS_OVERLAPPEDWINDOW,            
  99. //        0,
  100.         CW_USEDEFAULT,                  
  101.         CW_USEDEFAULT,                  
  102.         CW_USEDEFAULT,                  
  103.         CW_USEDEFAULT,                  
  104.         NULL,                           
  105.         NULL,                           
  106.         hInstance,                      
  107.         NULL);
  108.  
  109.     UpdateSystemMenu();
  110.  
  111.     ShowWindow(WinSaver, SW_SHOWMINIMIZED);
  112.     UpdateWindow(WinSaver);        
  113.  
  114.     while(GetMessage(&msg, NULL, 0, 0))
  115.         {
  116.         TranslateMessage(&msg);
  117.         DispatchMessage(&msg); 
  118.         }
  119.     return (msg.wParam);  
  120.     }
  121.  
  122.  
  123.  
  124. long FAR PASCAL WndProc(HWND hWnd, WORD message,
  125.     WORD wParam, LONG lParam)
  126.     {
  127.     static BOOL done1 = FALSE;
  128.     static BOOL done2 = FALSE;
  129.  
  130.     switch(message)
  131.         {
  132.         case WM_CREATE:
  133.             {
  134.             WORD w = LOBYTE(GetVersion());
  135.  
  136.             if(w < 3)                           // if Win 1.0 or Win 2.0
  137.                 {
  138.                 MessageBox(hWnd,
  139.                     "Sorry, WinSaver must run on Windows 3.x or higher!",
  140.                     szWinSaver,
  141.                     MB_ICONEXCLAMATION);
  142.                 SendMessage(hWnd,WM_DESTROY,0,0L);
  143.                 }
  144.             else if(hPrev)                      // if a previous instance
  145.                 {
  146.                 MessageBox(hWnd,
  147.                     "WinSaver already running!",
  148.                     szWinSaver,
  149.                     MB_ICONEXCLAMATION);
  150.                 SendMessage(hWnd,WM_DESTROY,0,0L);
  151.                 }
  152.             else
  153.                 SetTimer(hWnd,MYTIMER,50,NULL);
  154.             break;
  155.             }
  156.  
  157.         case WM_INITMENU:           // modify the system menu
  158.             CheckMenuItem(GetSystemMenu(WinSaver,0),
  159.                 SC_AUTOSAVE,
  160.                 (MF_BYCOMMAND | (AutoSave ? MF_CHECKED : MF_UNCHECKED)));
  161.             CheckMenuItem(GetSystemMenu(WinSaver,0),
  162.                 SC_AUTOLOAD,
  163.                 (MF_BYCOMMAND | (AutoLoad ? MF_CHECKED : MF_UNCHECKED)));
  164.             break;
  165.  
  166.         case WM_TIMER:
  167.             {
  168.             if(!done1 && wParam == MYTIMER)
  169.                 {
  170.                 SetCursor(hHourGlassCursor);
  171.                 KillTimer(hWnd,MYTIMER);
  172.                 RestoreSession();
  173.                 GetModuleFileName(GetClassWord(hWnd,GCW_HMODULE),
  174.                         szAppName, MAXFILENAME-1);
  175.                 SetTimer(hWnd,MYTIMER2,TimerVal,NULL);
  176.                 done1 = TRUE;
  177.                 }
  178.             if(!done2 && wParam == MYTIMER2)
  179.                 {
  180.                 char buf[20];
  181.  
  182.                 KillTimer(hWnd,MYTIMER2);
  183.                 SetCursor(hNormalCursor);
  184.                 done2 = TRUE;
  185.                 }
  186.             }
  187.             break;
  188.  
  189.         case WM_QUERYENDSESSION:
  190.             if(AutoSave || (MessageBox(hWnd,"Save this session?",szWinSaver,
  191.                 MB_ICONQUESTION | MB_YESNO) == IDYES))
  192.                 {
  193.                 SetCursor(hHourGlassCursor);
  194.                 GetWindowList();
  195.                 SaveSession();
  196.                 }
  197.             return -1L;
  198.  
  199.         case WM_QUERYOPEN:                      // prevent icon from opening
  200.             return 0L;                          // into a window
  201.  
  202.         case WM_CLOSE:
  203.             DestroyWindow(hWnd);
  204.             break;
  205.  
  206.         case WM_DESTROY:
  207.             PostQuitMessage(0);
  208.             break;
  209.  
  210.         case WM_SYSCOMMAND:
  211.             switch(wParam)
  212.                 {
  213.                 case SC_ABOUT:
  214.                     MessageBox(hWnd,szCopyRight,szWinSaver,
  215.                         MB_ICONINFORMATION);
  216.                     break;
  217.  
  218.                 case SC_AUTOLOAD:
  219.                     Autoload();
  220.                     break;
  221.  
  222.                 case SC_AUTOSAVE:
  223.                     AutoSave = !AutoSave;
  224.                     CheckMenuItem(GetSystemMenu(WinSaver,0),
  225.                         SC_AUTOSAVE,(MF_BYCOMMAND |
  226.                             (AutoSave ? MF_CHECKED : MF_UNCHECKED)));
  227.                     break;
  228.                 default:
  229.                     goto defwindowproc;
  230.                 }
  231.             break;
  232.  
  233.          default:
  234. defwindowproc:
  235.             return DefWindowProc(hWnd, message, wParam, lParam);
  236.         }
  237.     return 0L;
  238.     }
  239.  
  240.  
  241. void UpdateSystemMenu(void)
  242.     {
  243.     HMENU hSysMenu = GetSystemMenu(WinSaver,0);
  244.  
  245.     RemoveMenu(hSysMenu, SC_RESTORE, MF_BYCOMMAND);
  246.     RemoveMenu(hSysMenu, SC_MINIMIZE, MF_BYCOMMAND);
  247.     RemoveMenu(hSysMenu, SC_MAXIMIZE, MF_BYCOMMAND);
  248.     RemoveMenu(hSysMenu, SC_SIZE, MF_BYCOMMAND);
  249.  
  250.     AppendMenu(hSysMenu, MF_SEPARATOR, 0, NULL);
  251.     AppendMenu(hSysMenu, MF_STRING, SC_ABOUT, "About...");
  252.     AppendMenu(hSysMenu, MF_STRING, SC_AUTOSAVE, "Autosave");
  253.     AppendMenu(hSysMenu, MF_STRING, SC_AUTOLOAD, "Autoload");
  254.     }
  255.  
  256. char outbuf[MAXWINDOWSPARM];
  257.  
  258. void Autoload(void)
  259.     {
  260.     char *p;
  261.  
  262.     GetProfileString("windows","load",NULL,outbuf,sizeof(outbuf));
  263.     strupr(outbuf);
  264.  
  265.     AutoLoad = !AutoLoad;
  266.  
  267.         // if Auto Load is selected and app name is not in LOAD= list
  268.     if(AutoLoad && !strstr(outbuf,szAppName))
  269.         {
  270.         strcat(outbuf," ");                     // add blank and name to 
  271.         strcat(outbuf,szAppName);               // LOAD= list
  272.         WriteProfileString("windows","load",outbuf);
  273.         }
  274.         // if Auto Load is not selected and app name is in LOAD= list
  275.     if(!AutoLoad && (p = strstr(outbuf,szAppName)))
  276.         {
  277.         *p = '\0';                              // NULL at start of appname
  278.         p += strlen(szAppName);                 // move past appname
  279.         strcat(outbuf,p);