/****************************************************************
** Title  :!IntSoundapp main ** **
** Purpose: internal sound(s) level and bip test
** Version: 0.00 19.11.21 JMB
** fichier: window.c
*****************************************************************/

#include "swis.h"
#include "kernel.h"
#include "assert.h"
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <stdio.h>
//#include <stdbool.h>


#define TOOLBOX_H   // #include "toolbox.h"
#define WIMP_H   // #include "wimp.h"
#define WINDOW_H
#define MESSAGETRANS_H

// **********  from Oslib support   **********
#include "OSLibSupport:SysTypes.h"
#include "OSLibSupport:Err.h"   // error handling library
#include "OSLibSupport:x.h"
#include "OSLibSupport:event.h"
#include "OSLibSupport:message.h"
#include "OSLibSupport:ToolBoxTypes.h"


// **********  from Oslib  **********
#include "OSLib/actionbutton.h"
#include "OSLib/adjuster.h"
#include "OSLib/button.h"
#include "OSLib/displayfield.h"
#include "OSLib/dragasprite.h"
#include "OSLib/fileswitch.h"
#include "OSLib/menu.h"
#include "OSLib/osfile.h"
#include "OSLib/osfscontrol.h"
#include "OSLib/proginfo.h"
#include "OSLib/quit.h"
#include "OSLib/radiobutton.h"
#include "OSLib/report.h"
#include "OSLib/slider.h"
#include "OSLib/sound.h"
#include "OSLib/taskmanager.h"
#include "OSLib/textarea.h"
#include "OSLib/toolaction.h"
#include "OSLib/wimpspriteop.h"


// ************* Cathlibcpp ************
//#include <cathlibcpp:fstream.h>
//#include <cathlibcpp:string.h>
//#include <cathlibcpp:sstream.h>

#define MAINWINDOWS
#ifndef false
 #define false FALSE
#endif
#ifndef true
 #define true TRUE
#endif

#include "Main.h"

#include "debug.h"
#include "ResEventComps.h"
//#include "iconbarmenu.h"
#include "iconbar.h"
#include "MainWindow.h"
#include "itoa.h"

void read_var_val(char *var, char *varextended )
{
  int len;
  int context=0;
  char par[256] ;
  #define VARTYPE_EXPANDED 3
  int vartype;

   if(  _swix(OS_ReadVarVal,  _INR(0,4) | _OUTR(2,4), var, par, 255 , context, VARTYPE_EXPANDED, &len, &context, &vartype)== NULL){
    par[len] = '\0';
    sprintf(varextended,"%s",par);
   } else {
     //debug_printf("Not found");
   }

}

//-----   constructeur ------
MainWindow::MainWindow(toolbox_id OBJ_TEMPLATE)
{
  x_exception xx;
 //debug_printf("Constructeur de MainWindow");
  x_TRY( &xx )
  {
    WindowId = toolbox_create_object( 0, OBJ_TEMPLATE );
    WindowHd = window_get_wimp_handle( 0, WindowId);
    //gMainWindow_id = WindowId; // global
    win_open = false;
    gfenprinstate = FALSE;
    slider1_cur = 127;
    sFenTitre = new char(64);
    TA_index = 0;
    textarea_set_state(0, WindowId,  TEXT_AREA,
                    ( textarea_ALLOW_SELECTION
                      |textarea_ALLOW_SAVE
                      | textarea_ALLOW_LOAD
                      | textarea_STATE_WORDWRAPPING_ENABLED
                      | textarea_CLIPBOARD
                      | textarea_STATE_HAS_VERTICAL_SCROLLBAR
                      | textarea_AUTO_SCROLL
                    ));
    TextArea_display("C++ development model\n ");
    TextArea_display("DDE using ToolBox, OSLib, OsLibSupport.\n ");
    TextArea_display("Built with a specific tool for DDE !Diderot. (jm)");


    displayfield_set_value(0, WindowId, DISPLAY_FIELD_3, "127" );
    slider_set_value(0, WindowId, SLIDER_1, slider1_cur);

    // ================  ToolBox handlers  registration  ================
    event_register_toolbox_handler( event_ANY, action_WINDOW_ABOUT_TO_BE_SHOWN, AboutToBeShow_handler, this);
    
    event_register_toolbox_handler(WindowId, action_ACTION_BUTTON_SELECTED, Actions_button_handler, this);
    
    event_register_toolbox_handler(WindowId, action_RADIO_BUTTON_STATE_CHANGED , Radios_button_handler, this);
    
    event_register_toolbox_handler(WindowId, action_SLIDER_VALUE_CHANGED , Sliders_handler, this);

    event_register_toolbox_handler(WindowId, action_ADJUSTER_CLICKED, Arrow_adjusters_handler, this);

    // ================  Wimp  handlers  registration  ================

    // ================  Message handlers  registration  ================

  }
  x_CATCH( &xx )
  {
    Err_ReportError( Err_FATAL, "Unable to create window: %s", xx.error -> errmess );
  }
}

