1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
|
{
fpGUI IDE - Maximus
Copyright (C) 2012 - 2013 Graeme Geldenhuys
See the file COPYING.modifiedLGPL, included in this distribution,
for details about redistributing fpGUI.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Description:
---
}
unit ideutils;
{$mode objfpc}{$H+}
interface
uses
Classes
,SysUtils
,fpg_base
,fpg_widget
;
// Set the Screen.Cursor to crHourGlass until the interface goes out of scope
function TempHourGlassCursor(var AWidget: TfpgWidget): IInterface;
function tiNumToken(const AValue, AToken: string): integer;
function tiToken(const AValue, AToken: string; const APos: integer): string;
procedure ShowString(const AString: TfpgString; const AHeading: TfpgString);
// Extract the file extension from FileName and return it in all UPPERCASE.
function ExtractUpperFileExt(const FileName: string): string;
// Transforms all consecutive sequences of #10, #13, #32, and #9 in Str
// into a single space, and strips off whitespace at the beginning and
// end of the string
function CompressWhiteSpace(const Str: string): string;
// See if a string begins/ends with a specific substring
function StrBeginsWith(const SubStr, Str: string; CaseSensitive: Boolean = True): Boolean;
function StrEndsWith(const SubStr, Str: string; CaseSensitive: Boolean = True): Boolean;
// See is a string contains another substring
function StrContains(const SubStr, Str: string; CaseSensitive: Boolean = True): Boolean;
// Find SubString in S; do not consider case;
// this works exactly the same as the Pos function,
// except for case-INsensitivity.
function CaseInsensitivePos(Pat, Text: PChar): Integer; overload;
function CaseInsensitivePos(const Pat, Text: string): Integer; overload;
function AnsiCaseInsensitivePos(const SubString, S: string): Integer;
procedure MakeASCIICharTable;
procedure Initialize;
function IsCharAlpha(ch: Char): Boolean;
function IsCharUpper(ch: Char): Boolean;
function IsCharLower(ch: Char): Boolean;
function IsCharAlphaNumeric(ch: Char): Boolean;
// Emulates the VB $Right function to obtain up to n of the
// rightmost characters in a string.
function RightString(const Value: string; NumChars: Integer): string;
function IsPas(const FileName: string): Boolean;
function IsInc(const FileName: string): Boolean;
function IsProgram(const FileName: string): Boolean;
{ Handles key shortcuts like Ctrl+Ins or Ctrl+Del to add or remove grid rows.
We add this here, so we can reuse it all over in the IDE. }
procedure CheckGridModifyKeyPresses(Sender: TObject; var KeyCode: word; var ShiftState: TShiftState; var Consumed: boolean);
implementation
uses
fpg_form
,fpg_memo
,fpg_grid
,fpg_main
,fpg_utils
,dbugintf
;
var
LocaleIdentifierChars: set of Char;
ASCIICharTable: array [#0..#255] of Byte;
const
EmptyString = '';
GxIdentChars = ['A'..'Z', 'a'..'z', '0'..'9', '_'];
GxIdentStartChars = ['A'..'Z', 'a'..'z', '0'..'9'];
GxAlphaChars = ['A'..'Z', 'a'..'z'];
GxUpperAlphaChars = ['A'..'Z'];
GxLowerAlphaChars = ['a'..'z'];
GxSentenceEndChars = ['.', '!', '?'];
SAllAlphaNumericChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
type
{ once it goes out of scope, it restores the mouse cursor }
TTempHourClassCursor = class(TInterfacedObject, IInterface)
private
FOldCursor: TMouseCursor;
FWidget: TfpgWidget;
public
constructor Create(var AWidget: TfpgWidget);
destructor Destroy; override;
end;
constructor TTempHourClassCursor.Create(var AWidget: TfpgWidget);
begin
inherited Create;
FOldCursor := AWidget.MouseCursor;
FWidget := AWidget;
AWidget.MouseCursor := mcHourGlass;
end;
destructor TTempHourClassCursor.Destroy;
begin
FWidget.MouseCursor := FOldCursor;
inherited Destroy;
end;
function TempHourGlassCursor(var AWidget: TfpgWidget): IInterface;
begin
Result := TTempHourClassCursor.Create(AWidget) as IInterface;
end;
function tiNumToken(const AValue, AToken : string): integer;
var
i, iCount : integer;
lsValue : string;
begin
Result := 0;
if AValue = '' then
Exit; //==>
iCount := 0;
lsValue := AValue;
i := pos(AToken, lsValue);
while i <> 0 do begin
delete(lsValue, i, length(AToken));
inc(iCount);
i := pos(AToken, lsValue);
end;
Result := iCount + 1;
end;
function tiToken(const AValue, AToken : string; const APos : integer): string;
var
i, iCount, iNumToken : integer;
lsValue : string;
begin
result := '';
iNumToken := tiNumToken(AValue, AToken);
if APos = 1 then
begin
if pos(AToken, AValue) = 0 then
result := AValue
else
result := copy(AValue, 1, pos(AToken, AValue)-1);
end
else if (iNumToken < APos-1) or (APos<1) then
begin
result := '';
end
else
begin
{ Remove leading blocks }
iCount := 1;
lsValue := AValue;
i := pos(AToken, lsValue);
while (i<>0) and (iCount<APos) do
begin
delete(lsValue, 1, i + length(AToken) - 1);
inc(iCount);
i := pos(AToken, lsValue);
end;
if (i=0) and (iCount=APos) then
result := lsValue
else if (i=0) and (iCount<>APos) then
result := ''
else
result := copy(lsValue, 1, i-1);
end;
end;
procedure ShowString(const AString: TfpgString; const AHeading: TfpgString);
var
lForm: TfpgForm;
lMemo: TfpgMemo;
begin
lForm := TfpgForm.Create(nil);
lMemo := TfpgMemo.Create(lForm);
try
lForm.WindowTitle := AHeading;
lForm.Width := 450;
lForm.Height := 250;
lForm.WindowPosition := wpOneThirdDown;
lForm.Name := 'FormShowStrings';
lMemo.Lines.Text := AString;
lMemo.FontDesc := '#Edit2';
lMemo.SetPosition(0, 0, lForm.Width, lForm.Height);
lMemo.Align := alClient;
lForm.ShowModal;
finally
lForm.free;
end;
end;
function ExtractUpperFileExt(const FileName: string): string;
begin
Result := UpperCase(fpgExtractFileExt(FileName));
end;
function CompressWhiteSpace(const Str: string): string;
var
i: Integer;
Len: Integer;
NextResultChar: Integer;
CheckChar: Char;
NextChar: Char;
begin
Len := Length(Str);
NextResultChar := 1;
SetLength(Result, Len);
for i := 1 to Len do
begin
CheckChar := Str[i];
{$RANGECHECKS OFF}
NextChar := Str[i + 1];
{$RANGECHECKS ON}
case CheckChar of
#9, #10, #13, #32:
begin
if (NextChar in [#0, #9, #10, #13, #32]) or (NextResultChar = 1) then
Continue
else
begin
Result[NextResultChar] := #32;
Inc(NextResultChar);
end;
end;
else
begin
Result[NextResultChar] := Str[i];
Inc(NextResultChar);
end;
end;
end;
if Len = 0 then
Exit;
SetLength(Result, NextResultChar - 1);
end;
function StrBeginsWith(const SubStr, Str: string; CaseSensitive: Boolean): Boolean;
begin
if CaseSensitive then
Result := Pos(SubStr, Str) = 1
else
Result := CaseInsensitivePos(SubStr, Str) = 1;
end;
function StrEndsWith(const SubStr, Str: string; CaseSensitive: Boolean): Boolean;
begin
if CaseSensitive then
Result := RightString(Str, Length(SubStr)) = SubStr
else
Result := SameText(RightString(Str, Length(SubStr)), SubStr);
end;
function StrContains(const SubStr, Str: string; CaseSensitive: Boolean): Boolean;
begin
if CaseSensitive then
Result := Pos(SubStr, Str) > 0
else
Result := CaseInsensitivePos(SubStr, Str) > 0;
end;
function CaseInsensitivePos(Pat, Text: PChar): Integer;
var
RunPat, RunText, PosPtr: PChar;
begin
Result := 0;
RunPat := Pat;
RunText := Text;
while RunText^ <> #0 do
begin
if (ASCIICharTable[RunPat^] = ASCIICharTable[RunText^]) then
begin
PosPtr := RunText;
while RunPat^ <> #0 do
begin
if ASCIICharTable[RunPat^] <> ASCIICharTable[RunText^] then
Break;
Inc(RunPat);
Inc(RunText);
end;
if RunPat^ = #0 then
begin
Result := PosPtr - Text + 1;
Break;
end;
end
else
Inc(RunText);
RunPat := Pat;
end;
end;
function CaseInsensitivePos(const Pat, Text: string): Integer; overload;
begin
Result := CaseInsensitivePos(PChar(Pat), PChar(Text));
end;
function AnsiCaseInsensitivePos(const SubString, S: string): Integer;
begin
Result := AnsiPos(AnsiUpperCase(SubString), AnsiUpperCase(S));
end;
procedure MakeASCIICharTable;
var
i: Integer;
begin
for i := 0 to 255 do
begin
If (I > 64) and (I < 91) then
ASCIICharTable[Char(I)] := i + 32
else
ASCIICharTable[Char(I)] := i;
end;
end;
procedure Initialize;
var
i: Char;
begin
for i := Low(Char) to High(Char) do
if IsCharAlphaNumeric(i) then
Include(LocaleIdentifierChars, i);
Include(LocaleIdentifierChars, '_');
MakeASCIICharTable;
end;
function IsCharAlpha(ch: Char): Boolean;
begin
Result := (ch in GxAlphaChars);
end;
function IsCharUpper(ch: Char): Boolean;
begin
Result := (ch in GxUpperAlphaChars);
end;
function IsCharLower(ch: Char): Boolean;
begin
Result := (ch in GxLowerAlphaChars);
end;
function IsCharAlphaNumeric(ch: Char): Boolean;
begin
Result := (ch in GxIdentStartChars);
end;
function RightString(const Value: string; NumChars: Integer): string;
begin
Result := Copy(Value, (Length(Value) - NumChars) + 1, NumChars);
end;
function IsPas(const FileName: string): Boolean;
var
FileExt: string;
begin
FileExt := ExtractUpperFileExt(FileName);
Result := (FileExt = '.PAS');
end;
function IsInc(const FileName: string): Boolean;
var
FileExt: string;
begin
FileExt := ExtractUpperFileExt(FileName);
Result := (FileExt = '.INC');
end;
function IsProgram(const FileName: string): Boolean;
var
FileExt: string;
begin
FileExt := ExtractUpperFileExt(FileName);
Result := (FileExt = '.LPR') or (FileExt = '.DPR');
end;
procedure CheckGridModifyKeyPresses(Sender: TObject; var KeyCode: word;
var ShiftState: TShiftState; var Consumed: boolean);
var
grd: TfpgStringGrid;
r: integer;
begin
grd := TfpgStringGrid(Sender);
if (KeyCode = keyInsert) and (ssCtrl in ShiftState) then
begin
grd.RowCount := grd.RowCount + 1;
Consumed := True;
Exit;
end
else if (KeyCode = keyDelete) and (ssCtrl in ShiftState) then
begin
if grd.RowCount = 0 then
Exit;
r := grd.FocusRow;
grd.DeleteRow(r);
if (grd.RowCount <> 0) and (grd.RowCount > r) then
grd.FocusRow := r // focus what was next row
else if (grd.RowCount <> 0) and (grd.RowCount <= r) then
grd.FocusRow := r-1; // focus what was previous row
Consumed := True;
Exit;
end;
end;
initialization
Initialize;
end.
|