[WELCOME]: Addendum to r73656: The application title is also customizable!
[reactos.git] / reactos / base / setup / welcome / welcome.c
index a0b5a4b..8391fed 100644 (file)
@@ -71,8 +71,7 @@ HBITMAP hDefaultTopicBitmap = NULL;
 HWND hWndCloseButton = NULL;
 HWND hWndCheckButton = NULL;
 
-/* TODO: Retrieve the preferences from a configuration file */
-BOOL bDisplayCheckBox = FALSE; // FIXME!
+BOOL bDisplayCheckBox = FALSE; // FIXME: We should also repaint the OS version correctly!
 BOOL bDisplayExitBtn  = TRUE;
 
 #define BUFFER_SIZE 1024
@@ -121,7 +120,7 @@ MainWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
 
 /* FUNCTIONS ****************************************************************/
 
-INT GetLocaleName(LCID Locale, LPTSTR lpLCData, SIZE_T cchData)
+INT GetLocaleName(IN LCID Locale, OUT LPTSTR lpLCData, IN SIZE_T cchData)
 {
     INT ret, ret2;
 
@@ -197,7 +196,8 @@ VOID TranslateEscapes(IN OUT LPTSTR lpString)
     }
 }
 
-BOOL LoadTopicsFromINI(LCID Locale)
+static BOOL
+LoadLocalizedResourcesFromINI(LCID Locale, LPTSTR lpResPath)
 {
     DWORD dwRet;
     DWORD dwSize;
@@ -215,7 +215,7 @@ BOOL LoadTopicsFromINI(LCID Locale)
     }
 
     /* Build the INI file name */
-    GetCurrentDirectory(ARRAYSIZE(szIniPath), szIniPath);
+    StringCchCopy(szIniPath, ARRAYSIZE(szIniPath), lpResPath);
     StringCchCat(szIniPath, ARRAYSIZE(szIniPath), TEXT("\\"));
     StringCchCat(szIniPath, ARRAYSIZE(szIniPath), szBuffer);
     StringCchCat(szIniPath, ARRAYSIZE(szIniPath), TEXT(".ini"));
