dwrite: Implement GetWeight() for IDWriteFont.
[wine] / programs / regedit / childwnd.c
1 /*
2  * Regedit child window
3  *
4  * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * 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 #define WIN32_LEAN_AND_MEAN     /* Exclude rarely-used stuff from Windows headers */
22 #include <windows.h>
23 #include <commctrl.h>
24 #include <stdio.h>
25
26 #include "main.h"
27 #include "regproc.h"
28
29 #include "wine/debug.h"
30 #include "wine/unicode.h"
31                                                                                                                              
32 WINE_DEFAULT_DEBUG_CHANNEL(regedit);
33
34 ChildWnd* g_pChildWnd;
35 static int last_split;
36
37 static const WCHAR wszLastKey[] = {'L','a','s','t','K','e','y',0};
38 static const WCHAR wszKeyName[] = {'S','o','f','t','w','a','r','e','\\',
39                                    'M','i','c','r','o','s','o','f','t','\\',
40                                    'W','i','n','d','o','w','s','\\',
41                                    'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
42                                    'A','p','p','l','e','t','s','\\','R','e','g','e','d','i','t',0};
43
44 /*******************************************************************************
45  * Local module support methods
46  */
47
48 static LPCWSTR GetRootKeyName(HKEY hRootKey)
49 {
50     if(hRootKey == HKEY_CLASSES_ROOT)
51         return reg_class_namesW[INDEX_HKEY_CLASSES_ROOT];
52     if(hRootKey == HKEY_CURRENT_USER)
53         return reg_class_namesW[INDEX_HKEY_CURRENT_USER];
54     if(hRootKey == HKEY_LOCAL_MACHINE)
55         return reg_class_namesW[INDEX_HKEY_LOCAL_MACHINE];
56     if(hRootKey == HKEY_USERS)
57         return reg_class_namesW[INDEX_HKEY_USERS];
58     if(hRootKey == HKEY_CURRENT_CONFIG)
59         return reg_class_namesW[INDEX_HKEY_CURRENT_CONFIG];
60     if(hRootKey == HKEY_DYN_DATA)
61         return reg_class_namesW[INDEX_HKEY_DYN_DATA];
62     else
63     {
64         static const WCHAR unknown_key[] = {'U','N','K','N','O','W','N',' ','H','K','E','Y',',',' ',
65                                             'P','L','E','A','S','E',' ','R','E','P','O','R','T',0};
66         return unknown_key;
67     }
68 }
69
70 static void draw_splitbar(HWND hWnd, int x)
71 {
72     RECT rt;
73     HDC hdc = GetDC(hWnd);
74
75     GetClientRect(hWnd, &rt);
76     rt.left = x - SPLIT_WIDTH/2;
77     rt.right = x + SPLIT_WIDTH/2+1;
78     InvertRect(hdc, &rt);
79     ReleaseDC(hWnd, hdc);
80 }
81
82 static void ResizeWnd(int cx, int cy)
83 {
84     HDWP hdwp = BeginDeferWindowPos(2);
85     RECT rt = {0, 0, cx, cy};
86
87     cx = g_pChildWnd->nSplitPos + SPLIT_WIDTH/2;
88     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);
89     DeferWindowPos(hdwp, g_pChildWnd->hListWnd, 0, rt.left+cx  , rt.top, rt.right-cx, rt.bottom-rt.top, SWP_NOZORDER|SWP_NOACTIVATE);
90     EndDeferWindowPos(hdwp);
91 }
92
93 static void OnPaint(HWND hWnd)
94 {
95     PAINTSTRUCT ps;
96     RECT rt;
97
98     GetClientRect(hWnd, &rt);
99     BeginPaint(hWnd, &ps);
100     FillRect(ps.hdc, &rt, GetSysColorBrush(COLOR_BTNFACE));
101     EndPaint(hWnd, &ps);
102 }
103
104 static LPWSTR CombinePaths(LPCWSTR pPaths[], int nPaths) {
105     int i, len, pos;
106     LPWSTR combined;
107     for (i=0, len=0; i<nPaths; i++) {
108         if (pPaths[i] && *pPaths[i]) {
109             len += lstrlenW(pPaths[i])+1;
110         }
111     }
112     combined = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
113     *combined = '\0';
114     for (i=0, pos=0; i<nPaths; i++) {
115         if (pPaths[i] && *pPaths[i]) {
116             int llen = lstrlenW(pPaths[i]);
117             if (!*combined)
118                 lstrcpyW(combined, pPaths[i]);
119             else {
120                 combined[pos++] = '\\';
121                 lstrcpyW(combined+pos, pPaths[i]);
122             }
123             pos += llen;
124         }
125     }
126     return combined;
127 }
128
129 static LPWSTR GetPathRoot(HWND hwndTV, HTREEITEM hItem, BOOL bFull) {
130     LPCWSTR parts[2] = {0,0};
131     WCHAR text[260];
132     HKEY hRootKey = NULL;
133     if (!hItem)
134         hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CARET, 0);
135     HeapFree(GetProcessHeap(), 0, GetItemPath(hwndTV, hItem, &hRootKey));
136     if (!bFull && !hRootKey)
137         return NULL;
138     if (hRootKey)
139         parts[1] = GetRootKeyName(hRootKey);
140     if (bFull) {
141         DWORD dwSize = sizeof(text)/sizeof(WCHAR);
142         GetComputerNameW(text, &dwSize);
143         parts[0] = text;
144     }
145     return CombinePaths(parts, 2);
146 }
147
148 LPWSTR GetItemFullPath(HWND hwndTV, HTREEITEM hItem, BOOL bFull) {
149     LPWSTR parts[2];
150     LPWSTR ret;
151     HKEY hRootKey = NULL;
152
153     parts[0] = GetPathRoot(hwndTV, hItem, bFull);
154     parts[1] = GetItemPath(hwndTV, hItem, &hRootKey);
155     ret = CombinePaths((LPCWSTR *)parts, 2);
156     HeapFree(GetProcessHeap(), 0, parts[0]);
157     HeapFree(GetProcessHeap(), 0, parts[1]);
158     return ret;
159 }
160
161 static LPWSTR GetPathFullPath(HWND hwndTV, LPWSTR path) {
162     LPWSTR parts[2];
163     LPWSTR ret;
164
165     parts[0] = GetPathRoot(hwndTV, 0, TRUE);
166     parts[1] = path;
167     ret = CombinePaths((LPCWSTR*)parts, 2);
168     HeapFree(GetProcessHeap(), 0, parts[0]);
169     return ret;
170 }
171
172 static void OnTreeSelectionChanged(HWND hwndTV, HWND hwndLV, HTREEITEM hItem, BOOL bRefreshLV)
173 {
174     if (bRefreshLV) {
175         LPWSTR keyPath;
176         HKEY hRootKey = NULL;
177         keyPath = GetItemPath(hwndTV, hItem, &hRootKey);
178         RefreshListView(hwndLV, hRootKey, keyPath, NULL);
179         HeapFree(GetProcessHeap(), 0, keyPath);
180     }
181     UpdateStatusBar();
182 }
183
184 /*******************************************************************************
185  * finish_splitbar [internal]
186  *
187  * make the splitbar invisible and resize the windows
188  * (helper for ChildWndProc)
189  */
190 static void finish_splitbar(HWND hWnd, int x)
191 {
192     RECT rt;
193
194     draw_splitbar(hWnd, last_split);
195     last_split = -1;
196     GetClientRect(hWnd, &rt);
197     g_pChildWnd->nSplitPos = x;
198     ResizeWnd(rt.right, rt.bottom);
199     ReleaseCapture();
200 }
201
202 /*******************************************************************************
203  *
204  *  FUNCTION: _CmdWndProc(HWND, unsigned, WORD, LONG)
205  *
206  *  PURPOSE:  Processes WM_COMMAND messages for the main frame window.
207  *
208  */
209
210 static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
211 {
212     switch (LOWORD(wParam)) {
213         /* Parse the menu selections: */
214     case ID_REGISTRY_EXIT:
215         DestroyWindow(hWnd);
216         break;
217     case ID_VIEW_REFRESH:
218         WINE_TRACE("Is this ever called or is it just dead code?\n");
219         /* TODO */
220         break;
221     case ID_SWITCH_PANELS:
222         g_pChildWnd->nFocusPanel = !g_pChildWnd->nFocusPanel;
223         SetFocus(g_pChildWnd->nFocusPanel? g_pChildWnd->hListWnd: g_pChildWnd->hTreeWnd);
224         break;
225     default:
226         return FALSE;
227     }
228     return TRUE;
229 }
230
231 /*******************************************************************************
232  * get_last_key [internal]
233  *
234  * open last key
235  *
236  */
237 static void get_last_key(HWND hwndTV)
238 {
239     HKEY hkey;
240     WCHAR wszVal[KEY_MAX_LEN];
241     DWORD dwSize = sizeof(wszVal);
242
243     if (RegCreateKeyExW(HKEY_CURRENT_USER, wszKeyName, 0, NULL, 0, KEY_READ, NULL, &hkey, NULL) == ERROR_SUCCESS)
244     {
245         if (RegQueryValueExW(hkey, wszLastKey, NULL, NULL, (LPBYTE)wszVal, &dwSize) == ERROR_SUCCESS)
246             SendMessageW(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)FindPathInTree(hwndTV, wszVal));
247
248         RegCloseKey(hkey);
249     }
250 }
251
252 /*******************************************************************************
253  * set_last_key [internal]
254  *
255  * save last key
256  *
257  */
258 static void set_last_key(HWND hwndTV)
259 {
260     HKEY hkey;
261     WCHAR *wszVal;
262
263     if (RegCreateKeyExW(HKEY_CURRENT_USER, wszKeyName, 0, NULL, 0, KEY_WRITE, NULL, &hkey, NULL) == ERROR_SUCCESS)
264     {
265         HTREEITEM selection = (HTREEITEM)SendMessageW(g_pChildWnd->hTreeWnd, TVM_GETNEXTITEM, TVGN_CARET, 0);
266
267         wszVal = GetItemFullPath(g_pChildWnd->hTreeWnd, selection, FALSE);
268         RegSetValueExW(hkey, wszLastKey, 0, REG_SZ, (LPBYTE)wszVal, (lstrlenW(wszVal) + 1) * sizeof(WCHAR));
269         HeapFree(GetProcessHeap(), 0, wszVal);
270         RegCloseKey(hkey);
271     }
272 }
273
274 /*******************************************************************************
275  *
276  *  FUNCTION: ChildWndProc(HWND, unsigned, WORD, LONG)
277  *
278  *  PURPOSE:  Processes messages for the child windows.
279  *
280  *  WM_COMMAND  - process the application menu
281  *  WM_PAINT    - Paint the main window
282  *  WM_DESTROY  - post a quit message and return
283  *
284  */
285 LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
286 {
287     switch (message) {
288     case WM_CREATE:
289         g_pChildWnd = HeapAlloc(GetProcessHeap(), 0, sizeof(ChildWnd));
290         if (!g_pChildWnd) return 0;
291         LoadStringW(hInst, IDS_REGISTRY_ROOT_NAME, g_pChildWnd->szPath, MAX_PATH);
292         g_pChildWnd->nSplitPos = 250;
293         g_pChildWnd->hWnd = hWnd;
294         g_pChildWnd->hTreeWnd = CreateTreeView(hWnd, g_pChildWnd->szPath, TREE_WINDOW);
295         g_pChildWnd->hListWnd = CreateListView(hWnd, LIST_WINDOW/*, g_pChildWnd->szPath*/);
296         g_pChildWnd->nFocusPanel = 1;
297         SetFocus(g_pChildWnd->hTreeWnd);
298         get_last_key(g_pChildWnd->hTreeWnd);
299         break;
300     case WM_COMMAND:
301         if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
302             goto def;
303         }
304         break;
305     case WM_PAINT:
306         OnPaint(hWnd);
307         return 0;
308     case WM_SETCURSOR:
309         if (LOWORD(lParam) == HTCLIENT) {
310             POINT pt;
311             GetCursorPos(&pt);
312             ScreenToClient(hWnd, &pt);
313             if (pt.x>=g_pChildWnd->nSplitPos-SPLIT_WIDTH/2 && pt.x<g_pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
314                 SetCursor(LoadCursorW(0, (LPCWSTR)IDC_SIZEWE));
315                 return TRUE;
316             }
317         }
318         goto def;
319     case WM_DESTROY:
320         set_last_key(g_pChildWnd->hTreeWnd);
321         HeapFree(GetProcessHeap(), 0, g_pChildWnd);
322         g_pChildWnd = NULL;
323         PostQuitMessage(0);
324         break;
325     case WM_LBUTTONDOWN: {
326             RECT rt;
327             int x = (short)LOWORD(lParam);
328             GetClientRect(hWnd, &rt);
329             if (x>=g_pChildWnd->nSplitPos-SPLIT_WIDTH/2 && x<g_pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
330                 last_split = g_pChildWnd->nSplitPos;
331                 draw_splitbar(hWnd, last_split);
332                 SetCapture(hWnd);
333             }
334             break;
335         }
336
337     /* WM_RBUTTONDOWN sets the splitbar the same way as WM_LBUTTONUP */
338     case WM_LBUTTONUP:
339     case WM_RBUTTONDOWN:
340         if (GetCapture() == hWnd) {
341             finish_splitbar(hWnd, LOWORD(lParam));
342         }
343         break;
344
345     case WM_CAPTURECHANGED:
346         if (GetCapture()==hWnd && last_split>=0)
347             draw_splitbar(hWnd, last_split);
348         break;
349
350     case WM_KEYDOWN:
351         if (wParam == VK_ESCAPE)
352             if (GetCapture() == hWnd) {
353                 RECT rt;
354                 draw_splitbar(hWnd, last_split);
355                 GetClientRect(hWnd, &rt);
356                 ResizeWnd(rt.right, rt.bottom);
357                 last_split = -1;
358                 ReleaseCapture();
359                 SetCursor(LoadCursorW(0, (LPCWSTR)IDC_ARROW));
360             }
361         break;
362
363     case WM_MOUSEMOVE:
364         if (GetCapture() == hWnd) {
365             RECT rt;
366             int x = LOWORD(lParam);
367             HDC hdc = GetDC(hWnd);
368             GetClientRect(hWnd, &rt);
369             rt.left = last_split-SPLIT_WIDTH/2;
370             rt.right = last_split+SPLIT_WIDTH/2+1;
371             InvertRect(hdc, &rt);
372             last_split = x;
373             rt.left = x-SPLIT_WIDTH/2;
374             rt.right = x+SPLIT_WIDTH/2+1;
375             InvertRect(hdc, &rt);
376             ReleaseDC(hWnd, hdc);
377         }
378         break;
379
380     case WM_SETFOCUS:
381         if (g_pChildWnd != NULL) {
382             SetFocus(g_pChildWnd->nFocusPanel? g_pChildWnd->hListWnd: g_pChildWnd->hTreeWnd);
383         }
384         break;
385
386     case WM_TIMER:
387         break;
388
389     case WM_NOTIFY:
390         if (((int)wParam == TREE_WINDOW) && (g_pChildWnd != NULL)) {
391             switch (((LPNMHDR)lParam)->code) {
392             case TVN_ITEMEXPANDINGW:
393                 return !OnTreeExpanding(g_pChildWnd->hTreeWnd, (NMTREEVIEWW*)lParam);
394             case TVN_SELCHANGEDW:
395                 OnTreeSelectionChanged(g_pChildWnd->hTreeWnd, g_pChildWnd->hListWnd,
396                     ((NMTREEVIEWW *)lParam)->itemNew.hItem, TRUE);
397                 break;
398             case NM_SETFOCUS:
399                 g_pChildWnd->nFocusPanel = 0;
400                 break;
401             case NM_RCLICK: {
402                 POINT pt;
403                 GetCursorPos(&pt);
404                 TrackPopupMenu(GetSubMenu(hPopupMenus, PM_NEW),
405                                TPM_RIGHTBUTTON, pt.x, pt.y, 0, hFrameWnd, NULL);
406                 break;
407             }
408             case TVN_ENDLABELEDITW: {
409                 HKEY hRootKey;
410                 LPNMTVDISPINFOW dispInfo = (LPNMTVDISPINFOW)lParam;
411                 LPWSTR path = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
412                 BOOL res = RenameKey(hWnd, hRootKey, path, dispInfo->item.pszText);
413                 if (res) {
414                     TVITEMEXW item;
415                     LPWSTR fullPath = GetPathFullPath(g_pChildWnd->hTreeWnd,
416                      dispInfo->item.pszText);
417                     item.mask = TVIF_HANDLE | TVIF_TEXT;
418                     item.hItem = (HTREEITEM)SendMessageW(g_pChildWnd->hTreeWnd, TVM_GETNEXTITEM, TVGN_CARET, 0);
419                     item.pszText = dispInfo->item.pszText;
420                     SendMessageW( g_pChildWnd->hTreeWnd, TVM_SETITEMW, 0, (LPARAM)&item );
421                     SendMessageW(hStatusBar, SB_SETTEXTW, 0, (LPARAM)fullPath);
422                     HeapFree(GetProcessHeap(), 0, fullPath);
423                 }
424                 HeapFree(GetProcessHeap(), 0, path);
425                 return res;
426             }
427             default:
428                 return 0; /* goto def; */
429             }
430         } else
431             if (((int)wParam == LIST_WINDOW) && (g_pChildWnd != NULL)) {
432                 if (((LPNMHDR)lParam)->code == NM_SETFOCUS) {
433                     g_pChildWnd->nFocusPanel = 1;
434                 } else if (!SendMessageW(g_pChildWnd->hListWnd, WM_NOTIFY_REFLECT, wParam, lParam)) {
435                     goto def;
436                 }
437             }
438         break;
439
440     case WM_SIZE:
441         if (wParam != SIZE_MINIMIZED && g_pChildWnd != NULL) {
442             ResizeWnd(LOWORD(lParam), HIWORD(lParam));
443         }
444         /* fall through */
445 default: def:
446         return DefWindowProcW(hWnd, message, wParam, lParam);
447     }
448     return 0;
449 }