home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / owlsrc.pak / RANGEVAL.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-24  |  2.2 KB  |  92 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // (C) Copyright 1992, 1994 by Borland International, All Rights Reserved
  4. //
  5. //   Implementation of TRangeValidator, integer numeric range input validator
  6. //----------------------------------------------------------------------------
  7. #pragma hdrignore SECTION
  8. #include <owl/owlpch.h>
  9. #include <owl/validate.h>
  10. #include <owl/applicat.h>
  11. #include <owl/appdict.h>
  12. #include <owl/framewin.h>
  13.  
  14. #if !defined(SECTION) || SECTION == 1
  15.  
  16. //
  17. //
  18. //
  19. TRangeValidator::TRangeValidator(long min, long max)
  20. :
  21.   TFilterValidator("0-9+-")
  22. {
  23.   if (min >= 0)
  24.     ValidChars -= '-';
  25.   Min = min;
  26.   Max = max;
  27. }
  28.  
  29. void
  30. TRangeValidator::Error()
  31. {
  32.   TApplication* app = GetApplicationObject();
  33.   string msgTmpl = app->LoadString(IDS_VALNOTINRANGE).c_str();
  34.   char* msg = new char[msgTmpl.length() + 10 + 10 + 1];
  35.   wsprintf(msg, msgTmpl.c_str(), Min, Max);
  36.   TWindow* w = GetWindowPtr(app->GetMainWindow()->GetLastActivePopup());
  37.   if (w)
  38.     w->MessageBox(msg, app->GetName(), MB_ICONEXCLAMATION|MB_OK);
  39.   else
  40.     ::MessageBox(0, msg, app->GetName(), MB_ICONEXCLAMATION|MB_OK|MB_TASKMODAL);
  41.   delete [] msg;
  42. }
  43.  
  44. bool
  45. TRangeValidator::IsValid(const char far* s)
  46. {
  47.   if (TFilterValidator::IsValid(s)) {
  48.     long value = atol(s);
  49.     if (value >= Min && value <= Max)
  50.       return true;
  51.   }
  52.   return false;
  53. }
  54.  
  55. uint
  56. TRangeValidator::Transfer(char far* s, void* buffer, TTransferDirection direction)
  57. {
  58.   if (Options & voTransfer) {
  59.     if (direction == tdGetData) {
  60.       *(long*)buffer = atol(s);
  61.     }
  62.     else if (direction == tdSetData) {
  63.       wsprintf(s, "%ld", *(long*)buffer);
  64.     }
  65.     return sizeof(long);
  66.   }
  67.   else
  68.     return 0;
  69. }
  70.  
  71. #endif
  72. #if !defined(SECTION) || SECTION == 2
  73.  
  74. IMPLEMENT_STREAMABLE1(TRangeValidator, TFilterValidator);
  75.  
  76. void*
  77. TRangeValidator::Streamer::Read(ipstream& is, uint32 /*version*/) const
  78. {
  79.   ReadBaseObject((TFilterValidator*)GetObject(), is);
  80.   is >> GetObject()->Min >> GetObject()->Max;
  81.   return GetObject();
  82. }
  83.  
  84. void
  85. TRangeValidator::Streamer::Write(opstream& os) const
  86. {
  87.   WriteBaseObject((TFilterValidator*)GetObject(), os);
  88.   os << GetObject()->Min << GetObject()->Max;
  89. }
  90.  
  91. #endif
  92.