dword imgbtn_oninput(void * x, int action, ATEV * atev) {
  ACONTROLP ctl  = (ACONTROLP) x;
  IMGBTNDP  d  = (IMGBTNDP) ctl->d;
  dword msg = 0;
  
  switch (action) {
    case ATEV_MOUSEDN: {
        vibrate(30);
        d->pushed = 1;
        msg = aw_msg(0, 1, 0, 0);
        ctl->ondraw(ctl);
      }
      break;
      
    case ATEV_MOUSEUP: {
        d->pushed = 0;
        
        if (aw_touchoncontrol(ctl, atev->x, atev->y)) {
          msg = aw_msg(d->touchmsg, 1, 0, 0);
        }
        else {
          msg = aw_msg(0, 1, 0, 0);
        }
        
        ctl->ondraw(ctl);
      }
      break;
      
    case ATEV_SELECT: {
        if (atev->d) {
          vibrate(30);
          d->pushed = 1;
          msg = aw_msg(0, 1, 0, 0);
          ctl->ondraw(ctl);
        }
        else {
          d->pushed = 0;
          msg = aw_msg(d->touchmsg, 1, 0, 0);
          ctl->ondraw(ctl);
        }
      }
      break;
  }
  
  return msg;
}
void acime2_sendmsg(ACONTROLP ctl, byte a2, byte a3, byte a4) {
  ACIMEDP  d      = (ACIMEDP) ctl->d;
  dword msg = aw_msg(d->inputMsg, a2, (d->onCTRL ? 1 : 0), a4);
  
  if (d->onCTRL) {
    d->keyD[34] = 0;
    d->onCTRL = 0;
    aw_draw(ctl->win);
  }
  
  atouch_send_message(msg);
}
/*
 * Descriptions:
 * -------------
 * AROMA File Manager : Cut File
 *
 */
