Revision: 28702
          http://sourceforge.net/p/bibdesk/svn/28702
Author:   hofman
Date:     2024-02-07 17:03:21 +0000 (Wed, 07 Feb 2024)
Log Message:
-----------
replace matrix by radio buttons

Modified Paths:
--------------
    trunk/bibdesk/BDSKColorLabelWell.h
    trunk/bibdesk/BDSKColorLabelWell.m

Modified: trunk/bibdesk/BDSKColorLabelWell.h
===================================================================
--- trunk/bibdesk/BDSKColorLabelWell.h  2024-02-06 23:31:00 UTC (rev 28701)
+++ trunk/bibdesk/BDSKColorLabelWell.h  2024-02-07 17:03:21 UTC (rev 28702)
@@ -40,12 +40,11 @@
 
 
 @interface BDSKColorLabelWell : NSColorWell {
-    NSMatrix *matrix;
+    NSArray *buttons;
     BOOL suppressColor;
     BOOL suppressSelection;
     NSTextFieldCell *titleCell;
     NSTextFieldCell *labelCell;
-    NSArray *trackingAreas;
 }
 
 @property (class, nonatomic) BOOL updatingColorPanel;

Modified: trunk/bibdesk/BDSKColorLabelWell.m
===================================================================
--- trunk/bibdesk/BDSKColorLabelWell.m  2024-02-06 23:31:00 UTC (rev 28701)
+++ trunk/bibdesk/BDSKColorLabelWell.m  2024-02-07 17:03:21 UTC (rev 28702)
@@ -39,8 +39,14 @@
 #import "BDSKColorLabelWell.h"
 #import "NSString_BDSKExtensions.h"
 #import "NSColor_BDSKExtensions.h"
+#import "NSEvent_BDSKExtensions.h"
 
 
+@interface BDSKColorLabelButton : NSButton
+@end
+
+#pragma mark -
+
 @interface BDSKColorLabelCell : NSButtonCell {
     BOOL hovered;
 }
@@ -49,7 +55,8 @@
 
 #pragma mark -
 
-@interface BDSKColorLabelWell (Private)
+@interface BDSKColorLabelWell ()
+@property (nonatomic, copy) NSString *label;
 - (void)colorLabelAction:(id)sender;
 - (void)updateColorSelection;
 @end
@@ -57,7 +64,7 @@
 
 @implementation BDSKColorLabelWell
 
-@dynamic margin, title, showLabel;
+@dynamic margin, title, label, showLabel;
 
 static BOOL updatingColorPanel = NO;
 
