1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
 *  ReactOS Application
 *
 *  framewnd.c
 *
 *  Copyright (C) 2002  Robert Dickenson <robd@reactos.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ifdef _MSC_VER
#include "stdafx.h"
#else
#define WIN32_LEAN_AND_MEAN     // Exclude rarely-used stuff from Windows headers
#include <windows.h>
#include <commctrl.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#include <process.h>
#include <stdio.h>
#endif

#include "main.h"
#include "about.h"
#include "framewnd.h"


////////////////////////////////////////////////////////////////////////////////
// Global Variables:
//

BOOL bInMenuLoop = FALSE;        // Tells us if we are in the menu loop

////////////////////////////////////////////////////////////////////////////////
// Local module support methods
//

static void resize_frame_rect(HWND hWnd, PRECT prect)
{
    RECT rt;

    if (IsWindowVisible(hToolBar)) {
        SendMessage(hToolBar, WM_SIZE, 0, 0);
        GetClientRect(hToolBar, &rt);
        prect->top = rt.bottom+3;
        prect->bottom -= rt.bottom+3;
    }
    if (IsWindowVisible(hStatusBar)) {
        int parts[] = {300, 500};

        SendMessage(hStatusBar, WM_SIZE, 0, 0);
        SendMessage(hStatusBar, SB_SETPARTS, 2, (LPARAM)&parts);
        GetClientRect(hStatusBar, &rt);
        prect->bottom -= rt.bottom;
    }
    MoveWindow(hMDIClient, prect->left-1,prect->top-1,prect->right+2,prect->bottom+1, TRUE);
}

/* static void resize_frame(HWND hWnd, int cx, int cy)
{
    RECT rect = {0, 0, cx, cy};

    resize_frame_rect(hWnd, &rect);
}*/

void resize_frame_client(HWND hWnd)
{
    RECT rect;

    GetClientRect(hWnd, &rect);
    resize_frame_rect(hWnd, &rect);
}

////////////////////////////////////////////////////////////////////////////////
static HHOOK hcbthook;
static ChildWnd* newchild = NULL;

LRESULT CALLBACK CBTProc(int code, WPARAM wParam, LPARAM lParam)
{
    if (code == HCBT_CREATEWND && newchild) {
        ChildWnd* pChildWnd = newchild;
        newchild = NULL;
        pChildWnd->hWnd = (HWND)wParam;
        SetWindowLongPtr(pChildWnd->hWnd, GWLP_USERDATA, (LPARAM)pChildWnd);
    }
    return CallNextHookEx(hcbthook, code, wParam, lParam);
}

#if 0
HWND create_child_window(ChildWnd* pChildWnd)
{
    MDICREATESTRUCT mcs = {
        szChildClass, (LPTSTR)pChildWnd->path, hInst,
        pChildWnd->pos.rcNormalPosition.left, pChildWnd->pos.rcNormalPosition.top,
        pChildWnd->pos.rcNormalPosition.right-pChildWnd->pos.rcNormalPosition.left,
        pChildWnd->pos.rcNormalPosition.bottom-pChildWnd->pos.rcNormalPosition.top,
        0/*style*/, 0/*lParam*/
    };
    hcbthook = SetWindowsHookEx(WH_CBT, CBTProc, 0, GetCurrentThreadId());
    newchild = pChildWnd;
    pChildWnd->hWnd = (HWND)SendMessage(hMDIClient, WM_MDICREATE, 0, (LPARAM)&mcs);
    if (!pChildWnd->hWnd)
        return 0;
    UnhookWindowsHookEx(hcbthook);
    return pChildWnd->hWnd;
}

#endif


void toggle_child(HWND hWnd, UINT cmd, HWND hchild)
{
    BOOL vis = IsWindowVisible(hchild);

    HMENU hMenuOptions = GetSubMenu(hMenuFrame, ID_OPTIONS_MENU);
    CheckMenuItem(hMenuOptions, cmd, vis?MF_BYCOMMAND:MF_BYCOMMAND|MF_CHECKED);
    ShowWindow(hchild, vis?SW_HIDE:SW_SHOW);
    resize_frame_client(hWnd);
}


