/*
 * $Log: main,v $
 * Revision 1.2  1998/05/27 22:36:03  joseph
 * Changed to use wimpclib
 *
 * Revision 1.1.1.1  1997/12/29 14:37:55  jogu
 * WebGet Initial CMS Ver
 *
 */

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

#include "kernel.h"
#include "swis.h"
#include "wimp.h"
#include "wimplib.h"

#include "defines.h"
#include "macros.h"

#include "wimpclib.h"

#define WimpVersion 310

static int wimp_messages [] = {
       /* Wimp_MQuit is implicit */
       0};

const char frontend_taskname[] = "WebGet";

static int pollmask = (
       	   	      Wimp_Poll_NullMask |
//                      Wimp_Poll_RedrawWindowRequestMask |
                      Wimp_Poll_PointerLeavingWindowMask |
                      Wimp_Poll_PointerEnteringWindowMask |
//                      Wimp_Poll_MouseClickMask |
//                      Wimp_Poll_KeyPressedMask |
                      Wimp_Poll_LoseCaretMask |
                      Wimp_Poll_GainCaretMask |
                      Wimp_Poll_PollWordNonZeroMask |
//                      Wimp_Poll_UserMessageMask |
//                      Wimp_Poll_UserMessageRecordedMask |
                      Wimp_Poll_UserMessageAcknowledgeMask |
                      Wimp_Poll_PollWord |
                      Wimp_Poll_PollWordHighPriority |
                      Wimp_Poll_SaveFPRegs |
                      0);

static int quit = 0;
static int window_tofetch = 0;
static int window_tofetchwidth = 0;

#define ITEM_HEIGHT 32
#define ITEM_BORDER 4


void load_templates(/* const (acorn. pah.) */ char *fname)
{
  _kernel_oserror *e = wimp_open_template(fname);
  E_CHECK(e);
  if (e) exit(1);


  window_tofetch = wimpc_loadtemplate( "ToFetch" );
  if ( window_tofetch == -1 ) goto abort;

  return;

  abort:
//  E_CHECK(e);
  E_CHECK(wimp_close_template());
  exit(1);
}

char text[4][]={"Testyxxxxxxxxxxxxxxxxxxxxxxxxx.","Wheeeeeeeeeeeeeeeeeeeeeeeeee!","Wheeeeee!","plopoooooooooooooooooooooooof"};
int selected[4]={1,0,1,0};

#define OS_PLOT(c,x,y) { E_CHECK(_swix(OS_Plot, _INR(0,2), c, x, y)); }
#define OS_MOVE 188
#define OS_BLOCK 96 + 1
/* 1-607 */

void draw_item(int no, int xmin, int ymax)
{
  if (selected[no])
  {
    E_CHECK(_swix(ColourTrans_SetGCOL, _IN(0) | _INR(3,4), 0, 0, 0));
    OS_PLOT(OS_MOVE, xmin, ymax - (ITEM_HEIGHT * (no+1)) - ITEM_BORDER); /* Move to bottom left of area */
    OS_PLOT(OS_BLOCK, window_tofetchwidth, 32); /* Plot up & right */
    E_CHECK(_swix(Wimp_TextOp, _INR(0,2), 0, 0xffffff00, 0));
  }
  else
  {
    E_CHECK(_swix(Wimp_TextOp, _INR(0,2), 0, 0x0, 0xffffff00));
  }

  E_CHECK(_swix(Wimp_TextOp, _INR(0,5), 2 | (1<<30), text[no], -1, -1, xmin+ITEM_BORDER, ymax-(ITEM_HEIGHT * (no+1))));
}

int find_itemno(int y)
{
  /* y is a screen coordinate */
  WimpGetWindowStateBlock state;


  state.window_handle = window_tofetch;
  wimp_get_window_state(&state);

  y = y - state.visible_area.ymax - state.yscroll;
  y+=4; /* when we get ITEM_HEIGHT, that's still the 1st item */
  y = -y/ITEM_HEIGHT;

  if (y<0) y=0;
  if (y>3) y=3;

  return y;
}


void redraw_handler(WimpPollBlock *pollblock)
{
  WimpRedrawWindowBlock redraw;
//  WimpGetWindowStateBlock state;
  int more, xmin, ymax;


  redraw.window_handle = pollblock->redraw_window_request.window_handle;

  wimp_redraw_window(&redraw, &more);
  xmin = redraw.visible_area.xmin - redraw.xscroll;
  ymax = redraw.visible_area.ymax - redraw.yscroll;

  while (more)
  {
    int min,max,x;
    /* NB. TextOp does not work < 3.5 */
    min=find_itemno(redraw.redraw_area.ymax);
    max=find_itemno(redraw.redraw_area.ymin);
    for(x=min; x<=max; x++)
      draw_item(x,xmin,ymax);

    wimp_get_rectangle(&redraw, & more);
  }
}

#define wimp_textwidth(t) (_swi(Wimp_TextOp, _INR(0,2) | _RETURN(0), 1, t, 0))

int main(int argc, char *argv[])
{
  int reason;
  WimpPollBlock pollblock;

  E_CHECKABORT(wimp_initialise(WimpVersion,TASK_NAME,wimp_messages,NULL /* wimp version*/ ,NULL /* task handle */));

  load_templates(TASK_PATH"templates");

  /* calculate window width */
  {
    int width = 0;

    width = wimp_textwidth(text[0]);
    width = MAX(width, wimp_textwidth(text[1]));
    width = MAX(width, wimp_textwidth(text[2]));
    width = MAX(width, wimp_textwidth(text[3]));
    window_tofetchwidth = width + ITEM_BORDER * 3;
    wimpc_setextent(window_tofetch, window_tofetchwidth, 4*ITEM_HEIGHT+ITEM_BORDER*2);
  }

  wimpc_openwindow(window_tofetch);
/*  wimp_set_caret_position(MainWindow, 6, 0, 0, -1, 2);*/

  while (!quit)
  {
    E_CHECK(wimp_poll (pollmask,&pollblock, NULL, &reason));

    switch (reason) {

      case Wimp_EOpenWindow:
        E_CHECK(wimp_open_window((WimpOpenWindowBlock *)&pollblock));
        break;

      case Wimp_ERedrawWindow:
        redraw_handler(&pollblock);
        break;

      case Wimp_ECloseWindow:
        if ( pollblock.close_window_request.window_handle == window_tofetch )
          quit=1;
        break;

      case Wimp_EMouseClick:
        if (pollblock.mouse_click.window_handle == window_tofetch) {
          int item = find_itemno(pollblock.mouse_click.mouse_y);
          if (item>=0 && item < 4)
          {
            WimpGetWindowInfoBlock block;
            block.window_handle=window_tofetch;
            selected[item] = !selected[item];
            wimp_get_window_info(&block);
            wimp_force_redraw(window_tofetch, 0,
                                              -(item+1) * ITEM_HEIGHT - ITEM_BORDER,
                                              window_tofetchwidth,
                                              -(item) * ITEM_HEIGHT - ITEM_BORDER+4);

//            force_redraw(item);
          }
        }
/*          switch (pollblock.mouse_click.icon_handle) {
          }*/
        break;

      case Wimp_EKeyPressed:
        switch (pollblock.key_pressed.key_code)
        {
          default:
            wimp_process_key(pollblock.key_pressed.key_code);
        }
        break;

      case Wimp_EUserMessage:
      case Wimp_EUserMessageRecorded:
        switch(pollblock.user_message.hdr.action_code) {
          case Wimp_MQuit:
            quit = 1;
            break;
        }
        break;

    }
  }

  return 0;
}
