Sunc with trunk revision 58971.
[reactos.git] / base / applications / regedit / settings.c
1 /*
2 * Regedit settings
3 *
4 * Copyright (C) 2012 Edijs Kolesnikovics <terminedijs@yahoo.com>
5 * Copyright (C) 2012 Grégori Macário Harbs <mysoft64bits at gmail dot com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "regedit.h"
23
24 const WCHAR g_szGeneralRegKey[] = L"Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Regedit";
25
26 /*
27 VV,VV,VV,VV,WA,WA,WA,WA,WB,WB,WB,WB,R1,R1,R1,R1
28 R2,R2,R2,R2,R3,R3,R3,R3,R4,R4,R4,r4,LL,LL,LL,LL
29 TT,TT,TT,TT,RR,RR,RR,RR,BB,BB,BB,BB,SS,SS,SS,SS
30 NN,NN,NN,NN,KK,KK,KK,KK,DD,DD,DD,DD,SB,SB,SB,SB
31
32 VV = Version or Sanity? WINDOWPLACEMENT? (2C?)
33 WA = (0=restored / 1=maximized)
34 WB = (1=restored / 3=maximized)
35 R1 = ???? \
36 R2 = ???? | either those are reserved unused or they will
37 R3 = ???? | have IP/INFO if connected to remote registry
38 R4 = ???? /
39 LL = Left position of window
40 TT = top position of window
41 RR = right position of window
42 BB = bottom position of window
43 SS = size of key tree view (splitter)
44 NN = size of 'name' column
45 KK = size of 'type' column (kind)
46 DD = size of 'data' coumn
47 SB = status bar (1=visible / 0=hidden)
48 */
49
50 typedef struct
51 {
52 WINDOWPLACEMENT tPlacement;
53 int TreeViewSize;
54 int NameColumnSize;
55 int TypeColumnSize;
56 int DataColumnSize;
57 BOOL StatusBarVisible;
58 } RegistryBinaryConfig;
59
60 extern void LoadSettings(void)
61 {
62 HKEY hKey = NULL;
63 WCHAR szBuffer[MAX_PATH];
64
65 if (RegOpenKeyW(HKEY_CURRENT_USER, g_szGeneralRegKey, &hKey) == ERROR_SUCCESS)
66 {
67 RegistryBinaryConfig tConfig;
68 DWORD iBufferSize = sizeof(tConfig);
69
70 if (RegQueryValueExW(hKey, L"View", NULL, NULL, (LPBYTE)&tConfig, &iBufferSize) == ERROR_SUCCESS)
71 {
72 if (iBufferSize == sizeof(tConfig))
73 {
74 RECT rcTemp;
75
76 /* Update status bar settings */
77 CheckMenuItem(GetSubMenu(hMenuFrame, ID_VIEW_MENU), ID_VIEW_STATUSBAR, MF_BYCOMMAND | (tConfig.StatusBarVisible ? MF_CHECKED : MF_UNCHECKED));
78 ShowWindow(hStatusBar, (tConfig.StatusBarVisible ? SW_SHOW : SW_HIDE));
79
80 /* Update listview column width */
81 (void)ListView_SetColumnWidth(g_pChildWnd->hListWnd, 0, tConfig.NameColumnSize);
82 (void)ListView_SetColumnWidth(g_pChildWnd->hListWnd, 1, tConfig.TypeColumnSize);
83 (void)ListView_SetColumnWidth(g_pChildWnd->hListWnd, 2, tConfig.DataColumnSize);
84
85 /* Update treeview (splitter) */
86 GetClientRect(hFrameWnd, &rcTemp);
87 g_pChildWnd->nSplitPos = tConfig.TreeViewSize;
88 ResizeWnd(rcTemp.right, rcTemp.bottom);
89
90 /* Apply program window settings */
91 tConfig.tPlacement.length = sizeof(WINDOWPLACEMENT);
92 if (SetWindowPlacement(hFrameWnd, &tConfig.tPlacement) == FALSE)
93 /* In case we fail, show normal */
94 ShowWindow(hFrameWnd, SW_SHOWNORMAL);
95 }
96 }
97
98 /* Restore key position */
99 if (QueryStringValue(HKEY_CURRENT_USER, g_szGeneralRegKey, L"LastKey", szBuffer, COUNT_OF(szBuffer)) == ERROR_SUCCESS)
100 {
101 SelectNode(g_pChildWnd->hTreeWnd, szBuffer);
102 }
103
104 RegCloseKey(hKey);
105 }
106 else
107 {
108 /* Failed to open key, show normal */
109 ShowWindow(hFrameWnd, SW_SHOWNORMAL);
110 }
111 }
112
113 extern void SaveSettings(void)
114 {
115 HKEY hKey = NULL;
116
117 if (RegCreateKeyW(HKEY_CURRENT_USER, g_szGeneralRegKey, &hKey) == ERROR_SUCCESS)
118 {
119 RegistryBinaryConfig tConfig;
120 DWORD iBufferSize = sizeof(tConfig);
121 WCHAR szBuffer[MAX_PATH];
122 LPCWSTR keyPath, rootName;
123 HKEY hRootKey;
124
125 /* Save key position */
126 keyPath = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
127 if (keyPath)
128 {
129 rootName = get_root_key_name(hRootKey);
130
131 /* Load "My Computer" string and complete it */
132 LoadStringW(hInst, IDS_MY_COMPUTER, szBuffer, COUNT_OF(szBuffer));
133 wcscat(szBuffer, L"\\"); wcscat(szBuffer, rootName);
134 wcscat(szBuffer, L"\\"); wcscat(szBuffer, keyPath);
135
136 RegSetValueExW(hKey, L"LastKey", 0, REG_SZ, (LPBYTE)szBuffer, (DWORD)wcslen(szBuffer) * sizeof(WCHAR));
137 }
138
139 /* Get statusbar settings */
140 tConfig.StatusBarVisible = ((GetMenuState(GetSubMenu(hMenuFrame, ID_VIEW_MENU), ID_VIEW_STATUSBAR, MF_BYCOMMAND) & MF_CHECKED) ? 1 : 0);
141
142 /* Get splitter position */
143 tConfig.TreeViewSize = g_pChildWnd->nSplitPos;
144
145 /* Get list view column width*/
146 tConfig.NameColumnSize = ListView_GetColumnWidth(g_pChildWnd->hListWnd, 0);
147 tConfig.TypeColumnSize = ListView_GetColumnWidth(g_pChildWnd->hListWnd, 1);
148 tConfig.DataColumnSize = ListView_GetColumnWidth(g_pChildWnd->hListWnd, 2);
149
150 /* Get program window settings */
151 tConfig.tPlacement.length = sizeof(WINDOWPLACEMENT);
152 GetWindowPlacement(hFrameWnd , &tConfig.tPlacement);
153
154 /* Save all the data */
155 RegSetValueExW(hKey, L"View", 0, REG_BINARY, (LPBYTE)&tConfig, iBufferSize);
156
157 RegCloseKey(hKey);
158 }
159 }
160 /* EOF */