:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / lib / shell32 / control / settings.c
1 /*
2  *  ReactOS shell32 - Control Panel
3  *
4  *  settings.c
5  *
6  *  Copyright (C) 2002  Robert Dickenson <robd@reactos.org>
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22     
23 #define WIN32_LEAN_AND_MEAN     // Exclude rarely-used stuff from Windows headers
24 #include <windows.h>
25 #include <cpl.h>
26 #include <commctrl.h>
27 #include <tchar.h>
28 #include <assert.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 //#include <memory.h>
33 //#include <process.h>
34     
35 #include "control.h"
36 //#include "framewnd.h"
37 //#include "shell32.h"
38 #include "trace.h"
39
40 //#define WIN32_LEAN_AND_MEAN     // Exclude rarely-used stuff from Windows headers
41 //#include <windows.h>
42 //#include <tchar.h>
43 //#include <assert.h>
44 //#define ASSERT assert
45   
46 //#include "control.h"
47 #include "settings.h"
48
49
50 ////////////////////////////////////////////////////////////////////////////////
51 // Global and Local Variables:
52 //
53
54 extern DWORD nListStyle;
55 extern DWORD nSortOrder;
56
57 ////////////////////////////////////////////////////////////////////////////////
58 // Local module support methods
59 //
60
61 BOOL CheckResult(LONG error)
62 {
63     if (error != ERROR_SUCCESS) {
64         PTSTR msg;
65         if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
66                 0, error, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), (PTSTR)&msg, 0, NULL))
67                     MessageBox(NULL, msg, szTitle, MB_ICONERROR | MB_OK);
68         else
69                 MessageBox(NULL, _T("Error"), szTitle, MB_ICONERROR | MB_OK);
70         LocalFree(msg);
71         return FALSE;
72     }
73     return TRUE;
74 }
75
76 static BOOL CreateRegistryPath(LPTSTR szRegPath, int nMaxLen)
77 {
78     LPTSTR pRegPath = szRegPath;
79
80     // Initialise registry path string from application PATH and KEY resources
81     int nLength = LoadString(hInst, IDS_APP_REG_PATH, szRegPath, nMaxLen);
82     nLength += LoadString(hInst, IDS_APP_REG_KEY, szRegPath + nLength, nMaxLen - nLength);
83 //    ASSERT(nLength < (nMaxLen - 1));
84     szRegPath[nLength] = _T('\\');
85
86     // walk the registry path string creating the tree if required
87     while ((pRegPath = _tcschr(pRegPath, _T('\\')))) {
88         LONG  result;
89         HKEY  hKey = NULL;
90         *pRegPath = _T('\0');
91         // Open (or create) the key
92         result = RegCreateKeyEx(HKEY_CURRENT_USER, szRegPath, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
93         if (!CheckResult(result)) return FALSE;
94         RegCloseKey(hKey);
95         *pRegPath = _T('\\');
96         pRegPath = pRegPath + 1;
97     }
98     szRegPath[nLength] = _T('\0');
99     return TRUE;
100 }
101
102 BOOL LoadSettings(RECT* prc)
103 {
104     BOOL retval = TRUE;
105     TCHAR szRegPath[MAX_LOADSTRING];
106         HMENU hMenuView = GetSubMenu(hMenuFrame, ID_VIEW_MENU);
107
108     HKEY  hKey;
109     DWORD dwSize;
110     LONG  result;
111
112     if (!CreateRegistryPath(szRegPath, MAX_LOADSTRING)) return FALSE;
113
114     // Open the key
115     result = RegOpenKeyEx(HKEY_CURRENT_USER, szRegPath, 0, KEY_READ, &hKey);
116     if (!CheckResult(result)) return FALSE;
117
118     // Read the settings
119     dwSize = sizeof(nListStyle);
120     result = RegQueryValueEx(hKey, _T("ListStyle"), NULL, NULL, (LPBYTE)&nListStyle, &dwSize);
121     dwSize = sizeof(nSortOrder);
122     result = RegQueryValueEx(hKey, _T("SortOrder"), NULL, NULL, (LPBYTE)&nSortOrder, &dwSize);
123     dwSize = sizeof(RECT);
124     result = RegQueryValueEx(hKey, _T("WindowPos"), NULL, NULL, (LPBYTE)prc, &dwSize);
125     if (result != ERROR_SUCCESS) {
126         retval = FALSE;
127     }
128
129     // Close the key
130     RegCloseKey(hKey);
131     return retval;
132 }
133
134 void SaveSettings(RECT* prc)
135 {
136     TCHAR szRegPath[MAX_LOADSTRING];
137     HKEY hKey = NULL;
138     LONG result;
139
140     if (!CreateRegistryPath(szRegPath, MAX_LOADSTRING)) return;
141
142     // Open the key
143     result = RegOpenKeyEx(HKEY_CURRENT_USER, szRegPath, 0, KEY_WRITE, &hKey);
144     if (!CheckResult(result)) return;
145
146     // Save the settings
147     result = RegSetValueEx(hKey, _T("ListStyle"), 0, REG_DWORD, (LPBYTE)&nListStyle, sizeof(nListStyle));
148     if (!CheckResult(result)) goto abort;
149     result = RegSetValueEx(hKey, _T("SortOrder"), 0, REG_DWORD, (LPBYTE)&nSortOrder, sizeof(nSortOrder));
150     if (!CheckResult(result)) goto abort;
151     result = RegSetValueEx(hKey, _T("WindowPos"), 0, REG_BINARY, (LPBYTE)prc, sizeof(RECT));
152     if (!CheckResult(result)) goto abort;
153
154 abort:
155     // Close the key
156     RegCloseKey(hKey);
157 }