changeset 2250:a691de150bef

Win: Fix crash in dw_window_set_font() with a NULL font name on Rich Edit MLEs.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Sun, 24 Jan 2021 20:20:06 +0000
parents 14a1e07e8f4e
children 15347d28995a
files win/dw.c
diffstat 1 files changed, 46 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/win/dw.c	Sun Jan 24 19:38:54 2021 +0000
+++ b/win/dw.c	Sun Jan 24 20:20:06 2021 +0000
@@ -42,6 +42,7 @@
 
 #ifdef RICHEDIT
 int _DW_MLE_RICH_EDIT = DW_FEATURE_UNSUPPORTED;
+static CHARFORMAT _dw_default_charformat = {0};
 #endif
 
 #define STATICCLASSNAME TEXT("STATIC")
@@ -5688,30 +5689,38 @@
     if (_tcsnicmp(tmpbuf, RICHEDIT_CLASS, _tcslen(RICHEDIT_CLASS)+1) == 0 ||
         _tcsnicmp(tmpbuf, MSFTEDIT_CLASS, _tcslen(MSFTEDIT_CLASS)+1) == 0)
     {
-        CHARFORMAT cf;
-        char  *Italic, *Bold, *myFontName = strchr(fontname, '.');
-        int size = atoi(fontname);
-
-        /* If we found a '.' use the location after the . */
-        if(myFontName)
-           myFontName = _strdup(++myFontName);
-        else /* Otherwise use the whole fontname and default size of 9 */
-           myFontName = _strdup(fontname);
-
-        if((Italic = strstr(myFontName, " Italic")))
-          *Italic = 0;
-        if((Bold = strstr(myFontName, " Bold")))
-          *Bold = 0;
-
-        SendMessage(handle, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM)&cf);
-        cf.cbSize = sizeof(cf);
-        cf.dwMask = CFM_FACE | CFM_ITALIC | CFM_BOLD;
-        cf.dwEffects = (Italic ? CFE_ITALIC : 0) | (Bold ? CFE_BOLD : 0);
-        _tcsncpy(cf.szFaceName, UTF8toWide(myFontName), (sizeof(cf.szFaceName)/sizeof(TCHAR))-1);
-        SendMessage(handle, EM_SETCHARFORMAT, SCF_DEFAULT, (LPARAM)&cf);
-
-        SendMessage(handle, EM_SETFONTSIZE , (WPARAM)(size ? size : 9), 0);
-        free(myFontName);
+       if(fontname)
+       {
+            CHARFORMAT cf = {0};
+            char  *Italic, *Bold, *myFontName = strchr(fontname, '.');
+            int size = atoi(fontname);
+
+            /* If we found a '.' use the location after the . */
+            if(myFontName)
+               myFontName = _strdup(++myFontName);
+            else /* Otherwise use the whole fontname and default size of 9 */
+               myFontName = _strdup(fontname);
+
+            if((Italic = strstr(myFontName, " Italic")))
+              *Italic = 0;
+            if((Bold = strstr(myFontName, " Bold")))
+              *Bold = 0;
+
+            cf.cbSize = sizeof(cf);
+            SendMessage(handle, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM)&cf);
+            cf.dwMask = CFM_FACE | CFM_ITALIC | CFM_BOLD;
+            cf.dwEffects = (Italic ? CFE_ITALIC : 0) | (Bold ? CFE_BOLD : 0);
+            _tcsncpy(cf.szFaceName, UTF8toWide(myFontName), (sizeof(cf.szFaceName)/sizeof(TCHAR))-1);
+            SendMessage(handle, EM_SETCHARFORMAT, SCF_DEFAULT, (LPARAM)&cf);
+
+            SendMessage(handle, EM_SETFONTSIZE , (WPARAM)(size ? size : 9), 0);
+            free(myFontName);
+       }
+       else
+       {
+           SendMessage(handle, EM_SETCHARFORMAT, SCF_DEFAULT, (LPARAM)&_dw_default_charformat);
+           SendMessage(handle, EM_SETFONTSIZE , (WPARAM)9, 0);
+       }
     }
     else
 #endif
@@ -7033,7 +7042,14 @@
 
 #ifdef RICHEDIT
    if(_DW_MLE_RICH_EDIT == DW_FEATURE_ENABLED && (hrichedit || hmsftedit))
+   {
       cinfo->cinfo.pOldProc = SubclassWindow(tmp, _richeditwndproc);
+      /* Save the default RichEdit font for later use */
+      _dw_default_charformat.cbSize = sizeof(CHARFORMAT);
+      SendMessage(tmp, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM)&_dw_default_charformat);
+      /* Make sure we set the mask we will use for reseting the MLE later */
+      _dw_default_charformat.dwMask = CFM_FACE | CFM_ITALIC | CFM_BOLD;
+   }
    else
 #endif
    cinfo->cinfo.pOldProc = SubclassWindow(tmp, _simplewndproc);
@@ -9431,12 +9447,14 @@
  */
 void API dw_mle_set_word_wrap(HWND handle, int state)
 {
+#ifdef RICHEDIT
    /* If it is a rich edit control use the rich edit message */
    if(_DW_MLE_RICH_EDIT == DW_FEATURE_ENABLED && (hrichedit || hmsftedit))
    {
       SendMessage(handle, EM_SHOWSCROLLBAR, (WPARAM)SB_HORZ, (LPARAM)(state ? FALSE : TRUE));
       SendMessage(handle, EM_SETTARGETDEVICE, 0, state ? 0 : 1);
    }
+#endif   
 }
 
 /*
@@ -9458,8 +9476,10 @@
 void API dw_mle_set_cursor(HWND handle, int point)
 {
    SendMessage(handle, EM_SETSEL, (WPARAM)point, (LPARAM)point);
+#ifdef RICHEDIT
    if(_DW_MLE_RICH_EDIT == DW_FEATURE_ENABLED && (hrichedit || hmsftedit))
       SendMessage(handle, EM_HIDESELECTION, 0, 0);
+#endif
    SendMessage(handle, EM_SCROLLCARET, 0, 0);
 }
 
@@ -9502,8 +9522,10 @@
    if(retval)
    {
       SendMessage(handle, EM_SETSEL, (WPARAM)retval - textlen, (LPARAM)retval);
+#ifdef RICHEDIT
       if(_DW_MLE_RICH_EDIT == DW_FEATURE_ENABLED && (hrichedit || hmsftedit))
          SendMessage(handle, EM_HIDESELECTION, 0, 0);
+#endif
       SendMessage(handle, EM_SCROLLCARET, 0, 0);
    }