static HWND InitChildWindow(LPTSTR param)
{
	//TCHAR drv[_MAX_DRIVE];
	TCHAR path[MAX_PATH];
	ChildWnd* pChildWnd = NULL;
/*
	LPCTSTR root = Globals.drives;
	int i;
	for(i = cmd - ID_DRIVE_FIRST; i--; root++)
		while(*root)
			root++;
	if (activate_drive_window(root))
		return 0;
	_tsplitpath(root, drv, 0, 0, 0);
	if (!SetCurrentDirectory(drv)) {
		display_error(hWnd, GetLastError());
		return 0;
	}
 */
	GetCurrentDirectory(MAX_PATH, path);
//	pChildWnd = alloc_child_window(path);
//	if (!create_child_window(pChildWnd))
//		free(pChildWnd);
	pChildWnd = (ChildWnd*)malloc(sizeof(ChildWnd));
	if (pChildWnd != NULL) {
        MDICREATESTRUCT mcs = {
            szChildClass, path, hInst,
            CW_USEDEFAULT, CW_USEDEFAULT,
            CW_USEDEFAULT, CW_USEDEFAULT,
            0/*style*/, 0/*lParam*/
		};
		memset(pChildWnd, 0, sizeof(ChildWnd));
        lstrcpy(pChildWnd->szPath, path);
		pChildWnd->pos.length = sizeof(WINDOWPLACEMENT);
		pChildWnd->pos.flags = 0;
		pChildWnd->pos.showCmd = SW_SHOWNORMAL;
		pChildWnd->pos.rcNormalPosition.left = CW_USEDEFAULT;
		pChildWnd->pos.rcNormalPosition.top = CW_USEDEFAULT;
	    pChildWnd->pos.rcNormalPosition.right = CW_USEDEFAULT;
    	pChildWnd->pos.rcNormalPosition.bottom = CW_USEDEFAULT;
  	    pChildWnd->nFocusPanel = 0;
	    pChildWnd->nSplitPos = 200;
        hcbthook = SetWindowsHookEx(WH_CBT, CBTProc, 0, GetCurrentThreadId());
    	newchild = pChildWnd;
        pChildWnd->hWnd = (HWND)SendMessage(hMDIClient, WM_MDICREATE, 0, (LPARAM)&mcs);
        UnhookWindowsHookEx(hcbthook);
        if (pChildWnd->hWnd == NULL) {
            free(pChildWnd);
        	newchild = pChildWnd = NULL;<--- Assignment 'pChildWnd=NULL', assigned value is 0
        }
        return pChildWnd->hWnd;<--- Null pointer dereference
	}
    return 0;
}

BOOL CALLBACK CloseEnumProc(HWND hWnd, LPARAM lParam)
{
    if (!GetWindow(hWnd, GW_OWNER)) {
        SendMessage(GetParent(hWnd), WM_MDIRESTORE, (WPARAM)hWnd, 0);
        if (SendMessage(hWnd, WM_QUERYENDSESSION, 0, 0)) {
            SendMessage(GetParent(hWnd), WM_MDIDESTROY, (WPARAM)hWnd, 0);
        }
    }
    return 1;
}

void OnEnterMenuLoop(HWND hWnd)
{
    int nParts;

    // Update the status bar pane sizes
    nParts = -1;
    SendMessage(hStatusBar, SB_SETPARTS, 1, (LPARAM)&nParts);
    bInMenuLoop = TRUE;
    SendMessage(hStatusBar, SB_SETTEXT, (WPARAM)0, (LPARAM)_T(""));
}

void OnExitMenuLoop(HWND hWnd)
{
    RECT  rc;
    int   nParts[3];

    bInMenuLoop = FALSE;
    // Update the status bar pane sizes
    GetClientRect(hWnd, &rc);
    nParts[0] = 100;
    nParts[1] = 210;
    nParts[2] = rc.right;
    SendMessage(hStatusBar, SB_SETPARTS, 3, (LPARAM)nParts);
    SendMessage(hStatusBar, SB_SETTEXT, 0, (LPARAM)_T(""));
    UpdateStatusBar();
}

void OnMenuSelect(HWND hWnd, UINT nItemID, UINT nFlags, HMENU hSysMenu)
{
    TCHAR str[100];

    strcpy(str, TEXT(""));
    if (nFlags & MF_POPUP) {
        if (hSysMenu != GetMenu(hWnd)) {
            if (nItemID == 2) nItemID = 5;
        }
    }
    if (LoadString(hInst, nItemID, str, 100)) {
        // load appropriate string
        LPTSTR lpsz = str;
        // first newline terminates actual string
        lpsz = _tcschr(lpsz, '\n');
        if (lpsz != NULL)
            *lpsz = '\0';
    }
    SendMessage(hStatusBar, SB_SETTEXT, 0, (LPARAM)str);
}

////////////////////////////////////////////////////////////////////////////////
//
//  FUNCTION: _CmdWndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes WM_COMMAND messages for the main frame window.
//
//

