#include "popupbox.h"
#include "popupshl.h"
#include "dialogsh.h"
#include "frame.h"
#include "scrowind.h"
#include "drawarea.h"

@implementation WDCVariablePopupBox

- initWithName:(char*)aName managedBy:anObject
{
  if (![anObject isKindOfClassNamed:"WDCWindow"]
      && ![anObject isKindOfClassNamed:"WDCFrame"])
    {
      [self buildPopupShellWithName:aName managedBy:anObject];
      anObject=[self getPopupWindow];
    }
  [self setWidget:[[[WDCRowColumn alloc] 
		    initWithName : aName 
		    managedBy : anObject]
		   getWidget]];
  [self setParentObject: anObject] ;
  
  // this need to be managed before its parent. So, just do this for now.

  [super manage];
  return self;
}

- buildPopupShellWithName:(char*)aName managedBy:anObject
{
  [self setPopupShell : [[WDCFormDialog alloc]
			 initWithName : aName 
			 managedBy : anObject]];  
  [self setPopupWindow : [[[WDCScrolledWindow alloc]
			   setArg : XmNscrollingPolicy 
			   to : (char*) XmAUTOMATIC]
			  initWithName : aName
			  managedBy : [self getPopupShell]]];
  return self;
}


- setAction
{
  return self;
}

- setPopupShell:shell
{
  _popupShell=shell;
  return self;
}

- setPopupWindow:window
{
  _popupWindow=window;
  return self;
}

- getPopupShell
{
  return _popupShell;
}

- getPopupWindow
{
  return _popupWindow;
}

- (BOOL)isManaged
{
  id object;
  return (object=[self getPopupShell])? [object isManaged]:[super isManaged];
}  

- manage
{
  id object;
  if (object=[self getPopupShell])
    [object manage];
  else
    [super manage];
  return self;
}

- unManage
{
  id object;
  if (object=[self getPopupShell])
    [object unManage];
  else
    [super unManage];
  return self;
}

- setArg:(char*)aName to:(char*)aValue
{
  id object;
  if (object=[self getPopupShell])
    {
      [object setArg:aName to:aValue];
      [[self getPopupWindow] setArg:aName to:aValue];
    } 
  [super setArg:aName to:aValue];
  return self;
}

- (char*)getArgValue:(char*)aName
{
  id object;
  return (object=[self getPopupShell]) ? 
    [object getArgValue:aName] : [super getArgValue : aName];
}

@end

@implementation WDCDrawBox

- initWithName:(char*)aName managedBy:anObject
{
  if (![anObject isKindOfClassNamed:"WDCWindow"]
      && ![anObject isKindOfClassNamed:"WDCFrame"])
    {
      [self buildPopupShellWithName:aName managedBy:anObject];
      anObject=[self getPopupWindow];
    }
  [self setDrawArea:[[WDCDrawArea alloc] 
		     initWithName : aName managedBy : anObject]];
  [self setWidget : [[self getDrawArea] getWidget]];
  [self setParentObject: anObject];

  // this need to be managed before its parent. So, just do this for now.
  // I don't like this, however, it does work.
  XtManageChild([self getWidget]);
  return self;
}

- setDrawArea:drawArea
{
  _drawArea=drawArea;
  return self;
}

- getDrawArea
{
  return _drawArea;
}

- forward:(SEL)aSel :(arglist_t)argFrame
{
  if ([[self getDrawArea] respondsTo:aSel])
    [[self getDrawArea] performv:aSel :argFrame];
  else
    [self error:"DrawArea does not responds to forwarding message"];
  return self;
}

@end


@implementation WDCPopupBox

- buildPopupShellWithName:(char*)aName managedBy:anObject
{
  [self setPopupShell : [[WDCFormDialog alloc]
			 initWithName : aName 
			 managedBy : anObject]];  
  // [self setPopupWindow : [[WDCWindow alloc] 
  //                         initWithName : aName
  //			  managedBy : [self getPopupShell]]];
  // [[self getPopupShell] setArg:XmNallowShellResize to:(char*)True];

  // default to have a window. If a window is not desired. The _popupWindow
  // object need to point to the _popupShell also.

  [self setPopupWindow : [self getPopupShell]];

  return self;
}

@end

@implementation WDCRadioBox


- initWithName:(char*)aName managedBy:anObject
{
  if (![anObject isKindOfClassNamed:"WDCWindow"]
      && ![anObject isKindOfClassNamed:"WDCFrame"])
    {
      [self buildPopupShellWithName:aName managedBy:anObject];
      anObject=[self getPopupWindow];
    }
  [self setWidget:XmCreateRadioBox([anObject getWidget],
				   aName,
				   [self getArgList],
				   [self getNumArgs])];
  [self setParentObject: anObject];

  // this need to be managed before its parent. So, just do this for now.
  // I don't like this, however, it does work.
  XtManageChild([self getWidget]);

  return self;
}

@end

@implementation WDCCheckBox

- initWithName:(char*)aName managedBy:anObject
{
  if (![anObject isKindOfClassNamed:"WDCWindow"]
      && ![anObject isKindOfClassNamed:"WDCFrame"])
    {
      [self buildPopupShellWithName:aName managedBy:anObject];
      anObject=[self getPopupWindow];
    }
  [self setWidget:XmCreateSimpleCheckBox([anObject getWidget],
					 aName,
					 [self getArgList],
					 [self getNumArgs])];
  [self setParentObject: anObject];
  
  // I don't like this, however, it does work.
  XtManageChild([self getWidget]);

  return self;
}

@end



