#!/bin/sh
# Copyright (c) 1997  Dustin Sallings
#
# $Id: dothings,v 1.3 1997/11/14 09:07:38 dustin Exp $
#
# Build the compose options

ComposeTextFields=ComposeTextFields
ComposeBoolFields=ComposeBoolFields
Preferences=Preferences

# FUNCTION doComposeOptions

doComposeOptions()
{

cat <<EOF
/* This was automatically generated, back off punk */

#include "ml.h"

#ifdef __STDC__
MLComposeOptions *new_MLComposeOptions(void)
#else
MLComposeOptions *new_MLComposeOptions()
#endif
{
  MLComposeOptions *options = (MLComposeOptions *)
      fs_get(sizeof(MLComposeOptions));
  options->window          = NULL;
  options->prefix          = cpystr(preferences.reply_prefix);

EOF

    awk ' {
	    printf "  options->%s = preferences.%s;\n", $1, $2
	    printf "  options->%s_tmp = preferences.%s;\n", $1, $2
	  }' $ComposeBoolFields

    awk ' {
	printf "  options->%s = cpystr(preferences.%s);\n", $1, $2
    } ' $ComposeTextFields

cat <<EOF
  return(options);
}

EOF

}

# FUNCTION docompose_preferences

docompose_preferences()
{
cat <<EOF

Menu copts_menu[] = {

  { NULL, "accept", NUL_TERM,
      cwin_accept, NULL, 0, NULL, NULL, BTN_ON },
  { NULL, "save", NUL_TERM,
      cwin_save, NULL, 0, NULL, NULL, BTN_ON },
  { NULL, "dismiss", NUL_TERM,
      cwin_dismiss, NULL, 0, NULL, NULL, BTN_ON },
  { NULL, "HELP", NUL_TERM,
      cwin_help, NULL, 0, NULL, NULL, BTN_ON },
};

#ifdef __STDC__
void compose_preferences(MLCompose *compose)
#else
void compose_preferences(compose)
     MLCompose *compose;
#endif
{

  Arg args[ARGLISTSIZE];
  int n = 0;
  MLComposeOptions *options;
  MLComposeOptionsWindow *win;
 
  if(compose->options->window != NULL) {
    win = compose->options->window;
    if(win->destroyed == FALSE) {
      if(win->is_realized == TRUE)
        de_iconify(win->shell);
      else
        XtPopup(win->shell, XtGrabNone);
      return;
    }
  }

  options = compose->options;
  options->window = (MLComposeOptionsWindow *)
    fs_get(sizeof(MLComposeOptionsWindow));


  win = options->window;
  win->buttonlist = NULL;
  win->destroyed = FALSE;
  win->is_realized = FALSE;

  if(preferences.autoPlace == TRUE) {
    XtSetArg(args[n], XtNx, -1000); n ++;
    XtSetArg(args[n], XtNy, -1000); n ++;
  }

  XtSetArg (args[n], XmNdeleteResponse, XmDO_NOTHING); n++;
  win->shell = XtCreatePopupShell("composeOptions",
                                  topLevelShellWidgetClass,
                                  compose->window->shell,
                                  args, n);
  AddDestroyCallback (win->shell);
  n = 0;

  setup_editres(win->shell);

  if(ml_icon != (Pixmap) None)
    XtVaSetValues(win->shell,
                  XmNiconPixmap,ml_icon,
                  NULL);


  win->form = XmCreateForm(win->shell, "form", args, n ); n = 0;

  XtSetArg(args[n], XmNtopAttachment,   XmATTACH_FORM); n ++;
  XtSetArg(args[n], XmNleftAttachment,  XmATTACH_FORM); n ++;
  XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n ++;
  win->menubar = XmCreateMenuBar(win->form, "menubar", args, n); n = 0;
  XtManageChild(win->menubar);


  make_buttons(&win->buttonlist, NULL,
               win->menubar, copts_menu,
               XtNumber(copts_menu),
               BTN_ON, (XtPointer) compose,
               ROOTMENULEVEL);

EOF


    awk ' BEGIN { OLD="menubar" }
    {
     printf "  win->%s =\n", $1
     printf "    create_text_field(win->form, win->%s,\n", OLD
     printf "        \"%s\",\n", $1
     printf "        options->%s,\n", $1
     printf "        0, NULL, NULL);\n\n"
     OLD=$1
    } ' $ComposeTextFields

cat <<EOF
  XtSetArg(args[n], XmNadjustMargin, FALSE);      n ++;
  XtSetArg(args[n], XmNmarginWidth, 0);           n ++;
  XtSetArg(args[n], XmNborderWidth, 0);           n ++;

  XtSetArg(args[n], XmNnumColumns, 3);                       n ++;
  XtSetArg(args[n], XmNtopAttachment,   XmATTACH_WIDGET);    n ++;
  XtSetArg(args[n], XmNtopWidget, win->language);            n ++;
  XtSetArg(args[n], XmNleftAttachment,  XmATTACH_FORM);      n ++;
  XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM);      n ++;
  XtSetArg(args[n], XmNorientation,     XmVERTICAL);         n ++;
  XtSetArg(args[n], XmNpacking, XmPACK_COLUMN);              n ++;
  win->rowcol = XtCreateWidget("rowcol",
                                   xmRowColumnWidgetClass,
                                   win->form, args, n );
  XtManageChild(win->rowcol);

EOF

    awk '
       {
	   printf "  n=0;\n  XtSetArg(args[n], XmNborderWidth, 0);\n"
	   printf "  XtSetArg(args[n], XmNtraversalOn, FALSE);\n"
	   printf "  win->%s = XmCreateToggleButton(win->rowcol,", $1
	   printf " \"%s_toggle\",\n    args, n);", $1
	   printf "  XtManageChild(win->%s);\n\n", $1

	   printf "  XtAddCallback(win->%s, XmNvalueChangedCallback,\n", $1
	   printf "    (XtCallbackProc) cwin_%s, compose);\n\n\n", $1
       } ' $ComposeBoolFields

cat <<EOF
  XtAddCallback(win->shell, XmNpopdownCallback,
                (XtCallbackProc) cwin_popdown, compose);

  XtAddCallback(win->shell, XmNdestroyCallback,
                (XtCallbackProc) cwin_destroy, compose);

  set_initial_buttons(compose);


  XtManageChild(win->form);
  XtManageChild(win->shell);
  XtPopup(win->shell, XtGrabNone);
  position_popup_widget(win->shell, FALSE);
  win->is_realized = TRUE;
  return;
}

