/* Copyright 1993 by gregg hanna */
/*
 * Generated by the ICS builderXcessory (BX).
 *
 *
 * Builder Xcessory 1.0.1.
 *
 */
/*
 * REQUIRED MOTIF INCLUDE FILES
 */
#include <Xm/Xm.h>
#include <X11/Shell.h>
#include <Xm/DialogS.h>
#include <X11/StringDefs.h>
#include <stdio.h>

/* GLOBAL WIDGET VARIABLES */
Widget Shell000;
Widget Form;
Widget Createform();
Widget cardForm;
Widget confirmDialog;
Widget CreateMessageDialog();
Widget CreateFormDialog();
Widget CreatePicker();

#ifdef XFILESEARCHPATH
static void AddPathToSearchPath();
#endif

void dumpcore(){char *s; s = 0; *s = 'x'; }

#include "roloP.h"
#include "rolo.h"
#include "mrolo.bm"
#include "compat.h"

typedef struct {
  String  indexElements;
  Boolean showSecondary;
  int     sizePrimary;
  Boolean selectFilter;
} mroloAppResources, *mroloAppResourcesP;

#define MOffset(x) XtOffset(mroloAppResourcesP, x)

static XtResource resources[] = {
  {"indexElements", "IndexElements", XtRString, sizeof(String),
     MOffset(indexElements), XtRString, "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"},
  {"showSecondary", "ShowSecondary", XtRBoolean, sizeof(Boolean),
     MOffset(showSecondary), XtRImmediate, (XtPointer) True},
  {"sizePrimary", "SizePrimary", XtRInt, sizeof(int),
     MOffset(sizePrimary), XtRImmediate, (XtPointer) 20},
  {"selectFilter", "SelectFilter", XtRBoolean, sizeof(Boolean),
     MOffset(selectFilter), XtRImmediate, (XtPointer) False},
};

static char *appDefaults[] = {
/* #include "MRolo.ad.h" */
      NULL
};

Boolean showSecondary;
int sizePrimary;
String indexElements;

void SetResources(w)
   Widget  w;
{
  mroloAppResources appResources;
  XtGetApplicationResources(w, (XtPointer) &appResources,
			    resources, XtNumber(resources), NULL, 0);
  showSecondary = appResources.showSecondary;
  sizePrimary = appResources.sizePrimary;
  indexElements = mystrdup(appResources.indexElements);
  if ( ! selectFilter )
    selectFilter = (int)appResources.selectFilter;
}

void SetIcon(w)
	Widget w;
{
  Pixmap p;
  Arg arg[1];

  p = XCreateBitmapFromData(XtDisplay(w),XtWindow(w),mrolo_bits,mrolo_width,mrolo_height);
  XtSetArg(arg[0],XmNiconPixmap,p);
  XtSetValues(w,arg,1);
}

#define APP_NAME "MRolo"

/* Tweeks the size of a widget to make old ScrolledLists work right */
void DoResize()
{
  Dimension width, height, bwidth;

  XtVaGetValues(Shell000,XmNwidth,&width,XmNheight,&height,XmNborderWidth,&bwidth,NULL);
  XtResizeWidget(Shell000,width,height-1,bwidth);
  XtResizeWidget(Shell000,width,height,bwidth);
}

/* MAIN PROGRAM */
main(argc, argv)
int argc;
char **argv;
{
    Arg 	args[256];
    int 	argcnt;
    XtAppContext context;
    extern Widget filterText;
    extern Widget picker;

#ifdef XFILESEARCHPATH
    AddPathToSearchPath(XFILESEARCHPATH);
#endif

    XmRegisterConverters();
    argcnt = 0;
    XtSetArg(args[argcnt], XmNtitle, "Rolodex"); argcnt++;
    Shell000 = XtAppInitialize(&context,APP_NAME,NULL,0,(Cardinal*)&argc,argv,
			       appDefaults,args,argcnt);
    SetResources(Shell000);

    InitRolo(argc,argv,1);
    SetupRolo();

    Form = Createform(Shell000);
    XtManageChild(Form);

    cardForm = CreateFormDialog(Shell000);
    confirmDialog = CreateMessageDialog(Shell000);

    if ( selectFilter )
      picker = CreatePicker(Shell000);

    XtRealizeWidget(Shell000);

    SetIcon(Shell000);
    PutListOnScreen();

#ifdef WANT_CORE
    XSetErrorHandler(dumpcore);
#endif

    XmProcessTraversal(filterText,XmTRAVERSE_CURRENT);

    DoResize();  /* kludge for old ScrolledList */

    XtAppMainLoop(context);

    return 0;

}

#ifdef XFILESEARCHPATH
static void
AddPathToSearchPath(path)
char *path;
{
     char *old, *new;

     old = getenv("XFILESEARCHPATH");
     if (old) {
#ifndef HAVE_SETENV
	  /* +1 for =, +2 for :, +3 for null */
	  new = malloc(strlen("XFILESEARCHPATH") + strlen(old) +
		       strlen(path) + 3);
	  strcpy(new, "XFILESEARCHPATH");
	  strcat(new, "=");
	  strcat(new, old);
	  strcat(new, ":");
	  strcat(new, path);
	  putenv(new);
#else
	  /* +1 for colon, +2 for null */
	  new = malloc(strlen(old) + strlen(path) + 2);
	  strcpy(new, old);
	  strcat(new, ":");
	  strcat(new, path);
	  setenv("XFILESEARCHPATH", new, 1);
#endif
     } else {
#ifndef HAVE_SETENV
	  new = malloc(strlen("XFILESEARCHPATH") + strlen(path) + 2);
	  strcpy(new, "XFILESEARCHPATH");
	  strcat(new, "=");
	  strcat(new, path);
	  putenv(new);
#else
	  setenv("XFILESEARCHPATH", path, 1);
#endif
     }
}
#endif
