changeset 2252:5dbe950115c1

Mac: Fix crash in dw_window_set_font() with NULL font name. Not sure if I am getting the default font correctly, may need to revisit. Also switched Helv to Helvetica so it works on Mac, might need an #ifdef if this doesn't work on other platforms.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Sun, 24 Jan 2021 23:17:02 +0000
parents 15347d28995a
children 08369a79850f
files dwtest.c mac/dw.m
diffstat 2 files changed, 29 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/dwtest.c	Sun Jan 24 21:00:15 2021 +0000
+++ b/dwtest.c	Sun Jan 24 23:17:02 2021 +0000
@@ -1388,11 +1388,11 @@
         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_box_pack_start(hbox, fontsize, 50, -1, TRUE, FALSE, 1);
+        dw_box_pack_start(hbox, fontsize, 50, -1, FALSE, FALSE, 1);
         dw_spinbutton_set_limits(fontsize, 100, 5);
         fontname = dw_combobox_new("Default", 0);
         dw_listbox_append(fontname, "Default");
-        dw_listbox_append(fontname, "Helv");
+        dw_listbox_append(fontname, "Helvetica");
         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);
--- a/mac/dw.m	Sun Jan 24 21:00:15 2021 +0000
+++ b/mac/dw.m	Sun Jan 24 23:17:02 2021 +0000
@@ -8691,18 +8691,19 @@
 /* Internal function to convert fontname to NSFont */
 NSFont *_dw_font_by_name(const char *fontname)
 {
-    char *fontcopy = strdup(fontname);
-    char *name = strchr(fontcopy, '.');
-    NSFont *font = nil;
-
-    if(name)
-    {
-        int size = atoi(fontcopy);
-        *name = 0;
-        name++;
-        font = [NSFont fontWithName:[ NSString stringWithUTF8String:name ] size:(float)size];
-    }
-    free(fontcopy);
+    NSFont *font = DWDefaultFont;
+    
+    if(fontname)
+    {
+        char *name = strchr(fontname, '.');
+
+        if(name && (name++))
+        {
+            int size = atoi(fontname);
+            
+            font = [NSFont fontWithName:[ NSString stringWithUTF8String:name ] size:(float)size];
+        }
+    }
     return font;
 }
 
@@ -10293,7 +10294,8 @@
  */
 int API dw_window_set_font(HWND handle, const char *fontname)
 {
-    NSFont *font = _dw_font_by_name(fontname);
+    NSFont *font = fontname ? _dw_font_by_name(fontname) :
+    (DWDefaultFont ? DWDefaultFont : [NSFont systemFontOfSize:[NSFont smallSystemFontSize]]);
 
     if(font)
     {
@@ -10308,6 +10310,12 @@
         {
             [object setTitleFont:font];
         }
+        else if([object isMemberOfClass:[DWMLE class]])
+        {
+            DWMLE *mle = object;
+            
+            [[mle textStorage] setFont:font];
+        }
         else if([object isKindOfClass:[NSControl class]])
         {
             [object setFont:font];
@@ -12396,8 +12404,12 @@
 void API dw_font_set_default(const char *fontname)
 {
     NSFont *oldfont = DWDefaultFont;
-    DWDefaultFont = _dw_font_by_name(fontname);
-    [DWDefaultFont retain];
+    DWDefaultFont = nil;
+    if(fontname)
+    {
+        DWDefaultFont = _dw_font_by_name(fontname);
+        [DWDefaultFont retain];
+    }
     [oldfont release];
 }