EOF

}

# FUNCTION set_initial_buttons

doset_initial_buttons()
{
cat <<EOF
#ifdef __STDC__
void set_initial_buttons(MLCompose *compose)
#else
void set_initial_buttons(compose)
     MLCompose *compose;
#endif
{
  MLComposeOptions *options = compose->options;
  MLComposeOptionsWindow *win = compose->options->window;

EOF

    awk ' {
	printf "  XmToggleButtonSetState(win->%s,options->%s,FALSE);\n", $1, $1
          } ' $ComposeBoolFields

cat <<EOF
  return;
}

EOF

}

# FUNCTION cwin_save

docwin_save()
{
cat <<EOF
#ifdef __STDC__
void cwin_save(Widget w, MLCompose *compose, XtPointer xp)
#else
void cwin_save(w, compose, xp)
     Widget w;
     MLCompose *compose;
     XtPointer xp;
#endif
{
  MLComposeOptions *options = compose->options;
  get_compose_options(compose);

EOF


    awk ' {
      printf "  fs_give((void **) &preferences.%s);\n", $2
      printf "  preferences.%s = cpystr(options->%s);\n", $2, $1
    } ' $ComposeTextFields

    echo ""

    awk ' {
      printf "  preferences.%s = options->%s;\n", $2, $1
    } ' $ComposeBoolFields

cat <<EOF

  save_defaults();
  cwin_dismiss(w,compose,xp);
}

EOF

}

# FUNCTION doPreferences

doPreferences()
{
    echo "typedef struct _PREFERENCES_ {"
    echo ""

    awk ' {
	printf "  %s %s;\n", $1, $2
    } ' $Preferences

    awk ' {
	printf "  Boolean %s;\n", $2
    } ' $ComposeBoolFields

    awk ' {
	printf "  char *%s;\n", $2
    } ' $ComposeTextFields

    echo "} Preferences;"
    echo ""
}

# FUNCTION doMLComposeOptionsWindow
doMLComposeOptionsWindow()
{
    echo "typedef struct _ML_COMPOSE_OPTIONS_WINDOW_ {"
    echo "  Tool form;"
    echo "  Tool menubar;"
    echo "  Tool prefix;"
    echo "  Tool rowcol;"
    echo "  Tool shell;"
    echo ""

    awk ' {
	printf "  Tool %s;\n", $1
    } ' $ComposeBoolFields $ComposeTextFields

    cat <<EOF

  ButtonList *buttonlist;
  Boolean is_realized;
  Boolean destroyed;

} MLComposeOptionsWindow;

EOF
}

