File: UCEHighlighter.pas

package info (click to toggle)
mysql-query-browser 1.1.6-1sarge0
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 36,320 kB
  • ctags: 24,680
  • sloc: pascal: 203,479; xml: 136,561; ansic: 47,502; cpp: 28,926; sh: 12,433; objc: 4,823; java: 1,849; php: 1,485; python: 1,225; sql: 1,128; makefile: 872
file content (552 lines) | stat: -rw-r--r-- 16,376 bytes parent folder | download
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
unit UCEHighlighter;

//----------------------------------------------------------------------------------------------------------------------
//
// UniCodeEditor, a Unicode Source Code Editor for Delphi.
//
// UniCodeEditor is released under the MIT license:
// Copyright (c) 1999-2004 Mike Lischke (support@soft-gems.net, www.soft-gems.net).
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
// Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
// OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// You are asked to give the author(s) the due credit. This means that you acknowledge the work of the author(s)
// in the product documentation, about box, help or wherever a prominent place is.
//
//----------------------------------------------------------------------------------------------------------------------
//
// Description
//   This unit implements the base functionality for a syntax highlighter which is used
//   in TSyntaxEdit. Some of the code used here was taken form mwEdit.
//
// Changes:
//   Version 2.0, 2003-08-17, Mike Zinner
//     Repackaged for D7
//   Version 1.0, 1999-03-10, Mike Lischke
//     Initial Version
//
//----------------------------------------------------------------------------------------------------------------------

interface

uses
  Windows, SysUtils, Classes, Graphics, Registry, Controls;

type
  TWindowList = class(TList)
  public
    procedure AddInstance(Control: TWinControl);
    procedure RemoveInstance(Control: TWinControl);
    procedure Invalidate;
  end;

  THighlightData = record
    Foreground,
    Background: TColor;
    Style: TFontStyles;
  end;

  TTokenData = record
    Token: PChar;
    TokenType: Integer;
    Position,
    Length: Integer;
    Background,
    ForeGround: TColor;
    Style: TFontStyles;
  end;

  THighlightAttributes = class(TPersistent)
  private
    FHighlightData: THighlightData;
    FOnChange: TNotifyEvent;
    FName: string;
    procedure SetBackground(Value: TColor);
    procedure SetForeground(Value: TColor);
    procedure SetStyle(Value: TFontStyles);
  public
    procedure Assign(Source: TPersistent); override;
    function LoadFromBorlandRegistry(Path: string; Version: Integer): Boolean; virtual;
    function LoadFromRegistry(Reg: TRegistry): Boolean;
    function SaveToRegistry(Reg: TRegistry): Boolean;

    property Name: string read FName;
  published
    constructor Create(Name: string);

    property Background: TColor read FHighlightData.Background write SetBackground;
    property Foreground: TColor read FHighlightData.Foreground write SetForeground;
    property Style: TFontStyles read FHighlightData.Style write SetStyle;

    property OnChange: TNotifyEvent read FOnChange write FOnChange;
  end;

  TIdentChars = set of Char;

  THighlighterCapability = (
    hcUserSettings, // supports Enum/UseUserSettings
    hcRegistry      // supports LoadFrom/SaveToRegistry
  );

  THighlighterCapabilities = set of THighlighterCapability;

  TUCEHighlighter = class(TComponent)
  private
    FWindowList: TWindowList;
    FDefaultFilter: string;
  protected
    function GetIdentChars: TIdentChars; virtual;
    function GetLanguageName: string; virtual; abstract;

    function GetAttributeCount: Integer; virtual; abstract;
    function GetAttribute(idx: Integer): THighlightAttributes; virtual; abstract;
    function GetCapabilities: THighlighterCapabilities; virtual;
    function GetDefaultFilter: string; virtual;
    procedure SetDefaultFilter(Value: string); virtual;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;

    procedure AssignAttributesFrom(const Source: TUCEHighlighter); virtual;
    function EOL: Boolean; virtual; abstract;
    function GetRange: Integer; virtual; abstract;
    function GetToken: string; virtual; abstract;
    function GetTokenInfo: TTokenData; virtual; abstract;
    //function GetTokenPos: Integer; virtual; abstract;
    procedure Next; virtual; abstract;
    procedure SetLine(const Value: string); virtual; abstract;
    procedure SetRange(Value: Integer); virtual; abstract;
    procedure ResetRange; virtual; abstract;
    function UseUserSettings(Path: string): Boolean; virtual;
    procedure EnumUserSettings(Settings: TStrings); virtual;
    function LoadFromRegistry(RootKey: HKEY; Key: string): Boolean; virtual;
    function SaveToRegistry(RootKey: HKEY; Key: string): Boolean; virtual;

    property WindowList: TWindowList read FWindowList;
    property IdentChars: TIdentChars read GetIdentChars;
    property LanguageName: string read GetLanguageName;

    property AttributeCount: Integer read GetAttributeCount;
    property Attribute[Index: Integer]: THighlightAttributes read GetAttribute;
    property Capabilities: THighlighterCapabilities read GetCapabilities;
  published
    property DefaultFilter: string read GetDefaultFilter write SetDefaultFilter;
  end;

  THighlighterClass = class of TUCEHighlighter;

