PageRenderTime 50ms CodeModel.GetById 13ms app.highlight 33ms RepoModel.GetById 1ms app.codeStats 1ms

/components/synedit/syneditmiscprocs.pp

http://github.com/graemeg/lazarus
Pascal | 258 lines | 177 code | 40 blank | 41 comment | 28 complexity | 0343b4e9aa0f6f8d5ab7f3d4a3ac5b85 MD5 | raw file
  1{-------------------------------------------------------------------------------
  2The contents of this file are subject to the Mozilla Public License
  3Version 1.1 (the "License"); you may not use this file except in compliance
  4with the License. You may obtain a copy of the License at
  5http://www.mozilla.org/MPL/
  6
  7Software distributed under the License is distributed on an "AS IS" basis,
  8WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
  9the specific language governing rights and limitations under the License.
 10
 11The Original Code is: SynEditMiscProcs.pas, released 2000-04-07.
 12The Original Code is based on the mwSupportProcs.pas file from the
 13mwEdit component suite by Martin Waldenburg and other developers, the Initial
 14Author of this file is Michael Hieke.
 15All Rights Reserved.
 16
 17Contributors to the SynEdit and mwEdit projects are listed in the
 18Contributors.txt file.
 19
 20Alternatively, the contents of this file may be used under the terms of the
 21GNU General Public License Version 2 or later (the "GPL"), in which case
 22the provisions of the GPL are applicable instead of those above.
 23If you wish to allow use of your version of this file only under the terms
 24of the GPL and not to allow others to use your version of this file
 25under the MPL, indicate your decision by deleting the provisions above and
 26replace them with the notice and other provisions required by the GPL.
 27If you do not delete the provisions above, a recipient may use your version
 28of this file under either the MPL or the GPL.
 29
 30$Id$
 31
 32You may retrieve the latest version of this file at the SynEdit home page,
 33located at http://SynEdit.SourceForge.net
 34
 35Known Issues:
 36-------------------------------------------------------------------------------}
 37
 38unit SynEditMiscProcs;
 39
 40{$I synedit.inc}
 41
 42interface
 43
 44uses
 45  LCLIntf, LCLType, Classes, SynEditTypes, Graphics;
 46
 47type
 48  PIntArray = ^TIntArray;
 49  TIntArray = array[0..MaxListSize - 1] of integer;
 50
 51function MulDiv(Factor1,Factor2,Divisor:integer):integer;{$IFDEF HasInline}inline;{$ENDIF}
 52function Max(x, y: integer): integer;{$IFDEF HasInline}inline;{$ENDIF}
 53function Min(x, y: integer): integer;{$IFDEF HasInline}inline;{$ENDIF}
 54function MinMax(x, mi, ma: integer): integer;{$IFDEF HasInline}inline;{$ENDIF}
 55procedure SwapInt(var l, r: integer);{$IFDEF HasInline}inline;{$ENDIF}
 56function maxPoint(P1, P2: TPoint): TPoint;
 57function minPoint(P1, P2: TPoint): TPoint;
 58function eqPoint(P1, P2: TPoint): Boolean;
 59procedure SwapPoint(var P1, P2: TPoint);
 60
 61procedure InternalFillRect(dc: HDC; const rcPaint: TRect);
 62
 63// search for the first char of set AChars in Line, starting at index Start
 64function StrScanForCharInSet(const Line: string; Start: integer;
 65  AChars: TSynIdentChars): integer;
 66
 67function GetEOL(Line: PChar): PChar;
 68
 69function CompareCarets(const FirstCaret, SecondCaret: TPoint): integer;
 70
 71function fsNot (s : TFontStyles) : TFontStyles; inline;
 72function fsXor (s1,s2 : TFontStyles) : TFontStyles; inline;
 73
 74function CreateTabsAndSpaces(StartPos, SpaceLen, TabWidth: integer;
 75  UseTabs: boolean): string;
 76
 77procedure SynAssert(Condition: Boolean; Msg: String);
 78procedure SynAssert(Condition: Boolean; Msg: String; Args: Array of Const);
 79
 80function ToIdx(APos: Integer): Integer; inline;
 81function ToPos(AIdx: Integer): Integer; inline;
 82
 83implementation
 84
 85uses
 86  SysUtils;
 87
 88function ToIdx(APos: Integer): Integer; inline;
 89begin
 90  Result := APos - 1;
 91end;
 92
 93function ToPos(AIdx: Integer): Integer; inline;
 94begin
 95  Result := AIdx + 1;
 96end;
 97
 98{* fontstyle utilities *}
 99
