Mercurial > dwindows
comparison mac/dw.m @ 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 | 3e9c5bff0a57 |
children | 7cd64f66f1cc |
comparison
equal
deleted
inserted
replaced
2251:15347d28995a | 2252:5dbe950115c1 |
---|---|
8689 } | 8689 } |
8690 | 8690 |
8691 /* Internal function to convert fontname to NSFont */ | 8691 /* Internal function to convert fontname to NSFont */ |
8692 NSFont *_dw_font_by_name(const char *fontname) | 8692 NSFont *_dw_font_by_name(const char *fontname) |
8693 { | 8693 { |
8694 char *fontcopy = strdup(fontname); | 8694 NSFont *font = DWDefaultFont; |
8695 char *name = strchr(fontcopy, '.'); | 8695 |
8696 NSFont *font = nil; | 8696 if(fontname) |
8697 | 8697 { |
8698 if(name) | 8698 char *name = strchr(fontname, '.'); |
8699 { | 8699 |
8700 int size = atoi(fontcopy); | 8700 if(name && (name++)) |
8701 *name = 0; | 8701 { |
8702 name++; | 8702 int size = atoi(fontname); |
8703 font = [NSFont fontWithName:[ NSString stringWithUTF8String:name ] size:(float)size]; | 8703 |
8704 } | 8704 font = [NSFont fontWithName:[ NSString stringWithUTF8String:name ] size:(float)size]; |
8705 free(fontcopy); | 8705 } |
8706 } | |
8706 return font; | 8707 return font; |
8707 } | 8708 } |
8708 | 8709 |
8709 /* | 8710 /* |
8710 * Create a bitmap object to be packed. | 8711 * Create a bitmap object to be packed. |
10291 * handle: The window (widget) handle. | 10292 * handle: The window (widget) handle. |
10292 * fontname: Name and size of the font in the form "size.fontname" | 10293 * fontname: Name and size of the font in the form "size.fontname" |
10293 */ | 10294 */ |
10294 int API dw_window_set_font(HWND handle, const char *fontname) | 10295 int API dw_window_set_font(HWND handle, const char *fontname) |
10295 { | 10296 { |
10296 NSFont *font = _dw_font_by_name(fontname); | 10297 NSFont *font = fontname ? _dw_font_by_name(fontname) : |
10298 (DWDefaultFont ? DWDefaultFont : [NSFont systemFontOfSize:[NSFont smallSystemFontSize]]); | |
10297 | 10299 |
10298 if(font) | 10300 if(font) |
10299 { | 10301 { |
10300 id object = _text_handle(handle); | 10302 id object = _text_handle(handle); |
10301 if([object window]) | 10303 if([object window]) |
10305 [object unlockFocus]; | 10307 [object unlockFocus]; |
10306 } | 10308 } |
10307 if([object isMemberOfClass:[DWGroupBox class]]) | 10309 if([object isMemberOfClass:[DWGroupBox class]]) |
10308 { | 10310 { |
10309 [object setTitleFont:font]; | 10311 [object setTitleFont:font]; |
10312 } | |
10313 else if([object isMemberOfClass:[DWMLE class]]) | |
10314 { | |
10315 DWMLE *mle = object; | |
10316 | |
10317 [[mle textStorage] setFont:font]; | |
10310 } | 10318 } |
10311 else if([object isKindOfClass:[NSControl class]]) | 10319 else if([object isKindOfClass:[NSControl class]]) |
10312 { | 10320 { |
10313 [object setFont:font]; | 10321 [object setFont:font]; |
10314 [[object cell] setFont:font]; | 10322 [[object cell] setFont:font]; |
12394 * fontname: Font name in Dynamic Windows format. | 12402 * fontname: Font name in Dynamic Windows format. |
12395 */ | 12403 */ |
12396 void API dw_font_set_default(const char *fontname) | 12404 void API dw_font_set_default(const char *fontname) |
12397 { | 12405 { |
12398 NSFont *oldfont = DWDefaultFont; | 12406 NSFont *oldfont = DWDefaultFont; |
12399 DWDefaultFont = _dw_font_by_name(fontname); | 12407 DWDefaultFont = nil; |
12400 [DWDefaultFont retain]; | 12408 if(fontname) |
12409 { | |
12410 DWDefaultFont = _dw_font_by_name(fontname); | |
12411 [DWDefaultFont retain]; | |
12412 } | |
12401 [oldfont release]; | 12413 [oldfont release]; |
12402 } | 12414 } |
12403 | 12415 |
12404 /* If DWApp is uninitialized, initialize it */ | 12416 /* If DWApp is uninitialized, initialize it */ |
12405 void _dw_app_init(void) | 12417 void _dw_app_init(void) |