@@ -225,7 +225,7 @@ BOOL LoadTopicsFromINI(LCID Locale)
     {
         StringCchCopy(szBuffer, ARRAYSIZE(szBuffer), TEXT("en-US"));
 
-        GetCurrentDirectory(ARRAYSIZE(szIniPath), szIniPath);
+        StringCchCopy(szIniPath, ARRAYSIZE(szIniPath), lpResPath);
         StringCchCat(szIniPath, ARRAYSIZE(szIniPath), TEXT("\\"));
         StringCchCat(szIniPath, ARRAYSIZE(szIniPath), szBuffer);
         StringCchCat(szIniPath, ARRAYSIZE(szIniPath), TEXT(".ini"));
@@ -236,6 +236,8 @@ BOOL LoadTopicsFromINI(LCID Locale)
         return FALSE; // TODO: For localized resource, see the general function.
 
     /* Try to load the default localized strings */
+    GetPrivateProfileString(TEXT("Defaults"), TEXT("AppTitle"), TEXT("ReactOS Welcome") /* default */,
+                            szAppTitle, ARRAYSIZE(szAppTitle), szIniPath);
     if (!GetPrivateProfileString(TEXT("Defaults"), TEXT("DefaultTopicTitle"), NULL /* default */,
                                  szDefaultTitle, ARRAYSIZE(szDefaultTitle), szIniPath))
     {
@@ -324,7 +326,8 @@ BOOL LoadTopicsFromINI(LCID Locale)
     return TRUE;
 }
 
-BOOL LoadTopics(VOID)
+static BOOL
+LoadLocalizedResources(LPTSTR lpResPath)
 {
 #define MAX_NUMBER_INTERNAL_TOPICS  3
 
@@ -338,13 +341,15 @@ BOOL LoadTopics(VOID)
      * First, try to load the default internal (localized) strings.
      * They can be redefined by the localized INI files.
      */
+    if (!LoadString(hInstance, IDS_APPTITLE, szAppTitle, ARRAYSIZE(szAppTitle)))
+        StringCchCopy(szAppTitle, ARRAYSIZE(szAppTitle), TEXT("ReactOS Welcome"));
     if (!LoadString(hInstance, IDS_DEFAULTTOPICTITLE, szDefaultTitle, ARRAYSIZE(szDefaultTitle)))
         *szDefaultTitle = 0;
     if (!LoadString(hInstance, IDS_DEFAULTTOPICDESC, szDefaultDesc, ARRAYSIZE(szDefaultDesc)))
         *szDefaultDesc = 0;
 
-    /* Try to load the topics from INI file */
-    if (LoadTopicsFromINI(LOCALE_USER_DEFAULT))
+    /* Try to load the resources from INI file */
+    if (*lpResPath && LoadLocalizedResourcesFromINI(LOCALE_USER_DEFAULT, lpResPath))
         return TRUE;
 
     /* We failed, fall back to internal (localized) resource */
@@ -379,7 +384,8 @@ BOOL LoadTopics(VOID)
     return TRUE;
 }
 
-VOID FreeTopics(VOID)
+static VOID
+FreeResources(VOID)
 {
     if (!pTopics)
         return;
@@ -394,6 +400,50 @@ VOID FreeTopics(VOID)
     dwNumberTopics = 0;
 }
 
+static BOOL
+LoadConfiguration(VOID)
+{
+    TCHAR szAppPath[MAX_PATH];
+    TCHAR szIniPath[MAX_PATH];
+    TCHAR szResPath[MAX_PATH];
+
+    /* Retrieve the full path to this application */
+    GetModuleFileName(NULL, szAppPath, ARRAYSIZE(szAppPath));
+    if (*szAppPath)
+    {
+        LPTSTR lpFileName = _tcsrchr(szAppPath, _T('\\'));
+        if (lpFileName)
+            *lpFileName = 0;
+        else
+            *szAppPath = 0;
+    }
+
+    /* Build the full INI file path name */
+    StringCchCopy(szIniPath, ARRAYSIZE(szIniPath), szAppPath);
+    StringCchCat(szIniPath, ARRAYSIZE(szIniPath), TEXT("\\welcome.ini"));
+
+    /* Verify that the file exists, otherwise use the default configuration */
+    if (GetFileAttributes(szIniPath) == INVALID_FILE_ATTRIBUTES)
+    {
+        /* Use the default configuration and retrieve the default resources */
+        return LoadLocalizedResources(TEXT(""));
+    }
+
+    /* Load the settings from the INI configuration file */
+    bDisplayCheckBox = !!GetPrivateProfileInt(TEXT("Welcome"), TEXT("DisplayCheckBox"),  FALSE /* default */, szIniPath);
+    bDisplayExitBtn  = !!GetPrivateProfileInt(TEXT("Welcome"), TEXT("DisplayExitButton"), TRUE /* default */, szIniPath);
+
+    if (!GetPrivateProfileString(TEXT("Welcome"), TEXT("ResourceDir"), NULL /* default */,
+                                 szResPath, ARRAYSIZE(szResPath), szIniPath))
+    {
+        *szResPath = 0;
+    }
+
+    /* Set the current directory to the one of this application, and retrieve the resources */
+    SetCurrentDirectory(szAppPath);
+    return LoadLocalizedResources(szResPath);
+}
+
 #if 0
 static VOID
 ShowLastWin32Error(HWND hWnd)
@@ -510,10 +560,8 @@ _tWinMain(HINSTANCE hInst,
     rcRightPanel.left = rcLeftPanel.right;
     rcRightPanel.right = ulInnerWidth - 1;
 
-    if (!LoadString(hInstance, IDS_APPTITLE, szAppTitle, ARRAYSIZE(szAppTitle)))
-        StringCchCopy(szAppTitle, ARRAYSIZE(szAppTitle), TEXT("ReactOS Welcome"));
-
-    LoadTopics();
+    /* Load the configuration and the resources */
+    LoadConfiguration();
 
     /* Create main window */
     hWndMain = CreateWindow(szFrameClass,
@@ -544,7 +592,8 @@ _tWinMain(HINSTANCE hInst,
         DispatchMessage(&msg);
     }
 
-    FreeTopics();
+    /* Cleanup */
+    FreeResources();
 
     return msg.wParam;
 }