/*
    ####             #    #     # #
    #   #            #    #       #          The FreeWare C library for 
    #   #  ##   ###  #  # #     # ###             RISC OS machines
    #   # #  # #     # #  #     # #  #   ___________________________________
    #   # ####  ###  ##   #     # #  #                                      
    #   # #        # # #  #     # #  #    Please refer to the accompanying
    ####   ### ####  #  # ##### # ###    documentation for conditions of use
    ________________________________________________________________________

    File:    Window.Show.c
    Author:  Copyright  1992 Jason Williams
    Version: 1.00 (19 Mar 1992)
    Purpose: High-level window management functions: Show a window
*/


#include "Desk.WimpSWIs.h"
#include "Desk.Window.h"
#include "Desk.Screen.h"

static Desk_wimp_point lastopenpos = {-1, -1};


extern void Desk_Window_Show(Desk_window_handle window, Desk_window_openpos openpos)
{
  Desk_window_state  wstate;
  Desk_wimp_point    moveto = {0, 0};
  int           w, h;

  Desk_Screen_CacheModeInfo();            /* Ensure got correct screen mode info. */

  Desk_Wimp_GetWindowState(window, &wstate);
  wstate.openblock.behind = -1;                             /* open in front */

  w = wstate.openblock.screenrect.max.x - wstate.openblock.screenrect.min.x;
  h = wstate.openblock.screenrect.max.y - wstate.openblock.screenrect.min.y;
                                        
  switch(openpos)
  {
    case Desk_open_CENTERED:
      moveto.x = (Desk_screen_size.x - w) / 2;
      moveto.y = (Desk_screen_size.y + h) / 2;
      break;
      
    case Desk_open_CENTEREDUNDERPOINTER:
      {
        Desk_mouse_block ptr;

        Desk_Wimp_GetPointerInfo(&ptr);
        moveto.x = ptr.pos.x - (w / 2);
        moveto.y = ptr.pos.y + (h / 2);
      }
      break;

    case Desk_open_OVERCARET:
      {
        Desk_caret_block  caret;
        Desk_window_state wstate;

        Desk_Wimp_GetCaretPosition(&caret);

        if (caret.window > 0)
        {
          Desk_Wimp_GetWindowState(caret.window, &wstate);

          moveto.x = wstate.openblock.screenrect.min.x +
                     (caret.offset.x - wstate.openblock.scroll.x) - 64;
          moveto.y = wstate.openblock.screenrect.max.y -
                     (caret.offset.y - wstate.openblock.scroll.y) + 64;
        }
        else
        {
          /* No caret, so just open centered on screen */
          moveto.x = (Desk_screen_size.x - w) / 2;
          moveto.y = (Desk_screen_size.y + h) / 2;
        }
      }
      break;

    case Desk_open_UNDERPOINTER:
      {
        Desk_mouse_block ptr;

        Desk_Wimp_GetPointerInfo(&ptr);
        moveto.x = ptr.pos.x - 64;
        moveto.y = ptr.pos.y + 64;
      }
      break;

    case Desk_open_NEARLAST:
      if (lastopenpos.x >= 0)
      {
        moveto.x = lastopenpos.x + 16;
        moveto.y = lastopenpos.y - 16;
      }
      else
      {
        moveto.x = (Desk_screen_size.x - w) / 2;
        moveto.y = (Desk_screen_size.y + h) / 2;
      }
      
      if (moveto.x > ((Desk_screen_size.x / 2) + 128))
        moveto.x = (Desk_screen_size.x / 2) - 128;

      if (moveto.y < ((Desk_screen_size.y / 2) - 128))
        moveto.y = (Desk_screen_size.y / 2) + 128;
      break;

    default:
      /* Open wherever it is defined in the template file. */
      moveto.x = wstate.openblock.screenrect.min.x;
      moveto.y = wstate.openblock.screenrect.max.y;
      break;
  }

  if (moveto.x < 0)  moveto.x = 0;
  if (moveto.y < 64) moveto.y = 64;
  if (moveto.x > Desk_screen_size.x - 96) moveto.x = Desk_screen_size.x - 96;
  if (moveto.y > Desk_screen_size.y - 32) moveto.y = Desk_screen_size.y - 32;

  wstate.openblock.screenrect.min.x = moveto.x;
  wstate.openblock.screenrect.max.y = moveto.y;

  wstate.openblock.screenrect.max.x = wstate.openblock.screenrect.min.x + w;
  wstate.openblock.screenrect.min.y = wstate.openblock.screenrect.max.y - h;

  if (openpos == Desk_open_NEARLAST)
  {
    lastopenpos.x = wstate.openblock.screenrect.min.x;  /* save last open pos*/
    lastopenpos.y = wstate.openblock.screenrect.max.y;
  }

  Desk_Wimp_OpenWindow(&wstate.openblock);
}
