The R Project SVN R-packages

Rev

Rev 1256 | Rev 5708 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1254 iacus 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-5  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
 
1252 urbaneks 30
#import "RGUI.h"
1211 iacus 31
#import "SelectList.h"
32
 
33
static SelectList *sharedController = nil;
34
 
35
@implementation SelectList
36
 
1223 iacus 37
BOOL IsSelectList;
38
 
1211 iacus 39
- (id)init
40
{
41
    self = [super init];
42
    if (self) {
43
		sharedController = self;
44
		[listDataSource setTarget: self];
45
		totalItems = 0;
46
		listItem = 0;
1252 urbaneks 47
		result = -1;
1255 urbaneks 48
		title = nil;
1252 urbaneks 49
		running = NO;
1211 iacus 50
    }
51
 
52
    return self;
53
}
54
 
55
+ (SelectList*) sharedController
56
{
57
	return sharedController;
58
}
59
 
60
- (void)dealloc {
61
	[super dealloc];
62
}
1252 urbaneks 63
 
1211 iacus 64
/* These two routines are needed to update the SelectList TableView */
65
- (int)numberOfRowsInTableView: (NSTableView *)tableView
66
{
67
	return totalItems;
68
}
69
 
70
- (id)tableView: (NSTableView *)tableView objectValueForTableColumn: (NSTableColumn *)tableColumn row: (int)row
71
{
72
	if (row<totalItems) {
73
		if([[tableColumn identifier] isEqualToString:@"item"])
1252 urbaneks 74
			return listItem[row];
1211 iacus 75
	}
76
	return nil; 
77
}
78
 
79
- (id) window
80
{
81
	return SelectListWindow;
82
}
83
 
84
- (void) reloadData
85
{
86
	[listDataSource reloadData];
87
}
88
 
89
- (void) resetListItems
90
{
1252 urbaneks 91
	if (!totalItems || !listItem) return;
1211 iacus 92
	int i=0;
93
	while (i<totalItems) {
1252 urbaneks 94
		[listItem[i] release];
1211 iacus 95
		i++;
96
	}
97
	free(listItem);
98
	totalItems=0;
99
}
100
 
1255 urbaneks 101
- (int) selectList: (int) count withNames: (char**) item status: (BOOL*) stat multiple: (BOOL) multiple title: (NSString*) ttl
1211 iacus 102
{
1255 urbaneks 103
	NSAutoreleasePool *pool = nil;
104
	int i=0, k=0;
1252 urbaneks 105
	title = ttl;
106
	result = -1;
1255 urbaneks 107
 
1223 iacus 108
	[listDataSource setAllowsMultipleSelection: multiple];
109
 
1211 iacus 110
	if (totalItems) [self resetListItems];
1252 urbaneks 111
	if (count<1)
1255 urbaneks 112
		return 0;	
1211 iacus 113
 
1252 urbaneks 114
	listItem = (NSString**) malloc(sizeof(NSString*) * count);
1211 iacus 115
	while (i<count) {
1252 urbaneks 116
		listItem[i] =[[NSString alloc] initWithUTF8String: item[i]];
1211 iacus 117
		i++;
118
	}
119
	totalItems = count;
1256 urbaneks 120
	[listDataSource deselectAll:self];
121
	[listDataSource reloadData]; // make sure we have all data loaded before proceeding with the selection
1255 urbaneks 122
 
123
	pool = [[NSAutoreleasePool alloc] init];	
124
 
125
	if (stat)
126
		for(i=0; i<totalItems; i++){
127
			if(stat[i]){
128
				k++;
129
				[listDataSource selectRowIndexes:[NSIndexSet indexSetWithIndex:i] 
130
							byExtendingSelection:(k!=1)?YES:NO];
131
			}
132
		};
133
 
134
	[[self window] setTitle:title];
1256 urbaneks 135
	running = YES;
136
	{ /* try to be smart - resize the window such that the all items are visible (with an upper limit) */
137
		NSRect rw = [[self window] frame];
138
		NSRect rf = [listDataSource visibleRect];
139
		NSRect rr = [listDataSource frameOfCellAtColumn:0 row:0];
140
		NSRect rl = [listDataSource frameOfCellAtColumn:0 row:1];
141
 
142
		float nonTableH = rw.size.height - rf.size.height;
143
		float showCount = ((float)((count>20)?20:count));
144
 
1481 urbaneks 145
		if (count==1) rl.origin.y = rr.origin.y + rr.size.height + 1.0; // rl will be bogus if there is just 1 entry, so we need to fake it
146
 
1256 urbaneks 147
		rw.size.height = nonTableH + rr.origin.y + (rl.origin.y-rr.origin.y)*showCount - 1.0;
148
 
149
		[[self window] setFrame:rw display:YES];
150
	}
1255 urbaneks 151
	[self show];
152
	[NSApp runModalForWindow:[self window]];
153
 
154
	if (result == 1) {
155
		NSIndexSet *rows =  [listDataSource selectedRowIndexes];			
156
		unsigned current_index = [rows firstIndex];
157
 
158
		if(current_index == NSNotFound)
159
			return 0;
160
 
161
		if (stat) {
162
			memset(stat, 0, sizeof(BOOL)*count);
163
			while (current_index != NSNotFound) {
164
				stat[current_index] = YES;
165
				current_index = [rows indexGreaterThanIndex: current_index];
166
			}
167
		}
168
	}
169
 
170
	[pool release];
171
	[self resetListItems];
172
 
173
	return result;
1211 iacus 174
}
175
 
176
- (int) count
177
{
178
	return totalItems;
179
}
180
 
181
- (void) show
182
{
183
	[listDataSource reloadData];
184
	[[self window] makeKeyAndOrderFront:self];
185
}
186
 
1223 iacus 187
 
188
- (IBAction)returnSelected:(id)sender
189
{
1252 urbaneks 190
	result = 1;
1223 iacus 191
 
192
	[[self window] performClose: sender];
193
 
194
}
195
 
196
- (BOOL)windowShouldClose:(id)sender{
197
 
1252 urbaneks 198
	if(running){
1223 iacus 199
		[NSApp stopModal];
1252 urbaneks 200
		running = NO;
1223 iacus 201
	}
202
	return YES;	
203
}
204
 
205
- (IBAction)cancelSelection:(id)sender
206
{
1252 urbaneks 207
	result = 0;
1223 iacus 208
 
209
	[[self window] performClose: sender];
210
}
211
 
1211 iacus 212
@end