The R Project SVN R-packages

Rev

Rev 5875 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5868 bibiko 1
/*
2
 *  R.app : a Cocoa front end to: "R A Computer Language for Statistical Data Analysis"
3
 *  
4
 *  R.app Copyright notes:
5
 *                     Copyright (C) 2004-11  The R Foundation
6
 *                     written by Stefano M. Iacus and Simon Urbanek
7
 *
8
 *                  
9
 *  R Copyright notes:
10
 *                     Copyright (C) 1995-1996   Robert Gentleman and Ross Ihaka
11
 *                     Copyright (C) 1998-2001   The R Development Core Team
12
 *                     Copyright (C) 2002-2004   The R Foundation
13
 *
14
 *  This program is free software; you can redistribute it and/or modify
15
 *  it under the terms of the GNU General Public License as published by
16
 *  the Free Software Foundation; either version 2 of the License, or
17
 *  (at your option) any later version.
18
 *
19
 *  This program is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  A copy of the GNU General Public License is available via WWW at
25
 *  http://www.gnu.org/copyleft/gpl.html.  You can also obtain it by
26
 *  writing to the Free Software Foundation, Inc., 59 Temple Place,
27
 *  Suite 330, Boston, MA  02111-1307  USA.
28
 *
29
 *  Code snippets were taken from http://cocotron.googlecode.com/svn/trunk/AppKit/NSTableHeaderView.m
30
 *  Copyright (c) 2006-2007 Christopher J. W. Lloyd
31
 *
32
 *
33
 *  RDataEditorTableHeaderView.m
34
 *
35
 *  Created by Hans-J. Bibiko on 05/07/2011.
36
 *
37
 */
38
 
39
#import "RDataEditorTableHeaderView.h"
40
#import "../REditor.h"
41
 
42
@interface RDataEditorTableHeaderView (Private)
43
 
44
- (NSRect)_resizeRectBeforeColumn:(NSInteger)column;
45
 
46
@end
47
 
48
@implementation RDataEditorTableHeaderView (private)
49
 
50
- (NSRect)_resizeRectBeforeColumn:(NSInteger)column
51
{
52
	NSRect rect=[self headerRectOfColumn:column];
53
 
54
	rect.origin.x -= 2;
55
	rect.size.width = 6;
56
 
57
	return rect;
58
}
59
 
60
@end
61
 
62
#pragma mark -
63
 
64
@implementation RDataEditorTableHeaderView
65
 
