childwnd.c 11.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Regedit child window
 *
 * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20 21 22 23
 */

#define WIN32_LEAN_AND_MEAN     /* Exclude rarely-used stuff from Windows headers */
#include <windows.h>
#include <commctrl.h>
24 25
#include <tchar.h>
#include <stdio.h>
26 27 28

#include "main.h"

29 30 31 32 33
#include "wine/debug.h"
#include "wine/unicode.h"
                                                                                                                             
WINE_DEFAULT_DEBUG_CHANNEL(regedit);
                                                                                                                             
34
ChildWnd* g_pChildWnd;
35 36 37 38 39

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

40
LPCTSTR GetRootKeyName(HKEY hRootKey)
41
{
42 43 44 45 46
    if (hRootKey == HKEY_CLASSES_ROOT) return _T("HKEY_CLASSES_ROOT");
    if (hRootKey == HKEY_CURRENT_USER) return _T("HKEY_CURRENT_USER");
    if (hRootKey == HKEY_LOCAL_MACHINE) return _T("HKEY_LOCAL_MACHINE");
    if (hRootKey == HKEY_USERS) return _T("HKEY_USERS");
    if (hRootKey == HKEY_CURRENT_CONFIG) return _T("HKEY_CURRENT_CONFIG");
47
    if (hRootKey == HKEY_DYN_DATA) return _T("HKEY_DYN_DATA");
48
    return _T("UNKNOWN HKEY, PLEASE REPORT");
49 50 51 52
}

static void draw_splitbar(HWND hWnd, int x)
{
53 54
    RECT rt;
    HDC hdc = GetDC(hWnd);
55

56 57 58 59 60
    GetClientRect(hWnd, &rt);
    rt.left = x - SPLIT_WIDTH/2;
    rt.right = x + SPLIT_WIDTH/2+1;
    InvertRect(hdc, &rt);
    ReleaseDC(hWnd, hdc);
61 62
}

63
static void ResizeWnd(int cx, int cy)
64
{
65 66
    HDWP hdwp = BeginDeferWindowPos(2);
    RECT rt = {0, 0, cx, cy};
67

68 69 70
    cx = g_pChildWnd->nSplitPos + SPLIT_WIDTH/2;
    DeferWindowPos(hdwp, g_pChildWnd->hTreeWnd, 0, rt.left, rt.top, g_pChildWnd->nSplitPos-SPLIT_WIDTH/2-rt.left, rt.bottom-rt.top, SWP_NOZORDER|SWP_NOACTIVATE);
    DeferWindowPos(hdwp, g_pChildWnd->hListWnd, 0, rt.left+cx  , rt.top, rt.right-cx, rt.bottom-rt.top, SWP_NOZORDER|SWP_NOACTIVATE);
71
    EndDeferWindowPos(hdwp);
72 73 74 75 76 77 78 79 80 81
}

static void OnPaint(HWND hWnd)
{
    PAINTSTRUCT ps;
    RECT rt;
    HDC hdc;

    GetClientRect(hWnd, &rt);
    hdc = BeginPaint(hWnd, &ps);
82
    FillRect(ps.hdc, &rt, GetSysColorBrush(COLOR_BTNFACE));
83 84 85
    EndPaint(hWnd, &ps);
}

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
static LPTSTR CombinePaths(LPCTSTR pPaths[], int nPaths) {
    int i, len, pos;
    LPTSTR combined;
    for (i=0, len=0; i<nPaths; i++) {
        if (pPaths[i] && *pPaths[i]) {
            len += lstrlen(pPaths[i])+1;
        }
    }
    combined = HeapAlloc(GetProcessHeap(), 0, len * sizeof(TCHAR));
    *combined = '\0';
    for (i=0, pos=0; i<nPaths; i++) {
        if (pPaths[i] && *pPaths[i]) {
            int llen = _tcslen(pPaths[i]);
            if (!*combined)
                _tcscpy(combined, pPaths[i]);
            else {
                combined[pos++] = (TCHAR)'\\';
                _tcscpy(combined+pos, pPaths[i]);
            }
            pos += llen;
106 107
        }
    }
108 109 110
    return combined;
}

