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