Mercurial > dwindows
comparison mac/dw.m @ 2179:bd6146d4bdbc
Mac: Enable autoresizing on the NSTableCellViews and code cleanup.
author | bsmith@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Tue, 13 Oct 2020 01:51:40 +0000 |
parents | 2fa38504803e |
children | bfc089ab053b |
comparison
equal
deleted
inserted
replaced
2178:2fa38504803e | 2179:bd6146d4bdbc |
---|---|
1470 { | 1470 { |
1471 NSScanner *objScanner = [NSScanner scannerWithString:usernotification.identifier]; | 1471 NSScanner *objScanner = [NSScanner scannerWithString:usernotification.identifier]; |
1472 unsigned long long handle; | 1472 unsigned long long handle; |
1473 HWND notification; | 1473 HWND notification; |
1474 | 1474 |
1475 // Skip the dw-notification- prefix | 1475 /* Skip the dw-notification- prefix */ |
1476 [objScanner scanString:@"dw-notification-" intoString:nil]; | 1476 [objScanner scanString:@"dw-notification-" intoString:nil]; |
1477 [objScanner scanUnsignedLongLong:&handle]; | 1477 [objScanner scanUnsignedLongLong:&handle]; |
1478 notification = DW_UINT_TO_POINTER(handle); | 1478 notification = DW_UINT_TO_POINTER(handle); |
1479 | 1479 |
1480 switch(usernotification.activationType) | 1480 switch(usernotification.activationType) |
2357 return cellSize; | 2357 return cellSize; |
2358 } | 2358 } |
2359 @end | 2359 @end |
2360 #endif | 2360 #endif |
2361 | 2361 |
2362 #define _DW_CONTAINER_ICON_WIDTH 16 | |
2363 #define _DW_CONTAINER_ICON_HEIGHT 16 | |
2364 #define _DW_CONTAINER_ROW_HEIGHT 20 | |
2365 | |
2366 #ifdef BUILDING_FOR_YOSEMITE | 2362 #ifdef BUILDING_FOR_YOSEMITE |
2367 NSTableCellView *_dw_table_cell_view_new(NSImage *icon, NSString *text) | 2363 NSTableCellView *_dw_table_cell_view_new(NSImage *icon, NSString *text) |
2368 { | 2364 { |
2369 id browsercell = [[[NSTableCellView alloc] init] autorelease]; | 2365 NSTableCellView *browsercell = [[[NSTableCellView alloc] init] autorelease]; |
2366 [browsercell setAutoresizesSubviews:YES]; | |
2370 if(icon) | 2367 if(icon) |
2371 { | 2368 { |
2372 NSImageView *iv = [[[NSImageView alloc] init] autorelease]; | 2369 NSImageView *iv = [[[NSImageView alloc] init] autorelease]; |
2370 [iv setAutoresizingMask:NSViewHeightSizable]; | |
2371 [iv setImage:icon]; | |
2373 [browsercell setImageView:iv]; | 2372 [browsercell setImageView:iv]; |
2374 [browsercell addSubview:iv]; | 2373 [browsercell addSubview:iv]; |
2375 [iv setImage:icon]; | |
2376 } | 2374 } |
2377 if(text) | 2375 if(text) |
2378 { | 2376 { |
2379 NSTextField *tf = [[[NSTextField alloc] init] autorelease]; | 2377 NSTextField *tf = [[[NSTextField alloc] init] autorelease]; |
2380 [browsercell setTextField:tf]; | 2378 [tf setAutoresizingMask:NSViewHeightSizable|NSViewWidthSizable]; |
2381 [browsercell addSubview:tf]; | |
2382 [tf setStringValue:text]; | 2379 [tf setStringValue:text]; |
2383 [tf setEditable:NO]; | 2380 [tf setEditable:NO]; |
2384 [tf setBezeled:NO]; | 2381 [tf setBezeled:NO]; |
2385 [tf setBordered:NO]; | 2382 [tf setBordered:NO]; |
2383 [tf setDrawsBackground:NO]; | |
2384 [browsercell setTextField:tf]; | |
2385 [browsercell addSubview:tf]; | |
2386 } | 2386 } |
2387 return browsercell; | 2387 return browsercell; |
2388 } | 2388 } |
2389 | 2389 |
2390 void _dw_table_cell_view_layout(NSTableCellView *result) | 2390 void _dw_table_cell_view_layout(NSTableCellView *result) |
2391 { | 2391 { |
2392 int width = 0; | |
2393 NSRect rect = result.frame; | |
2394 | |
2392 /* Adjust the frames of the textField and imageView */ | 2395 /* Adjust the frames of the textField and imageView */ |
2393 if([result imageView]) | 2396 if([result imageView]) |
2394 { | 2397 { |
2395 NSRect rect = NSMakeRect((_DW_CONTAINER_ROW_HEIGHT - _DW_CONTAINER_ICON_WIDTH) / 2, | 2398 NSImage *icon = [[result imageView] image]; |
2396 (_DW_CONTAINER_ROW_HEIGHT - _DW_CONTAINER_ICON_HEIGHT) / 2, | 2399 width = [icon size].width; |
2397 _DW_CONTAINER_ICON_WIDTH,_DW_CONTAINER_ICON_HEIGHT); | 2400 [[result imageView] setFrame:NSMakeRect(0,0,width,rect.size.height)]; |
2398 [[result imageView] setFrame:rect]; | |
2399 } | 2401 } |
2400 if([result textField]) | 2402 if([result textField]) |
2401 { | 2403 { |
2402 NSRect rect = result.frame; | |
2403 | 2404 |
2404 /* Adjust the rect to allow space for the image */ | 2405 /* Adjust the rect to allow space for the image */ |
2405 if([result imageView]) | 2406 if([result imageView]) |
2406 { | 2407 { |
2407 rect.origin.x += _DW_CONTAINER_ICON_WIDTH; | 2408 rect.origin.x += width; |
2408 rect.size.width -= _DW_CONTAINER_ICON_WIDTH; | 2409 rect.size.width -= width; |
2409 } | 2410 } |
2410 [[result textField] setFrame:rect]; | 2411 [[result textField] setFrame:rect]; |
2411 } | 2412 } |
2412 } | 2413 } |
2413 #endif | 2414 #endif |
2625 /* There is no existing cell to reuse so create a new one */ | 2626 /* There is no existing cell to reuse so create a new one */ |
2626 if(result == nil) | 2627 if(result == nil) |
2627 { | 2628 { |
2628 /* The data is already a NSTableCellView so just return that */ | 2629 /* The data is already a NSTableCellView so just return that */ |
2629 if([celldata isMemberOfClass:[NSTableCellView class]]) | 2630 if([celldata isMemberOfClass:[NSTableCellView class]]) |
2630 { | |
2631 result = celldata; | 2631 result = celldata; |
2632 [result setFrame:NSMakeRect(0,0,tableColumn.width,_DW_CONTAINER_ROW_HEIGHT)]; | |
2633 [[result textField] setFrame:result.frame]; | |
2634 } | |
2635 else | 2632 else |
2636 { | 2633 { |
2637 /* Create the new NSTableCellView with a frame of the {0,0} with the width of the table. | 2634 /* Create the new NSTableCellView with a frame of the {0,0} with the width of the table. |
2638 * Note that the height of the frame is not really relevant, because the row height will modify the height. | 2635 * Note that the height of the frame is not really relevant, because the row height will modify the height. |
2639 */ | 2636 */ |
2640 result = [[NSTableCellView alloc] initWithFrame:NSMakeRect(0,0,tableColumn.width,_DW_CONTAINER_ROW_HEIGHT)]; | 2637 result = [[NSTableCellView alloc] init]; |
2641 | 2638 |
2642 /* The identifier of the NSTextField instance is set to MyView. | 2639 /* The identifier of the NSTextField instance is set to MyView. |
2643 * This allows the cell to be reused. | 2640 * This allows the cell to be reused. |
2644 */ | 2641 */ |
2645 [result setIdentifier:tableColumn.identifier]; | 2642 [result setIdentifier:tableColumn.identifier]; |
2646 } | 2643 } |
2647 } | 2644 } |
2648 | 2645 |
2649 _dw_table_cell_view_layout(result); | 2646 _dw_table_cell_view_layout(result); |
2650 | 2647 |
2651 /*NSLog(@"viewForTableColumn:%@ row:%d textField:%@ celldata class:%@\n", tableColumn.identifier, (int)row, [[result textField] stringValue], [celldata className]);*/ | |
2652 | |
2653 tcell = [[result textField] cell]; | 2648 tcell = [[result textField] cell]; |
2654 | 2649 |
2655 /* Handle drawing alternating row colors if enabled */ | 2650 /* Handle drawing alternating row colors if enabled */ |
2656 if ((row % 2) == 0) | 2651 if ((row % 2) == 0) |
2657 { | 2652 { |
3179 [[view imageView] setImage:icon]; | 3174 [[view imageView] setImage:icon]; |
3180 } | 3175 } |
3181 else | 3176 else |
3182 view = _dw_table_cell_view_new(icon, text); | 3177 view = _dw_table_cell_view_new(icon, text); |
3183 } | 3178 } |
3184 [view setFrame:NSMakeRect(0,0,tableColumn.width,_DW_CONTAINER_ROW_HEIGHT)]; | |
3185 [[view textField] setFrame:view.frame]; | |
3186 _dw_table_cell_view_layout(view); | 3179 _dw_table_cell_view_layout(view); |
3187 return view; | 3180 return view; |
3188 } | 3181 } |
3189 #else | 3182 #else |
3190 -(id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item | 3183 -(id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item |
3478 API_AVAILABLE(macos(10.14)) | 3471 API_AVAILABLE(macos(10.14)) |
3479 @interface DWUserNotificationCenterDelegate : NSObject <UNUserNotificationCenterDelegate> | 3472 @interface DWUserNotificationCenterDelegate : NSObject <UNUserNotificationCenterDelegate> |
3480 @end | 3473 @end |
3481 | 3474 |
3482 @implementation DWUserNotificationCenterDelegate | 3475 @implementation DWUserNotificationCenterDelegate |
3483 //Called when a notification is delivered to a foreground app. | 3476 /* Called when a notification is delivered to a foreground app. */ |
3484 -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler API_AVAILABLE(macos(10.14)) | 3477 -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler API_AVAILABLE(macos(10.14)) |
3485 { | 3478 { |
3486 completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge); | 3479 completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge); |
3487 } | 3480 } |
3488 //Called to let your app know which action was selected by the user for a given notification. | 3481 /* Called to let your app know which action was selected by the user for a given notification. */ |
3489 -(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler API_AVAILABLE(macos(10.14)) | 3482 -(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler API_AVAILABLE(macos(10.14)) |
3490 { | 3483 { |
3491 NSScanner *objScanner = [NSScanner scannerWithString:response.notification.request.identifier]; | 3484 NSScanner *objScanner = [NSScanner scannerWithString:response.notification.request.identifier]; |
3492 unsigned long long handle; | 3485 unsigned long long handle; |
3493 HWND notification; | 3486 HWND notification; |
3494 | 3487 |
3495 // Skip the dw-notification- prefix | 3488 /* Skip the dw-notification- prefix */ |
3496 [objScanner scanString:@"dw-notification-" intoString:nil]; | 3489 [objScanner scanString:@"dw-notification-" intoString:nil]; |
3497 [objScanner scanUnsignedLongLong:&handle]; | 3490 [objScanner scanUnsignedLongLong:&handle]; |
3498 notification = DW_UINT_TO_POINTER(handle); | 3491 notification = DW_UINT_TO_POINTER(handle); |
3499 | 3492 |
3500 if ([response.actionIdentifier isEqualToString:UNNotificationDismissActionIdentifier]) | 3493 if ([response.actionIdentifier isEqualToString:UNNotificationDismissActionIdentifier]) |
3501 { | 3494 { |
3502 // The user dismissed the notification without taking action. | 3495 /* The user dismissed the notification without taking action. */ |
3503 dw_signal_disconnect_by_window(notification); | 3496 dw_signal_disconnect_by_window(notification); |
3504 } | 3497 } |
3505 else if ([response.actionIdentifier isEqualToString:UNNotificationDefaultActionIdentifier]) | 3498 else if ([response.actionIdentifier isEqualToString:UNNotificationDefaultActionIdentifier]) |
3506 { | 3499 { |
3507 // The user launched the app. | 3500 /* The user launched the app. */ |
3508 _event_handler(notification, nil, 8); | 3501 _event_handler(notification, nil, 8); |
3509 dw_signal_disconnect_by_window(notification); | 3502 dw_signal_disconnect_by_window(notification); |
3510 } | 3503 } |
3511 completionHandler(); | 3504 completionHandler(); |
3512 } | 3505 } |
4957 } | 4950 } |
4958 | 4951 |
4959 /* Internal box packing function called by the other 3 functions */ | 4952 /* Internal box packing function called by the other 3 functions */ |
4960 void _dw_box_pack(HWND box, HWND item, int index, int width, int height, int hsize, int vsize, int pad, char *funcname) | 4953 void _dw_box_pack(HWND box, HWND item, int index, int width, int height, int hsize, int vsize, int pad, char *funcname) |
4961 { | 4954 { |
4962 //int _locked_by_me = FALSE; | |
4963 //DW_MUTEX_LOCK; | |
4964 id object = box; | 4955 id object = box; |
4965 DWBox *view = box; | 4956 DWBox *view = box; |
4966 DWBox *this = item; | 4957 DWBox *this = item; |
4967 Box *thisbox; | 4958 Box *thisbox; |
4968 int z, x = 0; | 4959 int z, x = 0; |
5076 _dw_redraw([view window], TRUE); | 5067 _dw_redraw([view window], TRUE); |
5077 | 5068 |
5078 /* Free the old data */ | 5069 /* Free the old data */ |
5079 if(thisitem) | 5070 if(thisitem) |
5080 free(thisitem); | 5071 free(thisitem); |
5081 //DW_MUTEX_UNLOCK; | |
5082 } | 5072 } |
5083 | 5073 |
5084 /* | 5074 /* |
5085 * Remove windows (widgets) from the box they are packed into. | 5075 * Remove windows (widgets) from the box they are packed into. |
5086 * Parameters: | 5076 * Parameters: |
11022 vsnprintf(outbuf, 1024, description, args); | 11012 vsnprintf(outbuf, 1024, description, args); |
11023 va_end(args); | 11013 va_end(args); |
11024 } | 11014 } |
11025 | 11015 |
11026 #ifdef BUILDING_FOR_MOJAVE | 11016 #ifdef BUILDING_FOR_MOJAVE |
11027 // Configure the notification's payload. | 11017 /* Configure the notification's payload. */ |
11028 if (@available(macOS 10.14, *)) | 11018 if (@available(macOS 10.14, *)) |
11029 { | 11019 { |
11030 if([[NSBundle mainBundle] bundleIdentifier] != nil) | 11020 if([[NSBundle mainBundle] bundleIdentifier] != nil) |
11031 { | 11021 { |
11032 UNMutableNotificationContent* notification = [[UNMutableNotificationContent alloc] init]; | 11022 UNMutableNotificationContent* notification = [[UNMutableNotificationContent alloc] init]; |
11053 } | 11043 } |
11054 else | 11044 else |
11055 #endif | 11045 #endif |
11056 { | 11046 { |
11057 #ifndef BUILDING_FOR_BIG_SUR | 11047 #ifndef BUILDING_FOR_BIG_SUR |
11058 // Fallback on earlier versions | 11048 /* Fallback on earlier versions */ |
11059 NSUserNotification *notification = [[NSUserNotification alloc] init]; | 11049 NSUserNotification *notification = [[NSUserNotification alloc] init]; |
11060 | 11050 |
11061 if(notification) | 11051 if(notification) |
11062 { | 11052 { |
11063 notification.title = [NSString stringWithUTF8String:title]; | 11053 notification.title = [NSString stringWithUTF8String:title]; |
11087 if(notification) | 11077 if(notification) |
11088 { | 11078 { |
11089 NSString *notid = [NSString stringWithFormat:@"dw-notification-%llu", DW_POINTER_TO_ULONGLONG(notification)]; | 11079 NSString *notid = [NSString stringWithFormat:@"dw-notification-%llu", DW_POINTER_TO_ULONGLONG(notification)]; |
11090 | 11080 |
11091 #ifdef BUILDING_FOR_MOJAVE | 11081 #ifdef BUILDING_FOR_MOJAVE |
11092 // Schedule the notification. | 11082 /* Schedule the notification. */ |
11093 if (@available(macOS 10.14, *)) | 11083 if (@available(macOS 10.14, *)) |
11094 { | 11084 { |
11095 if([[NSBundle mainBundle] bundleIdentifier] != nil) | 11085 if([[NSBundle mainBundle] bundleIdentifier] != nil) |
11096 { | 11086 { |
11097 UNMutableNotificationContent* content = (UNMutableNotificationContent *)notification; | 11087 UNMutableNotificationContent* content = (UNMutableNotificationContent *)notification; |
11103 } | 11093 } |
11104 else | 11094 else |
11105 #endif | 11095 #endif |
11106 { | 11096 { |
11107 #ifndef BUILDING_FOR_BIG_SUR | 11097 #ifndef BUILDING_FOR_BIG_SUR |
11108 // Fallback on earlier versions | 11098 /* Fallback on earlier versions */ |
11109 NSUserNotification *request = (NSUserNotification *)notification; | 11099 NSUserNotification *request = (NSUserNotification *)notification; |
11110 request.identifier = notid; | 11100 request.identifier = notid; |
11111 [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:request]; | 11101 [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:request]; |
11112 #endif | 11102 #endif |
11113 } | 11103 } |