#include "toolbox.h"
#include "event.h"
#include "gadgets.h"
#include "vars.h"
#include "printing.h"
#include "swis.h"
#include "utils.h"

extern void do_printing_initialise(void)
{
  raise_error(event_register_toolbox_handler(printing_window,ActionButton_Selected,printing_window_click_handler,0));
  raise_error(event_register_toolbox_handler(printing_window,OptionButton_StateChanged,printing_window_click_handler,0));
  raise_error(event_register_toolbox_handler(printing_window,RadioButton_StateChanged ,printing_window_click_handler,0));

/*  config.printing.printing_options = RISCOS_PRINTER_STREAM;
  config.printing.direct_parallel  = TRUE;*/
}

extern void setup_printing_window(void)
{
  update_printing_window();
  toolbox_show_object(0,printing_window,0,0,0,0);
}

void update_printing_window(void)
{
  switch(config.printing.printing_options)
  {
    case RISCOS_PRINTER_STREAM:
     radiobutton_set_state(0,printing_window,PRINTING_RO_PRINT_STREAM,1);
     break;
    case LPT1_TO_RISCOS_STREAM:
     radiobutton_set_state(0,printing_window,PRINTING_LPT1_TO_RO_STREAM,1);
     break;
    case LPT2_TO_RISCOS_STREAM:
     radiobutton_set_state(0,printing_window,PRINTING_LPT2_TO_RO_STREAM,1);
     break;
  }

  if(config.printing.direct_parallel)
  {
   optionbutton_set_state(0,printing_window,PRINTING_DIRECT_PARALLEL,1);
   raise_error(gadget_set_flags(0,printing_window,PRINTING_TURBODRIVERBODGE,0));
  }
  else
  {
   optionbutton_set_state(0,printing_window,PRINTING_DIRECT_PARALLEL,0);
   raise_error(gadget_set_flags(0,printing_window,PRINTING_TURBODRIVERBODGE,Gadget_Faded));
  }
   
  if(config.printing.turbodriverbodge)
   optionbutton_set_state(0,printing_window,PRINTING_TURBODRIVERBODGE,1);
  else
   optionbutton_set_state(0,printing_window,PRINTING_TURBODRIVERBODGE,0);
}

int printing_window_click_handler(int event_code, ToolboxEvent *event, IdBlock *id_block,void *handle)
{
  int selected;
  NotUsed (handle);
  NotUsed (event);
  NotUsed (event_code);

  switch(id_block->self_component)
  {
    int Directparallel_state;
    
    case PRINTING_OK:

     optionbutton_get_state(0,printing_window,PRINTING_DIRECT_PARALLEL,&tmp_config.printing.direct_parallel);
     optionbutton_get_state(0,printing_window,PRINTING_TURBODRIVERBODGE,&tmp_config.printing.turbodriverbodge);
     radiobutton_get_state(0,printing_window,PRINTING_RO_PRINT_STREAM,0,&selected);

     switch(selected)
     {
       case PRINTING_RO_PRINT_STREAM:
        tmp_config.printing.printing_options = RISCOS_PRINTER_STREAM;
        break;

       case PRINTING_LPT1_TO_RO_STREAM:
        tmp_config.printing.printing_options = LPT1_TO_RISCOS_STREAM;
        break;

       case PRINTING_LPT2_TO_RO_STREAM:
        tmp_config.printing.printing_options = LPT2_TO_RISCOS_STREAM;
        break;
     }
     check_for_modified_data((int)&config.printing, (int)&tmp_config.printing, sizeof(config.printing));
     config.printing = tmp_config.printing;
     break;

    //if using riscos printer then you can't use direct parallel
    case PRINTING_LPT1_TO_RO_STREAM:
    case PRINTING_LPT2_TO_RO_STREAM:
     raise_error(optionbutton_set_state(0,printing_window,PRINTING_DIRECT_PARALLEL,0));
     raise_error(optionbutton_set_state(0,printing_window,PRINTING_TURBODRIVERBODGE,0));
     //need to grey out turbodriver bodge too
     raise_error(gadget_set_flags(0,printing_window,PRINTING_TURBODRIVERBODGE,Gadget_Faded));
     break;

    case PRINTING_DIRECT_PARALLEL:
     raise_error(radiobutton_set_state(0,printing_window,PRINTING_RO_PRINT_STREAM,1));
     optionbutton_get_state(0,printing_window,PRINTING_DIRECT_PARALLEL,&Directparallel_state);
     //grey out tdriverbodge if directparallel not selected
     if (Directparallel_state)
     raise_error(gadget_set_flags(0,printing_window,PRINTING_TURBODRIVERBODGE,0));
     else
     raise_error(gadget_set_flags(0,printing_window,PRINTING_TURBODRIVERBODGE,Gadget_Faded));
     break;

   }
  return(1);
}