111 112
static LPTSTR GetPathRoot(HWND hwndTV, HTREEITEM hItem, BOOL bFull) {
    LPCTSTR parts[2] = {_T(""), _T("")};
113 114 115 116
    TCHAR text[260];
    HKEY hRootKey = NULL;
    if (!hItem)
        hItem = TreeView_GetSelection(hwndTV);
117 118
    GetItemPath(hwndTV, hItem, &hRootKey);
    if (!bFull && !hRootKey)
119 120 121 122 123 124 125 126
        return NULL;
    if (hRootKey)
        parts[1] = GetRootKeyName(hRootKey);
    if (bFull) {
        DWORD dwSize = sizeof(text)/sizeof(TCHAR);
        GetComputerName(text, &dwSize);
        parts[0] = text;
    }
127 128 129 130
    return CombinePaths(parts, 2);
}

LPTSTR GetItemFullPath(HWND hwndTV, HTREEITEM hItem, BOOL bFull) {
131
    LPTSTR parts[2];
132
    LPTSTR ret;
133 134
    HKEY hRootKey = NULL;

135 136 137 138 139 140 141 142
    parts[0] = GetPathRoot(hwndTV, hItem, bFull);
    parts[1] = GetItemPath(hwndTV, hItem, &hRootKey);
    ret = CombinePaths((LPCTSTR *)parts, 2);
    HeapFree(GetProcessHeap(), 0, parts[0]);
    return ret;
}

LPTSTR GetPathFullPath(HWND hwndTV, LPTSTR path) {
143 144 145
    LPTSTR parts[2];
    LPTSTR ret;

146 147 148 149 150
    parts[0] = GetPathRoot(hwndTV, 0, TRUE);
    parts[1] = path;
    ret = CombinePaths((LPCTSTR *)parts, 2);
    HeapFree(GetProcessHeap(), 0, parts[0]);
    return ret;
151 152 153 154 155 156 157 158 159
}

static void OnTreeSelectionChanged(HWND hwndTV, HWND hwndLV, HTREEITEM hItem, BOOL bRefreshLV)
{
    if (bRefreshLV) {
        LPCTSTR keyPath;
        HKEY hRootKey = NULL;
        keyPath = GetItemPath(hwndTV, hItem, &hRootKey);
        RefreshListView(hwndLV, hRootKey, keyPath, NULL);
160
    }
161
    UpdateStatusBar();
162 163
}

164 165 166 167 168 169 170 171 172 173
/*******************************************************************************
 *
 *  FUNCTION: _CmdWndProc(HWND, unsigned, WORD, LONG)
 *
 *  PURPOSE:  Processes WM_COMMAND messages for the main frame window.
 *
 */

static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
174 175
    switch (LOWORD(wParam)) {
        /* Parse the menu selections: */
176 177 178 179
    case ID_REGISTRY_EXIT:
        DestroyWindow(hWnd);
        break;
    case ID_VIEW_REFRESH:
Crestez Leonard's avatar
Crestez Leonard committed
180
        WINE_TRACE("Is this ever called or is it just dead code?\n");
181 182
        /* TODO */
        break;
183
    case ID_SWITCH_PANELS:
184 185
        g_pChildWnd->nFocusPanel = !g_pChildWnd->nFocusPanel;
        SetFocus(g_pChildWnd->nFocusPanel? g_pChildWnd->hListWnd: g_pChildWnd->hTreeWnd);
186
        break;
187 188 189
    default:
        return FALSE;
    }
190
    return TRUE;
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
}

