ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

registry.c
Go to the documentation of this file.
00001 /*
00002  * Wordpad implementation - Registry functions
00003  *
00004  * Copyright 2007 by Alexander N. Sørnes <alex@thehandofagony.com>
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
00019  */
00020 
00021 #include <windows.h>
00022 #include <shlobj.h>
00023 #include <richedit.h>
00024 
00025 #include "wordpad.h"
00026 
00027 static const WCHAR key_recentfiles[] = {'R','e','c','e','n','t',' ','f','i','l','e',
00028                                         ' ','l','i','s','t',0};
00029 static const WCHAR key_options[] = {'O','p','t','i','o','n','s',0};
00030 static const WCHAR key_settings[] = {'S','e','t','t','i','n','g','s',0};
00031 static const WCHAR key_rtf[] = {'R','T','F',0};
00032 static const WCHAR key_text[] = {'T','e','x','t',0};
00033 
00034 static const WCHAR var_file[] = {'F','i','l','e','%','d',0};
00035 static const WCHAR var_framerect[] = {'F','r','a','m','e','R','e','c','t',0};
00036 static const WCHAR var_barstate0[] = {'B','a','r','S','t','a','t','e','0',0};
00037 static const WCHAR var_wrap[] = {'W','r','a','p',0};
00038 static const WCHAR var_maximized[] = {'M','a','x','i','m','i','z','e','d',0};
00039 
00040 static LRESULT registry_get_handle(HKEY *hKey, LPDWORD action, LPCWSTR subKey)
00041 {
00042     LONG ret;
00043     static const WCHAR wszProgramKey[] = {'S','o','f','t','w','a','r','e','\\',
00044         'M','i','c','r','o','s','o','f','t','\\',
00045         'W','i','n','d','o','w','s','\\',
00046         'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
00047         'A','p','p','l','e','t','s','\\',
00048         'W','o','r','d','p','a','d',0};
00049         LPWSTR key = (LPWSTR)wszProgramKey;
00050 
00051         if(subKey)
00052         {
00053             WCHAR backslash[] = {'\\',0};
00054             key = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
00055                             (lstrlenW(wszProgramKey)+lstrlenW(subKey)+lstrlenW(backslash)+1)
00056                                     *sizeof(WCHAR));
00057 
00058             if(!key)
00059                 return 1;
00060 
00061             lstrcpyW(key, wszProgramKey);
00062             lstrcatW(key, backslash);
00063             lstrcatW(key, subKey);
00064         }
00065 
00066         if(action)
00067         {
00068             ret = RegCreateKeyExW(HKEY_CURRENT_USER, key, 0, NULL, REG_OPTION_NON_VOLATILE,
00069                                   KEY_READ | KEY_WRITE, NULL, hKey, action);
00070         } else
00071         {
00072             ret = RegOpenKeyExW(HKEY_CURRENT_USER, key, 0, KEY_READ | KEY_WRITE, hKey);
00073         }
00074 
00075         if(subKey)
00076             HeapFree(GetProcessHeap(), 0, key);
00077 
00078         return ret;
00079 }
00080 
00081 void registry_set_options(HWND hMainWnd)
00082 {
00083     HKEY hKey = 0;
00084     DWORD action;
00085 
00086     if(registry_get_handle(&hKey, &action, key_options) == ERROR_SUCCESS)
00087     {
00088         WINDOWPLACEMENT wp;
00089         DWORD isMaximized;
00090 
00091         wp.length = sizeof(WINDOWPLACEMENT);
00092         GetWindowPlacement(hMainWnd, &wp);
00093         isMaximized = (wp.showCmd == SW_SHOWMAXIMIZED);
00094 
00095         RegSetValueExW(hKey, var_framerect, 0, REG_BINARY, (LPBYTE)&wp.rcNormalPosition, sizeof(RECT));
00096         RegSetValueExW(hKey, var_maximized, 0, REG_DWORD, (LPBYTE)&isMaximized, sizeof(DWORD));
00097 
00098         registry_set_pagemargins(hKey);
00099         RegCloseKey(hKey);
00100     }
00101 
00102     if(registry_get_handle(&hKey, &action, key_settings) == ERROR_SUCCESS)
00103     {
00104         registry_set_previewpages(hKey);
00105         RegCloseKey(hKey);
00106     }
00107 }
00108 
00109 void registry_read_winrect(RECT* rc)
00110 {
00111     HKEY hKey = 0;
00112     DWORD size = sizeof(RECT);
00113 
00114     if(registry_get_handle(&hKey, 0, key_options) != ERROR_SUCCESS ||
00115        RegQueryValueExW(hKey, var_framerect, 0, NULL, (LPBYTE)rc, &size) !=
00116        ERROR_SUCCESS || size != sizeof(RECT))
00117     {
00118         rc->top = 0;
00119         rc->left = 0;
00120         rc->bottom = 300;
00121         rc->right = 600;
00122     }
00123 
00124     RegCloseKey(hKey);
00125 }
00126 
00127 void registry_read_maximized(DWORD *bMaximized)
00128 {
00129     HKEY hKey = 0;
00130     DWORD size = sizeof(DWORD);
00131 
00132     if(registry_get_handle(&hKey, 0, key_options) != ERROR_SUCCESS ||
00133        RegQueryValueExW(hKey, var_maximized, 0, NULL, (LPBYTE)bMaximized, &size) !=
00134        ERROR_SUCCESS || size != sizeof(DWORD))
00135     {
00136         *bMaximized = FALSE;
00137     }
00138 
00139     RegCloseKey(hKey);
00140 }
00141 
00142 static void truncate_path(LPWSTR file, LPWSTR out, LPWSTR pos1, LPWSTR pos2)
00143 {
00144     static const WCHAR dots[] = {'.','.','.',0};
00145 
00146     *++pos1 = 0;
00147 
00148     lstrcatW(out, file);
00149     lstrcatW(out, dots);
00150     lstrcatW(out, pos2);
00151 }
00152 
00153 static void format_filelist_filename(LPWSTR file, LPWSTR out)
00154 {
00155     LPWSTR pos_basename;
00156     LPWSTR truncpos1, truncpos2;
00157     WCHAR myDocs[MAX_STRING_LEN];
00158 
00159     SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, (LPWSTR)&myDocs);
00160     pos_basename = file_basename(file);
00161     truncpos1 = NULL;
00162     truncpos2 = NULL;
00163 
00164     *(pos_basename-1) = 0;
00165     if(!lstrcmpiW(file, myDocs) || (lstrlenW(pos_basename) > FILELIST_ENTRY_LENGTH))
00166     {
00167         truncpos1 = pos_basename;
00168         *(pos_basename-1) = '\\';
00169     } else
00170     {
00171         LPWSTR pos;
00172         BOOL morespace = FALSE;
00173 
00174         *(pos_basename-1) = '\\';
00175 
00176         for(pos = file; pos < pos_basename; pos++)
00177         {
00178             if(*pos == '\\' || *pos == '/')
00179             {
00180                 if(truncpos1)
00181                 {
00182                     if((pos - file + lstrlenW(pos_basename)) > FILELIST_ENTRY_LENGTH)
00183                         break;
00184 
00185                     truncpos1 = pos;
00186                     morespace = TRUE;
00187                     break;
00188                 }
00189 
00190                 if((pos - file + lstrlenW(pos_basename)) > FILELIST_ENTRY_LENGTH)
00191                     break;
00192 
00193                 truncpos1 = pos;
00194             }
00195         }
00196 
00197         if(morespace)
00198         {
00199             for(pos = pos_basename; pos >= truncpos1; pos--)
00200             {
00201                 if(*pos == '\\' || *pos == '/')
00202                 {
00203                     if((truncpos1 - file + lstrlenW(pos_basename) + pos_basename - pos) > FILELIST_ENTRY_LENGTH)
00204                         break;
00205 
00206                     truncpos2 = pos;
00207                 }
00208             }
00209         }
00210     }
00211 
00212     if(truncpos1 == pos_basename)
00213         lstrcatW(out, pos_basename);
00214     else if(truncpos1 == truncpos2 || !truncpos2)
00215         lstrcatW(out, file);
00216     else
00217         truncate_path(file, out, truncpos1, truncpos2);
00218 }
00219 
00220 void registry_read_filelist(HWND hMainWnd)
00221 {
00222     HKEY hFileKey;
00223 
00224     if(registry_get_handle(&hFileKey, 0, key_recentfiles) == ERROR_SUCCESS)
00225     {
00226         WCHAR itemText[MAX_PATH+3], buffer[MAX_PATH];
00227         /* The menu item name is not the same as the file name, so we need to store
00228         the file name here */
00229         static WCHAR file1[MAX_PATH], file2[MAX_PATH], file3[MAX_PATH], file4[MAX_PATH];
00230         WCHAR numFormat[] = {'&','%','d',' ',0};
00231         LPWSTR pFile[] = {file1, file2, file3, file4};
00232         DWORD pathSize = MAX_PATH*sizeof(WCHAR);
00233         int i;
00234         WCHAR key[6];
00235         MENUITEMINFOW mi;
00236         HMENU hMenu = GetMenu(hMainWnd);
00237 
00238         mi.cbSize = sizeof(MENUITEMINFOW);
00239         mi.fMask = MIIM_ID | MIIM_DATA | MIIM_STRING | MIIM_FTYPE;
00240         mi.fType = MFT_STRING;
00241         mi.dwTypeData = itemText;
00242         mi.wID = ID_FILE_RECENT1;
00243 
00244         RemoveMenu(hMenu, ID_FILE_RECENT_SEPARATOR, MF_BYCOMMAND);
00245         for(i = 0; i < FILELIST_ENTRIES; i++)
00246         {
00247             wsprintfW(key, var_file, i+1);
00248             RemoveMenu(hMenu, ID_FILE_RECENT1+i, MF_BYCOMMAND);
00249             if(RegQueryValueExW(hFileKey, (LPWSTR)key, 0, NULL, (LPBYTE)pFile[i], &pathSize)
00250                != ERROR_SUCCESS)
00251                 break;
00252 
00253             mi.dwItemData = (ULONG_PTR)pFile[i];
00254             wsprintfW(itemText, numFormat, i+1);
00255 
00256             lstrcpyW(buffer, pFile[i]);
00257 
00258             format_filelist_filename(buffer, itemText);
00259 
00260             InsertMenuItemW(hMenu, ID_FILE_EXIT, FALSE, &mi);
00261             mi.wID++;
00262             pathSize = MAX_PATH*sizeof(WCHAR);
00263         }
00264         mi.fType = MFT_SEPARATOR;
00265         mi.fMask = MIIM_FTYPE | MIIM_ID;
00266         InsertMenuItemW(hMenu, ID_FILE_EXIT, FALSE, &mi);
00267 
00268         RegCloseKey(hFileKey);
00269     }
00270 }
00271 
00272 void registry_set_filelist(LPCWSTR newFile, HWND hMainWnd)
00273 {
00274     HKEY hKey;
00275     DWORD action;
00276 
00277     if(registry_get_handle(&hKey, &action, key_recentfiles) == ERROR_SUCCESS)
00278     {
00279         LPCWSTR pFiles[FILELIST_ENTRIES];
00280         int i;
00281         HMENU hMenu = GetMenu(hMainWnd);
00282         MENUITEMINFOW mi;
00283         WCHAR buffer[6];
00284 
00285         mi.cbSize = sizeof(MENUITEMINFOW);
00286         mi.fMask = MIIM_DATA;
00287 
00288         for(i = 0; i < FILELIST_ENTRIES; i++)
00289             pFiles[i] = NULL;
00290 
00291         for(i = 0; GetMenuItemInfoW(hMenu, ID_FILE_RECENT1+i, FALSE, &mi); i++)
00292             pFiles[i] = (LPWSTR)mi.dwItemData;
00293 
00294         if(lstrcmpiW(newFile, pFiles[0]))
00295         {
00296             for(i = 0; pFiles[i] && i < FILELIST_ENTRIES; i++)
00297             {
00298                 if(!lstrcmpiW(pFiles[i], newFile))
00299                 {
00300                     int j;
00301                     for(j = 0; pFiles[j] && j < i; j++)
00302                     {
00303                         pFiles[i-j] = pFiles[i-j-1];
00304                     }
00305                     pFiles[0] = NULL;
00306                     break;
00307                 }
00308             }
00309 
00310             if(!pFiles[0])
00311             {
00312                 pFiles[0] = newFile;
00313             } else
00314             {
00315                 for(i = 0; i < FILELIST_ENTRIES-1; i++)
00316                     pFiles[FILELIST_ENTRIES-1-i] = pFiles[FILELIST_ENTRIES-2-i];
00317 
00318                 pFiles[0] = newFile;
00319             }
00320 
00321             for(i = 0; pFiles[i] && i < FILELIST_ENTRIES; i++)
00322             {
00323                 wsprintfW(buffer, var_file, i+1);
00324                 RegSetValueExW(hKey, (LPWSTR)&buffer, 0, REG_SZ, (const BYTE*)pFiles[i],
00325                                (lstrlenW(pFiles[i])+1)*sizeof(WCHAR));
00326             }
00327         }
00328         RegCloseKey(hKey);
00329     }
00330     registry_read_filelist(hMainWnd);
00331 }
00332 
00333 int reg_formatindex(WPARAM format)
00334 {
00335     return (format & SF_TEXT) ? 1 : 0;
00336 }
00337 
00338 void registry_read_options(void)
00339 {
00340     HKEY hKey;
00341 
00342     if(registry_get_handle(&hKey, 0, key_options) != ERROR_SUCCESS)
00343         registry_read_pagemargins(NULL);
00344     else
00345     {
00346         registry_read_pagemargins(hKey);
00347         RegCloseKey(hKey);
00348     }
00349 
00350     if(registry_get_handle(&hKey, 0, key_settings) != ERROR_SUCCESS) {
00351         registry_read_previewpages(NULL);
00352     } else {
00353         registry_read_previewpages(hKey);
00354         RegCloseKey(hKey);
00355     }
00356 }
00357 
00358 static void registry_read_formatopts(int index, LPCWSTR key, DWORD barState[], DWORD wordWrap[])
00359 {
00360     HKEY hKey;
00361     DWORD action = 0;
00362     BOOL fetched = FALSE;
00363     barState[index] = 0;
00364     wordWrap[index] = 0;
00365 
00366     if(registry_get_handle(&hKey, &action, key) != ERROR_SUCCESS)
00367         return;
00368 
00369     if(action == REG_OPENED_EXISTING_KEY)
00370     {
00371         DWORD size = sizeof(DWORD);
00372 
00373         if(RegQueryValueExW(hKey, var_barstate0, 0, NULL, (LPBYTE)&barState[index],
00374            &size) == ERROR_SUCCESS)
00375             fetched = TRUE;
00376     }
00377 
00378     if(!fetched)
00379         barState[index] = (1 << BANDID_TOOLBAR) | (1 << BANDID_FORMATBAR) | (1 << BANDID_RULER) | (1 << BANDID_STATUSBAR);
00380 
00381     fetched = FALSE;
00382     if(action == REG_OPENED_EXISTING_KEY)
00383     {
00384         DWORD size = sizeof(DWORD);
00385         if(RegQueryValueExW(hKey, var_wrap, 0, NULL, (LPBYTE)&wordWrap[index],
00386            &size) == ERROR_SUCCESS)
00387             fetched = TRUE;
00388     }
00389 
00390     if (!fetched)
00391     {
00392         if(index == reg_formatindex(SF_RTF))
00393             wordWrap[index] = ID_WORDWRAP_WINDOW;
00394         else if(index == reg_formatindex(SF_TEXT))
00395             wordWrap[index] = ID_WORDWRAP_NONE;
00396     }
00397 
00398     RegCloseKey(hKey);
00399 }
00400 
00401 void registry_read_formatopts_all(DWORD barState[], DWORD wordWrap[])
00402 {
00403     registry_read_formatopts(reg_formatindex(SF_RTF), key_rtf, barState, wordWrap);
00404     registry_read_formatopts(reg_formatindex(SF_TEXT), key_text, barState, wordWrap);
00405 }
00406 
00407 static void registry_set_formatopts(int index, LPCWSTR key, DWORD barState[], DWORD wordWrap[])
00408 {
00409     HKEY hKey;
00410     DWORD action = 0;
00411 
00412     if(registry_get_handle(&hKey, &action, key) == ERROR_SUCCESS)
00413     {
00414         RegSetValueExW(hKey, var_barstate0, 0, REG_DWORD, (LPBYTE)&barState[index],
00415                        sizeof(DWORD));
00416         RegSetValueExW(hKey, var_wrap, 0, REG_DWORD, (LPBYTE)&wordWrap[index],
00417                        sizeof(DWORD));
00418         RegCloseKey(hKey);
00419     }
00420 }
00421 
00422 void registry_set_formatopts_all(DWORD barState[], DWORD wordWrap[])
00423 {
00424     registry_set_formatopts(reg_formatindex(SF_RTF), key_rtf, barState, wordWrap);
00425     registry_set_formatopts(reg_formatindex(SF_TEXT), key_text, barState, wordWrap);
00426 }

Generated on Sun May 27 2012 04:17:06 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.