home *** CD-ROM | disk | FTP | other *** search
- /*
- ** WINMAIN
- **
- ** This is the main driver for the Windows driver of the Sambar
- ** Server.
- **
- ** Confidential Property of Tod Sambar
- ** (c) Copyright Tod Sambar 1995-1997
- ** All rights reserved.
- **
- **
- ** Syntax:
- **
- ** server
- **
- **
- ** History:
- ** Chg# Date Description Resp
- ** ---- ------- ------------------------------------------------------- ----
- ** 9MAR97 Created sambar
- */
-
- #include <windows.h>
- #include <process.h>
- #include <sambar.h>
- #include <resource.h>
-
- /*
- ** Local Defines
- */
- #define SERVER_ID 1001
- #define SERVER_MSG WM_USER + 69
- #define NAME "Sambar Server"
-
- static HWND MainWindow;
- static HANDLE MainInstance;
- static BOOL ShellTray = FALSE;
-
-
- /*
- ** Local Prototypes
- */
- long __stdcall WndProc(
- HWND hWnd,
- UINT message,
- WPARAM wParam,
- LPARAM lParam
- );
- LRESULT CALLBACK About(
- HWND hDlg,
- UINT message,
- WPARAM wParam,
- LPARAM lParam
- );
- BOOL CenterWindow(
- HWND hwndChild,
- HWND hwndParent
- );
- void DisplayMenu(
- HWND hWnd
- );
- void ShutdownServer(
- HWND hWnd
- );
-
- int __stdcall
- WinMain(HANDLE Instance, HANDLE PrevInstance, LPSTR CmdLine, int CmdShow)
- {
- unsigned long thread;
- SA_BOOL ver3;
- DWORD dwVersion; // To hold return value from GetVersion
- MSG message;
- HWND hWnd;
- WNDCLASS MainClass;
- HICON ServerIcon;
- NOTIFYICONDATA IconData;
-
- /* Save the application Instance */
- MainInstance = Instance;
-
- /* If a Server is running, show it */
- hWnd = FindWindow(NAME, NULL);
- if (hWnd)
- {
- if (IsIconic(hWnd))
- ShowWindow(hWnd, SW_RESTORE);
-
- SetForegroundWindow(hWnd);
- return (0);
- }
-
- /* Create the tray Icon resource */
- ServerIcon = LoadIcon(Instance, "SERVER_ICON");
-
- /* Create a window class */
- MainClass.style = CS_HREDRAW | CS_VREDRAW;
- MainClass.lpfnWndProc = WndProc;
- MainClass.cbClsExtra = 0;
- MainClass.cbWndExtra = 0;
- MainClass.hInstance = Instance;
- MainClass.hIcon = ServerIcon;
- MainClass.hCursor = LoadCursor(NULL, IDC_ARROW);
- MainClass.hbrBackground = GetStockObject(WHITE_BRUSH);
- MainClass.lpszMenuName = "MAIN";
- MainClass.lpszClassName = NAME;
-
- if (!RegisterClass(&MainClass))
- return (0);
-
- /* Determine if this is windows 3.51 -- change the window type */
- ver3 = 0;
- dwVersion = GetVersion();
- if (dwVersion < 0x80000000)
- {
- if ((DWORD)(LOBYTE(LOWORD(dwVersion))) == 3)
- ver3 = 1;
- }
-
- /* Create the main window */
- MainWindow = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, NAME, NAME,
- ver3 ? (WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX) :
- (WS_CAPTION | WS_SYSMENU),
- CW_USEDEFAULT, 0, 250, 80, NULL, NULL, Instance, NULL);
-
- /* Add text to the main window */
- hWnd = CreateWindow("STATIC", "Version Info", WS_CHILD | WS_VISIBLE,
- 0, 0, 250, 80, MainWindow, NULL, Instance, NULL);
- SetWindowText(hWnd, "Sambar Server is Active");
-
- /* Add tray icon to system tray */
- IconData.cbSize = sizeof(NOTIFYICONDATA);
- IconData.hWnd = MainWindow;
- IconData.uID = SERVER_ID;
- IconData.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
- IconData.uCallbackMessage = SERVER_MSG;
- IconData.hIcon = ServerIcon;
- strcpy(IconData.szTip, "Sambar Server is Active");
-
- /* Start the Sambar Server */
- /* Win95 and NT4.0 only */
- if (!Shell_NotifyIcon(NIM_ADD, &IconData))
- {
- ShowWindow(MainWindow, CmdShow);
- UpdateWindow(MainWindow);
- }
- else
- {
- ShellTray = TRUE;
- ShowWindow(MainWindow, SW_HIDE);
- }
-
- /*
- ** Start the Sambar Server.
- ** On failure, the server will shutdown and destroy the MainWindow.
- */
- thread = _beginthread(sa_server, 20480, (SA_VOID *)&MainWindow);
- if (thread == -1)
- return (0);
-
- /* Message loop */
- while (GetMessage(&message, NULL, 0, 0))
- DispatchMessage(&message);
-
- Shell_NotifyIcon(NIM_DELETE, &IconData);
- UnregisterClass(NAME, Instance);
-
- /* Shutdown the Sambar Server */
- (SA_VOID)sa_shutdown();
-
- return (message.wParam);
- }
-
- long __stdcall
- WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- int wmId;
- int wmEvent;
-
- switch (message)
- {
- case WM_COMMAND:
- wmId = LOWORD(wParam);
- wmEvent = HIWORD(wParam);
-
- switch (wmId)
- {
- case IDM_ABOUT:
- DialogBox(MainInstance, "AboutBox", hWnd, (DLGPROC)About);
- break;
-
- case IDM_HIDE:
- if (ShellTray)
- ShowWindow(MainWindow, SW_HIDE);
- else
- ShowWindow(MainWindow, SW_MINIMIZE);
- break;
-
- case IDM_EXIT:
- DestroyWindow(hWnd);
- break;
-
- default:
- /* Unhandled Messages end up here (DefWindowProc) */
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- break;
-
- case SERVER_MSG:
- if ((wParam == SERVER_ID) && (lParam == WM_RBUTTONDOWN))
- DisplayMenu(hWnd);
- break;
-
- case WM_DESTROY:
- /* Shutdown the main window */
- PostQuitMessage(0);
- break;
-
- default:
- /* Unhandled Messages end up here (DefWindowProc) */
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
-
- return(0);
- }
-
- void
- DisplayMenu(HWND hWnd)
- {
- HMENU MenuHnd;
- POINT MousePos;
- int ScreenWidth;
- int ScreenHeight;
- int SelItem;
-
- MenuHnd = CreatePopupMenu();
- AppendMenu(MenuHnd, MF_ENABLED, 1, "Open");
- AppendMenu(MenuHnd, MF_SEPARATOR, 0, NULL);
- AppendMenu(MenuHnd, MF_ENABLED, 2, "Shutdown");
-
- //Get Mouse Pos
- GetCursorPos(&MousePos);
-
- //Get Screen Metrics
- ScreenWidth = GetSystemMetrics(SM_CXSCREEN);
- ScreenHeight = GetSystemMetrics(SM_CYSCREEN);
-
- SetForegroundWindow(MainWindow);
-
- //Handle the different possible task bar locations
- if ((MousePos.x >= (ScreenWidth / 2)) && (MousePos.y >= (ScreenHeight / 2)))
- {
- //Bottom or Right
- SelItem = TrackPopupMenu(MenuHnd,
- TPM_BOTTOMALIGN | TPM_RIGHTALIGN | TPM_RETURNCMD | TPM_LEFTBUTTON,
- MousePos.x, ScreenHeight, 0, MainWindow, NULL);
- }
- else if (MousePos.y < (ScreenHeight / 2))
- {
- //Top
- SelItem = TrackPopupMenu(MenuHnd,
- TPM_TOPALIGN | TPM_RIGHTALIGN | TPM_RETURNCMD | TPM_LEFTBUTTON,
- MousePos.x, MousePos.y, 0, MainWindow, NULL);
- }
- else
- {
- //Left
- SelItem = TrackPopupMenu(MenuHnd,
- TPM_BOTTOMALIGN | TPM_LEFTALIGN | TPM_RETURNCMD | TPM_LEFTBUTTON,
- MousePos.x, ScreenHeight, 0, MainWindow, NULL);
- }
-
- SetForegroundWindow(MainWindow);
- DestroyMenu(MenuHnd);
-
- switch (SelItem)
- {
- case 1:
- ShowWindow(MainWindow, SW_SHOW);
- break;
- case 2:
- ShutdownServer(MainWindow);
- break;
- default:
- break;
- }
- }
-
- void
- ShutdownServer(HWND MainWindow)
- {
- int Answer;
-
- Answer = MessageBox(MainWindow,
- "Are you sure you want to shutdown Sambar Server?",
- NAME, MB_YESNO | MB_ICONQUESTION);
-
- //If they do destroy the main window and let the the rest fall in place...
- if (Answer == IDYES)
- DestroyWindow(MainWindow);
- }
-
- LRESULT CALLBACK
- About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
- {
- static HFONT hfontDlg; // Font for dialog text
-
- DWORD dwVersion; // To hold return value from GetVersion
- char szVersion[40]; // Temporary string for building output
-
- switch (message)
- {
- // When the dialog is first being initialized, we want to do a number
- // of things to 'customize' the look of this 'About' box.
- // 1. Substitute a 'non-bold' font for most of the text
- // 2. Substitute a 'fine print' font for the legal text at the
- // bottom.
- // 3. Center the dialog over the parent window, and keep it within
- // the limits of the screen.
- // 4. Substitute strings from the 'version' section of the resource
- // for various strings in the dialog
- // 5. Display information regarding the 'version' of Windows that
- // we are running on
- //
- case WM_INITDIALOG:
- ShowWindow(hDlg, SW_HIDE);
-
- // Create some replacement fonts:
- hfontDlg = CreateFont(16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- VARIABLE_PITCH | FF_SWISS, "");
-
- // Reposition the dialog:
- CenterWindow(hDlg, GetWindow(hDlg, GW_OWNER));
-
- SetWindowText(hDlg, "About Sambar Server");
-
- // Get the version information about Windows:
- // We are using GetVersion rather then GetVersionEx
- // because earlier versions of Windows NT and Win32s
- // didn't include GetVersionEx:
- dwVersion = GetVersion();
-
- if (dwVersion < 0x80000000)
- {
- // Windows NT
- wsprintf(szVersion, "Microsoft Windows NT %u.%u (Build: %u)",
- (DWORD)(LOBYTE(LOWORD(dwVersion))),
- (DWORD)(HIBYTE(LOWORD(dwVersion))),
- (DWORD)(HIWORD(dwVersion)) );
- }
- else if (LOBYTE(LOWORD(dwVersion)) < 4)
- {
- // Win32s
- wsprintf(szVersion, "Microsoft Win32s %u.%u (Build: %u)",
- (DWORD)(LOBYTE(LOWORD(dwVersion))),
- (DWORD)(HIBYTE(LOWORD(dwVersion))),
- (DWORD)(HIWORD(dwVersion) & ~0x8000) );
- }
- else
- {
- // Windows 95
- wsprintf(szVersion, "Microsoft Windows 95 (osv: %u.%u)",
- (DWORD)(LOBYTE(LOWORD(dwVersion))),
- (DWORD)(HIBYTE(LOWORD(dwVersion))) );
- }
-
- SendMessage(GetDlgItem(hDlg, IDC_OSVERSION), WM_SETFONT,
- (WPARAM)hfontDlg, (LPARAM)TRUE);
- SetWindowText(GetDlgItem(hDlg, IDC_OSVERSION), szVersion);
-
- ShowWindow(hDlg, SW_SHOW);
- return (TRUE);
-
- case WM_COMMAND:
- // Only possible option is to close down. Make sure we
- // clean things up on our way out...
- if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
- {
- EndDialog(hDlg, TRUE);
- DeleteObject(hfontDlg);
-
- return (TRUE);
- }
- break;
- }
-
- return FALSE;
- }
-
- BOOL
- CenterWindow(HWND hwndChild, HWND hwndParent)
- {
- RECT rChild, rParent, rWorkArea = {0,0,0,0};
- int wChild, hChild, wParent, hParent;
- int xNew, yNew;
- BOOL bResult;
-
- /* Get the Height and Width of the child window */
- GetWindowRect (hwndChild, &rChild);
- wChild = rChild.right - rChild.left;
- hChild = rChild.bottom - rChild.top;
-
- /* Get the Height and Width of the parent window */
- GetWindowRect (hwndParent, &rParent);
- wParent = rParent.right - rParent.left;
- hParent = rParent.bottom - rParent.top;
-
- /* Get the limits of the 'workarea' */
- bResult = SystemParametersInfo(SPI_GETWORKAREA, sizeof(RECT),
- &rWorkArea, 0);
- if (!bResult)
- {
- rWorkArea.left = rWorkArea.top = 0;
- rWorkArea.right = GetSystemMetrics(SM_CXSCREEN);
- rWorkArea.bottom = GetSystemMetrics(SM_CYSCREEN);
- }
-
- /* Calculate new X position, then adjust for workarea */
- xNew = rParent.left + ((wParent - wChild) /2);
- if (xNew < rWorkArea.left)
- xNew = rWorkArea.left;
- else if ((xNew+wChild) > rWorkArea.right)
- xNew = rWorkArea.right - wChild;
-
- /* Calculate new Y position, then adjust for workarea */
- yNew = rParent.top + ((hParent - hChild) /2);
- if (yNew < rWorkArea.top)
- yNew = rWorkArea.top;
- else if ((yNew+hChild) > rWorkArea.bottom)
- yNew = rWorkArea.bottom - hChild;
-
- /* Set it, and return */
- bResult = SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
- SWP_NOSIZE | SWP_NOZORDER);
-
- return (bResult);
- }
-