1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
|
/** <title>NSOpenPanel</title> -*-objc-*-
<abstract>Standard open panel for opening files</abstract>
Copyright (C) 1996, 1998, 1999, 2000 Free Software Foundation, Inc.
Author: Scott Christley <scottc@net-community.com>
Date: 1996
Author: Daniel Bhringer <boehring@biomed.ruhr-uni-bochum.de>
Date: August 1998
Source by Daniel Bhringer integrated into Scott Christley's preliminary
implementation by Felipe A. Rodriguez <far@ix.netcom.com>
Author: Nicola Pero <n.pero@mi.flashnet.it>
Date: October 1999 Completely Rewritten.
Author: Mirko Viviani <mirko.viviani@rccr.cremona.it>
Date: September 2000
This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <gnustep/gui/config.h>
#include <string.h>
#include <Foundation/NSString.h>
#include <Foundation/NSArray.h>
#include <Foundation/NSException.h>
#include <AppKit/GMArchiver.h>
#include <AppKit/IMLoading.h>
#include <AppKit/NSApplication.h>
#include <AppKit/NSBrowser.h>
#include <AppKit/NSBrowserCell.h>
#include <AppKit/NSMatrix.h>
#include <AppKit/NSOpenPanel.h>
static NSOpenPanel *_gs_gui_open_panel = nil;
// Pacify the compiler
@interface NSSavePanel (_PrivateMethods)
- (void) _resetDefaults;
- (void) _selectCellName: (NSString *)title;
- (void) _selectTextInColumn: (int)column;
- (void) _setupForDirectory: (NSString *)path file: (NSString *)filename;
- (BOOL) _shouldShowExtension: (NSString *)extension isDir: (BOOL *)isDir;
@end
@implementation NSOpenPanel (_PrivateMethods)
- (void) _resetDefaults
{
[super _resetDefaults];
[self setTitle: @"Open"];
[self setCanChooseFiles: YES];
[self setCanChooseDirectories: YES];
[self setAllowsMultipleSelection: NO];
[_okButton setEnabled: YES];
}
- (BOOL) _shouldShowExtension: (NSString *)extension
isDir: (BOOL *)isDir;
{
BOOL found = YES;
if (_fileTypes)
{
if ([_fileTypes containsObject: extension] == YES)
{
if ([self treatsFilePackagesAsDirectories] == NO)
*isDir = NO;
}
else
found = NO;
}
if (*isDir == YES || (found == YES && _canChooseFiles == YES))
return YES;
return NO;
}
- (void) _selectTextInColumn: (int)column
{
NSMatrix *matrix;
if(column == -1)
return;
matrix = [_browser matrixInColumn:column];
if ([_browser allowsMultipleSelection])
{
NSArray *selectedCells;
selectedCells = [matrix selectedCells];
if([selectedCells count] <= 1)
{
if(_canChooseDirectories == NO ||
[[matrix selectedCell] isLeaf] == YES)
[super _selectTextInColumn:column];
else
{
[self _selectCellName:[[_form cellAtIndex: 0] stringValue]];
// [_form selectTextAtIndex:0];
[_okButton setEnabled:YES];
}
}
else
{
[_form abortEditing];
[[_form cellAtIndex: 0] setStringValue:@""];
// [_form selectTextAtIndex:0];
[_form setNeedsDisplay:YES];
[_okButton setEnabled:YES];
}
}
else
{
if(_canChooseDirectories == NO || [[matrix selectedCell] isLeaf] == YES)
[super _selectTextInColumn:column];
else
{
if([[[_form cellAtIndex: 0] stringValue] length] > 0)
{
[self _selectCellName:[[_form cellAtIndex: 0] stringValue]];
// [_form selectTextAtIndex:0];
[_form setNeedsDisplay:YES];
}
[_okButton setEnabled:YES];
}
}
}
- (void) _selectCellName: (NSString *)title
{
NSString *cellString;
NSArray *cells;
NSMatrix *matrix;
NSComparisonResult result;
NSRange range;
int i, titleLength, cellLength, numberOfCells;
matrix = [_browser matrixInColumn:[_browser lastColumn]];
if([matrix selectedCell])
return;
titleLength = [title length];
if(!titleLength)
{
[_okButton setEnabled:NO];
return;
}
range.location = 0;
range.length = titleLength;
cells = [matrix cells];
numberOfCells = [cells count];
for(i = 0; i < numberOfCells; i++)
{
cellString = [[matrix cellAtRow:i column:0] stringValue];
cellLength = [cellString length];
if(cellLength < titleLength)
continue;
result = [cellString compare:title options:0 range:range];
if(result == NSOrderedSame)
{
[matrix selectCellAtRow:i column:0];
[matrix scrollCellToVisibleAtRow:i column:0];
[_okButton setEnabled:YES];
return;
}
else if(result == NSOrderedDescending)
break;
}
}
- (void) _setupForDirectory: (NSString *)path file: (NSString *)filename
{
if (path == nil)
{
if (_directory)
path = _directory;
else
path = [[NSFileManager defaultManager] currentDirectoryPath];
}
if (filename == nil)
filename = @"";
else if([filename isEqual: @""] == NO)
[_okButton setEnabled: YES];
if (_canChooseDirectories == NO)
{
if([_browser allowsMultipleSelection] == YES)
[_browser setAllowsBranchSelection:NO];
}
[super _setupForDirectory: path file: filename];
}
@end
@implementation NSOpenPanel
/*
* Class methods
*/
+ (void) initialize
{
if (self == [NSOpenPanel class])
{
[self setVersion: 1];
}
}
/*
* Accessing the NSOpenPanel shared instance
*/
+ (NSOpenPanel *) openPanel
{
if (!_gs_gui_open_panel)
_gs_gui_open_panel = [[NSOpenPanel alloc] init];
[_gs_gui_open_panel _resetDefaults];
return _gs_gui_open_panel;
}
- (id) init
{
[super init];
_canChooseDirectories = YES;
_canChooseFiles = YES;
return self;
}
// dealloc is the same as NSSavePanel
/*
* Filtering Files
*/
- (void) setAllowsMultipleSelection: (BOOL)flag
{
[_browser setAllowsMultipleSelection: flag];
}
- (BOOL) allowsMultipleSelection
{
return [_browser allowsMultipleSelection];
}
- (void) setCanChooseDirectories: (BOOL)flag
{
_canChooseDirectories = flag;
[_browser setAllowsBranchSelection: flag];
}
- (BOOL) canChooseDirectories
{
return _canChooseDirectories;
}
- (void) setCanChooseFiles: (BOOL)flag
{
_canChooseFiles = flag;
}
- (BOOL) canChooseFiles
{
return _canChooseFiles;
}
- (NSString*) filename
{
NSArray *ret;
ret = [self filenames];
if ([ret count] == 1)
return [ret objectAtIndex: 0];
else
return nil;
}
/*
* Querying the Chosen Files
*/
- (NSArray *) filenames
{
if ([_browser allowsMultipleSelection])
{
NSArray *cells = [_browser selectedCells];
NSEnumerator *cellEnum = [cells objectEnumerator];
NSBrowserCell *currCell;
NSMutableArray *ret = [NSMutableArray array];
NSString *dir = [self directory];
if ([_browser selectedColumn] != [_browser lastColumn])
{
/*
* The last column doesn't have anything selected - so we must
* have selected a directory.
*/
if (_canChooseDirectories == YES)
{
[ret addObject: dir];
}
}
else
{
while ((currCell = [cellEnum nextObject]))
{
[ret addObject: [NSString stringWithFormat: @"%@/%@", dir,
[currCell stringValue]]];
}
}
return ret;
}
else
{
if (_canChooseDirectories == YES)
{
if ([_browser selectedColumn] != [_browser lastColumn])
return [NSArray arrayWithObject: [self directory]];
}
return [NSArray arrayWithObject: [super filename]];
}
}
- (NSArray *) URLs
{
NSMutableArray *ret = [NSMutableArray new];
NSEnumerator *enumerator = [[self filenames] objectEnumerator];
NSString *filename;
while ((filename = [enumerator nextObject]) != nil)
{
[ret addObject: [NSURL fileURLWithPath: filename]];
}
return AUTORELEASE(ret);
}
/*
* Running the NSOpenPanel
*/
- (int) runModalForTypes: (NSArray *)fileTypes
{
return [self runModalForDirectory: nil
file: nil
types: fileTypes];
}
- (int) runModalForDirectory: (NSString *)path
file: (NSString *)name
types: (NSArray *)fileTypes
{
ASSIGN (_fileTypes, fileTypes);
return [self runModalForDirectory: path
file: name];
}
- (int)runModalForDirectory:(NSString *)path
file:(NSString *)name
types:(NSArray *)fileTypes
relativeToWindow:(NSWindow*)window
{
ASSIGN (_fileTypes, fileTypes);
return [self runModalForDirectory: path
file: name
relativeToWindow: window];
}
- (void)beginSheetForDirectory:(NSString *)path
file:(NSString *)name
types:(NSArray *)fileTypes
modalForWindow:(NSWindow *)docWindow
modalDelegate:(id)delegate
didEndSelector:(SEL)didEndSelector
contextInfo:(void *)contextInfo
{
ASSIGN (_fileTypes, fileTypes);
[self beginSheetForDirectory: path
file: name
modalForWindow: docWindow
modalDelegate: delegate
didEndSelector: didEndSelector
contextInfo: contextInfo];
}
- (void) ok: (id)sender
{
NSMatrix *matrix = nil;
NSBrowserCell *selectedCell = nil;
NSArray *selectedCells = nil;
int selectedColumn, lastColumn;
selectedColumn = [_browser selectedColumn];
lastColumn = [_browser lastColumn];
if (selectedColumn >= 0)
{
matrix = [_browser matrixInColumn: selectedColumn];
if ([_browser allowsMultipleSelection] == YES)
{
selectedCells = [matrix selectedCells];
if (selectedColumn == lastColumn &&
[selectedCells count] == 1)
selectedCell = [selectedCells objectAtIndex: 0];
}
else
{
if (_canChooseDirectories == NO)
{
if (selectedColumn == lastColumn)
selectedCell = [matrix selectedCell];
}
else if (selectedColumn == lastColumn)
selectedCell = [matrix selectedCell];
}
}
if (selectedCell)
{
if ([selectedCell isLeaf] == NO)
{
[[_form cellAtIndex: 0] setStringValue: @""];
[_browser doClick: matrix];
[_form selectTextAtIndex: 0];
[_form setNeedsDisplay: YES];
return;
}
}
else if (_canChooseDirectories == NO
&& (![_browser allowsMultipleSelection] || !selectedCells
|| selectedColumn != lastColumn || ![selectedCells count]))
{
[_form selectTextAtIndex: 0];
[_form setNeedsDisplay: YES];
return;
}
ASSIGN (_directory, [_browser pathToColumn:[_browser lastColumn]]);
if (selectedCell)
ASSIGN (_fullFileName, [_directory stringByAppendingPathComponent:
[selectedCell stringValue]]);
else
ASSIGN (_fullFileName, [_directory stringByAppendingPathComponent:
[[_form cellAtIndex: 0] stringValue]]);
if (_delegateHasValidNameFilter)
{
NSEnumerator *enumerator;
NSArray *filenames = [self filenames];
NSString *filename;
enumerator = [filenames objectEnumerator];
while ((filename = [enumerator nextObject]))
{
if ([_delegate panel: self isValidFilename: filename] == NO)
return;
}
}
_OKButtonPressed = YES;
[NSApp stopModal];
[self close];
}
/*
* NSCoding protocol
*/
- (void) encodeWithCoder: (NSCoder*)aCoder
{
[super encodeWithCoder: aCoder];
[aCoder encodeValueOfObjCType: @encode(BOOL) at: &_canChooseDirectories];
[aCoder encodeValueOfObjCType: @encode(BOOL) at: &_canChooseFiles];
}
- (id) initWithCoder: (NSCoder*)aDecoder
{
[super initWithCoder: aDecoder];
[aDecoder decodeValueOfObjCType: @encode(BOOL) at: &_canChooseDirectories];
[aDecoder decodeValueOfObjCType: @encode(BOOL) at: &_canChooseFiles];
return self;
}
@end
//
// NSOpenPanel browser delegate methods
//
@interface NSOpenPanel (_BrowserDelegate)
- (BOOL) browser: (NSBrowser *)sender
selectCellWithString: (NSString *)title
inColumn: (int)column;
@end
@implementation NSOpenPanel (_BrowserDelegate)
- (BOOL) browser: (NSBrowser *)sender
selectCellWithString: (NSString *)title
inColumn: (int)column
{
NSMatrix *m;
NSArray *c;
BOOL isLeaf;
m = [_browser matrixInColumn: column];
c = [m selectedCells];
if ([c count] == 1)
{
isLeaf = [[c objectAtIndex: 0] isLeaf];
if (_canChooseDirectories == NO)
{
[_okButton setEnabled: isLeaf];
return [super browser: sender
selectCellWithString: title
inColumn: column];
}
else // _canChooseDirectories
{
BOOL ret;
ret = [super browser: sender
selectCellWithString: title
inColumn: column];
if (isLeaf == NO)
ASSIGN (_fullFileName, _directory);
return ret;
}
}
else // Multiple Selection, and it is not the first item of the selection
{
return YES;
}
}
@end
//
// NSForm delegate methods
//
@interface NSOpenPanel (FormDelegate)
- (void) controlTextDidChange: (NSNotification *)aNotification;
@end
@implementation NSOpenPanel (FormDelegate)
- (void) controlTextDidChange: (NSNotification *)aNotification;
{
NSString *s, *selectedString;
NSArray *cells;
NSMatrix *matrix;
NSCell *selectedCell;
int i, sLength, cellLength, selectedRow;
NSComparisonResult result;
NSRange range;
matrix = [_browser matrixInColumn: [_browser lastColumn]];
s = [[[aNotification userInfo] objectForKey: @"NSFieldEditor"] string];
sLength = [s length];
range.location = 0;
range.length = sLength;
if(sLength == 0)
{
[matrix deselectAllCells];
if(_canChooseDirectories == NO)
[_okButton setEnabled: NO];
return;
}
selectedCell = [matrix selectedCell];
selectedString = [selectedCell stringValue];
selectedRow = [matrix selectedRow];
cells = [matrix cells];
if(selectedString)
{
cellLength = [selectedString length];
if(cellLength < sLength)
range.length = cellLength;
result = [selectedString compare: s options: 0 range: range];
if(result == NSOrderedSame)
return;
else if(result == NSOrderedAscending)
result = NSOrderedDescending;
else if(result == NSOrderedDescending)
result = NSOrderedAscending;
range.length = sLength;
}
else
result = NSOrderedDescending;
if(result == NSOrderedDescending)
{
int numberOfCells = [cells count];
for(i = selectedRow+1; i < numberOfCells; i++)
{
selectedString = [[matrix cellAtRow: i column: 0] stringValue];
cellLength = [selectedString length];
if(cellLength < sLength)
continue;
result = [selectedString compare: s options: 0 range: range];
if(result == NSOrderedSame)
{
[matrix deselectAllCells];
[matrix selectCellAtRow: i column: 0];
[matrix scrollCellToVisibleAtRow: i column: 0];
[_okButton setEnabled: YES];
return;
}
}
}
else
{
for(i = selectedRow; i >= 0; --i)
{
selectedString = [[matrix cellAtRow: i column: 0] stringValue];
cellLength = [selectedString length];
if(cellLength < sLength)
continue;
result = [selectedString compare: s options: 0 range: range];
if(result == NSOrderedSame)
{
[matrix deselectAllCells];
[matrix selectCellAtRow: i column: 0];
[matrix scrollCellToVisibleAtRow: i column: 0];
[_okButton setEnabled: YES];
return;
}
}
}
[matrix deselectAllCells];
[_okButton setEnabled: YES];
}
@end /* NSOpenPanel */
|