/*
 * osd.c: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id$
 */

#include <vdr/remote.h>
#include "osd.h"

cHideSubtitles::cHideSubtitles(void) {
  osd = NULL;
  mode = move;
}

cHideSubtitles::~cHideSubtitles() {
  hsc.Store(); // store setup when OSD is closed
  delete osd;
}

void cHideSubtitles::Show(void) {
  osd = cOsdProvider::NewOsd((SCREEN_WIDTH-OSD_WIDTH)/2-1, OSD_Y, 5);
  if (osd) {
    tArea Area = { 0, 0, OSD_WIDTH-1, OSD_HEIGHT-1,  2 };
    osd->SetAreas(&Area, 1);
    DrawBars();
  }
}

void cHideSubtitles::DrawBars(void) {
  osd->DrawRectangle(0, 0, OSD_WIDTH-1, hsc.y+OSD_HEIGHT-1, clrTransparent);
  osd->DrawRectangle(hsc.x, hsc.y, hsc.x+hsc.width-1, hsc.y+hsc.height-1, clrBlack);
  osd->DrawRectangle(hsc.x, hsc.y+hsc.height+hsc.space-1, hsc.x+hsc.width-1, hsc.y+2*hsc.height-2+hsc.space, clrBlack);
  osd->Flush();
}

eOSState cHideSubtitles::ProcessKey(eKeys Key) {
  eOSState state = cOsdObject::ProcessKey(Key);
  if (state == osUnknown) {
    switch (Key & ~k_Repeat) {
      case kUp:     if(mode == move) {
                      if (hsc.y > 0) hsc.y--;
                    }
                    else {
                      if (hsc.height > 1) hsc.height--;
                    }
                    break;
      case kDown:   if(mode == move) {
      	              if (hsc.y < OSD_HEIGHT-(2*hsc.height-2+hsc.space)) hsc.y++;
      	            }
      	            else {
                      if (OSD_HEIGHT-(2*hsc.height-2+hsc.space+hsc.y) > 2) hsc.height++;
      	            }
      	            break;
      case kLeft:   if (mode == move) {
                      if (hsc.x > 0) hsc.x--;
                    }
                    else {
                      if ( hsc.width > 1 ) hsc.width--;                      
                    }
                    break;
      case kRight:  if ( mode == move ) {
                      if (hsc.x < OSD_WIDTH-hsc.width-1) hsc.x++;
                    }
                    else {
                      if (OSD_WIDTH-(hsc.width-1+hsc.x) > 1) hsc.width++;
                    }
                    break;
      case kRed:    mode = resize; break;
      case kGreen:  if (hsc.space > 1) hsc.space--; break;
      case kYellow: if (OSD_HEIGHT-(2*hsc.height-2+hsc.space+hsc.y) > 1) hsc.space++; break;
      case kBlue:   mode = move; break;
      case kOk: 
      case kBack:   return osEnd;
      default:      return state;
    }
    DrawBars();
  }
  return state;
}