//----------------------------------------------------------------------------------------------------------------------

implementation

//----------------- TWindowList ----------------------------------------------------------------------------------------

procedure TWindowList.AddInstance(Control: TWinControl);

begin
  if IndexOf(Control) = -1 then
    Add(Control);
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TWindowList.RemoveInstance(Control: TWinControl);

var
  Index: Integer;

begin
  Index := IndexOf(Control);
  if Index <> -1 then
    Delete(Index);
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TWindowList.Invalidate;

var
  I: Integer;

begin
  for I := 0 to Count - 1 do
    TWinControl(Items[I]).Invalidate;
end;

//----------------- THighlightAttributes -----------------------------------------

procedure THighlightAttributes.Assign(Source: TPersistent);

begin
  if Source is THighlightAttributes then
  begin
    FHighlightData := (Source as THighlightAttributes).FHighlightData;
    FName := (Source as THighlightAttributes).FName;
  end
  else
    inherited Assign(Source);
end;

//----------------------------------------------------------------------------------------------------------------------

constructor THighlightAttributes.Create(Name: string);

begin
  inherited Create;
  FHighlightData.Background := clWindow;
  FHighlightData.Foreground := clWindowText;
  FName := Name;
end;

//----------------------------------------------------------------------------------------------------------------------

function THighlightAttributes.LoadFromBorlandRegistry(Path: string; Version: Integer): Boolean;

// Reads the attribute values from the registry assuming the structure used by Borland.
// Path is the path to the attribute sub key to read. It is interpreted relative to HKCU.
// Version determines the way the data must be read. It must be > 4 (for Delphi 4 and higher).

const
  StandardPalette: array[0..15] of TColor = (
    clBlack, clMaroon, clGreen, clOlive, clNavy, clPurple, clTeal, clDkGray,
    clLtGray, clRed, clLime, clYellow, clBlue, clFuchsia, clAqua, clWhite
  );

var
  Registry: TRegistry;
  UseOldStyle: Boolean;
  AttributeSuffix: string;

  //--------------- local functions --------------------------------------------

  function ReadColor(Name: string): TColor;

  var
    AttributeName: string;
    Index: Integer;
    Color: string;

  begin
    Result := clNone;
    AttributeName := Name + AttributeSuffix;
    if Registry.ValueExists(AttributeName) then
      if UseOldStyle then
      begin
        Index := Registry.ReadInteger(AttributeName);
        if Index in [0..15] then
          Result := StandardPalette[Index];
      end
      else
      begin
        Color := Registry.ReadString(AttributeName);
        Result := StringToColor(Color);
      end;
  end;

  //----------------------------------------------------------------------------

  function ReadBoolean(Name: string): Boolean;

  begin
    Result := False;
    if Registry.ValueExists(Name) then
      Result := StrToBool(Registry.ReadString(Name));
  end;

  //--------------- End local functions ----------------------------------------

var
  Color: TColor;
  Switch: Boolean;
  Style: TFontStyles;

begin
  UseOldStyle := Version < 7;
  if UseOldStyle then
    AttributeSuffix := ''
  else
    AttributeSuffix := ' New';

  Result := False;
  Registry := TRegistry.Create;
  try
    with Registry do
    begin
      RootKey := HKEY_CURRENT_USER;
      if OpenKeyReadOnly(Path) then
      begin
        try
          Switch := ReadBoolean('Default Foreground');
          if not Switch then
          begin
            Color := ReadColor('Foreground Color');
            if Color <> clNone then
              Foreground := Color;
          end;
          Switch := ReadBoolean('Default Background');
          if not Switch then
          begin
            Color := ReadColor('Background Color');
            if Color <> clNone then
              Background := Color;
          end;
          Style := [];
          Switch := ReadBoolean('Bold');
          if Switch then
            Style := Style + [fsBold];
          Switch := ReadBoolean('Italic');
          if Switch then
            Style := Style + [fsItalic];
          Switch := ReadBoolean('Underline');
          if Switch then
            Style := Style + [fsUnderline];

          Self.Style := Style;
        
          Result := True;
        finally
          CloseKey;
        end;
      end;
    end;
  finally
    Registry.Free;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure THighlightAttributes.SetBackground(Value: TColor);

