/* * PCProjectEditor.m created by probert on 2002-02-10 09:27:09 +0000 * * Project ProjectCenter * * Created with ProjectCenter - http://www.gnustep.org * * $Id$ */ #include "PCDefines.h" #include "PCProject.h" #include "PCProjectWindow.h" #include "PCProjectBrowser.h" #include "PCProjectEditor.h" #include "PCEditor.h" #include "PCEditorView.h" #include "ProjectComponent.h" NSString *PCEditorDidOpenNotification = @"PCEditorDidOpenNotification"; NSString *PCEditorDidCloseNotification = @"PCEditorDidCloseNotification"; NSString *PCEditorDidBecomeActiveNotification = @"PCEditorDidBecomeActiveNotification"; NSString *PCEditorDidResignActiveNotification = @"PCEditorDidResignActiveNotification"; @interface PCProjectEditor (CreateUI) - (void) _createComponentView; @end @implementation PCProjectEditor (CreateUI) - (void) _createComponentView { NSRect frame; NSTextView *textView; frame = NSMakeRect(0,0,562,248); componentView = [[NSBox alloc] initWithFrame:frame]; [componentView setTitlePosition: NSNoTitle]; [componentView setBorderType: NSNoBorder]; [componentView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable]; [componentView setContentViewMargins: NSMakeSize(0.0,0.0)]; frame = NSMakeRect (0, 0, 562, 40); scrollView = [[NSScrollView alloc] initWithFrame:frame]; [scrollView setHasHorizontalScroller: NO]; [scrollView setHasVerticalScroller: YES]; [scrollView setBorderType: NSBezelBorder]; [scrollView setAutoresizingMask:(NSViewWidthSizable|NSViewHeightSizable)]; // This is a placeholder! frame = [[scrollView contentView] frame]; textView = [[NSTextView alloc] initWithFrame:frame]; [textView setMinSize: NSMakeSize (0, 0)]; [textView setMaxSize: NSMakeSize(1e7, 1e7)]; [textView setRichText: NO]; [textView setEditable: NO]; [textView setSelectable: YES]; [textView setVerticallyResizable: YES]; [textView setHorizontallyResizable: NO]; [textView setAutoresizingMask:(NSViewWidthSizable|NSViewHeightSizable)]; [[textView textContainer] setWidthTracksTextView: YES]; [scrollView setDocumentView: textView]; RELEASE(textView); frame.size = NSMakeSize([scrollView contentSize].width,1e7); [[textView textContainer] setContainerSize:frame.size]; [componentView setContentView:scrollView]; // RELEASE(scrollView); [componentView sizeToFit]; } @end @implementation PCProjectEditor // =========================================================================== // ==== Class Methods // =========================================================================== + (PCEditor *)openFileInEditor:(NSString *)path { NSUserDefaults *ud = [NSUserDefaults standardUserDefaults]; if ([[ud objectForKey:ExternalEditor] isEqualToString:@"YES"]) { NSTask *editorTask; NSMutableArray *args; NSString *editor = [ud objectForKey:Editor]; NSString *app; NSArray *ea = [editor componentsSeparatedByString: @" "]; args = [NSMutableArray arrayWithArray:ea]; app = [args objectAtIndex: 0]; if ([[app pathExtension] isEqualToString:@"app"]) { BOOL ret = [[NSWorkspace sharedWorkspace] openFile:path withApplication:app]; if (ret == NO) { NSLog(@"Could not open %@ using %@",path,app); } return nil; } editorTask = [[NSTask alloc] init]; [editorTask setLaunchPath:app]; [args removeObjectAtIndex: 0]; [args addObject:path]; [editorTask setArguments:args]; AUTORELEASE(editorTask); [editorTask launch]; } else { PCEditor *editor; editor = [[PCEditor alloc] initWithPath:path category:nil]; [editor setWindowed:YES]; [editor show]; return editor; } return nil; } // =========================================================================== // ==== Initialisation // =========================================================================== - (id)initWithProject: (PCProject *)aProject { NSAssert(aProject, @"No project specified!"); if ((self = [super init])) { project = aProject; componentView = nil; editorsDict = [[NSMutableDictionary alloc] init]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(editorDidClose:) name:PCEditorDidCloseNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(editorDidBecomeActive:) name:PCEditorDidBecomeActiveNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(editorDidResignActive:) name:PCEditorDidResignActiveNotification object:nil]; } return self; } - (void)dealloc { NSLog (@"PCProjectEditor: dealloc"); if (componentView) { RELEASE(componentView); } [self closeAllEditors]; RELEASE(editorsDict); [super dealloc]; } - (NSView *)componentView { if (componentView == nil) { [self _createComponentView]; } return componentView; } // =========================================================================== // ==== Project and Editor handling // =========================================================================== - (PCEditor *)editorForFile:(NSString *)path category:(NSString *)category windowed:(BOOL)yn { PCEditor *editor; if (!(editor = [editorsDict objectForKey:path])) { editor = [[PCEditor alloc] initWithPath:path category:category]; [editorsDict setObject:editor forKey:path]; RELEASE(editor); } [editor setWindowed:yn]; [componentView setContentView:[editor componentView]]; [[project projectWindow] makeFirstResponder:[editor editorView]]; if (yn) { [editor show]; } return editor; } - (void)orderFrontEditorForFile:(NSString *)path { PCEditor *editor = [editorsDict objectForKey:path]; if ([editor isWindowed]) { [editor show]; } else { [componentView setContentView:[editor componentView]]; [[project projectWindow] makeFirstResponder:[editor editorView]]; } } - (void)setActiveEditor:(PCEditor *)anEditor { if (anEditor != activeEditor) { activeEditor = anEditor; } } - (PCEditor *)activeEditor { /* NSEnumerator *enumerator = [editorsDict keyEnumerator]; PCEditor *editor; NSString *key; NSWindow *window; while (( key = [enumerator nextObject] )) { editor = [editorsDict objectForKey:key]; window = [editor editorWindow]; if (([window isVisible] && [window isKeyWindow]) || ([[editor componentView] superview] && [[project projectWindow] isKeyWindow])) { return editor; } } return nil;*/ return activeEditor; } - (NSArray *)allEditors { return [editorsDict allValues]; } - (void)closeActiveEditor:(id)sender { [[self activeEditor] closeFile:self]; } - (void)closeEditorForFile:(NSString *)file { PCEditor *editor; editor = [editorsDict objectForKey:file]; [editor closeFile:self]; [editorsDict removeObjectForKey:file]; } - (void)closeAllEditors { NSEnumerator *enumerator = [editorsDict keyEnumerator]; PCEditor *editor; NSString *key; while ((key = [enumerator nextObject])) { editor = [editorsDict objectForKey:key]; [editor closeFile:self]; } [editorsDict removeAllObjects]; } // =========================================================================== // ==== File handling // =========================================================================== - (BOOL)saveAllFiles { NSEnumerator *enumerator = [editorsDict keyEnumerator]; PCEditor *editor; NSString *key; BOOL ret = YES; while(( key = [enumerator nextObject] )) { editor = [editorsDict objectForKey:key]; if( [editor saveFileIfNeeded] == NO ) { ret = NO; } } return ret; } - (BOOL)saveFile { PCEditor *editor = [self activeEditor]; if (editor != nil) { return [editor saveFileIfNeeded]; } return NO; } - (BOOL)saveFileAs:(NSString *)file { PCEditor *editor = [self activeEditor]; if (editor != nil) { BOOL res; BOOL iw = [editor isWindowed]; NSString *c = [editor category]; res = [editor saveFileTo:file]; [editor closeFile:self]; [self editorForFile:file category:c windowed:iw]; return res; } return NO; } - (BOOL)saveFileTo:(NSString *)file { PCEditor *editor = [self activeEditor]; if (editor != nil) { return [editor saveFileTo:file]; } return NO; } - (BOOL)revertFileToSaved { PCEditor *editor = [self activeEditor]; if (editor != nil) { return [editor revertFileToSaved]; } return NO; } // =========================================================================== // ==== Notifications // =========================================================================== - (void)editorDidClose:(NSNotification *)aNotif { PCEditor *editor = [aNotif object]; NSLog(@"PCProjectEditor: editorDidClose"); [editorsDict removeObjectForKey:[editor path]]; if ([editorsDict count]) { NSString *lastEditorKey = [[editorsDict allKeys] lastObject]; PCEditor *lastEditor = [editorsDict objectForKey:lastEditorKey]; lastEditorKey = [[editorsDict allKeys] lastObject]; [componentView setContentView:[lastEditor componentView]]; [[project projectWindow] makeFirstResponder:[lastEditor editorView]]; [self setActiveEditor:lastEditor]; } else { [componentView setContentView:scrollView]; [self setActiveEditor:nil]; } } - (void)editorDidBecomeActive:(NSNotification *)aNotif { PCEditor *editor = [aNotif object]; [self setActiveEditor:editor]; if ([editor category]) { [[project projectBrowser] setPathForFile:[[editor path] lastPathComponent] category:[editor category]]; } } - (void)editorDidResignActive:(NSNotification *)aNotif { } @end