@@ -70,47 +77,28 @@
 }
 
 - (void)commonInit {
-    if (matrix == nil) {
-        matrix = [[NSMatrix alloc] initWithFrame:NSMakeRect(0.0, 2.0, 178.0, 
18.0)];
-        BDSKColorLabelCell *cell = [[BDSKColorLabelCell alloc] 
initTextCell:@""];
-        [cell setButtonType:NSButtonTypeRadio];
-        [cell setBordered:NO];
-        [matrix setPrototype:cell];
-        [matrix setCellSize:NSMakeSize(18.0, 18.0)];
-        [matrix setIntercellSpacing:NSMakeSize(2.0, 4.0)];
-        [matrix setMode:NSRadioModeMatrix];
-        [matrix setSelectionByRect:NO];
-        [matrix setAllowsEmptySelection:YES];
-        [matrix renewRows:1 columns:9];
-        [matrix sizeToCells];
-        [matrix deselectAllCells];
-        [matrix setEnabled:[self isEnabled]];
+    if ([buttons count] == 0) {
+        NSMutableArray *array = [NSMutableArray array];
+        NSRect rect = NSMakeRect(0.0, 2.0, 18.0, 18.0);
         NSArray *colors = [NSColor labelColors];
-        NSInteger column;
-        for (column = 0; column < 9; column++) {
-            cell = [matrix cellAtRow:0 column:column];
-            [cell setTag:column];
-            if (column > 0 && column < 8)
-                [cell setRepresentedObject:[colors objectAtIndex:column - 1]];
+        for (NSUInteger i = 0; i < 9; i++) {
+            NSButton *button = [[BDSKColorLabelButton alloc] 
initWithFrame:rect];
+            [button setButtonType:NSButtonTypeRadio];
+            [button setBordered:NO];
+            [button setTarget:self];
+            [button setAction:@selector(colorLabelAction:)];
+            [button setAutoresizingMask:NSViewMaxXMargin | NSViewMaxYMargin];
+            [button setTag:i];
+            [[button cell] setTag:i];
+            if (i > 0 && i < 8)
+                [[button cell] setRepresentedObject:[colors objectAtIndex:i - 
1]];
+            [self addSubview:button];
+            [array addObject:button];
+            rect.origin.x += 20.0;
         }
-        [matrix setTarget:self];
-        [matrix setAction:@selector(colorLabelAction:)];
-        [matrix setAutoresizingMask:NSViewMaxXMargin | NSViewMaxYMargin];
-        [self addSubview:matrix];
-        [matrix deselectAllCells];
+        buttons = [array copy];
     }
     
-    NSMutableArray *areas = [NSMutableArray array];
-    NSUInteger i;
-    NSTrackingAreaOptions options = NSTrackingMouseEnteredAndExited | 
NSTrackingActiveInKeyWindow | NSTrackingEnabledDuringMouseDrag;
-    for (i = 0; i < 9; i++) {
-        NSRect rect = [matrix cellFrameAtRow:0 column:i];
-        NSTrackingArea *area = [[NSTrackingArea alloc] initWithRect:rect 
options:options owner:self userInfo:nil];
-        [matrix addTrackingArea:area];
-        [areas addObject:area];
-    }
-    trackingAreas = [areas copy];
-    
     [self updateColorSelection];
 }
 
@@ -125,14 +113,16 @@
 - (instancetype)initWithCoder:(NSCoder *)coder {
     self = [super initWithCoder:coder];
     if (self) {
-        if ([coder allowsKeyedCoding]) {
-            matrix = [coder decodeObjectForKey:@"matrix"];
-            suppressSelection = [coder decodeBoolForKey:@"suppressSelection"];
-            titleCell = [coder decodeObjectForKey:@"titleCell"];
-            labelCell = [coder decodeObjectForKey:@"labelCell"];
-        } else if ([[[self subviews] firstObject] isKindOfClass:[NSMatrix 
class]]) {
-            matrix = [[self subviews] firstObject];
+        NSMutableArray *array = [NSMutableArray array];
+        for (NSUInteger i = 0; i < 9; i++) {
+            NSButton *button = [coder decodeObjectForKey:[NSString 
stringWithFormat:@"button%lu", i]];
+            if (button)
+                [array addObject:button];
         }
+        buttons = [array copy];
+        suppressSelection = [coder decodeBoolForKey:@"suppressSelection"];
+        titleCell = [coder decodeObjectForKey:@"titleCell"];
+        labelCell = [coder decodeObjectForKey:@"labelCell"];
         [self commonInit];
     }
     return self;
@@ -141,7 +131,8 @@
 - (void)encodeWithCoder:(NSCoder *)aCoder {
     [super encodeWithCoder:aCoder];
     if ([aCoder allowsKeyedCoding]) {
-        [aCoder encodeConditionalObject:matrix forKey:@"matrix"];
+        for (NSUInteger i = 0; i < 9; i++)
+            [aCoder encodeConditionalObject:[buttons objectAtIndex:i] 
forKey:[NSString stringWithFormat:@"button%lu", i]];
         [aCoder encodeBool:suppressSelection forKey:@"suppressSelection"];
         [aCoder encodeObject:titleCell forKey:@"titleCell"];
         [aCoder encodeObject:labelCell forKey:@"labelCell"];
@@ -149,7 +140,7 @@
 }
 
 - (void)sizeToFit {
-    NSRect frame = [matrix frame];
+    NSRect frame = NSUnionRect([[buttons firstObject] frame], [[buttons 
lastObject] frame]);
     if (titleCell) {
         NSSize titleSize = [titleCell cellSize];
         frame.size.width = fmax(NSWidth(frame), ceil(titleSize.width));
@@ -159,7 +150,8 @@
 }
 
 - (NSSize)intrinsicContentSize {
-    NSSize size = [matrix frame].size;
+    NSRect frame = NSUnionRect([[buttons firstObject] frame], [[buttons 
lastObject] frame]);
+    NSSize size = frame.size;
     if (titleCell) {
         NSSize titleSize = [titleCell cellSize];
         size.width = fmax(size.width, ceil(titleSize.width));
@@ -181,11 +173,14 @@
 }
 
 - (CGFloat)margin {
-    return NSMinX([matrix frame]);
+    return NSMinX([[buttons firstObject] frame]);
 }
 
 - (void)setMargin:(CGFloat)margin {
-    [matrix setFrameOrigin:NSMakePoint(margin, NSMinY([matrix frame]))];
+    for (NSButton *button in buttons) {
+        [button setFrameOrigin:NSMakePoint(margin, NSMinY([button frame]))];
+        margin += 20.0;
+    }
 }
 
 - (NSString *)title {
@@ -206,6 +201,17 @@
     [self setNeedsDisplay:YES];
 }
 
+- (NSString *)label {
+    return [labelCell stringValue];
+}
+
+- (void)setLabel:(NSString *)label {
+    if (labelCell) {
+        [labelCell setStringValue:label ?: @""];
+        [self setNeedsDisplay:YES];
+    }
+}
+
 - (BOOL)showLabel {
     return labelCell != nil;
 }
@@ -216,16 +222,18 @@
         [labelCell setFont:[NSFont boldSystemFontOfSize:[NSFont 
smallSystemFontSize]]];
         [labelCell setTextColor:[NSColor systemGrayColor]];
         [labelCell setAlignment:NSTextAlignmentCenter];
-        [matrix setFrameOrigin:NSMakePoint(NSMinX([matrix frame]), [labelCell 
cellSize].height + 2.0)];
+        for (NSButton *button in buttons)
+            [button setFrameOrigin:NSMakePoint(NSMinX([button frame]), 
[labelCell cellSize].height + 2.0)];
     } else if (flag == NO && labelCell) {
         labelCell = nil;
-        [matrix setFrameOrigin:NSMakePoint(NSMinX([matrix frame]), 2.0)];
+        for (NSButton *button in buttons)
+            [button setFrameOrigin:NSMakePoint(NSMinX([button frame]), 2.0)];
     }
 }
 
 - (void)setEnabled:(BOOL)enabled {
     [super setEnabled:enabled];
-    [matrix setEnabled:enabled];
+    [buttons setValue:[NSNumber numberWithBool:enabled] forKey:@"enabled"];
 }
 
 - (BOOL)isOpaque {
@@ -241,11 +249,10 @@
         idx++;
     else
         idx = 8;
-    [[matrix cellWithTag:8] setRepresentedObject:idx == 8 ? color : nil];
-    if (suppressSelection)
-        [matrix deselectAllCells];
-    else
-        [matrix selectCellWithTag:idx];
+    [[[buttons lastObject] cell] setRepresentedObject:idx == 8 ? color : nil];
+    [buttons enumerateObjectsUsingBlock:^(NSButton *button, NSUInteger i, BOOL 
*stop){
+        [button setState:suppressSelection == NO && i == idx ? 
NSControlStateValueOn : NSControlStateValueOff];
+    }];
 }
 
 - (NSColor *)color {
@@ -265,19 +272,20 @@
 
 - (void)drawRect:(NSRect)dirtyRect {
     if (titleCell) {
-        NSRect rect = [matrix frame];
+        NSRect rect = [[buttons firstObject] frame];
         rect.size = [titleCell cellSize];
         rect.origin.y = NSMaxY([self bounds]) - NSHeight(rect);
         [titleCell drawWithFrame:rect inView:self];
     }
     if ([NSString isEmptyString:[labelCell stringValue]] == NO) {
-        NSRect rect = [matrix frame];
+        NSRect rect = NSUnionRect([[buttons firstObject] frame], [[buttons 
lastObject] frame]);
         rect.size.height = [labelCell cellSize].height;
         rect.origin.y = 0.0;
         [labelCell drawWithFrame:rect inView:self];
     }
     if ([self isActive]) {
-        NSRect rect = [matrix convertRect:[matrix cellFrameAtRow:0 column:8] 
toView:self];
+        NSButton *lastButton = [buttons lastObject];
+        NSRect rect = [lastButton convertRect:[lastButton frame] toView:self];
         [NSGraphicsContext saveGraphicsState];
         [[NSColor colorWithGenericGamma22White:0.7 alpha:0.75] setFill];
         [[NSBezierPath bezierPathWithOvalInRect:rect] fill];
@@ -287,19 +295,48 @@
 
 - (BOOL)becomeFirstResponder {
     if ([super becomeFirstResponder]) {
-        [[self window] makeFirstResponder:matrix];
+        [[self window] makeFirstResponder:[buttons firstObject]];
         return YES;
     }
     return NO;
 }
 
-- (void)mouseDown:(NSEvent *)event {}
+- (void)mouseDown:(NSEvent *)event {
+    [[self nextResponder] mouseDown:event];
+}
 
+- (void)keyDown:(NSEvent *)event {
+    unichar ch = [event firstCharacter];
+    if (ch == NSRightArrowFunctionKey ||
+        ([self enclosingMenuItem] && ch == NSTabCharacter)) {
+        NSUInteger i = [buttons indexOfObject:[[self window] firstResponder] 
?: [NSNull null]];
+        if (i == NSNotFound)
+            i = 0;
+        else if (i < 8)
+            ++i;
+        else
+            return;
+        [[self window] makeFirstResponder:[buttons objectAtIndex:i]];
+    } else if (ch == NSLeftArrowFunctionKey ||
+               ([self enclosingMenuItem] && ch == NSBackTabCharacter)) {
+        NSUInteger i = [buttons indexOfObject:[[self window] firstResponder] 
?: [NSNull null]];
+        if (i == NSNotFound)
+            i = 8;
+        else if (i > 0)
+            --i;
+        else
+            return;
+        [[self window] makeFirstResponder:[buttons objectAtIndex:i]];
+    } else {
+        [super keyDown:event];
+    }
+}
+
 - (void)changeColor:(id)sender {}
 
 - (void)colorLabelAction:(id)sender {
     NSColor *color = nil;
-    NSInteger tag = [sender selectedTag];
+    NSInteger tag = [sender tag];
     if (tag < 0) {
         [self updateColorSelection];
         return;
@@ -337,7 +374,7 @@
         [BDSKColorLabelWell setUpdatingColorPanel:NO];
         if ([self action]) {
             suppressColor = YES;
-            [NSApp sendAction:[self action] to:[self target] from:self];
+            [self sendAction:[self action] to:[self target]];
             suppressColor = NO;
         }
     }
@@ -347,31 +384,62 @@
 - (void)viewWillMoveToWindow:(NSWindow *)window {
     [super viewWillMoveToWindow:window];
     [labelCell setStringValue:@""];
-    [[matrix cells] setValue:@NO forKey:@"hovered"];
 }
 
+- (BOOL)accessibilityIsIgnored {
+    return YES;
+}
+
+- (BOOL)isAccessibilityElement {
+    return NO;
+}
+
+@end
+
+#pragma mark -
+
+@implementation BDSKColorLabelButton
+
++ (Class)cellClass {
+    return [BDSKColorLabelCell class];
+}
+
+- (void)commonInit {
+    NSTrackingAreaOptions options = NSTrackingMouseEnteredAndExited | 
NSTrackingActiveInKeyWindow | NSTrackingEnabledDuringMouseDrag | 
NSTrackingInVisibleRect;
+    NSTrackingArea *area = [[NSTrackingArea alloc] initWithRect:[self bounds] 
options:options owner:self userInfo:nil];
+    [self addTrackingArea:area];
+}
+
+- (instancetype)initWithFrame:(NSRect)frameRect {
+    self = [super initWithFrame:frameRect];
+    if (self) {
+        [self commonInit];
+    }
+    return self;
+}
+
+- (instancetype)initWithCoder:(NSCoder *)coder {
+    self = [super initWithCoder:coder];
+    if (self) {
+        [self commonInit];
+    }
+    return self;
+}
+
 - (void)mouseEntered:(NSEvent *)event {
-    NSTrackingArea *area = [event trackingArea];
-    if (area) {
-        NSUInteger i = [trackingAreas indexOfObject:area];
-        if (i != NSNotFound) {
-            if ([self isEnabled]) {
-                [[matrix cells] setValue:@NO forKey:@"hovered"];
-                [[matrix cellAtRow:0 column:i] setHovered:YES];
-                [matrix setNeedsDisplay:YES];
-                if (labelCell) {
-                    NSString *label = @"";
-                    if (i == 0)
-                        label = NSLocalizedString(@"Clear", @"");
-                    else if (i < 8)
-                        label = [[NSColor labelNames] objectAtIndex:i - 1];
-                    else if (i == 8)
-                        label = [NSLocalizedString(@"Other", @"") 
stringByAppendingEllipsis];
-                    [labelCell setStringValue:label];
-                    [self setNeedsDisplay:YES];
-                }
-            }
-            return;
+    if ([self isEnabled]) {
+        [(BDSKColorLabelCell *)[self cell] setHovered:YES];
+        [self setNeedsDisplay:YES];
+        if ([[self superview] respondsToSelector:@selector(setLabel:)]) {
+            NSString *label = @"";
+            NSInteger tag = [self tag];
+            if (tag == 0)
+                label = NSLocalizedString(@"Clear", @"");
+            else if (tag < 8)
+                label = [[NSColor labelNames] objectAtIndex:tag - 1];
+            else if (tag == 8)
+                label = [NSLocalizedString(@"Other", @"") 
stringByAppendingEllipsis];
+            [(BDSKColorLabelWell *)[self superview] setLabel:label];
         }
     }
     [super mouseEntered:event];
@@ -378,34 +446,20 @@
 }
 
 - (void)mouseExited:(NSEvent *)event {
-    NSTrackingArea *area = [event trackingArea];
-    if (area) {
-        if ([self isEnabled] == NO)
-            return;
-        NSUInteger i = [trackingAreas indexOfObject:area];
-        if (i != NSNotFound) {
-            if ([self isEnabled]) {
-                [[matrix cells] setValue:@NO forKey:@"hovered"];
-                [matrix setNeedsDisplay:YES];
-                if (labelCell) {
-                    [labelCell setStringValue:@""];
-                    [self setNeedsDisplay:YES];
-                }
-            }
-            return;
-        }
+    if ([self isEnabled]) {
+        [(BDSKColorLabelCell *)[self cell] setHovered:NO];
+        [self setNeedsDisplay:YES];
+        if ([[self superview] respondsToSelector:@selector(setLabel:)])
+            [(BDSKColorLabelWell *)[self superview] setLabel:nil];
     }
     [super mouseExited:event];
 }
 
-- (BOOL)accessibilityIsIgnored {
-    return YES;
+- (void)viewWillMoveToWindow:(NSWindow *)window {
+    [(BDSKColorLabelCell *)[self cell] setHovered:NO];
+    [super viewWillMoveToWindow:window];
 }
 
-- (BOOL)isAccessibilityElement {
-    return NO;
-}
-
 @end
 
 #pragma mark -

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
Bibdesk-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to