changeset 2247:703023e1a644

Win: Implement dw_window_set_font() support for Rich Edit based MLEs. Also add font size and name support for changing MLE in dwtest.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Sat, 23 Jan 2021 00:48:12 +0000
parents 471cdeaef7ac
children 4fb54b9fcf78
files dwtest.c win/dw.c
diffstat 2 files changed, 100 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/dwtest.c	Wed Jan 20 20:57:33 2021 +0000
+++ b/dwtest.c	Sat Jan 23 00:48:12 2021 +0000
@@ -1309,6 +1309,44 @@
     return 0;
 }
 
+void mle_font_set(HWND mle, int fontsize, char *fontname)
+{
+    char font[101] = {0};
+
+    if(fontname)
+        snprintf(font, 100, "%d.%s", fontsize, fontname);
+    dw_window_set_font(mle, fontname ? font : NULL);
+}
+
+int DWSIGNAL mle_fontname_cb(HWND hwnd, int pos, void *data)
+{
+    HWND hbox = (HWND)data;
+    HWND fontsize = (HWND)dw_window_get_data(hbox, "fontsize");
+    HWND fontname = (HWND)dw_window_get_data(hbox, "fontname");
+    char font[101] = {0};
+
+    dw_listbox_get_text(fontname, pos, font, 100);
+    mle_font_set(container_mle, (int)dw_spinbutton_get_pos(fontsize), strcmp(font, "Default") == 0 ? NULL : font);
+    return 0;
+}
+
+int mle_fontsize_cb(HWND hwnd, int size, void *data)
+{
+    HWND hbox = (HWND)data;
+    HWND fontsize = (HWND)dw_window_get_data(hbox, "fontsize");
+    HWND fontname = (HWND)dw_window_get_data(hbox, "fontname");
+    char *font = dw_window_get_text(fontname);
+
+    if(font)
+    {
+        mle_font_set(container_mle, size, strcmp(font, "Default") == 0 ? NULL : font);
+        dw_free(font);
+    }
+    else
+        mle_font_set(container_mle, size, NULL);
+    return 0;
+}
+
 void container_add(void)
 {
     char *titles[4];
@@ -1322,7 +1360,7 @@
     CDATE date;
     unsigned long size, newpoint;
     HICN thisicon;
-    HWND checkbox, mlefore, mleback, hbox;
+    HWND checkbox, mlefore, mleback, fontsize, fontname, hbox;
 
     /* create a box to pack into the notebook page */
     containerbox = dw_box_new(DW_HORZ, 2);
@@ -1347,10 +1385,23 @@
         mleback = color_combobox();
         dw_box_pack_start(hbox, mleback, 150, -1, TRUE, FALSE, 1);
         dw_checkbox_set(checkbox, TRUE);
-        dw_box_pack_start(notebookbox4, hbox, -1, -1, FALSE, FALSE, 1);
+        text = dw_text_new("Font:", 0);
+        dw_window_set_style(text, DW_DT_VCENTER, DW_DT_VCENTER);
+        dw_box_pack_start(hbox, text, -1, -1, FALSE, TRUE, 1);
+        fontsize = dw_spinbutton_new("9", 0);
+        dw_spinbutton_set_limits(fontsize, 5, 100);
+        dw_box_pack_start(hbox, fontname, 50, -1, TRUE, FALSE, 1);
+        fontname = dw_combobox_new("Default", 0);
+        dw_listbox_append(fontname, "Default");
+        dw_listbox_append(fontname, "Helv");
+        dw_listbox_append(fontname, "Arial");
+        dw_box_pack_start(hbox, fontname, 150, -1, TRUE, FALSE, 1);
+        dw_box_pack_start(notebookbox4, hbox, -1, -1, TRUE, FALSE, 1);
 
         dw_window_set_data(hbox, "mlefore", DW_POINTER(mlefore));
         dw_window_set_data(hbox, "mleback", DW_POINTER(mleback));
+        dw_window_set_data(hbox, "fontsize", DW_POINTER(fontsize));
+        dw_window_set_data(hbox, "fontname", DW_POINTER(fontname));
     }
 
     /* now a container area under this box */
@@ -1437,6 +1488,8 @@
     dw_signal_connect(checkbox, DW_SIGNAL_CLICKED, DW_SIGNAL_FUNC(word_wrap_click_cb), DW_POINTER(container_mle));
     dw_signal_connect(mlefore, DW_SIGNAL_LIST_SELECT, DW_SIGNAL_FUNC(mle_color_cb), DW_POINTER(hbox));
     dw_signal_connect(mleback, DW_SIGNAL_LIST_SELECT, DW_SIGNAL_FUNC(mle_color_cb), DW_POINTER(hbox));
+    dw_signal_connect(fontname, DW_SIGNAL_LIST_SELECT, DW_SIGNAL_FUNC(mle_fontname_cb), DW_POINTER(hbox));
+    dw_signal_connect(fontsize, DW_SIGNAL_VALUE_CHANGED, DW_SIGNAL_FUNC(mle_fontsize_cb), DW_POINTER(hbox));
 }
 
 /* Beep every second */
--- a/win/dw.c	Wed Jan 20 20:57:33 2021 +0000
+++ b/win/dw.c	Sat Jan 23 00:48:12 2021 +0000
@@ -5670,7 +5670,7 @@
  */
 int API dw_window_set_font(HWND handle, const char *fontname)
 {
-    HFONT hfont, oldfont;
+    HFONT hfont = NULL, oldfont = NULL;
     ColorInfo *cinfo = _dw_window_get_cinfo(handle);
     TCHAR tmpbuf[100] = {0};
 
@@ -5684,19 +5684,54 @@
             handle = thisbox->grouphwnd;
         }
     }
-
-    /* This needs to be after we get the correct handle */
-    oldfont = (HFONT)SendMessage(handle, WM_GETFONT, 0, 0);
-    hfont = _acquire_font(handle, fontname);
-
-    if(hfont && fontname)
+#ifdef RICHEDIT
+    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);
+    }
+    else
+#endif
+    {
+        /* This needs to be after we get the correct handle */
+        oldfont = (HFONT)SendMessage(handle, WM_GETFONT, 0, 0);
+        hfont = _acquire_font(handle, fontname);
+    }
+
+    if(fontname)
     {
         if(cinfo || (cinfo = _dw_window_new_cinfo(handle, TRUE)))
         {
             strncpy(cinfo->fontname, fontname, 127);
-            if(!oldfont)
-                oldfont = cinfo->hfont;
-            cinfo->hfont = hfont;
+            if(hfont)
+            {
+                if(!oldfont)
+                    oldfont = cinfo->hfont;
+                cinfo->hfont = hfont;
+            }
         }
     }
     /* If we changed the font... */