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