MainWindow::~MainWindow()
{

  delete file_name;
  //debug_printf("Destructeur de MainWindow");
}


         /*-------------------------------------------------------------*/
         /* ---------           Mthodes          -------------         */
         /*-------------------------------------------------------------*/
osbool MainWindow::Titre_fen(char* titre) {
//osbool MainWindow::Titre_fen(char* titre) {
  //sFenTitre = titre;
  window_set_title(0,WindowId,titre);
  //window_set_title(0,WindowId,titre);
  return true;
}


void  MainWindow::TextArea_display(char *texte)
{
  textarea_insert_text(0, WindowId, TEXT_AREA, TA_index,texte );
  TA_index += textarea_get_text_size(0, WindowId, TEXT_AREA );
}

void  MainWindow::TextArea_clear(void)
{
  textarea_set_text(0, WindowId, TEXT_AREA  , "" );
  TA_index = 0;
}

osbool MainWindow::Open(void)
{
  if (!win_open){
//    sprintf(gbufMess," eXML workspace pointeur: %p\n", xml_workspace);
//    TextArea_display( gbufMess);
    win_open  = TRUE;
  }

  return true;
}


         /*------------------------------------------------------------*/
         /*---------           ToolBox Handlers           -------------*/
         /*------------------------------------------------------------*/
osbool MainWindow::AboutToBeShow_handler(   bits Event_Code, toolbox_action *Event, toolbox_block *TBblock, void *vHandle)
{
  IGNORE(Event_Code);  IGNORE(Event); IGNORE(TBblock);
  MainWindow* MWin ;
  MWin = ( MainWindow *) vHandle;
  MWin->Titre_fen("Bonjour");
  MWin->Open();
  return true;
}

osbool MainWindow::Actions_button_handler(bits Event_Code, toolbox_action *Event, toolbox_block *TBblock, void *vHandle)
{
  IGNORE(Event_Code);  IGNORE(Event); IGNORE(TBblock);
  MainWindow* MWin ;
  MWin = ( MainWindow *) vHandle;
  //toolbox_action * tbaction =  (toolbox_action*) pxEvent;
   switch(TBblock->this_cmp)
   {
   case ACTION_BUTTON_1:
      MWin->Titre_fen("Rhubarb");
      displayfield_set_value(0,TBblock->this_obj, DISPLAY_FIELD_1, "408");
      break;
    case ACTION_BUTTON_2:
      MWin->Titre_fen("Hello");
      displayfield_set_value(0,TBblock->this_obj, DISPLAY_FIELD_1, "208");
      break;    
           
   }
  
  return true;
}

osbool MainWindow::Radios_button_handler(bits Event_Code, toolbox_action *Event, toolbox_block *TBblock, void *vHandle)
{
  IGNORE(Event_Code);  IGNORE(Event); IGNORE(TBblock);
  MainWindow* MWin ;
  MWin = ( MainWindow *) vHandle;
  char *TBufferLabel;
  TBufferLabel = calloc(32,1);
  radiobutton_get_label(0, TBblock->this_obj, TBblock->this_cmp, TBufferLabel, 32);
   switch(TBblock->this_cmp)
   {
   case RADIO_BUTTON_1:
      //radiobutton_get_label(0, TBblock->this_obj, RADIO_BUTTON_1, TBufferLabel, 32);
      displayfield_set_value(0,TBblock->this_obj, DISPLAY_FIELD_2, TBufferLabel);
      break;
    case RADIO_BUTTON_2:
      //radiobutton_get_label(0, TBblock->this_obj, RADIO_BUTTON_2, TBufferLabel, 32);
      displayfield_set_value(0,TBblock->this_obj, DISPLAY_FIELD_2, TBufferLabel);
      break;
    case RADIO_BUTTON_3:
      //radiobutton_get_label(0, TBblock->this_obj, RADIO_BUTTON_3, TBufferLabel, 32);
      //debug_printf( "Radios_button_handler: %s",TBufferLabel);
      displayfield_set_value(0,TBblock->this_obj, DISPLAY_FIELD_2, TBufferLabel);
      break;     
   }
  
  return true;
}

