home *** CD-ROM | disk | FTP | other *** search
- // MORegexFormCellInspector.h
- //
- // by Mike Ferris
- // Part of MOKit - MORegexPalette
- // Copyright 1993, all rights reserved.
-
- #import "MORegexFormCellInspector.h"
- #import "MOKit/MORegexFormCell.h"
- #import "MOKit/MOString.h"
- #import <objc/objc-runtime.h>
-
- #define CLASS_VERSION 0
-
- #define ADD_TAG 0
- #define CHANGE_TAG 1
- #define REMOVE_TAG 2
-
- #define BUNDLE_TYPE "bundle"
- #define MOREGEXFORMCELL_CLASS_NAME "MORegexFormCell"
-
- #define NIB_TYPE "nib"
- #define NIB_NAME "MORegexFormCellInspector"
-
- @implementation MORegexFormCellInspector
-
- static id MORegexFormCellClass;
-
- + MO_loadClassBundle:(const char *)className
- // Finds the bundle of the same name as the class, grabs it and loads the
- // class from it.
- {
- char pathBuff[MAXPATHLEN+1];
-
- // Load the bundle
- if (objc_lookUpClass(className) == nil) {
- // class is not already loaded... load it.
- id classBundle;
-
- // Look for the bundle in the main bundle first,
- // else try in this classes bundle.
- if (![[NXBundle mainBundle] getPath:pathBuff forResource:className
- ofType:BUNDLE_TYPE]) {
- if (![[NXBundle bundleForClass:[self class]] getPath:pathBuff
- forResource:className ofType:BUNDLE_TYPE]) {
- NXLogError("[MORegexPalette loadClassBundle] failed to "
- "find %s class bundle.", className);
- return nil;
- }
- }
- classBundle = [[NXBundle allocFromZone:[self zone]]
- initForDirectory:pathBuff];
- if (!classBundle) {
- NXLogError("[MORegexPalette loadClassBundle] failed to "
- "create bundle for class %s.", className);
- return nil;
- }
- if (![classBundle classNamed:className]) {
- NXLogError("[MORegexPalette loadClassBundle] failed to "
- "load %s class from bundle.", className);
- return nil;
- }
- }
-
- return self;
- }
-
- + initialize
- // Set the version.
- {
- if (self == [MORegexFormCellInspector class]) {
- [self setVersion:CLASS_VERSION];
-
- // Load the MORegexFormCell class if necessary
- if ((MORegexFormCellClass =
- objc_lookUpClass(MOREGEXFORMCELL_CLASS_NAME)) == nil) {
- [self MO_loadClassBundle:MOREGEXFORMCELL_CLASS_NAME];
- MORegexFormCellClass =
- objc_lookUpClass(MOREGEXFORMCELL_CLASS_NAME);
- if (!MORegexFormCellClass) {
- NXLogError("[MORegexFormCell initialize]: failed to find "
- "MORegexFormCell class.");
- }
- }
- }
- return self;
- }
-
- - init
- // Load our nib file.
- {
- char buf[MAXPATHLEN+1];
- id bundle;
-
- [super init];
-
- // load our nib file.
- bundle = [NXBundle bundleForClass:[MORegexFormCellInspector class]];
- [bundle getPath:buf forResource:NIB_NAME ofType:NIB_TYPE];
- [NXApp loadNibFile:buf owner:self withNames:NO fromZone:[self zone]];
-
- [patternText setDelegate:self];
- [patternText setCharFilter:NXFieldFilter];
- [patternText setFont:[Font userFixedPitchFontOfSize:0
- matrix:NX_FLIPPEDMATRIX]];
-
- return self;
- }
-
- - (int)browserSelectedRow
- // Return the row that is selected in the patternBrowser or -1 if none is.
- {
- return [[patternBrowser matrixInColumn:0] selectedRow];
- }
-
- - (const char *)getPatternText
- {
- static char *text=NULL;
-
- if (text) NX_FREE(text);
- NX_MALLOC(text, char, [patternText byteLength]+1);
- [patternText getSubstring:text start:0 length:[patternText textLength]+1];
- return text;
- }
-
- - enablePatternButtons
- // Enables or disables the add, change, remove buttons as appropriate for
- // the current state of the inspector.
- {
- if ([self browserSelectedRow] == -1) {
- [[patternButtonMatrix cellAt:0 :CHANGE_TAG] setEnabled:NO];
- [[patternButtonMatrix cellAt:0 :REMOVE_TAG] setEnabled:NO];
- } else {
- [[patternButtonMatrix cellAt:0 :CHANGE_TAG] setEnabled:YES];
- [[patternButtonMatrix cellAt:0 :REMOVE_TAG] setEnabled:YES];
- }
-
- if (strlen([self getPatternText])>0) {
- [[patternButtonMatrix cellAt:0 :ADD_TAG] setEnabled:YES];
- } else {
- [[patternButtonMatrix cellAt:0 :ADD_TAG] setEnabled:NO];
- [[patternButtonMatrix cellAt:0 :CHANGE_TAG] setEnabled:NO];
- }
- return self;
- }
-
- - browserAction:sender
- // Copies the string of the currently selected pattern to the pattern text
- // field for editing.
- {
- int row = [self browserSelectedRow];
-
- // put the text of the selected row's pattern in the patternField.
- if (row >= 0) {
- [patternText setText:[object regexStrAt:row]];
- } else {
- [patternText setText:""];
- }
- [patternText sizeToFit];
- [patternText setSel:0 :[patternText textLength]];
- [self enablePatternButtons];
- return self;
- }
-
- - patternButtonAction:sender
- // Adds the text of patternField to the cell's patterns, or changes the pattern
- // currently selected in the patternBrowser to the text in patternField, or
- // removes the pattern in the selected row of the patternBrowser from the
- // cell's patterns.
- {
- int tag = [[sender selectedCell] tag];
- const char *pattern = [self getPatternText];
- char *pcpy;
- int row = [self browserSelectedRow];
- id matrix = [patternBrowser matrixInColumn:0];
-
- NX_MALLOC(pcpy, char, strlen(pattern)+1);
- strcpy(pcpy, pattern);
-
- if (tag==ADD_TAG) {
- // add the string in patternField to the object's patterns.
- if (![MORegexFormCellClass isValidRegex:pattern]) {
- NXRunAlertPanel("Regexp Error", "'%s' is not a valid regular "
- "expression.", "OK", NULL, NULL, pattern);
- NX_FREE(pcpy);
- return self;
- }
- [object addRegexStr:pattern];
- } else if (tag==CHANGE_TAG) {
- // change the pattern at the index of the selected row in the
- // browser to the string in patternField.
- id string = [[object regexStrList] objectAt:row];
- if (![MORegexFormCellClass isValidRegex:pattern]) {
- NXRunAlertPanel("Regexp Error", "'%s' is not a valid regular "
- "expression.", "OK", NULL, NULL, pattern);
- NX_FREE(pcpy);
- return self;
- }
- [string setStringValue:pattern];
- } else if (tag==REMOVE_TAG) {
- // remove the pattern at the index of the selected row in the browser.
- [object removeRegexStrAt:row];
- NX_FREE(pcpy);
- pcpy = NULL;
- pattern = [[matrix cellAt:((row==0)?1:row-1) :0] stringValue];
- if (pattern) {
- NX_MALLOC(pcpy, char, strlen(pattern)+1);
- strcpy(pcpy, pattern);
- }
- }
- [patternBrowser reloadColumn:0];
- if (pcpy) {
- char *buff;
- NX_MALLOC(buff, char, strlen(pcpy)+2);
- sprintf(buff, "/%s", pcpy);
- [patternBrowser setPath:buff];
- NX_FREE(buff);
- }
- NX_FREE(pcpy);
- [self browserAction:patternBrowser];
-
- [self enablePatternButtons];
- [self touch:self];
-
- return self;
- }
-
-
- - ok:sender
- // set the tag and enabled flag. The patternButtonAction takes care of the
- // other stuff.
- {
- [object setTag:[tagForm intValueAt:0]];
- [object setEnabled:![[checkboxMatrix cellAt:0:0] state]];
- [object setAllowEmptyString:[[checkboxMatrix cellAt:1:0] state]];
- return [super ok:sender];
- }
-
- - revert:sender
- // fill in the inspector with the attributes of "object"
- {
- id matrix;
-
- [checkboxMatrix setState:![object isEnabled] at:0:0];
- [checkboxMatrix setState:[object doesAllowEmptyString] at:1:0];
- [tagForm setIntValue:[object tag] at:0];
-
- // browser
- [patternBrowser loadColumnZero];
-
- matrix = [patternBrowser matrixInColumn:0];
- if ([matrix cellCount] > 0) {
- const char *pattern = [[matrix cellAt:0:0] stringValue];
- char *buff;
- NX_MALLOC(buff, char, strlen(pattern)+2);
- sprintf(buff, "/%s", pattern);
- [patternBrowser setPath:buff];
- NX_FREE(buff);
- }
- [self browserAction:patternBrowser];
-
- [self enablePatternButtons];
- [patternButtonMatrix display];
-
- return [super revert:sender];
- }
-
- - (BOOL)wantsButtons
- // Our inspector does not have OK or Revert buttons.
- {
- return NO;
- }
-
- - (int)browser:sender fillMatrix:matrix inColumn:(int)column
- // Fill the browser with the pattern strings from the cell.
- {
- int i, c = [object regexStrCount];
-
- for (i=0; i<c; i++) {
- [matrix renewRows:i+1 cols:1];
- [[matrix cellAt:i :0] setStringValue:[object regexStrAt:i]];
- [[matrix cellAt:i :0] setLoaded:YES];
- [[matrix cellAt:i :0] setLeaf:YES];
- }
-
- return i;
- }
-
- - textDidGetKeys:sender isEmpty:(BOOL)flag
- // Having text in the patternField affects what buttons to enable.
- {
- if (sender==patternText) {
- [self enablePatternButtons];
- [patternButtonMatrix display];
- }
- return self;
- }
-
- - textDidChange:sender;
- // The patternField shouldn't be passed on.
- {
- if (sender==patternText) {
- return self;
- } else {
- return [super textDidChange:sender];
- }
- }
-
- @end
-