LRESULT _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HWND hChildWnd;

    if (1) {

        switch (LOWORD(wParam)) {
        case ID_WINDOW_CLOSEALL:
            EnumChildWindows(hMDIClient, &CloseEnumProc, 0);
            break;
        case ID_WINDOW_CLOSE:
            hChildWnd = (HWND) SendMessage(hMDIClient, WM_MDIGETACTIVE, 0, 0);
            if (!SendMessage(hChildWnd, WM_QUERYENDSESSION, 0, 0))
                SendMessage(hMDIClient, WM_MDIDESTROY, (WPARAM)hChildWnd, 0);
            break;
        case ID_FILE_EXIT:
            SendMessage(hWnd, WM_CLOSE, 0, 0);
            break;
        case ID_OPTIONS_TOOLBAR:
            toggle_child(hWnd, LOWORD(wParam), hToolBar);
            break;
        case ID_OPTIONS_STATUSBAR:
            toggle_child(hWnd, LOWORD(wParam), hStatusBar);
            break;

        case ID_FILE_OPEN:
        case ID_WINDOW_NEW_WINDOW:
            InitChildWindow("Child Window");
            return 0;
        case ID_WINDOW_CASCADE:
            SendMessage(hMDIClient, WM_MDICASCADE, 0, 0);
            break;
        case ID_WINDOW_TILE_HORZ:
            SendMessage(hMDIClient, WM_MDITILE, MDITILE_HORIZONTAL, 0);
            break;
        case ID_WINDOW_TILE_VERT:
            SendMessage(hMDIClient, WM_MDITILE, MDITILE_VERTICAL, 0);
            break;
        case ID_WINDOW_ARRANGE_ICONS:
            SendMessage(hMDIClient, WM_MDIICONARRANGE, 0, 0);
            break;
        case ID_HELP_ABOUT:
            ShowAboutBox(hWnd);
            break;
        default:
            hChildWnd = (HWND)SendMessage(hMDIClient, WM_MDIGETACTIVE, 0, 0);
            if (IsWindow(hChildWnd))
                SendMessage(hChildWnd, WM_COMMAND, wParam, lParam);
            else
			    return DefFrameProc(hWnd, hMDIClient, message, wParam, lParam);
        }
    }
	return 0;
}

////////////////////////////////////////////////////////////////////////////////
//
//  FUNCTION: FrameWndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main frame window.
//
//  WM_COMMAND  - process the application menu
//  WM_DESTROY  - post a quit message and return
//
//

LRESULT CALLBACK FrameWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
        switch (message) {
        case WM_CREATE:
            {
            HMENU hMenuWindow = GetSubMenu(hMenuFrame, GetMenuItemCount(hMenuFrame)-2);
            CLIENTCREATESTRUCT ccs = { hMenuWindow, IDW_FIRST_CHILD };
#if 0
            hMDIClient = CreateWindow(_T("MDICLIENT"), NULL,
                    WS_CHILD|WS_CLIPCHILDREN|WS_VISIBLE,
                    0, 0, 0, 0,
                    hWnd, (HMENU)1, hInst, &ccs);
#else
            hMDIClient = CreateWindowEx(0, _T("MDICLIENT"), NULL,
//            hMDIClient = CreateWindowEx(0, (LPCTSTR)(int)hChildWndClass, NULL,
                    WS_CHILD|WS_CLIPCHILDREN|WS_VSCROLL|WS_HSCROLL|WS_VISIBLE|WS_BORDER,
                    0, 0, 0, 0,
                    hWnd, (HMENU)0, hInst, &ccs);
#endif
            }
		    break;
    	case WM_COMMAND:
	    	return _CmdWndProc(hWnd, message, wParam, lParam);
		    break;
        case WM_SIZE:
            resize_frame_client(hWnd);
		    break;
        case WM_ENTERMENULOOP:
            OnEnterMenuLoop(hWnd);
		    break;
        case WM_EXITMENULOOP:
            OnExitMenuLoop(hWnd);
		    break;
        case WM_MENUSELECT:
            OnMenuSelect(hWnd, LOWORD(wParam), HIWORD(wParam), (HMENU)lParam);
		    break;
        case WM_DESTROY:
            PostQuitMessage(0);
		    break;
        case WM_QUERYENDSESSION:
        case WM_CLOSE:
            SendMessage(hWnd, WM_COMMAND, ID_WINDOW_CLOSEALL, 0);
            if (GetWindow(hMDIClient, GW_CHILD) != NULL)
                return 0;
        // else fall thru...
        default:
            return DefFrameProc(hWnd, hMDIClient, message, wParam, lParam);
        }
    return 0;
}