/*
 * Input return setting and raw callback. Terrible hack.
 */
#include "forms.h"
#include <stdlib.h>

extern void howreturn_callback(FL_OBJECT *, long);
extern void input_callback(FL_OBJECT *, long);

/**** Forms and Objects ****/

typedef struct {
	FL_FORM *inputform;
	FL_OBJECT *input1;
	FL_OBJECT *howreturn;
	FL_OBJECT *status;
	FL_OBJECT *input2;
	void *vdata;
	long ldata;
} FD_inputform;

extern FD_inputform * create_form_inputform(void);

/* end of minput.h */

FD_inputform *ui;

int peek_event(FL_FORM *form, void *xev)
{
    if(((XEvent *)xev)->type == KeyPress)
    {
       fl_set_object_label(ui->status,"keyboard input");
       XFlush(fl_get_display()); /* necessary to show the label */
       fl_msleep(50);
    }
    return 0;  
}

int main(int argc, char *argv[])
{

    fl_initialize(&argc, argv, "FormDemo", 0, 0);
    ui = create_form_inputform();
    fl_register_raw_callback(ui->inputform, KeyPressMask, peek_event);
    fl_show_form(ui->inputform, FL_PLACE_CENTER, FL_TRANSIENT, "Input");
    fl_do_forms();
    return 0;
}

void input_callback(FL_OBJECT *ob, long data)
{ 
     char buf[32];
     sprintf(buf,"Input%ld returned",data);
     fl_set_object_label(ui->status,buf);
     XFlush(fl_get_display());
     fl_msleep(50);
}

void howreturn_callback(FL_OBJECT *ob, long data)
{
    fl_set_input_return(ui->input1, fl_get_button(ob));
    fl_set_input_return(ui->input2, fl_get_button(ob));
}

FD_inputform *create_form_inputform(void)
{
  FL_OBJECT *obj;
  FD_inputform *fdui = fl_calloc(1, sizeof(FD_inputform));

  fdui->inputform = fl_bgn_form(FL_NO_BOX, 475, 485);
  obj = fl_add_box(FL_UP_BOX,0,0,475,485,"");
  fdui->input1 = obj = fl_add_input(FL_MULTILINE_INPUT,15,275,350,180,"");
    fl_set_object_lalign(obj,FL_ALIGN_TOP);
    fl_set_object_callback(obj,input_callback,1);
  fdui->howreturn = obj = fl_add_checkbutton(FL_PUSH_BUTTON,375,435,80,35,
		     "always\nreturn");
    fl_set_object_color(obj,FL_COL1,FL_BLUE);
    fl_set_object_callback(obj,howreturn_callback,0);
  fdui->status = obj = fl_add_text(FL_NORMAL_TEXT,20,15,270,30,"");
    fl_set_object_boxtype(obj,FL_FRAME_BOX);
  obj = fl_add_button(FL_NORMAL_BUTTON,375,15,80,35,"Done");
  fdui->input2 = obj = fl_add_input(FL_MULTILINE_INPUT,15,60,349,185,"");
    fl_set_object_lalign(obj,FL_ALIGN_TOP);
    fl_set_object_callback(obj,input_callback,2);
  fl_end_form();

  return fdui;
}

