blob: 7e9c567268be0099a138e72acbb9bc4bd8c02471 [file] [log] [blame]
Alexandre Lision2db8f472015-07-22 15:05:46 -04001/*
2 * Copyright (C) 2015 Savoir-faire Linux Inc.
3 * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Alexandre Lision2db8f472015-07-22 15:05:46 -040018 */
19
20#import "RingOutlineView.h"
21
Alexandre Lision4e280d62015-09-09 15:56:30 -040022#import "HoverTableRowView.h" // For the grid drawing shared code
23
Alexandre Lision2db8f472015-07-22 15:05:46 -040024@implementation RingOutlineView
25
26- (NSMenu*)menuForEvent:(NSEvent*)evt
27{
28 NSPoint pt = [self convertPoint:[evt locationInWindow] fromView:nil];
29 int rowIdx = [self rowAtPoint:pt];
30 int colIdx = [self columnAtPoint:pt];
31 if (self.contextMenuDelegate && rowIdx >= 0 && colIdx >= 0) {
32 NSUInteger indexes[2] = {static_cast<NSUInteger>(rowIdx), static_cast<NSUInteger>(colIdx)};
33 NSIndexPath* path = [NSIndexPath indexPathWithIndexes:indexes length:2];
34 return [self.contextMenuDelegate contextualMenuForIndex:path];
35 }
36 return nil;
37}
38
39- (void)keyDown:(NSEvent *)theEvent
40{
41 // Handle the Tab key
42 if ([[theEvent characters] characterAtIndex:0] == NSTabCharacter) {
43 if (([theEvent modifierFlags] & NSShiftKeyMask) != NSShiftKeyMask) {
44 [[self window] selectKeyViewFollowingView:self];
45 } else {
46 [[self window] selectKeyViewPrecedingView:self];
47 }
48 }
49 else if (([theEvent modifierFlags] & NSCommandKeyMask) == NSCommandKeyMask) {
50 if (self.shortcutsDelegate) {
51 if ([[theEvent characters] characterAtIndex:0] == 'a') {
52 [self.shortcutsDelegate onAddShortcut];
53 }
54 }
55 } else
56 [super keyDown:theEvent];
57}
58
Alexandre Lision4e280d62015-09-09 15:56:30 -040059- (CGFloat)yPositionPastLastRow {
60 // Only draw the grid past the last visible row
61 NSInteger numberOfRows = self.numberOfRows;
62 CGFloat yStart = 0;
63 if (numberOfRows > 0) {
64 yStart = NSMaxY([self rectOfRow:numberOfRows - 1]);
65 }
66 return yStart;
67}
68
69- (void)drawGridInClipRect:(NSRect)clipRect {
70 // Only draw the grid past the last visible row
71 CGFloat yStart = [self yPositionPastLastRow];
72 // Draw the first separator one row past the last row
73 yStart += self.rowHeight;
74
75 // One thing to do is smarter clip testing to see if we actually need to draw!
76 NSRect boundsToDraw = self.bounds;
77 NSRect separatorRect = boundsToDraw;
78 separatorRect.size.height = 1;
79 while (yStart < NSMaxY(boundsToDraw)) {
80 separatorRect.origin.y = yStart;
81 DrawSeparatorInRect(separatorRect);
82 yStart += self.rowHeight;
83 }
84}
85
86- (void)setFrameSize:(NSSize)size {
87 [super setFrameSize:size];
88 // We need to invalidate more things when live-resizing since we fill with a gradient and stroke
89 if ([self inLiveResize]) {
90 CGFloat yStart = [self yPositionPastLastRow];
91 if (NSHeight(self.bounds) > yStart) {
92 // Redraw our horizontal grid lines
93 NSRect boundsPastY = self.bounds;
94 boundsPastY.size.height -= yStart;
95 boundsPastY.origin.y = yStart;
96 [self setNeedsDisplayInRect:boundsPastY];
97 }
98 }
99}
100
Alexandre Lision2db8f472015-07-22 15:05:46 -0400101@end