/*******************************************************************************
 *
 *  FUNCTION: ChildWndProc(HWND, unsigned, WORD, LONG)
 *
 *  PURPOSE:  Processes messages for the child windows.
 *
 *  WM_COMMAND  - process the application menu
 *  WM_PAINT    - Paint the main window
 *  WM_DESTROY  - post a quit message and return
 *
 */
LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static int last_split;

    switch (message) {
    case WM_CREATE:
210 211 212 213 214 215 216 217 218
        g_pChildWnd = HeapAlloc(GetProcessHeap(), 0, sizeof(ChildWnd));
        if (!g_pChildWnd) return 0;
        LoadString(hInst, IDS_REGISTRY_ROOT_NAME, g_pChildWnd->szPath, MAX_PATH);
        g_pChildWnd->nSplitPos = 250;
        g_pChildWnd->hWnd = hWnd;
        g_pChildWnd->hTreeWnd = CreateTreeView(hWnd, g_pChildWnd->szPath, TREE_WINDOW);
        g_pChildWnd->hListWnd = CreateListView(hWnd, LIST_WINDOW/*, g_pChildWnd->szPath*/);
        g_pChildWnd->nFocusPanel = 1;
        SetFocus(g_pChildWnd->hTreeWnd);
219 220 221 222 223
        break;
    case WM_COMMAND:
        if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
            goto def;
        }
224
        break;
225 226 227
    case WM_PAINT:
        OnPaint(hWnd);
        return 0;
228 229 230 231 232
    case WM_SETCURSOR:
        if (LOWORD(lParam) == HTCLIENT) {
            POINT pt;
            GetCursorPos(&pt);
            ScreenToClient(hWnd, &pt);
233
            if (pt.x>=g_pChildWnd->nSplitPos-SPLIT_WIDTH/2 && pt.x<g_pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
234 235 236 237 238
                SetCursor(LoadCursor(0, IDC_SIZEWE));
                return TRUE;
            }
        }
        goto def;
239
    case WM_DESTROY:
240 241
        HeapFree(GetProcessHeap(), 0, g_pChildWnd);
        g_pChildWnd = NULL;
242 243
        PostQuitMessage(0);
        break;
244 245
    case WM_LBUTTONDOWN: {
            RECT rt;
246
            int x = (short)LOWORD(lParam);
247
            GetClientRect(hWnd, &rt);
248 249
            if (x>=g_pChildWnd->nSplitPos-SPLIT_WIDTH/2 && x<g_pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
                last_split = g_pChildWnd->nSplitPos;
250 251 252 253 254
                draw_splitbar(hWnd, last_split);
                SetCapture(hWnd);
            }
            break;
        }
255

256 257 258 259 260 261 262
    case WM_LBUTTONUP:
        if (GetCapture() == hWnd) {
            RECT rt;
            int x = LOWORD(lParam);
            draw_splitbar(hWnd, last_split);
            last_split = -1;
            GetClientRect(hWnd, &rt);
263
            g_pChildWnd->nSplitPos = x;
264
            ResizeWnd(rt.right, rt.bottom);
265 266 267
            ReleaseCapture();
        }
        break;
268

269 270 271 272
    case WM_CAPTURECHANGED:
        if (GetCapture()==hWnd && last_split>=0)
            draw_splitbar(hWnd, last_split);
        break;
273 274

    case WM_KEYDOWN:
275 276 277 278 279
        if (wParam == VK_ESCAPE)
            if (GetCapture() == hWnd) {
                RECT rt;
                draw_splitbar(hWnd, last_split);
                GetClientRect(hWnd, &rt);
280
                ResizeWnd(rt.right, rt.bottom);
281 282 283 284 285
                last_split = -1;
                ReleaseCapture();
                SetCursor(LoadCursor(0, IDC_ARROW));
            }
        break;
286

