[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(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 if ((dwStyle > 2) || (dwTile > 2))
41 return;
42
43 CRegKey desktop;
44 if (desktop.Open(HKEY_CURRENT_USER, _T("Control Panel\\Desktop")) == ERROR_SUCCESS)
45 {
46 CString strStyle, strTile;
47
48 desktop.SetStringValue(_T("Wallpaper"), szFileName);
49
50 strStyle.Format(_T("%lu"), dwStyle);
51 strTile.Format(_T("%lu"), dwTile);
52
53 desktop.SetStringValue(_T("WallpaperStyle"), strStyle);
54 desktop.SetStringValue(_T("TileWallpaper"), strTile);
55 }
56
57 SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (PVOID) szFileName, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
58 }
59
60 void RegistrySettings::LoadPresets()
61 {
62 BMPHeight = 300;
63 BMPWidth = 400;
64 GridExtent = 1;
65 NoStretching = 0;
66 ShowThumbnail = 0;
67 SnapToGrid = 0;
68 ThumbHeight = 100;
69 ThumbWidth = 120;
70 ThumbXPos = 180;
71 ThumbYPos = 200;
72 UnitSetting = 0;
73 const WINDOWPLACEMENT DefaultWindowPlacement = {
74 sizeof(WINDOWPLACEMENT),
75 0,
76 SW_SHOWNORMAL,
77 {0, 0},
78 {-1, -1},
79 {100, 100, 700, 550}
80 };
81 WindowPlacement = DefaultWindowPlacement;
82 }
83
84 void RegistrySettings::Load()
85 {
86 LoadPresets();
87
88 CRegKey view;
89 if (view.Open(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Paint\\View"), KEY_READ) == ERROR_SUCCESS)
90 {
91 ReadDWORD(view, _T("BMPHeight"), BMPHeight, TRUE);
92 ReadDWORD(view, _T("BMPWidth"), BMPWidth, TRUE);
93 ReadDWORD(view, _T("GridExtent"), GridExtent, FALSE);
94 ReadDWORD(view, _T("NoStretching"), NoStretching, FALSE);
95 ReadDWORD(view, _T("ShowThumbnail"), ShowThumbnail, FALSE);
96 ReadDWORD(view, _T("SnapToGrid"), SnapToGrid, FALSE);
97 ReadDWORD(view, _T("ThumbHeight"), ThumbHeight, TRUE);
98 ReadDWORD(view, _T("ThumbWidth"), ThumbWidth, TRUE);
99 ReadDWORD(view, _T("ThumbXPos"), ThumbXPos, TRUE);
100 ReadDWORD(view, _T("ThumbYPos"), ThumbYPos, TRUE);
101 ReadDWORD(view, _T("UnitSetting"), UnitSetting, FALSE);
102
103 ULONG pnBytes = sizeof(WINDOWPLACEMENT);
104 view.QueryBinaryValue(_T("WindowPlacement"), &WindowPlacement, &pnBytes);
105 }
106
107 CRegKey files;
108 if (files.Open(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Paint\\Recent File List"), KEY_READ) == ERROR_SUCCESS)
109 {
110 ReadFileHistory(files, _T("File1"), strFile1);
111 ReadFileHistory(files, _T("File2"), strFile2);
112 ReadFileHistory(files, _T("File3"), strFile3);
113 ReadFileHistory(files, _T("File4"), strFile4);
114 }
115 }
116
117 void RegistrySettings::Store()
118 {
119 CRegKey view;
120 if (view.Create(HKEY_CURRENT_USER,
121 _T("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Paint\\View")) == ERROR_SUCCESS)
122 {
123 view.SetDWORDValue(_T("BMPHeight"), BMPHeight);
124 view.SetDWORDValue(_T("BMPWidth"), BMPWidth);
125 view.SetDWORDValue(_T("GridExtent"), GridExtent);
126 view.SetDWORDValue(_T("NoStretching"), NoStretching);
127 view.SetDWORDValue(_T("ShowThumbnail"), ShowThumbnail);
128 view.SetDWORDValue(_T("SnapToGrid"), SnapToGrid);
129 view.SetDWORDValue(_T("ThumbHeight"), ThumbHeight);
130 view.SetDWORDValue(_T("ThumbWidth"), ThumbWidth);
131 view.SetDWORDValue(_T("ThumbXPos"), ThumbXPos);
132 view.SetDWORDValue(_T("ThumbYPos"), ThumbYPos);
133 view.SetDWORDValue(_T("UnitSetting"), UnitSetting);
134
135 view.SetBinaryValue(_T("WindowPlacement"), &WindowPlacement, sizeof(WINDOWPLACEMENT));
136 }
137
138 CRegKey files;
139 if (files.Create(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Paint\\Recent File List")) == ERROR_SUCCESS)
140 {
141 if (!strFile1.IsEmpty())
142 files.SetStringValue(_T("File1"), strFile1);
143 if (!strFile2.IsEmpty())
144 files.SetStringValue(_T("File2"), strFile2);
145 if (!strFile3.IsEmpty())
146 files.SetStringValue(_T("File3"), strFile3);
147 if (!strFile4.IsEmpty())
148 files.SetStringValue(_T("File4"), strFile4);
149 }
150 }
151
152 void RegistrySettings::SetMostRecentFile(LPCTSTR szPathName)
153 {
154 if (strFile1 == szPathName)
155 {
156 // do nothing
157 }
158 else if (strFile2 == szPathName)
159 {
160 CString strTemp = strFile2;
161 strFile2 = strFile1;
162 strFile1 = strTemp;
163 }
164 else if (strFile3 == szPathName)
165 {
166 CString strTemp = strFile3;
167 strFile3 = strFile2;
168 strFile2 = strFile1;
169 strFile1 = strTemp;
170 }
171 else if (strFile4 == szPathName)
172 {
173 CString strTemp = strFile4;
174 strFile4 = strFile3;
175 strFile3 = strFile2;
176 strFile2 = strFile1;
177 strFile1 = strTemp;
178 }
179 else
180 {
181 strFile4 = strFile3;
182 strFile3 = strFile2;
183 strFile2 = strFile1;
184 strFile1 = szPathName;
185 }
186 }