/************************************************************
**
** Application: UnitConv
**
** Title:       c.iconbar
**
*************************************************************/

/*
*
* Copyright (c) 2015, Chris Johnson
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*   * Redistributions of source code must retain the above copyright
*     notice, this list of conditions and the following disclaimer.
*   * Redistributions in binary form must reproduce the above
*     copyright notice, this list of conditions and the following
*     disclaimer in the documentation and/or other materials provided
*     with the distribution.
*   * Neither the name of the copyright holder nor the names of their
*     contributors may be used to endorse or promote products derived
*     from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/

/* Include files */
/* from oslib */
#include "OSLib:os.h"
#include "OSLib:osbyte.h"
#include "OSLib:iconbar.h"
#include "OSLib:proginfo.h"
#include "OSLib:toolbox.h"


/* from oslib support */
#include "OSLibSupport:x.h"
#include "OSLibSupport:event.h"

/* from CJLib */
#include "CJLib:etc.h"
#include "CJLib:kbd.h"
#include "CJLib:os.h"
#include "CJLib:file.h"

/* from application */
#include "app.h"
#include "main.h"
#include "revision.h"
#include "cjoslib.h"
#include "editunits.h"
#include "gases.h"
#include "imagesize.h"
#include "imagecrop.h"
#include "binhex.h"
#include "unitconv.h"
#include "choices.h"
#include "iconbar.h"

/************************************************************/

        // icon bar
#define ICONBAR_TEMPLATE 	((toolbox_id)"Iconbar")

        // icon bar menu
#define ICONBARMENU_TEMPLATE	((toolbox_id)"IbarMenu"
#define uevent_ICONBARMENU_QUIT           0x01
#define uevent_ICONBARMENU_HELP           0x03
#define uevent_ICONBARMENU_ABOUT_TO_OPEN  0x04
#define uevent_ICONBARMENU_ABOUT_TO_CLOSE 0x05
#define uevent_ICONBARMENU_CHOICES        0x06
#define uevent_ICONBARMENU_OPEN_UNITS     0x07
#define uevent_ICONBARMENU_OPEN_BINHEX    0x08
#define uevent_ICONBARMENU_OPEN_IMAGES    0x09
#define uevent_ICONBARMENU_OPEN_GASES     0x0A
#define uevent_ICONBARMENU_OPEN_EDITUNITS 0x0B

/* Local defines */

#define HTML_HELP_FILE     "<UnitConvRes$Dir>.Documents.index/html"
#define TEXT_HELP_FILE     "<UnitConvRes$Dir>.Documents.index/txt"


/************************************************************/
/* global variables */
static toolbox_o Ibaricon_id;


/************************************************************/
/* forward function declarations */
static osbool IconbarMenuHandler ( bits event_code,
                                   toolbox_action *action,
                                   toolbox_block *id_block,
                                   void *handle );


/************************************************************/

/*  ProgInfo box about to be shown -- set the version and date */

static osbool ProgInfoShow( bits event_code,
                                toolbox_action *action,
                                toolbox_block *id_block,
                                void *handle )
{
  char temp[64];

  // set the 'version' control
#ifdef RELEASE
  sprintf( temp, "%s (%s)", APPVERSION, APP_VERSION_DATE );
#else
  sprintf( temp, "Dev %s (%s)", REVISION, __DATE__ );
#endif
  proginfo_set_version( 0, id_block -> this_obj, temp );
  return (TRUE);

  IGNORE (handle);
  IGNORE (action);
  IGNORE (event_code);
}


/* Launch the help file
** Check if browser has been seen
** Check if html help file exists
** If not check for text fhelp file
** If cannot run either, then issue a message
*/


/************************************************************/

void IconbarShowHelp (void)
{
  char cmd[256];
  char * file;

  file = NULL;
  /* check that a browser has been seen by the filer */
  /* if so we can try to run the html help file */
  /* check that it exists */
  if ( CJL_sysvar_exists ( "alias$@RunType_FAF" )
                           && CJLfile_file_exists ( HTML_HELP_FILE ) )
  {
    file = HTML_HELP_FILE;
  }
  else
  {
    /* we can try to run the text help file */
    /* check that it exists */
    if ( CJLfile_file_exists ( TEXT_HELP_FILE ) )
    {
      file = TEXT_HELP_FILE;
    }
  }
  if (file)
  {
    sprintf(cmd,"Filer_Run %s",file);
    os_cli ( cmd ) ;
  }
  else
  {
    /* need error message */
  }
  return;
}






/************************************************************/

/* Input menu event handlers */

static osbool IconbarMenuClosing ( toolbox_o obj_id )
{

  /* deregister event handlers for the icon bar menu options */
  event_deregister_toolbox_handler( obj_id,
                               	    action_ANY,
                                    IconbarMenuHandler,
                                    NULL );

  /* deregister handler for the proginfo box */
  event_deregister_toolbox_handler( event_ANY,
                                    action_PROG_INFO_ABOUT_TO_BE_SHOWN,
                                    ProgInfoShow,
                                    NULL );

  return (TRUE);

}




/************************************************************/

