1 |
|
---|
2 | {*****************************************************************************}
|
---|
3 | { }
|
---|
4 | { Tnt Delphi Unicode Controls }
|
---|
5 | { http://www.tntware.com/delphicontrols/unicode/ }
|
---|
6 | { Version: 2.3.0 }
|
---|
7 | { }
|
---|
8 | { Copyright (c) 2002-2007, Troy Wolbrink (troy.wolbrink@tntware.com) }
|
---|
9 | { }
|
---|
10 | {*****************************************************************************}
|
---|
11 |
|
---|
12 | unit TntDB;
|
---|
13 |
|
---|
14 | {$INCLUDE TntCompilers.inc}
|
---|
15 |
|
---|
16 | interface
|
---|
17 |
|
---|
18 | uses
|
---|
19 | Classes, DB;
|
---|
20 |
|
---|
21 | type
|
---|
22 | {TNT-WARN TDateTimeField}
|
---|
23 | TTntDateTimeField = class(TDateTimeField{TNT-ALLOW TDateTimeField})
|
---|
24 | protected
|
---|
25 | procedure SetAsString(const Value: AnsiString); override;
|
---|
26 | end;
|
---|
27 |
|
---|
28 | {TNT-WARN TDateField}
|
---|
29 | TTntDateField = class(TDateField{TNT-ALLOW TDateField})
|
---|
30 | protected
|
---|
31 | procedure SetAsString(const Value: AnsiString); override;
|
---|
32 | end;
|
---|
33 |
|
---|
34 | {TNT-WARN TTimeField}
|
---|
35 | TTntTimeField = class(TTimeField{TNT-ALLOW TTimeField})
|
---|
36 | protected
|
---|
37 | procedure SetAsString(const Value: AnsiString); override;
|
---|
38 | end;
|
---|
39 |
|
---|
40 | TFieldGetWideTextEvent = procedure(Sender: TField; var Text: WideString;
|
---|
41 | DoDisplayText: Boolean) of object;
|
---|
42 | TFieldSetWideTextEvent = procedure(Sender: TField; const Text: WideString) of object;
|
---|
43 |
|
---|
44 | IWideStringField = interface
|
---|
45 | ['{679C5F1A-4356-4696-A8F3-9C7C6970A9F6}']
|
---|
46 | {$IFNDEF COMPILER_10_UP}
|
---|
47 | function GetAsWideString: WideString;
|
---|
48 | procedure SetAsWideString(const Value: WideString);
|
---|
49 | {$ENDIF}
|
---|
50 | function GetWideDisplayText: WideString;
|
---|
51 | function GetWideEditText: WideString;
|
---|
52 | procedure SetWideEditText(const Value: WideString);
|
---|
53 | //--
|
---|
54 | {$IFNDEF COMPILER_10_UP}
|
---|
55 | property AsWideString: WideString read GetAsWideString write SetAsWideString{inherited};
|
---|
56 | {$ENDIF}
|
---|
57 | property WideDisplayText: WideString read GetWideDisplayText;
|
---|
58 | property WideText: WideString read GetWideEditText write SetWideEditText;
|
---|
59 | end;
|
---|
60 |
|
---|
61 | {TNT-WARN TWideStringField}
|
---|
62 | TTntWideStringField = class(TWideStringField{TNT-ALLOW TWideStringField}, IWideStringField)
|
---|
63 | private
|
---|
64 | FOnGetText: TFieldGetWideTextEvent;
|
---|
65 | FOnSetText: TFieldSetWideTextEvent;
|
---|
66 | procedure SetOnGetText(const Value: TFieldGetWideTextEvent);
|
---|
67 | procedure SetOnSetText(const Value: TFieldSetWideTextEvent);
|
---|
68 | procedure LegacyGetText(Sender: TField; var AnsiText: AnsiString; DoDisplayText: Boolean);
|
---|
69 | procedure LegacySetText(Sender: TField; const AnsiText: AnsiString);
|
---|
70 | function GetWideDisplayText: WideString;
|
---|
71 | function GetWideEditText: WideString;
|
---|
72 | procedure SetWideEditText(const Value: WideString);
|
---|
73 | protected
|
---|
74 | {$IFNDEF COMPILER_10_UP}
|
---|
75 | function GetAsWideString: WideString;
|
---|
76 | {$ENDIF}
|
---|
77 | public
|
---|
78 | property Value: WideString read GetAsWideString write SetAsWideString;
|
---|
79 | property DisplayText{TNT-ALLOW DisplayText}: WideString read GetWideDisplayText;
|
---|
80 | property Text: WideString read GetWideEditText write SetWideEditText;
|
---|
81 | {$IFNDEF COMPILER_10_UP}
|
---|
82 | property AsWideString: WideString read GetAsWideString write SetAsWideString;
|
---|
83 | {$ENDIF}
|
---|
84 | property WideDisplayText: WideString read GetWideDisplayText;
|
---|
85 | property WideText: WideString read GetWideEditText write SetWideEditText;
|
---|
86 | published
|
---|
87 | property OnGetText: TFieldGetWideTextEvent read FOnGetText write SetOnGetText;
|
---|
88 | property OnSetText: TFieldSetWideTextEvent read FOnSetText write SetOnSetText;
|
---|
89 | end;
|
---|
90 |
|
---|
91 | TTntStringFieldEncodingMode = (emNone, emUTF8, emUTF7, emFixedCodePage, emFixedCodePage_Safe);
|
---|
92 |
|
---|
93 | //-------------------------------------------------------------------------------------------
|
---|
94 | // Comments on TTntStringFieldEncodingMode:
|
---|
95 | //
|
---|
96 | // emNone - Works like TStringField.
|
---|
97 | // emUTF8 - Should work well most databases.
|
---|
98 | // emUTF7 - Almost guaranteed to work with any database. Wasteful in database space.
|
---|
99 | // emFixedCodePage - Only tested it with Access 97, which doesn't support Unicode.
|
---|
100 | // emFixedCodePage_Safe - Like emFixedCodePage but uses char<=#128. Wasteful in database space.
|
---|
101 | //
|
---|
102 | // Only emUTF8 and emUTF7 fully support Unicode.
|
---|
103 | //-------------------------------------------------------------------------------------------
|
---|
104 |
|
---|
105 | TTntStringFieldCodePageEnum = (fcpOther,
|
---|
106 | fcpThai, fcpJapanese, fcpSimplifiedChinese, fcpTraditionalChinese, fcpKorean,
|
---|
107 | fcpCentralEuropean, fcpCyrillic, fcpLatinWestern, fcpGreek, fcpTurkish,
|
---|
108 | fcpHebrew, fcpArabic, fcpBaltic, fcpVietnamese);
|
---|
109 |
|
---|
110 | const
|
---|
111 | TntStringFieldCodePageEnumMap: array[TTntStringFieldCodePageEnum] of Word = (0,
|
---|
112 | 874, 932, 936, 950, 949,
|
---|
113 | 1250, 1251, 1252, 1253, 1254,
|
---|
114 | 1255, 1256, 1257, 1258);
|
---|
115 |
|
---|
116 | type
|
---|
117 | {TNT-WARN TStringField}
|
---|
118 | TTntStringField = class(TStringField{TNT-ALLOW TStringField},IWideStringField)
|
---|
119 | private
|
---|
120 | FOnGetText: TFieldGetWideTextEvent;
|
---|
121 | FOnSetText: TFieldSetWideTextEvent;
|
---|
122 | FEncodingMode: TTntStringFieldEncodingMode;
|
---|
123 | FFixedCodePage: Word;
|
---|
124 | FRawVariantAccess: Boolean;
|
---|
125 | procedure SetOnGetText(const Value: TFieldGetWideTextEvent);
|
---|
126 | procedure SetOnSetText(const Value: TFieldSetWideTextEvent);
|
---|
127 | procedure LegacyGetText(Sender: TField; var AnsiText: AnsiString; DoDisplayText: Boolean);
|
---|
128 | procedure LegacySetText(Sender: TField; const AnsiText: AnsiString);
|
---|
129 | function GetWideDisplayText: WideString;
|
---|
130 | function GetWideEditText: WideString;
|
---|
131 | procedure SetWideEditText(const Value: WideString);
|
---|
132 | function GetFixedCodePageEnum: TTntStringFieldCodePageEnum;
|
---|
133 | procedure SetFixedCodePageEnum(const Value: TTntStringFieldCodePageEnum);
|
---|
134 | function IsFixedCodePageStored: Boolean;
|
---|
135 | protected
|
---|
136 | {$IFDEF COMPILER_10_UP}
|
---|
137 | function GetAsWideString: WideString; override;
|
---|
138 | procedure SetAsWideString(const Value: WideString); override;
|
---|
139 | {$ELSE}
|
---|
140 | function GetAsWideString: WideString; virtual;
|
---|
141 | procedure SetAsWideString(const Value: WideString); virtual;
|
---|
142 | {$ENDIF}
|
---|
143 | function GetAsVariant: Variant; override;
|
---|
144 | procedure SetVarValue(const Value: Variant); override;
|
---|
145 | function GetAsString: string{TNT-ALLOW string}; override;
|
---|
146 | procedure SetAsString(const Value: string{TNT-ALLOW string}); override;
|
---|
147 | public
|
---|
148 | constructor Create(AOwner: TComponent); override;
|
---|
149 | property Value: WideString read GetAsWideString write SetAsWideString;
|
---|
150 | property DisplayText{TNT-ALLOW DisplayText}: WideString read GetWideDisplayText;
|
---|
151 | property Text: WideString read GetWideEditText write SetWideEditText;
|
---|
152 | {$IFNDEF COMPILER_10_UP}
|
---|
153 | property AsWideString: WideString read GetAsWideString write SetAsWideString;
|
---|
154 | {$ENDIF}
|
---|
155 | property WideDisplayText: WideString read GetWideDisplayText;
|
---|
156 | property WideText: WideString read GetWideEditText write SetWideEditText;
|
---|
157 | published
|
---|
158 | property EncodingMode: TTntStringFieldEncodingMode read FEncodingMode write FEncodingMode default emUTF8;
|
---|
159 | property FixedCodePageEnum: TTntStringFieldCodePageEnum read GetFixedCodePageEnum write SetFixedCodePageEnum stored False;
|
---|
160 | property FixedCodePage: Word read FFixedCodePage write FFixedCodePage stored IsFixedCodePageStored;
|
---|
161 | property RawVariantAccess: Boolean read FRawVariantAccess write FRawVariantAccess default False;
|
---|
162 | property OnGetText: TFieldGetWideTextEvent read FOnGetText write SetOnGetText;
|
---|
163 | property OnSetText: TFieldSetWideTextEvent read FOnSetText write SetOnSetText;
|
---|
164 | end;
|
---|
165 |
|
---|
166 | //======================
|
---|
167 | type
|
---|
168 | {TNT-WARN TMemoField}
|
---|
169 | TTntMemoField = class(TMemoField{TNT-ALLOW TMemoField}, IWideStringField)
|
---|
170 | private
|
---|
171 | FOnGetText: TFieldGetWideTextEvent;
|
---|
172 | FOnSetText: TFieldSetWideTextEvent;
|
---|
173 | FEncodingMode: TTntStringFieldEncodingMode;
|
---|
174 | FFixedCodePage: Word;
|
---|
175 | FRawVariantAccess: Boolean;
|
---|
176 | procedure SetOnGetText(const Value: TFieldGetWideTextEvent);
|
---|
177 | procedure SetOnSetText(const Value: TFieldSetWideTextEvent);
|
---|
178 | procedure LegacyGetText(Sender: TField; var AnsiText: AnsiString; DoDisplayText: Boolean);
|
---|
179 | procedure LegacySetText(Sender: TField; const AnsiText: AnsiString);
|
---|
180 | function GetWideDisplayText: WideString;
|
---|
181 | function GetWideEditText: WideString;
|
---|
182 | procedure SetWideEditText(const Value: WideString);
|
---|
183 | function GetFixedCodePageEnum: TTntStringFieldCodePageEnum;
|
---|
184 | procedure SetFixedCodePageEnum(const Value: TTntStringFieldCodePageEnum);
|
---|
185 | function IsFixedCodePageStored: Boolean;
|
---|
186 | protected
|
---|
187 | {$IFDEF COMPILER_10_UP}
|
---|
188 | function GetAsWideString: WideString; override;
|
---|
189 | procedure SetAsWideString(const Value: WideString); override;
|
---|
190 | {$ELSE}
|
---|
191 | function GetAsWideString: WideString; virtual;
|
---|
192 | procedure SetAsWideString(const Value: WideString); virtual;
|
---|
193 | {$ENDIF}
|
---|
194 | function GetAsVariant: Variant; override;
|
---|
195 | procedure SetVarValue(const Value: Variant); override;
|
---|
196 | function GetAsString: string{TNT-ALLOW string}; override;
|
---|
197 | procedure SetAsString(const Value: string{TNT-ALLOW string}); override;
|
---|
198 | public
|
---|
199 | constructor Create(AOwner: TComponent); override;
|
---|
200 | property Value: WideString read GetAsWideString write SetAsWideString;
|
---|
201 | property DisplayText{TNT-ALLOW DisplayText}: WideString read GetWideDisplayText;
|
---|
202 | property Text: WideString read GetWideEditText write SetWideEditText;
|
---|
203 | {$IFNDEF COMPILER_10_UP}
|
---|
204 | property AsWideString: WideString read GetAsWideString write SetAsWideString;
|
---|
205 | {$ENDIF}
|
---|
206 | property WideDisplayText: WideString read GetWideDisplayText;
|
---|
207 | property WideText: WideString read GetWideEditText write SetWideEditText;
|
---|
208 | published
|
---|
209 | property EncodingMode: TTntStringFieldEncodingMode read FEncodingMode write FEncodingMode default emUTF8;
|
---|
210 | property FixedCodePageEnum: TTntStringFieldCodePageEnum read GetFixedCodePageEnum write SetFixedCodePageEnum stored False;
|
---|
211 | property FixedCodePage: Word read FFixedCodePage write FFixedCodePage stored IsFixedCodePageStored;
|
---|
212 | property RawVariantAccess: Boolean read FRawVariantAccess write FRawVariantAccess default False;
|
---|
213 | property OnGetText: TFieldGetWideTextEvent read FOnGetText write SetOnGetText;
|
---|
214 | property OnSetText: TFieldSetWideTextEvent read FOnSetText write SetOnSetText;
|
---|
215 | end;
|
---|
216 |
|
---|
217 | //======================
|
---|
218 | function GetTntFieldClass(FieldClass: TFieldClass): TFieldClass;
|
---|
219 |
|
---|
220 | function GetWideDisplayName(Field: TField): WideString; deprecated; // for Unicode-enabled functionality, use Delphi 2006 or newer
|
---|
221 | function GetWideDisplayLabel(Field: TField): WideString; deprecated; // for Unicode-enabled functionality, use Delphi 2006 or newer
|
---|
222 | procedure SetWideDisplayLabel(Field: TField; const Value: WideString); deprecated; // for Unicode-enabled functionality, use Delphi 2006 or newer
|
---|
223 |
|
---|
224 | {TNT-WARN AsString}
|
---|
225 | {TNT-WARN DisplayText}
|
---|
226 |
|
---|
227 | function GetAsWideString(Field: TField): WideString;
|
---|
228 | procedure SetAsWideString(Field: TField; const Value: WideString);
|
---|
229 |
|
---|
230 | function GetWideDisplayText(Field: TField): WideString;
|
---|
231 |
|
---|
232 | function GetWideText(Field: TField): WideString;
|
---|
233 | procedure SetWideText(Field: TField; const Value: WideString);
|
---|
234 |
|
---|
235 | procedure RegisterTntFields;
|
---|
236 |
|
---|
237 | { TTntWideStringField / TTntStringField common handlers }
|
---|
238 | procedure TntWideStringField_GetWideText(Field: TField;
|
---|
239 | var Text: WideString; DoDisplayText: Boolean);
|
---|
240 | function TntWideStringField_GetWideDisplayText(Field: TField;
|
---|
241 | OnGetText: TFieldGetWideTextEvent): WideString;
|
---|
242 | function TntWideStringField_GetWideEditText(Field: TField;
|
---|
243 | OnGetText: TFieldGetWideTextEvent): WideString;
|
---|
244 | procedure TntWideStringField_SetWideText(Field: TField;
|
---|
245 | const Value: WideString);
|
---|
246 | procedure TntWideStringField_SetWideEditText(Field: TField;
|
---|
247 | OnSetText: TFieldSetWideTextEvent; const Value: WideString);
|
---|
248 |
|
---|
249 |
|
---|
250 | implementation
|
---|
251 |
|
---|
252 | uses
|
---|
253 | SysUtils, MaskUtils, Variants, Contnrs, TntSystem, TntSysUtils;
|
---|
254 |
|
---|
255 | function GetTntFieldClass(FieldClass: TFieldClass): TFieldClass;
|
---|
256 | begin
|
---|
257 | if FieldClass = TDateTimeField{TNT-ALLOW TDateTimeField} then
|
---|
258 | Result := TTntDateTimeField
|
---|
259 | else if FieldClass = TDateField{TNT-ALLOW TDateField} then
|
---|
260 | Result := TTntDateField
|
---|
261 | else if FieldClass = TTimeField{TNT-ALLOW TTimeField} then
|
---|
262 | Result := TTntTimeField
|
---|
263 | else if FieldClass = TWideStringField{TNT-ALLOW TWideStringField} then
|
---|
264 | Result := TTntWideStringField
|
---|
265 | else if FieldClass = TStringField{TNT-ALLOW TStringField} then
|
---|
266 | Result := TTntStringField
|
---|
267 | else
|
---|
268 | Result := FieldClass;
|
---|
269 | end;
|
---|
270 |
|
---|
271 | function GetWideDisplayName(Field: TField): WideString;
|
---|
272 | begin
|
---|
273 | Result := Field.DisplayName;
|
---|
274 | end;
|
---|
275 |
|
---|
276 | function GetWideDisplayLabel(Field: TField): WideString;
|
---|
277 | begin
|
---|
278 | Result := Field.DisplayLabel;
|
---|
279 | end;
|
---|
280 |
|
---|
281 | procedure SetWideDisplayLabel(Field: TField; const Value: WideString);
|
---|
282 | begin
|
---|
283 | Field.DisplayLabel := Value;
|
---|
284 | end;
|
---|
285 |
|
---|
286 | function GetAsWideString(Field: TField): WideString;
|
---|
287 | {$IFDEF COMPILER_10_UP}
|
---|
288 | begin
|
---|
289 | if (Field.ClassType = TMemoField{TNT-ALLOW TMemoField}) then
|
---|
290 | Result := VarToWideStr(Field.AsVariant) { works for NexusDB BLOB Wide }
|
---|
291 | else
|
---|
292 | Result := Field.AsWideString
|
---|
293 | end;
|
---|
294 | {$ELSE}
|
---|
295 | var
|
---|
296 | WideField: IWideStringField;
|
---|
297 | begin
|
---|
298 | if Field.GetInterface(IWideStringField, WideField) then
|
---|
299 | Result := WideField.AsWideString
|
---|
300 | else if (Field is TWideStringField{TNT-ALLOW TWideStringField}) then
|
---|
301 | begin
|
---|
302 | if Field.IsNull then
|
---|
303 | // This fixes a bug in TWideStringField.GetAsWideString which does not handle Null at all.
|
---|
304 | Result := ''
|
---|
305 | else
|
---|
306 | Result := TWideStringField{TNT-ALLOW TWideStringField}(Field).Value
|
---|
307 | end else if (Field is TMemoField{TNT-ALLOW TMemoField}) then
|
---|
308 | Result := VarToWideStr(Field.AsVariant) { works for NexusDB BLOB Wide }
|
---|
309 | else
|
---|
310 | Result := Field.AsString{TNT-ALLOW AsString};
|
---|
311 | end;
|
---|
312 | {$ENDIF}
|
---|
313 |
|
---|
314 | procedure SetAsWideString(Field: TField; const Value: WideString);
|
---|
315 | {$IFDEF COMPILER_10_UP}
|
---|
316 | begin
|
---|
317 | if (Field.ClassType = TMemoField{TNT-ALLOW TMemoField}) then
|
---|
318 | Field.AsVariant := Value { works for NexusDB BLOB Wide }
|
---|
319 | else
|
---|
320 | Field.AsWideString := Value;
|
---|
321 | end;
|
---|
322 | {$ELSE}
|
---|
323 | var
|
---|
324 | WideField: IWideStringField;
|
---|
325 | begin
|
---|
326 | if Field.GetInterface(IWideStringField, WideField) then
|
---|
327 | WideField.AsWideString := Value
|
---|
328 | else if (Field is TWideStringField{TNT-ALLOW TWideStringField}) then
|
---|
329 | TWideStringField{TNT-ALLOW TWideStringField}(Field).Value := Value
|
---|
330 | else if (Field is TMemoField{TNT-ALLOW TMemoField}) then
|
---|
331 | Field.AsVariant := Value { works for NexusDB BLOB Wide }
|
---|
332 | else
|
---|
333 | Field.AsString{TNT-ALLOW AsString} := Value;
|
---|
334 | end;
|
---|
335 | {$ENDIF}
|
---|
336 |
|
---|
337 | function GetWideDisplayText(Field: TField): WideString;
|
---|
338 | var
|
---|
339 | WideField: IWideStringField;
|
---|
340 | begin
|
---|
341 | if Field.GetInterface(IWideStringField, WideField) then
|
---|
342 | Result := WideField.WideDisplayText
|
---|
343 | else if (Field is TWideStringField{TNT-ALLOW TWideStringField})
|
---|
344 | and (not Assigned(Field.OnGetText)) then
|
---|
345 | Result := GetAsWideString(Field)
|
---|
346 | else
|
---|
347 | Result := Field.DisplayText{TNT-ALLOW DisplayText};
|
---|
348 | end;
|
---|
349 |
|
---|
350 | function GetWideText(Field: TField): WideString;
|
---|
351 | var
|
---|
352 | WideField: IWideStringField;
|
---|
353 | begin
|
---|
354 | if Field.GetInterface(IWideStringField, WideField) then
|
---|
355 | Result := WideField.WideText
|
---|
356 | else if (Field is TWideStringField{TNT-ALLOW TWideStringField})
|
---|
357 | and (not Assigned(Field.OnGetText)) then
|
---|
358 | Result := GetAsWideString(Field)
|
---|
359 | else
|
---|
360 | Result := Field.Text;
|
---|
361 | end;
|
---|
362 |
|
---|
363 | procedure SetWideText(Field: TField; const Value: WideString);
|
---|
364 | var
|
---|
365 | WideField: IWideStringField;
|
---|
366 | begin
|
---|
367 | if Field.GetInterface(IWideStringField, WideField) then
|
---|
368 | WideField.WideText := Value
|
---|
369 | else if (Field is TWideStringField{TNT-ALLOW TWideStringField})
|
---|
370 | and (not Assigned(Field.OnSetText)) then
|
---|
371 | SetAsWideString(Field, Value)
|
---|
372 | else
|
---|
373 | Field.Text := Value
|
---|
374 | end;
|
---|
375 |
|
---|
376 | { TTntDateTimeField }
|
---|
377 |
|
---|
378 | procedure TTntDateTimeField.SetAsString(const Value: AnsiString);
|
---|
379 | begin
|
---|
380 | if Value = '' then
|
---|
381 | inherited
|
---|
382 | else
|
---|
383 | SetAsDateTime(TntStrToDateTime(Value));
|
---|
384 | end;
|
---|
385 |
|
---|
386 | { TTntDateField }
|
---|
387 |
|
---|
388 | procedure TTntDateField.SetAsString(const Value: AnsiString);
|
---|
389 | begin
|
---|
390 | if Value = '' then
|
---|
391 | inherited
|
---|
392 | else
|
---|
393 | SetAsDateTime(TntStrToDate(Value));
|
---|
394 | end;
|
---|
395 |
|
---|
396 | { TTntTimeField }
|
---|
397 |
|
---|
398 | procedure TTntTimeField.SetAsString(const Value: AnsiString);
|
---|
399 | begin
|
---|
400 | if Value = '' then
|
---|
401 | inherited
|
---|
402 | else
|
---|
403 | SetAsDateTime(TntStrToTime(Value));
|
---|
404 | end;
|
---|
405 |
|
---|
406 | { TTntWideStringField / TTntStringField common handlers }
|
---|
407 |
|
---|
408 | procedure TntWideStringField_LegacyGetText(Sender: TField; OnGetText: TFieldGetWideTextEvent;
|
---|
409 | var AnsiText: AnsiString; DoDisplayText: Boolean);
|
---|
410 | var
|
---|
411 | WideText: WideString;
|
---|
412 | begin
|
---|
413 | if Assigned(OnGetText) then begin
|
---|
414 | WideText := AnsiText;
|
---|
415 | OnGetText(Sender, WideText, DoDisplayText);
|
---|
416 | AnsiText := WideText;
|
---|
417 | end;
|
---|
418 | end;
|
---|
419 |
|
---|
420 | procedure TntWideStringField_LegacySetText(Sender: TField; OnSetText: TFieldSetWideTextEvent;
|
---|
421 | const AnsiText: AnsiString);
|
---|
422 | begin
|
---|
423 | if Assigned(OnSetText) then
|
---|
424 | OnSetText(Sender, AnsiText);
|
---|
425 | end;
|
---|
426 |
|
---|
427 | procedure TntWideStringField_GetWideText(Field: TField;
|
---|
428 | var Text: WideString; DoDisplayText: Boolean);
|
---|
429 | var
|
---|
430 | WideStringField: IWideStringField;
|
---|
431 | begin
|
---|
432 | Field.GetInterface(IWideStringField, WideStringField);
|
---|
433 | Assert(WideStringField <> nil);
|
---|
434 | if DoDisplayText and (Field.EditMaskPtr <> '') then
|
---|
435 | { to gain the mask, we lose Unicode! }
|
---|
436 | Text := FormatMaskText(Field.EditMaskPtr, GetAsWideString(Field))
|
---|
437 | else
|
---|
438 | Text := GetAsWideString(Field);
|
---|
439 | end;
|
---|
440 |
|
---|
441 | function TntWideStringField_GetWideDisplayText(Field: TField;
|
---|
442 | OnGetText: TFieldGetWideTextEvent): WideString;
|
---|
443 | begin
|
---|
444 | Result := '';
|
---|
445 | if Assigned(OnGetText) then
|
---|
446 | OnGetText(Field, Result, True)
|
---|
447 | else if Assigned(Field.OnGetText) then
|
---|
448 | Result := Field.DisplayText{TNT-ALLOW DisplayText} {we lose Unicode to handle this event}
|
---|
449 | else
|
---|
450 | TntWideStringField_GetWideText(Field, Result, True);
|
---|
451 | end;
|
---|
452 |
|
---|
453 | function TntWideStringField_GetWideEditText(Field: TField;
|
---|
454 | OnGetText: TFieldGetWideTextEvent): WideString;
|
---|
455 | begin
|
---|
456 | Result := '';
|
---|
457 | if Assigned(OnGetText) then
|
---|
458 | OnGetText(Field, Result, False)
|
---|
459 | else if Assigned(Field.OnGetText) then
|
---|
460 | Result := Field.Text {we lose Unicode to handle this event}
|
---|
461 | else
|
---|
462 | TntWideStringField_GetWideText(Field, Result, False);
|
---|
463 | end;
|
---|
464 |
|
---|
465 | procedure TntWideStringField_SetWideText(Field: TField;
|
---|
466 | const Value: WideString);
|
---|
467 | {$IFDEF COMPILER_10_UP}
|
---|
468 | begin
|
---|
469 | Field.AsWideString := Value;
|
---|
470 | end;
|
---|
471 | {$ELSE}
|
---|
472 | var
|
---|
473 | WideStringField: IWideStringField;
|
---|
474 | begin
|
---|
475 | Field.GetInterface(IWideStringField, WideStringField);
|
---|
476 | Assert(WideStringField <> nil);
|
---|
477 | WideStringField.SetAsWideString(Value);
|
---|
478 | end;
|
---|
479 | {$ENDIF}
|
---|
480 |
|
---|
481 | procedure TntWideStringField_SetWideEditText(Field: TField;
|
---|
482 | OnSetText: TFieldSetWideTextEvent; const Value: WideString);
|
---|
483 | begin
|
---|
484 | if Assigned(OnSetText) then
|
---|
485 | OnSetText(Field, Value)
|
---|
486 | else if Assigned(Field.OnSetText) then
|
---|
487 | Field.Text := Value {we lose Unicode to handle this event}
|
---|
488 | else
|
---|
489 | TntWideStringField_SetWideText(Field, Value);
|
---|
490 | end;
|
---|
491 |
|
---|
492 | { TTntWideStringField }
|
---|
493 |
|
---|
494 | {$IFNDEF COMPILER_10_UP}
|
---|
495 | function TTntWideStringField.GetAsWideString: WideString;
|
---|
496 | begin
|
---|
497 | if not GetData(@Result, False) then
|
---|
498 | Result := ''; {fixes a bug in inherited which has unpredictable results for NULL}
|
---|
499 | end;
|
---|
500 | {$ENDIF}
|
---|
501 |
|
---|
502 | procedure TTntWideStringField.LegacyGetText(Sender: TField; var AnsiText: AnsiString;
|
---|
503 | DoDisplayText: Boolean);
|
---|
504 | begin
|
---|
505 | TntWideStringField_LegacyGetText(Sender, OnGetText, AnsiText, DoDisplayText);
|
---|
506 | end;
|
---|
507 |
|
---|
508 | procedure TTntWideStringField.LegacySetText(Sender: TField; const AnsiText: AnsiString);
|
---|
509 | begin
|
---|
510 | TntWideStringField_LegacySetText(Sender, OnSetText, AnsiText);
|
---|
511 | end;
|
---|
512 |
|
---|
513 | procedure TTntWideStringField.SetOnGetText(const Value: TFieldGetWideTextEvent);
|
---|
514 | begin
|
---|
515 | FOnGetText := Value;
|
---|
516 | if Assigned(OnGetText) then
|
---|
517 | inherited OnGetText := LegacyGetText
|
---|
518 | else
|
---|
519 | inherited OnGetText := nil;
|
---|
520 | end;
|
---|
521 |
|
---|
522 | procedure TTntWideStringField.SetOnSetText(const Value: TFieldSetWideTextEvent);
|
---|
523 | begin
|
---|
524 | FOnSetText := Value;
|
---|
525 | if Assigned(OnSetText) then
|
---|
526 | inherited OnSetText := LegacySetText
|
---|
527 | else
|
---|
528 | inherited OnSetText := nil;
|
---|
529 | end;
|
---|
530 |
|
---|
531 | function TTntWideStringField.GetWideDisplayText: WideString;
|
---|
532 | begin
|
---|
533 | Result := TntWideStringField_GetWideDisplayText(Self, OnGetText);
|
---|
534 | end;
|
---|
535 |
|
---|
536 | function TTntWideStringField.GetWideEditText: WideString;
|
---|
537 | begin
|
---|
538 | Result := TntWideStringField_GetWideEditText(Self, OnGetText);
|
---|
539 | end;
|
---|
540 |
|
---|
541 | procedure TTntWideStringField.SetWideEditText(const Value: WideString);
|
---|
542 | begin
|
---|
543 | TntWideStringField_SetWideEditText(Self, OnSetText, Value);
|
---|
544 | end;
|
---|
545 |
|
---|
546 | (* This stuffing method works with CJK codepages - intended to store accented characters in Access 97 *)
|
---|
547 |
|
---|
548 | function SafeStringToWideStringEx(const S: AnsiString; Codepage: Word): WideString;
|
---|
549 | var
|
---|
550 | R: AnsiString;
|
---|
551 | i: Integer;
|
---|
552 | begin
|
---|
553 | R := '';
|
---|
554 | i := 1;
|
---|
555 | while i <= Length(S) do
|
---|
556 | begin
|
---|
557 | if (S[i] = #128) then
|
---|
558 | begin
|
---|
559 | Inc(i);
|
---|
560 | if S[i] = #128 then
|
---|
561 | R := R + #128
|
---|
562 | else
|
---|
563 | R := R + Chr(Ord(S[i]) + 128);
|
---|
564 | Inc(i);
|
---|
565 | end
|
---|
566 | else
|
---|
567 | begin
|
---|
568 | R := R + S[I];
|
---|
569 | Inc(i);
|
---|
570 | end;
|
---|
571 | end;
|
---|
572 | Result := StringToWideStringEx(R, CodePage);
|
---|
573 | end;
|
---|
574 |
|
---|
575 | function SafeWideStringToStringEx(const W: WideString; CodePage: Word): AnsiString;
|
---|
576 | var
|
---|
577 | TempS: AnsiString;
|
---|
578 | i: integer;
|
---|
579 | begin
|
---|
580 | TempS := WideStringToStringEx(W, CodePage);
|
---|
581 | Result := '';
|
---|
582 | for i := 1 to Length(TempS) do
|
---|
583 | begin
|
---|
584 | if TempS[i] > #128 then
|
---|
585 | Result := Result + #128 + Chr(Ord(TempS[i]) - 128)
|
---|
586 | else if TempS[i] = #128 then
|
---|
587 | Result := Result + #128 + #128
|
---|
588 | else
|
---|
589 | Result := Result + TempS[i];
|
---|
590 | end;
|
---|
591 | end;
|
---|
592 |
|
---|
593 | { TTntStringField }
|
---|
594 |
|
---|
595 | constructor TTntStringField.Create(AOwner: TComponent);
|
---|
596 | begin
|
---|
597 | inherited;
|
---|
598 | FEncodingMode := emUTF8;
|
---|
599 | FFixedCodePage := TntStringFieldCodePageEnumMap[fcpLatinWestern]
|
---|
600 | end;
|
---|
601 |
|
---|
602 | function TTntStringField.GetFixedCodePageEnum: TTntStringFieldCodePageEnum;
|
---|
603 | var
|
---|
604 | i: TTntStringFieldCodePageEnum;
|
---|
605 | begin
|
---|
606 | Result := fcpOther;
|
---|
607 | for i := Low(TntStringFieldCodePageEnumMap) to High(TntStringFieldCodePageEnumMap) do begin
|
---|
608 | if TntStringFieldCodePageEnumMap[i] = FixedCodePage then begin
|
---|
609 | Result := i;
|
---|
610 | Break; {found it}
|
---|
611 | end;
|
---|
612 | end;
|
---|
613 | end;
|
---|
614 |
|
---|
615 | procedure TTntStringField.SetFixedCodePageEnum(const Value: TTntStringFieldCodePageEnum);
|
---|
616 | begin
|
---|
617 | if (Value <> fcpOther) then
|
---|
618 | FixedCodePage := TntStringFieldCodePageEnumMap[Value];
|
---|
619 | end;
|
---|
620 |
|
---|
621 | function TTntStringField.GetAsVariant: Variant;
|
---|
622 | begin
|
---|
623 | if RawVariantAccess then
|
---|
624 | Result := inherited GetAsVariant
|
---|
625 | else if IsNull then
|
---|
626 | Result := Null
|
---|
627 | else
|
---|
628 | Result := GetAsWideString;
|
---|
629 | end;
|
---|
630 |
|
---|
631 | procedure TTntStringField.SetVarValue(const Value: Variant);
|
---|
632 | begin
|
---|
633 | if RawVariantAccess then
|
---|
634 | inherited
|
---|
635 | else
|
---|
636 | SetAsWideString(Value);
|
---|
637 | end;
|
---|
638 |
|
---|
639 | function TTntStringField.GetAsWideString: WideString;
|
---|
640 | begin
|
---|
641 | case EncodingMode of
|
---|
642 | emNone: Result := (inherited GetAsString);
|
---|
643 | emUTF8: Result := UTF8ToWideString(inherited GetAsString);
|
---|
644 | emUTF7: try
|
---|
645 | Result := UTF7ToWideString(inherited GetAsString);
|
---|
646 | except
|
---|
647 | Result := inherited GetAsString;
|
---|
648 | end;
|
---|
649 | emFixedCodePage: Result := StringToWideStringEx(inherited GetAsString, FixedCodePage);
|
---|
650 | emFixedCodePage_Safe: Result := SafeStringToWideStringEx(inherited GetAsString, FixedCodePage);
|
---|
651 | else
|
---|
652 | raise ETntInternalError.Create('Internal Error: Unexpected EncodingMode');
|
---|
653 | end;
|
---|
654 | end;
|
---|
655 |
|
---|
656 | procedure TTntStringField.SetAsWideString(const Value: WideString);
|
---|
657 | begin
|
---|
658 | case EncodingMode of
|
---|
659 | emNone: inherited SetAsString(Value);
|
---|
660 | emUTF8: inherited SetAsString(WideStringToUTF8(Value));
|
---|
661 | emUTF7: inherited SetAsString(WideStringToUTF7(Value));
|
---|
662 | emFixedCodePage: inherited SetAsString(WideStringToStringEx(Value, FixedCodePage));
|
---|
663 | emFixedCodePage_Safe: inherited SetAsString(SafeWideStringToStringEx(Value, FixedCodePage));
|
---|
664 | else
|
---|
665 | raise ETntInternalError.Create('Internal Error: Unexpected EncodingMode');
|
---|
666 | end;
|
---|
667 | end;
|
---|
668 |
|
---|
669 | function TTntStringField.GetAsString: string{TNT-ALLOW string};
|
---|
670 | begin
|
---|
671 | if EncodingMode = emNone then
|
---|
672 | Result := inherited GetAsString
|
---|
673 | else
|
---|
674 | Result := GetAsWideString;
|
---|
675 | end;
|
---|
676 |
|
---|
677 | procedure TTntStringField.SetAsString(const Value: string{TNT-ALLOW string});
|
---|
678 | begin
|
---|
679 | if EncodingMode = emNone then
|
---|
680 | inherited SetAsString(Value)
|
---|
681 | else
|
---|
682 | SetAsWideString(Value);
|
---|
683 | end;
|
---|
684 |
|
---|
685 | procedure TTntStringField.LegacyGetText(Sender: TField; var AnsiText: AnsiString;
|
---|
686 | DoDisplayText: Boolean);
|
---|
687 | begin
|
---|
688 | TntWideStringField_LegacyGetText(Sender, OnGetText, AnsiText, DoDisplayText);
|
---|
689 | end;
|
---|
690 |
|
---|
691 | procedure TTntStringField.LegacySetText(Sender: TField; const AnsiText: AnsiString);
|
---|
692 | begin
|
---|
693 | TntWideStringField_LegacySetText(Sender, OnSetText, AnsiText);
|
---|
694 | end;
|
---|
695 |
|
---|
696 | procedure TTntStringField.SetOnGetText(const Value: TFieldGetWideTextEvent);
|
---|
697 | begin
|
---|
698 | FOnGetText := Value;
|
---|
699 | if Assigned(OnGetText) then
|
---|
700 | inherited OnGetText := LegacyGetText
|
---|
701 | else
|
---|
702 | inherited OnGetText := nil;
|
---|
703 | end;
|
---|
704 |
|
---|
705 | procedure TTntStringField.SetOnSetText(const Value: TFieldSetWideTextEvent);
|
---|
706 | begin
|
---|
707 | FOnSetText := Value;
|
---|
708 | if Assigned(OnSetText) then
|
---|
709 | inherited OnSetText := LegacySetText
|
---|
710 | else
|
---|
711 | inherited OnSetText := nil;
|
---|
712 | end;
|
---|
713 |
|
---|
714 | function TTntStringField.GetWideDisplayText: WideString;
|
---|
715 | begin
|
---|
716 | Result := TntWideStringField_GetWideDisplayText(Self, OnGetText)
|
---|
717 | end;
|
---|
718 |
|
---|
719 | function TTntStringField.GetWideEditText: WideString;
|
---|
720 | begin
|
---|
721 | Result := TntWideStringField_GetWideEditText(Self, OnGetText);
|
---|
722 | end;
|
---|
723 |
|
---|
724 | procedure TTntStringField.SetWideEditText(const Value: WideString);
|
---|
725 | begin
|
---|
726 | TntWideStringField_SetWideEditText(Self, OnSetText, Value);
|
---|
727 | end;
|
---|
728 |
|
---|
729 | function TTntStringField.IsFixedCodePageStored: Boolean;
|
---|
730 | begin
|
---|
731 | Result := EncodingMode = emFixedCodePage;
|
---|
732 | end;
|
---|
733 |
|
---|
734 | //---------------------------------------------------------------------------------------------
|
---|
735 | { TTntMemoField }
|
---|
736 |
|
---|
737 | constructor TTntMemoField.Create(AOwner: TComponent);
|
---|
738 | begin
|
---|
739 | inherited;
|
---|
740 | FEncodingMode := emUTF8;
|
---|
741 | FFixedCodePage := TntStringFieldCodePageEnumMap[fcpLatinWestern]
|
---|
742 | end;
|
---|
743 |
|
---|
744 | function TTntMemoField.GetFixedCodePageEnum: TTntStringFieldCodePageEnum;
|
---|
745 | var
|
---|
746 | i: TTntStringFieldCodePageEnum;
|
---|
747 | begin
|
---|
748 | Result := fcpOther;
|
---|
749 | for i := Low(TntStringFieldCodePageEnumMap) to High(TntStringFieldCodePageEnumMap) do begin
|
---|
750 | if TntStringFieldCodePageEnumMap[i] = FixedCodePage then begin
|
---|
751 | Result := i;
|
---|
752 | Break; {found it}
|
---|
753 | end;
|
---|
754 | end;
|
---|
755 | end;
|
---|
756 |
|
---|
757 | procedure TTntMemoField.SetFixedCodePageEnum(const Value: TTntStringFieldCodePageEnum);
|
---|
758 | begin
|
---|
759 | if (Value <> fcpOther) then
|
---|
760 | FixedCodePage := TntStringFieldCodePageEnumMap[Value];
|
---|
761 | end;
|
---|
762 |
|
---|
763 | function TTntMemoField.GetAsVariant: Variant;
|
---|
764 | begin
|
---|
765 | if RawVariantAccess then
|
---|
766 | Result := inherited GetAsVariant
|
---|
767 | else if IsNull then
|
---|
768 | Result := Null
|
---|
769 | else
|
---|
770 | Result := GetAsWideString;
|
---|
771 | end;
|
---|
772 |
|
---|
773 | procedure TTntMemoField.SetVarValue(const Value: Variant);
|
---|
774 | begin
|
---|
775 | if RawVariantAccess then
|
---|
776 | inherited
|
---|
777 | else
|
---|
778 | SetAsWideString(Value);
|
---|
779 | end;
|
---|
780 |
|
---|
781 | function TTntMemoField.GetAsWideString: WideString;
|
---|
782 | begin
|
---|
783 | case EncodingMode of
|
---|
784 | emNone: Result := (inherited GetAsString);
|
---|
785 | emUTF8: Result := UTF8ToWideString(inherited GetAsString);
|
---|
786 | emUTF7: try
|
---|
787 | Result := UTF7ToWideString(inherited GetAsString);
|
---|
788 | except
|
---|
789 | Result := inherited GetAsString;
|
---|
790 | end;
|
---|
791 | emFixedCodePage: Result := StringToWideStringEx(inherited GetAsString, FixedCodePage);
|
---|
792 | emFixedCodePage_Safe: Result := SafeStringToWideStringEx(inherited GetAsString, FixedCodePage);
|
---|
793 | else
|
---|
794 | raise ETntInternalError.Create('Internal Error: Unexpected EncodingMode');
|
---|
795 | end;
|
---|
796 | end;
|
---|
797 |
|
---|
798 | procedure TTntMemoField.SetAsWideString(const Value: WideString);
|
---|
799 | begin
|
---|
800 | case EncodingMode of
|
---|
801 | emNone: inherited SetAsString(Value);
|
---|
802 | emUTF8: inherited SetAsString(WideStringToUTF8(Value));
|
---|
803 | emUTF7: inherited SetAsString(WideStringToUTF7(Value));
|
---|
804 | emFixedCodePage: inherited SetAsString(WideStringToStringEx(Value, FixedCodePage));
|
---|
805 | emFixedCodePage_Safe: inherited SetAsString(SafeWideStringToStringEx(Value, FixedCodePage));
|
---|
806 | else
|
---|
807 | raise ETntInternalError.Create('Internal Error: Unexpected EncodingMode');
|
---|
808 | end;
|
---|
809 | end;
|
---|
810 |
|
---|
811 | function TTntMemoField.GetAsString: string{TNT-ALLOW string};
|
---|
812 | begin
|
---|
813 | if EncodingMode = emNone then
|
---|
814 | Result := inherited GetAsString
|
---|
815 | else
|
---|
816 | Result := GetAsWideString;
|
---|
817 | end;
|
---|
818 |
|
---|
819 | procedure TTntMemoField.SetAsString(const Value: string{TNT-ALLOW string});
|
---|
820 | begin
|
---|
821 | if EncodingMode = emNone then
|
---|
822 | inherited SetAsString(Value)
|
---|
823 | else
|
---|
824 | SetAsWideString(Value);
|
---|
825 | end;
|
---|
826 |
|
---|
827 | procedure TTntMemoField.LegacyGetText(Sender: TField; var AnsiText: AnsiString;
|
---|
828 | DoDisplayText: Boolean);
|
---|
829 | begin
|
---|
830 | TntWideStringField_LegacyGetText(Sender, OnGetText, AnsiText, DoDisplayText);
|
---|
831 | end;
|
---|
832 |
|
---|
833 | procedure TTntMemoField.LegacySetText(Sender: TField; const AnsiText: AnsiString);
|
---|
834 | begin
|
---|
835 | TntWideStringField_LegacySetText(Sender, OnSetText, AnsiText);
|
---|
836 | end;
|
---|
837 |
|
---|
838 | procedure TTntMemoField.SetOnGetText(const Value: TFieldGetWideTextEvent);
|
---|
839 | begin
|
---|
840 | FOnGetText := Value;
|
---|
841 | if Assigned(OnGetText) then
|
---|
842 | inherited OnGetText := LegacyGetText
|
---|
843 | else
|
---|
844 | inherited OnGetText := nil;
|
---|
845 | end;
|
---|
846 |
|
---|
847 | procedure TTntMemoField.SetOnSetText(const Value: TFieldSetWideTextEvent);
|
---|
848 | begin
|
---|
849 | FOnSetText := Value;
|
---|
850 | if Assigned(OnSetText) then
|
---|
851 | inherited OnSetText := LegacySetText
|
---|
852 | else
|
---|
853 | inherited OnSetText := nil;
|
---|
854 | end;
|
---|
855 |
|
---|
856 | function TTntMemoField.GetWideDisplayText: WideString;
|
---|
857 | begin
|
---|
858 | Result := TntWideStringField_GetWideDisplayText(Self, OnGetText)
|
---|
859 | end;
|
---|
860 |
|
---|
861 | function TTntMemoField.GetWideEditText: WideString;
|
---|
862 | begin
|
---|
863 | Result := TntWideStringField_GetWideEditText(Self, OnGetText);
|
---|
864 | end;
|
---|
865 |
|
---|
866 | procedure TTntMemoField.SetWideEditText(const Value: WideString);
|
---|
867 | begin
|
---|
868 | TntWideStringField_SetWideEditText(Self, OnSetText, Value);
|
---|
869 | end;
|
---|
870 |
|
---|
871 | function TTntMemoField.IsFixedCodePageStored: Boolean;
|
---|
872 | begin
|
---|
873 | Result := EncodingMode = emFixedCodePage;
|
---|
874 | end;
|
---|
875 | //==================================================================
|
---|
876 | procedure RegisterTntFields;
|
---|
877 | begin
|
---|
878 | RegisterFields([TTntDateTimeField]);
|
---|
879 | RegisterFields([TTntDateField]);
|
---|
880 | RegisterFields([TTntTimeField]);
|
---|
881 | RegisterFields([TTntWideStringField]);
|
---|
882 | RegisterFields([TTntStringField]);
|
---|
883 | RegisterFields([TTntMemoField]);
|
---|
884 | end;
|
---|
885 |
|
---|
886 | type PFieldClass = ^TFieldClass;
|
---|
887 |
|
---|
888 | initialization
|
---|
889 | {$IFDEF TNT_FIELDS}
|
---|
890 | PFieldClass(@DefaultFieldClasses[ftDate])^ := TTntDateField;
|
---|
891 | PFieldClass(@DefaultFieldClasses[ftTime])^ := TTntTimeField;
|
---|
892 | PFieldClass(@DefaultFieldClasses[ftDateTime])^ := TTntDateTimeField;
|
---|
893 | PFieldClass(@DefaultFieldClasses[ftWideString])^ := TTntWideStringField;
|
---|
894 | PFieldClass(@DefaultFieldClasses[ftString])^ := TTntStringField;
|
---|
895 | PFieldClass(@DefaultFieldClasses[ftFixedChar])^ := TTntStringField;
|
---|
896 | {$ENDIF}
|
---|
897 |
|
---|
898 | finalization
|
---|
899 |
|
---|
900 | end.
|
---|