
/* centrewin.c
 *
 * Wimp function library
 *  Joseph Heenan, 1998.
 *
 *
 *
 *
 * $Log: centrewin,v $
 * Revision 1.2  1998/05/10 20:13:10  jogu
 * Added names to file headers
 * Added implicit cast for frontend_taskname to wimp_report_error
 *   (it defines 'name' without 'const')
 *
 * Revision 1.1.1.1  1998/05/10 19:56:03  jogu
 * WimpCLib V1.00 (created from newshound c.wimpc rev 1.2)
 *
 *
 */

#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

#include "swis.h"
#include "wimplib.h"

#include "wimpclib.h"

int wimpc_centrewindow( WimpGetWindowStateBlock *window, int handle, int size )
{
  int eig,x,y,width,height;

  /* First center window on screen */
  E_CHECK_RETURN( -1, _swix(OS_ReadModeVariable, _INR(0,1) | _OUT(2), -1,  4, &eig) ); /* XEigFactor */
  E_CHECK_RETURN( -1, _swix(OS_ReadModeVariable, _INR(0,1) | _OUT(2), -1, 11, &x  ) ); /* XWindLimit */
  x = (x + 1) << eig;

  E_CHECK_RETURN( -1, _swix(OS_ReadModeVariable, _INR(0,1) | _OUT(2), -1,  5, &eig) ); /* YEigFactor */
  E_CHECK_RETURN( -1, _swix(OS_ReadModeVariable, _INR(0,1) | _OUT(2), -1, 12, &y  ) ); /* YWindLimit */
  y = (y + 1) << eig;

  window->window_handle = handle;
  E_CHECK_RETURN( -1, wimp_get_window_state( window ) );

  if ( size )
  {
    /* extend visible area of window to it's full extent */
    WimpGetWindowInfoBlock block;
    block.window_handle=handle;
    E_CHECK_RETURN( -1, wimp_get_window_info( (WimpGetWindowInfoBlock *) (((int)(&block))+1) ) );
    window->visible_area.xmin = block.window_data.extent.xmin;
    window->visible_area.xmax = block.window_data.extent.xmax;
    window->visible_area.ymin = block.window_data.extent.ymin;
    window->visible_area.ymax = block.window_data.extent.ymax;
  }

  width  = window->visible_area.xmax - window->visible_area.xmin;
  height = window->visible_area.ymax - window->visible_area.ymin;

  window->visible_area.xmin = (x/2) - (width/2);
  window->visible_area.ymin = (y/2) - (height/2);
  window->visible_area.xmax = window->visible_area.xmin + width;
  window->visible_area.ymax = window->visible_area.ymin + height;

  window->behind = -1;

  return 0;
}

