[WINHTTP] Sync with Wine Staging 3.9. CORE-14656
[reactos.git] / dll / cpl / intl / misc.c
index 343d4a0..0967fd6 100644 (file)
@@ -1,27 +1,27 @@
 #include "intl.h"
 
-#define NUM_SHEETS           4
-
 /* Insert the space  */
-LPTSTR
-InsSpacePos(LPCTSTR szInsStr, const int nPos)
+PWSTR
+InsSpacePos(PCWSTR szInsStr, const int nPos)
 {
-    LPTSTR pszDestStr;
+    PWSTR pszDestStr;
     INT nDestStrCnt = 0;
     INT nStrCnt;
     INT nStrSize;
 
-    pszDestStr = (LPTSTR)malloc(MAX_SAMPLES_STR_SIZE * sizeof(TCHAR));
+    pszDestStr = (PWSTR)HeapAlloc(GetProcessHeap(), 0, MAX_SAMPLES_STR_SIZE * sizeof(WCHAR));
+    if (pszDestStr == NULL)
+        return NULL;
 
-    _tcscpy(pszDestStr, szInsStr);
+    wcscpy(pszDestStr, szInsStr);
 
-    nStrSize = _tcslen(szInsStr);
+    nStrSize = wcslen(szInsStr);
 
     for (nStrCnt = 0; nStrCnt < nStrSize; nStrCnt++)
     {
         if (nStrCnt == nStrSize - nPos)
         {
-            pszDestStr[nDestStrCnt] = _T(' ');
+            pszDestStr[nDestStrCnt] = L' ';
             nDestStrCnt++;
         }
 
@@ -29,38 +29,40 @@ InsSpacePos(LPCTSTR szInsStr, const int nPos)
         nDestStrCnt++;
     }
 
-    pszDestStr[nDestStrCnt] = _T('\0');
+    pszDestStr[nDestStrCnt] = L'\0';
 
     return pszDestStr;
 }
 
 /* Insert the spaces by format string separated by ';' */
