/*
 * This subclass of Window just fixes the processing of "setFrameFromString".
 * There should be a similar subclass written for Panel.
 * 		Garance Alistair Drosehn/gad@eclipse.its.rpi.edu
 * 		May 31/1995
 */
#import "Window_FixSF.h"

@implementation Window_FixSF

- (void)setFrameFromString:(const char *)string
{
#define MIN_PIXELS 2
    int                 numScreens,
                        screenCount;
    const NXScreen     *screenList,
                       *originScreen,
                       *upperLeftScreen;
    BOOL                valuesOK,
                        frameOK;
    const char         *pos1,
                       *pos2;
    NXCoord             tmpValue;
    NXRect              frameRect,
                        upperLeft;
    const NXRect       *curScreenRect;

    if (string == NULL)
	return;
    if (*string == '\0')
	return;

    [super setFrameFromString:string];

    valuesOK = NO;
    frameRect.origin.x = frameRect.origin.y = 0.0;
    frameRect.size.width = frameRect.size.height = 0.0;

    for (pos1 = string; *pos1 != '\0'; pos1++) {
	if (NXIsDigit(*pos1) || *pos1 == '-')
	    break;
    }
    tmpValue = strtod(pos1, &pos2);
    if (pos1 != pos2) {
	frameRect.origin.x = tmpValue;
	for (pos1 = pos2; *pos1 != '\0'; pos1++) {
	    if (NXIsDigit(*pos1) || *pos1 == '-')
		break;
	}
	tmpValue = strtod(pos1, &pos2);
    }
    if (pos1 != pos2) {
	frameRect.origin.y = tmpValue;
	for (pos1 = pos2; *pos1 != '\0'; pos1++) {
	    if (NXIsDigit(*pos1))
		break;
	}
	tmpValue = strtod(pos1, &pos2);
    }
    if (pos1 != pos2) {
	frameRect.size.width = tmpValue;
	for (pos1 = pos2; *pos1 != '\0'; pos1++) {
	    if (NXIsDigit(*pos1))
		break;
	}
	tmpValue = strtod(pos1, &pos2);
    }
    if (pos1 != pos2) {
	frameRect.size.height = tmpValue;
	valuesOK = YES;
    }
#ifdef DEBUG
    printf("%s: Window_FixSF's setFrameFromString: ",[NXApp appName]);
    if (valuesOK) {
	printf("x=%1.0f y=%1.0f w=%1.0f h=%1.0f\n",
	       frameRect.origin.x, frameRect.origin.y,
	       frameRect.size.width, frameRect.size.height);
    } else {
	printf("unable to parse xywh values!\n");
	return;
    }
#else
    if (!valuesOK) {
	printf("%s: In Window_FixSF's setFrameFromString method:\n",[NXApp appName]);
	printf("%s: unable to parse xywh values from \"%s\" !\n",[NXApp appName], string);
	return;
    }
#endif

    upperLeft.origin.x = frameRect.origin.x;
    upperLeft.origin.y = frameRect.origin.y + frameRect.size.height - MIN_PIXELS;
    upperLeft.size.width = upperLeft.size.height = MIN_PIXELS;

    [NXApp getScreens:&screenList count:&numScreens];

    frameOK = NO;
    originScreen = upperLeftScreen = NULL;
    for (screenCount = 0; screenCount < numScreens; screenCount++) {
	curScreenRect = &screenList[screenCount].screenBounds;
#ifdef DEBUG
	printf("  %d) checking screen #%d (%1.0f,%1.0f - %1.0f,%1.0f)",
	       screenCount + 1, screenList[screenCount].screenNumber,
	       curScreenRect->origin.x, curScreenRect->origin.y,
	       curScreenRect->size.width, curScreenRect->size.height);
#endif
	if (NXContainsRect(curScreenRect, &frameRect)) {
#ifdef DEBUG
	    printf(":  screen contains frameRect\n");
#endif
	    frameOK = YES;
	    break;
	}
	if (NXIntersectsRect(curScreenRect, &frameRect)) {
#ifdef DEBUG
	    printf(":  screen has part of frameRect");
#endif
	    if (NXMouseInRect(&(frameRect.origin), curScreenRect, NO)) {
#ifdef DEBUG
		printf(" & contains it's origin");
#endif
		originScreen = &screenList[screenCount];
	    }
	    if (NXMouseInRect(&(upperLeft.origin), curScreenRect, NO)) {
#ifdef DEBUG
		printf(" & contains the upperLeft point");
#endif
		upperLeftScreen = &screenList[screenCount];
	    }
	}
#ifdef DEBUG
	printf("\n");
#endif
    }

 /* not sure how elaborate these boundry checks should be */
    if (originScreen || upperLeftScreen) {
	frameOK = YES;
    }
    if (frameOK) {
	[self moveTo:frameRect.origin.x:frameRect.origin.y];
    }
    return;
}

@end
