home *** CD-ROM | disk | FTP | other *** search
- //----------------------------------------------------------------------------------------------------
- //
- // TextSubInspector
- //
- // Inherits From: DefaultSubInspector
- //
- // Declared In: TextSubInspector.h
- //
- // Disclaimer
- //
- // You may freely copy, distribute and reuse this software and its
- // associated documentation. I disclaim any warranty of any kind,
- // expressed or implied, as to its fitness for any particular use.
- //
- //----------------------------------------------------------------------------------------------------
- #import "TextSubInspector.h"
- #import <appkit/appkit.h>
-
-
- @implementation TextSubInspector
-
- static id _SELF = nil;
- static id font;
-
- //----------------------------------------------------------------------------------------------------
- // Initialization and Free Methods
- //----------------------------------------------------------------------------------------------------
- + new
- {
- // Only allow one instance...
-
- if (_SELF) return _SELF;
- _SELF = self = [super new];
- font = [Text getDefaultFont];
- return _SELF;
- }
-
-
- //----------------------------------------------------------------------------------------------------
- // Private Methods
- //----------------------------------------------------------------------------------------------------
- - (STR) extension: (const char*) path
- {
- // File's extension if found, pointer to NULL otherwise. Caller must free.
-
- STR start = (STR)path;
- STR end;
-
- for (end = (start + strlen(start) -1); end >= start; end--)
- {
- if (*end == '.') break;
- if (*end == '/') return NULL;
- }
-
- if ((end - start) == -1) return NULL;
-
- start += (end - start) + 1;
- return NXCopyStringBuffer(start);
- }
-
-
- - (BOOL)isRTFDStream: (STR) path
- {
- // An RTFD can be either a typed stream or a directory containing
- // graphics and an rtf. Text uses different methods to access these
- // (and craters if you use the wrong one).
-
- struct stat statBuffer;
-
- stat (path, &statBuffer);
-
- if ( (statBuffer.st_mode & S_IFMT) != S_IFDIR)
- return YES;
-
- return NO;
- }
-
-
- //----------------------------------------------------------------------------------------------------
- // Inspection Methods
- //----------------------------------------------------------------------------------------------------
- - inspect: (STR) path
- {
- STR extension;
-
- if (! path) return self;
-
- if ( ! (extension = [self extension: path])) return self;
-
- [text setFont: font];
- [text setHorizResizable: YES];
- [text setVertResizable: YES];
- [[[text superview] superview] setHorizScrollerRequired: YES];
-
- if (NXOrderStrings(extension, "rtf", YES, -1, NULL) == 0)
- [self readRTF: path];
- else if (NXOrderStrings(extension, "rtfd", YES, -1, NULL) == 0)
- [self readRTFD: path];
- else
- [self readText: path];
-
- [text sizeToFit];
- return self;
- }
-
-
- //----------------------------------------------------------------------------------------------------
- // Read Methods
- //----------------------------------------------------------------------------------------------------
- - readRTF: (STR)path
- {
- // Read an RTF file.
-
- NXStream* stream = NXMapFile (path, NX_READONLY);
- if (! stream) return [self inspectionError: path];
- [text readRichText: stream];
- NXClose(stream);
- return self;
- }
-
-
- - readRTFD: (STR)path
- {
- // Read an RTFD file or stream.
-
- NXRTFDError openStatus;
-
- if ([self isRTFDStream: path])
- {
- NXStream* stream = NXMapFile (path, NX_READONLY);
- if (! stream) return [self inspectionError: path];
- [text readRTFDFrom: stream];
- NXClose(stream);
- }
- else
- {
- openStatus = [text openRTFDFrom: path];
- if (openStatus != NX_RTFDErrorNone) return [self inspectionError: path];
- }
-
- return self;
- }
-
-
- - readText: (STR)path;
- {
- // Read a (hopefully) text file.
-
- NXStream* stream = NXMapFile (path, NX_READONLY);
- if (! stream) return [self inspectionError: path];
- [(Text*)text readText: stream];
- NXClose(stream);
- return self;
- }
-
-
- //----------------------------------------------------------------------------------------------------
- // Accessing Inspection View
- //----------------------------------------------------------------------------------------------------
- - clearInspectorView
- {
- // Clear view...
-
- [text setText: ""];
- [text setSel:0 :0];
- return self;
- }
-
-
- @end