home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / WEBSERVE / SAMBAR / DATA.1 / winmain.c < prev    next >
C/C++ Source or Header  |  1997-04-27  |  11KB  |  437 lines

  1. /*
  2. ** WINMAIN
  3. **
  4. **      This is the main driver for the Windows driver of the Sambar
  5. **        Server. 
  6. **
  7. **        Confidential Property of Tod Sambar
  8. **        (c) Copyright Tod Sambar 1995-1997
  9. **        All rights reserved.
  10. **
  11. **
  12. ** Syntax:
  13. **
  14. **      server
  15. **
  16. **
  17. ** History:
  18. ** Chg#    Date    Description                                                Resp
  19. ** ----    -------    -------------------------------------------------------    ----
  20. **        9MAR97     Created                                                    sambar
  21. */
  22.  
  23. #include    <windows.h>
  24. #include    <process.h>
  25. #include    <sambar.h>
  26. #include    <resource.h>
  27.  
  28. /*
  29. ** Local Defines
  30. */
  31. #define SERVER_ID     1001
  32. #define SERVER_MSG     WM_USER + 69
  33. #define NAME        "Sambar Server"
  34.  
  35. static HWND            MainWindow;
  36. static HANDLE        MainInstance;
  37. static BOOL            ShellTray = FALSE;
  38.  
  39.  
  40. /*
  41. ** Local Prototypes
  42. */
  43. long __stdcall         WndProc(
  44.                     HWND         hWnd, 
  45.                     UINT         message, 
  46.                     WPARAM         wParam,
  47.                     LPARAM         lParam
  48.                     );
  49. LRESULT CALLBACK     About(
  50.                     HWND         hDlg, 
  51.                     UINT         message, 
  52.                     WPARAM         wParam, 
  53.                     LPARAM         lParam
  54.                     );
  55. BOOL                 CenterWindow(
  56.                     HWND         hwndChild, 
  57.                     HWND         hwndParent
  58.                     );
  59. void                DisplayMenu(
  60.                     HWND         hWnd
  61.                     );
  62. void                ShutdownServer(
  63.                     HWND         hWnd
  64.                     );
  65.  
  66. int     __stdcall
  67. WinMain(HANDLE Instance, HANDLE PrevInstance, LPSTR CmdLine, int CmdShow)
  68. {
  69.     unsigned long    thread;
  70.     SA_BOOL            ver3;
  71.     DWORD            dwVersion;        // To hold return value from GetVersion
  72.     MSG                message;
  73.     HWND            hWnd;
  74.     WNDCLASS        MainClass;
  75.     HICON            ServerIcon;
  76.     NOTIFYICONDATA    IconData;
  77.  
  78.     /* Save the application Instance                                    */
  79.     MainInstance = Instance;
  80.  
  81.     /* If a Server is running, show it                                    */
  82.     hWnd = FindWindow(NAME, NULL);
  83.     if (hWnd)
  84.     {
  85.         if (IsIconic(hWnd))
  86.             ShowWindow(hWnd, SW_RESTORE);
  87.  
  88.         SetForegroundWindow(hWnd);
  89.         return (0);
  90.     } 
  91.  
  92.     /* Create the tray Icon resource                                    */
  93.     ServerIcon = LoadIcon(Instance, "SERVER_ICON");
  94.  
  95.     /* Create a window class                                            */
  96.     MainClass.style =             CS_HREDRAW | CS_VREDRAW;
  97.     MainClass.lpfnWndProc =        WndProc;
  98.     MainClass.cbClsExtra =         0;
  99.     MainClass.cbWndExtra =         0;
  100.     MainClass.hInstance =         Instance;
  101.     MainClass.hIcon =             ServerIcon;
  102.     MainClass.hCursor =         LoadCursor(NULL, IDC_ARROW);
  103.     MainClass.hbrBackground =     GetStockObject(WHITE_BRUSH);
  104.     MainClass.lpszMenuName =     "MAIN";
  105.     MainClass.lpszClassName =     NAME;
  106.  
  107.     if (!RegisterClass(&MainClass))
  108.         return (0);
  109.  
  110.     /* Determine if this is windows 3.51 -- change the window type        */
  111.     ver3 = 0;
  112.     dwVersion = GetVersion();
  113.     if (dwVersion < 0x80000000) 
  114.     {
  115.         if ((DWORD)(LOBYTE(LOWORD(dwVersion))) == 3)
  116.             ver3 = 1;
  117.     }
  118.  
  119.     /* Create the main window                                            */
  120.     MainWindow = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, NAME, NAME,
  121.         ver3 ?     (WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX) : 
  122.                 (WS_CAPTION | WS_SYSMENU),
  123.         CW_USEDEFAULT, 0, 250, 80, NULL, NULL, Instance, NULL);
  124.  
  125.     /* Add text to the main window                                        */
  126.     hWnd = CreateWindow("STATIC", "Version Info", WS_CHILD | WS_VISIBLE, 
  127.             0, 0, 250, 80, MainWindow, NULL, Instance, NULL);
  128.     SetWindowText(hWnd, "Sambar Server is Active");
  129.  
  130.     /* Add tray icon to system tray                                        */
  131.     IconData.cbSize             = sizeof(NOTIFYICONDATA);
  132.     IconData.hWnd                 = MainWindow;
  133.     IconData.uID                 = SERVER_ID;
  134.     IconData.uFlags             = NIF_MESSAGE | NIF_ICON | NIF_TIP;
  135.     IconData.uCallbackMessage    = SERVER_MSG;
  136.     IconData.hIcon                = ServerIcon;
  137.     strcpy(IconData.szTip, "Sambar Server is Active");
  138.  
  139.     /* Start the Sambar Server                                            */
  140.     /* Win95 and NT4.0 only                                                */
  141.     if (!Shell_NotifyIcon(NIM_ADD, &IconData))
  142.     {
  143.         ShowWindow(MainWindow, CmdShow);
  144.         UpdateWindow(MainWindow);
  145.     }
  146.     else
  147.     {
  148.         ShellTray = TRUE;
  149.         ShowWindow(MainWindow, SW_HIDE);
  150.     }
  151.  
  152.     /*
  153.     ** Start the Sambar Server.
  154.     ** On failure, the server will shutdown and destroy the MainWindow.
  155.     */
  156.     thread = _beginthread(sa_server, 20480, (SA_VOID *)&MainWindow);
  157.     if (thread == -1)
  158.         return (0);
  159.  
  160.     /* Message loop                                                        */
  161.     while (GetMessage(&message, NULL, 0, 0))
  162.         DispatchMessage(&message);
  163.  
  164.     Shell_NotifyIcon(NIM_DELETE, &IconData);
  165.     UnregisterClass(NAME, Instance);
  166.  
  167.     /* Shutdown the Sambar Server                                        */
  168.     (SA_VOID)sa_shutdown();
  169.  
  170.     return (message.wParam);
  171. }
  172.  
  173. long __stdcall 
  174. WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  175. {
  176.     int            wmId;
  177.     int            wmEvent;
  178.  
  179.     switch (message) 
  180.     {
  181.     case WM_COMMAND:
  182.         wmId = LOWORD(wParam);
  183.         wmEvent = HIWORD(wParam);
  184.  
  185.         switch (wmId)
  186.         {
  187.         case IDM_ABOUT:
  188.             DialogBox(MainInstance, "AboutBox", hWnd, (DLGPROC)About);
  189.             break;
  190.  
  191.         case IDM_HIDE:
  192.             if (ShellTray)
  193.                 ShowWindow(MainWindow, SW_HIDE);
  194.             else
  195.                 ShowWindow(MainWindow, SW_MINIMIZE);
  196.             break;
  197.  
  198.         case IDM_EXIT:
  199.             DestroyWindow(hWnd);
  200.             break;
  201.  
  202.         default:
  203.             /* Unhandled Messages end up here (DefWindowProc)            */
  204.             return DefWindowProc(hWnd, message, wParam, lParam);
  205.         }
  206.         break;
  207.  
  208.     case SERVER_MSG:
  209.         if ((wParam == SERVER_ID) && (lParam == WM_RBUTTONDOWN))
  210.             DisplayMenu(hWnd);
  211.         break;
  212.  
  213.     case WM_DESTROY:
  214.         /* Shutdown the main window                                        */
  215.         PostQuitMessage(0);
  216.         break;
  217.  
  218.     default:
  219.         /* Unhandled Messages end up here (DefWindowProc)                */
  220.         return DefWindowProc(hWnd, message, wParam, lParam);
  221.     }
  222.  
  223.     return(0);
  224. }
  225.  
  226. void
  227. DisplayMenu(HWND hWnd)
  228. {
  229.     HMENU     MenuHnd;
  230.     POINT     MousePos;
  231.     int     ScreenWidth;
  232.     int     ScreenHeight;
  233.     int     SelItem;
  234.  
  235.     MenuHnd = CreatePopupMenu();
  236.     AppendMenu(MenuHnd, MF_ENABLED, 1, "Open");
  237.     AppendMenu(MenuHnd, MF_SEPARATOR, 0, NULL);
  238.     AppendMenu(MenuHnd, MF_ENABLED, 2, "Shutdown");
  239.  
  240.     //Get Mouse Pos
  241.     GetCursorPos(&MousePos);
  242.  
  243.     //Get Screen Metrics
  244.     ScreenWidth = GetSystemMetrics(SM_CXSCREEN);
  245.     ScreenHeight = GetSystemMetrics(SM_CYSCREEN);
  246.  
  247.     SetForegroundWindow(MainWindow);
  248.  
  249.     //Handle the different possible task bar locations
  250.     if ((MousePos.x >= (ScreenWidth / 2)) && (MousePos.y >= (ScreenHeight / 2)))
  251.     {
  252.         //Bottom or Right
  253.         SelItem = TrackPopupMenu(MenuHnd,
  254.             TPM_BOTTOMALIGN | TPM_RIGHTALIGN | TPM_RETURNCMD | TPM_LEFTBUTTON,
  255.             MousePos.x, ScreenHeight, 0, MainWindow, NULL);
  256.     }
  257.     else if (MousePos.y < (ScreenHeight / 2)) 
  258.     {
  259.         //Top
  260.         SelItem = TrackPopupMenu(MenuHnd,
  261.             TPM_TOPALIGN | TPM_RIGHTALIGN | TPM_RETURNCMD | TPM_LEFTBUTTON,
  262.             MousePos.x, MousePos.y, 0, MainWindow, NULL);
  263.     }
  264.     else 
  265.     {
  266.         //Left
  267.         SelItem = TrackPopupMenu(MenuHnd,
  268.             TPM_BOTTOMALIGN | TPM_LEFTALIGN | TPM_RETURNCMD | TPM_LEFTBUTTON,
  269.             MousePos.x, ScreenHeight, 0, MainWindow, NULL);
  270.     }
  271.  
  272.     SetForegroundWindow(MainWindow);
  273.     DestroyMenu(MenuHnd);
  274.  
  275.     switch (SelItem)
  276.     {
  277.     case 1:
  278.         ShowWindow(MainWindow, SW_SHOW);
  279.         break;
  280.     case 2:
  281.         ShutdownServer(MainWindow);
  282.         break;
  283.     default:
  284.         break;
  285.     }
  286. }
  287.  
  288. void
  289. ShutdownServer(HWND MainWindow)
  290. {
  291.     int Answer;
  292.  
  293.     Answer = MessageBox(MainWindow, 
  294.                 "Are you sure you want to shutdown Sambar Server?",
  295.                 NAME, MB_YESNO | MB_ICONQUESTION);
  296.  
  297.     //If they do destroy the main window and let the the rest fall in place...
  298.     if (Answer == IDYES)
  299.         DestroyWindow(MainWindow);
  300. }
  301.  
  302. LRESULT CALLBACK
  303. About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  304. {
  305.     static  HFONT hfontDlg;        // Font for dialog text
  306.  
  307.     DWORD    dwVersion;            // To hold return value from GetVersion
  308.     char    szVersion[40];        // Temporary string for building output
  309.  
  310.     switch (message) 
  311.     {
  312.         // When the dialog is first being initialized, we want to do a number
  313.         // of things to 'customize' the look of this 'About' box.
  314.         // 1. Substitute a 'non-bold' font for most of the text
  315.         // 2. Substitute a 'fine print' font for the legal text at the
  316.         //    bottom.
  317.         // 3. Center the dialog over the parent window, and keep it within
  318.         //    the limits of the screen.
  319.         // 4. Substitute strings from the 'version' section of the resource
  320.         //    for various strings in the dialog
  321.         // 5. Display information regarding the 'version' of Windows that
  322.         //    we are running on
  323.         //
  324.         case WM_INITDIALOG:
  325.             ShowWindow(hDlg, SW_HIDE);
  326.  
  327.             // Create some replacement fonts:
  328.             hfontDlg = CreateFont(16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  329.                 VARIABLE_PITCH | FF_SWISS, "");
  330.  
  331.             // Reposition the dialog:
  332.             CenterWindow(hDlg, GetWindow(hDlg, GW_OWNER));
  333.  
  334.             SetWindowText(hDlg, "About Sambar Server");
  335.  
  336.             // Get the version information about Windows:
  337.             // We are  using GetVersion rather then GetVersionEx
  338.             // because earlier versions of Windows NT and Win32s
  339.             // didn't include GetVersionEx:
  340.             dwVersion = GetVersion();
  341.  
  342.             if (dwVersion < 0x80000000) 
  343.             {
  344.                 // Windows NT
  345.                 wsprintf(szVersion, "Microsoft Windows NT %u.%u (Build: %u)",
  346.                     (DWORD)(LOBYTE(LOWORD(dwVersion))),
  347.                     (DWORD)(HIBYTE(LOWORD(dwVersion))),
  348.                     (DWORD)(HIWORD(dwVersion)) );
  349.             } 
  350.             else if (LOBYTE(LOWORD(dwVersion)) < 4) 
  351.             {
  352.                 // Win32s
  353.                 wsprintf(szVersion, "Microsoft Win32s %u.%u (Build: %u)",
  354.                     (DWORD)(LOBYTE(LOWORD(dwVersion))),
  355.                     (DWORD)(HIBYTE(LOWORD(dwVersion))),
  356.                     (DWORD)(HIWORD(dwVersion) & ~0x8000) );
  357.             } 
  358.             else 
  359.             {
  360.                 // Windows 95
  361.                 wsprintf(szVersion, "Microsoft Windows 95 (osv: %u.%u)",
  362.                     (DWORD)(LOBYTE(LOWORD(dwVersion))),
  363.                     (DWORD)(HIBYTE(LOWORD(dwVersion))) );
  364.             }
  365.  
  366.             SendMessage(GetDlgItem(hDlg, IDC_OSVERSION), WM_SETFONT,
  367.                 (WPARAM)hfontDlg, (LPARAM)TRUE);
  368.             SetWindowText(GetDlgItem(hDlg, IDC_OSVERSION), szVersion);
  369.  
  370.             ShowWindow(hDlg, SW_SHOW);
  371.             return (TRUE);
  372.  
  373.         case WM_COMMAND:
  374.             // Only possible option is to close down. Make sure we
  375.             // clean things up on our way out...
  376.             if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
  377.             {
  378.                 EndDialog(hDlg, TRUE);
  379.                 DeleteObject(hfontDlg);
  380.  
  381.                 return (TRUE);
  382.             }
  383.             break;
  384.     }
  385.  
  386.     return FALSE;
  387. }
  388.  
  389. BOOL 
  390. CenterWindow(HWND hwndChild, HWND hwndParent)
  391. {
  392.     RECT    rChild, rParent, rWorkArea = {0,0,0,0};
  393.     int     wChild, hChild, wParent, hParent;
  394.     int     xNew, yNew;
  395.     BOOL    bResult;
  396.  
  397.     /* Get the Height and Width of the child window                        */
  398.     GetWindowRect (hwndChild, &rChild);
  399.     wChild = rChild.right - rChild.left;
  400.     hChild = rChild.bottom - rChild.top;
  401.  
  402.     /* Get the Height and Width of the parent window                    */
  403.     GetWindowRect (hwndParent, &rParent);
  404.     wParent = rParent.right - rParent.left;
  405.     hParent = rParent.bottom - rParent.top;
  406.  
  407.     /* Get the limits of the 'workarea'                                    */
  408.     bResult = SystemParametersInfo(SPI_GETWORKAREA, sizeof(RECT), 
  409.                 &rWorkArea, 0);
  410.     if (!bResult) 
  411.     {
  412.         rWorkArea.left = rWorkArea.top = 0;
  413.         rWorkArea.right = GetSystemMetrics(SM_CXSCREEN);
  414.         rWorkArea.bottom = GetSystemMetrics(SM_CYSCREEN);
  415.     }
  416.  
  417.     /* Calculate new X position, then adjust for workarea                */
  418.     xNew = rParent.left + ((wParent - wChild) /2);
  419.     if (xNew < rWorkArea.left)
  420.         xNew = rWorkArea.left;
  421.     else if ((xNew+wChild) > rWorkArea.right)
  422.         xNew = rWorkArea.right - wChild;
  423.  
  424.     /* Calculate new Y position, then adjust for workarea                */
  425.     yNew = rParent.top  + ((hParent - hChild) /2);
  426.     if (yNew < rWorkArea.top)
  427.         yNew = rWorkArea.top;
  428.     else if ((yNew+hChild) > rWorkArea.bottom)
  429.         yNew = rWorkArea.bottom - hChild;
  430.  
  431.     /* Set it, and return                                                */
  432.     bResult = SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0, 
  433.                 SWP_NOSIZE | SWP_NOZORDER);
  434.  
  435.     return (bResult);
  436. }
  437.