- Fix logon screensaver preview
[reactos.git] / reactos / base / applications / screensavers / logon / logon.c
index 79c7da4..001aa12 100644 (file)
-/*\r
- *  Copyright 2003 J Brown\r
- *  Copyright 2006 Eric Kohl\r
- *  Copyright 2007 Marc Piulachs (marc.piulachs@codexchange.net)\r
- *\r
- *  This program is free software; you can redistribute it and/or modify\r
- *  it under the terms of the GNU General Public License as published by\r
- *  the Free Software Foundation; either version 2 of the License, or\r
- *  (at your option) any later version.\r
- *\r
- *  This program is distributed in the hope that it will be useful,\r
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
- *  GNU General Public License for more details.\r
- *\r
- *  You should have received a copy of the GNU General Public License\r
- *  along with this program; if not, write to the Free Software\r
- *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA\r
- */\r
-\r
-#include <windows.h>\r
-#include <tchar.h>\r
-#include "resource.h"\r
-\r
-#define RANDOM( min, max ) ((rand() % (int)(((max)+1) - (min))) + (min))\r
-\r
-#define APPNAME _T("Logon")\r
-#define APP_TIMER                      1\r
-#define APP_TIMER_INTERVAL     2000\r
-\r
-#define BITMAP_HEIGHT   240;\r
-#define BITMAP_WIDTH    340\r
-\r
-HINSTANCE hInstance;\r
-\r
-BOOL fullscreen = FALSE;\r
-\r
-void DrawScreen (HDC hdc, HDC hMemDC , RECT rect)\r
-{\r
-       int x;\r
-       int y;\r
-       int width = BITMAP_WIDTH;\r
-       int height = BITMAP_HEIGHT;\r
-\r
-       if (!fullscreen)\r
-       {\r
-               width =  width / 20;\r
-               height = height / 20;\r
-       }\r
-\r
-       x = RANDOM (0, rect.right - width);\r
-       y = RANDOM (0, rect.bottom - height);\r
-\r
-       BitBlt(\r
-               hdc, \r
-               x, \r
-               y, \r
-               width, \r
-               height, \r
-               hMemDC, \r
-               0, \r
-               0, \r
-               SRCCOPY);\r
-}\r
-\r
-HBITMAP GetScreenSaverBitmap (void)\r
-{\r
-       OSVERSIONINFOEX osvi;\r
-       \r
-       ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));\r
-       osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);\r
-       GetVersionEx ((OSVERSIONINFO *) &osvi);\r
-\r
-       switch(osvi.wProductType)\r
-       {\r
-               case VER_NT_WORKSTATION:\r
-                       return LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_WORKSTATION));\r
-                       break;\r
-               default:\r
-                       return LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_SERVER));\r
-                       break;\r
-       }\r
-}\r
-\r
-LRESULT WINAPI WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)\r
-{\r
-       static POINT ptLast;\r
-       static POINT ptCursor;\r
-       static BOOL  fFirstTime = TRUE;\r
-\r
-       static PAINTSTRUCT ps;\r
-       static RECT rect;\r
-       static HDC hDC;\r
-       static HDC hMemDC;\r
-       static HBRUSH hBlkBrush;\r
-       static HBITMAP bitmap;\r
-\r
-       switch (msg)\r
-       {\r
-               case WM_CREATE: \r
-               {\r
-                       hDC = GetDC(hwnd);\r
-                       hBlkBrush = (HBRUSH) GetStockObject(BLACK_BRUSH);\r
-                       hMemDC = CreateCompatibleDC(hDC);\r
-                       GetClientRect(hwnd, &rect);\r
-\r
-                       bitmap = GetScreenSaverBitmap ();\r
-\r
-                       if(bitmap == NULL)\r
-                       {\r
-                               MessageBox(\r
-                                  hwnd, \r
-                                  _T("Fatal Error: Could not load bitmap"), \r
-                                  _T("Error"),\r
-                                  MB_OK | MB_ICONEXCLAMATION); \r
-                       }\r
-\r
-                       SetTimer (\r
-                               hwnd, \r
-                               APP_TIMER, \r
-                               APP_TIMER_INTERVAL, \r
-                               NULL);\r
-\r
-                        break; \r
-                }\r
-               case WM_PAINT:\r
-                       {       \r
-                               hDC = BeginPaint(hwnd, &ps);\r
-                               SelectObject(hMemDC, bitmap);\r
-                               DrawScreen (hDC , hMemDC , rect);\r
-                               EndPaint(hwnd, &ps);\r
-                               break;\r
-                       }\r
-               case WM_TIMER :\r
-               {\r
-                       if (wParam == APP_TIMER)\r
-                       {\r
-                               InvalidateRect(hwnd, NULL, 1);\r
-                       }\r
-               }\r
-                 case WM_ERASEBKGND:\r
-                       {\r
-                               SelectObject(hDC, hBlkBrush);\r
-                       \r
-                               PatBlt(\r
-                                       hDC,\r
-                                       0,\r
-                                       0, \r
-                                       rect.right, \r
-                                       rect.bottom, \r
-                                       PATCOPY);\r
-                               break;\r
-                       }\r
-                       case WM_DESTROY:\r
-                       {\r
-                               KillTimer (hwnd, APP_TIMER);\r
-                               DeleteObject(bitmap);\r
-                               ShowCursor(TRUE);\r
-                               PostQuitMessage(0);\r
-                               break;  \r
-                       }\r
-                       \r
-                       // break out of screen-saver if any keyboard activity\r
-                       case WM_NOTIFY:\r
-                       case WM_SYSKEYDOWN:\r
-                               PostMessage(hwnd, WM_CLOSE, 0, 0);\r
-                               break;\r
-\r
-                       // break out of screen-saver if any mouse activity\r
-                       case WM_LBUTTONDOWN:\r
-                       case WM_LBUTTONUP:\r
-                       case WM_RBUTTONDOWN:\r
-                       case WM_RBUTTONUP:\r
-                       case WM_MBUTTONDOWN:\r
-                       case WM_MBUTTONUP:\r
-                       case WM_MOUSEMOVE:\r
-                               // If we've got a parent then we must be a preview\r
-                               if(GetParent(hwnd) != 0)\r
-                                       return 0;\r
-\r
-                               if(fFirstTime)\r
-                               {\r
-                                       GetCursorPos(&ptLast);\r
-                                       fFirstTime = FALSE;\r
-                               }\r
-\r
-                       GetCursorPos(&ptCursor);\r
-\r
-                       // if the mouse has moved more than 3 pixels then exit\r
-                       if(abs(ptCursor.x - ptLast.x) >= 3 || abs(ptCursor.y - ptLast.y) >= 3)\r
-                               PostMessage(hwnd, WM_CLOSE, 0, 0);\r
-\r
-                       ptLast = ptCursor;\r
-\r
-                       return 0;\r
-               }\r
-\r
-       return DefWindowProc(hwnd, msg, wParam, lParam);\r
-}\r
-\r
-void InitSaver(HWND hwndParent)\r
-{\r
-       WNDCLASS wc;\r
-       ZeroMemory(&wc, sizeof(wc));\r
-       wc.style            = CS_HREDRAW | CS_VREDRAW;\r
-       wc.lpfnWndProc      = WndProc;\r
-       wc.lpszClassName    = APPNAME;\r
-       wc.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);\r
-       RegisterClass(&wc);\r
-\r
-       if (hwndParent != 0)\r
-       {\r
-               RECT rect;\r
-               GetClientRect(hwndParent, &rect);\r
-               CreateWindow(APPNAME, APPNAME,\r
-                            WS_VISIBLE | WS_CHILD,\r
-                            0, 0,\r
-                            rect.right,\r
-                            rect.bottom,\r
-                            hwndParent, 0,\r
-                            hInstance, NULL);\r
-               fullscreen = FALSE;\r
-       }\r
-       else\r
-       {\r
-               HWND hwnd;\r
-               hwnd = CreateWindowEx(WS_EX_TOPMOST,\r
-                          APPNAME, \r
-                          APPNAME,\r
-                          WS_VISIBLE | WS_POPUP,\r
-                          0, 0,\r
-                          GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),\r
-                          HWND_DESKTOP, 0,\r
-                          hInstance, NULL);\r
-        \r
-    SetWindowPos(hwnd, \r
-                 0, 0, 0, 0, 0, \r
-                 SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOSIZE|SWP_SHOWWINDOW);\r
-\r
-    ShowCursor(FALSE);\r
-               fullscreen = TRUE;\r
-       }\r
-}\r
-\r
-void ParseCommandLine(PSTR szCmdLine, int *chOption, HWND *hwndParent)\r
-{\r
-       int ch = *szCmdLine++;\r
-\r
-       if(ch == '-' || ch == '/')\r
-               ch = *szCmdLine++;\r
-\r
-       if(ch >= 'A' && ch <= 'Z')\r
-               ch += 'a' - 'A';\r
-\r
-       *chOption = ch;\r
-       ch = *szCmdLine++;\r
-\r
-       if(ch == ':')\r
-               ch = *szCmdLine++;\r
-\r
-       while(ch == ' ' || ch == '\t')\r
-               ch = *szCmdLine++;\r
-\r
-       if(isdigit(ch))\r
-       {\r
-               unsigned int i = atoi(szCmdLine - 1);\r
-               *hwndParent = (HWND)i;\r
-       }\r
-       else\r
-               *hwndParent = 0;\r
-}\r
-\r
-int WINAPI WinMain (HINSTANCE hInst,\r
-                    HINSTANCE hPrev,\r
-                    LPSTR lpCmdLine,\r
-                    int iCmdShow)\r
-{\r
-       HWND    hwndParent;\r
-       UINT    nPreviousState;\r
-       int     chOption;\r
-       MSG     Message;\r
-\r
-       hInstance = hInst;\r
-\r
-       ParseCommandLine(lpCmdLine, &chOption, &hwndParent);\r
-\r
-       SystemParametersInfo(SPI_SETSCREENSAVERRUNNING, TRUE, &nPreviousState, 0);\r
-\r
-       switch (chOption)\r
-       {\r
-               case 's':\r
-                       InitSaver(0);\r
-                       break;\r
-\r
-               case 'p':\r
-                       InitSaver(hwndParent);\r
-                       break;\r
-\r
-               case 'c':\r
-               default:\r
-                       MessageBox(0,\r
-                                  _T("No options need to be set."),\r
-                                  _T("About"),\r
-                                  MB_OK | MB_ICONWARNING);\r
-                       return 0;\r
-       }\r
-\r
-       while (GetMessage(&Message, 0, 0, 0))\r
-               DispatchMessage(&Message);\r
-\r
-       SystemParametersInfo(SPI_SETSCREENSAVERRUNNING, FALSE, &nPreviousState, 0);\r
-\r
-       return Message.wParam;\r
-}\r
+/*
+ *  Copyright 2003 J Brown
+ *  Copyright 2006 Eric Kohl
+ *  Copyright 2007 Marc Piulachs (marc.piulachs@codexchange.net)
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ */
+
+#include <windows.h>
+#include <scrnsave.h>
+#include <tchar.h>
+#include "resource.h"
+
+#define RANDOM( min, max ) ((rand() % (int)(((max)+1) - (min))) + (min))
+
+#define APPNAME _T("Logon")
+#define APP_TIMER                      1
+#define APP_TIMER_INTERVAL     2000
+
+HBITMAP GetScreenSaverBitmap (void)
+{
+       OSVERSIONINFOEX osvi;
+
+       ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
+       osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
+       GetVersionEx ((OSVERSIONINFO *) &osvi);
+
+       switch(osvi.wProductType)
+       {
+               case VER_NT_WORKSTATION:
+                       return LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_WORKSTATION), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
+                       break;
+               default:
+                       return LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_SERVER), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
+                       break;
+       }
+}
+
+LRESULT CALLBACK
+ScreenSaverProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
+{
+       static RECT rect;
+       static HBITMAP bitmap;
+
+       switch (message)
+       {
+               case WM_CREATE:
+               {
+                       bitmap = GetScreenSaverBitmap ();
+
+                       if(bitmap == NULL)
+                       {
+                               MessageBox(
+                                  hWnd,
+                                  _T("Fatal Error: Could not load bitmap"),
+                                  _T("Error"),
+                                  MB_OK | MB_ICONEXCLAMATION);
+                       }
+
+                       SetTimer (
+                               hWnd,
+                               APP_TIMER,
+                               APP_TIMER_INTERVAL,
+                               NULL);
+
+                        break;
+                }
+               case WM_PAINT:
+               {
+             BITMAP bm; /* Bitmap structure as seen in bmWidth & bmHeight */ 
+             PAINTSTRUCT ps; 
+
+             // Obtain window coordinates.
+             GetClientRect (hWnd, &rect);
+
+             HDC hdc = BeginPaint(hWnd, &ps); 
+             HDC hdcMem = CreateCompatibleDC(hdc); 
+             HBITMAP hbmOld = SelectObject(hdcMem, bitmap); 
+
+             GetObject(bitmap, sizeof(bm), &bm);
+
+             if (rect.right < bm.bmWidth ||
+                 rect.bottom < bm.bmHeight)
+             {
+                StretchBlt(
+                    hdc,
+                    RANDOM (0, rect.right - (bm.bmWidth /5)),
+                    RANDOM (0, rect.bottom - (bm.bmHeight /5)),
+                    bm.bmWidth /5,
+                    bm.bmHeight /5,
+                    hdcMem,
+                    0,
+                    0,
+                    bm.bmWidth,
+                    bm.bmHeight,
+                    SRCCOPY);
+             }
+             else
+             {
+                 BitBlt(
+                     hdc, 
+                     RANDOM (0, rect.right - bm.bmWidth),
+                     RANDOM (0, rect.bottom - bm.bmHeight),
+                     bm.bmWidth, 
+                     bm.bmHeight, 
+                     hdcMem, 
+                     0, 
+                     0, 
+                     SRCCOPY); 
+             }
+
+             SelectObject(hdcMem, hbmOld); 
+             DeleteDC(hdcMem); 
+
+             EndPaint(hWnd, &ps);
+             break;
+               }
+        case WM_TIMER:
+        {
+          InvalidateRect(hWnd, NULL, 1);
+          break;
+        }
+               case WM_DESTROY:
+               {
+                       KillTimer (hWnd, APP_TIMER);
+                       DeleteObject(bitmap);
+                       PostQuitMessage(0);
+                       break;
+               }
+
+        default:
+            // Pass Windows Messages to the default screensaver window procedure
+            return DefScreenSaverProc(hWnd, message, wParam, lParam);
+    }
+
+       return 0;
+}
+
+BOOL WINAPI ScreenSaverConfigureDialog(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
+{
+    return FALSE;
+}
+
+// This function is only called one time before opening the configuration dialog.
+// Use it to show a message that no configuration is necesssary and return FALSE to indicate that no configuration dialog shall be opened.
+BOOL WINAPI RegisterDialogClasses(HANDLE hInst)
+{
+    TCHAR szMessage[256];
+    TCHAR szTitle[25];
+
+    LoadString(hInst, IDS_TEXT, szMessage, sizeof(szMessage) / sizeof(TCHAR));
+    LoadString(hInst, IDS_DESCRIPTION, szTitle, sizeof(szTitle) / sizeof(TCHAR));
+
+    MessageBox(NULL, szMessage, szTitle, MB_OK | MB_ICONEXCLAMATION);
+
+    return FALSE;
+}