summaryrefslogtreecommitdiff
path: root/prototypes/miglayout/gui_mig_constraintparser.pas
blob: 06daeb58b3566be53b5f5eff639c0b41ec35d155 (plain)
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
unit gui_mig_constraintparser;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, gui_mig_lc;
  
type
  TConstraintParser = class(TObject)
  public
    function ParseLayoutConstraint(const s: string): TLC;
  end;

implementation

uses
  gfx_tokenlibrary;

{ TConstraintParser }

function TConstraintParser.ParseLayoutConstraint(const s: string): TLC;
var
  tokenizer: TTokens;
  parts: TStringList;
  part: string;
  i: integer;
  len: integer;
begin
  result := TLC.Create;
  
  if s = '' then
    Exit;
    
  tokenizer := TTokens.Create(s, ',', '"', '"', '\', tsSingleSeparatorBetweenTokens);
  parts := TStringList.Create;
  for i := 1 to tokenizer.TokenCount do
    parts.Add(trim(tokenizer.Token(i)));
  tokenizer.Free;
  
  // First check for "ltr" or "rtl" since that will affect the interpretation
  // of the other constraints.
  for i := 0 to parts.Count-1 do
  begin
    part := parts[i];
    if part = '' then
      continue;
      
    len := Length(part);
    if (len = 3) or (len = 11) then // optimization
    begin
      if (part = 'ltr') or (part = 'rtl') or (part = 'lefttoright') or (part = 'righttoleft') then
      begin
        result.LeftToRight_prop := part[1] = 'l';
        parts[i] := ''; // so we don't process it again
      end;
      if (part = 'ttb') or (part = 'btt') or (part = 'toptobottom') or (part = 'bottomtotop') then
      begin
        result.TopToBottom_prop := part[1] = 't';
        parts[i] := ''; // so we don't process it again
      end;
    end;
  end;
  
  for i := 0 to parts.Count-1 do
  begin
    part := parts[i];
    if part = '' then
      continue;

  end;
  
  parts.Free;
end;

end.