2002-10-06 13:45:02 +00:00
|
|
|
|
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$
|
|
|
|
*/
|
|
|
|
|
2003-04-21 13:46:10 +00:00
|
|
|
#include "PCEditor.h"
|
|
|
|
#include "PCDefines.h"
|
|
|
|
#include "PCEditorView.h"
|
|
|
|
#include "ProjectComponent.h"
|
|
|
|
#include "PCProjectEditor.h"
|
2002-01-29 21:29:30 +00:00
|
|
|
|
2003-04-21 13:46:10 +00:00
|
|
|
#include "PCEditor+UInterface.h"
|
2002-11-27 17:40:09 +00:00
|
|
|
|
2002-02-02 21:12:49 +00:00
|
|
|
NSString *PCEditorDidBecomeKeyNotification=@"PCEditorDidBecomeKeyNotification";
|
|
|
|
NSString *PCEditorDidResignKeyNotification=@"PCEditorDidResignKeyNotification";
|
|
|
|
|
2002-01-29 21:29:30 +00:00
|
|
|
@implementation PCEditor
|
|
|
|
|
|
|
|
- (id)initWithPath:(NSString*)file
|
|
|
|
{
|
2002-10-06 13:45:02 +00:00
|
|
|
if((self = [super init]))
|
|
|
|
{
|
|
|
|
NSString *t;
|
|
|
|
NSAttributedString *as;
|
|
|
|
NSDictionary *at;
|
|
|
|
NSFont *ft;
|
2002-01-29 21:46:45 +00:00
|
|
|
|
2002-10-06 13:45:02 +00:00
|
|
|
ft = [NSFont userFixedPitchFontOfSize:0.0];
|
|
|
|
at = [NSDictionary dictionaryWithObject:ft forKey:NSFontAttributeName];
|
|
|
|
t = [NSString stringWithContentsOfFile:file];
|
|
|
|
as = [[NSAttributedString alloc] initWithString:t attributes:at];
|
2002-01-29 21:29:30 +00:00
|
|
|
|
2002-10-06 13:45:02 +00:00
|
|
|
_isEdited = NO;
|
|
|
|
_path = [file copy];
|
2002-01-29 21:29:30 +00:00
|
|
|
|
2002-10-06 13:45:02 +00:00
|
|
|
[self _initUI];
|
2002-02-11 22:19:39 +00:00
|
|
|
|
2002-10-06 13:45:02 +00:00
|
|
|
[_window setTitle:file];
|
|
|
|
[_storage setAttributedString:as];
|
|
|
|
RELEASE(as);
|
2002-02-03 19:31:28 +00:00
|
|
|
|
2002-10-06 13:45:02 +00:00
|
|
|
[_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;
|
2002-01-29 21:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
2002-10-06 13:45:02 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
2002-02-03 19:31:28 +00:00
|
|
|
|
2002-10-06 13:45:02 +00:00
|
|
|
RELEASE(_window);
|
|
|
|
RELEASE(_path);
|
2002-01-29 21:29:30 +00:00
|
|
|
|
2002-10-06 13:45:02 +00:00
|
|
|
RELEASE(_iView);
|
|
|
|
RELEASE(_storage);
|
2002-02-11 22:19:39 +00:00
|
|
|
|
2002-10-06 13:45:02 +00:00
|
|
|
[super dealloc];
|
2002-01-29 21:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setDelegate:(id)aDelegate
|
|
|
|
{
|
2002-10-06 13:45:02 +00:00
|
|
|
_delegate = aDelegate;
|
2002-01-29 21:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id)delegate
|
|
|
|
{
|
2002-10-06 13:45:02 +00:00
|
|
|
return _delegate;
|
2002-01-29 21:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSWindow *)editorWindow
|
|
|
|
{
|
2002-10-06 13:45:02 +00:00
|
|
|
return _window;
|
2002-01-29 21:29:30 +00:00
|
|
|
}
|
|
|
|
|
2002-02-02 17:56:44 +00:00
|
|
|
- (NSString *)path
|
|
|
|
{
|
2002-10-06 13:45:02 +00:00
|
|
|
return _path;
|
2002-02-02 17:56:44 +00:00
|
|
|
}
|
|
|
|
|
2002-02-11 22:19:39 +00:00
|
|
|
- (void)setIsEdited:(BOOL)yn
|
2002-01-29 21:46:45 +00:00
|
|
|
{
|
2002-10-06 13:45:02 +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-10-06 13:45:02 +00:00
|
|
|
[pe setEditorView:_iView];
|
2002-01-29 21:46:45 +00:00
|
|
|
}
|
|
|
|
|
2002-01-29 21:29:30 +00:00
|
|
|
- (void)show
|
|
|
|
{
|
2002-10-06 13:45:02 +00:00
|
|
|
[_window makeKeyAndOrderFront:self];
|
2002-01-29 21:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)close
|
|
|
|
{
|
2002-10-06 13:45:02 +00:00
|
|
|
if( _isEdited )
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
if( [_window isVisible] )
|
2002-02-02 17:56:44 +00:00
|
|
|
{
|
2002-10-06 13:45:02 +00:00
|
|
|
[_window makeKeyAndOrderFront:self];
|
2002-02-02 17:56:44 +00:00
|
|
|
}
|
|
|
|
|
2002-10-06 13:45:02 +00:00
|
|
|
ret = NSRunAlertPanel(@"Edited File!",
|
|
|
|
@"Should '%@' be saved before closing?",
|
|
|
|
@"Yes",@"No",nil,_path);
|
|
|
|
|
|
|
|
if( ret == YES )
|
2002-02-02 17:56:44 +00:00
|
|
|
{
|
2002-10-06 13:45:02 +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-10-06 13:45:02 +00:00
|
|
|
|
|
|
|
[self setIsEdited:NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2002-10-06 13:45:02 +00:00
|
|
|
if((_isEdited))
|
|
|
|
{
|
|
|
|
return [self saveFile];
|
|
|
|
}
|
2002-03-02 13:15:59 +00:00
|
|
|
|
2002-10-06 13:45:02 +00:00
|
|
|
return YES;
|
2002-03-02 13:15:59 +00:00
|
|
|
}
|
|
|
|
|
2002-02-02 19:21:00 +00:00
|
|
|
- (BOOL)saveFile
|
|
|
|
{
|
2002-10-06 13:45:02 +00:00
|
|
|
[self setIsEdited:NO];
|
2002-02-02 19:21:00 +00:00
|
|
|
|
2002-10-06 13:45:02 +00:00
|
|
|
// Operate on the text storage!
|
|
|
|
return [[_storage string] writeToFile:_path atomically:YES];
|
2002-02-02 19:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)revertFile
|
|
|
|
{
|
2002-10-06 13:45:02 +00:00
|
|
|
NSString *text = [NSString stringWithContentsOfFile:_path];
|
|
|
|
NSAttributedString *as = [[NSAttributedString alloc] initWithString:text];
|
2002-02-02 19:21:00 +00:00
|
|
|
|
2002-10-06 13:45:02 +00:00
|
|
|
[self setIsEdited:NO];
|
2002-02-03 19:31:28 +00:00
|
|
|
|
2002-10-06 13:45:02 +00:00
|
|
|
// Operate on the text storage!
|
|
|
|
[_storage setAttributedString:as];
|
|
|
|
RELEASE(as);
|
2002-02-11 22:19:39 +00:00
|
|
|
|
2002-10-06 13:45:02 +00:00
|
|
|
[_iView setNeedsDisplay:YES];
|
|
|
|
[_eView setNeedsDisplay:YES];
|
2002-11-24 11:08:02 +00:00
|
|
|
|
|
|
|
return 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-10-06 13:45:02 +00:00
|
|
|
if( [[aNotification object] isEqual:_window] )
|
|
|
|
{
|
|
|
|
[self close];
|
|
|
|
}
|
2002-01-29 21:29:30 +00:00
|
|
|
}
|
|
|
|
|
2002-02-02 21:12:49 +00:00
|
|
|
- (void)windowDidBecomeKey:(NSNotification *)aNotification
|
|
|
|
{
|
2002-10-06 13:45:02 +00:00
|
|
|
if( [[aNotification object] isEqual:_window] )
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:PCEditorDidBecomeKeyNotification object:self];
|
|
|
|
}
|
2002-02-02 21:12:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowDidResignKey:(NSNotification *)aNotification
|
|
|
|
{
|
2002-10-06 13:45:02 +00:00
|
|
|
if( [[aNotification object] isEqual:_window] )
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:PCEditorDidResignKeyNotification object:self];
|
|
|
|
}
|
2002-02-02 21:12:49 +00:00
|
|
|
}
|
|
|
|
|
2002-02-03 19:31:28 +00:00
|
|
|
- (void)textDidChange:(NSNotification *)aNotification
|
|
|
|
{
|
2002-10-06 13:45:02 +00:00
|
|
|
[self setIsEdited:YES];
|
2002-02-03 19:31:28 +00:00
|
|
|
}
|
|
|
|
|
2002-01-29 21:29:30 +00:00
|
|
|
@end
|