osbool MainWindow::Sliders_handler(bits Event_Code, toolbox_action *Event, toolbox_block *TBblock, void *vHandle)
{
  IGNORE(Event_Code);  IGNORE(Event); IGNORE(TBblock);
  MainWindow* MWin ;
  MWin = ( MainWindow *) vHandle;
  char buf[6];
 // debug_printf( "Sliders_handler: ref_no: %d", Event->ref_no);
 // debug_printf( "Sliders_handler: receive slider value change, cmp: %d",TBblock->this_cmp);
    switch(TBblock->this_cmp)
   {
    case SLIDER_1:
      MWin->slider1_cur = slider_get_value(0, TBblock->this_obj, SLIDER_1);
      //debug_printf( "Sliders_handler: slider 1 value: %d", MWin->slider1_cur);
      displayfield_set_value(0,TBblock->this_obj, DISPLAY_FIELD_3, itoa2(MWin->slider1_cur,buf ));
      break;
   }
  return true;
}


osbool MainWindow::Arrow_adjusters_handler(bits Event_Code, toolbox_action *Event, toolbox_block *TBblock, void *vHandle)
{
  IGNORE(Event_Code);  IGNORE(Event); IGNORE(TBblock);
  MainWindow* MWin ;
  MWin = ( MainWindow *) vHandle;
  int n;
  char buf[6];
  #if0
  //toolbox_action changeslider ;
  slider_action_value_changed_block savchanged;
  savchanged.size = 20; //sizeof
  //savchanged.ref_no = 30000; //fait par la toolbox
  savchanged.action_no = 0x82886;
  savchanged.flags     = 0x0;
  #endif 
  adjuster_action_clicked_block *aacblk;
  aacblk = (adjuster_action_clicked_block *) Event ;
  
  //debug_printf( "Sliders_handler: block  %d, actn: %0x, up= %d", aacblk->size, aacblk->action_no,  aacblk->up);
  switch(TBblock->this_cmp)
  {
   case ADJUSTER_ARROW_1_R:
     // manque get limite min et max,  ajouter
     MWin->slider1_cur = slider_get_value(0, TBblock->this_obj, SLIDER_1);
     if(aacblk->up) n= ++MWin->slider1_cur; else  n= --MWin->slider1_cur;
     n= (n < 0 )?  0:n;
     n= (n >255)? 255:n;
     slider_set_value(0, TBblock->this_obj, SLIDER_1, n);
     //debug_printf( "Sliders_handler: slider 1 value: %d",n);
 //    MWin->TextArea_display("\n");
 //    MWin->TextArea_display(itoa2(n, buf));  
     displayfield_set_value(0,TBblock->this_obj, DISPLAY_FIELD_3, itoa2(n, buf));
   #if 0
     savchanged.value = 1;
     toolbox_raise_toolbox_event(0, TBblock->this_obj, SLIDER_1, (toolbox_action*)&savchanged);
   #endif          
     break;
    
   case ADJUSTER_ARROW_1_L:
     MWin->slider1_cur = slider_get_value(0, TBblock->this_obj, SLIDER_1);
     if(aacblk->up) n= ++MWin->slider1_cur; else  n= --MWin->slider1_cur;  
     slider_set_value(0, TBblock->this_obj, SLIDER_1, n);
     n= (n < 0 )?  0:n;
     n= (n >255)? 255:n;
     displayfield_set_value(0,TBblock->this_obj, DISPLAY_FIELD_3, itoa2(n, buf));
     break;
  }

  return true;
}

         /*-------------------------------------------------------------*/
         /* ---------           Message Handlers           -------------*/
         /*-------------------------------------------------------------*/
#if 0
osbool MainWindow::DataLoad_Handler(wimp_message *pxMessage, void *handle)
{
  x_LOCAL_ERROR( Error_ChargeFichier, 2, "Fichier non Charg" );
  MainWindow* MWin ;
  MWin = ( MainWindow *) handle;
  //debug_printf("DataLoad_Handler this: %p", MWin);

  wimp_message msg;
  x_exception xx;
  int TypeFic ;
  TypeFic = pxMessage->data.data_xfer.file_type; 
  msg = *pxMessage;
  //debug_printf("DataLoad_Handler file_name: %s", pxMessage->data.data_xfer.file_name);

#if 1
  if( pxMessage -> your_ref == 0 ){
    x_TRY( &xx )
    {
       // re init xml


      if (pxMessage->action == message_DATA_LOAD){
         msg.action = message_DATA_LOAD_ACK;
      }
      wimp_send_message(wimp_USER_MESSAGE, &msg, pxMessage->sender);

      // traitement du fichier
      //debug_printf(" MainWindow::DataLoad_Handler, dcodage du fichier");

    }
    x_CATCH( &xx )
    {
      Err_ReportError( !Err_FATAL, "Impossible de traiter %s: %s", \
      strrchr( pxMessage -> data.data_xfer.file_name,'.') + 1, \
       xx.error -> errmess );
    }

  }

#endif


//  gFileLoaded = true;
  return true;
}

#endif





