OWLNext    7.0
Borland's Object Windows Library for the modern age
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
lookval.cpp
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// ObjectWindows
3// Copyright (c) 1993, 1996 by Borland International, All Rights Reserved
4//
5//----------------------------------------------------------------------------
6#include <owl/pch.h>
7
8#include <owl/validate.h>
9#include <owl/validate.rh>
10#include <owl/applicat.h>
11#include <owl/appdict.h>
12#include <owl/framewin.h>
13
14namespace owl {
15
17
18// Let the compiler know that the following template instances will be defined elsewhere.
19//#pragma option -Jgx
20
21
22//
23/// Constructs a TLookupValidator object.
24//
28
29//
30/// IsValid overrides TValidator's virtual function and calls Lookup to find the
31/// string str in the list of valid input items. IsValid returns true if Lookup
32/// returns true, meaning Lookup found str in its list; otherwise, it returns false.
33//
34bool
36{
37 return Lookup(str);
38}
39
40//
41/// Searches for the string str in the list of valid entries and returns true if it
42/// finds str; otherwise, returns false. TLookupValidator's Lookup is an abstract
43/// method that always returns false. Descendant lookup validator types must
44/// override Lookup to perform a search based on the actual list of acceptable
45/// items.
46//
47bool
49{
50 return true;
51}
52
53//----------------------------------------------------------------------------
54//
55// TStringLookupValidator
56//
57
58/// Constructs a string-lookup object by first calling the constructor inherited
59/// from TLookupValidator and then setting Strings to strings.
66
67//
68/// This destructor disposes of a list of valid strings by calling NewStringList and
69/// then disposes of the string-lookup validator object by calling the Destructor
70/// inherited from TLookupValidator.
71//
76
77//
78/// Overrides TValidator's virtual function and displays a message box indicating
79/// that the typed string does not match an entry in the string list.
80//
81void
87
88//
89/// Overrides TLookupValidator's virtual function. Returns true if the string passed
90/// in str matches any of the strings. Uses the search method of the string
91/// collection to determine if str is present.
92//
93bool
95{
96 if (Strings)
97 return Strings->Find(tstring{str}) != Strings->NPOS;
98 return false;
99}
100
101//
102/// Sets the list of valid input string for the string-lookup validator. Disposes of
103/// any existing string list and then sets Strings to strings.
104//
105void
107{
108 delete Strings;
109 Strings = strings;
110}
111
112/// Adjusts the 'value' of the text, given a cursor position and an amount. Returns
113/// the actual amount adjusted.
114//
115int
116TStringLookupValidator::Adjust(tstring& text, int& /*begPos*/, int& /*endPos*/, int amount)
117{
118 if (!Strings)
119 return 0;
120
121 const auto count = static_cast<int>(Strings->Size());
122 const auto index = Strings->Find(text);
123 if (index == Strings->NPOS)
124 return 0;
125 auto newIndex = index + amount;
126 if (newIndex < 0)
127 newIndex = 0;
128 else if (newIndex >= count)
129 newIndex = count - 1;
130
131 text = (*Strings)[newIndex];
132 return newIndex - index;
133}
134
135
138
139#if OWL_PERSISTENT_STREAMS
140
141//
142//
143//
144void*
145TLookupValidator::Streamer::Read(ipstream& is, uint32 /*version*/) const
146{
147 ReadBaseObject((TValidator*)GetObject(), is);
148 return GetObject();
149}
150
151//
152//
153//
154void
155TLookupValidator::Streamer::Write(opstream& os) const
156{
157 WriteBaseObject((TValidator*)GetObject(), os);
158}
159
160//
161//
162//
163void*
164TStringLookupValidator::Streamer::Read(ipstream& is, uint32 /*version*/) const
165{
166 ReadBaseObject((TLookupValidator*)GetObject(), is);
167 unsigned count;
168 is >> count;
169 GetObject()->Strings = new TSortedStringArray;
170 for (unsigned i = 0; i < count; i++ ) {
172 is >> temp;
173 GetObject()->Strings->Add(temp);
174 }
175 return GetObject();
176}
177
178//
179//
180//
181void
182TStringLookupValidator::Streamer::Write(opstream& os) const
183{
184 WriteBaseObject((TLookupValidator*)GetObject(), os);
185 unsigned count = GetObject()->Strings->Size();
186 os << count;
187 for (unsigned i = 0; i < count; i++)
188 os << (*GetObject()->Strings)[i];
189}
190
191#endif
192
193} // OWL namespace
194/* ========================================================================== */
195
Definition of class TAppDictionary.
Definition of class TApplication.
#define PRECONDITION(condition)
Definition checks.h:227
uint Size() const
Definition template.h:672
static const int NPOS
Definition template.h:682
A streamable class, TLookupValidator compares the string typed by a user with a list of acceptable va...
Definition validate.h:269
TLookupValidator()
Constructs a TLookupValidator object.
Definition lookval.cpp:25
bool IsValid(LPCTSTR str)
IsValid overrides TValidator's virtual function and calls Lookup to find the string str in the list o...
Definition lookval.cpp:35
virtual bool Lookup(LPCTSTR str)
Searches for the string str in the list of valid entries and returns true if it finds str; otherwise,...
Definition lookval.cpp:48
int Find(const T &t) const
Definition template.h:1487
Derived from TLookupValidator, TStringLookupValidator is a streamable class.
Definition validate.h:295
void Error(TWindow *owner)
Overrides TValidator's virtual function and displays a message box indicating that the typed string d...
Definition lookval.cpp:82
int Adjust(tstring &text, int &begPos, int &endPos, int amount)
Adjusts the 'value' of the text, given a cursor position and an amount.
Definition lookval.cpp:116
~TStringLookupValidator()
This destructor disposes of a list of valid strings by calling NewStringList and then disposes of the...
Definition lookval.cpp:72
TStringLookupValidator(TSortedStringArray *strings)
Constructs a string-lookup object by first calling the constructor inherited from TLookupValidator an...
Definition lookval.cpp:60
bool Lookup(LPCTSTR str)
Overrides TLookupValidator's virtual function.
Definition lookval.cpp:94
void NewStringList(TSortedStringArray *strings)
Sets the list of valid input string for the string-lookup validator.
Definition lookval.cpp:106
A streamable class, TValidator defines an abstract data validation object.
Definition validate.h:71
TWindow, derived from TEventHandler and TStreamableBase, provides window-specific behavior and encaps...
Definition window.h:414
ipstream, a specialized input stream derivative of pstream, is the base class for reading (extracting...
Definition objstrm.h:391
Definition of class TFrameWindow.
void ReadBaseObject(Base *base, ipstream &in)
Definition objstrm.h:1159
#define IMPLEMENT_STREAMABLE1(cls, base1)
Definition objstrm.h:1725
void WriteBaseObject(Base *base, opstream &out)
Definition objstrm.h:1150
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
TSortedObjectArray< tstring > TSortedStringArray
Definition contain.h:39
unsigned long uint32
Definition number.h:34
OWL_DIAGINFO
Definition animctrl.cpp:14
std::string tstring
Definition defs.h:79