byte
auido_next_del_size(AFSDTP dt, int *curr_id, byte * copy_status,
		    char **source_path, int number_files)
{
	int i;
	for (i = *curr_id; i < number_files; i++) {
		if (copy_status[i] == 0) {
			*curr_id = i;
			dt->status = 1;
			dt->flag = 1;
			dt->path = source_path[i];
			dt->msgFinish = aw_msg(11, 0, 0, 0);
			dt->msgTick = aw_msg(10, 0, 0, 0);
			dt->intTick = 25;
			dt->lstTick = 0;
			afs_filedelete(dt);
			return 1;
		}
	}
	return 0;
}
dword accb_oninput(void * x,int action,ATEV * atev){
  ACONTROLP ctl  = (ACONTROLP) x;
  ACCBDP  d  = (ACCBDP) ctl->d;
  dword msg = 0;
  switch (action){
    case ATEV_MOUSEDN:
      {
        vibrate(30);
        d->pushed=1;
        msg=aw_msg(0,1,0,0);
        ctl->ondraw(ctl);
      }
      break;
    case ATEV_MOUSEUP:
      {
        d->pushed=0;
        if (aw_touchoncontrol(ctl,atev->x,atev->y))
          d->checked = !d->checked;
        msg=aw_msg(0,1,0,0);
        ctl->ondraw(ctl);
      }
      break;
    case ATEV_SELECT:
      {
        if (atev->d){
          vibrate(30);
          d->pushed=1;
        }
        else{
          d->pushed=0;
          d->checked = !d->checked;
        }
        msg=aw_msg(0,1,0,0);
        ctl->ondraw(ctl);
      }
      break;
  }
  return msg;
}
void afbox_setcheckall(ACONTROLP ctl, byte checked){
  AFBOXDP d = (AFBOXDP) ctl->d;
  if (d->acheck_signature != 177) return;
  int i;
  for (i=0;i<d->itemn;i++){
    AFBOXIP p = d->items[i];
    if (p->checked!=checked){
      p->checked=checked;
      afbox_redrawitem(ctl,i);
      d->check_n+=(checked)?1:-1;
    }
  }
  ctl->ondraw(ctl);
  aw_redraw(ctl->win);
  ag_sync();
  
  dword msg=aw_msg(d->changemsg,1,0,0);
  atouch_send_message(msg);
}
dword accheck_oninput(void * x,int action,ATEV * atev){
  ACONTROLP ctl= (ACONTROLP) x;
  ACCHECKDP d  = (ACCHECKDP) ctl->d;
  dword msg = 0;
  switch (action){
    case ATEV_MOUSEDN:
      {
        d->prevTouchY  = atev->y;
        akinetic_downhandler(&d->akin,atev->y);
        
        int touchpos = atev->y - ctl->y + d->scrollY;
        int i;
        for (i=0;i<d->itemn;i++){
          if ((touchpos>=d->items[i]->y)&&(touchpos<d->items[i]->y+d->items[i]->h)){
            ac_regpushwait(
              ctl,&d->prevTouchY,&d->invalidDrawItem,i
            );
            break;
          }
        }
      }
      break;
    case ATEV_MOUSEUP:
      {
        if ((d->prevTouchY!=-50)&&(abs(d->prevTouchY-atev->y)<agdp()*5)){
          d->prevTouchY=-50;
          int touchpos = atev->y - ctl->y + d->scrollY;
          
          int i;
          for (i=0;i<d->itemn;i++){
            if ((!d->items[i]->isTitle)&&(touchpos>=d->items[i]->y)&&(touchpos<d->items[i]->y+d->items[i]->h)){
              d->items[i]->checked = (d->items[i]->checked)?0:1;
              if ((d->touchedItem != -1)&&(d->touchedItem!=i)){
                int tmptouch=d->touchedItem;
                d->touchedItem = -1;
                accheck_redrawitem(ctl,tmptouch);
              }
              
              int prevfocus = d->focusedItem;
              d->focusedItem= i;
              d->touchedItem= i;
              if ((prevfocus!=-1)&&(prevfocus!=i)){
                accheck_redrawitem(ctl,prevfocus);
              }
              
              
              accheck_redrawitem(ctl,i);
              ctl->ondraw(ctl);
              aw_draw(ctl->win);
              vibrate(30);              
              break;
            }
          }
          
          if ((d->scrollY<0)||(d->scrollY>d->maxScrollY)){
            ac_regbounce(ctl,&d->scrollY,d->maxScrollY);
          }
        }
        else{
          if (akinetic_uphandler(&d->akin,atev->y)){
            ac_regfling(ctl,&d->akin,&d->scrollY,d->maxScrollY);
          }
          else if ((d->scrollY<0)||(d->scrollY>d->maxScrollY)){
            ac_regbounce(ctl,&d->scrollY,d->maxScrollY);
          }
        }
        if (d->touchedItem != -1){
          usleep(30);
          int tmptouch=d->touchedItem;
          d->touchedItem = -1;
          accheck_redrawitem(ctl,tmptouch);
          ctl->ondraw(ctl);
          msg=aw_msg(0,1,0,0);
        }
      }
      break;
    case ATEV_MOUSEMV:
      {
        byte allowscroll=1;
        if (atev->y!=0){
          if (d->prevTouchY!=-50){
            if (abs(d->prevTouchY-atev->y)>=agdp()*5){
              d->prevTouchY=-50;
              if (d->touchedItem != -1){
                int tmptouch=d->touchedItem;
                d->touchedItem = -1;
                accheck_redrawitem(ctl,tmptouch);
                ctl->ondraw(ctl);
                aw_draw(ctl->win);
              }
            }
            else
              allowscroll=0;
          }
          if (allowscroll){
            int mv = akinetic_movehandler(&d->akin,atev->y);
            if (mv!=0){
              if ((d->scrollY<0)&&(mv<0)){
                float dumpsz = 0.6-(0.6*(((float) abs(d->scrollY))/(ctl->h/4)));
                d->scrollY+=floor(mv*dumpsz);
              }
              else if ((d->scrollY>d->maxScrollY)&&(mv>0)){
                float dumpsz = 0.6-(0.6*(((float) abs(d->scrollY-d->maxScrollY))/(ctl->h/4)));
                d->scrollY+=floor(mv*dumpsz);
              }
              else
                d->scrollY+=mv;
  
              if (d->scrollY<0-(ctl->h/4)) d->scrollY=0-(ctl->h/4);
              if (d->scrollY>d->maxScrollY+(ctl->h/4)) d->scrollY=d->maxScrollY+(ctl->h/4);
              msg=aw_msg(0,1,0,0);
              ctl->ondraw(ctl);
            }
          }
        }
      }
      break;
      case ATEV_SELECT:
      {
        if ((d->focusedItem>-1)&&(d->draweditemn>0)){
          if (atev->d){
            if ((d->touchedItem != -1)&&(d->touchedItem!=d->focusedItem)){
              int tmptouch=d->touchedItem;
              d->touchedItem = -1;
              accheck_redrawitem(ctl,tmptouch);
            }
            vibrate(30);
            d->touchedItem=d->focusedItem;
            accheck_redrawitem(ctl,d->focusedItem);
            ctl->ondraw(ctl);
            msg=aw_msg(0,1,0,0);
          }
          else{
            if ((d->touchedItem != -1)&&(d->touchedItem!=d->focusedItem)){
              int tmptouch=d->touchedItem;
              d->touchedItem = -1;
              accheck_redrawitem(ctl,tmptouch);
            }
            d->items[d->focusedItem]->checked = (d->items[d->focusedItem]->checked)?0:1;
            d->touchedItem=-1;
            accheck_redrawitem(ctl,d->focusedItem);
            ctl->ondraw(ctl);
            msg=aw_msg(0,1,0,0);
          }
        }
      }
      break;
      case ATEV_DOWN:
        {
          if ((d->focusedItem<d->itemn-1)&&(d->draweditemn>0)){
            int prevfocus = d->focusedItem;
            d->focusedItem++;
            while(d->items[d->focusedItem]->isTitle){
              d->focusedItem++;
              if (d->focusedItem>d->itemn-1){
                d->focusedItem = prevfocus;
                return 0;
              }
            }
            accheck_redrawitem(ctl,prevfocus);
            accheck_redrawitem(ctl,d->focusedItem);
            ctl->ondraw(ctl);
            msg=aw_msg(0,1,1,0);
            
            int reqY = d->items[d->focusedItem]->y - round((ctl->h/2) - (d->items[d->focusedItem]->h/2));
            ac_regscrollto(
              ctl,
              &d->scrollY,
              d->maxScrollY,
              reqY,
              &d->focusedItem,
              d->focusedItem
            );
          }
        }
      break;
      case ATEV_UP:
        {
          if ((d->focusedItem>0)&&(d->draweditemn>0)){
            int prevfocus = d->focusedItem;
            d->focusedItem--;
            while(d->items[d->focusedItem]->isTitle){
              d->focusedItem--;
              if (d->focusedItem<0){
                d->focusedItem = prevfocus;
                return 0;
              }
            }
            accheck_redrawitem(ctl,prevfocus);
            accheck_redrawitem(ctl,d->focusedItem);
            ctl->ondraw(ctl);
            msg=aw_msg(0,1,1,0);
            
            int reqY = d->items[d->focusedItem]->y - round((ctl->h/2) - (d->items[d->focusedItem]->h/2));
            ac_regscrollto(
              ctl,
              &d->scrollY,
              d->maxScrollY,
              reqY,
              &d->focusedItem,
              d->focusedItem
            );
          }
        }
      break;
  }
  return msg;
}
dword acime2_oninput(void * x, int action, ATEV * atev) {
  ACONTROLP ctl   = (ACONTROLP) x;
  ACIMEDP  d      = (ACIMEDP) ctl->d;
  dword msg = 0;
  
  switch (action) {
    case ATEV_MOUSEUP: {
        if (d->pushedId < ACIME2_BTNCNT) {
          int keyID = d->pushedId;
          d->keyD[keyID]  = 0;
          d->pushedId     = 255;
          acime2_action(ctl, keyID, 1);
          
          if (((keyID < 27) && (keyID != 19)) || (keyID == 30) || (keyID == 29) || (keyID == 31)) {
            if (d->onShift == 1) {
              d->onShift = 0;
              int i;
              
              for (i = 0; i < ACIME2_BTNCNT; i++) {
                d->keyD[i] = 0;
              }
            }
          }
          
          ctl->ondraw(ctl);
          msg = aw_msg(0, 1, 0, 0);
        }
        else {
          d->pushedId = 255;
        }
      }
      break;
      
    case ATEV_MOUSEDN:
    case ATEV_MOUSEMV: {
        if ((d->pushedId != 200) && (d->pushedId != 254)) {
          int clientX = atev->x - ctl->x;
          int clientY = atev->y - ctl->y;
          
          if (clientY >= 0) {
            int i;
            
            for (i = 1; i < 5; i++) {
              if (clientY < (i * d->btnH)) {
                break;
              }
            }
            
            int r = i;
            byte idpos[5][2] = {
              {0,  9},
              {10, 18},
              {19, 27},
              {28, 32},
              {33, 38}
            };
            
            for (i = idpos[r - 1][0]; i < idpos[r - 1][1]; i++) {
              if (clientX < (d->keyX[i] + d->keyW[i])) {
                break;
              }
            }
            
            if (d->pushedId != i) {
              msg = aw_msg(0, 1, 0, 0);
              
              if (d->pushedId < ACIME2_BTNCNT) {
                d->keyD[d->pushedId]  = 0;
              }
              
              d->pushedId = i;
              acime2_action(ctl, i, 0);
              d->keyD[i]  = 0;
              ctl->ondraw(ctl);
              
              if (action == ATEV_MOUSEDN) {
                vibrate(30);
              }
            }
          }
        }
      }
      break;
  }
  
  return msg;
}
//-- Dispatch Messages
dword aw_dispatch(AWINDOWP win){
  dword msg;
  int i;
  
  ui_clear_key_queue();
  while(1){
    //-- Wait For Event
    ATEV        atev;
    int action  =atouch_wait(&atev);
    
    //-- Reset Message Value
    msg         = aw_msg(0,0,0,0);
    
    //-- Check an Action Value
    switch (action){
      case ATEV_MESSAGE:{
        msg = atev.msg;
      }
      break;
      case ATEV_BACK:{
        if (!atev.d){
          msg = aw_msg(5,0,0,0);
        }
      }
      break;
      case ATEV_DOWN: case ATEV_RIGHT:
        if (!atev.d){
          if (win->focusIndex!=-1){
            ACONTROLP ctl = (ACONTROLP) win->controls[win->focusIndex];
            if (ctl->oninput!=NULL){
              msg = ctl->oninput((void*)ctl,action,&atev);
            }
            if (aw_gl(msg)==0){
              for (i=win->focusIndex+1;i<win->controln;i++){
                ACONTROLP fctl = (ACONTROLP) win->controls[i];
                if (fctl->onfocus!=NULL){
                  if (fctl->onfocus(fctl)){
                    win->focusIndex = i;
                    ctl->onblur(ctl);
                    aw_draw(win);
                    break;
                  }
                }
              }
            }
          }
        }
      break;
      case ATEV_UP: case ATEV_LEFT:
        if (!atev.d){
          if (win->focusIndex!=-1){
            ACONTROLP ctl = (ACONTROLP) win->controls[win->focusIndex];
            if (ctl->oninput!=NULL){
              msg = ctl->oninput((void*)ctl,action,&atev);
            }
            if (aw_gl(msg)==0){
              for (i=win->focusIndex-1;i>=0;i--){
                ACONTROLP fctl = (ACONTROLP) win->controls[i];
                if (fctl->onfocus!=NULL){
                  if (fctl->onfocus(fctl)){
                    win->focusIndex = i;
                    ctl->onblur(ctl);
                    aw_draw(win);
                    break;
                  }
                }
              }
            }
          }
        }
      break;
      case ATEV_MENU:
      case ATEV_SEARCH:
      case ATEV_HOME:
      case ATEV_SELECT:{
        if (win->focusIndex!=-1){
          ACONTROLP ctl = (ACONTROLP) win->controls[win->focusIndex];
          if (ctl->oninput!=NULL){
            msg = ctl->oninput((void*)ctl,action,&atev);
          }
        }
      }
      break;
      case ATEV_MOUSEDN:
      {
        if (win->controln>0){
          int i;
          for (i=win->controln-1;i>=0;i--){
            ACONTROLP ctl = (ACONTROLP) win->controls[i];
            if (aw_touchoncontrol(ctl,atev.x,atev.y)){
              if (ctl->oninput!=NULL){
                msg             = ctl->oninput((void*)ctl,action,&atev);
                win->touchIndex = i;
                break;
              }
            }
          }
        }
      }
      break;
      case ATEV_MOUSEUP:{
        if (win->touchIndex!=-1){
          ACONTROLP ctl = (ACONTROLP) win->controls[win->touchIndex];
          if (ctl->oninput!=NULL)
            msg             = ctl->oninput((void*)ctl,action,&atev);
          win->touchIndex   = -1;
        }
      }
      break;
      case ATEV_MOUSEMV:{
        if (win->touchIndex!=-1){
          ACONTROLP ctl = (ACONTROLP) win->controls[win->touchIndex];
          if (ctl->oninput!=NULL)
            msg             = ctl->oninput((void*)ctl,action,&atev);
        }
      }
      break;
    }
    
    if (aw_gd(msg)==1) aw_draw(win);
    if (aw_gm(msg)!=0) return msg;
  }
  return msg;
}
dword actext_oninput(void * x,int action,ATEV * atev){
  ACONTROLP ctl= (ACONTROLP) x;
  ACTEXTDP  d  = (ACTEXTDP) ctl->d;
  if (d->maxScrollY==0) return 0;
  dword msg = 0;
  switch (action){
    case ATEV_MOUSEDN:
      {
        akinetic_downhandler(&d->akin,atev->y);
      }
      break;
    case ATEV_MOUSEUP:
      {
        if (akinetic_uphandler(&d->akin,atev->y))
          ac_regfling(ctl,&d->akin,&d->scrollY,d->maxScrollY);
        else if ((d->scrollY<0)||(d->scrollY>d->maxScrollY)){
          ac_regbounce(ctl,&d->scrollY,d->maxScrollY);
        }
      }
      break;
    case ATEV_MOUSEMV:
      {
        if (atev->y!=0){
          int mv = akinetic_movehandler(&d->akin,atev->y);
          if (mv!=0){
            if ((d->scrollY<0)&&(mv<0)){
              float dumpsz = 0.6-(0.6*(((float) abs(d->scrollY))/(ctl->h/4)));
              d->scrollY+=floor(mv*dumpsz);
            }
            else if ((d->scrollY>d->maxScrollY)&&(mv>0)){
              float dumpsz = 0.6-(0.6*(((float) abs(d->scrollY-d->maxScrollY))/(ctl->h/4)));
              d->scrollY+=floor(mv*dumpsz);
            }
            else
              d->scrollY+=mv;

            if (d->scrollY<0-(ctl->h/4)) d->scrollY=0-(ctl->h/4);
            if (d->scrollY>d->maxScrollY+(ctl->h/4)) d->scrollY=d->maxScrollY+(ctl->h/4);
            msg=aw_msg(0,1,0,0);
            ctl->ondraw(ctl);
          }
        }
      }
      break;
      case ATEV_DOWN:
        {
          if (d->scrollY<d->maxScrollY){
            msg=aw_msg(0,1,1,0);
            int reqY = d->scrollY+ceil(ctl->h/8);
            if (reqY>d->maxScrollY) reqY = d->maxScrollY;
            d->targetY=reqY;
            ac_regscrollto(
              ctl,
              &d->scrollY,
              d->maxScrollY,
              reqY,
              &d->targetY,
              d->targetY
            );
          }
        }
      break;
      case ATEV_UP:
        {
          if (d->scrollY>0){
            msg=aw_msg(0,1,1,0);
            int reqY = d->scrollY-ceil(ctl->h/8);
            if (reqY<0) reqY = 0;
            d->targetY=reqY;
            ac_regscrollto(
              ctl,
              &d->scrollY,
              d->maxScrollY,
              reqY,
              &d->targetY,
              d->targetY
            );
          }
        }
      break;
  }
  return msg;
}
dword afbox_oninput(void * x,int action,ATEV * atev){
  ACONTROLP ctl= (ACONTROLP) x;
  AFBOXDP d  = (AFBOXDP) ctl->d;
  dword msg = 0;
  switch (action){
    case 444:
      {
        // printf("HOLDED: %i - %s",d->touchedItem,d->items[d->touchedItem]->title);
        vibrate(30);
        int tmptouch=d->touchedItem;
        int tmpfocus=d->focusedItem;
        d->selectedId  = tmptouch;
        d->focusedItem = tmptouch;
        d->touchedItem = -1;
        if (tmpfocus!=tmptouch) afbox_redrawitem_ex(ctl,tmpfocus);
        afbox_redrawitem_ex(ctl,tmptouch);
        ctl->ondraw(ctl);
        msg=aw_msg(d->holdmsg,1,0,0);
        atouch_send_message(msg);
      }
      break;
    case ATEV_MOUSEDN:
      {
        d->prevTouchY  = atev->y;
        akinetic_downhandler(&d->akin,atev->y);
        
        int touchpos = atev->y - ctl->y + d->scrollY;
        int i;
        for (i=0;i<d->itemn;i++){
          if ((touchpos>=d->items[i]->y)&&(touchpos<d->items[i]->y+d->items[i]->h)){
            ac_regpushwait(
              ctl,&d->prevTouchY,&d->invalidDrawItem,i
            );
            break;
          }
        }
      }
      break;
    case ATEV_MOUSEUP:
      {
        byte is_holded = 0;
        if (d->lasttouch==-1) is_holded=1;
        d->lasttouch=0;
        byte ag_check_msg = 0;
        if ((d->prevTouchY!=-50)&&(abs(d->prevTouchY-atev->y)<agdp()*5)){
          d->prevTouchY=-50;
          int touchpos = atev->y - ctl->y + d->scrollY;
          
          byte oncheckpush=((d->check_n>0)||(atev->x>((ctl->x+ctl->w)-(agdp()*32))))?1:0;
          
          if (d->boxtype!=0) oncheckpush=0;
          
          int i;
          for (i=0;i<d->itemn;i++){
            if ((!d->items[i]->isTitle)&&(touchpos>=d->items[i]->y)&&(touchpos<d->items[i]->y+d->items[i]->h)){
              
              if ((oncheckpush)&&(!is_holded)){
                d->items[i]->checked = (d->items[i]->checked)?0:1;
                if (d->items[i]->checked)
                  d->check_n++;
                else
                  d->check_n--;
                ag_check_msg = d->changemsg;
              }
              
              if ((d->touchedItem != -1)&&(d->touchedItem!=i)){
                int tmptouch=d->touchedItem;
                d->touchedItem = -1;
                afbox_redrawitem_ex(ctl,tmptouch);
              }
              
              int prevfocus = d->focusedItem;
              d->focusedItem= i;
              d->touchedItem= i;
              if ((prevfocus!=-1)&&(prevfocus!=i)){
                afbox_redrawitem_ex(ctl,prevfocus);
              }
              
              
              afbox_redrawitem_ex(ctl,i);
              ctl->ondraw(ctl);
              aw_draw(ctl->win);
              
              if (!is_holded){
                vibrate(30);
                
                if (!oncheckpush){
                  ag_check_msg = d->touchmsg;
                  d->selectedId=i;
                  msg=aw_msg(ag_check_msg,1,0,0);
                }
              }
              break;
            }
          }
          if ((d->scrollY<0)||(d->scrollY>d->maxScrollY)){
            ac_regbounce(ctl,&d->scrollY,d->maxScrollY);
          }
        }
        else{
          if (akinetic_uphandler(&d->akin,atev->y)){
            ac_regfling(ctl,&d->akin,&d->scrollY,d->maxScrollY);
          }
          else if ((d->scrollY<0)||(d->scrollY>d->maxScrollY)){
            ac_regbounce(ctl,&d->scrollY,d->maxScrollY);
          }
        }
        if (d->touchedItem != -1){
          usleep(30);
          int tmptouch=d->touchedItem;
          d->touchedItem = -1;
          afbox_redrawitem_ex(ctl,tmptouch);
          ctl->ondraw(ctl);
          msg=aw_msg(ag_check_msg,1,0,0);
        }
      }
      break;
    case ATEV_MOUSEMV:
      {
        byte allowscroll=1;
        if (atev->y!=0){
          if (d->prevTouchY!=-50){
            if (abs(d->prevTouchY-atev->y)>=agdp()*5){
              d->prevTouchY=-50;
              if (d->touchedItem != -1){
                int tmptouch=d->touchedItem;
                d->touchedItem = -1;
                afbox_redrawitem_ex(ctl,tmptouch);
                ctl->ondraw(ctl);
                aw_draw(ctl->win);
              }
            }
            else
              allowscroll=0;
          }
          if (allowscroll){
            d->lasttouch=0;
            int mv = akinetic_movehandler(&d->akin,atev->y);
            if (mv!=0){
              if ((d->scrollY<0)&&(mv<0)){
                float dumpsz = 0.6-(0.6*(((float) abs(d->scrollY))/(ctl->h/4)));
                d->scrollY+=floor(mv*dumpsz);
              }
              else if ((d->scrollY>d->maxScrollY)&&(mv>0)){
                float dumpsz = 0.6-(0.6*(((float) abs(d->scrollY-d->maxScrollY))/(ctl->h/4)));
                d->scrollY+=floor(mv*dumpsz);
              }
              else
                d->scrollY+=mv;
  
              if (d->scrollY<0-(ctl->h/4)) d->scrollY=0-(ctl->h/4);
              if (d->scrollY>d->maxScrollY+(ctl->h/4)) d->scrollY=d->maxScrollY+(ctl->h/4);
              msg=aw_msg(0,1,0,0);
              ctl->ondraw(ctl);
            }
          }
        }
      }
      break;
      case ATEV_SEARCH:
      {
        d->lasttouch=0;
        if (d->boxtype==0){
          if ((d->focusedItem>-1)&&(d->draweditemn>0)){
            if (atev->d){
              if ((d->touchedItem != -1)&&(d->touchedItem!=d->focusedItem)){
                int tmptouch=d->touchedItem;
                d->touchedItem = -1;
                afbox_redrawitem_ex(ctl,tmptouch);
              }
              vibrate(30);
              d->touchedItem=d->focusedItem;
              afbox_redrawitem_ex(ctl,d->focusedItem);
              ctl->ondraw(ctl);
              msg=aw_msg(0,1,0,0);
            }
            else{
              d->items[d->focusedItem]->checked = (d->items[d->focusedItem]->checked)?0:1;
              if (d->items[d->focusedItem]->checked)
                d->check_n++;
              else
                d->check_n--;
              d->touchedItem=-1;
              afbox_redrawitem_ex(ctl,d->focusedItem);
              ctl->ondraw(ctl);
              msg=aw_msg(d->changemsg,1,0,0);
            }
          }
        }
      }
      break;
      case ATEV_SELECT:
        {
          d->lasttouch=0;
          if ((d->focusedItem>-1)&&(d->draweditemn>0)){
            if (atev->d){
              if ((d->touchedItem != -1)&&(d->touchedItem!=d->focusedItem)){
                int tmptouch=d->touchedItem;
                d->touchedItem = -1;
                afbox_redrawitem_ex(ctl,tmptouch);
              }
              vibrate(30);
              d->touchedItem=d->focusedItem;
              afbox_redrawitem_ex(ctl,d->focusedItem);
              ctl->ondraw(ctl);
              msg=aw_msg(0,1,0,0);
            }
            else{
              if ((d->check_n>0)&&(d->boxtype==0)){
                d->items[d->focusedItem]->checked = (d->items[d->focusedItem]->checked)?0:1;
                if (d->items[d->focusedItem]->checked)
                  d->check_n++;
                else
                  d->check_n--;
                d->touchedItem=-1;
                afbox_redrawitem_ex(ctl,d->focusedItem);
                ctl->ondraw(ctl);
                msg=aw_msg(d->changemsg,1,0,0);
              }
              else{
                if ((d->touchedItem != -1)&&(d->touchedItem!=d->focusedItem)){
                  int tmptouch=d->touchedItem;
                  d->touchedItem = -1;
                  afbox_redrawitem_ex(ctl,tmptouch);
                }
                d->selectedId=d->focusedItem;
                d->touchedItem=-1;
                afbox_redrawitem_ex(ctl,d->focusedItem);
                ctl->ondraw(ctl);
                msg=aw_msg(d->touchmsg,1,0,0);
              }
            }
          }
        }
        break;
      case ATEV_DOWN:
        {
          d->lasttouch=0;
          if ((d->focusedItem<d->itemn-1)&&(d->draweditemn>0)){
            int prevfocus = d->focusedItem;
            d->focusedItem++;
            while(d->items[d->focusedItem]->isTitle){
              d->focusedItem++;
              if (d->focusedItem>d->itemn-1){
                d->focusedItem = prevfocus;
                d->selectedId  = d->focusedItem;
                return 0;
              }
            }
            d->selectedId  = d->focusedItem;
            afbox_redrawitem_ex(ctl,prevfocus);
            afbox_redrawitem_ex(ctl,d->focusedItem);
            ctl->ondraw(ctl);
            msg=aw_msg(0,1,1,0);
            
            int reqY = d->items[d->focusedItem]->y - round((ctl->h/2) - (d->items[d->focusedItem]->h/2));
            ac_regscrollto(
              ctl,
              &d->scrollY,
              d->maxScrollY,
              reqY,
              &d->focusedItem,
              d->focusedItem
            );
          }
        }
      break;
      case ATEV_UP:
        {
          d->lasttouch=0;
          if ((d->focusedItem>0)&&(d->draweditemn>0)){
            int prevfocus = d->focusedItem;
            d->focusedItem--;
            while(d->items[d->focusedItem]->isTitle){
              d->focusedItem--;
              if (d->focusedItem<0){
                d->focusedItem = prevfocus;
                d->selectedId  = d->focusedItem;
                return 0;
              }
            }
            d->selectedId  = d->focusedItem;
            afbox_redrawitem_ex(ctl,prevfocus);
            afbox_redrawitem_ex(ctl,d->focusedItem);
            ctl->ondraw(ctl);
            msg=aw_msg(0,1,1,0);
            
            int reqY = d->items[d->focusedItem]->y - round((ctl->h/2) - (d->items[d->focusedItem]->h/2));
            ac_regscrollto(
              ctl,
              &d->scrollY,
              d->maxScrollY,
              reqY,
              &d->focusedItem,
              d->focusedItem
            );
          }
        }
      break;
  }
  return msg;
}