Validated LineEdit Tester - OOP344 20103

From CDOT Wiki
Revision as of 16:29, 8 November 2010 by Fardad (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


OOP344 | Weekly Schedule | Student List | Teams | Project | Student Resources

#include "confw.h"
#include "fwborder.h"
#include "fwdialog.h"
#include "fwlabel.h"
#include "fwveditline.h"
#include "fwbutton.h"
#include <cstring>
#include <cctype>
using namespace std;
void Move(FWBorder &Br);

bool Yes(const char* message, FWDialog* owner);
void Help(FWDialog* owner);
void PhoneHelp(MessageStatus st, FWDialog& owner);
void LastNameHelp(MessageStatus st, FWDialog& owner);
bool ValidPhone(const char* ph , FWDialog& owner);

  int main() {
  int insert = 1;
  int key;
  bool done = false;
  iol_init();
  FWDialog Screen;
  FWDialog F(&Screen,5, 5, 70, 15, true);
  FWLabel PhHelp(8, 34, 30);
  FWLabel LnHelp(5, 37, 30);
  FWLabel ErrMes(10, 2, 67);
  Screen<< new FWLabel("F1: HELP ", 0, 0);
  F<<new FWLabel("Name:", 2, 2)
    <<new FWLineEdit(1, 10, 20, 40, &insert,true)
    <<new FWLabel("Lastname:", 5, 2)
    <<new FWValEdit(4, 13, 20, 40, &insert,NO_VALDFUNC, LastNameHelp, true)
    <<new FWLabel("Phone Number", 8,2)
    <<new FWValEdit(7, 16, 15, 12, &insert,ValidPhone,PhoneHelp, true)
    <<PhHelp
    <<LnHelp
    <<ErrMes;
  F.draw(FW_REFRESH);
  while(!done){
    key = F.edit();
    switch(key){
      case F1_KEY:
        Help(&F);
        break;
      case ESCAPE_KEY:
        if(Yes("Do you really want to quit?", &F)){
          done = true;
        }
        break;
      case F6_KEY:
        Move(F);
        break;
    }
  }
  iol_end();

  return 0;
}

bool Yes(const char* message, FWDialog* owner){
  int key;
  bool res = false;
  bool done = false;
  FWButton bt_yes("Yes", 4,4 ,true,"     _  ");
  FWButton bt_no("No", 4,15 ,true,"     _  ");
  FWDialog YesNo(owner,(iol_rows()- 10)/2, (iol_cols()-40)/2, 40, 10, true);
  YesNo<<new FWLabel(2, 2, 36)<<bt_yes<<bt_no;
  YesNo[0].set(message);
  YesNo.draw(FW_FULL_FRAME);
  while(!done){
    key = YesNo.edit();
    if(key == FW_BUTTON_HIT){
      res =  &YesNo.curField() == &bt_yes;
      done = true;
    }
  }
  owner->draw(FW_REFRESH);
  return res;
}
void Help(FWDialog* owner){
  FWDialog help(owner,(iol_rows()- 10)/2, (iol_cols()-40)/2, 40, 10, true);
  
  help<<new FWLabel(2, 3,36)
      <<new FWLabel("Escape Key: Exit the test program.", 4, 3)
      <<new FWLabel("F1 Key: Open this window.", 6, 3); 
  switch(owner->curIndex()){
    case 1:
      help[0].set("Enter the name here!");
      break;
    case 3: 
      help[0].set("Enter the Last name here!");
      break;
    case 5:
      help[0].set("Enter Phone number: 999-999-9999");
  }
  help.edit(FW_FULL_FRAME);
  owner->draw(FW_REFRESH);
}

void PhoneHelp(MessageStatus st, FWDialog& owner){
  if(st == ClearMessage){
    owner[6].set("");
  }
  else{
    owner[6].set("Phone Format: 999-999-9999");
  }
  owner.draw(7);
}
void LastNameHelp(MessageStatus st, FWDialog& owner){
  if(st == ClearMessage){
    owner[7].set("");
  }
  else{
    owner[7].set("i.e. Middle name and Surname");
  }
  owner.draw(8);
}

bool ValidPhone(const char* ph , FWDialog& owner){
  bool ok = true;
  int i;
  for(i=0;ok && i<3;ok = (bool)isdigit(ph[i]), i++);
  ok = ph[i++] == '-';
  for(;ok && i<7;ok = (bool)isdigit(ph[i]), i++);
  ok = ph[i++] == '-';
  for(;ok && i<12;ok = (bool)isdigit(ph[i]), i++);
  if(ok){
    owner[8].set("");
  }
  else{
    owner[8].set("Invlid phone number, please use the specified phone number format!");
  }
  owner.draw(9);
  return ok;
}


void Move(FWBorder &Br){
  bool done = false;
  int key;
  while(!done){
    Br.draw(FW_REFRESH);
    iol_display("Moving! ESC: exit", 0, 0,-1);
    key = iol_getch();
    switch(key){
    case ESCAPE_KEY:
      done = true;
      break;
    case UP_KEY:
      if(Br.row() > 0){
        Br.row(Br.row()-1);
      }
      break;
    case DOWN_KEY:
      if(Br.row() + Br.height() < Br.container()->height()){
        Br.row(Br.row()+1);
      }
      break;
    case LEFT_KEY:
      if(Br.col() > 0){
        Br.col(Br.col()-1);
      }
      break;
    case RIGHT_KEY:
      if(Br.col() + Br.width() < Br.container()->width()){
        Br.col(Br.col() + 1);
      }
      break;
    }
  }
}