begin
  with FHighlightData do
    if Background <> Value then
    begin
      Background := Value;
      if Assigned(FOnChange) then
        FOnChange(Self);
    end;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure THighlightAttributes.SetForeground(Value: TColor);

begin
  with FHighlightData do
    if Foreground <> Value then
    begin
      Foreground := Value;
      if Assigned(FOnChange) then
        FOnChange(Self);
    end;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure THighlightAttributes.SetStyle(Value: TFontStyles);

begin
  with FHighlightData do
    if Style <> Value then
    begin
      Style := Value;
      if Assigned(FOnChange) then
        FOnChange(Self);
    end;
end;

//----------------------------------------------------------------------------------------------------------------------

function THighlightAttributes.LoadFromRegistry(Reg: TRegistry): Boolean;

var
  Key: string;

begin
  Key := Reg.CurrentPath;
  if Reg.OpenKeyReadOnly(Name) then
  begin
    if Reg.ValueExists('Attributes') then
      Reg.ReadBinaryData('Attributes', FHighlightData, SizeOf(FHighlightData));
    Reg.OpenKeyReadOnly('\' + Key);
    Result := True;
  end
  else
    Result := False;
end;

//----------------------------------------------------------------------------------------------------------------------

function THighlightAttributes.SaveToRegistry(Reg: TRegistry): Boolean;

var
  Key: string;

begin
  Key := Reg.CurrentPath;
  if Reg.OpenKey(Name, True) then
  begin
    Reg.WriteBinaryData('Attributes', FHighlightData, SizeOf(FHighlightData));
    Reg.OpenKey('\' + Key, False);
    Result := True;
  end
  else
    Result := False;
end;

//----------------- TUCEHighlighter -----------------------------------------------------------------------------------

constructor TUCEHighlighter.Create(AOwner: TComponent);

begin
  inherited Create(AOwner);
  FWindowList := TWindowList.Create;
  FDefaultFilter := '';
end;

//----------------------------------------------------------------------------------------------------------------------

destructor TUCEHighlighter.Destroy;

begin
  FWindowList.Free;
  inherited Destroy;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TUCEHighlighter.EnumUserSettings(Settings: TStrings);

begin
  Settings.Clear;
end;

//----------------------------------------------------------------------------------------------------------------------

function TUCEHighlighter.UseUserSettings(Path: string): Boolean;

begin
  Result := False;
end;

//----------------------------------------------------------------------------------------------------------------------

function TUCEHighlighter.GetIdentChars: TIdentChars;

begin
  Result := [#33..#255];
end;

//----------------------------------------------------------------------------------------------------------------------

function TUCEHighlighter.LoadFromRegistry(RootKey: HKEY; Key: string): Boolean;

var
  Reg: TRegistry;
  I: Integer;

begin
  Reg := TRegistry.Create;
  try
    Reg.RootKey := RootKey;
    if Reg.OpenKeyReadOnly(Key) then
    begin
      Result := True;
      for I := 0 to AttributeCount - 1 do
        Result := Result and Attribute[i].LoadFromRegistry(Reg);
    end
    else
      Result := False;
  finally
    Reg.Free;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

function TUCEHighlighter.SaveToRegistry(RootKey: HKEY; Key: string): Boolean;

var
  Reg: TRegistry;
  I: Integer;

begin
  Reg := TRegistry.Create;
  try
    Reg.RootKey := RootKey;
    if Reg.OpenKey(Key, True) then
    begin
      Result := True;
      for I := 0 to AttributeCount - 1 do
        Result := Result and Attribute[i].SaveToRegistry(Reg);
    end
    else
      Result := False;
  finally
    Reg.Free;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

function TUCEHighlighter.GetCapabilities: THighlighterCapabilities;

begin
  Result := [hcRegistry];
end;

//----------------------------------------------------------------------------------------------------------------------

function TUCEHighlighter.GetDefaultFilter: string;

begin
  Result := FDefaultFilter;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TUCEHighlighter.SetDefaultFilter(Value: string);

begin
  if FDefaultFilter <> Value then
    FDefaultFilter := Value;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TUCEHighlighter.AssignAttributesFrom(const Source: TUCEHighlighter);

// copies all attribute settings from Source to the local list, only attributes whose names
// do match are copied

var
  I, J: Integer;
  AttrName: string;

begin
  for I := 0 to Source.AttributeCount - 1 do
  begin
    AttrName := Source.Attribute[I].Name;
    for J := 0 to AttributeCount - 1 do
      if CompareText(AttrName, Attribute[J].Name) = 0 then
        Attribute[J].Assign(Source.Attribute[I]);
  end;
  FWindowList.Invalidate;
end;

//----------------------------------------------------------------------------------------------------------------------

end.