Added some resources.
[reactos.git] / rosapps / winfile / framewnd.c
1 /*
2 * ReactOS winfile
3 *
4 * framewnd.c
5 *
6 * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #ifdef _MSC_VER
24 #include "stdafx.h"
25 #else
26 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
27 #include <windows.h>
28 #include <commctrl.h>
29 #include <stdlib.h>
30 #include <malloc.h>
31 #include <memory.h>
32 #include <tchar.h>
33 #include <process.h>
34 #include <stdio.h>
35 #endif
36
37 //#include <shellapi.h>
38 #include <windowsx.h>
39 #include <assert.h>
40 #define ASSERT assert
41
42 #include "main.h"
43 #include "about.h"
44 #include "framewnd.h"
45 #include "childwnd.h"
46 #include "utils.h"
47 #include "run.h"
48 #include "format.h"
49 #include "dialogs.h"
50
51
52 ////////////////////////////////////////////////////////////////////////////////
53 // Global Variables:
54 //
55
56 BOOL bInMenuLoop = FALSE; // Tells us if we are in the menu loop
57 int nOldWidth; // Holds the previous client area width
58 int nOldHeight; // Holds the previous client area height
59
60
61 ////////////////////////////////////////////////////////////////////////////////
62 // Local module support methods
63 //
64
65 static void resize_frame_rect(HWND hWnd, PRECT prect)
66 {
67 int new_top;
68 RECT rt;
69
70 if (IsWindowVisible(Globals.hToolBar)) {
71 SendMessage(Globals.hToolBar, WM_SIZE, 0, 0);
72 GetClientRect(Globals.hToolBar, &rt);
73 prect->top = rt.bottom+3;
74 prect->bottom -= rt.bottom+3;
75 }
76 if (IsWindowVisible(Globals.hDriveBar)) {
77 SendMessage(Globals.hDriveBar, WM_SIZE, 0, 0);
78 GetClientRect(Globals.hDriveBar, &rt);
79 new_top = --prect->top + rt.bottom+3;
80 MoveWindow(Globals.hDriveBar, 0, prect->top, rt.right, new_top, TRUE);
81 prect->top = new_top;
82 prect->bottom -= rt.bottom+2;
83 }
84 if (IsWindowVisible(Globals.hStatusBar)) {
85 SetupStatusBar(TRUE);
86 // int parts[] = {300, 500};
87 // SendMessage(Globals.hStatusBar, WM_SIZE, 0, 0);
88 // SendMessage(Globals.hStatusBar, SB_SETPARTS, 2, (LPARAM)&parts);
89 GetClientRect(Globals.hStatusBar, &rt);
90 prect->bottom -= rt.bottom;
91 }
92 MoveWindow(Globals.hMDIClient, prect->left-1,prect->top-1,prect->right+2,prect->bottom+1, TRUE);
93 }
94
95 static void resize_frame(HWND hWnd, int cx, int cy)
96 {
97 RECT rect = {0, 0, cx, cy};
98
99 resize_frame_rect(hWnd, &rect);
100 }
101
102 void resize_frame_client(HWND hWnd)
103 {
104 RECT rect;
105
106 GetClientRect(hWnd, &rect);
107 resize_frame_rect(hWnd, &rect);
108 }
109
110 ////////////////////////////////////////////////////////////////////////////////
111 static HHOOK hcbthook;
112 static ChildWnd* newchild = NULL;
113
114 static LRESULT CALLBACK CBTProc(int code, WPARAM wParam, LPARAM lParam)
115 {
116 if (code == HCBT_CREATEWND && newchild) {
117 ChildWnd* pChildWnd = newchild;
118 newchild = NULL;
119 pChildWnd->hWnd = (HWND)wParam;
120 SetWindowLong(pChildWnd->hWnd, GWL_USERDATA, (LPARAM)pChildWnd);
121 }
122 return CallNextHookEx(hcbthook, code, wParam, lParam);
123 }
124
125
126 /*
127 BOOL FindChildWindow(int cmd)
128 {
129 TCHAR drv[_MAX_DRIVE];
130 LPCTSTR root = Globals.drives;
131 int i;
132 for(i = cmd - ID_DRIVE_FIRST; i--; root++)
133 while(*root)
134 root++;
135 if (activate_drive_window(root))
136 return TRUE;
137 _tsplitpath(root, drv, 0, 0, 0);
138 if (!SetCurrentDirectory(drv)) {
139 display_error(hWnd, GetLastError());
140 //return TRUE;
141 }
142 return FALSE;
143 }
144 */
145
146 static ChildWnd* alloc_child_window(LPCTSTR path)
147 {
148 TCHAR drv[_MAX_DRIVE+1], dir[_MAX_DIR], name[_MAX_FNAME], ext[_MAX_EXT];
149 ChildWnd* pChildWnd = (ChildWnd*)malloc(sizeof(ChildWnd));
150 Root* root = &pChildWnd->root;
151 Entry* entry;
152
153 memset(pChildWnd, 0, sizeof(ChildWnd));
154 pChildWnd->left.treePane = TRUE;
155 pChildWnd->left.visible_cols = 0;
156 pChildWnd->right.treePane = FALSE;
157 #ifndef _NO_EXTENSIONS
158 pChildWnd->right.visible_cols = COL_SIZE|COL_DATE|COL_TIME|COL_ATTRIBUTES|COL_INDEX|COL_LINKS;
159 #else
160 pChildWnd->right.visible_cols = COL_SIZE|COL_DATE|COL_TIME|COL_ATTRIBUTES;
161 #endif
162 pChildWnd->pos.length = sizeof(WINDOWPLACEMENT);
163 pChildWnd->pos.flags = 0;
164 pChildWnd->pos.showCmd = SW_SHOWNORMAL;
165 pChildWnd->pos.rcNormalPosition.left = CW_USEDEFAULT;
166 pChildWnd->pos.rcNormalPosition.top = CW_USEDEFAULT;
167 pChildWnd->pos.rcNormalPosition.right = CW_USEDEFAULT;
168 pChildWnd->pos.rcNormalPosition.bottom = CW_USEDEFAULT;
169 pChildWnd->nFocusPanel = 0;
170 pChildWnd->nSplitPos = 200;
171 pChildWnd->sortOrder = SORT_NAME;
172 pChildWnd->header_wdths_ok = FALSE;
173 lstrcpy(pChildWnd->szPath, path);
174 _tsplitpath(path, drv, dir, name, ext);
175 #if !defined(_NO_EXTENSIONS) && defined(__linux__)
176 if (*path == '/') {
177 root->drive_type = GetDriveType(path);
178 lstrcat(drv, _T("/"));
179 lstrcpy(root->volname, _T("root fs"));
180 root->fs_flags = 0;
181 lstrcpy(root->fs, _T("unixfs"));
182 lstrcpy(root->path, _T("/"));
183 entry = read_tree_unix(root, path, pChildWnd->sortOrder);
184 } else
185 #endif
186 {
187 root->drive_type = GetDriveType(path);
188 lstrcat(drv, _T("\\"));
189 GetVolumeInformation(drv, root->volname, _MAX_FNAME, 0, 0, &root->fs_flags, root->fs, _MAX_DIR);
190 lstrcpy(root->path, drv);
191 entry = read_tree_win(root, path, pChildWnd->sortOrder);
192 }
193 //@@lstrcpy(root->entry.data.cFileName, drv);
194 wsprintf(root->entry.data.cFileName, _T("%s - %s"), drv, root->fs);
195 root->entry.data.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
196 pChildWnd->left.root = &root->entry;
197 set_curdir(pChildWnd, entry);
198 return pChildWnd;
199 }
200
201 HWND CreateChildWindow(int drv_id)
202 {
203 //TCHAR drv[_MAX_DRIVE];
204 TCHAR path[MAX_PATH];
205 ChildWnd* pChildWnd = NULL;
206 /*
207 LPCTSTR root = Globals.drives;
208 int i;
209 for(i = cmd - ID_DRIVE_FIRST; i--; root++)
210 while(*root)
211 root++;
212 if (activate_drive_window(root))
213 return 0;
214 _tsplitpath(root, drv, 0, 0, 0);
215 if (!SetCurrentDirectory(drv)) {
216 display_error(hWnd, GetLastError());
217 return 0;
218 }
219 */
220 GetCurrentDirectory(MAX_PATH, path);
221 // pChildWnd = (ChildWnd*)malloc(sizeof(ChildWnd));
222 pChildWnd = alloc_child_window(path);
223 // if (!create_child_window(pChildWnd))
224 // free(pChildWnd);
225
226 if (pChildWnd != NULL) {
227 MDICREATESTRUCT mcs = {
228 szChildClass, path, hInst,
229 CW_USEDEFAULT, CW_USEDEFAULT,
230 CW_USEDEFAULT, CW_USEDEFAULT,
231 0/*style*/, 0/*lParam*/
232 };
233 hcbthook = SetWindowsHookEx(WH_CBT, CBTProc, 0, GetCurrentThreadId());
234 newchild = pChildWnd;
235 pChildWnd->hWnd = (HWND)SendMessage(Globals.hMDIClient, WM_MDICREATE, 0, (LPARAM)&mcs);
236 UnhookWindowsHookEx(hcbthook);
237 if (pChildWnd->hWnd == NULL) {
238 free(pChildWnd);
239 newchild = pChildWnd = NULL;
240 }
241 return pChildWnd->hWnd;
242 }
243 return 0;
244 }
245
246 BOOL CALLBACK CloseEnumProc(HWND hWnd, LPARAM lParam)
247 {
248 if (!GetWindow(hWnd, GW_OWNER)) {
249 SendMessage(GetParent(hWnd), WM_MDIRESTORE, (WPARAM)hWnd, 0);
250 if (SendMessage(hWnd, WM_QUERYENDSESSION, 0, 0)) {
251 SendMessage(GetParent(hWnd), WM_MDIDESTROY, (WPARAM)hWnd, 0);
252 }
253 }
254 return 1;
255 }
256
257 void OnEnterMenuLoop(HWND hWnd)
258 {
259 int nParts;
260
261 // Update the status bar pane sizes
262 nParts = -1;
263 SendMessage(Globals.hStatusBar, SB_SETPARTS, 1, (long)&nParts);
264 bInMenuLoop = TRUE;
265 SendMessage(Globals.hStatusBar, SB_SETTEXT, (WPARAM)0, (LPARAM)_T(""));
266 }
267
268 void OnExitMenuLoop(HWND hWnd)
269 {
270 RECT rc;
271 int nParts[3];
272
273 bInMenuLoop = FALSE;
274 // Update the status bar pane sizes
275 GetClientRect(hWnd, &rc);
276 nParts[0] = 100;
277 nParts[1] = 210;
278 nParts[2] = rc.right;
279 SendMessage(Globals.hStatusBar, SB_SETPARTS, 3, (long)nParts);
280 SendMessage(Globals.hStatusBar, SB_SETTEXT, 0, (LPARAM)_T(""));
281 SetupStatusBar(TRUE);
282 UpdateStatusBar();
283 }
284
285 void OnMenuSelect(HWND hWnd, UINT nItemID, UINT nFlags, HMENU hSysMenu)
286 {
287 TCHAR str[100];
288
289 strcpy(str, TEXT(""));
290 if (nFlags & MF_POPUP) {
291 if (hSysMenu != GetMenu(hWnd)) {
292 if (nItemID == 2) nItemID = 5;
293 }
294 }
295 if (LoadString(Globals.hInstance, nItemID, str, 100)) {
296 // load appropriate string
297 LPTSTR lpsz = str;
298 // first newline terminates actual string
299 lpsz = _tcschr(lpsz, '\n');
300 if (lpsz != NULL)
301 *lpsz = '\0';
302 }
303 SendMessage(Globals.hStatusBar, SB_SETTEXT, 0, (LPARAM)str);
304 }
305
306
307 static BOOL activate_drive_window(LPCTSTR path)
308 {
309 TCHAR drv1[_MAX_DRIVE], drv2[_MAX_DRIVE];
310 HWND child_wnd;
311
312 _tsplitpath(path, drv1, 0, 0, 0);
313
314 // search for a already open window for the same drive
315 for (child_wnd = GetNextWindow(Globals.hMDIClient,GW_CHILD);
316 child_wnd;
317 child_wnd = GetNextWindow(child_wnd, GW_HWNDNEXT)) {
318 ChildWnd* pChildWnd = (ChildWnd*) GetWindowLong(child_wnd, GWL_USERDATA);
319 if (pChildWnd) {
320 _tsplitpath(pChildWnd->root.path, drv2, 0, 0, 0);
321 if (!lstrcmpi(drv2, drv1)) {
322 SendMessage(Globals.hMDIClient, WM_MDIACTIVATE, (WPARAM)child_wnd, 0);
323 if (IsMinimized(child_wnd))
324 ShowWindow(child_wnd, SW_SHOWNORMAL);
325 return TRUE;
326 }
327 }
328 }
329 return FALSE;
330 }
331
332 static void toggle_child(HWND hWnd, UINT cmd, HWND hchild)
333 {
334 BOOL vis = IsWindowVisible(hchild);
335
336 CheckMenuItem(Globals.hMenuOptions, cmd, vis?MF_BYCOMMAND:MF_BYCOMMAND|MF_CHECKED);
337 ShowWindow(hchild, vis?SW_HIDE:SW_SHOW);
338 #ifndef _NO_EXTENSIONS
339 if (g_fullscreen.mode)
340 fullscreen_move(hWnd);
341 #endif
342 resize_frame_client(hWnd);
343 }
344
345 ////////////////////////////////////////////////////////////////////////////////
346 //
347 // FUNCTION: _CmdWndProc(HWND, unsigned, WORD, LONG)
348 //
349 // PURPOSE: Processes WM_COMMAND messages for the main frame window.
350 //
351 //
352
353 LRESULT _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
354 {
355 UINT cmd = LOWORD(wParam);
356 HWND hChildWnd;
357 // HWND hwndClient = (HWND)SendMessage(Globals.hMDIClient, WM_MDIGETACTIVE, 0, 0);
358 // if (hwndClient)
359 // if (SendMessage(hwndClient, WM_DISPATCH_COMMAND, wParam, lParam))
360 // return 0;
361
362 if (cmd >= ID_DRIVE_FIRST && cmd <= (ID_DRIVE_FIRST + 0xFF)) {
363 TCHAR drv[_MAX_DRIVE];
364 //TCHAR path[MAX_PATH];
365 //ChildWnd* pChildWnd;
366 LPCTSTR root = Globals.drives;
367 int i;
368 for (i = cmd - ID_DRIVE_FIRST; i--; root++)
369 while (*root)
370 root++;
371 if (activate_drive_window(root))
372 return 0;
373 _tsplitpath(root, drv, 0, 0, 0);
374 if (!SetCurrentDirectory(drv)) {
375 display_error(hWnd, GetLastError());
376 return 0;
377 }
378 //GetCurrentDirectory(MAX_PATH, path); //@@ letztes Verzeichnis pro Laufwerk speichern
379 //CreateChildWindow(path);
380 CreateChildWindow(cmd - ID_DRIVE_FIRST);
381
382 // pChildWnd = alloc_child_window(path);
383 // if (!create_child_window(pChildWnd))
384 // free(pChildWnd);
385 } else {
386 switch (cmd) {
387 case ID_WINDOW_CLOSEALL:
388 EnumChildWindows(Globals.hMDIClient, &CloseEnumProc, 0);
389 break;
390 case ID_WINDOW_CLOSE:
391 hChildWnd = (HWND) SendMessage(Globals.hMDIClient, WM_MDIGETACTIVE, 0, 0);
392 if (!SendMessage(hChildWnd, WM_QUERYENDSESSION, 0, 0))
393 SendMessage(Globals.hMDIClient, WM_MDIDESTROY, (WPARAM)hChildWnd, 0);
394 break;
395 case ID_FILE_EXIT:
396 SendMessage(hWnd, WM_CLOSE, 0, 0);
397 break;
398 case ID_FILE_RUN:
399 OnFileRun();
400 break;
401 case ID_DISK_FORMAT_DISK:
402 // SHFormatDrive(hWnd, 0 /* A: */, SHFMT_ID_DEFAULT, 0);
403 {
404 UINT OldMode = SetErrorMode(0); // Get the current Error Mode settings.
405 SetErrorMode(OldMode & ~SEM_FAILCRITICALERRORS); // Force O/S to handle
406 // Call SHFormatDrive here.
407 SHFormatDrive(hWnd, 0 /* A: */, SHFMT_ID_DEFAULT, 0);
408 SetErrorMode(OldMode); // Put it back the way it was.
409 }
410 break;
411 case ID_VIEW_BY_FILE_TYPE:
412 {
413 struct ExecuteDialog dlg = {{0}};
414 if (DialogBoxParam(Globals.hInstance, MAKEINTRESOURCE(IDD_DIALOG_VIEW_TYPE), hWnd, ExecuteDialogWndProg, (LPARAM)&dlg) == IDOK) {
415 }
416 }
417 break;
418 case ID_OPTIONS_CONFIRMATION:
419 /*
420 #define IDD_DIALOG_DIRECTORY 144
421 #define IDD_DIALOG_VIEW_TYPE 145
422 #define IDD_DIALOG_OPTIONS_CONFIRMATON 146
423 */
424 {
425 struct ExecuteDialog dlg = {{0}};
426 if (DialogBoxParam(Globals.hInstance, MAKEINTRESOURCE(IDD_DIALOG_OPTIONS_CONFIRMATON), hWnd, ExecuteDialogWndProg, (LPARAM)&dlg) == IDOK) {
427 // ShellExecute(hWnd, _T("open")/*operation*/, dlg.cmd/*file*/, NULL/*parameters*/, NULL/*dir*/, dlg.cmdshow);
428 }
429 }
430 break;
431 case ID_OPTIONS_FONT:
432 break;
433 case ID_OPTIONS_CUSTOMISE_TOOLBAR:
434 break;
435 case ID_OPTIONS_TOOLBAR:
436 toggle_child(hWnd, cmd, Globals.hToolBar);
437 break;
438 case ID_OPTIONS_DRIVEBAR:
439 toggle_child(hWnd, cmd, Globals.hDriveBar);
440 break;
441 case ID_OPTIONS_STATUSBAR:
442 toggle_child(hWnd, cmd, Globals.hStatusBar);
443 break;
444 case ID_OPTIONS_OPEN_NEW_WINDOW_ON_CONNECT:
445 break;
446 case ID_OPTIONS_MINIMISE_ON_USE:
447 break;
448 case ID_OPTIONS_SAVE_ON_EXIT:
449 break;
450
451 case ID_WINDOW_NEW_WINDOW:
452 CreateChildWindow(-1);
453 // {
454 // ChildWnd* pChildWnd = alloc_child_window(path);
455 // if (!create_child_window(pChildWnd))
456 // free(pChildWnd);
457 // }
458 break;
459 case ID_WINDOW_CASCADE:
460 SendMessage(Globals.hMDIClient, WM_MDICASCADE, 0, 0);
461 break;
462 case ID_WINDOW_TILE_HORZ:
463 SendMessage(Globals.hMDIClient, WM_MDITILE, MDITILE_HORIZONTAL, 0);
464 break;
465 case ID_WINDOW_TILE_VERT:
466 SendMessage(Globals.hMDIClient, WM_MDITILE, MDITILE_VERTICAL, 0);
467 break;
468 case ID_WINDOW_ARRANGE_ICONS:
469 SendMessage(Globals.hMDIClient, WM_MDIICONARRANGE, 0, 0);
470 break;
471 case ID_HELP_CONTENTS:
472 WinHelp(hWnd, _T("winfile"), HELP_INDEX, 0);
473 break;
474 case ID_HELP_SEARCH_HELP:
475 break;
476 case ID_HELP_HOW_TO_USE_HELP:
477 #ifdef WINSHELLAPI
478 ShellAbout(hWnd, szTitle, "", LoadIcon(Globals.hInstance, (LPCTSTR)IDI_WINFILE));
479 #endif
480 break;
481 case ID_HELP_ABOUT:
482 ShowAboutBox(hWnd);
483 break;
484 default:
485 /*
486 if ((cmd<IDW_FIRST_CHILD || cmd>=IDW_FIRST_CHILD+0x100) &&
487 (cmd<SC_SIZE || cmd>SC_RESTORE)) {
488 MessageBox(hWnd, _T("Not yet implemented"), _T("Winefile"), MB_OK);
489 }
490 return DefFrameProc(hWnd, Globals.hMDIClient, message, wParam, lParam);
491 */
492 hChildWnd = (HWND)SendMessage(Globals.hMDIClient, WM_MDIGETACTIVE, 0, 0);
493 if (IsWindow(hChildWnd))
494 SendMessage(hChildWnd, WM_COMMAND, wParam, lParam);
495 else
496 return DefFrameProc(hWnd, Globals.hMDIClient, message, wParam, lParam);
497 }
498 }
499 return 0;
500 }
501
502
503 ////////////////////////////////////////////////////////////////////////////////
504 //
505 // FUNCTION: FrameWndProc(HWND, unsigned, WORD, LONG)
506 //
507 // PURPOSE: Processes messages for the main frame window.
508 //
509 // WM_COMMAND - process the application menu
510 // WM_DESTROY - post a quit message and return
511 //
512 //
513
514 LRESULT CALLBACK FrameWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
515 {
516 switch (message) {
517 case WM_CREATE:
518 {
519 HMENU hMenuWindow = GetSubMenu(Globals.hMenuFrame, GetMenuItemCount(Globals.hMenuFrame)-2);
520 CLIENTCREATESTRUCT ccs = { hMenuWindow, IDW_FIRST_CHILD };
521 #if 0
522 hMDIClient = CreateWindow(_T("MDICLIENT"), NULL,
523 WS_CHILD|WS_CLIPCHILDREN|WS_VISIBLE,
524 0, 0, 0, 0,
525 hWnd, (HMENU)1, hInst, &ccs);
526 #else
527 Globals.hMDIClient = CreateWindowEx(0, _T("MDICLIENT"), NULL,
528 //WS_CHILD|WS_CLIPCHILDREN|WS_VSCROLL|WS_HSCROLL|WS_VISIBLE|WS_BORDER,
529 WS_CHILD|WS_CLIPCHILDREN|WS_VISIBLE,
530 0, 0, 0, 0,
531 hWnd, (HMENU)0, hInst, &ccs);
532 #endif
533 }
534 break;
535 case WM_COMMAND:
536 return _CmdWndProc(hWnd, message, wParam, lParam);
537 break;
538 case WM_SIZE:
539 resize_frame_client(hWnd);
540 break;
541 case WM_ENTERMENULOOP:
542 OnEnterMenuLoop(hWnd);
543 break;
544 case WM_EXITMENULOOP:
545 OnExitMenuLoop(hWnd);
546 break;
547 case WM_MENUSELECT:
548 OnMenuSelect(hWnd, LOWORD(wParam), HIWORD(wParam), (HMENU)lParam);
549 break;
550 case WM_DESTROY:
551 PostQuitMessage(0);
552 break;
553 case WM_QUERYENDSESSION:
554 case WM_CLOSE:
555 SendMessage(hWnd, WM_COMMAND, ID_WINDOW_CLOSEALL, 0);
556 if (GetWindow(Globals.hMDIClient, GW_CHILD) != NULL)
557 return 0;
558 // else fall thru...
559 default:
560 return DefFrameProc(hWnd, Globals.hMDIClient, message, wParam, lParam);
561 }
562 return 0;
563 }