2002-01-29 21:29:30 +00:00
|
|
|
/*
|
|
|
|
* 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"
|
2002-02-11 22:19:39 +00:00
|
|
|
#import "PCProjectEditor.h"
|
2002-01-29 21:29:30 +00:00
|
|
|
|
2002-02-02 21:12:49 +00:00
|
|
|
NSString *PCEditorDidBecomeKeyNotification=@"PCEditorDidBecomeKeyNotification";
|
|
|
|
NSString *PCEditorDidResignKeyNotification=@"PCEditorDidResignKeyNotification";
|
|
|
|
|
2002-01-29 21:29:30 +00:00
|
|
|
@interface PCEditor (InitUI)
|
|
|
|
|
|
|
|
- (void)_initUI;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation PCEditor (InitUI)
|
|
|
|
|
|
|
|
- (void)_initUI
|
|
|
|
{
|
|
|
|
NSScrollView *scrollView;
|
2002-02-11 22:19:39 +00:00
|
|
|
NSLayoutManager *lm;
|
|
|
|
NSTextContainer *tc;
|
2002-01-29 21:29:30 +00:00
|
|
|
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)];
|
|
|
|
|
2002-02-03 16:30:35 +00:00
|
|
|
rect = [[window contentView] frame];
|
|
|
|
rect.origin.x = -1;
|
|
|
|
rect.origin.y = -1;
|
|
|
|
rect.size.width += 2;
|
|
|
|
|
|
|
|
scrollView = [[NSScrollView alloc] initWithFrame:rect];
|
|
|
|
|
2002-02-11 22:19:39 +00:00
|
|
|
// Now the text editing stuff
|
|
|
|
storage = [[NSTextStorage alloc] init];
|
|
|
|
|
|
|
|
lm = [[NSLayoutManager alloc] init];
|
|
|
|
|
2002-02-03 16:30:35 +00:00
|
|
|
rect.origin.x = 0;
|
|
|
|
rect.origin.y = 0;
|
|
|
|
rect.size.height -= 24;
|
|
|
|
rect.size.width -= 4;
|
|
|
|
|
2002-02-11 22:19:39 +00:00
|
|
|
tc = [[NSTextContainer alloc] initWithContainerSize:rect.size];
|
|
|
|
[lm addTextContainer:tc];
|
|
|
|
RELEASE(tc);
|
|
|
|
|
2002-08-10 12:10:55 +00:00
|
|
|
[storage addLayoutManager:lm];
|
|
|
|
RELEASE(lm);
|
|
|
|
|
2002-02-11 22:19:39 +00:00
|
|
|
iView = [[PCEditorView alloc] initWithFrame:rect
|
|
|
|
textContainer:tc];
|
|
|
|
[iView setEditor:self];
|
|
|
|
|
|
|
|
[iView setMinSize:NSMakeSize (0, 0)];
|
|
|
|
[iView setMaxSize:NSMakeSize(1e7, 1e7)];
|
2002-03-02 13:58:13 +00:00
|
|
|
[iView setRichText:YES];
|
|
|
|
[iView setUsesFontPanel:YES];
|
2002-02-11 22:19:39 +00:00
|
|
|
[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];
|
|
|
|
|
|
|
|
tc = [[NSTextContainer alloc] initWithContainerSize:rect.size];
|
|
|
|
[lm addTextContainer:tc];
|
|
|
|
RELEASE(tc);
|
|
|
|
|
2002-08-10 12:10:55 +00:00
|
|
|
[storage addLayoutManager:lm];
|
|
|
|
RELEASE(lm);
|
|
|
|
|
2002-02-11 22:19:39 +00:00
|
|
|
eView = [[PCEditorView alloc] initWithFrame:rect
|
|
|
|
textContainer:tc];
|
|
|
|
[eView setEditor:self];
|
|
|
|
|
|
|
|
[eView setMinSize: NSMakeSize (0, 0)];
|
|
|
|
[eView setMaxSize:NSMakeSize(1e7, 1e7)];
|
2002-03-02 13:58:13 +00:00
|
|
|
[eView setRichText:YES];
|
|
|
|
[eView setUsesFontPanel:YES];
|
2002-02-11 22:19:39 +00:00
|
|
|
[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);
|
2002-01-29 21:29:30 +00:00
|
|
|
|
2002-02-03 16:30:35 +00:00
|
|
|
rect.size = NSMakeSize([scrollView contentSize].width,1e7);
|
2002-02-11 22:19:39 +00:00
|
|
|
[[eView textContainer] setContainerSize:rect.size];
|
2002-01-29 21:29:30 +00:00
|
|
|
|
|
|
|
[scrollView setHasHorizontalScroller: YES];
|
|
|
|
[scrollView setHasVerticalScroller: YES];
|
|
|
|
[scrollView setBorderType: NSBezelBorder];
|
|
|
|
[scrollView setAutoresizingMask: (NSViewWidthSizable | NSViewHeightSizable)];
|
|
|
|
|
|
|
|
[window setContentView:scrollView];
|
2002-02-02 17:56:44 +00:00
|
|
|
[window setDelegate:self];
|
2002-02-11 22:19:39 +00:00
|
|
|
[window makeFirstResponder:eView];
|
2002-02-02 17:56:44 +00:00
|
|
|
|
2002-01-29 21:29:30 +00:00
|
|
|
RELEASE(scrollView);
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation PCEditor
|
|
|
|
|
|
|
|
- (id)initWithPath:(NSString*)file
|
|
|
|
{
|
|
|
|
if((self = [super init]))
|
|
|
|
{
|
2002-02-11 22:19:39 +00:00
|
|
|
NSString *t = [NSString stringWithContentsOfFile:file];
|
|
|
|
NSAttributedString *as = [[NSAttributedString alloc] initWithString:t];
|
2002-01-29 21:29:30 +00:00
|
|
|
|
2002-02-11 22:19:39 +00:00
|
|
|
isEdited = NO;
|
|
|
|
path = [file copy];
|
2002-01-29 21:46:45 +00:00
|
|
|
|
2002-01-29 21:29:30 +00:00
|
|
|
[self _initUI];
|
|
|
|
|
|
|
|
[window setTitle:file];
|
2002-02-11 22:19:39 +00:00
|
|
|
[storage setAttributedString:as];
|
|
|
|
RELEASE(as);
|
2002-01-29 21:29:30 +00:00
|
|
|
|
2002-02-11 22:19:39 +00:00
|
|
|
[iView setNeedsDisplay:YES];
|
|
|
|
[eView setNeedsDisplay:YES];
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(textDidChange:)
|
|
|
|
name:NSTextDidChangeNotification
|
|
|
|
object:eView];
|
2002-02-03 19:31:28 +00:00
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(textDidChange:)
|
|
|
|
name:NSTextDidChangeNotification
|
2002-02-11 22:19:39 +00:00
|
|
|
object:iView];
|
2002-01-29 21:29:30 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
2002-02-03 19:31:28 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
|
2002-01-29 21:29:30 +00:00
|
|
|
RELEASE(window);
|
|
|
|
RELEASE(path);
|
|
|
|
|
2002-02-11 22:19:39 +00:00
|
|
|
RELEASE(iView);
|
|
|
|
RELEASE(storage);
|
|
|
|
|
2002-01-29 21:29:30 +00:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setDelegate:(id)aDelegate
|
|
|
|
{
|
|
|
|
delegate = aDelegate;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)delegate
|
|
|
|
{
|
|
|
|
return delegate;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSWindow *)editorWindow
|
|
|
|
{
|
|
|
|
return window;
|
|
|
|
}
|
|
|
|
|
2002-02-02 17:56:44 +00:00
|
|
|
- (NSString *)path
|
|
|
|
{
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2002-02-11 22:19:39 +00:00
|
|
|
- (void)setIsEdited:(BOOL)yn
|
2002-01-29 21:46:45 +00:00
|
|
|
{
|
2002-02-11 22:19:39 +00:00
|
|
|
[window setDocumentEdited:yn];
|
|
|
|
isEdited = yn;
|
2002-01-29 21:46:45 +00:00
|
|
|
}
|
|
|
|
|
2002-02-11 22:19:39 +00:00
|
|
|
- (void)showInProjectEditor:(PCProjectEditor *)pe
|
2002-01-29 21:46:45 +00:00
|
|
|
{
|
2002-02-11 22:19:39 +00:00
|
|
|
[pe setEditorView:iView];
|
2002-01-29 21:46:45 +00:00
|
|
|
}
|
|
|
|
|
2002-01-29 21:29:30 +00:00
|
|
|
- (void)show
|
|
|
|
{
|
2002-02-11 22:19:39 +00:00
|
|
|
[window makeKeyAndOrderFront:self];
|
2002-01-29 21:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)close
|
|
|
|
{
|
2002-02-11 22:19:39 +00:00
|
|
|
if( isEdited )
|
2002-02-02 17:56:44 +00:00
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
2002-02-11 22:19:39 +00:00
|
|
|
if( [window isVisible] )
|
|
|
|
{
|
|
|
|
[window makeKeyAndOrderFront:self];
|
|
|
|
}
|
2002-02-02 17:56:44 +00:00
|
|
|
|
|
|
|
ret = NSRunAlertPanel(@"Edited File!",
|
2002-02-17 13:14:51 +00:00
|
|
|
@"Should '%@' be saved before closing?",
|
|
|
|
@"Yes",@"No",nil,path);
|
2002-02-02 17:56:44 +00:00
|
|
|
|
|
|
|
if( ret == YES )
|
|
|
|
{
|
2002-02-02 19:21:00 +00:00
|
|
|
ret = [self saveFile];
|
|
|
|
|
|
|
|
if( ret == NO )
|
|
|
|
{
|
|
|
|
NSRunAlertPanel(@"Save Failed!",
|
|
|
|
@"Could not save file '%@'!",
|
|
|
|
@"OK",nil,nil,path);
|
|
|
|
}
|
2002-02-02 17:56:44 +00:00
|
|
|
}
|
|
|
|
|
2002-02-17 13:14:51 +00:00
|
|
|
[self setIsEdited:NO];
|
2002-02-02 17:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( delegate && [delegate respondsToSelector:@selector(editorDidClose:)] )
|
|
|
|
{
|
|
|
|
[delegate editorDidClose:self];
|
|
|
|
}
|
2002-01-29 21:29:30 +00:00
|
|
|
}
|
|
|
|
|
2002-03-02 13:15:59 +00:00
|
|
|
- (BOOL)saveFileIfNeeded
|
|
|
|
{
|
|
|
|
if( isEdited )
|
|
|
|
{
|
|
|
|
return [self saveFile];
|
|
|
|
}
|
|
|
|
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2002-02-02 19:21:00 +00:00
|
|
|
- (BOOL)saveFile
|
|
|
|
{
|
2002-02-11 22:19:39 +00:00
|
|
|
[self setIsEdited:NO];
|
2002-02-02 19:21:00 +00:00
|
|
|
|
2002-02-11 22:19:39 +00:00
|
|
|
// Operate on the text storage!
|
|
|
|
return [[storage string] writeToFile:path atomically:YES];
|
2002-02-02 19:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)revertFile
|
|
|
|
{
|
|
|
|
NSString *text = [NSString stringWithContentsOfFile:path];
|
2002-02-11 22:19:39 +00:00
|
|
|
NSAttributedString *as = [[NSAttributedString alloc] initWithString:text];
|
2002-02-02 19:21:00 +00:00
|
|
|
|
2002-02-11 22:19:39 +00:00
|
|
|
[self setIsEdited:NO];
|
2002-02-03 19:31:28 +00:00
|
|
|
|
2002-02-11 22:19:39 +00:00
|
|
|
// Operate on the text storage!
|
|
|
|
[storage setAttributedString:as];
|
|
|
|
RELEASE(as);
|
|
|
|
|
|
|
|
[iView setNeedsDisplay:YES];
|
|
|
|
[eView setNeedsDisplay:YES];
|
2002-02-02 19:21:00 +00:00
|
|
|
}
|
|
|
|
|
2002-02-02 21:12:49 +00:00
|
|
|
- (void)windowWillClose:(NSNotification *)aNotification
|
2002-01-29 21:29:30 +00:00
|
|
|
{
|
2002-02-02 21:12:49 +00:00
|
|
|
if( [[aNotification object] isEqual:window] )
|
2002-01-29 21:29:30 +00:00
|
|
|
{
|
2002-02-02 17:56:44 +00:00
|
|
|
[self close];
|
2002-01-29 21:29:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-02-02 21:12:49 +00:00
|
|
|
- (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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-02-03 19:31:28 +00:00
|
|
|
- (void)textDidChange:(NSNotification *)aNotification
|
|
|
|
{
|
2002-02-11 22:19:39 +00:00
|
|
|
[self setIsEdited:YES];
|
2002-02-03 19:31:28 +00:00
|
|
|
}
|
|
|
|
|
2002-01-29 21:29:30 +00:00
|
|
|
@end
|