summaryrefslogtreecommitdiffstats
path: root/kbarcode/dstextedit.cpp
blob: 88c5e0f3e11309d95d7d0d2457aa9ace19a39574 (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
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
/***************************************************************************
                         dstextedit.cpp  -  description
                             -------------------
    begin                : Sam Jun 04 2005
    copyright            : (C) 2005 by Dominik Seichter
    email                : domseichter@web.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "dstextedit.h"
#include "dstextedit.moc"
#include <tqregexp.h>

DSTextEdit::DSTextEdit( TQWidget* parent, const char* name )
    : KTextEdit( parent, name )
{
    connect( this, TQ_SIGNAL( textChanged() ), this, TQ_SLOT( fixParagraphs() ) );
}

void DSTextEdit::fixParagraphs()
{
    struct { 
        TQFont  font;
        TQColor color;
        int    alignment;
    } tFormattings;

    TQString t;
    int pos = 0;
    int count = 0;
    int i;
    int para, index;                          // needed to save the cursor position
    int paraFrom, indexFrom, paraTo, indexTo; // needed to save the selection
    TQValueList<int> chars;
    TQRegExp reg("<p[^>]*>");

    for( i = 0; i < paragraphs(); i++ )
        chars.append( paragraphLength( i ) );

    // disconnect us first as we change the text here
    disconnect( this, TQ_SIGNAL( textChanged() ), this, TQ_SLOT( fixParagraphs() ) );

    getCursorPosition( &para, &index );
    getSelection( &paraFrom, &indexFrom, &paraTo, &indexTo );

    if( !para && !index ) 
        setCursorPosition( 0, index+1 );

    t                      = this->text();
    tFormattings.font      = this->currentFont();
    tFormattings.color     = this->color();
    tFormattings.alignment = this->alignment();

    while( pos != -1 ) 
    {
        pos = reg.search( t, pos );
        if( pos != -1 )
        {
            if( count && count == para )
            {
                for( i = 0; i < count; i++ )
                    index += chars[i];
                ++index; // count the new <br> that is inserted later
            }

            ++count;

            if( count > 1 ) //&& pos != -1 ) 
                t = t.remove( pos, reg.matchedLength() );
            else
                pos += reg.matchedLength();
        }
    }

    pos = t.length();
    count = 0;

    while( pos != -1 ) 
    {
        pos = t.findRev( "</p>", pos );
        if( pos != -1 )
        {
            ++count;

            if( count > 1 ) //&& pos != -1 ) 
                t = t.replace( pos, 4, "<br />" );
            else
                pos -= 4;
        }
    }

    this->setText( t );
    this->setCursorPosition( 0, index );
    this->setCurrentFont( tFormattings.font );
    this->setColor( tFormattings.color );
    this->setAlignment( tFormattings.alignment );
    this->setSelection( paraFrom, indexFrom, paraTo, indexTo );


    connect( this, TQ_SIGNAL( textChanged() ), this, TQ_SLOT( fixParagraphs() ) );
}

/*
void DSTextEdit::moveCursor( CursorAction action, bool select )
{
    do { 
        TextEditBase::moveCursor( action, select );
    } while( cursorIsInToken() );
}

bool DSTextEdit::cursorIsInToken() 
{
    int para, index;
    int firstopen, firstclose;
    TQString data;

    getCursorPosition( &para, &index );

    data = text( para );

    tqDebug("data=" + data );
    --index;
    firstopen = data.findRev( "[", index );
    firstclose = data.findRev( "]", index );
    ++index;

    if( firstopen != -1 && firstopen > firstclose )
    {
        firstopen = data.find( "[", index );
        firstclose = data.find( "]", index );
        if( ( firstclose != -1 && firstopen != -1 && firstclose < firstopen ) ||
            ( firstclose != -1 && firstopen == -1 ) )
            return true;
    }

    return false;
}

*/