#include "avolctl.h"

extern int 		perc;
extern alsa		*alsamixer;
extern mixerlist	*mixer;

editoffset::editoffset(const char *Name,int *Value,bool controlable):cMenuEditItem(Name)
{
 _controlable=controlable;
 value=Value;
 strcpy(name,Name);
 Set();
}

void editoffset::Set(void)
{
 char buf[16];
 long vol;
 
 if(_controlable)vol=(long)*value;
 else vol=(long)*value-100+perc;
 
 snprintf(buf,sizeof(buf),"%d",*value);
 SetValue(buf);
 alsamixer->setVolume(name,vol);
}

eOSState editoffset::ProcessKey(eKeys Key)
{
 eOSState state=cMenuEditItem::ProcessKey(Key);
 
 if(state==osUnknown)
 {
  int newValue=*value;
  Key=NORMALKEY(Key);
  switch(Key)
  {
   case kNone:break;
   case kLeft:
    newValue=*value-1;
    break;
   case kRight:
    newValue=*value+1;
    break;
   default:
    if(*value<0){*value=0;Set();}
    if(*value>100){*value=100;Set();}
    return state;
  }
  if(newValue>=0&&newValue<=100)
  {
   *value=newValue;
   Set();
  }
  state=osContinue;
 }
 return state;
}

editselect::editselect(const char *Name,int *Value,bool Switch):cMenuEditItem(Name)
{
 value=Value;
 strcpy(name,Name);
 isSwitch=Switch;
 Set();
}

void editselect::Set()
{
 char buf[16];
 
 switch(*value)
 {
  case 0:
   snprintf(buf,sizeof(buf),tr("no"));
   SetValue(buf);
   break;
  case 1:
   snprintf(buf,sizeof(buf),"VDR");
   SetValue(buf);
   break;
  case 2:
   snprintf(buf,sizeof(buf),tr("controlable"));
   SetValue(buf);
   break;
 }
}

eOSState editselect::ProcessKey(eKeys Key)
{
 eOSState state=cMenuEditItem::ProcessKey(Key);
 
 if(state==osUnknown)
 {
  int newValue=*value;
  Key=NORMALKEY(Key);
  switch(Key)
  {
   case kNone:break;
   case kLeft:
    if(*value-1>=0)newValue=*value-1;
    if(isSwitch&&newValue==1)newValue=0;
    break;
   case kRight:
    if(*value+1<=2)newValue=*value+1;
    if(isSwitch&&newValue==1)newValue=2;
    break;
   default:
    if(*value<0){*value=0;Set();}
    if(*value>2){*value=2;Set();}
    return state;
  }
  if(newValue>=0&&newValue<=2)
  {
   *value=newValue;
   Set();
  }
  state=osContinue;
 }
 return state;
}

editswitch::editswitch(const char *Name,int *Value):cMenuEditItem(Name)
{
 value=Value;
 strcpy(name,Name);
 Set();
}

void editswitch::Set()
{
 char buf[16];
 snprintf(buf,sizeof(buf),"%s",*value?tr("on"):tr("off"));
 SetValue(buf);
 alsamixer->setSwitch(name,*value);
}

eOSState editswitch::ProcessKey(eKeys Key)
{
 eOSState state=cMenuEditItem::ProcessKey(Key);
 
 if(state==osUnknown)
 { 
  int newValue=*value;
  Key=NORMALKEY(Key);
  switch(Key)
  {
   case kNone:break;
   case kLeft:
    if(*value-1>=0)newValue=*value-1;
    break;
   case kRight:
    if(*value+1<=1)newValue=*value+1;
    break;
   default:
    if(*value<0){*value=0;Set();}
    if(*value>1){*value=1;Set();}
    return state;
  }
  if(newValue>=0&&newValue<=1)
  {
   *value=newValue;
   Set();
  }
  state=osContinue;
 }
 return state;
}
