apps-projectcenter/PCLib/PCEditor.m
Philippe C.D. Robert bae1bafa98 Fixed 2 bugs which caused a severe crash.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/apps/projectcenter/trunk@12551 72102866-910b-0410-8b05-ffd578937521
2002-02-17 13:14:51 +00:00

298 lines
6.7 KiB
Objective-C

/*
* PCEditor.m created by probert on 2002-01-29 20:37:27 +0000
*
* Project ProjectCenter
*
* Created with ProjectCenter - http://www.gnustep.org
*
* $Id$
*/
#import "PCEditor.h"
#import "PCEditorView.h"
#import "PCProjectEditor.h"
NSString *PCEditorDidBecomeKeyNotification=@"PCEditorDidBecomeKeyNotification";
NSString *PCEditorDidResignKeyNotification=@"PCEditorDidResignKeyNotification";
@interface PCEditor (InitUI)
- (void)_initUI;
@end
@implementation PCEditor (InitUI)
- (void)_initUI
{
NSScrollView *scrollView;
NSLayoutManager *lm;
NSTextContainer *tc;
unsigned int style = NSTitledWindowMask
| NSClosableWindowMask
| NSMiniaturizableWindowMask
| NSResizableWindowMask;
NSRect rect = NSMakeRect(100,100,512,320);
window = [[NSWindow alloc] initWithContentRect:rect
styleMask:style
backing:NSBackingStoreBuffered
defer:YES];
[window setReleasedWhenClosed:NO];
[window setMinSize:NSMakeSize(512,320)];
rect = [[window contentView] frame];
rect.origin.x = -1;
rect.origin.y = -1;
rect.size.width += 2;
scrollView = [[NSScrollView alloc] initWithFrame:rect];
// Now the text editing stuff
storage = [[NSTextStorage alloc] init];
lm = [[NSLayoutManager alloc] init];
[storage addLayoutManager:lm];
RELEASE(lm);
rect.origin.x = 0;
rect.origin.y = 0;
rect.size.height -= 24;
rect.size.width -= 4;
tc = [[NSTextContainer alloc] initWithContainerSize:rect.size];
[lm addTextContainer:tc];
RELEASE(tc);
iView = [[PCEditorView alloc] initWithFrame:rect
textContainer:tc];
[iView setEditor:self];
[iView setMinSize:NSMakeSize (0, 0)];
[iView setMaxSize:NSMakeSize(1e7, 1e7)];
[iView setRichText:NO];
[iView setEditable:YES];
[iView setSelectable:YES];
[iView setVerticallyResizable:YES];
[iView setHorizontallyResizable:NO];
[iView setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];
[iView setBackgroundColor:[NSColor whiteColor]];
[[iView textContainer] setWidthTracksTextView:YES];
lm = [[NSLayoutManager alloc] init];
[storage addLayoutManager:lm];
RELEASE(lm);
tc = [[NSTextContainer alloc] initWithContainerSize:rect.size];
[lm addTextContainer:tc];
RELEASE(tc);
eView = [[PCEditorView alloc] initWithFrame:rect
textContainer:tc];
[eView setEditor:self];
[eView setMinSize: NSMakeSize (0, 0)];
[eView setMaxSize:NSMakeSize(1e7, 1e7)];
[eView setRichText:NO];
[eView setEditable:YES];
[eView setSelectable:YES];
[eView setVerticallyResizable:YES];
[eView setHorizontallyResizable:NO];
[eView setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];
[eView setBackgroundColor:[NSColor whiteColor]];
[[eView textContainer] setWidthTracksTextView:YES];
[scrollView setDocumentView:eView];
RELEASE(eView);
rect.size = NSMakeSize([scrollView contentSize].width,1e7);
[[eView textContainer] setContainerSize:rect.size];
[scrollView setHasHorizontalScroller: YES];
[scrollView setHasVerticalScroller: YES];
[scrollView setBorderType: NSBezelBorder];
[scrollView setAutoresizingMask: (NSViewWidthSizable | NSViewHeightSizable)];
[window setContentView:scrollView];
[window setDelegate:self];
[window makeFirstResponder:eView];
RELEASE(scrollView);
}
@end
@implementation PCEditor
- (id)initWithPath:(NSString*)file
{
if((self = [super init]))
{
NSString *t = [NSString stringWithContentsOfFile:file];
NSAttributedString *as = [[NSAttributedString alloc] initWithString:t];
isEdited = NO;
path = [file copy];
[self _initUI];
[window setTitle:file];
[storage setAttributedString:as];
RELEASE(as);
[iView setNeedsDisplay:YES];
[eView setNeedsDisplay:YES];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(textDidChange:)
name:NSTextDidChangeNotification
object:eView];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(textDidChange:)
name:NSTextDidChangeNotification
object:iView];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
RELEASE(window);
RELEASE(path);
RELEASE(iView);
RELEASE(storage);
[super dealloc];
}
- (void)setDelegate:(id)aDelegate
{
delegate = aDelegate;
}
- (id)delegate
{
return delegate;
}
- (NSWindow *)editorWindow
{
return window;
}
- (NSString *)path
{
return path;
}
- (void)setIsEdited:(BOOL)yn
{
[window setDocumentEdited:yn];
isEdited = yn;
}
- (void)showInProjectEditor:(PCProjectEditor *)pe
{
[pe setEditorView:iView];
}
- (void)show
{
[window makeKeyAndOrderFront:self];
}
- (void)close
{
if( isEdited )
{
BOOL ret;
if( [window isVisible] )
{
[window makeKeyAndOrderFront:self];
}
ret = NSRunAlertPanel(@"Edited File!",
@"Should '%@' be saved before closing?",
@"Yes",@"No",nil,path);
if( ret == YES )
{
ret = [self saveFile];
if( ret == NO )
{
NSRunAlertPanel(@"Save Failed!",
@"Could not save file '%@'!",
@"OK",nil,nil,path);
}
}
[self setIsEdited:NO];
}
if( delegate && [delegate respondsToSelector:@selector(editorDidClose:)] )
{
[delegate editorDidClose:self];
}
}
- (BOOL)saveFile
{
[self setIsEdited:NO];
// Operate on the text storage!
return [[storage string] writeToFile:path atomically:YES];
}
- (BOOL)revertFile
{
NSString *text = [NSString stringWithContentsOfFile:path];
NSAttributedString *as = [[NSAttributedString alloc] initWithString:text];
[self setIsEdited:NO];
// Operate on the text storage!
[storage setAttributedString:as];
RELEASE(as);
[iView setNeedsDisplay:YES];
[eView setNeedsDisplay:YES];
}
- (void)windowWillClose:(NSNotification *)aNotification
{
if( [[aNotification object] isEqual:window] )
{
[self close];
}
}
- (void)windowDidBecomeKey:(NSNotification *)aNotification
{
if( [[aNotification object] isEqual:window] )
{
[[NSNotificationCenter defaultCenter] postNotificationName:PCEditorDidBecomeKeyNotification object:self];
}
}
- (void)windowDidResignKey:(NSNotification *)aNotification
{
if( [[aNotification object] isEqual:window] )
{
[[NSNotificationCenter defaultCenter] postNotificationName:PCEditorDidResignKeyNotification object:self];
}
}
- (void)textDidChange:(NSNotification *)aNotification
{
[self setIsEdited:YES];
}
@end