February, 1996 - Vol. 3 No. 2


Listing A: VALEDIT.CPP


#include <owl\applicat.h> 
#include <owl\dialog.h>
#include <owl\framewin.h>
#include <owl\edit.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <mem.h>
#include "valedit.rh"

class TNameEdit : public TEdit {
public:
  TNameEdit(TWindow* parent, int resourceID, 
            TModule* module = 0);
  void EvChar(UINT key, UINT repeatCount, 
              UINT flags);
  DECLARE_RESPONSE_TABLE(TNameEdit);
};

DEFINE_RESPONSE_TABLE1(TNameEdit, TEdit)
  EV_WM_CHAR,
END_RESPONSE_TABLE;



TNameEdit::TNameEdit(TWindow* parent, 
                     int resourceID, 
                     TModule* module)
    : TEdit(parent, resourceID, 31, module)
{
}

void TNameEdit::EvChar(UINT key, UINT repeatCount, 
                       UINT flags)
{
  if (key == VK_BACK) {
    TEdit::EvChar(key, repeatCount, flags);
    return;
  }
  char Buffer[255];
  memset(Buffer, 0, 255);
  GetText(Buffer, 255);
  int Len = strlen(Buffer);
  if (Len==0 && isalpha(key)) {
    Buffer[0]=toupper(key);
    SetText(Buffer);
    SetSelection(1,1);
  }
  else if (Buffer[Len-1]==' ') {
    Buffer[Len]=toupper(key);
    SetText(Buffer);
    SetSelection(Len+1,Len+1);
  }        
  else
    TEdit::EvChar(key, repeatCount, flags);
}

class TCreditCardNo : public TEdit {
  int   IsValidCard();
  char Card[17];
public:
  static int Lookup[10];
  TCreditCardNo(TWindow* parent, int resourceID, 
                TModule* module = 0);
  void EvChar(UINT key, UINT repeatCount, 
              UINT flags);
  void CmPostEdit();
  DECLARE_RESPONSE_TABLE(TCreditCardNo);
};

int TCreditCardNo::Lookup[] = 
       {0,2,4,6,8,1,3,5,7,9 };

DEFINE_RESPONSE_TABLE1(TCreditCardNo, TEdit)
  EV_WM_CHAR,
  EV_EN_KILLFOCUS(-1, CmPostEdit),
END_RESPONSE_TABLE;

TCreditCardNo::TCreditCardNo(TWindow* parent, 
                             int resourceID, 
                             TModule* module)
    : TEdit(parent, resourceID, 17, module)
{
}

int TCreditCardNo::IsValidCard()
{
  int CheckSum = 0;
  int Len = strlen(Card); 
  for (int i=0; i<Len; i++) {
    if (Len == 16)
      CheckSum += (i&1) ? (Card[i]-'0') : 
                          Lookup[Card[i]- '0'];
    else
      CheckSum += !(i&1) ? (Card[i]-'0') : 
                           Lookup[Card[i]- '0'];
  }
  return (CheckSum%10);
}

void TCreditCardNo::CmPostEdit()
{
  GetText(Card, 17);
  if (strlen(Card)==0) 
    return;
  if (IsValidCard()==0)
    return;
  else {
    MessageBox("Invalid Card Number - Try Again!");
    SetText("");
    SetFocus();
  }
}

void TCreditCardNo::EvChar(UINT key, 
                           UINT repeatCount, 
                           UINT flags)
{
  if (key == VK_BACK) {
    TEdit::EvChar(key, repeatCount, flags);
    return;
  }

  if (key >= 0x30 && key <= 0x39)
    TEdit::EvChar(key, repeatCount, flags);
  else {
    MessageBeep(-1);
    return;
  }

  memset(Card, 0, 17);
  GetText(Card, 17);
  if (strlen(Card)==16 || (strlen(Card)==15 && 
                           Card[0]=='3')) {
    Parent->PostMessage(WM_NEXTDLGCTL, (WPARAM)0, 
                        FALSE);
    return;
  }
}

class TEditDialog : public TDialog {
public:
  TEditDialog(TWindow* parent, TResId resId, 
              TModule* module = 0);
};

TEditDialog::TEditDialog(TWindow* parent, 
                         TResId resId, TModule* module) : TDialog(parent, resId, module)
{
  TCreditCardNo *pint1 = 
    new TCreditCardNo(this, 101,0);
  TNameEdit *pint2 = new TNameEdit(this, 102);
}

class TEditsApp : public TApplication {
public:
  void InitMainWindow();
};

void TEditsApp::InitMainWindow()
{
  TDialog *pd = new TEditDialog(0, DIALOG_1);
  MainWindow = new TFrameWindow(0, "TEdit Classes",
                                pd, TRUE);

  MainWindow->Attr.Style &= 
    ~WS_THICKFRAME&~WS_MAXIMIZEBOX;

  EnableBWCC();
}

int OwlMain(int, char *[])
{
  return TEditsApp().Run();
}

Return to "OWL Programming - Creating your own validation classes"


Copyright (c) 1996 The Cobb Group, a division of Ziff-Davis Publishing Company. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of Ziff-Davis Publishing Company is prohibited. The Cobb Group and The Cobb Group logo are trademarks of Ziff-Davis Publishing Company.