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-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;
|
|
|
|
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];
|
|
|
|
|
|
|
|
rect.origin.x = 0;
|
|
|
|
rect.origin.y = 0;
|
|
|
|
rect.size.height -= 24;
|
|
|
|
rect.size.width -= 4;
|
|
|
|
|
|
|
|
view = [[PCEditorView alloc] initWithFrame:rect];
|
2002-02-02 19:21:00 +00:00
|
|
|
[view setEditor:self];
|
2002-01-29 21:29:30 +00:00
|
|
|
|
|
|
|
[view setMinSize: NSMakeSize (0, 0)];
|
|
|
|
[view setMaxSize:NSMakeSize(1e7, 1e7)];
|
|
|
|
[view setRichText:NO];
|
|
|
|
[view setEditable:YES];
|
|
|
|
[view setSelectable:YES];
|
|
|
|
[view setVerticallyResizable:YES];
|
|
|
|
[view setHorizontallyResizable:NO];
|
|
|
|
[view setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];
|
|
|
|
[view setBackgroundColor:[NSColor whiteColor]];
|
|
|
|
[[view textContainer] setWidthTracksTextView:YES];
|
|
|
|
|
|
|
|
[scrollView setDocumentView:view];
|
2002-02-03 16:30:35 +00:00
|
|
|
RELEASE(view);
|
2002-01-29 21:29:30 +00:00
|
|
|
|
2002-02-03 16:30:35 +00:00
|
|
|
rect.size = NSMakeSize([scrollView contentSize].width,1e7);
|
|
|
|
[[view 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-01-29 21:29:30 +00:00
|
|
|
RELEASE(scrollView);
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation PCEditor
|
|
|
|
|
|
|
|
- (id)initWithPath:(NSString*)file
|
|
|
|
{
|
|
|
|
if((self = [super init]))
|
|
|
|
{
|
|
|
|
NSString *text = [NSString stringWithContentsOfFile:file];
|
|
|
|
|
2002-01-29 21:46:45 +00:00
|
|
|
// Should take that from preferences!
|
|
|
|
isEmbedded = NO;
|
|
|
|
|
2002-01-29 21:29:30 +00:00
|
|
|
[self _initUI];
|
|
|
|
|
|
|
|
[window setTitle:file];
|
|
|
|
[view setText:text];
|
|
|
|
|
|
|
|
path = [file copy];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
RELEASE(window);
|
|
|
|
RELEASE(path);
|
|
|
|
|
|
|
|
[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-01-29 21:46:45 +00:00
|
|
|
- (void)setEmbedded:(BOOL)yn
|
|
|
|
{
|
|
|
|
isEmbedded = yn;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)isEmbedded
|
|
|
|
{
|
|
|
|
return isEmbedded;
|
|
|
|
}
|
|
|
|
|
2002-01-29 21:29:30 +00:00
|
|
|
- (void)show
|
|
|
|
{
|
2002-01-29 21:46:45 +00:00
|
|
|
if( isEmbedded == NO )
|
|
|
|
{
|
|
|
|
[window makeKeyAndOrderFront:self];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
}
|
2002-01-29 21:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)close
|
|
|
|
{
|
2002-02-02 17:56:44 +00:00
|
|
|
if( isEmbedded == NO && [window isDocumentEdited] )
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
[window makeKeyAndOrderFront:self];
|
|
|
|
|
|
|
|
ret = NSRunAlertPanel(@"Edited File!",
|
|
|
|
@"Should the file be saved before closing?",
|
|
|
|
@"Yes",@"No",nil);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
[window setDocumentEdited:NO];
|
|
|
|
}
|
|
|
|
else if( isEmbedded )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
if( delegate && [delegate respondsToSelector:@selector(editorDidClose:)] )
|
|
|
|
{
|
|
|
|
[delegate editorDidClose:self];
|
|
|
|
}
|
2002-01-29 21:29:30 +00:00
|
|
|
}
|
|
|
|
|
2002-02-02 19:21:00 +00:00
|
|
|
- (BOOL)saveFile
|
|
|
|
{
|
|
|
|
if( isEmbedded == NO )
|
|
|
|
{
|
|
|
|
[window setDocumentEdited:NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [[view text] writeToFile:path atomically:YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)revertFile
|
|
|
|
{
|
|
|
|
NSString *text = [NSString stringWithContentsOfFile:path];
|
|
|
|
|
|
|
|
if( isEmbedded == NO )
|
|
|
|
{
|
|
|
|
[window setDocumentEdited:NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
[view setText:text];
|
|
|
|
}
|
|
|
|
|
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-01-29 21:29:30 +00:00
|
|
|
@end
|