up/down navigation for shell views in SDI mode
[reactos.git] / reactos / subsys / system / explorer / explorer.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 // explorer.cpp
24 //
25 // Martin Fuchs, 23.07.2003
26 //
27 // Credits: Thanks to Leon Finker for his explorer window example
28 //
29
30
31 #include "utility/utility.h"
32
33 #include "explorer.h"
34 #include "desktop/desktop.h"
35
36 #include "globals.h"
37 #include "externals.h"
38
39 #include "explorer_intres.h"
40
41 #include <locale.h> // for setlocale()
42
43
44 ExplorerGlobals g_Globals;
45
46
47 ExplorerGlobals::ExplorerGlobals()
48 {
49 _hInstance = 0;
50 _hframeClass = 0;
51 _cfStrFName = 0;
52 _hMainWnd = 0;
53 _prescan_nodes = false;
54 _desktop_mode = false;
55 _log = NULL;
56 #ifndef __MINGW32__ // SHRestricted() missing in MinGW (as of 29.10.2003)
57 _SHRestricted = 0;
58 #endif
59 _hwndDesktopBar = 0;
60 _hwndShellView = 0;
61 _hwndDesktop = 0;
62 }
63
64
65 void ExplorerGlobals::init(HINSTANCE hInstance)
66 {
67 _hInstance = hInstance;
68
69 #ifndef __MINGW32__ // SHRestricted() missing in MinGW (as of 29.10.2003)
70 _SHRestricted = (DWORD(STDAPICALLTYPE*)(RESTRICTIONS)) GetProcAddress(GetModuleHandle(TEXT("SHELL32")), "SHRestricted");
71 #endif
72
73 _icon_cache.init();
74 }
75
76
77 void _log_(LPCTSTR txt)
78 {
79 FmtString msg(TEXT("%s\n"), txt);
80
81 if (g_Globals._log)
82 _fputts(msg, g_Globals._log);
83
84 OutputDebugString(msg);
85 }
86
87
88 bool FileTypeManager::is_exe_file(LPCTSTR ext)
89 {
90 static const LPCTSTR s_executable_extensions[] = {
91 TEXT("COM"),
92 TEXT("EXE"),
93 TEXT("BAT"),
94 TEXT("CMD"),
95 TEXT("CMM"),
96 TEXT("BTM"),
97 TEXT("AWK"),
98 0
99 };
100
101 TCHAR ext_buffer[_MAX_EXT];
102 const LPCTSTR* p;
103 LPCTSTR s;
104 LPTSTR d;
105
106 for(s=ext+1,d=ext_buffer; (*d=toupper(*s)); s++)
107 ++d;
108
109 for(p=s_executable_extensions; *p; p++)
110 if (!lstrcmp(ext_buffer, *p))
111 return true;
112
113 return false;
114 }
115
116
117 const FileTypeInfo& FileTypeManager::operator[](String ext)
118 {
119 _tcslwr((LPTSTR)ext.c_str());
120
121 iterator found = find(ext);
122 if (found != end())
123 return found->second;
124
125 FileTypeInfo& ftype = super::operator[](ext);
126
127 ftype._neverShowExt = false;
128
129 HKEY hkey;
130 TCHAR value[MAX_PATH], display_name[MAX_PATH];;
131 LONG valuelen = sizeof(value);
132
133 if (!RegQueryValue(HKEY_CLASSES_ROOT, ext, value, &valuelen)) {
134 ftype._classname = value;
135
136 valuelen = sizeof(display_name);
137 if (!RegQueryValue(HKEY_CLASSES_ROOT, ftype._classname, display_name, &valuelen))
138 ftype._displayname = display_name;
139
140 if (!RegOpenKey(HKEY_CLASSES_ROOT, ftype._classname, &hkey)) {
141 if (!RegQueryValueEx(hkey, TEXT("NeverShowExt"), 0, NULL, NULL, NULL))
142 ftype._neverShowExt = true;
143
144 RegCloseKey(hkey);
145 }
146 }
147
148 return ftype;
149 }
150
151 LPCTSTR FileTypeManager::set_type(Entry* entry, bool dont_hide_ext)
152 {
153 LPCTSTR ext = _tcsrchr(entry->_data.cFileName, TEXT('.'));
154
155 if (ext) {
156 const FileTypeInfo& type = (*this)[ext];
157
158 if (!type._displayname.empty())
159 entry->_type_name = _tcsdup(type._displayname);
160
161 // hide some file extensions
162 if (type._neverShowExt && !dont_hide_ext) {
163 int len = ext - entry->_data.cFileName;
164 entry->_display_name = (LPTSTR) malloc((len+1)*sizeof(TCHAR));
165 _tcsncpy(entry->_display_name, entry->_data.cFileName, len);
166 entry->_display_name[len] = TEXT('\0');
167 }
168
169 if (is_exe_file(ext))
170 entry->_data.dwFileAttributes |= ATTRIBUTE_EXECUTABLE;
171 }
172
173 return ext;
174 }
175
176
177 Icon::Icon()
178 : _id(ICID_UNKNOWN),
179 _itype(IT_STATIC),
180 _hicon(0)
181 {
182 }
183
184 Icon::Icon(ICON_ID id, UINT nid)
185 : _id(id),
186 _itype(IT_STATIC),
187 _hicon(SmallIcon(nid))
188 {
189 }
190
191 Icon::Icon(ICON_TYPE itype, int id, HICON hIcon)
192 : _id((ICON_ID)id),
193 _itype(itype),
194 _hicon(hIcon)
195 {
196 }
197
198 Icon::Icon(ICON_TYPE itype, int id, int sys_idx)
199 : _id((ICON_ID)id),
200 _itype(itype),
201 _sys_idx(sys_idx)
202 {
203 }
204
205 void Icon::draw(HDC hdc, int x, int y, int cx, int cy, COLORREF bk_color, HBRUSH bk_brush) const
206 {
207 if (_itype == IT_SYSCACHE)
208 ImageList_DrawEx(g_Globals._icon_cache.get_sys_imagelist(), _sys_idx, hdc, x, y, cx, cy, bk_color, CLR_DEFAULT, ILD_NORMAL);
209 else
210 DrawIconEx(hdc, x, y, _hicon, cx, cy, 0, bk_brush, DI_NORMAL);
211 }
212
213 HBITMAP Icon::create_bitmap(COLORREF bk_color, HBRUSH hbrBkgnd, HDC hdc_wnd) const
214 {
215 if (_itype == IT_SYSCACHE) {
216 HIMAGELIST himl = g_Globals._icon_cache.get_sys_imagelist();
217
218 int cx, cy;
219 ImageList_GetIconSize(himl, &cx, &cy);
220
221 HBITMAP hbmp = CreateCompatibleBitmap(hdc_wnd, cx, cy);
222 HDC hdc = CreateCompatibleDC(hdc_wnd);
223 HBITMAP hbmp_old = SelectBitmap(hdc, hbmp);
224 ImageList_DrawEx(himl, _sys_idx, hdc, 0, 0, cx, cy, bk_color, CLR_DEFAULT, ILD_NORMAL);
225 SelectBitmap(hdc, hbmp_old);
226 DeleteDC(hdc);
227 return hbmp;
228 } else
229 return create_bitmap_from_icon(_hicon, hbrBkgnd, hdc_wnd);
230 }
231
232 HBITMAP create_bitmap_from_icon(HICON hIcon, HBRUSH hbrush_bkgnd, HDC hdc_wnd)
233 {
234 HBITMAP hbmp = CreateCompatibleBitmap(hdc_wnd, 16, 16);
235
236 MemCanvas canvas;
237 BitmapSelection sel(canvas, hbmp);
238
239 RECT rect = {0, 0, 16, 16};
240 FillRect(canvas, &rect, hbrush_bkgnd);
241
242 DrawIconEx(canvas, 0, 0, hIcon, 16, 16, 0, hbrush_bkgnd, DI_NORMAL);
243
244 return hbmp;
245 }
246
247
248 int IconCache::s_next_id = ICID_DYNAMIC;
249
250
251 void IconCache::init()
252 {
253 _icons[ICID_NONE] = Icon(IT_STATIC, ICID_NONE, (HICON)0);
254
255 _icons[ICID_FOLDER] = Icon(ICID_FOLDER, IDI_FOLDER);
256 //_icons[ICID_DOCUMENT] = Icon(ICID_DOCUMENT, IDI_DOCUMENT);
257 _icons[ICID_EXPLORER] = Icon(ICID_EXPLORER, IDI_EXPLORER);
258 _icons[ICID_APP] = Icon(ICID_APP, IDI_APPICON);
259
260 _icons[ICID_CONFIG] = Icon(ICID_CONFIG, IDI_CONFIG);
261 _icons[ICID_DOCUMENTS] = Icon(ICID_DOCUMENTS, IDI_DOCUMENTS);
262 _icons[ICID_FAVORITES] = Icon(ICID_FAVORITES, IDI_FAVORITES);
263 _icons[ICID_INFO] = Icon(ICID_INFO, IDI_INFO);
264 _icons[ICID_APPS] = Icon(ICID_APPS, IDI_APPS);
265 _icons[ICID_SEARCH] = Icon(ICID_SEARCH, IDI_SEARCH);
266 _icons[ICID_ACTION] = Icon(ICID_ACTION, IDI_ACTION);
267 _icons[ICID_SEARCH_DOC] = Icon(ICID_SEARCH_DOC, IDI_SEARCH_DOC);
268 _icons[ICID_PRINTER] = Icon(ICID_PRINTER, IDI_PRINTER);
269 _icons[ICID_NETWORK] = Icon(ICID_NETWORK, IDI_NETWORK);
270 _icons[ICID_COMPUTER] = Icon(ICID_COMPUTER, IDI_COMPUTER);
271 _icons[ICID_LOGOFF] = Icon(ICID_LOGOFF, IDI_LOGOFF);
272 }
273
274
275 const Icon& IconCache::extract(const String& path)
276 {
277 PathMap::iterator found = _pathMap.find(path);
278
279 if (found != _pathMap.end())
280 return _icons[found->second];
281
282 SHFILEINFO sfi;
283
284 #if 1 // use system image list
285 HIMAGELIST himlSys = (HIMAGELIST) SHGetFileInfo(path, 0, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX|SHGFI_SMALLICON);
286
287 if (himlSys) {
288 _himlSys = himlSys;
289
290 const Icon& icon = add(sfi.iIcon/*, IT_SYSCACHE*/);
291 #else
292 if (SHGetFileInfo(path, 0, &sfi, sizeof(sfi), SHGFI_ICON|SHGFI_SMALLICON)) {
293 const Icon& icon = add(sfi.hIcon, IT_CACHED);
294 #endif
295
296 ///@todo limit cache size
297 _pathMap[path] = icon;
298
299 return icon;
300 } else
301 return _icons[ICID_NONE];
302 }
303
304 const Icon& IconCache::extract(LPCTSTR path, int idx)
305 {
306 CachePair key(path, idx);
307
308 _tcslwr((LPTSTR)key.first.c_str());
309
310 PathIdxMap::iterator found = _pathIdxMap.find(key);
311
312 if (found != _pathIdxMap.end())
313 return _icons[found->second];
314
315 HICON hIcon;
316
317 if ((int)ExtractIconEx(path, idx, NULL, &hIcon, 1) > 0) {
318 const Icon& icon = add(hIcon, IT_CACHED);
319
320 _pathIdxMap[key] = icon;
321
322 return icon;
323 } else
324 return _icons[ICID_NONE];
325 }
326
327 const Icon& IconCache::extract(IExtractIcon* pExtract, LPCTSTR path, int idx)
328 {
329 HICON hIconLarge = 0;
330 HICON hIcon;
331
332 HRESULT hr = pExtract->Extract(path, idx, &hIconLarge, &hIcon, MAKELONG(0/*GetSystemMetrics(SM_CXICON)*/,GetSystemMetrics(SM_CXSMICON)));
333
334 if (hr == NOERROR) {
335 if (hIconLarge)
336 DestroyIcon(hIconLarge);
337
338 if (hIcon)
339 return add(hIcon);
340 }
341
342 return _icons[ICID_NONE];
343 }
344
345 const Icon& IconCache::add(HICON hIcon, ICON_TYPE type)
346 {
347 int id = ++s_next_id;
348
349 return _icons[id] = Icon(type, id, hIcon);
350 }
351
352 const Icon& IconCache::add(int sys_idx/*, ICON_TYPE type=IT_SYSCACHE*/)
353 {
354 int id = ++s_next_id;
355
356 return _icons[id] = SysCacheIcon(id, sys_idx);
357 }
358
359 const Icon& IconCache::get_icon(int id)
360 {
361 return _icons[id];
362 }
363
364 void IconCache::free_icon(int icon_id)
365 {
366 IconMap::iterator found = _icons.find(icon_id);
367
368 if (found != _icons.end()) {
369 Icon& icon = found->second;
370
371 if (icon.destroy())
372 _icons.erase(found);
373 }
374 }
375
376
377 ResString::ResString(UINT nid)
378 {
379 TCHAR buffer[BUFFER_LEN];
380
381 int len = LoadString(g_Globals._hInstance, nid, buffer, sizeof(buffer)/sizeof(TCHAR));
382
383 super::assign(buffer, len);
384 }
385
386
387 ResIcon::ResIcon(UINT nid)
388 {
389 _hicon = LoadIcon(g_Globals._hInstance, MAKEINTRESOURCE(nid));
390 }
391
392 SmallIcon::SmallIcon(UINT nid)
393 {
394 _hicon = (HICON)LoadImage(g_Globals._hInstance, MAKEINTRESOURCE(nid), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
395 }
396
397 ResIconEx::ResIconEx(UINT nid, int w, int h)
398 {
399 _hicon = (HICON)LoadImage(g_Globals._hInstance, MAKEINTRESOURCE(nid), IMAGE_ICON, w, h, LR_SHARED);
400 }
401
402
403 void SetWindowIcon(HWND hwnd, UINT nid)
404 {
405 HICON hIcon = ResIcon(nid);
406 Window_SetIcon(hwnd, ICON_BIG, hIcon);
407
408 HICON hIconSmall = SmallIcon(nid);
409 Window_SetIcon(hwnd, ICON_SMALL, hIconSmall);
410 }
411
412
413 ResBitmap::ResBitmap(UINT nid)
414 {
415 _hBmp = LoadBitmap(g_Globals._hInstance, MAKEINTRESOURCE(nid));
416 }
417
418
419 void explorer_show_frame(int cmdshow, LPTSTR lpCmdLine)
420 {
421 if (g_Globals._hMainWnd) {
422 if (IsIconic(g_Globals._hMainWnd))
423 ShowWindow(g_Globals._hMainWnd, SW_RESTORE);
424 else
425 SetForegroundWindow(g_Globals._hMainWnd);
426
427 return;
428 }
429
430 g_Globals._prescan_nodes = false;
431
432 // create main window
433 HWND hMainFrame = MainFrame::Create();
434
435 if (hMainFrame) {
436 g_Globals._hMainWnd = hMainFrame;
437
438 ShowWindow(hMainFrame, cmdshow);
439 UpdateWindow(hMainFrame);
440
441 bool valid_dir = false;
442
443 if (lpCmdLine) {
444 DWORD attribs = GetFileAttributes(lpCmdLine);
445
446 if (attribs!=INVALID_FILE_ATTRIBUTES && (attribs&FILE_ATTRIBUTE_DIRECTORY))
447 valid_dir = true;
448 else if (*lpCmdLine==':' || *lpCmdLine=='"')
449 valid_dir = true;
450 }
451
452 // Open the first child window after initializing the application
453 if (valid_dir)
454 PostMessage(hMainFrame, PM_OPEN_WINDOW, 0, (LPARAM)lpCmdLine);
455 else
456 PostMessage(hMainFrame, PM_OPEN_WINDOW, 0/*OWM_EXPLORE|OWM_DETAILS*/, 0);
457 }
458 }
459
460
461 PopupMenu::PopupMenu(UINT nid)
462 {
463 HMENU hMenu = LoadMenu(g_Globals._hInstance, MAKEINTRESOURCE(nid));
464 _hmenu = GetSubMenu(hMenu, 0);
465 }
466
467
468 /// "About Explorer" Dialog
469 struct ExplorerAboutDlg : public
470 CtlColorParent<
471 OwnerDrawParent<Dialog>
472 >
473 {
474 typedef CtlColorParent<
475 OwnerDrawParent<Dialog>
476 > super;
477
478 ExplorerAboutDlg(HWND hwnd)
479 : super(hwnd)
480 {
481 SetWindowIcon(hwnd, IDI_REACTOS);
482
483 new FlatButton(hwnd, IDOK);
484
485 _hfont = CreateFont(20, 0, 0, 0, FW_BOLD, TRUE, 0, 0, 0, 0, 0, 0, 0, TEXT("Sans Serif"));
486 new ColorStatic(hwnd, IDC_ROS_EXPLORER, RGB(32,32,128), 0, _hfont);
487
488 new HyperlinkCtrl(hwnd, IDC_WWW);
489
490 CenterWindow(hwnd);
491 }
492
493 ~ExplorerAboutDlg()
494 {
495 DeleteObject(_hfont);
496 }
497
498 LRESULT WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam)
499 {
500 switch(nmsg) {
501 case WM_PAINT:
502 Paint();
503 break;
504
505 default:
506 return super::WndProc(nmsg, wparam, lparam);
507 }
508
509 return 0;
510 }
511
512 void Paint()
513 {
514 PaintCanvas canvas(_hwnd);
515
516 HICON hicon = (HICON) LoadImage(g_Globals._hInstance, MAKEINTRESOURCE(IDI_REACTOS_BIG), IMAGE_ICON, 0, 0, LR_SHARED);
517
518 DrawIconEx(canvas, 20, 10, hicon, 0, 0, 0, 0, DI_NORMAL);
519 }
520
521 protected:
522 HFONT _hfont;
523 };
524
525 void explorer_about(HWND hwndParent)
526 {
527 Dialog::DoModal(IDD_ABOUT_EXPLORER, WINDOW_CREATOR(ExplorerAboutDlg), hwndParent);
528 }
529
530
531 static void InitInstance(HINSTANCE hInstance)
532 {
533 CONTEXT("InitInstance");
534
535 setlocale(LC_COLLATE, ""); // set collating rules to local settings for compareName
536
537 // register frame window class
538 g_Globals._hframeClass = IconWindowClass(CLASSNAME_FRAME,IDI_EXPLORER);
539
540 // register child windows class
541 WindowClass(CLASSNAME_CHILDWND, CS_CLASSDC|CS_DBLCLKS|CS_VREDRAW).Register();
542
543 // register tree windows class
544 WindowClass(CLASSNAME_WINEFILETREE, CS_CLASSDC|CS_DBLCLKS|CS_VREDRAW).Register();
545
546 g_Globals._cfStrFName = RegisterClipboardFormat(CFSTR_FILENAME);
547 }
548
549
550 int explorer_main(HINSTANCE hInstance, LPTSTR lpCmdLine, int cmdshow)
551 {
552 CONTEXT("explorer_main");
553
554 // initialize Common Controls library
555 CommonControlInit usingCmnCtrl;
556
557 try {
558 InitInstance(hInstance);
559 } catch(COMException& e) {
560 HandleException(e, GetDesktopWindow());
561 return -1;
562 }
563
564 if (cmdshow != SW_HIDE) {
565 /* // don't maximize if being called from the ROS desktop
566 if (cmdshow == SW_SHOWNORMAL)
567 ///@todo read window placement from registry
568 cmdshow = SW_MAXIMIZE;
569 */
570
571 explorer_show_frame(cmdshow, lpCmdLine);
572 }
573
574 return Window::MessageLoop();
575 }
576
577
578 // MinGW does not provide a Unicode startup routine, so we have to implement an own.
579 #if defined(__MINGW32__) && defined(UNICODE)
580
581 #define _tWinMain wWinMain
582 int WINAPI wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int);
583
584 int main(int argc, char* argv[])
585 {
586 CONTEXT("main");
587
588 STARTUPINFO startupinfo;
589 int nShowCmd = SW_SHOWNORMAL;
590
591 GetStartupInfo(&startupinfo);
592
593 if (startupinfo.dwFlags & STARTF_USESHOWWINDOW)
594 nShowCmd = startupinfo.wShowWindow;
595
596 LPWSTR cmdline = GetCommandLineW();
597
598 while(*cmdline && !_istspace(*cmdline))
599 ++cmdline;
600
601 return wWinMain(GetModuleHandle(NULL), 0, cmdline, nShowCmd);
602 }
603
604 #endif // __MINGW && UNICODE
605
606
607 int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd)
608 {
609 CONTEXT("WinMain()");
610
611 BOOL any_desktop_running = IsAnyDesktopRunning();
612
613 // create desktop window and task bar only, if there is no other shell and we are
614 // the first explorer instance
615 BOOL startup_desktop = !any_desktop_running;
616
617 bool autostart = !any_desktop_running;
618
619 // disable autostart if the SHIFT key is pressed
620 if (GetAsyncKeyState(VK_SHIFT) < 0)
621 autostart = false;
622
623 #ifdef _DEBUG //MF: disabled for debugging
624 autostart = false;
625 #endif
626
627 // If there is given the command line option "-desktop", create desktop window anyways
628 if (_tcsstr(lpCmdLine,TEXT("-desktop")))
629 startup_desktop = TRUE;
630 else if (_tcsstr(lpCmdLine,TEXT("-nodesktop")))
631 startup_desktop = FALSE;
632
633 // Don't display cabinet window in desktop mode
634 if (startup_desktop && !_tcsstr(lpCmdLine,TEXT("-explorer")))
635 nShowCmd = SW_HIDE;
636
637 if (_tcsstr(lpCmdLine,TEXT("-noautostart")))
638 autostart = false;
639
640 g_Globals.init(hInstance);
641
642 // initialize COM and OLE before creating the desktop window
643 OleInit usingCOM;
644
645 if (startup_desktop) {
646 g_Globals._desktops.init();
647
648 g_Globals._hwndDesktop = DesktopWindow::Create();
649
650 if (autostart) {
651 char* argv[] = {"", "s"}; // call startup routine in SESSION_START mode
652 startup(2, argv);
653 }
654 }
655
656 /**TODO fix command line handling */
657 if (*lpCmdLine=='"' && lpCmdLine[_tcslen(lpCmdLine)-1]=='"') {
658 ++lpCmdLine;
659 lpCmdLine[_tcslen(lpCmdLine)-1] = '\0';
660 }
661
662 if (g_Globals._hwndDesktop)
663 g_Globals._desktop_mode = true;
664
665 int ret = explorer_main(hInstance, lpCmdLine, nShowCmd);
666
667 return ret;
668 }