| 1 |
// |
| 2 |
// ContextMenuTextFieldCell.m |
| 3 |
// Alchemusica |
| 4 |
// |
| 5 |
// Created by Toshi Nagata on 2017/09/24. |
| 6 |
/* |
| 7 |
Copyright (c) 2008-2017 Toshi Nagata. All rights reserved. |
| 8 |
|
| 9 |
This program is free software; you can redistribute it and/or modify |
| 10 |
it under the terms of the GNU General Public License as published by |
| 11 |
the Free Software Foundation version 2 of the License. |
| 12 |
|
| 13 |
This program is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
GNU General Public License for more details. |
| 17 |
*/ |
| 18 |
|
| 19 |
#import "ContextMenuTextFieldCell.h" |
| 20 |
|
| 21 |
@implementation ContextMenuTextFieldCell |
| 22 |
|
| 23 |
- (void)setDrawsUnderline:(BOOL)underline |
| 24 |
{ |
| 25 |
drawsUnderline = underline; |
| 26 |
} |
| 27 |
|
| 28 |
- (BOOL)drawsUnderline |
| 29 |
{ |
| 30 |
return drawsUnderline; |
| 31 |
} |
| 32 |
|
| 33 |
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView |
| 34 |
{ |
| 35 |
[super drawWithFrame:cellFrame inView:controlView]; |
| 36 |
if (drawsUnderline) { |
| 37 |
CGFloat x1, x2, y; |
| 38 |
y = cellFrame.origin.y + cellFrame.size.height - 1; |
| 39 |
x1 = cellFrame.origin.x; |
| 40 |
x2 = x1 + cellFrame.size.width; |
| 41 |
[[NSColor grayColor] set]; |
| 42 |
[NSBezierPath strokeLineFromPoint:NSMakePoint(x1, y) toPoint:NSMakePoint(x2, y)]; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
- (NSMenu *)menuForEvent:(NSEvent *)anEvent inRect:(NSRect)cellFrame ofView:(NSView *)aView |
| 47 |
{ |
| 48 |
id controlView = [self controlView]; |
| 49 |
id menu = [self menu]; |
| 50 |
lastMenuPoint = [controlView convertPoint:[anEvent locationInWindow] fromView:nil]; |
| 51 |
if ([controlView isKindOfClass: [NSTableView class]]) { |
| 52 |
id delegate = [controlView delegate]; |
| 53 |
int row = (int)[controlView rowAtPoint:lastMenuPoint]; |
| 54 |
if ([delegate respondsToSelector: @selector(willUseMenu:ofCell:inRow:)]) |
| 55 |
menu = [delegate willUseMenu:menu ofCell:self inRow:row]; |
| 56 |
[controlView selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection: NO]; |
| 57 |
} |
| 58 |
return menu; |
| 59 |
} |
| 60 |
|
| 61 |
- (IBAction)contextMenuSelected:(id)sender |
| 62 |
{ |
| 63 |
NSString *stringValue; |
| 64 |
id controlView, delegate; |
| 65 |
int row; |
| 66 |
controlView = [self controlView]; |
| 67 |
if (![controlView isKindOfClass:[NSTableView class]]) |
| 68 |
return; |
| 69 |
delegate = [controlView delegate]; |
| 70 |
if (![delegate respondsToSelector: @selector(stringValueForMenuItem:ofCell:inRow:)]) |
| 71 |
return; |
| 72 |
row = (int)[controlView rowAtPoint:lastMenuPoint]; |
| 73 |
stringValue = [delegate stringValueForMenuItem:sender ofCell:self inRow:row]; |
| 74 |
if (stringValue != nil) { |
| 75 |
int column = (int)[controlView columnAtPoint: lastMenuPoint]; |
| 76 |
id myWindow = [controlView window]; |
| 77 |
id fieldEditor; |
| 78 |
// Start editing mode programatically, modify the text, and end editing mode. |
| 79 |
// (This seems to be the most consistent way to modify a particular cell in |
| 80 |
// the table view.) |
| 81 |
[controlView editColumn:column row:row withEvent:nil select:YES]; |
| 82 |
fieldEditor = [myWindow fieldEditor:NO forObject:controlView]; |
| 83 |
if (fieldEditor != nil) { |
| 84 |
// shouldChangeTextInRange:replacementString: is absolutely necessary. If this |
| 85 |
// call is omitted, then Cocoa binding of the table view does not work properly. |
| 86 |
[fieldEditor selectAll:nil]; |
| 87 |
if ([fieldEditor shouldChangeTextInRange:[fieldEditor selectedRange] replacementString:stringValue]) { // Send notifications _before_ modification |
| 88 |
[fieldEditor setString:stringValue]; // Change the value |
| 89 |
[fieldEditor didChangeText]; // Send notifications _after_ modification |
| 90 |
} |
| 91 |
[myWindow makeFirstResponder: controlView]; // End editing |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
@end |