static osbool IconbarMenuHandler ( bits event_code,
                                   toolbox_action *action,
                                   toolbox_block *id_block,
                                   void *handle )
{

  switch (event_code)
  {
    case uevent_ICONBARMENU_HELP:
      IconbarShowHelp ();
      break;


    case uevent_ICONBARMENU_OPEN_UNITS:
      Unitconv_Open ();
      break;

    case uevent_ICONBARMENU_OPEN_BINHEX:
      Binhex_Open ();
      break;

    case uevent_ICONBARMENU_OPEN_IMAGES:
      Imagesize_Open ();
      break;

    case uevent_ICONBARMENU_OPEN_GASES:
      Gases_Open ();
      break;

    case uevent_ICONBARMENU_OPEN_EDITUNITS:
      Editunits_Open ();
      break;

    case uevent_ICONBARMENU_CHOICES:
      Choices_OpenDialogue();
      break;

    case uevent_ICONBARMENU_ABOUT_TO_CLOSE:
      IconbarMenuClosing ( id_block -> this_obj );
      break;

    case uevent_ICONBARMENU_QUIT:
      Quit = TRUE;
      break;

  }
  return (TRUE);

  IGNORE(handle);
  IGNORE(action);
  IGNORE(id_block);
}







/************************************************************/

static osbool IconbarMenuOpening (bits event_code,
                                toolbox_action *action,
                                toolbox_block *id_block,
                                void *handle )
{

  /* register event handler for the icon bar menu */
  event_register_toolbox_handler( id_block -> this_obj,
                               	  action_ANY,
                                  IconbarMenuHandler,
                                  NULL );

  /* register a handler to be called when the proginfo box is about to be opened */
  event_register_toolbox_handler( event_ANY,
                                  action_PROG_INFO_ABOUT_TO_BE_SHOWN,
                                  ProgInfoShow,
                                  NULL );

  return (TRUE);

  IGNORE (handle);
  IGNORE (action);
  IGNORE (event_code);
}





/* Handler to deal with SELECT and ADJUST clicks, with or without
   SHIFT or CONTROL, on the iconbar icon */

/************************************************************/

static osbool IconbarClick ( bits event_code,
                             toolbox_action *action,
                             toolbox_block *id_block,
                             void *handle)
{
  int value;
  int open = PREFS_CLICK_NONE ;

  /* Check keyboard state */
  value = osbyte_read ( osbyte_VAR_KEYBOARD_STATE );
  /* We are only interested in state of Shift and Control */
  value &= SHIFT_CTRL_PRESSED;

  switch (action->flags)
  {
    case iconbar_CLICKED_SELECT:
      switch (value)
      {
        case SHIFT_PRESSED:
          open = app.ch.shift_select_click;
          break;

        case CTRL_PRESSED:
          open = app.ch.ctrl_select_click;
          break;

        case SHIFT_CTRL_PRESSED:
          /* always do nothing */
          break;

        case 0:
          open = app.ch.select_click;
          break;
      }
      break;

    case iconbar_CLICKED_ADJUST:
      switch (value)
      {
        case SHIFT_PRESSED:
          open = app.ch.shift_adjust_click;
          break;

        case CTRL_PRESSED:
          open = app.ch.ctrl_adjust_click;
          break;

#ifdef RELEASE
        case SHIFT_CTRL_PRESSED:
          /* always do nothing */
          break;
#else
        case SHIFT_CTRL_PRESSED:
          /* Undocumented feature */
          open = PREFS_CLICK_NONE;
          Imagecrop_Open ();
          break;
#endif

        case 0:
          open = app.ch.adjust_click;
          break;
      }
      break;
  }

  switch ( open )
  {
    case PREFS_CLICK_UNITS:
      Unitconv_Open ();
      break;

    case PREFS_CLICK_BINHEX:
      Binhex_Open ();
      break;

    case PREFS_CLICK_IMAGESIZE:
      Imagesize_Open ();
      break;

    case PREFS_CLICK_GASES:
      Gases_Open ();
      break;

    case PREFS_CLICK_CHOICES:
      Choices_OpenDialogue();
      break;

    default:
      break;
  }

  return( TRUE );

  IGNORE (event_code);
  IGNORE (handle);
  IGNORE (id_block);
}







/************************************************************/

void  IconbarInit( void )
{

  /* the following create/show is not actually necessary if we set the
  ** autocreate & autoshow flags in the object template.
  ** However, you then do not easily get the icon handle back from the toolbox.
  */
  Ibaricon_id = toolbox_create_object( 0, ICONBAR_TEMPLATE );
  toolbox_show_object( 0, Ibaricon_id, 0, 0, 0, 0 );

  /* register the 'permanent' events - these will never be deregistered */

  /* Register handlers for open/close iconbar menu */
  event_register_toolbox_handler (event_ANY,
                                  uevent_ICONBARMENU_ABOUT_TO_OPEN,
                                  IconbarMenuOpening,
                                  NULL);
  /* register an event handler to be called when the icon is clicked */
  event_register_toolbox_handler ( Ibaricon_id,
                                   action_ICONBAR_CLICKED,
                                   IconbarClick,
                                   NULL);


  return ;
  }



/************************************************************/





/*************  End of c.iconbar  ***********************/

