/**********************************************************************
 * callbacks for the "week" program
 *
 * $Author: brlewis $
 * $Source: /afs/athena.mit.edu/astaff/project/wcl/doc/iap/day4/RCS/callbacks.c,v $
 * $Header: /afs/athena.mit.edu/astaff/project/wcl/doc/iap/day4/RCS/callbacks.c,v 1.2 94/01/24 16:59:24 brlewis Exp Locker: brlewis $
 *
 * Copyright 1993 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 **********************************************************************/
#include <mit-copyright.h>

static char rcsid_callbacks_c[] = "$Header: /afs/athena.mit.edu/astaff/project/wcl/doc/iap/day4/RCS/callbacks.c,v 1.2 94/01/24 16:59:24 brlewis Exp Locker: brlewis $";


#include <stdio.h>
#include <Xm/Xm.h>
#include "week.h"

/**********************************************************************
 * change background color according to widget name
 **********************************************************************/

/*ARGSUSED*/
void SetBGColorFromName(w, unusedstring, unusedptr)
     Widget w;
     char *unusedstring;
     XtPointer unusedptr;
{
  Colormap colormap;
  XColor screen_colordef, exact_colordef;
  Status retval;

  XtVaGetValues(w,
		XtNcolormap, &colormap,
		NULL);

  /* If background color is "default", don't do anything */
  if (!strcmp(XtName(w), "default")) return;

  /* Get color from color name */
  retval = XAllocNamedColor(XtDisplay(w), colormap, XtName(w),
		   &screen_colordef, &exact_colordef);

  if (retval == 0) return;    /* failure */

  XmChangeColor(w, screen_colordef.pixel);
  return;
}

weekstate
weekstate_create()
{
  weekstate to_return;

  to_return = (weekstate) malloc(sizeof(weekstateStruct));
  if (to_return) to_return->mainButton = (Widget)0;
  return(to_return);
}

#define Offset(field) (XtOffset(weekstateStruct *, field))
static XtResource week_Resources[] =
{
  {"mainbtn", "Mainbtn", XtRWidget, sizeof(Widget),
     Offset(mainButton), XtRString, (XtPointer)"*mainButton" },
  {"mainLabel", "MainLabel", XtRWidget, sizeof(Widget),
     Offset(mainLabel), XtRString, (XtPointer)"*label" }
};

/*
 * The following callback is specified as the WcAfterChildren callback
 * of the child of a top-level shell, i.e.
 *
 *  *mainForm.wcAfterChildren:  AppState::Create()
 *
 * It associates data with the mainForm widget.  This is better
 * than keeping the data in glbal variables, because it becomes easier
 * to make "clones" of your original window.
 */

void
AppStateCreate(w, client_data, cbs)
     Widget w;
     XtPointer client_data;
     XmAnyCallbackStruct *cbs;
{
  weekstate newweekstate;

  if (!(newweekstate = weekstate_create()))
    {
      fprintf(stderr, "Out of memory.\n");
      exit(1);
    }

  /* shell widget */
  newweekstate->myShell = XtParent(w);

  /* get all application-specific resources */
  XtGetApplicationResources(newweekstate->myShell,
			    (XtPointer) newweekstate,
			    week_Resources, XtNumber(week_Resources),
			    NULL, 0);

  /* associate data with widget */
  WcAttachThisToWidget((XtPointer) newweekstate, "AppState", w);
		       
  return;
}

void
MainButtonSetBGColor(w, client_data, cbs)
     Widget w;
     WcMethodData client_data;
     XmAnyCallbackStruct *cbs;
{
  weekstate myweekstate;
  Pixel pixel;
  Status retval;

  myweekstate = (weekstate) (client_data->object);
  if (!(myweekstate))
    {
      fprintf(stderr, "MainButtonSetBGColor: NULL client_data->object\n");
      fprintf(stderr, "called from %s\n", WcWidgetToFullName(w));
      exit(1);
    }

  if (!(myweekstate->mainButton))
    {
      fprintf(stderr, "MainButtonSetBGColor: NULL myweekstate->mainButton\n");
      fprintf(stderr, "called from %s\n", WcWidgetToFullName(w));
      exit(1);
    }

  /* get background color of calling widget */
  XtVaGetValues(w,
		XmNbackground, &pixel,
		NULL);

  /* set background color of main button */
  XmChangeColor(myweekstate->mainButton, pixel);

  /* set label of main button */
  XtVaSetValues(myweekstate->mainButton,
		XmNlabelString, XmStringCreateSimple(XtName(w)),
		NULL);

  return;
}

void
MainLabelFrob(w, client_data, cbs)
     Widget w;
     WcMethodData client_data;
     XmAnyCallbackStruct *cbs;
{
  weekstate myweekstate;
  char *label;
  Status retval;

  myweekstate = (weekstate) (client_data->object);
  if (!(myweekstate))
    {
      fprintf(stderr, "MainLabelFrob: NULL client_data->object\n");
      fprintf(stderr, "called from %s\n", WcWidgetToFullName(w));
      exit(1);
    }

  if (!(myweekstate->mainLabel))
    {
      fprintf(stderr, "MainLabelFrob: NULL myweekstate->mainButton\n");
      fprintf(stderr, "called from %s\n", WcWidgetToFullName(w));
      exit(1);
    }

  printf("called\n");

  XtVaSetValues(myweekstate->mainLabel,
		XmNlabelString, XmStringCreateSimple("foo"),
		NULL);
  return;
}