5875 bibiko 66
// -(void)mouseDown:(NSEvent *)theEvent
67
// {
68
// 	// Submit pending changes of column names
69
// 	if([[[NSApp keyWindow] firstResponder] isKindOfClass:[NSTextView class]])
7824 urbaneks 70
// 		[[self.tableView delegate] textView:(NSTextView*)[[NSApp keyWindow] firstResponder] doCommandBySelector:@selector(insertNewline:)];
5875 bibiko 71
// }
72
// 
5868 bibiko 73
-(void)mouseDown:(NSEvent *)theEvent
74
{
5875 bibiko 75
	NSPoint location = [self convertPoint:[theEvent locationInWindow] fromView:nil];
76
	NSInteger clickedColumn = [self columnAtPoint:location];
77
 
5868 bibiko 78
	// Submit pending changes of column names
79
	if([[[NSApp keyWindow] firstResponder] isKindOfClass:[NSTextView class]])
7824 urbaneks 80
		[[self.tableView delegate] textView:(NSTextView*)[[NSApp keyWindow] firstResponder] doCommandBySelector:@selector(insertNewline:)];
5868 bibiko 81
 
82
	// check if delegate implemented tableView:mouseDownInHeaderOfTableColumn:
7824 urbaneks 83
	if ([[self.tableView delegate] respondsToSelector:@selector(tableView:mouseDownInHeaderOfTableColumn:)]) {
5868 bibiko 84
		if(clickedColumn != -1)
7824 urbaneks 85
			[[self.tableView delegate] tableView:self.tableView mouseDownInHeaderOfTableColumn:[[self.tableView tableColumns] objectAtIndex:clickedColumn]];
5868 bibiko 86
		if([[NSApp currentEvent] clickCount] > 1) {
87
			// deselect clicked column for editing column name and return
7824 urbaneks 88
			[self.tableView deselectAll:nil];
5868 bibiko 89
			return;
90
		}
91
	}
92
 
93
	// resizing mode
7824 urbaneks 94
	if ([self.tableView allowsColumnResizing]) {
95
		NSInteger i, count=[[self.tableView tableColumns] count];
5868 bibiko 96
 
97
		// ends editing
7824 urbaneks 98
		if ([self.tableView editedColumn] != -1 || [self.tableView editedRow] != -1)
5868 bibiko 99
			[[self window] endEditingFor:nil];
100
 
101
		for (i = 1; i < count; ++i) {
102
			if (NSMouseInRect(location, [self _resizeRectBeforeColumn:i], [self isFlipped])) {
7824 urbaneks 103
				NSTableColumn *resizingColumn = [[self.tableView tableColumns] objectAtIndex:i-1];
5868 bibiko 104
 
105
				if ([resizingColumn resizingMask] == NSTableColumnNoResizing)
106
					return;
107
 
7824 urbaneks 108
				//FIXME: self.resizedColumn = i - 1;
5868 bibiko 109
				location=[self convertPoint:[theEvent locationInWindow] fromView:nil];
110
				do {
111
					NSPoint newPoint;
112
					NSRect newRect;
113
					NSInteger q;
114
					CGFloat newWidth=newPoint.x;
115
 
116
					theEvent=[[self window] nextEventMatchingMask:NSLeftMouseUpMask|NSLeftMouseDraggedMask];
117
					newPoint=[self convertPoint:[theEvent locationInWindow] fromView:nil];
118
 
119
					newWidth=newPoint.x;
7824 urbaneks 120
					for (q = 0; q < self.resizedColumn; ++q) {
121
						newWidth -= [[[self.tableView tableColumns] objectAtIndex:q] width];
122
						newWidth -= [self.tableView intercellSpacing].width;
5868 bibiko 123
					}
124
 
125
					[resizingColumn setWidth:newWidth];
126
 
7824 urbaneks 127
					[self.tableView tile];
128
					newRect.origin=[self.tableView convertPoint:newPoint fromView:self];
5868 bibiko 129
					newRect.size=NSMakeSize(10,10);
7824 urbaneks 130
					[self.tableView scrollRectToVisible:newRect];
5868 bibiko 131
 
132
					location=newPoint;
133
				} while ([theEvent type] != NSLeftMouseUp);
134
 
135
				[[self window] invalidateCursorRectsForView:self];
136
 
7824 urbaneks 137
				//self.resizedColumn = -1;
5868 bibiko 138
				return;
139
			}
140
		}
141
	}
142
 
143
	// column selection including the chance to select non-continous columns via holding ⌘
7824 urbaneks 144
	if ([self.tableView allowsColumnSelection]) {
5868 bibiko 145
		// extend/change selection
146
		if ([theEvent modifierFlags] & NSCommandKeyMask) {
147
			// deselect previously selected?
7824 urbaneks 148
			if ([self.tableView isColumnSelected:clickedColumn])
149
				[self.tableView deselectColumn:clickedColumn];
150
			else if ([self.tableView allowsMultipleSelection] == YES) {
5868 bibiko 151
				// add to selection
7824 urbaneks 152
				[self.tableView selectColumnIndexes:[NSIndexSet indexSetWithIndex:clickedColumn] byExtendingSelection:YES];
5868 bibiko 153
			}
154
		}
155
		else if ([theEvent modifierFlags] & NSShiftKeyMask) {
7824 urbaneks 156
			NSInteger firstColumn = [self.tableView selectedColumn];
5868 bibiko 157
			NSInteger lastColumn  = clickedColumn;
158
 
159
			if (firstColumn == -1)
160
				firstColumn = 0;
161
			if (firstColumn > lastColumn) {
162
				lastColumn = firstColumn;
163
				firstColumn = clickedColumn;
164
			}
165
 
7824 urbaneks 166
			[self.tableView deselectAll:nil];
5868 bibiko 167
			while (firstColumn <= lastColumn)
7824 urbaneks 168
				[self.tableView selectColumnIndexes:[NSIndexSet indexSetWithIndex:firstColumn++] byExtendingSelection:YES];
5868 bibiko 169
		}
170
		else
7824 urbaneks 171
			[self.tableView selectColumnIndexes:[NSIndexSet indexSetWithIndex:clickedColumn] byExtendingSelection:NO];
5868 bibiko 172
	}
173
 
174
}
175
 
176
@end