# include "Ximage.h"

int InterpretKeys (Graphic *graphic, Layout *layout, XEvent *event) {

  double X, Y, offset;
  int    modstate;
  char   string[10];
  KeySym keysym;
  XComposeStatus  composestatus;
  XKeyEvent *keyevent;

  keyevent = (XKeyEvent *) event;
  XLookupString (keyevent, string, 9, &keysym, &composestatus);
  modstate = keyevent[0].state;

  // offset is in image pixels: 
  // 0.5 image pixels is 1 screen pixel for expand == +2
  // 2.0 image pixels is 1 screen pixel for expand == -2
  if (layout[0].expand == 0) layout[0].expand = 1;
  offset = (layout[0].expand > 0) ? 1.0 / layout[0].expand : -layout[0].expand;
  if (modstate & ControlMask) offset *= 100;

  switch (keysym) {

    case XK_KP_Home:
    case XK_Home:
      layout[0].expand = 1;
      Reorient (graphic, layout, layout[0].X, layout[0].Y, 0);
      break;
    case XK_KP_End:
    case XK_End:
      layout[0].expand = 1;
      Reorient (graphic, layout, 0, 0, 0);
      break;
    case XK_KP_Enter:
    case XK_KP_Begin:
    case XK_Return:
      Screen_to_Image (&X, &Y, (double)keyevent[0].x, (double)keyevent[0].y, layout);
      X = 0.5*layout[0].matrix.Naxis[0] - X;
      Y = 0.5*layout[0].matrix.Naxis[1] - Y;
      Reorient (graphic, layout, X, Y, 0);
      break;
    case XK_Prior:
    case XK_KP_Prior:
      Reorient (graphic, layout, layout[0].X, layout[0].Y, +1);
      break;
    case XK_Next:
    case XK_KP_Next:
      Reorient (graphic, layout, layout[0].X, layout[0].Y, -1);
      break;
    case XK_Up:
    case XK_KP_Up:
      Reorient (graphic, layout, layout[0].X, layout[0].Y + offset, 0);
      break;
    case XK_Down:
    case XK_KP_Down:
      Reorient (graphic, layout, layout[0].X, layout[0].Y - offset, 0);
      break;
    case XK_Left:
    case XK_KP_Left:
      Reorient (graphic, layout, layout[0].X + offset, layout[0].Y, 0);
      break;
    case XK_Right:
    case XK_KP_Right:
      Reorient (graphic, layout, layout[0].X - offset, layout[0].Y, 0);
      break;

    case XK_Tab:
      MOVE_POINTER = MOVE_POINTER ^ TRUE;
      if (MOVE_POINTER) UpdatePointer (graphic, layout, (XMotionEvent *)event);
      break;
  }

  return (TRUE);
}

/* other keys we might want to use:

  

*/

