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

char button_bits[] = {
   0xf8, 0x1f, 0x00, 0x04, 0x20, 0x00, 0x02, 0x40, 0x00, 0xf9, 0x9f, 0x00,
   0xf9, 0x9f, 0x01, 0x01, 0x80, 0x03, 0x01, 0x80, 0x03, 0xf9, 0x9f, 0x03,
   0xf9, 0x9f, 0x03, 0x01, 0x80, 0x03, 0x01, 0x80, 0x03, 0xf9, 0x9f, 0x03,
   0xf9, 0x9f, 0x03, 0x02, 0xc0, 0x03, 0x04, 0xe0, 0x03, 0xf8, 0xff, 0x01,
   0xf0, 0xff, 0x00, 0xe0, 0x7f, 0x00};

char* Fonts[NUMBER_OF_FONTS][10] = {
  {"-adobe-times-bold-r-normal--24-240-75-75-p-132-iso8859-1",
     "-adobe-times-bold-r-normal--18-180-75-75-p-99-iso8859-1",
     "9x15",
     "8x13bold",
     "8x13",
   },
  {"-adobe-times-bold-r-normal--18-180-75-75-p-99-iso8859-1",
     "-adobe-times-bold-r-normal--14-140-75-75-p-77-iso8859-1",
     "8x13bold",
     "8x13",
     "9x15",
   },
  {"-adobe-times-bold-r-normal--14-140-75-75-p-77-iso8859-1",
     "-adobe-times-bold-r-normal--12-120-75-75-p-67-iso8859-1",
     "8x13bold",
     "8x13",
     "9x15",
     "6x13",
   },
  {"-adobe-times-medium-r-normal--14-140-75-75-p-74-iso8859-1",
     "-adobe-times-medium-r-normal--12-120-75-75-p-64-iso8859-1",
     "-adobe-times-medium-r-normal--10-100-75-75-p-54-iso8859-1",
     "8x13",
   },
};

Registry Fonts_FR=NULL;

/*--------------------*/
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));
}

/*-----------------------*/
XFontStruct * AlUI_GetFont(w,FName)
Widget w;
int FName;
{
  XID Font_id;
  XFontStruct *FS;
  Display *Dis;
  char *name;
  int i;

  if (FName>=NUMBER_OF_FONTS || FName<0)
    Al_fatal_error("AlUI_GetFont: font number out of range.\n");
  FS=(XFontStruct*)Registry_get(Fonts_FR,(VOIDP)FName);
  if (FS==NULL) {
    Dis=XtDisplay(w);
    i=0;
    while ((name=Fonts[FName][i++])!=NULL) {
      FS=XLoadQueryFont(Dis,name);
      if (FS!=NULL) break;
    }
    if (FS==NULL) 
      FS=XLoadQueryFont(Dis,"fixed");
    if (FS==NULL)
      Al_fatal_error("Could not load fixed font!\n");
    Registry_add(Fonts_FR,(VOIDP)FName,(VOIDP)FS);
  }
  return(FS);
}



/*-----------------------------*/
void AlUI_Initialize()
{
  if (Fonts_FR==NULL)
    Fonts_FR=Registry_create(Registry_ptrcmp,Registry_ptrhash);
}


/*--------------------------------------------------*/
NORET Alxt_SetArg(w,name,value)
Widget w;
String name;
XtArgVal value;
{
  ArgPack AP;

  AP=ArgPack_create();
  ArgPack_add_arg(AP,name,value);
  XtSetValues(w,ArgPack_the_args(AP),1);
  ArgPack_delete(AP);
}


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

  display=XtDisplay(w);
  screen=XtScreen(w);
  ScreenNumber=XDefaultScreen(display);
  MenuPM=XCreatePixmapFromBitmapData(display,
				     RootWindowOfScreen(screen),
				     button_bits,
				     button_width,
				     button_height,
				     XBlackPixel(display,ScreenNumber),
				     XWhitePixel(display,ScreenNumber),
				     DefaultDepthOfScreen(screen));
  return(MenuPM);
}

/*--------------------------------------------------
Pixel  Alxt_get_foreground()
{
  return((Pixel)Alxt_get_widget_feature(TestWidget_GR,XtNforeground));
}

Pixel  Alxt_get_background()
{
  return((Pixel)Alxt_get_widget_feature(TestWidget_GR,XtNbackground));
}
*/

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

/*--------------------------------------------------*/
Bool Alxt_set_menu_item_with_arg(mp,item_name,arg,value)
MenuPack mp;
char *item_name, *arg;
XtArgVal value;
{
  Widget SW;
				  
  if ((SW=MenuPack_get_widget(mp,item_name))!=NULL) {
    Alxt_SetArg(SW,arg,value);
    return(Bool_TRUE);
  }
  else
    return(Bool_FALSE);
}
