#import  "../BackgCtr.h"
#import  <appkit/Application.h>
#import  <appkit/publicWraps.h>
#import  <appkit/Control.h>
#import  <appkit/MenuCell.h>
#import  <appkit/workspaceRequest.h>
#import  <objc/NXBundle.h>	/* LocalizedString */
#import  <defaults/defaults.h>
#import  <stdio.h>
#import  <stdlib.h>
#import  <string.h>
#import  <objc/zone.h>
#import  <bsd/libc.h>
#import  <mach/mach_init.h>
#import  "../TVController.h"
#import  "../ToyWin.h"
#import  "../ToyView.h"
#import  "../common.h"
#import  "../strfunc.h"
#import  "Background.h"

@implementation BackgCtr

NXZone *backZone;

- init
{
	backWin = nil;
	backZone = NULL;
	return self;
}

- cleanBackground: sender
{
	if (backWin == nil)
		return nil;
	[backWin free];
	backWin = nil;
	NXDestroyZone(backZone);
	backZone = NULL;
	[theController backWinFront: NO];
	[theController backgMenuEnabled: NO];
	return self;
}

- makeFront: sender
{
	id v;

	if (backWin == nil)
		return nil;
	if ([(v = [backWin contentView]) isFront]) /* toggle */
		[v toBehind: self];
	else
		[v toFront: self];
	return self;
}


/* Local Method */
- viewOfBackground
{
	NXRect rect;
	Background *view;

	if (backWin)
		return [backWin contentView];
	[NXApp getScreenSize: &rect.size];
	rect.origin.x = rect.origin.y = 0;
	if (backZone == NULL)
		backZone = NXCreateZone(vm_page_size, vm_page_size, YES);
	view = [[Background allocFromZone: backZone] initFrame: &rect];
	backWin = [view initWinAttr];
	return view;
}

- setImage:(NXImage *)backimage hasAlpha:(BOOL)alpha with:(int)method
{
	Background *view;
	id	rtn;

	view = [self viewOfBackground];
	rtn = [view setImage: backimage hasAlpha: alpha with: method];
	[theController backgMenuEnabled: YES];
	[backWin display];
	NXPing();
	return rtn;	/* If nil Err_Memory */
}

- setStream: (NXStream *)stream with: (int)method
{
	Background *view;
	id	rtn;

	view = [self viewOfBackground];
	rtn = [view setStream: stream with: method];
	[theController backgMenuEnabled: YES];
	[backWin display];
	NXPing();
	return rtn;	/* If nil Err_Memory */
}

@end
