[MSPAINT] get rid of all _stprintf occurrences and the respective magic number sized...
[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(CRegKey &key, LPCTSTR lpName, DWORD &dwValue, BOOL bCheckForDef)
17 {
18 DWORD dwPrev = dwValue;
19
20 if (key.QueryDWORDValue(lpName, dwValue) != ERROR_SUCCESS)
21 dwValue = dwPrev;
22
23 if (bCheckForDef && dwValue == 0)
24 dwValue = dwPrev;
25
26 return dwPrev;
27 }
28
29 static void ReadFileHistory(CRegKey &key, LPCTSTR lpName, CString &strFile)
30 {
31 ULONG nChars = MAX_PATH;
32 LPTSTR szFile = strFile.GetBuffer(nChars);
33 if (key.QueryStringValue(lpName, szFile, &nChars) != ERROR_SUCCESS)
34 szFile[0] = '\0';
35 strFile.ReleaseBuffer();
36 }
37
38 void RegistrySettings::SetWallpaper(LPCTSTR szFileName, DWORD dwStyle, DWORD dwTile)
39 {
40 // TODO: find out whether this line still serves a purpose
41 SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (PVOID) szFileName, SPIF_UPDATEINIFILE);
42
43 if ((dwStyle > 2) || (dwTile > 2))
44 return;
45
46 CRegKey desktop;
47 if (desktop.Open(HKEY_CURRENT_USER, _T("Control Panel\\Desktop")) == ERROR_SUCCESS)
48 {
49 CString strStyle, strTile;
50
51 desktop.SetStringValue(_T("Wallpaper"), szFileName);
52
53 strStyle.Format(_T("%lu"), dwStyle);
54 strTile.Format(_T("%lu"), dwTile);
55
56 desktop.SetStringValue(_T("WallpaperStyle"), strStyle);
57 desktop.SetStringValue(_T("TileWallpaper"), strTile);
58 }
59 }
60
61 void RegistrySettings::LoadPresets()
62 {
63 BMPHeight = 300;
64 BMPWidth = 400;
65 GridExtent = 1;
66 NoStretching = 0;
67 ShowThumbnail = 0;
68 SnapToGrid = 0;
69 ThumbHeight = 100;
70 ThumbWidth = 120;
71 ThumbXPos = 180;
72 ThumbYPos = 200;
73 UnitSetting = 0;
74 const WINDOWPLACEMENT DefaultWindowPlacement = {
75 sizeof(WINDOWPLACEMENT),
76 0,
77 SW_SHOWNORMAL,
78 {0, 0},
79 {-1, -1},
80 {100, 100, 700, 550}
81 };
82 WindowPlacement = DefaultWindowPlacement;
83 }
84
85 void RegistrySettings::Load()
86 {
87 LoadPresets();
88
89 CRegKey view;
90 if (view.Open(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Paint\\View"), KEY_READ) == ERROR_SUCCESS)
91 {
92 ReadDWORD(view, _T("BMPHeight"), BMPHeight, TRUE);
93 ReadDWORD(view, _T("BMPWidth"), BMPWidth, TRUE);
94 ReadDWORD(view, _T("GridExtent"), GridExtent, FALSE);
95 ReadDWORD(view, _T("NoStretching"), NoStretching, FALSE);
96 ReadDWORD(view, _T("ShowThumbnail"), ShowThumbnail, FALSE);
97 ReadDWORD(view, _T("SnapToGrid"), SnapToGrid, FALSE);
98 ReadDWORD(view, _T("ThumbHeight"), ThumbHeight, TRUE);
99 ReadDWORD(view, _T("ThumbWidth"), ThumbWidth, TRUE);
100 ReadDWORD(view, _T("ThumbXPos"), ThumbXPos, TRUE);
101 ReadDWORD(view, _T("ThumbYPos"), ThumbYPos, TRUE);
102 ReadDWORD(view, _T("UnitSetting"), UnitSetting, FALSE);
103
104 ULONG pnBytes = sizeof(WINDOWPLACEMENT);
105 view.QueryBinaryValue(_T("WindowPlacement"), &WindowPlacement, &pnBytes);
106 }
107
108 CRegKey files;
109 if (files.Open(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Paint\\Recent File List"), KEY_READ) == ERROR_SUCCESS)
110 {
111 ReadFileHistory(files, _T("File1"), strFile1);
112 ReadFileHistory(files, _T("File2"), strFile2);
113 ReadFileHistory(files, _T("File3"), strFile3);
114 ReadFileHistory(files, _T("File4"), strFile4);
115 }
116 }
117
118 void RegistrySettings::Store()
119 {
120 CRegKey view;
121 if (view.Create(HKEY_CURRENT_USER,
122 _T("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Paint\\View")) == ERROR_SUCCESS)
123 {
124 view.SetDWORDValue(_T("BMPHeight"), BMPHeight);
125 view.SetDWORDValue(_T("BMPWidth"), BMPWidth);
126 view.SetDWORDValue(_T("GridExtent"), GridExtent);
127 view.SetDWORDValue(_T("NoStretching"), NoStretching);
128 view.SetDWORDValue(_T("ShowThumbnail"), ShowThumbnail);
129 view.SetDWORDValue(_T("SnapToGrid"), SnapToGrid);
130 view.SetDWORDValue(_T("ThumbHeight"), ThumbHeight);
131 view.SetDWORDValue(_T("ThumbWidth"), ThumbWidth);
132 view.SetDWORDValue(_T("ThumbXPos"), ThumbXPos);
133 view.SetDWORDValue(_T("ThumbYPos"), ThumbYPos);
134 view.SetDWORDValue(_T("UnitSetting"), UnitSetting);
135
136 view.SetBinaryValue(_T("WindowPlacement"), &WindowPlacement, sizeof(WINDOWPLACEMENT));
137 }
138
139 CRegKey files;
140 if (files.Create(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Paint\\Recent File List")) == ERROR_SUCCESS)
141 {
142 if (!strFile1.IsEmpty())
143 files.SetStringValue(_T("File1"), strFile1);
144 if (!strFile2.IsEmpty())
145 files.SetStringValue(_T("File2"), strFile2);
146 if (!strFile3.IsEmpty())
147 files.SetStringValue(_T("File3"), strFile3);
148 if (!strFile4.IsEmpty())
149 files.SetStringValue(_T("File4"), strFile4);
150 }
151 }
152
153 void RegistrySettings::SetMostRecentFile(LPCTSTR szPathName)
154 {
155 if (strFile1 == szPathName)
156 {
157 // do nothing
158 }
159 else if (strFile2 == szPathName)
160 {
161 CString strTemp = strFile2;
162 strFile2 = strFile1;
163 strFile1 = strTemp;
164 }
165 else if (strFile3 == szPathName)
166 {
167 CString strTemp = strFile3;
168 strFile3 = strFile2;
169 strFile2 = strFile1;
170 strFile1 = strTemp;
171 }
172 else if (strFile4 == szPathName)
173 {
174 CString strTemp = strFile4;
175 strFile4 = strFile3;
176 strFile3 = strFile2;
177 strFile2 = strFile1;
178 strFile1 = strTemp;
179 }
180 else
181 {
182 strFile4 = strFile3;
183 strFile3 = strFile2;
184 strFile2 = strFile1;
185 strFile1 = szPathName;
186 }
187 }