/* 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. */ /* Copyright (C) 2006 Dario Abatianni */ #include "multilinetextedit.h" #include #include #include MultilineTextEdit::MultilineTextEdit(TQWidget* parent,const char* name) : TQTextEdit(parent,name) { // make sure, our whitespace highlighting gets called whenever needed connect(this,TQT_SIGNAL(textChanged()),this,TQT_SLOT(drawWhitespaces())); connect(this,TQT_SIGNAL(cursorPositionChanged(int,int)),this,TQT_SLOT(cursorChanged(int,int))); } MultilineTextEdit::~MultilineTextEdit() { } void MultilineTextEdit::drawContents(TQPainter* p,int clipx,int clipy,int clipw,int cliph) { // redraw text TQTextEdit::drawContents(p,clipx,clipy,clipw,cliph); // overlay whitespace markup drawWhitespaces(); } void MultilineTextEdit::drawWhitespaces() { // prepare a rectangle to store the width of the whitespace found TQRect space; // get the painter for the text area TQPainter pa(viewport()); // get a sane color TQColor col=colorGroup().link(); // and a brush of the same color TQBrush fillBrush(col); // use it for line drawing pa.setPen(col); // and for filling pa.setBrush(fillBrush); // prepare the carriage return coordinates array TQPointArray cr(4); // and the tabulator arrow coordinate array TQPointArray tab(7); // whitespace expression TQRegExp regex("\\s"); // line buffer TQString line; int x,y,pos,paragraph; // start looking in every paragraph for(paragraph=0;paragraph paragraphs() || index < 0 || index > paragraphLength(para) ) return TQRect(); //invalid rectangle const TQFontMetrics& fm = fontMetrics(); const TQString& paratext = text(para); // Find index of the first character on the same line as parameter // 'index' using binary search. Very fast, even for long texts. int linestart = 0; int indexline = lineOfChar( para, index ); if ( indexline > 0 ) { int min = 0, max = index; int i = (min + max)/2; int iline = lineOfChar( para, i ); while ( iline != indexline-1 || lineOfChar( para, i+1 ) != indexline ) { Q_ASSERT( min != max && min != i && max != i ); if ( iline < indexline ) min = i; else max = i; i = (min + max)/2; iline = lineOfChar( para, i ); } linestart = i+1; } Q_ASSERT( linestart >= 0 ); int linewidth = fm.size(ExpandTabs, paratext.mid( linestart, index-linestart )).width(); int linewidth2 = fm.size(ExpandTabs, paratext.mid( linestart, index-linestart+1 )).width(); // FIXME as soon as it's possible to ask real margins from TQTextEdit: const int left_margin = 4; // const int top_margin = 4; TQPainter painter( viewport()); const TQRect& linerect = paragraphRect(para); return TQRect( contentsToViewport( TQPoint( left_margin + linerect.left() + linewidth , /*top_margin + */linerect.top() + indexline * fm.lineSpacing() + fm.leading())), TQSize( linewidth2-linewidth, fm.lineSpacing() )); } #include "multilinetextedit.moc"