# FUNCTION doMLComposeOptions
doMLComposeOptions()
{
    echo "typedef struct _ML_COMPOSE_OPTIONS_ {"
    echo "  MLComposeOptionsWindow *window;"
    echo ""

    awk ' {
	printf "  Boolean %s;\n  Boolean %s_tmp;\n", $1, $1
    } ' $ComposeBoolFields

    echo ""

    awk ' {
	printf "  char *%s;\n", $1
    } ' $ComposeTextFields

    echo "  char* prefix;"

    echo ""
    echo "} MLComposeOptions;"
}

# FUNCTION doTypedefs
doTypedefs()
{
    cat <<EOF
typedef unsigned long ButtonState;

/*
 * This structure was designed for much more general use than is currently
 * implemented. The label and mnemonic remain unused to allow over-ride by
 * Xdefaults. These, or the widget in association with the callback
 * function and callback data allow "on-the-fly" changes of the entire
 * button configuration.
 */

typedef struct _menu_structure_ {
  char                     *label;      /* label. Unused.        */
  char                     *name;       /* resource name.        */
  char                     mnemonic;    /* Mnemonic key. Unused. */
  void                     (*func)();   /* Callback function or  */
  struct _menu_structure_  *sub_menu;   /* recursive menu with   */
  int                      size;        /* known size.           */
  caddr_t                  data;        /* Callback data. Unused.*/
  Tool                     widget;      /* The button itself.    */
  unsigned long            statemap;    /* sensitivity flags.    */
} Menu;

typedef struct _BUTTON_LIST_ {
  Tool w;
  ButtonState state;
  struct _BUTTON_LIST_ * next;
} ButtonList;
EOF
}

# FUNCTION doget_compose_options
doget_compose_options()
{
   cat <<EOF
#ifdef __STDC__
void get_compose_options(MLCompose *compose)
#else
void get_compose_options(compose)
     MLCompose *compose;
#endif
{
  MLComposeOptions *options = compose->options;

  if(options->word_wrap != options->word_wrap_tmp)
    if(options->word_wrap_tmp == TRUE)
      compose_wrap_on(NULL,compose,NULL);
    else
      compose_wrap_off(NULL,compose,NULL);

EOF

    awk ' {
	printf "  fs_give((void **) &options->%s);\n", $1
	printf "  options->%s = GetTextField(options->window->%s);\n", $1, $1
    } ' $ComposeTextFields

    echo ""

    awk ' {
	printf "  options->%s = options->%s_tmp;\n", $1, $1
    } ' $ComposeBoolFields

    echo "return;"
    echo "}"
    echo ""
}

makeBoolCallback()
{
cat <<EOF
#ifdef __STDC__
void cwin_$1(Widget w,MLCompose *compose,
                  XmToggleButtonCallbackStruct *xp)
#else
void cwin_$1(w,compose,xp)
     Widget w;
     MLCompose *compose;
     XmToggleButtonCallbackStruct *xp;
#endif
{
  MLComposeOptions *options = compose->options;
  if(xp->set == TRUE)
    options->$1_tmp = TRUE;
  else
    options->$1_tmp = FALSE;
  return;
}

EOF
}

# FUNCTION dofree_MLComposeOptions
dofree_MLComposeOptions()
{
cat <<EOF
#ifdef __STDC__
void free_MLComposeOptions(MLComposeOptions *options)
#else
void free_MLComposeOptions(options)
     MLComposeOptions *options;
#endif
{
  if(options) {
    if(options->window) {
      if(options->window->buttonlist)
        fs_give((void **) &options->window->buttonlist);
      fs_give((void **) &options->window);
    }
    if(options->prefix)
      fs_give((void **) &options->prefix);

EOF

    awk ' {
	printf "    if(options->%s)\n", $1
	printf "      fs_give((void **)&options->%s);\n", $1
    } ' $ComposeTextFields

    cat <<EOF

    fs_give((void **) &options);
  }
  return;
}

EOF
}

# FUNCTION doBoolCallbacks
doBoolCallbacks()
{
    for i in `cut -f1 -d\  $ComposeBoolFields`
    do
	makeBoolCallback $i
    done
}

# DO C-code

do_C_code()
{
    doComposeOptions
    dofree_MLComposeOptions
    docompose_preferences
    doset_initial_buttons
    docwin_save
    doget_compose_options
    doBoolCallbacks
}

# DO Headers

do_Headers()
{
    echo "/* Automatically Generated, back off PUNK! */"
    echo ""
    echo "#ifndef _AUTOSHIT_H"
    echo "#define _AUTOSHIT_H"
    doTypedefs
    doPreferences
    doMLComposeOptionsWindow
    doMLComposeOptions
    echo "#endif /* _AUTOSHIT_H */"
}

# BEGINNING

do_C_code > ../autoshit.c
do_Headers > ../autoshit.h
