trichview.com

trichview.support




Re: Thank you, this is my c++builder code for auto indentation


Return to index


Author

Message

J.M. Ok

Posted: 11/01/2004 5:29:00


With your help i made this code and succeeded.

Thank you.


---------------------------------------------------------------

bool __fastcall ApplyAutoIndent(TRichViewEdit *RVE);


void __fastcall TForm1::rveKeyDown(TObject *Sender, WORD &Key, TShiftState

Shift)

{

    if(Key == VK_RETURN)

    {

        if(ApplyAutoIndent(rve))

            Key = 0;

    }

}


AnsiString ExtractLeadingBlanks(AnsiString s)

{

    int n = s.Length();

    int p = 0;

    for(int i = 1; i <= n; ++i)

    {

        if(s[i] == ' ' || s[i] == '\t')

            ++p;

        else

            break;

    }

    if(p > 0)

        return s.SubString(0, p);

    return "";

}


bool __fastcall ApplyAutoIndent(TRichViewEdit *RVE)

{

    int nItemCheck = RVE->GetItemStyle(RVE->CurItemNo);

    if(nItemCheck == rvsTable

    || nItemCheck == rvsBreak)

        return false;

       

    int nItemNoFrom = RVE->CurItemNo;

    int nItemNoTo = nItemNoFrom;

    int nCurTextStyleNo = RVE->CurTextStyleNo;

    int nCurParaStyleNo = RVE->CurParaStyleNo;


    while(nItemNoFrom > 0 && !RVE->IsFromNewLine(nItemNoFrom))

        --nItemNoFrom;


    AnsiString sBlank, sText;

    int nTextStyleNo;

    int nParaStyleNo;

    bool bBlankExist = false;

    int nItemNoAdded = 0;


    for(int i = nItemNoFrom; i <= nItemNoTo; ++i)

    {

        nTextStyleNo = RVE->GetItemStyle(i);

        nParaStyleNo = RVE->GetItemPara(i);

        sText = RVE->GetItemTextA(i);

        sBlank = ExtractLeadingBlanks(sText);

        if(sBlank != "")

        {

            bBlankExist = true;

            ++nItemNoAdded;

            RVE->CurTextStyleNo = nTextStyleNo;

            RVE->CurParaStyleNo = nParaStyleNo;

            if(i == nItemNoFrom)

                RVE->InsertText("\n" + sBlank);

            else

                RVE->InsertText(sBlank);

        }

        if(sText != sBlank)

            break;

    }

    RVE->CurTextStyleNo = nCurTextStyleNo;

    RVE->CurParaStyleNo = nCurParaStyleNo;

    if(!bBlankExist)

    {

        ++nItemNoAdded;

        RVE->InsertText("\n");

    }

    RVE->Format();


    int nItemNo = nItemNoTo + nItemNoAdded;

    int nOffset = RVE->GetOffsAfterItem(nItemNo);

    RVE->SetSelectionBounds(nItemNo, nOffset, nItemNo, nOffset);

    RVE->CurTextStyleNo = nCurTextStyleNo;

    RVE->CurParaStyleNo = nCurParaStyleNo;


    return true;

}





Powered by ABC Amber Outlook Express Converter