100function fsNot (s : TFontStyles) : TFontStyles; inline;
101begin
102  Result := [low(TFontStyle)..High(TFontStyle)] - s;
103end;
104function fsXor (s1,s2 : TFontStyles) : TFontStyles; inline;
105begin
106  Result := s1 + s2 - (s1*s2);
107end;
108
109{***}
110
111function MulDiv(Factor1,Factor2,Divisor:integer):integer;
112begin
113  Result:=(int64(Factor1)*int64(Factor2)) div Divisor;
114end;
115
116function Max(x, y: integer): integer;
117begin
118  if x > y then Result := x else Result := y;
119end;
120
121function Min(x, y: integer): integer;
122begin
123  if x < y then Result := x else Result := y;
124end;
125
126function MinMax(x, mi, ma: integer): integer;
127begin
128  if (x < mi) then Result := mi
129    else if (x > ma) then Result := ma else Result := x;
130end;
131
132procedure SwapInt(var l, r: integer);
133var
134  tmp: integer;
135begin
136  tmp := r;
137  r := l;
138  l := tmp;
139end;
140
141function maxPoint(P1, P2: TPoint): TPoint;
142begin
143  Result := P1;
144  if (P2.y > P1.y) or ((P2.y = P1.y) and (P2.x > P1.x)) then
145    Result := P2;
146end;
147
148function minPoint(P1, P2: TPoint): TPoint;
149begin
150  Result := P1;
151  if (P2.y < P1.y) or ((P2.y = P1.y) and (P2.x < P1.x)) then
152    Result := P2;
153end;
154
155function eqPoint(P1, P2: TPoint): Boolean;
156begin
157  Result := (P2.y = P1.y) and (P2.x = P1.x);
158end;
159
160procedure SwapPoint(var P1, P2: TPoint);
161var
162  tmp : TPoint;
163begin
164  tmp := P1;
165  P1 := P2;
166  P2 := tmp;
167end;
168
169procedure InternalFillRect(dc: HDC; const rcPaint: TRect);
170begin
171  ExtTextOut(dc, 0, 0, ETO_OPAQUE, @rcPaint, nil, 0, nil);
172end;
173
174{***}
175
176function StrScanForCharInSet(const Line: string; Start: integer;
177  AChars: TSynIdentChars): integer;
178var
179  p: PChar;
180begin
181  if (Start > 0) and (Start <= Length(Line)) then
182  begin
183    p := PChar(@Line[Start]);
184    repeat
185      if p^ in AChars then
186      begin
187        Result := Start;
188        exit;
189      end;
190      Inc(p);
191      Inc(Start);
192    until p^ = #0;
193  end;
194  Result := 0;
195end;
196
197function GetEOL(Line: PChar): PChar;
198begin
199  Result := Line;
200  if Assigned(Result) then
201    while not (Result^ in [#0, #10, #13]) do
202      Inc(Result);
203end;
204
205function CompareCarets(const FirstCaret, SecondCaret: TPoint): integer;
206begin
207  if (FirstCaret.Y<SecondCaret.Y) then
208    Result:=1
209  else if (FirstCaret.Y>SecondCaret.Y) then
210    Result:=-1
211  else if (FirstCaret.X<SecondCaret.X) then
212    Result:=1
213  else if (FirstCaret.X>SecondCaret.X) then
214    Result:=-1
215  else
216    Result:=0;
217end;
218
219function CreateTabsAndSpaces(StartPos, SpaceLen, TabWidth: integer;
220  UseTabs: boolean): string;
221var
222  TabCount: Integer;
223  EndPos: Integer;
224  PosPlusOneTab: Integer;
225begin
226  Result:='';
227  if not UseTabs then begin
228    Result:=StringOfChar(' ',SpaceLen);
229    exit;
230  end;
231  TabCount:=0;
232  EndPos:=StartPos+SpaceLen;
233  while StartPos<EndPos do begin
234    PosPlusOneTab:=StartPos+TabWidth-((StartPos-1) mod TabWidth);
235    if PosPlusOneTab<=EndPos then begin
236      inc(TabCount);
237      StartPos:=PosPlusOneTab;
238    end else begin
239      Result:=StringOfChar(' ',EndPos-StartPos);
240      break;
241    end;
242  end;
243  if TabCount>0 then
244    Result:=StringOfChar(#9,TabCount)+Result;
245end;
246
247procedure SynAssert(Condition: Boolean; Msg: String);
248begin
249  if not Condition then raise Exception.Create(Msg);
250end;
251
252procedure SynAssert(Condition: Boolean; Msg: String; Args: array of const);
253begin
254  if not Condition then raise Exception.Create(Format(Msg, Args));
255end;
256
257end.
258