287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
    case WM_MOUSEMOVE:
        if (GetCapture() == hWnd) {
            RECT rt;
            int x = LOWORD(lParam);
            HDC hdc = GetDC(hWnd);
            GetClientRect(hWnd, &rt);
            rt.left = last_split-SPLIT_WIDTH/2;
            rt.right = last_split+SPLIT_WIDTH/2+1;
            InvertRect(hdc, &rt);
            last_split = x;
            rt.left = x-SPLIT_WIDTH/2;
            rt.right = x+SPLIT_WIDTH/2+1;
            InvertRect(hdc, &rt);
            ReleaseDC(hWnd, hdc);
        }
        break;
303

304
    case WM_SETFOCUS:
305 306
        if (g_pChildWnd != NULL) {
            SetFocus(g_pChildWnd->nFocusPanel? g_pChildWnd->hListWnd: g_pChildWnd->hTreeWnd);
307
        }
308
        break;
309 310 311 312

    case WM_TIMER:
        break;

313
    case WM_NOTIFY:
314
        if (((int)wParam == TREE_WINDOW) && (g_pChildWnd != NULL)) {
315 316
            switch (((LPNMHDR)lParam)->code) {
            case TVN_ITEMEXPANDING:
317
                return !OnTreeExpanding(g_pChildWnd->hTreeWnd, (NMTREEVIEW*)lParam);
318
            case TVN_SELCHANGED:
319
                OnTreeSelectionChanged(g_pChildWnd->hTreeWnd, g_pChildWnd->hListWnd,
320
                    ((NMTREEVIEW *)lParam)->itemNew.hItem, TRUE);
321
                break;
322
	    case NM_SETFOCUS:
323
		g_pChildWnd->nFocusPanel = 0;
324
		break;
325 326 327 328 329 330 331
            case NM_RCLICK: {
		POINT pt;
                GetCursorPos(&pt);
		TrackPopupMenu(GetSubMenu(hPopupMenus, PM_NEW),
			       TPM_RIGHTBUTTON, pt.x, pt.y, 0, hFrameWnd, NULL);
		break;
            }
332 333 334
	    case TVN_ENDLABELEDIT: {
		HKEY hRootKey;
	        LPNMTVDISPINFO dispInfo = (LPNMTVDISPINFO)lParam;
335
		LPCTSTR path = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
336 337 338
	        BOOL res = RenameKey(hWnd, hRootKey, path, dispInfo->item.pszText);
		if (res) {
		    TVITEMEX item;
339
                    LPTSTR fullPath = GetPathFullPath(g_pChildWnd->hTreeWnd,
340
                     dispInfo->item.pszText);
341
		    item.mask = TVIF_HANDLE | TVIF_TEXT;
342
		    item.hItem = TreeView_GetSelection(g_pChildWnd->hTreeWnd);
343
		    item.pszText = dispInfo->item.pszText;
344
                    SendMessage( g_pChildWnd->hTreeWnd, TVM_SETITEMW, 0, (LPARAM)&item );
345 346
                    SendMessage(hStatusBar, SB_SETTEXT, 0, (LPARAM)fullPath);
                    HeapFree(GetProcessHeap(), 0, fullPath);
347 348 349
		}
		return res;
	    }
350
            default:
351
                return 0; /* goto def; */
352 353
            }
        } else
354
            if (((int)wParam == LIST_WINDOW) && (g_pChildWnd != NULL)) {
355
		if (((LPNMHDR)lParam)->code == NM_SETFOCUS) {
356 357
		    g_pChildWnd->nFocusPanel = 1;
		} else if (!SendMessage(g_pChildWnd->hListWnd, WM_NOTIFY_REFLECT, wParam, lParam)) {
358 359
                    goto def;
                }
360 361 362
            }
        break;

363
    case WM_SIZE:
364
        if (wParam != SIZE_MINIMIZED && g_pChildWnd != NULL) {
365
            ResizeWnd(LOWORD(lParam), HIWORD(lParam));
366 367
        }
        /* fall through */
368
default: def:
369
        return DefWindowProc(hWnd, message, wParam, lParam);
370 371
    }
    return 0;
372
}