home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / BORLAND TURBO / OWLSRC.PAK / RANGEVAL.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  2.9 KB  |  142 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1992, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.4  $
  6. //
  7. // Implementation of TRangeValidator, integer numeric range input validator
  8. //----------------------------------------------------------------------------
  9. #pragma hdrignore SECTION
  10. #include <owl/pch.h>
  11. #if !defined(OWL_VALIDATE_H)
  12. # include <owl/validate.h>
  13. #endif
  14. #if !defined(OWL_APPLICAT_H)
  15. # include <owl/applicat.h>
  16. #endif
  17. #if !defined(OWL_APPDICT_H)
  18. # include <owl/appdict.h>
  19. #endif
  20. #if !defined(OWL_FRAMEWIN_H)
  21. # include <owl/framewin.h>
  22. #endif
  23. #include <stdio.h>
  24.  
  25. OWL_DIAGINFO;
  26.  
  27. #if !defined(SECTION) || SECTION == 1
  28.  
  29. //
  30. //
  31. //
  32. TRangeValidator::TRangeValidator(long min, long max)
  33. :
  34.   TFilterValidator("0-9+-")
  35. {
  36.   if (min >= 0)
  37.     ValidChars -= '-';
  38.   Min = min;
  39.   Max = max;
  40. }
  41.  
  42. //
  43. //
  44. //
  45. void
  46. TRangeValidator::Error(TWindow* owner)
  47. {
  48.   PRECONDITION(owner);
  49.   TApplication* app = owner->GetApplication();
  50.   string msgTmpl = app->LoadString(IDS_VALNOTINRANGE);
  51.   TAPointer<char> msg = new char[msgTmpl.length() + 10 + 10 + 1];
  52.   sprintf(msg, msgTmpl.c_str(), Min, Max);
  53.   owner->MessageBox(msg, app->GetName(), MB_ICONEXCLAMATION|MB_OK);
  54. }
  55.  
  56. //
  57. //
  58. //
  59. bool
  60. TRangeValidator::IsValid(const char far* s)
  61. {
  62.   if (TFilterValidator::IsValid(s)) {
  63.     long value = atol(s);
  64.     if (value >= Min && value <= Max)
  65.       return true;
  66.   }
  67.   return false;
  68. }
  69.  
  70. //
  71. //
  72. //
  73. uint
  74. TRangeValidator::Transfer(char far* s, void* buffer, TTransferDirection direction)
  75. {
  76.   if (Options & voTransfer) {
  77.     if (direction == tdGetData) {
  78.       *(long*)buffer = atol(s);
  79.     }
  80.     else if (direction == tdSetData) {
  81.       wsprintf(s, "%ld", *(long*)buffer);  // need wsprintf for char far*
  82.     }
  83.     return sizeof(long);
  84.   }
  85.   else
  86.     return 0;
  87. }
  88.  
  89. //
  90. // Adjust the 'value' of the text, given a cursor position & an amount
  91. // Return the actual amount adjusted.
  92. //
  93. int
  94. TRangeValidator::Adjust(string& text, uint& /*begPos*/, uint& /*endPos*/, int amount)
  95. {
  96.   long value = atol(text.c_str());
  97.   long newValue = value + amount;
  98.   if (newValue < Min)
  99.     newValue = Min;
  100.   else if (newValue > Max)
  101.     newValue = Max;
  102.  
  103.   char buffer[15];
  104.   sprintf(buffer, "%ld", newValue);
  105.   text = buffer;
  106.  
  107.   return int(newValue - value);
  108. }
  109.  
  110.  
  111. #endif
  112. #if !defined(SECTION) || SECTION == 2
  113.  
  114. IMPLEMENT_STREAMABLE1(TRangeValidator, TFilterValidator);
  115.  
  116. #if !defined(BI_NO_OBJ_STREAMING)
  117.  
  118. //
  119. //
  120. //
  121. void*
  122. TRangeValidator::Streamer::Read(ipstream& is, uint32 /*version*/) const
  123. {
  124.   ReadBaseObject((TFilterValidator*)GetObject(), is);
  125.   is >> GetObject()->Min >> GetObject()->Max;
  126.   return GetObject();
  127. }
  128.  
  129. //
  130. //
  131. //
  132. void
  133. TRangeValidator::Streamer::Write(opstream& os) const
  134. {
  135.   WriteBaseObject((TFilterValidator*)GetObject(), os);
  136.   os << GetObject()->Min << GetObject()->Max;
  137. }
  138.  
  139. #endif  // if !defined(BI_NO_OBJ_STREAMING)
  140.  
  141. #endif
  142.