-LPTSTR
-InsSpacesFmt(LPCTSTR szSourceStr, LPCTSTR szFmtStr)
+PWSTR
+InsSpacesFmt(PCWSTR szSourceStr, PCWSTR szFmtStr)
 {
-    LPTSTR pszDestStr;
-    LPTSTR pszTempStr;
-    TCHAR szFmtVal[255];
+    PWSTR pszDestStr;
+    PWSTR pszTempStr;
+    WCHAR szFmtVal[255];
     UINT nFmtCount = 0;
     INT nValCount = 0;
     INT nLastVal = 0;
     INT nSpaceOffset = 0;
     BOOL wasNul=FALSE;
 
-    pszDestStr = (LPTSTR)malloc(255 * sizeof(TCHAR));
+    pszDestStr = (PWSTR)HeapAlloc(GetProcessHeap(), 0, 255 * sizeof(WCHAR));
+    if (pszDestStr == NULL)
+        return NULL;
 
-    _tcscpy(pszDestStr, szSourceStr);
+    wcscpy(pszDestStr, szSourceStr);
 
     /* If format is clean return source string */
     if (!*szFmtStr)
         return pszDestStr;
 
     /* Search for all format values */
-    for (nFmtCount = 0; nFmtCount <= _tcslen(szFmtStr); nFmtCount++)
+    for (nFmtCount = 0; nFmtCount <= wcslen(szFmtStr); nFmtCount++)
     {
-        if (szFmtStr[nFmtCount] == _T(';') || szFmtStr[nFmtCount] == _T('\0'))
+        if (szFmtStr[nFmtCount] == L';' || szFmtStr[nFmtCount] == L'\0')
         {
-            if (_ttoi(szFmtVal) == 0 && !wasNul)
+            if (_wtoi(szFmtVal) == 0 && !wasNul)
             {
                 wasNul=TRUE;
                 break;
@@ -73,22 +75,22 @@ InsSpacesFmt(LPCTSTR szSourceStr, LPCTSTR szFmtStr)
             }
             else
             {
-                nSpaceOffset += _ttoi(szFmtVal);
+                nSpaceOffset += _wtoi(szFmtVal);
             }
 
-            szFmtVal[nValCount] = _T('\0');
+            szFmtVal[nValCount] = L'\0';
             nValCount=0;
 
             /* Insert space to finded position plus all pos before */
             pszTempStr = InsSpacePos(pszDestStr, nSpaceOffset);
-            _tcscpy(pszDestStr,pszTempStr);
-            free(pszTempStr);
+            wcscpy(pszDestStr,pszTempStr);
+            HeapFree(GetProcessHeap(), 0, pszTempStr);
 
             /* Num of spaces total increment */
             if (!wasNul)
             {
                 nSpaceOffset++;
-                nLastVal = _ttoi(szFmtVal);
+                nLastVal = _wtoi(szFmtVal);
             }
         }
         else
@@ -100,11 +102,11 @@ InsSpacesFmt(LPCTSTR szSourceStr, LPCTSTR szFmtStr)
     /* Create spaces for rest part of string */
     if (wasNul && nLastVal != 0)
     {
-        for (nFmtCount = nSpaceOffset + nLastVal; nFmtCount < _tcslen(pszDestStr); nFmtCount += nLastVal + 1)
+        for (nFmtCount = nSpaceOffset + nLastVal; nFmtCount < wcslen(pszDestStr); nFmtCount += nLastVal + 1)
         {
             pszTempStr = InsSpacePos(pszDestStr, nFmtCount);
-            _tcscpy(pszDestStr, pszTempStr);
-            free(pszTempStr);
+            wcscpy(pszDestStr, pszTempStr);
+            HeapFree(GetProcessHeap(), 0, pszTempStr);
         }
     }
 
@@ -112,30 +114,32 @@ InsSpacesFmt(LPCTSTR szSourceStr, LPCTSTR szFmtStr)
 }
 
 /* Replace given template in source string with string to replace and return received string */
-LPTSTR
-ReplaceSubStr(LPCTSTR szSourceStr,
-              LPCTSTR szStrToReplace,
-              LPCTSTR szTempl)
+PWSTR
+ReplaceSubStr(PCWSTR szSourceStr,
+              PCWSTR szStrToReplace,
+              PCWSTR szTempl)
 {
-    LPTSTR szDestStr;
+    PWSTR szDestStr;
     UINT nCharCnt;
     UINT nSubStrCnt;
     UINT nDestStrCnt;
     UINT nFirstCharCnt;
 
-    szDestStr = (LPTSTR)malloc(MAX_SAMPLES_STR_SIZE * sizeof(TCHAR));
+    szDestStr = (PWSTR)HeapAlloc(GetProcessHeap(), 0, MAX_SAMPLES_STR_SIZE * sizeof(WCHAR));
+    if (szDestStr == NULL)
+        return NULL;
 
     nDestStrCnt = 0;
     nFirstCharCnt = 0;
 
-    _tcscpy(szDestStr, _T(""));
+    wcscpy(szDestStr, L"");
 
-    while (nFirstCharCnt < _tcslen(szSourceStr))
+    while (nFirstCharCnt < wcslen(szSourceStr))
     {
         if (szSourceStr[nFirstCharCnt] == szTempl[0])
         {
             nSubStrCnt = 0;
-            for (nCharCnt = nFirstCharCnt; nCharCnt < nFirstCharCnt + _tcslen(szTempl); nCharCnt++)
+            for (nCharCnt = nFirstCharCnt; nCharCnt < nFirstCharCnt + wcslen(szTempl); nCharCnt++)
             {
                 if (szSourceStr[nCharCnt] == szTempl[nSubStrCnt])
                 {
@@ -146,11 +150,11 @@ ReplaceSubStr(LPCTSTR szSourceStr,
                     break;
                 }
 
-                if (_tcslen(szTempl) == nSubStrCnt)
+                if (wcslen(szTempl) == nSubStrCnt)
                 {
-                    _tcscat(szDestStr, szStrToReplace);
-                    nDestStrCnt = _tcslen(szDestStr);
-                    nFirstCharCnt += _tcslen(szTempl) - 1;
+                    wcscat(szDestStr, szStrToReplace);
+                    nDestStrCnt = wcslen(szDestStr);
+                    nFirstCharCnt += wcslen(szTempl) - 1;
                     break;
                 }
             }
@@ -158,7 +162,7 @@ ReplaceSubStr(LPCTSTR szSourceStr,
         else
         {
             szDestStr[nDestStrCnt++] = szSourceStr[nFirstCharCnt];
-            szDestStr[nDestStrCnt] = _T('\0');
+            szDestStr[nDestStrCnt] = L'\0';
         }
 
         nFirstCharCnt++;
@@ -168,63 +172,60 @@ ReplaceSubStr(LPCTSTR szSourceStr,
 }
 
 
-static VOID
-InitPropSheetPage(PROPSHEETPAGE *psp, WORD idDlg, DLGPROC DlgProc, PGLOBALDATA pGlobalData)
-{
-  ZeroMemory(psp, sizeof(PROPSHEETPAGE));
-  psp->dwSize = sizeof(PROPSHEETPAGE);
-  psp->dwFlags = PSP_DEFAULT;
-  psp->hInstance = hApplet;
-  psp->pszTemplate = MAKEINTRESOURCE(idDlg);
-  psp->pfnDlgProc = DlgProc;
-  psp->lParam = (LPARAM)pGlobalData;
-}
-
-
-/* Create applets */
-LONG
-APIENTRY
-SetupApplet(HWND hwndDlg, LCID lcid)
+VOID
+GetSelectedComboBoxText(
+    HWND hwndDlg,
+    INT nIdDlgItem,
+    PWSTR Buffer,
+    UINT uSize)
 {
-    PROPSHEETPAGE PsPage[NUM_SHEETS + 1];
-    PROPSHEETHEADER psh;
-    PGLOBALDATA pGlobalData;
-    TCHAR Caption[MAX_STR_SIZE];
-    INT ret;
-
-    LoadString(hApplet, IDS_CUSTOMIZE_TITLE, Caption, sizeof(Caption) / sizeof(TCHAR));
-
-    pGlobalData = (PGLOBALDATA)malloc(sizeof(GLOBALDATA));
-
-    pGlobalData->lcid = lcid;
-
-    ZeroMemory(&psh, sizeof(PROPSHEETHEADER));
-    psh.dwSize = sizeof(PROPSHEETHEADER);
-    psh.dwFlags =  PSH_PROPSHEETPAGE | PSH_USECALLBACK | PSH_PROPTITLE;
-    psh.hwndParent = hwndDlg;
-    psh.hInstance = hApplet;
-    psh.hIcon = LoadIcon(hApplet, MAKEINTRESOURCE(IDC_CPLICON));
-    psh.pszCaption = Caption;
-    psh.nPages = (sizeof(PsPage) / sizeof(PROPSHEETPAGE)) - 1;
-    psh.nStartPage = 0;
-    psh.ppsp = PsPage;
-
-    InitPropSheetPage(&PsPage[0], IDD_NUMBERSPAGE, NumbersPageProc, pGlobalData);
-    InitPropSheetPage(&PsPage[1], IDD_CURRENCYPAGE, CurrencyPageProc, pGlobalData);
-    InitPropSheetPage(&PsPage[2], IDD_TIMEPAGE, TimePageProc, pGlobalData);
-    InitPropSheetPage(&PsPage[3], IDD_DATEPAGE, DatePageProc, pGlobalData);
-
-    if (IsSortPageNeeded(lcid))
+    HWND hChildWnd;
+    PWSTR tmp;
+    INT nIndex;
+    UINT uReqSize;
+
+    /* Get handle to time format control */
+    hChildWnd = GetDlgItem(hwndDlg, nIdDlgItem);
+    if (hChildWnd == NULL)
+        return;
+
+    /* Get index to selected time format */
+    nIndex = SendMessageW(hChildWnd, CB_GETCURSEL, 0, 0);
+    if (nIndex == CB_ERR)
     {
-        psh.nPages++;
-        InitPropSheetPage(&PsPage[4], IDD_SORTPAGE, SortPageProc, pGlobalData);
+        /* No selection? Get content of the edit control */
+        SendMessageW(hChildWnd, WM_GETTEXT, uSize, (LPARAM)Buffer);
     }
+    else
+    {
+        /* Get requested size, including the null terminator;
+         * it shouldn't be required because the previous CB_LIMITTEXT,
+         * but it would be better to check it anyways */
+        uReqSize = SendMessageW(hChildWnd, CB_GETLBTEXTLEN, (WPARAM)nIndex, 0) + 1;
+
+        /* Allocate enough space to be more safe */
+        tmp = (PWSTR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, uReqSize * sizeof(WCHAR));
+        if (tmp != NULL)
+        {
+            /* Get selected time format text */
+            SendMessageW(hChildWnd, CB_GETLBTEXT, (WPARAM)nIndex, (LPARAM)tmp);
+
+            /* Finally, copy the result into the output */
+            wcsncpy(Buffer, tmp, uSize);
 
-    ret = PropertySheet(&psh);
+            HeapFree(GetProcessHeap(), 0, tmp);
+        }
+    }
+}
 
-    free(pGlobalData);
 
-    return (LONG)(ret != -1);
+VOID
+GetSelectedComboBoxIndex(
+    HWND hwndDlg,
+    INT nIdDlgItem,
+    PINT pValue)
+{
+    *pValue = SendDlgItemMessageW(hwndDlg, nIdDlgItem, CB_GETCURSEL, 0, 0);
 }
 
 /* EOF */