home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / OWLSRC.PAK / LOOKVAL.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  5.7 KB  |  375 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1993, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.4  $
  6. //
  7. //----------------------------------------------------------------------------
  8. #pragma hdrignore SECTION
  9. #include <owl/pch.h>
  10. #if !defined(OWL_VALIDATE_H)
  11. # include <owl/validate.h>
  12. #endif
  13. #if !defined(OWL_APPLICAT_H)
  14. # include <owl/applicat.h>
  15. #endif
  16. #if !defined(OWL_APPDICT_H)
  17. # include <owl/appdict.h>
  18. #endif
  19. #if !defined(OWL_FRAMEWIN_H)
  20. # include <owl/framewin.h>
  21. #endif
  22.  
  23. OWL_DIAGINFO;
  24.  
  25. #if !defined(SECTION) || SECTION == 1
  26.  
  27. //
  28. // TLookupValidator
  29. //
  30. TLookupValidator::TLookupValidator()
  31. {
  32. }
  33.  
  34. //
  35. //
  36. //
  37. bool
  38. TLookupValidator::IsValid(const char far* str)
  39. {
  40.   return Lookup(str);
  41. }
  42.  
  43. //
  44. //
  45. //
  46. bool
  47. TLookupValidator::Lookup(const char far* /*str*/)
  48. {
  49.   return true;
  50. }
  51.  
  52. //----------------------------------------------------------------------------
  53.  
  54. //
  55. // TSortedStringArray implementation
  56. //
  57. TSortedStringArray::TSortedStringArray(int upper, int lower, int delta)
  58. :
  59.   Data(upper, lower, delta)
  60. {
  61. }
  62.  
  63. //
  64. //
  65. //
  66. int
  67. TSortedStringArray::LowerBound() const
  68. {
  69.   return Data.LowerBound();
  70. }
  71.  
  72. //
  73. //
  74. //
  75. int
  76. TSortedStringArray::UpperBound() const
  77. {
  78.   return Data.UpperBound();
  79. }
  80.  
  81. //
  82. //
  83. //
  84. unsigned
  85. TSortedStringArray::ArraySize() const
  86. {
  87.   return Data.ArraySize();
  88. }
  89.  
  90. //
  91. //
  92. //
  93. int
  94. TSortedStringArray::IsFull() const
  95. {
  96.   return Data.IsFull();
  97. }
  98.  
  99. //
  100. //
  101. //
  102. int
  103. TSortedStringArray::IsEmpty() const
  104. {
  105.   return Data.IsEmpty();
  106. }
  107.  
  108. //
  109. //
  110. //
  111. unsigned
  112. TSortedStringArray::GetItemsInContainer() const
  113. {
  114.   return Data.GetItemsInContainer();
  115. }
  116.  
  117. //
  118. //
  119. //
  120. int
  121. TSortedStringArray::Add(const string& t)
  122. {
  123.   return Data.Add(t);
  124. }
  125.  
  126. //
  127. //
  128. //
  129. int
  130. TSortedStringArray::Detach(const string& t)
  131. {
  132.   return Data.Detach(t);
  133. }
  134.  
  135. //
  136. //
  137. //
  138. int
  139. TSortedStringArray::Detach(int loc)
  140. {
  141.   return Data.Detach(loc);
  142. }
  143.  
  144. //
  145. //
  146. //
  147. int
  148. TSortedStringArray::Destroy(const string& t)
  149. {
  150.   return Detach(t);
  151. }
  152.  
  153. //
  154. //
  155. //
  156. int
  157. TSortedStringArray::Destroy(int loc)
  158. {
  159.   return Detach(loc);
  160. }
  161.  
  162. //
  163. //
  164. //
  165. int
  166. TSortedStringArray::HasMember(const string& t) const
  167. {
  168.   return Data.HasMember(t);
  169. }
  170.  
  171. //
  172. //
  173. //
  174. int
  175. TSortedStringArray::Find(const string& t) const
  176. {
  177.   return Data.Find(t);
  178. }
  179.  
  180. //
  181. //
  182. //
  183. string&
  184. TSortedStringArray::operator [](int loc)
  185. {
  186.   return Data[loc];
  187. }
  188.  
  189. //
  190. //
  191. //
  192. string&
  193. TSortedStringArray::operator [](int loc) const
  194. {
  195.   return Data[loc];
  196. }
  197.  
  198. //
  199. //
  200. //
  201. void
  202. TSortedStringArray::ForEach(IterFunc iter, void* args)
  203. {
  204.   Data.ForEach(iter, args);
  205. }
  206.  
  207. //
  208. //
  209. //
  210. string*
  211. TSortedStringArray::FirstThat(CondFunc cond, void* args) const
  212. {
  213.   return Data.FirstThat(cond, args);
  214. }
  215.  
  216. //
  217. //
  218. //
  219. string*
  220. TSortedStringArray::LastThat(CondFunc cond, void* args) const
  221. {
  222.   return Data.LastThat(cond, args);
  223. }
  224.  
  225. //
  226. //
  227. //
  228. void
  229. TSortedStringArray::Flush()
  230. {
  231.   Data.Flush();
  232. }
  233.  
  234. //----------------------------------------------------------------------------
  235.  
  236. //
  237. // TStringLookupValidator
  238. //
  239.  
  240. //
  241. //
  242. //
  243. TStringLookupValidator::TStringLookupValidator(TSortedStringArray* strings)
  244. :
  245.   TLookupValidator()
  246. {
  247.   Strings = strings ? strings : new TSortedStringArray(0, 0, 0);
  248. }
  249.  
  250. //
  251. //
  252. //
  253. TStringLookupValidator::~TStringLookupValidator()
  254. {
  255.   delete Strings;
  256. }
  257.  
  258. //
  259. //
  260. //
  261. void
  262. TStringLookupValidator::Error(TWindow* owner)
  263. {
  264.   PRECONDITION(owner);
  265.   TApplication* app = owner->GetApplication();
  266.   const char* msg = app->LoadString(IDS_VALNOTINLIST).c_str();
  267.   owner->MessageBox(msg, app->GetName(), MB_ICONEXCLAMATION|MB_OK);
  268. }
  269.  
  270. //
  271. //
  272. //
  273. bool
  274. TStringLookupValidator::Lookup(const char far* str)
  275. {
  276.   if (Strings)
  277.     return Strings->HasMember(str);
  278.   return false;
  279. }
  280.  
  281. //
  282. //
  283. //
  284. void
  285. TStringLookupValidator::NewStringList(TSortedStringArray* strings)
  286. {
  287.   delete Strings;
  288.   Strings = strings;
  289. }
  290.  
  291. //
  292. // Adjust the 'value' of the text, given a cursor position & an amount
  293. // Return the actual amount adjusted.
  294. //
  295. int
  296. TStringLookupValidator::Adjust(string& text, uint& /*begPos*/, uint& /*endPos*/, int amount)
  297. {
  298.   if (!Strings)
  299.     return 0;
  300.  
  301.   int count = Strings->GetItemsInContainer();
  302.   int index = Strings->Find(text);
  303.   int newIndex = index + amount;
  304.   if (newIndex < 0)
  305.     newIndex = 0;
  306.   else if (newIndex >= count)
  307.     newIndex = count-1;
  308.  
  309.   text = (*Strings)[newIndex];
  310.   return newIndex - index;
  311. }
  312.  
  313. #endif
  314.  
  315. #if !defined(SECTION) || SECTION == 2
  316. IMPLEMENT_STREAMABLE1(TLookupValidator, TValidator);
  317.  
  318. #if !defined(BI_NO_OBJ_STREAMING)
  319.  
  320. //
  321. //
  322. //
  323. void*
  324. TLookupValidator::Streamer::Read(ipstream& is, uint32 /*version*/) const
  325. {
  326.   ReadBaseObject((TValidator*)GetObject(), is);
  327.   return GetObject();
  328. }
  329.  
  330. //
  331. //
  332. //
  333. void
  334. TLookupValidator::Streamer::Write(opstream& os) const
  335. {
  336.   WriteBaseObject((TValidator*)GetObject(), os);
  337. }
  338.  
  339. IMPLEMENT_STREAMABLE1(TStringLookupValidator, TLookupValidator);
  340.  
  341. //
  342. //
  343. //
  344. void*
  345. TStringLookupValidator::Streamer::Read(ipstream& is, uint32 /*version*/) const
  346. {
  347.   ReadBaseObject((TLookupValidator*)GetObject(), is);
  348.   unsigned count;
  349.   is >> count;
  350.   GetObject()->Strings = new TSortedStringArray(count, 0, 5);
  351.   for (unsigned i = 0; i < count; i++ ) {
  352.     string temp;
  353.     is >> temp;
  354.     GetObject()->Strings->Add(temp);
  355.   }
  356.   return GetObject();
  357. }
  358.  
  359. //
  360. //
  361. //
  362. void
  363. TStringLookupValidator::Streamer::Write(opstream& os) const
  364. {
  365.   WriteBaseObject((TLookupValidator*)GetObject(), os);
  366.   unsigned count = GetObject()->Strings->GetItemsInContainer();
  367.   os << count;
  368.   for (unsigned i = 0; i < count; i++)
  369.     os << (*GetObject()->Strings)[i];
  370. }
  371.  
  372. #endif  // if !defined(BI_NO_OBJ_STREAMING)
  373.  
  374. #endif
  375.