/*****************   CJLib   ********************************
**
** Title  :     c.tbox
**
*****************************************************************/



// from osLib
#include "oslib/gadget.h"
#include "oslib/toolbox.h"
#include "oslib/wimp.h"
#include "oslib/window.h"

// private headers
#include "tbox.h"




void CJL_GadgetFade (toolbox_o window, toolbox_c gadget)
{
  gadget_flags flags;

  flags = gadget_get_flags (0, window, gadget);
  flags = flags | gadget_FADED;
  gadget_set_flags (0, window, gadget, flags);

  return;

}


void CJL_GadgetUnfade (toolbox_o window, toolbox_c gadget)
{
  gadget_flags flags;

  flags = gadget_get_flags (0, window, gadget);
  flags = flags & ~gadget_FADED;
  gadget_set_flags (0, window, gadget, flags);

  return;

}


void CJL_GadgetSetFade (toolbox_o window, toolbox_c gadget, osbool state)
{
  gadget_flags flags;

  flags = gadget_get_flags ( 0, window, gadget);
  if (state)
    flags = flags | gadget_FADED;
  else
    flags = flags & ~gadget_FADED;
  gadget_set_flags ( 0, window, gadget, flags);
  return;
}


void CJL_GetTopLeftWinPos ( wimp_w win, toolbox_position *pos)
{
  wimp_window_state state;

  state.w = win;
  wimp_get_window_state ( &state );
  pos->top_left.x = state.visible.x0;
  pos->top_left.y = state.visible.y1;
  return;
}




void CJL_TBoxGetTopLeftWinPos ( toolbox_o winid, toolbox_position *pos)
{
  wimp_w win;

  win = window_get_wimp_handle (0, winid);
  CJL_GetTopLeftWinPos ( win, pos);
  return;
}





void CJL_GetFullWinPos ( wimp_w win, toolbox_position *pos)
{
  wimp_window_state state;

  state.w = win;
  wimp_get_window_state ( &state );
  pos->full.visible.x0 = state.visible.x0;
  pos->full.visible.y0 = state.visible.y0;
  pos->full.visible.x1 = state.visible.x1;
  pos->full.visible.y1 = state.visible.y1;
  pos->full.xscroll = 0;
  pos->full.yscroll = 0;
  pos->full.next = wimp_TOP;
  return;
}



void CJL_HideObject ( toolbox_o id )
{
  if (id)
  {
    toolbox_hide_object (0, id);
  }
  return;
}




void CJL_DeleteObject ( toolbox_o *id )
{
  if (*id)
  {
    toolbox_delete_object (0, *id);
    *id = toolbox_NULL_OBJECT;
  }
  return;
}



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

/*
*
* Use this function when creating or opening a window.
* If the window exists and is open, then just show it at the default
* position.
* If the window is being created for the first time, then the x,y top left
* will be zero - if so, open centred on the screen.
*
* If the window has been previously created and closed, then open it
* at the position saved when it was closed and deleted.
*
*/

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

void CJtbox_OpenWindowPosition (osbool is_open, toolbox_o win_id, toolbox_position * pos)

{
  if (is_open)
  {
    toolbox_show_object ( 0,
                          win_id,
                          toolbox_POSITION_DEFAULT,
                          0,
                          toolbox_NULL_OBJECT,
                          toolbox_NULL_COMPONENT ) ;
  }
  else
  {
    if (pos->top_left.x == 0)
    {
      toolbox_show_object ( 0,
                            win_id,
                            toolbox_POSITION_CENTRED,
                            0,
                            toolbox_NULL_OBJECT,
                            toolbox_NULL_COMPONENT ) ;
    }
    else
    {
      toolbox_show_object ( 0,
                            win_id,
                            toolbox_POSITION_TOP_LEFT,
                            pos,
                            toolbox_NULL_OBJECT,
                            toolbox_NULL_COMPONENT ) ;
    }
  }
  return;
}




