This commit was manufactured by cvs2svn to create branch 'captive'.
[reactos.git] / subsys / system / explorer / winefile / desktop.c
1 /*
2     Desktop creation example
3
4     Silver Blade (silverblade_uk@hotmail.com)
5     5th July 2003
6 */
7
8 #include <stdio.h>
9 #include <windows.h>
10
11 #include "externals.h"
12
13  // Unicode support
14 #ifdef UNICODE
15 #define _UNICODE
16 #endif
17 #include <tchar.h>
18
19
20 /* GetShellWindow is already present in the header files
21 static HWND (WINAPI*GetShellWindow)(); */
22 static BOOL (WINAPI*SetShellWindow)(HWND);
23
24
25 BOOL IsAnyDesktopRunning()
26 {
27 /*      POINT pt;*/
28         HINSTANCE shell32 = GetModuleHandle(TEXT("user32"));
29
30         SetShellWindow = (BOOL(WINAPI*)(HWND)) GetProcAddress(shell32, "SetShellWindow");
31
32 /* GetShellWindow is already present in the header files
33         GetShellWindow = (HWND(WINAPI*)()) GetProcAddress(shell32, "GetShellWindow");
34
35         if (GetShellWindow) */
36                 return GetShellWindow() != 0;
37 /*
38         pt.x = 0;
39         pt.y = 0;
40
41         return WindowFromPoint(pt) != GetDesktopWindow(); */
42 }
43
44
45 LRESULT CALLBACK DeskWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
46 {
47     switch(uMsg)
48     {
49         case WM_CLOSE :
50         {
51             // Over-ride close. We need to close desktop some other way.
52             break;
53         }
54
55         case WM_PAINT :
56         {
57             // We'd want to draw the desktop wallpaper here. Need to
58             // maintain a copy of the wallpaper in an off-screen DC and then
59             // bitblt (or stretchblt?) it to the screen appropriately. For
60             // now, though, we'll just draw some text.
61
62             PAINTSTRUCT ps;
63             HDC DesktopDC = BeginPaint(hwnd, &ps);
64
65             TCHAR Text [] = TEXT("ReactOS 0.1.2 Desktop Example\nby Silver Blade");
66
67             int Width, Height;
68
69             Width = GetSystemMetrics(SM_CXSCREEN);
70             Height = GetSystemMetrics(SM_CYSCREEN);
71
72             // This next part could be improved by working out how much
73             // space the text actually needs...
74
75                         {
76                         RECT r;
77             r.left = Width - 260;
78             r.top = Height - 80;
79             r.right = r.left + 250;
80             r.bottom = r.top + 40;
81
82             SetTextColor(DesktopDC, 0x00ffffff);
83             SetBkMode(DesktopDC, TRANSPARENT);
84             DrawText(DesktopDC, Text, -1, &r, DT_RIGHT);
85                         }
86
87             EndPaint(hwnd, &ps);
88
89                         break;
90         }
91
92                 case WM_LBUTTONDBLCLK:
93                         explorer_show_frame(hwnd, SW_SHOWNORMAL);
94                         break;
95
96                 case WM_DESTROY:
97                         if (SetShellWindow)
98                                 SetShellWindow(0);
99                         break;
100
101                 default:
102                     return DefWindowProc(hwnd, uMsg, wParam, lParam);
103     }
104
105         return 0;
106 }
107
108
109 const TCHAR DesktopClassName[] = TEXT("DesktopWindow");
110
111
112 HWND create_desktop_window(HINSTANCE hInstance)
113 {
114     WNDCLASSEX wc;
115         HWND hwndDesktop;
116     int Width, Height;
117
118         wc.cbSize       = sizeof(WNDCLASSEX);
119         wc.style        = CS_DBLCLKS;
120         wc.lpfnWndProc  = &DeskWndProc;
121         wc.cbClsExtra   = 0;
122         wc.cbWndExtra   = 0;
123         wc.hInstance    = hInstance;
124         wc.hIcon        = LoadIcon(0, IDI_APPLICATION);
125         wc.hCursor      = LoadCursor(0, IDC_ARROW);
126         wc.hbrBackground= (HBRUSH) GetStockObject(BLACK_BRUSH);
127         wc.lpszMenuName = NULL;
128         wc.lpszClassName= DesktopClassName;
129         wc.hIconSm      = NULL;
130
131         if (!RegisterClassEx(&wc))
132                 return 0;
133
134         Width = GetSystemMetrics(SM_CXSCREEN);
135         Height = GetSystemMetrics(SM_CYSCREEN);
136
137         hwndDesktop = CreateWindowEx(0, DesktopClassName, TEXT("Desktop"),
138                                                         WS_VISIBLE | WS_POPUP | WS_CLIPCHILDREN,
139                                                         0, 0, Width, Height,
140                                                         NULL, NULL, hInstance, NULL);
141
142         if (SetShellWindow)
143                 SetShellWindow(hwndDesktop);
144
145         return hwndDesktop;
146 }
147
148
149 #ifdef _CONSOLE
150 int main(int argc, char *argv[])
151 #else
152 int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd)
153 #endif
154 {
155         int ret;
156     HWND hwndDesktop = 0;
157
158 #ifdef _CONSOLE
159         STARTUPINFO startupinfo;
160         int nShowCmd = SW_SHOWNORMAL;
161
162         HINSTANCE hInstance = GetModuleHandle(NULL);
163 #endif
164
165          // create desktop window and task bar only, if there is no other shell and we are
166          // the first explorer instance (just in case SetShellWindow() is not supported by the OS)
167         BOOL startup_desktop = !IsAnyDesktopRunning() && !find_window_class(DesktopClassName);
168
169 #ifdef _CONSOLE
170         if (argc>1 && !strcmp(argv[1],"-desktop"))
171 #else
172         if (!lstrcmp(lpCmdLine,TEXT("-desktop")))
173 #endif
174                 startup_desktop = TRUE;
175
176         if (startup_desktop)
177         {
178                 HWND hwndExplorerBar;
179
180                 hwndDesktop = create_desktop_window(hInstance);
181
182                 if (!hwndDesktop)
183                 {
184                         fprintf(stderr,"FATAL: Desktop window could not be initialized properly - Exiting.\n");
185                         return 1;   // error
186                 }
187
188 #ifdef _CONSOLE
189                  // call winefile startup routine
190                 GetStartupInfo(&startupinfo);
191
192                 if (startupinfo.dwFlags & STARTF_USESHOWWINDOW)
193                         nShowCmd = startupinfo.wShowWindow;
194 #endif
195
196                  // Initializing the Explorer Bar
197                 if (!(hwndExplorerBar=InitializeExplorerBar(hInstance)))
198                 {
199                         fprintf(stderr,"FATAL: Explorer bar could not be initialized properly ! Exiting !\n");
200                         return 1;
201                 }
202
203                  // Load plugins
204                 if (!LoadAvailablePlugIns(hwndExplorerBar))
205                 {
206                         fprintf(stderr,"WARNING: No plugin for desktop bar could be loaded.\n");
207                 }
208
209 #ifndef _DEBUG  //MF: disabled for debugging
210 #ifdef _CONSOLE
211             startup(argc, argv); // invoke the startup groups
212 #else
213                 {
214                 char* argv[] = {""};
215             startup(1, argv);
216                 }
217 #endif
218 #endif
219         }
220
221         ret = explorer_main(hInstance, hwndDesktop, nShowCmd);
222
223         ReleaseAvailablePlugIns();
224
225         return ret;
226 }