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 ReadString(CRegKey &key, LPCTSTR lpName, CString &strValue, LPCTSTR lpDefault = TEXT(""))
29 {
30     CString strTemp;
31     ULONG nChars = MAX_PATH;
32     LPTSTR psz = strTemp.GetBuffer(nChars);
33     LONG error = key.QueryStringValue(lpName, psz, &nChars);
34     strTemp.ReleaseBuffer();
35 
36     if (error == ERROR_SUCCESS)
37         strValue = strTemp;
38     else
39         strValue = lpDefault;
40 }
41 
42 void RegistrySettings::SetWallpaper(LPCTSTR szFileName, RegistrySettings::WallpaperStyle style)
43 {
44     CRegKey desktop;
45     if (desktop.Open(HKEY_CURRENT_USER, _T("Control Panel\\Desktop")) == ERROR_SUCCESS)
46     {
47         desktop.SetStringValue(_T("Wallpaper"), szFileName);
48 
49         desktop.SetStringValue(_T("WallpaperStyle"), (style == RegistrySettings::STRETCHED) ? _T("2") : _T("0"));
50         desktop.SetStringValue(_T("TileWallpaper"), (style == RegistrySettings::TILED) ? _T("1") : _T("0"));
51     }
52 
53     SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (PVOID) szFileName, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);
54 }
55 
56 void RegistrySettings::LoadPresets()
57 {
58     BMPHeight = GetSystemMetrics(SM_CYSCREEN) / 2;
59     BMPWidth = GetSystemMetrics(SM_CXSCREEN) / 2;
60     GridExtent = 1;
61     NoStretching = 0;
62     ShowThumbnail = 0;
63     SnapToGrid = 0;
64     ThumbHeight = 100;
65     ThumbWidth = 120;
66     ThumbXPos = 180;
67     ThumbYPos = 200;
68     UnitSetting = 0;
69     Bold = FALSE;
70     Italic = FALSE;
71     Underline = FALSE;
72     CharSet = DEFAULT_CHARSET;
73     PointSize = 14;
74     FontsPositionX = 0;
75     FontsPositionY = 0;
76     ShowTextTool = TRUE;
77 
78     LOGFONT lf;
79     GetObject(GetStockObject(DEFAULT_GUI_FONT), sizeof(lf), &lf);
80     strFontName = lf.lfFaceName;
81 
82     ZeroMemory(&WindowPlacement, sizeof(WindowPlacement));
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         ReadString(files, _T("File1"), strFile1);
112         ReadString(files, _T("File2"), strFile2);
113         ReadString(files, _T("File3"), strFile3);
114         ReadString(files, _T("File4"), strFile4);
115     }
116 
117     CRegKey text;
118     if (text.Open(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Paint\\Text"), KEY_READ) == ERROR_SUCCESS)
119     {
120         ReadDWORD(text, _T("Bold"),         Bold,           FALSE);
121         ReadDWORD(text, _T("Italic"),       Italic,         FALSE);
122         ReadDWORD(text, _T("Underline"),    Underline,      FALSE);
123         ReadDWORD(text, _T("CharSet"),      CharSet,        FALSE);
124         ReadDWORD(text, _T("PointSize"),    PointSize,      FALSE);
125         ReadDWORD(text, _T("PositionX"),    FontsPositionX, FALSE);
126         ReadDWORD(text, _T("PositionY"),    FontsPositionY, FALSE);
127         ReadDWORD(text, _T("ShowTextTool"), ShowTextTool,   FALSE);
128         ReadString(text, _T("TypeFaceName"), strFontName, strFontName);
129     }
130 
131     // Fix the bitmap size if too large
132     if (BMPWidth > 5000)
133         BMPWidth = (GetSystemMetrics(SM_CXSCREEN) * 6) / 10;
134     if (BMPHeight > 5000)
135         BMPHeight = (GetSystemMetrics(SM_CYSCREEN) * 6) / 10;
136 }
137 
138 void RegistrySettings::Store()
139 {
140     CRegKey view;
141     if (view.Create(HKEY_CURRENT_USER,
142                      _T("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Paint\\View")) == ERROR_SUCCESS)
143     {
144         view.SetDWORDValue(_T("BMPHeight"),     BMPHeight);
145         view.SetDWORDValue(_T("BMPWidth"),      BMPWidth);
146         view.SetDWORDValue(_T("GridExtent"),    GridExtent);
147         view.SetDWORDValue(_T("NoStretching"),  NoStretching);
148         view.SetDWORDValue(_T("ShowThumbnail"), ShowThumbnail);
149         view.SetDWORDValue(_T("SnapToGrid"),    SnapToGrid);
150         view.SetDWORDValue(_T("ThumbHeight"),   ThumbHeight);
151         view.SetDWORDValue(_T("ThumbWidth"),    ThumbWidth);
152         view.SetDWORDValue(_T("ThumbXPos"),     ThumbXPos);
153         view.SetDWORDValue(_T("ThumbYPos"),     ThumbYPos);
154         view.SetDWORDValue(_T("UnitSetting"),   UnitSetting);
155 
156         view.SetBinaryValue(_T("WindowPlacement"), &WindowPlacement, sizeof(WINDOWPLACEMENT));
157     }
158 
159     CRegKey files;
160     if (files.Create(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Paint\\Recent File List")) == ERROR_SUCCESS)
161     {
162         if (!strFile1.IsEmpty())
163             files.SetStringValue(_T("File1"), strFile1);
164         if (!strFile2.IsEmpty())
165             files.SetStringValue(_T("File2"), strFile2);
166         if (!strFile3.IsEmpty())
167             files.SetStringValue(_T("File3"), strFile3);
168         if (!strFile4.IsEmpty())
169             files.SetStringValue(_T("File4"), strFile4);
170     }
171 
172     CRegKey text;
173     if (text.Create(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Paint\\Text")) == ERROR_SUCCESS)
174     {
175         text.SetDWORDValue(_T("Bold"),          Bold);
176         text.SetDWORDValue(_T("Italic"),        Italic);
177         text.SetDWORDValue(_T("Underline"),     Underline);
178         text.SetDWORDValue(_T("CharSet"),       CharSet);
179         text.SetDWORDValue(_T("PointSize"),     PointSize);
180         text.SetDWORDValue(_T("PositionX"),     FontsPositionX);
181         text.SetDWORDValue(_T("PositionY"),     FontsPositionY);
182         text.SetDWORDValue(_T("ShowTextTool"),  ShowTextTool);
183         text.SetStringValue(_T("TypeFaceName"), strFontName);
184     }
185 }
186 
187 void RegistrySettings::SetMostRecentFile(LPCTSTR szPathName)
188 {
189     if (szPathName && szPathName[0])
190         SHAddToRecentDocs(SHARD_PATHW, szPathName);
191 
192     if (strFile1 == szPathName)
193     {
194         // do nothing
195     }
196     else if (strFile2 == szPathName)
197     {
198         CString strTemp = strFile2;
199         strFile2 = strFile1;
200         strFile1 = strTemp;
201     }
202     else if (strFile3 == szPathName)
203     {
204         CString strTemp = strFile3;
205         strFile3 = strFile2;
206         strFile2 = strFile1;
207         strFile1 = strTemp;
208     }
209     else if (strFile4 == szPathName)
210     {
211         CString strTemp = strFile4;
212         strFile4 = strFile3;
213         strFile3 = strFile2;
214         strFile2 = strFile1;
215         strFile1 = strTemp;
216     }
217     else
218     {
219         strFile4 = strFile3;
220         strFile3 = strFile2;
221         strFile2 = strFile1;
222         strFile1 = szPathName;
223     }
224 }
225