[REGEDIT
[reactos.git] / reactos / 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 TCHAR g_szGeneralRegKey[] = _T("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 WINDOWPLACEMENT tPlacement;
52 int TreeViewSize;
53 int NameColumnSize;
54 int TypeColumnSize;
55 int DataColumnSize;
56 BOOL StatusBarVisible;
57 } RegistryBinaryConfig;
58
59 extern void LoadSettings(void)
60 {
61 HKEY hKey = NULL;
62 TCHAR szBuffer[MAX_PATH];
63
64 if (RegOpenKey(HKEY_CURRENT_USER, g_szGeneralRegKey, &hKey) == ERROR_SUCCESS)
65 {
66 RegistryBinaryConfig tConfig;
67 DWORD iBufferSize = sizeof(tConfig);
68
69 if (RegQueryValueEx(hKey, L"View", NULL, NULL, (LPBYTE)&tConfig, &iBufferSize) == ERROR_SUCCESS)
70 {
71 if ( iBufferSize == sizeof(tConfig) )
72 {
73 RECT rcTemp;
74
75 /* Update status bar settings */
76 CheckMenuItem(GetSubMenu(hMenuFrame, ID_VIEW_MENU), ID_VIEW_STATUSBAR, MF_BYCOMMAND|(tConfig.StatusBarVisible ? MF_CHECKED : MF_UNCHECKED));
77 ShowWindow(hStatusBar, (tConfig.StatusBarVisible ? SW_SHOW : SW_HIDE));
78
79 /* Update listview column width */
80 (void)ListView_SetColumnWidth(g_pChildWnd->hListWnd, 0, tConfig.NameColumnSize);
81 (void)ListView_SetColumnWidth(g_pChildWnd->hListWnd, 1, tConfig.TypeColumnSize);
82 (void)ListView_SetColumnWidth(g_pChildWnd->hListWnd, 2, tConfig.DataColumnSize);
83
84 /* Update treeview (splitter) */
85 GetClientRect(hFrameWnd, &rcTemp);
86 g_pChildWnd->nSplitPos = tConfig.TreeViewSize;
87 ResizeWnd(rcTemp.right, rcTemp.bottom);
88
89 /* Apply program window settings */
90 tConfig.tPlacement.length = sizeof(WINDOWPLACEMENT);
91 if (SetWindowPlacement(hFrameWnd, &tConfig.tPlacement) == FALSE)
92 /* In case we fail, show normal */
93 ShowWindow(hFrameWnd, SW_SHOWNORMAL);
94 }
95 }
96
97 /* Restore key position */
98 if (QueryStringValue(HKEY_CURRENT_USER, g_szGeneralRegKey, _T("LastKey"), szBuffer, COUNT_OF(szBuffer)) == ERROR_SUCCESS)
99 {
100 SelectNode(g_pChildWnd->hTreeWnd, szBuffer);
101 }
102
103 RegCloseKey(hKey);
104 }
105 else
106 {
107 /* Failed to open key, show normal */
108 ShowWindow(hFrameWnd, SW_SHOWNORMAL);
109 }
110 }
111
112 extern void SaveSettings(void)
113 {
114 HKEY hKey = NULL;
115
116 if (RegCreateKey(HKEY_CURRENT_USER, g_szGeneralRegKey, &hKey) == ERROR_SUCCESS)
117 {
118 if (RegOpenKey(HKEY_CURRENT_USER, g_szGeneralRegKey, &hKey) == ERROR_SUCCESS)
119 {
120 RegistryBinaryConfig tConfig;
121 DWORD iBufferSize = sizeof(tConfig);
122 TCHAR szBuffer[MAX_PATH];
123 LPCTSTR keyPath, rootName;
124 HKEY hRootKey;
125
126 /* Save key position */
127 keyPath = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
128 if (keyPath)
129 {
130 rootName = get_root_key_name(hRootKey);
131 _sntprintf(szBuffer, COUNT_OF(szBuffer), _T("My Computer\\%s\\%s"), rootName, keyPath);
132 RegSetValueEx(hKey, _T("LastKey"), 0, REG_SZ, (LPBYTE) szBuffer, (DWORD) _tcslen(szBuffer) * sizeof(szBuffer[0]));
133 }
134
135 /* Get statusbar settings */
136 tConfig.StatusBarVisible = ((GetMenuState(GetSubMenu(hMenuFrame, ID_VIEW_MENU), ID_VIEW_STATUSBAR, MF_BYCOMMAND) & MF_CHECKED) ? 1 : 0 );
137
138 /* Get splitter position */
139 tConfig.TreeViewSize = g_pChildWnd->nSplitPos;
140
141 /* Get list view column width*/
142 tConfig.NameColumnSize = ListView_GetColumnWidth(g_pChildWnd->hListWnd, 0);
143 tConfig.TypeColumnSize = ListView_GetColumnWidth(g_pChildWnd->hListWnd, 1);
144 tConfig.DataColumnSize = ListView_GetColumnWidth(g_pChildWnd->hListWnd, 2);
145
146 /* Get program window settings */
147 tConfig.tPlacement.length = sizeof(WINDOWPLACEMENT);
148 GetWindowPlacement(hFrameWnd , &tConfig.tPlacement);
149
150 /* Save all the data */
151 RegSetValueEx(hKey, L"View", 0, REG_BINARY, (LPBYTE)&tConfig, iBufferSize);
152
153 RegCloseKey(hKey);
154 }
155 }
156 }