procedure TCustomSyntaxMemo.AdjustScrollBar;
var
  si: TScrollInfo;

  procedure ShowScroll(wBar: integer; bShow: Boolean);
  begin
    if FFlatScrollBars then
      FlatSB_ShowScrollBar(Handle, wBar, bShow)
    else
      ShowScrollBar(Handle, wBar, bShow);
  end;

  procedure Set_ScrollPos(nBar, nPos: Integer);
  begin
    if FFlatScrollBars then
      FlatSB_SetScrollPos(Handle, nBar, nPos, False)
    else
      SetScrollPos(Handle, nBar, nPos, False);
  end;

  procedure SetScrInfo(BarFlag: Integer);
  begin
    if FFlatScrollBars then
      FlatSB_SetScrollInfo(Handle, BarFlag, si, True)
    else
      SetScrollInfo(Handle, BarFlag, si, True);
  end;

  function DefineDelit: integer;
  begin
    Result := Ceil(si.nMax / MaxLinesResolution);
    if Result < 1 then Result := 1;
    if Result > 1 then
      begin
        si.nMax := Round(si.nMax / Result);
        si.nPage := Round(si.nPage / Result);
        if si.nPage < 1 then
          si.nPage := 1;
        si.nPos := Round(si.nPos / Result);
      end;
  end;

var
  VV: integer;
  b, bShowVert, bShowHorz: boolean;
begin
  if FUpdateCount <> 0 then Exit;
  if HandleAllocated then
   begin
    si.cbSize := sizeof(TScrollInfo);
    si.fMask := SIF_ALL or SIF_DISABLENOSCROLL; //AT
    si.nMin := 0;

    case FCurScroll of
      ssVertical:
        if not WordWrap then
        begin
          Set_ScrollPos(SB_VERT, Round(ScrollPosY / FScrollDelit.Y));
          Exit;
        end;
      ssHorizontal:
        begin
          Set_ScrollPos(SB_HORZ, Round(ScrollPosX / FScrollDelit.X));
          Exit;
        end;
    end;

    bShowVert:= FScrollBars in [ssBoth, ssVertical];
    bShowHorz:= FScrollBars in [ssBoth, ssHorizontal];

    b := bShowVert and FMultiLine and (Lines.Count > 0);
    if b then
    begin
      VV:= VisibleLines;
      if VV > 0 then //AT
        si.nPage := VV
      else
        si.nPage := 0;
      si.nMax := FLineCount;
      if (soScrollLastLine in FOptions) and (si.nPage > 0) then
        Inc(si.nMax, si.nPage - 1);
      si.nPos := ScrollPosY;
      FScrollDelit.Y := DefineDelit;
      {if soScrollLastLine in FOptions then }
        Dec(si.nMax);
    end
    else
    begin
      //disable scrollbar
      si.nMax := 1;
      si.nPos := 0;
      si.nPage := 2;
    end;

    SetScrInfo(SB_VERT);
    ShowScroll(SB_VERT, bShowVert);

    // Horizontal Scroll Bar
    b := bShowHorz;
    if b then
    begin
      si.nPage := VisibleCols;
      si.nPos := ScrollPosX;

      if FWordWrap then
      begin
        {
        b := (soBreakOnRightMargin in FOptions) and (RightMargin > 0) and
             ((ScrollPosX > 0) or
              (ClientWidth - FMargin.Right - FMargin.Left  < RightMargin * DefTextExt.cx));
        } //AT commented
        si.nMax := RightMargin;
      end
      else
        if soVariableHorzScrollBar in FOptions then
          begin
            //AT - orig method of calculating si.nMax is wrong
            si.nMax := Max(
              si.nPos + integer(si.nPage) - 1,
              VisibleLinesWidth div DefTextExt.cx + 1);
          end
          else
            si.nMax := 800;

       FScrollDelit.X := DefineDelit;
    end;

    SetScrInfo(SB_HORZ);
    ShowScroll(SB_HORZ, bShowHorz);

    //RecalcIMEWindow; //AT commented, too slow
   end;
end;
