#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include "Mintrins.h"
#include <Xm/Form.h>
#include "xstuff.h"
#include "assert.h"
#include "registry.h"
#include "useful.h"
#include "menupack.h"
#include "al.h"             /* lens dependant */
#include "menu_icon.h"

/*--------------------*/
void AlUI_AttachCursor(w,CursorNumber)
Widget w;
int CursorNumber;
{
  Window Win;
  Display *Disp;
  Cursor Curs;

  if (w==NULL) {
    printf("AlUI_AttachCursor: invalid Widget passed in, not attaching\n");
    return;
  }
  if (CursorNumber % 2 !=0 ) {
    printf("AttachCursor: bad cursor number: must be a multiple of twp\n");
    return;
  }
  Disp=XtDisplay(w);
  Curs=XCreateFontCursor(Disp,(unsigned)CursorNumber);
  Win=XtWindow(w);
  XDefineCursor(Disp,Win,Curs);
}

/*---------------------------*/
void AlUI_DetachCursor(w)
Widget w;
{
  assert(w!=NULL);
  XUndefineCursor(XtDisplay(w),XtWindow(w));
}

/*-----------------------------*/
void AlUI_Initialize()
{

}


/*--------------------------------------------------*/
NORET Alxt_SetArg(w,name,value)
Widget w;
String name;
XtArgVal value;
{
  Arg arg[1];

  arg[0].name=name;
  arg[0].value=value;
  XtSetValues(w,arg,1);
}


/*--------------------------------------------------*/
Pixmap Alxt_get_menu_pixmap(w)
     Widget w;
{
  static Pixmap MenuPM=NULL;
  Screen *screen;
  Display *display;
  int ScreenNumber;
  Pixel fg, bg;

  fg=(Pixel)Alxt_get_widget_feature(w,XmNforeground);
  bg=(Pixel)Alxt_get_widget_feature(w,XmNbackground);

  display=XtDisplay(w);
  screen=XtScreen(w);
  ScreenNumber=XDefaultScreen(display);
  MenuPM=XCreatePixmapFromBitmapData(display,
				     RootWindowOfScreen(screen),
				     menu_icon_bits,
				     menu_icon_width,
				     menu_icon_height,
				     fg,
				     bg,
				     DefaultDepthOfScreen(screen));
  return(MenuPM);
}

/*--------------------------------------------------*/
VOIDP Alxt_get_widget_feature(W,Feature)
Widget W;
char *Feature;
{
  ArgPack AP;
  XtArgVal ReturnVal;
  AP=ArgPack_create();
  ArgPack_add_arg(AP,Feature,(XtArgVal)&ReturnVal);
  XtGetValues(W,ArgPack_the_args(AP),ArgPack_num_args(AP));
  return((VOIDP)ReturnVal);
}

/*--------------------------------------------------*/
NORET Alxt_ReverseVideo(w)
Widget w;
{
  Pixel fg, bg;
  fg=(Pixel)Alxt_get_widget_feature(w,XmNforeground);
  bg=(Pixel)Alxt_get_widget_feature(w,XmNbackground);
  Alxt_SetArg(w,XmNforeground,bg);
  Alxt_SetArg(w,XmNbackground,fg);
}

/*--------------------------------------------------*/
NORET Alxt_NormalVideo(w,prot)
Widget w,prot;
{
  Pixel fg, bg;
  fg=(Pixel)Alxt_get_widget_feature(prot,XmNforeground);
  bg=(Pixel)Alxt_get_widget_feature(prot,XmNbackground);
  Alxt_SetArg(w,XmNforeground,fg);
  Alxt_SetArg(w,XmNbackground,bg);
}

/*--------------------------------------------------*/
NORET Alxt_center_text_in_text_widget(tw)
Widget tw;
{
  int height, marginHeight, fontHeight, realHeight, fontHeight_;
  XmFontList fontList;
  XFontStruct *font;

  height=(int)Alxt_get_widget_feature(tw,XmNheight);
  marginHeight=(int)Alxt_get_widget_feature(tw,XmNmarginHeight);
  fontList=(XmFontList)Alxt_get_widget_feature(tw,XmNfontList);

  /*
    * I am, of course, assuming here that the first font in the fontlist is
    * the font being used. This assumption coincides with my experience.
   */
  font=(fontList[0]).font;

  fontHeight_=font->ascent+font->descent;
  fontHeight=XmConvertUnits(tw,XmVERTICAL, 
			    XmPIXELS,
			    fontHeight_,
			    Xm100TH_MILLIMETERS);
  
  realHeight=(marginHeight*2)+fontHeight;

  if (realHeight>height) {
    printf("real>height\n");
    Alxt_SetArg(tw,XmNheight,(XtArgVal)realHeight);
  }

  else if (realHeight<height) {
    printf("real<height\n");
    Alxt_SetArg(tw,XmNmarginHeight, (XtArgVal)(height-fontHeight)/2);
  }
  printf("marginHeight=%d, height=%d, ",marginHeight, height);
  printf("fontheight=%d, realHeight=%d\n",fontHeight,realHeight);
}
