small improvements for DesktopWindow
[reactos.git] / reactos / subsys / system / explorer / desktop / desktop.cpp
1 /*
2 * Copyright 2003 Martin Fuchs
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19
20 //
21 // Explorer clone
22 //
23 // desktop.cpp
24 //
25 // Martin Fuchs, 09.08.2003
26 //
27
28
29 #include "desktop.h"
30
31 #include "../externals.h"
32
33
34 static BOOL (WINAPI*SetShellWindow)(HWND);
35 static BOOL (WINAPI*SetShellWindowEx)(HWND, HWND);
36
37
38 BOOL IsAnyDesktopRunning()
39 {
40 HINSTANCE shell32 = GetModuleHandle(TEXT("user32"));
41
42 SetShellWindow = (BOOL(WINAPI*)(HWND)) GetProcAddress(shell32, "SetShellWindow");
43 SetShellWindowEx = (BOOL(WINAPI*)(HWND,HWND)) GetProcAddress(shell32, "SetShellWindowEx");
44
45 return GetShellWindow() != 0;
46 }
47
48
49 DesktopWindow::DesktopWindow(HWND hwnd)
50 : super(hwnd)
51 {
52 _pShellView = NULL;
53 }
54
55 DesktopWindow::~DesktopWindow()
56 {
57 if (_pShellView)
58 _pShellView->Release();
59 }
60
61
62 LRESULT DesktopWindow::WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam)
63 {
64 switch(nmsg) {
65 case WM_PAINT: {
66 // We'd want to draw the desktop wallpaper here. Need to
67 // maintain a copy of the wallpaper in an off-screen DC and then
68 // bitblt (or stretchblt?) it to the screen appropriately. For
69 // now, though, we'll just draw some text.
70
71 PAINTSTRUCT ps;
72 HDC DesktopDC = BeginPaint(_hwnd, &ps);
73
74 static const TCHAR Text [] = TEXT("ReactOS 0.1.2 Desktop Example\nby Silver Blade, Martin Fuchs");
75
76 RECT rect;
77 GetClientRect(_hwnd, &rect);
78
79 // This next part could be improved by working out how much
80 // space the text actually needs...
81
82 rect.left = rect.right - 260;
83 rect.top = rect.bottom - 80;
84 rect.right = rect.left + 250;
85 rect.bottom = rect.top + 40;
86
87 SetTextColor(DesktopDC, 0x00ffffff);
88 SetBkMode(DesktopDC, TRANSPARENT);
89 DrawText(DesktopDC, Text, -1, &rect, DT_RIGHT);
90
91 EndPaint(_hwnd, &ps);
92 break;}
93
94 case WM_LBUTTONDBLCLK:
95 explorer_show_frame(_hwnd, SW_SHOWNORMAL);
96 break;
97
98 case WM_GETISHELLBROWSER:
99 return (LRESULT)static_cast<IShellBrowser*>(this);
100
101 case WM_CREATE: {
102 HRESULT hr = Desktop()->CreateViewObject(_hwnd, IID_IShellView, (void**)&_pShellView);
103
104 HWND hWndListView = 0;
105
106 if (SUCCEEDED(hr)) {
107 FOLDERSETTINGS fs;
108
109 fs.ViewMode = FWF_DESKTOP|FWF_TRANSPARENT|FWF_NOSUBFOLDERS|FWF_BESTFITWINDOW|FWF_ABBREVIATEDNAMES;
110 fs.fFlags = FVM_ICON;
111
112 RECT rect;
113 GetClientRect(_hwnd, &rect);
114
115 hr = _pShellView->CreateViewWindow(NULL, &fs, this, &rect, &hWndListView);
116
117 if (SUCCEEDED(hr)) {
118 HWND hwndFolderView = ::GetNextWindow(hWndListView, GW_CHILD);
119
120 ShowWindow(hwndFolderView, SW_SHOW);
121 }
122 }
123
124 if (hWndListView && SetShellWindowEx)
125 SetShellWindowEx(_hwnd, hWndListView);
126 else if (SetShellWindow)
127 SetShellWindow(_hwnd);
128 break;}
129
130 case WM_DESTROY:
131 if (SetShellWindow)
132 SetShellWindow(0);
133 break;
134
135 case WM_CLOSE:
136 break; // Over-ride close. We need to close desktop some other way.
137
138 default:
139 return super::WndProc(nmsg, wparam, lparam);
140 }
141
142 return 0;
143 }
144
145
146 HWND create_desktop_window(HINSTANCE hInstance)
147 {
148 WindowClass wcDesktop(_T("Program Manager"));
149
150 wcDesktop.style = CS_DBLCLKS;
151 wcDesktop.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
152 wcDesktop.hIcon = LoadIcon(NULL, IDI_APPLICATION);
153 wcDesktop.hCursor = LoadCursor(NULL, IDC_ARROW);
154
155
156 ATOM desktopClass = wcDesktop.Register();
157
158 int width = GetSystemMetrics(SM_CXSCREEN);
159 int height = GetSystemMetrics(SM_CYSCREEN);
160
161 return Window::Create(WINDOW_CREATOR(DesktopWindow),
162 0, (LPCTSTR)(int)desktopClass, _T("Progman"), WS_POPUP|WS_VISIBLE|WS_CLIPCHILDREN,
163 0, 0, width, height, 0);
164 }