[MSPAINT]
[reactos.git] / reactos / base / applications / mspaint / registry.cpp
1 /*
2 * PROJECT: PAINT for ReactOS
3 * LICENSE: LGPL
4 * FILE: base/applications/mspaint/registry.cpp
5 * PURPOSE: Offering functions dealing with registry values
6 * PROGRAMMERS: Benedikt Freisen
7 */
8
9 /* INCLUDES *********************************************************/
10
11 #include "precomp.h"
12
13 #include <winreg.h>
14
15 /* FUNCTIONS ********************************************************/
16 static DWORD ReadDWORD(HKEY hKey, LPCTSTR lpName, DWORD *pdwValue, BOOL bCheckForDef)
17 {
18 DWORD dwPrev = *pdwValue;
19 DWORD cbData = sizeof(DWORD);
20
21 if (ERROR_SUCCESS != RegQueryValueEx(hKey, lpName, 0, NULL, (LPBYTE) pdwValue, &cbData))
22 *pdwValue = dwPrev;
23
24 if (bCheckForDef && *pdwValue == 0)
25 *pdwValue = dwPrev;
26
27 return dwPrev;
28 }
29
30 void RegistrySettings::SetWallpaper(TCHAR * FileName, DWORD dwStyle, DWORD dwTile) //FIXME: Has to be called 2x to apply the pattern (tiled/stretched) too
31 {
32 HKEY hDesktop;
33 TCHAR szStyle[3], szTile[3];
34
35 SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (PVOID) FileName, SPIF_UPDATEINIFILE);
36
37 if ((dwStyle > 2) || (dwTile > 2))
38 return;
39
40 if (RegOpenKeyEx(HKEY_CURRENT_USER,
41 _T("Control Panel\\Desktop"), 0, KEY_READ | KEY_SET_VALUE, &hDesktop) == ERROR_SUCCESS)
42 {
43 RegSetValueEx(hDesktop, _T("Wallpaper"), 0, REG_SZ, (LPBYTE) FileName,
44 _tcslen(FileName) * sizeof(TCHAR));
45
46 _stprintf(szStyle, _T("%lu"), dwStyle);
47 _stprintf(szTile, _T("%lu"), dwTile);
48
49 RegSetValueEx(hDesktop, _T("WallpaperStyle"), 0, REG_SZ, (LPBYTE) szStyle,
50 _tcslen(szStyle) * sizeof(TCHAR));
51 RegSetValueEx(hDesktop, _T("TileWallpaper"), 0, REG_SZ, (LPBYTE) szTile,
52 _tcslen(szTile) * sizeof(TCHAR));
53
54 RegCloseKey(hDesktop);
55 }
56 }
57
58 void RegistrySettings::LoadPresets()
59 {
60 BMPHeight = 300;
61 BMPWidth = 400;
62 GridExtent = 1;
63 NoStretching = 0;
64 ShowThumbnail = 0;
65 SnapToGrid = 0;
66 ThumbHeight = 100;
67 ThumbWidth = 120;
68 ThumbXPos = 180;
69 ThumbYPos = 200;
70 UnitSetting = 0;
71 const WINDOWPLACEMENT DefaultWindowPlacement = {
72 sizeof(WINDOWPLACEMENT),
73 0,
74 SW_SHOWNORMAL,
75 {0, 0},
76 {-1, -1},
77 {100, 100, 700, 550}
78 };
79 WindowPlacement = DefaultWindowPlacement;
80 }
81
82 void RegistrySettings::Load()
83 {
84 HKEY hView;
85 LoadPresets();
86 if (RegOpenKeyEx(HKEY_CURRENT_USER,
87 _T("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Paint\\View"),
88 0, KEY_READ | KEY_SET_VALUE, &hView) == ERROR_SUCCESS)
89 {
90 DWORD cbData;
91
92 ReadDWORD(hView, _T("BMPHeight"), &BMPHeight, TRUE);
93 ReadDWORD(hView, _T("BMPWidth"), &BMPWidth, TRUE);
94 ReadDWORD(hView, _T("GridExtent"), &GridExtent, FALSE);
95 ReadDWORD(hView, _T("NoStretching"), &NoStretching, FALSE);
96 ReadDWORD(hView, _T("ShowThumbnail"), &ShowThumbnail, FALSE);
97 ReadDWORD(hView, _T("SnapToGrid"), &SnapToGrid, FALSE);
98 ReadDWORD(hView, _T("ThumbHeight"), &ThumbHeight, TRUE);
99 ReadDWORD(hView, _T("ThumbWidth"), &ThumbWidth, TRUE);
100 ReadDWORD(hView, _T("ThumbXPos"), &ThumbXPos, TRUE);
101 ReadDWORD(hView, _T("ThumbYPos"), &ThumbYPos, TRUE);
102 ReadDWORD(hView, _T("UnitSetting"), &UnitSetting, FALSE);
103
104 cbData = sizeof(WINDOWPLACEMENT);
105 RegQueryValueEx(hView, _T("WindowPlacement"), 0, NULL, (LPBYTE) &WindowPlacement, &cbData);
106 }
107
108 CRegKey hKey;
109 if (hKey.Open(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Paint\\Recent File List"), KEY_READ) == ERROR_SUCCESS)
110 {
111 ULONG nChars = MAX_PATH;
112 LPTSTR pszFile1 = strFile1.GetBuffer(nChars);
113 hKey.QueryStringValue(_T("File1"), pszFile1, &nChars);
114 strFile1.ReleaseBuffer();
115 nChars = MAX_PATH;
116 LPTSTR pszFile2 = strFile2.GetBuffer(nChars);
117 hKey.QueryStringValue(_T("File2"), pszFile2, &nChars);
118 strFile2.ReleaseBuffer();
119 nChars = MAX_PATH;
120 LPTSTR pszFile3 = strFile3.GetBuffer(nChars);
121 hKey.QueryStringValue(_T("File3"), pszFile3, &nChars);
122 strFile3.ReleaseBuffer();
123 nChars = MAX_PATH;
124 LPTSTR pszFile4 = strFile4.GetBuffer(nChars);
125 hKey.QueryStringValue(_T("File4"), pszFile4, &nChars);
126 strFile4.ReleaseBuffer();
127 }
128 }
129
130 void RegistrySettings::Store()
131 {
132 HKEY hView;
133 if (RegCreateKeyEx(HKEY_CURRENT_USER,
134 _T("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Paint\\View"),
135 0, NULL, 0, KEY_READ | KEY_SET_VALUE, NULL, &hView, NULL) == ERROR_SUCCESS)
136 {
137 RegSetValueEx(hView, _T("BMPHeight"), 0, REG_DWORD, (LPBYTE) &BMPHeight, sizeof(DWORD));
138 RegSetValueEx(hView, _T("BMPWidth"), 0, REG_DWORD, (LPBYTE) &BMPWidth, sizeof(DWORD));
139 RegSetValueEx(hView, _T("GridExtent"), 0, REG_DWORD, (LPBYTE) &GridExtent, sizeof(DWORD));
140 RegSetValueEx(hView, _T("NoStretching"), 0, REG_DWORD, (LPBYTE) &NoStretching, sizeof(DWORD));
141 RegSetValueEx(hView, _T("ShowThumbnail"), 0, REG_DWORD, (LPBYTE) &ShowThumbnail, sizeof(DWORD));
142 RegSetValueEx(hView, _T("SnapToGrid"), 0, REG_DWORD, (LPBYTE) &SnapToGrid, sizeof(DWORD));
143 RegSetValueEx(hView, _T("ThumbHeight"), 0, REG_DWORD, (LPBYTE) &ThumbHeight, sizeof(DWORD));
144 RegSetValueEx(hView, _T("ThumbWidth"), 0, REG_DWORD, (LPBYTE) &ThumbWidth, sizeof(DWORD));
145 RegSetValueEx(hView, _T("ThumbXPos"), 0, REG_DWORD, (LPBYTE) &ThumbXPos, sizeof(DWORD));
146 RegSetValueEx(hView, _T("ThumbYPos"), 0, REG_DWORD, (LPBYTE) &ThumbYPos, sizeof(DWORD));
147 RegSetValueEx(hView, _T("UnitSetting"), 0, REG_DWORD, (LPBYTE) &UnitSetting, sizeof(DWORD));
148 RegSetValueEx(hView, _T("WindowPlacement"), 0, REG_BINARY, (LPBYTE) &WindowPlacement, sizeof(WINDOWPLACEMENT));
149 }
150
151 CRegKey hKey;
152 if (hKey.Create(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Paint\\Recent File List")) == ERROR_SUCCESS)
153 {
154 if (!strFile1.IsEmpty())
155 hKey.SetStringValue(_T("File1"), strFile1);
156 if (!strFile2.IsEmpty())
157 hKey.SetStringValue(_T("File2"), strFile2);
158 if (!strFile3.IsEmpty())
159 hKey.SetStringValue(_T("File3"), strFile3);
160 if (!strFile4.IsEmpty())
161 hKey.SetStringValue(_T("File4"), strFile4);
162 }
163 }
164
165 void RegistrySettings::SetMostRecentFile(LPCTSTR pszPathName)
166 {
167 if (strFile1 == pszPathName)
168 {
169 // do nothing
170 }
171 else if (strFile2 == pszPathName)
172 {
173 CString strTemp = strFile2;
174 strFile2 = strFile1;
175 strFile1 = strTemp;
176 }
177 else if (strFile3 == pszPathName)
178 {
179 CString strTemp = strFile3;
180 strFile3 = strFile2;
181 strFile2 = strFile1;
182 strFile1 = strTemp;
183 }
184 else if (strFile4 == pszPathName)
185 {
186 CString strTemp = strFile4;
187 strFile4 = strFile3;
188 strFile3 = strFile2;
189 strFile2 = strFile1;
190 strFile1 = strTemp;
191 }
192 else
193 {
194 strFile4 = strFile3;
195 strFile3 = strFile2;
196 strFile2 = strFile1;
197 strFile1 = pszPathName;
198 }
199 }