comparison mac/dw.m @ 2002:a2931caa3422

Fix setting/getting dates in dw_calendar under macos. Only tested on Mojave. Sorry about the white space changes.
author mhessling@81767d24-ef19-dc11-ae90-00e081727c95
date Mon, 04 Nov 2019 01:15:03 +0000
parents 289a8ca4855b
children ebbdb2120659
comparison
equal deleted inserted replaced
2001:f7acca46f795 2002:a2931caa3422
417 /* Returns TRUE of Mojave or later is in Dark Mode */ 417 /* Returns TRUE of Mojave or later is in Dark Mode */
418 BOOL _is_dark(id object) 418 BOOL _is_dark(id object)
419 { 419 {
420 #ifdef BUILDING_FOR_MOJAVE 420 #ifdef BUILDING_FOR_MOJAVE
421 NSAppearance *appearance = [object effectiveAppearance]; 421 NSAppearance *appearance = [object effectiveAppearance];
422 422
423 if(@available(macOS 10.14, *)) 423 if(@available(macOS 10.14, *))
424 { 424 {
425 NSAppearanceName basicAppearance = [appearance bestMatchFromAppearancesWithNames:@[ 425 NSAppearanceName basicAppearance = [appearance bestMatchFromAppearancesWithNames:@[
426 NSAppearanceNameAqua, 426 NSAppearanceNameAqua,
427 NSAppearanceNameDarkAqua]]; 427 NSAppearanceNameDarkAqua]];
1183 DWTID curr = pthread_self(); 1183 DWTID curr = pthread_self();
1184 1184
1185 if(DWThread == (DWTID)-1 || DWThread == curr) 1185 if(DWThread == (DWTID)-1 || DWThread == curr)
1186 { 1186 {
1187 DWIMP imp = (DWIMP)[self methodForSelector:sel]; 1187 DWIMP imp = (DWIMP)[self methodForSelector:sel];
1188 1188
1189 if(imp) 1189 if(imp)
1190 imp(self, sel, param); 1190 imp(self, sel, param);
1191 } 1191 }
1192 else 1192 else
1193 [self performSelectorOnMainThread:sel withObject:param waitUntilDone:YES]; 1193 [self performSelectorOnMainThread:sel withObject:param waitUntilDone:YES];
1197 { 1197 {
1198 NSValue *bi = (NSValue *)param; 1198 NSValue *bi = (NSValue *)param;
1199 DWBitBlt *bltinfo = (DWBitBlt *)[bi pointerValue]; 1199 DWBitBlt *bltinfo = (DWBitBlt *)[bi pointerValue];
1200 id bltdest = bltinfo->dest; 1200 id bltdest = bltinfo->dest;
1201 id bltsrc = bltinfo->src; 1201 id bltsrc = bltinfo->src;
1202 1202
1203 #ifdef BUILDING_FOR_MOJAVE 1203 #ifdef BUILDING_FOR_MOJAVE
1204 if([bltdest isMemberOfClass:[DWRender class]]) 1204 if([bltdest isMemberOfClass:[DWRender class]])
1205 { 1205 {
1206 DWRender *render = bltdest; 1206 DWRender *render = bltdest;
1207 1207
1208 bltdest = [render cachedDrawingRep]; 1208 bltdest = [render cachedDrawingRep];
1209 } 1209 }
1210 #endif 1210 #endif
1211 if([bltdest isMemberOfClass:[NSBitmapImageRep class]]) 1211 if([bltdest isMemberOfClass:[NSBitmapImageRep class]])
1212 { 1212 {
1229 { 1229 {
1230 NSBitmapImageRep *rep = bltsrc; 1230 NSBitmapImageRep *rep = bltsrc;
1231 NSImage *image = [NSImage alloc]; 1231 NSImage *image = [NSImage alloc];
1232 SEL siwc = NSSelectorFromString(@"initWithCGImage"); 1232 SEL siwc = NSSelectorFromString(@"initWithCGImage");
1233 NSCompositingOperation op = DWCompositingOperationSourceOver; 1233 NSCompositingOperation op = DWCompositingOperationSourceOver;
1234 1234
1235 if([image respondsToSelector:siwc]) 1235 if([image respondsToSelector:siwc])
1236 { 1236 {
1237 DWIMP iiwc = (DWIMP)[image methodForSelector:siwc]; 1237 DWIMP iiwc = (DWIMP)[image methodForSelector:siwc];
1238 image = iiwc(image, siwc, [rep CGImage], NSZeroSize); 1238 image = iiwc(image, siwc, [rep CGImage], NSZeroSize);
1239 } 1239 }
1272 -(void)doFlush:(id)param 1272 -(void)doFlush:(id)param
1273 { 1273 {
1274 #ifdef BUILDING_FOR_MOJAVE 1274 #ifdef BUILDING_FOR_MOJAVE
1275 NSEnumerator *enumerator = [_DWDirtyDrawables objectEnumerator]; 1275 NSEnumerator *enumerator = [_DWDirtyDrawables objectEnumerator];
1276 DWRender *rend; 1276 DWRender *rend;
1277 1277
1278 while (rend = [enumerator nextObject]) 1278 while (rend = [enumerator nextObject])
1279 rend.needsDisplay = YES; 1279 rend.needsDisplay = YES;
1280 [_DWDirtyDrawables removeAllObjects]; 1280 [_DWDirtyDrawables removeAllObjects];
1281 #else 1281 #else
1282 if(_DWLastDrawable) 1282 if(_DWLastDrawable)
1291 -(void)doWindowFunc:(id)param 1291 -(void)doWindowFunc:(id)param
1292 { 1292 {
1293 NSValue *v = (NSValue *)param; 1293 NSValue *v = (NSValue *)param;
1294 void **params = (void **)[v pointerValue]; 1294 void **params = (void **)[v pointerValue];
1295 void (* windowfunc)(void *); 1295 void (* windowfunc)(void *);
1296 1296
1297 if(params) 1297 if(params)
1298 { 1298 {
1299 windowfunc = params[0]; 1299 windowfunc = params[0];
1300 if(windowfunc) 1300 if(windowfunc)
1301 { 1301 {
3244 /* The 10.14 appkit warns this property does nothing... not sure 3244 /* The 10.14 appkit warns this property does nothing... not sure
3245 * how long that has been the case but just removing it for Mojave. 3245 * how long that has been the case but just removing it for Mojave.
3246 */ 3246 */
3247 #ifndef BUILDING_FOR_MOJAVE 3247 #ifndef BUILDING_FOR_MOJAVE
3248 NSWindow *window = [render window]; 3248 NSWindow *window = [render window];
3249 3249
3250 if([window preferredBackingLocation] != NSWindowBackingLocationVideoMemory) 3250 if([window preferredBackingLocation] != NSWindowBackingLocationVideoMemory)
3251 { 3251 {
3252 [window setPreferredBackingLocation:NSWindowBackingLocationVideoMemory]; 3252 [window setPreferredBackingLocation:NSWindowBackingLocationVideoMemory];
3253 } 3253 }
3254 #endif 3254 #endif
3255 3255
3256 /* Eliminate duplicate configure requests */ 3256 /* Eliminate duplicate configure requests */
3257 if(oldsize.width != newsize.width || oldsize.height != newsize.height) 3257 if(oldsize.width != newsize.width || oldsize.height != newsize.height)
3258 { 3258 {
3259 if(newsize.width > 0 && newsize.height > 0) 3259 if(newsize.width > 0 && newsize.height > 0)
3260 { 3260 {
3892 3892
3893 if(flags & DW_MB_ERROR) 3893 if(flags & DW_MB_ERROR)
3894 mstyle = DWAlertStyleCritical; 3894 mstyle = DWAlertStyleCritical;
3895 else if(flags & DW_MB_INFORMATION) 3895 else if(flags & DW_MB_INFORMATION)
3896 mstyle = DWAlertStyleInformational; 3896 mstyle = DWAlertStyleInformational;
3897 3897
3898 params = [NSMutableArray arrayWithObjects:mtitle, mtext, [NSNumber numberWithInteger:mstyle], button1, button2, button3, nil]; 3898 params = [NSMutableArray arrayWithObjects:mtitle, mtext, [NSNumber numberWithInteger:mstyle], button1, button2, button3, nil];
3899 [DWObj safeCall:@selector(messageBox:) withObject:params]; 3899 [DWObj safeCall:@selector(messageBox:) withObject:params];
3900 iResponse = [[params lastObject] integerValue]; 3900 iResponse = [[params lastObject] integerValue];
3901 3901
3902 switch(iResponse) 3902 switch(iResponse)
6335 return [NSGraphicsContext graphicsContextWithCGContext:[[NSGraphicsContext graphicsContextWithBitmapImageRep:image] CGContext] flipped:YES]; 6335 return [NSGraphicsContext graphicsContextWithCGContext:[[NSGraphicsContext graphicsContextWithBitmapImageRep:image] CGContext] flipped:YES];
6336 #else 6336 #else
6337 return [NSGraphicsContext graphicsContextWithGraphicsPort:[[NSGraphicsContext graphicsContextWithBitmapImageRep:image] graphicsPort] flipped:YES]; 6337 return [NSGraphicsContext graphicsContextWithGraphicsPort:[[NSGraphicsContext graphicsContextWithBitmapImageRep:image] graphicsPort] flipped:YES];
6338 #endif 6338 #endif
6339 } 6339 }
6340 6340
6341 /* Draw a point on a window (preferably a render window). 6341 /* Draw a point on a window (preferably a render window).
6342 * Parameters: 6342 * Parameters:
6343 * handle: Handle to the window. 6343 * handle: Handle to the window.
6344 * pixmap: Handle to the pixmap. (choose only one of these) 6344 * pixmap: Handle to the pixmap. (choose only one of these)
6345 * x: X coordinate. 6345 * x: X coordinate.
6353 DW_FUNCTION_INIT; 6353 DW_FUNCTION_INIT;
6354 DW_LOCAL_POOL_IN; 6354 DW_LOCAL_POOL_IN;
6355 id image = handle; 6355 id image = handle;
6356 NSBitmapImageRep *bi = nil; 6356 NSBitmapImageRep *bi = nil;
6357 bool bCanDraw = YES; 6357 bool bCanDraw = YES;
6358 6358
6359 if(pixmap) 6359 if(pixmap)
6360 bi = image = (id)pixmap->image; 6360 bi = image = (id)pixmap->image;
6361 else 6361 else
6362 { 6362 {
6363 #ifdef BUILDING_FOR_MOJAVE 6363 #ifdef BUILDING_FOR_MOJAVE
6364 if([image isMemberOfClass:[DWRender class]]) 6364 if([image isMemberOfClass:[DWRender class]])
6365 { 6365 {
6366 DWRender *render = image; 6366 DWRender *render = image;
6367 6367
6368 bi = [render cachedDrawingRep]; 6368 bi = [render cachedDrawingRep];
6369 } 6369 }
6370 #else 6370 #else
6371 if((bCanDraw = [image lockFocusIfCanDraw]) == YES) 6371 if((bCanDraw = [image lockFocusIfCanDraw]) == YES)
6372 _DWLastDrawable = handle; 6372 _DWLastDrawable = handle;
6423 { 6423 {
6424 #ifdef BUILDING_FOR_MOJAVE 6424 #ifdef BUILDING_FOR_MOJAVE
6425 if([image isMemberOfClass:[DWRender class]]) 6425 if([image isMemberOfClass:[DWRender class]])
6426 { 6426 {
6427 DWRender *render = image; 6427 DWRender *render = image;
6428 6428
6429 bi = [render cachedDrawingRep]; 6429 bi = [render cachedDrawingRep];
6430 } 6430 }
6431 #else 6431 #else
6432 if((bCanDraw = [image lockFocusIfCanDraw]) == YES) 6432 if((bCanDraw = [image lockFocusIfCanDraw]) == YES)
6433 _DWLastDrawable = handle; 6433 _DWLastDrawable = handle;
6478 NSString *nstr = [ NSString stringWithUTF8String:text ]; 6478 NSString *nstr = [ NSString stringWithUTF8String:text ];
6479 NSBitmapImageRep *bi = nil; 6479 NSBitmapImageRep *bi = nil;
6480 NSFont *font = nil; 6480 NSFont *font = nil;
6481 DWRender *render; 6481 DWRender *render;
6482 bool bCanDraw = YES; 6482 bool bCanDraw = YES;
6483 6483
6484 if(pixmap) 6484 if(pixmap)
6485 { 6485 {
6486 bi = image = (id)pixmap->image; 6486 bi = image = (id)pixmap->image;
6487 font = pixmap->font; 6487 font = pixmap->font;
6488 render = pixmap->handle; 6488 render = pixmap->handle;
6617 DW_LOCAL_POOL_IN; 6617 DW_LOCAL_POOL_IN;
6618 id image = handle; 6618 id image = handle;
6619 NSBitmapImageRep *bi = nil; 6619 NSBitmapImageRep *bi = nil;
6620 bool bCanDraw = YES; 6620 bool bCanDraw = YES;
6621 int z; 6621 int z;
6622 6622
6623 if(pixmap) 6623 if(pixmap)
6624 bi = image = (id)pixmap->image; 6624 bi = image = (id)pixmap->image;
6625 else 6625 else
6626 { 6626 {
6627 #ifdef BUILDING_FOR_MOJAVE 6627 #ifdef BUILDING_FOR_MOJAVE
6628 if([image isMemberOfClass:[DWRender class]]) 6628 if([image isMemberOfClass:[DWRender class]])
6629 { 6629 {
6630 DWRender *render = image; 6630 DWRender *render = image;
6631 6631
6632 bi = [render cachedDrawingRep]; 6632 bi = [render cachedDrawingRep];
6633 } 6633 }
6634 #else 6634 #else
6635 if((bCanDraw = [image lockFocusIfCanDraw]) == YES) 6635 if((bCanDraw = [image lockFocusIfCanDraw]) == YES)
6636 { 6636 {
6662 { 6662 {
6663 [aPath fill]; 6663 [aPath fill];
6664 } 6664 }
6665 [aPath stroke]; 6665 [aPath stroke];
6666 } 6666 }
6667 6667
6668 if(bi) 6668 if(bi)
6669 [NSGraphicsContext restoreGraphicsState]; 6669 [NSGraphicsContext restoreGraphicsState];
6670 #ifndef BUILDING_FOR_MOJAVE 6670 #ifndef BUILDING_FOR_MOJAVE
6671 if(bCanDraw == YES && !pixmap) 6671 if(bCanDraw == YES && !pixmap)
6672 [image unlockFocus]; 6672 [image unlockFocus];
6693 DW_FUNCTION_INIT; 6693 DW_FUNCTION_INIT;
6694 DW_LOCAL_POOL_IN; 6694 DW_LOCAL_POOL_IN;
6695 id image = handle; 6695 id image = handle;
6696 NSBitmapImageRep *bi = nil; 6696 NSBitmapImageRep *bi = nil;
6697 bool bCanDraw = YES; 6697 bool bCanDraw = YES;
6698 6698
6699 if(pixmap) 6699 if(pixmap)
6700 bi = image = (id)pixmap->image; 6700 bi = image = (id)pixmap->image;
6701 else 6701 else
6702 { 6702 {
6703 #ifdef BUILDING_FOR_MOJAVE 6703 #ifdef BUILDING_FOR_MOJAVE
6704 if([image isMemberOfClass:[DWRender class]]) 6704 if([image isMemberOfClass:[DWRender class]])
6705 { 6705 {
6706 DWRender *render = image; 6706 DWRender *render = image;
6707 6707
6708 bi = [render cachedDrawingRep]; 6708 bi = [render cachedDrawingRep];
6709 } 6709 }
6710 #else 6710 #else
6711 if((bCanDraw = [image lockFocusIfCanDraw]) == YES) 6711 if((bCanDraw = [image lockFocusIfCanDraw]) == YES)
6712 { 6712 {
6719 { 6719 {
6720 id gc = _create_gc(bi, flags & DW_DRAW_NOAA ? NO : YES); 6720 id gc = _create_gc(bi, flags & DW_DRAW_NOAA ? NO : YES);
6721 [NSGraphicsContext saveGraphicsState]; 6721 [NSGraphicsContext saveGraphicsState];
6722 [NSGraphicsContext setCurrentContext:gc]; 6722 [NSGraphicsContext setCurrentContext:gc];
6723 } 6723 }
6724 6724
6725 if(bCanDraw == YES) 6725 if(bCanDraw == YES)
6726 { 6726 {
6727 NSColor *color = pthread_getspecific(_dw_fg_color_key); 6727 NSColor *color = pthread_getspecific(_dw_fg_color_key);
6728 [color set]; 6728 [color set];
6729 6729
6730 if(flags & DW_DRAW_FILL) 6730 if(flags & DW_DRAW_FILL)
6731 [NSBezierPath fillRect:NSMakeRect(x, y, width, height)]; 6731 [NSBezierPath fillRect:NSMakeRect(x, y, width, height)];
6732 else 6732 else
6733 [NSBezierPath strokeRect:NSMakeRect(x, y, width, height)]; 6733 [NSBezierPath strokeRect:NSMakeRect(x, y, width, height)];
6734 } 6734 }
6735 6735
6736 if(bi) 6736 if(bi)
6737 [NSGraphicsContext restoreGraphicsState]; 6737 [NSGraphicsContext restoreGraphicsState];
6738 #ifndef BUILDING_FOR_MOJAVE 6738 #ifndef BUILDING_FOR_MOJAVE
6739 if(bCanDraw == YES && !pixmap) 6739 if(bCanDraw == YES && !pixmap)
6740 [image unlockFocus]; 6740 [image unlockFocus];
6764 DW_FUNCTION_INIT; 6764 DW_FUNCTION_INIT;
6765 DW_LOCAL_POOL_IN; 6765 DW_LOCAL_POOL_IN;
6766 id image = handle; 6766 id image = handle;
6767 NSBitmapImageRep *bi = nil; 6767 NSBitmapImageRep *bi = nil;
6768 bool bCanDraw = YES; 6768 bool bCanDraw = YES;
6769 6769
6770 if(pixmap) 6770 if(pixmap)
6771 bi = image = (id)pixmap->image; 6771 bi = image = (id)pixmap->image;
6772 else 6772 else
6773 { 6773 {
6774 #ifdef BUILDING_FOR_MOJAVE 6774 #ifdef BUILDING_FOR_MOJAVE
6775 if([image isMemberOfClass:[DWRender class]]) 6775 if([image isMemberOfClass:[DWRender class]])
6776 { 6776 {
6777 DWRender *render = image; 6777 DWRender *render = image;
6778 6778
6779 bi = [render cachedDrawingRep]; 6779 bi = [render cachedDrawingRep];
6780 } 6780 }
6781 #else 6781 #else
6782 if((bCanDraw = [image lockFocusIfCanDraw]) == YES) 6782 if((bCanDraw = [image lockFocusIfCanDraw]) == YES)
6783 { 6783 {
6790 { 6790 {
6791 id gc = _create_gc(bi, flags & DW_DRAW_NOAA ? NO : YES); 6791 id gc = _create_gc(bi, flags & DW_DRAW_NOAA ? NO : YES);
6792 [NSGraphicsContext saveGraphicsState]; 6792 [NSGraphicsContext saveGraphicsState];
6793 [NSGraphicsContext setCurrentContext:gc]; 6793 [NSGraphicsContext setCurrentContext:gc];
6794 } 6794 }
6795 6795
6796 if(bCanDraw) 6796 if(bCanDraw)
6797 { 6797 {
6798 NSBezierPath* aPath = [NSBezierPath bezierPath]; 6798 NSBezierPath* aPath = [NSBezierPath bezierPath];
6799 NSColor *color = pthread_getspecific(_dw_fg_color_key); 6799 NSColor *color = pthread_getspecific(_dw_fg_color_key);
6800 [color set]; 6800 [color set];
6826 [aPath fill]; 6826 [aPath fill];
6827 } 6827 }
6828 /* Actually do the drawing */ 6828 /* Actually do the drawing */
6829 [aPath stroke]; 6829 [aPath stroke];
6830 } 6830 }
6831 6831
6832 if(bi) 6832 if(bi)
6833 [NSGraphicsContext restoreGraphicsState]; 6833 [NSGraphicsContext restoreGraphicsState];
6834 #ifndef BUILDING_FOR_MOJAVE 6834 #ifndef BUILDING_FOR_MOJAVE
6835 if(bCanDraw == YES && !pixmap) 6835 if(bCanDraw == YES && !pixmap)
6836 [image unlockFocus]; 6836 [image unlockFocus];
8613 DW_LOCAL_POOL_IN; 8613 DW_LOCAL_POOL_IN;
8614 8614
8615 snprintf(buffer, 100, "%04d-%02d-%02d", year, month, day); 8615 snprintf(buffer, 100, "%04d-%02d-%02d", year, month, day);
8616 8616
8617 NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 8617 NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
8618 dateFormatter.dateFormat = @"yyyy-mm-dd"; 8618 dateFormatter.dateFormat = @"yyyy-MM-dd";
8619 8619
8620 date = [dateFormatter dateFromString:[NSString stringWithUTF8String:buffer]]; 8620 date = [dateFormatter dateFromString:[NSString stringWithUTF8String:buffer]];
8621 [calendar setDateValue:date]; 8621 [calendar setDateValue:date];
8622 [date release]; 8622 [date release];
8623 DW_LOCAL_POOL_OUT; 8623 DW_LOCAL_POOL_OUT;
8630 */ 8630 */
8631 void dw_calendar_get_date(HWND handle, unsigned int *year, unsigned int *month, unsigned int *day) 8631 void dw_calendar_get_date(HWND handle, unsigned int *year, unsigned int *month, unsigned int *day)
8632 { 8632 {
8633 DWCalendar *calendar = handle; 8633 DWCalendar *calendar = handle;
8634 DW_LOCAL_POOL_IN; 8634 DW_LOCAL_POOL_IN;
8635 NSCalendar *mycalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
8635 NSDate *date = [calendar dateValue]; 8636 NSDate *date = [calendar dateValue];
8636 NSDateFormatter *df = [[NSDateFormatter alloc] init]; 8637 NSDateComponents* components = [mycalendar components:NSCalendarUnitDay|NSCalendarUnitMonth|NSCalendarUnitYear fromDate:date];
8637 [df setDateStyle:NSDateFormatterShortStyle]; 8638 *day = [components day];
8638 NSString *nstr = [df stringFromDate:date]; 8639 *month = [components month];
8639 sscanf([ nstr UTF8String ], "%d/%d/%d", month, day, year); 8640 *year = [components year];
8640 if(*year < 70) 8641 [mycalendar release];
8641 {
8642 *year += 2000;
8643 }
8644 else if(*year < 100)
8645 {
8646 *year += 1900;
8647 }
8648 [df release];
8649 DW_LOCAL_POOL_OUT; 8642 DW_LOCAL_POOL_OUT;
8650 } 8643 }
8651 8644
8652 /* 8645 /*
8653 * Causes the embedded HTML widget to take action. 8646 * Causes the embedded HTML widget to take action.