#include "button.h"
     

@implementation WDCButton

- init
{
  [super initWithName:0 managedBy:0];
  return self;
}

- unManage
{
  [[self getChildObject] unManage];
  [super unManage];
  return self;
}


@end

@implementation WDCPushButton : WDCButton  

- initWithName:(char*)aName managedBy:anObject
{ 
  XmString xmstr;
  [super initWithName:aName managedBy:anObject];
  xmstr = XmStringCreateSimple((char*)aName);
  [self setArg:XmNlabelString to:xmstr];
  XmStringFree (xmstr);
  return self;
}

- (WidgetClass)getWidgetClass
{
  return xmPushButtonWidgetClass;
}

- setCallData:(XtPointer)aCall_data
{
  XmPushButtonCallbackStruct *aCall_data_ptr
    =(XmPushButtonCallbackStruct*)aCall_data;
  _call_data.reason=aCall_data_ptr->reason;
  _call_data.event=aCall_data_ptr->event;
  _call_data.click_count=aCall_data_ptr->click_count;
  return self;
}
  
- manageChild
{
  id child=[self getChildObject];
  if ([child isKindOfClassNamed:"WDCMenu"])
    {

      // to position a menu according to the button pressed position.

      XmMenuPosition([child getWidget], _call_data.event);
    }
  [child manage];
  return self;
}

- unManageChild
{
  [[self getChildObject] unManage];
  return self;
}

- (BOOL)isChildManaged
{
  return [[self getChildObject] isManaged];
}

- setAction
{
  XtAddCallback([self getWidget], XmNactivateCallback, theCallback, self);
  [self setActionMessage:"doManageChild" withReceiver: self];
  return self;
}

- doManageChild
{
  return [self perform:sel_get_uid([[self getChildObject] isManaged]
				   ? "unManageChild"
				   : "manageChild")];
}  

@end


@implementation WDCQuitButton : WDCPushButton  

- managedBy:anObject
{
  [self initWithName:"Quit" managedBy:anObject];
  return [self setActionMessage:"doExit" withReceiver: self];
}

- doExit
{
  exit(0);
}

@end


@implementation WDCCascadeButton : WDCButton

- (WidgetClass)getWidgetClass
{
  return xmCascadeButtonWidgetClass;
}

- setAction
{
  /* I do not know what does a Cascade button do */

  return self;
}

- setChildObject: anObject
{
  [super setChildObject:anObject];
  return [self setArg:XmNsubMenuId to:(char*)[anObject getWidget]];
}

@end

@implementation WDCToggleButton : WDCButton


- (WidgetClass)getWidgetClass
{
  return xmToggleButtonWidgetClass;
}

- manageChild
{
  id child=[self getChildObject];
  if ([child isKindOfClassNamed:"WDCMenu"])
    {

      // to position a menu according to the button pressed position.

      XmMenuPosition([child getWidget], _call_data.event);
    }
  /*
  printf("x & y position : %d %d\n", ((XButtonEvent*)_call_data.event)->x, 
	 ((XButtonEvent*)_call_data.event)->y);
	 */
  [child manage];
  return self;
}

- unManageChild
{
//  printf("Unmanaging child\n");
  [[self getChildObject] unManage];
  return self;
}

- (BOOL)isChildManaged
{
  return [[self getChildObject] isManaged];
}


- setAction
{
  /* available callback resources:
     XmNactivateCallback, XmNarmCallback, XmNvalueChangedCallback.
     */
  XtAddCallback([self getWidget], XmNvalueChangedCallback, theCallback, self);
  return [self setActionMessage: "doManageChild" withReceiver: self];
}


- setCallData:(XtPointer)aCall_data
{
  XmToggleButtonCallbackStruct *aCall_data_ptr
    =(XmToggleButtonCallbackStruct*)aCall_data;
  _call_data.reason=aCall_data_ptr->reason;
  _call_data.event=aCall_data_ptr->event;
  _call_data.set=aCall_data_ptr->set;
  return self;
}

- doManageChild
{

   return [self perform:sel_get_uid(_call_data.set
				   ? "manageChild"
				   : "unManageChild")];
}

@end



