9cf2a46d5e66eb50be5ef72714c840b1ab070a6d
[reactos.git] / 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>
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 }
56
57
58 ResString::ResString(UINT nid)
59 {
60         TCHAR buffer[BUFFER_LEN];
61
62         int len = LoadString(g_Globals._hInstance, nid, buffer, sizeof(buffer)/sizeof(TCHAR));
63
64         assign(buffer, len);
65 }
66
67
68 ResIcon::ResIcon(UINT nid)
69 {
70         _hIcon = LoadIcon(g_Globals._hInstance, MAKEINTRESOURCE(nid));
71 }
72
73 SmallIcon::SmallIcon(UINT nid)
74 {
75         _hIcon = (HICON)LoadImage(g_Globals._hInstance, MAKEINTRESOURCE(nid), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
76 }
77
78
79 ResBitmap::ResBitmap(UINT nid)
80 {
81         _hBmp = LoadBitmap(g_Globals._hInstance, MAKEINTRESOURCE(nid));
82 }
83
84
85 void explorer_show_frame(HWND hwndDesktop, int cmdshow)
86 {
87         if (g_Globals._hMainWnd)
88                 return;
89
90         g_Globals._prescan_nodes = false;
91
92          // create main window
93         HWND hMainFrame = MainFrame::Create();
94
95         if (hMainFrame) {
96                 g_Globals._hMainWnd = hMainFrame;
97
98                 ShowWindow(hMainFrame, cmdshow);
99                 UpdateWindow(hMainFrame);
100
101                  // Open the first child window after initializing the whole application
102                 PostMessage(hMainFrame, PM_OPEN_WINDOW, TRUE/*mode_explore*/, 0);
103         }
104 }
105
106
107 static void InitInstance(HINSTANCE hInstance)
108 {
109         setlocale(LC_COLLATE, "");      // set collating rules to local settings for compareName
110
111          // register frame window class
112         g_Globals._hframeClass = IconWindowClass(CLASSNAME_FRAME,IDI_EXPLORER);
113
114          // register child windows class
115         WindowClass(CLASSNAME_CHILDWND, CS_CLASSDC|CS_DBLCLKS|CS_VREDRAW).Register();
116
117          // register tree windows class
118         WindowClass(CLASSNAME_WINEFILETREE, CS_CLASSDC|CS_DBLCLKS|CS_VREDRAW).Register();
119
120         g_Globals._cfStrFName = RegisterClipboardFormat(CFSTR_FILENAME);
121 }
122
123
124 int explorer_main(HINSTANCE hInstance, HWND hwndDesktop, int cmdshow)
125 {
126          // initialize COM and OLE
127         OleInit usingCOM;
128
129          // initialize Common Controls library
130         CommonControlInit usingCmnCtrl;
131
132         try {
133                 InitInstance(hInstance);
134         } catch(COMException& e) {
135                 HandleException(e, g_Globals._hMainWnd);
136                 return -1;
137         }
138
139         if (hwndDesktop)
140                 g_Globals._desktop_mode = true;
141
142         if (cmdshow != SW_HIDE) {
143 /*      // don't maximize if being called from the ROS desktop
144                 if (cmdshow == SW_SHOWNORMAL)
145                                 //TODO: read window placement from registry
146                         cmdshow = SW_MAXIMIZE;
147 */
148
149                 explorer_show_frame(hwndDesktop, cmdshow);
150         }
151
152         return Window::MessageLoop();
153 }
154
155
156 int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd)
157 {
158          // create desktop window and task bar only, if there is no other shell and we are
159          // the first explorer instance
160         BOOL startup_desktop = !IsAnyDesktopRunning();
161
162          // If there is given the command line option "-desktop", create desktop window anyways
163         if (!lstrcmp(lpCmdLine,TEXT("-desktop")))
164                 startup_desktop = TRUE;
165
166         if (!lstrcmp(lpCmdLine,TEXT("-noexplorer"))) {
167                 nShowCmd = SW_HIDE;
168                 startup_desktop = TRUE;
169         }
170
171         g_Globals._hInstance = hInstance;
172
173         HWND hwndDesktop = 0;
174
175         if (startup_desktop)
176         {
177                 hwndDesktop = DesktopWindow::Create();
178
179 #ifndef _DEBUG  //MF: disabled for debugging
180                 {
181                 char* argv[] = {""};
182                 startup(1, argv);
183                 }
184 #endif
185         }
186
187         int ret = explorer_main(hInstance, hwndDesktop, nShowCmd);
188
189         return ret;
190 }